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