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