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