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