blob: dde7e74aa0f9c012fd9d0099e6e9c420b6abb208 [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'ob044c2e2001-01-11 15:26:39 +000043ext2_filsys current_fs = NULL;
44ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000045
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'o5dd8f962001-01-01 15:51:50 +0000231static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000232{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000233 int i, j, printed=0;
234__u32 *mask = &s->s_feature_compat, m;
235
236 printf ("Filesystem features:");
237 for (i=0; i <3; i++,mask++) {
238 for (j=0,m=1; j < 32; j++, m<<=1) {
239 if (*mask & m) {
240 fprintf(f, " %s", e2p_feature2string(i, m));
241 printed++;
242 }
243 }
244 }
245 if (printed == 0)
246 printf("(none)");
247 printf("\n");
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000248}
249
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250void do_show_super_stats(int argc, char *argv[])
251{
252 int i;
253 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000254 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000255 int c, header_only = 0;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000256 const char *usage = "Usage: show_super [-h]";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000257
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000258 optind = 0;
259#ifdef HAVE_OPTRESET
260 optreset = 1; /* Makes BSD getopt happy */
261#endif
262 while ((c = getopt (argc, argv, "h")) != EOF) {
263 switch (c) {
264 case 'h':
265 header_only++;
266 break;
267 default:
268 com_err(argv[0], 0, usage);
269 return;
270 }
271 }
272 if (optind != argc) {
273 com_err(argv[0], 0, usage);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274 return;
275 }
276 if (check_fs_open(argv[0]))
277 return;
278 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000279
280 list_super2(current_fs->super, out);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000281
282 if (header_only) {
283 close_pager(out);
284 return;
285 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000286
287 gdp = &current_fs->group_desc[0];
288 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000289 fprintf(out, " Group %2d: block bitmap at %d, "
290 "inode bitmap at %d, "
291 "inode table at %d\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000292 " %d free %s, "
293 "%d free %s, "
294 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000295 i, gdp->bg_block_bitmap,
296 gdp->bg_inode_bitmap, gdp->bg_inode_table,
297 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000298 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000299 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000300 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000301 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000302 gdp->bg_used_dirs_count != 1 ? "directories"
303 : "directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000304 close_pager(out);
305}
306
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000307void do_dirty_filesys(int argc, char **argv)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000308{
309 if (check_fs_open(argv[0]))
310 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000311 if (check_fs_read_write(argv[0]))
312 return;
313
314 if (argv[1] && !strcmp(argv[1], "-clean"))
315 current_fs->super->s_state |= EXT2_VALID_FS;
316 else
317 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000318 ext2fs_mark_super_dirty(current_fs);
319}
320
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321struct list_blocks_struct {
322 FILE *f;
323 int total;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000324 blk_t first_block, last_block;
325 int first_bcnt, last_bcnt;
326 int first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000327};
328
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000329static void finish_range(struct list_blocks_struct *lb)
330{
331 if (lb->first_block == 0)
332 return;
333 if (lb->first)
334 lb->first = 0;
335 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000336 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000337 if (lb->first_block == lb->last_block)
338 fprintf(lb->f, "(%d):%d", lb->first_bcnt, lb->first_block);
339 else
340 fprintf(lb->f, "(%d-%d):%d-%d", lb->first_bcnt,
341 lb->last_bcnt, lb->first_block, lb->last_block);
342 lb->first_block = 0;
343}
344
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000345static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
346 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000347{
348 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
349
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000350 lb->total++;
351 if (blockcnt >= 0) {
352 /*
353 * See if we can add on to the existing range (if it exists)
354 */
355 if (lb->first_block &&
356 (lb->last_block+1 == *blocknr) &&
357 (lb->last_bcnt+1 == blockcnt)) {
358 lb->last_block = *blocknr;
359 lb->last_bcnt = blockcnt;
360 return 0;
361 }
362 /*
363 * Start a new range.
364 */
365 finish_range(lb);
366 lb->first_block = lb->last_block = *blocknr;
367 lb->first_bcnt = lb->last_bcnt = blockcnt;
368 return 0;
369 }
370 /*
371 * Not a normal block. Always force a new range.
372 */
373 finish_range(lb);
374 if (lb->first)
375 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000376 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000377 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000378 if (blockcnt == -1)
379 fprintf(lb->f, "(IND):%d", *blocknr);
380 else if (blockcnt == -2)
381 fprintf(lb->f, "(DIND):%d", *blocknr);
382 else if (blockcnt == -3)
383 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 return 0;
385}
386
387
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000388static void dump_blocks(FILE *f, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000389{
390 struct list_blocks_struct lb;
391
392 fprintf(f, "BLOCKS:\n");
393 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000394 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000396 lb.first = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000397 ext2fs_block_iterate(current_fs, inode, 0, NULL,
398 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000399 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000400 if (lb.total)
401 fprintf(f, "\nTOTAL: %d\n", lb.total);
402 fprintf(f,"\n");
403}
404
405
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000406static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000408 const char *i_type;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000410 char frag, fsize;
411 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412
413 out = open_pager();
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000414 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
415 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
416 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
417 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
418 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
419 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
420 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000421 else i_type = "bad type";
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000422 fprintf(out, "Inode: %u Type: %s ", inode_num, i_type);
Theodore Ts'o14197172000-07-05 17:48:34 +0000423 fprintf(out, "Mode: %04o Flags: 0x%x Generation: %u\n",
Theodore Ts'ob41d3601999-06-25 15:32:37 +0000424 inode.i_mode & 0777, inode.i_flags, inode.i_generation);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000425 fprintf(out, "User: %5d Group: %5d Size: ",
426 inode.i_uid, inode.i_gid);
427 if (LINUX_S_ISDIR(inode.i_mode))
428 fprintf(out, "%d\n", inode.i_size);
429 else {
430 __u64 i_size = (inode.i_size |
431 ((unsigned long long)inode.i_size_high << 32));
432
433 fprintf(out, "%lld\n", i_size);
434 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000435 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000436 fprintf(out,
437 "File ACL: %d Directory ACL: %d Translator: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000438 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000439 inode.osd1.hurd1.h_i_translator);
440 else
441 fprintf(out, "File ACL: %d Directory ACL: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000442 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000443 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444 inode.i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000445 switch (os) {
446 case EXT2_OS_LINUX:
447 frag = inode.osd2.linux2.l_i_frag;
448 fsize = inode.osd2.linux2.l_i_fsize;
449 break;
450 case EXT2_OS_HURD:
451 frag = inode.osd2.hurd2.h_i_frag;
452 fsize = inode.osd2.hurd2.h_i_fsize;
453 break;
454 case EXT2_OS_MASIX:
455 frag = inode.osd2.masix2.m_i_frag;
456 fsize = inode.osd2.masix2.m_i_fsize;
457 break;
458 default:
459 frag = fsize = 0;
460 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000461 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000462 inode.i_faddr, frag, fsize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000463 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000464 time_to_string(inode.i_ctime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000465 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000466 time_to_string(inode.i_atime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000467 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000468 time_to_string(inode.i_mtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 if (inode.i_dtime)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000470 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000471 time_to_string(inode.i_dtime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000472 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000473 fprintf(out, "Fast_link_dest: %.*s\n",
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000474 (int) inode.i_size, (char *)inode.i_block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 else
476 dump_blocks(out, inode_num);
477 close_pager(out);
478}
479
480
481void do_stat(int argc, char *argv[])
482{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000483 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 struct ext2_inode inode_buf;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000485 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000486
487 if (argc != 2) {
488 com_err(argv[0], 0, "Usage: stat <file>");
489 return;
490 }
491 if (check_fs_open(argv[0]))
492 return;
493 inode = string_to_inode(argv[1]);
494 if (!inode)
495 return;
496
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000497 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498 if (retval)
499 {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000500 com_err(argv[0], retval, "while trying to read inode %d", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501 return;
502 }
503
504 dump_inode(inode,inode_buf);
505 return;
506}
507
508void do_chroot(int argc, char *argv[])
509{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000510 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 int retval;
512
513 if (argc != 2) {
514 com_err(argv[0], 0, "Usage: chroot <file>");
515 return;
516 }
517 if (check_fs_open(argv[0]))
518 return;
519 inode = string_to_inode(argv[1]);
520 if (!inode)
521 return;
522
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000523 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 if (retval) {
525 com_err(argv[1], retval, "");
526 return;
527 }
528 root = inode;
529}
530
531void do_clri(int argc, char *argv[])
532{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000533 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534 int retval;
535 struct ext2_inode inode_buf;
536
537 if (argc != 2) {
538 com_err(argv[0], 0, "Usage: clri <file>");
539 return;
540 }
541 if (check_fs_open(argv[0]))
542 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000543 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 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) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000551 com_err(argv[0], retval, "while trying to read inode %d",
552 inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000553 return;
554 }
555 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000556 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557 if (retval) {
558 com_err(argv[0], retval, "while trying to write inode %d",
559 inode);
560 return;
561 }
562}
563
564void do_freei(int argc, char *argv[])
565{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000566 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567
568 if (argc != 2) {
569 com_err(argv[0], 0, "Usage: freei <file>");
570 return;
571 }
572 if (check_fs_open(argv[0]))
573 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000574 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 inode = string_to_inode(argv[1]);
577 if (!inode)
578 return;
579
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000580 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000582 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
583 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584}
585
586void do_seti(int argc, char *argv[])
587{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000588 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589
590 if (argc != 2) {
591 com_err(argv[0], 0, "Usage: seti <file>");
592 return;
593 }
594 if (check_fs_open(argv[0]))
595 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000596 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 inode = string_to_inode(argv[1]);
599 if (!inode)
600 return;
601
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000602 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000604 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
605 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606}
607
608void do_testi(int argc, char *argv[])
609{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000610 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611
612 if (argc != 2) {
613 com_err(argv[0], 0, "Usage: testi <file>");
614 return;
615 }
616 if (check_fs_open(argv[0]))
617 return;
Theodore Ts'od61f6172000-05-27 16:04:00 +0000618 if (check_fs_bitmaps(argv[0]))
619 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000620 inode = string_to_inode(argv[1]);
621 if (!inode)
622 return;
623
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000624 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000625 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000627 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628}
629
630
631void do_freeb(int argc, char *argv[])
632{
633 blk_t block;
634 char *tmp;
635
636 if (argc != 2) {
637 com_err(argv[0], 0, "Usage: freeb <block>");
638 return;
639 }
640 if (check_fs_open(argv[0]))
641 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000642 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000644 block = strtoul(argv[1], &tmp, 0);
645 if (!block || *tmp) {
646 com_err(argv[0], 0, "No block 0");
647 return;
648 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000649 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000650 com_err(argv[0], 0, "Warning: block already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000651 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
652 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653}
654
655void do_setb(int argc, char *argv[])
656{
657 blk_t block;
658 char *tmp;
659
660 if (argc != 2) {
661 com_err(argv[0], 0, "Usage: setb <block>");
662 return;
663 }
664 if (check_fs_open(argv[0]))
665 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000666 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 block = strtoul(argv[1], &tmp, 0);
669 if (!block || *tmp) {
670 com_err(argv[0], 0, "No block 0");
671 return;
672 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000673 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 com_err(argv[0], 0, "Warning: block already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000675 ext2fs_mark_block_bitmap(current_fs->block_map,block);
676 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677}
678
679void do_testb(int argc, char *argv[])
680{
681 blk_t block;
682 char *tmp;
683
684 if (argc != 2) {
685 com_err(argv[0], 0, "Usage: testb <block>");
686 return;
687 }
688 if (check_fs_open(argv[0]))
689 return;
Theodore Ts'od61f6172000-05-27 16:04:00 +0000690 if (check_fs_bitmaps(argv[0]))
691 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000692 block = strtoul(argv[1], &tmp, 0);
693 if (!block || *tmp) {
694 com_err(argv[0], 0, "No block 0");
695 return;
696 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000697 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000698 printf("Block %d marked in use\n", block);
699 else printf("Block %d not in use\n", block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700}
701
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000702static void modify_u8(char *com, const char *prompt,
703 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704{
705 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000706 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707 char *tmp;
708
709 sprintf(buf, format, *val);
710 printf("%30s [%s] ", prompt, buf);
711 fgets(buf, sizeof(buf), stdin);
712 if (buf[strlen (buf) - 1] == '\n')
713 buf[strlen (buf) - 1] = '\0';
714 if (!buf[0])
715 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000716 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 if (*tmp)
718 com_err(com, 0, "Bad value - %s", buf);
719 else
720 *val = v;
721}
722
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000723static void modify_u16(char *com, const char *prompt,
724 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725{
726 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000727 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 char *tmp;
729
730 sprintf(buf, format, *val);
731 printf("%30s [%s] ", prompt, buf);
732 fgets(buf, sizeof(buf), stdin);
733 if (buf[strlen (buf) - 1] == '\n')
734 buf[strlen (buf) - 1] = '\0';
735 if (!buf[0])
736 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000737 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000738 if (*tmp)
739 com_err(com, 0, "Bad value - %s", buf);
740 else
741 *val = v;
742}
743
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000744static void modify_u32(char *com, const char *prompt,
745 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000746{
747 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000748 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749 char *tmp;
750
751 sprintf(buf, format, *val);
752 printf("%30s [%s] ", prompt, buf);
753 fgets(buf, sizeof(buf), stdin);
754 if (buf[strlen (buf) - 1] == '\n')
755 buf[strlen (buf) - 1] = '\0';
756 if (!buf[0])
757 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000758 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000759 if (*tmp)
760 com_err(com, 0, "Bad value - %s", buf);
761 else
762 *val = v;
763}
764
765
766void do_modify_inode(int argc, char *argv[])
767{
768 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000769 ext2_ino_t inode_num;
770 int i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000771 errcode_t retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000772 unsigned char *frag, *fsize;
773 char buf[80];
774 int os = current_fs->super->s_creator_os;
775 const char *hex_format = "0x%x";
776 const char *octal_format = "0%o";
777 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778
779 if (argc != 2) {
780 com_err(argv[0], 0, "Usage: modify_inode <file>");
781 return;
782 }
783 if (check_fs_open(argv[0]))
784 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000785 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000786 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000787
788 inode_num = string_to_inode(argv[1]);
789 if (!inode_num)
790 return;
791
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000792 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793 if (retval) {
794 com_err(argv[1], retval, "while trying to read inode %d",
795 inode_num);
796 return;
797 }
798
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000799 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
800 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
801 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
802 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
803 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
804 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
805 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
806 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
807 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
808 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
809 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000810 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000811#if 0
812 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
813#endif
814 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000815 if (LINUX_S_ISDIR(inode.i_mode))
816 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
817 else
818 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000819
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000820 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000821 modify_u32(argv[0], "Translator Block",
822 decimal_format, &inode.osd1.hurd1.h_i_translator);
823
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000824 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000825 switch (os) {
826 case EXT2_OS_LINUX:
827 frag = &inode.osd2.linux2.l_i_frag;
828 fsize = &inode.osd2.linux2.l_i_fsize;
829 break;
830 case EXT2_OS_HURD:
831 frag = &inode.osd2.hurd2.h_i_frag;
832 fsize = &inode.osd2.hurd2.h_i_fsize;
833 break;
834 case EXT2_OS_MASIX:
835 frag = &inode.osd2.masix2.m_i_frag;
836 fsize = &inode.osd2.masix2.m_i_fsize;
837 break;
838 default:
839 frag = fsize = 0;
840 }
841 if (frag)
842 modify_u8(argv[0], "Fragment number", decimal_format, frag);
843 if (fsize)
844 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
845
Theodore Ts'o3839e651997-04-26 13:21:57 +0000846 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
847 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000848 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000849 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000850 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000851 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000852 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000853 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000854 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000856 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000857 if (retval) {
858 com_err(argv[1], retval, "while trying to write inode %d",
859 inode_num);
860 return;
861 }
862}
863
Theodore Ts'o3839e651997-04-26 13:21:57 +0000864void do_change_working_dir(int argc, char *argv[])
865{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000866 ext2_ino_t inode;
867 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000868
869 if (argc != 2) {
870 com_err(argv[0], 0, "Usage: cd <file>");
871 return;
872 }
873 if (check_fs_open(argv[0]))
874 return;
875
876 inode = string_to_inode(argv[1]);
877 if (!inode)
878 return;
879
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000880 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000881 if (retval) {
882 com_err(argv[1], retval, "");
883 return;
884 }
885 cwd = inode;
886 return;
887}
888
Theodore Ts'o3839e651997-04-26 13:21:57 +0000889void do_print_working_directory(int argc, char *argv[])
890{
891 int retval;
892 char *pathname = NULL;
893
894 if (argc > 1) {
895 com_err(argv[0], 0, "Usage: print_working_directory");
896 return;
897 }
898 if (check_fs_open(argv[0]))
899 return;
900
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000901 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000902 if (retval) {
903 com_err(argv[0], retval,
904 "while trying to get pathname of cwd");
905 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000906 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000907 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000908 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 if (retval) {
910 com_err(argv[0], retval,
911 "while trying to get pathname of root");
912 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000913 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914 free(pathname);
915 return;
916}
917
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000918static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000919{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000920 ext2_ino_t inode;
921 int retval;
922 ext2_ino_t dir;
923 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000924
925 /*
926 * Get the source inode
927 */
928 inode = string_to_inode(sourcename);
929 if (!inode)
930 return;
931 basename = strrchr(sourcename, '/');
932 if (basename)
933 basename++;
934 else
935 basename = sourcename;
936 /*
937 * Figure out the destination. First see if it exists and is
938 * a directory.
939 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000940 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000941 dest = basename;
942 else {
943 /*
944 * OK, it doesn't exist. See if it is
945 * '<dir>/basename' or 'basename'
946 */
947 cp = strrchr(destname, '/');
948 if (cp) {
949 *cp = 0;
950 dir = string_to_inode(destname);
951 if (!dir)
952 return;
953 dest = cp+1;
954 } else {
955 dir = cwd;
956 dest = destname;
957 }
958 }
959
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000960 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000961 if (retval)
962 com_err("make_link", retval, "");
963 return;
964}
965
966
967void do_link(int argc, char *argv[])
968{
969 if (argc != 3) {
970 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
971 return;
972 }
973 if (check_fs_open(argv[0]))
974 return;
975
976 make_link(argv[1], argv[2]);
977}
978
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000979static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000980{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000981 int retval;
982 ext2_ino_t dir;
983 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000984
985 basename = strrchr(filename, '/');
986 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000987 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000988 dir = string_to_inode(filename);
989 if (!dir)
990 return;
991 } else {
992 dir = cwd;
993 basename = filename;
994 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000995 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000996 if (retval)
997 com_err("unlink_file_by_name", retval, "");
998 return;
999}
1000
1001void do_unlink(int argc, char *argv[])
1002{
1003 if (argc != 2) {
1004 com_err(argv[0], 0, "Usage: unlink <pathname>");
1005 return;
1006 }
1007 if (check_fs_open(argv[0]))
1008 return;
1009
1010 unlink_file_by_name(argv[1]);
1011}
1012
1013void do_find_free_block(int argc, char *argv[])
1014{
1015 blk_t free_blk, goal;
1016 errcode_t retval;
1017 char *tmp;
1018
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001019 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
1020 com_err(argv[0], 0, "Usage: find_free_block [goal]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001021 return;
1022 }
1023 if (check_fs_open(argv[0]))
1024 return;
1025
1026 if (argc > 1) {
1027 goal = strtol(argv[1], &tmp, 0);
1028 if (*tmp) {
1029 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1030 return;
1031 }
1032 }
1033 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001034 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001035
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001036 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001037 if (retval)
1038 com_err("ext2fs_new_block", retval, "");
1039 else
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001040 printf("Free block found: %d\n", free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001041
1042}
1043
1044void do_find_free_inode(int argc, char *argv[])
1045{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001046 ext2_ino_t free_inode, dir;
1047 int mode;
1048 int retval;
1049 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001050
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001051 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1052 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001053 return;
1054 }
1055 if (check_fs_open(argv[0]))
1056 return;
1057
1058 if (argc > 1) {
1059 dir = strtol(argv[1], &tmp, 0);
1060 if (*tmp) {
1061 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1062 return;
1063 }
1064 }
1065 else
1066 dir = root;
1067 if (argc > 2) {
1068 mode = strtol(argv[2], &tmp, 0);
1069 if (*tmp) {
1070 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1071 return;
1072 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001073 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001074 mode = 010755;
1075
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001076 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001077 if (retval)
1078 com_err("ext2fs_new_inode", retval, "");
1079 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001080 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001081}
1082
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001083static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001084{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001085 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001086 errcode_t retval;
Theodore Ts'o4a31c481998-03-30 01:27:25 +00001087 unsigned int got, written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001088 char buf[8192];
1089 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001090
Theodore Ts'o5a513841997-10-25 22:41:14 +00001091 retval = ext2fs_file_open(current_fs, newfile,
1092 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001093 if (retval)
1094 return retval;
1095
Theodore Ts'o5a513841997-10-25 22:41:14 +00001096 while (1) {
1097 got = read(fd, buf, sizeof(buf));
1098 if (got == 0)
1099 break;
1100 if (got < 0) {
1101 retval = errno;
1102 goto fail;
1103 }
1104 ptr = buf;
1105 while (got > 0) {
1106 retval = ext2fs_file_write(e2_file, ptr,
1107 got, &written);
1108 if (retval)
1109 goto fail;
1110
1111 got -= written;
1112 ptr += written;
1113 }
1114 }
1115 retval = ext2fs_file_close(e2_file);
1116
1117 return retval;
1118
1119fail:
1120 (void) ext2fs_file_close(e2_file);
1121 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001122}
1123
Theodore Ts'o5a513841997-10-25 22:41:14 +00001124
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001125void do_write(int argc, char *argv[])
1126{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001127 int fd;
1128 struct stat statbuf;
1129 ext2_ino_t newfile;
1130 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001131 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001132 dgrp_t group;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001133
1134 if (check_fs_open(argv[0]))
1135 return;
1136 if (argc != 3) {
1137 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1138 return;
1139 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001140 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001141 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001142 fd = open(argv[1], O_RDONLY);
1143 if (fd < 0) {
1144 com_err(argv[1], fd, "");
1145 return;
1146 }
1147 if (fstat(fd, &statbuf) < 0) {
1148 com_err(argv[1], errno, "");
1149 close(fd);
1150 return;
1151 }
1152
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001153 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001154 if (retval) {
1155 com_err(argv[0], retval, "");
1156 close(fd);
1157 return;
1158 }
Theodore Ts'o5a513841997-10-25 22:41:14 +00001159 group = ext2fs_group_of_ino(current_fs, newfile);
1160 current_fs->group_desc[group].bg_free_inodes_count--;
1161 current_fs->super->s_free_inodes_count--;
1162 ext2fs_mark_super_dirty(current_fs);
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001163 printf("Allocated inode: %u\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001164 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001165 if (retval) {
1166 com_err(argv[2], retval, "");
1167 close(fd);
1168 return;
1169 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001170 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001171 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001172 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1173 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001174 memset(&inode, 0, sizeof(inode));
1175 inode.i_mode = statbuf.st_mode;
1176 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1177 inode.i_links_count = 1;
1178 inode.i_size = statbuf.st_size;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001179 ext2fs_write_inode(current_fs, newfile, &inode);
1180 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001181 if (retval) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001182 com_err(argv[0], retval, "while trying to write inode %d",
1183 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001184 close(fd);
1185 return;
1186 }
1187 if (LINUX_S_ISREG(inode.i_mode)) {
1188 retval = copy_file(fd, newfile);
1189 if (retval)
1190 com_err("copy_file", retval, "");
1191 }
1192 close(fd);
1193}
1194
1195void do_mknod(int argc, char *argv[])
1196{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001197 unsigned long mode, major, minor, nr;
1198 ext2_ino_t newfile;
1199 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001200 struct ext2_inode inode;
1201
1202 if (check_fs_open(argv[0]))
1203 return;
1204 if (argc < 3 || argv[2][1]) {
1205 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1206 return;
1207 }
1208 mode = minor = major = 0;
1209 switch (argv[2][0]) {
1210 case 'p':
1211 mode = LINUX_S_IFIFO;
1212 nr = 3;
1213 break;
1214 case 'c':
1215 mode = LINUX_S_IFCHR;
1216 nr = 5;
1217 break;
1218 case 'b':
1219 mode = LINUX_S_IFBLK;
1220 nr = 5;
1221 break;
1222 default:
1223 nr = 0;
1224 }
1225 if (nr == 5) {
1226 major = strtoul(argv[3], argv+3, 0);
1227 minor = strtoul(argv[4], argv+4, 0);
1228 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1229 nr = 0;
1230 }
1231 if (argc != nr) {
1232 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1233 return;
1234 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001235 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001236 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001237 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001238 if (retval) {
1239 com_err(argv[0], retval, "");
1240 return;
1241 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001242 printf("Allocated inode: %u\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001243 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001244 if (retval) {
1245 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001246 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001247 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001248 retval = ext2fs_link(current_fs, cwd,
1249 argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001250 }
1251 if (retval) {
1252 com_err(argv[1], retval, "");
1253 return;
1254 }
1255 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001256 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001257 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001258 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1259 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001260 memset(&inode, 0, sizeof(inode));
1261 inode.i_mode = mode;
1262 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1263 inode.i_block[0] = major*256+minor;
1264 inode.i_links_count = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001265 ext2fs_write_inode(current_fs, newfile, &inode);
1266 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001267 if (retval) {
1268 com_err(argv[0], retval, "while trying to write inode %d", inode);
1269 return;
1270 }
1271}
1272
Theodore Ts'o3839e651997-04-26 13:21:57 +00001273void do_mkdir(int argc, char *argv[])
1274{
1275 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001276 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001277 char *name;
1278 errcode_t retval;
1279
1280 if (check_fs_open(argv[0]))
1281 return;
1282
1283 if (argc != 2) {
1284 com_err(argv[0], 0, "Usage: mkdir <file>");
1285 return;
1286 }
1287
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001288 if (check_fs_read_write(argv[0]))
1289 return;
1290
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291 cp = strrchr(argv[1], '/');
1292 if (cp) {
1293 *cp = 0;
1294 parent = string_to_inode(argv[1]);
1295 if (!parent) {
1296 com_err(argv[1], ENOENT, "");
1297 return;
1298 }
1299 name = cp+1;
1300 } else {
1301 parent = cwd;
1302 name = argv[1];
1303 }
1304
1305
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001306 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001307 if (retval) {
1308 com_err("ext2fs_mkdir", retval, "");
1309 return;
1310 }
1311
1312}
1313
1314void do_rmdir(int argc, char *argv[])
1315{
1316 printf("Unimplemented\n");
1317}
1318
1319
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001320static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1321 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001322{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001323 printf("%d ", *blocknr);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001324 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001325 return 0;
1326}
1327
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001328static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001329{
1330 struct ext2_inode inode_buf;
1331
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001332 ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001333 inode_buf.i_dtime = time(NULL);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001334 ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001335
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001336 printf("Kill file by inode %u\n", inode);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001337 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1338 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001339 printf("\n");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001340 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001341
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001342 ext2fs_mark_bb_dirty(current_fs);
1343 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001344}
1345
1346
1347void do_kill_file(int argc, char *argv[])
1348{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001349 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001350
1351 if (argc != 2) {
1352 com_err(argv[0], 0, "Usage: kill_file <file>");
1353 return;
1354 }
1355 if (check_fs_open(argv[0]))
1356 return;
1357
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001358 if (check_fs_read_write(argv[0]))
1359 return;
1360
Theodore Ts'o3839e651997-04-26 13:21:57 +00001361 inode_num = string_to_inode(argv[1]);
1362 if (!inode_num) {
1363 com_err(argv[0], 0, "Cannot find file");
1364 return;
1365 }
1366 kill_file_by_inode(inode_num);
1367}
1368
1369void do_rm(int argc, char *argv[])
1370{
1371 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001372 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001373 struct ext2_inode inode;
1374
1375 if (argc != 2) {
1376 com_err(argv[0], 0, "Usage: rm <filename>");
1377 return;
1378 }
1379 if (check_fs_open(argv[0]))
1380 return;
1381
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001382 if (check_fs_read_write(argv[0]))
1383 return;
1384
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001385 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001386 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001387 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001388 return;
1389 }
1390
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001391 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001392 if (retval) {
1393 com_err(argv[0], retval, "while reading file's inode");
1394 return;
1395 }
1396
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001397 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001398 com_err(argv[0], 0, "file is a directory");
1399 return;
1400 }
1401
1402 --inode.i_links_count;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001403 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001404 if (retval) {
1405 com_err(argv[0], retval, "while writing inode");
1406 return;
1407 }
1408
1409 unlink_file_by_name(argv[1]);
1410 if (inode.i_links_count == 0)
1411 kill_file_by_inode(inode_num);
1412}
1413
1414void do_show_debugfs_params(int argc, char *argv[])
1415{
1416 FILE *out = stdout;
1417
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001418 if (current_fs)
1419 fprintf(out, "Open mode: read-%s\n",
1420 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001421 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001422 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001423}
1424
1425void do_expand_dir(int argc, char *argv[])
1426{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001427 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001428 int retval;
1429
1430 if (argc != 2) {
1431 com_err(argv[0], 0, "Usage: expand_dir <file>");
1432 return;
1433 }
1434 if (check_fs_open(argv[0]))
1435 return;
1436 inode = string_to_inode(argv[1]);
1437 if (!inode)
1438 return;
1439
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001440 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001441 if (retval)
1442 com_err("ext2fs_expand_dir", retval, "");
1443 return;
1444}
1445
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001446void do_features(int argc, char *argv[])
1447{
1448 int i;
1449
1450 if (check_fs_open(argv[0]))
1451 return;
1452
1453 if ((argc != 1) && check_fs_read_write(argv[0]))
1454 return;
1455 for (i=1; i < argc; i++) {
1456 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001457 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001458 com_err(argv[0], 0, "Unknown feature: %s\n",
1459 argv[i]);
1460 else
1461 ext2fs_mark_super_dirty(current_fs);
1462 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001463 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001464}
1465
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001466static int source_file(const char *cmd_file, int sci_idx)
1467{
1468 FILE *f;
1469 char buf[256];
1470 char *cp;
1471 int exit_status = 0;
1472 int retval;
1473
1474 if (strcmp(cmd_file, "-") == 0)
1475 f = stdin;
1476 else {
1477 f = fopen(cmd_file, "r");
1478 if (!f) {
1479 perror(cmd_file);
1480 exit(1);
1481 }
1482 }
1483 setbuf(stdout, NULL);
1484 setbuf(stderr, NULL);
1485 while (!feof(f)) {
1486 if (fgets(buf, sizeof(buf), f) == NULL)
1487 break;
1488 cp = strchr(buf, '\n');
1489 if (cp)
1490 *cp = 0;
1491 cp = strchr(buf, '\r');
1492 if (cp)
1493 *cp = 0;
1494 printf("debugfs: %s\n", buf);
1495 retval = ss_execute_line(sci_idx, buf);
1496 if (retval) {
1497 ss_perror(sci_idx, retval, buf);
1498 exit_status++;
1499 }
1500 }
1501 return exit_status;
1502}
1503
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001504int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001505{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001506 int retval;
1507 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001508 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 +00001509 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001510 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001511 char *request = 0;
1512 int exit_status = 0;
1513 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001514 blk_t superblock = 0;
1515 blk_t blocksize = 0;
1516 int catastrophic = 0;
1517 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001518
1519 initialize_ext2_error_table();
Theodore Ts'o818180c1998-06-27 05:11:14 +00001520 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1521 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1522 EXT2FS_VERSION, EXT2FS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001523
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001524 while ((c = getopt (argc, argv, "wcR:f:b:s:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001525 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001526 case 'R':
1527 request = optarg;
1528 break;
1529 case 'f':
1530 cmd_file = optarg;
1531 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001532 case 'w':
1533 open_flags = EXT2_FLAG_RW;
1534 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001535 case 'b':
1536 blocksize = strtoul(optarg, &tmp, 0);
1537 if (*tmp) {
1538 com_err(argv[0], 0,
1539 "Bad block size - %s", optarg);
1540 return 1;
1541 }
1542 break;
1543 case 's':
1544 superblock = strtoul(optarg, &tmp, 0);
1545 if (*tmp) {
1546 com_err(argv[0], 0,
1547 "Bad superblock number - %s", optarg);
1548 return 1;
1549 }
1550 break;
1551 case 'c':
1552 catastrophic = 1;
1553 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001554 case 'V':
1555 /* Print version number and exit */
1556 fprintf(stderr, "\tUsing %s\n",
1557 error_message(EXT2_ET_BASE));
1558 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001559 default:
1560 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001561 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001562 }
1563 }
1564 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001565 open_filesystem(argv[optind], open_flags,
1566 superblock, blocksize, catastrophic);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001567
1568 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1569 &debug_cmds, &retval);
1570 if (retval) {
1571 ss_perror(sci_idx, retval, "creating invocation");
1572 exit(1);
1573 }
1574
1575 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1576 if (retval) {
1577 ss_perror(sci_idx, retval, "adding standard requests");
1578 exit (1);
1579 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001580 if (request) {
1581 retval = 0;
1582 retval = ss_execute_line(sci_idx, request);
1583 if (retval) {
1584 ss_perror(sci_idx, retval, request);
1585 exit_status++;
1586 }
1587 } else if (cmd_file) {
1588 exit_status = source_file(cmd_file, sci_idx);
1589 } else {
1590 ss_listen(sci_idx);
1591 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001592
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001593 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001594 close_filesystem();
1595
Theodore Ts'oe5973042000-01-18 17:58:34 +00001596 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001597}