Message ID | 160375515483.880118.8069916247570952970.stgit@magnolia (mailing list archive) |
---|---|
State | Superseded |
Headers | show |
Series | xfs_db: add minimal directory navigation | expand |
On Mon, Oct 26, 2020 at 04:32:34PM -0700, Darrick J. Wong wrote: > From: Darrick J. Wong <darrick.wong@oracle.com> > > Add a command to xfs_db so that we can navigate to inodes by path. > > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> .... > +/* 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; > +} This could return negative errors.... > +/* 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 ret = 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]) { > + dbprintf(_("current object is not an inode.\n")); > + return -1; > + } > + > + if (!S_ISDIR(iocur_top->mode)) { > + dbprintf(_("current inode %llu is not a directory.\n"), > + (unsigned long long)iocur_top->ino); > + return -1; > + } > + rootino = iocur_top->ino; > + } > + > + dirpath = path_parse(p); > + if (!dirpath) { > + dbprintf(_("%s: not enough memory to parse.\n"), path); > + return -1; > + } and this could return -ENOMEM here with no error message.... > + > + ret = path_navigate(mp, rootino, dirpath); > + if (ret) { > + dbprintf(_("%s: %s\n"), path, strerror(ret)); > + ret = -1; > + } ... don't overwrite ret here, move the dbprintf() to the caller and the one error message captures all possible errors. Also, no need for _() for a format string that contains no translatable text.... Otherwise, looks fine. -Dave.
On Wed, Oct 28, 2020 at 11:35:51AM +1100, Dave Chinner wrote: > On Mon, Oct 26, 2020 at 04:32:34PM -0700, Darrick J. Wong wrote: > > From: Darrick J. Wong <darrick.wong@oracle.com> > > > > Add a command to xfs_db so that we can navigate to inodes by path. > > > > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> > .... > > +/* 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; > > +} > > This could return negative errors.... > > > +/* 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 ret = 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]) { > > + dbprintf(_("current object is not an inode.\n")); > > + return -1; > > + } > > + > > + if (!S_ISDIR(iocur_top->mode)) { > > + dbprintf(_("current inode %llu is not a directory.\n"), > > + (unsigned long long)iocur_top->ino); > > + return -1; > > + } > > + rootino = iocur_top->ino; > > + } > > + > > + dirpath = path_parse(p); > > + if (!dirpath) { > > + dbprintf(_("%s: not enough memory to parse.\n"), path); > > + return -1; > > + } > > and this could return -ENOMEM here with no error message.... > > > + > > + ret = path_navigate(mp, rootino, dirpath); > > + if (ret) { > > + dbprintf(_("%s: %s\n"), path, strerror(ret)); > > + ret = -1; > > + } > > ... don't overwrite ret here, move the dbprintf() to the caller and > the one error message captures all possible errors. > > Also, no need for _() for a format string that contains no > translatable text.... Ok, will fix. Thanks! --D > Otherwise, looks fine. > > -Dave. > -- > Dave Chinner > david@fromorbit.com
On 10/26/20 6:32 PM, Darrick J. Wong wrote: > From: Darrick J. Wong <darrick.wong@oracle.com> > > Add a command to xfs_db so that we can navigate to inodes by path. > > Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com> ... > +/* Path lookup */ > + > +/* Key for looking up metadata inodes. */ > +struct dirpath { > + /* Array of string pointers. */ > + char **path; > + > + /* Number of strings in path. */ > + unsigned int depth; > +}; This generates warnings for me in gcc-4.8.5 (hah, I know) as well as gcc-9. namei.c:101:12: warning: pointer targets in initialization of ‘const unsigned char *’ from ‘char *’ differ in signedness [-Wpointer-sign] due to the "unsigned char *" type from: commit e2bcd936eb95d0019ca5e05f9fdd27e770ddded1 Author: Dave Chinner <david@fromorbit.com> Date: Wed Jan 20 10:44:58 2010 +1100 xfs: directory names are unsigned Convert the struct xfs_name to use unsigned chars for the name strings to match both what is stored on disk (__uint8_t) and what the VFS expects (unsigned char). Signed-off-by: Dave Chinner <david@fromorbit.com> Reviewed-by: Christoph Hellwig <hch@lst.de> so maybe just cast to shut it up? diff --git a/db/namei.c b/db/namei.c index 4b467ff8..a902f302 100644 --- a/db/namei.c +++ b/db/namei.c @@ -98,7 +98,7 @@ path_navigate( for (i = 0; i < dirpath->depth; i++) { struct xfs_name xname = { - .name = dirpath->path[i], + .name = (unsigned char *)dirpath->path[i], .len = strlen(dirpath->path[i]), }; @@ -253,7 +253,7 @@ dir_emit( uint8_t dtype) { char *display_name; - struct xfs_name xname = { .name = name }; + struct xfs_name xname = { .name = (unsigned char *)name }; const char *dstr = get_dstr(mp, dtype); xfs_dahash_t hash; bool good;
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..3c9889d62338 --- /dev/null +++ b/db/namei.c @@ -0,0 +1,228 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * Copyright (C) 2020 Oracle. All Rights Reserved. + * Author: Darrick J. Wong <darrick.wong@oracle.com> + */ +#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 ret = 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]) { + dbprintf(_("current object is not an inode.\n")); + return -1; + } + + if (!S_ISDIR(iocur_top->mode)) { + dbprintf(_("current inode %llu is not a directory.\n"), + (unsigned long long)iocur_top->ino); + return -1; + } + rootino = iocur_top->ino; + } + + dirpath = path_parse(p); + if (!dirpath) { + dbprintf(_("%s: not enough memory to parse.\n"), path); + return -1; + } + + ret = path_navigate(mp, rootino, dirpath); + if (ret) { + dbprintf(_("%s: %s\n"), path, strerror(ret)); + ret = -1; + } + + path_free(dirpath); + return ret; +} + +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; + + while ((c = getopt(argc, argv, "")) != -1) { + switch (c) { + default: + path_help(); + return 0; + } + } + + if (path_walk(argv[optind])) + exitcode = 1; + return 0; +} + +static const cmdinfo_t path_cmd = { + .name = "path", + .altname = NULL, + .cfunc = path_f, + .argmin = 1, + .argmax = 1, + .canpush = 0, + .args = "", + .oneline = N_("navigate to an inode by path"), + .help = path_help, +}; + +void +namei_init(void) +{ + 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