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