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