]> 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_fast_link(FILE *out, ext2_ino_t inode_num,
782 struct ext2_inode *inode, const char *prefix)
783 {
784 errcode_t retval = 0;
785 char *buf;
786 size_t size;
787
788 if (inode->i_flags & EXT4_INLINE_DATA_FL) {
789 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
790 if (retval)
791 goto out;
792
793 retval = ext2fs_get_memzero(size + 1, &buf);
794 if (retval)
795 goto out;
796
797 retval = ext2fs_inline_data_get(current_fs, inode_num,
798 inode, buf, &size);
799 if (retval)
800 goto out;
801 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
802 (int)size, buf);
803
804 retval = ext2fs_free_mem(&buf);
805 if (retval)
806 goto out;
807 } else {
808 size_t sz = EXT2_I_SIZE(inode);
809
810 if (sz > sizeof(inode->i_block))
811 sz = sizeof(inode->i_block);
812 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix, (int) sz,
813 (char *)inode->i_block);
814 }
815 out:
816 if (retval)
817 com_err(__func__, retval, "while dumping link destination");
818 }
819
820 void internal_dump_inode(FILE *out, const char *prefix,
821 ext2_ino_t inode_num, struct ext2_inode *inode,
822 int do_dump_blocks)
823 {
824 const char *i_type;
825 char frag, fsize;
826 int os = current_fs->super->s_creator_os;
827 struct ext2_inode_large *large_inode;
828 int is_large_inode = 0;
829
830 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
831 is_large_inode = 1;
832 large_inode = (struct ext2_inode_large *) inode;
833
834 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
835 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
836 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
837 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
838 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
839 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
840 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
841 else i_type = "bad type";
842 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
843 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
844 prefix, inode->i_mode & 0777, inode->i_flags);
845 if (is_large_inode && large_inode->i_extra_isize >= 24) {
846 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
847 prefix, inode->i_generation, large_inode->i_version_hi,
848 inode->osd1.linux1.l_i_version);
849 } else {
850 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
851 inode->i_generation, inode->osd1.linux1.l_i_version);
852 }
853 fprintf(out, "%sUser: %5d Group: %5d",
854 prefix, inode_uid(*inode), inode_gid(*inode));
855 if (is_large_inode && large_inode->i_extra_isize >= 32)
856 fprintf(out, " Project: %5d", large_inode->i_projid);
857 fputs(" Size: ", out);
858 if (LINUX_S_ISREG(inode->i_mode))
859 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
860 else
861 fprintf(out, "%d\n", inode->i_size);
862 if (os == EXT2_OS_HURD)
863 fprintf(out,
864 "%sFile ACL: %d Translator: %d\n",
865 prefix,
866 inode->i_file_acl,
867 inode->osd1.hurd1.h_i_translator);
868 else
869 fprintf(out, "%sFile ACL: %llu\n",
870 prefix,
871 inode->i_file_acl | ((long long)
872 (inode->osd2.linux2.l_i_file_acl_high) << 32));
873 if (os != EXT2_OS_HURD)
874 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
875 prefix, inode->i_links_count,
876 (((unsigned long long)
877 inode->osd2.linux2.l_i_blocks_hi << 32)) +
878 inode->i_blocks);
879 else
880 fprintf(out, "%sLinks: %d Blockcount: %u\n",
881 prefix, inode->i_links_count, inode->i_blocks);
882 switch (os) {
883 case EXT2_OS_HURD:
884 frag = inode->osd2.hurd2.h_i_frag;
885 fsize = inode->osd2.hurd2.h_i_fsize;
886 break;
887 default:
888 frag = fsize = 0;
889 }
890 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
891 prefix, inode->i_faddr, frag, fsize);
892 if (is_large_inode && large_inode->i_extra_isize >= 24) {
893 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
894 inode->i_ctime, large_inode->i_ctime_extra,
895 inode_time_to_string(inode->i_ctime,
896 large_inode->i_ctime_extra));
897 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
898 inode->i_atime, large_inode->i_atime_extra,
899 inode_time_to_string(inode->i_atime,
900 large_inode->i_atime_extra));
901 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
902 inode->i_mtime, large_inode->i_mtime_extra,
903 inode_time_to_string(inode->i_mtime,
904 large_inode->i_mtime_extra));
905 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
906 large_inode->i_crtime, large_inode->i_crtime_extra,
907 inode_time_to_string(large_inode->i_crtime,
908 large_inode->i_crtime_extra));
909 if (inode->i_dtime)
910 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
911 large_inode->i_dtime, large_inode->i_ctime_extra,
912 inode_time_to_string(inode->i_dtime,
913 large_inode->i_ctime_extra));
914 } else {
915 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
916 time_to_string((__s32) inode->i_ctime));
917 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
918 time_to_string((__s32) inode->i_atime));
919 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
920 time_to_string((__s32) inode->i_mtime));
921 if (inode->i_dtime)
922 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
923 inode->i_dtime,
924 time_to_string((__s32) inode->i_dtime));
925 }
926 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
927 internal_dump_inode_extra(out, prefix, inode_num,
928 (struct ext2_inode_large *) inode);
929 dump_inode_attributes(out, inode_num);
930 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
931 __u32 crc = inode->i_checksum_lo;
932 if (is_large_inode &&
933 large_inode->i_extra_isize >=
934 (offsetof(struct ext2_inode_large,
935 i_checksum_hi) -
936 EXT2_GOOD_OLD_INODE_SIZE))
937 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
938 fprintf(out, "Inode checksum: 0x%08x\n", crc);
939 }
940
941 if (LINUX_S_ISLNK(inode->i_mode) &&
942 ext2fs_inode_data_blocks(current_fs, inode) == 0)
943 dump_fast_link(out, inode_num, inode, prefix);
944 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
945 int major, minor;
946 const char *devnote;
947
948 if (inode->i_block[0]) {
949 major = (inode->i_block[0] >> 8) & 255;
950 minor = inode->i_block[0] & 255;
951 devnote = "";
952 } else {
953 major = (inode->i_block[1] & 0xfff00) >> 8;
954 minor = ((inode->i_block[1] & 0xff) |
955 ((inode->i_block[1] >> 12) & 0xfff00));
956 devnote = "(New-style) ";
957 }
958 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
959 devnote, major, minor, major, minor);
960 } else if (do_dump_blocks) {
961 if (inode->i_flags & EXT4_EXTENTS_FL)
962 dump_extents(out, prefix, inode_num,
963 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
964 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
965 dump_inline_data(out, prefix, inode_num);
966 else
967 dump_blocks(out, prefix, inode_num);
968 }
969 }
970
971 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
972 {
973 FILE *out;
974
975 out = open_pager();
976 internal_dump_inode(out, "", inode_num, inode, 1);
977 close_pager(out);
978 }
979
980 void do_stat(int argc, char *argv[])
981 {
982 ext2_ino_t inode;
983 struct ext2_inode * inode_buf;
984
985 if (check_fs_open(argv[0]))
986 return;
987
988 inode_buf = (struct ext2_inode *)
989 malloc(EXT2_INODE_SIZE(current_fs->super));
990 if (!inode_buf) {
991 fprintf(stderr, "do_stat: can't allocate buffer\n");
992 return;
993 }
994
995 if (common_inode_args_process(argc, argv, &inode, 0)) {
996 free(inode_buf);
997 return;
998 }
999
1000 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
1001 EXT2_INODE_SIZE(current_fs->super))) {
1002 free(inode_buf);
1003 return;
1004 }
1005
1006 dump_inode(inode, inode_buf);
1007 free(inode_buf);
1008 return;
1009 }
1010
1011 void do_dump_extents(int argc, char **argv)
1012 {
1013 struct ext2_inode inode;
1014 ext2_ino_t ino;
1015 FILE *out;
1016 int c, flags = 0;
1017 int logical_width;
1018 int physical_width;
1019
1020 reset_getopt();
1021 while ((c = getopt(argc, argv, "nl")) != EOF) {
1022 switch (c) {
1023 case 'n':
1024 flags |= DUMP_NODE_EXTENTS;
1025 break;
1026 case 'l':
1027 flags |= DUMP_LEAF_EXTENTS;
1028 break;
1029 }
1030 }
1031
1032 if (argc != optind + 1) {
1033 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1034 return;
1035 }
1036
1037 if (flags == 0)
1038 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1039 flags |= DUMP_EXTENT_TABLE;
1040
1041 if (check_fs_open(argv[0]))
1042 return;
1043
1044 ino = string_to_inode(argv[optind]);
1045 if (ino == 0)
1046 return;
1047
1048 if (debugfs_read_inode(ino, &inode, argv[0]))
1049 return;
1050
1051 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1052 fprintf(stderr, "%s: does not uses extent block maps\n",
1053 argv[optind]);
1054 return;
1055 }
1056
1057 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1058 current_fs->blocksize) + 1;
1059 if (logical_width < 5)
1060 logical_width = 5;
1061 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1062 if (physical_width < 5)
1063 physical_width = 5;
1064
1065 out = open_pager();
1066 dump_extents(out, "", ino, flags, logical_width, physical_width);
1067 close_pager(out);
1068 return;
1069 }
1070
1071 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1072 blk64_t *blocknr,
1073 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1074 blk64_t ref_block EXT2FS_ATTR((unused)),
1075 int ref_offset EXT2FS_ATTR((unused)),
1076 void *private EXT2FS_ATTR((unused)))
1077 {
1078 printf("%llu ", *blocknr);
1079 return 0;
1080 }
1081
1082 void do_blocks(int argc, char *argv[])
1083 {
1084 ext2_ino_t inode;
1085
1086 if (check_fs_open(argv[0]))
1087 return;
1088
1089 if (common_inode_args_process(argc, argv, &inode, 0)) {
1090 return;
1091 }
1092
1093 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1094 print_blocks_proc, NULL);
1095 fputc('\n', stdout);
1096 return;
1097 }
1098
1099 void do_chroot(int argc, char *argv[])
1100 {
1101 ext2_ino_t inode;
1102 int retval;
1103
1104 if (common_inode_args_process(argc, argv, &inode, 0))
1105 return;
1106
1107 retval = ext2fs_check_directory(current_fs, inode);
1108 if (retval) {
1109 com_err(argv[1], retval, 0);
1110 return;
1111 }
1112 root = inode;
1113 }
1114
1115 #ifndef READ_ONLY
1116 void do_clri(int argc, char *argv[])
1117 {
1118 ext2_ino_t inode;
1119 struct ext2_inode inode_buf;
1120
1121 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1122 return;
1123
1124 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1125 return;
1126 memset(&inode_buf, 0, sizeof(inode_buf));
1127 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1128 return;
1129 }
1130
1131 void do_freei(int argc, char *argv[])
1132 {
1133 unsigned int len = 1;
1134 int err = 0;
1135 ext2_ino_t inode;
1136
1137 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1138 CHECK_FS_RW | CHECK_FS_BITMAPS))
1139 return;
1140 if (check_fs_read_write(argv[0]))
1141 return;
1142
1143 inode = string_to_inode(argv[1]);
1144 if (!inode)
1145 return;
1146
1147 if (argc == 3) {
1148 len = parse_ulong(argv[2], argv[0], "length", &err);
1149 if (err)
1150 return;
1151 }
1152
1153 if (len == 1 &&
1154 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1155 com_err(argv[0], 0, "Warning: inode already clear");
1156 while (len-- > 0)
1157 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1158 ext2fs_mark_ib_dirty(current_fs);
1159 }
1160
1161 void do_seti(int argc, char *argv[])
1162 {
1163 unsigned int len = 1;
1164 int err = 0;
1165 ext2_ino_t inode;
1166
1167 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1168 CHECK_FS_RW | CHECK_FS_BITMAPS))
1169 return;
1170 if (check_fs_read_write(argv[0]))
1171 return;
1172
1173 inode = string_to_inode(argv[1]);
1174 if (!inode)
1175 return;
1176
1177 if (argc == 3) {
1178 len = parse_ulong(argv[2], argv[0], "length", &err);
1179 if (err)
1180 return;
1181 }
1182
1183 if ((len == 1) &&
1184 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1185 com_err(argv[0], 0, "Warning: inode already set");
1186 while (len-- > 0)
1187 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1188 ext2fs_mark_ib_dirty(current_fs);
1189 }
1190 #endif /* READ_ONLY */
1191
1192 void do_testi(int argc, char *argv[])
1193 {
1194 ext2_ino_t inode;
1195
1196 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1197 return;
1198
1199 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1200 printf("Inode %u is marked in use\n", inode);
1201 else
1202 printf("Inode %u is not in use\n", inode);
1203 }
1204
1205 #ifndef READ_ONLY
1206 void do_freeb(int argc, char *argv[])
1207 {
1208 blk64_t block;
1209 blk64_t count = 1;
1210
1211 if (common_block_args_process(argc, argv, &block, &count))
1212 return;
1213 if (check_fs_read_write(argv[0]))
1214 return;
1215 while (count-- > 0) {
1216 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1217 com_err(argv[0], 0, "Warning: block %llu already clear",
1218 block);
1219 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1220 block++;
1221 }
1222 ext2fs_mark_bb_dirty(current_fs);
1223 }
1224
1225 void do_setb(int argc, char *argv[])
1226 {
1227 blk64_t block;
1228 blk64_t count = 1;
1229
1230 if (common_block_args_process(argc, argv, &block, &count))
1231 return;
1232 if (check_fs_read_write(argv[0]))
1233 return;
1234 while (count-- > 0) {
1235 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1236 com_err(argv[0], 0, "Warning: block %llu already set",
1237 block);
1238 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1239 block++;
1240 }
1241 ext2fs_mark_bb_dirty(current_fs);
1242 }
1243 #endif /* READ_ONLY */
1244
1245 void do_testb(int argc, char *argv[])
1246 {
1247 blk64_t block;
1248 blk64_t count = 1;
1249
1250 if (common_block_args_process(argc, argv, &block, &count))
1251 return;
1252 while (count-- > 0) {
1253 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1254 printf("Block %llu marked in use\n", block);
1255 else
1256 printf("Block %llu not in use\n", block);
1257 block++;
1258 }
1259 }
1260
1261 #ifndef READ_ONLY
1262 static void modify_u8(char *com, const char *prompt,
1263 const char *format, __u8 *val)
1264 {
1265 char buf[200];
1266 unsigned long v;
1267 char *tmp;
1268
1269 sprintf(buf, format, *val);
1270 printf("%30s [%s] ", prompt, buf);
1271 if (!fgets(buf, sizeof(buf), stdin))
1272 return;
1273 if (buf[strlen (buf) - 1] == '\n')
1274 buf[strlen (buf) - 1] = '\0';
1275 if (!buf[0])
1276 return;
1277 v = strtoul(buf, &tmp, 0);
1278 if (*tmp)
1279 com_err(com, 0, "Bad value - %s", buf);
1280 else
1281 *val = v;
1282 }
1283
1284 static void modify_u16(char *com, const char *prompt,
1285 const char *format, __u16 *val)
1286 {
1287 char buf[200];
1288 unsigned long v;
1289 char *tmp;
1290
1291 sprintf(buf, format, *val);
1292 printf("%30s [%s] ", prompt, buf);
1293 if (!fgets(buf, sizeof(buf), stdin))
1294 return;
1295 if (buf[strlen (buf) - 1] == '\n')
1296 buf[strlen (buf) - 1] = '\0';
1297 if (!buf[0])
1298 return;
1299 v = strtoul(buf, &tmp, 0);
1300 if (*tmp)
1301 com_err(com, 0, "Bad value - %s", buf);
1302 else
1303 *val = v;
1304 }
1305
1306 static void modify_u32(char *com, const char *prompt,
1307 const char *format, __u32 *val)
1308 {
1309 char buf[200];
1310 unsigned long v;
1311 char *tmp;
1312
1313 sprintf(buf, format, *val);
1314 printf("%30s [%s] ", prompt, buf);
1315 if (!fgets(buf, sizeof(buf), stdin))
1316 return;
1317 if (buf[strlen (buf) - 1] == '\n')
1318 buf[strlen (buf) - 1] = '\0';
1319 if (!buf[0])
1320 return;
1321 v = strtoul(buf, &tmp, 0);
1322 if (*tmp)
1323 com_err(com, 0, "Bad value - %s", buf);
1324 else
1325 *val = v;
1326 }
1327
1328
1329 void do_modify_inode(int argc, char *argv[])
1330 {
1331 struct ext2_inode inode;
1332 ext2_ino_t inode_num;
1333 int i;
1334 unsigned char *frag, *fsize;
1335 char buf[80];
1336 int os;
1337 const char *hex_format = "0x%x";
1338 const char *octal_format = "0%o";
1339 const char *decimal_format = "%d";
1340 const char *unsignedlong_format = "%lu";
1341
1342 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1343 return;
1344
1345 os = current_fs->super->s_creator_os;
1346
1347 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1348 return;
1349
1350 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1351 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1352 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1353 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1354 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1355 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1356 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1357 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1358 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1359 if (os == EXT2_OS_LINUX)
1360 modify_u16(argv[0], "Block count high", unsignedlong_format,
1361 &inode.osd2.linux2.l_i_blocks_hi);
1362 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1363 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1364 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1365 #if 0
1366 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1367 #endif
1368 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1369
1370 modify_u32(argv[0], "High 32bits of size", decimal_format,
1371 &inode.i_size_high);
1372
1373 if (os == EXT2_OS_HURD)
1374 modify_u32(argv[0], "Translator Block",
1375 decimal_format, &inode.osd1.hurd1.h_i_translator);
1376
1377 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1378 switch (os) {
1379 case EXT2_OS_HURD:
1380 frag = &inode.osd2.hurd2.h_i_frag;
1381 fsize = &inode.osd2.hurd2.h_i_fsize;
1382 break;
1383 default:
1384 frag = fsize = 0;
1385 }
1386 if (frag)
1387 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1388 if (fsize)
1389 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1390
1391 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1392 sprintf(buf, "Direct Block #%d", i);
1393 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1394 }
1395 modify_u32(argv[0], "Indirect Block", decimal_format,
1396 &inode.i_block[EXT2_IND_BLOCK]);
1397 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1398 &inode.i_block[EXT2_DIND_BLOCK]);
1399 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1400 &inode.i_block[EXT2_TIND_BLOCK]);
1401 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1402 return;
1403 }
1404 #endif /* READ_ONLY */
1405
1406 void do_change_working_dir(int argc, char *argv[])
1407 {
1408 ext2_ino_t inode;
1409 int retval;
1410
1411 if (common_inode_args_process(argc, argv, &inode, 0))
1412 return;
1413
1414 retval = ext2fs_check_directory(current_fs, inode);
1415 if (retval) {
1416 com_err(argv[1], retval, 0);
1417 return;
1418 }
1419 cwd = inode;
1420 return;
1421 }
1422
1423 void do_print_working_directory(int argc, char *argv[])
1424 {
1425 int retval;
1426 char *pathname = NULL;
1427
1428 if (common_args_process(argc, argv, 1, 1,
1429 "print_working_directory", "", 0))
1430 return;
1431
1432 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1433 if (retval) {
1434 com_err(argv[0], retval,
1435 "while trying to get pathname of cwd");
1436 }
1437 printf("[pwd] INODE: %6u PATH: %s\n",
1438 cwd, pathname ? pathname : "NULL");
1439 if (pathname) {
1440 free(pathname);
1441 pathname = NULL;
1442 }
1443 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1444 if (retval) {
1445 com_err(argv[0], retval,
1446 "while trying to get pathname of root");
1447 }
1448 printf("[root] INODE: %6u PATH: %s\n",
1449 root, pathname ? pathname : "NULL");
1450 if (pathname) {
1451 free(pathname);
1452 pathname = NULL;
1453 }
1454 return;
1455 }
1456
1457 #ifndef READ_ONLY
1458 static void make_link(char *sourcename, char *destname)
1459 {
1460 ext2_ino_t ino;
1461 struct ext2_inode inode;
1462 int retval;
1463 ext2_ino_t dir;
1464 char *dest, *cp, *base_name;
1465
1466 /*
1467 * Get the source inode
1468 */
1469 ino = string_to_inode(sourcename);
1470 if (!ino)
1471 return;
1472 base_name = strrchr(sourcename, '/');
1473 if (base_name)
1474 base_name++;
1475 else
1476 base_name = sourcename;
1477 /*
1478 * Figure out the destination. First see if it exists and is
1479 * a directory.
1480 */
1481 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1482 dest = base_name;
1483 else {
1484 /*
1485 * OK, it doesn't exist. See if it is
1486 * '<dir>/basename' or 'basename'
1487 */
1488 cp = strrchr(destname, '/');
1489 if (cp) {
1490 *cp = 0;
1491 dir = string_to_inode(destname);
1492 if (!dir)
1493 return;
1494 dest = cp+1;
1495 } else {
1496 dir = cwd;
1497 dest = destname;
1498 }
1499 }
1500
1501 if (debugfs_read_inode(ino, &inode, sourcename))
1502 return;
1503
1504 retval = ext2fs_link(current_fs, dir, dest, ino,
1505 ext2_file_type(inode.i_mode));
1506 if (retval)
1507 com_err("make_link", retval, 0);
1508 return;
1509 }
1510
1511
1512 void do_link(int argc, char *argv[])
1513 {
1514 if (common_args_process(argc, argv, 3, 3, "link",
1515 "<source file> <dest_name>", CHECK_FS_RW))
1516 return;
1517
1518 make_link(argv[1], argv[2]);
1519 }
1520
1521 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1522 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1523 blk64_t ref_block EXT2FS_ATTR((unused)),
1524 int ref_offset EXT2FS_ATTR((unused)),
1525 void *private EXT2FS_ATTR((unused)))
1526 {
1527 blk64_t block;
1528
1529 block = *blocknr;
1530 ext2fs_block_alloc_stats2(fs, block, +1);
1531 return 0;
1532 }
1533
1534 void do_undel(int argc, char *argv[])
1535 {
1536 ext2_ino_t ino;
1537 struct ext2_inode inode;
1538
1539 if (common_args_process(argc, argv, 2, 3, "undelete",
1540 "<inode_num> [dest_name]",
1541 CHECK_FS_RW | CHECK_FS_BITMAPS))
1542 return;
1543
1544 ino = string_to_inode(argv[1]);
1545 if (!ino)
1546 return;
1547
1548 if (debugfs_read_inode(ino, &inode, argv[1]))
1549 return;
1550
1551 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1552 com_err(argv[1], 0, "Inode is not marked as deleted");
1553 return;
1554 }
1555
1556 /*
1557 * XXX this function doesn't handle changing the links count on the
1558 * parent directory when undeleting a directory.
1559 */
1560 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1561 inode.i_dtime = 0;
1562
1563 if (debugfs_write_inode(ino, &inode, argv[0]))
1564 return;
1565
1566 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1567 mark_blocks_proc, NULL);
1568
1569 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1570
1571 if (argc > 2)
1572 make_link(argv[1], argv[2]);
1573 }
1574
1575 static void unlink_file_by_name(char *filename)
1576 {
1577 int retval;
1578 ext2_ino_t dir;
1579 char *base_name;
1580
1581 base_name = strrchr(filename, '/');
1582 if (base_name) {
1583 *base_name++ = '\0';
1584 dir = string_to_inode(filename);
1585 if (!dir)
1586 return;
1587 } else {
1588 dir = cwd;
1589 base_name = filename;
1590 }
1591 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1592 if (retval)
1593 com_err("unlink_file_by_name", retval, 0);
1594 return;
1595 }
1596
1597 void do_unlink(int argc, char *argv[])
1598 {
1599 if (common_args_process(argc, argv, 2, 2, "link",
1600 "<pathname>", CHECK_FS_RW))
1601 return;
1602
1603 unlink_file_by_name(argv[1]);
1604 }
1605
1606 void do_copy_inode(int argc, char *argv[])
1607 {
1608 ext2_ino_t src_ino, dest_ino;
1609 unsigned char buf[4096];
1610
1611 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1612 "<source file> <dest_name>", CHECK_FS_RW))
1613 return;
1614
1615 src_ino = string_to_inode(argv[1]);
1616 if (!src_ino)
1617 return;
1618
1619 dest_ino = string_to_inode(argv[2]);
1620 if (!dest_ino)
1621 return;
1622
1623 if (debugfs_read_inode_full(src_ino, (struct ext2_inode *) buf,
1624 argv[0], sizeof(buf)))
1625 return;
1626
1627 if (debugfs_write_inode_full(dest_ino, (struct ext2_inode *) buf,
1628 argv[0], sizeof(buf)))
1629 return;
1630 }
1631
1632 #endif /* READ_ONLY */
1633
1634 void do_find_free_block(int argc, char *argv[])
1635 {
1636 blk64_t free_blk, goal, first_free = 0;
1637 int count;
1638 errcode_t retval;
1639 char *tmp;
1640
1641 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1642 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1643 return;
1644 }
1645 if (check_fs_open(argv[0]))
1646 return;
1647
1648 if (argc > 1) {
1649 count = strtol(argv[1],&tmp,0);
1650 if (*tmp) {
1651 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1652 return;
1653 }
1654 } else
1655 count = 1;
1656
1657 if (argc > 2) {
1658 goal = strtol(argv[2], &tmp, 0);
1659 if (*tmp) {
1660 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1661 return;
1662 }
1663 }
1664 else
1665 goal = current_fs->super->s_first_data_block;
1666
1667 printf("Free blocks found: ");
1668 free_blk = goal - 1;
1669 while (count-- > 0) {
1670 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1671 &free_blk);
1672 if (first_free) {
1673 if (first_free == free_blk)
1674 break;
1675 } else
1676 first_free = free_blk;
1677 if (retval) {
1678 com_err("ext2fs_new_block", retval, 0);
1679 return;
1680 } else
1681 printf("%llu ", free_blk);
1682 }
1683 printf("\n");
1684 }
1685
1686 void do_find_free_inode(int argc, char *argv[])
1687 {
1688 ext2_ino_t free_inode, dir;
1689 int mode;
1690 int retval;
1691 char *tmp;
1692
1693 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1694 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1695 return;
1696 }
1697 if (check_fs_open(argv[0]))
1698 return;
1699
1700 if (argc > 1) {
1701 dir = strtol(argv[1], &tmp, 0);
1702 if (*tmp) {
1703 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1704 return;
1705 }
1706 }
1707 else
1708 dir = root;
1709 if (argc > 2) {
1710 mode = strtol(argv[2], &tmp, 0);
1711 if (*tmp) {
1712 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1713 return;
1714 }
1715 } else
1716 mode = 010755;
1717
1718 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1719 if (retval)
1720 com_err("ext2fs_new_inode", retval, 0);
1721 else
1722 printf("Free inode found: %u\n", free_inode);
1723 }
1724
1725 #ifndef READ_ONLY
1726 void do_write(int argc, char *argv[])
1727 {
1728 errcode_t retval;
1729
1730 if (common_args_process(argc, argv, 3, 3, "write",
1731 "<native file> <new file>", CHECK_FS_RW))
1732 return;
1733
1734 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1735 if (retval)
1736 com_err(argv[0], retval, 0);
1737 }
1738
1739 void do_mknod(int argc, char *argv[])
1740 {
1741 unsigned long major, minor;
1742 errcode_t retval;
1743 int nr;
1744 struct stat st;
1745
1746 if (check_fs_open(argv[0]))
1747 return;
1748 if (argc < 3 || argv[2][1]) {
1749 usage:
1750 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1751 return;
1752 }
1753
1754 minor = major = 0;
1755 switch (argv[2][0]) {
1756 case 'p':
1757 st.st_mode = S_IFIFO;
1758 nr = 3;
1759 break;
1760 case 'c':
1761 st.st_mode = S_IFCHR;
1762 nr = 5;
1763 break;
1764 case 'b':
1765 st.st_mode = S_IFBLK;
1766 nr = 5;
1767 break;
1768 default:
1769 nr = 0;
1770 }
1771
1772 if (nr == 5) {
1773 major = strtoul(argv[3], argv+3, 0);
1774 minor = strtoul(argv[4], argv+4, 0);
1775 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1776 nr = 0;
1777 }
1778
1779 if (argc != nr)
1780 goto usage;
1781
1782 st.st_rdev = makedev(major, minor);
1783 retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1784 if (retval)
1785 com_err(argv[0], retval, 0);
1786 }
1787
1788 void do_mkdir(int argc, char *argv[])
1789 {
1790 errcode_t retval;
1791
1792 if (common_args_process(argc, argv, 2, 2, "mkdir",
1793 "<filename>", CHECK_FS_RW))
1794 return;
1795
1796 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1797 if (retval)
1798 com_err(argv[0], retval, 0);
1799
1800 }
1801
1802 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1803 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1804 blk64_t ref_block EXT2FS_ATTR((unused)),
1805 int ref_offset EXT2FS_ATTR((unused)),
1806 void *private EXT2FS_ATTR((unused)))
1807 {
1808 blk64_t block;
1809
1810 block = *blocknr;
1811 ext2fs_block_alloc_stats2(fs, block, -1);
1812 return 0;
1813 }
1814
1815 static void kill_file_by_inode(ext2_ino_t inode)
1816 {
1817 struct ext2_inode inode_buf;
1818
1819 if (debugfs_read_inode(inode, &inode_buf, 0))
1820 return;
1821 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1822 if (debugfs_write_inode(inode, &inode_buf, 0))
1823 return;
1824 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1825 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1826 NULL, release_blocks_proc, NULL);
1827 }
1828 printf("\n");
1829 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1830 LINUX_S_ISDIR(inode_buf.i_mode));
1831 }
1832
1833
1834 void do_kill_file(int argc, char *argv[])
1835 {
1836 ext2_ino_t inode_num;
1837
1838 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1839 return;
1840
1841 kill_file_by_inode(inode_num);
1842 }
1843
1844 void do_rm(int argc, char *argv[])
1845 {
1846 int retval;
1847 ext2_ino_t inode_num;
1848 struct ext2_inode inode;
1849
1850 if (common_args_process(argc, argv, 2, 2, "rm",
1851 "<filename>", CHECK_FS_RW))
1852 return;
1853
1854 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1855 if (retval) {
1856 com_err(argv[0], retval, "while trying to resolve filename");
1857 return;
1858 }
1859
1860 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1861 return;
1862
1863 if (LINUX_S_ISDIR(inode.i_mode)) {
1864 com_err(argv[0], 0, "file is a directory");
1865 return;
1866 }
1867
1868 --inode.i_links_count;
1869 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1870 return;
1871
1872 unlink_file_by_name(argv[1]);
1873 if (inode.i_links_count == 0)
1874 kill_file_by_inode(inode_num);
1875 }
1876
1877 struct rd_struct {
1878 ext2_ino_t parent;
1879 int empty;
1880 };
1881
1882 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1883 int entry EXT2FS_ATTR((unused)),
1884 struct ext2_dir_entry *dirent,
1885 int offset EXT2FS_ATTR((unused)),
1886 int blocksize EXT2FS_ATTR((unused)),
1887 char *buf EXT2FS_ATTR((unused)),
1888 void *private)
1889 {
1890 struct rd_struct *rds = (struct rd_struct *) private;
1891
1892 if (dirent->inode == 0)
1893 return 0;
1894 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1895 return 0;
1896 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1897 (dirent->name[1] == '.')) {
1898 rds->parent = dirent->inode;
1899 return 0;
1900 }
1901 rds->empty = 0;
1902 return 0;
1903 }
1904
1905 void do_rmdir(int argc, char *argv[])
1906 {
1907 int retval;
1908 ext2_ino_t inode_num;
1909 struct ext2_inode inode;
1910 struct rd_struct rds;
1911
1912 if (common_args_process(argc, argv, 2, 2, "rmdir",
1913 "<filename>", CHECK_FS_RW))
1914 return;
1915
1916 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1917 if (retval) {
1918 com_err(argv[0], retval, "while trying to resolve filename");
1919 return;
1920 }
1921
1922 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1923 return;
1924
1925 if (!LINUX_S_ISDIR(inode.i_mode)) {
1926 com_err(argv[0], 0, "file is not a directory");
1927 return;
1928 }
1929
1930 rds.parent = 0;
1931 rds.empty = 1;
1932
1933 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1934 0, rmdir_proc, &rds);
1935 if (retval) {
1936 com_err(argv[0], retval, "while iterating over directory");
1937 return;
1938 }
1939 if (rds.empty == 0) {
1940 com_err(argv[0], 0, "directory not empty");
1941 return;
1942 }
1943
1944 inode.i_links_count = 0;
1945 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1946 return;
1947
1948 unlink_file_by_name(argv[1]);
1949 kill_file_by_inode(inode_num);
1950
1951 if (rds.parent) {
1952 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1953 return;
1954 if (inode.i_links_count > 1)
1955 inode.i_links_count--;
1956 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1957 return;
1958 }
1959 }
1960 #endif /* READ_ONLY */
1961
1962 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1963 char *argv[] EXT2FS_ATTR((unused)))
1964 {
1965 if (current_fs)
1966 printf("Open mode: read-%s\n",
1967 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1968 printf("Filesystem in use: %s\n",
1969 current_fs ? current_fs->device_name : "--none--");
1970 }
1971
1972 #ifndef READ_ONLY
1973 void do_expand_dir(int argc, char *argv[])
1974 {
1975 ext2_ino_t inode;
1976 int retval;
1977
1978 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1979 return;
1980
1981 retval = ext2fs_expand_dir(current_fs, inode);
1982 if (retval)
1983 com_err("ext2fs_expand_dir", retval, 0);
1984 return;
1985 }
1986
1987 void do_features(int argc, char *argv[])
1988 {
1989 int i;
1990
1991 if (check_fs_open(argv[0]))
1992 return;
1993
1994 if ((argc != 1) && check_fs_read_write(argv[0]))
1995 return;
1996 for (i=1; i < argc; i++) {
1997 if (e2p_edit_feature(argv[i],
1998 &current_fs->super->s_feature_compat, 0))
1999 com_err(argv[0], 0, "Unknown feature: %s\n",
2000 argv[i]);
2001 else
2002 ext2fs_mark_super_dirty(current_fs);
2003 }
2004 print_features(current_fs->super, stdout);
2005 }
2006 #endif /* READ_ONLY */
2007
2008 void do_bmap(int argc, char *argv[])
2009 {
2010 ext2_ino_t ino;
2011 blk64_t blk, pblk = 0;
2012 int c, err, flags = 0, ret_flags = 0;
2013 errcode_t errcode;
2014
2015 if (check_fs_open(argv[0]))
2016 return;
2017
2018 reset_getopt();
2019 while ((c = getopt (argc, argv, "a")) != EOF) {
2020 switch (c) {
2021 case 'a':
2022 flags |= BMAP_ALLOC;
2023 break;
2024 default:
2025 goto print_usage;
2026 }
2027 }
2028
2029 if (argc <= optind+1) {
2030 print_usage:
2031 com_err(0, 0,
2032 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2033 return;
2034 }
2035
2036 ino = string_to_inode(argv[optind++]);
2037 if (!ino)
2038 return;
2039 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2040 if (err)
2041 return;
2042
2043 if (argc > optind+1)
2044 goto print_usage;
2045
2046 if (argc == optind+1) {
2047 err = strtoblk(argv[0], argv[optind++],
2048 "physical block", &pblk);
2049 if (err)
2050 return;
2051 if (flags & BMAP_ALLOC) {
2052 com_err(0, 0, "Can't set and allocate a block");
2053 return;
2054 }
2055 flags |= BMAP_SET;
2056 }
2057
2058 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2059 &ret_flags, &pblk);
2060 if (errcode) {
2061 com_err(argv[0], errcode,
2062 "while mapping logical block %llu\n", blk);
2063 return;
2064 }
2065 printf("%llu", pblk);
2066 if (ret_flags & BMAP_RET_UNINIT)
2067 fputs(" (uninit)", stdout);
2068 fputc('\n', stdout);
2069 }
2070
2071 void do_imap(int argc, char *argv[])
2072 {
2073 ext2_ino_t ino;
2074 unsigned long group, block, block_nr, offset;
2075
2076 if (common_args_process(argc, argv, 2, 2, argv[0],
2077 "<file>", 0))
2078 return;
2079 ino = string_to_inode(argv[1]);
2080 if (!ino)
2081 return;
2082
2083 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2084 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2085 EXT2_INODE_SIZE(current_fs->super);
2086 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2087 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2088 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2089 group);
2090 return;
2091 }
2092 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2093 block;
2094 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2095
2096 printf("Inode %d is part of block group %lu\n"
2097 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2098 block_nr, offset);
2099
2100 }
2101
2102 void do_idump(int argc, char *argv[])
2103 {
2104 ext2_ino_t ino;
2105 unsigned char *buf;
2106 errcode_t err;
2107 int isize;
2108
2109 if (common_args_process(argc, argv, 2, 2, argv[0],
2110 "<file>", 0))
2111 return;
2112 ino = string_to_inode(argv[1]);
2113 if (!ino)
2114 return;
2115
2116 isize = EXT2_INODE_SIZE(current_fs->super);
2117 err = ext2fs_get_mem(isize, &buf);
2118 if (err) {
2119 com_err(argv[0], err, "while allocating memory");
2120 return;
2121 }
2122
2123 err = ext2fs_read_inode_full(current_fs, ino,
2124 (struct ext2_inode *)buf, isize);
2125 if (err) {
2126 com_err(argv[0], err, "while reading inode %d", ino);
2127 goto err;
2128 }
2129
2130 do_byte_hexdump(stdout, buf, isize);
2131 err:
2132 ext2fs_free_mem(&buf);
2133 }
2134
2135 #ifndef READ_ONLY
2136 void do_set_current_time(int argc, char *argv[])
2137 {
2138 __s64 now;
2139
2140 if (common_args_process(argc, argv, 2, 2, argv[0],
2141 "<time>", 0))
2142 return;
2143
2144 now = string_to_time(argv[1]);
2145 if (now == -1) {
2146 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2147 argv[1]);
2148 return;
2149
2150 } else {
2151 printf("Setting current time to %s\n", time_to_string(now));
2152 current_fs->now = now;
2153 }
2154 }
2155 #endif /* READ_ONLY */
2156
2157 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2158 {
2159 int compat, bit, ret;
2160 unsigned int feature_mask;
2161
2162 if (name) {
2163 if (feature_type == E2P_FS_FEATURE)
2164 ret = e2p_string2feature(name, &compat, &feature_mask);
2165 else
2166 ret = e2p_jrnl_string2feature(name, &compat,
2167 &feature_mask);
2168 if (ret)
2169 return ret;
2170
2171 if (!(supp[compat] & feature_mask))
2172 return 1;
2173 } else {
2174 for (compat = 0; compat < 3; compat++) {
2175 for (bit = 0, feature_mask = 1; bit < 32;
2176 bit++, feature_mask <<= 1) {
2177 if (supp[compat] & feature_mask) {
2178 if (feature_type == E2P_FS_FEATURE)
2179 fprintf(stdout, " %s",
2180 e2p_feature2string(compat,
2181 feature_mask));
2182 else
2183 fprintf(stdout, " %s",
2184 e2p_jrnl_feature2string(compat,
2185 feature_mask));
2186 }
2187 }
2188 }
2189 fprintf(stdout, "\n");
2190 }
2191
2192 return 0;
2193 }
2194
2195 void do_supported_features(int argc, char *argv[])
2196 {
2197 int ret;
2198 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2199 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2200 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2201 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2202 JFS_KNOWN_INCOMPAT_FEATURES,
2203 JFS_KNOWN_ROCOMPAT_FEATURES };
2204
2205 if (argc > 1) {
2206 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2207 if (ret) {
2208 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2209 argv[1]);
2210 }
2211 if (ret)
2212 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2213 else
2214 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2215 } else {
2216 fprintf(stdout, "Supported features:");
2217 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2218 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2219 }
2220 }
2221
2222 #ifndef READ_ONLY
2223 void do_punch(int argc, char *argv[])
2224 {
2225 ext2_ino_t ino;
2226 blk64_t start, end;
2227 int err;
2228 errcode_t errcode;
2229
2230 if (common_args_process(argc, argv, 3, 4, argv[0],
2231 "<file> start_blk [end_blk]",
2232 CHECK_FS_RW | CHECK_FS_BITMAPS))
2233 return;
2234
2235 ino = string_to_inode(argv[1]);
2236 if (!ino)
2237 return;
2238 err = strtoblk(argv[0], argv[2], "logical block", &start);
2239 if (err)
2240 return;
2241 if (argc == 4) {
2242 err = strtoblk(argv[0], argv[3], "logical block", &end);
2243 if (err)
2244 return;
2245 } else
2246 end = ~0;
2247
2248 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2249
2250 if (errcode) {
2251 com_err(argv[0], errcode,
2252 "while truncating inode %u from %llu to %llu\n", ino,
2253 (unsigned long long) start, (unsigned long long) end);
2254 return;
2255 }
2256 }
2257
2258 void do_fallocate(int argc, char *argv[])
2259 {
2260 ext2_ino_t ino;
2261 blk64_t start, end;
2262 int err;
2263 errcode_t errcode;
2264
2265 if (common_args_process(argc, argv, 3, 4, argv[0],
2266 "<file> start_blk [end_blk]",
2267 CHECK_FS_RW | CHECK_FS_BITMAPS))
2268 return;
2269
2270 ino = string_to_inode(argv[1]);
2271 if (!ino)
2272 return;
2273 err = strtoblk(argv[0], argv[2], "logical block", &start);
2274 if (err)
2275 return;
2276 if (argc == 4) {
2277 err = strtoblk(argv[0], argv[3], "logical block", &end);
2278 if (err)
2279 return;
2280 } else
2281 end = ~0;
2282
2283 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2284 ino, NULL, ~0ULL, start, end - start + 1);
2285
2286 if (errcode) {
2287 com_err(argv[0], errcode,
2288 "while fallocating inode %u from %llu to %llu\n", ino,
2289 (unsigned long long) start, (unsigned long long) end);
2290 return;
2291 }
2292 }
2293 #endif /* READ_ONLY */
2294
2295 void do_symlink(int argc, char *argv[])
2296 {
2297 errcode_t retval;
2298
2299 if (common_args_process(argc, argv, 3, 3, "symlink",
2300 "<filename> <target>", CHECK_FS_RW))
2301 return;
2302
2303 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2304 if (retval)
2305 com_err(argv[0], retval, 0);
2306
2307 }
2308
2309 #if CONFIG_MMP
2310 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2311 {
2312 struct mmp_struct *mmp_s;
2313 unsigned long long mmp_block;
2314 time_t t;
2315 errcode_t retval = 0;
2316
2317 if (check_fs_open(argv[0]))
2318 return;
2319
2320 if (argc > 1) {
2321 char *end = NULL;
2322 mmp_block = strtoull(argv[1], &end, 0);
2323 if (end == argv[0] || mmp_block == 0) {
2324 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2325 argv[0], argv[1]);
2326 return;
2327 }
2328 } else {
2329 mmp_block = current_fs->super->s_mmp_block;
2330 }
2331
2332 if (mmp_block == 0) {
2333 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2334 argv[0]);
2335 return;
2336 }
2337
2338 if (current_fs->mmp_buf == NULL) {
2339 retval = ext2fs_get_mem(current_fs->blocksize,
2340 &current_fs->mmp_buf);
2341 if (retval) {
2342 com_err(argv[0], retval, "allocating MMP buffer.\n");
2343 return;
2344 }
2345 }
2346
2347 mmp_s = current_fs->mmp_buf;
2348
2349 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2350 if (retval) {
2351 com_err(argv[0], retval, "reading MMP block %llu.\n",
2352 mmp_block);
2353 return;
2354 }
2355
2356 t = mmp_s->mmp_time;
2357 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2358 fprintf(stdout, "update_interval: %d\n",
2359 current_fs->super->s_mmp_update_interval);
2360 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2361 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2362 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2363 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2364 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2365 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2366 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2367 fprintf(stdout, "MMP is unsupported, please recompile with "
2368 "--enable-mmp\n");
2369 }
2370 #else
2371 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2372 char *argv[] EXT2FS_ATTR((unused)))
2373 {
2374 fprintf(stdout, "MMP is unsupported, please recompile with "
2375 "--enable-mmp\n");
2376 }
2377 #endif
2378
2379 static int source_file(const char *cmd_file, int ss_idx)
2380 {
2381 FILE *f;
2382 char buf[BUFSIZ];
2383 char *cp;
2384 int exit_status = 0;
2385 int retval;
2386
2387 if (strcmp(cmd_file, "-") == 0)
2388 f = stdin;
2389 else {
2390 f = fopen(cmd_file, "r");
2391 if (!f) {
2392 perror(cmd_file);
2393 exit(1);
2394 }
2395 }
2396 fflush(stdout);
2397 fflush(stderr);
2398 setbuf(stdout, NULL);
2399 setbuf(stderr, NULL);
2400 while (!feof(f)) {
2401 if (fgets(buf, sizeof(buf), f) == NULL)
2402 break;
2403 cp = strchr(buf, '\n');
2404 if (cp)
2405 *cp = 0;
2406 cp = strchr(buf, '\r');
2407 if (cp)
2408 *cp = 0;
2409 printf("debugfs: %s\n", buf);
2410 retval = ss_execute_line(ss_idx, buf);
2411 if (retval) {
2412 ss_perror(ss_idx, retval, buf);
2413 exit_status++;
2414 }
2415 }
2416 if (f != stdin)
2417 fclose(f);
2418 return exit_status;
2419 }
2420
2421 int main(int argc, char **argv)
2422 {
2423 int retval;
2424 const char *usage =
2425 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2426 "[-R request] [-V] ["
2427 #ifndef READ_ONLY
2428 "[-w] [-z undo_file] "
2429 #endif
2430 "[-c] device]";
2431 int c;
2432 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2433 char *request = 0;
2434 int exit_status = 0;
2435 char *cmd_file = 0;
2436 blk64_t superblock = 0;
2437 blk64_t blocksize = 0;
2438 int catastrophic = 0;
2439 char *data_filename = 0;
2440 #ifdef READ_ONLY
2441 const char *opt_string = "nicR:f:b:s:Vd:D";
2442 #else
2443 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2444 char *undo_file = NULL;
2445 #endif
2446 #ifdef CONFIG_JBD_DEBUG
2447 char *jbd_debug;
2448 #endif
2449
2450 if (debug_prog_name == 0)
2451 #ifdef READ_ONLY
2452 debug_prog_name = "rdebugfs";
2453 #else
2454 debug_prog_name = "debugfs";
2455 #endif
2456 add_error_table(&et_ext2_error_table);
2457 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2458 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2459
2460 #ifdef CONFIG_JBD_DEBUG
2461 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2462 if (jbd_debug) {
2463 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2464
2465 if (res != 1) {
2466 fprintf(stderr,
2467 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2468 jbd_debug);
2469 exit(1);
2470 }
2471 }
2472 #endif
2473 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2474 switch (c) {
2475 case 'R':
2476 request = optarg;
2477 break;
2478 case 'f':
2479 cmd_file = optarg;
2480 break;
2481 case 'd':
2482 data_filename = optarg;
2483 break;
2484 case 'i':
2485 open_flags |= EXT2_FLAG_IMAGE_FILE;
2486 break;
2487 case 'n':
2488 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2489 break;
2490 #ifndef READ_ONLY
2491 case 'w':
2492 open_flags |= EXT2_FLAG_RW;
2493 break;
2494 #endif
2495 case 'D':
2496 open_flags |= EXT2_FLAG_DIRECT_IO;
2497 break;
2498 case 'b':
2499 blocksize = parse_ulong(optarg, argv[0],
2500 "block size", 0);
2501 break;
2502 case 's':
2503 retval = strtoblk(argv[0], optarg,
2504 "superblock block number",
2505 &superblock);
2506 if (retval)
2507 return 1;
2508 break;
2509 case 'c':
2510 catastrophic = 1;
2511 break;
2512 case 'V':
2513 /* Print version number and exit */
2514 fprintf(stderr, "\tUsing %s\n",
2515 error_message(EXT2_ET_BASE));
2516 exit(0);
2517 case 'z':
2518 undo_file = optarg;
2519 break;
2520 default:
2521 com_err(argv[0], 0, usage, debug_prog_name);
2522 return 1;
2523 }
2524 }
2525 if (optind < argc)
2526 open_filesystem(argv[optind], open_flags,
2527 superblock, blocksize, catastrophic,
2528 data_filename, undo_file);
2529
2530 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2531 &debug_cmds, &retval);
2532 if (retval) {
2533 ss_perror(sci_idx, retval, "creating invocation");
2534 exit(1);
2535 }
2536 ss_get_readline(sci_idx);
2537
2538 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2539 if (retval) {
2540 ss_perror(sci_idx, retval, "adding standard requests");
2541 exit (1);
2542 }
2543 if (extra_cmds)
2544 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2545 if (retval) {
2546 ss_perror(sci_idx, retval, "adding extra requests");
2547 exit (1);
2548 }
2549 if (request) {
2550 retval = 0;
2551 retval = ss_execute_line(sci_idx, request);
2552 if (retval) {
2553 ss_perror(sci_idx, retval, request);
2554 exit_status++;
2555 }
2556 } else if (cmd_file) {
2557 exit_status = source_file(cmd_file, sci_idx);
2558 } else {
2559 ss_listen(sci_idx);
2560 }
2561
2562 ss_delete_invocation(sci_idx);
2563
2564 if (current_fs)
2565 close_filesystem();
2566
2567 remove_error_table(&et_ext2_error_table);
2568 return exit_status;
2569 }