X-Git-Url: https://git.whamcloud.com/?a=blobdiff_plain;f=debugfs%2Fdebugfs.c;h=dab49d4809fa894e2150e1bd0bcb6a234e6dcd7e;hb=921f4ad53646091767de1e64c9b3332c85f37e5b;hp=ed4f23bb356f9a4ee421217729afa6ebf6beecd5;hpb=7380ac903316c0fe91ed6706eb4d84249a9b348d;p=tools%2Fe2fsprogs.git diff --git a/debugfs/debugfs.c b/debugfs/debugfs.c index ed4f23b..dab49d4 100644 --- a/debugfs/debugfs.c +++ b/debugfs/debugfs.c @@ -20,9 +20,6 @@ extern int optind; extern char *optarg; #endif -#ifdef HAVE_OPTRESET -extern int optreset; /* defined by BSD, but not others */ -#endif #ifdef HAVE_ERRNO_H #include #endif @@ -44,9 +41,11 @@ ext2_filsys current_fs = NULL; ext2_ino_t root, cwd; static void open_filesystem(char *device, int open_flags, blk_t superblock, - blk_t blocksize, int catastrophic) + blk_t blocksize, int catastrophic, + char *data_filename) { int retval; + io_channel data_io = 0; if (superblock != 0 && blocksize == 0) { com_err(device, 0, "if you specify the superblock, you must also specify the block size"); @@ -54,6 +53,21 @@ static void open_filesystem(char *device, int open_flags, blk_t superblock, return; } + if (data_filename) { + if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) { + com_err(device, 0, + "The -d option is only valid when reading an e2image file"); + current_fs = NULL; + return; + } + retval = unix_io_manager->open(data_filename, 0, &data_io); + if (retval) { + com_err(data_filename, 0, "while opening data source"); + current_fs = NULL; + return; + } + } + if (catastrophic && (open_flags & EXT2_FLAG_RW)) { com_err(device, 0, "opening read-only because of catastrophic mode"); @@ -82,6 +96,16 @@ static void open_filesystem(char *device, int open_flags, blk_t superblock, goto errout; } } + + if (data_io) { + retval = ext2fs_set_data_io(current_fs, data_io); + if (retval) { + com_err(device, retval, + "while setting data source"); + goto errout; + } + } + root = cwd = EXT2_ROOT_INO; return; @@ -99,13 +123,11 @@ void do_open_filesys(int argc, char **argv) int catastrophic = 0; blk_t superblock = 0; blk_t blocksize = 0; - int open_flags = 0; + int open_flags = 0; + char *data_filename; - optind = 0; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif - while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) { + reset_getopt(); + while ((c = getopt (argc, argv, "iwfcb:s:d:")) != EOF) { switch (c) { case 'i': open_flags |= EXT2_FLAG_IMAGE_FILE; @@ -119,6 +141,9 @@ void do_open_filesys(int argc, char **argv) case 'c': catastrophic = 1; break; + case 'd': + data_filename = optarg; + break; case 'b': blocksize = parse_ulong(optarg, argv[0], "block size", &err); @@ -143,7 +168,8 @@ void do_open_filesys(int argc, char **argv) if (check_fs_not_open(argv[0])) return; open_filesystem(argv[optind], open_flags, - superblock, blocksize, catastrophic); + superblock, blocksize, catastrophic, + data_filename); } void do_lcd(int argc, char **argv) @@ -199,7 +225,7 @@ void do_init_filesys(int argc, char **argv) return; memset(¶m, 0, sizeof(struct ext2_super_block)); - param.s_blocks_count = parse_ulong(argv[0], argv[2], + param.s_blocks_count = parse_ulong(argv[2], argv[0], "blocks count", &err); if (err) return; @@ -235,16 +261,14 @@ static void print_features(struct ext2_super_block * s, FILE *f) void do_show_super_stats(int argc, char *argv[]) { - int i; + dgrp_t i; FILE *out; struct ext2_group_desc *gdp; int c, header_only = 0; + int numdirs = 0; const char *usage = "Usage: show_super [-h]"; - optind = 0; -#ifdef HAVE_OPTRESET - optreset = 1; /* Makes BSD getopt happy */ -#endif + reset_getopt(); while ((c = getopt (argc, argv, "h")) != EOF) { switch (c) { case 'h': @@ -264,7 +288,10 @@ void do_show_super_stats(int argc, char *argv[]) out = open_pager(); list_super2(current_fs->super, out); - + for (i=0; i < current_fs->group_desc_count; i++) + numdirs += current_fs->group_desc[i].bg_used_dirs_count; + fprintf(out, "Directories: %d\n", numdirs); + if (header_only) { close_pager(out); return; @@ -290,7 +317,8 @@ void do_show_super_stats(int argc, char *argv[]) close_pager(out); } -void do_dirty_filesys(int argc, char **argv) +void do_dirty_filesys(int argc EXT2FS_ATTR((unused)), + char **argv EXT2FS_ATTR((unused))) { if (check_fs_open(argv[0])) return; @@ -328,9 +356,11 @@ static void finish_range(struct list_blocks_struct *lb) lb->first_block = 0; } -static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, - e2_blkcnt_t blockcnt, blk_t ref_block, - int ref_offset, void *private) +static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), + blk_t *blocknr, e2_blkcnt_t blockcnt, + blk_t ref_block EXT2FS_ATTR((unused)), + int ref_offset EXT2FS_ATTR((unused)), + void *private) { struct list_blocks_struct *lb = (struct list_blocks_struct *) private; @@ -458,9 +488,26 @@ void internal_dump_inode(FILE *out, const char *prefix, if (inode->i_dtime) fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime, time_to_string(inode->i_dtime)); - if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0) + if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0) fprintf(out, "%sFast_link_dest: %.*s\n", prefix, (int) inode->i_size, (char *)inode->i_block); + else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) { + int major, minor; + const char *devnote; + + if (inode->i_block[0]) { + major = (inode->i_block[0] >> 8) & 255; + minor = inode->i_block[0] & 255; + devnote = ""; + } else { + major = (inode->i_block[1] & 0xfff00) >> 8; + minor = ((inode->i_block[1] & 0xff) | + ((inode->i_block[1] >> 12) & 0xfff00)); + devnote = "(New-style) "; + } + fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n", + devnote, major, minor, major, minor); + } else if (do_dump_blocks) dump_blocks(out, prefix, inode_num); } @@ -803,9 +850,39 @@ void do_print_working_directory(int argc, char *argv[]) return; } +/* + * Given a mode, return the ext2 file type + */ +static int ext2_file_type(unsigned int mode) +{ + if (LINUX_S_ISREG(mode)) + return EXT2_FT_REG_FILE; + + if (LINUX_S_ISDIR(mode)) + return EXT2_FT_DIR; + + if (LINUX_S_ISCHR(mode)) + return EXT2_FT_CHRDEV; + + if (LINUX_S_ISBLK(mode)) + return EXT2_FT_BLKDEV; + + if (LINUX_S_ISLNK(mode)) + return EXT2_FT_SYMLINK; + + if (LINUX_S_ISFIFO(mode)) + return EXT2_FT_FIFO; + + if (LINUX_S_ISSOCK(mode)) + return EXT2_FT_SOCK; + + return 0; +} + static void make_link(char *sourcename, char *destname) { - ext2_ino_t inode; + ext2_ino_t ino; + struct ext2_inode inode; int retval; ext2_ino_t dir; char *dest, *cp, *basename; @@ -813,8 +890,8 @@ static void make_link(char *sourcename, char *destname) /* * Get the source inode */ - inode = string_to_inode(sourcename); - if (!inode) + ino = string_to_inode(sourcename); + if (!ino) return; basename = strrchr(sourcename, '/'); if (basename) @@ -844,8 +921,12 @@ static void make_link(char *sourcename, char *destname) dest = destname; } } + + if (debugfs_read_inode(ino, &inode, sourcename)) + return; - retval = ext2fs_link(current_fs, dir, dest, inode, 0); + retval = ext2fs_link(current_fs, dir, dest, ino, + ext2_file_type(inode.i_mode)); if (retval) com_err("make_link", retval, ""); return; @@ -862,7 +943,8 @@ void do_link(int argc, char *argv[]) } static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr, - int blockcnt, void *private) + int blockcnt EXT2FS_ATTR((unused)), + void *private EXT2FS_ATTR((unused))) { blk_t block; @@ -906,7 +988,7 @@ void do_undel(int argc, char *argv[]) ext2fs_block_iterate(current_fs, ino, 0, NULL, mark_blocks_proc, NULL); - ext2fs_inode_alloc_stats(current_fs, ino, +1); + ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0); make_link(argv[1], argv[2]); } @@ -1093,6 +1175,13 @@ void do_write(int argc, char *argv[]) return; } + retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile); + if (retval == 0) { + com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]); + close(fd); + return; + } + retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); if (retval) { com_err(argv[0], retval, ""); @@ -1102,6 +1191,15 @@ void do_write(int argc, char *argv[]) printf("Allocated inode: %u\n", newfile); retval = ext2fs_link(current_fs, cwd, argv[2], newfile, EXT2_FT_REG_FILE); + if (retval == EXT2_ET_DIR_NO_SPACE) { + retval = ext2fs_expand_dir(current_fs, cwd); + if (retval) { + com_err(argv[0], retval, "while expanding directory"); + return; + } + retval = ext2fs_link(current_fs, cwd, argv[2], newfile, + EXT2_FT_REG_FILE); + } if (retval) { com_err(argv[2], retval, ""); close(fd); @@ -1109,9 +1207,9 @@ void do_write(int argc, char *argv[]) } if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile)) com_err(argv[0], 0, "Warning: inode already set"); - ext2fs_inode_alloc_stats(current_fs, newfile, +1); + ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0); memset(&inode, 0, sizeof(inode)); - inode.i_mode = statbuf.st_mode; + inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG; inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL); inode.i_links_count = 1; inode.i_size = statbuf.st_size; @@ -1129,11 +1227,11 @@ void do_write(int argc, char *argv[]) void do_mknod(int argc, char *argv[]) { - unsigned long mode, major, minor, nr; + unsigned long mode, major, minor; ext2_ino_t newfile; errcode_t retval; struct ext2_inode inode; - int filetype; + int filetype, nr; if (check_fs_open(argv[0])) return; @@ -1166,7 +1264,7 @@ void do_mknod(int argc, char *argv[]) if (nr == 5) { major = strtoul(argv[3], argv+3, 0); minor = strtoul(argv[4], argv+4, 0); - if (major > 255 || minor > 255 || argv[3][0] || argv[4][0]) + if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0]) nr = 0; } if (argc != nr) @@ -1180,18 +1278,18 @@ void do_mknod(int argc, char *argv[]) } printf("Allocated inode: %u\n", newfile); retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype); - if (retval) { - if (retval == EXT2_ET_DIR_NO_SPACE) { - retval = ext2fs_expand_dir(current_fs, cwd); - if (!retval) - retval = ext2fs_link(current_fs, cwd, - argv[1], newfile, - filetype); - } + if (retval == EXT2_ET_DIR_NO_SPACE) { + retval = ext2fs_expand_dir(current_fs, cwd); if (retval) { - com_err(argv[1], retval, ""); + com_err(argv[0], retval, "while expanding directory"); return; } + retval = ext2fs_link(current_fs, cwd, argv[1], newfile, + filetype); + } + if (retval) { + com_err(argv[1], retval, ""); + return; } if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile)) com_err(argv[0], 0, "Warning: inode already set"); @@ -1200,7 +1298,13 @@ void do_mknod(int argc, char *argv[]) memset(&inode, 0, sizeof(inode)); inode.i_mode = mode; inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL); - inode.i_block[0] = major*256+minor; + if ((major < 256) && (minor < 256)) { + inode.i_block[0] = major*256+minor; + inode.i_block[1] = 0; + } else { + inode.i_block[0] = 0; + inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12); + } inode.i_links_count = 1; if (debugfs_write_inode(newfile, &inode, argv[0])) return; @@ -1231,8 +1335,16 @@ void do_mkdir(int argc, char *argv[]) name = argv[1]; } - +try_again: retval = ext2fs_mkdir(current_fs, parent, 0, name); + if (retval == EXT2_ET_DIR_NO_SPACE) { + retval = ext2fs_expand_dir(current_fs, parent); + if (retval) { + com_err("argv[0]", retval, "while expanding directory"); + return; + } + goto try_again; + } if (retval) { com_err("ext2fs_mkdir", retval, ""); return; @@ -1240,14 +1352,9 @@ void do_mkdir(int argc, char *argv[]) } -void do_rmdir(int argc, char *argv[]) -{ - printf("Unimplemented\n"); -} - - static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr, - int blockcnt, void *private) + int blockcnt EXT2FS_ATTR((unused)), + void *private EXT2FS_ATTR((unused))) { blk_t block; @@ -1265,12 +1372,14 @@ static void kill_file_by_inode(ext2_ino_t inode) inode_buf.i_dtime = time(NULL); if (debugfs_write_inode(inode, &inode_buf, 0)) return; + if (!ext2fs_inode_has_valid_blocks(&inode_buf)) + return; - printf("Kill file by inode %u\n", inode); ext2fs_block_iterate(current_fs, inode, 0, NULL, release_blocks_proc, NULL); printf("\n"); - ext2fs_inode_alloc_stats(current_fs, inode, -1); + ext2fs_inode_alloc_stats2(current_fs, inode, -1, + LINUX_S_ISDIR(inode_buf.i_mode)); } @@ -1317,7 +1426,92 @@ void do_rm(int argc, char *argv[]) kill_file_by_inode(inode_num); } -void do_show_debugfs_params(int argc, char *argv[]) +struct rd_struct { + ext2_ino_t parent; + int empty; +}; + +static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), + int entry EXT2FS_ATTR((unused)), + struct ext2_dir_entry *dirent, + int offset EXT2FS_ATTR((unused)), + int blocksize EXT2FS_ATTR((unused)), + char *buf EXT2FS_ATTR((unused)), + void *private) +{ + struct rd_struct *rds = (struct rd_struct *) private; + + if (dirent->inode == 0) + return 0; + if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.')) + return 0; + if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') && + (dirent->name[1] == '.')) { + rds->parent = dirent->inode; + return 0; + } + rds->empty = 0; + return 0; +} + +void do_rmdir(int argc, char *argv[]) +{ + int retval; + ext2_ino_t inode_num; + struct ext2_inode inode; + struct rd_struct rds; + + if (common_args_process(argc, argv, 2, 2, "rmdir", + "", CHECK_FS_RW)) + return; + + retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); + if (retval) { + com_err(argv[0], retval, "while trying to resolve filename"); + return; + } + + if (debugfs_read_inode(inode_num, &inode, argv[0])) + return; + + if (!LINUX_S_ISDIR(inode.i_mode)) { + com_err(argv[0], 0, "file is not a directory"); + return; + } + + rds.parent = 0; + rds.empty = 1; + + retval = ext2fs_dir_iterate2(current_fs, inode_num, 0, + 0, rmdir_proc, &rds); + if (retval) { + com_err(argv[0], retval, "while iterating over directory"); + return; + } + if (rds.empty == 0) { + com_err(argv[0], 0, "directory not empty"); + return; + } + + inode.i_links_count = 0; + if (debugfs_write_inode(inode_num, &inode, argv[0])) + return; + + unlink_file_by_name(argv[1]); + kill_file_by_inode(inode_num); + + if (rds.parent) { + if (debugfs_read_inode(rds.parent, &inode, argv[0])) + return; + if (inode.i_links_count > 1) + inode.i_links_count--; + if (debugfs_write_inode(rds.parent, &inode, argv[0])) + return; + } +} + +void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)), + char *argv[] EXT2FS_ATTR((unused))) { FILE *out = stdout; @@ -1362,6 +1556,64 @@ void do_features(int argc, char *argv[]) print_features(current_fs->super, stdout); } +void do_bmap(int argc, char *argv[]) +{ + ext2_ino_t ino; + blk_t blk, pblk; + int err; + errcode_t errcode; + + if (common_args_process(argc, argv, 3, 3, argv[0], + " logical_blk", 0)) + return; + + ino = string_to_inode(argv[1]); + if (!ino) + return; + blk = parse_ulong(argv[2], argv[0], "logical_block", &err); + + errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk); + if (errcode) { + com_err("argv[0]", errcode, + "while mapping logical block %d\n", blk); + return; + } + printf("%d\n", pblk); +} + +void do_imap(int argc, char *argv[]) +{ + ext2_ino_t ino; + unsigned long group, block, block_nr, offset; + + if (common_args_process(argc, argv, 2, 2, argv[0], + "", 0)) + return; + ino = string_to_inode(argv[1]); + if (!ino) + return; + + group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super); + offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) * + EXT2_INODE_SIZE(current_fs->super); + block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super); + if (!current_fs->group_desc[(unsigned)group].bg_inode_table) { + com_err(argv[0], 0, "Inode table for group %lu is missing\n", + group); + return; + } + block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table + + block; + offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1); + + printf("Inode %d is part of block group %lu\n" + "\tlocated at block %lu, offset 0x%04lx\n", ino, group, + block_nr, offset); + +} + + + static int source_file(const char *cmd_file, int sci_idx) { FILE *f; @@ -1413,12 +1665,13 @@ int main(int argc, char **argv) blk_t superblock = 0; blk_t blocksize = 0; int catastrophic = 0; + char *data_filename = 0; initialize_ext2_error_table(); fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION, E2FSPROGS_DATE); - while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) { + while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) { switch (c) { case 'R': request = optarg; @@ -1426,6 +1679,9 @@ int main(int argc, char **argv) case 'f': cmd_file = optarg; break; + case 'd': + data_filename = optarg; + break; case 'i': open_flags |= EXT2_FLAG_IMAGE_FILE; break; @@ -1433,11 +1689,11 @@ int main(int argc, char **argv) open_flags |= EXT2_FLAG_RW; break; case 'b': - blocksize = parse_ulong(argv[0], optarg, + blocksize = parse_ulong(optarg, argv[0], "block size", 0); break; case 's': - superblock = parse_ulong(argv[0], optarg, + superblock = parse_ulong(optarg, argv[0], "superblock number", 0); break; case 'c': @@ -1455,7 +1711,8 @@ int main(int argc, char **argv) } if (optind < argc) open_filesystem(argv[optind], open_flags, - superblock, blocksize, catastrophic); + superblock, blocksize, catastrophic, + data_filename); sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL, &debug_cmds, &retval); @@ -1463,6 +1720,7 @@ int main(int argc, char **argv) ss_perror(sci_idx, retval, "creating invocation"); exit(1); } + ss_get_readline(sci_idx); (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval); if (retval) {