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