blob: 6d18ef96dc00b19197d6b72ca0b0a5c629190243 [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
Theodore Ts'o50e1e101997-04-26 13:58:21 +000023#ifdef HAVE_ERRNO_H
24#include <errno.h>
25#endif
26#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <sys/types.h>
28#include <sys/stat.h>
29
30#include "et/com_err.h"
31#include "ss/ss.h"
32#include "debugfs.h"
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000033#include "uuid/uuid.h"
Theodore Ts'of68aa411999-10-26 14:20:22 +000034#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000035
Theodore Ts'oea822ee2005-03-20 18:03:58 -050036#include <ext2fs/ext2_ext_attr.h>
37
Theodore Ts'o818180c1998-06-27 05:11:14 +000038#include "../version.h"
39
Theodore Ts'o3839e651997-04-26 13:21:57 +000040extern ss_request_table debug_cmds;
41
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000042ext2_filsys current_fs = NULL;
43ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000044
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000045static void open_filesystem(char *device, int open_flags, blk_t superblock,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040046 blk_t blocksize, int catastrophic,
47 char *data_filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +000048{
49 int retval;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040050 io_channel data_io = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000051
52 if (superblock != 0 && blocksize == 0) {
53 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
54 current_fs = NULL;
55 return;
56 }
57
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040058 if (data_filename) {
59 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
60 com_err(device, 0,
61 "The -d option is only valid when reading an e2image file");
62 current_fs = NULL;
63 return;
64 }
65 retval = unix_io_manager->open(data_filename, 0, &data_io);
66 if (retval) {
67 com_err(data_filename, 0, "while opening data source");
68 current_fs = NULL;
69 return;
70 }
71 }
72
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000073 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
74 com_err(device, 0,
75 "opening read-only because of catastrophic mode");
76 open_flags &= ~EXT2_FLAG_RW;
77 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000078
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000079 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000080 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 if (retval) {
82 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000083 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 return;
85 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000086
87 if (catastrophic)
88 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
89 else {
90 retval = ext2fs_read_inode_bitmap(current_fs);
91 if (retval) {
92 com_err(device, retval, "while reading inode bitmap");
93 goto errout;
94 }
95 retval = ext2fs_read_block_bitmap(current_fs);
96 if (retval) {
97 com_err(device, retval, "while reading block bitmap");
98 goto errout;
99 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100 }
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400101
102 if (data_io) {
103 retval = ext2fs_set_data_io(current_fs, data_io);
104 if (retval) {
105 com_err(device, retval,
106 "while setting data source");
107 goto errout;
108 }
109 }
110
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 root = cwd = EXT2_ROOT_INO;
112 return;
113
114errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000115 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 if (retval)
117 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000118 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119}
120
121void do_open_filesys(int argc, char **argv)
122{
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000123 const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500124 int c, err;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000125 int catastrophic = 0;
126 blk_t superblock = 0;
127 blk_t blocksize = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400128 int open_flags = 0;
Theodore Ts'ob0d17e02004-11-29 17:35:58 -0500129 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000130
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400131 reset_getopt();
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400132 while ((c = getopt (argc, argv, "iwfcb:s:d:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000133 switch (c) {
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000134 case 'i':
135 open_flags |= EXT2_FLAG_IMAGE_FILE;
136 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000138 open_flags |= EXT2_FLAG_RW;
139 break;
140 case 'f':
141 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000142 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000143 case 'c':
144 catastrophic = 1;
145 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400146 case 'd':
147 data_filename = optarg;
148 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000149 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500150 blocksize = parse_ulong(optarg, argv[0],
151 "block size", &err);
152 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000153 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000154 break;
155 case 's':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500156 superblock = parse_ulong(optarg, argv[0],
157 "superblock number", &err);
158 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000159 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000160 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000161 default:
162 com_err(argv[0], 0, usage);
163 return;
164 }
165 }
166 if (optind != argc-1) {
167 com_err(argv[0], 0, usage);
168 return;
169 }
170 if (check_fs_not_open(argv[0]))
171 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000172 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400173 superblock, blocksize, catastrophic,
174 data_filename);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000175}
176
177void do_lcd(int argc, char **argv)
178{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500179 if (common_args_process(argc, argv, 2, 2, "lcd",
180 "<native dir>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000181 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000182
183 if (chdir(argv[1]) == -1) {
184 com_err(argv[0], errno,
185 "while trying to change native directory to %s",
186 argv[1]);
187 return;
188 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189}
190
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000191static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000192{
193 int retval;
194
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000195 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
196 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197 if (retval)
198 com_err("ext2fs_write_inode_bitmap", retval, "");
199 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000200 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
201 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 if (retval)
203 com_err("ext2fs_write_block_bitmap", retval, "");
204 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000205 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 if (retval)
207 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000208 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 return;
210}
211
212void do_close_filesys(int argc, char **argv)
213{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500214 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000215 return;
216 close_filesystem();
217}
218
219void do_init_filesys(int argc, char **argv)
220{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000221 struct ext2_super_block param;
222 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500223 int err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000224
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500225 if (common_args_process(argc, argv, 3, 3, "initialize",
226 "<device> <blocksize>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000227 return;
228
229 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400230 param.s_blocks_count = parse_ulong(argv[2], argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500231 "blocks count", &err);
232 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000234 retval = ext2fs_initialize(argv[1], 0, &param,
235 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 if (retval) {
237 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000238 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 return;
240 }
241 root = cwd = EXT2_ROOT_INO;
242 return;
243}
244
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000245static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000246{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000247 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000248 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000249
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000250 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000251 for (i=0; i <3; i++,mask++) {
252 for (j=0,m=1; j < 32; j++, m<<=1) {
253 if (*mask & m) {
254 fprintf(f, " %s", e2p_feature2string(i, m));
255 printed++;
256 }
257 }
258 }
259 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000260 fputs("(none)", f);
261 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000262}
263
Theodore Ts'o3839e651997-04-26 13:21:57 +0000264void do_show_super_stats(int argc, char *argv[])
265{
Theodore Ts'o54434922003-12-07 01:28:50 -0500266 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000267 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000268 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000269 int c, header_only = 0;
Theodore Ts'o34be9602002-07-15 16:56:41 -0400270 int numdirs = 0;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000271 const char *usage = "Usage: show_super [-h]";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400273 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000274 while ((c = getopt (argc, argv, "h")) != EOF) {
275 switch (c) {
276 case 'h':
277 header_only++;
278 break;
279 default:
280 com_err(argv[0], 0, usage);
281 return;
282 }
283 }
284 if (optind != argc) {
285 com_err(argv[0], 0, usage);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 return;
287 }
288 if (check_fs_open(argv[0]))
289 return;
290 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000291
292 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400293 for (i=0; i < current_fs->group_desc_count; i++)
294 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
295 fprintf(out, "Directories: %d\n", numdirs);
296
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000297 if (header_only) {
298 close_pager(out);
299 return;
300 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000301
302 gdp = &current_fs->group_desc[0];
303 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000304 fprintf(out, " Group %2d: block bitmap at %d, "
305 "inode bitmap at %d, "
306 "inode table at %d\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000307 " %d free %s, "
308 "%d free %s, "
309 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000310 i, gdp->bg_block_bitmap,
311 gdp->bg_inode_bitmap, gdp->bg_inode_table,
312 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000313 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000314 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000315 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000316 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000317 gdp->bg_used_dirs_count != 1 ? "directories"
318 : "directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000319 close_pager(out);
320}
321
Theodore Ts'o54434922003-12-07 01:28:50 -0500322void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
323 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000324{
325 if (check_fs_open(argv[0]))
326 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000327 if (check_fs_read_write(argv[0]))
328 return;
329
330 if (argv[1] && !strcmp(argv[1], "-clean"))
331 current_fs->super->s_state |= EXT2_VALID_FS;
332 else
333 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000334 ext2fs_mark_super_dirty(current_fs);
335}
336
Theodore Ts'o3839e651997-04-26 13:21:57 +0000337struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700338 FILE *f;
339 e2_blkcnt_t total;
340 blk_t first_block, last_block;
341 e2_blkcnt_t first_bcnt, last_bcnt;
342 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000343};
344
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000345static void finish_range(struct list_blocks_struct *lb)
346{
347 if (lb->first_block == 0)
348 return;
349 if (lb->first)
350 lb->first = 0;
351 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000352 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000353 if (lb->first_block == lb->last_block)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100354 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000355 else
Theodore Ts'oe8981882001-11-30 11:51:30 +0100356 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000357 lb->last_bcnt, lb->first_block, lb->last_block);
358 lb->first_block = 0;
359}
360
Theodore Ts'o54434922003-12-07 01:28:50 -0500361static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
362 blk_t *blocknr, e2_blkcnt_t blockcnt,
363 blk_t ref_block EXT2FS_ATTR((unused)),
364 int ref_offset EXT2FS_ATTR((unused)),
365 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366{
367 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
368
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000369 lb->total++;
370 if (blockcnt >= 0) {
371 /*
372 * See if we can add on to the existing range (if it exists)
373 */
374 if (lb->first_block &&
375 (lb->last_block+1 == *blocknr) &&
376 (lb->last_bcnt+1 == blockcnt)) {
377 lb->last_block = *blocknr;
378 lb->last_bcnt = blockcnt;
379 return 0;
380 }
381 /*
382 * Start a new range.
383 */
384 finish_range(lb);
385 lb->first_block = lb->last_block = *blocknr;
386 lb->first_bcnt = lb->last_bcnt = blockcnt;
387 return 0;
388 }
389 /*
390 * Not a normal block. Always force a new range.
391 */
392 finish_range(lb);
393 if (lb->first)
394 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000395 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000396 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000397 if (blockcnt == -1)
398 fprintf(lb->f, "(IND):%d", *blocknr);
399 else if (blockcnt == -2)
400 fprintf(lb->f, "(DIND):%d", *blocknr);
401 else if (blockcnt == -3)
402 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 return 0;
404}
405
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500406void dump_xattr_string(FILE *out, const unsigned char *str, int len)
407{
408 int printable = 1;
409 int i;
410
411 /* check is string printable? */
412 for (i = 0; i < len; i++)
413 if (!isprint(str[i])) {
414 printable = 0;
415 break;
416 }
417
418 for (i = 0; i < len; i++)
419 if (printable)
420 fprintf(out, "%c", str[i]);
421 else
422 fprintf(out, "%02x ", str[i]);
423}
424
425void internal_dump_inode_extra(FILE *out, const char *prefix,
426 ext2_ino_t inode_num, struct ext2_inode_large *inode)
427{
428 struct ext2_ext_attr_entry *entry;
429 __u32 *magic;
430 char *start, *end;
431 int storage_size;
432 int i;
433
434 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
435 EXT2_GOOD_OLD_INODE_SIZE) {
436 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
437 inode->i_extra_isize);
438 return;
439 }
440 storage_size = EXT2_INODE_SIZE(current_fs->super) -
441 EXT2_GOOD_OLD_INODE_SIZE -
442 inode->i_extra_isize;
443 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
444 inode->i_extra_isize);
445 if (*magic == EXT2_EXT_ATTR_MAGIC) {
446 fprintf(out, "Extended attributes stored in inode body: \n");
447 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
448 start = (char *) magic + sizeof(__u32);
449 entry = (struct ext2_ext_attr_entry *) start;
450 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
451 struct ext2_ext_attr_entry *next =
452 EXT2_EXT_ATTR_NEXT(entry);
453 if (entry->e_value_size > storage_size ||
454 (char *) next >= end) {
455 fprintf(out, "invalid EA entry in inode\n");
456 return;
457 }
458 fprintf(out, " ");
459 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
460 entry->e_name_len);
461 fprintf(out, " = \"");
462 dump_xattr_string(out, start + entry->e_value_offs,
463 entry->e_value_size);
464 fprintf(out, "\" (%d)\n", entry->e_value_size);
465 entry = next;
466 }
467 }
468}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000470static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471{
472 struct list_blocks_struct lb;
473
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000474 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000476 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000478 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700479 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000480 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000481 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482 if (lb.total)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100483 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000484 fprintf(f,"\n");
485}
486
487
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000488void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000489 ext2_ino_t inode_num, struct ext2_inode *inode,
490 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000492 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000493 char frag, fsize;
494 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000495
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000496 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
497 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
498 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
499 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
500 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
501 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
502 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000503 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000504 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
505 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
506 prefix,
507 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
508 fprintf(out, "%sUser: %5d Group: %5d Size: ",
509 prefix, inode->i_uid, inode->i_gid);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700510 if (LINUX_S_ISREG(inode->i_mode)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000511 __u64 i_size = (inode->i_size |
512 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700513
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000514 fprintf(out, "%lld\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700515 } else
516 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000517 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000518 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000519 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
520 prefix,
521 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
522 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000523 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000524 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
525 prefix,
526 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
527 fprintf(out, "%sLinks: %d Blockcount: %d\n",
528 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000529 switch (os) {
530 case EXT2_OS_LINUX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000531 frag = inode->osd2.linux2.l_i_frag;
532 fsize = inode->osd2.linux2.l_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000533 break;
534 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000535 frag = inode->osd2.hurd2.h_i_frag;
536 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000537 break;
538 case EXT2_OS_MASIX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000539 frag = inode->osd2.masix2.m_i_frag;
540 fsize = inode->osd2.masix2.m_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000541 break;
542 default:
543 frag = fsize = 0;
544 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000545 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
546 prefix, inode->i_faddr, frag, fsize);
547 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
548 time_to_string(inode->i_ctime));
549 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
550 time_to_string(inode->i_atime));
551 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
552 time_to_string(inode->i_mtime));
553 if (inode->i_dtime)
554 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
555 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500556 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
557 internal_dump_inode_extra(out, prefix, inode_num,
558 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500559 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000560 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
561 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500562 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
563 int major, minor;
564 const char *devnote;
565
566 if (inode->i_block[0]) {
567 major = (inode->i_block[0] >> 8) & 255;
568 minor = inode->i_block[0] & 255;
569 devnote = "";
570 } else {
571 major = (inode->i_block[1] & 0xfff00) >> 8;
572 minor = ((inode->i_block[1] & 0xff) |
573 ((inode->i_block[1] >> 12) & 0xfff00));
574 devnote = "(New-style) ";
575 }
576 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
577 devnote, major, minor, major, minor);
578 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000579 else if (do_dump_blocks)
580 dump_blocks(out, prefix, inode_num);
581}
582
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500583static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000584{
585 FILE *out;
586
587 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500588 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589 close_pager(out);
590}
591
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592void do_stat(int argc, char *argv[])
593{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000594 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500595 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500597 inode_buf = (struct ext2_inode *)
598 malloc(EXT2_INODE_SIZE(current_fs->super));
599 if (!inode_buf) {
600 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500602 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500604 if (common_inode_args_process(argc, argv, &inode, 0)) {
605 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500606 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500607 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500609 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
610 EXT2_INODE_SIZE(current_fs->super))) {
611 free(inode_buf);
612 return;
613 }
614
615 dump_inode(inode, inode_buf);
616 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 return;
618}
619
620void do_chroot(int argc, char *argv[])
621{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000622 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623 int retval;
624
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500625 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 return;
627
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000628 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 if (retval) {
630 com_err(argv[1], retval, "");
631 return;
632 }
633 root = inode;
634}
635
636void do_clri(int argc, char *argv[])
637{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000638 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 struct ext2_inode inode_buf;
640
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500641 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642 return;
643
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500644 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500647 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649}
650
651void do_freei(int argc, char *argv[])
652{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000653 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500655 if (common_inode_args_process(argc, argv, &inode,
656 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657 return;
658
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000659 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000661 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
662 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663}
664
665void do_seti(int argc, char *argv[])
666{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000667 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500669 if (common_inode_args_process(argc, argv, &inode,
670 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 return;
672
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000673 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000675 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
676 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677}
678
679void do_testi(int argc, char *argv[])
680{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000681 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500683 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684 return;
685
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000686 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000687 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000689 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690}
691
Theodore Ts'o3839e651997-04-26 13:21:57 +0000692void do_freeb(int argc, char *argv[])
693{
694 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500695 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000696
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500697 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000699 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500701 while (count-- > 0) {
702 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
703 com_err(argv[0], 0, "Warning: block %d already clear",
704 block);
705 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
706 block++;
707 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000708 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709}
710
711void do_setb(int argc, char *argv[])
712{
713 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500714 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500716 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 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'oe1018ee2002-01-03 04:55:25 -0500720 while (count-- > 0) {
721 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
722 com_err(argv[0], 0, "Warning: block %d already set",
723 block);
724 ext2fs_mark_block_bitmap(current_fs->block_map,block);
725 block++;
726 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000727 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728}
729
730void do_testb(int argc, char *argv[])
731{
732 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500733 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000734
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500735 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000736 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500737 while (count-- > 0) {
738 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
739 printf("Block %d marked in use\n", block);
740 else
741 printf("Block %d not in use\n", block);
742 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744}
745
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000746static void modify_u8(char *com, const char *prompt,
747 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000748{
749 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000750 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000751 char *tmp;
752
753 sprintf(buf, format, *val);
754 printf("%30s [%s] ", prompt, buf);
755 fgets(buf, sizeof(buf), stdin);
756 if (buf[strlen (buf) - 1] == '\n')
757 buf[strlen (buf) - 1] = '\0';
758 if (!buf[0])
759 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000760 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761 if (*tmp)
762 com_err(com, 0, "Bad value - %s", buf);
763 else
764 *val = v;
765}
766
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000767static void modify_u16(char *com, const char *prompt,
768 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769{
770 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000771 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000772 char *tmp;
773
774 sprintf(buf, format, *val);
775 printf("%30s [%s] ", prompt, buf);
776 fgets(buf, sizeof(buf), stdin);
777 if (buf[strlen (buf) - 1] == '\n')
778 buf[strlen (buf) - 1] = '\0';
779 if (!buf[0])
780 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000781 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000782 if (*tmp)
783 com_err(com, 0, "Bad value - %s", buf);
784 else
785 *val = v;
786}
787
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000788static void modify_u32(char *com, const char *prompt,
789 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000790{
791 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000792 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793 char *tmp;
794
795 sprintf(buf, format, *val);
796 printf("%30s [%s] ", prompt, buf);
797 fgets(buf, sizeof(buf), stdin);
798 if (buf[strlen (buf) - 1] == '\n')
799 buf[strlen (buf) - 1] = '\0';
800 if (!buf[0])
801 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000802 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000803 if (*tmp)
804 com_err(com, 0, "Bad value - %s", buf);
805 else
806 *val = v;
807}
808
809
810void do_modify_inode(int argc, char *argv[])
811{
812 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000813 ext2_ino_t inode_num;
814 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000815 unsigned char *frag, *fsize;
816 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500817 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000818 const char *hex_format = "0x%x";
819 const char *octal_format = "0%o";
820 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500822 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000823 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000824
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500825 os = current_fs->super->s_creator_os;
826
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500827 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000828 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000830 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
831 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
832 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
833 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
834 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
835 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
836 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
837 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
838 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
839 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
840 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000841 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000842#if 0
843 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
844#endif
845 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000846 if (LINUX_S_ISDIR(inode.i_mode))
847 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
848 else
849 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000850
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000851 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000852 modify_u32(argv[0], "Translator Block",
853 decimal_format, &inode.osd1.hurd1.h_i_translator);
854
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000855 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000856 switch (os) {
857 case EXT2_OS_LINUX:
858 frag = &inode.osd2.linux2.l_i_frag;
859 fsize = &inode.osd2.linux2.l_i_fsize;
860 break;
861 case EXT2_OS_HURD:
862 frag = &inode.osd2.hurd2.h_i_frag;
863 fsize = &inode.osd2.hurd2.h_i_fsize;
864 break;
865 case EXT2_OS_MASIX:
866 frag = &inode.osd2.masix2.m_i_frag;
867 fsize = &inode.osd2.masix2.m_i_fsize;
868 break;
869 default:
870 frag = fsize = 0;
871 }
872 if (frag)
873 modify_u8(argv[0], "Fragment number", decimal_format, frag);
874 if (fsize)
875 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
876
Theodore Ts'o3839e651997-04-26 13:21:57 +0000877 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
878 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000879 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000880 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000881 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000882 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000883 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000884 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000885 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500887 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000889}
890
Theodore Ts'o3839e651997-04-26 13:21:57 +0000891void do_change_working_dir(int argc, char *argv[])
892{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000893 ext2_ino_t inode;
894 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500896 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897 return;
898
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000899 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 if (retval) {
901 com_err(argv[1], retval, "");
902 return;
903 }
904 cwd = inode;
905 return;
906}
907
Theodore Ts'o3839e651997-04-26 13:21:57 +0000908void do_print_working_directory(int argc, char *argv[])
909{
910 int retval;
911 char *pathname = NULL;
912
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500913 if (common_args_process(argc, argv, 1, 1,
914 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000915 return;
916
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000917 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 if (retval) {
919 com_err(argv[0], retval,
920 "while trying to get pathname of cwd");
921 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000922 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000923 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000924 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000925 if (retval) {
926 com_err(argv[0], retval,
927 "while trying to get pathname of root");
928 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000929 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 free(pathname);
931 return;
932}
933
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500934/*
935 * Given a mode, return the ext2 file type
936 */
937static int ext2_file_type(unsigned int mode)
938{
939 if (LINUX_S_ISREG(mode))
940 return EXT2_FT_REG_FILE;
941
942 if (LINUX_S_ISDIR(mode))
943 return EXT2_FT_DIR;
944
945 if (LINUX_S_ISCHR(mode))
946 return EXT2_FT_CHRDEV;
947
948 if (LINUX_S_ISBLK(mode))
949 return EXT2_FT_BLKDEV;
950
951 if (LINUX_S_ISLNK(mode))
952 return EXT2_FT_SYMLINK;
953
954 if (LINUX_S_ISFIFO(mode))
955 return EXT2_FT_FIFO;
956
957 if (LINUX_S_ISSOCK(mode))
958 return EXT2_FT_SOCK;
959
960 return 0;
961}
962
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000963static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000964{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500965 ext2_ino_t ino;
966 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000967 int retval;
968 ext2_ino_t dir;
969 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000970
971 /*
972 * Get the source inode
973 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500974 ino = string_to_inode(sourcename);
975 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976 return;
977 basename = strrchr(sourcename, '/');
978 if (basename)
979 basename++;
980 else
981 basename = sourcename;
982 /*
983 * Figure out the destination. First see if it exists and is
984 * a directory.
985 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000986 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000987 dest = basename;
988 else {
989 /*
990 * OK, it doesn't exist. See if it is
991 * '<dir>/basename' or 'basename'
992 */
993 cp = strrchr(destname, '/');
994 if (cp) {
995 *cp = 0;
996 dir = string_to_inode(destname);
997 if (!dir)
998 return;
999 dest = cp+1;
1000 } else {
1001 dir = cwd;
1002 dest = destname;
1003 }
1004 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001005
1006 if (debugfs_read_inode(ino, &inode, sourcename))
1007 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001008
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001009 retval = ext2fs_link(current_fs, dir, dest, ino,
1010 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001011 if (retval)
1012 com_err("make_link", retval, "");
1013 return;
1014}
1015
1016
1017void do_link(int argc, char *argv[])
1018{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001019 if (common_args_process(argc, argv, 3, 3, "link",
1020 "<source file> <dest_name>", CHECK_FS_RW))
1021 return;
1022
1023 make_link(argv[1], argv[2]);
1024}
1025
1026static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001027 int blockcnt EXT2FS_ATTR((unused)),
1028 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001029{
1030 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001031
1032 block = *blocknr;
1033 ext2fs_block_alloc_stats(fs, block, +1);
1034 return 0;
1035}
1036
1037void do_undel(int argc, char *argv[])
1038{
1039 ext2_ino_t ino;
1040 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001041
1042 if (common_args_process(argc, argv, 3, 3, "undelete",
1043 "<inode_num> <dest_name>",
1044 CHECK_FS_RW | CHECK_FS_BITMAPS))
1045 return;
1046
1047 ino = string_to_inode(argv[1]);
1048 if (!ino)
1049 return;
1050
1051 if (debugfs_read_inode(ino, &inode, argv[1]))
1052 return;
1053
1054 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1055 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001056 return;
1057 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001058
1059 /*
1060 * XXX this function doesn't handle changing the links count on the
1061 * parent directory when undeleting a directory.
1062 */
1063 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1064 inode.i_dtime = 0;
1065
1066 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001067 return;
1068
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001069 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1070 mark_blocks_proc, NULL);
1071
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001072 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001073
Theodore Ts'o3839e651997-04-26 13:21:57 +00001074 make_link(argv[1], argv[2]);
1075}
1076
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001077static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001078{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001079 int retval;
1080 ext2_ino_t dir;
1081 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001082
1083 basename = strrchr(filename, '/');
1084 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001085 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001086 dir = string_to_inode(filename);
1087 if (!dir)
1088 return;
1089 } else {
1090 dir = cwd;
1091 basename = filename;
1092 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001093 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001094 if (retval)
1095 com_err("unlink_file_by_name", retval, "");
1096 return;
1097}
1098
1099void do_unlink(int argc, char *argv[])
1100{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001101 if (common_args_process(argc, argv, 2, 2, "link",
1102 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001103 return;
1104
1105 unlink_file_by_name(argv[1]);
1106}
1107
1108void do_find_free_block(int argc, char *argv[])
1109{
1110 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001111 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001112 errcode_t retval;
1113 char *tmp;
1114
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001115 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1116 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001117 return;
1118 }
1119 if (check_fs_open(argv[0]))
1120 return;
1121
1122 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001123 count = strtol(argv[1],&tmp,0);
1124 if (*tmp) {
1125 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1126 return;
1127 }
1128 } else
1129 count = 1;
1130
1131 if (argc > 2) {
1132 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001133 if (*tmp) {
1134 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1135 return;
1136 }
1137 }
1138 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001139 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001140
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001141 printf("Free blocks found: ");
1142 free_blk = goal - 1;
1143 while (count-- > 0) {
1144 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1145 &free_blk);
1146 if (retval) {
1147 com_err("ext2fs_new_block", retval, "");
1148 return;
1149 } else
1150 printf("%d ", free_blk);
1151 }
1152 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001153}
1154
1155void do_find_free_inode(int argc, char *argv[])
1156{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001157 ext2_ino_t free_inode, dir;
1158 int mode;
1159 int retval;
1160 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001161
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001162 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1163 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001164 return;
1165 }
1166 if (check_fs_open(argv[0]))
1167 return;
1168
1169 if (argc > 1) {
1170 dir = strtol(argv[1], &tmp, 0);
1171 if (*tmp) {
1172 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1173 return;
1174 }
1175 }
1176 else
1177 dir = root;
1178 if (argc > 2) {
1179 mode = strtol(argv[2], &tmp, 0);
1180 if (*tmp) {
1181 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1182 return;
1183 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001184 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001185 mode = 010755;
1186
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001187 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001188 if (retval)
1189 com_err("ext2fs_new_inode", retval, "");
1190 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001191 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001192}
1193
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001194static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001195{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001196 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001197 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001198 int got;
1199 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001200 char buf[8192];
1201 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001202
Theodore Ts'o5a513841997-10-25 22:41:14 +00001203 retval = ext2fs_file_open(current_fs, newfile,
1204 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001205 if (retval)
1206 return retval;
1207
Theodore Ts'o5a513841997-10-25 22:41:14 +00001208 while (1) {
1209 got = read(fd, buf, sizeof(buf));
1210 if (got == 0)
1211 break;
1212 if (got < 0) {
1213 retval = errno;
1214 goto fail;
1215 }
1216 ptr = buf;
1217 while (got > 0) {
1218 retval = ext2fs_file_write(e2_file, ptr,
1219 got, &written);
1220 if (retval)
1221 goto fail;
1222
1223 got -= written;
1224 ptr += written;
1225 }
1226 }
1227 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001228 return retval;
1229
1230fail:
1231 (void) ext2fs_file_close(e2_file);
1232 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001233}
1234
Theodore Ts'o5a513841997-10-25 22:41:14 +00001235
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001236void do_write(int argc, char *argv[])
1237{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001238 int fd;
1239 struct stat statbuf;
1240 ext2_ino_t newfile;
1241 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001242 struct ext2_inode inode;
1243
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001244 if (common_args_process(argc, argv, 3, 3, "write",
1245 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001246 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001247
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001248 fd = open(argv[1], O_RDONLY);
1249 if (fd < 0) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001250 com_err(argv[1], errno, "");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001251 return;
1252 }
1253 if (fstat(fd, &statbuf) < 0) {
1254 com_err(argv[1], errno, "");
1255 close(fd);
1256 return;
1257 }
1258
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001259 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1260 if (retval == 0) {
1261 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1262 close(fd);
1263 return;
1264 }
1265
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001266 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001267 if (retval) {
1268 com_err(argv[0], retval, "");
1269 close(fd);
1270 return;
1271 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001272 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001273 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1274 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001275 if (retval == EXT2_ET_DIR_NO_SPACE) {
1276 retval = ext2fs_expand_dir(current_fs, cwd);
1277 if (retval) {
1278 com_err(argv[0], retval, "while expanding directory");
1279 return;
1280 }
1281 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1282 EXT2_FT_REG_FILE);
1283 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001284 if (retval) {
1285 com_err(argv[2], retval, "");
1286 close(fd);
1287 return;
1288 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001289 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001290 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001291 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001292 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001293 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001294 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1295 inode.i_links_count = 1;
1296 inode.i_size = statbuf.st_size;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001297 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001298 close(fd);
1299 return;
1300 }
1301 if (LINUX_S_ISREG(inode.i_mode)) {
1302 retval = copy_file(fd, newfile);
1303 if (retval)
1304 com_err("copy_file", retval, "");
1305 }
1306 close(fd);
1307}
1308
1309void do_mknod(int argc, char *argv[])
1310{
Theodore Ts'o54434922003-12-07 01:28:50 -05001311 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001312 ext2_ino_t newfile;
1313 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001314 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001315 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001316
1317 if (check_fs_open(argv[0]))
1318 return;
1319 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001320 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001321 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1322 return;
1323 }
1324 mode = minor = major = 0;
1325 switch (argv[2][0]) {
1326 case 'p':
1327 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001328 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001329 nr = 3;
1330 break;
1331 case 'c':
1332 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001333 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001334 nr = 5;
1335 break;
1336 case 'b':
1337 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001338 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001339 nr = 5;
1340 break;
1341 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001342 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001343 nr = 0;
1344 }
1345 if (nr == 5) {
1346 major = strtoul(argv[3], argv+3, 0);
1347 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001348 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001349 nr = 0;
1350 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001351 if (argc != nr)
1352 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001353 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001354 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001355 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001356 if (retval) {
1357 com_err(argv[0], retval, "");
1358 return;
1359 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001360 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001361 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001362 if (retval == EXT2_ET_DIR_NO_SPACE) {
1363 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001364 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001365 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001366 return;
1367 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001368 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1369 filetype);
1370 }
1371 if (retval) {
1372 com_err(argv[1], retval, "");
1373 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001374 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001375 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001376 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001377 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1378 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001379 memset(&inode, 0, sizeof(inode));
1380 inode.i_mode = mode;
1381 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001382 if ((major < 256) && (minor < 256)) {
1383 inode.i_block[0] = major*256+minor;
1384 inode.i_block[1] = 0;
1385 } else {
1386 inode.i_block[0] = 0;
1387 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1388 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001389 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001390 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001391 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001392}
1393
Theodore Ts'o3839e651997-04-26 13:21:57 +00001394void do_mkdir(int argc, char *argv[])
1395{
1396 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001397 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001398 char *name;
1399 errcode_t retval;
1400
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001401 if (common_args_process(argc, argv, 2, 2, "mkdir",
1402 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001403 return;
1404
Theodore Ts'o3839e651997-04-26 13:21:57 +00001405 cp = strrchr(argv[1], '/');
1406 if (cp) {
1407 *cp = 0;
1408 parent = string_to_inode(argv[1]);
1409 if (!parent) {
1410 com_err(argv[1], ENOENT, "");
1411 return;
1412 }
1413 name = cp+1;
1414 } else {
1415 parent = cwd;
1416 name = argv[1];
1417 }
1418
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001419try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001420 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001421 if (retval == EXT2_ET_DIR_NO_SPACE) {
1422 retval = ext2fs_expand_dir(current_fs, parent);
1423 if (retval) {
1424 com_err("argv[0]", retval, "while expanding directory");
1425 return;
1426 }
1427 goto try_again;
1428 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001429 if (retval) {
1430 com_err("ext2fs_mkdir", retval, "");
1431 return;
1432 }
1433
1434}
1435
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001436static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001437 int blockcnt EXT2FS_ATTR((unused)),
1438 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001439{
Theodore Ts'o34436892001-12-22 13:06:02 -05001440 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001441
1442 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001443 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001444 return 0;
1445}
1446
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001447static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001448{
1449 struct ext2_inode inode_buf;
1450
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001451 if (debugfs_read_inode(inode, &inode_buf, 0))
1452 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001453 inode_buf.i_dtime = time(NULL);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001454 if (debugfs_write_inode(inode, &inode_buf, 0))
1455 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001456 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1457 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001458
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001459 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1460 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001461 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001462 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1463 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001464}
1465
1466
1467void do_kill_file(int argc, char *argv[])
1468{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001469 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001470
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001471 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001472 return;
1473
Theodore Ts'o3839e651997-04-26 13:21:57 +00001474 kill_file_by_inode(inode_num);
1475}
1476
1477void do_rm(int argc, char *argv[])
1478{
1479 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001480 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001481 struct ext2_inode inode;
1482
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001483 if (common_args_process(argc, argv, 2, 2, "rm",
1484 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001485 return;
1486
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001487 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001488 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001489 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001490 return;
1491 }
1492
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001493 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001494 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001495
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001496 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001497 com_err(argv[0], 0, "file is a directory");
1498 return;
1499 }
1500
1501 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001502 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001503 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001504
1505 unlink_file_by_name(argv[1]);
1506 if (inode.i_links_count == 0)
1507 kill_file_by_inode(inode_num);
1508}
1509
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001510struct rd_struct {
1511 ext2_ino_t parent;
1512 int empty;
1513};
1514
Theodore Ts'o54434922003-12-07 01:28:50 -05001515static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1516 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001517 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001518 int offset EXT2FS_ATTR((unused)),
1519 int blocksize EXT2FS_ATTR((unused)),
1520 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001521 void *private)
1522{
1523 struct rd_struct *rds = (struct rd_struct *) private;
1524
1525 if (dirent->inode == 0)
1526 return 0;
1527 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1528 return 0;
1529 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1530 (dirent->name[1] == '.')) {
1531 rds->parent = dirent->inode;
1532 return 0;
1533 }
1534 rds->empty = 0;
1535 return 0;
1536}
1537
1538void do_rmdir(int argc, char *argv[])
1539{
1540 int retval;
1541 ext2_ino_t inode_num;
1542 struct ext2_inode inode;
1543 struct rd_struct rds;
1544
1545 if (common_args_process(argc, argv, 2, 2, "rmdir",
1546 "<filename>", CHECK_FS_RW))
1547 return;
1548
1549 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1550 if (retval) {
1551 com_err(argv[0], retval, "while trying to resolve filename");
1552 return;
1553 }
1554
1555 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1556 return;
1557
1558 if (!LINUX_S_ISDIR(inode.i_mode)) {
1559 com_err(argv[0], 0, "file is not a directory");
1560 return;
1561 }
1562
1563 rds.parent = 0;
1564 rds.empty = 1;
1565
1566 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1567 0, rmdir_proc, &rds);
1568 if (retval) {
1569 com_err(argv[0], retval, "while iterating over directory");
1570 return;
1571 }
1572 if (rds.empty == 0) {
1573 com_err(argv[0], 0, "directory not empty");
1574 return;
1575 }
1576
1577 inode.i_links_count = 0;
1578 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1579 return;
1580
1581 unlink_file_by_name(argv[1]);
1582 kill_file_by_inode(inode_num);
1583
1584 if (rds.parent) {
1585 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1586 return;
1587 if (inode.i_links_count > 1)
1588 inode.i_links_count--;
1589 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1590 return;
1591 }
1592}
1593
Theodore Ts'o54434922003-12-07 01:28:50 -05001594void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1595 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001596{
1597 FILE *out = stdout;
1598
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001599 if (current_fs)
1600 fprintf(out, "Open mode: read-%s\n",
1601 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001602 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001603 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001604}
1605
1606void do_expand_dir(int argc, char *argv[])
1607{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001608 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001609 int retval;
1610
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001611 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001612 return;
1613
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001614 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001615 if (retval)
1616 com_err("ext2fs_expand_dir", retval, "");
1617 return;
1618}
1619
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001620void do_features(int argc, char *argv[])
1621{
1622 int i;
1623
1624 if (check_fs_open(argv[0]))
1625 return;
1626
1627 if ((argc != 1) && check_fs_read_write(argv[0]))
1628 return;
1629 for (i=1; i < argc; i++) {
1630 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001631 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001632 com_err(argv[0], 0, "Unknown feature: %s\n",
1633 argv[i]);
1634 else
1635 ext2fs_mark_super_dirty(current_fs);
1636 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001637 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001638}
1639
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001640void do_bmap(int argc, char *argv[])
1641{
1642 ext2_ino_t ino;
1643 blk_t blk, pblk;
1644 int err;
1645 errcode_t errcode;
1646
1647 if (common_args_process(argc, argv, 3, 3, argv[0],
1648 "<file> logical_blk", 0))
1649 return;
1650
1651 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001652 if (!ino)
1653 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001654 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1655
1656 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1657 if (errcode) {
1658 com_err("argv[0]", errcode,
1659 "while mapping logical block %d\n", blk);
1660 return;
1661 }
1662 printf("%d\n", pblk);
1663}
1664
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001665void do_imap(int argc, char *argv[])
1666{
1667 ext2_ino_t ino;
1668 unsigned long group, block, block_nr, offset;
1669
1670 if (common_args_process(argc, argv, 2, 2, argv[0],
1671 "<file>", 0))
1672 return;
1673 ino = string_to_inode(argv[1]);
1674 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001675 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001676
1677 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1678 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1679 EXT2_INODE_SIZE(current_fs->super);
1680 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1681 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001682 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001683 group);
1684 return;
1685 }
1686 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1687 block;
1688 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1689
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001690 printf("Inode %d is part of block group %lu\n"
1691 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001692 block_nr, offset);
1693
1694}
1695
1696
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001697
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001698static int source_file(const char *cmd_file, int sci_idx)
1699{
1700 FILE *f;
1701 char buf[256];
1702 char *cp;
1703 int exit_status = 0;
1704 int retval;
1705
1706 if (strcmp(cmd_file, "-") == 0)
1707 f = stdin;
1708 else {
1709 f = fopen(cmd_file, "r");
1710 if (!f) {
1711 perror(cmd_file);
1712 exit(1);
1713 }
1714 }
1715 setbuf(stdout, NULL);
1716 setbuf(stderr, NULL);
1717 while (!feof(f)) {
1718 if (fgets(buf, sizeof(buf), f) == NULL)
1719 break;
1720 cp = strchr(buf, '\n');
1721 if (cp)
1722 *cp = 0;
1723 cp = strchr(buf, '\r');
1724 if (cp)
1725 *cp = 0;
1726 printf("debugfs: %s\n", buf);
1727 retval = ss_execute_line(sci_idx, buf);
1728 if (retval) {
1729 ss_perror(sci_idx, retval, buf);
1730 exit_status++;
1731 }
1732 }
1733 return exit_status;
1734}
1735
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001736int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001737{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001738 int retval;
1739 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001740 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 +00001741 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001742 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001743 char *request = 0;
1744 int exit_status = 0;
1745 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001746 blk_t superblock = 0;
1747 blk_t blocksize = 0;
1748 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001749 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001750
1751 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001752 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1753 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001754
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001755 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001756 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001757 case 'R':
1758 request = optarg;
1759 break;
1760 case 'f':
1761 cmd_file = optarg;
1762 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001763 case 'd':
1764 data_filename = optarg;
1765 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001766 case 'i':
1767 open_flags |= EXT2_FLAG_IMAGE_FILE;
1768 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001769 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001770 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001771 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001772 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001773 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001774 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001775 break;
1776 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001777 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001778 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001779 break;
1780 case 'c':
1781 catastrophic = 1;
1782 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001783 case 'V':
1784 /* Print version number and exit */
1785 fprintf(stderr, "\tUsing %s\n",
1786 error_message(EXT2_ET_BASE));
1787 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001788 default:
1789 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001790 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001791 }
1792 }
1793 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001794 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001795 superblock, blocksize, catastrophic,
1796 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001797
1798 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1799 &debug_cmds, &retval);
1800 if (retval) {
1801 ss_perror(sci_idx, retval, "creating invocation");
1802 exit(1);
1803 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001804 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001805
1806 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1807 if (retval) {
1808 ss_perror(sci_idx, retval, "adding standard requests");
1809 exit (1);
1810 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001811 if (request) {
1812 retval = 0;
1813 retval = ss_execute_line(sci_idx, request);
1814 if (retval) {
1815 ss_perror(sci_idx, retval, request);
1816 exit_status++;
1817 }
1818 } else if (cmd_file) {
1819 exit_status = source_file(cmd_file, sci_idx);
1820 } else {
1821 ss_listen(sci_idx);
1822 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001823
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001824 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001825 close_filesystem();
1826
Theodore Ts'oe5973042000-01-18 17:58:34 +00001827 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001828}