blob: 1fcb9d372d81294bea6b1c2fcc85ba1b5064d63c [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'o818180c1998-06-27 05:11:14 +000036#include "../version.h"
37
Theodore Ts'o3839e651997-04-26 13:21:57 +000038extern ss_request_table debug_cmds;
39
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000040ext2_filsys current_fs = NULL;
41ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000042
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000043static void open_filesystem(char *device, int open_flags, blk_t superblock,
44 blk_t blocksize, int catastrophic)
Theodore Ts'o3839e651997-04-26 13:21:57 +000045{
46 int retval;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000047
48 if (superblock != 0 && blocksize == 0) {
49 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
50 current_fs = NULL;
51 return;
52 }
53
54 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
55 com_err(device, 0,
56 "opening read-only because of catastrophic mode");
57 open_flags &= ~EXT2_FLAG_RW;
58 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000060 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000061 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000062 if (retval) {
63 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000064 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000065 return;
66 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000067
68 if (catastrophic)
69 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
70 else {
71 retval = ext2fs_read_inode_bitmap(current_fs);
72 if (retval) {
73 com_err(device, retval, "while reading inode bitmap");
74 goto errout;
75 }
76 retval = ext2fs_read_block_bitmap(current_fs);
77 if (retval) {
78 com_err(device, retval, "while reading block bitmap");
79 goto errout;
80 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 }
82 root = cwd = EXT2_ROOT_INO;
83 return;
84
85errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000086 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000087 if (retval)
88 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000089 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000090}
91
92void do_open_filesys(int argc, char **argv)
93{
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000094 const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
Theodore Ts'oe1018ee2002-01-03 04:55:25 -050095 int c, err;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000096 int catastrophic = 0;
97 blk_t superblock = 0;
98 blk_t blocksize = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +000099 int open_flags = 0;
100
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400101 reset_getopt();
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000102 while ((c = getopt (argc, argv, "iwfcb:s:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000103 switch (c) {
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000104 case 'i':
105 open_flags |= EXT2_FLAG_IMAGE_FILE;
106 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000107 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000108 open_flags |= EXT2_FLAG_RW;
109 break;
110 case 'f':
111 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000112 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000113 case 'c':
114 catastrophic = 1;
115 break;
116 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500117 blocksize = parse_ulong(optarg, argv[0],
118 "block size", &err);
119 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000120 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000121 break;
122 case 's':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500123 superblock = parse_ulong(optarg, argv[0],
124 "superblock number", &err);
125 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000126 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000127 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128 default:
129 com_err(argv[0], 0, usage);
130 return;
131 }
132 }
133 if (optind != argc-1) {
134 com_err(argv[0], 0, usage);
135 return;
136 }
137 if (check_fs_not_open(argv[0]))
138 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000139 open_filesystem(argv[optind], open_flags,
140 superblock, blocksize, catastrophic);
141}
142
143void do_lcd(int argc, char **argv)
144{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500145 if (common_args_process(argc, argv, 2, 2, "lcd",
146 "<native dir>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000147 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000148
149 if (chdir(argv[1]) == -1) {
150 com_err(argv[0], errno,
151 "while trying to change native directory to %s",
152 argv[1]);
153 return;
154 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000155}
156
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000157static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000158{
159 int retval;
160
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000161 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
162 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 if (retval)
164 com_err("ext2fs_write_inode_bitmap", retval, "");
165 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000166 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
167 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000168 if (retval)
169 com_err("ext2fs_write_block_bitmap", retval, "");
170 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000171 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000172 if (retval)
173 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000174 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000175 return;
176}
177
178void do_close_filesys(int argc, char **argv)
179{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500180 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000181 return;
182 close_filesystem();
183}
184
185void do_init_filesys(int argc, char **argv)
186{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 struct ext2_super_block param;
188 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500189 int err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500191 if (common_args_process(argc, argv, 3, 3, "initialize",
192 "<device> <blocksize>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 return;
194
195 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400196 param.s_blocks_count = parse_ulong(argv[2], argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500197 "blocks count", &err);
198 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000200 retval = ext2fs_initialize(argv[1], 0, &param,
201 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 if (retval) {
203 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000204 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 return;
206 }
207 root = cwd = EXT2_ROOT_INO;
208 return;
209}
210
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000211static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000212{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000213 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000214 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000215
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000216 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000217 for (i=0; i <3; i++,mask++) {
218 for (j=0,m=1; j < 32; j++, m<<=1) {
219 if (*mask & m) {
220 fprintf(f, " %s", e2p_feature2string(i, m));
221 printed++;
222 }
223 }
224 }
225 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000226 fputs("(none)", f);
227 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000228}
229
Theodore Ts'o3839e651997-04-26 13:21:57 +0000230void do_show_super_stats(int argc, char *argv[])
231{
232 int i;
233 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000234 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000235 int c, header_only = 0;
Theodore Ts'o34be9602002-07-15 16:56:41 -0400236 int numdirs = 0;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000237 const char *usage = "Usage: show_super [-h]";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400239 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000240 while ((c = getopt (argc, argv, "h")) != EOF) {
241 switch (c) {
242 case 'h':
243 header_only++;
244 break;
245 default:
246 com_err(argv[0], 0, usage);
247 return;
248 }
249 }
250 if (optind != argc) {
251 com_err(argv[0], 0, usage);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000252 return;
253 }
254 if (check_fs_open(argv[0]))
255 return;
256 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000257
258 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400259 for (i=0; i < current_fs->group_desc_count; i++)
260 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
261 fprintf(out, "Directories: %d\n", numdirs);
262
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000263 if (header_only) {
264 close_pager(out);
265 return;
266 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000267
268 gdp = &current_fs->group_desc[0];
269 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000270 fprintf(out, " Group %2d: block bitmap at %d, "
271 "inode bitmap at %d, "
272 "inode table at %d\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000273 " %d free %s, "
274 "%d free %s, "
275 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000276 i, gdp->bg_block_bitmap,
277 gdp->bg_inode_bitmap, gdp->bg_inode_table,
278 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000279 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000280 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000281 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000282 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000283 gdp->bg_used_dirs_count != 1 ? "directories"
284 : "directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 close_pager(out);
286}
287
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000288void do_dirty_filesys(int argc, char **argv)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000289{
290 if (check_fs_open(argv[0]))
291 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000292 if (check_fs_read_write(argv[0]))
293 return;
294
295 if (argv[1] && !strcmp(argv[1], "-clean"))
296 current_fs->super->s_state |= EXT2_VALID_FS;
297 else
298 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000299 ext2fs_mark_super_dirty(current_fs);
300}
301
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700303 FILE *f;
304 e2_blkcnt_t total;
305 blk_t first_block, last_block;
306 e2_blkcnt_t first_bcnt, last_bcnt;
307 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308};
309
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000310static void finish_range(struct list_blocks_struct *lb)
311{
312 if (lb->first_block == 0)
313 return;
314 if (lb->first)
315 lb->first = 0;
316 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000317 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000318 if (lb->first_block == lb->last_block)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100319 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000320 else
Theodore Ts'oe8981882001-11-30 11:51:30 +0100321 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000322 lb->last_bcnt, lb->first_block, lb->last_block);
323 lb->first_block = 0;
324}
325
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700326static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr,
327 e2_blkcnt_t blockcnt, blk_t ref_block,
328 int ref_offset, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000329{
330 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
331
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000332 lb->total++;
333 if (blockcnt >= 0) {
334 /*
335 * See if we can add on to the existing range (if it exists)
336 */
337 if (lb->first_block &&
338 (lb->last_block+1 == *blocknr) &&
339 (lb->last_bcnt+1 == blockcnt)) {
340 lb->last_block = *blocknr;
341 lb->last_bcnt = blockcnt;
342 return 0;
343 }
344 /*
345 * Start a new range.
346 */
347 finish_range(lb);
348 lb->first_block = lb->last_block = *blocknr;
349 lb->first_bcnt = lb->last_bcnt = blockcnt;
350 return 0;
351 }
352 /*
353 * Not a normal block. Always force a new range.
354 */
355 finish_range(lb);
356 if (lb->first)
357 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000358 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000359 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000360 if (blockcnt == -1)
361 fprintf(lb->f, "(IND):%d", *blocknr);
362 else if (blockcnt == -2)
363 fprintf(lb->f, "(DIND):%d", *blocknr);
364 else if (blockcnt == -3)
365 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366 return 0;
367}
368
369
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000370static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371{
372 struct list_blocks_struct lb;
373
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000374 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000375 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000376 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000377 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000378 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700379 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000380 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000381 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000382 if (lb.total)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100383 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384 fprintf(f,"\n");
385}
386
387
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000388void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000389 ext2_ino_t inode_num, struct ext2_inode *inode,
390 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000391{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000392 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000393 char frag, fsize;
394 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000395
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000396 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
397 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
398 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
399 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
400 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
401 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
402 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000403 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000404 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
405 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
406 prefix,
407 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
408 fprintf(out, "%sUser: %5d Group: %5d Size: ",
409 prefix, inode->i_uid, inode->i_gid);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700410 if (LINUX_S_ISREG(inode->i_mode)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000411 __u64 i_size = (inode->i_size |
412 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700413
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000414 fprintf(out, "%lld\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700415 } else
416 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000417 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000418 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000419 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
420 prefix,
421 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
422 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000423 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000424 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
425 prefix,
426 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
427 fprintf(out, "%sLinks: %d Blockcount: %d\n",
428 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000429 switch (os) {
430 case EXT2_OS_LINUX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000431 frag = inode->osd2.linux2.l_i_frag;
432 fsize = inode->osd2.linux2.l_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000433 break;
434 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000435 frag = inode->osd2.hurd2.h_i_frag;
436 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000437 break;
438 case EXT2_OS_MASIX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000439 frag = inode->osd2.masix2.m_i_frag;
440 fsize = inode->osd2.masix2.m_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000441 break;
442 default:
443 frag = fsize = 0;
444 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000445 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
446 prefix, inode->i_faddr, frag, fsize);
447 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
448 time_to_string(inode->i_ctime));
449 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
450 time_to_string(inode->i_atime));
451 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
452 time_to_string(inode->i_mtime));
453 if (inode->i_dtime)
454 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
455 time_to_string(inode->i_dtime));
456 if (LINUX_S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
457 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
458 (int) inode->i_size, (char *)inode->i_block);
459 else if (do_dump_blocks)
460 dump_blocks(out, prefix, inode_num);
461}
462
463static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
464{
465 FILE *out;
466
467 out = open_pager();
468 internal_dump_inode(out, "", inode_num, &inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469 close_pager(out);
470}
471
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472void do_stat(int argc, char *argv[])
473{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000474 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475 struct ext2_inode inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000476
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500477 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 return;
479
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500480 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
481 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482
483 dump_inode(inode,inode_buf);
484 return;
485}
486
487void do_chroot(int argc, char *argv[])
488{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000489 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 int retval;
491
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500492 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493 return;
494
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000495 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 if (retval) {
497 com_err(argv[1], retval, "");
498 return;
499 }
500 root = inode;
501}
502
503void do_clri(int argc, char *argv[])
504{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000505 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 struct ext2_inode inode_buf;
507
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500508 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 return;
510
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500511 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000513 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500514 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516}
517
518void do_freei(int argc, char *argv[])
519{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000520 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500522 if (common_inode_args_process(argc, argv, &inode,
523 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 return;
525
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000526 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000528 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
529 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530}
531
532void do_seti(int argc, char *argv[])
533{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000534 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500536 if (common_inode_args_process(argc, argv, &inode,
537 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 return;
539
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000540 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000542 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
543 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544}
545
546void do_testi(int argc, char *argv[])
547{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000548 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500550 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 return;
552
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000553 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000554 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000555 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000556 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557}
558
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559void do_freeb(int argc, char *argv[])
560{
561 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500562 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000563
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500564 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000565 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000566 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500568 while (count-- > 0) {
569 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
570 com_err(argv[0], 0, "Warning: block %d already clear",
571 block);
572 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
573 block++;
574 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000575 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576}
577
578void do_setb(int argc, char *argv[])
579{
580 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500581 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500583 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000585 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000586 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500587 while (count-- > 0) {
588 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
589 com_err(argv[0], 0, "Warning: block %d already set",
590 block);
591 ext2fs_mark_block_bitmap(current_fs->block_map,block);
592 block++;
593 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000594 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595}
596
597void do_testb(int argc, char *argv[])
598{
599 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500600 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500602 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500604 while (count-- > 0) {
605 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
606 printf("Block %d marked in use\n", block);
607 else
608 printf("Block %d not in use\n", block);
609 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611}
612
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000613static void modify_u8(char *com, const char *prompt,
614 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615{
616 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000617 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618 char *tmp;
619
620 sprintf(buf, format, *val);
621 printf("%30s [%s] ", prompt, buf);
622 fgets(buf, sizeof(buf), stdin);
623 if (buf[strlen (buf) - 1] == '\n')
624 buf[strlen (buf) - 1] = '\0';
625 if (!buf[0])
626 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000627 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628 if (*tmp)
629 com_err(com, 0, "Bad value - %s", buf);
630 else
631 *val = v;
632}
633
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000634static void modify_u16(char *com, const char *prompt,
635 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636{
637 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000638 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 char *tmp;
640
641 sprintf(buf, format, *val);
642 printf("%30s [%s] ", prompt, buf);
643 fgets(buf, sizeof(buf), stdin);
644 if (buf[strlen (buf) - 1] == '\n')
645 buf[strlen (buf) - 1] = '\0';
646 if (!buf[0])
647 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000648 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000649 if (*tmp)
650 com_err(com, 0, "Bad value - %s", buf);
651 else
652 *val = v;
653}
654
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000655static void modify_u32(char *com, const char *prompt,
656 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657{
658 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000659 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 char *tmp;
661
662 sprintf(buf, format, *val);
663 printf("%30s [%s] ", prompt, buf);
664 fgets(buf, sizeof(buf), stdin);
665 if (buf[strlen (buf) - 1] == '\n')
666 buf[strlen (buf) - 1] = '\0';
667 if (!buf[0])
668 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000669 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670 if (*tmp)
671 com_err(com, 0, "Bad value - %s", buf);
672 else
673 *val = v;
674}
675
676
677void do_modify_inode(int argc, char *argv[])
678{
679 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000680 ext2_ino_t inode_num;
681 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000682 unsigned char *frag, *fsize;
683 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500684 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000685 const char *hex_format = "0x%x";
686 const char *octal_format = "0%o";
687 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500689 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500692 os = current_fs->super->s_creator_os;
693
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500694 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000696
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000697 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
698 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
699 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
700 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
701 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
702 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
703 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
704 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
705 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
706 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
707 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000708 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000709#if 0
710 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
711#endif
712 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000713 if (LINUX_S_ISDIR(inode.i_mode))
714 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
715 else
716 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000717
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000718 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000719 modify_u32(argv[0], "Translator Block",
720 decimal_format, &inode.osd1.hurd1.h_i_translator);
721
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000722 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000723 switch (os) {
724 case EXT2_OS_LINUX:
725 frag = &inode.osd2.linux2.l_i_frag;
726 fsize = &inode.osd2.linux2.l_i_fsize;
727 break;
728 case EXT2_OS_HURD:
729 frag = &inode.osd2.hurd2.h_i_frag;
730 fsize = &inode.osd2.hurd2.h_i_fsize;
731 break;
732 case EXT2_OS_MASIX:
733 frag = &inode.osd2.masix2.m_i_frag;
734 fsize = &inode.osd2.masix2.m_i_fsize;
735 break;
736 default:
737 frag = fsize = 0;
738 }
739 if (frag)
740 modify_u8(argv[0], "Fragment number", decimal_format, frag);
741 if (fsize)
742 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
743
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
745 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000746 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000748 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000750 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000751 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000752 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000753 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500754 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000755 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000756}
757
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758void do_change_working_dir(int argc, char *argv[])
759{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000760 ext2_ino_t inode;
761 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000762
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500763 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000764 return;
765
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000766 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000767 if (retval) {
768 com_err(argv[1], retval, "");
769 return;
770 }
771 cwd = inode;
772 return;
773}
774
Theodore Ts'o3839e651997-04-26 13:21:57 +0000775void do_print_working_directory(int argc, char *argv[])
776{
777 int retval;
778 char *pathname = NULL;
779
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500780 if (common_args_process(argc, argv, 1, 1,
781 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000782 return;
783
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000784 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785 if (retval) {
786 com_err(argv[0], retval,
787 "while trying to get pathname of cwd");
788 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000789 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000790 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000791 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000792 if (retval) {
793 com_err(argv[0], retval,
794 "while trying to get pathname of root");
795 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000796 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000797 free(pathname);
798 return;
799}
800
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000801static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000802{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000803 ext2_ino_t inode;
804 int retval;
805 ext2_ino_t dir;
806 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000807
808 /*
809 * Get the source inode
810 */
811 inode = string_to_inode(sourcename);
812 if (!inode)
813 return;
814 basename = strrchr(sourcename, '/');
815 if (basename)
816 basename++;
817 else
818 basename = sourcename;
819 /*
820 * Figure out the destination. First see if it exists and is
821 * a directory.
822 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000823 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000824 dest = basename;
825 else {
826 /*
827 * OK, it doesn't exist. See if it is
828 * '<dir>/basename' or 'basename'
829 */
830 cp = strrchr(destname, '/');
831 if (cp) {
832 *cp = 0;
833 dir = string_to_inode(destname);
834 if (!dir)
835 return;
836 dest = cp+1;
837 } else {
838 dir = cwd;
839 dest = destname;
840 }
841 }
842
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000843 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000844 if (retval)
845 com_err("make_link", retval, "");
846 return;
847}
848
849
850void do_link(int argc, char *argv[])
851{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500852 if (common_args_process(argc, argv, 3, 3, "link",
853 "<source file> <dest_name>", CHECK_FS_RW))
854 return;
855
856 make_link(argv[1], argv[2]);
857}
858
859static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
860 int blockcnt, void *private)
861{
862 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500863
864 block = *blocknr;
865 ext2fs_block_alloc_stats(fs, block, +1);
866 return 0;
867}
868
869void do_undel(int argc, char *argv[])
870{
871 ext2_ino_t ino;
872 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500873
874 if (common_args_process(argc, argv, 3, 3, "undelete",
875 "<inode_num> <dest_name>",
876 CHECK_FS_RW | CHECK_FS_BITMAPS))
877 return;
878
879 ino = string_to_inode(argv[1]);
880 if (!ino)
881 return;
882
883 if (debugfs_read_inode(ino, &inode, argv[1]))
884 return;
885
886 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
887 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888 return;
889 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500890
891 /*
892 * XXX this function doesn't handle changing the links count on the
893 * parent directory when undeleting a directory.
894 */
895 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
896 inode.i_dtime = 0;
897
898 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 return;
900
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500901 ext2fs_block_iterate(current_fs, ino, 0, NULL,
902 mark_blocks_proc, NULL);
903
Theodore Ts'od7f64ae2002-07-09 01:27:05 -0400904 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500905
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 make_link(argv[1], argv[2]);
907}
908
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000909static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000910{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000911 int retval;
912 ext2_ino_t dir;
913 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914
915 basename = strrchr(filename, '/');
916 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000917 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 dir = string_to_inode(filename);
919 if (!dir)
920 return;
921 } else {
922 dir = cwd;
923 basename = filename;
924 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000925 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000926 if (retval)
927 com_err("unlink_file_by_name", retval, "");
928 return;
929}
930
931void do_unlink(int argc, char *argv[])
932{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500933 if (common_args_process(argc, argv, 2, 2, "link",
934 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000935 return;
936
937 unlink_file_by_name(argv[1]);
938}
939
940void do_find_free_block(int argc, char *argv[])
941{
942 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500943 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000944 errcode_t retval;
945 char *tmp;
946
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500947 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
948 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000949 return;
950 }
951 if (check_fs_open(argv[0]))
952 return;
953
954 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500955 count = strtol(argv[1],&tmp,0);
956 if (*tmp) {
957 com_err(argv[0], 0, "Bad count - %s", argv[1]);
958 return;
959 }
960 } else
961 count = 1;
962
963 if (argc > 2) {
964 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000965 if (*tmp) {
966 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
967 return;
968 }
969 }
970 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000971 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000972
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500973 printf("Free blocks found: ");
974 free_blk = goal - 1;
975 while (count-- > 0) {
976 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
977 &free_blk);
978 if (retval) {
979 com_err("ext2fs_new_block", retval, "");
980 return;
981 } else
982 printf("%d ", free_blk);
983 }
984 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000985}
986
987void do_find_free_inode(int argc, char *argv[])
988{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000989 ext2_ino_t free_inode, dir;
990 int mode;
991 int retval;
992 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000994 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
995 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000996 return;
997 }
998 if (check_fs_open(argv[0]))
999 return;
1000
1001 if (argc > 1) {
1002 dir = strtol(argv[1], &tmp, 0);
1003 if (*tmp) {
1004 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1005 return;
1006 }
1007 }
1008 else
1009 dir = root;
1010 if (argc > 2) {
1011 mode = strtol(argv[2], &tmp, 0);
1012 if (*tmp) {
1013 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1014 return;
1015 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001016 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001017 mode = 010755;
1018
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001019 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001020 if (retval)
1021 com_err("ext2fs_new_inode", retval, "");
1022 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001023 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001024}
1025
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001026static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001027{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001028 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001029 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001030 int got;
1031 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001032 char buf[8192];
1033 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001034
Theodore Ts'o5a513841997-10-25 22:41:14 +00001035 retval = ext2fs_file_open(current_fs, newfile,
1036 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001037 if (retval)
1038 return retval;
1039
Theodore Ts'o5a513841997-10-25 22:41:14 +00001040 while (1) {
1041 got = read(fd, buf, sizeof(buf));
1042 if (got == 0)
1043 break;
1044 if (got < 0) {
1045 retval = errno;
1046 goto fail;
1047 }
1048 ptr = buf;
1049 while (got > 0) {
1050 retval = ext2fs_file_write(e2_file, ptr,
1051 got, &written);
1052 if (retval)
1053 goto fail;
1054
1055 got -= written;
1056 ptr += written;
1057 }
1058 }
1059 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001060 return retval;
1061
1062fail:
1063 (void) ext2fs_file_close(e2_file);
1064 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001065}
1066
Theodore Ts'o5a513841997-10-25 22:41:14 +00001067
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001068void do_write(int argc, char *argv[])
1069{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001070 int fd;
1071 struct stat statbuf;
1072 ext2_ino_t newfile;
1073 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001074 struct ext2_inode inode;
1075
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001076 if (common_args_process(argc, argv, 3, 3, "write",
1077 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001078 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001079
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001080 fd = open(argv[1], O_RDONLY);
1081 if (fd < 0) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001082 com_err(argv[1], errno, "");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001083 return;
1084 }
1085 if (fstat(fd, &statbuf) < 0) {
1086 com_err(argv[1], errno, "");
1087 close(fd);
1088 return;
1089 }
1090
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001091 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1092 if (retval == 0) {
1093 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1094 close(fd);
1095 return;
1096 }
1097
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001098 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001099 if (retval) {
1100 com_err(argv[0], retval, "");
1101 close(fd);
1102 return;
1103 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001104 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001105 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1106 EXT2_FT_REG_FILE);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001107 if (retval) {
1108 com_err(argv[2], retval, "");
1109 close(fd);
1110 return;
1111 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001112 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001113 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001114 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001115 memset(&inode, 0, sizeof(inode));
1116 inode.i_mode = statbuf.st_mode;
1117 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1118 inode.i_links_count = 1;
1119 inode.i_size = statbuf.st_size;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001120 if (debugfs_write_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001121 close(fd);
1122 return;
1123 }
1124 if (LINUX_S_ISREG(inode.i_mode)) {
1125 retval = copy_file(fd, newfile);
1126 if (retval)
1127 com_err("copy_file", retval, "");
1128 }
1129 close(fd);
1130}
1131
1132void do_mknod(int argc, char *argv[])
1133{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001134 unsigned long mode, major, minor, nr;
1135 ext2_ino_t newfile;
1136 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001137 struct ext2_inode inode;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001138 int filetype;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001139
1140 if (check_fs_open(argv[0]))
1141 return;
1142 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001143 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001144 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1145 return;
1146 }
1147 mode = minor = major = 0;
1148 switch (argv[2][0]) {
1149 case 'p':
1150 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001151 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001152 nr = 3;
1153 break;
1154 case 'c':
1155 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001156 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001157 nr = 5;
1158 break;
1159 case 'b':
1160 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001161 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001162 nr = 5;
1163 break;
1164 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001165 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001166 nr = 0;
1167 }
1168 if (nr == 5) {
1169 major = strtoul(argv[3], argv+3, 0);
1170 minor = strtoul(argv[4], argv+4, 0);
1171 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1172 nr = 0;
1173 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001174 if (argc != nr)
1175 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001176 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001177 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001178 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001179 if (retval) {
1180 com_err(argv[0], retval, "");
1181 return;
1182 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001183 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001184 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001185 if (retval) {
1186 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001187 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001188 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001189 retval = ext2fs_link(current_fs, cwd,
Theodore Ts'o085cb192001-05-09 06:09:12 +00001190 argv[1], newfile,
1191 filetype);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001192 }
1193 if (retval) {
1194 com_err(argv[1], retval, "");
1195 return;
1196 }
1197 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001198 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001199 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001200 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1201 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001202 memset(&inode, 0, sizeof(inode));
1203 inode.i_mode = mode;
1204 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1205 inode.i_block[0] = major*256+minor;
1206 inode.i_links_count = 1;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001207 if (debugfs_write_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001208 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001209}
1210
Theodore Ts'o3839e651997-04-26 13:21:57 +00001211void do_mkdir(int argc, char *argv[])
1212{
1213 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001214 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001215 char *name;
1216 errcode_t retval;
1217
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001218 if (common_args_process(argc, argv, 2, 2, "mkdir",
1219 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001220 return;
1221
Theodore Ts'o3839e651997-04-26 13:21:57 +00001222 cp = strrchr(argv[1], '/');
1223 if (cp) {
1224 *cp = 0;
1225 parent = string_to_inode(argv[1]);
1226 if (!parent) {
1227 com_err(argv[1], ENOENT, "");
1228 return;
1229 }
1230 name = cp+1;
1231 } else {
1232 parent = cwd;
1233 name = argv[1];
1234 }
1235
1236
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001237 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001238 if (retval) {
1239 com_err("ext2fs_mkdir", retval, "");
1240 return;
1241 }
1242
1243}
1244
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001245static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1246 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001247{
Theodore Ts'o34436892001-12-22 13:06:02 -05001248 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001249
1250 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001251 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001252 return 0;
1253}
1254
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001255static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001256{
1257 struct ext2_inode inode_buf;
1258
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001259 if (debugfs_read_inode(inode, &inode_buf, 0))
1260 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001261 inode_buf.i_dtime = time(NULL);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001262 if (debugfs_write_inode(inode, &inode_buf, 0))
1263 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001264
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001265 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1266 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001267 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001268 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1269 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001270}
1271
1272
1273void do_kill_file(int argc, char *argv[])
1274{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001275 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001276
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001277 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001278 return;
1279
Theodore Ts'o3839e651997-04-26 13:21:57 +00001280 kill_file_by_inode(inode_num);
1281}
1282
1283void do_rm(int argc, char *argv[])
1284{
1285 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001286 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001287 struct ext2_inode inode;
1288
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001289 if (common_args_process(argc, argv, 2, 2, "rm",
1290 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001291 return;
1292
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001293 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001294 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001295 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001296 return;
1297 }
1298
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001299 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001300 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001301
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001302 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001303 com_err(argv[0], 0, "file is a directory");
1304 return;
1305 }
1306
1307 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001308 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001309 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001310
1311 unlink_file_by_name(argv[1]);
1312 if (inode.i_links_count == 0)
1313 kill_file_by_inode(inode_num);
1314}
1315
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001316struct rd_struct {
1317 ext2_ino_t parent;
1318 int empty;
1319};
1320
1321static int rmdir_proc(ext2_ino_t dir,
1322 int entry,
1323 struct ext2_dir_entry *dirent,
1324 int offset,
1325 int blocksize,
1326 char *buf,
1327 void *private)
1328{
1329 struct rd_struct *rds = (struct rd_struct *) private;
1330
1331 if (dirent->inode == 0)
1332 return 0;
1333 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1334 return 0;
1335 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1336 (dirent->name[1] == '.')) {
1337 rds->parent = dirent->inode;
1338 return 0;
1339 }
1340 rds->empty = 0;
1341 return 0;
1342}
1343
1344void do_rmdir(int argc, char *argv[])
1345{
1346 int retval;
1347 ext2_ino_t inode_num;
1348 struct ext2_inode inode;
1349 struct rd_struct rds;
1350
1351 if (common_args_process(argc, argv, 2, 2, "rmdir",
1352 "<filename>", CHECK_FS_RW))
1353 return;
1354
1355 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1356 if (retval) {
1357 com_err(argv[0], retval, "while trying to resolve filename");
1358 return;
1359 }
1360
1361 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1362 return;
1363
1364 if (!LINUX_S_ISDIR(inode.i_mode)) {
1365 com_err(argv[0], 0, "file is not a directory");
1366 return;
1367 }
1368
1369 rds.parent = 0;
1370 rds.empty = 1;
1371
1372 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1373 0, rmdir_proc, &rds);
1374 if (retval) {
1375 com_err(argv[0], retval, "while iterating over directory");
1376 return;
1377 }
1378 if (rds.empty == 0) {
1379 com_err(argv[0], 0, "directory not empty");
1380 return;
1381 }
1382
1383 inode.i_links_count = 0;
1384 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1385 return;
1386
1387 unlink_file_by_name(argv[1]);
1388 kill_file_by_inode(inode_num);
1389
1390 if (rds.parent) {
1391 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1392 return;
1393 if (inode.i_links_count > 1)
1394 inode.i_links_count--;
1395 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1396 return;
1397 }
1398}
1399
Theodore Ts'o3839e651997-04-26 13:21:57 +00001400void do_show_debugfs_params(int argc, char *argv[])
1401{
1402 FILE *out = stdout;
1403
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001404 if (current_fs)
1405 fprintf(out, "Open mode: read-%s\n",
1406 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001407 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001408 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001409}
1410
1411void do_expand_dir(int argc, char *argv[])
1412{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001413 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001414 int retval;
1415
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001416 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001417 return;
1418
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001419 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001420 if (retval)
1421 com_err("ext2fs_expand_dir", retval, "");
1422 return;
1423}
1424
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001425void do_features(int argc, char *argv[])
1426{
1427 int i;
1428
1429 if (check_fs_open(argv[0]))
1430 return;
1431
1432 if ((argc != 1) && check_fs_read_write(argv[0]))
1433 return;
1434 for (i=1; i < argc; i++) {
1435 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001436 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001437 com_err(argv[0], 0, "Unknown feature: %s\n",
1438 argv[i]);
1439 else
1440 ext2fs_mark_super_dirty(current_fs);
1441 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001442 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001443}
1444
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001445void do_bmap(int argc, char *argv[])
1446{
1447 ext2_ino_t ino;
1448 blk_t blk, pblk;
1449 int err;
1450 errcode_t errcode;
1451
1452 if (common_args_process(argc, argv, 3, 3, argv[0],
1453 "<file> logical_blk", 0))
1454 return;
1455
1456 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001457 if (!ino)
1458 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001459 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1460
1461 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1462 if (errcode) {
1463 com_err("argv[0]", errcode,
1464 "while mapping logical block %d\n", blk);
1465 return;
1466 }
1467 printf("%d\n", pblk);
1468}
1469
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001470void do_imap(int argc, char *argv[])
1471{
1472 ext2_ino_t ino;
1473 unsigned long group, block, block_nr, offset;
1474
1475 if (common_args_process(argc, argv, 2, 2, argv[0],
1476 "<file>", 0))
1477 return;
1478 ino = string_to_inode(argv[1]);
1479 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001480 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001481
1482 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1483 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1484 EXT2_INODE_SIZE(current_fs->super);
1485 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1486 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
1487 com_err(argv[0], 0, "Inode table for group %d is missing\n",
1488 group);
1489 return;
1490 }
1491 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1492 block;
1493 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1494
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001495 printf("Inode %d is part of block group %lu\n"
1496 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001497 block_nr, offset);
1498
1499}
1500
1501
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001502
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001503static int source_file(const char *cmd_file, int sci_idx)
1504{
1505 FILE *f;
1506 char buf[256];
1507 char *cp;
1508 int exit_status = 0;
1509 int retval;
1510
1511 if (strcmp(cmd_file, "-") == 0)
1512 f = stdin;
1513 else {
1514 f = fopen(cmd_file, "r");
1515 if (!f) {
1516 perror(cmd_file);
1517 exit(1);
1518 }
1519 }
1520 setbuf(stdout, NULL);
1521 setbuf(stderr, NULL);
1522 while (!feof(f)) {
1523 if (fgets(buf, sizeof(buf), f) == NULL)
1524 break;
1525 cp = strchr(buf, '\n');
1526 if (cp)
1527 *cp = 0;
1528 cp = strchr(buf, '\r');
1529 if (cp)
1530 *cp = 0;
1531 printf("debugfs: %s\n", buf);
1532 retval = ss_execute_line(sci_idx, buf);
1533 if (retval) {
1534 ss_perror(sci_idx, retval, buf);
1535 exit_status++;
1536 }
1537 }
1538 return exit_status;
1539}
1540
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001541int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001542{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001543 int retval;
1544 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001545 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 +00001546 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001547 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001548 char *request = 0;
1549 int exit_status = 0;
1550 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001551 blk_t superblock = 0;
1552 blk_t blocksize = 0;
1553 int catastrophic = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001554
1555 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001556 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1557 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001558
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001559 while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001560 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001561 case 'R':
1562 request = optarg;
1563 break;
1564 case 'f':
1565 cmd_file = optarg;
1566 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001567 case 'i':
1568 open_flags |= EXT2_FLAG_IMAGE_FILE;
1569 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001570 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001571 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001572 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001573 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001574 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001575 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001576 break;
1577 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001578 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001579 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001580 break;
1581 case 'c':
1582 catastrophic = 1;
1583 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001584 case 'V':
1585 /* Print version number and exit */
1586 fprintf(stderr, "\tUsing %s\n",
1587 error_message(EXT2_ET_BASE));
1588 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001589 default:
1590 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001591 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001592 }
1593 }
1594 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001595 open_filesystem(argv[optind], open_flags,
1596 superblock, blocksize, catastrophic);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001597
1598 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1599 &debug_cmds, &retval);
1600 if (retval) {
1601 ss_perror(sci_idx, retval, "creating invocation");
1602 exit(1);
1603 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001604 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001605
1606 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1607 if (retval) {
1608 ss_perror(sci_idx, retval, "adding standard requests");
1609 exit (1);
1610 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001611 if (request) {
1612 retval = 0;
1613 retval = ss_execute_line(sci_idx, request);
1614 if (retval) {
1615 ss_perror(sci_idx, retval, request);
1616 exit_status++;
1617 }
1618 } else if (cmd_file) {
1619 exit_status = source_file(cmd_file, sci_idx);
1620 } else {
1621 ss_listen(sci_idx);
1622 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001623
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001624 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001625 close_filesystem();
1626
Theodore Ts'oe5973042000-01-18 17:58:34 +00001627 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001628}