Whamcloud - gitweb
misc: clean up compiler warnings
[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, blk64_t superblock,
49                             blk64_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         blk64_t superblock = 0;
129         blk64_t blocksize = 0;
130         int     open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS; 
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         ext2fs_blocks_count_set(&param, 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(ext2_filsys fs, dgrp_t group, int mask,
297                           const char *str, int *first, FILE *f)
298 {
299         if (ext2fs_bg_flags_test(fs, group, 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         int     c, header_only = 0;
314         int     numdirs = 0, first, gdt_csum;
315
316         reset_getopt();
317         while ((c = getopt (argc, argv, "h")) != EOF) {
318                 switch (c) {
319                 case 'h':
320                         header_only++;
321                         break;
322                 default:
323                         goto print_usage;
324                 }
325         }
326         if (optind != argc) {
327                 goto print_usage;
328         }
329         if (check_fs_open(argv[0]))
330                 return;
331         out = open_pager();
332
333         list_super2(current_fs->super, out);
334         for (i=0; i < current_fs->group_desc_count; i++)
335                 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
336         fprintf(out, "Directories:              %d\n", numdirs);
337
338         if (header_only) {
339                 close_pager(out);
340                 return;
341         }
342
343         gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
344                                               EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
345         for (i = 0; i < current_fs->group_desc_count; i++) {
346                 fprintf(out, " Group %2d: block bitmap at %llu, "
347                         "inode bitmap at %llu, "
348                         "inode table at %llu\n"
349                         "           %u free %s, "
350                         "%u free %s, "
351                         "%u used %s%s",
352                         i, ext2fs_block_bitmap_loc(current_fs, i),
353                         ext2fs_inode_bitmap_loc(current_fs, i),
354                         ext2fs_inode_table_loc(current_fs, i),
355                         ext2fs_bg_free_blocks_count(current_fs, i),
356                         ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
357                         "blocks" : "block",
358                         ext2fs_bg_free_inodes_count(current_fs, i),
359                         ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
360                         "inodes" : "inode",
361                         ext2fs_bg_used_dirs_count(current_fs, i),
362                         ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
363                                 : "directory", gdt_csum ? ", " : "\n");
364                 if (gdt_csum)
365                         fprintf(out, "%u unused %s\n",
366                                 ext2fs_bg_itable_unused(current_fs, i),
367                                 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
368                                 "inodes" : "inode");
369                 first = 1;
370                 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
371                               &first, out);
372                 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
373                               &first, out);
374                 if (gdt_csum) {
375                         fprintf(out, "%sChecksum 0x%04x",
376                                 first ? "           [":", ", ext2fs_bg_checksum(current_fs, i));
377                         first = 0;
378                 }
379                 if (!first)
380                         fputs("]\n", out);
381         }
382         close_pager(out);
383         return;
384 print_usage:
385         fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
386 }
387
388 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
389                       char **argv EXT2FS_ATTR((unused)))
390 {
391         if (check_fs_open(argv[0]))
392                 return;
393         if (check_fs_read_write(argv[0]))
394                 return;
395
396         if (argv[1] && !strcmp(argv[1], "-clean"))
397                 current_fs->super->s_state |= EXT2_VALID_FS;
398         else
399                 current_fs->super->s_state &= ~EXT2_VALID_FS;
400         ext2fs_mark_super_dirty(current_fs);
401 }
402
403 struct list_blocks_struct {
404         FILE            *f;
405         e2_blkcnt_t     total;
406         blk64_t         first_block, last_block;
407         e2_blkcnt_t     first_bcnt, last_bcnt;
408         e2_blkcnt_t     first;
409 };
410
411 static void finish_range(struct list_blocks_struct *lb)
412 {
413         if (lb->first_block == 0)
414                 return;
415         if (lb->first)
416                 lb->first = 0;
417         else
418                 fprintf(lb->f, ", ");
419         if (lb->first_block == lb->last_block)
420                 fprintf(lb->f, "(%lld):%llu",
421                         (long long)lb->first_bcnt, lb->first_block);
422         else
423                 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
424                         (long long)lb->first_bcnt, (long long)lb->last_bcnt,
425                         lb->first_block, lb->last_block);
426         lb->first_block = 0;
427 }
428
429 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
430                             blk64_t *blocknr, e2_blkcnt_t blockcnt,
431                             blk64_t ref_block EXT2FS_ATTR((unused)),
432                             int ref_offset EXT2FS_ATTR((unused)),
433                             void *private)
434 {
435         struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
436
437         lb->total++;
438         if (blockcnt >= 0) {
439                 /*
440                  * See if we can add on to the existing range (if it exists)
441                  */
442                 if (lb->first_block &&
443                     (lb->last_block+1 == *blocknr) &&
444                     (lb->last_bcnt+1 == blockcnt)) {
445                         lb->last_block = *blocknr;
446                         lb->last_bcnt = blockcnt;
447                         return 0;
448                 }
449                 /*
450                  * Start a new range.
451                  */
452                 finish_range(lb);
453                 lb->first_block = lb->last_block = *blocknr;
454                 lb->first_bcnt = lb->last_bcnt = blockcnt;
455                 return 0;
456         }
457         /*
458          * Not a normal block.  Always force a new range.
459          */
460         finish_range(lb);
461         if (lb->first)
462                 lb->first = 0;
463         else
464                 fprintf(lb->f, ", ");
465         if (blockcnt == -1)
466                 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
467         else if (blockcnt == -2)
468                 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
469         else if (blockcnt == -3)
470                 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
471         return 0;
472 }
473
474 static void dump_xattr_string(FILE *out, const char *str, int len)
475 {
476         int printable = 0;
477         int i;
478
479         /* check: is string "printable enough?" */
480         for (i = 0; i < len; i++)
481                 if (isprint(str[i]))
482                         printable++;
483
484         if (printable <= len*7/8)
485                 printable = 0;
486
487         for (i = 0; i < len; i++)
488                 if (printable)
489                         fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
490                                 (unsigned char)str[i]);
491                 else
492                         fprintf(out, "%02x ", (unsigned char)str[i]);
493 }
494
495 static void internal_dump_inode_extra(FILE *out,
496                                       const char *prefix EXT2FS_ATTR((unused)),
497                                       ext2_ino_t inode_num EXT2FS_ATTR((unused)),
498                                       struct ext2_inode_large *inode)
499 {
500         struct ext2_ext_attr_entry *entry;
501         __u32 *magic;
502         char *start, *end;
503         unsigned int storage_size;
504
505         fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
506         if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
507                         EXT2_GOOD_OLD_INODE_SIZE) {
508                 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
509                                 inode->i_extra_isize);
510                 return;
511         }
512         storage_size = EXT2_INODE_SIZE(current_fs->super) -
513                         EXT2_GOOD_OLD_INODE_SIZE -
514                         inode->i_extra_isize;
515         magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
516                         inode->i_extra_isize);
517         if (*magic == EXT2_EXT_ATTR_MAGIC) {
518                 fprintf(out, "Extended attributes stored in inode body: \n");
519                 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
520                 start = (char *) magic + sizeof(__u32);
521                 entry = (struct ext2_ext_attr_entry *) start;
522                 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
523                         struct ext2_ext_attr_entry *next =
524                                 EXT2_EXT_ATTR_NEXT(entry);
525                         if (entry->e_value_size > storage_size ||
526                                         (char *) next >= end) {
527                                 fprintf(out, "invalid EA entry in inode\n");
528                                 return;
529                         }
530                         fprintf(out, "  ");
531                         dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
532                                           entry->e_name_len);
533                         fprintf(out, " = \"");
534                         dump_xattr_string(out, start + entry->e_value_offs,
535                                                 entry->e_value_size);
536                         fprintf(out, "\" (%u)\n", entry->e_value_size);
537                         entry = next;
538                 }
539         }
540 }
541
542 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
543 {
544         struct list_blocks_struct lb;
545
546         fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
547         lb.total = 0;
548         lb.first_block = 0;
549         lb.f = f;
550         lb.first = 1;
551         ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
552                               list_blocks_proc, (void *)&lb);
553         finish_range(&lb);
554         if (lb.total)
555                 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
556         fprintf(f,"\n");
557 }
558
559 static int int_log10(unsigned long long arg)
560 {
561         int     l = 0;
562
563         arg = arg / 10;
564         while (arg) {
565                 l++;
566                 arg = arg / 10;
567         }
568         return l;
569 }
570
571 #define DUMP_LEAF_EXTENTS       0x01
572 #define DUMP_NODE_EXTENTS       0x02
573 #define DUMP_EXTENT_TABLE       0x04
574
575 static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
576                          int flags, int logical_width, int physical_width)
577 {
578         ext2_extent_handle_t    handle;
579         struct ext2fs_extent    extent;
580         struct ext2_extent_info info;
581         int                     op = EXT2_EXTENT_ROOT;
582         unsigned int            printed = 0;
583         errcode_t               errcode;
584
585         errcode = ext2fs_extent_open(current_fs, ino, &handle);
586         if (errcode)
587                 return;
588
589         if (flags & DUMP_EXTENT_TABLE)
590                 fprintf(f, "Level Entries %*s %*s Length Flags\n",
591                         (logical_width*2)+3, "Logical",
592                         (physical_width*2)+3, "Physical");
593         else
594                 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
595
596         while (1) {
597                 errcode = ext2fs_extent_get(handle, op, &extent);
598
599                 if (errcode)
600                         break;
601
602                 op = EXT2_EXTENT_NEXT;
603
604                 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
605                         continue;
606
607                 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
608                         if ((flags & DUMP_LEAF_EXTENTS) == 0)
609                                 continue;
610                 } else {
611                         if ((flags & DUMP_NODE_EXTENTS) == 0)
612                                 continue;
613                 }
614
615                 errcode = ext2fs_extent_get_info(handle, &info);
616                 if (errcode)
617                         continue;
618
619                 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
620                         if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
621                                 continue;
622
623                         if (flags & DUMP_EXTENT_TABLE) {
624                                 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
625                                         "%*llu%*s %6u\n",
626                                         info.curr_level, info.max_depth,
627                                         info.curr_entry, info.num_entries,
628                                         logical_width,
629                                         extent.e_lblk,
630                                         logical_width,
631                                         extent.e_lblk + (extent.e_len - 1),
632                                         physical_width,
633                                         extent.e_pblk,
634                                         physical_width+3, "", extent.e_len);
635                                 continue;
636                         }
637
638                         fprintf(f, "%s(ETB%d):%lld",
639                                 printed ? ", " : "", info.curr_level,
640                                 extent.e_pblk);
641                         printed = 1;
642                         continue;
643                 }
644
645                 if (flags & DUMP_EXTENT_TABLE) {
646                         fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
647                                 "%*llu - %*llu %6u %s\n",
648                                 info.curr_level, info.max_depth,
649                                 info.curr_entry, info.num_entries,
650                                 logical_width,
651                                 extent.e_lblk,
652                                 logical_width,
653                                 extent.e_lblk + (extent.e_len - 1),
654                                 physical_width,
655                                 extent.e_pblk,
656                                 physical_width,
657                                 extent.e_pblk + (extent.e_len - 1),
658                                 extent.e_len,
659                                 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
660                                         "Uninit" : "");
661                         continue;
662                 }
663
664                 if (extent.e_len == 0)
665                         continue;
666                 else if (extent.e_len == 1)
667                         fprintf(f,
668                                 "%s(%lld%s):%lld",
669                                 printed ? ", " : "",
670                                 extent.e_lblk,
671                                 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
672                                 "[u]" : "",
673                                 extent.e_pblk);
674                 else
675                         fprintf(f,
676                                 "%s(%lld-%lld%s):%lld-%lld",
677                                 printed ? ", " : "",
678                                 extent.e_lblk,
679                                 extent.e_lblk + (extent.e_len - 1),
680                                 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
681                                         "[u]" : "",
682                                 extent.e_pblk,
683                                 extent.e_pblk + (extent.e_len - 1));
684                 printed = 1;
685         }
686         if (printed)
687                 fprintf(f, "\n");
688 }
689
690 void internal_dump_inode(FILE *out, const char *prefix,
691                          ext2_ino_t inode_num, struct ext2_inode *inode,
692                          int do_dump_blocks)
693 {
694         const char *i_type;
695         char frag, fsize;
696         int os = current_fs->super->s_creator_os;
697         struct ext2_inode_large *large_inode;
698         int is_large_inode = 0;
699
700         if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
701                 is_large_inode = 1;
702         large_inode = (struct ext2_inode_large *) inode;
703
704         if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
705         else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
706         else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
707         else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
708         else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
709         else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
710         else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
711         else i_type = "bad type";
712         fprintf(out, "%sInode: %u   Type: %s    ", prefix, inode_num, i_type);
713         fprintf(out, "%sMode:  %04o   Flags: 0x%x\n",
714                 prefix, inode->i_mode & 0777, inode->i_flags);
715         if (is_large_inode && large_inode->i_extra_isize >= 24) {
716                 fprintf(out, "%sGeneration: %u    Version: 0x%08x:%08x\n",
717                         prefix, inode->i_generation, large_inode->i_version_hi,
718                         inode->osd1.linux1.l_i_version);
719         } else {
720                 fprintf(out, "%sGeneration: %u    Version: 0x%08x\n", prefix,
721                         inode->i_generation, inode->osd1.linux1.l_i_version);
722         }
723         fprintf(out, "%sUser: %5d   Group: %5d   Size: ",
724                 prefix, inode_uid(*inode), inode_gid(*inode));
725         if (LINUX_S_ISREG(inode->i_mode)) {
726                 unsigned long long i_size = (inode->i_size |
727                                     ((unsigned long long)inode->i_size_high << 32));
728
729                 fprintf(out, "%llu\n", i_size);
730         } else
731                 fprintf(out, "%d\n", inode->i_size);
732         if (os == EXT2_OS_HURD)
733                 fprintf(out,
734                         "%sFile ACL: %d    Directory ACL: %d Translator: %d\n",
735                         prefix,
736                         inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
737                         inode->osd1.hurd1.h_i_translator);
738         else
739                 fprintf(out, "%sFile ACL: %llu    Directory ACL: %d\n",
740                         prefix,
741                         inode->i_file_acl | ((long long)
742                                 (inode->osd2.linux2.l_i_file_acl_high) << 32),
743                         LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
744         if (os == EXT2_OS_LINUX)
745                 fprintf(out, "%sLinks: %d   Blockcount: %llu\n",
746                         prefix, inode->i_links_count,
747                         (((unsigned long long)
748                           inode->osd2.linux2.l_i_blocks_hi << 32)) +
749                         inode->i_blocks);
750         else
751                 fprintf(out, "%sLinks: %d   Blockcount: %u\n",
752                         prefix, inode->i_links_count, inode->i_blocks);
753         switch (os) {
754             case EXT2_OS_HURD:
755                 frag = inode->osd2.hurd2.h_i_frag;
756                 fsize = inode->osd2.hurd2.h_i_fsize;
757                 break;
758             default:
759                 frag = fsize = 0;
760         }
761         fprintf(out, "%sFragment:  Address: %d    Number: %d    Size: %d\n",
762                 prefix, inode->i_faddr, frag, fsize);
763         if (is_large_inode && large_inode->i_extra_isize >= 24) {
764                 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
765                         inode->i_ctime, large_inode->i_ctime_extra,
766                         time_to_string(inode->i_ctime));
767                 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
768                         inode->i_atime, large_inode->i_atime_extra,
769                         time_to_string(inode->i_atime));
770                 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
771                         inode->i_mtime, large_inode->i_mtime_extra,
772                         time_to_string(inode->i_mtime));
773                 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
774                         large_inode->i_crtime, large_inode->i_crtime_extra,
775                         time_to_string(large_inode->i_crtime));
776         } else {
777                 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
778                         time_to_string(inode->i_ctime));
779                 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
780                         time_to_string(inode->i_atime));
781                 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
782                         time_to_string(inode->i_mtime));
783         }
784         if (inode->i_dtime)
785           fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
786                   time_to_string(inode->i_dtime));
787         if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
788                 internal_dump_inode_extra(out, prefix, inode_num,
789                                           (struct ext2_inode_large *) inode);
790         if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
791                 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
792                         (int) inode->i_size, (char *)inode->i_block);
793         else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
794                 int major, minor;
795                 const char *devnote;
796
797                 if (inode->i_block[0]) {
798                         major = (inode->i_block[0] >> 8) & 255;
799                         minor = inode->i_block[0] & 255;
800                         devnote = "";
801                 } else {
802                         major = (inode->i_block[1] & 0xfff00) >> 8;
803                         minor = ((inode->i_block[1] & 0xff) |
804                                  ((inode->i_block[1] >> 12) & 0xfff00));
805                         devnote = "(New-style) ";
806                 }
807                 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
808                         devnote, major, minor, major, minor);
809         } else if (do_dump_blocks) {
810                 if (inode->i_flags & EXT4_EXTENTS_FL)
811                         dump_extents(out, prefix, inode_num,
812                                      DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
813                 else
814                         dump_blocks(out, prefix, inode_num);
815         }
816 }
817
818 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
819 {
820         FILE    *out;
821
822         out = open_pager();
823         internal_dump_inode(out, "", inode_num, inode, 1);
824         close_pager(out);
825 }
826
827 void do_stat(int argc, char *argv[])
828 {
829         ext2_ino_t      inode;
830         struct ext2_inode * inode_buf;
831
832         if (check_fs_open(argv[0]))
833                 return;
834
835         inode_buf = (struct ext2_inode *)
836                         malloc(EXT2_INODE_SIZE(current_fs->super));
837         if (!inode_buf) {
838                 fprintf(stderr, "do_stat: can't allocate buffer\n");
839                 return;
840         }
841
842         if (common_inode_args_process(argc, argv, &inode, 0)) {
843                 free(inode_buf);
844                 return;
845         }
846
847         if (debugfs_read_inode_full(inode, inode_buf, argv[0],
848                                         EXT2_INODE_SIZE(current_fs->super))) {
849                 free(inode_buf);
850                 return;
851         }
852
853         dump_inode(inode, inode_buf);
854         free(inode_buf);
855         return;
856 }
857
858 void do_dump_extents(int argc, char **argv)
859 {
860         struct ext2_inode inode;
861         ext2_ino_t      ino;
862         FILE            *out;
863         int             c, flags = 0;
864         int             logical_width;
865         int             physical_width;
866
867         reset_getopt();
868         while ((c = getopt(argc, argv, "nl")) != EOF) {
869                 switch (c) {
870                 case 'n':
871                         flags |= DUMP_NODE_EXTENTS;
872                         break;
873                 case 'l':
874                         flags |= DUMP_LEAF_EXTENTS;
875                         break;
876                 }
877         }
878
879         if (argc != optind + 1) {
880                 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
881                 return;
882         }
883
884         if (flags == 0)
885                 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
886         flags |= DUMP_EXTENT_TABLE;
887
888         if (check_fs_open(argv[0]))
889                 return;
890
891         ino = string_to_inode(argv[optind]);
892         if (ino == 0)
893                 return;
894
895         if (debugfs_read_inode(ino, &inode, argv[0]))
896                 return;
897
898         if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
899                 fprintf(stderr, "%s: does not uses extent block maps\n",
900                         argv[optind]);
901                 return;
902         }
903
904         logical_width = int_log10(((inode.i_size |
905                                     (__u64) inode.i_size_high << 32) +
906                                    current_fs->blocksize - 1) /
907                                   current_fs->blocksize) + 1;
908         if (logical_width < 5)
909                 logical_width = 5;
910         physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
911         if (physical_width < 5)
912                 physical_width = 5;
913
914         out = open_pager();
915         dump_extents(out, "", ino, flags, logical_width, physical_width);
916         close_pager(out);
917         return;
918 }
919
920 void do_chroot(int argc, char *argv[])
921 {
922         ext2_ino_t inode;
923         int retval;
924
925         if (common_inode_args_process(argc, argv, &inode, 0))
926                 return;
927
928         retval = ext2fs_check_directory(current_fs, inode);
929         if (retval)  {
930                 com_err(argv[1], retval, 0);
931                 return;
932         }
933         root = inode;
934 }
935
936 void do_clri(int argc, char *argv[])
937 {
938         ext2_ino_t inode;
939         struct ext2_inode inode_buf;
940
941         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
942                 return;
943
944         if (debugfs_read_inode(inode, &inode_buf, argv[0]))
945                 return;
946         memset(&inode_buf, 0, sizeof(inode_buf));
947         if (debugfs_write_inode(inode, &inode_buf, argv[0]))
948                 return;
949 }
950
951 void do_freei(int argc, char *argv[])
952 {
953         ext2_ino_t inode;
954
955         if (common_inode_args_process(argc, argv, &inode,
956                                       CHECK_FS_RW | CHECK_FS_BITMAPS))
957                 return;
958
959         if (!ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
960                 com_err(argv[0], 0, "Warning: inode already clear");
961         ext2fs_unmark_inode_bitmap2(current_fs->inode_map,inode);
962         ext2fs_mark_ib_dirty(current_fs);
963 }
964
965 void do_seti(int argc, char *argv[])
966 {
967         ext2_ino_t inode;
968
969         if (common_inode_args_process(argc, argv, &inode,
970                                       CHECK_FS_RW | CHECK_FS_BITMAPS))
971                 return;
972
973         if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
974                 com_err(argv[0], 0, "Warning: inode already set");
975         ext2fs_mark_inode_bitmap2(current_fs->inode_map,inode);
976         ext2fs_mark_ib_dirty(current_fs);
977 }
978
979 void do_testi(int argc, char *argv[])
980 {
981         ext2_ino_t inode;
982
983         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
984                 return;
985
986         if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
987                 printf("Inode %u is marked in use\n", inode);
988         else
989                 printf("Inode %u is not in use\n", inode);
990 }
991
992 void do_freeb(int argc, char *argv[])
993 {
994         blk64_t block;
995         blk64_t count = 1;
996
997         if (common_block_args_process(argc, argv, &block, &count))
998                 return;
999         if (check_fs_read_write(argv[0]))
1000                 return;
1001         while (count-- > 0) {
1002                 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1003                         com_err(argv[0], 0, "Warning: block %llu already clear",
1004                                 block);
1005                 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1006                 block++;
1007         }
1008         ext2fs_mark_bb_dirty(current_fs);
1009 }
1010
1011 void do_setb(int argc, char *argv[])
1012 {
1013         blk64_t block;
1014         blk64_t count = 1;
1015
1016         if (common_block_args_process(argc, argv, &block, &count))
1017                 return;
1018         if (check_fs_read_write(argv[0]))
1019                 return;
1020         while (count-- > 0) {
1021                 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1022                         com_err(argv[0], 0, "Warning: block %llu already set",
1023                                 block);
1024                 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1025                 block++;
1026         }
1027         ext2fs_mark_bb_dirty(current_fs);
1028 }
1029
1030 void do_testb(int argc, char *argv[])
1031 {
1032         blk64_t block;
1033         blk64_t count = 1;
1034
1035         if (common_block_args_process(argc, argv, &block, &count))
1036                 return;
1037         while (count-- > 0) {
1038                 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1039                         printf("Block %llu marked in use\n", block);
1040                 else
1041                         printf("Block %llu not in use\n", block);
1042                 block++;
1043         }
1044 }
1045
1046 static void modify_u8(char *com, const char *prompt,
1047                       const char *format, __u8 *val)
1048 {
1049         char buf[200];
1050         unsigned long v;
1051         char *tmp;
1052
1053         sprintf(buf, format, *val);
1054         printf("%30s    [%s] ", prompt, buf);
1055         if (!fgets(buf, sizeof(buf), stdin))
1056                 return;
1057         if (buf[strlen (buf) - 1] == '\n')
1058                 buf[strlen (buf) - 1] = '\0';
1059         if (!buf[0])
1060                 return;
1061         v = strtoul(buf, &tmp, 0);
1062         if (*tmp)
1063                 com_err(com, 0, "Bad value - %s", buf);
1064         else
1065                 *val = v;
1066 }
1067
1068 static void modify_u16(char *com, const char *prompt,
1069                        const char *format, __u16 *val)
1070 {
1071         char buf[200];
1072         unsigned long v;
1073         char *tmp;
1074
1075         sprintf(buf, format, *val);
1076         printf("%30s    [%s] ", prompt, buf);
1077         if (!fgets(buf, sizeof(buf), stdin))
1078                 return;
1079         if (buf[strlen (buf) - 1] == '\n')
1080                 buf[strlen (buf) - 1] = '\0';
1081         if (!buf[0])
1082                 return;
1083         v = strtoul(buf, &tmp, 0);
1084         if (*tmp)
1085                 com_err(com, 0, "Bad value - %s", buf);
1086         else
1087                 *val = v;
1088 }
1089
1090 static void modify_u32(char *com, const char *prompt,
1091                        const char *format, __u32 *val)
1092 {
1093         char buf[200];
1094         unsigned long v;
1095         char *tmp;
1096
1097         sprintf(buf, format, *val);
1098         printf("%30s    [%s] ", prompt, buf);
1099         if (!fgets(buf, sizeof(buf), stdin))
1100                 return;
1101         if (buf[strlen (buf) - 1] == '\n')
1102                 buf[strlen (buf) - 1] = '\0';
1103         if (!buf[0])
1104                 return;
1105         v = strtoul(buf, &tmp, 0);
1106         if (*tmp)
1107                 com_err(com, 0, "Bad value - %s", buf);
1108         else
1109                 *val = v;
1110 }
1111
1112
1113 void do_modify_inode(int argc, char *argv[])
1114 {
1115         struct ext2_inode inode;
1116         ext2_ino_t      inode_num;
1117         int             i;
1118         unsigned char   *frag, *fsize;
1119         char            buf[80];
1120         int             os;
1121         const char      *hex_format = "0x%x";
1122         const char      *octal_format = "0%o";
1123         const char      *decimal_format = "%d";
1124         const char      *unsignedlong_format = "%lu";
1125
1126         if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1127                 return;
1128
1129         os = current_fs->super->s_creator_os;
1130
1131         if (debugfs_read_inode(inode_num, &inode, argv[1]))
1132                 return;
1133
1134         modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1135         modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1136         modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1137         modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1138         modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1139         modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1140         modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1141         modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1142         modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1143         if (os == EXT2_OS_LINUX)
1144                 modify_u16(argv[0], "Block count high", unsignedlong_format,
1145                            &inode.osd2.linux2.l_i_blocks_hi);
1146         modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1147         modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1148         modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1149 #if 0
1150         modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1151 #endif
1152         modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1153         if (LINUX_S_ISDIR(inode.i_mode))
1154                 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1155         else
1156                 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1157
1158         if (os == EXT2_OS_HURD)
1159                 modify_u32(argv[0], "Translator Block",
1160                             decimal_format, &inode.osd1.hurd1.h_i_translator);
1161
1162         modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1163         switch (os) {
1164             case EXT2_OS_HURD:
1165                 frag = &inode.osd2.hurd2.h_i_frag;
1166                 fsize = &inode.osd2.hurd2.h_i_fsize;
1167                 break;
1168             default:
1169                 frag = fsize = 0;
1170         }
1171         if (frag)
1172                 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1173         if (fsize)
1174                 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1175
1176         for (i=0;  i < EXT2_NDIR_BLOCKS; i++) {
1177                 sprintf(buf, "Direct Block #%d", i);
1178                 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1179         }
1180         modify_u32(argv[0], "Indirect Block", decimal_format,
1181                     &inode.i_block[EXT2_IND_BLOCK]);
1182         modify_u32(argv[0], "Double Indirect Block", decimal_format,
1183                     &inode.i_block[EXT2_DIND_BLOCK]);
1184         modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1185                     &inode.i_block[EXT2_TIND_BLOCK]);
1186         if (debugfs_write_inode(inode_num, &inode, argv[1]))
1187                 return;
1188 }
1189
1190 void do_change_working_dir(int argc, char *argv[])
1191 {
1192         ext2_ino_t      inode;
1193         int             retval;
1194
1195         if (common_inode_args_process(argc, argv, &inode, 0))
1196                 return;
1197
1198         retval = ext2fs_check_directory(current_fs, inode);
1199         if (retval) {
1200                 com_err(argv[1], retval, 0);
1201                 return;
1202         }
1203         cwd = inode;
1204         return;
1205 }
1206
1207 void do_print_working_directory(int argc, char *argv[])
1208 {
1209         int     retval;
1210         char    *pathname = NULL;
1211
1212         if (common_args_process(argc, argv, 1, 1,
1213                                 "print_working_directory", "", 0))
1214                 return;
1215
1216         retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1217         if (retval) {
1218                 com_err(argv[0], retval,
1219                         "while trying to get pathname of cwd");
1220         }
1221         printf("[pwd]   INODE: %6u  PATH: %s\n",
1222                cwd, pathname ? pathname : "NULL");
1223         if (pathname) {
1224                 free(pathname);
1225                 pathname = NULL;
1226         }
1227         retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1228         if (retval) {
1229                 com_err(argv[0], retval,
1230                         "while trying to get pathname of root");
1231         }
1232         printf("[root]  INODE: %6u  PATH: %s\n",
1233                root, pathname ? pathname : "NULL");
1234         if (pathname) {
1235                 free(pathname);
1236                 pathname = NULL;
1237         }
1238         return;
1239 }
1240
1241 /*
1242  * Given a mode, return the ext2 file type
1243  */
1244 static int ext2_file_type(unsigned int mode)
1245 {
1246         if (LINUX_S_ISREG(mode))
1247                 return EXT2_FT_REG_FILE;
1248
1249         if (LINUX_S_ISDIR(mode))
1250                 return EXT2_FT_DIR;
1251
1252         if (LINUX_S_ISCHR(mode))
1253                 return EXT2_FT_CHRDEV;
1254
1255         if (LINUX_S_ISBLK(mode))
1256                 return EXT2_FT_BLKDEV;
1257
1258         if (LINUX_S_ISLNK(mode))
1259                 return EXT2_FT_SYMLINK;
1260
1261         if (LINUX_S_ISFIFO(mode))
1262                 return EXT2_FT_FIFO;
1263
1264         if (LINUX_S_ISSOCK(mode))
1265                 return EXT2_FT_SOCK;
1266
1267         return 0;
1268 }
1269
1270 static void make_link(char *sourcename, char *destname)
1271 {
1272         ext2_ino_t      ino;
1273         struct ext2_inode inode;
1274         int             retval;
1275         ext2_ino_t      dir;
1276         char            *dest, *cp, *base_name;
1277
1278         /*
1279          * Get the source inode
1280          */
1281         ino = string_to_inode(sourcename);
1282         if (!ino)
1283                 return;
1284         base_name = strrchr(sourcename, '/');
1285         if (base_name)
1286                 base_name++;
1287         else
1288                 base_name = sourcename;
1289         /*
1290          * Figure out the destination.  First see if it exists and is
1291          * a directory.
1292          */
1293         if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1294                 dest = base_name;
1295         else {
1296                 /*
1297                  * OK, it doesn't exist.  See if it is
1298                  * '<dir>/basename' or 'basename'
1299                  */
1300                 cp = strrchr(destname, '/');
1301                 if (cp) {
1302                         *cp = 0;
1303                         dir = string_to_inode(destname);
1304                         if (!dir)
1305                                 return;
1306                         dest = cp+1;
1307                 } else {
1308                         dir = cwd;
1309                         dest = destname;
1310                 }
1311         }
1312
1313         if (debugfs_read_inode(ino, &inode, sourcename))
1314                 return;
1315
1316         retval = ext2fs_link(current_fs, dir, dest, ino,
1317                              ext2_file_type(inode.i_mode));
1318         if (retval)
1319                 com_err("make_link", retval, 0);
1320         return;
1321 }
1322
1323
1324 void do_link(int argc, char *argv[])
1325 {
1326         if (common_args_process(argc, argv, 3, 3, "link",
1327                                 "<source file> <dest_name>", CHECK_FS_RW))
1328                 return;
1329
1330         make_link(argv[1], argv[2]);
1331 }
1332
1333 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1334                             e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1335                             blk64_t ref_block EXT2FS_ATTR((unused)),
1336                             int ref_offset EXT2FS_ATTR((unused)),
1337                             void *private EXT2FS_ATTR((unused)))
1338 {
1339         blk64_t block;
1340
1341         block = *blocknr;
1342         ext2fs_block_alloc_stats2(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_bitmap2(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_iterate3(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         blk64_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_block2(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("%llu ", 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_bitmap2(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_bitmap2(current_fs->inode_map,newfile))
1696                 com_err(argv[0], 0, "Warning: inode already set");
1697         ext2fs_mark_inode_bitmap2(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, blk64_t *blocknr,
1758                                e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1759                                blk64_t ref_block EXT2FS_ATTR((unused)),
1760                                int ref_offset EXT2FS_ATTR((unused)),
1761                                void *private EXT2FS_ATTR((unused)))
1762 {
1763         blk64_t block;
1764
1765         block = *blocknr;
1766         ext2fs_block_alloc_stats2(fs, block, -1);
1767         return 0;
1768 }
1769
1770 static void kill_file_by_inode(ext2_ino_t inode)
1771 {
1772         struct ext2_inode inode_buf;
1773
1774         if (debugfs_read_inode(inode, &inode_buf, 0))
1775                 return;
1776         inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1777         if (debugfs_write_inode(inode, &inode_buf, 0))
1778                 return;
1779         if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1780                 return;
1781
1782         ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1783                               release_blocks_proc, NULL);
1784         printf("\n");
1785         ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1786                                   LINUX_S_ISDIR(inode_buf.i_mode));
1787 }
1788
1789
1790 void do_kill_file(int argc, char *argv[])
1791 {
1792         ext2_ino_t inode_num;
1793
1794         if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1795                 return;
1796
1797         kill_file_by_inode(inode_num);
1798 }
1799
1800 void do_rm(int argc, char *argv[])
1801 {
1802         int retval;
1803         ext2_ino_t inode_num;
1804         struct ext2_inode inode;
1805
1806         if (common_args_process(argc, argv, 2, 2, "rm",
1807                                 "<filename>", CHECK_FS_RW))
1808                 return;
1809
1810         retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1811         if (retval) {
1812                 com_err(argv[0], retval, "while trying to resolve filename");
1813                 return;
1814         }
1815
1816         if (debugfs_read_inode(inode_num, &inode, argv[0]))
1817                 return;
1818
1819         if (LINUX_S_ISDIR(inode.i_mode)) {
1820                 com_err(argv[0], 0, "file is a directory");
1821                 return;
1822         }
1823
1824         --inode.i_links_count;
1825         if (debugfs_write_inode(inode_num, &inode, argv[0]))
1826                 return;
1827
1828         unlink_file_by_name(argv[1]);
1829         if (inode.i_links_count == 0)
1830                 kill_file_by_inode(inode_num);
1831 }
1832
1833 struct rd_struct {
1834         ext2_ino_t      parent;
1835         int             empty;
1836 };
1837
1838 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1839                       int       entry EXT2FS_ATTR((unused)),
1840                       struct ext2_dir_entry *dirent,
1841                       int       offset EXT2FS_ATTR((unused)),
1842                       int       blocksize EXT2FS_ATTR((unused)),
1843                       char      *buf EXT2FS_ATTR((unused)),
1844                       void      *private)
1845 {
1846         struct rd_struct *rds = (struct rd_struct *) private;
1847
1848         if (dirent->inode == 0)
1849                 return 0;
1850         if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1851                 return 0;
1852         if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1853             (dirent->name[1] == '.')) {
1854                 rds->parent = dirent->inode;
1855                 return 0;
1856         }
1857         rds->empty = 0;
1858         return 0;
1859 }
1860
1861 void do_rmdir(int argc, char *argv[])
1862 {
1863         int retval;
1864         ext2_ino_t inode_num;
1865         struct ext2_inode inode;
1866         struct rd_struct rds;
1867
1868         if (common_args_process(argc, argv, 2, 2, "rmdir",
1869                                 "<filename>", CHECK_FS_RW))
1870                 return;
1871
1872         retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1873         if (retval) {
1874                 com_err(argv[0], retval, "while trying to resolve filename");
1875                 return;
1876         }
1877
1878         if (debugfs_read_inode(inode_num, &inode, argv[0]))
1879                 return;
1880
1881         if (!LINUX_S_ISDIR(inode.i_mode)) {
1882                 com_err(argv[0], 0, "file is not a directory");
1883                 return;
1884         }
1885
1886         rds.parent = 0;
1887         rds.empty = 1;
1888
1889         retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1890                                     0, rmdir_proc, &rds);
1891         if (retval) {
1892                 com_err(argv[0], retval, "while iterating over directory");
1893                 return;
1894         }
1895         if (rds.empty == 0) {
1896                 com_err(argv[0], 0, "directory not empty");
1897                 return;
1898         }
1899
1900         inode.i_links_count = 0;
1901         if (debugfs_write_inode(inode_num, &inode, argv[0]))
1902                 return;
1903
1904         unlink_file_by_name(argv[1]);
1905         kill_file_by_inode(inode_num);
1906
1907         if (rds.parent) {
1908                 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1909                         return;
1910                 if (inode.i_links_count > 1)
1911                         inode.i_links_count--;
1912                 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1913                         return;
1914         }
1915 }
1916
1917 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1918                             char *argv[] EXT2FS_ATTR((unused)))
1919 {
1920         FILE *out = stdout;
1921
1922         if (current_fs)
1923                 fprintf(out, "Open mode: read-%s\n",
1924                         current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1925         fprintf(out, "Filesystem in use: %s\n",
1926                 current_fs ? current_fs->device_name : "--none--");
1927 }
1928
1929 void do_expand_dir(int argc, char *argv[])
1930 {
1931         ext2_ino_t inode;
1932         int retval;
1933
1934         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1935                 return;
1936
1937         retval = ext2fs_expand_dir(current_fs, inode);
1938         if (retval)
1939                 com_err("ext2fs_expand_dir", retval, 0);
1940         return;
1941 }
1942
1943 void do_features(int argc, char *argv[])
1944 {
1945         int     i;
1946
1947         if (check_fs_open(argv[0]))
1948                 return;
1949
1950         if ((argc != 1) && check_fs_read_write(argv[0]))
1951                 return;
1952         for (i=1; i < argc; i++) {
1953                 if (e2p_edit_feature(argv[i],
1954                                      &current_fs->super->s_feature_compat, 0))
1955                         com_err(argv[0], 0, "Unknown feature: %s\n",
1956                                 argv[i]);
1957                 else
1958                         ext2fs_mark_super_dirty(current_fs);
1959         }
1960         print_features(current_fs->super, stdout);
1961 }
1962
1963 void do_bmap(int argc, char *argv[])
1964 {
1965         ext2_ino_t      ino;
1966         blk64_t         blk, pblk;
1967         int             err;
1968         errcode_t       errcode;
1969
1970         if (common_args_process(argc, argv, 3, 3, argv[0],
1971                                 "<file> logical_blk", 0))
1972                 return;
1973
1974         ino = string_to_inode(argv[1]);
1975         if (!ino)
1976                 return;
1977         blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1978
1979         errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
1980         if (errcode) {
1981                 com_err("argv[0]", errcode,
1982                         "while mapping logical block %llu\n", blk);
1983                 return;
1984         }
1985         printf("%llu\n", pblk);
1986 }
1987
1988 void do_imap(int argc, char *argv[])
1989 {
1990         ext2_ino_t      ino;
1991         unsigned long   group, block, block_nr, offset;
1992
1993         if (common_args_process(argc, argv, 2, 2, argv[0],
1994                                 "<file>", 0))
1995                 return;
1996         ino = string_to_inode(argv[1]);
1997         if (!ino)
1998                 return;
1999
2000         group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2001         offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2002                 EXT2_INODE_SIZE(current_fs->super);
2003         block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2004         if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2005                 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2006                         group);
2007                 return;
2008         }
2009         block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2010                 block;
2011         offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2012
2013         printf("Inode %d is part of block group %lu\n"
2014                "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2015                block_nr, offset);
2016
2017 }
2018
2019 void do_set_current_time(int argc, char *argv[])
2020 {
2021         time_t now;
2022
2023         if (common_args_process(argc, argv, 2, 2, argv[0],
2024                                 "<time>", 0))
2025                 return;
2026
2027         now = string_to_time(argv[1]);
2028         if (now == ((time_t) -1)) {
2029                 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2030                         argv[1]);
2031                 return;
2032
2033         } else {
2034                 printf("Setting current time to %s\n", time_to_string(now));
2035                 current_fs->now = now;
2036         }
2037 }
2038
2039 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2040 {
2041         int compat, bit, ret;
2042         unsigned int feature_mask;
2043
2044         if (name) {
2045                 if (feature_type == E2P_FS_FEATURE)
2046                         ret = e2p_string2feature(name, &compat, &feature_mask);
2047                 else
2048                         ret = e2p_jrnl_string2feature(name, &compat,
2049                                                       &feature_mask);
2050                 if (ret)
2051                         return ret;
2052
2053                 if (!(supp[compat] & feature_mask))
2054                         return 1;
2055         } else {
2056                 for (compat = 0; compat < 3; compat++) {
2057                         for (bit = 0, feature_mask = 1; bit < 32;
2058                              bit++, feature_mask <<= 1) {
2059                                 if (supp[compat] & feature_mask) {
2060                                         if (feature_type == E2P_FS_FEATURE)
2061                                                 fprintf(stdout, " %s",
2062                                                 e2p_feature2string(compat,
2063                                                 feature_mask));
2064                                         else
2065                                                 fprintf(stdout, " %s",
2066                                                 e2p_jrnl_feature2string(compat,
2067                                                 feature_mask));
2068                                 }
2069                         }
2070                 }
2071                 fprintf(stdout, "\n");
2072         }
2073
2074         return 0;
2075 }
2076
2077 void do_supported_features(int argc, char *argv[])
2078 {
2079         int     ret;
2080         __u32   supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2081                             EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2082                             EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2083         __u32   jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2084                                  JFS_KNOWN_INCOMPAT_FEATURES,
2085                                  JFS_KNOWN_ROCOMPAT_FEATURES };
2086
2087         if (argc > 1) {
2088                 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2089                 if (ret) {
2090                         ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2091                                                 argv[1]);
2092                 }
2093                 if (ret)
2094                         com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2095                 else
2096                         fprintf(stdout, "Supported feature: %s\n", argv[1]);
2097         } else {
2098                 fprintf(stdout, "Supported features:");
2099                 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2100                 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2101         }
2102 }
2103
2104 void do_punch(int argc, char *argv[])
2105 {
2106         ext2_ino_t      ino;
2107         blk64_t         start, end;
2108         int             err;
2109         errcode_t       errcode;
2110
2111         if (common_args_process(argc, argv, 3, 4, argv[0],
2112                                 "<file> start_blk [end_blk]",
2113                                 CHECK_FS_RW | CHECK_FS_BITMAPS))
2114                 return;
2115
2116         ino = string_to_inode(argv[1]);
2117         if (!ino)
2118                 return;
2119         start = parse_ulong(argv[2], argv[0], "logical_block", &err);
2120         if (argc == 4)
2121                 end = parse_ulong(argv[3], argv[0], "logical_block", &err);
2122         else
2123                 end = ~0;
2124
2125         errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2126
2127         if (errcode) {
2128                 com_err(argv[0], errcode,
2129                         "while truncating inode %u from %llu to %llu\n", ino,
2130                         (unsigned long long) start, (unsigned long long) end);
2131                 return;
2132         }
2133 }
2134
2135 static int source_file(const char *cmd_file, int sci_idx)
2136 {
2137         FILE            *f;
2138         char            buf[256];
2139         char            *cp;
2140         int             exit_status = 0;
2141         int             retval;
2142
2143         if (strcmp(cmd_file, "-") == 0)
2144                 f = stdin;
2145         else {
2146                 f = fopen(cmd_file, "r");
2147                 if (!f) {
2148                         perror(cmd_file);
2149                         exit(1);
2150                 }
2151         }
2152         fflush(stdout);
2153         fflush(stderr);
2154         setbuf(stdout, NULL);
2155         setbuf(stderr, NULL);
2156         while (!feof(f)) {
2157                 if (fgets(buf, sizeof(buf), f) == NULL)
2158                         break;
2159                 cp = strchr(buf, '\n');
2160                 if (cp)
2161                         *cp = 0;
2162                 cp = strchr(buf, '\r');
2163                 if (cp)
2164                         *cp = 0;
2165                 printf("debugfs: %s\n", buf);
2166                 retval = ss_execute_line(sci_idx, buf);
2167                 if (retval) {
2168                         ss_perror(sci_idx, retval, buf);
2169                         exit_status++;
2170                 }
2171         }
2172         if (f != stdin)
2173                 fclose(f);
2174         return exit_status;
2175 }
2176
2177 int main(int argc, char **argv)
2178 {
2179         int             retval;
2180         int             sci_idx;
2181         const char      *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
2182         int             c;
2183         int             open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2184         char            *request = 0;
2185         int             exit_status = 0;
2186         char            *cmd_file = 0;
2187         blk64_t         superblock = 0;
2188         blk64_t         blocksize = 0;
2189         int             catastrophic = 0;
2190         char            *data_filename = 0;
2191
2192         if (debug_prog_name == 0)
2193                 debug_prog_name = "debugfs";
2194
2195         add_error_table(&et_ext2_error_table);
2196         fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2197                  E2FSPROGS_VERSION, E2FSPROGS_DATE);
2198
2199         while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:D")) != EOF) {
2200                 switch (c) {
2201                 case 'R':
2202                         request = optarg;
2203                         break;
2204                 case 'f':
2205                         cmd_file = optarg;
2206                         break;
2207                 case 'd':
2208                         data_filename = optarg;
2209                         break;
2210                 case 'i':
2211                         open_flags |= EXT2_FLAG_IMAGE_FILE;
2212                         break;
2213                 case 'w':
2214                         open_flags |= EXT2_FLAG_RW;
2215                         break;
2216                 case 'D':
2217                         open_flags |= EXT2_FLAG_DIRECT_IO;
2218                         break;
2219                 case 'b':
2220                         blocksize = parse_ulong(optarg, argv[0],
2221                                                 "block size", 0);
2222                         break;
2223                 case 's':
2224                         superblock = parse_ulong(optarg, argv[0],
2225                                                  "superblock number", 0);
2226                         break;
2227                 case 'c':
2228                         catastrophic = 1;
2229                         break;
2230                 case 'V':
2231                         /* Print version number and exit */
2232                         fprintf(stderr, "\tUsing %s\n",
2233                                 error_message(EXT2_ET_BASE));
2234                         exit(0);
2235                 default:
2236                         com_err(argv[0], 0, usage, debug_prog_name);
2237                         return 1;
2238                 }
2239         }
2240         if (optind < argc)
2241                 open_filesystem(argv[optind], open_flags,
2242                                 superblock, blocksize, catastrophic,
2243                                 data_filename);
2244
2245         sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2246                                        &debug_cmds, &retval);
2247         if (retval) {
2248                 ss_perror(sci_idx, retval, "creating invocation");
2249                 exit(1);
2250         }
2251         ss_get_readline(sci_idx);
2252
2253         (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2254         if (retval) {
2255                 ss_perror(sci_idx, retval, "adding standard requests");
2256                 exit (1);
2257         }
2258         if (extra_cmds)
2259                 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2260         if (retval) {
2261                 ss_perror(sci_idx, retval, "adding extra requests");
2262                 exit (1);
2263         }
2264         if (request) {
2265                 retval = 0;
2266                 retval = ss_execute_line(sci_idx, request);
2267                 if (retval) {
2268                         ss_perror(sci_idx, retval, request);
2269                         exit_status++;
2270                 }
2271         } else if (cmd_file) {
2272                 exit_status = source_file(cmd_file, sci_idx);
2273         } else {
2274                 ss_listen(sci_idx);
2275         }
2276
2277         ss_delete_invocation(sci_idx);
2278
2279         if (current_fs)
2280                 close_filesystem();
2281
2282         remove_error_table(&et_ext2_error_table);
2283         return exit_status;
2284 }