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