Whamcloud - gitweb
debugfs.c (do_write): Mask off the file type bits, and OR in the
[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 <stdio.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15 #include <string.h>
16 #include <time.h>
17 #ifdef HAVE_GETOPT_H
18 #include <getopt.h>
19 #else 
20 extern int optind;
21 extern char *optarg;
22 #endif
23 #ifdef HAVE_ERRNO_H
24 #include <errno.h>
25 #endif
26 #include <fcntl.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include "et/com_err.h"
31 #include "ss/ss.h"
32 #include "debugfs.h"
33 #include "uuid/uuid.h"
34 #include "e2p/e2p.h"
35
36 #include "../version.h"
37
38 extern ss_request_table debug_cmds;
39
40 ext2_filsys     current_fs = NULL;
41 ext2_ino_t      root, cwd;
42
43 static void open_filesystem(char *device, int open_flags, blk_t superblock,
44                             blk_t blocksize, int catastrophic)
45 {
46         int     retval;
47
48         if (superblock != 0 && blocksize == 0) {
49                 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
50                 current_fs = NULL;
51                 return;
52         }
53
54         if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
55                 com_err(device, 0,
56                         "opening read-only because of catastrophic mode");
57                 open_flags &= ~EXT2_FLAG_RW;
58         }
59         
60         retval = ext2fs_open(device, open_flags, superblock, blocksize,
61                              unix_io_manager, &current_fs);
62         if (retval) {
63                 com_err(device, retval, "while opening filesystem");
64                 current_fs = NULL;
65                 return;
66         }
67
68         if (catastrophic)
69                 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
70         else {
71                 retval = ext2fs_read_inode_bitmap(current_fs);
72                 if (retval) {
73                         com_err(device, retval, "while reading inode bitmap");
74                         goto errout;
75                 }
76                 retval = ext2fs_read_block_bitmap(current_fs);
77                 if (retval) {
78                         com_err(device, retval, "while reading block bitmap");
79                         goto errout;
80                 }
81         }
82         root = cwd = EXT2_ROOT_INO;
83         return;
84
85 errout:
86         retval = ext2fs_close(current_fs);
87         if (retval)
88                 com_err(device, retval, "while trying to close filesystem");
89         current_fs = NULL;
90 }
91
92 void do_open_filesys(int argc, char **argv)
93 {
94         const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
95         int     c, err;
96         int     catastrophic = 0;
97         blk_t   superblock = 0;
98         blk_t   blocksize = 0;
99         int open_flags = 0;
100         
101         reset_getopt();
102         while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) {
103                 switch (c) {
104                 case 'i':
105                         open_flags |= EXT2_FLAG_IMAGE_FILE;
106                         break;
107                 case 'w':
108                         open_flags |= EXT2_FLAG_RW;
109                         break;
110                 case 'f':
111                         open_flags |= EXT2_FLAG_FORCE;
112                         break;
113                 case 'c':
114                         catastrophic = 1;
115                         break;
116                 case 'b':
117                         blocksize = parse_ulong(optarg, argv[0],
118                                                 "block size", &err);
119                         if (err)
120                                 return;
121                         break;
122                 case 's':
123                         superblock = parse_ulong(optarg, argv[0],
124                                                  "superblock number", &err);
125                         if (err)
126                                 return;
127                         break;
128                 default:
129                         com_err(argv[0], 0, usage);
130                         return;
131                 }
132         }
133         if (optind != argc-1) {
134                 com_err(argv[0], 0, usage);
135                 return;
136         }
137         if (check_fs_not_open(argv[0]))
138                 return;
139         open_filesystem(argv[optind], open_flags,
140                         superblock, blocksize, catastrophic);
141 }
142
143 void do_lcd(int argc, char **argv)
144 {
145         if (common_args_process(argc, argv, 2, 2, "lcd",
146                                 "<native dir>", 0))
147                 return;
148
149         if (chdir(argv[1]) == -1) {
150                 com_err(argv[0], errno,
151                         "while trying to change native directory to %s",
152                         argv[1]);
153                 return;
154         }
155 }
156
157 static void close_filesystem(NOARGS)
158 {
159         int     retval;
160         
161         if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
162                 retval = ext2fs_write_inode_bitmap(current_fs);
163                 if (retval)
164                         com_err("ext2fs_write_inode_bitmap", retval, "");
165         }
166         if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
167                 retval = ext2fs_write_block_bitmap(current_fs);
168                 if (retval)
169                         com_err("ext2fs_write_block_bitmap", retval, "");
170         }
171         retval = ext2fs_close(current_fs);
172         if (retval)
173                 com_err("ext2fs_close", retval, "");
174         current_fs = NULL;
175         return;
176 }
177
178 void do_close_filesys(int argc, char **argv)
179 {
180         if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
181                 return;
182         close_filesystem();
183 }
184
185 void do_init_filesys(int argc, char **argv)
186 {
187         struct ext2_super_block param;
188         errcode_t       retval;
189         int             err;
190         
191         if (common_args_process(argc, argv, 3, 3, "initialize",
192                                 "<device> <blocksize>", CHECK_FS_NOTOPEN))
193                 return;
194
195         memset(&param, 0, sizeof(struct ext2_super_block));
196         param.s_blocks_count = parse_ulong(argv[2], argv[0],
197                                            "blocks count", &err);
198         if (err)
199                 return;
200         retval = ext2fs_initialize(argv[1], 0, &param,
201                                    unix_io_manager, &current_fs);
202         if (retval) {
203                 com_err(argv[1], retval, "while initializing filesystem");
204                 current_fs = NULL;
205                 return;
206         }
207         root = cwd = EXT2_ROOT_INO;
208         return;
209 }
210
211 static void print_features(struct ext2_super_block * s, FILE *f)
212 {
213         int     i, j, printed=0;
214         __u32   *mask = &s->s_feature_compat, m;
215
216         fputs("Filesystem features:", f);
217         for (i=0; i <3; i++,mask++) {
218                 for (j=0,m=1; j < 32; j++, m<<=1) {
219                         if (*mask & m) {
220                                 fprintf(f, " %s", e2p_feature2string(i, m));
221                                 printed++;
222                         }
223                 }
224         }
225         if (printed == 0)
226                 fputs("(none)", f);
227         fputs("\n", f);
228 }
229
230 void do_show_super_stats(int argc, char *argv[])
231 {
232         dgrp_t  i;
233         FILE    *out;
234         struct ext2_group_desc *gdp;
235         int     c, header_only = 0;
236         int     numdirs = 0;
237         const char *usage = "Usage: show_super [-h]";
238
239         reset_getopt();
240         while ((c = getopt (argc, argv, "h")) != EOF) {
241                 switch (c) {
242                 case 'h':
243                         header_only++;
244                         break;
245                 default:
246                         com_err(argv[0], 0, usage);
247                         return;
248                 }
249         }
250         if (optind != argc) {
251                 com_err(argv[0], 0, usage);
252                 return;
253         }
254         if (check_fs_open(argv[0]))
255                 return;
256         out = open_pager();
257
258         list_super2(current_fs->super, out);
259         for (i=0; i < current_fs->group_desc_count; i++)
260                 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
261         fprintf(out, "Directories:              %d\n", numdirs);
262         
263         if (header_only) {
264                 close_pager(out);
265                 return;
266         }
267         
268         gdp = &current_fs->group_desc[0];
269         for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
270                 fprintf(out, " Group %2d: block bitmap at %d, "
271                         "inode bitmap at %d, "
272                         "inode table at %d\n"
273                         "           %d free %s, "
274                         "%d free %s, "
275                         "%d used %s\n",
276                         i, gdp->bg_block_bitmap,
277                         gdp->bg_inode_bitmap, gdp->bg_inode_table,
278                         gdp->bg_free_blocks_count,
279                         gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
280                         gdp->bg_free_inodes_count,
281                         gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
282                         gdp->bg_used_dirs_count,
283                         gdp->bg_used_dirs_count != 1 ? "directories"
284                                 : "directory");
285         close_pager(out);
286 }
287
288 void do_dirty_filesys(int argc EXT2FS_ATTR((unused)), 
289                       char **argv EXT2FS_ATTR((unused)))
290 {
291         if (check_fs_open(argv[0]))
292                 return;
293         if (check_fs_read_write(argv[0]))
294                 return;
295
296         if (argv[1] && !strcmp(argv[1], "-clean"))
297                 current_fs->super->s_state |= EXT2_VALID_FS;
298         else
299                 current_fs->super->s_state &= ~EXT2_VALID_FS;
300         ext2fs_mark_super_dirty(current_fs);
301 }
302
303 struct list_blocks_struct {
304         FILE            *f;
305         e2_blkcnt_t     total;
306         blk_t           first_block, last_block;
307         e2_blkcnt_t     first_bcnt, last_bcnt;
308         e2_blkcnt_t     first;
309 };
310
311 static void finish_range(struct list_blocks_struct *lb)
312 {
313         if (lb->first_block == 0)
314                 return;
315         if (lb->first)
316                 lb->first = 0;
317         else
318                 fprintf(lb->f, ", ");
319         if (lb->first_block == lb->last_block)
320                 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
321         else
322                 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
323                         lb->last_bcnt, lb->first_block, lb->last_block);
324         lb->first_block = 0;
325 }
326
327 static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)), 
328                             blk_t *blocknr, e2_blkcnt_t blockcnt, 
329                             blk_t ref_block EXT2FS_ATTR((unused)),
330                             int ref_offset EXT2FS_ATTR((unused)), 
331                             void *private)
332 {
333         struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
334
335         lb->total++;
336         if (blockcnt >= 0) {
337                 /*
338                  * See if we can add on to the existing range (if it exists)
339                  */
340                 if (lb->first_block &&
341                     (lb->last_block+1 == *blocknr) &&
342                     (lb->last_bcnt+1 == blockcnt)) {
343                         lb->last_block = *blocknr;
344                         lb->last_bcnt = blockcnt;
345                         return 0;
346                 }
347                 /*
348                  * Start a new range.
349                  */
350                 finish_range(lb);
351                 lb->first_block = lb->last_block = *blocknr;
352                 lb->first_bcnt = lb->last_bcnt = blockcnt;
353                 return 0;
354         }
355         /*
356          * Not a normal block.  Always force a new range.
357          */
358         finish_range(lb);
359         if (lb->first)
360                 lb->first = 0;
361         else
362                 fprintf(lb->f, ", ");
363         if (blockcnt == -1)
364                 fprintf(lb->f, "(IND):%d", *blocknr);
365         else if (blockcnt == -2)
366                 fprintf(lb->f, "(DIND):%d", *blocknr);
367         else if (blockcnt == -3)
368                 fprintf(lb->f, "(TIND):%d", *blocknr);
369         return 0;
370 }
371
372
373 static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
374 {
375         struct list_blocks_struct lb;
376
377         fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
378         lb.total = 0;
379         lb.first_block = 0;
380         lb.f = f;
381         lb.first = 1;
382         ext2fs_block_iterate2(current_fs, inode, 0, NULL,
383                              list_blocks_proc, (void *)&lb);
384         finish_range(&lb);
385         if (lb.total)
386                 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
387         fprintf(f,"\n");
388 }
389
390
391 void internal_dump_inode(FILE *out, const char *prefix,
392                          ext2_ino_t inode_num, struct ext2_inode *inode,
393                          int do_dump_blocks)
394 {
395         const char *i_type;
396         char frag, fsize;
397         int os = current_fs->super->s_creator_os;
398         
399         if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
400         else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
401         else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
402         else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
403         else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
404         else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
405         else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
406         else i_type = "bad type";
407         fprintf(out, "%sInode: %u   Type: %s    ", prefix, inode_num, i_type);
408         fprintf(out, "%sMode:  %04o   Flags: 0x%x   Generation: %u\n",
409                 prefix, 
410                 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
411         fprintf(out, "%sUser: %5d   Group: %5d   Size: ",
412                 prefix, inode->i_uid, inode->i_gid);
413         if (LINUX_S_ISREG(inode->i_mode)) {
414                 __u64 i_size = (inode->i_size |
415                                 ((unsigned long long)inode->i_size_high << 32));
416
417                 fprintf(out, "%lld\n", i_size);
418         } else
419                 fprintf(out, "%d\n", inode->i_size);
420         if (current_fs->super->s_creator_os == EXT2_OS_HURD)
421                 fprintf(out,
422                         "%sFile ACL: %d    Directory ACL: %d Translator: %d\n",
423                         prefix,
424                         inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
425                         inode->osd1.hurd1.h_i_translator);
426         else
427                 fprintf(out, "%sFile ACL: %d    Directory ACL: %d\n",
428                         prefix,
429                         inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
430         fprintf(out, "%sLinks: %d   Blockcount: %d\n", 
431                 prefix, inode->i_links_count, inode->i_blocks);
432         switch (os) {
433             case EXT2_OS_LINUX:
434                 frag = inode->osd2.linux2.l_i_frag;
435                 fsize = inode->osd2.linux2.l_i_fsize;
436                 break;
437             case EXT2_OS_HURD:
438                 frag = inode->osd2.hurd2.h_i_frag;
439                 fsize = inode->osd2.hurd2.h_i_fsize;
440                 break;
441             case EXT2_OS_MASIX:
442                 frag = inode->osd2.masix2.m_i_frag;
443                 fsize = inode->osd2.masix2.m_i_fsize;
444                 break;
445             default:
446                 frag = fsize = 0;
447         }
448         fprintf(out, "%sFragment:  Address: %d    Number: %d    Size: %d\n",
449                 prefix, inode->i_faddr, frag, fsize);
450         fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
451                 time_to_string(inode->i_ctime));
452         fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
453                 time_to_string(inode->i_atime));
454         fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
455                 time_to_string(inode->i_mtime));
456         if (inode->i_dtime) 
457           fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
458                   time_to_string(inode->i_dtime));
459         if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
460                 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
461                         (int) inode->i_size, (char *)inode->i_block);
462         else if (do_dump_blocks)
463                 dump_blocks(out, prefix, inode_num);
464 }
465
466 static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
467 {
468         FILE    *out;
469         
470         out = open_pager();
471         internal_dump_inode(out, "", inode_num, &inode, 1);
472         close_pager(out);
473 }
474
475 void do_stat(int argc, char *argv[])
476 {
477         ext2_ino_t      inode;
478         struct ext2_inode inode_buf;
479
480         if (common_inode_args_process(argc, argv, &inode, 0))
481                 return;
482
483         if (debugfs_read_inode(inode, &inode_buf, argv[0]))
484                 return;
485
486         dump_inode(inode,inode_buf);
487         return;
488 }
489
490 void do_chroot(int argc, char *argv[])
491 {
492         ext2_ino_t inode;
493         int retval;
494
495         if (common_inode_args_process(argc, argv, &inode, 0))
496                 return;
497
498         retval = ext2fs_check_directory(current_fs, inode);
499         if (retval)  {
500                 com_err(argv[1], retval, "");
501                 return;
502         }
503         root = inode;
504 }
505
506 void do_clri(int argc, char *argv[])
507 {
508         ext2_ino_t inode;
509         struct ext2_inode inode_buf;
510
511         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
512                 return;
513
514         if (debugfs_read_inode(inode, &inode_buf, argv[0]))
515                 return;
516         memset(&inode_buf, 0, sizeof(inode_buf));
517         if (debugfs_write_inode(inode, &inode_buf, argv[0]))
518                 return;
519 }
520
521 void do_freei(int argc, char *argv[])
522 {
523         ext2_ino_t inode;
524
525         if (common_inode_args_process(argc, argv, &inode,
526                                       CHECK_FS_RW | CHECK_FS_BITMAPS))
527                 return;
528
529         if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
530                 com_err(argv[0], 0, "Warning: inode already clear");
531         ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
532         ext2fs_mark_ib_dirty(current_fs);
533 }
534
535 void do_seti(int argc, char *argv[])
536 {
537         ext2_ino_t inode;
538
539         if (common_inode_args_process(argc, argv, &inode,
540                                       CHECK_FS_RW | CHECK_FS_BITMAPS))
541                 return;
542
543         if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
544                 com_err(argv[0], 0, "Warning: inode already set");
545         ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
546         ext2fs_mark_ib_dirty(current_fs);
547 }
548
549 void do_testi(int argc, char *argv[])
550 {
551         ext2_ino_t inode;
552
553         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
554                 return;
555
556         if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
557                 printf("Inode %u is marked in use\n", inode);
558         else
559                 printf("Inode %u is not in use\n", inode);
560 }
561
562 void do_freeb(int argc, char *argv[])
563 {
564         blk_t block;
565         int count = 1;
566
567         if (common_block_args_process(argc, argv, &block, &count))
568                 return;
569         if (check_fs_read_write(argv[0]))
570                 return;
571         while (count-- > 0) {
572                 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
573                         com_err(argv[0], 0, "Warning: block %d already clear",
574                                 block);
575                 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
576                 block++;
577         }
578         ext2fs_mark_bb_dirty(current_fs);
579 }
580
581 void do_setb(int argc, char *argv[])
582 {
583         blk_t block;
584         int count = 1;
585
586         if (common_block_args_process(argc, argv, &block, &count))
587                 return;
588         if (check_fs_read_write(argv[0]))
589                 return;
590         while (count-- > 0) {
591                 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
592                         com_err(argv[0], 0, "Warning: block %d already set",
593                                 block);
594                 ext2fs_mark_block_bitmap(current_fs->block_map,block);
595                 block++;
596         }
597         ext2fs_mark_bb_dirty(current_fs);
598 }
599
600 void do_testb(int argc, char *argv[])
601 {
602         blk_t block;
603         int count = 1;
604
605         if (common_block_args_process(argc, argv, &block, &count))
606                 return;
607         while (count-- > 0) {
608                 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
609                         printf("Block %d marked in use\n", block);
610                 else
611                         printf("Block %d not in use\n", block);
612                 block++;
613         }
614 }
615
616 static void modify_u8(char *com, const char *prompt,
617                       const char *format, __u8 *val)
618 {
619         char buf[200];
620         unsigned long v;
621         char *tmp;
622
623         sprintf(buf, format, *val);
624         printf("%30s    [%s] ", prompt, buf);
625         fgets(buf, sizeof(buf), stdin);
626         if (buf[strlen (buf) - 1] == '\n')
627                 buf[strlen (buf) - 1] = '\0';
628         if (!buf[0])
629                 return;
630         v = strtoul(buf, &tmp, 0);
631         if (*tmp)
632                 com_err(com, 0, "Bad value - %s", buf);
633         else
634                 *val = v;
635 }
636
637 static void modify_u16(char *com, const char *prompt,
638                        const char *format, __u16 *val)
639 {
640         char buf[200];
641         unsigned long v;
642         char *tmp;
643
644         sprintf(buf, format, *val);
645         printf("%30s    [%s] ", prompt, buf);
646         fgets(buf, sizeof(buf), stdin);
647         if (buf[strlen (buf) - 1] == '\n')
648                 buf[strlen (buf) - 1] = '\0';
649         if (!buf[0])
650                 return;
651         v = strtoul(buf, &tmp, 0);
652         if (*tmp)
653                 com_err(com, 0, "Bad value - %s", buf);
654         else
655                 *val = v;
656 }
657
658 static void modify_u32(char *com, const char *prompt,
659                        const char *format, __u32 *val)
660 {
661         char buf[200];
662         unsigned long v;
663         char *tmp;
664
665         sprintf(buf, format, *val);
666         printf("%30s    [%s] ", prompt, buf);
667         fgets(buf, sizeof(buf), stdin);
668         if (buf[strlen (buf) - 1] == '\n')
669                 buf[strlen (buf) - 1] = '\0';
670         if (!buf[0])
671                 return;
672         v = strtoul(buf, &tmp, 0);
673         if (*tmp)
674                 com_err(com, 0, "Bad value - %s", buf);
675         else
676                 *val = v;
677 }
678
679
680 void do_modify_inode(int argc, char *argv[])
681 {
682         struct ext2_inode inode;
683         ext2_ino_t      inode_num;
684         int             i;
685         unsigned char   *frag, *fsize;
686         char            buf[80];
687         int             os;
688         const char      *hex_format = "0x%x";
689         const char      *octal_format = "0%o";
690         const char      *decimal_format = "%d";
691         
692         if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
693                 return;
694
695         os = current_fs->super->s_creator_os;
696
697         if (debugfs_read_inode(inode_num, &inode, argv[1]))
698                 return;
699         
700         modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
701         modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
702         modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
703         modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
704         modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
705         modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
706         modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
707         modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
708         modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
709         modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
710         modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
711         modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
712 #if 0
713         modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
714 #endif
715         modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
716         if (LINUX_S_ISDIR(inode.i_mode))
717                 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
718         else
719                 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
720
721         if (current_fs->super->s_creator_os == EXT2_OS_HURD)
722                 modify_u32(argv[0], "Translator Block",
723                             decimal_format, &inode.osd1.hurd1.h_i_translator);
724         
725         modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
726         switch (os) {
727             case EXT2_OS_LINUX:
728                 frag = &inode.osd2.linux2.l_i_frag;
729                 fsize = &inode.osd2.linux2.l_i_fsize;
730                 break;
731             case EXT2_OS_HURD:
732                 frag = &inode.osd2.hurd2.h_i_frag;
733                 fsize = &inode.osd2.hurd2.h_i_fsize;
734                 break;
735             case EXT2_OS_MASIX:
736                 frag = &inode.osd2.masix2.m_i_frag;
737                 fsize = &inode.osd2.masix2.m_i_fsize;
738                 break;
739             default:
740                 frag = fsize = 0;
741         }
742         if (frag)
743                 modify_u8(argv[0], "Fragment number", decimal_format, frag);
744         if (fsize)
745                 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
746
747         for (i=0;  i < EXT2_NDIR_BLOCKS; i++) {
748                 sprintf(buf, "Direct Block #%d", i);
749                 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
750         }
751         modify_u32(argv[0], "Indirect Block", decimal_format,
752                     &inode.i_block[EXT2_IND_BLOCK]);    
753         modify_u32(argv[0], "Double Indirect Block", decimal_format,
754                     &inode.i_block[EXT2_DIND_BLOCK]);
755         modify_u32(argv[0], "Triple Indirect Block", decimal_format,
756                     &inode.i_block[EXT2_TIND_BLOCK]);
757         if (debugfs_write_inode(inode_num, &inode, argv[1]))
758                 return;
759 }
760
761 void do_change_working_dir(int argc, char *argv[])
762 {
763         ext2_ino_t      inode;
764         int             retval;
765         
766         if (common_inode_args_process(argc, argv, &inode, 0))
767                 return;
768
769         retval = ext2fs_check_directory(current_fs, inode);
770         if (retval) {
771                 com_err(argv[1], retval, "");
772                 return;
773         }
774         cwd = inode;
775         return;
776 }
777
778 void do_print_working_directory(int argc, char *argv[])
779 {
780         int     retval;
781         char    *pathname = NULL;
782         
783         if (common_args_process(argc, argv, 1, 1,
784                                 "print_working_directory", "", 0))
785                 return;
786
787         retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
788         if (retval) {
789                 com_err(argv[0], retval,
790                         "while trying to get pathname of cwd");
791         }
792         printf("[pwd]   INODE: %6u  PATH: %s\n", cwd, pathname);
793         free(pathname);
794         retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
795         if (retval) {
796                 com_err(argv[0], retval,
797                         "while trying to get pathname of root");
798         }
799         printf("[root]  INODE: %6u  PATH: %s\n", root, pathname);
800         free(pathname);
801         return;
802 }
803
804 static void make_link(char *sourcename, char *destname)
805 {
806         ext2_ino_t      inode;
807         int             retval;
808         ext2_ino_t      dir;
809         char            *dest, *cp, *basename;
810
811         /*
812          * Get the source inode
813          */
814         inode = string_to_inode(sourcename);
815         if (!inode)
816                 return;
817         basename = strrchr(sourcename, '/');
818         if (basename)
819                 basename++;
820         else
821                 basename = sourcename;
822         /*
823          * Figure out the destination.  First see if it exists and is
824          * a directory.  
825          */
826         if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
827                 dest = basename;
828         else {
829                 /*
830                  * OK, it doesn't exist.  See if it is
831                  * '<dir>/basename' or 'basename'
832                  */
833                 cp = strrchr(destname, '/');
834                 if (cp) {
835                         *cp = 0;
836                         dir = string_to_inode(destname);
837                         if (!dir)
838                                 return;
839                         dest = cp+1;
840                 } else {
841                         dir = cwd;
842                         dest = destname;
843                 }
844         }
845         
846         retval = ext2fs_link(current_fs, dir, dest, inode, 0);
847         if (retval)
848                 com_err("make_link", retval, "");
849         return;
850 }
851
852
853 void do_link(int argc, char *argv[])
854 {
855         if (common_args_process(argc, argv, 3, 3, "link",
856                                 "<source file> <dest_name>", CHECK_FS_RW))
857                 return;
858
859         make_link(argv[1], argv[2]);
860 }
861
862 static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
863                             int blockcnt EXT2FS_ATTR((unused)), 
864                             void *private EXT2FS_ATTR((unused)))
865 {
866         blk_t   block;
867
868         block = *blocknr;
869         ext2fs_block_alloc_stats(fs, block, +1);
870         return 0;
871 }
872
873 void do_undel(int argc, char *argv[])
874 {
875         ext2_ino_t      ino;
876         struct ext2_inode inode;
877
878         if (common_args_process(argc, argv, 3, 3, "undelete",
879                                 "<inode_num> <dest_name>",
880                                 CHECK_FS_RW | CHECK_FS_BITMAPS))
881                 return;
882
883         ino = string_to_inode(argv[1]);
884         if (!ino)
885                 return;
886
887         if (debugfs_read_inode(ino, &inode, argv[1]))
888                 return;
889
890         if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
891                 com_err(argv[1], 0, "Inode is not marked as deleted");
892                 return;
893         }
894
895         /*
896          * XXX this function doesn't handle changing the links count on the
897          * parent directory when undeleting a directory.  
898          */
899         inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
900         inode.i_dtime = 0;
901
902         if (debugfs_write_inode(ino, &inode, argv[0]))
903                 return;
904
905         ext2fs_block_iterate(current_fs, ino, 0, NULL,
906                              mark_blocks_proc, NULL);
907
908         ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
909
910         make_link(argv[1], argv[2]);
911 }
912
913 static void unlink_file_by_name(char *filename)
914 {
915         int             retval;
916         ext2_ino_t      dir;
917         char            *basename;
918         
919         basename = strrchr(filename, '/');
920         if (basename) {
921                 *basename++ = '\0';
922                 dir = string_to_inode(filename);
923                 if (!dir)
924                         return;
925         } else {
926                 dir = cwd;
927                 basename = filename;
928         }
929         retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
930         if (retval)
931                 com_err("unlink_file_by_name", retval, "");
932         return;
933 }
934
935 void do_unlink(int argc, char *argv[])
936 {
937         if (common_args_process(argc, argv, 2, 2, "link",
938                                 "<pathname>", CHECK_FS_RW))
939                 return;
940
941         unlink_file_by_name(argv[1]);
942 }
943
944 void do_find_free_block(int argc, char *argv[])
945 {
946         blk_t   free_blk, goal;
947         int             count;
948         errcode_t       retval;
949         char            *tmp;
950         
951         if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
952                 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
953                 return;
954         }
955         if (check_fs_open(argv[0]))
956                 return;
957
958         if (argc > 1) {
959                 count = strtol(argv[1],&tmp,0);
960                 if (*tmp) {
961                         com_err(argv[0], 0, "Bad count - %s", argv[1]);
962                         return;
963                 }
964         } else
965                 count = 1;
966
967         if (argc > 2) {
968                 goal = strtol(argv[2], &tmp, 0);
969                 if (*tmp) {
970                         com_err(argv[0], 0, "Bad goal - %s", argv[1]);
971                         return;
972                 }
973         }
974         else
975                 goal = current_fs->super->s_first_data_block;
976
977         printf("Free blocks found: ");
978         free_blk = goal - 1;    
979         while (count-- > 0) {
980                 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
981                                           &free_blk);
982                 if (retval) {
983                         com_err("ext2fs_new_block", retval, "");
984                         return;
985                 } else
986                         printf("%d ", free_blk);
987         }
988         printf("\n");
989 }
990
991 void do_find_free_inode(int argc, char *argv[])
992 {
993         ext2_ino_t      free_inode, dir;
994         int             mode;
995         int             retval;
996         char            *tmp;
997         
998         if (argc > 3 || (argc>1 && *argv[1] == '?')) {
999                 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1000                 return;
1001         }
1002         if (check_fs_open(argv[0]))
1003                 return;
1004
1005         if (argc > 1) {
1006                 dir = strtol(argv[1], &tmp, 0);
1007                 if (*tmp) {
1008                         com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1009                         return;
1010                 }
1011         }
1012         else
1013                 dir = root;
1014         if (argc > 2) {
1015                 mode = strtol(argv[2], &tmp, 0);
1016                 if (*tmp) {
1017                         com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1018                         return;
1019                 }
1020         } else
1021                 mode = 010755;
1022
1023         retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1024         if (retval)
1025                 com_err("ext2fs_new_inode", retval, "");
1026         else
1027                 printf("Free inode found: %u\n", free_inode);
1028 }
1029
1030 static errcode_t copy_file(int fd, ext2_ino_t newfile)
1031 {
1032         ext2_file_t     e2_file;
1033         errcode_t       retval;
1034         int             got;
1035         unsigned int    written;
1036         char            buf[8192];
1037         char            *ptr;
1038
1039         retval = ext2fs_file_open(current_fs, newfile,
1040                                   EXT2_FILE_WRITE, &e2_file);
1041         if (retval)
1042                 return retval;
1043
1044         while (1) {
1045                 got = read(fd, buf, sizeof(buf));
1046                 if (got == 0)
1047                         break;
1048                 if (got < 0) {
1049                         retval = errno;
1050                         goto fail;
1051                 }
1052                 ptr = buf;
1053                 while (got > 0) {
1054                         retval = ext2fs_file_write(e2_file, ptr,
1055                                                    got, &written);
1056                         if (retval)
1057                                 goto fail;
1058
1059                         got -= written;
1060                         ptr += written;
1061                 }
1062         }
1063         retval = ext2fs_file_close(e2_file);
1064         return retval;
1065
1066 fail:
1067         (void) ext2fs_file_close(e2_file);
1068         return retval;
1069 }
1070
1071
1072 void do_write(int argc, char *argv[])
1073 {
1074         int             fd;
1075         struct stat     statbuf;
1076         ext2_ino_t      newfile;
1077         errcode_t       retval;
1078         struct ext2_inode inode;
1079
1080         if (common_args_process(argc, argv, 3, 3, "write",
1081                                 "<native file> <new file>", CHECK_FS_RW))
1082                 return;
1083
1084         fd = open(argv[1], O_RDONLY);
1085         if (fd < 0) {
1086                 com_err(argv[1], errno, "");
1087                 return;
1088         }
1089         if (fstat(fd, &statbuf) < 0) {
1090                 com_err(argv[1], errno, "");
1091                 close(fd);
1092                 return;
1093         }
1094
1095         retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1096         if (retval == 0) {
1097                 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1098                 close(fd);
1099                 return;
1100         }
1101
1102         retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1103         if (retval) {
1104                 com_err(argv[0], retval, "");
1105                 close(fd);
1106                 return;
1107         }
1108         printf("Allocated inode: %u\n", newfile);
1109         retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1110                              EXT2_FT_REG_FILE);
1111         if (retval) {
1112                 com_err(argv[2], retval, "");
1113                 close(fd);
1114                 return;
1115         }
1116         if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1117                 com_err(argv[0], 0, "Warning: inode already set");
1118         ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
1119         memset(&inode, 0, sizeof(inode));
1120         inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
1121         inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1122         inode.i_links_count = 1;
1123         inode.i_size = statbuf.st_size;
1124         if (debugfs_write_inode(newfile, &inode, argv[0])) {
1125                 close(fd);
1126                 return;
1127         }
1128         if (LINUX_S_ISREG(inode.i_mode)) {
1129                 retval = copy_file(fd, newfile);
1130                 if (retval)
1131                         com_err("copy_file", retval, "");
1132         }
1133         close(fd);
1134 }
1135
1136 void do_mknod(int argc, char *argv[])
1137 {
1138         unsigned long   mode, major, minor;
1139         ext2_ino_t      newfile;
1140         errcode_t       retval;
1141         struct ext2_inode inode;
1142         int             filetype, nr;
1143
1144         if (check_fs_open(argv[0]))
1145                 return;
1146         if (argc < 3 || argv[2][1]) {
1147         usage:
1148                 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1149                 return;
1150         }
1151         mode = minor = major = 0;
1152         switch (argv[2][0]) {
1153                 case 'p':
1154                         mode = LINUX_S_IFIFO;
1155                         filetype = EXT2_FT_FIFO;
1156                         nr = 3;
1157                         break;
1158                 case 'c':
1159                         mode = LINUX_S_IFCHR;
1160                         filetype = EXT2_FT_CHRDEV;
1161                         nr = 5;
1162                         break;
1163                 case 'b':
1164                         mode = LINUX_S_IFBLK;
1165                         filetype = EXT2_FT_BLKDEV;
1166                         nr = 5;
1167                         break;
1168                 default:
1169                         filetype = 0;
1170                         nr = 0;
1171         }
1172         if (nr == 5) {
1173                 major = strtoul(argv[3], argv+3, 0);
1174                 minor = strtoul(argv[4], argv+4, 0);
1175                 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1176                         nr = 0;
1177         }
1178         if (argc != nr)
1179                 goto usage;
1180         if (check_fs_read_write(argv[0]))
1181                 return;
1182         retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1183         if (retval) {
1184                 com_err(argv[0], retval, "");
1185                 return;
1186         }
1187         printf("Allocated inode: %u\n", newfile);
1188         retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
1189         if (retval) {
1190                 if (retval == EXT2_ET_DIR_NO_SPACE) {
1191                         retval = ext2fs_expand_dir(current_fs, cwd);
1192                         if (!retval)
1193                                 retval = ext2fs_link(current_fs, cwd,
1194                                                      argv[1], newfile,
1195                                                      filetype);
1196                 }
1197                 if (retval) {
1198                         com_err(argv[1], retval, "");
1199                         return;
1200                 }
1201         }
1202         if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1203                 com_err(argv[0], 0, "Warning: inode already set");
1204         ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1205         ext2fs_mark_ib_dirty(current_fs);
1206         memset(&inode, 0, sizeof(inode));
1207         inode.i_mode = mode;
1208         inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1209         inode.i_block[0] = major*256+minor;
1210         inode.i_links_count = 1;
1211         if (debugfs_write_inode(newfile, &inode, argv[0]))
1212                 return;
1213 }
1214
1215 void do_mkdir(int argc, char *argv[])
1216 {
1217         char    *cp;
1218         ext2_ino_t      parent;
1219         char    *name;
1220         errcode_t retval;
1221
1222         if (common_args_process(argc, argv, 2, 2, "mkdir",
1223                                 "<filename>", CHECK_FS_RW))
1224                 return;
1225
1226         cp = strrchr(argv[1], '/');
1227         if (cp) {
1228                 *cp = 0;
1229                 parent = string_to_inode(argv[1]);
1230                 if (!parent) {
1231                         com_err(argv[1], ENOENT, "");
1232                         return;
1233                 }
1234                 name = cp+1;
1235         } else {
1236                 parent = cwd;
1237                 name = argv[1];
1238         }
1239
1240
1241         retval = ext2fs_mkdir(current_fs, parent, 0, name);
1242         if (retval) {
1243                 com_err("ext2fs_mkdir", retval, "");
1244                 return;
1245         }
1246
1247 }
1248
1249 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1250                                int blockcnt EXT2FS_ATTR((unused)), 
1251                                void *private EXT2FS_ATTR((unused)))
1252 {
1253         blk_t   block;
1254
1255         block = *blocknr;
1256         ext2fs_block_alloc_stats(fs, block, -1);
1257         return 0;
1258 }
1259
1260 static void kill_file_by_inode(ext2_ino_t inode)
1261 {
1262         struct ext2_inode inode_buf;
1263
1264         if (debugfs_read_inode(inode, &inode_buf, 0))
1265                 return;
1266         inode_buf.i_dtime = time(NULL);
1267         if (debugfs_write_inode(inode, &inode_buf, 0))
1268                 return;
1269
1270         ext2fs_block_iterate(current_fs, inode, 0, NULL,
1271                              release_blocks_proc, NULL);
1272         printf("\n");
1273         ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1274                                   LINUX_S_ISDIR(inode_buf.i_mode));
1275 }
1276
1277
1278 void do_kill_file(int argc, char *argv[])
1279 {
1280         ext2_ino_t inode_num;
1281
1282         if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
1283                 return;
1284
1285         kill_file_by_inode(inode_num);
1286 }
1287
1288 void do_rm(int argc, char *argv[])
1289 {
1290         int retval;
1291         ext2_ino_t inode_num;
1292         struct ext2_inode inode;
1293
1294         if (common_args_process(argc, argv, 2, 2, "rm",
1295                                 "<filename>", CHECK_FS_RW))
1296                 return;
1297
1298         retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1299         if (retval) {
1300                 com_err(argv[0], retval, "while trying to resolve filename");
1301                 return;
1302         }
1303
1304         if (debugfs_read_inode(inode_num, &inode, argv[0]))
1305                 return;
1306
1307         if (LINUX_S_ISDIR(inode.i_mode)) {
1308                 com_err(argv[0], 0, "file is a directory");
1309                 return;
1310         }
1311
1312         --inode.i_links_count;
1313         if (debugfs_write_inode(inode_num, &inode, argv[0]))
1314                 return;
1315
1316         unlink_file_by_name(argv[1]);
1317         if (inode.i_links_count == 0)
1318                 kill_file_by_inode(inode_num);
1319 }
1320
1321 struct rd_struct {
1322         ext2_ino_t      parent;
1323         int             empty;
1324 };
1325
1326 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1327                       int       entry EXT2FS_ATTR((unused)),
1328                       struct ext2_dir_entry *dirent,
1329                       int       offset EXT2FS_ATTR((unused)),
1330                       int       blocksize EXT2FS_ATTR((unused)),
1331                       char      *buf EXT2FS_ATTR((unused)),
1332                       void      *private)
1333 {
1334         struct rd_struct *rds = (struct rd_struct *) private;
1335
1336         if (dirent->inode == 0)
1337                 return 0;
1338         if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1339                 return 0;
1340         if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1341             (dirent->name[1] == '.')) {
1342                 rds->parent = dirent->inode;
1343                 return 0;
1344         }
1345         rds->empty = 0;
1346         return 0;
1347 }
1348         
1349 void do_rmdir(int argc, char *argv[])
1350 {
1351         int retval;
1352         ext2_ino_t inode_num;
1353         struct ext2_inode inode;
1354         struct rd_struct rds;
1355
1356         if (common_args_process(argc, argv, 2, 2, "rmdir",
1357                                 "<filename>", CHECK_FS_RW))
1358                 return;
1359
1360         retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1361         if (retval) {
1362                 com_err(argv[0], retval, "while trying to resolve filename");
1363                 return;
1364         }
1365
1366         if (debugfs_read_inode(inode_num, &inode, argv[0]))
1367                 return;
1368
1369         if (!LINUX_S_ISDIR(inode.i_mode)) {
1370                 com_err(argv[0], 0, "file is not a directory");
1371                 return;
1372         }
1373
1374         rds.parent = 0;
1375         rds.empty = 1;
1376
1377         retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1378                                     0, rmdir_proc, &rds);
1379         if (retval) {
1380                 com_err(argv[0], retval, "while iterating over directory");
1381                 return;
1382         }
1383         if (rds.empty == 0) {
1384                 com_err(argv[0], 0, "directory not empty");
1385                 return;
1386         }
1387
1388         inode.i_links_count = 0;
1389         if (debugfs_write_inode(inode_num, &inode, argv[0]))
1390                 return;
1391
1392         unlink_file_by_name(argv[1]);
1393         kill_file_by_inode(inode_num);
1394
1395         if (rds.parent) {
1396                 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1397                         return;
1398                 if (inode.i_links_count > 1)
1399                         inode.i_links_count--;
1400                 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1401                         return;
1402         }
1403 }
1404
1405 void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)), 
1406                             char *argv[] EXT2FS_ATTR((unused)))
1407 {
1408         FILE *out = stdout;
1409
1410         if (current_fs)
1411                 fprintf(out, "Open mode: read-%s\n",
1412                         current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1413         fprintf(out, "Filesystem in use: %s\n",
1414                 current_fs ? current_fs->device_name : "--none--");
1415 }
1416
1417 void do_expand_dir(int argc, char *argv[])
1418 {
1419         ext2_ino_t inode;
1420         int retval;
1421
1422         if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
1423                 return;
1424
1425         retval = ext2fs_expand_dir(current_fs, inode);
1426         if (retval)
1427                 com_err("ext2fs_expand_dir", retval, "");
1428         return;
1429 }
1430
1431 void do_features(int argc, char *argv[])
1432 {
1433         int     i;
1434         
1435         if (check_fs_open(argv[0]))
1436                 return;
1437
1438         if ((argc != 1) && check_fs_read_write(argv[0]))
1439                 return;
1440         for (i=1; i < argc; i++) {
1441                 if (e2p_edit_feature(argv[i],
1442                                      &current_fs->super->s_feature_compat, 0))
1443                         com_err(argv[0], 0, "Unknown feature: %s\n",
1444                                 argv[i]);
1445                 else
1446                         ext2fs_mark_super_dirty(current_fs);
1447         }
1448         print_features(current_fs->super, stdout);
1449 }
1450
1451 void do_bmap(int argc, char *argv[])
1452 {
1453         ext2_ino_t      ino;
1454         blk_t           blk, pblk;
1455         int             err;
1456         errcode_t       errcode;
1457         
1458         if (common_args_process(argc, argv, 3, 3, argv[0],
1459                                 "<file> logical_blk", 0))
1460                 return;
1461
1462         ino = string_to_inode(argv[1]);
1463         if (!ino)
1464                 return;
1465         blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1466
1467         errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1468         if (errcode) {
1469                 com_err("argv[0]", errcode,
1470                         "while mapping logical block %d\n", blk);
1471                 return;
1472         }
1473         printf("%d\n", pblk);
1474 }
1475
1476 void do_imap(int argc, char *argv[])
1477 {
1478         ext2_ino_t      ino;
1479         unsigned long   group, block, block_nr, offset;
1480
1481         if (common_args_process(argc, argv, 2, 2, argv[0],
1482                                 "<file>", 0))
1483                 return;
1484         ino = string_to_inode(argv[1]);
1485         if (!ino)
1486                 return;
1487
1488         group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1489         offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1490                 EXT2_INODE_SIZE(current_fs->super);
1491         block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1492         if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1493                 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
1494                         group);
1495                 return;
1496         }
1497         block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table + 
1498                 block;
1499         offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1500
1501         printf("Inode %d is part of block group %lu\n"
1502                "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
1503                block_nr, offset);
1504
1505 }
1506
1507
1508
1509 static int source_file(const char *cmd_file, int sci_idx)
1510 {
1511         FILE            *f;
1512         char            buf[256];
1513         char            *cp;
1514         int             exit_status = 0;
1515         int             retval;
1516
1517         if (strcmp(cmd_file, "-") == 0)
1518                 f = stdin;
1519         else {
1520                 f = fopen(cmd_file, "r");
1521                 if (!f) {
1522                         perror(cmd_file);
1523                         exit(1);
1524                 }
1525         }
1526         setbuf(stdout, NULL);
1527         setbuf(stderr, NULL);
1528         while (!feof(f)) {
1529                 if (fgets(buf, sizeof(buf), f) == NULL)
1530                         break;
1531                 cp = strchr(buf, '\n');
1532                 if (cp)
1533                         *cp = 0;
1534                 cp = strchr(buf, '\r');
1535                 if (cp)
1536                         *cp = 0;
1537                 printf("debugfs: %s\n", buf);
1538                 retval = ss_execute_line(sci_idx, buf);
1539                 if (retval) {
1540                         ss_perror(sci_idx, retval, buf);
1541                         exit_status++;
1542                 }
1543         }
1544         return exit_status;
1545 }
1546
1547 int main(int argc, char **argv)
1548 {
1549         int             retval;
1550         int             sci_idx;
1551         const char      *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1552         int             c;
1553         int             open_flags = 0;
1554         char            *request = 0;
1555         int             exit_status = 0;
1556         char            *cmd_file = 0;
1557         blk_t           superblock = 0;
1558         blk_t           blocksize = 0;
1559         int             catastrophic = 0;
1560         
1561         initialize_ext2_error_table();
1562         fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1563                  E2FSPROGS_DATE);
1564
1565         while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) {
1566                 switch (c) {
1567                 case 'R':
1568                         request = optarg;
1569                         break;
1570                 case 'f':
1571                         cmd_file = optarg;
1572                         break;
1573                 case 'i':
1574                         open_flags |= EXT2_FLAG_IMAGE_FILE;
1575                         break;
1576                 case 'w':
1577                         open_flags |= EXT2_FLAG_RW;
1578                         break;
1579                 case 'b':
1580                         blocksize = parse_ulong(optarg, argv[0], 
1581                                                 "block size", 0);
1582                         break;
1583                 case 's':
1584                         superblock = parse_ulong(optarg, argv[0], 
1585                                                  "superblock number", 0);
1586                         break;
1587                 case 'c':
1588                         catastrophic = 1;
1589                         break;
1590                 case 'V':
1591                         /* Print version number and exit */
1592                         fprintf(stderr, "\tUsing %s\n",
1593                                 error_message(EXT2_ET_BASE));
1594                         exit(0);
1595                 default:
1596                         com_err(argv[0], 0, usage);
1597                         return 1;
1598                 }
1599         }
1600         if (optind < argc)
1601                 open_filesystem(argv[optind], open_flags,
1602                                 superblock, blocksize, catastrophic);
1603         
1604         sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1605                                        &debug_cmds, &retval);
1606         if (retval) {
1607                 ss_perror(sci_idx, retval, "creating invocation");
1608                 exit(1);
1609         }
1610         ss_get_readline(sci_idx);
1611
1612         (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1613         if (retval) {
1614                 ss_perror(sci_idx, retval, "adding standard requests");
1615                 exit (1);
1616         }
1617         if (request) {
1618                 retval = 0;
1619                 retval = ss_execute_line(sci_idx, request);
1620                 if (retval) {
1621                         ss_perror(sci_idx, retval, request);
1622                         exit_status++;
1623                 }
1624         } else if (cmd_file) {
1625                 exit_status = source_file(cmd_file, sci_idx);
1626         } else {
1627                 ss_listen(sci_idx);
1628         }
1629
1630         if (current_fs)
1631                 close_filesystem();
1632         
1633         return exit_status;
1634 }