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