Whamcloud - gitweb
0cf0837ebe2c495b8eacd8b34ef8f5a04fff36e9
[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",
830                 prefix, inode_uid(*inode), inode_gid(*inode));
831         if (is_large_inode && large_inode->i_extra_isize >= 32)
832                 fprintf(out, "   Project: %5d", large_inode->i_projid);
833         fputs("   Size: ", out);
834         if (LINUX_S_ISREG(inode->i_mode))
835                 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
836         else
837                 fprintf(out, "%d\n", inode->i_size);
838         if (os == EXT2_OS_HURD)
839                 fprintf(out,
840                         "%sFile ACL: %d    Directory ACL: %d Translator: %d\n",
841                         prefix,
842                         inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
843                         inode->osd1.hurd1.h_i_translator);
844         else
845                 fprintf(out, "%sFile ACL: %llu    Directory ACL: %d\n",
846                         prefix,
847                         inode->i_file_acl | ((long long)
848                                 (inode->osd2.linux2.l_i_file_acl_high) << 32),
849                         LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
850         if (os == EXT2_OS_LINUX)
851                 fprintf(out, "%sLinks: %d   Blockcount: %llu\n",
852                         prefix, inode->i_links_count,
853                         (((unsigned long long)
854                           inode->osd2.linux2.l_i_blocks_hi << 32)) +
855                         inode->i_blocks);
856         else
857                 fprintf(out, "%sLinks: %d   Blockcount: %u\n",
858                         prefix, inode->i_links_count, inode->i_blocks);
859         switch (os) {
860             case EXT2_OS_HURD:
861                 frag = inode->osd2.hurd2.h_i_frag;
862                 fsize = inode->osd2.hurd2.h_i_fsize;
863                 break;
864             default:
865                 frag = fsize = 0;
866         }
867         fprintf(out, "%sFragment:  Address: %d    Number: %d    Size: %d\n",
868                 prefix, inode->i_faddr, frag, fsize);
869         if (is_large_inode && large_inode->i_extra_isize >= 24) {
870                 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
871                         inode->i_ctime, large_inode->i_ctime_extra,
872                         inode_time_to_string(inode->i_ctime,
873                                              large_inode->i_ctime_extra));
874                 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
875                         inode->i_atime, large_inode->i_atime_extra,
876                         inode_time_to_string(inode->i_atime,
877                                              large_inode->i_atime_extra));
878                 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
879                         inode->i_mtime, large_inode->i_mtime_extra,
880                         inode_time_to_string(inode->i_mtime,
881                                              large_inode->i_mtime_extra));
882                 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
883                         large_inode->i_crtime, large_inode->i_crtime_extra,
884                         inode_time_to_string(large_inode->i_crtime,
885                                              large_inode->i_crtime_extra));
886                 if (inode->i_dtime)
887                         fprintf(out, "%scrtime: 0x%08x:(%08x) -- %s", prefix,
888                                 large_inode->i_dtime, large_inode->i_ctime_extra,
889                                 inode_time_to_string(inode->i_dtime,
890                                                      large_inode->i_ctime_extra));
891         } else {
892                 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
893                         time_to_string((__s32) inode->i_ctime));
894                 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
895                         time_to_string((__s32) inode->i_atime));
896                 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
897                         time_to_string((__s32) inode->i_mtime));
898                 if (inode->i_dtime)
899                         fprintf(out, "%sdtime: 0x%08x -- %s", prefix,
900                                 inode->i_dtime,
901                                 time_to_string((__s32) inode->i_dtime));
902         }
903         if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
904                 internal_dump_inode_extra(out, prefix, inode_num,
905                                           (struct ext2_inode_large *) inode);
906         dump_inode_attributes(out, inode_num);
907         if (current_fs->super->s_creator_os == EXT2_OS_LINUX &&
908             ext2fs_has_feature_metadata_csum(current_fs->super)) {
909                 __u32 crc = inode->i_checksum_lo;
910                 if (is_large_inode &&
911                     large_inode->i_extra_isize >=
912                                 (offsetof(struct ext2_inode_large,
913                                           i_checksum_hi) -
914                                  EXT2_GOOD_OLD_INODE_SIZE))
915                         crc |= ((__u32)large_inode->i_checksum_hi) << 16;
916                 fprintf(out, "Inode checksum: 0x%08x\n", crc);
917         }
918
919         if (LINUX_S_ISLNK(inode->i_mode) &&
920             ext2fs_inode_data_blocks(current_fs, inode) == 0)
921                 dump_fast_link(out, inode_num, inode, prefix);
922         else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
923                 int major, minor;
924                 const char *devnote;
925
926                 if (inode->i_block[0]) {
927                         major = (inode->i_block[0] >> 8) & 255;
928                         minor = inode->i_block[0] & 255;
929                         devnote = "";
930                 } else {
931                         major = (inode->i_block[1] & 0xfff00) >> 8;
932                         minor = ((inode->i_block[1] & 0xff) |
933                                  ((inode->i_block[1] >> 12) & 0xfff00));
934                         devnote = "(New-style) ";
935                 }
936                 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
937                         devnote, major, minor, major, minor);
938         } else if (do_dump_blocks) {
939                 if (inode->i_flags & EXT4_EXTENTS_FL)
940                         dump_extents(out, prefix, inode_num,
941                                      DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
942                 else if (inode->i_flags & EXT4_INLINE_DATA_FL)
943                         dump_inline_data(out, prefix, inode_num);
944                 else
945                         dump_blocks(out, prefix, inode_num);
946         }
947 }
948
949 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
950 {
951         FILE    *out;
952
953         out = open_pager();
954         internal_dump_inode(out, "", inode_num, inode, 1);
955         close_pager(out);
956 }
957
958 void do_stat(int argc, char *argv[])
959 {
960         ext2_ino_t      inode;
961         struct ext2_inode * inode_buf;
962
963         if (check_fs_open(argv[0]))
964                 return;
965
966         inode_buf = (struct ext2_inode *)
967                         malloc(EXT2_INODE_SIZE(current_fs->super));
968         if (!inode_buf) {
969                 fprintf(stderr, "do_stat: can't allocate buffer\n");
970                 return;
971         }
972
973         if (common_inode_args_process(argc, argv, &inode, 0)) {
974                 free(inode_buf);
975                 return;
976         }
977
978         if (debugfs_read_inode_full(inode, inode_buf, argv[0],
979                                         EXT2_INODE_SIZE(current_fs->super))) {
980                 free(inode_buf);
981                 return;
982         }
983
984         dump_inode(inode, inode_buf);
985         free(inode_buf);
986         return;
987 }
988
989 void do_dump_extents(int argc, char **argv)
990 {
991         struct ext2_inode inode;
992         ext2_ino_t      ino;
993         FILE            *out;
994         int             c, flags = 0;
995         int             logical_width;
996         int             physical_width;
997
998         reset_getopt();
999         while ((c = getopt(argc, argv, "nl")) != EOF) {
1000                 switch (c) {
1001                 case 'n':
1002                         flags |= DUMP_NODE_EXTENTS;
1003                         break;
1004                 case 'l':
1005                         flags |= DUMP_LEAF_EXTENTS;
1006                         break;
1007                 }
1008         }
1009
1010         if (argc != optind + 1) {
1011                 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
1012                 return;
1013         }
1014
1015         if (flags == 0)
1016                 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
1017         flags |= DUMP_EXTENT_TABLE;
1018
1019         if (check_fs_open(argv[0]))
1020                 return;
1021
1022         ino = string_to_inode(argv[optind]);
1023         if (ino == 0)
1024                 return;
1025
1026         if (debugfs_read_inode(ino, &inode, argv[0]))
1027                 return;
1028
1029         if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
1030                 fprintf(stderr, "%s: does not uses extent block maps\n",
1031                         argv[optind]);
1032                 return;
1033         }
1034
1035         logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
1036                                   current_fs->blocksize) + 1;
1037         if (logical_width < 5)
1038                 logical_width = 5;
1039         physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
1040         if (physical_width < 5)
1041                 physical_width = 5;
1042
1043         out = open_pager();
1044         dump_extents(out, "", ino, flags, logical_width, physical_width);
1045         close_pager(out);
1046         return;
1047 }
1048
1049 static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
1050                              blk64_t *blocknr,
1051                              e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1052                              blk64_t ref_block EXT2FS_ATTR((unused)),
1053                              int ref_offset EXT2FS_ATTR((unused)),
1054                              void *private EXT2FS_ATTR((unused)))
1055 {
1056         printf("%llu ", *blocknr);
1057         return 0;
1058 }
1059
1060 void do_blocks(int argc, char *argv[])
1061 {
1062         ext2_ino_t      inode;
1063
1064         if (check_fs_open(argv[0]))
1065                 return;
1066
1067         if (common_inode_args_process(argc, argv, &inode, 0)) {
1068                 return;
1069         }
1070
1071         ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1072                               print_blocks_proc, NULL);
1073         fputc('\n', stdout);
1074         return;
1075 }
1076
1077 void do_chroot(int argc, char *argv[])
1078 {
1079         ext2_ino_t inode;
1080         int retval;
1081
1082         if (common_inode_args_process(argc, argv, &inode, 0))
1083                 return;
1084
1085         retval = ext2fs_check_directory(current_fs, inode);
1086         if (retval)  {
1087                 com_err(argv[1], retval, 0);
1088                 return;
1089         }
1090         root = inode;
1091 }
1092
1093 #ifndef READ_ONLY
1094 void do_clri(int argc, char *argv[])
1095 {
1096         ext2_ino_t inode;
1097         struct ext2_inode inode_buf;
1098
1099         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1100                 return;
1101
1102         if (debugfs_read_inode(inode, &inode_buf, argv[0]))
1103                 return;
1104         memset(&inode_buf, 0, sizeof(inode_buf));
1105         if (debugfs_write_inode(inode, &inode_buf, argv[0]))
1106                 return;
1107 }
1108
1109 void do_freei(int argc, char *argv[])
1110 {
1111         unsigned int    len = 1;
1112         int             err = 0;
1113         ext2_ino_t      inode;
1114
1115         if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1116                                 CHECK_FS_RW | CHECK_FS_BITMAPS))
1117                 return;
1118         if (check_fs_read_write(argv[0]))
1119                 return;
1120
1121         inode = string_to_inode(argv[1]);
1122         if (!inode)
1123                 return;
1124
1125         if (argc == 3) {
1126                 len = parse_ulong(argv[2], argv[0], "length", &err);
1127                 if (err)
1128                         return;
1129         }
1130
1131         if (len == 1 &&
1132             !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1133                 com_err(argv[0], 0, "Warning: inode already clear");
1134         while (len-- > 0)
1135                 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
1136         ext2fs_mark_ib_dirty(current_fs);
1137 }
1138
1139 void do_seti(int argc, char *argv[])
1140 {
1141         unsigned int    len = 1;
1142         int             err = 0;
1143         ext2_ino_t      inode;
1144
1145         if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1146                                 CHECK_FS_RW | CHECK_FS_BITMAPS))
1147                 return;
1148         if (check_fs_read_write(argv[0]))
1149                 return;
1150
1151         inode = string_to_inode(argv[1]);
1152         if (!inode)
1153                 return;
1154
1155         if (argc == 3) {
1156                 len = parse_ulong(argv[2], argv[0], "length", &err);
1157                 if (err)
1158                         return;
1159         }
1160
1161         if ((len == 1) &&
1162             ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1163                 com_err(argv[0], 0, "Warning: inode already set");
1164         while (len-- > 0)
1165                 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
1166         ext2fs_mark_ib_dirty(current_fs);
1167 }
1168 #endif /* READ_ONLY */
1169
1170 void do_testi(int argc, char *argv[])
1171 {
1172         ext2_ino_t inode;
1173
1174         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
1175                 return;
1176
1177         if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
1178                 printf("Inode %u is marked in use\n", inode);
1179         else
1180                 printf("Inode %u is not in use\n", inode);
1181 }
1182
1183 #ifndef READ_ONLY
1184 void do_freeb(int argc, char *argv[])
1185 {
1186         blk64_t block;
1187         blk64_t count = 1;
1188
1189         if (common_block_args_process(argc, argv, &block, &count))
1190                 return;
1191         if (check_fs_read_write(argv[0]))
1192                 return;
1193         while (count-- > 0) {
1194                 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1195                         com_err(argv[0], 0, "Warning: block %llu already clear",
1196                                 block);
1197                 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
1198                 block++;
1199         }
1200         ext2fs_mark_bb_dirty(current_fs);
1201 }
1202
1203 void do_setb(int argc, char *argv[])
1204 {
1205         blk64_t block;
1206         blk64_t count = 1;
1207
1208         if (common_block_args_process(argc, argv, &block, &count))
1209                 return;
1210         if (check_fs_read_write(argv[0]))
1211                 return;
1212         while (count-- > 0) {
1213                 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1214                         com_err(argv[0], 0, "Warning: block %llu already set",
1215                                 block);
1216                 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
1217                 block++;
1218         }
1219         ext2fs_mark_bb_dirty(current_fs);
1220 }
1221 #endif /* READ_ONLY */
1222
1223 void do_testb(int argc, char *argv[])
1224 {
1225         blk64_t block;
1226         blk64_t count = 1;
1227
1228         if (common_block_args_process(argc, argv, &block, &count))
1229                 return;
1230         while (count-- > 0) {
1231                 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1232                         printf("Block %llu marked in use\n", block);
1233                 else
1234                         printf("Block %llu not in use\n", block);
1235                 block++;
1236         }
1237 }
1238
1239 #ifndef READ_ONLY
1240 static void modify_u8(char *com, const char *prompt,
1241                       const char *format, __u8 *val)
1242 {
1243         char buf[200];
1244         unsigned long v;
1245         char *tmp;
1246
1247         sprintf(buf, format, *val);
1248         printf("%30s    [%s] ", prompt, buf);
1249         if (!fgets(buf, sizeof(buf), stdin))
1250                 return;
1251         if (buf[strlen (buf) - 1] == '\n')
1252                 buf[strlen (buf) - 1] = '\0';
1253         if (!buf[0])
1254                 return;
1255         v = strtoul(buf, &tmp, 0);
1256         if (*tmp)
1257                 com_err(com, 0, "Bad value - %s", buf);
1258         else
1259                 *val = v;
1260 }
1261
1262 static void modify_u16(char *com, const char *prompt,
1263                        const char *format, __u16 *val)
1264 {
1265         char buf[200];
1266         unsigned long v;
1267         char *tmp;
1268
1269         sprintf(buf, format, *val);
1270         printf("%30s    [%s] ", prompt, buf);
1271         if (!fgets(buf, sizeof(buf), stdin))
1272                 return;
1273         if (buf[strlen (buf) - 1] == '\n')
1274                 buf[strlen (buf) - 1] = '\0';
1275         if (!buf[0])
1276                 return;
1277         v = strtoul(buf, &tmp, 0);
1278         if (*tmp)
1279                 com_err(com, 0, "Bad value - %s", buf);
1280         else
1281                 *val = v;
1282 }
1283
1284 static void modify_u32(char *com, const char *prompt,
1285                        const char *format, __u32 *val)
1286 {
1287         char buf[200];
1288         unsigned long v;
1289         char *tmp;
1290
1291         sprintf(buf, format, *val);
1292         printf("%30s    [%s] ", prompt, buf);
1293         if (!fgets(buf, sizeof(buf), stdin))
1294                 return;
1295         if (buf[strlen (buf) - 1] == '\n')
1296                 buf[strlen (buf) - 1] = '\0';
1297         if (!buf[0])
1298                 return;
1299         v = strtoul(buf, &tmp, 0);
1300         if (*tmp)
1301                 com_err(com, 0, "Bad value - %s", buf);
1302         else
1303                 *val = v;
1304 }
1305
1306
1307 void do_modify_inode(int argc, char *argv[])
1308 {
1309         struct ext2_inode inode;
1310         ext2_ino_t      inode_num;
1311         int             i;
1312         unsigned char   *frag, *fsize;
1313         char            buf[80];
1314         int             os;
1315         const char      *hex_format = "0x%x";
1316         const char      *octal_format = "0%o";
1317         const char      *decimal_format = "%d";
1318         const char      *unsignedlong_format = "%lu";
1319
1320         if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1321                 return;
1322
1323         os = current_fs->super->s_creator_os;
1324
1325         if (debugfs_read_inode(inode_num, &inode, argv[1]))
1326                 return;
1327
1328         modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1329         modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1330         modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
1331         modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
1332         modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1333         modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1334         modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1335         modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1336         modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
1337         if (os == EXT2_OS_LINUX)
1338                 modify_u16(argv[0], "Block count high", unsignedlong_format,
1339                            &inode.osd2.linux2.l_i_blocks_hi);
1340         modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
1341         modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
1342         modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
1343 #if 0
1344         modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1345 #endif
1346         modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
1347         if (LINUX_S_ISDIR(inode.i_mode))
1348                 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1349         else
1350                 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
1351
1352         if (os == EXT2_OS_HURD)
1353                 modify_u32(argv[0], "Translator Block",
1354                             decimal_format, &inode.osd1.hurd1.h_i_translator);
1355
1356         modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
1357         switch (os) {
1358             case EXT2_OS_HURD:
1359                 frag = &inode.osd2.hurd2.h_i_frag;
1360                 fsize = &inode.osd2.hurd2.h_i_fsize;
1361                 break;
1362             default:
1363                 frag = fsize = 0;
1364         }
1365         if (frag)
1366                 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1367         if (fsize)
1368                 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1369
1370         for (i=0;  i < EXT2_NDIR_BLOCKS; i++) {
1371                 sprintf(buf, "Direct Block #%d", i);
1372                 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
1373         }
1374         modify_u32(argv[0], "Indirect Block", decimal_format,
1375                     &inode.i_block[EXT2_IND_BLOCK]);
1376         modify_u32(argv[0], "Double Indirect Block", decimal_format,
1377                     &inode.i_block[EXT2_DIND_BLOCK]);
1378         modify_u32(argv[0], "Triple Indirect Block", decimal_format,
1379                     &inode.i_block[EXT2_TIND_BLOCK]);
1380         if (debugfs_write_inode(inode_num, &inode, argv[1]))
1381                 return;
1382 }
1383 #endif /* READ_ONLY */
1384
1385 void do_change_working_dir(int argc, char *argv[])
1386 {
1387         ext2_ino_t      inode;
1388         int             retval;
1389
1390         if (common_inode_args_process(argc, argv, &inode, 0))
1391                 return;
1392
1393         retval = ext2fs_check_directory(current_fs, inode);
1394         if (retval) {
1395                 com_err(argv[1], retval, 0);
1396                 return;
1397         }
1398         cwd = inode;
1399         return;
1400 }
1401
1402 void do_print_working_directory(int argc, char *argv[])
1403 {
1404         int     retval;
1405         char    *pathname = NULL;
1406
1407         if (common_args_process(argc, argv, 1, 1,
1408                                 "print_working_directory", "", 0))
1409                 return;
1410
1411         retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
1412         if (retval) {
1413                 com_err(argv[0], retval,
1414                         "while trying to get pathname of cwd");
1415         }
1416         printf("[pwd]   INODE: %6u  PATH: %s\n",
1417                cwd, pathname ? pathname : "NULL");
1418         if (pathname) {
1419                 free(pathname);
1420                 pathname = NULL;
1421         }
1422         retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
1423         if (retval) {
1424                 com_err(argv[0], retval,
1425                         "while trying to get pathname of root");
1426         }
1427         printf("[root]  INODE: %6u  PATH: %s\n",
1428                root, pathname ? pathname : "NULL");
1429         if (pathname) {
1430                 free(pathname);
1431                 pathname = NULL;
1432         }
1433         return;
1434 }
1435
1436 #ifndef READ_ONLY
1437 static void make_link(char *sourcename, char *destname)
1438 {
1439         ext2_ino_t      ino;
1440         struct ext2_inode inode;
1441         int             retval;
1442         ext2_ino_t      dir;
1443         char            *dest, *cp, *base_name;
1444
1445         /*
1446          * Get the source inode
1447          */
1448         ino = string_to_inode(sourcename);
1449         if (!ino)
1450                 return;
1451         base_name = strrchr(sourcename, '/');
1452         if (base_name)
1453                 base_name++;
1454         else
1455                 base_name = sourcename;
1456         /*
1457          * Figure out the destination.  First see if it exists and is
1458          * a directory.
1459          */
1460         if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
1461                 dest = base_name;
1462         else {
1463                 /*
1464                  * OK, it doesn't exist.  See if it is
1465                  * '<dir>/basename' or 'basename'
1466                  */
1467                 cp = strrchr(destname, '/');
1468                 if (cp) {
1469                         *cp = 0;
1470                         dir = string_to_inode(destname);
1471                         if (!dir)
1472                                 return;
1473                         dest = cp+1;
1474                 } else {
1475                         dir = cwd;
1476                         dest = destname;
1477                 }
1478         }
1479
1480         if (debugfs_read_inode(ino, &inode, sourcename))
1481                 return;
1482
1483         retval = ext2fs_link(current_fs, dir, dest, ino,
1484                              ext2_file_type(inode.i_mode));
1485         if (retval)
1486                 com_err("make_link", retval, 0);
1487         return;
1488 }
1489
1490
1491 void do_link(int argc, char *argv[])
1492 {
1493         if (common_args_process(argc, argv, 3, 3, "link",
1494                                 "<source file> <dest_name>", CHECK_FS_RW))
1495                 return;
1496
1497         make_link(argv[1], argv[2]);
1498 }
1499
1500 static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1501                             e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1502                             blk64_t ref_block EXT2FS_ATTR((unused)),
1503                             int ref_offset EXT2FS_ATTR((unused)),
1504                             void *private EXT2FS_ATTR((unused)))
1505 {
1506         blk64_t block;
1507
1508         block = *blocknr;
1509         ext2fs_block_alloc_stats2(fs, block, +1);
1510         return 0;
1511 }
1512
1513 void do_undel(int argc, char *argv[])
1514 {
1515         ext2_ino_t      ino;
1516         struct ext2_inode inode;
1517
1518         if (common_args_process(argc, argv, 2, 3, "undelete",
1519                                 "<inode_num> [dest_name]",
1520                                 CHECK_FS_RW | CHECK_FS_BITMAPS))
1521                 return;
1522
1523         ino = string_to_inode(argv[1]);
1524         if (!ino)
1525                 return;
1526
1527         if (debugfs_read_inode(ino, &inode, argv[1]))
1528                 return;
1529
1530         if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
1531                 com_err(argv[1], 0, "Inode is not marked as deleted");
1532                 return;
1533         }
1534
1535         /*
1536          * XXX this function doesn't handle changing the links count on the
1537          * parent directory when undeleting a directory.
1538          */
1539         inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1540         inode.i_dtime = 0;
1541
1542         if (debugfs_write_inode(ino, &inode, argv[0]))
1543                 return;
1544
1545         ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1546                               mark_blocks_proc, NULL);
1547
1548         ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
1549
1550         if (argc > 2)
1551                 make_link(argv[1], argv[2]);
1552 }
1553
1554 static void unlink_file_by_name(char *filename)
1555 {
1556         int             retval;
1557         ext2_ino_t      dir;
1558         char            *base_name;
1559
1560         base_name = strrchr(filename, '/');
1561         if (base_name) {
1562                 *base_name++ = '\0';
1563                 dir = string_to_inode(filename);
1564                 if (!dir)
1565                         return;
1566         } else {
1567                 dir = cwd;
1568                 base_name = filename;
1569         }
1570         retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
1571         if (retval)
1572                 com_err("unlink_file_by_name", retval, 0);
1573         return;
1574 }
1575
1576 void do_unlink(int argc, char *argv[])
1577 {
1578         if (common_args_process(argc, argv, 2, 2, "link",
1579                                 "<pathname>", CHECK_FS_RW))
1580                 return;
1581
1582         unlink_file_by_name(argv[1]);
1583 }
1584 #endif /* READ_ONLY */
1585
1586 void do_find_free_block(int argc, char *argv[])
1587 {
1588         blk64_t free_blk, goal, first_free = 0;
1589         int             count;
1590         errcode_t       retval;
1591         char            *tmp;
1592
1593         if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1594                 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
1595                 return;
1596         }
1597         if (check_fs_open(argv[0]))
1598                 return;
1599
1600         if (argc > 1) {
1601                 count = strtol(argv[1],&tmp,0);
1602                 if (*tmp) {
1603                         com_err(argv[0], 0, "Bad count - %s", argv[1]);
1604                         return;
1605                 }
1606         } else
1607                 count = 1;
1608
1609         if (argc > 2) {
1610                 goal = strtol(argv[2], &tmp, 0);
1611                 if (*tmp) {
1612                         com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1613                         return;
1614                 }
1615         }
1616         else
1617                 goal = current_fs->super->s_first_data_block;
1618
1619         printf("Free blocks found: ");
1620         free_blk = goal - 1;
1621         while (count-- > 0) {
1622                 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1623                                            &free_blk);
1624                 if (first_free) {
1625                         if (first_free == free_blk)
1626                                 break;
1627                 } else
1628                         first_free = free_blk;
1629                 if (retval) {
1630                         com_err("ext2fs_new_block", retval, 0);
1631                         return;
1632                 } else
1633                         printf("%llu ", free_blk);
1634         }
1635         printf("\n");
1636 }
1637
1638 void do_find_free_inode(int argc, char *argv[])
1639 {
1640         ext2_ino_t      free_inode, dir;
1641         int             mode;
1642         int             retval;
1643         char            *tmp;
1644
1645         if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1646                 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1647                 return;
1648         }
1649         if (check_fs_open(argv[0]))
1650                 return;
1651
1652         if (argc > 1) {
1653                 dir = strtol(argv[1], &tmp, 0);
1654                 if (*tmp) {
1655                         com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1656                         return;
1657                 }
1658         }
1659         else
1660                 dir = root;
1661         if (argc > 2) {
1662                 mode = strtol(argv[2], &tmp, 0);
1663                 if (*tmp) {
1664                         com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1665                         return;
1666                 }
1667         } else
1668                 mode = 010755;
1669
1670         retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1671         if (retval)
1672                 com_err("ext2fs_new_inode", retval, 0);
1673         else
1674                 printf("Free inode found: %u\n", free_inode);
1675 }
1676
1677 #ifndef READ_ONLY
1678 void do_write(int argc, char *argv[])
1679 {
1680         errcode_t       retval;
1681
1682         if (common_args_process(argc, argv, 3, 3, "write",
1683                                 "<native file> <new file>", CHECK_FS_RW))
1684                 return;
1685
1686         retval = do_write_internal(current_fs, cwd, argv[1], argv[2], root);
1687         if (retval)
1688                 com_err(argv[0], retval, 0);
1689 }
1690
1691 void do_mknod(int argc, char *argv[])
1692 {
1693         unsigned long   major, minor;
1694         errcode_t       retval;
1695         int             nr;
1696         struct stat     st;
1697
1698         if (check_fs_open(argv[0]))
1699                 return;
1700         if (argc < 3 || argv[2][1]) {
1701         usage:
1702                 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1703                 return;
1704         }
1705
1706         minor = major = 0;
1707         switch (argv[2][0]) {
1708                 case 'p':
1709                         st.st_mode = S_IFIFO;
1710                         nr = 3;
1711                         break;
1712                 case 'c':
1713                         st.st_mode = S_IFCHR;
1714                         nr = 5;
1715                         break;
1716                 case 'b':
1717                         st.st_mode = S_IFBLK;
1718                         nr = 5;
1719                         break;
1720                 default:
1721                         nr = 0;
1722         }
1723
1724         if (nr == 5) {
1725                 major = strtoul(argv[3], argv+3, 0);
1726                 minor = strtoul(argv[4], argv+4, 0);
1727                 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
1728                         nr = 0;
1729         }
1730
1731         if (argc != nr)
1732                 goto usage;
1733
1734         st.st_rdev = makedev(major, minor);
1735         retval = do_mknod_internal(current_fs, cwd, argv[1], &st);
1736         if (retval)
1737                 com_err(argv[0], retval, 0);
1738 }
1739
1740 void do_mkdir(int argc, char *argv[])
1741 {
1742         errcode_t retval;
1743
1744         if (common_args_process(argc, argv, 2, 2, "mkdir",
1745                                 "<filename>", CHECK_FS_RW))
1746                 return;
1747
1748         retval = do_mkdir_internal(current_fs, cwd, argv[1], root);
1749         if (retval)
1750                 com_err(argv[0], retval, 0);
1751
1752 }
1753
1754 static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1755                                e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1756                                blk64_t ref_block EXT2FS_ATTR((unused)),
1757                                int ref_offset EXT2FS_ATTR((unused)),
1758                                void *private EXT2FS_ATTR((unused)))
1759 {
1760         blk64_t block;
1761
1762         block = *blocknr;
1763         ext2fs_block_alloc_stats2(fs, block, -1);
1764         return 0;
1765 }
1766
1767 static void kill_file_by_inode(ext2_ino_t inode)
1768 {
1769         struct ext2_inode inode_buf;
1770
1771         if (debugfs_read_inode(inode, &inode_buf, 0))
1772                 return;
1773         inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
1774         if (debugfs_write_inode(inode, &inode_buf, 0))
1775                 return;
1776         if (ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf)) {
1777                 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY,
1778                                       NULL, release_blocks_proc, NULL);
1779         }
1780         printf("\n");
1781         ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1782                                   LINUX_S_ISDIR(inode_buf.i_mode));
1783 }
1784
1785
1786 void do_kill_file(int argc, char *argv[])
1787 {
1788         ext2_ino_t inode_num;
1789
1790         if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1791                 return;
1792
1793         kill_file_by_inode(inode_num);
1794 }
1795
1796 void do_rm(int argc, char *argv[])
1797 {
1798         int retval;
1799         ext2_ino_t inode_num;
1800         struct ext2_inode inode;
1801
1802         if (common_args_process(argc, argv, 2, 2, "rm",
1803                                 "<filename>", CHECK_FS_RW))
1804                 return;
1805
1806         retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1807         if (retval) {
1808                 com_err(argv[0], retval, "while trying to resolve filename");
1809                 return;
1810         }
1811
1812         if (debugfs_read_inode(inode_num, &inode, argv[0]))
1813                 return;
1814
1815         if (LINUX_S_ISDIR(inode.i_mode)) {
1816                 com_err(argv[0], 0, "file is a directory");
1817                 return;
1818         }
1819
1820         --inode.i_links_count;
1821         if (debugfs_write_inode(inode_num, &inode, argv[0]))
1822                 return;
1823
1824         unlink_file_by_name(argv[1]);
1825         if (inode.i_links_count == 0)
1826                 kill_file_by_inode(inode_num);
1827 }
1828
1829 struct rd_struct {
1830         ext2_ino_t      parent;
1831         int             empty;
1832 };
1833
1834 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1835                       int       entry EXT2FS_ATTR((unused)),
1836                       struct ext2_dir_entry *dirent,
1837                       int       offset EXT2FS_ATTR((unused)),
1838                       int       blocksize EXT2FS_ATTR((unused)),
1839                       char      *buf EXT2FS_ATTR((unused)),
1840                       void      *private)
1841 {
1842         struct rd_struct *rds = (struct rd_struct *) private;
1843
1844         if (dirent->inode == 0)
1845                 return 0;
1846         if ((ext2fs_dirent_name_len(dirent) == 1) && (dirent->name[0] == '.'))
1847                 return 0;
1848         if ((ext2fs_dirent_name_len(dirent) == 2) && (dirent->name[0] == '.') &&
1849             (dirent->name[1] == '.')) {
1850                 rds->parent = dirent->inode;
1851                 return 0;
1852         }
1853         rds->empty = 0;
1854         return 0;
1855 }
1856
1857 void do_rmdir(int argc, char *argv[])
1858 {
1859         int retval;
1860         ext2_ino_t inode_num;
1861         struct ext2_inode inode;
1862         struct rd_struct rds;
1863
1864         if (common_args_process(argc, argv, 2, 2, "rmdir",
1865                                 "<filename>", CHECK_FS_RW))
1866                 return;
1867
1868         retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1869         if (retval) {
1870                 com_err(argv[0], retval, "while trying to resolve filename");
1871                 return;
1872         }
1873
1874         if (debugfs_read_inode(inode_num, &inode, argv[0]))
1875                 return;
1876
1877         if (!LINUX_S_ISDIR(inode.i_mode)) {
1878                 com_err(argv[0], 0, "file is not a directory");
1879                 return;
1880         }
1881
1882         rds.parent = 0;
1883         rds.empty = 1;
1884
1885         retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1886                                     0, rmdir_proc, &rds);
1887         if (retval) {
1888                 com_err(argv[0], retval, "while iterating over directory");
1889                 return;
1890         }
1891         if (rds.empty == 0) {
1892                 com_err(argv[0], 0, "directory not empty");
1893                 return;
1894         }
1895
1896         inode.i_links_count = 0;
1897         if (debugfs_write_inode(inode_num, &inode, argv[0]))
1898                 return;
1899
1900         unlink_file_by_name(argv[1]);
1901         kill_file_by_inode(inode_num);
1902
1903         if (rds.parent) {
1904                 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1905                         return;
1906                 if (inode.i_links_count > 1)
1907                         inode.i_links_count--;
1908                 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1909                         return;
1910         }
1911 }
1912 #endif /* READ_ONLY */
1913
1914 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1915                             char *argv[] EXT2FS_ATTR((unused)))
1916 {
1917         if (current_fs)
1918                 printf("Open mode: read-%s\n",
1919                        current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1920         printf("Filesystem in use: %s\n",
1921                current_fs ? current_fs->device_name : "--none--");
1922 }
1923
1924 #ifndef READ_ONLY
1925 void do_expand_dir(int argc, char *argv[])
1926 {
1927         ext2_ino_t inode;
1928         int retval;
1929
1930         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1931                 return;
1932
1933         retval = ext2fs_expand_dir(current_fs, inode);
1934         if (retval)
1935                 com_err("ext2fs_expand_dir", retval, 0);
1936         return;
1937 }
1938
1939 void do_features(int argc, char *argv[])
1940 {
1941         int     i;
1942
1943         if (check_fs_open(argv[0]))
1944                 return;
1945
1946         if ((argc != 1) && check_fs_read_write(argv[0]))
1947                 return;
1948         for (i=1; i < argc; i++) {
1949                 if (e2p_edit_feature(argv[i],
1950                                      &current_fs->super->s_feature_compat, 0))
1951                         com_err(argv[0], 0, "Unknown feature: %s\n",
1952                                 argv[i]);
1953                 else
1954                         ext2fs_mark_super_dirty(current_fs);
1955         }
1956         print_features(current_fs->super, stdout);
1957 }
1958 #endif /* READ_ONLY */
1959
1960 void do_bmap(int argc, char *argv[])
1961 {
1962         ext2_ino_t      ino;
1963         blk64_t         blk, pblk = 0;
1964         int             c, err, flags = 0, ret_flags = 0;
1965         errcode_t       errcode;
1966
1967         if (check_fs_open(argv[0]))
1968                 return;
1969
1970         reset_getopt();
1971         while ((c = getopt (argc, argv, "a")) != EOF) {
1972                 switch (c) {
1973                 case 'a':
1974                         flags |= BMAP_ALLOC;
1975                         break;
1976                 default:
1977                         goto print_usage;
1978                 }
1979         }
1980
1981         if (argc <= optind+1) {
1982         print_usage:
1983                 com_err(0, 0,
1984                         "Usage: bmap [-a] <file> logical_blk [physical_blk]");
1985                 return;
1986         }
1987
1988         ino = string_to_inode(argv[optind++]);
1989         if (!ino)
1990                 return;
1991         err = strtoblk(argv[0], argv[optind++], "logical block", &blk);
1992         if (err)
1993                 return;
1994
1995         if (argc > optind+1)
1996                 goto print_usage;
1997
1998         if (argc == optind+1) {
1999                 err = strtoblk(argv[0], argv[optind++],
2000                                "physical block", &pblk);
2001                 if (err)
2002                         return;
2003                 if (flags & BMAP_ALLOC) {
2004                         com_err(0, 0, "Can't set and allocate a block");
2005                         return;
2006                 }
2007                 flags |= BMAP_SET;
2008         }
2009
2010         errcode = ext2fs_bmap2(current_fs, ino, 0, 0, flags, blk,
2011                                &ret_flags, &pblk);
2012         if (errcode) {
2013                 com_err(argv[0], errcode,
2014                         "while mapping logical block %llu\n", blk);
2015                 return;
2016         }
2017         printf("%llu", pblk);
2018         if (ret_flags & BMAP_RET_UNINIT)
2019                 fputs(" (uninit)", stdout);
2020         fputc('\n', stdout);
2021 }
2022
2023 void do_imap(int argc, char *argv[])
2024 {
2025         ext2_ino_t      ino;
2026         unsigned long   group, block, block_nr, offset;
2027
2028         if (common_args_process(argc, argv, 2, 2, argv[0],
2029                                 "<file>", 0))
2030                 return;
2031         ino = string_to_inode(argv[1]);
2032         if (!ino)
2033                 return;
2034
2035         group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2036         offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2037                 EXT2_INODE_SIZE(current_fs->super);
2038         block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
2039         if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
2040                 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
2041                         group);
2042                 return;
2043         }
2044         block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
2045                 block;
2046         offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2047
2048         printf("Inode %d is part of block group %lu\n"
2049                "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
2050                block_nr, offset);
2051
2052 }
2053
2054 void do_idump(int argc, char *argv[])
2055 {
2056         ext2_ino_t      ino;
2057         unsigned char   *buf;
2058         errcode_t       err;
2059         int             isize;
2060
2061         if (common_args_process(argc, argv, 2, 2, argv[0],
2062                                 "<file>", 0))
2063                 return;
2064         ino = string_to_inode(argv[1]);
2065         if (!ino)
2066                 return;
2067
2068         isize = EXT2_INODE_SIZE(current_fs->super);
2069         err = ext2fs_get_mem(isize, &buf);
2070         if (err) {
2071                 com_err(argv[0], err, "while allocating memory");
2072                 return;
2073         }
2074
2075         err = ext2fs_read_inode_full(current_fs, ino,
2076                                      (struct ext2_inode *)buf, isize);
2077         if (err) {
2078                 com_err(argv[0], err, "while reading inode %d", ino);
2079                 goto err;
2080         }
2081
2082         do_byte_hexdump(stdout, buf, isize);
2083 err:
2084         ext2fs_free_mem(&buf);
2085 }
2086
2087 #ifndef READ_ONLY
2088 void do_set_current_time(int argc, char *argv[])
2089 {
2090         __s64 now;
2091
2092         if (common_args_process(argc, argv, 2, 2, argv[0],
2093                                 "<time>", 0))
2094                 return;
2095
2096         now = string_to_time(argv[1]);
2097         if (now == -1) {
2098                 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2099                         argv[1]);
2100                 return;
2101
2102         } else {
2103                 printf("Setting current time to %s\n", time_to_string(now));
2104                 current_fs->now = now;
2105         }
2106 }
2107 #endif /* READ_ONLY */
2108
2109 static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2110 {
2111         int compat, bit, ret;
2112         unsigned int feature_mask;
2113
2114         if (name) {
2115                 if (feature_type == E2P_FS_FEATURE)
2116                         ret = e2p_string2feature(name, &compat, &feature_mask);
2117                 else
2118                         ret = e2p_jrnl_string2feature(name, &compat,
2119                                                       &feature_mask);
2120                 if (ret)
2121                         return ret;
2122
2123                 if (!(supp[compat] & feature_mask))
2124                         return 1;
2125         } else {
2126                 for (compat = 0; compat < 3; compat++) {
2127                         for (bit = 0, feature_mask = 1; bit < 32;
2128                              bit++, feature_mask <<= 1) {
2129                                 if (supp[compat] & feature_mask) {
2130                                         if (feature_type == E2P_FS_FEATURE)
2131                                                 fprintf(stdout, " %s",
2132                                                 e2p_feature2string(compat,
2133                                                 feature_mask));
2134                                         else
2135                                                 fprintf(stdout, " %s",
2136                                                 e2p_jrnl_feature2string(compat,
2137                                                 feature_mask));
2138                                 }
2139                         }
2140                 }
2141                 fprintf(stdout, "\n");
2142         }
2143
2144         return 0;
2145 }
2146
2147 void do_supported_features(int argc, char *argv[])
2148 {
2149         int     ret;
2150         __u32   supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2151                             EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2152                             EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2153         __u32   jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2154                                  JFS_KNOWN_INCOMPAT_FEATURES,
2155                                  JFS_KNOWN_ROCOMPAT_FEATURES };
2156
2157         if (argc > 1) {
2158                 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2159                 if (ret) {
2160                         ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2161                                                 argv[1]);
2162                 }
2163                 if (ret)
2164                         com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2165                 else
2166                         fprintf(stdout, "Supported feature: %s\n", argv[1]);
2167         } else {
2168                 fprintf(stdout, "Supported features:");
2169                 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2170                 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2171         }
2172 }
2173
2174 #ifndef READ_ONLY
2175 void do_punch(int argc, char *argv[])
2176 {
2177         ext2_ino_t      ino;
2178         blk64_t         start, end;
2179         int             err;
2180         errcode_t       errcode;
2181
2182         if (common_args_process(argc, argv, 3, 4, argv[0],
2183                                 "<file> start_blk [end_blk]",
2184                                 CHECK_FS_RW | CHECK_FS_BITMAPS))
2185                 return;
2186
2187         ino = string_to_inode(argv[1]);
2188         if (!ino)
2189                 return;
2190         err = strtoblk(argv[0], argv[2], "logical block", &start);
2191         if (err)
2192                 return;
2193         if (argc == 4) {
2194                 err = strtoblk(argv[0], argv[3], "logical block", &end);
2195                 if (err)
2196                         return;
2197         } else
2198                 end = ~0;
2199
2200         errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2201
2202         if (errcode) {
2203                 com_err(argv[0], errcode,
2204                         "while truncating inode %u from %llu to %llu\n", ino,
2205                         (unsigned long long) start, (unsigned long long) end);
2206                 return;
2207         }
2208 }
2209
2210 void do_fallocate(int argc, char *argv[])
2211 {
2212         ext2_ino_t      ino;
2213         blk64_t         start, end;
2214         int             err;
2215         errcode_t       errcode;
2216
2217         if (common_args_process(argc, argv, 3, 4, argv[0],
2218                                 "<file> start_blk [end_blk]",
2219                                 CHECK_FS_RW | CHECK_FS_BITMAPS))
2220                 return;
2221
2222         ino = string_to_inode(argv[1]);
2223         if (!ino)
2224                 return;
2225         err = strtoblk(argv[0], argv[2], "logical block", &start);
2226         if (err)
2227                 return;
2228         if (argc == 4) {
2229                 err = strtoblk(argv[0], argv[3], "logical block", &end);
2230                 if (err)
2231                         return;
2232         } else
2233                 end = ~0;
2234
2235         errcode = ext2fs_fallocate(current_fs, EXT2_FALLOCATE_INIT_BEYOND_EOF,
2236                                    ino, NULL, ~0ULL, start, end - start + 1);
2237
2238         if (errcode) {
2239                 com_err(argv[0], errcode,
2240                         "while fallocating inode %u from %llu to %llu\n", ino,
2241                         (unsigned long long) start, (unsigned long long) end);
2242                 return;
2243         }
2244 }
2245 #endif /* READ_ONLY */
2246
2247 void do_symlink(int argc, char *argv[])
2248 {
2249         errcode_t       retval;
2250
2251         if (common_args_process(argc, argv, 3, 3, "symlink",
2252                                 "<filename> <target>", CHECK_FS_RW))
2253                 return;
2254
2255         retval = do_symlink_internal(current_fs, cwd, argv[1], argv[2], root);
2256         if (retval)
2257                 com_err(argv[0], retval, 0);
2258
2259 }
2260
2261 #if CONFIG_MMP
2262 void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2263 {
2264         struct mmp_struct *mmp_s;
2265         unsigned long long mmp_block;
2266         time_t t;
2267         errcode_t retval = 0;
2268
2269         if (check_fs_open(argv[0]))
2270                 return;
2271
2272         if (argc > 1) {
2273                 char *end = NULL;
2274                 mmp_block = strtoull(argv[1], &end, 0);
2275                 if (end == argv[0] || mmp_block == 0) {
2276                         fprintf(stderr, "%s: invalid MMP block '%s' given\n",
2277                                 argv[0], argv[1]);
2278                         return;
2279                 }
2280         } else {
2281                 mmp_block = current_fs->super->s_mmp_block;
2282         }
2283
2284         if (mmp_block == 0) {
2285                 fprintf(stderr, "%s: MMP: not active on this filesystem.\n",
2286                         argv[0]);
2287                 return;
2288         }
2289
2290         if (current_fs->mmp_buf == NULL) {
2291                 retval = ext2fs_get_mem(current_fs->blocksize,
2292                                         &current_fs->mmp_buf);
2293                 if (retval) {
2294                         com_err(argv[0], retval, "allocating MMP buffer.\n");
2295                         return;
2296                 }
2297         }
2298
2299         mmp_s = current_fs->mmp_buf;
2300
2301         retval = ext2fs_mmp_read(current_fs, mmp_block, current_fs->mmp_buf);
2302         if (retval) {
2303                 com_err(argv[0], retval, "reading MMP block %llu.\n",
2304                         mmp_block);
2305                 return;
2306         }
2307
2308         t = mmp_s->mmp_time;
2309         fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2310         fprintf(stdout, "update_interval: %d\n",
2311                 current_fs->super->s_mmp_update_interval);
2312         fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2313         fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2314         fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2315         fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2316         fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2317         fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2318         fprintf(stdout, "checksum: 0x%08x\n", mmp_s->mmp_checksum);
2319         fprintf(stdout, "MMP is unsupported, please recompile with "
2320                         "--enable-mmp\n");
2321 }
2322 #else
2323 void do_dump_mmp(int argc EXT2FS_ATTR((unused)),
2324                  char *argv[] EXT2FS_ATTR((unused)))
2325 {
2326         fprintf(stdout, "MMP is unsupported, please recompile with "
2327                         "--enable-mmp\n");
2328 }
2329 #endif
2330
2331 static int source_file(const char *cmd_file, int ss_idx)
2332 {
2333         FILE            *f;
2334         char            buf[BUFSIZ];
2335         char            *cp;
2336         int             exit_status = 0;
2337         int             retval;
2338
2339         if (strcmp(cmd_file, "-") == 0)
2340                 f = stdin;
2341         else {
2342                 f = fopen(cmd_file, "r");
2343                 if (!f) {
2344                         perror(cmd_file);
2345                         exit(1);
2346                 }
2347         }
2348         fflush(stdout);
2349         fflush(stderr);
2350         setbuf(stdout, NULL);
2351         setbuf(stderr, NULL);
2352         while (!feof(f)) {
2353                 if (fgets(buf, sizeof(buf), f) == NULL)
2354                         break;
2355                 cp = strchr(buf, '\n');
2356                 if (cp)
2357                         *cp = 0;
2358                 cp = strchr(buf, '\r');
2359                 if (cp)
2360                         *cp = 0;
2361                 printf("debugfs: %s\n", buf);
2362                 retval = ss_execute_line(ss_idx, buf);
2363                 if (retval) {
2364                         ss_perror(ss_idx, retval, buf);
2365                         exit_status++;
2366                 }
2367         }
2368         if (f != stdin)
2369                 fclose(f);
2370         return exit_status;
2371 }
2372
2373 int main(int argc, char **argv)
2374 {
2375         int             retval;
2376         const char      *usage = 
2377                 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2378                 "[-R request] [-V] ["
2379 #ifndef READ_ONLY
2380                 "[-w] [-z undo_file] "
2381 #endif
2382                 "[-c] device]";
2383         int             c;
2384         int             open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
2385         char            *request = 0;
2386         int             exit_status = 0;
2387         char            *cmd_file = 0;
2388         blk64_t         superblock = 0;
2389         blk64_t         blocksize = 0;
2390         int             catastrophic = 0;
2391         char            *data_filename = 0;
2392 #ifdef READ_ONLY
2393         const char      *opt_string = "nicR:f:b:s:Vd:D";
2394 #else
2395         const char      *opt_string = "niwcR:f:b:s:Vd:Dz:";
2396         char            *undo_file = NULL;
2397 #endif
2398
2399         if (debug_prog_name == 0)
2400 #ifdef READ_ONLY
2401                 debug_prog_name = "rdebugfs";
2402 #else
2403                 debug_prog_name = "debugfs";
2404 #endif
2405         add_error_table(&et_ext2_error_table);
2406         fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2407                  E2FSPROGS_VERSION, E2FSPROGS_DATE);
2408
2409         while ((c = getopt (argc, argv, opt_string)) != EOF) {
2410                 switch (c) {
2411                 case 'R':
2412                         request = optarg;
2413                         break;
2414                 case 'f':
2415                         cmd_file = optarg;
2416                         break;
2417                 case 'd':
2418                         data_filename = optarg;
2419                         break;
2420                 case 'i':
2421                         open_flags |= EXT2_FLAG_IMAGE_FILE;
2422                         break;
2423                 case 'n':
2424                         open_flags |= EXT2_FLAG_IGNORE_CSUM_ERRORS;
2425                         break;
2426 #ifndef READ_ONLY
2427                 case 'w':
2428                         open_flags |= EXT2_FLAG_RW;
2429                         break;
2430 #endif
2431                 case 'D':
2432                         open_flags |= EXT2_FLAG_DIRECT_IO;
2433                         break;
2434                 case 'b':
2435                         blocksize = parse_ulong(optarg, argv[0],
2436                                                 "block size", 0);
2437                         break;
2438                 case 's':
2439                         retval = strtoblk(argv[0], optarg,
2440                                           "superblock block number",
2441                                           &superblock);
2442                         if (retval)
2443                                 return 1;
2444                         break;
2445                 case 'c':
2446                         catastrophic = 1;
2447                         break;
2448                 case 'V':
2449                         /* Print version number and exit */
2450                         fprintf(stderr, "\tUsing %s\n",
2451                                 error_message(EXT2_ET_BASE));
2452                         exit(0);
2453                 case 'z':
2454                         undo_file = optarg;
2455                         break;
2456                 default:
2457                         com_err(argv[0], 0, usage, debug_prog_name);
2458                         return 1;
2459                 }
2460         }
2461         if (optind < argc)
2462                 open_filesystem(argv[optind], open_flags,
2463                                 superblock, blocksize, catastrophic,
2464                                 data_filename, undo_file);
2465
2466         sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
2467                                        &debug_cmds, &retval);
2468         if (retval) {
2469                 ss_perror(sci_idx, retval, "creating invocation");
2470                 exit(1);
2471         }
2472         ss_get_readline(sci_idx);
2473
2474         (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2475         if (retval) {
2476                 ss_perror(sci_idx, retval, "adding standard requests");
2477                 exit (1);
2478         }
2479         if (extra_cmds)
2480                 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2481         if (retval) {
2482                 ss_perror(sci_idx, retval, "adding extra requests");
2483                 exit (1);
2484         }
2485         if (request) {
2486                 retval = 0;
2487                 retval = ss_execute_line(sci_idx, request);
2488                 if (retval) {
2489                         ss_perror(sci_idx, retval, request);
2490                         exit_status++;
2491                 }
2492         } else if (cmd_file) {
2493                 exit_status = source_file(cmd_file, sci_idx);
2494         } else {
2495                 ss_listen(sci_idx);
2496         }
2497
2498         ss_delete_invocation(sci_idx);
2499
2500         if (current_fs)
2501                 close_filesystem();
2502
2503         remove_error_table(&et_ext2_error_table);
2504         return exit_status;
2505 }