Whamcloud - gitweb
da16e3da6bdd748fc8780f07a8f970dc6e6aeb07
[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         if (blockcnt == -1)
384                 fprintf(lb->f, "(IND):%d ", *blocknr);
385         else if (blockcnt == -2)
386                 fprintf(lb->f, "(DIND):%d ", *blocknr);
387         else if (blockcnt == -3)
388                 fprintf(lb->f, "(TIND):%d ", *blocknr);
389         else
390                 fprintf(lb->f, "(%d):%d ", blockcnt, *blocknr);
391         if (*blocknr)
392                 lb->total++;
393         return 0;
394 }
395
396
397 static void dump_blocks(FILE *f, ino_t inode)
398 {
399         struct list_blocks_struct lb;
400
401         fprintf(f, "BLOCKS:\n");
402         lb.total = 0;
403         lb.f = f;
404         ext2fs_block_iterate(current_fs, inode, 0, NULL,
405                              list_blocks_proc, (void *)&lb);
406         if (lb.total)
407                 fprintf(f, "\nTOTAL: %d\n", lb.total);
408         fprintf(f,"\n");
409 }
410
411
412 static void dump_inode(ino_t inode_num, struct ext2_inode inode)
413 {
414         const char *i_type;
415         FILE    *out;
416         char frag, fsize;
417         int os = current_fs->super->s_creator_os;
418         
419         out = open_pager();
420         if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
421         else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
422         else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
423         else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
424         else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
425         else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
426         else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
427         else i_type = "bad type";
428         fprintf(out, "Inode: %ld   Type: %s    ", inode_num, i_type);
429         fprintf(out, "Mode:  %04o   Flags: 0x%x   Generation: %u\n",
430                 inode.i_mode & 0777, inode.i_flags, inode.i_generation);
431         fprintf(out, "User: %5d   Group: %5d   Size: ",
432                 inode.i_uid, inode.i_gid);
433         if (LINUX_S_ISDIR(inode.i_mode))
434                 fprintf(out, "%d\n", inode.i_size);
435         else {
436                 __u64 i_size = (inode.i_size |
437                                 ((unsigned long long)inode.i_size_high << 32));
438                 
439                 fprintf(out, "%lld\n", i_size);
440         }
441         if (current_fs->super->s_creator_os == EXT2_OS_HURD)
442                 fprintf(out,
443                         "File ACL: %d    Directory ACL: %d Translator: %d\n",
444                         inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
445                         inode.osd1.hurd1.h_i_translator);
446         else
447                 fprintf(out, "File ACL: %d    Directory ACL: %d\n",
448                         inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
449         fprintf(out, "Links: %d   Blockcount: %d\n", inode.i_links_count,
450                 inode.i_blocks);
451         switch (os) {
452             case EXT2_OS_LINUX:
453                 frag = inode.osd2.linux2.l_i_frag;
454                 fsize = inode.osd2.linux2.l_i_fsize;
455                 break;
456             case EXT2_OS_HURD:
457                 frag = inode.osd2.hurd2.h_i_frag;
458                 fsize = inode.osd2.hurd2.h_i_fsize;
459                 break;
460             case EXT2_OS_MASIX:
461                 frag = inode.osd2.masix2.m_i_frag;
462                 fsize = inode.osd2.masix2.m_i_fsize;
463                 break;
464             default:
465                 frag = fsize = 0;
466         }
467         fprintf(out, "Fragment:  Address: %d    Number: %d    Size: %d\n",
468                 inode.i_faddr, frag, fsize);
469         fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
470                 time_to_string(inode.i_ctime));
471         fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
472                 time_to_string(inode.i_atime));
473         fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
474                 time_to_string(inode.i_mtime));
475         if (inode.i_dtime) 
476           fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
477                   time_to_string(inode.i_dtime));
478         if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
479                 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
480         else
481                 dump_blocks(out, inode_num);
482         close_pager(out);
483 }
484
485
486 void do_stat(int argc, char *argv[])
487 {
488         ino_t   inode;
489         struct ext2_inode inode_buf;
490         int retval;
491
492         if (argc != 2) {
493                 com_err(argv[0], 0, "Usage: stat <file>");
494                 return;
495         }
496         if (check_fs_open(argv[0]))
497                 return;
498         inode = string_to_inode(argv[1]);
499         if (!inode) 
500                 return;
501
502         retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
503         if (retval) 
504           {
505             com_err(argv[0], retval, "while trying to read inode %d", inode);
506             return;
507           }
508
509         dump_inode(inode,inode_buf);
510         return;
511 }
512
513 void do_chroot(int argc, char *argv[])
514 {
515         ino_t inode;
516         int retval;
517
518         if (argc != 2) {
519                 com_err(argv[0], 0, "Usage: chroot <file>");
520                 return;
521         }
522         if (check_fs_open(argv[0]))
523                 return;
524         inode = string_to_inode(argv[1]);
525         if (!inode) 
526                 return;
527
528         retval = ext2fs_check_directory(current_fs, inode);
529         if (retval)  {
530                 com_err(argv[1], retval, "");
531                 return;
532         }
533         root = inode;
534 }
535
536 void do_clri(int argc, char *argv[])
537 {
538         ino_t inode;
539         int retval;
540         struct ext2_inode inode_buf;
541
542         if (argc != 2) {
543                 com_err(argv[0], 0, "Usage: clri <file>");
544                 return;
545         }
546         if (check_fs_open(argv[0]))
547                 return;
548         if (check_fs_read_write(argv[0]))
549                 return;
550         inode = string_to_inode(argv[1]);
551         if (!inode) 
552                 return;
553
554         retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
555         if (retval) {
556                 com_err(argv[0], retval, "while trying to read inode %d", 
557                         inode);
558                 return;
559         }
560         memset(&inode_buf, 0, sizeof(inode_buf));
561         retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
562         if (retval) {
563                 com_err(argv[0], retval, "while trying to write inode %d",
564                         inode);
565                 return;
566         }
567 }
568
569 void do_freei(int argc, char *argv[])
570 {
571         ino_t inode;
572
573         if (argc != 2) {
574                 com_err(argv[0], 0, "Usage: freei <file>");
575                 return;
576         }
577         if (check_fs_open(argv[0]))
578                 return;
579         if (check_fs_read_write(argv[0]))
580                 return;
581         inode = string_to_inode(argv[1]);
582         if (!inode) 
583                 return;
584
585         if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
586                 com_err(argv[0], 0, "Warning: inode already clear");
587         ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
588         ext2fs_mark_ib_dirty(current_fs);
589 }
590
591 void do_seti(int argc, char *argv[])
592 {
593         ino_t inode;
594
595         if (argc != 2) {
596                 com_err(argv[0], 0, "Usage: seti <file>");
597                 return;
598         }
599         if (check_fs_open(argv[0]))
600                 return;
601         if (check_fs_read_write(argv[0]))
602                 return;
603         inode = string_to_inode(argv[1]);
604         if (!inode) 
605                 return;
606
607         if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
608                 com_err(argv[0], 0, "Warning: inode already set");
609         ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
610         ext2fs_mark_ib_dirty(current_fs);
611 }
612
613 void do_testi(int argc, char *argv[])
614 {
615         ino_t inode;
616
617         if (argc != 2) {
618                 com_err(argv[0], 0, "Usage: testi <file>");
619                 return;
620         }
621         if (check_fs_open(argv[0]))
622                 return;
623         if (check_fs_bitmaps(argv[0]))
624                 return;
625         inode = string_to_inode(argv[1]);
626         if (!inode) 
627                 return;
628
629         if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
630                 printf("Inode %ld is marked in use\n", inode);
631         else
632                 printf("Inode %ld is not in use\n", inode);
633 }
634
635
636 void do_freeb(int argc, char *argv[])
637 {
638         blk_t block;
639         char *tmp;
640
641         if (argc != 2) {
642                 com_err(argv[0], 0, "Usage: freeb <block>");
643                 return;
644         }
645         if (check_fs_open(argv[0]))
646                 return;
647         if (check_fs_read_write(argv[0]))
648                 return;
649         block = strtoul(argv[1], &tmp, 0);
650         if (!block || *tmp) {
651                 com_err(argv[0], 0, "No block 0");
652                 return;
653         } 
654         if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
655                 com_err(argv[0], 0, "Warning: block already clear");
656         ext2fs_unmark_block_bitmap(current_fs->block_map,block);
657         ext2fs_mark_bb_dirty(current_fs);
658 }
659
660 void do_setb(int argc, char *argv[])
661 {
662         blk_t block;
663         char *tmp;
664
665         if (argc != 2) {
666                 com_err(argv[0], 0, "Usage: setb <block>");
667                 return;
668         }
669         if (check_fs_open(argv[0]))
670                 return;
671         if (check_fs_read_write(argv[0]))
672                 return;
673         block = strtoul(argv[1], &tmp, 0);
674         if (!block || *tmp) {
675                 com_err(argv[0], 0, "No block 0");
676                 return;
677         } 
678         if (ext2fs_test_block_bitmap(current_fs->block_map,block))
679                 com_err(argv[0], 0, "Warning: block already set");
680         ext2fs_mark_block_bitmap(current_fs->block_map,block);
681         ext2fs_mark_bb_dirty(current_fs);
682 }
683
684 void do_testb(int argc, char *argv[])
685 {
686         blk_t block;
687         char *tmp;
688
689         if (argc != 2) {
690                 com_err(argv[0], 0, "Usage: testb <block>");
691                 return;
692         }
693         if (check_fs_open(argv[0]))
694                 return;
695         if (check_fs_bitmaps(argv[0]))
696                 return;
697         block = strtoul(argv[1], &tmp, 0);
698         if (!block || *tmp) {
699                 com_err(argv[0], 0, "No block 0");
700                 return;
701         } 
702         if (ext2fs_test_block_bitmap(current_fs->block_map,block))
703                 printf("Block %d marked in use\n", block);
704         else printf("Block %d not in use\n", block);
705 }
706
707 static void modify_u8(char *com, const char *prompt,
708                       const char *format, __u8 *val)
709 {
710         char buf[200];
711         unsigned long v;
712         char *tmp;
713
714         sprintf(buf, format, *val);
715         printf("%30s    [%s] ", prompt, buf);
716         fgets(buf, sizeof(buf), stdin);
717         if (buf[strlen (buf) - 1] == '\n')
718                 buf[strlen (buf) - 1] = '\0';
719         if (!buf[0])
720                 return;
721         v = strtoul(buf, &tmp, 0);
722         if (*tmp)
723                 com_err(com, 0, "Bad value - %s", buf);
724         else
725                 *val = v;
726 }
727
728 static void modify_u16(char *com, const char *prompt,
729                        const char *format, __u16 *val)
730 {
731         char buf[200];
732         unsigned long v;
733         char *tmp;
734
735         sprintf(buf, format, *val);
736         printf("%30s    [%s] ", prompt, buf);
737         fgets(buf, sizeof(buf), stdin);
738         if (buf[strlen (buf) - 1] == '\n')
739                 buf[strlen (buf) - 1] = '\0';
740         if (!buf[0])
741                 return;
742         v = strtoul(buf, &tmp, 0);
743         if (*tmp)
744                 com_err(com, 0, "Bad value - %s", buf);
745         else
746                 *val = v;
747 }
748
749 static void modify_u32(char *com, const char *prompt,
750                        const char *format, __u32 *val)
751 {
752         char buf[200];
753         unsigned long v;
754         char *tmp;
755
756         sprintf(buf, format, *val);
757         printf("%30s    [%s] ", prompt, buf);
758         fgets(buf, sizeof(buf), stdin);
759         if (buf[strlen (buf) - 1] == '\n')
760                 buf[strlen (buf) - 1] = '\0';
761         if (!buf[0])
762                 return;
763         v = strtoul(buf, &tmp, 0);
764         if (*tmp)
765                 com_err(com, 0, "Bad value - %s", buf);
766         else
767                 *val = v;
768 }
769
770
771 void do_modify_inode(int argc, char *argv[])
772 {
773         struct ext2_inode inode;
774         ino_t inode_num;
775         int i;
776         errcode_t       retval;
777         unsigned char *frag, *fsize;
778         char    buf[80];
779         int os = current_fs->super->s_creator_os;
780         const char *hex_format = "0x%x";
781         const char *octal_format = "0%o";
782         const char *decimal_format = "%d";
783         
784         if (argc != 2) {
785                 com_err(argv[0], 0, "Usage: modify_inode <file>");
786                 return;
787         }
788         if (check_fs_open(argv[0]))
789                 return;
790         if (check_fs_read_write(argv[0]))
791                 return;
792
793         inode_num = string_to_inode(argv[1]);
794         if (!inode_num) 
795                 return;
796
797         retval = ext2fs_read_inode(current_fs, inode_num, &inode);
798         if (retval) {
799                 com_err(argv[1], retval, "while trying to read inode %d",
800                         inode_num);
801                 return;
802         }
803         
804         modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
805         modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
806         modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
807         modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
808         modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
809         modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
810         modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
811         modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
812         modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
813         modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
814         modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
815 #if 0
816         modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
817 #endif
818         modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
819         if (LINUX_S_ISDIR(inode.i_mode))
820                 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
821         else
822                 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
823
824         if (current_fs->super->s_creator_os == EXT2_OS_HURD)
825                 modify_u32(argv[0], "Translator Block",
826                             decimal_format, &inode.osd1.hurd1.h_i_translator);
827         
828         modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
829         switch (os) {
830             case EXT2_OS_LINUX:
831                 frag = &inode.osd2.linux2.l_i_frag;
832                 fsize = &inode.osd2.linux2.l_i_fsize;
833                 break;
834             case EXT2_OS_HURD:
835                 frag = &inode.osd2.hurd2.h_i_frag;
836                 fsize = &inode.osd2.hurd2.h_i_fsize;
837                 break;
838             case EXT2_OS_MASIX:
839                 frag = &inode.osd2.masix2.m_i_frag;
840                 fsize = &inode.osd2.masix2.m_i_fsize;
841                 break;
842             default:
843                 frag = fsize = 0;
844         }
845         if (frag)
846                 modify_u8(argv[0], "Fragment number", decimal_format, frag);
847         if (fsize)
848                 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
849
850         for (i=0;  i < EXT2_NDIR_BLOCKS; i++) {
851                 sprintf(buf, "Direct Block #%d", i);
852                 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
853         }
854         modify_u32(argv[0], "Indirect Block", decimal_format,
855                     &inode.i_block[EXT2_IND_BLOCK]);    
856         modify_u32(argv[0], "Double Indirect Block", decimal_format,
857                     &inode.i_block[EXT2_DIND_BLOCK]);
858         modify_u32(argv[0], "Triple Indirect Block", decimal_format,
859                     &inode.i_block[EXT2_TIND_BLOCK]);
860         retval = ext2fs_write_inode(current_fs, inode_num, &inode);
861         if (retval) {
862                 com_err(argv[1], retval, "while trying to write inode %d",
863                         inode_num);
864                 return;
865         }
866 }
867
868 void do_change_working_dir(int argc, char *argv[])
869 {
870         ino_t   inode;
871         int     retval;
872         
873         if (argc != 2) {
874                 com_err(argv[0], 0, "Usage: cd <file>");
875                 return;
876         }
877         if (check_fs_open(argv[0]))
878                 return;
879
880         inode = string_to_inode(argv[1]);
881         if (!inode) 
882                 return;
883
884         retval = ext2fs_check_directory(current_fs, inode);
885         if (retval) {
886                 com_err(argv[1], retval, "");
887                 return;
888         }
889         cwd = inode;
890         return;
891 }
892
893 void do_print_working_directory(int argc, char *argv[])
894 {
895         int     retval;
896         char    *pathname = NULL;
897         
898         if (argc > 1) {
899                 com_err(argv[0], 0, "Usage: print_working_directory");
900                 return;
901         }
902         if (check_fs_open(argv[0]))
903                 return;
904
905         retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
906         if (retval) {
907                 com_err(argv[0], retval,
908                         "while trying to get pathname of cwd");
909         }
910         printf("[pwd]   INODE: %6ld  PATH: %s\n", cwd, pathname);
911         free(pathname);
912         retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
913         if (retval) {
914                 com_err(argv[0], retval,
915                         "while trying to get pathname of root");
916         }
917         printf("[root]  INODE: %6ld  PATH: %s\n", root, pathname);
918         free(pathname);
919         return;
920 }
921
922 static void make_link(char *sourcename, char *destname)
923 {
924         ino_t   inode;
925         int     retval;
926         ino_t   dir;
927         char    *dest, *cp, *basename;
928
929         /*
930          * Get the source inode
931          */
932         inode = string_to_inode(sourcename);
933         if (!inode)
934                 return;
935         basename = strrchr(sourcename, '/');
936         if (basename)
937                 basename++;
938         else
939                 basename = sourcename;
940         /*
941          * Figure out the destination.  First see if it exists and is
942          * a directory.  
943          */
944         if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
945                 dest = basename;
946         else {
947                 /*
948                  * OK, it doesn't exist.  See if it is
949                  * '<dir>/basename' or 'basename'
950                  */
951                 cp = strrchr(destname, '/');
952                 if (cp) {
953                         *cp = 0;
954                         dir = string_to_inode(destname);
955                         if (!dir)
956                                 return;
957                         dest = cp+1;
958                 } else {
959                         dir = cwd;
960                         dest = destname;
961                 }
962         }
963         
964         retval = ext2fs_link(current_fs, dir, dest, inode, 0);
965         if (retval)
966                 com_err("make_link", retval, "");
967         return;
968 }
969
970
971 void do_link(int argc, char *argv[])
972 {
973         if (argc != 3) {
974                 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
975                 return;
976         }
977         if (check_fs_open(argv[0]))
978                 return;
979
980         make_link(argv[1], argv[2]);
981 }
982
983 static void unlink_file_by_name(char *filename)
984 {
985         int     retval;
986         ino_t   dir;
987         char    *basename;
988         
989         basename = strrchr(filename, '/');
990         if (basename) {
991                 *basename++ = '\0';
992                 dir = string_to_inode(filename);
993                 if (!dir)
994                         return;
995         } else {
996                 dir = cwd;
997                 basename = filename;
998         }
999         retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
1000         if (retval)
1001                 com_err("unlink_file_by_name", retval, "");
1002         return;
1003 }
1004
1005 void do_unlink(int argc, char *argv[])
1006 {
1007         if (argc != 2) {
1008                 com_err(argv[0], 0, "Usage: unlink <pathname>");
1009                 return;
1010         }
1011         if (check_fs_open(argv[0]))
1012                 return;
1013
1014         unlink_file_by_name(argv[1]);
1015 }
1016
1017 void do_find_free_block(int argc, char *argv[])
1018 {
1019         blk_t   free_blk, goal;
1020         errcode_t       retval;
1021         char            *tmp;
1022         
1023         if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
1024                 com_err(argv[0], 0, "Usage: find_free_block [goal]");
1025                 return;
1026         }
1027         if (check_fs_open(argv[0]))
1028                 return;
1029
1030         if (argc > 1) {
1031                 goal = strtol(argv[1], &tmp, 0);
1032                 if (*tmp) {
1033                         com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1034                         return;
1035                 }
1036         }
1037         else
1038                 goal = current_fs->super->s_first_data_block;
1039
1040         retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
1041         if (retval)
1042                 com_err("ext2fs_new_block", retval, "");
1043         else
1044                 printf("Free block found: %d\n", free_blk);
1045
1046 }
1047
1048 void do_find_free_inode(int argc, char *argv[])
1049 {
1050         ino_t   free_inode, dir;
1051         int     mode;
1052         int     retval;
1053         char    *tmp;
1054         
1055         if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1056                 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
1057                 return;
1058         }
1059         if (check_fs_open(argv[0]))
1060                 return;
1061
1062         if (argc > 1) {
1063                 dir = strtol(argv[1], &tmp, 0);
1064                 if (*tmp) {
1065                         com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1066                         return;
1067                 }
1068         }
1069         else
1070                 dir = root;
1071         if (argc > 2) {
1072                 mode = strtol(argv[2], &tmp, 0);
1073                 if (*tmp) {
1074                         com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1075                         return;
1076                 }
1077         } else
1078                 mode = 010755;
1079
1080         retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
1081         if (retval)
1082                 com_err("ext2fs_new_inode", retval, "");
1083         else
1084                 printf("Free inode found: %ld\n", free_inode);
1085 }
1086
1087 static errcode_t copy_file(int fd, ino_t newfile)
1088 {
1089         ext2_file_t     e2_file;
1090         errcode_t       retval;
1091         unsigned int    got, written;
1092         char            buf[8192];
1093         char            *ptr;
1094
1095         retval = ext2fs_file_open(current_fs, newfile,
1096                                   EXT2_FILE_WRITE, &e2_file);
1097         if (retval)
1098                 return retval;
1099
1100         while (1) {
1101                 got = read(fd, buf, sizeof(buf));
1102                 if (got == 0)
1103                         break;
1104                 if (got < 0) {
1105                         retval = errno;
1106                         goto fail;
1107                 }
1108                 ptr = buf;
1109                 while (got > 0) {
1110                         retval = ext2fs_file_write(e2_file, ptr,
1111                                                    got, &written);
1112                         if (retval)
1113                                 goto fail;
1114
1115                         got -= written;
1116                         ptr += written;
1117                 }
1118         }
1119         retval = ext2fs_file_close(e2_file);
1120
1121         return retval;
1122
1123 fail:
1124         (void) ext2fs_file_close(e2_file);
1125         return retval;
1126 }
1127
1128
1129 void do_write(int argc, char *argv[])
1130 {
1131         int     fd;
1132         struct stat statbuf;
1133         ino_t   newfile;
1134         errcode_t retval;
1135         struct ext2_inode inode;
1136         dgrp_t group;
1137
1138         if (check_fs_open(argv[0]))
1139                 return;
1140         if (argc != 3) {
1141                 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1142                 return;
1143         }
1144         if (check_fs_read_write(argv[0]))
1145                 return;
1146         fd = open(argv[1], O_RDONLY);
1147         if (fd < 0) {
1148                 com_err(argv[1], fd, "");
1149                 return;
1150         }
1151         if (fstat(fd, &statbuf) < 0) {
1152                 com_err(argv[1], errno, "");
1153                 close(fd);
1154                 return;
1155         }
1156
1157         retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1158         if (retval) {
1159                 com_err(argv[0], retval, "");
1160                 close(fd);
1161                 return;
1162         }
1163         group = ext2fs_group_of_ino(current_fs, newfile);
1164         current_fs->group_desc[group].bg_free_inodes_count--;
1165         current_fs->super->s_free_inodes_count--;
1166         ext2fs_mark_super_dirty(current_fs);
1167         printf("Allocated inode: %ld\n", newfile);
1168         retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
1169         if (retval) {
1170                 com_err(argv[2], retval, "");
1171                 close(fd);
1172                 return;
1173         }
1174         if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1175                 com_err(argv[0], 0, "Warning: inode already set");
1176         ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1177         ext2fs_mark_ib_dirty(current_fs);
1178         memset(&inode, 0, sizeof(inode));
1179         inode.i_mode = statbuf.st_mode;
1180         inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1181         inode.i_links_count = 1;
1182         inode.i_size = statbuf.st_size;
1183         ext2fs_write_inode(current_fs, newfile, &inode);
1184         retval = ext2fs_write_inode(current_fs, newfile, &inode);
1185         if (retval) {
1186                 com_err(argv[0], retval, "while trying to write inode %d", 
1187                         inode);
1188                 close(fd);
1189                 return;
1190         }
1191         if (LINUX_S_ISREG(inode.i_mode)) {
1192                 retval = copy_file(fd, newfile);
1193                 if (retval)
1194                         com_err("copy_file", retval, "");
1195         }
1196         close(fd);
1197 }
1198
1199 void do_mknod(int argc, char *argv[])
1200 {
1201         unsigned long mode, major, minor, nr;
1202         ino_t   newfile;
1203         errcode_t retval;
1204         struct ext2_inode inode;
1205
1206         if (check_fs_open(argv[0]))
1207                 return;
1208         if (argc < 3 || argv[2][1]) {
1209                 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1210                 return;
1211         }
1212         mode = minor = major = 0;
1213         switch (argv[2][0]) {
1214                 case 'p':
1215                         mode = LINUX_S_IFIFO;
1216                         nr = 3;
1217                         break;
1218                 case 'c':
1219                         mode = LINUX_S_IFCHR;
1220                         nr = 5;
1221                         break;
1222                 case 'b':
1223                         mode = LINUX_S_IFBLK;
1224                         nr = 5;
1225                         break;
1226                 default:
1227                         nr = 0;
1228         }
1229         if (nr == 5) {
1230                 major = strtoul(argv[3], argv+3, 0);
1231                 minor = strtoul(argv[4], argv+4, 0);
1232                 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1233                         nr = 0;
1234         }
1235         if (argc != nr) {
1236                 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1237                 return;
1238         }
1239         if (check_fs_read_write(argv[0]))
1240                 return;
1241         retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
1242         if (retval) {
1243                 com_err(argv[0], retval, "");
1244                 return;
1245         }
1246         printf("Allocated inode: %ld\n", newfile);
1247         retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
1248         if (retval) {
1249                 if (retval == EXT2_ET_DIR_NO_SPACE) {
1250                         retval = ext2fs_expand_dir(current_fs, cwd);
1251                         if (!retval)
1252                                 retval = ext2fs_link(current_fs, cwd,
1253                                                      argv[1], newfile, 0);
1254                 }
1255                 if (retval) {
1256                         com_err(argv[1], retval, "");
1257                         return;
1258                 }
1259         }
1260         if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
1261                 com_err(argv[0], 0, "Warning: inode already set");
1262         ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1263         ext2fs_mark_ib_dirty(current_fs);
1264         memset(&inode, 0, sizeof(inode));
1265         inode.i_mode = mode;
1266         inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1267         inode.i_block[0] = major*256+minor;
1268         inode.i_links_count = 1;
1269         ext2fs_write_inode(current_fs, newfile, &inode);
1270         retval = ext2fs_write_inode(current_fs, newfile, &inode);
1271         if (retval) {
1272                 com_err(argv[0], retval, "while trying to write inode %d", inode);
1273                 return;
1274         }
1275 }
1276
1277 void do_mkdir(int argc, char *argv[])
1278 {
1279         char    *cp;
1280         ino_t   parent;
1281         char    *name;
1282         errcode_t retval;
1283
1284         if (check_fs_open(argv[0]))
1285                 return;
1286
1287         if (argc != 2) {
1288                 com_err(argv[0], 0, "Usage: mkdir <file>");
1289                 return;
1290         }
1291
1292         if (check_fs_read_write(argv[0]))
1293                 return;
1294
1295         cp = strrchr(argv[1], '/');
1296         if (cp) {
1297                 *cp = 0;
1298                 parent = string_to_inode(argv[1]);
1299                 if (!parent) {
1300                         com_err(argv[1], ENOENT, "");
1301                         return;
1302                 }
1303                 name = cp+1;
1304         } else {
1305                 parent = cwd;
1306                 name = argv[1];
1307         }
1308
1309
1310         retval = ext2fs_mkdir(current_fs, parent, 0, name);
1311         if (retval) {
1312                 com_err("ext2fs_mkdir", retval, "");
1313                 return;
1314         }
1315
1316 }
1317
1318 void do_rmdir(int argc, char *argv[])
1319 {
1320         printf("Unimplemented\n");
1321 }
1322
1323
1324 static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1325                                int blockcnt, void *private)
1326 {
1327         printf("%d ", *blocknr);
1328         ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
1329         return 0;
1330 }
1331
1332 static void kill_file_by_inode(ino_t inode)
1333 {
1334         struct ext2_inode inode_buf;
1335
1336         ext2fs_read_inode(current_fs, inode, &inode_buf);
1337         inode_buf.i_dtime = time(NULL);
1338         ext2fs_write_inode(current_fs, inode, &inode_buf);
1339
1340         printf("Kill file by inode %ld\n", inode);
1341         ext2fs_block_iterate(current_fs, inode, 0, NULL,
1342                              release_blocks_proc, NULL);
1343         printf("\n");
1344         ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
1345
1346         ext2fs_mark_bb_dirty(current_fs);
1347         ext2fs_mark_ib_dirty(current_fs);
1348 }
1349
1350
1351 void do_kill_file(int argc, char *argv[])
1352 {
1353         ino_t inode_num;
1354
1355         if (argc != 2) {
1356                 com_err(argv[0], 0, "Usage: kill_file <file>");
1357                 return;
1358         }
1359         if (check_fs_open(argv[0]))
1360                 return;
1361
1362         if (check_fs_read_write(argv[0]))
1363                 return;
1364
1365         inode_num = string_to_inode(argv[1]);
1366         if (!inode_num) {
1367                 com_err(argv[0], 0, "Cannot find file");
1368                 return;
1369         }
1370         kill_file_by_inode(inode_num);
1371 }
1372
1373 void do_rm(int argc, char *argv[])
1374 {
1375         int retval;
1376         ino_t inode_num;
1377         struct ext2_inode inode;
1378
1379         if (argc != 2) {
1380                 com_err(argv[0], 0, "Usage: rm <filename>");
1381                 return;
1382         }
1383         if (check_fs_open(argv[0]))
1384                 return;
1385
1386         if (check_fs_read_write(argv[0]))
1387                 return;
1388
1389         retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1390         if (retval) {
1391                 com_err(argv[0], retval, "while trying to resolve filename");
1392                 return;
1393         }
1394
1395         retval = ext2fs_read_inode(current_fs,inode_num,&inode);
1396         if (retval) {
1397                 com_err(argv[0], retval, "while reading file's inode");
1398                 return;
1399         }
1400
1401         if (LINUX_S_ISDIR(inode.i_mode)) {
1402                 com_err(argv[0], 0, "file is a directory");
1403                 return;
1404         }
1405
1406         --inode.i_links_count;
1407         retval = ext2fs_write_inode(current_fs,inode_num,&inode);
1408         if (retval) {
1409                 com_err(argv[0], retval, "while writing inode");
1410                 return;
1411         }
1412
1413         unlink_file_by_name(argv[1]);
1414         if (inode.i_links_count == 0)
1415                 kill_file_by_inode(inode_num);
1416 }
1417
1418 void do_show_debugfs_params(int argc, char *argv[])
1419 {
1420         FILE *out = stdout;
1421
1422         if (current_fs)
1423                 fprintf(out, "Open mode: read-%s\n",
1424                         current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
1425         fprintf(out, "Filesystem in use: %s\n",
1426                 current_fs ? current_fs->device_name : "--none--");
1427 }
1428
1429 void do_expand_dir(int argc, char *argv[])
1430 {
1431         ino_t inode;
1432         int retval;
1433
1434         if (argc != 2) {
1435                 com_err(argv[0], 0, "Usage: expand_dir <file>");
1436                 return;
1437         }
1438         if (check_fs_open(argv[0]))
1439                 return;
1440         inode = string_to_inode(argv[1]);
1441         if (!inode)
1442                 return;
1443
1444         retval = ext2fs_expand_dir(current_fs, inode);
1445         if (retval)
1446                 com_err("ext2fs_expand_dir", retval, "");
1447         return;
1448 }
1449
1450 void do_features(int argc, char *argv[])
1451 {
1452         int     i;
1453         
1454         if (check_fs_open(argv[0]))
1455                 return;
1456
1457         if ((argc != 1) && check_fs_read_write(argv[0]))
1458                 return;
1459         for (i=1; i < argc; i++) {
1460                 if (e2p_edit_feature(argv[i],
1461                                      &current_fs->super->s_feature_compat, 0))
1462                         com_err(argv[0], 0, "Unknown feature: %s\n",
1463                                 argv[i]);
1464                 else
1465                         ext2fs_mark_super_dirty(current_fs);
1466         }
1467         print_features((struct ext2fs_sb *) current_fs->super, stdout);
1468 }
1469
1470 static int source_file(const char *cmd_file, int sci_idx)
1471 {
1472         FILE            *f;
1473         char            buf[256];
1474         char            *cp;
1475         int             exit_status = 0;
1476         int             retval;
1477
1478         if (strcmp(cmd_file, "-") == 0)
1479                 f = stdin;
1480         else {
1481                 f = fopen(cmd_file, "r");
1482                 if (!f) {
1483                         perror(cmd_file);
1484                         exit(1);
1485                 }
1486         }
1487         setbuf(stdout, NULL);
1488         setbuf(stderr, NULL);
1489         while (!feof(f)) {
1490                 if (fgets(buf, sizeof(buf), f) == NULL)
1491                         break;
1492                 cp = strchr(buf, '\n');
1493                 if (cp)
1494                         *cp = 0;
1495                 cp = strchr(buf, '\r');
1496                 if (cp)
1497                         *cp = 0;
1498                 printf("debugfs: %s\n", buf);
1499                 retval = ss_execute_line(sci_idx, buf);
1500                 if (retval) {
1501                         ss_perror(sci_idx, retval, buf);
1502                         exit_status++;
1503                 }
1504         }
1505         return exit_status;
1506 }
1507
1508 int main(int argc, char **argv)
1509 {
1510         int             retval;
1511         int             sci_idx;
1512         const char      *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
1513         int             c;
1514         int             open_flags = 0;
1515         char            *request = 0;
1516         int             exit_status = 0;
1517         char            *cmd_file = 0;
1518         blk_t           superblock = 0;
1519         blk_t           blocksize = 0;
1520         int             catastrophic = 0;
1521         char            *tmp;
1522         
1523         initialize_ext2_error_table();
1524         fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1525                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
1526                  EXT2FS_VERSION, EXT2FS_DATE);
1527
1528         while ((c = getopt (argc, argv, "wcR:f:b:s:V")) != EOF) {
1529                 switch (c) {
1530                 case 'R':
1531                         request = optarg;
1532                         break;
1533                 case 'f':
1534                         cmd_file = optarg;
1535                         break;
1536                 case 'w':
1537                         open_flags = EXT2_FLAG_RW;
1538                         break;
1539                 case 'b':
1540                         blocksize = strtoul(optarg, &tmp, 0);
1541                         if (*tmp) {
1542                                 com_err(argv[0], 0,
1543                                         "Bad block size - %s", optarg);
1544                                 return 1;
1545                         }
1546                         break;
1547                 case 's':
1548                         superblock = strtoul(optarg, &tmp, 0);
1549                         if (*tmp) {
1550                                 com_err(argv[0], 0,
1551                                         "Bad superblock number - %s", optarg);
1552                                 return 1;
1553                         }
1554                         break;
1555                 case 'c':
1556                         catastrophic = 1;
1557                         break;
1558                 case 'V':
1559                         /* Print version number and exit */
1560                         fprintf(stderr, "\tUsing %s\n",
1561                                 error_message(EXT2_ET_BASE));
1562                         exit(0);
1563                 default:
1564                         com_err(argv[0], 0, usage);
1565                         return 1;
1566                 }
1567         }
1568         if (optind < argc)
1569                 open_filesystem(argv[optind], open_flags,
1570                                 superblock, blocksize, catastrophic);
1571         
1572         sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1573                                        &debug_cmds, &retval);
1574         if (retval) {
1575                 ss_perror(sci_idx, retval, "creating invocation");
1576                 exit(1);
1577         }
1578
1579         (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1580         if (retval) {
1581                 ss_perror(sci_idx, retval, "adding standard requests");
1582                 exit (1);
1583         }
1584         if (request) {
1585                 retval = 0;
1586                 retval = ss_execute_line(sci_idx, request);
1587                 if (retval) {
1588                         ss_perror(sci_idx, retval, request);
1589                         exit_status++;
1590                 }
1591         } else if (cmd_file) {
1592                 exit_status = source_file(cmd_file, sci_idx);
1593         } else {
1594                 ss_listen(sci_idx);
1595         }
1596
1597         if (current_fs)
1598                 close_filesystem();
1599         
1600         return exit_status;
1601 }