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