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