blob: bcd9a9be6e219cb3478e1ce7979bbdc022f2ed7c [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
4 *
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
7 *
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
9 */
10
11#include <stdio.h>
12#include <unistd.h>
13#include <stdlib.h>
14#include <ctype.h>
15#include <string.h>
16#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#else
20extern int optind;
21extern char *optarg;
22#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000023#ifdef HAVE_ERRNO_H
24#include <errno.h>
25#endif
26#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <sys/types.h>
28#include <sys/stat.h>
29
30#include "et/com_err.h"
31#include "ss/ss.h"
32#include "debugfs.h"
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000033#include "uuid/uuid.h"
Theodore Ts'of68aa411999-10-26 14:20:22 +000034#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000035
Theodore Ts'oea822ee2005-03-20 18:03:58 -050036#include <ext2fs/ext2_ext_attr.h>
37
Theodore Ts'o818180c1998-06-27 05:11:14 +000038#include "../version.h"
39
Theodore Ts'o3839e651997-04-26 13:21:57 +000040extern ss_request_table debug_cmds;
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -040041ss_request_table *extra_cmds;
42char *debug_prog_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000044ext2_filsys current_fs = NULL;
45ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000046
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000047static void open_filesystem(char *device, int open_flags, blk_t superblock,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040048 blk_t blocksize, int catastrophic,
49 char *data_filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +000050{
51 int retval;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040052 io_channel data_io = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000053
54 if (superblock != 0 && blocksize == 0) {
55 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
56 current_fs = NULL;
57 return;
58 }
59
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040060 if (data_filename) {
61 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
62 com_err(device, 0,
63 "The -d option is only valid when reading an e2image file");
64 current_fs = NULL;
65 return;
66 }
67 retval = unix_io_manager->open(data_filename, 0, &data_io);
68 if (retval) {
69 com_err(data_filename, 0, "while opening data source");
70 current_fs = NULL;
71 return;
72 }
73 }
74
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000075 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
76 com_err(device, 0,
77 "opening read-only because of catastrophic mode");
78 open_flags &= ~EXT2_FLAG_RW;
79 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000080
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000081 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000082 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000083 if (retval) {
84 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000085 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000086 return;
87 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000088
89 if (catastrophic)
90 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
91 else {
92 retval = ext2fs_read_inode_bitmap(current_fs);
93 if (retval) {
94 com_err(device, retval, "while reading inode bitmap");
95 goto errout;
96 }
97 retval = ext2fs_read_block_bitmap(current_fs);
98 if (retval) {
99 com_err(device, retval, "while reading block bitmap");
100 goto errout;
101 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000102 }
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400103
104 if (data_io) {
105 retval = ext2fs_set_data_io(current_fs, data_io);
106 if (retval) {
107 com_err(device, retval,
108 "while setting data source");
109 goto errout;
110 }
111 }
112
Theodore Ts'o3839e651997-04-26 13:21:57 +0000113 root = cwd = EXT2_ROOT_INO;
114 return;
115
116errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000117 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 if (retval)
119 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000120 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121}
122
123void do_open_filesys(int argc, char **argv)
124{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500125 int c, err;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000126 int catastrophic = 0;
127 blk_t superblock = 0;
128 blk_t blocksize = 0;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -0500129 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
Theodore Ts'ob0d17e02004-11-29 17:35:58 -0500130 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400132 reset_getopt();
Theodore Ts'o98eb44b2006-03-18 19:58:13 -0500133 while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134 switch (c) {
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000135 case 'i':
136 open_flags |= EXT2_FLAG_IMAGE_FILE;
137 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000139 open_flags |= EXT2_FLAG_RW;
140 break;
141 case 'f':
142 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000143 break;
Theodore Ts'o98eb44b2006-03-18 19:58:13 -0500144 case 'e':
145 open_flags |= EXT2_FLAG_EXCLUSIVE;
146 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000147 case 'c':
148 catastrophic = 1;
149 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400150 case 'd':
151 data_filename = optarg;
152 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000153 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500154 blocksize = parse_ulong(optarg, argv[0],
155 "block size", &err);
156 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000157 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000158 break;
159 case 's':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500160 superblock = parse_ulong(optarg, argv[0],
161 "superblock number", &err);
162 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000163 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000164 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500166 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000167 }
168 }
169 if (optind != argc-1) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500170 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000171 }
172 if (check_fs_not_open(argv[0]))
173 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000174 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400175 superblock, blocksize, catastrophic,
176 data_filename);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500177 return;
178
179print_usage:
180 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
181 "[-c] [-w] <device>\n", argv[0]);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000182}
183
184void do_lcd(int argc, char **argv)
185{
Theodore Ts'o57a1cbb2007-03-07 08:09:10 -0500186 if (argc != 2) {
187 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000188 return;
Theodore Ts'o57a1cbb2007-03-07 08:09:10 -0500189 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000190
191 if (chdir(argv[1]) == -1) {
192 com_err(argv[0], errno,
193 "while trying to change native directory to %s",
194 argv[1]);
195 return;
196 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197}
198
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000199static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200{
201 int retval;
202
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000203 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
204 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000205 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500206 com_err("ext2fs_write_inode_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000208 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
209 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000210 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500211 com_err("ext2fs_write_block_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000212 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000213 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000214 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500215 com_err("ext2fs_close", retval, 0);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000216 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000217 return;
218}
219
220void do_close_filesys(int argc, char **argv)
221{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500222 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 return;
224 close_filesystem();
225}
226
227void do_init_filesys(int argc, char **argv)
228{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229 struct ext2_super_block param;
230 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500231 int err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500233 if (common_args_process(argc, argv, 3, 3, "initialize",
234 "<device> <blocksize>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000235 return;
236
237 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400238 param.s_blocks_count = parse_ulong(argv[2], argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500239 "blocks count", &err);
240 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000242 retval = ext2fs_initialize(argv[1], 0, &param,
243 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 if (retval) {
245 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000246 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000247 return;
248 }
249 root = cwd = EXT2_ROOT_INO;
250 return;
251}
252
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000253static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000254{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000255 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000256 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000257
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000258 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000259 for (i=0; i <3; i++,mask++) {
260 for (j=0,m=1; j < 32; j++, m<<=1) {
261 if (*mask & m) {
262 fprintf(f, " %s", e2p_feature2string(i, m));
263 printed++;
264 }
265 }
266 }
267 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000268 fputs("(none)", f);
269 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000270}
271
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400272static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
273 const char *str, int *first, FILE *f)
274{
275 if (gdp->bg_flags & mask) {
276 if (*first) {
277 fputs(" [", f);
278 *first = 0;
279 } else
280 fputs(", ", f);
281 fputs(str, f);
282 }
283}
284
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285void do_show_super_stats(int argc, char *argv[])
286{
Theodore Ts'o54434922003-12-07 01:28:50 -0500287 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000288 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000289 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000290 int c, header_only = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400291 int numdirs = 0, first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000292
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400293 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000294 while ((c = getopt (argc, argv, "h")) != EOF) {
295 switch (c) {
296 case 'h':
297 header_only++;
298 break;
299 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500300 goto print_usage;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000301 }
302 }
303 if (optind != argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500304 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 }
306 if (check_fs_open(argv[0]))
307 return;
308 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000309
310 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400311 for (i=0; i < current_fs->group_desc_count; i++)
312 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
313 fprintf(out, "Directories: %d\n", numdirs);
314
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000315 if (header_only) {
316 close_pager(out);
317 return;
318 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000319
320 gdp = &current_fs->group_desc[0];
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400321 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
Takashi Sato8deb80a2006-03-18 21:43:46 -0500322 fprintf(out, " Group %2d: block bitmap at %u, "
323 "inode bitmap at %u, "
324 "inode table at %u\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000325 " %d free %s, "
326 "%d free %s, "
327 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000328 i, gdp->bg_block_bitmap,
329 gdp->bg_inode_bitmap, gdp->bg_inode_table,
330 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000331 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000332 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000333 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000334 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000335 gdp->bg_used_dirs_count != 1 ? "directories"
336 : "directory");
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400337 first = 1;
338 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
339 &first, out);
340 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
341 &first, out);
342 if (!first)
343 fputs("]\n", out);
344 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 close_pager(out);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500346 return;
347print_usage:
348 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349}
350
Theodore Ts'o54434922003-12-07 01:28:50 -0500351void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
352 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000353{
354 if (check_fs_open(argv[0]))
355 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000356 if (check_fs_read_write(argv[0]))
357 return;
358
359 if (argv[1] && !strcmp(argv[1], "-clean"))
360 current_fs->super->s_state |= EXT2_VALID_FS;
361 else
362 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000363 ext2fs_mark_super_dirty(current_fs);
364}
365
Theodore Ts'o3839e651997-04-26 13:21:57 +0000366struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700367 FILE *f;
368 e2_blkcnt_t total;
369 blk_t first_block, last_block;
370 e2_blkcnt_t first_bcnt, last_bcnt;
371 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000372};
373
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000374static void finish_range(struct list_blocks_struct *lb)
375{
376 if (lb->first_block == 0)
377 return;
378 if (lb->first)
379 lb->first = 0;
380 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000381 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000382 if (lb->first_block == lb->last_block)
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400383 fprintf(lb->f, "(%lld):%u",
384 (long long)lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000385 else
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400386 fprintf(lb->f, "(%lld-%lld):%u-%u",
387 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
388 lb->first_block, lb->last_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000389 lb->first_block = 0;
390}
391
Theodore Ts'o54434922003-12-07 01:28:50 -0500392static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
393 blk_t *blocknr, e2_blkcnt_t blockcnt,
394 blk_t ref_block EXT2FS_ATTR((unused)),
395 int ref_offset EXT2FS_ATTR((unused)),
396 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000397{
398 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
399
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000400 lb->total++;
401 if (blockcnt >= 0) {
402 /*
403 * See if we can add on to the existing range (if it exists)
404 */
405 if (lb->first_block &&
406 (lb->last_block+1 == *blocknr) &&
407 (lb->last_bcnt+1 == blockcnt)) {
408 lb->last_block = *blocknr;
409 lb->last_bcnt = blockcnt;
410 return 0;
411 }
412 /*
413 * Start a new range.
414 */
415 finish_range(lb);
416 lb->first_block = lb->last_block = *blocknr;
417 lb->first_bcnt = lb->last_bcnt = blockcnt;
418 return 0;
419 }
420 /*
421 * Not a normal block. Always force a new range.
422 */
423 finish_range(lb);
424 if (lb->first)
425 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000426 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000427 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000428 if (blockcnt == -1)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500429 fprintf(lb->f, "(IND):%u", *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000430 else if (blockcnt == -2)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500431 fprintf(lb->f, "(DIND):%u", *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000432 else if (blockcnt == -3)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500433 fprintf(lb->f, "(TIND):%u", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000434 return 0;
435}
436
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500437static void dump_xattr_string(FILE *out, const char *str, int len)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500438{
439 int printable = 1;
440 int i;
441
442 /* check is string printable? */
443 for (i = 0; i < len; i++)
444 if (!isprint(str[i])) {
445 printable = 0;
446 break;
447 }
448
449 for (i = 0; i < len; i++)
450 if (printable)
Andreas Dilgerd047e072006-06-21 00:01:42 -0400451 fprintf(out, "%c", (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500452 else
Andreas Dilgerd047e072006-06-21 00:01:42 -0400453 fprintf(out, "%02x ", (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500454}
455
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400456static void internal_dump_inode_extra(FILE *out,
457 const char *prefix EXT2FS_ATTR((unused)),
458 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500459 struct ext2_inode_large *inode)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500460{
461 struct ext2_ext_attr_entry *entry;
462 __u32 *magic;
463 char *start, *end;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500464 unsigned int storage_size;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500465
Takashi Sato8deb80a2006-03-18 21:43:46 -0500466 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500467 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
468 EXT2_GOOD_OLD_INODE_SIZE) {
469 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
470 inode->i_extra_isize);
471 return;
472 }
473 storage_size = EXT2_INODE_SIZE(current_fs->super) -
474 EXT2_GOOD_OLD_INODE_SIZE -
475 inode->i_extra_isize;
476 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
477 inode->i_extra_isize);
478 if (*magic == EXT2_EXT_ATTR_MAGIC) {
479 fprintf(out, "Extended attributes stored in inode body: \n");
480 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
481 start = (char *) magic + sizeof(__u32);
482 entry = (struct ext2_ext_attr_entry *) start;
483 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
484 struct ext2_ext_attr_entry *next =
485 EXT2_EXT_ATTR_NEXT(entry);
486 if (entry->e_value_size > storage_size ||
487 (char *) next >= end) {
488 fprintf(out, "invalid EA entry in inode\n");
489 return;
490 }
491 fprintf(out, " ");
492 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
493 entry->e_name_len);
494 fprintf(out, " = \"");
495 dump_xattr_string(out, start + entry->e_value_offs,
496 entry->e_value_size);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500497 fprintf(out, "\" (%u)\n", entry->e_value_size);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500498 entry = next;
499 }
500 }
501}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000503static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504{
505 struct list_blocks_struct lb;
506
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000507 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000509 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000511 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700512 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000513 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000514 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 if (lb.total)
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400516 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 fprintf(f,"\n");
518}
519
520
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000521void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000522 ext2_ino_t inode_num, struct ext2_inode *inode,
523 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000525 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000526 char frag, fsize;
527 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000529 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
530 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
531 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
532 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
533 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
534 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
535 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000537 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
538 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
539 prefix,
540 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
541 fprintf(out, "%sUser: %5d Group: %5d Size: ",
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400542 prefix, inode_uid(*inode), inode_gid(*inode));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700543 if (LINUX_S_ISREG(inode->i_mode)) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400544 unsigned long long i_size = (inode->i_size |
545 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700546
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400547 fprintf(out, "%llu\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700548 } else
549 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500550 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000551 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000552 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
553 prefix,
554 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
555 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000556 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000557 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
558 prefix,
559 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500560 if (os == EXT2_OS_LINUX)
561 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
562 prefix, inode->i_links_count,
563 (((unsigned long long)
564 inode->osd2.linux2.l_i_blocks_hi << 32)) +
565 inode->i_blocks);
566 else
567 fprintf(out, "%sLinks: %d Blockcount: %u\n",
568 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000569 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000570 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000571 frag = inode->osd2.hurd2.h_i_frag;
572 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000573 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000574 default:
575 frag = fsize = 0;
576 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000577 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
578 prefix, inode->i_faddr, frag, fsize);
579 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
580 time_to_string(inode->i_ctime));
581 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
582 time_to_string(inode->i_atime));
583 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
584 time_to_string(inode->i_mtime));
585 if (inode->i_dtime)
586 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
587 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500588 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
589 internal_dump_inode_extra(out, prefix, inode_num,
590 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500591 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000592 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
593 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500594 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
595 int major, minor;
596 const char *devnote;
597
598 if (inode->i_block[0]) {
599 major = (inode->i_block[0] >> 8) & 255;
600 minor = inode->i_block[0] & 255;
601 devnote = "";
602 } else {
603 major = (inode->i_block[1] & 0xfff00) >> 8;
604 minor = ((inode->i_block[1] & 0xff) |
605 ((inode->i_block[1] >> 12) & 0xfff00));
606 devnote = "(New-style) ";
607 }
608 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
609 devnote, major, minor, major, minor);
610 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000611 else if (do_dump_blocks)
612 dump_blocks(out, prefix, inode_num);
613}
614
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500615static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000616{
617 FILE *out;
618
619 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500620 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000621 close_pager(out);
622}
623
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624void do_stat(int argc, char *argv[])
625{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000626 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500627 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628
Theodore Ts'o64777392005-05-05 17:21:46 -0400629 if (check_fs_open(argv[0]))
Theodore Ts'o8363e352005-05-06 09:04:37 -0400630 return;
Theodore Ts'o64777392005-05-05 17:21:46 -0400631
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500632 inode_buf = (struct ext2_inode *)
633 malloc(EXT2_INODE_SIZE(current_fs->super));
634 if (!inode_buf) {
635 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500637 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500639 if (common_inode_args_process(argc, argv, &inode, 0)) {
640 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500641 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500642 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500644 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
645 EXT2_INODE_SIZE(current_fs->super))) {
646 free(inode_buf);
647 return;
648 }
649
650 dump_inode(inode, inode_buf);
651 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 return;
653}
654
655void do_chroot(int argc, char *argv[])
656{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000657 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658 int retval;
659
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500660 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 return;
662
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000663 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500665 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 return;
667 }
668 root = inode;
669}
670
671void do_clri(int argc, char *argv[])
672{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000673 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 struct ext2_inode inode_buf;
675
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500676 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 return;
678
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500679 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000681 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500682 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684}
685
686void do_freei(int argc, char *argv[])
687{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000688 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000689
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500690 if (common_inode_args_process(argc, argv, &inode,
691 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000692 return;
693
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000694 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000695 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000696 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
697 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698}
699
700void do_seti(int argc, char *argv[])
701{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000702 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000703
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500704 if (common_inode_args_process(argc, argv, &inode,
705 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706 return;
707
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000708 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000710 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
711 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712}
713
714void do_testi(int argc, char *argv[])
715{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000716 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500718 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719 return;
720
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000721 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000722 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000724 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725}
726
Theodore Ts'o3839e651997-04-26 13:21:57 +0000727void do_freeb(int argc, char *argv[])
728{
729 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400730 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000731
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500732 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000733 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000734 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000735 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500736 while (count-- > 0) {
737 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500738 com_err(argv[0], 0, "Warning: block %u already clear",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500739 block);
740 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
741 block++;
742 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000743 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744}
745
746void do_setb(int argc, char *argv[])
747{
748 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400749 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000750
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500751 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000753 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500755 while (count-- > 0) {
756 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500757 com_err(argv[0], 0, "Warning: block %u already set",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500758 block);
759 ext2fs_mark_block_bitmap(current_fs->block_map,block);
760 block++;
761 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000762 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000763}
764
765void do_testb(int argc, char *argv[])
766{
767 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400768 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500770 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000771 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500772 while (count-- > 0) {
773 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500774 printf("Block %u marked in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500775 else
Takashi Sato8deb80a2006-03-18 21:43:46 -0500776 printf("Block %u not in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500777 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000779}
780
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000781static void modify_u8(char *com, const char *prompt,
782 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000783{
784 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000785 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000786 char *tmp;
787
788 sprintf(buf, format, *val);
789 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400790 if (!fgets(buf, sizeof(buf), stdin))
791 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000792 if (buf[strlen (buf) - 1] == '\n')
793 buf[strlen (buf) - 1] = '\0';
794 if (!buf[0])
795 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000796 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000797 if (*tmp)
798 com_err(com, 0, "Bad value - %s", buf);
799 else
800 *val = v;
801}
802
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000803static void modify_u16(char *com, const char *prompt,
804 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000805{
806 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000807 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000808 char *tmp;
809
810 sprintf(buf, format, *val);
811 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400812 if (!fgets(buf, sizeof(buf), stdin))
813 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814 if (buf[strlen (buf) - 1] == '\n')
815 buf[strlen (buf) - 1] = '\0';
816 if (!buf[0])
817 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000818 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000819 if (*tmp)
820 com_err(com, 0, "Bad value - %s", buf);
821 else
822 *val = v;
823}
824
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000825static void modify_u32(char *com, const char *prompt,
826 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827{
828 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000829 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000830 char *tmp;
831
832 sprintf(buf, format, *val);
833 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400834 if (!fgets(buf, sizeof(buf), stdin))
835 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000836 if (buf[strlen (buf) - 1] == '\n')
837 buf[strlen (buf) - 1] = '\0';
838 if (!buf[0])
839 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000840 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000841 if (*tmp)
842 com_err(com, 0, "Bad value - %s", buf);
843 else
844 *val = v;
845}
846
847
848void do_modify_inode(int argc, char *argv[])
849{
850 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000851 ext2_ino_t inode_num;
852 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000853 unsigned char *frag, *fsize;
854 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500855 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000856 const char *hex_format = "0x%x";
857 const char *octal_format = "0%o";
858 const char *decimal_format = "%d";
Takashi Sato8deb80a2006-03-18 21:43:46 -0500859 const char *unsignedlong_format = "%lu";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000860
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500861 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000863
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500864 os = current_fs->super->s_creator_os;
865
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500866 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000867 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000868
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000869 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
870 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
871 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500872 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000873 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
874 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
875 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
876 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
877 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500878 if (os == EXT2_OS_LINUX)
879 modify_u16(argv[0], "Block count high", unsignedlong_format,
880 &inode.osd2.linux2.l_i_blocks_hi);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500881 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000882 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000883 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000884#if 0
885 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
886#endif
887 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000888 if (LINUX_S_ISDIR(inode.i_mode))
889 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
890 else
891 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000892
Theodore Ts'o5d171192006-11-11 06:32:03 -0500893 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000894 modify_u32(argv[0], "Translator Block",
895 decimal_format, &inode.osd1.hurd1.h_i_translator);
896
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000897 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000898 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000899 case EXT2_OS_HURD:
900 frag = &inode.osd2.hurd2.h_i_frag;
901 fsize = &inode.osd2.hurd2.h_i_fsize;
902 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000903 default:
904 frag = fsize = 0;
905 }
906 if (frag)
907 modify_u8(argv[0], "Fragment number", decimal_format, frag);
908 if (fsize)
909 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
910
Theodore Ts'o3839e651997-04-26 13:21:57 +0000911 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
912 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000913 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000915 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000917 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000919 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000920 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500921 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000922 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000923}
924
Theodore Ts'o3839e651997-04-26 13:21:57 +0000925void do_change_working_dir(int argc, char *argv[])
926{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000927 ext2_ino_t inode;
928 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500930 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000931 return;
932
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000933 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000934 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500935 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000936 return;
937 }
938 cwd = inode;
939 return;
940}
941
Theodore Ts'o3839e651997-04-26 13:21:57 +0000942void do_print_working_directory(int argc, char *argv[])
943{
944 int retval;
945 char *pathname = NULL;
946
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500947 if (common_args_process(argc, argv, 1, 1,
948 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000949 return;
950
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000951 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000952 if (retval) {
953 com_err(argv[0], retval,
954 "while trying to get pathname of cwd");
955 }
Brian Behlendorf971fe052007-03-29 00:32:23 -0400956 printf("[pwd] INODE: %6u PATH: %s\n",
957 cwd, pathname ? pathname : "NULL");
958 if (pathname) {
959 free(pathname);
960 pathname = NULL;
961 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000962 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000963 if (retval) {
964 com_err(argv[0], retval,
965 "while trying to get pathname of root");
966 }
Brian Behlendorf971fe052007-03-29 00:32:23 -0400967 printf("[root] INODE: %6u PATH: %s\n",
968 root, pathname ? pathname : "NULL");
969 if (pathname) {
970 free(pathname);
971 pathname = NULL;
972 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973 return;
974}
975
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500976/*
977 * Given a mode, return the ext2 file type
978 */
979static int ext2_file_type(unsigned int mode)
980{
981 if (LINUX_S_ISREG(mode))
982 return EXT2_FT_REG_FILE;
983
984 if (LINUX_S_ISDIR(mode))
985 return EXT2_FT_DIR;
986
987 if (LINUX_S_ISCHR(mode))
988 return EXT2_FT_CHRDEV;
989
990 if (LINUX_S_ISBLK(mode))
991 return EXT2_FT_BLKDEV;
992
993 if (LINUX_S_ISLNK(mode))
994 return EXT2_FT_SYMLINK;
995
996 if (LINUX_S_ISFIFO(mode))
997 return EXT2_FT_FIFO;
998
999 if (LINUX_S_ISSOCK(mode))
1000 return EXT2_FT_SOCK;
1001
1002 return 0;
1003}
1004
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001005static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001007 ext2_ino_t ino;
1008 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001009 int retval;
1010 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001011 char *dest, *cp, *base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001012
1013 /*
1014 * Get the source inode
1015 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001016 ino = string_to_inode(sourcename);
1017 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001018 return;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001019 base_name = strrchr(sourcename, '/');
1020 if (base_name)
1021 base_name++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001022 else
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001023 base_name = sourcename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001024 /*
1025 * Figure out the destination. First see if it exists and is
1026 * a directory.
1027 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001028 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001029 dest = base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001030 else {
1031 /*
1032 * OK, it doesn't exist. See if it is
1033 * '<dir>/basename' or 'basename'
1034 */
1035 cp = strrchr(destname, '/');
1036 if (cp) {
1037 *cp = 0;
1038 dir = string_to_inode(destname);
1039 if (!dir)
1040 return;
1041 dest = cp+1;
1042 } else {
1043 dir = cwd;
1044 dest = destname;
1045 }
1046 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001047
1048 if (debugfs_read_inode(ino, &inode, sourcename))
1049 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001050
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001051 retval = ext2fs_link(current_fs, dir, dest, ino,
1052 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001053 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001054 com_err("make_link", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001055 return;
1056}
1057
1058
1059void do_link(int argc, char *argv[])
1060{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001061 if (common_args_process(argc, argv, 3, 3, "link",
1062 "<source file> <dest_name>", CHECK_FS_RW))
1063 return;
1064
1065 make_link(argv[1], argv[2]);
1066}
1067
1068static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001069 int blockcnt EXT2FS_ATTR((unused)),
1070 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001071{
1072 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001073
1074 block = *blocknr;
1075 ext2fs_block_alloc_stats(fs, block, +1);
1076 return 0;
1077}
1078
1079void do_undel(int argc, char *argv[])
1080{
1081 ext2_ino_t ino;
1082 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001083
Theodore Ts'ob026d532008-01-01 11:37:20 -05001084 if (common_args_process(argc, argv, 2, 3, "undelete",
1085 "<inode_num> [dest_name]",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001086 CHECK_FS_RW | CHECK_FS_BITMAPS))
1087 return;
1088
1089 ino = string_to_inode(argv[1]);
1090 if (!ino)
1091 return;
1092
1093 if (debugfs_read_inode(ino, &inode, argv[1]))
1094 return;
1095
1096 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1097 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001098 return;
1099 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001100
1101 /*
1102 * XXX this function doesn't handle changing the links count on the
1103 * parent directory when undeleting a directory.
1104 */
1105 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1106 inode.i_dtime = 0;
1107
1108 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001109 return;
1110
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001111 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1112 mark_blocks_proc, NULL);
1113
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001114 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001115
Theodore Ts'ob026d532008-01-01 11:37:20 -05001116 if (argc > 2)
1117 make_link(argv[1], argv[2]);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001118}
1119
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001120static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001121{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001122 int retval;
1123 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001124 char *base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001125
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001126 base_name = strrchr(filename, '/');
1127 if (base_name) {
1128 *base_name++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001129 dir = string_to_inode(filename);
1130 if (!dir)
1131 return;
1132 } else {
1133 dir = cwd;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001134 base_name = filename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001135 }
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001136 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001137 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001138 com_err("unlink_file_by_name", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001139 return;
1140}
1141
1142void do_unlink(int argc, char *argv[])
1143{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001144 if (common_args_process(argc, argv, 2, 2, "link",
1145 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001146 return;
1147
1148 unlink_file_by_name(argv[1]);
1149}
1150
1151void do_find_free_block(int argc, char *argv[])
1152{
1153 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001154 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001155 errcode_t retval;
1156 char *tmp;
1157
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001158 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1159 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001160 return;
1161 }
1162 if (check_fs_open(argv[0]))
1163 return;
1164
1165 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001166 count = strtol(argv[1],&tmp,0);
1167 if (*tmp) {
1168 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1169 return;
1170 }
1171 } else
1172 count = 1;
1173
1174 if (argc > 2) {
1175 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001176 if (*tmp) {
1177 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1178 return;
1179 }
1180 }
1181 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001182 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001183
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001184 printf("Free blocks found: ");
1185 free_blk = goal - 1;
1186 while (count-- > 0) {
1187 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1188 &free_blk);
1189 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001190 com_err("ext2fs_new_block", retval, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001191 return;
1192 } else
Takashi Sato8deb80a2006-03-18 21:43:46 -05001193 printf("%u ", free_blk);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001194 }
1195 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001196}
1197
1198void do_find_free_inode(int argc, char *argv[])
1199{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001200 ext2_ino_t free_inode, dir;
1201 int mode;
1202 int retval;
1203 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001204
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001205 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1206 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001207 return;
1208 }
1209 if (check_fs_open(argv[0]))
1210 return;
1211
1212 if (argc > 1) {
1213 dir = strtol(argv[1], &tmp, 0);
1214 if (*tmp) {
1215 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1216 return;
1217 }
1218 }
1219 else
1220 dir = root;
1221 if (argc > 2) {
1222 mode = strtol(argv[2], &tmp, 0);
1223 if (*tmp) {
1224 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1225 return;
1226 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001227 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001228 mode = 010755;
1229
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001230 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001231 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001232 com_err("ext2fs_new_inode", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001233 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001234 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001235}
1236
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001237static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001238{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001239 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001240 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001241 int got;
1242 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001243 char buf[8192];
1244 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001245
Theodore Ts'o5a513841997-10-25 22:41:14 +00001246 retval = ext2fs_file_open(current_fs, newfile,
1247 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001248 if (retval)
1249 return retval;
1250
Theodore Ts'o5a513841997-10-25 22:41:14 +00001251 while (1) {
1252 got = read(fd, buf, sizeof(buf));
1253 if (got == 0)
1254 break;
1255 if (got < 0) {
1256 retval = errno;
1257 goto fail;
1258 }
1259 ptr = buf;
1260 while (got > 0) {
1261 retval = ext2fs_file_write(e2_file, ptr,
1262 got, &written);
1263 if (retval)
1264 goto fail;
1265
1266 got -= written;
1267 ptr += written;
1268 }
1269 }
1270 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001271 return retval;
1272
1273fail:
1274 (void) ext2fs_file_close(e2_file);
1275 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001276}
1277
Theodore Ts'o5a513841997-10-25 22:41:14 +00001278
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001279void do_write(int argc, char *argv[])
1280{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001281 int fd;
1282 struct stat statbuf;
1283 ext2_ino_t newfile;
1284 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001285 struct ext2_inode inode;
1286
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001287 if (common_args_process(argc, argv, 3, 3, "write",
1288 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001289 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001290
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001291 fd = open(argv[1], O_RDONLY);
1292 if (fd < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001293 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001294 return;
1295 }
1296 if (fstat(fd, &statbuf) < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001297 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001298 close(fd);
1299 return;
1300 }
1301
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001302 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1303 if (retval == 0) {
1304 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1305 close(fd);
1306 return;
1307 }
1308
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001309 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001310 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001311 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001312 close(fd);
1313 return;
1314 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001315 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001316 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1317 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001318 if (retval == EXT2_ET_DIR_NO_SPACE) {
1319 retval = ext2fs_expand_dir(current_fs, cwd);
1320 if (retval) {
1321 com_err(argv[0], retval, "while expanding directory");
1322 return;
1323 }
1324 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1325 EXT2_FT_REG_FILE);
1326 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001327 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001328 com_err(argv[2], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001329 close(fd);
1330 return;
1331 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001332 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001333 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001334 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001335 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001336 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001337 inode.i_atime = inode.i_ctime = inode.i_mtime =
1338 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001339 inode.i_links_count = 1;
1340 inode.i_size = statbuf.st_size;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001341 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001342 close(fd);
1343 return;
1344 }
1345 if (LINUX_S_ISREG(inode.i_mode)) {
1346 retval = copy_file(fd, newfile);
1347 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001348 com_err("copy_file", retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001349 }
1350 close(fd);
1351}
1352
1353void do_mknod(int argc, char *argv[])
1354{
Theodore Ts'o54434922003-12-07 01:28:50 -05001355 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001356 ext2_ino_t newfile;
1357 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001358 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001359 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001360
1361 if (check_fs_open(argv[0]))
1362 return;
1363 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001364 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001365 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1366 return;
1367 }
1368 mode = minor = major = 0;
1369 switch (argv[2][0]) {
1370 case 'p':
1371 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001372 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001373 nr = 3;
1374 break;
1375 case 'c':
1376 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001377 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001378 nr = 5;
1379 break;
1380 case 'b':
1381 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001382 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001383 nr = 5;
1384 break;
1385 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001386 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001387 nr = 0;
1388 }
1389 if (nr == 5) {
1390 major = strtoul(argv[3], argv+3, 0);
1391 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001392 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001393 nr = 0;
1394 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001395 if (argc != nr)
1396 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001397 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001398 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001399 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001400 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001401 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001402 return;
1403 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001404 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001405 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001406 if (retval == EXT2_ET_DIR_NO_SPACE) {
1407 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001408 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001409 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001410 return;
1411 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001412 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1413 filetype);
1414 }
1415 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001416 com_err(argv[1], retval, 0);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001417 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001418 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001419 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001420 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001421 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1422 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001423 memset(&inode, 0, sizeof(inode));
1424 inode.i_mode = mode;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001425 inode.i_atime = inode.i_ctime = inode.i_mtime =
1426 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001427 if ((major < 256) && (minor < 256)) {
1428 inode.i_block[0] = major*256+minor;
1429 inode.i_block[1] = 0;
1430 } else {
1431 inode.i_block[0] = 0;
1432 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1433 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001434 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001435 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001436 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001437}
1438
Theodore Ts'o3839e651997-04-26 13:21:57 +00001439void do_mkdir(int argc, char *argv[])
1440{
1441 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001442 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001443 char *name;
1444 errcode_t retval;
1445
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001446 if (common_args_process(argc, argv, 2, 2, "mkdir",
1447 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001448 return;
1449
Theodore Ts'o3839e651997-04-26 13:21:57 +00001450 cp = strrchr(argv[1], '/');
1451 if (cp) {
1452 *cp = 0;
1453 parent = string_to_inode(argv[1]);
1454 if (!parent) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001455 com_err(argv[1], ENOENT, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001456 return;
1457 }
1458 name = cp+1;
1459 } else {
1460 parent = cwd;
1461 name = argv[1];
1462 }
1463
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001464try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001465 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001466 if (retval == EXT2_ET_DIR_NO_SPACE) {
1467 retval = ext2fs_expand_dir(current_fs, parent);
1468 if (retval) {
1469 com_err("argv[0]", retval, "while expanding directory");
1470 return;
1471 }
1472 goto try_again;
1473 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001474 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001475 com_err("ext2fs_mkdir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001476 return;
1477 }
1478
1479}
1480
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001481static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001482 int blockcnt EXT2FS_ATTR((unused)),
1483 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001484{
Theodore Ts'o34436892001-12-22 13:06:02 -05001485 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001486
1487 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001488 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001489 return 0;
1490}
1491
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001492static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001493{
1494 struct ext2_inode inode_buf;
1495
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001496 if (debugfs_read_inode(inode, &inode_buf, 0))
1497 return;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001498 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001499 if (debugfs_write_inode(inode, &inode_buf, 0))
1500 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001501 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1502 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001503
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001504 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1505 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001506 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001507 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1508 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001509}
1510
1511
1512void do_kill_file(int argc, char *argv[])
1513{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001514 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001515
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001516 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001517 return;
1518
Theodore Ts'o3839e651997-04-26 13:21:57 +00001519 kill_file_by_inode(inode_num);
1520}
1521
1522void do_rm(int argc, char *argv[])
1523{
1524 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001525 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001526 struct ext2_inode inode;
1527
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001528 if (common_args_process(argc, argv, 2, 2, "rm",
1529 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001530 return;
1531
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001532 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001533 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001534 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001535 return;
1536 }
1537
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001538 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001539 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001540
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001541 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001542 com_err(argv[0], 0, "file is a directory");
1543 return;
1544 }
1545
1546 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001547 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001548 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001549
1550 unlink_file_by_name(argv[1]);
1551 if (inode.i_links_count == 0)
1552 kill_file_by_inode(inode_num);
1553}
1554
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001555struct rd_struct {
1556 ext2_ino_t parent;
1557 int empty;
1558};
1559
Theodore Ts'o54434922003-12-07 01:28:50 -05001560static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1561 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001562 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001563 int offset EXT2FS_ATTR((unused)),
1564 int blocksize EXT2FS_ATTR((unused)),
1565 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001566 void *private)
1567{
1568 struct rd_struct *rds = (struct rd_struct *) private;
1569
1570 if (dirent->inode == 0)
1571 return 0;
1572 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1573 return 0;
1574 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1575 (dirent->name[1] == '.')) {
1576 rds->parent = dirent->inode;
1577 return 0;
1578 }
1579 rds->empty = 0;
1580 return 0;
1581}
1582
1583void do_rmdir(int argc, char *argv[])
1584{
1585 int retval;
1586 ext2_ino_t inode_num;
1587 struct ext2_inode inode;
1588 struct rd_struct rds;
1589
1590 if (common_args_process(argc, argv, 2, 2, "rmdir",
1591 "<filename>", CHECK_FS_RW))
1592 return;
1593
1594 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1595 if (retval) {
1596 com_err(argv[0], retval, "while trying to resolve filename");
1597 return;
1598 }
1599
1600 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1601 return;
1602
1603 if (!LINUX_S_ISDIR(inode.i_mode)) {
1604 com_err(argv[0], 0, "file is not a directory");
1605 return;
1606 }
1607
1608 rds.parent = 0;
1609 rds.empty = 1;
1610
1611 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1612 0, rmdir_proc, &rds);
1613 if (retval) {
1614 com_err(argv[0], retval, "while iterating over directory");
1615 return;
1616 }
1617 if (rds.empty == 0) {
1618 com_err(argv[0], 0, "directory not empty");
1619 return;
1620 }
1621
1622 inode.i_links_count = 0;
1623 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1624 return;
1625
1626 unlink_file_by_name(argv[1]);
1627 kill_file_by_inode(inode_num);
1628
1629 if (rds.parent) {
1630 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1631 return;
1632 if (inode.i_links_count > 1)
1633 inode.i_links_count--;
1634 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1635 return;
1636 }
1637}
1638
Theodore Ts'o54434922003-12-07 01:28:50 -05001639void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1640 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001641{
1642 FILE *out = stdout;
1643
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001644 if (current_fs)
1645 fprintf(out, "Open mode: read-%s\n",
1646 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001647 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001648 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001649}
1650
1651void do_expand_dir(int argc, char *argv[])
1652{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001653 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001654 int retval;
1655
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001656 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001657 return;
1658
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001659 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001660 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001661 com_err("ext2fs_expand_dir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001662 return;
1663}
1664
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001665void do_features(int argc, char *argv[])
1666{
1667 int i;
1668
1669 if (check_fs_open(argv[0]))
1670 return;
1671
1672 if ((argc != 1) && check_fs_read_write(argv[0]))
1673 return;
1674 for (i=1; i < argc; i++) {
1675 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001676 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001677 com_err(argv[0], 0, "Unknown feature: %s\n",
1678 argv[i]);
1679 else
1680 ext2fs_mark_super_dirty(current_fs);
1681 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001682 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001683}
1684
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001685void do_bmap(int argc, char *argv[])
1686{
1687 ext2_ino_t ino;
1688 blk_t blk, pblk;
1689 int err;
1690 errcode_t errcode;
1691
1692 if (common_args_process(argc, argv, 3, 3, argv[0],
1693 "<file> logical_blk", 0))
1694 return;
1695
1696 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001697 if (!ino)
1698 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001699 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1700
1701 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1702 if (errcode) {
1703 com_err("argv[0]", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -05001704 "while mapping logical block %u\n", blk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001705 return;
1706 }
Takashi Sato8deb80a2006-03-18 21:43:46 -05001707 printf("%u\n", pblk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001708}
1709
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001710void do_imap(int argc, char *argv[])
1711{
1712 ext2_ino_t ino;
1713 unsigned long group, block, block_nr, offset;
1714
1715 if (common_args_process(argc, argv, 2, 2, argv[0],
1716 "<file>", 0))
1717 return;
1718 ino = string_to_inode(argv[1]);
1719 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001720 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001721
1722 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1723 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1724 EXT2_INODE_SIZE(current_fs->super);
1725 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1726 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001727 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001728 group);
1729 return;
1730 }
1731 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1732 block;
1733 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1734
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001735 printf("Inode %d is part of block group %lu\n"
1736 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001737 block_nr, offset);
1738
1739}
1740
Theodore Ts'o4efae602005-09-24 21:56:38 -04001741void do_set_current_time(int argc, char *argv[])
1742{
Theodore Ts'o4efae602005-09-24 21:56:38 -04001743 time_t now;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001744
Theodore Ts'o4efae602005-09-24 21:56:38 -04001745 if (common_args_process(argc, argv, 2, 2, argv[0],
1746 "<time>", 0))
1747 return;
1748
1749 now = string_to_time(argv[1]);
1750 if (now == ((time_t) -1)) {
1751 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1752 argv[1]);
1753 return;
1754
1755 } else {
1756 printf("Setting current time to %s\n", time_to_string(now));
1757 current_fs->now = now;
1758 }
1759}
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001760
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001761static int source_file(const char *cmd_file, int sci_idx)
1762{
1763 FILE *f;
1764 char buf[256];
1765 char *cp;
1766 int exit_status = 0;
1767 int retval;
1768
1769 if (strcmp(cmd_file, "-") == 0)
1770 f = stdin;
1771 else {
1772 f = fopen(cmd_file, "r");
1773 if (!f) {
1774 perror(cmd_file);
1775 exit(1);
1776 }
1777 }
1778 setbuf(stdout, NULL);
1779 setbuf(stderr, NULL);
1780 while (!feof(f)) {
1781 if (fgets(buf, sizeof(buf), f) == NULL)
1782 break;
1783 cp = strchr(buf, '\n');
1784 if (cp)
1785 *cp = 0;
1786 cp = strchr(buf, '\r');
1787 if (cp)
1788 *cp = 0;
1789 printf("debugfs: %s\n", buf);
1790 retval = ss_execute_line(sci_idx, buf);
1791 if (retval) {
1792 ss_perror(sci_idx, retval, buf);
1793 exit_status++;
1794 }
1795 }
1796 return exit_status;
1797}
1798
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001799int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001800{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001801 int retval;
1802 int sci_idx;
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001803 const char *usage = "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00001804 int c;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -05001805 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001806 char *request = 0;
1807 int exit_status = 0;
1808 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001809 blk_t superblock = 0;
1810 blk_t blocksize = 0;
1811 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001812 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001813
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001814 if (debug_prog_name == 0)
1815 debug_prog_name = "debugfs";
1816
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001817 add_error_table(&et_ext2_error_table);
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001818 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
1819 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001820
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001821 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001822 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001823 case 'R':
1824 request = optarg;
1825 break;
1826 case 'f':
1827 cmd_file = optarg;
1828 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001829 case 'd':
1830 data_filename = optarg;
1831 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001832 case 'i':
1833 open_flags |= EXT2_FLAG_IMAGE_FILE;
1834 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001835 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001836 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001837 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001838 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001839 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001840 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001841 break;
1842 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001843 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001844 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001845 break;
1846 case 'c':
1847 catastrophic = 1;
1848 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001849 case 'V':
1850 /* Print version number and exit */
1851 fprintf(stderr, "\tUsing %s\n",
1852 error_message(EXT2_ET_BASE));
1853 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001854 default:
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001855 com_err(argv[0], 0, usage, debug_prog_name);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001856 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001857 }
1858 }
1859 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001860 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001861 superblock, blocksize, catastrophic,
1862 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001863
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001864 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
Theodore Ts'o3839e651997-04-26 13:21:57 +00001865 &debug_cmds, &retval);
1866 if (retval) {
1867 ss_perror(sci_idx, retval, "creating invocation");
1868 exit(1);
1869 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001870 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001871
1872 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1873 if (retval) {
1874 ss_perror(sci_idx, retval, "adding standard requests");
1875 exit (1);
1876 }
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001877 if (extra_cmds)
1878 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
1879 if (retval) {
1880 ss_perror(sci_idx, retval, "adding extra requests");
1881 exit (1);
1882 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001883 if (request) {
1884 retval = 0;
1885 retval = ss_execute_line(sci_idx, request);
1886 if (retval) {
1887 ss_perror(sci_idx, retval, request);
1888 exit_status++;
1889 }
1890 } else if (cmd_file) {
1891 exit_status = source_file(cmd_file, sci_idx);
1892 } else {
1893 ss_listen(sci_idx);
1894 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001895
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001896 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001897 close_filesystem();
1898
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001899 remove_error_table(&et_ext2_error_table);
Theodore Ts'oe5973042000-01-18 17:58:34 +00001900 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001901}