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