void do_set_inode(int argc, char *argv[]) { const char *usage = "<inode> <field> <value>\n" "\t\"set_inode_field -l\" will list the names of " "the fields in an ext2 inode\n\twhich can be set."; static struct field_set_info *ss; if ((argc == 2) && !strcmp(argv[1], "-l")) { print_possible_fields(inode_fields); return; } if (common_args_process(argc, argv, 4, 4, "set_inode", usage, CHECK_FS_RW)) return; if ((ss = find_field(inode_fields, argv[2])) == 0) { com_err(argv[0], 0, "invalid field specifier: %s", argv[2]); return; } set_ino = string_to_inode(argv[1]); if (!set_ino) return; if (debugfs_read_inode(set_ino, &set_inode, argv[1])) return; if (ss->func(ss, argv[3]) == 0) { if (debugfs_write_inode(set_ino, &set_inode, argv[1])) return; } }
static void dump_file(const char *cmdname, ext2_ino_t ino, int fd, int preserve, char *outname) { errcode_t retval; struct ext2_inode inode; char *buf = 0; ext2_file_t e2_file; int nbytes; unsigned int got, blocksize = current_fs->blocksize; if (debugfs_read_inode(ino, &inode, cmdname)) return; retval = ext2fs_file_open(current_fs, ino, 0, &e2_file); if (retval) { com_err(cmdname, retval, "while opening ext2 file"); return; } retval = ext2fs_get_mem(blocksize, &buf); if (retval) { com_err(cmdname, retval, "while allocating memory"); return; } while (1) { retval = ext2fs_file_read(e2_file, buf, blocksize, &got); if (retval) com_err(cmdname, retval, "while reading ext2 file"); if (got == 0) break; nbytes = write(fd, buf, got); if ((unsigned) nbytes != got) com_err(cmdname, errno, "while writing file"); } if (buf) ext2fs_free_mem(&buf); retval = ext2fs_file_close(e2_file); if (retval) { com_err(cmdname, retval, "while closing ext2 file"); return; } if (preserve) fix_perms("dump_file", &inode, fd, outname); else if (fd != 1) close(fd); return; }
void do_htree_dump(int argc, char *argv[]) { ext2_ino_t ino; struct ext2_inode inode; int c; int long_opt = 0; blk_t blk; char *buf = NULL; struct ext2_dx_root_info *rootnode; struct ext2_dx_entry *ent; struct ext2_dx_countlimit *limit; errcode_t errcode; if (check_fs_open(argv[0])) return; pager = open_pager(); reset_getopt(); while ((c = getopt (argc, argv, "l")) != EOF) { switch (c) { case 'l': long_opt++; break; default: goto print_usage; } } if (argc > optind+1) { print_usage: com_err(0, 0, "Usage: htree_dump [-l] file"); goto errout; } if (argc == optind) ino = cwd; else ino = string_to_inode(argv[optind]); if (!ino) goto errout; if (debugfs_read_inode(ino, &inode, argv[1])) goto errout; if (!LINUX_S_ISDIR(inode.i_mode)) { com_err(argv[0], 0, "Not a directory"); goto errout; } if ((inode.i_flags & EXT2_BTREE_FL) == 0) { com_err(argv[0], 0, "Not a hash-indexed directory"); goto errout; } buf = malloc(2*current_fs->blocksize); if (!buf) { com_err(argv[0], 0, "Couldn't allocate htree buffer"); goto errout; } errcode = ext2fs_bmap(current_fs, ino, &inode, buf, 0, 0, &blk); if (errcode) { com_err("do_htree_block", errcode, "while mapping logical block 0\n"); goto errout; } errcode = io_channel_read_blk(current_fs->io, blk, 1, buf); if (errcode) { com_err(argv[0], errcode, "Error reading root node"); goto errout; } rootnode = (struct ext2_dx_root_info *) (buf + 24); fprintf(pager, "Root node dump:\n"); fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero); fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version); fprintf(pager, "\t Info length: %d\n", rootnode->info_length); fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels); fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags); ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length); limit = (struct ext2_dx_countlimit *) ent; htree_dump_int_node(current_fs, ino, &inode, rootnode, ent, buf + current_fs->blocksize, rootnode->indirect_levels); errout: free(buf); close_pager(pager); }
void do_logdump(int argc, char **argv) { int c; int retval; char *out_fn; FILE *out_file; char *inode_spec = NULL; char *journal_fn = NULL; int journal_fd = 0; int use_sb = 0; ext2_ino_t journal_inum; struct ext2_inode journal_inode; ext2_file_t journal_file; char *tmp; struct journal_source journal_source; struct ext2_super_block *es = NULL; journal_source.where = 0; journal_source.fd = 0; journal_source.file = 0; dump_all = 0; dump_contents = 0; dump_descriptors = 1; block_to_dump = ANY_BLOCK; bitmap_to_dump = -1; inode_block_to_dump = ANY_BLOCK; inode_to_dump = -1; reset_getopt(); while ((c = getopt (argc, argv, "ab:ci:f:s")) != EOF) { switch (c) { case 'a': dump_all++; break; case 'b': block_to_dump = strtoul(optarg, &tmp, 0); if (*tmp) { com_err(argv[0], 0, "Bad block number - %s", optarg); return; } dump_descriptors = 0; break; case 'c': dump_contents++; break; case 'f': journal_fn = optarg; break; case 'i': inode_spec = optarg; dump_descriptors = 0; break; case 's': use_sb++; break; default: goto print_usage; } } if (optind != argc && optind != argc-1) { goto print_usage; } if (current_fs) es = current_fs->super; if (inode_spec) { int inode_group, group_offset, inodes_per_block; if (check_fs_open(argv[0])) return; inode_to_dump = string_to_inode(inode_spec); if (!inode_to_dump) return; inode_group = ((inode_to_dump - 1) / es->s_inodes_per_group); group_offset = ((inode_to_dump - 1) % es->s_inodes_per_group); inodes_per_block = (current_fs->blocksize / sizeof(struct ext2_inode)); inode_block_to_dump = current_fs->group_desc[inode_group].bg_inode_table + (group_offset / inodes_per_block); inode_offset_to_dump = ((group_offset % inodes_per_block) * sizeof(struct ext2_inode)); printf("Inode %u is at group %u, block %u, offset %u\n", inode_to_dump, inode_group, inode_block_to_dump, inode_offset_to_dump); } if (optind == argc) { out_file = stdout; } else { out_fn = argv[optind]; out_file = fopen(out_fn, "w"); if (!out_file < 0) { com_err(argv[0], errno, "while opening %s for logdump", out_fn); return; } } if (block_to_dump != ANY_BLOCK && current_fs != NULL) { group_to_dump = ((block_to_dump - es->s_first_data_block) / es->s_blocks_per_group); bitmap_to_dump = current_fs->group_desc[group_to_dump].bg_block_bitmap; } if (!journal_fn && check_fs_open(argv[0])) return; if (journal_fn) { /* Set up to read journal from a regular file somewhere */ journal_fd = open(journal_fn, O_RDONLY, 0); if (journal_fd < 0) { com_err(argv[0], errno, "while opening %s for logdump", journal_fn); return; } journal_source.where = JOURNAL_IS_EXTERNAL; journal_source.fd = journal_fd; } else if ((journal_inum = es->s_journal_inum)) { if (use_sb) { if (es->s_jnl_backup_type != EXT3_JNL_BACKUP_BLOCKS) { com_err(argv[0], 0, "no journal backup in super block\n"); return; } memset(&journal_inode, 0, sizeof(struct ext2_inode)); memcpy(&journal_inode.i_block[0], es->s_jnl_blocks, EXT2_N_BLOCKS*4); journal_inode.i_size = es->s_jnl_blocks[16]; journal_inode.i_links_count = 1; journal_inode.i_mode = LINUX_S_IFREG | 0600; } else { if (debugfs_read_inode(journal_inum, &journal_inode, argv[0])) return; } retval = ext2fs_file_open2(current_fs, journal_inum, &journal_inode, 0, &journal_file); if (retval) { com_err(argv[0], retval, "while opening ext2 file"); return; } journal_source.where = JOURNAL_IS_INTERNAL; journal_source.file = journal_file; } else { char uuid[37]; uuid_unparse(es->s_journal_uuid, uuid); journal_fn = blkid_get_devname(NULL, "UUID", uuid); if (!journal_fn) journal_fn = blkid_devno_to_devname(es->s_journal_dev); if (!journal_fn) { com_err(argv[0], 0, "filesystem has no journal"); return; } journal_fd = open(journal_fn, O_RDONLY, 0); if (journal_fd < 0) { com_err(argv[0], errno, "while opening %s for logdump", journal_fn); free(journal_fn); return; } fprintf(out_file, "Using external journal found at %s\n", journal_fn); free(journal_fn); journal_source.where = JOURNAL_IS_EXTERNAL; journal_source.fd = journal_fd; } dump_journal(argv[0], out_file, &journal_source); if (journal_source.where == JOURNAL_IS_INTERNAL) ext2fs_file_close(journal_file); else close(journal_fd); if (out_file != stdout) fclose(out_file); return; print_usage: fprintf(stderr, "%s: Usage: logdump [-ac] [-b<block>] [-i<inode>]\n\t" "[-f<journal_file>] [output_file]\n", argv[0]); }
void do_htree_dump(int argc, char *argv[]) { ext2_ino_t ino; struct ext2_inode inode; blk64_t blk; char *buf = NULL; struct ext2_dx_root_info *rootnode; struct ext2_dx_entry *ent; errcode_t errcode; if (check_fs_open(argv[0])) return; pager = open_pager(); if (common_inode_args_process(argc, argv, &ino, 0)) goto errout; if (debugfs_read_inode(ino, &inode, argv[1])) goto errout; if (!LINUX_S_ISDIR(inode.i_mode)) { com_err(argv[0], 0, "Not a directory"); goto errout; } if ((inode.i_flags & EXT2_BTREE_FL) == 0) { com_err(argv[0], 0, "Not a hash-indexed directory"); goto errout; } buf = malloc(2*current_fs->blocksize); if (!buf) { com_err(argv[0], 0, "Couldn't allocate htree buffer"); goto errout; } errcode = ext2fs_bmap2(current_fs, ino, &inode, buf, 0, 0, 0, &blk); if (errcode) { com_err("do_htree_block", errcode, "while mapping logical block 0\n"); goto errout; } errcode = io_channel_read_blk64(current_fs->io, blk, 1, buf); if (errcode) { com_err(argv[0], errcode, "Error reading root node"); goto errout; } rootnode = (struct ext2_dx_root_info *) (buf + 24); fprintf(pager, "Root node dump:\n"); fprintf(pager, "\t Reserved zero: %u\n", rootnode->reserved_zero); fprintf(pager, "\t Hash Version: %d\n", rootnode->hash_version); fprintf(pager, "\t Info length: %d\n", rootnode->info_length); fprintf(pager, "\t Indirect levels: %d\n", rootnode->indirect_levels); fprintf(pager, "\t Flags: %d\n", rootnode->unused_flags); ent = (struct ext2_dx_entry *) (buf + 24 + rootnode->info_length); htree_dump_int_node(current_fs, ino, &inode, rootnode, ent, buf + current_fs->blocksize, rootnode->indirect_levels); errout: free(buf); close_pager(pager); }