debugfs.c revision af0df2aa4a073f7e1a2d58b40010ecd891e80a60
1/* 2 * debugfs.c --- a program which allows you to attach an ext2fs 3 * filesystem and play with it. 4 * 5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed 6 * under the terms of the GNU Public License. 7 * 8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu> 9 */ 10 11#include <stdio.h> 12#include <unistd.h> 13#include <stdlib.h> 14#include <ctype.h> 15#include <string.h> 16#include <time.h> 17#ifdef HAVE_GETOPT_H 18#include <getopt.h> 19#else 20extern int optind; 21extern char *optarg; 22#endif 23#ifdef HAVE_ERRNO_H 24#include <errno.h> 25#endif 26#include <fcntl.h> 27#include <sys/types.h> 28#include <sys/stat.h> 29 30#include "et/com_err.h" 31#include "ss/ss.h" 32#include "debugfs.h" 33#include "uuid/uuid.h" 34#include "e2p/e2p.h" 35 36#include <ext2fs/ext2_ext_attr.h> 37 38#include "../version.h" 39#include "jfs_user.h" 40 41extern ss_request_table debug_cmds; 42ss_request_table *extra_cmds; 43const char *debug_prog_name; 44 45ext2_filsys current_fs = NULL; 46ext2_ino_t root, cwd; 47 48static void open_filesystem(char *device, int open_flags, blk64_t superblock, 49 blk64_t blocksize, int catastrophic, 50 char *data_filename) 51{ 52 int retval; 53 io_channel data_io = 0; 54 55 if (superblock != 0 && blocksize == 0) { 56 com_err(device, 0, "if you specify the superblock, you must also specify the block size"); 57 current_fs = NULL; 58 return; 59 } 60 61 if (data_filename) { 62 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) { 63 com_err(device, 0, 64 "The -d option is only valid when reading an e2image file"); 65 current_fs = NULL; 66 return; 67 } 68 retval = unix_io_manager->open(data_filename, 0, &data_io); 69 if (retval) { 70 com_err(data_filename, 0, "while opening data source"); 71 current_fs = NULL; 72 return; 73 } 74 } 75 76 if (catastrophic && (open_flags & EXT2_FLAG_RW)) { 77 com_err(device, 0, 78 "opening read-only because of catastrophic mode"); 79 open_flags &= ~EXT2_FLAG_RW; 80 } 81 82 retval = ext2fs_open(device, open_flags, superblock, blocksize, 83 unix_io_manager, ¤t_fs); 84 if (retval) { 85 com_err(device, retval, "while opening filesystem"); 86 current_fs = NULL; 87 return; 88 } 89 90 if (catastrophic) 91 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps"); 92 else { 93 retval = ext2fs_read_inode_bitmap(current_fs); 94 if (retval) { 95 com_err(device, retval, "while reading inode bitmap"); 96 goto errout; 97 } 98 retval = ext2fs_read_block_bitmap(current_fs); 99 if (retval) { 100 com_err(device, retval, "while reading block bitmap"); 101 goto errout; 102 } 103 } 104 105 if (data_io) { 106 retval = ext2fs_set_data_io(current_fs, data_io); 107 if (retval) { 108 com_err(device, retval, 109 "while setting data source"); 110 goto errout; 111 } 112 } 113 114 root = cwd = EXT2_ROOT_INO; 115 return; 116 117errout: 118 retval = ext2fs_close(current_fs); 119 if (retval) 120 com_err(device, retval, "while trying to close filesystem"); 121 current_fs = NULL; 122} 123 124void do_open_filesys(int argc, char **argv) 125{ 126 int c, err; 127 int catastrophic = 0; 128 blk64_t superblock = 0; 129 blk64_t blocksize = 0; 130 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; 131 char *data_filename = 0; 132 133 reset_getopt(); 134 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) { 135 switch (c) { 136 case 'i': 137 open_flags |= EXT2_FLAG_IMAGE_FILE; 138 break; 139 case 'w': 140 open_flags |= EXT2_FLAG_RW; 141 break; 142 case 'f': 143 open_flags |= EXT2_FLAG_FORCE; 144 break; 145 case 'e': 146 open_flags |= EXT2_FLAG_EXCLUSIVE; 147 break; 148 case 'c': 149 catastrophic = 1; 150 break; 151 case 'd': 152 data_filename = optarg; 153 break; 154 case 'D': 155 open_flags |= EXT2_FLAG_DIRECT_IO; 156 break; 157 case 'b': 158 blocksize = parse_ulong(optarg, argv[0], 159 "block size", &err); 160 if (err) 161 return; 162 break; 163 case 's': 164 superblock = parse_ulong(optarg, argv[0], 165 "superblock number", &err); 166 if (err) 167 return; 168 break; 169 default: 170 goto print_usage; 171 } 172 } 173 if (optind != argc-1) { 174 goto print_usage; 175 } 176 if (check_fs_not_open(argv[0])) 177 return; 178 open_filesystem(argv[optind], open_flags, 179 superblock, blocksize, catastrophic, 180 data_filename); 181 return; 182 183print_usage: 184 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] " 185 "[-c] [-w] <device>\n", argv[0]); 186} 187 188void do_lcd(int argc, char **argv) 189{ 190 if (argc != 2) { 191 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>"); 192 return; 193 } 194 195 if (chdir(argv[1]) == -1) { 196 com_err(argv[0], errno, 197 "while trying to change native directory to %s", 198 argv[1]); 199 return; 200 } 201} 202 203static void close_filesystem(NOARGS) 204{ 205 int retval; 206 207 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) { 208 retval = ext2fs_write_inode_bitmap(current_fs); 209 if (retval) 210 com_err("ext2fs_write_inode_bitmap", retval, 0); 211 } 212 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) { 213 retval = ext2fs_write_block_bitmap(current_fs); 214 if (retval) 215 com_err("ext2fs_write_block_bitmap", retval, 0); 216 } 217 retval = ext2fs_close(current_fs); 218 if (retval) 219 com_err("ext2fs_close", retval, 0); 220 current_fs = NULL; 221 return; 222} 223 224void do_close_filesys(int argc, char **argv) 225{ 226 int c; 227 228 if (check_fs_open(argv[0])) 229 return; 230 231 reset_getopt(); 232 while ((c = getopt (argc, argv, "a")) != EOF) { 233 switch (c) { 234 case 'a': 235 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY; 236 break; 237 default: 238 goto print_usage; 239 } 240 } 241 242 if (argc > optind) { 243 print_usage: 244 com_err(0, 0, "Usage: close_filesys [-a]"); 245 return; 246 } 247 248 close_filesystem(); 249} 250 251void do_init_filesys(int argc, char **argv) 252{ 253 struct ext2_super_block param; 254 errcode_t retval; 255 int err; 256 257 if (common_args_process(argc, argv, 3, 3, "initialize", 258 "<device> <blocksize>", CHECK_FS_NOTOPEN)) 259 return; 260 261 memset(¶m, 0, sizeof(struct ext2_super_block)); 262 ext2fs_blocks_count_set(¶m, parse_ulong(argv[2], argv[0], 263 "blocks count", &err)); 264 if (err) 265 return; 266 retval = ext2fs_initialize(argv[1], 0, ¶m, 267 unix_io_manager, ¤t_fs); 268 if (retval) { 269 com_err(argv[1], retval, "while initializing filesystem"); 270 current_fs = NULL; 271 return; 272 } 273 root = cwd = EXT2_ROOT_INO; 274 return; 275} 276 277static void print_features(struct ext2_super_block * s, FILE *f) 278{ 279 int i, j, printed=0; 280 __u32 *mask = &s->s_feature_compat, m; 281 282 fputs("Filesystem features:", f); 283 for (i=0; i <3; i++,mask++) { 284 for (j=0,m=1; j < 32; j++, m<<=1) { 285 if (*mask & m) { 286 fprintf(f, " %s", e2p_feature2string(i, m)); 287 printed++; 288 } 289 } 290 } 291 if (printed == 0) 292 fputs("(none)", f); 293 fputs("\n", f); 294} 295 296static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask, 297 const char *str, int *first, FILE *f) 298{ 299 if (ext2fs_bg_flags_test(fs, group, mask)) { 300 if (*first) { 301 fputs(" [", f); 302 *first = 0; 303 } else 304 fputs(", ", f); 305 fputs(str, f); 306 } 307} 308 309void do_show_super_stats(int argc, char *argv[]) 310{ 311 dgrp_t i; 312 FILE *out; 313 int c, header_only = 0; 314 int numdirs = 0, first, gdt_csum; 315 316 reset_getopt(); 317 while ((c = getopt (argc, argv, "h")) != EOF) { 318 switch (c) { 319 case 'h': 320 header_only++; 321 break; 322 default: 323 goto print_usage; 324 } 325 } 326 if (optind != argc) { 327 goto print_usage; 328 } 329 if (check_fs_open(argv[0])) 330 return; 331 out = open_pager(); 332 333 list_super2(current_fs->super, out); 334 for (i=0; i < current_fs->group_desc_count; i++) 335 numdirs += ext2fs_bg_used_dirs_count(current_fs, i); 336 fprintf(out, "Directories: %d\n", numdirs); 337 338 if (header_only) { 339 close_pager(out); 340 return; 341 } 342 343 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super, 344 EXT4_FEATURE_RO_COMPAT_GDT_CSUM); 345 for (i = 0; i < current_fs->group_desc_count; i++) { 346 fprintf(out, " Group %2d: block bitmap at %llu, " 347 "inode bitmap at %llu, " 348 "inode table at %llu\n" 349 " %u free %s, " 350 "%u free %s, " 351 "%u used %s%s", 352 i, ext2fs_block_bitmap_loc(current_fs, i), 353 ext2fs_inode_bitmap_loc(current_fs, i), 354 ext2fs_inode_table_loc(current_fs, i), 355 ext2fs_bg_free_blocks_count(current_fs, i), 356 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ? 357 "blocks" : "block", 358 ext2fs_bg_free_inodes_count(current_fs, i), 359 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ? 360 "inodes" : "inode", 361 ext2fs_bg_used_dirs_count(current_fs, i), 362 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories" 363 : "directory", gdt_csum ? ", " : "\n"); 364 if (gdt_csum) 365 fprintf(out, "%u unused %s\n", 366 ext2fs_bg_itable_unused(current_fs, i), 367 ext2fs_bg_itable_unused(current_fs, i) != 1 ? 368 "inodes" : "inode"); 369 first = 1; 370 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init", 371 &first, out); 372 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init", 373 &first, out); 374 if (gdt_csum) { 375 fprintf(out, "%sChecksum 0x%04x", 376 first ? " [":", ", ext2fs_bg_checksum(current_fs, i)); 377 first = 0; 378 } 379 if (!first) 380 fputs("]\n", out); 381 } 382 close_pager(out); 383 return; 384print_usage: 385 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]); 386} 387 388void do_dirty_filesys(int argc EXT2FS_ATTR((unused)), 389 char **argv EXT2FS_ATTR((unused))) 390{ 391 if (check_fs_open(argv[0])) 392 return; 393 if (check_fs_read_write(argv[0])) 394 return; 395 396 if (argv[1] && !strcmp(argv[1], "-clean")) 397 current_fs->super->s_state |= EXT2_VALID_FS; 398 else 399 current_fs->super->s_state &= ~EXT2_VALID_FS; 400 ext2fs_mark_super_dirty(current_fs); 401} 402 403struct list_blocks_struct { 404 FILE *f; 405 e2_blkcnt_t total; 406 blk64_t first_block, last_block; 407 e2_blkcnt_t first_bcnt, last_bcnt; 408 e2_blkcnt_t first; 409}; 410 411static void finish_range(struct list_blocks_struct *lb) 412{ 413 if (lb->first_block == 0) 414 return; 415 if (lb->first) 416 lb->first = 0; 417 else 418 fprintf(lb->f, ", "); 419 if (lb->first_block == lb->last_block) 420 fprintf(lb->f, "(%lld):%llu", 421 (long long)lb->first_bcnt, lb->first_block); 422 else 423 fprintf(lb->f, "(%lld-%lld):%llu-%llu", 424 (long long)lb->first_bcnt, (long long)lb->last_bcnt, 425 lb->first_block, lb->last_block); 426 lb->first_block = 0; 427} 428 429static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), 430 blk64_t *blocknr, e2_blkcnt_t blockcnt, 431 blk64_t ref_block EXT2FS_ATTR((unused)), 432 int ref_offset EXT2FS_ATTR((unused)), 433 void *private) 434{ 435 struct list_blocks_struct *lb = (struct list_blocks_struct *) private; 436 437 lb->total++; 438 if (blockcnt >= 0) { 439 /* 440 * See if we can add on to the existing range (if it exists) 441 */ 442 if (lb->first_block && 443 (lb->last_block+1 == *blocknr) && 444 (lb->last_bcnt+1 == blockcnt)) { 445 lb->last_block = *blocknr; 446 lb->last_bcnt = blockcnt; 447 return 0; 448 } 449 /* 450 * Start a new range. 451 */ 452 finish_range(lb); 453 lb->first_block = lb->last_block = *blocknr; 454 lb->first_bcnt = lb->last_bcnt = blockcnt; 455 return 0; 456 } 457 /* 458 * Not a normal block. Always force a new range. 459 */ 460 finish_range(lb); 461 if (lb->first) 462 lb->first = 0; 463 else 464 fprintf(lb->f, ", "); 465 if (blockcnt == -1) 466 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr); 467 else if (blockcnt == -2) 468 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr); 469 else if (blockcnt == -3) 470 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr); 471 return 0; 472} 473 474static void dump_xattr_string(FILE *out, const char *str, int len) 475{ 476 int printable = 0; 477 int i; 478 479 /* check: is string "printable enough?" */ 480 for (i = 0; i < len; i++) 481 if (isprint(str[i])) 482 printable++; 483 484 if (printable <= len*7/8) 485 printable = 0; 486 487 for (i = 0; i < len; i++) 488 if (printable) 489 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o", 490 (unsigned char)str[i]); 491 else 492 fprintf(out, "%02x ", (unsigned char)str[i]); 493} 494 495static void internal_dump_inode_extra(FILE *out, 496 const char *prefix EXT2FS_ATTR((unused)), 497 ext2_ino_t inode_num EXT2FS_ATTR((unused)), 498 struct ext2_inode_large *inode) 499{ 500 struct ext2_ext_attr_entry *entry; 501 __u32 *magic; 502 char *start, *end; 503 unsigned int storage_size; 504 505 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize); 506 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) - 507 EXT2_GOOD_OLD_INODE_SIZE) { 508 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n", 509 inode->i_extra_isize); 510 return; 511 } 512 storage_size = EXT2_INODE_SIZE(current_fs->super) - 513 EXT2_GOOD_OLD_INODE_SIZE - 514 inode->i_extra_isize; 515 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE + 516 inode->i_extra_isize); 517 if (*magic == EXT2_EXT_ATTR_MAGIC) { 518 fprintf(out, "Extended attributes stored in inode body: \n"); 519 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super); 520 start = (char *) magic + sizeof(__u32); 521 entry = (struct ext2_ext_attr_entry *) start; 522 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) { 523 struct ext2_ext_attr_entry *next = 524 EXT2_EXT_ATTR_NEXT(entry); 525 if (entry->e_value_size > storage_size || 526 (char *) next >= end) { 527 fprintf(out, "invalid EA entry in inode\n"); 528 return; 529 } 530 fprintf(out, " "); 531 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry), 532 entry->e_name_len); 533 fprintf(out, " = \""); 534 dump_xattr_string(out, start + entry->e_value_offs, 535 entry->e_value_size); 536 fprintf(out, "\" (%u)\n", entry->e_value_size); 537 entry = next; 538 } 539 } 540} 541 542static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode) 543{ 544 struct list_blocks_struct lb; 545 546 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix); 547 lb.total = 0; 548 lb.first_block = 0; 549 lb.f = f; 550 lb.first = 1; 551 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, 552 list_blocks_proc, (void *)&lb); 553 finish_range(&lb); 554 if (lb.total) 555 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total); 556 fprintf(f,"\n"); 557} 558 559static int int_log10(unsigned long long arg) 560{ 561 int l = 0; 562 563 arg = arg / 10; 564 while (arg) { 565 l++; 566 arg = arg / 10; 567 } 568 return l; 569} 570 571#define DUMP_LEAF_EXTENTS 0x01 572#define DUMP_NODE_EXTENTS 0x02 573#define DUMP_EXTENT_TABLE 0x04 574 575static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino, 576 int flags, int logical_width, int physical_width) 577{ 578 ext2_extent_handle_t handle; 579 struct ext2fs_extent extent; 580 struct ext2_extent_info info; 581 int op = EXT2_EXTENT_ROOT; 582 unsigned int printed = 0; 583 errcode_t errcode; 584 585 errcode = ext2fs_extent_open(current_fs, ino, &handle); 586 if (errcode) 587 return; 588 589 if (flags & DUMP_EXTENT_TABLE) 590 fprintf(f, "Level Entries %*s %*s Length Flags\n", 591 (logical_width*2)+3, "Logical", 592 (physical_width*2)+3, "Physical"); 593 else 594 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix); 595 596 while (1) { 597 errcode = ext2fs_extent_get(handle, op, &extent); 598 599 if (errcode) 600 break; 601 602 op = EXT2_EXTENT_NEXT; 603 604 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) 605 continue; 606 607 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) { 608 if ((flags & DUMP_LEAF_EXTENTS) == 0) 609 continue; 610 } else { 611 if ((flags & DUMP_NODE_EXTENTS) == 0) 612 continue; 613 } 614 615 errcode = ext2fs_extent_get_info(handle, &info); 616 if (errcode) 617 continue; 618 619 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) { 620 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT) 621 continue; 622 623 if (flags & DUMP_EXTENT_TABLE) { 624 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " 625 "%*llu%*s %6u\n", 626 info.curr_level, info.max_depth, 627 info.curr_entry, info.num_entries, 628 logical_width, 629 extent.e_lblk, 630 logical_width, 631 extent.e_lblk + (extent.e_len - 1), 632 physical_width, 633 extent.e_pblk, 634 physical_width+3, "", extent.e_len); 635 continue; 636 } 637 638 fprintf(f, "%s(ETB%d):%lld", 639 printed ? ", " : "", info.curr_level, 640 extent.e_pblk); 641 printed = 1; 642 continue; 643 } 644 645 if (flags & DUMP_EXTENT_TABLE) { 646 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu " 647 "%*llu - %*llu %6u %s\n", 648 info.curr_level, info.max_depth, 649 info.curr_entry, info.num_entries, 650 logical_width, 651 extent.e_lblk, 652 logical_width, 653 extent.e_lblk + (extent.e_len - 1), 654 physical_width, 655 extent.e_pblk, 656 physical_width, 657 extent.e_pblk + (extent.e_len - 1), 658 extent.e_len, 659 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? 660 "Uninit" : ""); 661 continue; 662 } 663 664 if (extent.e_len == 0) 665 continue; 666 else if (extent.e_len == 1) 667 fprintf(f, 668 "%s(%lld%s):%lld", 669 printed ? ", " : "", 670 extent.e_lblk, 671 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? 672 "[u]" : "", 673 extent.e_pblk); 674 else 675 fprintf(f, 676 "%s(%lld-%lld%s):%lld-%lld", 677 printed ? ", " : "", 678 extent.e_lblk, 679 extent.e_lblk + (extent.e_len - 1), 680 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ? 681 "[u]" : "", 682 extent.e_pblk, 683 extent.e_pblk + (extent.e_len - 1)); 684 printed = 1; 685 } 686 if (printed) 687 fprintf(f, "\n"); 688} 689 690void internal_dump_inode(FILE *out, const char *prefix, 691 ext2_ino_t inode_num, struct ext2_inode *inode, 692 int do_dump_blocks) 693{ 694 const char *i_type; 695 char frag, fsize; 696 int os = current_fs->super->s_creator_os; 697 struct ext2_inode_large *large_inode; 698 int is_large_inode = 0; 699 700 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) 701 is_large_inode = 1; 702 large_inode = (struct ext2_inode_large *) inode; 703 704 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory"; 705 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular"; 706 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink"; 707 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special"; 708 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special"; 709 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO"; 710 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket"; 711 else i_type = "bad type"; 712 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type); 713 fprintf(out, "%sMode: %04o Flags: 0x%x\n", 714 prefix, inode->i_mode & 0777, inode->i_flags); 715 if (is_large_inode && large_inode->i_extra_isize >= 24) { 716 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n", 717 prefix, inode->i_generation, large_inode->i_version_hi, 718 inode->osd1.linux1.l_i_version); 719 } else { 720 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix, 721 inode->i_generation, inode->osd1.linux1.l_i_version); 722 } 723 fprintf(out, "%sUser: %5d Group: %5d Size: ", 724 prefix, inode_uid(*inode), inode_gid(*inode)); 725 if (LINUX_S_ISREG(inode->i_mode)) 726 fprintf(out, "%llu\n", EXT2_I_SIZE(inode)); 727 else 728 fprintf(out, "%d\n", inode->i_size); 729 if (os == EXT2_OS_HURD) 730 fprintf(out, 731 "%sFile ACL: %d Directory ACL: %d Translator: %d\n", 732 prefix, 733 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0, 734 inode->osd1.hurd1.h_i_translator); 735 else 736 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n", 737 prefix, 738 inode->i_file_acl | ((long long) 739 (inode->osd2.linux2.l_i_file_acl_high) << 32), 740 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0); 741 if (os == EXT2_OS_LINUX) 742 fprintf(out, "%sLinks: %d Blockcount: %llu\n", 743 prefix, inode->i_links_count, 744 (((unsigned long long) 745 inode->osd2.linux2.l_i_blocks_hi << 32)) + 746 inode->i_blocks); 747 else 748 fprintf(out, "%sLinks: %d Blockcount: %u\n", 749 prefix, inode->i_links_count, inode->i_blocks); 750 switch (os) { 751 case EXT2_OS_HURD: 752 frag = inode->osd2.hurd2.h_i_frag; 753 fsize = inode->osd2.hurd2.h_i_fsize; 754 break; 755 default: 756 frag = fsize = 0; 757 } 758 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n", 759 prefix, inode->i_faddr, frag, fsize); 760 if (is_large_inode && large_inode->i_extra_isize >= 24) { 761 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix, 762 inode->i_ctime, large_inode->i_ctime_extra, 763 time_to_string(inode->i_ctime)); 764 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix, 765 inode->i_atime, large_inode->i_atime_extra, 766 time_to_string(inode->i_atime)); 767 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix, 768 inode->i_mtime, large_inode->i_mtime_extra, 769 time_to_string(inode->i_mtime)); 770 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix, 771 large_inode->i_crtime, large_inode->i_crtime_extra, 772 time_to_string(large_inode->i_crtime)); 773 } else { 774 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime, 775 time_to_string(inode->i_ctime)); 776 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime, 777 time_to_string(inode->i_atime)); 778 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime, 779 time_to_string(inode->i_mtime)); 780 } 781 if (inode->i_dtime) 782 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime, 783 time_to_string(inode->i_dtime)); 784 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE) 785 internal_dump_inode_extra(out, prefix, inode_num, 786 (struct ext2_inode_large *) inode); 787 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0) 788 fprintf(out, "%sFast_link_dest: %.*s\n", prefix, 789 (int) inode->i_size, (char *)inode->i_block); 790 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) { 791 int major, minor; 792 const char *devnote; 793 794 if (inode->i_block[0]) { 795 major = (inode->i_block[0] >> 8) & 255; 796 minor = inode->i_block[0] & 255; 797 devnote = ""; 798 } else { 799 major = (inode->i_block[1] & 0xfff00) >> 8; 800 minor = ((inode->i_block[1] & 0xff) | 801 ((inode->i_block[1] >> 12) & 0xfff00)); 802 devnote = "(New-style) "; 803 } 804 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n", 805 devnote, major, minor, major, minor); 806 } else if (do_dump_blocks) { 807 if (inode->i_flags & EXT4_EXTENTS_FL) 808 dump_extents(out, prefix, inode_num, 809 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0); 810 else 811 dump_blocks(out, prefix, inode_num); 812 } 813} 814 815static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode) 816{ 817 FILE *out; 818 819 out = open_pager(); 820 internal_dump_inode(out, "", inode_num, inode, 1); 821 close_pager(out); 822} 823 824void do_stat(int argc, char *argv[]) 825{ 826 ext2_ino_t inode; 827 struct ext2_inode * inode_buf; 828 829 if (check_fs_open(argv[0])) 830 return; 831 832 inode_buf = (struct ext2_inode *) 833 malloc(EXT2_INODE_SIZE(current_fs->super)); 834 if (!inode_buf) { 835 fprintf(stderr, "do_stat: can't allocate buffer\n"); 836 return; 837 } 838 839 if (common_inode_args_process(argc, argv, &inode, 0)) { 840 free(inode_buf); 841 return; 842 } 843 844 if (debugfs_read_inode_full(inode, inode_buf, argv[0], 845 EXT2_INODE_SIZE(current_fs->super))) { 846 free(inode_buf); 847 return; 848 } 849 850 dump_inode(inode, inode_buf); 851 free(inode_buf); 852 return; 853} 854 855void do_dump_extents(int argc, char **argv) 856{ 857 struct ext2_inode inode; 858 ext2_ino_t ino; 859 FILE *out; 860 int c, flags = 0; 861 int logical_width; 862 int physical_width; 863 864 reset_getopt(); 865 while ((c = getopt(argc, argv, "nl")) != EOF) { 866 switch (c) { 867 case 'n': 868 flags |= DUMP_NODE_EXTENTS; 869 break; 870 case 'l': 871 flags |= DUMP_LEAF_EXTENTS; 872 break; 873 } 874 } 875 876 if (argc != optind + 1) { 877 com_err(0, 0, "Usage: dump_extents [-n] [-l] file"); 878 return; 879 } 880 881 if (flags == 0) 882 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS; 883 flags |= DUMP_EXTENT_TABLE; 884 885 if (check_fs_open(argv[0])) 886 return; 887 888 ino = string_to_inode(argv[optind]); 889 if (ino == 0) 890 return; 891 892 if (debugfs_read_inode(ino, &inode, argv[0])) 893 return; 894 895 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) { 896 fprintf(stderr, "%s: does not uses extent block maps\n", 897 argv[optind]); 898 return; 899 } 900 901 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/ 902 current_fs->blocksize) + 1; 903 if (logical_width < 5) 904 logical_width = 5; 905 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1; 906 if (physical_width < 5) 907 physical_width = 5; 908 909 out = open_pager(); 910 dump_extents(out, "", ino, flags, logical_width, physical_width); 911 close_pager(out); 912 return; 913} 914 915static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), 916 blk64_t *blocknr, 917 e2_blkcnt_t blockcnt, 918 blk64_t ref_block EXT2FS_ATTR((unused)), 919 int ref_offset EXT2FS_ATTR((unused)), 920 void *private EXT2FS_ATTR((unused))) 921{ 922 printf("%llu ", *blocknr); 923 return 0; 924} 925 926void do_blocks(int argc, char *argv[]) 927{ 928 ext2_ino_t inode; 929 930 if (check_fs_open(argv[0])) 931 return; 932 933 if (common_inode_args_process(argc, argv, &inode, 0)) { 934 return; 935 } 936 937 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, 938 print_blocks_proc, NULL); 939 fputc('\n', stdout); 940 return; 941} 942 943void do_chroot(int argc, char *argv[]) 944{ 945 ext2_ino_t inode; 946 int retval; 947 948 if (common_inode_args_process(argc, argv, &inode, 0)) 949 return; 950 951 retval = ext2fs_check_directory(current_fs, inode); 952 if (retval) { 953 com_err(argv[1], retval, 0); 954 return; 955 } 956 root = inode; 957} 958 959void do_clri(int argc, char *argv[]) 960{ 961 ext2_ino_t inode; 962 struct ext2_inode inode_buf; 963 964 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) 965 return; 966 967 if (debugfs_read_inode(inode, &inode_buf, argv[0])) 968 return; 969 memset(&inode_buf, 0, sizeof(inode_buf)); 970 if (debugfs_write_inode(inode, &inode_buf, argv[0])) 971 return; 972} 973 974void do_freei(int argc, char *argv[]) 975{ 976 ext2_ino_t inode; 977 978 if (common_inode_args_process(argc, argv, &inode, 979 CHECK_FS_RW | CHECK_FS_BITMAPS)) 980 return; 981 982 if (!ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) 983 com_err(argv[0], 0, "Warning: inode already clear"); 984 ext2fs_unmark_inode_bitmap2(current_fs->inode_map,inode); 985 ext2fs_mark_ib_dirty(current_fs); 986} 987 988void do_seti(int argc, char *argv[]) 989{ 990 ext2_ino_t inode; 991 992 if (common_inode_args_process(argc, argv, &inode, 993 CHECK_FS_RW | CHECK_FS_BITMAPS)) 994 return; 995 996 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) 997 com_err(argv[0], 0, "Warning: inode already set"); 998 ext2fs_mark_inode_bitmap2(current_fs->inode_map,inode); 999 ext2fs_mark_ib_dirty(current_fs); 1000} 1001 1002void do_testi(int argc, char *argv[]) 1003{ 1004 ext2_ino_t inode; 1005 1006 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS)) 1007 return; 1008 1009 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode)) 1010 printf("Inode %u is marked in use\n", inode); 1011 else 1012 printf("Inode %u is not in use\n", inode); 1013} 1014 1015void do_freeb(int argc, char *argv[]) 1016{ 1017 blk64_t block; 1018 blk64_t count = 1; 1019 1020 if (common_block_args_process(argc, argv, &block, &count)) 1021 return; 1022 if (check_fs_read_write(argv[0])) 1023 return; 1024 while (count-- > 0) { 1025 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block)) 1026 com_err(argv[0], 0, "Warning: block %llu already clear", 1027 block); 1028 ext2fs_unmark_block_bitmap2(current_fs->block_map,block); 1029 block++; 1030 } 1031 ext2fs_mark_bb_dirty(current_fs); 1032} 1033 1034void do_setb(int argc, char *argv[]) 1035{ 1036 blk64_t block; 1037 blk64_t count = 1; 1038 1039 if (common_block_args_process(argc, argv, &block, &count)) 1040 return; 1041 if (check_fs_read_write(argv[0])) 1042 return; 1043 while (count-- > 0) { 1044 if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) 1045 com_err(argv[0], 0, "Warning: block %llu already set", 1046 block); 1047 ext2fs_mark_block_bitmap2(current_fs->block_map,block); 1048 block++; 1049 } 1050 ext2fs_mark_bb_dirty(current_fs); 1051} 1052 1053void do_testb(int argc, char *argv[]) 1054{ 1055 blk64_t block; 1056 blk64_t count = 1; 1057 1058 if (common_block_args_process(argc, argv, &block, &count)) 1059 return; 1060 while (count-- > 0) { 1061 if (ext2fs_test_block_bitmap2(current_fs->block_map,block)) 1062 printf("Block %llu marked in use\n", block); 1063 else 1064 printf("Block %llu not in use\n", block); 1065 block++; 1066 } 1067} 1068 1069static void modify_u8(char *com, const char *prompt, 1070 const char *format, __u8 *val) 1071{ 1072 char buf[200]; 1073 unsigned long v; 1074 char *tmp; 1075 1076 sprintf(buf, format, *val); 1077 printf("%30s [%s] ", prompt, buf); 1078 if (!fgets(buf, sizeof(buf), stdin)) 1079 return; 1080 if (buf[strlen (buf) - 1] == '\n') 1081 buf[strlen (buf) - 1] = '\0'; 1082 if (!buf[0]) 1083 return; 1084 v = strtoul(buf, &tmp, 0); 1085 if (*tmp) 1086 com_err(com, 0, "Bad value - %s", buf); 1087 else 1088 *val = v; 1089} 1090 1091static void modify_u16(char *com, const char *prompt, 1092 const char *format, __u16 *val) 1093{ 1094 char buf[200]; 1095 unsigned long v; 1096 char *tmp; 1097 1098 sprintf(buf, format, *val); 1099 printf("%30s [%s] ", prompt, buf); 1100 if (!fgets(buf, sizeof(buf), stdin)) 1101 return; 1102 if (buf[strlen (buf) - 1] == '\n') 1103 buf[strlen (buf) - 1] = '\0'; 1104 if (!buf[0]) 1105 return; 1106 v = strtoul(buf, &tmp, 0); 1107 if (*tmp) 1108 com_err(com, 0, "Bad value - %s", buf); 1109 else 1110 *val = v; 1111} 1112 1113static void modify_u32(char *com, const char *prompt, 1114 const char *format, __u32 *val) 1115{ 1116 char buf[200]; 1117 unsigned long v; 1118 char *tmp; 1119 1120 sprintf(buf, format, *val); 1121 printf("%30s [%s] ", prompt, buf); 1122 if (!fgets(buf, sizeof(buf), stdin)) 1123 return; 1124 if (buf[strlen (buf) - 1] == '\n') 1125 buf[strlen (buf) - 1] = '\0'; 1126 if (!buf[0]) 1127 return; 1128 v = strtoul(buf, &tmp, 0); 1129 if (*tmp) 1130 com_err(com, 0, "Bad value - %s", buf); 1131 else 1132 *val = v; 1133} 1134 1135 1136void do_modify_inode(int argc, char *argv[]) 1137{ 1138 struct ext2_inode inode; 1139 ext2_ino_t inode_num; 1140 int i; 1141 unsigned char *frag, *fsize; 1142 char buf[80]; 1143 int os; 1144 const char *hex_format = "0x%x"; 1145 const char *octal_format = "0%o"; 1146 const char *decimal_format = "%d"; 1147 const char *unsignedlong_format = "%lu"; 1148 1149 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) 1150 return; 1151 1152 os = current_fs->super->s_creator_os; 1153 1154 if (debugfs_read_inode(inode_num, &inode, argv[1])) 1155 return; 1156 1157 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode); 1158 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid); 1159 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid); 1160 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size); 1161 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime); 1162 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime); 1163 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime); 1164 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime); 1165 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count); 1166 if (os == EXT2_OS_LINUX) 1167 modify_u16(argv[0], "Block count high", unsignedlong_format, 1168 &inode.osd2.linux2.l_i_blocks_hi); 1169 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks); 1170 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags); 1171 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation); 1172#if 0 1173 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1); 1174#endif 1175 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl); 1176 if (LINUX_S_ISDIR(inode.i_mode)) 1177 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl); 1178 else 1179 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high); 1180 1181 if (os == EXT2_OS_HURD) 1182 modify_u32(argv[0], "Translator Block", 1183 decimal_format, &inode.osd1.hurd1.h_i_translator); 1184 1185 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr); 1186 switch (os) { 1187 case EXT2_OS_HURD: 1188 frag = &inode.osd2.hurd2.h_i_frag; 1189 fsize = &inode.osd2.hurd2.h_i_fsize; 1190 break; 1191 default: 1192 frag = fsize = 0; 1193 } 1194 if (frag) 1195 modify_u8(argv[0], "Fragment number", decimal_format, frag); 1196 if (fsize) 1197 modify_u8(argv[0], "Fragment size", decimal_format, fsize); 1198 1199 for (i=0; i < EXT2_NDIR_BLOCKS; i++) { 1200 sprintf(buf, "Direct Block #%d", i); 1201 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]); 1202 } 1203 modify_u32(argv[0], "Indirect Block", decimal_format, 1204 &inode.i_block[EXT2_IND_BLOCK]); 1205 modify_u32(argv[0], "Double Indirect Block", decimal_format, 1206 &inode.i_block[EXT2_DIND_BLOCK]); 1207 modify_u32(argv[0], "Triple Indirect Block", decimal_format, 1208 &inode.i_block[EXT2_TIND_BLOCK]); 1209 if (debugfs_write_inode(inode_num, &inode, argv[1])) 1210 return; 1211} 1212 1213void do_change_working_dir(int argc, char *argv[]) 1214{ 1215 ext2_ino_t inode; 1216 int retval; 1217 1218 if (common_inode_args_process(argc, argv, &inode, 0)) 1219 return; 1220 1221 retval = ext2fs_check_directory(current_fs, inode); 1222 if (retval) { 1223 com_err(argv[1], retval, 0); 1224 return; 1225 } 1226 cwd = inode; 1227 return; 1228} 1229 1230void do_print_working_directory(int argc, char *argv[]) 1231{ 1232 int retval; 1233 char *pathname = NULL; 1234 1235 if (common_args_process(argc, argv, 1, 1, 1236 "print_working_directory", "", 0)) 1237 return; 1238 1239 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname); 1240 if (retval) { 1241 com_err(argv[0], retval, 1242 "while trying to get pathname of cwd"); 1243 } 1244 printf("[pwd] INODE: %6u PATH: %s\n", 1245 cwd, pathname ? pathname : "NULL"); 1246 if (pathname) { 1247 free(pathname); 1248 pathname = NULL; 1249 } 1250 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname); 1251 if (retval) { 1252 com_err(argv[0], retval, 1253 "while trying to get pathname of root"); 1254 } 1255 printf("[root] INODE: %6u PATH: %s\n", 1256 root, pathname ? pathname : "NULL"); 1257 if (pathname) { 1258 free(pathname); 1259 pathname = NULL; 1260 } 1261 return; 1262} 1263 1264/* 1265 * Given a mode, return the ext2 file type 1266 */ 1267static int ext2_file_type(unsigned int mode) 1268{ 1269 if (LINUX_S_ISREG(mode)) 1270 return EXT2_FT_REG_FILE; 1271 1272 if (LINUX_S_ISDIR(mode)) 1273 return EXT2_FT_DIR; 1274 1275 if (LINUX_S_ISCHR(mode)) 1276 return EXT2_FT_CHRDEV; 1277 1278 if (LINUX_S_ISBLK(mode)) 1279 return EXT2_FT_BLKDEV; 1280 1281 if (LINUX_S_ISLNK(mode)) 1282 return EXT2_FT_SYMLINK; 1283 1284 if (LINUX_S_ISFIFO(mode)) 1285 return EXT2_FT_FIFO; 1286 1287 if (LINUX_S_ISSOCK(mode)) 1288 return EXT2_FT_SOCK; 1289 1290 return 0; 1291} 1292 1293static void make_link(char *sourcename, char *destname) 1294{ 1295 ext2_ino_t ino; 1296 struct ext2_inode inode; 1297 int retval; 1298 ext2_ino_t dir; 1299 char *dest, *cp, *base_name; 1300 1301 /* 1302 * Get the source inode 1303 */ 1304 ino = string_to_inode(sourcename); 1305 if (!ino) 1306 return; 1307 base_name = strrchr(sourcename, '/'); 1308 if (base_name) 1309 base_name++; 1310 else 1311 base_name = sourcename; 1312 /* 1313 * Figure out the destination. First see if it exists and is 1314 * a directory. 1315 */ 1316 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir))) 1317 dest = base_name; 1318 else { 1319 /* 1320 * OK, it doesn't exist. See if it is 1321 * '<dir>/basename' or 'basename' 1322 */ 1323 cp = strrchr(destname, '/'); 1324 if (cp) { 1325 *cp = 0; 1326 dir = string_to_inode(destname); 1327 if (!dir) 1328 return; 1329 dest = cp+1; 1330 } else { 1331 dir = cwd; 1332 dest = destname; 1333 } 1334 } 1335 1336 if (debugfs_read_inode(ino, &inode, sourcename)) 1337 return; 1338 1339 retval = ext2fs_link(current_fs, dir, dest, ino, 1340 ext2_file_type(inode.i_mode)); 1341 if (retval) 1342 com_err("make_link", retval, 0); 1343 return; 1344} 1345 1346 1347void do_link(int argc, char *argv[]) 1348{ 1349 if (common_args_process(argc, argv, 3, 3, "link", 1350 "<source file> <dest_name>", CHECK_FS_RW)) 1351 return; 1352 1353 make_link(argv[1], argv[2]); 1354} 1355 1356static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr, 1357 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 1358 blk64_t ref_block EXT2FS_ATTR((unused)), 1359 int ref_offset EXT2FS_ATTR((unused)), 1360 void *private EXT2FS_ATTR((unused))) 1361{ 1362 blk64_t block; 1363 1364 block = *blocknr; 1365 ext2fs_block_alloc_stats2(fs, block, +1); 1366 return 0; 1367} 1368 1369void do_undel(int argc, char *argv[]) 1370{ 1371 ext2_ino_t ino; 1372 struct ext2_inode inode; 1373 1374 if (common_args_process(argc, argv, 2, 3, "undelete", 1375 "<inode_num> [dest_name]", 1376 CHECK_FS_RW | CHECK_FS_BITMAPS)) 1377 return; 1378 1379 ino = string_to_inode(argv[1]); 1380 if (!ino) 1381 return; 1382 1383 if (debugfs_read_inode(ino, &inode, argv[1])) 1384 return; 1385 1386 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) { 1387 com_err(argv[1], 0, "Inode is not marked as deleted"); 1388 return; 1389 } 1390 1391 /* 1392 * XXX this function doesn't handle changing the links count on the 1393 * parent directory when undeleting a directory. 1394 */ 1395 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1; 1396 inode.i_dtime = 0; 1397 1398 if (debugfs_write_inode(ino, &inode, argv[0])) 1399 return; 1400 1401 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL, 1402 mark_blocks_proc, NULL); 1403 1404 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0); 1405 1406 if (argc > 2) 1407 make_link(argv[1], argv[2]); 1408} 1409 1410static void unlink_file_by_name(char *filename) 1411{ 1412 int retval; 1413 ext2_ino_t dir; 1414 char *base_name; 1415 1416 base_name = strrchr(filename, '/'); 1417 if (base_name) { 1418 *base_name++ = '\0'; 1419 dir = string_to_inode(filename); 1420 if (!dir) 1421 return; 1422 } else { 1423 dir = cwd; 1424 base_name = filename; 1425 } 1426 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0); 1427 if (retval) 1428 com_err("unlink_file_by_name", retval, 0); 1429 return; 1430} 1431 1432void do_unlink(int argc, char *argv[]) 1433{ 1434 if (common_args_process(argc, argv, 2, 2, "link", 1435 "<pathname>", CHECK_FS_RW)) 1436 return; 1437 1438 unlink_file_by_name(argv[1]); 1439} 1440 1441void do_find_free_block(int argc, char *argv[]) 1442{ 1443 blk64_t free_blk, goal, first_free = 0; 1444 int count; 1445 errcode_t retval; 1446 char *tmp; 1447 1448 if ((argc > 3) || (argc==2 && *argv[1] == '?')) { 1449 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]"); 1450 return; 1451 } 1452 if (check_fs_open(argv[0])) 1453 return; 1454 1455 if (argc > 1) { 1456 count = strtol(argv[1],&tmp,0); 1457 if (*tmp) { 1458 com_err(argv[0], 0, "Bad count - %s", argv[1]); 1459 return; 1460 } 1461 } else 1462 count = 1; 1463 1464 if (argc > 2) { 1465 goal = strtol(argv[2], &tmp, 0); 1466 if (*tmp) { 1467 com_err(argv[0], 0, "Bad goal - %s", argv[1]); 1468 return; 1469 } 1470 } 1471 else 1472 goal = current_fs->super->s_first_data_block; 1473 1474 printf("Free blocks found: "); 1475 free_blk = goal - 1; 1476 while (count-- > 0) { 1477 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0, 1478 &free_blk); 1479 if (first_free) { 1480 if (first_free == free_blk) 1481 break; 1482 } else 1483 first_free = free_blk; 1484 if (retval) { 1485 com_err("ext2fs_new_block", retval, 0); 1486 return; 1487 } else 1488 printf("%llu ", free_blk); 1489 } 1490 printf("\n"); 1491} 1492 1493void do_find_free_inode(int argc, char *argv[]) 1494{ 1495 ext2_ino_t free_inode, dir; 1496 int mode; 1497 int retval; 1498 char *tmp; 1499 1500 if (argc > 3 || (argc>1 && *argv[1] == '?')) { 1501 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]"); 1502 return; 1503 } 1504 if (check_fs_open(argv[0])) 1505 return; 1506 1507 if (argc > 1) { 1508 dir = strtol(argv[1], &tmp, 0); 1509 if (*tmp) { 1510 com_err(argv[0], 0, "Bad dir - %s", argv[1]); 1511 return; 1512 } 1513 } 1514 else 1515 dir = root; 1516 if (argc > 2) { 1517 mode = strtol(argv[2], &tmp, 0); 1518 if (*tmp) { 1519 com_err(argv[0], 0, "Bad mode - %s", argv[2]); 1520 return; 1521 } 1522 } else 1523 mode = 010755; 1524 1525 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode); 1526 if (retval) 1527 com_err("ext2fs_new_inode", retval, 0); 1528 else 1529 printf("Free inode found: %u\n", free_inode); 1530} 1531 1532static errcode_t copy_file(int fd, ext2_ino_t newfile) 1533{ 1534 ext2_file_t e2_file; 1535 errcode_t retval; 1536 int got; 1537 unsigned int written; 1538 char buf[8192]; 1539 char *ptr; 1540 1541 retval = ext2fs_file_open(current_fs, newfile, 1542 EXT2_FILE_WRITE, &e2_file); 1543 if (retval) 1544 return retval; 1545 1546 while (1) { 1547 got = read(fd, buf, sizeof(buf)); 1548 if (got == 0) 1549 break; 1550 if (got < 0) { 1551 retval = errno; 1552 goto fail; 1553 } 1554 ptr = buf; 1555 while (got > 0) { 1556 retval = ext2fs_file_write(e2_file, ptr, 1557 got, &written); 1558 if (retval) 1559 goto fail; 1560 1561 got -= written; 1562 ptr += written; 1563 } 1564 } 1565 retval = ext2fs_file_close(e2_file); 1566 return retval; 1567 1568fail: 1569 (void) ext2fs_file_close(e2_file); 1570 return retval; 1571} 1572 1573 1574void do_write(int argc, char *argv[]) 1575{ 1576 int fd; 1577 struct stat statbuf; 1578 ext2_ino_t newfile; 1579 errcode_t retval; 1580 struct ext2_inode inode; 1581 1582 if (common_args_process(argc, argv, 3, 3, "write", 1583 "<native file> <new file>", CHECK_FS_RW)) 1584 return; 1585 1586 fd = open(argv[1], O_RDONLY); 1587 if (fd < 0) { 1588 com_err(argv[1], errno, 0); 1589 return; 1590 } 1591 if (fstat(fd, &statbuf) < 0) { 1592 com_err(argv[1], errno, 0); 1593 close(fd); 1594 return; 1595 } 1596 1597 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile); 1598 if (retval == 0) { 1599 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]); 1600 close(fd); 1601 return; 1602 } 1603 1604 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); 1605 if (retval) { 1606 com_err(argv[0], retval, 0); 1607 close(fd); 1608 return; 1609 } 1610 printf("Allocated inode: %u\n", newfile); 1611 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 1612 EXT2_FT_REG_FILE); 1613 if (retval == EXT2_ET_DIR_NO_SPACE) { 1614 retval = ext2fs_expand_dir(current_fs, cwd); 1615 if (retval) { 1616 com_err(argv[0], retval, "while expanding directory"); 1617 close(fd); 1618 return; 1619 } 1620 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 1621 EXT2_FT_REG_FILE); 1622 } 1623 if (retval) { 1624 com_err(argv[2], retval, 0); 1625 close(fd); 1626 return; 1627 } 1628 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile)) 1629 com_err(argv[0], 0, "Warning: inode already set"); 1630 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0); 1631 memset(&inode, 0, sizeof(inode)); 1632 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG; 1633 inode.i_atime = inode.i_ctime = inode.i_mtime = 1634 current_fs->now ? current_fs->now : time(0); 1635 inode.i_links_count = 1; 1636 inode.i_size = statbuf.st_size; 1637 if (current_fs->super->s_feature_incompat & 1638 EXT3_FEATURE_INCOMPAT_EXTENTS) 1639 inode.i_flags |= EXT4_EXTENTS_FL; 1640 if (debugfs_write_new_inode(newfile, &inode, argv[0])) { 1641 close(fd); 1642 return; 1643 } 1644 if (LINUX_S_ISREG(inode.i_mode)) { 1645 retval = copy_file(fd, newfile); 1646 if (retval) 1647 com_err("copy_file", retval, 0); 1648 } 1649 close(fd); 1650} 1651 1652void do_mknod(int argc, char *argv[]) 1653{ 1654 unsigned long mode, major, minor; 1655 ext2_ino_t newfile; 1656 errcode_t retval; 1657 struct ext2_inode inode; 1658 int filetype, nr; 1659 1660 if (check_fs_open(argv[0])) 1661 return; 1662 if (argc < 3 || argv[2][1]) { 1663 usage: 1664 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]"); 1665 return; 1666 } 1667 mode = minor = major = 0; 1668 switch (argv[2][0]) { 1669 case 'p': 1670 mode = LINUX_S_IFIFO; 1671 filetype = EXT2_FT_FIFO; 1672 nr = 3; 1673 break; 1674 case 'c': 1675 mode = LINUX_S_IFCHR; 1676 filetype = EXT2_FT_CHRDEV; 1677 nr = 5; 1678 break; 1679 case 'b': 1680 mode = LINUX_S_IFBLK; 1681 filetype = EXT2_FT_BLKDEV; 1682 nr = 5; 1683 break; 1684 default: 1685 filetype = 0; 1686 nr = 0; 1687 } 1688 if (nr == 5) { 1689 major = strtoul(argv[3], argv+3, 0); 1690 minor = strtoul(argv[4], argv+4, 0); 1691 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0]) 1692 nr = 0; 1693 } 1694 if (argc != nr) 1695 goto usage; 1696 if (check_fs_read_write(argv[0])) 1697 return; 1698 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile); 1699 if (retval) { 1700 com_err(argv[0], retval, 0); 1701 return; 1702 } 1703 printf("Allocated inode: %u\n", newfile); 1704 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype); 1705 if (retval == EXT2_ET_DIR_NO_SPACE) { 1706 retval = ext2fs_expand_dir(current_fs, cwd); 1707 if (retval) { 1708 com_err(argv[0], retval, "while expanding directory"); 1709 return; 1710 } 1711 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 1712 filetype); 1713 } 1714 if (retval) { 1715 com_err(argv[1], retval, 0); 1716 return; 1717 } 1718 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile)) 1719 com_err(argv[0], 0, "Warning: inode already set"); 1720 ext2fs_mark_inode_bitmap2(current_fs->inode_map, newfile); 1721 ext2fs_mark_ib_dirty(current_fs); 1722 memset(&inode, 0, sizeof(inode)); 1723 inode.i_mode = mode; 1724 inode.i_atime = inode.i_ctime = inode.i_mtime = 1725 current_fs->now ? current_fs->now : time(0); 1726 if ((major < 256) && (minor < 256)) { 1727 inode.i_block[0] = major*256+minor; 1728 inode.i_block[1] = 0; 1729 } else { 1730 inode.i_block[0] = 0; 1731 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12); 1732 } 1733 inode.i_links_count = 1; 1734 if (debugfs_write_new_inode(newfile, &inode, argv[0])) 1735 return; 1736} 1737 1738void do_mkdir(int argc, char *argv[]) 1739{ 1740 char *cp; 1741 ext2_ino_t parent; 1742 char *name; 1743 errcode_t retval; 1744 1745 if (common_args_process(argc, argv, 2, 2, "mkdir", 1746 "<filename>", CHECK_FS_RW)) 1747 return; 1748 1749 cp = strrchr(argv[1], '/'); 1750 if (cp) { 1751 *cp = 0; 1752 parent = string_to_inode(argv[1]); 1753 if (!parent) { 1754 com_err(argv[1], ENOENT, 0); 1755 return; 1756 } 1757 name = cp+1; 1758 } else { 1759 parent = cwd; 1760 name = argv[1]; 1761 } 1762 1763try_again: 1764 retval = ext2fs_mkdir(current_fs, parent, 0, name); 1765 if (retval == EXT2_ET_DIR_NO_SPACE) { 1766 retval = ext2fs_expand_dir(current_fs, parent); 1767 if (retval) { 1768 com_err(argv[0], retval, "while expanding directory"); 1769 return; 1770 } 1771 goto try_again; 1772 } 1773 if (retval) { 1774 com_err("ext2fs_mkdir", retval, 0); 1775 return; 1776 } 1777 1778} 1779 1780static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr, 1781 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)), 1782 blk64_t ref_block EXT2FS_ATTR((unused)), 1783 int ref_offset EXT2FS_ATTR((unused)), 1784 void *private EXT2FS_ATTR((unused))) 1785{ 1786 blk64_t block; 1787 1788 block = *blocknr; 1789 ext2fs_block_alloc_stats2(fs, block, -1); 1790 return 0; 1791} 1792 1793static void kill_file_by_inode(ext2_ino_t inode) 1794{ 1795 struct ext2_inode inode_buf; 1796 1797 if (debugfs_read_inode(inode, &inode_buf, 0)) 1798 return; 1799 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0); 1800 if (debugfs_write_inode(inode, &inode_buf, 0)) 1801 return; 1802 if (!ext2fs_inode_has_valid_blocks(&inode_buf)) 1803 return; 1804 1805 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL, 1806 release_blocks_proc, NULL); 1807 printf("\n"); 1808 ext2fs_inode_alloc_stats2(current_fs, inode, -1, 1809 LINUX_S_ISDIR(inode_buf.i_mode)); 1810} 1811 1812 1813void do_kill_file(int argc, char *argv[]) 1814{ 1815 ext2_ino_t inode_num; 1816 1817 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW)) 1818 return; 1819 1820 kill_file_by_inode(inode_num); 1821} 1822 1823void do_rm(int argc, char *argv[]) 1824{ 1825 int retval; 1826 ext2_ino_t inode_num; 1827 struct ext2_inode inode; 1828 1829 if (common_args_process(argc, argv, 2, 2, "rm", 1830 "<filename>", CHECK_FS_RW)) 1831 return; 1832 1833 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); 1834 if (retval) { 1835 com_err(argv[0], retval, "while trying to resolve filename"); 1836 return; 1837 } 1838 1839 if (debugfs_read_inode(inode_num, &inode, argv[0])) 1840 return; 1841 1842 if (LINUX_S_ISDIR(inode.i_mode)) { 1843 com_err(argv[0], 0, "file is a directory"); 1844 return; 1845 } 1846 1847 --inode.i_links_count; 1848 if (debugfs_write_inode(inode_num, &inode, argv[0])) 1849 return; 1850 1851 unlink_file_by_name(argv[1]); 1852 if (inode.i_links_count == 0) 1853 kill_file_by_inode(inode_num); 1854} 1855 1856struct rd_struct { 1857 ext2_ino_t parent; 1858 int empty; 1859}; 1860 1861static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)), 1862 int entry EXT2FS_ATTR((unused)), 1863 struct ext2_dir_entry *dirent, 1864 int offset EXT2FS_ATTR((unused)), 1865 int blocksize EXT2FS_ATTR((unused)), 1866 char *buf EXT2FS_ATTR((unused)), 1867 void *private) 1868{ 1869 struct rd_struct *rds = (struct rd_struct *) private; 1870 1871 if (dirent->inode == 0) 1872 return 0; 1873 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.')) 1874 return 0; 1875 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') && 1876 (dirent->name[1] == '.')) { 1877 rds->parent = dirent->inode; 1878 return 0; 1879 } 1880 rds->empty = 0; 1881 return 0; 1882} 1883 1884void do_rmdir(int argc, char *argv[]) 1885{ 1886 int retval; 1887 ext2_ino_t inode_num; 1888 struct ext2_inode inode; 1889 struct rd_struct rds; 1890 1891 if (common_args_process(argc, argv, 2, 2, "rmdir", 1892 "<filename>", CHECK_FS_RW)) 1893 return; 1894 1895 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num); 1896 if (retval) { 1897 com_err(argv[0], retval, "while trying to resolve filename"); 1898 return; 1899 } 1900 1901 if (debugfs_read_inode(inode_num, &inode, argv[0])) 1902 return; 1903 1904 if (!LINUX_S_ISDIR(inode.i_mode)) { 1905 com_err(argv[0], 0, "file is not a directory"); 1906 return; 1907 } 1908 1909 rds.parent = 0; 1910 rds.empty = 1; 1911 1912 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0, 1913 0, rmdir_proc, &rds); 1914 if (retval) { 1915 com_err(argv[0], retval, "while iterating over directory"); 1916 return; 1917 } 1918 if (rds.empty == 0) { 1919 com_err(argv[0], 0, "directory not empty"); 1920 return; 1921 } 1922 1923 inode.i_links_count = 0; 1924 if (debugfs_write_inode(inode_num, &inode, argv[0])) 1925 return; 1926 1927 unlink_file_by_name(argv[1]); 1928 kill_file_by_inode(inode_num); 1929 1930 if (rds.parent) { 1931 if (debugfs_read_inode(rds.parent, &inode, argv[0])) 1932 return; 1933 if (inode.i_links_count > 1) 1934 inode.i_links_count--; 1935 if (debugfs_write_inode(rds.parent, &inode, argv[0])) 1936 return; 1937 } 1938} 1939 1940void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)), 1941 char *argv[] EXT2FS_ATTR((unused))) 1942{ 1943 FILE *out = stdout; 1944 1945 if (current_fs) 1946 fprintf(out, "Open mode: read-%s\n", 1947 current_fs->flags & EXT2_FLAG_RW ? "write" : "only"); 1948 fprintf(out, "Filesystem in use: %s\n", 1949 current_fs ? current_fs->device_name : "--none--"); 1950} 1951 1952void do_expand_dir(int argc, char *argv[]) 1953{ 1954 ext2_ino_t inode; 1955 int retval; 1956 1957 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW)) 1958 return; 1959 1960 retval = ext2fs_expand_dir(current_fs, inode); 1961 if (retval) 1962 com_err("ext2fs_expand_dir", retval, 0); 1963 return; 1964} 1965 1966void do_features(int argc, char *argv[]) 1967{ 1968 int i; 1969 1970 if (check_fs_open(argv[0])) 1971 return; 1972 1973 if ((argc != 1) && check_fs_read_write(argv[0])) 1974 return; 1975 for (i=1; i < argc; i++) { 1976 if (e2p_edit_feature(argv[i], 1977 ¤t_fs->super->s_feature_compat, 0)) 1978 com_err(argv[0], 0, "Unknown feature: %s\n", 1979 argv[i]); 1980 else 1981 ext2fs_mark_super_dirty(current_fs); 1982 } 1983 print_features(current_fs->super, stdout); 1984} 1985 1986void do_bmap(int argc, char *argv[]) 1987{ 1988 ext2_ino_t ino; 1989 blk64_t blk, pblk; 1990 int err; 1991 errcode_t errcode; 1992 1993 if (common_args_process(argc, argv, 3, 3, argv[0], 1994 "<file> logical_blk", 0)) 1995 return; 1996 1997 ino = string_to_inode(argv[1]); 1998 if (!ino) 1999 return; 2000 blk = parse_ulong(argv[2], argv[0], "logical_block", &err); 2001 2002 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk); 2003 if (errcode) { 2004 com_err("argv[0]", errcode, 2005 "while mapping logical block %llu\n", blk); 2006 return; 2007 } 2008 printf("%llu\n", pblk); 2009} 2010 2011void do_imap(int argc, char *argv[]) 2012{ 2013 ext2_ino_t ino; 2014 unsigned long group, block, block_nr, offset; 2015 2016 if (common_args_process(argc, argv, 2, 2, argv[0], 2017 "<file>", 0)) 2018 return; 2019 ino = string_to_inode(argv[1]); 2020 if (!ino) 2021 return; 2022 2023 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super); 2024 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) * 2025 EXT2_INODE_SIZE(current_fs->super); 2026 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super); 2027 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) { 2028 com_err(argv[0], 0, "Inode table for group %lu is missing\n", 2029 group); 2030 return; 2031 } 2032 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) + 2033 block; 2034 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1); 2035 2036 printf("Inode %d is part of block group %lu\n" 2037 "\tlocated at block %lu, offset 0x%04lx\n", ino, group, 2038 block_nr, offset); 2039 2040} 2041 2042void do_set_current_time(int argc, char *argv[]) 2043{ 2044 time_t now; 2045 2046 if (common_args_process(argc, argv, 2, 2, argv[0], 2047 "<time>", 0)) 2048 return; 2049 2050 now = string_to_time(argv[1]); 2051 if (now == ((time_t) -1)) { 2052 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n", 2053 argv[1]); 2054 return; 2055 2056 } else { 2057 printf("Setting current time to %s\n", time_to_string(now)); 2058 current_fs->now = now; 2059 } 2060} 2061 2062static int find_supp_feature(__u32 *supp, int feature_type, char *name) 2063{ 2064 int compat, bit, ret; 2065 unsigned int feature_mask; 2066 2067 if (name) { 2068 if (feature_type == E2P_FS_FEATURE) 2069 ret = e2p_string2feature(name, &compat, &feature_mask); 2070 else 2071 ret = e2p_jrnl_string2feature(name, &compat, 2072 &feature_mask); 2073 if (ret) 2074 return ret; 2075 2076 if (!(supp[compat] & feature_mask)) 2077 return 1; 2078 } else { 2079 for (compat = 0; compat < 3; compat++) { 2080 for (bit = 0, feature_mask = 1; bit < 32; 2081 bit++, feature_mask <<= 1) { 2082 if (supp[compat] & feature_mask) { 2083 if (feature_type == E2P_FS_FEATURE) 2084 fprintf(stdout, " %s", 2085 e2p_feature2string(compat, 2086 feature_mask)); 2087 else 2088 fprintf(stdout, " %s", 2089 e2p_jrnl_feature2string(compat, 2090 feature_mask)); 2091 } 2092 } 2093 } 2094 fprintf(stdout, "\n"); 2095 } 2096 2097 return 0; 2098} 2099 2100void do_supported_features(int argc, char *argv[]) 2101{ 2102 int ret; 2103 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP, 2104 EXT2_LIB_FEATURE_INCOMPAT_SUPP, 2105 EXT2_LIB_FEATURE_RO_COMPAT_SUPP }; 2106 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES, 2107 JFS_KNOWN_INCOMPAT_FEATURES, 2108 JFS_KNOWN_ROCOMPAT_FEATURES }; 2109 2110 if (argc > 1) { 2111 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]); 2112 if (ret) { 2113 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, 2114 argv[1]); 2115 } 2116 if (ret) 2117 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]); 2118 else 2119 fprintf(stdout, "Supported feature: %s\n", argv[1]); 2120 } else { 2121 fprintf(stdout, "Supported features:"); 2122 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL); 2123 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL); 2124 } 2125} 2126 2127void do_punch(int argc, char *argv[]) 2128{ 2129 ext2_ino_t ino; 2130 blk64_t start, end; 2131 int err; 2132 errcode_t errcode; 2133 2134 if (common_args_process(argc, argv, 3, 4, argv[0], 2135 "<file> start_blk [end_blk]", 2136 CHECK_FS_RW | CHECK_FS_BITMAPS)) 2137 return; 2138 2139 ino = string_to_inode(argv[1]); 2140 if (!ino) 2141 return; 2142 start = parse_ulong(argv[2], argv[0], "logical_block", &err); 2143 if (argc == 4) 2144 end = parse_ulong(argv[3], argv[0], "logical_block", &err); 2145 else 2146 end = ~0; 2147 2148 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end); 2149 2150 if (errcode) { 2151 com_err(argv[0], errcode, 2152 "while truncating inode %u from %llu to %llu\n", ino, 2153 (unsigned long long) start, (unsigned long long) end); 2154 return; 2155 } 2156} 2157 2158static int source_file(const char *cmd_file, int sci_idx) 2159{ 2160 FILE *f; 2161 char buf[256]; 2162 char *cp; 2163 int exit_status = 0; 2164 int retval; 2165 2166 if (strcmp(cmd_file, "-") == 0) 2167 f = stdin; 2168 else { 2169 f = fopen(cmd_file, "r"); 2170 if (!f) { 2171 perror(cmd_file); 2172 exit(1); 2173 } 2174 } 2175 fflush(stdout); 2176 fflush(stderr); 2177 setbuf(stdout, NULL); 2178 setbuf(stderr, NULL); 2179 while (!feof(f)) { 2180 if (fgets(buf, sizeof(buf), f) == NULL) 2181 break; 2182 cp = strchr(buf, '\n'); 2183 if (cp) 2184 *cp = 0; 2185 cp = strchr(buf, '\r'); 2186 if (cp) 2187 *cp = 0; 2188 printf("debugfs: %s\n", buf); 2189 retval = ss_execute_line(sci_idx, buf); 2190 if (retval) { 2191 ss_perror(sci_idx, retval, buf); 2192 exit_status++; 2193 } 2194 } 2195 if (f != stdin) 2196 fclose(f); 2197 return exit_status; 2198} 2199 2200int main(int argc, char **argv) 2201{ 2202 int retval; 2203 int sci_idx; 2204 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]"; 2205 int c; 2206 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; 2207 char *request = 0; 2208 int exit_status = 0; 2209 char *cmd_file = 0; 2210 blk64_t superblock = 0; 2211 blk64_t blocksize = 0; 2212 int catastrophic = 0; 2213 char *data_filename = 0; 2214 2215 if (debug_prog_name == 0) 2216 debug_prog_name = "debugfs"; 2217 2218 add_error_table(&et_ext2_error_table); 2219 fprintf (stderr, "%s %s (%s)\n", debug_prog_name, 2220 E2FSPROGS_VERSION, E2FSPROGS_DATE); 2221 2222 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:D")) != EOF) { 2223 switch (c) { 2224 case 'R': 2225 request = optarg; 2226 break; 2227 case 'f': 2228 cmd_file = optarg; 2229 break; 2230 case 'd': 2231 data_filename = optarg; 2232 break; 2233 case 'i': 2234 open_flags |= EXT2_FLAG_IMAGE_FILE; 2235 break; 2236 case 'w': 2237 open_flags |= EXT2_FLAG_RW; 2238 break; 2239 case 'D': 2240 open_flags |= EXT2_FLAG_DIRECT_IO; 2241 break; 2242 case 'b': 2243 blocksize = parse_ulong(optarg, argv[0], 2244 "block size", 0); 2245 break; 2246 case 's': 2247 superblock = parse_ulong(optarg, argv[0], 2248 "superblock number", 0); 2249 break; 2250 case 'c': 2251 catastrophic = 1; 2252 break; 2253 case 'V': 2254 /* Print version number and exit */ 2255 fprintf(stderr, "\tUsing %s\n", 2256 error_message(EXT2_ET_BASE)); 2257 exit(0); 2258 default: 2259 com_err(argv[0], 0, usage, debug_prog_name); 2260 return 1; 2261 } 2262 } 2263 if (optind < argc) 2264 open_filesystem(argv[optind], open_flags, 2265 superblock, blocksize, catastrophic, 2266 data_filename); 2267 2268 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL, 2269 &debug_cmds, &retval); 2270 if (retval) { 2271 ss_perror(sci_idx, retval, "creating invocation"); 2272 exit(1); 2273 } 2274 ss_get_readline(sci_idx); 2275 2276 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval); 2277 if (retval) { 2278 ss_perror(sci_idx, retval, "adding standard requests"); 2279 exit (1); 2280 } 2281 if (extra_cmds) 2282 ss_add_request_table (sci_idx, extra_cmds, 1, &retval); 2283 if (retval) { 2284 ss_perror(sci_idx, retval, "adding extra requests"); 2285 exit (1); 2286 } 2287 if (request) { 2288 retval = 0; 2289 retval = ss_execute_line(sci_idx, request); 2290 if (retval) { 2291 ss_perror(sci_idx, retval, request); 2292 exit_status++; 2293 } 2294 } else if (cmd_file) { 2295 exit_status = source_file(cmd_file, sci_idx); 2296 } else { 2297 ss_listen(sci_idx); 2298 } 2299 2300 ss_delete_invocation(sci_idx); 2301 2302 if (current_fs) 2303 close_filesystem(); 2304 2305 remove_error_table(&et_ext2_error_table); 2306 return exit_status; 2307} 2308