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