Update release notes, etc., for the 1.47.2 release
[e2fsprogs.git] / debugfs / debugfs.c
blob8acb56a4d4be63a8bf60efa11e09da73c55ade8b
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 #include <locale.h>
20 #ifdef HAVE_GETOPT_H
21 #include <getopt.h>
22 #else
23 extern int optind;
24 extern char *optarg;
25 #endif
26 #ifdef HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29 #include <fcntl.h>
30 #ifdef HAVE_SYS_SYSMACROS_H
31 #include <sys/sysmacros.h>
32 #endif
34 #include "debugfs.h"
35 #include "uuid/uuid.h"
36 #include "e2p/e2p.h"
38 #include <ext2fs/ext2_ext_attr.h>
40 #include "../version.h"
41 #include "jfs_user.h"
42 #include "support/plausible.h"
44 #ifndef BUFSIZ
45 #define BUFSIZ 8192
46 #endif
48 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
49 int journal_enable_debug = -1;
50 #endif
53 * There must be only one definition if we're hooking in extra commands or
54 * changing default prompt. Use -DSKIP_GLOBDEF for that.
56 #ifndef SKIP_GLOBDEFS
57 ss_request_table *extra_cmds;
58 const char *debug_prog_name;
59 #endif
60 int ss_sci_idx;
62 ext2_filsys current_fs;
63 quota_ctx_t current_qctx;
64 ext2_ino_t root, cwd;
65 int no_copy_xattrs;
67 static int debugfs_setup_tdb(const char *device_name, char *undo_file,
68 io_manager *io_ptr)
70 errcode_t retval = ENOMEM;
71 const char *tdb_dir = NULL;
72 char *tdb_file = NULL;
73 char *dev_name, *tmp_name;
75 /* (re)open a specific undo file */
76 if (undo_file && undo_file[0] != 0) {
77 retval = set_undo_io_backing_manager(*io_ptr);
78 if (retval)
79 goto err;
80 *io_ptr = undo_io_manager;
81 retval = set_undo_io_backup_file(undo_file);
82 if (retval)
83 goto err;
84 printf("Overwriting existing filesystem; this can be undone "
85 "using the command:\n"
86 " e2undo %s %s\n\n",
87 undo_file, device_name);
88 return retval;
92 * Configuration via a conf file would be
93 * nice
95 tdb_dir = ss_safe_getenv("E2FSPROGS_UNDO_DIR");
96 if (!tdb_dir)
97 tdb_dir = "/var/lib/e2fsprogs";
99 if (!strcmp(tdb_dir, "none") || (tdb_dir[0] == 0) ||
100 access(tdb_dir, W_OK))
101 return 0;
103 tmp_name = strdup(device_name);
104 if (!tmp_name)
105 goto errout;
106 dev_name = basename(tmp_name);
107 tdb_file = malloc(strlen(tdb_dir) + 9 + strlen(dev_name) + 7 + 1);
108 if (!tdb_file) {
109 free(tmp_name);
110 goto errout;
112 sprintf(tdb_file, "%s/debugfs-%s.e2undo", tdb_dir, dev_name);
113 free(tmp_name);
115 if ((unlink(tdb_file) < 0) && (errno != ENOENT)) {
116 retval = errno;
117 com_err("debugfs", retval,
118 "while trying to delete %s", tdb_file);
119 goto errout;
122 retval = set_undo_io_backing_manager(*io_ptr);
123 if (retval)
124 goto errout;
125 *io_ptr = undo_io_manager;
126 retval = set_undo_io_backup_file(tdb_file);
127 if (retval)
128 goto errout;
129 printf("Overwriting existing filesystem; this can be undone "
130 "using the command:\n"
131 " e2undo %s %s\n\n", tdb_file, device_name);
133 free(tdb_file);
134 return 0;
135 errout:
136 free(tdb_file);
137 err:
138 com_err("debugfs", retval, "while trying to setup undo file\n");
139 return retval;
142 static void open_filesystem(char *device, int open_flags, blk64_t superblock,
143 blk64_t blocksize, int catastrophic,
144 char *data_filename, char *undo_file)
146 int retval;
147 io_channel data_io = 0;
148 io_manager io_ptr = unix_io_manager;
150 if (superblock != 0 && blocksize == 0) {
151 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
152 current_fs = NULL;
153 return;
156 if (data_filename) {
157 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
158 com_err(device, 0,
159 "The -d option is only valid when reading an e2image file");
160 current_fs = NULL;
161 return;
163 retval = unix_io_manager->open(data_filename, 0, &data_io);
164 if (retval) {
165 com_err(data_filename, 0, "while opening data source");
166 current_fs = NULL;
167 return;
171 if (catastrophic)
172 open_flags |= EXT2_FLAG_SKIP_MMP | EXT2_FLAG_IGNORE_SB_ERRORS;
174 if (undo_file) {
175 retval = debugfs_setup_tdb(device, undo_file, &io_ptr);
176 if (retval)
177 exit(1);
180 try_open_again:
181 retval = ext2fs_open(device, open_flags, superblock, blocksize,
182 io_ptr, &current_fs);
183 if (retval && (retval == EXT2_ET_SB_CSUM_INVALID) &&
184 !(open_flags & EXT2_FLAG_IGNORE_CSUM_ERRORS)) {
185 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
186 printf("Checksum errors in superblock! Retrying...\n");
187 goto try_open_again;
189 if (retval) {
190 com_err(debug_prog_name, retval,
191 "while trying to open %s", device);
192 if (retval == EXT2_ET_BAD_MAGIC)
193 check_plausibility(device, CHECK_FS_EXIST, NULL);
194 current_fs = NULL;
195 return;
197 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
199 if (!catastrophic) {
200 retval = ext2fs_read_bitmaps(current_fs);
201 if (retval) {
202 com_err(device, retval,
203 "while reading allocation bitmaps");
204 goto errout;
208 if (data_io) {
209 retval = ext2fs_set_data_io(current_fs, data_io);
210 if (retval) {
211 com_err(device, retval,
212 "while setting data source");
213 goto errout;
217 root = cwd = EXT2_ROOT_INO;
218 return;
220 errout:
221 retval = ext2fs_close_free(&current_fs);
222 if (retval)
223 com_err(device, retval, "while trying to close filesystem");
226 void do_open_filesys(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
227 void *infop EXT2FS_ATTR((unused)))
229 int c, err;
230 int catastrophic = 0;
231 blk64_t superblock = 0;
232 blk64_t blocksize = 0;
233 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS |
234 EXT2_FLAG_THREADS;
235 char *data_filename = 0;
236 char *undo_file = NULL;
238 reset_getopt();
239 while ((c = getopt(argc, argv, "iwfecb:s:d:Dz:")) != EOF) {
240 switch (c) {
241 case 'i':
242 open_flags |= EXT2_FLAG_IMAGE_FILE;
243 break;
244 case 'w':
245 #ifdef READ_ONLY
246 goto print_usage;
247 #else
248 open_flags |= EXT2_FLAG_RW;
249 #endif /* READ_ONLY */
250 break;
251 case 'f':
252 open_flags |= EXT2_FLAG_FORCE;
253 break;
254 case 'e':
255 open_flags |= EXT2_FLAG_EXCLUSIVE;
256 break;
257 case 'c':
258 catastrophic = 1;
259 break;
260 case 'd':
261 data_filename = optarg;
262 break;
263 case 'D':
264 open_flags |= EXT2_FLAG_DIRECT_IO;
265 break;
266 case 'b':
267 blocksize = parse_ulong(optarg, argv[0],
268 "block size", &err);
269 if (err)
270 return;
271 break;
272 case 's':
273 err = strtoblk(argv[0], optarg,
274 "superblock block number", &superblock);
275 if (err)
276 return;
277 break;
278 case 'z':
279 #ifdef READ_ONLY
280 goto print_usage;
281 #else
282 undo_file = optarg;
283 #endif
284 break;
285 default:
286 goto print_usage;
289 if (optind != argc-1) {
290 goto print_usage;
292 if (check_fs_not_open(argv[0]))
293 return;
294 open_filesystem(argv[optind], open_flags,
295 superblock, blocksize, catastrophic,
296 data_filename, undo_file);
297 return;
299 print_usage:
300 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
301 #ifdef READ_ONLY
302 "[-d image_filename] [-z undo_file] [-c] [-i] [-f] [-e] [-D] "
303 #else
304 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] [-w] "
305 #endif
306 "<device>\n", argv[0]);
309 void do_lcd(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
310 void *infop EXT2FS_ATTR((unused)))
312 if (argc != 2) {
313 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
314 return;
317 if (chdir(argv[1]) == -1) {
318 com_err(argv[0], errno,
319 "while trying to change native directory to %s",
320 argv[1]);
321 return;
325 static void close_filesystem(NOARGS)
327 int retval;
329 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
330 retval = ext2fs_write_inode_bitmap(current_fs);
331 if (retval)
332 com_err("ext2fs_write_inode_bitmap", retval, 0);
334 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
335 retval = ext2fs_write_block_bitmap(current_fs);
336 if (retval)
337 com_err("ext2fs_write_block_bitmap", retval, 0);
339 if (current_qctx)
340 quota_release_context(&current_qctx);
341 retval = ext2fs_close_free(&current_fs);
342 if (retval)
343 com_err("ext2fs_close", retval, 0);
344 return;
347 void do_close_filesys(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
348 void *infop EXT2FS_ATTR((unused)))
350 int c;
352 if (check_fs_open(argv[0]))
353 return;
355 reset_getopt();
356 while ((c = getopt (argc, argv, "a")) != EOF) {
357 switch (c) {
358 case 'a':
359 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
360 break;
361 default:
362 goto print_usage;
366 if (argc > optind) {
367 print_usage:
368 com_err(0, 0, "Usage: close_filesys [-a]");
369 return;
372 close_filesystem();
375 #ifndef READ_ONLY
376 void do_init_filesys(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
377 void *infop EXT2FS_ATTR((unused)))
379 struct ext2_super_block param;
380 errcode_t retval;
381 int err;
382 blk64_t blocks;
384 if (common_args_process(argc, argv, 3, 3, "initialize",
385 "<device> <blocks>", CHECK_FS_NOTOPEN))
386 return;
388 memset(&param, 0, sizeof(struct ext2_super_block));
389 err = strtoblk(argv[0], argv[2], "blocks count", &blocks);
390 if (err)
391 return;
392 ext2fs_blocks_count_set(&param, blocks);
393 retval = ext2fs_initialize(argv[1], 0, &param,
394 unix_io_manager, &current_fs);
395 if (retval) {
396 com_err(argv[1], retval, "while initializing filesystem");
397 current_fs = NULL;
398 return;
400 root = cwd = EXT2_ROOT_INO;
401 return;
404 static void print_features(struct ext2_super_block * s, FILE *f)
406 int i, j, printed=0;
407 __u32 *mask = &s->s_feature_compat, m;
409 fputs("Filesystem features:", f);
410 for (i=0; i <3; i++,mask++) {
411 for (j=0,m=1; j < 32; j++, m<<=1) {
412 if (*mask & m) {
413 fprintf(f, " %s", e2p_feature2string(i, m));
414 printed++;
418 if (printed == 0)
419 fputs("(none)", f);
420 fputs("\n", f);
422 #endif /* READ_ONLY */
424 static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
425 const char *str, int *first, FILE *f)
427 if (ext2fs_bg_flags_test(fs, group, mask)) {
428 if (*first) {
429 fputs(" [", f);
430 *first = 0;
431 } else
432 fputs(", ", f);
433 fputs(str, f);
437 void do_show_super_stats(int argc, ss_argv_t argv,
438 int sci_idx EXT2FS_ATTR((unused)),
439 void *infop EXT2FS_ATTR((unused)))
441 const char *units ="block";
442 dgrp_t i;
443 FILE *out;
444 int c, header_only = 0;
445 int numdirs = 0, first, gdt_csum;
447 reset_getopt();
448 while ((c = getopt (argc, argv, "h")) != EOF) {
449 switch (c) {
450 case 'h':
451 header_only++;
452 break;
453 default:
454 goto print_usage;
457 if (optind != argc) {
458 goto print_usage;
460 if (check_fs_open(argv[0]))
461 return;
462 out = open_pager();
464 if (ext2fs_has_feature_bigalloc(current_fs->super))
465 units = "cluster";
467 list_super2(current_fs->super, out);
468 if (ext2fs_has_feature_metadata_csum(current_fs->super) &&
469 !ext2fs_superblock_csum_verify(current_fs,
470 current_fs->super)) {
471 __u32 orig_csum = current_fs->super->s_checksum;
473 ext2fs_superblock_csum_set(current_fs,
474 current_fs->super);
475 fprintf(out, "Expected Checksum: 0x%08x\n",
476 current_fs->super->s_checksum);
477 current_fs->super->s_checksum = orig_csum;
479 for (i=0; i < current_fs->group_desc_count; i++)
480 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
481 fprintf(out, "Directories: %u\n", numdirs);
483 if (header_only) {
484 close_pager(out);
485 return;
488 gdt_csum = ext2fs_has_group_desc_csum(current_fs);
489 for (i = 0; i < current_fs->group_desc_count; i++) {
490 fprintf(out, " Group %2d: block bitmap at %llu, "
491 "inode bitmap at %llu, "
492 "inode table at %llu\n"
493 " %u free %s%s, "
494 "%u free %s, "
495 "%u used %s%s", i,
496 (unsigned long long) ext2fs_block_bitmap_loc(current_fs, i),
497 (unsigned long long) ext2fs_inode_bitmap_loc(current_fs, i),
498 (unsigned long long) ext2fs_inode_table_loc(current_fs, i),
499 ext2fs_bg_free_blocks_count(current_fs, i),
500 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;
524 if (!first)
525 fputs("]\n", out);
527 close_pager(out);
528 return;
529 print_usage:
530 fprintf(stderr, "%s: Usage: show_super_stats [-h]\n", argv[0]);
533 #ifndef READ_ONLY
534 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
535 ss_argv_t argv EXT2FS_ATTR((unused)),
536 int sci_idx EXT2FS_ATTR((unused)),
537 void *infop EXT2FS_ATTR((unused)))
539 if (check_fs_open(argv[0]))
540 return;
541 if (check_fs_read_write(argv[0]))
542 return;
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);
550 #endif /* READ_ONLY */
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;
560 static void finish_range(struct list_blocks_struct *lb)
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,
571 (unsigned long long) lb->first_block);
572 else
573 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
574 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
575 (unsigned long long) lb->first_block,
576 (unsigned long long) lb->last_block);
577 lb->first_block = 0;
580 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
581 blk64_t *blocknr, e2_blkcnt_t blockcnt,
582 blk64_t ref_block EXT2FS_ATTR((unused)),
583 int ref_offset EXT2FS_ATTR((unused)),
584 void *private)
586 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
588 lb->total++;
589 if (blockcnt >= 0) {
591 * See if we can add on to the existing range (if it exists)
593 if (lb->first_block &&
594 (lb->last_block+1 == *blocknr) &&
595 (lb->last_bcnt+1 == blockcnt)) {
596 lb->last_block = *blocknr;
597 lb->last_bcnt = blockcnt;
598 return 0;
601 * Start a new range.
603 finish_range(lb);
604 lb->first_block = lb->last_block = *blocknr;
605 lb->first_bcnt = lb->last_bcnt = blockcnt;
606 return 0;
609 * Not a normal block. Always force a new range.
611 finish_range(lb);
612 if (lb->first)
613 lb->first = 0;
614 else
615 fprintf(lb->f, ", ");
616 if (blockcnt == -1)
617 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
618 else if (blockcnt == -2)
619 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
620 else if (blockcnt == -3)
621 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
622 return 0;
625 static void internal_dump_inode_extra(FILE *out,
626 const char *prefix EXT2FS_ATTR((unused)),
627 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
628 struct ext2_inode_large *inode)
630 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
631 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
632 EXT2_GOOD_OLD_INODE_SIZE) {
633 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
634 inode->i_extra_isize);
635 return;
639 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
641 struct list_blocks_struct lb;
643 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
644 lb.total = 0;
645 lb.first_block = 0;
646 lb.f = f;
647 lb.first = 1;
648 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
649 list_blocks_proc, (void *)&lb);
650 finish_range(&lb);
651 if (lb.total)
652 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
653 fprintf(f,"\n");
656 static int int_log10(unsigned long long arg)
658 int l = 0;
660 arg = arg / 10;
661 while (arg) {
662 l++;
663 arg = arg / 10;
665 return l;
668 #define DUMP_LEAF_EXTENTS 0x01
669 #define DUMP_NODE_EXTENTS 0x02
670 #define DUMP_EXTENT_TABLE 0x04
672 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
673 int flags, int logical_width, int physical_width)
675 ext2_extent_handle_t handle;
676 struct ext2fs_extent extent;
677 struct ext2_extent_info info;
678 int op = EXT2_EXTENT_ROOT;
679 unsigned int printed = 0;
680 errcode_t errcode;
682 errcode = ext2fs_extent_open(current_fs, ino, &handle);
683 if (errcode)
684 return;
686 if (flags & DUMP_EXTENT_TABLE)
687 fprintf(f, "Level Entries %*s %*s Length Flags\n",
688 (logical_width*2)+3, "Logical",
689 (physical_width*2)+3, "Physical");
690 else
691 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
693 while (1) {
694 errcode = ext2fs_extent_get(handle, op, &extent);
696 if (errcode)
697 break;
699 op = EXT2_EXTENT_NEXT;
701 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
702 continue;
704 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
705 if ((flags & DUMP_LEAF_EXTENTS) == 0)
706 continue;
707 } else {
708 if ((flags & DUMP_NODE_EXTENTS) == 0)
709 continue;
712 errcode = ext2fs_extent_get_info(handle, &info);
713 if (errcode)
714 continue;
716 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
717 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
718 continue;
720 if (flags & DUMP_EXTENT_TABLE) {
721 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
722 "%*llu%*s %6u\n",
723 info.curr_level, info.max_depth,
724 info.curr_entry, info.num_entries,
725 logical_width,
726 (unsigned long long) extent.e_lblk,
727 logical_width,
728 (unsigned long long) extent.e_lblk + (extent.e_len - 1),
729 physical_width,
730 (unsigned long long) extent.e_pblk,
731 physical_width+3, "", extent.e_len);
732 continue;
735 fprintf(f, "%s(ETB%d):%llu",
736 printed ? ", " : "", info.curr_level,
737 (unsigned long long) extent.e_pblk);
738 printed = 1;
739 continue;
742 if (flags & DUMP_EXTENT_TABLE) {
743 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
744 "%*llu - %*llu %6u %s\n",
745 info.curr_level, info.max_depth,
746 info.curr_entry, info.num_entries,
747 logical_width,
748 (unsigned long long) extent.e_lblk,
749 logical_width,
750 (unsigned long long) extent.e_lblk + (extent.e_len - 1),
751 physical_width,
752 (unsigned long long) extent.e_pblk,
753 physical_width,
754 (unsigned long long) extent.e_pblk + (extent.e_len - 1),
755 extent.e_len,
756 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
757 "Uninit" : "");
758 continue;
761 if (extent.e_len == 0)
762 continue;
763 else if (extent.e_len == 1)
764 fprintf(f,
765 "%s(%lld%s):%lld",
766 printed ? ", " : "",
767 (unsigned long long) extent.e_lblk,
768 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
769 "[u]" : "",
770 (unsigned long long) extent.e_pblk);
771 else
772 fprintf(f,
773 "%s(%lld-%lld%s):%lld-%lld",
774 printed ? ", " : "",
775 (unsigned long long) extent.e_lblk,
776 (unsigned long long) extent.e_lblk + (extent.e_len - 1),
777 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
778 "[u]" : "",
779 (unsigned long long) extent.e_pblk,
780 (unsigned long long) extent.e_pblk + (extent.e_len - 1));
781 printed = 1;
783 if (printed)
784 fprintf(f, "\n");
785 ext2fs_extent_free(handle);
788 static void dump_inline_data(FILE *out, const char *prefix, ext2_ino_t inode_num)
790 errcode_t retval;
791 size_t size;
793 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
794 if (!retval)
795 fprintf(out, "%sSize of inline data: %zu\n", prefix, size);
798 static void dump_inline_symlink(FILE *out, ext2_ino_t inode_num,
799 struct ext2_inode *inode, const char *prefix)
801 errcode_t retval;
802 char *buf = NULL;
803 size_t size;
805 retval = ext2fs_inline_data_size(current_fs, inode_num, &size);
806 if (retval)
807 goto out;
809 retval = ext2fs_get_memzero(size + 1, &buf);
810 if (retval)
811 goto out;
813 retval = ext2fs_inline_data_get(current_fs, inode_num,
814 inode, buf, &size);
815 if (retval)
816 goto out;
818 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
819 (int)size, buf);
820 out:
821 if (buf)
822 ext2fs_free_mem(&buf);
823 if (retval)
824 com_err(__func__, retval, "while dumping link destination");
827 void internal_dump_inode(FILE *out, const char *prefix,
828 ext2_ino_t inode_num, struct ext2_inode *inode,
829 int do_dump_blocks)
831 const char *i_type;
832 char frag, fsize;
833 int os = current_fs->super->s_creator_os;
834 struct ext2_inode_large *large_inode;
835 size_t inode_size;
837 large_inode = (struct ext2_inode_large *) inode;
838 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
839 inode_size = ext2fs_inode_actual_size(large_inode);
840 else
841 inode_size = EXT2_GOOD_OLD_INODE_SIZE;
843 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
844 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
845 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
846 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
847 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
848 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
849 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
850 else i_type = "bad type";
851 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
852 fprintf(out, "%sMode: 0%03o Flags: 0x%x\n",
853 prefix, inode->i_mode & 07777, inode->i_flags);
854 if (ext2fs_inode_includes(inode_size, i_version_hi)) {
855 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
856 prefix, inode->i_generation, large_inode->i_version_hi,
857 inode->osd1.linux1.l_i_version);
858 } else {
859 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
860 inode->i_generation, inode->osd1.linux1.l_i_version);
862 fprintf(out, "%sUser: %5d Group: %5d",
863 prefix, inode_uid(*inode), inode_gid(*inode));
864 if (ext2fs_inode_includes(inode_size, i_projid))
865 fprintf(out, " Project: %5d", large_inode->i_projid);
866 fputs(" Size: ", out);
867 if (LINUX_S_ISREG(inode->i_mode) || LINUX_S_ISDIR(inode->i_mode))
868 fprintf(out, "%llu\n", (unsigned long long) EXT2_I_SIZE(inode));
869 else
870 fprintf(out, "%u\n", inode->i_size);
871 if (os == EXT2_OS_HURD)
872 fprintf(out,
873 "%sFile ACL: %u Translator: %u\n",
874 prefix,
875 inode->i_file_acl,
876 inode->osd1.hurd1.h_i_translator);
877 else
878 fprintf(out, "%sFile ACL: %llu\n",
879 prefix,
880 inode->i_file_acl | ((long long)
881 (inode->osd2.linux2.l_i_file_acl_high) << 32));
882 if (os != EXT2_OS_HURD)
883 fprintf(out, "%sLinks: %u Blockcount: %llu\n",
884 prefix, inode->i_links_count,
885 (((unsigned long long)
886 inode->osd2.linux2.l_i_blocks_hi << 32)) +
887 inode->i_blocks);
888 else
889 fprintf(out, "%sLinks: %u Blockcount: %u\n",
890 prefix, inode->i_links_count, inode->i_blocks);
891 switch (os) {
892 case EXT2_OS_HURD:
893 frag = inode->osd2.hurd2.h_i_frag;
894 fsize = inode->osd2.hurd2.h_i_fsize;
895 break;
896 default:
897 frag = fsize = 0;
899 fprintf(out, "%sFragment: Address: %u Number: %u Size: %u\n",
900 prefix, inode->i_faddr, frag, fsize);
901 if (ext2fs_inode_includes(inode_size, i_ctime_extra))
902 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
903 inode->i_ctime, large_inode->i_ctime_extra,
904 time_to_string(ext2fs_inode_xtime_get(large_inode,
905 i_ctime)));
906 else
907 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
908 time_to_string((__s32) inode->i_ctime));
909 if (ext2fs_inode_includes(inode_size, i_atime_extra))
910 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
911 inode->i_atime, large_inode->i_atime_extra,
912 time_to_string(ext2fs_inode_xtime_get(large_inode,
913 i_atime)));
914 else
915 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
916 time_to_string((__s32) inode->i_atime));
917 if (ext2fs_inode_includes(inode_size, i_mtime_extra))
918 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
919 inode->i_mtime, large_inode->i_mtime_extra,
920 time_to_string(ext2fs_inode_xtime_get(large_inode,
921 i_mtime)));
922 else
923 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
924 time_to_string((__s32) inode->i_mtime));
925 if (ext2fs_inode_includes(inode_size, i_crtime_extra))
926 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
927 large_inode->i_crtime, large_inode->i_crtime_extra,
928 time_to_string(ext2fs_inode_xtime_get(large_inode,
929 i_crtime)));
930 if (inode->i_dtime) {
931 if (ext2fs_inode_includes(inode_size, i_ctime_extra)) {
932 time_t tm;
934 /* dtime doesn't have its own i_dtime_extra field, so
935 * approximate this with i_ctime_extra instead. */
936 tm = __decode_extra_sec(inode->i_dtime,
937 large_inode->i_ctime_extra);
938 fprintf(out, "%s dtime: 0x%08x:(%08x) -- %s", prefix,
939 inode->i_dtime, large_inode->i_ctime_extra,
940 time_to_string(tm));
941 } else {
942 fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
943 inode->i_dtime,
944 time_to_string((__s32) inode->i_dtime));
947 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
948 internal_dump_inode_extra(out, prefix, inode_num,
949 (struct ext2_inode_large *) inode);
950 dump_inode_attributes(out, inode_num);
951 if (ext2fs_has_feature_metadata_csum(current_fs->super)) {
952 __u32 crc = inode->i_checksum_lo;
953 if (ext2fs_inode_includes(inode_size, i_checksum_hi))
954 crc |= ((__u32)large_inode->i_checksum_hi) << 16;
955 fprintf(out, "Inode checksum: 0x%08x\n", crc);
958 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_is_fast_symlink(inode))
959 fprintf(out, "%sFast link dest: \"%.*s\"\n", prefix,
960 (int)EXT2_I_SIZE(inode), (char *)inode->i_block);
961 else if (LINUX_S_ISLNK(inode->i_mode) &&
962 (inode->i_flags & EXT4_INLINE_DATA_FL))
963 dump_inline_symlink(out, inode_num, inode, prefix);
964 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
965 int major, minor;
966 const char *devnote;
968 if (inode->i_block[0]) {
969 major = (inode->i_block[0] >> 8) & 255;
970 minor = inode->i_block[0] & 255;
971 devnote = "";
972 } else {
973 major = (inode->i_block[1] & 0xfff00) >> 8;
974 minor = ((inode->i_block[1] & 0xff) |
975 ((inode->i_block[1] >> 12) & 0xfff00));
976 devnote = "(New-style) ";
978 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
979 devnote, major, minor, major, minor);
980 } else if (do_dump_blocks) {
981 if (inode->i_flags & EXT4_EXTENTS_FL)
982 dump_extents(out, prefix, inode_num,
983 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
984 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
985 dump_inline_data(out, prefix, inode_num);
986 else
987 dump_blocks(out, prefix, inode_num);
991 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
993 FILE *out;
995 out = open_pager();
996 internal_dump_inode(out, "", inode_num, inode, 1);
997 close_pager(out);
1000 void do_stat(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1001 void *infop EXT2FS_ATTR((unused)))
1003 ext2_ino_t inode;
1004 struct ext2_inode * inode_buf;
1006 if (check_fs_open(argv[0]))
1007 return;
1009 inode_buf = (struct ext2_inode *)
1010 malloc(EXT2_INODE_SIZE(current_fs->super));
1011 if (!inode_buf) {
1012 fprintf(stderr, "do_stat: can't allocate buffer\n");
1013 return;
1016 if (common_inode_args_process(argc, argv, &inode, 0)) {
1017 free(inode_buf);
1018 return;
1021 if (debugfs_read_inode2(inode, inode_buf, argv[0],
1022 EXT2_INODE_SIZE(current_fs->super), 0)) {
1023 free(inode_buf);
1024 return;
1027 dump_inode(inode, inode_buf);
1028 free(inode_buf);
1029 return;
1032 void do_dump_extents(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1033 void *infop EXT2FS_ATTR((unused)))
1035 struct ext2_inode inode;
1036 ext2_ino_t ino;
1037 FILE *out;
1038 int c, flags = 0;
1039 int logical_width;
1040 int physical_width;
1042 reset_getopt();
1043 while ((c = getopt(argc, argv, "nl")) != EOF) {
1044 switch (c) {
1045 case 'n':
1046 flags |= DUMP_NODE_EXTENTS;
1047 break;
1048 case 'l':
1049 flags |= DUMP_LEAF_EXTENTS;
1050 break;
1054 if (argc != optind + 1) {
1055 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1056 return;
1059 if (flags == 0)
1060 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1061 flags |= DUMP_EXTENT_TABLE;
1063 if (check_fs_open(argv[0]))
1064 return;
1066 ino = string_to_inode(argv[optind]);
1067 if (ino == 0)
1068 return;
1070 if (debugfs_read_inode(ino, &inode, argv[0]))
1071 return;
1073 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1074 fprintf(stderr, "%s: does not uses extent block maps\n",
1075 argv[optind]);
1076 return;
1079 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1080 current_fs->blocksize) + 1;
1081 if (logical_width < 5)
1082 logical_width = 5;
1083 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1084 if (physical_width < 5)
1085 physical_width = 5;
1087 out = open_pager();
1088 dump_extents(out, "", ino, flags, logical_width, physical_width);
1089 close_pager(out);
1090 return;
1093 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1094 blk64_t *blocknr,
1095 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1096 blk64_t ref_block EXT2FS_ATTR((unused)),
1097 int ref_offset EXT2FS_ATTR((unused)),
1098 void *private EXT2FS_ATTR((unused)))
1100 printf("%llu ", (unsigned long long) *blocknr);
1101 return 0;
1104 void do_blocks(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1105 void *infop EXT2FS_ATTR((unused)))
1107 ext2_ino_t inode;
1109 if (check_fs_open(argv[0]))
1110 return;
1112 if (common_inode_args_process(argc, argv, &inode, 0)) {
1113 return;
1116 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1117 print_blocks_proc, NULL);
1118 fputc('\n', stdout);
1119 return;
1122 void do_chroot(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1123 void *infop EXT2FS_ATTR((unused)))
1125 ext2_ino_t inode;
1126 int retval;
1128 if (common_inode_args_process(argc, argv, &inode, 0))
1129 return;
1131 retval = ext2fs_check_directory(current_fs, inode);
1132 if (retval) {
1133 com_err(argv[1], retval, 0);
1134 return;
1136 root = inode;
1139 #ifndef READ_ONLY
1140 void do_clri(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1141 void *infop EXT2FS_ATTR((unused)))
1143 ext2_ino_t inode;
1144 struct ext2_inode inode_buf;
1146 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1147 return;
1149 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1150 return;
1151 memset(&inode_buf, 0, sizeof(inode_buf));
1152 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1153 return;
1156 void do_freei(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1157 void *infop EXT2FS_ATTR((unused)))
1159 unsigned int len = 1;
1160 int err = 0;
1161 ext2_ino_t inode;
1163 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1164 CHECK_FS_RW | CHECK_FS_BITMAPS))
1165 return;
1166 if (check_fs_read_write(argv[0]))
1167 return;
1169 inode = string_to_inode(argv[1]);
1170 if (!inode)
1171 return;
1173 if (argc == 3) {
1174 len = parse_ulong(argv[2], argv[0], "length", &err);
1175 if (err)
1176 return;
1179 if (len == 1 &&
1180 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1181 com_err(argv[0], 0, "Warning: inode already clear");
1182 while (len-- > 0)
1183 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1184 ext2fs_mark_ib_dirty(current_fs);
1187 void do_seti(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1188 void *infop EXT2FS_ATTR((unused)))
1190 unsigned int len = 1;
1191 int err = 0;
1192 ext2_ino_t inode;
1194 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1195 CHECK_FS_RW | CHECK_FS_BITMAPS))
1196 return;
1197 if (check_fs_read_write(argv[0]))
1198 return;
1200 inode = string_to_inode(argv[1]);
1201 if (!inode)
1202 return;
1204 if (argc == 3) {
1205 len = parse_ulong(argv[2], argv[0], "length", &err);
1206 if (err)
1207 return;
1210 if ((len == 1) &&
1211 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1212 com_err(argv[0], 0, "Warning: inode already set");
1213 while (len-- > 0)
1214 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1215 ext2fs_mark_ib_dirty(current_fs);
1217 #endif /* READ_ONLY */
1219 void do_testi(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1220 void *infop EXT2FS_ATTR((unused)))
1222 ext2_ino_t inode;
1224 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1225 return;
1227 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1228 printf("Inode %u is marked in use\n", inode);
1229 else
1230 printf("Inode %u is not in use\n", inode);
1233 #ifndef READ_ONLY
1234 void do_freeb(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1235 void *infop EXT2FS_ATTR((unused)))
1237 blk64_t block;
1238 blk64_t count = 1;
1240 if (common_block_args_process(argc, argv, &block, &count))
1241 return;
1242 if (check_fs_read_write(argv[0]))
1243 return;
1244 while (count-- > 0) {
1245 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1246 com_err(argv[0], 0, "Warning: block %llu already clear",
1247 (unsigned long long) block);
1248 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1249 block++;
1251 ext2fs_mark_bb_dirty(current_fs);
1254 void do_setb(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1255 void *infop EXT2FS_ATTR((unused)))
1257 blk64_t block;
1258 blk64_t count = 1;
1260 if (common_block_args_process(argc, argv, &block, &count))
1261 return;
1262 if (check_fs_read_write(argv[0]))
1263 return;
1264 while (count-- > 0) {
1265 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1266 com_err(argv[0], 0, "Warning: block %llu already set",
1267 (unsigned long long) block);
1268 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1269 block++;
1271 ext2fs_mark_bb_dirty(current_fs);
1273 #endif /* READ_ONLY */
1275 void do_testb(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1276 void *infop EXT2FS_ATTR((unused)))
1278 blk64_t block;
1279 blk64_t count = 1;
1281 if (common_block_args_process(argc, argv, &block, &count))
1282 return;
1283 while (count-- > 0) {
1284 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1285 printf("Block %llu marked in use\n",
1286 (unsigned long long) block);
1287 else
1288 printf("Block %llu not in use\n",
1289 (unsigned long long) block);
1290 block++;
1294 #ifndef READ_ONLY
1295 static void modify_u8(char *com, const char *prompt,
1296 const char *format, __u8 *val)
1298 char buf[200];
1299 unsigned long v;
1300 char *tmp;
1302 sprintf(buf, format, *val);
1303 printf("%30s [%s] ", prompt, buf);
1304 if (!fgets(buf, sizeof(buf), stdin))
1305 return;
1306 if (buf[strlen (buf) - 1] == '\n')
1307 buf[strlen (buf) - 1] = '\0';
1308 if (!buf[0])
1309 return;
1310 v = strtoul(buf, &tmp, 0);
1311 if (*tmp)
1312 com_err(com, 0, "Bad value - %s", buf);
1313 else
1314 *val = v;
1317 static void modify_u16(char *com, const char *prompt,
1318 const char *format, __u16 *val)
1320 char buf[200];
1321 unsigned long v;
1322 char *tmp;
1324 sprintf(buf, format, *val);
1325 printf("%30s [%s] ", prompt, buf);
1326 if (!fgets(buf, sizeof(buf), stdin))
1327 return;
1328 if (buf[strlen (buf) - 1] == '\n')
1329 buf[strlen (buf) - 1] = '\0';
1330 if (!buf[0])
1331 return;
1332 v = strtoul(buf, &tmp, 0);
1333 if (*tmp)
1334 com_err(com, 0, "Bad value - %s", buf);
1335 else
1336 *val = v;
1339 static void modify_u32(char *com, const char *prompt,
1340 const char *format, __u32 *val)
1342 char buf[200];
1343 unsigned long v;
1344 char *tmp;
1346 sprintf(buf, format, *val);
1347 printf("%30s [%s] ", prompt, buf);
1348 if (!fgets(buf, sizeof(buf), stdin))
1349 return;
1350 if (buf[strlen (buf) - 1] == '\n')
1351 buf[strlen (buf) - 1] = '\0';
1352 if (!buf[0])
1353 return;
1354 v = strtoul(buf, &tmp, 0);
1355 if (*tmp)
1356 com_err(com, 0, "Bad value - %s", buf);
1357 else
1358 *val = v;
1362 void do_modify_inode(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1363 void *infop EXT2FS_ATTR((unused)))
1365 struct ext2_inode inode;
1366 ext2_ino_t inode_num;
1367 int i;
1368 unsigned char *frag, *fsize;
1369 char buf[80];
1370 int os;
1371 const char *hex_format = "0x%x";
1372 const char *octal_format = "0%o";
1373 const char *decimal_format = "%d";
1374 const char *unsignedlong_format = "%lu";
1376 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1377 return;
1379 os = current_fs->super->s_creator_os;
1381 if (debugfs_read_inode(inode_num, &inode, argv[1]))
1382 return;
1384 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1385 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1386 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1387 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1388 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1389 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1390 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1391 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1392 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1393 if (os == EXT2_OS_LINUX)
1394 modify_u16(argv[0], "Block count high", unsignedlong_format,
1395 &inode.osd2.linux2.l_i_blocks_hi);
1396 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1397 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1398 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1399 #if 0
1400 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1401 #endif
1402 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1404 modify_u32(argv[0], "High 32bits of size", decimal_format,
1405 &inode.i_size_high);
1407 if (os == EXT2_OS_HURD)
1408 modify_u32(argv[0], "Translator Block",
1409 decimal_format, &inode.osd1.hurd1.h_i_translator);
1411 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1412 switch (os) {
1413 case EXT2_OS_HURD:
1414 frag = &inode.osd2.hurd2.h_i_frag;
1415 fsize = &inode.osd2.hurd2.h_i_fsize;
1416 break;
1417 default:
1418 frag = fsize = 0;
1420 if (frag)
1421 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1422 if (fsize)
1423 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1425 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1426 sprintf(buf, "Direct Block #%u", i);
1427 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1429 modify_u32(argv[0], "Indirect Block", decimal_format,
1430 &inode.i_block[EXT2_IND_BLOCK]);
1431 modify_u32(argv[0], "Double Indirect Block", decimal_format,
1432 &inode.i_block[EXT2_DIND_BLOCK]);
1433 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1434 &inode.i_block[EXT2_TIND_BLOCK]);
1435 if (debugfs_write_inode(inode_num, &inode, argv[1]))
1436 return;
1438 #endif /* READ_ONLY */
1440 void do_change_working_dir(int argc, ss_argv_t argv,
1441 int sci_idx EXT2FS_ATTR((unused)),
1442 void *infop EXT2FS_ATTR((unused)))
1444 ext2_ino_t inode;
1445 int retval;
1447 if (common_inode_args_process(argc, argv, &inode, 0))
1448 return;
1450 retval = ext2fs_check_directory(current_fs, inode);
1451 if (retval) {
1452 com_err(argv[1], retval, 0);
1453 return;
1455 cwd = inode;
1456 return;
1459 void do_print_working_directory(int argc, ss_argv_t argv,
1460 int sci_idx EXT2FS_ATTR((unused)),
1461 void *infop EXT2FS_ATTR((unused)))
1463 int retval;
1464 char *pathname = NULL;
1466 if (common_args_process(argc, argv, 1, 1,
1467 "print_working_directory", "", 0))
1468 return;
1470 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1471 if (retval) {
1472 com_err(argv[0], retval,
1473 "while trying to get pathname of cwd");
1475 printf("[pwd] INODE: %6u PATH: %s\n",
1476 cwd, pathname ? pathname : "NULL");
1477 if (pathname) {
1478 free(pathname);
1479 pathname = NULL;
1481 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1482 if (retval) {
1483 com_err(argv[0], retval,
1484 "while trying to get pathname of root");
1486 printf("[root] INODE: %6u PATH: %s\n",
1487 root, pathname ? pathname : "NULL");
1488 if (pathname) {
1489 free(pathname);
1490 pathname = NULL;
1492 return;
1495 #ifndef READ_ONLY
1496 static void make_link(char *sourcename, char *destname)
1498 ext2_ino_t ino;
1499 struct ext2_inode inode;
1500 int retval;
1501 ext2_ino_t dir;
1502 char *dest, *cp, *base_name;
1505 * Get the source inode
1507 ino = string_to_inode(sourcename);
1508 if (!ino)
1509 return;
1510 base_name = strrchr(sourcename, '/');
1511 if (base_name)
1512 base_name++;
1513 else
1514 base_name = sourcename;
1516 * Figure out the destination. First see if it exists and is
1517 * a directory.
1519 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1520 dest = base_name;
1521 else {
1523 * OK, it doesn't exist. See if it is
1524 * '<dir>/basename' or 'basename'
1526 cp = strrchr(destname, '/');
1527 if (cp) {
1528 *cp = 0;
1529 dir = string_to_inode(destname);
1530 if (!dir)
1531 return;
1532 dest = cp+1;
1533 } else {
1534 dir = cwd;
1535 dest = destname;
1539 if (debugfs_read_inode(ino, &inode, sourcename))
1540 return;
1542 retval = ext2fs_link(current_fs, dir, dest, ino,
1543 ext2_file_type(inode.i_mode));
1544 if (retval)
1545 com_err("make_link", retval, 0);
1546 return;
1550 void do_link(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1551 void *infop EXT2FS_ATTR((unused)))
1553 if (common_args_process(argc, argv, 3, 3, "link",
1554 "<source file> <dest_name>", CHECK_FS_RW))
1555 return;
1557 make_link(argv[1], argv[2]);
1560 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1561 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1562 blk64_t ref_block EXT2FS_ATTR((unused)),
1563 int ref_offset EXT2FS_ATTR((unused)),
1564 void *private EXT2FS_ATTR((unused)))
1566 blk64_t block;
1568 block = *blocknr;
1569 ext2fs_block_alloc_stats2(fs, block, +1);
1570 return 0;
1573 void do_undel(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1574 void *infop EXT2FS_ATTR((unused)))
1576 ext2_ino_t ino;
1577 struct ext2_inode inode;
1579 if (common_args_process(argc, argv, 2, 3, "undelete",
1580 "<inode_num> [dest_name]",
1581 CHECK_FS_RW | CHECK_FS_BITMAPS))
1582 return;
1584 ino = string_to_inode(argv[1]);
1585 if (!ino)
1586 return;
1588 if (debugfs_read_inode(ino, &inode, argv[1]))
1589 return;
1591 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1592 com_err(argv[1], 0, "Inode is not marked as deleted");
1593 return;
1597 * XXX this function doesn't handle changing the links count on the
1598 * parent directory when undeleting a directory.
1600 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1601 inode.i_dtime = 0;
1603 if (debugfs_write_inode(ino, &inode, argv[0]))
1604 return;
1606 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1607 mark_blocks_proc, NULL);
1609 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1611 if (argc > 2)
1612 make_link(argv[1], argv[2]);
1615 static void unlink_file_by_name(char *filename)
1617 int retval;
1618 ext2_ino_t dir;
1619 char *base_name;
1621 base_name = strrchr(filename, '/');
1622 if (base_name) {
1623 *base_name++ = '\0';
1624 dir = string_to_inode(filename);
1625 if (!dir)
1626 return;
1627 } else {
1628 dir = cwd;
1629 base_name = filename;
1631 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1632 if (retval)
1633 com_err("unlink_file_by_name", retval, 0);
1634 return;
1637 void do_unlink(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1638 void *infop EXT2FS_ATTR((unused)))
1640 if (common_args_process(argc, argv, 2, 2, "link",
1641 "<pathname>", CHECK_FS_RW))
1642 return;
1644 unlink_file_by_name(argv[1]);
1647 void do_copy_inode(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1648 void *infop EXT2FS_ATTR((unused)))
1650 ext2_ino_t src_ino, dest_ino;
1651 unsigned char buf[4096];
1653 if (common_args_process(argc, argv, 3, 3, "copy_inode",
1654 "<source file> <dest_name>", CHECK_FS_RW))
1655 return;
1657 src_ino = string_to_inode(argv[1]);
1658 if (!src_ino)
1659 return;
1661 dest_ino = string_to_inode(argv[2]);
1662 if (!dest_ino)
1663 return;
1665 if (debugfs_read_inode2(src_ino, (struct ext2_inode *) buf,
1666 argv[0], sizeof(buf), 0))
1667 return;
1669 if (debugfs_write_inode2(dest_ino, (struct ext2_inode *) buf,
1670 argv[0], sizeof(buf), 0))
1671 return;
1674 #endif /* READ_ONLY */
1676 void do_find_free_block(int argc, ss_argv_t argv,
1677 int sci_idx EXT2FS_ATTR((unused)),
1678 void *infop EXT2FS_ATTR((unused)))
1680 blk64_t free_blk, goal, first_free = 0;
1681 int count;
1682 errcode_t retval;
1683 char *tmp;
1685 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1686 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1687 return;
1689 if (check_fs_open(argv[0]))
1690 return;
1692 if (argc > 1) {
1693 count = strtol(argv[1],&tmp,0);
1694 if (*tmp) {
1695 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1696 return;
1698 } else
1699 count = 1;
1701 if (argc > 2) {
1702 goal = strtol(argv[2], &tmp, 0);
1703 if (*tmp) {
1704 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1705 return;
1708 else
1709 goal = current_fs->super->s_first_data_block;
1711 printf("Free blocks found: ");
1712 free_blk = goal - 1;
1713 while (count-- > 0) {
1714 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1715 &free_blk);
1716 if (first_free) {
1717 if (first_free == free_blk)
1718 break;
1719 } else
1720 first_free = free_blk;
1721 if (retval) {
1722 com_err("ext2fs_new_block", retval, 0);
1723 return;
1724 } else
1725 printf("%llu ", (unsigned long long) free_blk);
1727 printf("\n");
1730 void do_find_free_inode(int argc, ss_argv_t argv,
1731 int sci_idx EXT2FS_ATTR((unused)),
1732 void *infop EXT2FS_ATTR((unused)))
1734 ext2_ino_t free_inode, dir;
1735 int mode;
1736 int retval;
1737 char *tmp;
1739 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1740 com_err(argv[0], 0, "Usage: find_free_inode [dir [mode]]");
1741 return;
1743 if (check_fs_open(argv[0]))
1744 return;
1746 if (argc > 1) {
1747 dir = strtol(argv[1], &tmp, 0);
1748 if (*tmp) {
1749 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1750 return;
1753 else
1754 dir = root;
1755 if (argc > 2) {
1756 mode = strtol(argv[2], &tmp, 0);
1757 if (*tmp) {
1758 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1759 return;
1761 } else
1762 mode = 010755;
1764 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1765 if (retval)
1766 com_err("ext2fs_new_inode", retval, 0);
1767 else
1768 printf("Free inode found: %u\n", free_inode);
1771 #ifndef READ_ONLY
1772 void do_write(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1773 void *infop EXT2FS_ATTR((unused)))
1775 errcode_t retval;
1777 if (common_args_process(argc, argv, 3, 3, "write",
1778 "<native file> <new file>", CHECK_FS_RW))
1779 return;
1781 retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1782 if (retval)
1783 com_err(argv[0], retval, 0);
1786 void do_mknod(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1787 void *infop EXT2FS_ATTR((unused)))
1789 unsigned long major, minor;
1790 errcode_t retval;
1791 int nr;
1792 struct stat st;
1794 if (check_fs_open(argv[0]))
1795 return;
1796 if (argc < 3 || argv[2][1]) {
1797 usage:
1798 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1799 return;
1802 minor = major = 0;
1803 switch (argv[2][0]) {
1804 case 'p':
1805 st.st_mode = S_IFIFO;
1806 nr = 3;
1807 break;
1808 case 'c':
1809 st.st_mode = S_IFCHR;
1810 nr = 5;
1811 break;
1812 case 'b':
1813 st.st_mode = S_IFBLK;
1814 nr = 5;
1815 break;
1816 default:
1817 nr = 0;
1820 if (nr == 5) {
1821 char *end1 = NULL, *end2 = NULL;
1823 major = strtoul(argv[3], &end1, 0);
1824 minor = strtoul(argv[4], &end2, 0);
1825 if (major > 65535 || minor > 65535 ||
1826 (end1 && *end1) || (end2 && *end2))
1827 nr = 0;
1830 if (argc != nr)
1831 goto usage;
1833 st.st_rdev = makedev(major, minor);
1834 retval = do_mknod_internal(current_fs, cwd, argv[1],
1835 st.st_mode, st.st_rdev);
1836 if (retval)
1837 com_err(argv[0], retval, 0);
1840 void do_mkdir(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1841 void *infop EXT2FS_ATTR((unused)))
1843 errcode_t retval;
1845 if (common_args_process(argc, argv, 2, 2, "mkdir",
1846 "<filename>", CHECK_FS_RW))
1847 return;
1849 retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1850 if (retval)
1851 com_err(argv[0], retval, 0);
1855 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1856 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1857 blk64_t ref_block EXT2FS_ATTR((unused)),
1858 int ref_offset EXT2FS_ATTR((unused)),
1859 void *private)
1861 blk64_t block = *blocknr;
1862 blk64_t *last_cluster = (blk64_t *)private;
1863 blk64_t cluster = EXT2FS_B2C(fs, block);
1865 if (cluster == *last_cluster)
1866 return 0;
1868 *last_cluster = cluster;
1870 ext2fs_block_alloc_stats2(fs, block, -1);
1871 return 0;
1874 static void kill_file_by_inode(ext2_ino_t inode)
1876 struct ext2_inode inode_buf;
1878 if (debugfs_read_inode(inode, &inode_buf, 0))
1879 return;
1880 ext2fs_set_dtime(current_fs, &inode_buf);
1881 if (debugfs_write_inode(inode, &inode_buf, 0))
1882 return;
1883 if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1884 blk64_t last_cluster = 0;
1885 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1886 NULL, release_blocks_proc, &last_cluster);
1888 printf("\n");
1889 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1890 LINUX_S_ISDIR(inode_buf.i_mode));
1894 void do_kill_file(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1895 void *infop EXT2FS_ATTR((unused)))
1897 ext2_ino_t inode_num;
1899 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1900 return;
1902 kill_file_by_inode(inode_num);
1905 void do_rm(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1906 void *infop EXT2FS_ATTR((unused)))
1908 int retval;
1909 ext2_ino_t inode_num;
1910 struct ext2_inode inode;
1912 if (common_args_process(argc, argv, 2, 2, "rm",
1913 "<filename>", CHECK_FS_RW))
1914 return;
1916 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1917 if (retval) {
1918 com_err(argv[0], retval, "while trying to resolve filename");
1919 return;
1922 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1923 return;
1925 if (LINUX_S_ISDIR(inode.i_mode)) {
1926 com_err(argv[0], 0, "file is a directory");
1927 return;
1930 --inode.i_links_count;
1931 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1932 return;
1934 unlink_file_by_name(argv[1]);
1935 if (inode.i_links_count == 0)
1936 kill_file_by_inode(inode_num);
1939 struct rd_struct {
1940 ext2_ino_t parent;
1941 int empty;
1944 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1945 int entry EXT2FS_ATTR((unused)),
1946 struct ext2_dir_entry *dirent,
1947 int offset EXT2FS_ATTR((unused)),
1948 int blocksize EXT2FS_ATTR((unused)),
1949 char *buf EXT2FS_ATTR((unused)),
1950 void *private)
1952 struct rd_struct *rds = (struct rd_struct *) private;
1954 if (dirent->inode == 0)
1955 return 0;
1956 if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1957 return 0;
1958 if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1959 (dirent->name[1] == '.')) {
1960 rds->parent = dirent->inode;
1961 return 0;
1963 rds->empty = 0;
1964 return 0;
1967 void do_rmdir(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
1968 void *infop EXT2FS_ATTR((unused)))
1970 int retval;
1971 ext2_ino_t inode_num;
1972 struct ext2_inode inode;
1973 struct rd_struct rds;
1975 if (common_args_process(argc, argv, 2, 2, "rmdir",
1976 "<filename>", CHECK_FS_RW))
1977 return;
1979 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1980 if (retval) {
1981 com_err(argv[0], retval, "while trying to resolve filename");
1982 return;
1985 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1986 return;
1988 if (!LINUX_S_ISDIR(inode.i_mode)) {
1989 com_err(argv[0], 0, "file is not a directory");
1990 return;
1993 rds.parent = 0;
1994 rds.empty = 1;
1996 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1997 0, rmdir_proc, &rds);
1998 if (retval) {
1999 com_err(argv[0], retval, "while iterating over directory");
2000 return;
2002 if (rds.empty == 0) {
2003 com_err(argv[0], 0, "directory not empty");
2004 return;
2007 inode.i_links_count = 0;
2008 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2009 return;
2011 unlink_file_by_name(argv[1]);
2012 kill_file_by_inode(inode_num);
2014 if (rds.parent) {
2015 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2016 return;
2017 if (inode.i_links_count > 1)
2018 inode.i_links_count--;
2019 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2020 return;
2023 #endif /* READ_ONLY */
2025 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
2026 ss_argv_t argv EXT2FS_ATTR((unused)),
2027 int sci_idx EXT2FS_ATTR((unused)),
2028 void *infop EXT2FS_ATTR((unused)))
2030 if (current_fs)
2031 printf("Open mode: read-%s\n",
2032 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2033 printf("Filesystem in use: %s\n",
2034 current_fs ? current_fs->device_name : "--none--");
2037 #ifndef READ_ONLY
2038 void do_expand_dir(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2039 void *infop EXT2FS_ATTR((unused)))
2041 ext2_ino_t inode;
2042 int retval;
2044 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
2045 return;
2047 retval = ext2fs_expand_dir(current_fs, inode);
2048 if (retval)
2049 com_err("ext2fs_expand_dir", retval, 0);
2050 return;
2053 void do_features(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2054 void *infop EXT2FS_ATTR((unused)))
2056 int i;
2058 if (check_fs_open(argv[0]))
2059 return;
2061 if ((argc != 1) && check_fs_read_write(argv[0]))
2062 return;
2063 for (i=1; i < argc; i++) {
2064 if (e2p_edit_feature(argv[i],
2065 &current_fs->super->s_feature_compat, 0))
2066 com_err(argv[0], 0, "Unknown feature: %s\n",
2067 argv[i]);
2068 else
2069 ext2fs_mark_super_dirty(current_fs);
2071 print_features(current_fs->super, stdout);
2073 #endif /* READ_ONLY */
2075 void do_bmap(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2076 void *infop EXT2FS_ATTR((unused)))
2078 ext2_ino_t ino;
2079 blk64_t blk, pblk = 0;
2080 int c, err, flags = 0, ret_flags = 0;
2081 errcode_t errcode;
2083 if (check_fs_open(argv[0]))
2084 return;
2086 reset_getopt();
2087 while ((c = getopt (argc, argv, "a")) != EOF) {
2088 switch (c) {
2089 case 'a':
2090 flags |= BMAP_ALLOC;
2091 break;
2092 default:
2093 goto print_usage;
2097 if (argc <= optind+1) {
2098 print_usage:
2099 com_err(0, 0,
2100 "Usage: bmap [-a] <file> logical_blk [physical_blk]");
2101 return;
2104 ino = string_to_inode(argv[optind++]);
2105 if (!ino)
2106 return;
2107 err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
2108 if (err)
2109 return;
2111 if (argc > optind+1)
2112 goto print_usage;
2114 if (argc == optind+1) {
2115 err = strtoblk(argv[0], argv[optind++],
2116 "physical block", &pblk);
2117 if (err)
2118 return;
2119 if (flags & BMAP_ALLOC) {
2120 com_err(0, 0, "Can't set and allocate a block");
2121 return;
2123 flags |= BMAP_SET;
2126 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2127 &ret_flags, &pblk);
2128 if (errcode) {
2129 com_err(argv[0], errcode,
2130 "while mapping logical block %llu\n",
2131 (unsigned long long) blk);
2132 return;
2134 printf("%llu", (unsigned long long) pblk);
2135 if (ret_flags & BMAP_RET_UNINIT)
2136 fputs(" (uninit)", stdout);
2137 fputc('\n', stdout);
2140 void do_imap(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2141 void *infop EXT2FS_ATTR((unused)))
2143 ext2_ino_t ino;
2144 unsigned long group, block, block_nr, offset;
2146 if (common_args_process(argc, argv, 2, 2, argv[0],
2147 "<file>", 0))
2148 return;
2149 ino = string_to_inode(argv[1]);
2150 if (!ino)
2151 return;
2153 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2154 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2155 EXT2_INODE_SIZE(current_fs->super);
2156 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2157 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2158 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2159 group);
2160 return;
2162 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2163 block;
2164 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2166 printf("Inode %u is part of block group %lu\n"
2167 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2168 block_nr, offset);
2172 void do_idump(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2173 void *infop EXT2FS_ATTR((unused)))
2175 struct ext2_inode_large *inode;
2176 ext2_ino_t ino;
2177 unsigned char *buf;
2178 errcode_t err;
2179 unsigned int isize, size, offset = 0;
2180 int c, mode = 0;
2182 reset_getopt();
2183 while ((c = getopt (argc, argv, "bex")) != EOF) {
2184 if (mode || c == '?') {
2185 com_err(argv[0], 0,
2186 "Usage: inode_dump [-b]|[-e] <file>");
2187 return;
2189 mode = c;
2191 if (optind != argc-1)
2192 return;
2194 if (check_fs_open(argv[0]))
2195 return;
2197 ino = string_to_inode(argv[optind]);
2198 if (!ino)
2199 return;
2201 isize = EXT2_INODE_SIZE(current_fs->super);
2202 err = ext2fs_get_mem(isize, &buf);
2203 if (err) {
2204 com_err(argv[0], err, "while allocating memory");
2205 return;
2208 err = ext2fs_read_inode_full(current_fs, ino,
2209 (struct ext2_inode *)buf, isize);
2210 if (err) {
2211 com_err(argv[0], err, "while reading inode %u", ino);
2212 goto err;
2215 inode = (struct ext2_inode_large *) buf;
2216 size = isize;
2217 switch (mode) {
2218 case 'b':
2219 offset = ((char *) (&inode->i_block)) - ((char *) buf);
2220 size = sizeof(inode->i_block);
2221 break;
2222 case 'x':
2223 case 'e':
2224 if (size <= EXT2_GOOD_OLD_INODE_SIZE) {
2225 com_err(argv[0], 0, "No extra space in inode");
2226 goto err;
2228 offset = EXT2_GOOD_OLD_INODE_SIZE + inode->i_extra_isize;
2229 if (offset > size)
2230 goto err;
2231 size -= offset;
2232 break;
2234 if (mode == 'x')
2235 raw_inode_xattr_dump(stdout, buf + offset, size);
2236 else
2237 do_byte_hexdump(stdout, buf + offset, size);
2238 err:
2239 ext2fs_free_mem(&buf);
2242 #ifndef READ_ONLY
2243 void do_set_current_time(int argc, ss_argv_t argv,
2244 int sci_idx EXT2FS_ATTR((unused)),
2245 void *infop EXT2FS_ATTR((unused)))
2247 __s64 now;
2249 if (common_args_process(argc, argv, 2, 2, argv[0],
2250 "<time>", 0))
2251 return;
2253 now = string_to_time(argv[1]);
2254 if (now == -1) {
2255 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2256 argv[1]);
2257 return;
2259 } else {
2260 printf("Setting current time to %s\n", time_to_string(now));
2261 current_fs->now = now;
2264 #endif /* READ_ONLY */
2266 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2268 int compat, bit, ret;
2269 unsigned int feature_mask;
2271 if (name) {
2272 if (feature_type == E2P_FS_FEATURE)
2273 ret = e2p_string2feature(name, &compat, &feature_mask);
2274 else
2275 ret = e2p_jrnl_string2feature(name, &compat,
2276 &feature_mask);
2277 if (ret)
2278 return ret;
2280 if (!(supp[compat] & feature_mask))
2281 return 1;
2282 } else {
2283 for (compat = 0; compat < 3; compat++) {
2284 for (bit = 0, feature_mask = 1; bit < 32;
2285 bit++, feature_mask <<= 1) {
2286 if (supp[compat] & feature_mask) {
2287 if (feature_type == E2P_FS_FEATURE)
2288 fprintf(stdout, " %s",
2289 e2p_feature2string(compat,
2290 feature_mask));
2291 else
2292 fprintf(stdout, " %s",
2293 e2p_jrnl_feature2string(compat,
2294 feature_mask));
2298 fprintf(stdout, "\n");
2301 return 0;
2304 void do_supported_features(int argc, ss_argv_t argv,
2305 int sci_idx EXT2FS_ATTR((unused)),
2306 void *infop EXT2FS_ATTR((unused)))
2308 int ret;
2309 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2310 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2311 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2312 __u32 jrnl_supp[3] = { JBD2_KNOWN_COMPAT_FEATURES,
2313 JBD2_KNOWN_INCOMPAT_FEATURES,
2314 JBD2_KNOWN_ROCOMPAT_FEATURES };
2316 if (argc > 1) {
2317 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2318 if (ret) {
2319 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2320 argv[1]);
2322 if (ret)
2323 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2324 else
2325 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2326 } else {
2327 fprintf(stdout, "Supported features:");
2328 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2329 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2333 #ifndef READ_ONLY
2334 void do_punch(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2335 void *infop EXT2FS_ATTR((unused)))
2337 ext2_ino_t ino;
2338 blk64_t start, end;
2339 int err;
2340 errcode_t errcode;
2342 if (common_args_process(argc, argv, 3, 4, argv[0],
2343 "<file> start_blk [end_blk]",
2344 CHECK_FS_RW | CHECK_FS_BITMAPS))
2345 return;
2347 ino = string_to_inode(argv[1]);
2348 if (!ino)
2349 return;
2350 err = strtoblk(argv[0], argv[2], "logical block", &start);
2351 if (err)
2352 return;
2353 if (argc == 4) {
2354 err = strtoblk(argv[0], argv[3], "logical block", &end);
2355 if (err)
2356 return;
2357 } else
2358 end = ~0;
2360 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2362 if (errcode) {
2363 com_err(argv[0], errcode,
2364 "while truncating inode %u from %llu to %llu\n", ino,
2365 (unsigned long long) start, (unsigned long long) end);
2366 return;
2370 void do_fallocate(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2371 void *infop EXT2FS_ATTR((unused)))
2373 ext2_ino_t ino;
2374 blk64_t start, end;
2375 int err;
2376 errcode_t errcode;
2378 if (common_args_process(argc, argv, 3, 4, argv[0],
2379 "<file> start_blk [end_blk]",
2380 CHECK_FS_RW | CHECK_FS_BITMAPS))
2381 return;
2383 ino = string_to_inode(argv[1]);
2384 if (!ino)
2385 return;
2386 err = strtoblk(argv[0], argv[2], "logical block", &start);
2387 if (err)
2388 return;
2389 if (argc == 4) {
2390 err = strtoblk(argv[0], argv[3], "logical block", &end);
2391 if (err)
2392 return;
2393 } else
2394 end = ~0;
2396 errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2397 ino, NULL, ~0ULL, start, end - start + 1);
2399 if (errcode) {
2400 com_err(argv[0], errcode,
2401 "while fallocating inode %u from %llu to %llu\n", ino,
2402 (unsigned long long) start, (unsigned long long) end);
2403 return;
2407 void do_symlink(int argc, ss_argv_t argv, int sci_idx EXT2FS_ATTR((unused)),
2408 void *infop EXT2FS_ATTR((unused)))
2410 errcode_t retval;
2412 if (common_args_process(argc, argv, 3, 3, "symlink",
2413 "<filename> <target>", CHECK_FS_RW))
2414 return;
2416 retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2417 if (retval)
2418 com_err(argv[0], retval, 0);
2421 #endif /* READ_ONLY */
2423 #if CONFIG_MMP
2424 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), ss_argv_t argv,
2425 int sci_idx EXT2FS_ATTR((unused)),
2426 void *infop EXT2FS_ATTR((unused)))
2428 struct mmp_struct *mmp_s;
2429 unsigned long long mmp_block;
2430 time_t t;
2431 errcode_t retval = 0;
2433 if (check_fs_open(argv[0]))
2434 return;
2436 if (argc > 1) {
2437 char *end = NULL;
2438 mmp_block = strtoull(argv[1], &end, 0);
2439 if (end == argv[0] || mmp_block == 0) {
2440 fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2441 argv[0], argv[1]);
2442 return;
2444 } else {
2445 mmp_block = current_fs->super->s_mmp_block;
2448 if (mmp_block == 0) {
2449 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2450 argv[0]);
2451 return;
2454 if (current_fs->mmp_buf == NULL) {
2455 retval = ext2fs_get_mem(current_fs->blocksize,
2456 &current_fs->mmp_buf);
2457 if (retval) {
2458 com_err(argv[0], retval, "allocating MMP buffer.\n");
2459 return;
2463 mmp_s = current_fs->mmp_buf;
2465 retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2466 if (retval) {
2467 com_err(argv[0], retval, "reading MMP block %llu.\n",
2468 (unsigned long long) mmp_block);
2469 return;
2472 t = mmp_s->mmp_time;
2473 fprintf(stdout, "block_number: %llu\n",
2474 (unsigned long long) current_fs->super->s_mmp_block);
2475 fprintf(stdout, "update_interval: %d\n",
2476 current_fs->super->s_mmp_update_interval);
2477 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2478 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2479 fprintf(stdout, "time: %llu -- %s",
2480 (unsigned long long) mmp_s->mmp_time, ctime(&t));
2481 fprintf(stdout, "node_name: %.*s\n",
2482 EXT2_LEN_STR(mmp_s->mmp_nodename));
2483 fprintf(stdout, "device_name: %.*s\n",
2484 EXT2_LEN_STR(mmp_s->mmp_bdevname));
2485 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2486 fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2488 #else
2489 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2490 ss_argv_t argv EXT2FS_ATTR((unused)),
2491 int sci_idx EXT2FS_ATTR((unused)),
2492 void *infop EXT2FS_ATTR((unused)))
2494 fprintf(stdout, "MMP is unsupported, please recompile with "
2495 "--enable-mmp\n");
2497 #endif
2499 static int source_file(const char *cmd_file, int ss_idx)
2501 FILE *f;
2502 char buf[BUFSIZ];
2503 char *cp;
2504 int exit_status = 0;
2505 int retval;
2507 if (strcmp(cmd_file, "-") == 0)
2508 f = stdin;
2509 else {
2510 f = fopen(cmd_file, "r");
2511 if (!f) {
2512 perror(cmd_file);
2513 exit(1);
2516 fflush(stdout);
2517 fflush(stderr);
2518 setbuf(stdout, NULL);
2519 setbuf(stderr, NULL);
2520 while (!feof(f)) {
2521 if (fgets(buf, sizeof(buf), f) == NULL)
2522 break;
2523 if (buf[0] == '#') {
2524 printf("%s", buf);
2525 continue;
2527 cp = strchr(buf, '\n');
2528 if (cp)
2529 *cp = 0;
2530 cp = strchr(buf, '\r');
2531 if (cp)
2532 *cp = 0;
2533 printf("debugfs: %s\n", buf);
2534 retval = ss_execute_line(ss_idx, buf);
2535 if (retval) {
2536 ss_perror(ss_idx, retval, buf);
2537 exit_status++;
2540 if (f != stdin)
2541 fclose(f);
2542 return exit_status;
2545 int main(int argc, char **argv)
2547 int retval;
2548 const char *usage =
2549 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2550 "[-R request] [-d data_source_device] [-i] [-n] [-D] [-V] ["
2551 #ifndef READ_ONLY
2552 "[-w] [-z undo_file] "
2553 #endif
2554 "[-c]] [device]";
2555 int c;
2556 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES |
2557 EXT2_FLAG_64BITS | EXT2_FLAG_THREADS;
2558 char *request = 0;
2559 int exit_status = 0;
2560 char *cmd_file = 0;
2561 blk64_t superblock = 0;
2562 blk64_t blocksize = 0;
2563 int catastrophic = 0;
2564 char *data_filename = 0;
2565 #ifdef READ_ONLY
2566 const char *opt_string = "nicR:f:b:s:Vd:D";
2567 #else
2568 const char *opt_string = "niwcR:f:b:s:Vd:Dz:";
2569 #endif
2570 char *undo_file = NULL;
2571 #ifdef CONFIG_JBD_DEBUG
2572 char *jbd_debug;
2573 #endif
2575 setlocale(LC_CTYPE, "");
2577 if (debug_prog_name == 0)
2578 #ifdef READ_ONLY
2579 debug_prog_name = "rdebugfs";
2580 #else
2581 debug_prog_name = "debugfs";
2582 #endif
2583 add_error_table(&et_ext2_error_table);
2584 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2585 E2FSPROGS_VERSION, E2FSPROGS_DATE);
2587 #ifdef CONFIG_JBD_DEBUG
2588 jbd_debug = ss_safe_getenv("DEBUGFS_JBD_DEBUG");
2589 if (jbd_debug) {
2590 int res = sscanf(jbd_debug, "%d", &journal_enable_debug);
2592 if (res != 1) {
2593 fprintf(stderr,
2594 "DEBUGFS_JBD_DEBUG \"%s\" not an integer\n\n",
2595 jbd_debug);
2596 exit(1);
2599 #endif
2600 while ((c = getopt (argc, argv, opt_string)) != EOF) {
2601 switch (c) {
2602 case 'R':
2603 request = optarg;
2604 break;
2605 case 'f':
2606 cmd_file = optarg;
2607 break;
2608 case 'd':
2609 data_filename = optarg;
2610 break;
2611 case 'i':
2612 open_flags |= EXT2_FLAG_IMAGE_FILE;
2613 break;
2614 case 'n':
2615 open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2616 break;
2617 #ifndef READ_ONLY
2618 case 'w':
2619 open_flags |= EXT2_FLAG_RW;
2620 break;
2621 #endif
2622 case 'D':
2623 open_flags |= EXT2_FLAG_DIRECT_IO;
2624 break;
2625 case 'b':
2626 blocksize = parse_ulong(optarg, argv[0],
2627 "block size", 0);
2628 break;
2629 case 's':
2630 retval = strtoblk(argv[0], optarg,
2631 "superblock block number",
2632 &superblock);
2633 if (retval)
2634 return 1;
2635 break;
2636 case 'c':
2637 catastrophic = 1;
2638 break;
2639 case 'V':
2640 /* Print version number and exit */
2641 fprintf(stderr, "\tUsing %s\n",
2642 error_message(EXT2_ET_BASE));
2643 exit(0);
2644 #ifndef READ_ONLY
2645 case 'z':
2646 undo_file = optarg;
2647 break;
2648 #endif
2649 default:
2650 com_err(argv[0], 0, usage, debug_prog_name);
2651 return 1;
2654 if (optind < argc)
2655 open_filesystem(argv[optind], open_flags,
2656 superblock, blocksize, catastrophic,
2657 data_filename, undo_file);
2659 ss_sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2660 &debug_cmds, &retval);
2661 if (retval) {
2662 ss_perror(ss_sci_idx, retval, "creating invocation");
2663 exit(1);
2665 ss_get_readline(ss_sci_idx);
2667 (void) ss_add_request_table(ss_sci_idx, &ss_std_requests, 1, &retval);
2668 if (retval) {
2669 ss_perror(ss_sci_idx, retval, "adding standard requests");
2670 exit (1);
2672 if (extra_cmds)
2673 ss_add_request_table(ss_sci_idx, extra_cmds, 1, &retval);
2674 if (retval) {
2675 ss_perror(ss_sci_idx, retval, "adding extra requests");
2676 exit (1);
2678 if (request) {
2679 retval = 0;
2680 retval = ss_execute_line(ss_sci_idx, request);
2681 if (retval) {
2682 ss_perror(ss_sci_idx, retval, request);
2683 exit_status++;
2685 } else if (cmd_file) {
2686 exit_status = source_file(cmd_file, ss_sci_idx);
2687 } else {
2688 ss_listen(ss_sci_idx);
2691 ss_delete_invocation(ss_sci_idx);
2693 if (current_fs)
2694 close_filesystem();
2696 remove_error_table(&et_ext2_error_table);
2697 return exit_status;