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