blob: 662742e59a46073c03357ffcca82c13eb77b6030 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
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>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#else
20extern int optind;
21extern char *optarg;
22#endif
23#ifdef HAVE_OPTRESET
24extern 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>
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#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"
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000036#include "uuid/uuid.h"
Theodore Ts'of68aa411999-10-26 14:20:22 +000037#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000038
Theodore Ts'o818180c1998-06-27 05:11:14 +000039#include "../version.h"
40
Theodore Ts'o3839e651997-04-26 13:21:57 +000041extern ss_request_table debug_cmds;
42
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000043ext2_filsys current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000044ino_t root, cwd;
45
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000046static void open_filesystem(char *device, int open_flags, blk_t superblock,
47 blk_t blocksize, int catastrophic)
Theodore Ts'o3839e651997-04-26 13:21:57 +000048{
49 int retval;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000050
51 if (superblock != 0 && blocksize == 0) {
52 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
53 current_fs = NULL;
54 return;
55 }
56
57 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
58 com_err(device, 0,
59 "opening read-only because of catastrophic mode");
60 open_flags &= ~EXT2_FLAG_RW;
61 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000062
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000063 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000064 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000065 if (retval) {
66 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000067 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000068 return;
69 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000070
71 if (catastrophic)
72 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
73 else {
74 retval = ext2fs_read_inode_bitmap(current_fs);
75 if (retval) {
76 com_err(device, retval, "while reading inode bitmap");
77 goto errout;
78 }
79 retval = ext2fs_read_block_bitmap(current_fs);
80 if (retval) {
81 com_err(device, retval, "while reading block bitmap");
82 goto errout;
83 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 }
85 root = cwd = EXT2_ROOT_INO;
86 return;
87
88errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000089 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000090 if (retval)
91 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000092 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000093}
94
95void do_open_filesys(int argc, char **argv)
96{
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000097 const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
Theodore Ts'of1304811997-10-25 03:51:53 +000098 int c;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000099 int catastrophic = 0;
100 blk_t superblock = 0;
101 blk_t blocksize = 0;
102 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 int open_flags = 0;
104
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000105 optind = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000106#ifdef HAVE_OPTRESET
107 optreset = 1; /* Makes BSD getopt happy */
108#endif
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000109 while ((c = getopt (argc, argv, "wfcb:s:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110 switch (c) {
111 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000112 open_flags |= EXT2_FLAG_RW;
113 break;
114 case 'f':
115 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000117 case 'c':
118 catastrophic = 1;
119 break;
120 case 'b':
121 blocksize = strtoul(optarg, &tmp, 0);
122 if (*tmp) {
123 com_err(argv[0], 0,
124 "Bad block size - %s", optarg);
125 return;
126 }
127 break;
128 case 's':
129 superblock = strtoul(optarg, &tmp, 0);
130 if (*tmp) {
131 com_err(argv[0], 0,
132 "Bad superblock number - %s", optarg);
133 return;
134 }
135 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 default:
137 com_err(argv[0], 0, usage);
138 return;
139 }
140 }
141 if (optind != argc-1) {
142 com_err(argv[0], 0, usage);
143 return;
144 }
145 if (check_fs_not_open(argv[0]))
146 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000147 open_filesystem(argv[optind], open_flags,
148 superblock, blocksize, catastrophic);
149}
150
151void do_lcd(int argc, char **argv)
152{
153 const char *usage = "Usage: lcd <native dir>";
154
155 if (argc != 2) {
156 com_err(argv[0], 0, usage);
157 return;
158 }
159
160 if (chdir(argv[1]) == -1) {
161 com_err(argv[0], errno,
162 "while trying to change native directory to %s",
163 argv[1]);
164 return;
165 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166}
167
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000168static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169{
170 int retval;
171
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000172 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
173 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174 if (retval)
175 com_err("ext2fs_write_inode_bitmap", retval, "");
176 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000177 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
178 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000179 if (retval)
180 com_err("ext2fs_write_block_bitmap", retval, "");
181 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000182 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000183 if (retval)
184 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000185 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000186 return;
187}
188
189void do_close_filesys(int argc, char **argv)
190{
191 if (argc > 1) {
192 com_err(argv[0], 0, "Usage: close_filesys");
193 return;
194 }
195 if (check_fs_open(argv[0]))
196 return;
197 close_filesystem();
198}
199
200void do_init_filesys(int argc, char **argv)
201{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000202 const char *usage = "Usage: initialize <device> <blocksize>";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203 struct ext2_super_block param;
204 errcode_t retval;
205 char *tmp;
206
207 if (argc != 3) {
208 com_err(argv[0], 0, usage);
209 return;
210 }
211 if (check_fs_not_open(argv[0]))
212 return;
213
214 memset(&param, 0, sizeof(struct ext2_super_block));
215 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
216 if (*tmp) {
217 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
218 return;
219 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000220 retval = ext2fs_initialize(argv[1], 0, &param,
221 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222 if (retval) {
223 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000224 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 return;
226 }
227 root = cwd = EXT2_ROOT_INO;
228 return;
229}
230
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000231static void print_features(struct ext2fs_sb * s, FILE *f)
232{
233#ifdef EXT2_DYNAMIC_REV
234 int i, j, printed=0;
235__u32 *mask = &s->s_feature_compat, m;
236
237 printf ("Filesystem features:");
238 for (i=0; i <3; i++,mask++) {
239 for (j=0,m=1; j < 32; j++, m<<=1) {
240 if (*mask & m) {
241 fprintf(f, " %s", e2p_feature2string(i, m));
242 printed++;
243 }
244 }
245 }
246 if (printed == 0)
247 printf("(none)");
248 printf("\n");
249#endif
250}
251
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252void do_show_super_stats(int argc, char *argv[])
253{
254 int i;
255 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000256 struct ext2fs_sb *sb;
257 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000258 int c, header_only = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000259 char buf[80];
260 const char *none = "(none)";
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000261 const char *usage = "Usage: show_super [-h]";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000262
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000263 optind = 0;
264#ifdef HAVE_OPTRESET
265 optreset = 1; /* Makes BSD getopt happy */
266#endif
267 while ((c = getopt (argc, argv, "h")) != EOF) {
268 switch (c) {
269 case 'h':
270 header_only++;
271 break;
272 default:
273 com_err(argv[0], 0, usage);
274 return;
275 }
276 }
277 if (optind != argc) {
278 com_err(argv[0], 0, usage);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000279 return;
280 }
281 if (check_fs_open(argv[0]))
282 return;
283 out = open_pager();
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000284 sb = (struct ext2fs_sb *) current_fs->super;
285 fprintf(out, "Filesystem is read-%s\n",
286 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
287 if (sb->s_volume_name[0]) {
288 memset(buf, 0, sizeof(buf));
289 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
290 } else
291 strcpy(buf, none);
292 fprintf(out, "Volume name = %s\n", buf);
293 if (sb->s_last_mounted[0]) {
294 memset(buf, 0, sizeof(buf));
295 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
296 } else
297 strcpy(buf, none);
298 fprintf(out, "Last mounted directory = %s\n", buf);
299 if (!uuid_is_null(sb->s_uuid))
300 uuid_unparse(sb->s_uuid, buf);
301 else
302 strcpy(buf, none);
303 fprintf(out, "Filesystem UUID = %s\n", buf);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000304 print_features(sb, out);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000305 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
306 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 fprintf(out, "Mount counts = %d (maximal = %d)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000308 sb->s_mnt_count, sb->s_max_mnt_count);
309 fputs ("Filesystem OS type = ", out);
310 switch (sb->s_creator_os) {
311 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
312 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
313 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
314 default: fputs ("unknown\n", out);
315 }
316 fprintf(out, "Superblock size = %d\n",
317 sizeof(struct ext2_super_block));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000318 fprintf(out, "Block size = %d, fragment size = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000319 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
320 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
321 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
322 sb->s_free_inodes_count);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000323 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000324 sb->s_blocks_count, sb->s_free_blocks_count,
325 sb->s_r_blocks_count, sb->s_first_data_block);
326 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
327 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
328 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000330 current_fs->group_desc_count,
331 (current_fs->group_desc_count != 1) ? "s" : "",
332 current_fs->desc_blocks,
333 (current_fs->desc_blocks != 1) ? "s" : "");
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000334
335 if (header_only) {
336 close_pager(out);
337 return;
338 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000339
340 gdp = &current_fs->group_desc[0];
341 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000342 fprintf(out, " Group %2d: block bitmap at %d, "
343 "inode bitmap at %d, "
344 "inode table at %d\n"
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 " %d free block%s, "
346 "%d free inode%s, "
347 "%d used director%s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000348 i, gdp->bg_block_bitmap,
349 gdp->bg_inode_bitmap, gdp->bg_inode_table,
350 gdp->bg_free_blocks_count,
351 gdp->bg_free_blocks_count != 1 ? "s" : "",
352 gdp->bg_free_inodes_count,
353 gdp->bg_free_inodes_count != 1 ? "s" : "",
354 gdp->bg_used_dirs_count,
355 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356 close_pager(out);
357}
358
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000359void do_dirty_filesys(int argc, char **argv)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000360{
361 if (check_fs_open(argv[0]))
362 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000363 if (check_fs_read_write(argv[0]))
364 return;
365
366 if (argv[1] && !strcmp(argv[1], "-clean"))
367 current_fs->super->s_state |= EXT2_VALID_FS;
368 else
369 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000370 ext2fs_mark_super_dirty(current_fs);
371}
372
Theodore Ts'o3839e651997-04-26 13:21:57 +0000373struct list_blocks_struct {
374 FILE *f;
375 int total;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000376 blk_t first_block, last_block;
377 int first_bcnt, last_bcnt;
378 int first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000379};
380
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000381static void finish_range(struct list_blocks_struct *lb)
382{
383 if (lb->first_block == 0)
384 return;
385 if (lb->first)
386 lb->first = 0;
387 else
388 printf(", ");
389 if (lb->first_block == lb->last_block)
390 fprintf(lb->f, "(%d):%d", lb->first_bcnt, lb->first_block);
391 else
392 fprintf(lb->f, "(%d-%d):%d-%d", lb->first_bcnt,
393 lb->last_bcnt, lb->first_block, lb->last_block);
394 lb->first_block = 0;
395}
396
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000397static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
398 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000399{
400 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
401
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000402 lb->total++;
403 if (blockcnt >= 0) {
404 /*
405 * See if we can add on to the existing range (if it exists)
406 */
407 if (lb->first_block &&
408 (lb->last_block+1 == *blocknr) &&
409 (lb->last_bcnt+1 == blockcnt)) {
410 lb->last_block = *blocknr;
411 lb->last_bcnt = blockcnt;
412 return 0;
413 }
414 /*
415 * Start a new range.
416 */
417 finish_range(lb);
418 lb->first_block = lb->last_block = *blocknr;
419 lb->first_bcnt = lb->last_bcnt = blockcnt;
420 return 0;
421 }
422 /*
423 * Not a normal block. Always force a new range.
424 */
425 finish_range(lb);
426 if (lb->first)
427 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000428 else
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000429 printf(", ");
430 if (blockcnt == -1)
431 fprintf(lb->f, "(IND):%d", *blocknr);
432 else if (blockcnt == -2)
433 fprintf(lb->f, "(DIND):%d", *blocknr);
434 else if (blockcnt == -3)
435 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000436 return 0;
437}
438
439
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000440static void dump_blocks(FILE *f, ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441{
442 struct list_blocks_struct lb;
443
444 fprintf(f, "BLOCKS:\n");
445 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000446 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000447 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000448 lb.first = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000449 ext2fs_block_iterate(current_fs, inode, 0, NULL,
450 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000451 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000452 if (lb.total)
453 fprintf(f, "\nTOTAL: %d\n", lb.total);
454 fprintf(f,"\n");
455}
456
457
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000458static void dump_inode(ino_t inode_num, struct ext2_inode inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000460 const char *i_type;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000462 char frag, fsize;
463 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000464
465 out = open_pager();
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000466 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
467 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
468 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
469 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
470 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
471 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
472 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000473 else i_type = "bad type";
474 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
Theodore Ts'o14197172000-07-05 17:48:34 +0000475 fprintf(out, "Mode: %04o Flags: 0x%x Generation: %u\n",
Theodore Ts'ob41d3601999-06-25 15:32:37 +0000476 inode.i_mode & 0777, inode.i_flags, inode.i_generation);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000477 fprintf(out, "User: %5d Group: %5d Size: ",
478 inode.i_uid, inode.i_gid);
479 if (LINUX_S_ISDIR(inode.i_mode))
480 fprintf(out, "%d\n", inode.i_size);
481 else {
482 __u64 i_size = (inode.i_size |
483 ((unsigned long long)inode.i_size_high << 32));
484
485 fprintf(out, "%lld\n", i_size);
486 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000487 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000488 fprintf(out,
489 "File ACL: %d Directory ACL: %d Translator: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000490 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000491 inode.osd1.hurd1.h_i_translator);
492 else
493 fprintf(out, "File ACL: %d Directory ACL: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000494 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000495 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 inode.i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000497 switch (os) {
498 case EXT2_OS_LINUX:
499 frag = inode.osd2.linux2.l_i_frag;
500 fsize = inode.osd2.linux2.l_i_fsize;
501 break;
502 case EXT2_OS_HURD:
503 frag = inode.osd2.hurd2.h_i_frag;
504 fsize = inode.osd2.hurd2.h_i_fsize;
505 break;
506 case EXT2_OS_MASIX:
507 frag = inode.osd2.masix2.m_i_frag;
508 fsize = inode.osd2.masix2.m_i_fsize;
509 break;
510 default:
511 frag = fsize = 0;
512 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000513 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000514 inode.i_faddr, frag, fsize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000515 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000516 time_to_string(inode.i_ctime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000517 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000518 time_to_string(inode.i_atime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000519 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000520 time_to_string(inode.i_mtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 if (inode.i_dtime)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000522 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000523 time_to_string(inode.i_dtime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000524 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000525 fprintf(out, "Fast_link_dest: %.*s\n",
526 inode.i_size, (char *)inode.i_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 else
528 dump_blocks(out, inode_num);
529 close_pager(out);
530}
531
532
533void do_stat(int argc, char *argv[])
534{
535 ino_t inode;
536 struct ext2_inode inode_buf;
537 int retval;
538
539 if (argc != 2) {
540 com_err(argv[0], 0, "Usage: stat <file>");
541 return;
542 }
543 if (check_fs_open(argv[0]))
544 return;
545 inode = string_to_inode(argv[1]);
546 if (!inode)
547 return;
548
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000549 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 if (retval)
551 {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000552 com_err(argv[0], retval, "while trying to read inode %d", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 return;
554 }
555
556 dump_inode(inode,inode_buf);
557 return;
558}
559
560void do_chroot(int argc, char *argv[])
561{
562 ino_t inode;
563 int retval;
564
565 if (argc != 2) {
566 com_err(argv[0], 0, "Usage: chroot <file>");
567 return;
568 }
569 if (check_fs_open(argv[0]))
570 return;
571 inode = string_to_inode(argv[1]);
572 if (!inode)
573 return;
574
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000575 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 if (retval) {
577 com_err(argv[1], retval, "");
578 return;
579 }
580 root = inode;
581}
582
583void do_clri(int argc, char *argv[])
584{
585 ino_t inode;
586 int retval;
587 struct ext2_inode inode_buf;
588
589 if (argc != 2) {
590 com_err(argv[0], 0, "Usage: clri <file>");
591 return;
592 }
593 if (check_fs_open(argv[0]))
594 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000595 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 inode = string_to_inode(argv[1]);
598 if (!inode)
599 return;
600
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000601 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000603 com_err(argv[0], retval, "while trying to read inode %d",
604 inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605 return;
606 }
607 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000608 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 if (retval) {
610 com_err(argv[0], retval, "while trying to write inode %d",
611 inode);
612 return;
613 }
614}
615
616void do_freei(int argc, char *argv[])
617{
618 ino_t inode;
619
620 if (argc != 2) {
621 com_err(argv[0], 0, "Usage: freei <file>");
622 return;
623 }
624 if (check_fs_open(argv[0]))
625 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000626 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 inode = string_to_inode(argv[1]);
629 if (!inode)
630 return;
631
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000632 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000633 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000634 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
635 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636}
637
638void do_seti(int argc, char *argv[])
639{
640 ino_t inode;
641
642 if (argc != 2) {
643 com_err(argv[0], 0, "Usage: seti <file>");
644 return;
645 }
646 if (check_fs_open(argv[0]))
647 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000648 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650 inode = string_to_inode(argv[1]);
651 if (!inode)
652 return;
653
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000654 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000656 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
657 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658}
659
660void do_testi(int argc, char *argv[])
661{
662 ino_t inode;
663
664 if (argc != 2) {
665 com_err(argv[0], 0, "Usage: testi <file>");
666 return;
667 }
668 if (check_fs_open(argv[0]))
669 return;
Theodore Ts'od61f6172000-05-27 16:04:00 +0000670 if (check_fs_bitmaps(argv[0]))
671 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000672 inode = string_to_inode(argv[1]);
673 if (!inode)
674 return;
675
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000676 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 printf("Inode %ld is marked in use\n", inode);
678 else
679 printf("Inode %ld is not in use\n", inode);
680}
681
682
683void do_freeb(int argc, char *argv[])
684{
685 blk_t block;
686 char *tmp;
687
688 if (argc != 2) {
689 com_err(argv[0], 0, "Usage: freeb <block>");
690 return;
691 }
692 if (check_fs_open(argv[0]))
693 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000694 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000696 block = strtoul(argv[1], &tmp, 0);
697 if (!block || *tmp) {
698 com_err(argv[0], 0, "No block 0");
699 return;
700 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000701 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000702 com_err(argv[0], 0, "Warning: block already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000703 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
704 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705}
706
707void do_setb(int argc, char *argv[])
708{
709 blk_t block;
710 char *tmp;
711
712 if (argc != 2) {
713 com_err(argv[0], 0, "Usage: setb <block>");
714 return;
715 }
716 if (check_fs_open(argv[0]))
717 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000718 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000720 block = strtoul(argv[1], &tmp, 0);
721 if (!block || *tmp) {
722 com_err(argv[0], 0, "No block 0");
723 return;
724 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000725 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 com_err(argv[0], 0, "Warning: block already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000727 ext2fs_mark_block_bitmap(current_fs->block_map,block);
728 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729}
730
731void do_testb(int argc, char *argv[])
732{
733 blk_t block;
734 char *tmp;
735
736 if (argc != 2) {
737 com_err(argv[0], 0, "Usage: testb <block>");
738 return;
739 }
740 if (check_fs_open(argv[0]))
741 return;
Theodore Ts'od61f6172000-05-27 16:04:00 +0000742 if (check_fs_bitmaps(argv[0]))
743 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744 block = strtoul(argv[1], &tmp, 0);
745 if (!block || *tmp) {
746 com_err(argv[0], 0, "No block 0");
747 return;
748 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000749 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000750 printf("Block %d marked in use\n", block);
751 else printf("Block %d not in use\n", block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752}
753
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000754static void modify_u8(char *com, const char *prompt,
755 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000756{
757 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000758 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000759 char *tmp;
760
761 sprintf(buf, format, *val);
762 printf("%30s [%s] ", prompt, buf);
763 fgets(buf, sizeof(buf), stdin);
764 if (buf[strlen (buf) - 1] == '\n')
765 buf[strlen (buf) - 1] = '\0';
766 if (!buf[0])
767 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000768 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769 if (*tmp)
770 com_err(com, 0, "Bad value - %s", buf);
771 else
772 *val = v;
773}
774
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000775static void modify_u16(char *com, const char *prompt,
776 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000777{
778 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000779 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000780 char *tmp;
781
782 sprintf(buf, format, *val);
783 printf("%30s [%s] ", prompt, buf);
784 fgets(buf, sizeof(buf), stdin);
785 if (buf[strlen (buf) - 1] == '\n')
786 buf[strlen (buf) - 1] = '\0';
787 if (!buf[0])
788 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000789 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000790 if (*tmp)
791 com_err(com, 0, "Bad value - %s", buf);
792 else
793 *val = v;
794}
795
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000796static void modify_u32(char *com, const char *prompt,
797 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000798{
799 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000800 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801 char *tmp;
802
803 sprintf(buf, format, *val);
804 printf("%30s [%s] ", prompt, buf);
805 fgets(buf, sizeof(buf), stdin);
806 if (buf[strlen (buf) - 1] == '\n')
807 buf[strlen (buf) - 1] = '\0';
808 if (!buf[0])
809 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000810 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000811 if (*tmp)
812 com_err(com, 0, "Bad value - %s", buf);
813 else
814 *val = v;
815}
816
817
818void do_modify_inode(int argc, char *argv[])
819{
820 struct ext2_inode inode;
821 ino_t inode_num;
822 int i;
823 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000824 unsigned char *frag, *fsize;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825 char buf[80];
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000826 int os = current_fs->super->s_creator_os;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000827 const char *hex_format = "0x%x";
828 const char *octal_format = "0%o";
829 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000830
831 if (argc != 2) {
832 com_err(argv[0], 0, "Usage: modify_inode <file>");
833 return;
834 }
835 if (check_fs_open(argv[0]))
836 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000837 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000839
840 inode_num = string_to_inode(argv[1]);
841 if (!inode_num)
842 return;
843
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000844 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000845 if (retval) {
846 com_err(argv[1], retval, "while trying to read inode %d",
847 inode_num);
848 return;
849 }
850
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000851 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
852 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
853 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
854 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
855 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
856 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
857 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
858 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
859 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
860 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
861 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
862#if 0
863 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
864#endif
865 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000866 if (LINUX_S_ISDIR(inode.i_mode))
867 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
868 else
869 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000870
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000871 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000872 modify_u32(argv[0], "Translator Block",
873 decimal_format, &inode.osd1.hurd1.h_i_translator);
874
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000875 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000876 switch (os) {
877 case EXT2_OS_LINUX:
878 frag = &inode.osd2.linux2.l_i_frag;
879 fsize = &inode.osd2.linux2.l_i_fsize;
880 break;
881 case EXT2_OS_HURD:
882 frag = &inode.osd2.hurd2.h_i_frag;
883 fsize = &inode.osd2.hurd2.h_i_fsize;
884 break;
885 case EXT2_OS_MASIX:
886 frag = &inode.osd2.masix2.m_i_frag;
887 fsize = &inode.osd2.masix2.m_i_fsize;
888 break;
889 default:
890 frag = fsize = 0;
891 }
892 if (frag)
893 modify_u8(argv[0], "Fragment number", decimal_format, frag);
894 if (fsize)
895 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
896
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
898 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000899 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000901 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000902 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000903 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000905 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000907 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000908 if (retval) {
909 com_err(argv[1], retval, "while trying to write inode %d",
910 inode_num);
911 return;
912 }
913}
914
Theodore Ts'o3839e651997-04-26 13:21:57 +0000915void do_change_working_dir(int argc, char *argv[])
916{
917 ino_t inode;
918 int retval;
919
920 if (argc != 2) {
921 com_err(argv[0], 0, "Usage: cd <file>");
922 return;
923 }
924 if (check_fs_open(argv[0]))
925 return;
926
927 inode = string_to_inode(argv[1]);
928 if (!inode)
929 return;
930
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000931 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000932 if (retval) {
933 com_err(argv[1], retval, "");
934 return;
935 }
936 cwd = inode;
937 return;
938}
939
Theodore Ts'o3839e651997-04-26 13:21:57 +0000940void do_print_working_directory(int argc, char *argv[])
941{
942 int retval;
943 char *pathname = NULL;
944
945 if (argc > 1) {
946 com_err(argv[0], 0, "Usage: print_working_directory");
947 return;
948 }
949 if (check_fs_open(argv[0]))
950 return;
951
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000952 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000953 if (retval) {
954 com_err(argv[0], retval,
955 "while trying to get pathname of cwd");
956 }
957 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
958 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000959 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000960 if (retval) {
961 com_err(argv[0], retval,
962 "while trying to get pathname of root");
963 }
964 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
965 free(pathname);
966 return;
967}
968
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000969static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000970{
971 ino_t inode;
972 int retval;
973 ino_t dir;
974 char *dest, *cp, *basename;
975
976 /*
977 * Get the source inode
978 */
979 inode = string_to_inode(sourcename);
980 if (!inode)
981 return;
982 basename = strrchr(sourcename, '/');
983 if (basename)
984 basename++;
985 else
986 basename = sourcename;
987 /*
988 * Figure out the destination. First see if it exists and is
989 * a directory.
990 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000991 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000992 dest = basename;
993 else {
994 /*
995 * OK, it doesn't exist. See if it is
996 * '<dir>/basename' or 'basename'
997 */
998 cp = strrchr(destname, '/');
999 if (cp) {
1000 *cp = 0;
1001 dir = string_to_inode(destname);
1002 if (!dir)
1003 return;
1004 dest = cp+1;
1005 } else {
1006 dir = cwd;
1007 dest = destname;
1008 }
1009 }
1010
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001011 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001012 if (retval)
1013 com_err("make_link", retval, "");
1014 return;
1015}
1016
1017
1018void do_link(int argc, char *argv[])
1019{
1020 if (argc != 3) {
1021 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
1022 return;
1023 }
1024 if (check_fs_open(argv[0]))
1025 return;
1026
1027 make_link(argv[1], argv[2]);
1028}
1029
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001030static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001031{
1032 int retval;
1033 ino_t dir;
1034 char *basename;
1035
1036 basename = strrchr(filename, '/');
1037 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001038 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001039 dir = string_to_inode(filename);
1040 if (!dir)
1041 return;
1042 } else {
1043 dir = cwd;
1044 basename = filename;
1045 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001046 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001047 if (retval)
1048 com_err("unlink_file_by_name", retval, "");
1049 return;
1050}
1051
1052void do_unlink(int argc, char *argv[])
1053{
1054 if (argc != 2) {
1055 com_err(argv[0], 0, "Usage: unlink <pathname>");
1056 return;
1057 }
1058 if (check_fs_open(argv[0]))
1059 return;
1060
1061 unlink_file_by_name(argv[1]);
1062}
1063
1064void do_find_free_block(int argc, char *argv[])
1065{
1066 blk_t free_blk, goal;
1067 errcode_t retval;
1068 char *tmp;
1069
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001070 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
1071 com_err(argv[0], 0, "Usage: find_free_block [goal]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001072 return;
1073 }
1074 if (check_fs_open(argv[0]))
1075 return;
1076
1077 if (argc > 1) {
1078 goal = strtol(argv[1], &tmp, 0);
1079 if (*tmp) {
1080 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1081 return;
1082 }
1083 }
1084 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001085 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001086
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001087 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001088 if (retval)
1089 com_err("ext2fs_new_block", retval, "");
1090 else
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001091 printf("Free block found: %d\n", free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001092
1093}
1094
1095void do_find_free_inode(int argc, char *argv[])
1096{
1097 ino_t free_inode, dir;
1098 int mode;
1099 int retval;
1100 char *tmp;
1101
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001102 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1103 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001104 return;
1105 }
1106 if (check_fs_open(argv[0]))
1107 return;
1108
1109 if (argc > 1) {
1110 dir = strtol(argv[1], &tmp, 0);
1111 if (*tmp) {
1112 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1113 return;
1114 }
1115 }
1116 else
1117 dir = root;
1118 if (argc > 2) {
1119 mode = strtol(argv[2], &tmp, 0);
1120 if (*tmp) {
1121 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1122 return;
1123 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001124 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001125 mode = 010755;
1126
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001127 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001128 if (retval)
1129 com_err("ext2fs_new_inode", retval, "");
1130 else
1131 printf("Free inode found: %ld\n", free_inode);
1132}
1133
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001134static errcode_t copy_file(int fd, ino_t newfile)
1135{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001136 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001137 errcode_t retval;
Theodore Ts'o4a31c481998-03-30 01:27:25 +00001138 unsigned int got, written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001139 char buf[8192];
1140 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001141
Theodore Ts'o5a513841997-10-25 22:41:14 +00001142 retval = ext2fs_file_open(current_fs, newfile,
1143 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001144 if (retval)
1145 return retval;
1146
Theodore Ts'o5a513841997-10-25 22:41:14 +00001147 while (1) {
1148 got = read(fd, buf, sizeof(buf));
1149 if (got == 0)
1150 break;
1151 if (got < 0) {
1152 retval = errno;
1153 goto fail;
1154 }
1155 ptr = buf;
1156 while (got > 0) {
1157 retval = ext2fs_file_write(e2_file, ptr,
1158 got, &written);
1159 if (retval)
1160 goto fail;
1161
1162 got -= written;
1163 ptr += written;
1164 }
1165 }
1166 retval = ext2fs_file_close(e2_file);
1167
1168 return retval;
1169
1170fail:
1171 (void) ext2fs_file_close(e2_file);
1172 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001173}
1174
Theodore Ts'o5a513841997-10-25 22:41:14 +00001175
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001176void do_write(int argc, char *argv[])
1177{
1178 int fd;
1179 struct stat statbuf;
1180 ino_t newfile;
1181 errcode_t retval;
1182 struct ext2_inode inode;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001183 dgrp_t group;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001184
1185 if (check_fs_open(argv[0]))
1186 return;
1187 if (argc != 3) {
1188 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1189 return;
1190 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001191 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001192 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001193 fd = open(argv[1], O_RDONLY);
1194 if (fd < 0) {
1195 com_err(argv[1], fd, "");
1196 return;
1197 }
1198 if (fstat(fd, &statbuf) < 0) {
1199 com_err(argv[1], errno, "");
1200 close(fd);
1201 return;
1202 }
1203
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001204 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001205 if (retval) {
1206 com_err(argv[0], retval, "");
1207 close(fd);
1208 return;
1209 }
Theodore Ts'o5a513841997-10-25 22:41:14 +00001210 group = ext2fs_group_of_ino(current_fs, newfile);
1211 current_fs->group_desc[group].bg_free_inodes_count--;
1212 current_fs->super->s_free_inodes_count--;
1213 ext2fs_mark_super_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001214 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001215 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001216 if (retval) {
1217 com_err(argv[2], retval, "");
1218 close(fd);
1219 return;
1220 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001221 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001222 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001223 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1224 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001225 memset(&inode, 0, sizeof(inode));
1226 inode.i_mode = statbuf.st_mode;
1227 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1228 inode.i_links_count = 1;
1229 inode.i_size = statbuf.st_size;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001230 ext2fs_write_inode(current_fs, newfile, &inode);
1231 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001232 if (retval) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001233 com_err(argv[0], retval, "while trying to write inode %d",
1234 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001235 close(fd);
1236 return;
1237 }
1238 if (LINUX_S_ISREG(inode.i_mode)) {
1239 retval = copy_file(fd, newfile);
1240 if (retval)
1241 com_err("copy_file", retval, "");
1242 }
1243 close(fd);
1244}
1245
1246void do_mknod(int argc, char *argv[])
1247{
1248 unsigned long mode, major, minor, nr;
1249 ino_t newfile;
1250 errcode_t retval;
1251 struct ext2_inode inode;
1252
1253 if (check_fs_open(argv[0]))
1254 return;
1255 if (argc < 3 || argv[2][1]) {
1256 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1257 return;
1258 }
1259 mode = minor = major = 0;
1260 switch (argv[2][0]) {
1261 case 'p':
1262 mode = LINUX_S_IFIFO;
1263 nr = 3;
1264 break;
1265 case 'c':
1266 mode = LINUX_S_IFCHR;
1267 nr = 5;
1268 break;
1269 case 'b':
1270 mode = LINUX_S_IFBLK;
1271 nr = 5;
1272 break;
1273 default:
1274 nr = 0;
1275 }
1276 if (nr == 5) {
1277 major = strtoul(argv[3], argv+3, 0);
1278 minor = strtoul(argv[4], argv+4, 0);
1279 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1280 nr = 0;
1281 }
1282 if (argc != nr) {
1283 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1284 return;
1285 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001286 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001287 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001288 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001289 if (retval) {
1290 com_err(argv[0], retval, "");
1291 return;
1292 }
1293 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001294 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001295 if (retval) {
1296 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001297 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001298 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001299 retval = ext2fs_link(current_fs, cwd,
1300 argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001301 }
1302 if (retval) {
1303 com_err(argv[1], retval, "");
1304 return;
1305 }
1306 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001307 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001308 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001309 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1310 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001311 memset(&inode, 0, sizeof(inode));
1312 inode.i_mode = mode;
1313 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1314 inode.i_block[0] = major*256+minor;
1315 inode.i_links_count = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001316 ext2fs_write_inode(current_fs, newfile, &inode);
1317 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001318 if (retval) {
1319 com_err(argv[0], retval, "while trying to write inode %d", inode);
1320 return;
1321 }
1322}
1323
Theodore Ts'o3839e651997-04-26 13:21:57 +00001324void do_mkdir(int argc, char *argv[])
1325{
1326 char *cp;
1327 ino_t parent;
1328 char *name;
1329 errcode_t retval;
1330
1331 if (check_fs_open(argv[0]))
1332 return;
1333
1334 if (argc != 2) {
1335 com_err(argv[0], 0, "Usage: mkdir <file>");
1336 return;
1337 }
1338
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001339 if (check_fs_read_write(argv[0]))
1340 return;
1341
Theodore Ts'o3839e651997-04-26 13:21:57 +00001342 cp = strrchr(argv[1], '/');
1343 if (cp) {
1344 *cp = 0;
1345 parent = string_to_inode(argv[1]);
1346 if (!parent) {
1347 com_err(argv[1], ENOENT, "");
1348 return;
1349 }
1350 name = cp+1;
1351 } else {
1352 parent = cwd;
1353 name = argv[1];
1354 }
1355
1356
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001357 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001358 if (retval) {
1359 com_err("ext2fs_mkdir", retval, "");
1360 return;
1361 }
1362
1363}
1364
1365void do_rmdir(int argc, char *argv[])
1366{
1367 printf("Unimplemented\n");
1368}
1369
1370
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001371static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1372 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001373{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001374 printf("%d ", *blocknr);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001375 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001376 return 0;
1377}
1378
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001379static void kill_file_by_inode(ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001380{
1381 struct ext2_inode inode_buf;
1382
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001383 ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001384 inode_buf.i_dtime = time(NULL);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001385 ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001386
1387 printf("Kill file by inode %ld\n", inode);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001388 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1389 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001390 printf("\n");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001391 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001392
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001393 ext2fs_mark_bb_dirty(current_fs);
1394 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001395}
1396
1397
1398void do_kill_file(int argc, char *argv[])
1399{
1400 ino_t inode_num;
1401
1402 if (argc != 2) {
1403 com_err(argv[0], 0, "Usage: kill_file <file>");
1404 return;
1405 }
1406 if (check_fs_open(argv[0]))
1407 return;
1408
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001409 if (check_fs_read_write(argv[0]))
1410 return;
1411
Theodore Ts'o3839e651997-04-26 13:21:57 +00001412 inode_num = string_to_inode(argv[1]);
1413 if (!inode_num) {
1414 com_err(argv[0], 0, "Cannot find file");
1415 return;
1416 }
1417 kill_file_by_inode(inode_num);
1418}
1419
1420void do_rm(int argc, char *argv[])
1421{
1422 int retval;
1423 ino_t inode_num;
1424 struct ext2_inode inode;
1425
1426 if (argc != 2) {
1427 com_err(argv[0], 0, "Usage: rm <filename>");
1428 return;
1429 }
1430 if (check_fs_open(argv[0]))
1431 return;
1432
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001433 if (check_fs_read_write(argv[0]))
1434 return;
1435
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001436 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001437 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001438 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001439 return;
1440 }
1441
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001442 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001443 if (retval) {
1444 com_err(argv[0], retval, "while reading file's inode");
1445 return;
1446 }
1447
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001448 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001449 com_err(argv[0], 0, "file is a directory");
1450 return;
1451 }
1452
1453 --inode.i_links_count;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001454 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001455 if (retval) {
1456 com_err(argv[0], retval, "while writing inode");
1457 return;
1458 }
1459
1460 unlink_file_by_name(argv[1]);
1461 if (inode.i_links_count == 0)
1462 kill_file_by_inode(inode_num);
1463}
1464
1465void do_show_debugfs_params(int argc, char *argv[])
1466{
1467 FILE *out = stdout;
1468
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001469 if (current_fs)
1470 fprintf(out, "Open mode: read-%s\n",
1471 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001472 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001473 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001474}
1475
1476void do_expand_dir(int argc, char *argv[])
1477{
1478 ino_t inode;
1479 int retval;
1480
1481 if (argc != 2) {
1482 com_err(argv[0], 0, "Usage: expand_dir <file>");
1483 return;
1484 }
1485 if (check_fs_open(argv[0]))
1486 return;
1487 inode = string_to_inode(argv[1]);
1488 if (!inode)
1489 return;
1490
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001491 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001492 if (retval)
1493 com_err("ext2fs_expand_dir", retval, "");
1494 return;
1495}
1496
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001497void do_features(int argc, char *argv[])
1498{
1499 int i;
1500
1501 if (check_fs_open(argv[0]))
1502 return;
1503
1504 if ((argc != 1) && check_fs_read_write(argv[0]))
1505 return;
1506 for (i=1; i < argc; i++) {
1507 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001508 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001509 com_err(argv[0], 0, "Unknown feature: %s\n",
1510 argv[i]);
1511 else
1512 ext2fs_mark_super_dirty(current_fs);
1513 }
1514 print_features((struct ext2fs_sb *) current_fs->super, stdout);
1515}
1516
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001517static int source_file(const char *cmd_file, int sci_idx)
1518{
1519 FILE *f;
1520 char buf[256];
1521 char *cp;
1522 int exit_status = 0;
1523 int retval;
1524
1525 if (strcmp(cmd_file, "-") == 0)
1526 f = stdin;
1527 else {
1528 f = fopen(cmd_file, "r");
1529 if (!f) {
1530 perror(cmd_file);
1531 exit(1);
1532 }
1533 }
1534 setbuf(stdout, NULL);
1535 setbuf(stderr, NULL);
1536 while (!feof(f)) {
1537 if (fgets(buf, sizeof(buf), f) == NULL)
1538 break;
1539 cp = strchr(buf, '\n');
1540 if (cp)
1541 *cp = 0;
1542 cp = strchr(buf, '\r');
1543 if (cp)
1544 *cp = 0;
1545 printf("debugfs: %s\n", buf);
1546 retval = ss_execute_line(sci_idx, buf);
1547 if (retval) {
1548 ss_perror(sci_idx, retval, buf);
1549 exit_status++;
1550 }
1551 }
1552 return exit_status;
1553}
1554
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001555int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001556{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001557 int retval;
1558 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001559 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00001560 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001561 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001562 char *request = 0;
1563 int exit_status = 0;
1564 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001565 blk_t superblock = 0;
1566 blk_t blocksize = 0;
1567 int catastrophic = 0;
1568 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001569
1570 initialize_ext2_error_table();
Theodore Ts'o818180c1998-06-27 05:11:14 +00001571 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1572 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1573 EXT2FS_VERSION, EXT2FS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001574
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001575 while ((c = getopt (argc, argv, "wcR:f:b:s:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001576 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001577 case 'R':
1578 request = optarg;
1579 break;
1580 case 'f':
1581 cmd_file = optarg;
1582 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001583 case 'w':
1584 open_flags = EXT2_FLAG_RW;
1585 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001586 case 'b':
1587 blocksize = strtoul(optarg, &tmp, 0);
1588 if (*tmp) {
1589 com_err(argv[0], 0,
1590 "Bad block size - %s", optarg);
1591 return 1;
1592 }
1593 break;
1594 case 's':
1595 superblock = strtoul(optarg, &tmp, 0);
1596 if (*tmp) {
1597 com_err(argv[0], 0,
1598 "Bad superblock number - %s", optarg);
1599 return 1;
1600 }
1601 break;
1602 case 'c':
1603 catastrophic = 1;
1604 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001605 case 'V':
1606 /* Print version number and exit */
1607 fprintf(stderr, "\tUsing %s\n",
1608 error_message(EXT2_ET_BASE));
1609 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001610 default:
1611 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001612 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001613 }
1614 }
1615 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001616 open_filesystem(argv[optind], open_flags,
1617 superblock, blocksize, catastrophic);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001618
1619 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1620 &debug_cmds, &retval);
1621 if (retval) {
1622 ss_perror(sci_idx, retval, "creating invocation");
1623 exit(1);
1624 }
1625
1626 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1627 if (retval) {
1628 ss_perror(sci_idx, retval, "adding standard requests");
1629 exit (1);
1630 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001631 if (request) {
1632 retval = 0;
1633 retval = ss_execute_line(sci_idx, request);
1634 if (retval) {
1635 ss_perror(sci_idx, retval, request);
1636 exit_status++;
1637 }
1638 } else if (cmd_file) {
1639 exit_status = source_file(cmd_file, sci_idx);
1640 } else {
1641 ss_listen(sci_idx);
1642 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001643
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001644 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001645 close_filesystem();
1646
Theodore Ts'oe5973042000-01-18 17:58:34 +00001647 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001648}