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