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