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