From patchwork Mon Nov 16 17:35:10 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11910003 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 390BC14C0 for ; Mon, 16 Nov 2020 17:39:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 193A122265 for ; Mon, 16 Nov 2020 17:39:50 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="SuWA5vfc" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733157AbgKPRjL (ORCPT ); Mon, 16 Nov 2020 12:39:11 -0500 Received: from aserp2120.oracle.com ([141.146.126.78]:56424 "EHLO aserp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1733281AbgKPRh3 (ORCPT ); Mon, 16 Nov 2020 12:37:29 -0500 Received: from pps.filterd (aserp2120.oracle.com [127.0.0.1]) by aserp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0AGHUACP096873; Mon, 16 Nov 2020 17:37:27 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2020-01-29; bh=ka6V8pS0PHah7XuzCYv1WdctuFPvIb2XhDFpterR074=; b=SuWA5vfc6gRQjDzzGEYTbDy2k1No+/asP6r9WGF+hIKBnKg1kX6SfeN9dtx3H+iKZMit wD7wGWo5YP8ewHVlvNcWIQW8o/uGCZEaqbPS2/MsBoEqCZSbT3OpxAwfBkC8NTGbJBlS GxXj2FJWZZrrl2lHDrJ3/4SqffJ6iIemCpnA5pZVJMEsaxhMXkO2pIwFi0orXyi1rgFm EtTTuzvBOzPkzdxGXf6ii8615s/UOnxFiQMTOOnUj7N9kd9S+WeDbXI199HtzNWBQR9X v7jCvZqmTr+WXA6kSZEgUzxjDzWRbUw9X8RkXRCIt4OS+jWHP9d+9S9WVxqJP4wKK+Cm XQ== Received: from userp3020.oracle.com (userp3020.oracle.com [156.151.31.79]) by aserp2120.oracle.com with ESMTP id 34t76kp9jg-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 16 Nov 2020 17:37:27 +0000 Received: from pps.filterd (userp3020.oracle.com [127.0.0.1]) by userp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0AGHVQ61027310; Mon, 16 Nov 2020 17:35:26 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by userp3020.oracle.com with ESMTP id 34ts0pqsc9-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 16 Nov 2020 17:35:26 +0000 Received: from abhmp0017.oracle.com (abhmp0017.oracle.com [141.146.116.23]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 0AGHZQ5A011131; Mon, 16 Nov 2020 17:35:26 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 16 Nov 2020 09:35:11 -0800 Subject: [PATCH 1/2] xfs_db: add a directory path lookup command From: "Darrick J. Wong" To: sandeen@sandeen.net, darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Mon, 16 Nov 2020 09:35:10 -0800 Message-ID: <160554811025.2672934.1436964231757739287.stgit@magnolia> In-Reply-To: <160554810400.2672934.11229550425583973318.stgit@magnolia> References: <160554810400.2672934.11229550425583973318.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9807 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 adultscore=0 bulkscore=0 suspectscore=2 spamscore=0 malwarescore=0 phishscore=0 mlxscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011160102 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9807 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 phishscore=0 adultscore=0 priorityscore=1501 bulkscore=0 clxscore=1015 mlxlogscore=999 malwarescore=0 mlxscore=0 spamscore=0 lowpriorityscore=0 impostorscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011160102 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Add a command to xfs_db so that we can navigate to inodes by path. Signed-off-by: Darrick J. Wong --- db/Makefile | 2 db/command.c | 1 db/command.h | 1 db/namei.c | 223 +++++++++++++++++++++++++++++++++++++++++++++++++++++ man/man8/xfs_db.8 | 4 + 5 files changed, 230 insertions(+), 1 deletion(-) create mode 100644 db/namei.c diff --git a/db/Makefile b/db/Makefile index 9bd9bf514f5d..67908a2c3c98 100644 --- a/db/Makefile +++ b/db/Makefile @@ -14,7 +14,7 @@ HFILES = addr.h agf.h agfl.h agi.h attr.h attrshort.h bit.h block.h bmap.h \ io.h logformat.h malloc.h metadump.h output.h print.h quit.h sb.h \ sig.h strvec.h text.h type.h write.h attrset.h symlink.h fsmap.h \ fuzz.h -CFILES = $(HFILES:.h=.c) btdump.c btheight.c convert.c info.c +CFILES = $(HFILES:.h=.c) btdump.c btheight.c convert.c info.c namei.c LSRCFILES = xfs_admin.sh xfs_ncheck.sh xfs_metadump.sh LLDLIBS = $(LIBXFS) $(LIBXLOG) $(LIBFROG) $(LIBUUID) $(LIBRT) $(LIBPTHREAD) diff --git a/db/command.c b/db/command.c index 0fb44efaec59..053097742b12 100644 --- a/db/command.c +++ b/db/command.c @@ -131,6 +131,7 @@ init_commands(void) logformat_init(); io_init(); metadump_init(); + namei_init(); output_init(); print_init(); quit_init(); diff --git a/db/command.h b/db/command.h index b8499de0be17..bf130e63c85c 100644 --- a/db/command.h +++ b/db/command.h @@ -32,3 +32,4 @@ extern void convert_init(void); extern void btdump_init(void); extern void info_init(void); extern void btheight_init(void); +extern void namei_init(void); diff --git a/db/namei.c b/db/namei.c new file mode 100644 index 000000000000..13ae7f7d116b --- /dev/null +++ b/db/namei.c @@ -0,0 +1,223 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2020 Oracle. All Rights Reserved. + * Author: Darrick J. Wong + */ +#include "libxfs.h" +#include "command.h" +#include "output.h" +#include "init.h" +#include "io.h" +#include "type.h" +#include "input.h" +#include "faddr.h" +#include "fprint.h" +#include "field.h" +#include "inode.h" + +/* Path lookup */ + +/* Key for looking up metadata inodes. */ +struct dirpath { + /* Array of string pointers. */ + char **path; + + /* Number of strings in path. */ + unsigned int depth; +}; + +static void +path_free( + struct dirpath *dirpath) +{ + unsigned int i; + + for (i = 0; i < dirpath->depth; i++) + free(dirpath->path[i]); + free(dirpath->path); + free(dirpath); +} + +/* Chop a freeform string path into a structured path. */ +static struct dirpath * +path_parse( + const char *path) +{ + struct dirpath *dirpath; + const char *p = path; + const char *endp = path + strlen(path); + + dirpath = calloc(sizeof(*dirpath), 1); + if (!dirpath) + return NULL; + + while (p < endp) { + char **new_path; + const char *next_slash; + + next_slash = strchr(p, '/'); + if (next_slash == p) { + p++; + continue; + } + if (!next_slash) + next_slash = endp; + + new_path = realloc(dirpath->path, + (dirpath->depth + 1) * sizeof(char *)); + if (!new_path) { + path_free(dirpath); + return NULL; + } + + dirpath->path = new_path; + dirpath->path[dirpath->depth] = strndup(p, next_slash - p); + dirpath->depth++; + + p = next_slash + 1; + } + + return dirpath; +} + +/* Given a directory and a structured path, walk the path and set the cursor. */ +static int +path_navigate( + struct xfs_mount *mp, + xfs_ino_t rootino, + struct dirpath *dirpath) +{ + struct xfs_inode *dp; + xfs_ino_t ino = rootino; + unsigned int i; + int error; + + error = -libxfs_iget(mp, NULL, ino, 0, &dp); + if (error) + return error; + + for (i = 0; i < dirpath->depth; i++) { + struct xfs_name xname = { + .name = dirpath->path[i], + .len = strlen(dirpath->path[i]), + }; + + if (!S_ISDIR(VFS_I(dp)->i_mode)) { + error = ENOTDIR; + goto rele; + } + + error = -libxfs_dir_lookup(NULL, dp, &xname, &ino, NULL); + if (error) + goto rele; + if (!xfs_verify_ino(mp, ino)) { + error = EFSCORRUPTED; + goto rele; + } + + libxfs_irele(dp); + dp = NULL; + + error = -libxfs_iget(mp, NULL, ino, 0, &dp); + switch (error) { + case EFSCORRUPTED: + case EFSBADCRC: + case 0: + break; + default: + return error; + } + } + + set_cur_inode(ino); +rele: + if (dp) + libxfs_irele(dp); + return error; +} + +/* Walk a directory path to an inode and set the io cursor to that inode. */ +static int +path_walk( + char *path) +{ + struct dirpath *dirpath; + char *p = path; + xfs_ino_t rootino = mp->m_sb.sb_rootino; + int error = 0; + + if (*p == '/') { + /* Absolute path, start from the root inode. */ + p++; + } else { + /* Relative path, start from current dir. */ + if (iocur_top->typ != &typtab[TYP_INODE] || + !S_ISDIR(iocur_top->mode)) + return ENOTDIR; + + rootino = iocur_top->ino; + } + + dirpath = path_parse(p); + if (!dirpath) + return ENOMEM; + + error = path_navigate(mp, rootino, dirpath); + if (error) + return error; + + path_free(dirpath); + return 0; +} + +static void +path_help(void) +{ + dbprintf(_( +"\n" +" Navigate to an inode via directory path.\n" + )); +} + +static int +path_f( + int argc, + char **argv) +{ + int c; + int error; + + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + path_help(); + return 0; + } + } + + error = path_walk(argv[optind]); + if (error) { + dbprintf("%s: %s\n", argv[optind], strerror(error)); + exitcode = 1; + } + + return 0; +} + +static struct cmdinfo path_cmd = { + .name = "path", + .altname = NULL, + .cfunc = path_f, + .argmin = 1, + .argmax = 1, + .canpush = 0, + .args = "", + .help = path_help, +}; + +void +namei_init(void) +{ + path_cmd.oneline = _("navigate to an inode by path"); + add_command(&path_cmd); +} diff --git a/man/man8/xfs_db.8 b/man/man8/xfs_db.8 index 7f73d458cf76..d67e108a706a 100644 --- a/man/man8/xfs_db.8 +++ b/man/man8/xfs_db.8 @@ -831,6 +831,10 @@ See the .B print command. .TP +.BI "path " dir_path +Walk the directory tree to an inode using the supplied path. +Absolute and relative paths are supported. +.TP .B pop Pop location from the stack. .TP From patchwork Mon Nov 16 17:35:16 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Darrick J. Wong" X-Patchwork-Id: 11909885 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 1571415E6 for ; Mon, 16 Nov 2020 17:35:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id D86FC2225E for ; Mon, 16 Nov 2020 17:35:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=oracle.com header.i=@oracle.com header.b="d14bdfMf" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731420AbgKPRfY (ORCPT ); Mon, 16 Nov 2020 12:35:24 -0500 Received: from userp2120.oracle.com ([156.151.31.85]:40834 "EHLO userp2120.oracle.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1731203AbgKPRfX (ORCPT ); Mon, 16 Nov 2020 12:35:23 -0500 Received: from pps.filterd (userp2120.oracle.com [127.0.0.1]) by userp2120.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0AGHTfrj129786; Mon, 16 Nov 2020 17:35:20 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=subject : from : to : cc : date : message-id : in-reply-to : references : mime-version : content-type : content-transfer-encoding; s=corp-2020-01-29; bh=bYOtViICAG3UUJihsitN0RWKE0PsU1eGJddounKeR5U=; b=d14bdfMfv+GEuHV4JbJBOna58xKlnYYfVzsmxho40CLyA/S8mEe5QJbf+ailNACqgiwB 84oICpHSTjj8SCadSbEM8nQ5xr/Sc/t6vOFVLf7AFcjDm9ub34YalJeJD389Qtdn4nCA ommwIJNjfFry051Glpzojjdkhtac0pMn06mYmNKLNMu3+oyXdYNzdK1ypL29mzNYPLoT 7nucL8UVKRpa/suJUTixB0LTPtHRGY5stUJpCgs9KL+S2gpWLlsz+zWl0VrTFa8GV375 8A5JrLIQAsfK+nJGebK/LWJbHXYj0IHR+eJPir2OdQo9JWp1BSl/7wVHy+R2R7bVvcVf XQ== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by userp2120.oracle.com with ESMTP id 34t7vmx7pt-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 16 Nov 2020 17:35:20 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.0.42/8.16.0.42) with SMTP id 0AGHVDhG134278; Mon, 16 Nov 2020 17:35:19 GMT Received: from userv0121.oracle.com (userv0121.oracle.com [156.151.31.72]) by aserp3020.oracle.com with ESMTP id 34umcx33f2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 16 Nov 2020 17:35:19 +0000 Received: from abhmp0019.oracle.com (abhmp0019.oracle.com [141.146.116.25]) by userv0121.oracle.com (8.14.4/8.13.8) with ESMTP id 0AGHZH5A010992; Mon, 16 Nov 2020 17:35:18 GMT Received: from localhost (/67.169.218.210) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Mon, 16 Nov 2020 09:35:17 -0800 Subject: [PATCH 2/2] xfs_db: add an ls command From: "Darrick J. Wong" To: sandeen@sandeen.net, darrick.wong@oracle.com Cc: linux-xfs@vger.kernel.org Date: Mon, 16 Nov 2020 09:35:16 -0800 Message-ID: <160554811654.2672934.4045878675423463526.stgit@magnolia> In-Reply-To: <160554810400.2672934.11229550425583973318.stgit@magnolia> References: <160554810400.2672934.11229550425583973318.stgit@magnolia> User-Agent: StGit/0.19 MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9807 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 suspectscore=2 mlxscore=0 phishscore=0 spamscore=0 bulkscore=0 mlxlogscore=999 malwarescore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011160102 X-Proofpoint-Virus-Version: vendor=nai engine=6000 definitions=9807 signatures=668682 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 suspectscore=2 malwarescore=0 bulkscore=0 impostorscore=0 lowpriorityscore=0 spamscore=0 adultscore=0 mlxscore=0 priorityscore=1501 phishscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2011160102 Precedence: bulk List-ID: X-Mailing-List: linux-xfs@vger.kernel.org From: Darrick J. Wong Add to xfs_db the ability to list a directory. Signed-off-by: Darrick J. Wong --- db/namei.c | 389 ++++++++++++++++++++++++++++++++++++++++++++++ libxfs/libxfs_api_defs.h | 1 man/man8/xfs_db.8 | 16 ++ 3 files changed, 406 insertions(+) diff --git a/db/namei.c b/db/namei.c index 13ae7f7d116b..4b467ff8f1f5 100644 --- a/db/namei.c +++ b/db/namei.c @@ -215,9 +215,398 @@ static struct cmdinfo path_cmd = { .help = path_help, }; +/* List a directory's entries. */ + +static const char *filetype_strings[XFS_DIR3_FT_MAX] = { + [XFS_DIR3_FT_UNKNOWN] = "unknown", + [XFS_DIR3_FT_REG_FILE] = "regular", + [XFS_DIR3_FT_DIR] = "directory", + [XFS_DIR3_FT_CHRDEV] = "chardev", + [XFS_DIR3_FT_BLKDEV] = "blkdev", + [XFS_DIR3_FT_FIFO] = "fifo", + [XFS_DIR3_FT_SOCK] = "socket", + [XFS_DIR3_FT_SYMLINK] = "symlink", + [XFS_DIR3_FT_WHT] = "whiteout", +}; + +static const char * +get_dstr( + struct xfs_mount *mp, + uint8_t filetype) +{ + if (!xfs_sb_version_hasftype(&mp->m_sb)) + return filetype_strings[XFS_DIR3_FT_UNKNOWN]; + + if (filetype >= XFS_DIR3_FT_MAX) + return filetype_strings[XFS_DIR3_FT_UNKNOWN]; + + return filetype_strings[filetype]; +} + +static void +dir_emit( + struct xfs_mount *mp, + xfs_dir2_dataptr_t off, + char *name, + ssize_t namelen, + xfs_ino_t ino, + uint8_t dtype) +{ + char *display_name; + struct xfs_name xname = { .name = name }; + const char *dstr = get_dstr(mp, dtype); + xfs_dahash_t hash; + bool good; + + if (namelen < 0) { + /* Negative length means that name is null-terminated. */ + display_name = name; + xname.len = strlen(name); + good = true; + } else { + /* + * Otherwise, name came from a directory entry, so we have to + * copy the string to a buffer so that we can add the null + * terminator. + */ + display_name = malloc(namelen + 1); + memcpy(display_name, name, namelen); + display_name[namelen] = 0; + xname.len = namelen; + good = libxfs_dir2_namecheck(name, namelen); + } + hash = libxfs_dir2_hashname(mp, &xname); + + dbprintf("%-10u %-18llu %-14s 0x%08llx %3d %s %s\n", off & 0xFFFFFFFF, + ino, dstr, hash, xname.len, + display_name, good ? _("(good)") : _("(corrupt)")); + + if (display_name != name) + free(display_name); +} + +static int +list_sfdir( + struct xfs_da_args *args) +{ + struct xfs_inode *dp = args->dp; + struct xfs_mount *mp = dp->i_mount; + struct xfs_da_geometry *geo = args->geo; + struct xfs_dir2_sf_entry *sfep; + struct xfs_dir2_sf_hdr *sfp; + xfs_ino_t ino; + xfs_dir2_dataptr_t off; + unsigned int i; + uint8_t filetype; + + sfp = (struct xfs_dir2_sf_hdr *)dp->i_df.if_u1.if_data; + + /* . and .. entries */ + off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, + geo->data_entry_offset); + dir_emit(args->dp->i_mount, off, ".", -1, dp->i_ino, XFS_DIR3_FT_DIR); + + ino = libxfs_dir2_sf_get_parent_ino(sfp); + off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, + geo->data_entry_offset + + libxfs_dir2_data_entsize(mp, sizeof(".") - 1)); + dir_emit(args->dp->i_mount, off, "..", -1, ino, XFS_DIR3_FT_DIR); + + /* Walk everything else. */ + sfep = xfs_dir2_sf_firstentry(sfp); + for (i = 0; i < sfp->count; i++) { + ino = libxfs_dir2_sf_get_ino(mp, sfp, sfep); + filetype = libxfs_dir2_sf_get_ftype(mp, sfep); + off = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, + xfs_dir2_sf_get_offset(sfep)); + + dir_emit(args->dp->i_mount, off, (char *)sfep->name, + sfep->namelen, ino, filetype); + sfep = libxfs_dir2_sf_nextentry(mp, sfp, sfep); + } + + return 0; +} + +/* List entries in block format directory. */ +static int +list_blockdir( + struct xfs_da_args *args) +{ + struct xfs_inode *dp = args->dp; + struct xfs_mount *mp = dp->i_mount; + struct xfs_buf *bp; + struct xfs_da_geometry *geo = mp->m_dir_geo; + xfs_dir2_dataptr_t diroff; + unsigned int offset; + unsigned int end; + int error; + + error = xfs_dir3_block_read(NULL, dp, &bp); + if (error) + return error; + + end = xfs_dir3_data_end_offset(geo, bp->b_addr); + for (offset = geo->data_entry_offset; offset < end;) { + struct xfs_dir2_data_unused *dup = bp->b_addr + offset; + struct xfs_dir2_data_entry *dep = bp->b_addr + offset; + uint8_t filetype; + + if (be16_to_cpu(dup->freetag) == XFS_DIR2_DATA_FREE_TAG) { + /* Unused entry */ + offset += be16_to_cpu(dup->length); + continue; + } + + /* Real entry */ + diroff = xfs_dir2_db_off_to_dataptr(geo, geo->datablk, offset); + offset += libxfs_dir2_data_entsize(mp, dep->namelen); + filetype = libxfs_dir2_data_get_ftype(dp->i_mount, dep); + dir_emit(mp, diroff, (char *)dep->name, dep->namelen, + be64_to_cpu(dep->inumber), filetype); + } + + libxfs_trans_brelse(args->trans, bp); + return error; +} + +/* List entries in leaf format directory. */ +static int +list_leafdir( + struct xfs_da_args *args) +{ + struct xfs_bmbt_irec map; + struct xfs_iext_cursor icur; + struct xfs_inode *dp = args->dp; + struct xfs_mount *mp = dp->i_mount; + struct xfs_buf *bp = NULL; + struct xfs_ifork *ifp = XFS_IFORK_PTR(dp, XFS_DATA_FORK); + struct xfs_da_geometry *geo = mp->m_dir_geo; + xfs_dir2_off_t dirboff; + xfs_dablk_t dabno = 0; + int error = 0; + + /* Read extent map. */ + if (!(ifp->if_flags & XFS_IFEXTENTS)) { + error = -libxfs_iread_extents(NULL, dp, XFS_DATA_FORK); + if (error) + return error; + } + + while (dabno < geo->leafblk) { + unsigned int offset; + unsigned int length; + + /* Find mapping for leaf block. */ + if (!xfs_iext_lookup_extent(dp, ifp, dabno, &icur, &map)) + break; + if (map.br_startoff >= geo->leafblk) + break; + libxfs_trim_extent(&map, dabno, geo->leafblk - dabno); + + /* Read the directory block of that first mapping. */ + error = xfs_dir3_data_read(NULL, dp, map.br_startoff, 0, &bp); + if (error) + break; + + dirboff = xfs_dir2_da_to_byte(geo, map.br_startoff); + for (offset = geo->data_entry_offset; offset < geo->blksize;) { + struct xfs_dir2_data_entry *dep; + struct xfs_dir2_data_unused *dup; + uint8_t filetype; + + dup = bp->b_addr + offset; + dep = bp->b_addr + offset; + + if (be16_to_cpu(dup->freetag) == + XFS_DIR2_DATA_FREE_TAG) { + /* Skip unused entry */ + length = be16_to_cpu(dup->length); + offset += length; + continue; + } + + offset += libxfs_dir2_data_entsize(mp, dep->namelen); + filetype = libxfs_dir2_data_get_ftype(mp, dep); + + dir_emit(mp, xfs_dir2_byte_to_dataptr(dirboff + offset), + (char *)dep->name, dep->namelen, + be64_to_cpu(dep->inumber), filetype); + } + + dabno += XFS_DADDR_TO_FSB(mp, bp->b_length); + libxfs_buf_relse(bp); + bp = NULL; + } + + if (bp) + libxfs_buf_relse(bp); + + return error; +} + +/* Read the directory, display contents. */ +int +listdir( + struct xfs_inode *dp) +{ + struct xfs_da_args args = { + .dp = dp, + .geo = dp->i_mount->m_dir_geo, + }; + int error; + int isblock; + + if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) + return list_sfdir(&args); + + error = -libxfs_dir2_isblock(&args, &isblock); + if (error) + return error; + + if (isblock) + return list_blockdir(&args); + return list_leafdir(&args); +} + +/* List the inode number of the currently selected inode. */ +static int +inum_cur(void) +{ + if (iocur_top->typ != &typtab[TYP_INODE]) + return ENOENT; + + dbprintf("%llu\n", iocur_top->ino); + return 0; +} + +/* If the io cursor points to a directory, list its contents. */ +static int +ls_cur( + char *tag) +{ + struct xfs_inode *dp; + int error = 0; + + if (iocur_top->typ != &typtab[TYP_INODE] || + !S_ISDIR(iocur_top->mode)) + return ENOTDIR; + + error = -libxfs_iget(mp, NULL, iocur_top->ino, 0, &dp); + if (error) + return error; + + if (!S_ISDIR(VFS_I(dp)->i_mode)) { + error = ENOTDIR; + goto rele; + } + + /* List the contents of a directory. */ + if (tag) + dbprintf(_("%s:\n"), tag); + + error = listdir(dp); + if (error) + goto rele; + +rele: + libxfs_irele(dp); + return error; +} + +static void +ls_help(void) +{ + dbprintf(_( +"\n" +" List the contents of the currently selected directory inode.\n" +"\n" +" Options:\n" +" -i -- Resolve the given paths to their corresponding inode numbers.\n" +" If no paths are given, display the current inode number.\n" +"\n" +" Directory contents will be listed in the format:\n" +" dir_cookie inode_number type hash name_length name\n" + )); +} + +static int +ls_f( + int argc, + char **argv) +{ + bool inum_only = false; + int c; + int error = 0; + + while ((c = getopt(argc, argv, "i")) != -1) { + switch (c) { + case 'i': + inum_only = true; + break; + default: + ls_help(); + return 0; + } + } + + if (optind == argc) { + if (inum_only) + error = inum_cur(); + else + error = ls_cur(NULL); + if (error) { + dbprintf("%s\n", strerror(error)); + exitcode = 1; + } + + return 0; + } + + for (c = optind; c < argc; c++) { + push_cur(); + + error = path_walk(argv[c]); + if (error) + goto err_cur; + + if (inum_only) + error = inum_cur(); + else + error = ls_cur(argv[c]); + if (error) + goto err_cur; + + pop_cur(); + } + + return 0; +err_cur: + pop_cur(); + if (error) { + dbprintf("%s: %s\n", argv[c], strerror(error)); + exitcode = 1; + } + return 0; +} + +static struct cmdinfo ls_cmd = { + .name = "ls", + .altname = "l", + .cfunc = ls_f, + .argmin = 0, + .argmax = -1, + .canpush = 0, + .args = "[-i] [paths...]", + .help = ls_help, +}; + void namei_init(void) { path_cmd.oneline = _("navigate to an inode by path"); add_command(&path_cmd); + + ls_cmd.oneline = _("list directory contents"); + add_command(&ls_cmd); } diff --git a/libxfs/libxfs_api_defs.h b/libxfs/libxfs_api_defs.h index e7e42e93a07e..7029d0e7daf7 100644 --- a/libxfs/libxfs_api_defs.h +++ b/libxfs/libxfs_api_defs.h @@ -187,6 +187,7 @@ #define xfs_trans_resv_calc libxfs_trans_resv_calc #define xfs_trans_roll_inode libxfs_trans_roll_inode #define xfs_trans_roll libxfs_trans_roll +#define xfs_trim_extent libxfs_trim_extent #define xfs_verify_agbno libxfs_verify_agbno #define xfs_verify_agino libxfs_verify_agino diff --git a/man/man8/xfs_db.8 b/man/man8/xfs_db.8 index d67e108a706a..463e6f05ad82 100644 --- a/man/man8/xfs_db.8 +++ b/man/man8/xfs_db.8 @@ -806,6 +806,22 @@ This makes it easier to find discrepancies in the reservation calculations between xfsprogs and the kernel, which will help when diagnosing minimum log size calculation errors. .TP +.BI "ls [\-i] [" paths "]..." +List the contents of a directory. +If a path resolves to a directory, the directory will be listed. +If no paths are supplied and the IO cursor points at a directory inode, +the contents of that directory will be listed. + +The output format is: +directory cookie, inode number, file type, hash, name length, name. +.RS 1.0i +.TP 0.4i +.B \-i +Resolve each of the given paths to an inode number and print that number. +If no paths are given and the IO cursor points to an inode, print the inode +number. +.RE +.TP .BI "metadump [\-egow] " filename Dumps metadata to a file. See .BR xfs_metadump (8)