blob: 6e3d7dc5e199908e604bf2c81ff2da0e2f5c95aa [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{
Theodore Ts'o54434922003-12-07 01:28:50 -0500232 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 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'o54434922003-12-07 01:28:50 -0500288void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
289 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000290{
291 if (check_fs_open(argv[0]))
292 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000293 if (check_fs_read_write(argv[0]))
294 return;
295
296 if (argv[1] && !strcmp(argv[1], "-clean"))
297 current_fs->super->s_state |= EXT2_VALID_FS;
298 else
299 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000300 ext2fs_mark_super_dirty(current_fs);
301}
302
Theodore Ts'o3839e651997-04-26 13:21:57 +0000303struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700304 FILE *f;
305 e2_blkcnt_t total;
306 blk_t first_block, last_block;
307 e2_blkcnt_t first_bcnt, last_bcnt;
308 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000309};
310
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000311static void finish_range(struct list_blocks_struct *lb)
312{
313 if (lb->first_block == 0)
314 return;
315 if (lb->first)
316 lb->first = 0;
317 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000318 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000319 if (lb->first_block == lb->last_block)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100320 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000321 else
Theodore Ts'oe8981882001-11-30 11:51:30 +0100322 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000323 lb->last_bcnt, lb->first_block, lb->last_block);
324 lb->first_block = 0;
325}
326
Theodore Ts'o54434922003-12-07 01:28:50 -0500327static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
328 blk_t *blocknr, e2_blkcnt_t blockcnt,
329 blk_t ref_block EXT2FS_ATTR((unused)),
330 int ref_offset EXT2FS_ATTR((unused)),
331 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000332{
333 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
334
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000335 lb->total++;
336 if (blockcnt >= 0) {
337 /*
338 * See if we can add on to the existing range (if it exists)
339 */
340 if (lb->first_block &&
341 (lb->last_block+1 == *blocknr) &&
342 (lb->last_bcnt+1 == blockcnt)) {
343 lb->last_block = *blocknr;
344 lb->last_bcnt = blockcnt;
345 return 0;
346 }
347 /*
348 * Start a new range.
349 */
350 finish_range(lb);
351 lb->first_block = lb->last_block = *blocknr;
352 lb->first_bcnt = lb->last_bcnt = blockcnt;
353 return 0;
354 }
355 /*
356 * Not a normal block. Always force a new range.
357 */
358 finish_range(lb);
359 if (lb->first)
360 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000361 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000362 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000363 if (blockcnt == -1)
364 fprintf(lb->f, "(IND):%d", *blocknr);
365 else if (blockcnt == -2)
366 fprintf(lb->f, "(DIND):%d", *blocknr);
367 else if (blockcnt == -3)
368 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369 return 0;
370}
371
372
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000373static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000374{
375 struct list_blocks_struct lb;
376
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000377 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000379 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000380 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000381 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700382 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000383 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000384 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000385 if (lb.total)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100386 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000387 fprintf(f,"\n");
388}
389
390
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000391void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000392 ext2_ino_t inode_num, struct ext2_inode *inode,
393 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000394{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000395 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000396 char frag, fsize;
397 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000398
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000399 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
400 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
401 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
402 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
403 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
404 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
405 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000407 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
408 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
409 prefix,
410 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
411 fprintf(out, "%sUser: %5d Group: %5d Size: ",
412 prefix, inode->i_uid, inode->i_gid);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700413 if (LINUX_S_ISREG(inode->i_mode)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000414 __u64 i_size = (inode->i_size |
415 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700416
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000417 fprintf(out, "%lld\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700418 } else
419 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000420 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000421 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000422 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
423 prefix,
424 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
425 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000426 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000427 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
428 prefix,
429 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
430 fprintf(out, "%sLinks: %d Blockcount: %d\n",
431 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000432 switch (os) {
433 case EXT2_OS_LINUX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000434 frag = inode->osd2.linux2.l_i_frag;
435 fsize = inode->osd2.linux2.l_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000436 break;
437 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000438 frag = inode->osd2.hurd2.h_i_frag;
439 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000440 break;
441 case EXT2_OS_MASIX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000442 frag = inode->osd2.masix2.m_i_frag;
443 fsize = inode->osd2.masix2.m_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000444 break;
445 default:
446 frag = fsize = 0;
447 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000448 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
449 prefix, inode->i_faddr, frag, fsize);
450 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
451 time_to_string(inode->i_ctime));
452 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
453 time_to_string(inode->i_atime));
454 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
455 time_to_string(inode->i_mtime));
456 if (inode->i_dtime)
457 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
458 time_to_string(inode->i_dtime));
Theodore Ts'o795afc42004-02-21 20:54:31 -0500459 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000460 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
461 (int) inode->i_size, (char *)inode->i_block);
462 else if (do_dump_blocks)
463 dump_blocks(out, prefix, inode_num);
464}
465
466static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
467{
468 FILE *out;
469
470 out = open_pager();
471 internal_dump_inode(out, "", inode_num, &inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 close_pager(out);
473}
474
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475void do_stat(int argc, char *argv[])
476{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000477 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 struct ext2_inode inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500480 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481 return;
482
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500483 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
484 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485
486 dump_inode(inode,inode_buf);
487 return;
488}
489
490void do_chroot(int argc, char *argv[])
491{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000492 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493 int retval;
494
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500495 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 return;
497
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000498 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000499 if (retval) {
500 com_err(argv[1], retval, "");
501 return;
502 }
503 root = inode;
504}
505
506void do_clri(int argc, char *argv[])
507{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000508 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 struct ext2_inode inode_buf;
510
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500511 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 return;
513
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500514 if (debugfs_read_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 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500517 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519}
520
521void do_freei(int argc, char *argv[])
522{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000523 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500525 if (common_inode_args_process(argc, argv, &inode,
526 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 return;
528
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000529 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000531 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
532 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533}
534
535void do_seti(int argc, char *argv[])
536{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000537 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500539 if (common_inode_args_process(argc, argv, &inode,
540 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 return;
542
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000543 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000544 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000545 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
546 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547}
548
549void do_testi(int argc, char *argv[])
550{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000551 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000552
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500553 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554 return;
555
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000556 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000557 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000559 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560}
561
Theodore Ts'o3839e651997-04-26 13:21:57 +0000562void do_freeb(int argc, char *argv[])
563{
564 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500565 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500567 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000569 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500571 while (count-- > 0) {
572 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
573 com_err(argv[0], 0, "Warning: block %d already clear",
574 block);
575 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
576 block++;
577 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000578 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579}
580
581void do_setb(int argc, char *argv[])
582{
583 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500584 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500586 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000588 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000589 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500590 while (count-- > 0) {
591 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
592 com_err(argv[0], 0, "Warning: block %d already set",
593 block);
594 ext2fs_mark_block_bitmap(current_fs->block_map,block);
595 block++;
596 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000597 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598}
599
600void do_testb(int argc, char *argv[])
601{
602 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500603 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000604
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500605 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500607 while (count-- > 0) {
608 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
609 printf("Block %d marked in use\n", block);
610 else
611 printf("Block %d not in use\n", block);
612 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000613 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614}
615
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000616static void modify_u8(char *com, const char *prompt,
617 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618{
619 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000620 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 char *tmp;
622
623 sprintf(buf, format, *val);
624 printf("%30s [%s] ", prompt, buf);
625 fgets(buf, sizeof(buf), stdin);
626 if (buf[strlen (buf) - 1] == '\n')
627 buf[strlen (buf) - 1] = '\0';
628 if (!buf[0])
629 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000630 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 if (*tmp)
632 com_err(com, 0, "Bad value - %s", buf);
633 else
634 *val = v;
635}
636
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000637static void modify_u16(char *com, const char *prompt,
638 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639{
640 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000641 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000642 char *tmp;
643
644 sprintf(buf, format, *val);
645 printf("%30s [%s] ", prompt, buf);
646 fgets(buf, sizeof(buf), stdin);
647 if (buf[strlen (buf) - 1] == '\n')
648 buf[strlen (buf) - 1] = '\0';
649 if (!buf[0])
650 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000651 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 if (*tmp)
653 com_err(com, 0, "Bad value - %s", buf);
654 else
655 *val = v;
656}
657
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000658static void modify_u32(char *com, const char *prompt,
659 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660{
661 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000662 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 char *tmp;
664
665 sprintf(buf, format, *val);
666 printf("%30s [%s] ", prompt, buf);
667 fgets(buf, sizeof(buf), stdin);
668 if (buf[strlen (buf) - 1] == '\n')
669 buf[strlen (buf) - 1] = '\0';
670 if (!buf[0])
671 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000672 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 if (*tmp)
674 com_err(com, 0, "Bad value - %s", buf);
675 else
676 *val = v;
677}
678
679
680void do_modify_inode(int argc, char *argv[])
681{
682 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000683 ext2_ino_t inode_num;
684 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000685 unsigned char *frag, *fsize;
686 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500687 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000688 const char *hex_format = "0x%x";
689 const char *octal_format = "0%o";
690 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500692 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000693 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500695 os = current_fs->super->s_creator_os;
696
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500697 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000699
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000700 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
701 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
702 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
703 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
704 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
705 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
706 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
707 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
708 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
709 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
710 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000711 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000712#if 0
713 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
714#endif
715 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000716 if (LINUX_S_ISDIR(inode.i_mode))
717 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
718 else
719 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000720
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000721 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000722 modify_u32(argv[0], "Translator Block",
723 decimal_format, &inode.osd1.hurd1.h_i_translator);
724
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000725 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000726 switch (os) {
727 case EXT2_OS_LINUX:
728 frag = &inode.osd2.linux2.l_i_frag;
729 fsize = &inode.osd2.linux2.l_i_fsize;
730 break;
731 case EXT2_OS_HURD:
732 frag = &inode.osd2.hurd2.h_i_frag;
733 fsize = &inode.osd2.hurd2.h_i_fsize;
734 break;
735 case EXT2_OS_MASIX:
736 frag = &inode.osd2.masix2.m_i_frag;
737 fsize = &inode.osd2.masix2.m_i_fsize;
738 break;
739 default:
740 frag = fsize = 0;
741 }
742 if (frag)
743 modify_u8(argv[0], "Fragment number", decimal_format, frag);
744 if (fsize)
745 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
746
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
748 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000749 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000750 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000751 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000753 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000755 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000756 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500757 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000759}
760
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761void do_change_working_dir(int argc, char *argv[])
762{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000763 ext2_ino_t inode;
764 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000765
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500766 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000767 return;
768
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000769 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000770 if (retval) {
771 com_err(argv[1], retval, "");
772 return;
773 }
774 cwd = inode;
775 return;
776}
777
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778void do_print_working_directory(int argc, char *argv[])
779{
780 int retval;
781 char *pathname = NULL;
782
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500783 if (common_args_process(argc, argv, 1, 1,
784 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785 return;
786
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000787 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000788 if (retval) {
789 com_err(argv[0], retval,
790 "while trying to get pathname of cwd");
791 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000792 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000794 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 if (retval) {
796 com_err(argv[0], retval,
797 "while trying to get pathname of root");
798 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000799 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000800 free(pathname);
801 return;
802}
803
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000804static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000805{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000806 ext2_ino_t inode;
807 int retval;
808 ext2_ino_t dir;
809 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810
811 /*
812 * Get the source inode
813 */
814 inode = string_to_inode(sourcename);
815 if (!inode)
816 return;
817 basename = strrchr(sourcename, '/');
818 if (basename)
819 basename++;
820 else
821 basename = sourcename;
822 /*
823 * Figure out the destination. First see if it exists and is
824 * a directory.
825 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000826 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827 dest = basename;
828 else {
829 /*
830 * OK, it doesn't exist. See if it is
831 * '<dir>/basename' or 'basename'
832 */
833 cp = strrchr(destname, '/');
834 if (cp) {
835 *cp = 0;
836 dir = string_to_inode(destname);
837 if (!dir)
838 return;
839 dest = cp+1;
840 } else {
841 dir = cwd;
842 dest = destname;
843 }
844 }
845
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000846 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000847 if (retval)
848 com_err("make_link", retval, "");
849 return;
850}
851
852
853void do_link(int argc, char *argv[])
854{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500855 if (common_args_process(argc, argv, 3, 3, "link",
856 "<source file> <dest_name>", CHECK_FS_RW))
857 return;
858
859 make_link(argv[1], argv[2]);
860}
861
862static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500863 int blockcnt EXT2FS_ATTR((unused)),
864 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500865{
866 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500867
868 block = *blocknr;
869 ext2fs_block_alloc_stats(fs, block, +1);
870 return 0;
871}
872
873void do_undel(int argc, char *argv[])
874{
875 ext2_ino_t ino;
876 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500877
878 if (common_args_process(argc, argv, 3, 3, "undelete",
879 "<inode_num> <dest_name>",
880 CHECK_FS_RW | CHECK_FS_BITMAPS))
881 return;
882
883 ino = string_to_inode(argv[1]);
884 if (!ino)
885 return;
886
887 if (debugfs_read_inode(ino, &inode, argv[1]))
888 return;
889
890 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
891 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000892 return;
893 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500894
895 /*
896 * XXX this function doesn't handle changing the links count on the
897 * parent directory when undeleting a directory.
898 */
899 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
900 inode.i_dtime = 0;
901
902 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000903 return;
904
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500905 ext2fs_block_iterate(current_fs, ino, 0, NULL,
906 mark_blocks_proc, NULL);
907
Theodore Ts'od7f64ae2002-07-09 01:27:05 -0400908 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500909
Theodore Ts'o3839e651997-04-26 13:21:57 +0000910 make_link(argv[1], argv[2]);
911}
912
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000913static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000915 int retval;
916 ext2_ino_t dir;
917 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918
919 basename = strrchr(filename, '/');
920 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000921 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000922 dir = string_to_inode(filename);
923 if (!dir)
924 return;
925 } else {
926 dir = cwd;
927 basename = filename;
928 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000929 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 if (retval)
931 com_err("unlink_file_by_name", retval, "");
932 return;
933}
934
935void do_unlink(int argc, char *argv[])
936{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500937 if (common_args_process(argc, argv, 2, 2, "link",
938 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939 return;
940
941 unlink_file_by_name(argv[1]);
942}
943
944void do_find_free_block(int argc, char *argv[])
945{
946 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500947 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000948 errcode_t retval;
949 char *tmp;
950
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500951 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
952 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000953 return;
954 }
955 if (check_fs_open(argv[0]))
956 return;
957
958 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500959 count = strtol(argv[1],&tmp,0);
960 if (*tmp) {
961 com_err(argv[0], 0, "Bad count - %s", argv[1]);
962 return;
963 }
964 } else
965 count = 1;
966
967 if (argc > 2) {
968 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000969 if (*tmp) {
970 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
971 return;
972 }
973 }
974 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000975 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500977 printf("Free blocks found: ");
978 free_blk = goal - 1;
979 while (count-- > 0) {
980 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
981 &free_blk);
982 if (retval) {
983 com_err("ext2fs_new_block", retval, "");
984 return;
985 } else
986 printf("%d ", free_blk);
987 }
988 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000989}
990
991void do_find_free_inode(int argc, char *argv[])
992{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000993 ext2_ino_t free_inode, dir;
994 int mode;
995 int retval;
996 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000997
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000998 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
999 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000 return;
1001 }
1002 if (check_fs_open(argv[0]))
1003 return;
1004
1005 if (argc > 1) {
1006 dir = strtol(argv[1], &tmp, 0);
1007 if (*tmp) {
1008 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1009 return;
1010 }
1011 }
1012 else
1013 dir = root;
1014 if (argc > 2) {
1015 mode = strtol(argv[2], &tmp, 0);
1016 if (*tmp) {
1017 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1018 return;
1019 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001020 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001021 mode = 010755;
1022
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001023 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001024 if (retval)
1025 com_err("ext2fs_new_inode", retval, "");
1026 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001027 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001028}
1029
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001030static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001031{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001032 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001033 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001034 int got;
1035 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001036 char buf[8192];
1037 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001038
Theodore Ts'o5a513841997-10-25 22:41:14 +00001039 retval = ext2fs_file_open(current_fs, newfile,
1040 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001041 if (retval)
1042 return retval;
1043
Theodore Ts'o5a513841997-10-25 22:41:14 +00001044 while (1) {
1045 got = read(fd, buf, sizeof(buf));
1046 if (got == 0)
1047 break;
1048 if (got < 0) {
1049 retval = errno;
1050 goto fail;
1051 }
1052 ptr = buf;
1053 while (got > 0) {
1054 retval = ext2fs_file_write(e2_file, ptr,
1055 got, &written);
1056 if (retval)
1057 goto fail;
1058
1059 got -= written;
1060 ptr += written;
1061 }
1062 }
1063 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001064 return retval;
1065
1066fail:
1067 (void) ext2fs_file_close(e2_file);
1068 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001069}
1070
Theodore Ts'o5a513841997-10-25 22:41:14 +00001071
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001072void do_write(int argc, char *argv[])
1073{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001074 int fd;
1075 struct stat statbuf;
1076 ext2_ino_t newfile;
1077 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001078 struct ext2_inode inode;
1079
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001080 if (common_args_process(argc, argv, 3, 3, "write",
1081 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001082 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001083
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001084 fd = open(argv[1], O_RDONLY);
1085 if (fd < 0) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001086 com_err(argv[1], errno, "");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001087 return;
1088 }
1089 if (fstat(fd, &statbuf) < 0) {
1090 com_err(argv[1], errno, "");
1091 close(fd);
1092 return;
1093 }
1094
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001095 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1096 if (retval == 0) {
1097 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1098 close(fd);
1099 return;
1100 }
1101
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001102 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001103 if (retval) {
1104 com_err(argv[0], retval, "");
1105 close(fd);
1106 return;
1107 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001108 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001109 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1110 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001111 if (retval == EXT2_ET_DIR_NO_SPACE) {
1112 retval = ext2fs_expand_dir(current_fs, cwd);
1113 if (retval) {
1114 com_err(argv[0], retval, "while expanding directory");
1115 return;
1116 }
1117 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1118 EXT2_FT_REG_FILE);
1119 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001120 if (retval) {
1121 com_err(argv[2], retval, "");
1122 close(fd);
1123 return;
1124 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001125 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001126 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001127 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001128 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001129 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001130 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1131 inode.i_links_count = 1;
1132 inode.i_size = statbuf.st_size;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001133 if (debugfs_write_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001134 close(fd);
1135 return;
1136 }
1137 if (LINUX_S_ISREG(inode.i_mode)) {
1138 retval = copy_file(fd, newfile);
1139 if (retval)
1140 com_err("copy_file", retval, "");
1141 }
1142 close(fd);
1143}
1144
1145void do_mknod(int argc, char *argv[])
1146{
Theodore Ts'o54434922003-12-07 01:28:50 -05001147 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001148 ext2_ino_t newfile;
1149 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001150 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001151 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001152
1153 if (check_fs_open(argv[0]))
1154 return;
1155 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001156 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001157 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1158 return;
1159 }
1160 mode = minor = major = 0;
1161 switch (argv[2][0]) {
1162 case 'p':
1163 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001164 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001165 nr = 3;
1166 break;
1167 case 'c':
1168 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001169 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001170 nr = 5;
1171 break;
1172 case 'b':
1173 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001174 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001175 nr = 5;
1176 break;
1177 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001178 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001179 nr = 0;
1180 }
1181 if (nr == 5) {
1182 major = strtoul(argv[3], argv+3, 0);
1183 minor = strtoul(argv[4], argv+4, 0);
1184 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1185 nr = 0;
1186 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001187 if (argc != nr)
1188 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001189 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001190 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001191 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001192 if (retval) {
1193 com_err(argv[0], retval, "");
1194 return;
1195 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001196 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001197 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001198 if (retval == EXT2_ET_DIR_NO_SPACE) {
1199 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001200 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001201 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001202 return;
1203 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001204 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1205 filetype);
1206 }
1207 if (retval) {
1208 com_err(argv[1], retval, "");
1209 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001210 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001211 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001212 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001213 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1214 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001215 memset(&inode, 0, sizeof(inode));
1216 inode.i_mode = mode;
1217 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1218 inode.i_block[0] = major*256+minor;
1219 inode.i_links_count = 1;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001220 if (debugfs_write_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001221 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001222}
1223
Theodore Ts'o3839e651997-04-26 13:21:57 +00001224void do_mkdir(int argc, char *argv[])
1225{
1226 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001227 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001228 char *name;
1229 errcode_t retval;
1230
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001231 if (common_args_process(argc, argv, 2, 2, "mkdir",
1232 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001233 return;
1234
Theodore Ts'o3839e651997-04-26 13:21:57 +00001235 cp = strrchr(argv[1], '/');
1236 if (cp) {
1237 *cp = 0;
1238 parent = string_to_inode(argv[1]);
1239 if (!parent) {
1240 com_err(argv[1], ENOENT, "");
1241 return;
1242 }
1243 name = cp+1;
1244 } else {
1245 parent = cwd;
1246 name = argv[1];
1247 }
1248
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001249try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001250 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001251 if (retval == EXT2_ET_DIR_NO_SPACE) {
1252 retval = ext2fs_expand_dir(current_fs, parent);
1253 if (retval) {
1254 com_err("argv[0]", retval, "while expanding directory");
1255 return;
1256 }
1257 goto try_again;
1258 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001259 if (retval) {
1260 com_err("ext2fs_mkdir", retval, "");
1261 return;
1262 }
1263
1264}
1265
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001266static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001267 int blockcnt EXT2FS_ATTR((unused)),
1268 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001269{
Theodore Ts'o34436892001-12-22 13:06:02 -05001270 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001271
1272 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001273 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001274 return 0;
1275}
1276
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001277static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001278{
1279 struct ext2_inode inode_buf;
1280
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001281 if (debugfs_read_inode(inode, &inode_buf, 0))
1282 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001283 inode_buf.i_dtime = time(NULL);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001284 if (debugfs_write_inode(inode, &inode_buf, 0))
1285 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001286
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001287 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1288 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001289 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001290 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1291 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001292}
1293
1294
1295void do_kill_file(int argc, char *argv[])
1296{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001297 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001298
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001299 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001300 return;
1301
Theodore Ts'o3839e651997-04-26 13:21:57 +00001302 kill_file_by_inode(inode_num);
1303}
1304
1305void do_rm(int argc, char *argv[])
1306{
1307 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001308 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001309 struct ext2_inode inode;
1310
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001311 if (common_args_process(argc, argv, 2, 2, "rm",
1312 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001313 return;
1314
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001315 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001316 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001317 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001318 return;
1319 }
1320
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001321 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001322 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001323
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001324 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001325 com_err(argv[0], 0, "file is a directory");
1326 return;
1327 }
1328
1329 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001330 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001331 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001332
1333 unlink_file_by_name(argv[1]);
1334 if (inode.i_links_count == 0)
1335 kill_file_by_inode(inode_num);
1336}
1337
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001338struct rd_struct {
1339 ext2_ino_t parent;
1340 int empty;
1341};
1342
Theodore Ts'o54434922003-12-07 01:28:50 -05001343static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1344 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001345 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001346 int offset EXT2FS_ATTR((unused)),
1347 int blocksize EXT2FS_ATTR((unused)),
1348 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001349 void *private)
1350{
1351 struct rd_struct *rds = (struct rd_struct *) private;
1352
1353 if (dirent->inode == 0)
1354 return 0;
1355 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1356 return 0;
1357 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1358 (dirent->name[1] == '.')) {
1359 rds->parent = dirent->inode;
1360 return 0;
1361 }
1362 rds->empty = 0;
1363 return 0;
1364}
1365
1366void do_rmdir(int argc, char *argv[])
1367{
1368 int retval;
1369 ext2_ino_t inode_num;
1370 struct ext2_inode inode;
1371 struct rd_struct rds;
1372
1373 if (common_args_process(argc, argv, 2, 2, "rmdir",
1374 "<filename>", CHECK_FS_RW))
1375 return;
1376
1377 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1378 if (retval) {
1379 com_err(argv[0], retval, "while trying to resolve filename");
1380 return;
1381 }
1382
1383 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1384 return;
1385
1386 if (!LINUX_S_ISDIR(inode.i_mode)) {
1387 com_err(argv[0], 0, "file is not a directory");
1388 return;
1389 }
1390
1391 rds.parent = 0;
1392 rds.empty = 1;
1393
1394 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1395 0, rmdir_proc, &rds);
1396 if (retval) {
1397 com_err(argv[0], retval, "while iterating over directory");
1398 return;
1399 }
1400 if (rds.empty == 0) {
1401 com_err(argv[0], 0, "directory not empty");
1402 return;
1403 }
1404
1405 inode.i_links_count = 0;
1406 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1407 return;
1408
1409 unlink_file_by_name(argv[1]);
1410 kill_file_by_inode(inode_num);
1411
1412 if (rds.parent) {
1413 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1414 return;
1415 if (inode.i_links_count > 1)
1416 inode.i_links_count--;
1417 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1418 return;
1419 }
1420}
1421
Theodore Ts'o54434922003-12-07 01:28:50 -05001422void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1423 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001424{
1425 FILE *out = stdout;
1426
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001427 if (current_fs)
1428 fprintf(out, "Open mode: read-%s\n",
1429 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001430 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001431 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001432}
1433
1434void do_expand_dir(int argc, char *argv[])
1435{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001436 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001437 int retval;
1438
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001439 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001440 return;
1441
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001442 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001443 if (retval)
1444 com_err("ext2fs_expand_dir", retval, "");
1445 return;
1446}
1447
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001448void do_features(int argc, char *argv[])
1449{
1450 int i;
1451
1452 if (check_fs_open(argv[0]))
1453 return;
1454
1455 if ((argc != 1) && check_fs_read_write(argv[0]))
1456 return;
1457 for (i=1; i < argc; i++) {
1458 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001459 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001460 com_err(argv[0], 0, "Unknown feature: %s\n",
1461 argv[i]);
1462 else
1463 ext2fs_mark_super_dirty(current_fs);
1464 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001465 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001466}
1467
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001468void do_bmap(int argc, char *argv[])
1469{
1470 ext2_ino_t ino;
1471 blk_t blk, pblk;
1472 int err;
1473 errcode_t errcode;
1474
1475 if (common_args_process(argc, argv, 3, 3, argv[0],
1476 "<file> logical_blk", 0))
1477 return;
1478
1479 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001480 if (!ino)
1481 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001482 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1483
1484 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1485 if (errcode) {
1486 com_err("argv[0]", errcode,
1487 "while mapping logical block %d\n", blk);
1488 return;
1489 }
1490 printf("%d\n", pblk);
1491}
1492
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001493void do_imap(int argc, char *argv[])
1494{
1495 ext2_ino_t ino;
1496 unsigned long group, block, block_nr, offset;
1497
1498 if (common_args_process(argc, argv, 2, 2, argv[0],
1499 "<file>", 0))
1500 return;
1501 ino = string_to_inode(argv[1]);
1502 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001503 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001504
1505 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1506 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1507 EXT2_INODE_SIZE(current_fs->super);
1508 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1509 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001510 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001511 group);
1512 return;
1513 }
1514 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1515 block;
1516 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1517
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001518 printf("Inode %d is part of block group %lu\n"
1519 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001520 block_nr, offset);
1521
1522}
1523
1524
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001525
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001526static int source_file(const char *cmd_file, int sci_idx)
1527{
1528 FILE *f;
1529 char buf[256];
1530 char *cp;
1531 int exit_status = 0;
1532 int retval;
1533
1534 if (strcmp(cmd_file, "-") == 0)
1535 f = stdin;
1536 else {
1537 f = fopen(cmd_file, "r");
1538 if (!f) {
1539 perror(cmd_file);
1540 exit(1);
1541 }
1542 }
1543 setbuf(stdout, NULL);
1544 setbuf(stderr, NULL);
1545 while (!feof(f)) {
1546 if (fgets(buf, sizeof(buf), f) == NULL)
1547 break;
1548 cp = strchr(buf, '\n');
1549 if (cp)
1550 *cp = 0;
1551 cp = strchr(buf, '\r');
1552 if (cp)
1553 *cp = 0;
1554 printf("debugfs: %s\n", buf);
1555 retval = ss_execute_line(sci_idx, buf);
1556 if (retval) {
1557 ss_perror(sci_idx, retval, buf);
1558 exit_status++;
1559 }
1560 }
1561 return exit_status;
1562}
1563
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001564int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001565{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001566 int retval;
1567 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001568 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 +00001569 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001570 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001571 char *request = 0;
1572 int exit_status = 0;
1573 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001574 blk_t superblock = 0;
1575 blk_t blocksize = 0;
1576 int catastrophic = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001577
1578 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001579 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1580 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001581
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001582 while ((c = getopt (argc, argv, "iwcR:f:b:s:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001583 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001584 case 'R':
1585 request = optarg;
1586 break;
1587 case 'f':
1588 cmd_file = optarg;
1589 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001590 case 'i':
1591 open_flags |= EXT2_FLAG_IMAGE_FILE;
1592 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001593 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001594 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001595 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001596 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001597 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001598 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001599 break;
1600 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001601 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001602 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001603 break;
1604 case 'c':
1605 catastrophic = 1;
1606 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001607 case 'V':
1608 /* Print version number and exit */
1609 fprintf(stderr, "\tUsing %s\n",
1610 error_message(EXT2_ET_BASE));
1611 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001612 default:
1613 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001614 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001615 }
1616 }
1617 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001618 open_filesystem(argv[optind], open_flags,
1619 superblock, blocksize, catastrophic);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001620
1621 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1622 &debug_cmds, &retval);
1623 if (retval) {
1624 ss_perror(sci_idx, retval, "creating invocation");
1625 exit(1);
1626 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001627 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001628
1629 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1630 if (retval) {
1631 ss_perror(sci_idx, retval, "adding standard requests");
1632 exit (1);
1633 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001634 if (request) {
1635 retval = 0;
1636 retval = ss_execute_line(sci_idx, request);
1637 if (retval) {
1638 ss_perror(sci_idx, retval, request);
1639 exit_status++;
1640 }
1641 } else if (cmd_file) {
1642 exit_status = source_file(cmd_file, sci_idx);
1643 } else {
1644 ss_listen(sci_idx);
1645 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001646
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001647 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001648 close_filesystem();
1649
Theodore Ts'oe5973042000-01-18 17:58:34 +00001650 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001651}