tests: fix "format not a string literal" -Wall warning in test_icount
[e2fsprogs.git] / debugfs / debugfs.c
blobb67a88bcd5a143f3f736dd98fd63176cde841adc
1 /*
2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
9 */
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
33 #include "debugfs.h"
34 #include "uuid/uuid.h"
35 #include "e2p/e2p.h"
37 #include <ext2fs/ext2_ext_attr.h>
39 #include "../version.h"
40 #include "jfs_user.h"
41 #include "support/plausible.h"
43 #ifndef BUFSIZ
44 #define BUFSIZ 8192
45 #endif
47 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
48 int journal_enable_debug = -1;
49 #endif
52 * There must be only one definition if we're hooking in extra commands or
53 * chaging default prompt. Use -DSKIP_GLOBDEF for that.
55 #ifndef SKIP_GLOBDEFS
56 ss_request_table *extra_cmds;
57 const char *debug_prog_name;
58 #endif
59 int ss_sci_idx;
61 ext2_filsys current_fs;
62 quota_ctx_t current_qctx;
63 ext2_ino_t root, cwd;
64 int no_copy_xattrs;
66 static int debugfs_setup_tdb(const char *device_name, char *undo_file,
67 io_manager *io_ptr)
69 errcode_t retval = ENOMEM;
70 const char *tdb_dir = NULL;
71 char *tdb_file = NULL;
72 char *dev_name, *tmp_name;
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;
91 * Configuration via a conf file would be
92 * nice
94 tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR");
95 if (!tdb_dir)
96 tdb_dir = "/var/lib/e2fsprogs";
98 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
99 access(tdb_dir, W_OK))
100 return 0;
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;
111 sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name);
112 free(tmp_name);
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;
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);
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;
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)
145 int retval;
146 io_channel data_io = 0;
147 io_manager io_ptr = unix_io_manager;
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;
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;
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;
170 if (catastrophic)
171 open_flags |= EXT2_FLAG_SKIP_MMP | EXT2_FLAG_IGNORE_SB_ERRORS;
173 if (undo_file) {
174 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
175 if (retval)
176 exit(1);
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;
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;
196 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
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;
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;
218 root = cwd = EXT2_ROOT_INO;
219 return;
221 errout:
222 retval = ext2fs_close_free(&current_fs);
223 if (retval)
224 com_err(device, retval, "while trying to close filesystem");
227 void do_open_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
228 void *infop EXT2FS_ATTR((unused)))
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;
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;
290 if (optind != argc-1) {
291 goto print_usage;
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;
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]);
310 void do_lcd(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
311 void *infop EXT2FS_ATTR((unused)))
313 if (argc != 2) {
314 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
315 return;
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;
326 static void close_filesystem(NOARGS)
328 int retval;
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);
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);
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;
348 void do_close_filesys(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
349 void *infop EXT2FS_ATTR((unused)))
351 int c;
353 if (check_fs_open(argv[0]))
354 return;
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;
367 if (argc > optind) {
368 print_usage:
369 com_err(0, 0, "Usage: close_filesys [-a]");
370 return;
373 close_filesystem();
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)))
380 struct ext2_super_block param;
381 errcode_t retval;
382 int err;
383 blk64_t blocks;
385 if (common_args_process(argc, argv, 3, 3, "initialize",
386 "<device> <blocks>", CHECK_FS_NOTOPEN))
387 return;
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;
401 root = cwd = EXT2_ROOT_INO;
402 return;
405 static void print_features(struct ext2_super_block * s, FILE *f)
407 int i, j, printed=0;
408 __u32 *mask = &s->s_feature_compat, m;
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++;
419 if (printed == 0)
420 fputs("(none)", f);
421 fputs("\n", f);
423 #endif /* READ_ONLY */
425 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
426 const char *str, int *first, FILE *f)
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);
438 void do_show_super_stats(int argc, char *argv[],
439 int sci_idx EXT2FS_ATTR((unused)),
440 void *infop EXT2FS_ATTR((unused)))
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;
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;
458 if (optind != argc) {
459 goto print_usage;
461 if (check_fs_open(argv[0]))
462 return;
463 out = open_pager();
465 if (ext2fs_has_feature_bigalloc(current_fs->super))
466 units = "cluster";
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;
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;
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);
484 if (header_only) {
485 close_pager(out);
486 return;
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", i,
497 (unsigned long long) ext2fs_block_bitmap_loc(current_fs, i),
498 (unsigned long long) ext2fs_inode_bitmap_loc(current_fs, i),
499 (unsigned long long) ext2fs_inode_table_loc(current_fs, i),
500 ext2fs_bg_free_blocks_count(current_fs, i),
501 units,
502 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
503 "s" : "",
504 ext2fs_bg_free_inodes_count(current_fs, i),
505 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
506 "inodes" : "inode",
507 ext2fs_bg_used_dirs_count(current_fs, i),
508 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
509 : "directory", gdt_csum ? ", " : "\n");
510 if (gdt_csum)
511 fprintf(out, "%u unused %s\n",
512 ext2fs_bg_itable_unused(current_fs, i),
513 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
514 "inodes" : "inode");
515 first = 1;
516 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
517 &first, out);
518 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
519 &first, out);
520 if (gdt_csum) {
521 fprintf(out, "%sChecksum 0x%04x",
522 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
523 first = 0;
525 if (!first)
526 fputs("]\n", out);
528 close_pager(out);
529 return;
530 print_usage:
531 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
534 #ifndef READ_ONLY
535 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
536 char **argv EXT2FS_ATTR((unused)),
537 int sci_idx EXT2FS_ATTR((unused)),
538 void *infop EXT2FS_ATTR((unused)))
540 if (check_fs_open(argv[0]))
541 return;
542 if (check_fs_read_write(argv[0]))
543 return;
545 if (argv[1] && !strcmp(argv[1], "-clean"))
546 current_fs->super->s_state |= EXT2_VALID_FS;
547 else
548 current_fs->super->s_state &= ~EXT2_VALID_FS;
549 ext2fs_mark_super_dirty(current_fs);
551 #endif /* READ_ONLY */
553 struct list_blocks_struct {
554 FILE *f;
555 e2_blkcnt_t total;
556 blk64_t first_block, last_block;
557 e2_blkcnt_t first_bcnt, last_bcnt;
558 e2_blkcnt_t first;
561 static void finish_range(struct list_blocks_struct *lb)
563 if (lb->first_block == 0)
564 return;
565 if (lb->first)
566 lb->first = 0;
567 else
568 fprintf(lb->f, ", ");
569 if (lb->first_block == lb->last_block)
570 fprintf(lb->f, "(%lld):%llu",
571 (long long)lb->first_bcnt,
572 (unsigned long long) lb->first_block);
573 else
574 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
575 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
576 (unsigned long long) lb->first_block,
577 (unsigned long long) lb->last_block);
578 lb->first_block = 0;
581 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
582 blk64_t *blocknr, e2_blkcnt_t blockcnt,
583 blk64_t ref_block EXT2FS_ATTR((unused)),
584 int ref_offset EXT2FS_ATTR((unused)),
585 void *private)
587 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
589 lb->total++;
590 if (blockcnt >= 0) {
592 * See if we can add on to the existing range (if it exists)
594 if (lb->first_block &&
595 (lb->last_block+1 == *blocknr) &&
596 (lb->last_bcnt+1 == blockcnt)) {
597 lb->last_block = *blocknr;
598 lb->last_bcnt = blockcnt;
599 return 0;
602 * Start a new range.
604 finish_range(lb);
605 lb->first_block = lb->last_block = *blocknr;
606 lb->first_bcnt = lb->last_bcnt = blockcnt;
607 return 0;
610 * Not a normal block. Always force a new range.
612 finish_range(lb);
613 if (lb->first)
614 lb->first = 0;
615 else
616 fprintf(lb->f, ", ");
617 if (blockcnt == -1)
618 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
619 else if (blockcnt == -2)
620 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
621 else if (blockcnt == -3)
622 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
623 return 0;
626 static void internal_dump_inode_extra(FILE *out,
627 const char *prefix EXT2FS_ATTR((unused)),
628 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
629 struct ext2_inode_large *inode)
631 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
632 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
633 EXT2_GOOD_OLD_INODE_SIZE) {
634 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
635 inode->i_extra_isize);
636 return;
640 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
642 struct list_blocks_struct lb;
644 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
645 lb.total = 0;
646 lb.first_block = 0;
647 lb.f = f;
648 lb.first = 1;
649 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
650 list_blocks_proc, (void *)&lb);
651 finish_range(&lb);
652 if (lb.total)
653 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
654 fprintf(f,"\n");
657 static int int_log10(unsigned long long arg)
659 int l = 0;
661 arg = arg / 10;
662 while (arg) {
663 l++;
664 arg = arg / 10;
666 return l;
669 #define DUMP_LEAF_EXTENTS 0x01
670 #define DUMP_NODE_EXTENTS 0x02
671 #define DUMP_EXTENT_TABLE 0x04
673 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
674 int flags, int logical_width, int physical_width)
676 ext2_extent_handle_t handle;
677 struct ext2fs_extent extent;
678 struct ext2_extent_info info;
679 int op = EXT2_EXTENT_ROOT;
680 unsigned int printed = 0;
681 errcode_t errcode;
683 errcode = ext2fs_extent_open(current_fs, ino, &handle);
684 if (errcode)
685 return;
687 if (flags & DUMP_EXTENT_TABLE)
688 fprintf(f, "Level Entries %*s %*s Length Flags\n",
689 (logical_width*2)+3, "Logical",
690 (physical_width*2)+3, "Physical");
691 else
692 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
694 while (1) {
695 errcode = ext2fs_extent_get(handle, op, &extent);
697 if (errcode)
698 break;
700 op = EXT2_EXTENT_NEXT;
702 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
703 continue;
705 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
706 if ((flags & DUMP_LEAF_EXTENTS) == 0)
707 continue;
708 } else {
709 if ((flags & DUMP_NODE_EXTENTS) == 0)
710 continue;
713 errcode = ext2fs_extent_get_info(handle, &info);
714 if (errcode)
715 continue;
717 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
718 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
719 continue;
721 if (flags & DUMP_EXTENT_TABLE) {
722 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
723 "%*llu%*s %6u\n",
724 info.curr_level, info.max_depth,
725 info.curr_entry, info.num_entries,
726 logical_width,
727 (unsigned long long) extent.e_lblk,
728 logical_width,
729 (unsigned long long) extent.e_lblk + (extent.e_len - 1),
730 physical_width,
731 (unsigned long long) extent.e_pblk,
732 physical_width+3, "", extent.e_len);
733 continue;
736 fprintf(f, "%s(ETB%d):%llu",
737 printed ? ", " : "", info.curr_level,
738 (unsigned long long) extent.e_pblk);
739 printed = 1;
740 continue;
743 if (flags & DUMP_EXTENT_TABLE) {
744 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
745 "%*llu - %*llu %6u %s\n",
746 info.curr_level, info.max_depth,
747 info.curr_entry, info.num_entries,
748 logical_width,
749 (unsigned long long) extent.e_lblk,
750 logical_width,
751 (unsigned long long) extent.e_lblk + (extent.e_len - 1),
752 physical_width,
753 (unsigned long long) extent.e_pblk,
754 physical_width,
755 (unsigned long long) extent.e_pblk + (extent.e_len - 1),
756 extent.e_len,
757 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
758 "Uninit" : "");
759 continue;
762 if (extent.e_len == 0)
763 continue;
764 else if (extent.e_len == 1)
765 fprintf(f,
766 "%s(%lld%s):%lld",
767 printed ? ", " : "",
768 (unsigned long long) extent.e_lblk,
769 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
770 "[u]" : "",
771 (unsigned long long) extent.e_pblk);
772 else
773 fprintf(f,
774 "%s(%lld-%lld%s):%lld-%lld",
775 printed ? ", " : "",
776 (unsigned long long) extent.e_lblk,
777 (unsigned long long) extent.e_lblk + (extent.e_len - 1),
778 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
779 "[u]" : "",
780 (unsigned long long) extent.e_pblk,
781 (unsigned long long) extent.e_pblk + (extent.e_len - 1));
782 printed = 1;
784 if (printed)
785 fprintf(f, "\n");
786 ext2fs_extent_free(handle);
789 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
791 errcode_t retval;
792 size_t size;
794 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
795 if (!retval)
796 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
799 static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num,
800 struct ext2_inode *inode, const char *prefix)
802 errcode_t retval;
803 char *buf = NULL;
804 size_t size;
806 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
807 if (retval)
808 goto out;
810 retval = ext2fs_get_memzero(size + 1, &buf);
811 if (retval)
812 goto out;
814 retval = ext2fs_inline_data_get(current_fs, inode_num,
815 inode, buf, &size);
816 if (retval)
817 goto out;
819 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
820 (int)size, buf);
821 out:
822 if (buf)
823 ext2fs_free_mem(&buf);
824 if (retval)
825 com_err(__func__, retval, "while dumping link destination");
828 void internal_dump_inode(FILE *out, const char *prefix,
829 ext2_ino_t inode_num, struct ext2_inode *inode,
830 int do_dump_blocks)
832 const char *i_type;
833 char frag, fsize;
834 int os = current_fs->super->s_creator_os;
835 struct ext2_inode_large *large_inode;
836 int is_large_inode = 0;
838 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
839 is_large_inode = 1;
840 large_inode = (struct ext2_inode_large *) inode;
842 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
843 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
844 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
845 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
846 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
847 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
848 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
849 else i_type = "bad type";
850 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
851 fprintf(out, "%sMode: 0%03o Flags: 0x%x\n",
852 prefix, inode->i_mode & 07777, inode->i_flags);
853 if (is_large_inode && large_inode->i_extra_isize >= 24) {
854 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
855 prefix, inode->i_generation, large_inode->i_version_hi,
856 inode->osd1.linux1.l_i_version);
857 } else {
858 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
859 inode->i_generation, inode->osd1.linux1.l_i_version);
861 fprintf(out, "%sUser: %5d Group: %5d",
862 prefix, inode_uid(*inode), inode_gid(*inode));
863 if (is_large_inode && large_inode->i_extra_isize >= 32)
864 fprintf(out, " Project: %5d", large_inode->i_projid);
865 fputs(" Size: ", out);
866 if (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))
867 fprintf(out, "%llu\n", (unsigned long long) EXT2_I_SIZE(inode));
868 else
869 fprintf(out, "%u\n", inode->i_size);
870 if (os == EXT2_OS_HURD)
871 fprintf(out,
872 "%sFile ACL: %u Translator: %u\n",
873 prefix,
874 inode->i_file_acl,
875 inode->osd1.hurd1.h_i_translator);
876 else
877 fprintf(out, "%sFile ACL: %llu\n",
878 prefix,
879 inode->i_file_acl | ((long long)
880 (inode->osd2.linux2.l_i_file_acl_high) << 32));
881 if (os != EXT2_OS_HURD)
882 fprintf(out, "%sLinks: %u Blockcount: %llu\n",
883 prefix, inode->i_links_count,
884 (((unsigned long long)
885 inode->osd2.linux2.l_i_blocks_hi << 32)) +
886 inode->i_blocks);
887 else
888 fprintf(out, "%sLinks: %u Blockcount: %u\n",
889 prefix, inode->i_links_count, inode->i_blocks);
890 switch (os) {
891 case EXT2_OS_HURD:
892 frag = inode->osd2.hurd2.h_i_frag;
893 fsize = inode->osd2.hurd2.h_i_fsize;
894 break;
895 default:
896 frag = fsize = 0;
898 fprintf(out, "%sFragment: Address: %u Number: %u Size: %u\n",
899 prefix, inode->i_faddr, frag, fsize);
900 if (is_large_inode && large_inode->i_extra_isize >= 24) {
901 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
902 inode->i_ctime, large_inode->i_ctime_extra,
903 inode_time_to_string(inode->i_ctime,
904 large_inode->i_ctime_extra));
905 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
906 inode->i_atime, large_inode->i_atime_extra,
907 inode_time_to_string(inode->i_atime,
908 large_inode->i_atime_extra));
909 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
910 inode->i_mtime, large_inode->i_mtime_extra,
911 inode_time_to_string(inode->i_mtime,
912 large_inode->i_mtime_extra));
913 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
914 large_inode->i_crtime, large_inode->i_crtime_extra,
915 inode_time_to_string(large_inode->i_crtime,
916 large_inode->i_crtime_extra));
917 if (inode->i_dtime)
918 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
919 large_inode->i_dtime, large_inode->i_ctime_extra,
920 inode_time_to_string(inode->i_dtime,
921 large_inode->i_ctime_extra));
922 } else {
923 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
924 time_to_string((__s32) inode->i_ctime));
925 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
926 time_to_string((__s32) inode->i_atime));
927 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
928 time_to_string((__s32) inode->i_mtime));
929 if (inode->i_dtime)
930 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
931 inode->i_dtime,
932 time_to_string((__s32) inode->i_dtime));
934 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
935 internal_dump_inode_extra(out, prefix, inode_num,
936 (struct ext2_inode_large *) inode);
937 dump_inode_attributes(out, inode_num);
938 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
939 __u32 crc = inode->i_checksum_lo;
940 if (is_large_inode &&
941 large_inode->i_extra_isize >=
942 (offsetof(struct ext2_inode_large,
943 i_checksum_hi) -
944 EXT2_GOOD_OLD_INODE_SIZE))
945 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
946 fprintf(out, "Inode checksum: 0x%08x\n", crc);
949 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode))
950 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
951 (int)EXT2_I_SIZE(inode), (char *)inode->i_block);
952 else if (LINUX_S_ISLNK(inode->i_mode) &&
953 (inode->i_flags & EXT4_INLINE_DATA_FL))
954 dump_inline_symlink(out, inode_num, inode, prefix);
955 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
956 int major, minor;
957 const char *devnote;
959 if (inode->i_block[0]) {
960 major = (inode->i_block[0] >> 8) & 255;
961 minor = inode->i_block[0] & 255;
962 devnote = "";
963 } else {
964 major = (inode->i_block[1] & 0xfff00) >> 8;
965 minor = ((inode->i_block[1] & 0xff) |
966 ((inode->i_block[1] >> 12) & 0xfff00));
967 devnote = "(New-style) ";
969 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
970 devnote, major, minor, major, minor);
971 } else if (do_dump_blocks) {
972 if (inode->i_flags & EXT4_EXTENTS_FL)
973 dump_extents(out, prefix, inode_num,
974 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
975 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
976 dump_inline_data(out, prefix, inode_num);
977 else
978 dump_blocks(out, prefix, inode_num);
982 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
984 FILE *out;
986 out = open_pager();
987 internal_dump_inode(out, "", inode_num, inode, 1);
988 close_pager(out);
991 void do_stat(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
992 void *infop EXT2FS_ATTR((unused)))
994 ext2_ino_t inode;
995 struct ext2_inode * inode_buf;
997 if (check_fs_open(argv[0]))
998 return;
1000 inode_buf = (struct ext2_inode *)
1001 malloc(EXT2_INODE_SIZE(current_fs->super));
1002 if (!inode_buf) {
1003 fprintf(stderr, "do_stat: can't allocate buffer\n");
1004 return;
1007 if (common_inode_args_process(argc, argv, &inode, 0)) {
1008 free(inode_buf);
1009 return;
1012 if (debugfs_read_inode2(inode, inode_buf, argv[0],
1013 EXT2_INODE_SIZE(current_fs->super), 0)) {
1014 free(inode_buf);
1015 return;
1018 dump_inode(inode, inode_buf);
1019 free(inode_buf);
1020 return;
1023 void do_dump_extents(int argc, char **argv, int sci_idx EXT2FS_ATTR((unused)),
1024 void *infop EXT2FS_ATTR((unused)))
1026 struct ext2_inode inode;
1027 ext2_ino_t ino;
1028 FILE *out;
1029 int c, flags = 0;
1030 int logical_width;
1031 int physical_width;
1033 reset_getopt();
1034 while ((c = getopt(argc, argv, "nl")) != EOF) {
1035 switch (c) {
1036 case 'n':
1037 flags |= DUMP_NODE_EXTENTS;
1038 break;
1039 case 'l':
1040 flags |= DUMP_LEAF_EXTENTS;
1041 break;
1045 if (argc != optind + 1) {
1046 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1047 return;
1050 if (flags == 0)
1051 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1052 flags |= DUMP_EXTENT_TABLE;
1054 if (check_fs_open(argv[0]))
1055 return;
1057 ino = string_to_inode(argv[optind]);
1058 if (ino == 0)
1059 return;
1061 if (debugfs_read_inode(ino, &inode, argv[0]))
1062 return;
1064 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1065 fprintf(stderr, "%s: does not uses extent block maps\n",
1066 argv[optind]);
1067 return;
1070 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1071 current_fs->blocksize) + 1;
1072 if (logical_width < 5)
1073 logical_width = 5;
1074 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1075 if (physical_width < 5)
1076 physical_width = 5;
1078 out = open_pager();
1079 dump_extents(out, "", ino, flags, logical_width, physical_width);
1080 close_pager(out);
1081 return;
1084 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1085 blk64_t *blocknr,
1086 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1087 blk64_t ref_block EXT2FS_ATTR((unused)),
1088 int ref_offset EXT2FS_ATTR((unused)),
1089 void *private EXT2FS_ATTR((unused)))
1091 printf("%llu ", (unsigned long long) *blocknr);
1092 return 0;
1095 void do_blocks(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1096 void *infop EXT2FS_ATTR((unused)))
1098 ext2_ino_t inode;
1100 if (check_fs_open(argv[0]))
1101 return;
1103 if (common_inode_args_process(argc, argv, &inode, 0)) {
1104 return;
1107 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1108 print_blocks_proc, NULL);
1109 fputc('\n', stdout);
1110 return;
1113 void do_chroot(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1114 void *infop EXT2FS_ATTR((unused)))
1116 ext2_ino_t inode;
1117 int retval;
1119 if (common_inode_args_process(argc, argv, &inode, 0))
1120 return;
1122 retval = ext2fs_check_directory(current_fs, inode);
1123 if (retval) {
1124 com_err(argv[1], retval, 0);
1125 return;
1127 root = inode;
1130 #ifndef READ_ONLY
1131 void do_clri(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1132 void *infop EXT2FS_ATTR((unused)))
1134 ext2_ino_t inode;
1135 struct ext2_inode inode_buf;
1137 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1138 return;
1140 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1141 return;
1142 memset(&inode_buf, 0, sizeof(inode_buf));
1143 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1144 return;
1147 void do_freei(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1148 void *infop EXT2FS_ATTR((unused)))
1150 unsigned int len = 1;
1151 int err = 0;
1152 ext2_ino_t inode;
1154 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1155 CHECK_FS_RW | CHECK_FS_BITMAPS))
1156 return;
1157 if (check_fs_read_write(argv[0]))
1158 return;
1160 inode = string_to_inode(argv[1]);
1161 if (!inode)
1162 return;
1164 if (argc == 3) {
1165 len = parse_ulong(argv[2], argv[0], "length", &err);
1166 if (err)
1167 return;
1170 if (len == 1 &&
1171 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1172 com_err(argv[0], 0, "Warning: inode already clear");
1173 while (len-- > 0)
1174 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1175 ext2fs_mark_ib_dirty(current_fs);
1178 void do_seti(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1179 void *infop EXT2FS_ATTR((unused)))
1181 unsigned int len = 1;
1182 int err = 0;
1183 ext2_ino_t inode;
1185 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1186 CHECK_FS_RW | CHECK_FS_BITMAPS))
1187 return;
1188 if (check_fs_read_write(argv[0]))
1189 return;
1191 inode = string_to_inode(argv[1]);
1192 if (!inode)
1193 return;
1195 if (argc == 3) {
1196 len = parse_ulong(argv[2], argv[0], "length", &err);
1197 if (err)
1198 return;
1201 if ((len == 1) &&
1202 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1203 com_err(argv[0], 0, "Warning: inode already set");
1204 while (len-- > 0)
1205 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1206 ext2fs_mark_ib_dirty(current_fs);
1208 #endif /* READ_ONLY */
1210 void do_testi(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1211 void *infop EXT2FS_ATTR((unused)))
1213 ext2_ino_t inode;
1215 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1216 return;
1218 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1219 printf("Inode %u is marked in use\n", inode);
1220 else
1221 printf("Inode %u is not in use\n", inode);
1224 #ifndef READ_ONLY
1225 void do_freeb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1226 void *infop EXT2FS_ATTR((unused)))
1228 blk64_t block;
1229 blk64_t count = 1;
1231 if (common_block_args_process(argc, argv, &block, &count))
1232 return;
1233 if (check_fs_read_write(argv[0]))
1234 return;
1235 while (count-- > 0) {
1236 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1237 com_err(argv[0], 0, "Warning: block %llu already clear",
1238 (unsigned long long) block);
1239 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1240 block++;
1242 ext2fs_mark_bb_dirty(current_fs);
1245 void do_setb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1246 void *infop EXT2FS_ATTR((unused)))
1248 blk64_t block;
1249 blk64_t count = 1;
1251 if (common_block_args_process(argc, argv, &block, &count))
1252 return;
1253 if (check_fs_read_write(argv[0]))
1254 return;
1255 while (count-- > 0) {
1256 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1257 com_err(argv[0], 0, "Warning: block %llu already set",
1258 (unsigned long long) block);
1259 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1260 block++;
1262 ext2fs_mark_bb_dirty(current_fs);
1264 #endif /* READ_ONLY */
1266 void do_testb(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1267 void *infop EXT2FS_ATTR((unused)))
1269 blk64_t block;
1270 blk64_t count = 1;
1272 if (common_block_args_process(argc, argv, &block, &count))
1273 return;
1274 while (count-- > 0) {
1275 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1276 printf("Block %llu marked in use\n",
1277 (unsigned long long) block);
1278 else
1279 printf("Block %llu not in use\n",
1280 (unsigned long long) block);
1281 block++;
1285 #ifndef READ_ONLY
1286 static void modify_u8(char *com, const char *prompt,
1287 const char *format, __u8 *val)
1289 char buf[200];
1290 unsigned long v;
1291 char *tmp;
1293 sprintf(buf, format, *val);
1294 printf("%30s [%s] ", prompt, buf);
1295 if (!fgets(buf, sizeof(buf), stdin))
1296 return;
1297 if (buf[strlen (buf) - 1] == '\n')
1298 buf[strlen (buf) - 1] = '\0';
1299 if (!buf[0])
1300 return;
1301 v = strtoul(buf, &tmp, 0);
1302 if (*tmp)
1303 com_err(com, 0, "Bad value - %s", buf);
1304 else
1305 *val = v;
1308 static void modify_u16(char *com, const char *prompt,
1309 const char *format, __u16 *val)
1311 char buf[200];
1312 unsigned long v;
1313 char *tmp;
1315 sprintf(buf, format, *val);
1316 printf("%30s [%s] ", prompt, buf);
1317 if (!fgets(buf, sizeof(buf), stdin))
1318 return;
1319 if (buf[strlen (buf) - 1] == '\n')
1320 buf[strlen (buf) - 1] = '\0';
1321 if (!buf[0])
1322 return;
1323 v = strtoul(buf, &tmp, 0);
1324 if (*tmp)
1325 com_err(com, 0, "Bad value - %s", buf);
1326 else
1327 *val = v;
1330 static void modify_u32(char *com, const char *prompt,
1331 const char *format, __u32 *val)
1333 char buf[200];
1334 unsigned long v;
1335 char *tmp;
1337 sprintf(buf, format, *val);
1338 printf("%30s [%s] ", prompt, buf);
1339 if (!fgets(buf, sizeof(buf), stdin))
1340 return;
1341 if (buf[strlen (buf) - 1] == '\n')
1342 buf[strlen (buf) - 1] = '\0';
1343 if (!buf[0])
1344 return;
1345 v = strtoul(buf, &tmp, 0);
1346 if (*tmp)
1347 com_err(com, 0, "Bad value - %s", buf);
1348 else
1349 *val = v;
1353 void do_modify_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1354 void *infop EXT2FS_ATTR((unused)))
1356 struct ext2_inode inode;
1357 ext2_ino_t inode_num;
1358 int i;
1359 unsigned char *frag, *fsize;
1360 char buf[80];
1361 int os;
1362 const char *hex_format = "0x%x";
1363 const char *octal_format = "0%o";
1364 const char *decimal_format = "%d";
1365 const char *unsignedlong_format = "%lu";
1367 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1368 return;
1370 os = current_fs->super->s_creator_os;
1372 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1373 return;
1375 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1376 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1377 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1378 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1379 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1380 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1381 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1382 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1383 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1384 if (os == EXT2_OS_LINUX)
1385 modify_u16(argv[0], "Block count high", unsignedlong_format,
1386 &inode.osd2.linux2.l_i_blocks_hi);
1387 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1388 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1389 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1390 #if 0
1391 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1392 #endif
1393 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1395 modify_u32(argv[0], "High 32bits of size", decimal_format,
1396 &inode.i_size_high);
1398 if (os == EXT2_OS_HURD)
1399 modify_u32(argv[0], "Translator Block",
1400 decimal_format, &inode.osd1.hurd1.h_i_translator);
1402 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1403 switch (os) {
1404 case EXT2_OS_HURD:
1405 frag = &inode.osd2.hurd2.h_i_frag;
1406 fsize = &inode.osd2.hurd2.h_i_fsize;
1407 break;
1408 default:
1409 frag = fsize = 0;
1411 if (frag)
1412 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1413 if (fsize)
1414 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1416 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1417 sprintf(buf, "Direct Block #%u", i);
1418 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1420 modify_u32(argv[0], "Indirect Block", decimal_format,
1421 &inode.i_block[EXT2_IND_BLOCK]);
1422 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1423 &inode.i_block[EXT2_DIND_BLOCK]);
1424 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1425 &inode.i_block[EXT2_TIND_BLOCK]);
1426 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1427 return;
1429 #endif /* READ_ONLY */
1431 void do_change_working_dir(int argc, char *argv[],
1432 int sci_idx EXT2FS_ATTR((unused)),
1433 void *infop EXT2FS_ATTR((unused)))
1435 ext2_ino_t inode;
1436 int retval;
1438 if (common_inode_args_process(argc, argv, &inode, 0))
1439 return;
1441 retval = ext2fs_check_directory(current_fs, inode);
1442 if (retval) {
1443 com_err(argv[1], retval, 0);
1444 return;
1446 cwd = inode;
1447 return;
1450 void do_print_working_directory(int argc, char *argv[],
1451 int sci_idx EXT2FS_ATTR((unused)),
1452 void *infop EXT2FS_ATTR((unused)))
1454 int retval;
1455 char *pathname = NULL;
1457 if (common_args_process(argc, argv, 1, 1,
1458 "print_working_directory", "", 0))
1459 return;
1461 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1462 if (retval) {
1463 com_err(argv[0], retval,
1464 "while trying to get pathname of cwd");
1466 printf("[pwd] INODE: %6u PATH: %s\n",
1467 cwd, pathname ? pathname : "NULL");
1468 if (pathname) {
1469 free(pathname);
1470 pathname = NULL;
1472 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1473 if (retval) {
1474 com_err(argv[0], retval,
1475 "while trying to get pathname of root");
1477 printf("[root] INODE: %6u PATH: %s\n",
1478 root, pathname ? pathname : "NULL");
1479 if (pathname) {
1480 free(pathname);
1481 pathname = NULL;
1483 return;
1486 #ifndef READ_ONLY
1487 static void make_link(char *sourcename, char *destname)
1489 ext2_ino_t ino;
1490 struct ext2_inode inode;
1491 int retval;
1492 ext2_ino_t dir;
1493 char *dest, *cp, *base_name;
1496 * Get the source inode
1498 ino = string_to_inode(sourcename);
1499 if (!ino)
1500 return;
1501 base_name = strrchr(sourcename, '/');
1502 if (base_name)
1503 base_name++;
1504 else
1505 base_name = sourcename;
1507 * Figure out the destination. First see if it exists and is
1508 * a directory.
1510 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1511 dest = base_name;
1512 else {
1514 * OK, it doesn't exist. See if it is
1515 * '<dir>/basename' or 'basename'
1517 cp = strrchr(destname, '/');
1518 if (cp) {
1519 *cp = 0;
1520 dir = string_to_inode(destname);
1521 if (!dir)
1522 return;
1523 dest = cp+1;
1524 } else {
1525 dir = cwd;
1526 dest = destname;
1530 if (debugfs_read_inode(ino, &inode, sourcename))
1531 return;
1533 retval = ext2fs_link(current_fs, dir, dest, ino,
1534 ext2_file_type(inode.i_mode));
1535 if (retval)
1536 com_err("make_link", retval, 0);
1537 return;
1541 void do_link(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1542 void *infop EXT2FS_ATTR((unused)))
1544 if (common_args_process(argc, argv, 3, 3, "link",
1545 "<source file> <dest_name>", CHECK_FS_RW))
1546 return;
1548 make_link(argv[1], argv[2]);
1551 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1552 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1553 blk64_t ref_block EXT2FS_ATTR((unused)),
1554 int ref_offset EXT2FS_ATTR((unused)),
1555 void *private EXT2FS_ATTR((unused)))
1557 blk64_t block;
1559 block = *blocknr;
1560 ext2fs_block_alloc_stats2(fs, block, +1);
1561 return 0;
1564 void do_undel(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1565 void *infop EXT2FS_ATTR((unused)))
1567 ext2_ino_t ino;
1568 struct ext2_inode inode;
1570 if (common_args_process(argc, argv, 2, 3, "undelete",
1571 "<inode_num> [dest_name]",
1572 CHECK_FS_RW | CHECK_FS_BITMAPS))
1573 return;
1575 ino = string_to_inode(argv[1]);
1576 if (!ino)
1577 return;
1579 if (debugfs_read_inode(ino, &inode, argv[1]))
1580 return;
1582 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1583 com_err(argv[1], 0, "Inode is not marked as deleted");
1584 return;
1588 * XXX this function doesn't handle changing the links count on the
1589 * parent directory when undeleting a directory.
1591 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1592 inode.i_dtime = 0;
1594 if (debugfs_write_inode(ino, &inode, argv[0]))
1595 return;
1597 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1598 mark_blocks_proc, NULL);
1600 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1602 if (argc > 2)
1603 make_link(argv[1], argv[2]);
1606 static void unlink_file_by_name(char *filename)
1608 int retval;
1609 ext2_ino_t dir;
1610 char *base_name;
1612 base_name = strrchr(filename, '/');
1613 if (base_name) {
1614 *base_name++ = '\0';
1615 dir = string_to_inode(filename);
1616 if (!dir)
1617 return;
1618 } else {
1619 dir = cwd;
1620 base_name = filename;
1622 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1623 if (retval)
1624 com_err("unlink_file_by_name", retval, 0);
1625 return;
1628 void do_unlink(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1629 void *infop EXT2FS_ATTR((unused)))
1631 if (common_args_process(argc, argv, 2, 2, "link",
1632 "<pathname>", CHECK_FS_RW))
1633 return;
1635 unlink_file_by_name(argv[1]);
1638 void do_copy_inode(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1639 void *infop EXT2FS_ATTR((unused)))
1641 ext2_ino_t src_ino, dest_ino;
1642 unsigned char buf[4096];
1644 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1645 "<source file> <dest_name>", CHECK_FS_RW))
1646 return;
1648 src_ino = string_to_inode(argv[1]);
1649 if (!src_ino)
1650 return;
1652 dest_ino = string_to_inode(argv[2]);
1653 if (!dest_ino)
1654 return;
1656 if (debugfs_read_inode2(src_ino, (struct ext2_inode *) buf,
1657 argv[0], sizeof(buf), 0))
1658 return;
1660 if (debugfs_write_inode2(dest_ino, (struct ext2_inode *) buf,
1661 argv[0], sizeof(buf), 0))
1662 return;
1665 #endif /* READ_ONLY */
1667 void do_find_free_block(int argc, char *argv[],
1668 int sci_idx EXT2FS_ATTR((unused)),
1669 void *infop EXT2FS_ATTR((unused)))
1671 blk64_t free_blk, goal, first_free = 0;
1672 int count;
1673 errcode_t retval;
1674 char *tmp;
1676 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1677 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1678 return;
1680 if (check_fs_open(argv[0]))
1681 return;
1683 if (argc > 1) {
1684 count = strtol(argv[1],&tmp,0);
1685 if (*tmp) {
1686 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1687 return;
1689 } else
1690 count = 1;
1692 if (argc > 2) {
1693 goal = strtol(argv[2], &tmp, 0);
1694 if (*tmp) {
1695 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1696 return;
1699 else
1700 goal = current_fs->super->s_first_data_block;
1702 printf("Free blocks found: ");
1703 free_blk = goal - 1;
1704 while (count-- > 0) {
1705 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1706 &free_blk);
1707 if (first_free) {
1708 if (first_free == free_blk)
1709 break;
1710 } else
1711 first_free = free_blk;
1712 if (retval) {
1713 com_err("ext2fs_new_block", retval, 0);
1714 return;
1715 } else
1716 printf("%llu ", (unsigned long long) free_blk);
1718 printf("\n");
1721 void do_find_free_inode(int argc, char *argv[],
1722 int sci_idx EXT2FS_ATTR((unused)),
1723 void *infop EXT2FS_ATTR((unused)))
1725 ext2_ino_t free_inode, dir;
1726 int mode;
1727 int retval;
1728 char *tmp;
1730 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1731 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1732 return;
1734 if (check_fs_open(argv[0]))
1735 return;
1737 if (argc > 1) {
1738 dir = strtol(argv[1], &tmp, 0);
1739 if (*tmp) {
1740 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1741 return;
1744 else
1745 dir = root;
1746 if (argc > 2) {
1747 mode = strtol(argv[2], &tmp, 0);
1748 if (*tmp) {
1749 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1750 return;
1752 } else
1753 mode = 010755;
1755 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1756 if (retval)
1757 com_err("ext2fs_new_inode", retval, 0);
1758 else
1759 printf("Free inode found: %u\n", free_inode);
1762 #ifndef READ_ONLY
1763 void do_write(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1764 void *infop EXT2FS_ATTR((unused)))
1766 errcode_t retval;
1768 if (common_args_process(argc, argv, 3, 3, "write",
1769 "<native file> <new file>", CHECK_FS_RW))
1770 return;
1772 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1773 if (retval)
1774 com_err(argv[0], retval, 0);
1777 void do_mknod(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1778 void *infop EXT2FS_ATTR((unused)))
1780 unsigned long major, minor;
1781 errcode_t retval;
1782 int nr;
1783 struct stat st;
1785 if (check_fs_open(argv[0]))
1786 return;
1787 if (argc < 3 || argv[2][1]) {
1788 usage:
1789 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1790 return;
1793 minor = major = 0;
1794 switch (argv[2][0]) {
1795 case 'p':
1796 st.st_mode = S_IFIFO;
1797 nr = 3;
1798 break;
1799 case 'c':
1800 st.st_mode = S_IFCHR;
1801 nr = 5;
1802 break;
1803 case 'b':
1804 st.st_mode = S_IFBLK;
1805 nr = 5;
1806 break;
1807 default:
1808 nr = 0;
1811 if (nr == 5) {
1812 major = strtoul(argv[3], argv+3, 0);
1813 minor = strtoul(argv[4], argv+4, 0);
1814 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1815 nr = 0;
1818 if (argc != nr)
1819 goto usage;
1821 st.st_rdev = makedev(major, minor);
1822 retval = do_mknod_internal(current_fs, cwd, argv[1],
1823 st.st_mode, st.st_rdev);
1824 if (retval)
1825 com_err(argv[0], retval, 0);
1828 void do_mkdir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1829 void *infop EXT2FS_ATTR((unused)))
1831 errcode_t retval;
1833 if (common_args_process(argc, argv, 2, 2, "mkdir",
1834 "<filename>", CHECK_FS_RW))
1835 return;
1837 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1838 if (retval)
1839 com_err(argv[0], retval, 0);
1843 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1844 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1845 blk64_t ref_block EXT2FS_ATTR((unused)),
1846 int ref_offset EXT2FS_ATTR((unused)),
1847 void *private)
1849 blk64_t block = *blocknr;
1850 blk64_t *last_cluster = (blk64_t *)private;
1851 blk64_t cluster = EXT2FS_B2C(fs, block);
1853 if (cluster == *last_cluster)
1854 return 0;
1856 *last_cluster = cluster;
1858 ext2fs_block_alloc_stats2(fs, block, -1);
1859 return 0;
1862 static void kill_file_by_inode(ext2_ino_t inode)
1864 struct ext2_inode inode_buf;
1866 if (debugfs_read_inode(inode, &inode_buf, 0))
1867 return;
1868 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1869 if (debugfs_write_inode(inode, &inode_buf, 0))
1870 return;
1871 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1872 blk64_t last_cluster = 0;
1873 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1874 NULL, release_blocks_proc, &last_cluster);
1876 printf("\n");
1877 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1878 LINUX_S_ISDIR(inode_buf.i_mode));
1882 void do_kill_file(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1883 void *infop EXT2FS_ATTR((unused)))
1885 ext2_ino_t inode_num;
1887 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1888 return;
1890 kill_file_by_inode(inode_num);
1893 void do_rm(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1894 void *infop EXT2FS_ATTR((unused)))
1896 int retval;
1897 ext2_ino_t inode_num;
1898 struct ext2_inode inode;
1900 if (common_args_process(argc, argv, 2, 2, "rm",
1901 "<filename>", CHECK_FS_RW))
1902 return;
1904 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1905 if (retval) {
1906 com_err(argv[0], retval, "while trying to resolve filename");
1907 return;
1910 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1911 return;
1913 if (LINUX_S_ISDIR(inode.i_mode)) {
1914 com_err(argv[0], 0, "file is a directory");
1915 return;
1918 --inode.i_links_count;
1919 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1920 return;
1922 unlink_file_by_name(argv[1]);
1923 if (inode.i_links_count == 0)
1924 kill_file_by_inode(inode_num);
1927 struct rd_struct {
1928 ext2_ino_t parent;
1929 int empty;
1932 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1933 int entry EXT2FS_ATTR((unused)),
1934 struct ext2_dir_entry *dirent,
1935 int offset EXT2FS_ATTR((unused)),
1936 int blocksize EXT2FS_ATTR((unused)),
1937 char *buf EXT2FS_ATTR((unused)),
1938 void *private)
1940 struct rd_struct *rds = (struct rd_struct *) private;
1942 if (dirent->inode == 0)
1943 return 0;
1944 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1945 return 0;
1946 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1947 (dirent->name[1] == '.')) {
1948 rds->parent = dirent->inode;
1949 return 0;
1951 rds->empty = 0;
1952 return 0;
1955 void do_rmdir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
1956 void *infop EXT2FS_ATTR((unused)))
1958 int retval;
1959 ext2_ino_t inode_num;
1960 struct ext2_inode inode;
1961 struct rd_struct rds;
1963 if (common_args_process(argc, argv, 2, 2, "rmdir",
1964 "<filename>", CHECK_FS_RW))
1965 return;
1967 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1968 if (retval) {
1969 com_err(argv[0], retval, "while trying to resolve filename");
1970 return;
1973 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1974 return;
1976 if (!LINUX_S_ISDIR(inode.i_mode)) {
1977 com_err(argv[0], 0, "file is not a directory");
1978 return;
1981 rds.parent = 0;
1982 rds.empty = 1;
1984 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1985 0, rmdir_proc, &rds);
1986 if (retval) {
1987 com_err(argv[0], retval, "while iterating over directory");
1988 return;
1990 if (rds.empty == 0) {
1991 com_err(argv[0], 0, "directory not empty");
1992 return;
1995 inode.i_links_count = 0;
1996 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1997 return;
1999 unlink_file_by_name(argv[1]);
2000 kill_file_by_inode(inode_num);
2002 if (rds.parent) {
2003 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2004 return;
2005 if (inode.i_links_count > 1)
2006 inode.i_links_count--;
2007 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2008 return;
2011 #endif /* READ_ONLY */
2013 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2014 char *argv[] EXT2FS_ATTR((unused)),
2015 int sci_idx EXT2FS_ATTR((unused)),
2016 void *infop EXT2FS_ATTR((unused)))
2018 if (current_fs)
2019 printf("Open mode: read-%s\n",
2020 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2021 printf("Filesystem in use: %s\n",
2022 current_fs ? current_fs->device_name : "--none--");
2025 #ifndef READ_ONLY
2026 void do_expand_dir(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2027 void *infop EXT2FS_ATTR((unused)))
2029 ext2_ino_t inode;
2030 int retval;
2032 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2033 return;
2035 retval = ext2fs_expand_dir(current_fs, inode);
2036 if (retval)
2037 com_err("ext2fs_expand_dir", retval, 0);
2038 return;
2041 void do_features(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2042 void *infop EXT2FS_ATTR((unused)))
2044 int i;
2046 if (check_fs_open(argv[0]))
2047 return;
2049 if ((argc != 1) && check_fs_read_write(argv[0]))
2050 return;
2051 for (i=1; i < argc; i++) {
2052 if (e2p_edit_feature(argv[i],
2053 &current_fs->super->s_feature_compat, 0))
2054 com_err(argv[0], 0, "Unknown feature: %s\n",
2055 argv[i]);
2056 else
2057 ext2fs_mark_super_dirty(current_fs);
2059 print_features(current_fs->super, stdout);
2061 #endif /* READ_ONLY */
2063 void do_bmap(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2064 void *infop EXT2FS_ATTR((unused)))
2066 ext2_ino_t ino;
2067 blk64_t blk, pblk = 0;
2068 int c, err, flags = 0, ret_flags = 0;
2069 errcode_t errcode;
2071 if (check_fs_open(argv[0]))
2072 return;
2074 reset_getopt();
2075 while ((c = getopt (argc, argv, "a")) != EOF) {
2076 switch (c) {
2077 case 'a':
2078 flags |= BMAP_ALLOC;
2079 break;
2080 default:
2081 goto print_usage;
2085 if (argc <= optind+1) {
2086 print_usage:
2087 com_err(0, 0,
2088 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2089 return;
2092 ino = string_to_inode(argv[optind++]);
2093 if (!ino)
2094 return;
2095 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2096 if (err)
2097 return;
2099 if (argc > optind+1)
2100 goto print_usage;
2102 if (argc == optind+1) {
2103 err = strtoblk(argv[0], argv[optind++],
2104 "physical block", &pblk);
2105 if (err)
2106 return;
2107 if (flags & BMAP_ALLOC) {
2108 com_err(0, 0, "Can't set and allocate a block");
2109 return;
2111 flags |= BMAP_SET;
2114 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2115 &ret_flags, &pblk);
2116 if (errcode) {
2117 com_err(argv[0], errcode,
2118 "while mapping logical block %llu\n",
2119 (unsigned long long) blk);
2120 return;
2122 printf("%llu", (unsigned long long) pblk);
2123 if (ret_flags & BMAP_RET_UNINIT)
2124 fputs(" (uninit)", stdout);
2125 fputc('\n', stdout);
2128 void do_imap(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2129 void *infop EXT2FS_ATTR((unused)))
2131 ext2_ino_t ino;
2132 unsigned long group, block, block_nr, offset;
2134 if (common_args_process(argc, argv, 2, 2, argv[0],
2135 "<file>", 0))
2136 return;
2137 ino = string_to_inode(argv[1]);
2138 if (!ino)
2139 return;
2141 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2142 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2143 EXT2_INODE_SIZE(current_fs->super);
2144 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2145 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2146 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2147 group);
2148 return;
2150 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2151 block;
2152 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2154 printf("Inode %u is part of block group %lu\n"
2155 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2156 block_nr, offset);
2160 void do_idump(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2161 void *infop EXT2FS_ATTR((unused)))
2163 struct ext2_inode_large *inode;
2164 ext2_ino_t ino;
2165 unsigned char *buf;
2166 errcode_t err;
2167 unsigned int isize, size, offset = 0;
2168 int c, mode = 0;
2170 reset_getopt();
2171 while ((c = getopt (argc, argv, "bex")) != EOF) {
2172 if (mode || c == '?') {
2173 com_err(argv[0], 0,
2174 "Usage: inode_dump [-b]|[-e] <file>");
2175 return;
2177 mode = c;
2179 if (optind != argc-1)
2180 return;
2182 if (check_fs_open(argv[0]))
2183 return;
2185 ino = string_to_inode(argv[optind]);
2186 if (!ino)
2187 return;
2189 isize = EXT2_INODE_SIZE(current_fs->super);
2190 err = ext2fs_get_mem(isize, &buf);
2191 if (err) {
2192 com_err(argv[0], err, "while allocating memory");
2193 return;
2196 err = ext2fs_read_inode_full(current_fs, ino,
2197 (struct ext2_inode *)buf, isize);
2198 if (err) {
2199 com_err(argv[0], err, "while reading inode %u", ino);
2200 goto err;
2203 inode = (struct ext2_inode_large *) buf;
2204 size = isize;
2205 switch (mode) {
2206 case 'b':
2207 offset = ((char *) (&inode->i_block)) - ((char *) buf);
2208 size = sizeof(inode->i_block);
2209 break;
2210 case 'x':
2211 case 'e':
2212 if (size <= EXT2_GOOD_OLD_INODE_SIZE) {
2213 com_err(argv[0], 0, "No extra space in inode");
2214 goto err;
2216 offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2217 if (offset > size)
2218 goto err;
2219 size -= offset;
2220 break;
2222 if (mode == 'x')
2223 raw_inode_xattr_dump(stdout, buf + offset, size);
2224 else
2225 do_byte_hexdump(stdout, buf + offset, size);
2226 err:
2227 ext2fs_free_mem(&buf);
2230 #ifndef READ_ONLY
2231 void do_set_current_time(int argc, char *argv[],
2232 int sci_idx EXT2FS_ATTR((unused)),
2233 void *infop EXT2FS_ATTR((unused)))
2235 __s64 now;
2237 if (common_args_process(argc, argv, 2, 2, argv[0],
2238 "<time>", 0))
2239 return;
2241 now = string_to_time(argv[1]);
2242 if (now == -1) {
2243 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2244 argv[1]);
2245 return;
2247 } else {
2248 printf("Setting current time to %s\n", time_to_string(now));
2249 current_fs->now = now;
2252 #endif /* READ_ONLY */
2254 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2256 int compat, bit, ret;
2257 unsigned int feature_mask;
2259 if (name) {
2260 if (feature_type == E2P_FS_FEATURE)
2261 ret = e2p_string2feature(name, &compat, &feature_mask);
2262 else
2263 ret = e2p_jrnl_string2feature(name, &compat,
2264 &feature_mask);
2265 if (ret)
2266 return ret;
2268 if (!(supp[compat] & feature_mask))
2269 return 1;
2270 } else {
2271 for (compat = 0; compat < 3; compat++) {
2272 for (bit = 0, feature_mask = 1; bit < 32;
2273 bit++, feature_mask <<= 1) {
2274 if (supp[compat] & feature_mask) {
2275 if (feature_type == E2P_FS_FEATURE)
2276 fprintf(stdout, " %s",
2277 e2p_feature2string(compat,
2278 feature_mask));
2279 else
2280 fprintf(stdout, " %s",
2281 e2p_jrnl_feature2string(compat,
2282 feature_mask));
2286 fprintf(stdout, "\n");
2289 return 0;
2292 void do_supported_features(int argc, char *argv[],
2293 int sci_idx EXT2FS_ATTR((unused)),
2294 void *infop EXT2FS_ATTR((unused)))
2296 int ret;
2297 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2298 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2299 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2300 __u32 jrnl_supp[3] = { JBD2_KNOWN_COMPAT_FEATURES,
2301 JBD2_KNOWN_INCOMPAT_FEATURES,
2302 JBD2_KNOWN_ROCOMPAT_FEATURES };
2304 if (argc > 1) {
2305 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2306 if (ret) {
2307 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2308 argv[1]);
2310 if (ret)
2311 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2312 else
2313 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2314 } else {
2315 fprintf(stdout, "Supported features:");
2316 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2317 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2321 #ifndef READ_ONLY
2322 void do_punch(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2323 void *infop EXT2FS_ATTR((unused)))
2325 ext2_ino_t ino;
2326 blk64_t start, end;
2327 int err;
2328 errcode_t errcode;
2330 if (common_args_process(argc, argv, 3, 4, argv[0],
2331 "<file> start_blk [end_blk]",
2332 CHECK_FS_RW | CHECK_FS_BITMAPS))
2333 return;
2335 ino = string_to_inode(argv[1]);
2336 if (!ino)
2337 return;
2338 err = strtoblk(argv[0], argv[2], "logical block", &start);
2339 if (err)
2340 return;
2341 if (argc == 4) {
2342 err = strtoblk(argv[0], argv[3], "logical block", &end);
2343 if (err)
2344 return;
2345 } else
2346 end = ~0;
2348 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2350 if (errcode) {
2351 com_err(argv[0], errcode,
2352 "while truncating inode %u from %llu to %llu\n", ino,
2353 (unsigned long long) start, (unsigned long long) end);
2354 return;
2358 void do_fallocate(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2359 void *infop EXT2FS_ATTR((unused)))
2361 ext2_ino_t ino;
2362 blk64_t start, end;
2363 int err;
2364 errcode_t errcode;
2366 if (common_args_process(argc, argv, 3, 4, argv[0],
2367 "<file> start_blk [end_blk]",
2368 CHECK_FS_RW | CHECK_FS_BITMAPS))
2369 return;
2371 ino = string_to_inode(argv[1]);
2372 if (!ino)
2373 return;
2374 err = strtoblk(argv[0], argv[2], "logical block", &start);
2375 if (err)
2376 return;
2377 if (argc == 4) {
2378 err = strtoblk(argv[0], argv[3], "logical block", &end);
2379 if (err)
2380 return;
2381 } else
2382 end = ~0;
2384 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2385 ino, NULL, ~0ULL, start, end - start + 1);
2387 if (errcode) {
2388 com_err(argv[0], errcode,
2389 "while fallocating inode %u from %llu to %llu\n", ino,
2390 (unsigned long long) start, (unsigned long long) end);
2391 return;
2395 void do_symlink(int argc, char *argv[], int sci_idx EXT2FS_ATTR((unused)),
2396 void *infop EXT2FS_ATTR((unused)))
2398 errcode_t retval;
2400 if (common_args_process(argc, argv, 3, 3, "symlink",
2401 "<filename> <target>", CHECK_FS_RW))
2402 return;
2404 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2405 if (retval)
2406 com_err(argv[0], retval, 0);
2409 #endif /* READ_ONLY */
2411 #if CONFIG_MMP
2412 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[],
2413 int sci_idx EXT2FS_ATTR((unused)),
2414 void *infop EXT2FS_ATTR((unused)))
2416 struct mmp_struct *mmp_s;
2417 unsigned long long mmp_block;
2418 time_t t;
2419 errcode_t retval = 0;
2421 if (check_fs_open(argv[0]))
2422 return;
2424 if (argc > 1) {
2425 char *end = NULL;
2426 mmp_block = strtoull(argv[1], &end, 0);
2427 if (end == argv[0] || mmp_block == 0) {
2428 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2429 argv[0], argv[1]);
2430 return;
2432 } else {
2433 mmp_block = current_fs->super->s_mmp_block;
2436 if (mmp_block == 0) {
2437 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2438 argv[0]);
2439 return;
2442 if (current_fs->mmp_buf == NULL) {
2443 retval = ext2fs_get_mem(current_fs->blocksize,
2444 &current_fs->mmp_buf);
2445 if (retval) {
2446 com_err(argv[0], retval, "allocating MMP buffer.\n");
2447 return;
2451 mmp_s = current_fs->mmp_buf;
2453 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2454 if (retval) {
2455 com_err(argv[0], retval, "reading MMP block %llu.\n",
2456 (unsigned long long) mmp_block);
2457 return;
2460 t = mmp_s->mmp_time;
2461 fprintf(stdout, "block_number: %llu\n",
2462 (unsigned long long) current_fs->super->s_mmp_block);
2463 fprintf(stdout, "update_interval: %d\n",
2464 current_fs->super->s_mmp_update_interval);
2465 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2466 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2467 fprintf(stdout, "time: %llu -- %s",
2468 (unsigned long long) mmp_s->mmp_time, ctime(&t));
2469 fprintf(stdout, "node_name: %.*s\n",
2470 EXT2_LEN_STR(mmp_s->mmp_nodename));
2471 fprintf(stdout, "device_name: %.*s\n",
2472 EXT2_LEN_STR(mmp_s->mmp_bdevname));
2473 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2474 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2476 #else
2477 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2478 char *argv[] EXT2FS_ATTR((unused)),
2479 int sci_idx EXT2FS_ATTR((unused)),
2480 void *infop EXT2FS_ATTR((unused)))
2482 fprintf(stdout, "MMP is unsupported, please recompile with "
2483 "--enable-mmp\n");
2485 #endif
2487 static int source_file(const char *cmd_file, int ss_idx)
2489 FILE *f;
2490 char buf[BUFSIZ];
2491 char *cp;
2492 int exit_status = 0;
2493 int retval;
2495 if (strcmp(cmd_file, "-") == 0)
2496 f = stdin;
2497 else {
2498 f = fopen(cmd_file, "r");
2499 if (!f) {
2500 perror(cmd_file);
2501 exit(1);
2504 fflush(stdout);
2505 fflush(stderr);
2506 setbuf(stdout, NULL);
2507 setbuf(stderr, NULL);
2508 while (!feof(f)) {
2509 if (fgets(buf, sizeof(buf), f) == NULL)
2510 break;
2511 if (buf[0] == '#') {
2512 printf("%s", buf);
2513 continue;
2515 cp = strchr(buf, '\n');
2516 if (cp)
2517 *cp = 0;
2518 cp = strchr(buf, '\r');
2519 if (cp)
2520 *cp = 0;
2521 printf("debugfs: %s\n", buf);
2522 retval = ss_execute_line(ss_idx, buf);
2523 if (retval) {
2524 ss_perror(ss_idx, retval, buf);
2525 exit_status++;
2528 if (f != stdin)
2529 fclose(f);
2530 return exit_status;
2533 int main(int argc, char **argv)
2535 int retval;
2536 const char *usage =
2537 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2538 "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] ["
2539 #ifndef READ_ONLY
2540 "[-w] [-z undo_file] "
2541 #endif
2542 "[-c]] [device]";
2543 int c;
2544 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES |
2545 EXT2_FLAG_64BITS | EXT2_FLAG_THREADS;
2546 char *request = 0;
2547 int exit_status = 0;
2548 char *cmd_file = 0;
2549 blk64_t superblock = 0;
2550 blk64_t blocksize = 0;
2551 int catastrophic = 0;
2552 char *data_filename = 0;
2553 #ifdef READ_ONLY
2554 const char *opt_string = "nicR:f:b:s:Vd:D";
2555 #else
2556 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2557 #endif
2558 char *undo_file = NULL;
2559 #ifdef CONFIG_JBD_DEBUG
2560 char *jbd_debug;
2561 #endif
2563 if (debug_prog_name == 0)
2564 #ifdef READ_ONLY
2565 debug_prog_name = "rdebugfs";
2566 #else
2567 debug_prog_name = "debugfs";
2568 #endif
2569 add_error_table(&et_ext2_error_table);
2570 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2571 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2573 #ifdef CONFIG_JBD_DEBUG
2574 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2575 if (jbd_debug) {
2576 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2578 if (res != 1) {
2579 fprintf(stderr,
2580 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2581 jbd_debug);
2582 exit(1);
2585 #endif
2586 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2587 switch (c) {
2588 case 'R':
2589 request = optarg;
2590 break;
2591 case 'f':
2592 cmd_file = optarg;
2593 break;
2594 case 'd':
2595 data_filename = optarg;
2596 break;
2597 case 'i':
2598 open_flags |= EXT2_FLAG_IMAGE_FILE;
2599 break;
2600 case 'n':
2601 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2602 break;
2603 #ifndef READ_ONLY
2604 case 'w':
2605 open_flags |= EXT2_FLAG_RW;
2606 break;
2607 #endif
2608 case 'D':
2609 open_flags |= EXT2_FLAG_DIRECT_IO;
2610 break;
2611 case 'b':
2612 blocksize = parse_ulong(optarg, argv[0],
2613 "block size", 0);
2614 break;
2615 case 's':
2616 retval = strtoblk(argv[0], optarg,
2617 "superblock block number",
2618 &superblock);
2619 if (retval)
2620 return 1;
2621 break;
2622 case 'c':
2623 catastrophic = 1;
2624 break;
2625 case 'V':
2626 /* Print version number and exit */
2627 fprintf(stderr, "\tUsing %s\n",
2628 error_message(EXT2_ET_BASE));
2629 exit(0);
2630 #ifndef READ_ONLY
2631 case 'z':
2632 undo_file = optarg;
2633 break;
2634 #endif
2635 default:
2636 com_err(argv[0], 0, usage, debug_prog_name);
2637 return 1;
2640 if (optind < argc)
2641 open_filesystem(argv[optind], open_flags,
2642 superblock, blocksize, catastrophic,
2643 data_filename, undo_file);
2645 ss_sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2646 &debug_cmds, &retval);
2647 if (retval) {
2648 ss_perror(ss_sci_idx, retval, "creating invocation");
2649 exit(1);
2651 ss_get_readline(ss_sci_idx);
2653 (void) ss_add_request_table(ss_sci_idx, &ss_std_requests, 1, &retval);
2654 if (retval) {
2655 ss_perror(ss_sci_idx, retval, "adding standard requests");
2656 exit (1);
2658 if (extra_cmds)
2659 ss_add_request_table(ss_sci_idx, extra_cmds, 1, &retval);
2660 if (retval) {
2661 ss_perror(ss_sci_idx, retval, "adding extra requests");
2662 exit (1);
2664 if (request) {
2665 retval = 0;
2666 retval = ss_execute_line(ss_sci_idx, request);
2667 if (retval) {
2668 ss_perror(ss_sci_idx, retval, request);
2669 exit_status++;
2671 } else if (cmd_file) {
2672 exit_status = source_file(cmd_file, ss_sci_idx);
2673 } else {
2674 ss_listen(ss_sci_idx);
2677 ss_delete_invocation(ss_sci_idx);
2679 if (current_fs)
2680 close_filesystem();
2682 remove_error_table(&et_ext2_error_table);
2683 return exit_status;