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