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