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