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