blob: 1ed8615348dc3756b25ced122784f74bf43149ae [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;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -040042const char *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{
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600439 int printable = 0;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500440 int i;
441
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600442 /* check: is string "printable enough?" */
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500443 for (i = 0; i < len; i++)
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600444 if (isprint(str[i]))
445 printable++;
446
447 if (printable <= len*7/8)
448 printable = 0;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500449
450 for (i = 0; i < len; i++)
451 if (printable)
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600452 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
453 (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500454 else
Andreas Dilgerd047e072006-06-21 00:01:42 -0400455 fprintf(out, "%02x ", (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500456}
457
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400458static void internal_dump_inode_extra(FILE *out,
459 const char *prefix EXT2FS_ATTR((unused)),
460 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500461 struct ext2_inode_large *inode)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500462{
463 struct ext2_ext_attr_entry *entry;
464 __u32 *magic;
465 char *start, *end;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500466 unsigned int storage_size;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500467
Takashi Sato8deb80a2006-03-18 21:43:46 -0500468 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500469 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
470 EXT2_GOOD_OLD_INODE_SIZE) {
471 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
472 inode->i_extra_isize);
473 return;
474 }
475 storage_size = EXT2_INODE_SIZE(current_fs->super) -
476 EXT2_GOOD_OLD_INODE_SIZE -
477 inode->i_extra_isize;
478 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
479 inode->i_extra_isize);
480 if (*magic == EXT2_EXT_ATTR_MAGIC) {
481 fprintf(out, "Extended attributes stored in inode body: \n");
482 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
483 start = (char *) magic + sizeof(__u32);
484 entry = (struct ext2_ext_attr_entry *) start;
485 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
486 struct ext2_ext_attr_entry *next =
487 EXT2_EXT_ATTR_NEXT(entry);
488 if (entry->e_value_size > storage_size ||
489 (char *) next >= end) {
490 fprintf(out, "invalid EA entry in inode\n");
491 return;
492 }
493 fprintf(out, " ");
494 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
495 entry->e_name_len);
496 fprintf(out, " = \"");
497 dump_xattr_string(out, start + entry->e_value_offs,
498 entry->e_value_size);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500499 fprintf(out, "\" (%u)\n", entry->e_value_size);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500500 entry = next;
501 }
502 }
503}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000505static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506{
507 struct list_blocks_struct lb;
508
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000509 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000510 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000511 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000512 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000513 lb.first = 1;
Theodore Ts'o43323be2008-02-07 14:37:17 -0500514 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000515 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000516 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000517 if (lb.total)
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400518 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 fprintf(f,"\n");
520}
521
522
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000523void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000524 ext2_ino_t inode_num, struct ext2_inode *inode,
525 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000527 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000528 char frag, fsize;
529 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000531 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
532 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
533 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
534 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
535 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
536 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
537 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000539 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
540 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
541 prefix,
542 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
543 fprintf(out, "%sUser: %5d Group: %5d Size: ",
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400544 prefix, inode_uid(*inode), inode_gid(*inode));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700545 if (LINUX_S_ISREG(inode->i_mode)) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400546 unsigned long long i_size = (inode->i_size |
547 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700548
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400549 fprintf(out, "%llu\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700550 } else
551 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500552 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000553 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000554 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
555 prefix,
556 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
557 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000558 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000559 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
560 prefix,
561 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500562 if (os == EXT2_OS_LINUX)
563 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
564 prefix, inode->i_links_count,
565 (((unsigned long long)
566 inode->osd2.linux2.l_i_blocks_hi << 32)) +
567 inode->i_blocks);
568 else
569 fprintf(out, "%sLinks: %d Blockcount: %u\n",
570 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000571 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000572 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000573 frag = inode->osd2.hurd2.h_i_frag;
574 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000575 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000576 default:
577 frag = fsize = 0;
578 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000579 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
580 prefix, inode->i_faddr, frag, fsize);
581 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
582 time_to_string(inode->i_ctime));
583 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
584 time_to_string(inode->i_atime));
585 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
586 time_to_string(inode->i_mtime));
587 if (inode->i_dtime)
588 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
589 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500590 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
591 internal_dump_inode_extra(out, prefix, inode_num,
592 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500593 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000594 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
595 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500596 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
597 int major, minor;
598 const char *devnote;
599
600 if (inode->i_block[0]) {
601 major = (inode->i_block[0] >> 8) & 255;
602 minor = inode->i_block[0] & 255;
603 devnote = "";
604 } else {
605 major = (inode->i_block[1] & 0xfff00) >> 8;
606 minor = ((inode->i_block[1] & 0xff) |
607 ((inode->i_block[1] >> 12) & 0xfff00));
608 devnote = "(New-style) ";
609 }
610 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
611 devnote, major, minor, major, minor);
612 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000613 else if (do_dump_blocks)
614 dump_blocks(out, prefix, inode_num);
615}
616
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500617static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000618{
619 FILE *out;
620
621 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500622 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623 close_pager(out);
624}
625
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626void do_stat(int argc, char *argv[])
627{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000628 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500629 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630
Theodore Ts'o64777392005-05-05 17:21:46 -0400631 if (check_fs_open(argv[0]))
Theodore Ts'o8363e352005-05-06 09:04:37 -0400632 return;
Theodore Ts'o64777392005-05-05 17:21:46 -0400633
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500634 inode_buf = (struct ext2_inode *)
635 malloc(EXT2_INODE_SIZE(current_fs->super));
636 if (!inode_buf) {
637 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500639 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000640
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500641 if (common_inode_args_process(argc, argv, &inode, 0)) {
642 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500643 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500644 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500646 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
647 EXT2_INODE_SIZE(current_fs->super))) {
648 free(inode_buf);
649 return;
650 }
651
652 dump_inode(inode, inode_buf);
653 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 return;
655}
656
657void do_chroot(int argc, char *argv[])
658{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000659 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660 int retval;
661
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500662 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 return;
664
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000665 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500667 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668 return;
669 }
670 root = inode;
671}
672
673void do_clri(int argc, char *argv[])
674{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000675 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000676 struct ext2_inode inode_buf;
677
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500678 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000679 return;
680
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500681 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000682 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500684 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000686}
687
688void do_freei(int argc, char *argv[])
689{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000690 ext2_ino_t inode;
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,
693 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694 return;
695
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000696 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000697 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000698 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
699 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700}
701
702void do_seti(int argc, char *argv[])
703{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000704 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500706 if (common_inode_args_process(argc, argv, &inode,
707 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000708 return;
709
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000710 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000711 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000712 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
713 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714}
715
716void do_testi(int argc, char *argv[])
717{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000718 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500720 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000721 return;
722
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000723 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000724 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000726 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000727}
728
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729void do_freeb(int argc, char *argv[])
730{
731 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400732 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000733
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500734 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000735 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000736 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500738 while (count-- > 0) {
739 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500740 com_err(argv[0], 0, "Warning: block %u already clear",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500741 block);
742 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
743 block++;
744 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000745 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000746}
747
748void do_setb(int argc, char *argv[])
749{
750 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400751 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500753 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000755 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000756 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500757 while (count-- > 0) {
758 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500759 com_err(argv[0], 0, "Warning: block %u already set",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500760 block);
761 ext2fs_mark_block_bitmap(current_fs->block_map,block);
762 block++;
763 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000764 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000765}
766
767void do_testb(int argc, char *argv[])
768{
769 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400770 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000771
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500772 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000773 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500774 while (count-- > 0) {
775 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500776 printf("Block %u marked in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500777 else
Takashi Sato8deb80a2006-03-18 21:43:46 -0500778 printf("Block %u not in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500779 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000780 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781}
782
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000783static void modify_u8(char *com, const char *prompt,
784 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000785{
786 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000787 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000788 char *tmp;
789
790 sprintf(buf, format, *val);
791 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400792 if (!fgets(buf, sizeof(buf), stdin))
793 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000794 if (buf[strlen (buf) - 1] == '\n')
795 buf[strlen (buf) - 1] = '\0';
796 if (!buf[0])
797 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000798 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799 if (*tmp)
800 com_err(com, 0, "Bad value - %s", buf);
801 else
802 *val = v;
803}
804
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000805static void modify_u16(char *com, const char *prompt,
806 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000807{
808 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000809 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810 char *tmp;
811
812 sprintf(buf, format, *val);
813 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400814 if (!fgets(buf, sizeof(buf), stdin))
815 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000816 if (buf[strlen (buf) - 1] == '\n')
817 buf[strlen (buf) - 1] = '\0';
818 if (!buf[0])
819 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000820 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821 if (*tmp)
822 com_err(com, 0, "Bad value - %s", buf);
823 else
824 *val = v;
825}
826
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000827static void modify_u32(char *com, const char *prompt,
828 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829{
830 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000831 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000832 char *tmp;
833
834 sprintf(buf, format, *val);
835 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400836 if (!fgets(buf, sizeof(buf), stdin))
837 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 if (buf[strlen (buf) - 1] == '\n')
839 buf[strlen (buf) - 1] = '\0';
840 if (!buf[0])
841 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000842 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000843 if (*tmp)
844 com_err(com, 0, "Bad value - %s", buf);
845 else
846 *val = v;
847}
848
849
850void do_modify_inode(int argc, char *argv[])
851{
852 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000853 ext2_ino_t inode_num;
854 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000855 unsigned char *frag, *fsize;
856 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500857 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000858 const char *hex_format = "0x%x";
859 const char *octal_format = "0%o";
860 const char *decimal_format = "%d";
Takashi Sato8deb80a2006-03-18 21:43:46 -0500861 const char *unsignedlong_format = "%lu";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500863 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000864 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000865
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500866 os = current_fs->super->s_creator_os;
867
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500868 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000869 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000870
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000871 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
872 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
873 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500874 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000875 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
876 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
877 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
878 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
879 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500880 if (os == EXT2_OS_LINUX)
881 modify_u16(argv[0], "Block count high", unsignedlong_format,
882 &inode.osd2.linux2.l_i_blocks_hi);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500883 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000884 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000885 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000886#if 0
887 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
888#endif
889 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000890 if (LINUX_S_ISDIR(inode.i_mode))
891 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
892 else
893 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000894
Theodore Ts'o5d171192006-11-11 06:32:03 -0500895 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000896 modify_u32(argv[0], "Translator Block",
897 decimal_format, &inode.osd1.hurd1.h_i_translator);
898
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000899 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000900 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000901 case EXT2_OS_HURD:
902 frag = &inode.osd2.hurd2.h_i_frag;
903 fsize = &inode.osd2.hurd2.h_i_fsize;
904 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000905 default:
906 frag = fsize = 0;
907 }
908 if (frag)
909 modify_u8(argv[0], "Fragment number", decimal_format, frag);
910 if (fsize)
911 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
912
Theodore Ts'o3839e651997-04-26 13:21:57 +0000913 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
914 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000915 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000917 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000919 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000920 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000921 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000922 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500923 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000924 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000925}
926
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927void do_change_working_dir(int argc, char *argv[])
928{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000929 ext2_ino_t inode;
930 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000931
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500932 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000933 return;
934
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000935 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000936 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500937 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000938 return;
939 }
940 cwd = inode;
941 return;
942}
943
Theodore Ts'o3839e651997-04-26 13:21:57 +0000944void do_print_working_directory(int argc, char *argv[])
945{
946 int retval;
947 char *pathname = NULL;
948
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500949 if (common_args_process(argc, argv, 1, 1,
950 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000951 return;
952
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000953 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000954 if (retval) {
955 com_err(argv[0], retval,
956 "while trying to get pathname of cwd");
957 }
Brian Behlendorf971fe052007-03-29 00:32:23 -0400958 printf("[pwd] INODE: %6u PATH: %s\n",
959 cwd, pathname ? pathname : "NULL");
960 if (pathname) {
961 free(pathname);
962 pathname = NULL;
963 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000964 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000965 if (retval) {
966 com_err(argv[0], retval,
967 "while trying to get pathname of root");
968 }
Brian Behlendorf971fe052007-03-29 00:32:23 -0400969 printf("[root] INODE: %6u PATH: %s\n",
970 root, pathname ? pathname : "NULL");
971 if (pathname) {
972 free(pathname);
973 pathname = NULL;
974 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000975 return;
976}
977
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500978/*
979 * Given a mode, return the ext2 file type
980 */
981static int ext2_file_type(unsigned int mode)
982{
983 if (LINUX_S_ISREG(mode))
984 return EXT2_FT_REG_FILE;
985
986 if (LINUX_S_ISDIR(mode))
987 return EXT2_FT_DIR;
988
989 if (LINUX_S_ISCHR(mode))
990 return EXT2_FT_CHRDEV;
991
992 if (LINUX_S_ISBLK(mode))
993 return EXT2_FT_BLKDEV;
994
995 if (LINUX_S_ISLNK(mode))
996 return EXT2_FT_SYMLINK;
997
998 if (LINUX_S_ISFIFO(mode))
999 return EXT2_FT_FIFO;
1000
1001 if (LINUX_S_ISSOCK(mode))
1002 return EXT2_FT_SOCK;
1003
1004 return 0;
1005}
1006
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001007static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001008{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001009 ext2_ino_t ino;
1010 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001011 int retval;
1012 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001013 char *dest, *cp, *base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001014
1015 /*
1016 * Get the source inode
1017 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001018 ino = string_to_inode(sourcename);
1019 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001020 return;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001021 base_name = strrchr(sourcename, '/');
1022 if (base_name)
1023 base_name++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001024 else
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001025 base_name = sourcename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001026 /*
1027 * Figure out the destination. First see if it exists and is
1028 * a directory.
1029 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001030 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001031 dest = base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001032 else {
1033 /*
1034 * OK, it doesn't exist. See if it is
1035 * '<dir>/basename' or 'basename'
1036 */
1037 cp = strrchr(destname, '/');
1038 if (cp) {
1039 *cp = 0;
1040 dir = string_to_inode(destname);
1041 if (!dir)
1042 return;
1043 dest = cp+1;
1044 } else {
1045 dir = cwd;
1046 dest = destname;
1047 }
1048 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001049
1050 if (debugfs_read_inode(ino, &inode, sourcename))
1051 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001053 retval = ext2fs_link(current_fs, dir, dest, ino,
1054 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001055 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001056 com_err("make_link", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001057 return;
1058}
1059
1060
1061void do_link(int argc, char *argv[])
1062{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001063 if (common_args_process(argc, argv, 3, 3, "link",
1064 "<source file> <dest_name>", CHECK_FS_RW))
1065 return;
1066
1067 make_link(argv[1], argv[2]);
1068}
1069
1070static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001071 int blockcnt EXT2FS_ATTR((unused)),
1072 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001073{
1074 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001075
1076 block = *blocknr;
1077 ext2fs_block_alloc_stats(fs, block, +1);
1078 return 0;
1079}
1080
1081void do_undel(int argc, char *argv[])
1082{
1083 ext2_ino_t ino;
1084 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001085
Theodore Ts'ob026d532008-01-01 11:37:20 -05001086 if (common_args_process(argc, argv, 2, 3, "undelete",
1087 "<inode_num> [dest_name]",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001088 CHECK_FS_RW | CHECK_FS_BITMAPS))
1089 return;
1090
1091 ino = string_to_inode(argv[1]);
1092 if (!ino)
1093 return;
1094
1095 if (debugfs_read_inode(ino, &inode, argv[1]))
1096 return;
1097
1098 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1099 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001100 return;
1101 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001102
1103 /*
1104 * XXX this function doesn't handle changing the links count on the
1105 * parent directory when undeleting a directory.
1106 */
1107 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1108 inode.i_dtime = 0;
1109
1110 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001111 return;
1112
Theodore Ts'o43323be2008-02-07 14:37:17 -05001113 ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001114 mark_blocks_proc, NULL);
1115
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001116 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001117
Theodore Ts'ob026d532008-01-01 11:37:20 -05001118 if (argc > 2)
1119 make_link(argv[1], argv[2]);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001120}
1121
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001122static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001123{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001124 int retval;
1125 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001126 char *base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001127
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001128 base_name = strrchr(filename, '/');
1129 if (base_name) {
1130 *base_name++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001131 dir = string_to_inode(filename);
1132 if (!dir)
1133 return;
1134 } else {
1135 dir = cwd;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001136 base_name = filename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001137 }
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001138 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001139 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001140 com_err("unlink_file_by_name", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001141 return;
1142}
1143
1144void do_unlink(int argc, char *argv[])
1145{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001146 if (common_args_process(argc, argv, 2, 2, "link",
1147 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001148 return;
1149
1150 unlink_file_by_name(argv[1]);
1151}
1152
1153void do_find_free_block(int argc, char *argv[])
1154{
Theodore Ts'o5aae7c22008-02-27 13:38:56 -05001155 blk_t free_blk, goal, first_free = 0;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001156 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001157 errcode_t retval;
1158 char *tmp;
1159
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001160 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1161 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001162 return;
1163 }
1164 if (check_fs_open(argv[0]))
1165 return;
1166
1167 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001168 count = strtol(argv[1],&tmp,0);
1169 if (*tmp) {
1170 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1171 return;
1172 }
1173 } else
1174 count = 1;
1175
1176 if (argc > 2) {
1177 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001178 if (*tmp) {
1179 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1180 return;
1181 }
1182 }
1183 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001184 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001185
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001186 printf("Free blocks found: ");
1187 free_blk = goal - 1;
1188 while (count-- > 0) {
1189 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1190 &free_blk);
Theodore Ts'o5aae7c22008-02-27 13:38:56 -05001191 if (first_free) {
1192 if (first_free == free_blk)
1193 break;
1194 } else
1195 first_free = free_blk;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001196 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001197 com_err("ext2fs_new_block", retval, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001198 return;
1199 } else
Takashi Sato8deb80a2006-03-18 21:43:46 -05001200 printf("%u ", free_blk);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001201 }
1202 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001203}
1204
1205void do_find_free_inode(int argc, char *argv[])
1206{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001207 ext2_ino_t free_inode, dir;
1208 int mode;
1209 int retval;
1210 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001211
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001212 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1213 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001214 return;
1215 }
1216 if (check_fs_open(argv[0]))
1217 return;
1218
1219 if (argc > 1) {
1220 dir = strtol(argv[1], &tmp, 0);
1221 if (*tmp) {
1222 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1223 return;
1224 }
1225 }
1226 else
1227 dir = root;
1228 if (argc > 2) {
1229 mode = strtol(argv[2], &tmp, 0);
1230 if (*tmp) {
1231 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1232 return;
1233 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001234 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001235 mode = 010755;
1236
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001237 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001238 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001239 com_err("ext2fs_new_inode", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001240 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001241 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001242}
1243
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001244static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001245{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001246 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001247 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001248 int got;
1249 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001250 char buf[8192];
1251 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001252
Theodore Ts'o5a513841997-10-25 22:41:14 +00001253 retval = ext2fs_file_open(current_fs, newfile,
1254 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001255 if (retval)
1256 return retval;
1257
Theodore Ts'o5a513841997-10-25 22:41:14 +00001258 while (1) {
1259 got = read(fd, buf, sizeof(buf));
1260 if (got == 0)
1261 break;
1262 if (got < 0) {
1263 retval = errno;
1264 goto fail;
1265 }
1266 ptr = buf;
1267 while (got > 0) {
1268 retval = ext2fs_file_write(e2_file, ptr,
1269 got, &written);
1270 if (retval)
1271 goto fail;
1272
1273 got -= written;
1274 ptr += written;
1275 }
1276 }
1277 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001278 return retval;
1279
1280fail:
1281 (void) ext2fs_file_close(e2_file);
1282 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001283}
1284
Theodore Ts'o5a513841997-10-25 22:41:14 +00001285
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001286void do_write(int argc, char *argv[])
1287{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001288 int fd;
1289 struct stat statbuf;
1290 ext2_ino_t newfile;
1291 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001292 struct ext2_inode inode;
1293
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001294 if (common_args_process(argc, argv, 3, 3, "write",
1295 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001296 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001297
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001298 fd = open(argv[1], O_RDONLY);
1299 if (fd < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001300 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001301 return;
1302 }
1303 if (fstat(fd, &statbuf) < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001304 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001305 close(fd);
1306 return;
1307 }
1308
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001309 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1310 if (retval == 0) {
1311 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1312 close(fd);
1313 return;
1314 }
1315
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001316 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001317 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001318 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001319 close(fd);
1320 return;
1321 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001322 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001323 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1324 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001325 if (retval == EXT2_ET_DIR_NO_SPACE) {
1326 retval = ext2fs_expand_dir(current_fs, cwd);
1327 if (retval) {
1328 com_err(argv[0], retval, "while expanding directory");
1329 return;
1330 }
1331 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1332 EXT2_FT_REG_FILE);
1333 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001334 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001335 com_err(argv[2], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001336 close(fd);
1337 return;
1338 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001339 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001340 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001341 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001342 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001343 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001344 inode.i_atime = inode.i_ctime = inode.i_mtime =
1345 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001346 inode.i_links_count = 1;
1347 inode.i_size = statbuf.st_size;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001348 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001349 close(fd);
1350 return;
1351 }
1352 if (LINUX_S_ISREG(inode.i_mode)) {
1353 retval = copy_file(fd, newfile);
1354 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001355 com_err("copy_file", retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001356 }
1357 close(fd);
1358}
1359
1360void do_mknod(int argc, char *argv[])
1361{
Theodore Ts'o54434922003-12-07 01:28:50 -05001362 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001363 ext2_ino_t newfile;
1364 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001365 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001366 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001367
1368 if (check_fs_open(argv[0]))
1369 return;
1370 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001371 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001372 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1373 return;
1374 }
1375 mode = minor = major = 0;
1376 switch (argv[2][0]) {
1377 case 'p':
1378 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001379 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001380 nr = 3;
1381 break;
1382 case 'c':
1383 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001384 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001385 nr = 5;
1386 break;
1387 case 'b':
1388 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001389 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001390 nr = 5;
1391 break;
1392 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001393 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001394 nr = 0;
1395 }
1396 if (nr == 5) {
1397 major = strtoul(argv[3], argv+3, 0);
1398 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001399 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001400 nr = 0;
1401 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001402 if (argc != nr)
1403 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001404 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001405 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001406 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001407 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001408 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001409 return;
1410 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001411 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001412 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001413 if (retval == EXT2_ET_DIR_NO_SPACE) {
1414 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001415 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001416 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001417 return;
1418 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001419 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1420 filetype);
1421 }
1422 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001423 com_err(argv[1], retval, 0);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001424 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001425 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001426 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001427 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001428 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1429 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001430 memset(&inode, 0, sizeof(inode));
1431 inode.i_mode = mode;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001432 inode.i_atime = inode.i_ctime = inode.i_mtime =
1433 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001434 if ((major < 256) && (minor < 256)) {
1435 inode.i_block[0] = major*256+minor;
1436 inode.i_block[1] = 0;
1437 } else {
1438 inode.i_block[0] = 0;
1439 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1440 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001441 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001442 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001443 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001444}
1445
Theodore Ts'o3839e651997-04-26 13:21:57 +00001446void do_mkdir(int argc, char *argv[])
1447{
1448 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001449 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001450 char *name;
1451 errcode_t retval;
1452
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001453 if (common_args_process(argc, argv, 2, 2, "mkdir",
1454 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001455 return;
1456
Theodore Ts'o3839e651997-04-26 13:21:57 +00001457 cp = strrchr(argv[1], '/');
1458 if (cp) {
1459 *cp = 0;
1460 parent = string_to_inode(argv[1]);
1461 if (!parent) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001462 com_err(argv[1], ENOENT, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001463 return;
1464 }
1465 name = cp+1;
1466 } else {
1467 parent = cwd;
1468 name = argv[1];
1469 }
1470
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001471try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001472 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001473 if (retval == EXT2_ET_DIR_NO_SPACE) {
1474 retval = ext2fs_expand_dir(current_fs, parent);
1475 if (retval) {
1476 com_err("argv[0]", retval, "while expanding directory");
1477 return;
1478 }
1479 goto try_again;
1480 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001481 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001482 com_err("ext2fs_mkdir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001483 return;
1484 }
1485
1486}
1487
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001488static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001489 int blockcnt EXT2FS_ATTR((unused)),
1490 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001491{
Theodore Ts'o34436892001-12-22 13:06:02 -05001492 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001493
1494 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001495 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001496 return 0;
1497}
1498
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001499static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001500{
1501 struct ext2_inode inode_buf;
1502
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001503 if (debugfs_read_inode(inode, &inode_buf, 0))
1504 return;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001505 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001506 if (debugfs_write_inode(inode, &inode_buf, 0))
1507 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001508 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1509 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001510
Theodore Ts'o43323be2008-02-07 14:37:17 -05001511 ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001512 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001513 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001514 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1515 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001516}
1517
1518
1519void do_kill_file(int argc, char *argv[])
1520{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001521 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001522
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001523 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001524 return;
1525
Theodore Ts'o3839e651997-04-26 13:21:57 +00001526 kill_file_by_inode(inode_num);
1527}
1528
1529void do_rm(int argc, char *argv[])
1530{
1531 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001532 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001533 struct ext2_inode inode;
1534
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001535 if (common_args_process(argc, argv, 2, 2, "rm",
1536 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001537 return;
1538
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001539 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001540 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001541 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001542 return;
1543 }
1544
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001545 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001546 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001547
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001548 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001549 com_err(argv[0], 0, "file is a directory");
1550 return;
1551 }
1552
1553 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001554 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001555 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001556
1557 unlink_file_by_name(argv[1]);
1558 if (inode.i_links_count == 0)
1559 kill_file_by_inode(inode_num);
1560}
1561
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001562struct rd_struct {
1563 ext2_ino_t parent;
1564 int empty;
1565};
1566
Theodore Ts'o54434922003-12-07 01:28:50 -05001567static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1568 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001569 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001570 int offset EXT2FS_ATTR((unused)),
1571 int blocksize EXT2FS_ATTR((unused)),
1572 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001573 void *private)
1574{
1575 struct rd_struct *rds = (struct rd_struct *) private;
1576
1577 if (dirent->inode == 0)
1578 return 0;
1579 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1580 return 0;
1581 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1582 (dirent->name[1] == '.')) {
1583 rds->parent = dirent->inode;
1584 return 0;
1585 }
1586 rds->empty = 0;
1587 return 0;
1588}
1589
1590void do_rmdir(int argc, char *argv[])
1591{
1592 int retval;
1593 ext2_ino_t inode_num;
1594 struct ext2_inode inode;
1595 struct rd_struct rds;
1596
1597 if (common_args_process(argc, argv, 2, 2, "rmdir",
1598 "<filename>", CHECK_FS_RW))
1599 return;
1600
1601 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1602 if (retval) {
1603 com_err(argv[0], retval, "while trying to resolve filename");
1604 return;
1605 }
1606
1607 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1608 return;
1609
1610 if (!LINUX_S_ISDIR(inode.i_mode)) {
1611 com_err(argv[0], 0, "file is not a directory");
1612 return;
1613 }
1614
1615 rds.parent = 0;
1616 rds.empty = 1;
1617
1618 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1619 0, rmdir_proc, &rds);
1620 if (retval) {
1621 com_err(argv[0], retval, "while iterating over directory");
1622 return;
1623 }
1624 if (rds.empty == 0) {
1625 com_err(argv[0], 0, "directory not empty");
1626 return;
1627 }
1628
1629 inode.i_links_count = 0;
1630 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1631 return;
1632
1633 unlink_file_by_name(argv[1]);
1634 kill_file_by_inode(inode_num);
1635
1636 if (rds.parent) {
1637 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1638 return;
1639 if (inode.i_links_count > 1)
1640 inode.i_links_count--;
1641 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1642 return;
1643 }
1644}
1645
Theodore Ts'o54434922003-12-07 01:28:50 -05001646void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1647 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001648{
1649 FILE *out = stdout;
1650
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001651 if (current_fs)
1652 fprintf(out, "Open mode: read-%s\n",
1653 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001654 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001655 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001656}
1657
1658void do_expand_dir(int argc, char *argv[])
1659{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001660 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001661 int retval;
1662
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001663 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001664 return;
1665
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001666 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001667 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001668 com_err("ext2fs_expand_dir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001669 return;
1670}
1671
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001672void do_features(int argc, char *argv[])
1673{
1674 int i;
1675
1676 if (check_fs_open(argv[0]))
1677 return;
1678
1679 if ((argc != 1) && check_fs_read_write(argv[0]))
1680 return;
1681 for (i=1; i < argc; i++) {
1682 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001683 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001684 com_err(argv[0], 0, "Unknown feature: %s\n",
1685 argv[i]);
1686 else
1687 ext2fs_mark_super_dirty(current_fs);
1688 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001689 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001690}
1691
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001692void do_bmap(int argc, char *argv[])
1693{
1694 ext2_ino_t ino;
1695 blk_t blk, pblk;
1696 int err;
1697 errcode_t errcode;
1698
1699 if (common_args_process(argc, argv, 3, 3, argv[0],
1700 "<file> logical_blk", 0))
1701 return;
1702
1703 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001704 if (!ino)
1705 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001706 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1707
1708 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1709 if (errcode) {
1710 com_err("argv[0]", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -05001711 "while mapping logical block %u\n", blk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001712 return;
1713 }
Takashi Sato8deb80a2006-03-18 21:43:46 -05001714 printf("%u\n", pblk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001715}
1716
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001717void do_imap(int argc, char *argv[])
1718{
1719 ext2_ino_t ino;
1720 unsigned long group, block, block_nr, offset;
1721
1722 if (common_args_process(argc, argv, 2, 2, argv[0],
1723 "<file>", 0))
1724 return;
1725 ino = string_to_inode(argv[1]);
1726 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001727 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001728
1729 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1730 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1731 EXT2_INODE_SIZE(current_fs->super);
1732 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1733 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001734 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001735 group);
1736 return;
1737 }
1738 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1739 block;
1740 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1741
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001742 printf("Inode %d is part of block group %lu\n"
1743 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001744 block_nr, offset);
1745
1746}
1747
Theodore Ts'o4efae602005-09-24 21:56:38 -04001748void do_set_current_time(int argc, char *argv[])
1749{
Theodore Ts'o4efae602005-09-24 21:56:38 -04001750 time_t now;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001751
Theodore Ts'o4efae602005-09-24 21:56:38 -04001752 if (common_args_process(argc, argv, 2, 2, argv[0],
1753 "<time>", 0))
1754 return;
1755
1756 now = string_to_time(argv[1]);
1757 if (now == ((time_t) -1)) {
1758 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1759 argv[1]);
1760 return;
1761
1762 } else {
1763 printf("Setting current time to %s\n", time_to_string(now));
1764 current_fs->now = now;
1765 }
1766}
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001767
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001768static int source_file(const char *cmd_file, int sci_idx)
1769{
1770 FILE *f;
1771 char buf[256];
1772 char *cp;
1773 int exit_status = 0;
1774 int retval;
1775
1776 if (strcmp(cmd_file, "-") == 0)
1777 f = stdin;
1778 else {
1779 f = fopen(cmd_file, "r");
1780 if (!f) {
1781 perror(cmd_file);
1782 exit(1);
1783 }
1784 }
1785 setbuf(stdout, NULL);
1786 setbuf(stderr, NULL);
1787 while (!feof(f)) {
1788 if (fgets(buf, sizeof(buf), f) == NULL)
1789 break;
1790 cp = strchr(buf, '\n');
1791 if (cp)
1792 *cp = 0;
1793 cp = strchr(buf, '\r');
1794 if (cp)
1795 *cp = 0;
1796 printf("debugfs: %s\n", buf);
1797 retval = ss_execute_line(sci_idx, buf);
1798 if (retval) {
1799 ss_perror(sci_idx, retval, buf);
1800 exit_status++;
1801 }
1802 }
1803 return exit_status;
1804}
1805
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001806int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001807{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001808 int retval;
1809 int sci_idx;
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001810 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 +00001811 int c;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -05001812 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001813 char *request = 0;
1814 int exit_status = 0;
1815 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001816 blk_t superblock = 0;
1817 blk_t blocksize = 0;
1818 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001819 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001820
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001821 if (debug_prog_name == 0)
1822 debug_prog_name = "debugfs";
1823
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001824 add_error_table(&et_ext2_error_table);
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001825 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
1826 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001827
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001828 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001829 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001830 case 'R':
1831 request = optarg;
1832 break;
1833 case 'f':
1834 cmd_file = optarg;
1835 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001836 case 'd':
1837 data_filename = optarg;
1838 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001839 case 'i':
1840 open_flags |= EXT2_FLAG_IMAGE_FILE;
1841 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001842 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001843 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001844 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001845 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001846 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001847 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001848 break;
1849 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001850 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001851 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001852 break;
1853 case 'c':
1854 catastrophic = 1;
1855 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001856 case 'V':
1857 /* Print version number and exit */
1858 fprintf(stderr, "\tUsing %s\n",
1859 error_message(EXT2_ET_BASE));
1860 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001861 default:
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001862 com_err(argv[0], 0, usage, debug_prog_name);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001863 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001864 }
1865 }
1866 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001867 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001868 superblock, blocksize, catastrophic,
1869 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001870
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001871 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
Theodore Ts'o3839e651997-04-26 13:21:57 +00001872 &debug_cmds, &retval);
1873 if (retval) {
1874 ss_perror(sci_idx, retval, "creating invocation");
1875 exit(1);
1876 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001877 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001878
1879 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1880 if (retval) {
1881 ss_perror(sci_idx, retval, "adding standard requests");
1882 exit (1);
1883 }
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001884 if (extra_cmds)
1885 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
1886 if (retval) {
1887 ss_perror(sci_idx, retval, "adding extra requests");
1888 exit (1);
1889 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001890 if (request) {
1891 retval = 0;
1892 retval = ss_execute_line(sci_idx, request);
1893 if (retval) {
1894 ss_perror(sci_idx, retval, request);
1895 exit_status++;
1896 }
1897 } else if (cmd_file) {
1898 exit_status = source_file(cmd_file, sci_idx);
1899 } else {
1900 ss_listen(sci_idx);
1901 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001902
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001903 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001904 close_filesystem();
1905
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001906 remove_error_table(&et_ext2_error_table);
Theodore Ts'oe5973042000-01-18 17:58:34 +00001907 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001908}