blob: ab40de886ed613e84577836d0b3e15d4bfdfc967 [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;
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500291 int numdirs = 0, first, gdt_csum;
292
293 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
294 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400296 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000297 while ((c = getopt (argc, argv, "h")) != EOF) {
298 switch (c) {
299 case 'h':
300 header_only++;
301 break;
302 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500303 goto print_usage;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000304 }
305 }
306 if (optind != argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500307 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 }
309 if (check_fs_open(argv[0]))
310 return;
311 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000312
313 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400314 for (i=0; i < current_fs->group_desc_count; i++)
315 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
316 fprintf(out, "Directories: %d\n", numdirs);
317
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000318 if (header_only) {
319 close_pager(out);
320 return;
321 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000322
323 gdp = &current_fs->group_desc[0];
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400324 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
Takashi Sato8deb80a2006-03-18 21:43:46 -0500325 fprintf(out, " Group %2d: block bitmap at %u, "
326 "inode bitmap at %u, "
327 "inode table at %u\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000328 " %d free %s, "
329 "%d free %s, "
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500330 "%d used %s%s",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000331 i, gdp->bg_block_bitmap,
332 gdp->bg_inode_bitmap, gdp->bg_inode_table,
333 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000334 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000335 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000336 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000337 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000338 gdp->bg_used_dirs_count != 1 ? "directories"
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500339 : "directory", gdt_csum ? ", " : "\n");
340 if (gdt_csum)
341 fprintf(out, "%d unused %s\n",
342 gdp->bg_itable_unused,
343 gdp->bg_itable_unused != 1 ? "inodes":"inode");
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400344 first = 1;
345 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
346 &first, out);
347 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
348 &first, out);
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500349 if (gdt_csum) {
350 fprintf(out, "%sChecksum 0x%04x",
351 first ? " [":", ", gdp->bg_checksum);
352 first = 0;
353 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400354 if (!first)
355 fputs("]\n", out);
356 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000357 close_pager(out);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500358 return;
359print_usage:
360 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000361}
362
Theodore Ts'o54434922003-12-07 01:28:50 -0500363void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
364 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000365{
366 if (check_fs_open(argv[0]))
367 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000368 if (check_fs_read_write(argv[0]))
369 return;
370
371 if (argv[1] && !strcmp(argv[1], "-clean"))
372 current_fs->super->s_state |= EXT2_VALID_FS;
373 else
374 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000375 ext2fs_mark_super_dirty(current_fs);
376}
377
Theodore Ts'o3839e651997-04-26 13:21:57 +0000378struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700379 FILE *f;
380 e2_blkcnt_t total;
381 blk_t first_block, last_block;
382 e2_blkcnt_t first_bcnt, last_bcnt;
383 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000384};
385
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000386static void finish_range(struct list_blocks_struct *lb)
387{
388 if (lb->first_block == 0)
389 return;
390 if (lb->first)
391 lb->first = 0;
392 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000393 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000394 if (lb->first_block == lb->last_block)
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400395 fprintf(lb->f, "(%lld):%u",
396 (long long)lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000397 else
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400398 fprintf(lb->f, "(%lld-%lld):%u-%u",
399 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
400 lb->first_block, lb->last_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000401 lb->first_block = 0;
402}
403
Theodore Ts'o54434922003-12-07 01:28:50 -0500404static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
405 blk_t *blocknr, e2_blkcnt_t blockcnt,
406 blk_t ref_block EXT2FS_ATTR((unused)),
407 int ref_offset EXT2FS_ATTR((unused)),
408 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409{
410 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
411
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000412 lb->total++;
413 if (blockcnt >= 0) {
414 /*
415 * See if we can add on to the existing range (if it exists)
416 */
417 if (lb->first_block &&
418 (lb->last_block+1 == *blocknr) &&
419 (lb->last_bcnt+1 == blockcnt)) {
420 lb->last_block = *blocknr;
421 lb->last_bcnt = blockcnt;
422 return 0;
423 }
424 /*
425 * Start a new range.
426 */
427 finish_range(lb);
428 lb->first_block = lb->last_block = *blocknr;
429 lb->first_bcnt = lb->last_bcnt = blockcnt;
430 return 0;
431 }
432 /*
433 * Not a normal block. Always force a new range.
434 */
435 finish_range(lb);
436 if (lb->first)
437 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000438 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000439 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000440 if (blockcnt == -1)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500441 fprintf(lb->f, "(IND):%u", *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000442 else if (blockcnt == -2)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500443 fprintf(lb->f, "(DIND):%u", *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000444 else if (blockcnt == -3)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500445 fprintf(lb->f, "(TIND):%u", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000446 return 0;
447}
448
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500449static void dump_xattr_string(FILE *out, const char *str, int len)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500450{
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600451 int printable = 0;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500452 int i;
453
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600454 /* check: is string "printable enough?" */
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500455 for (i = 0; i < len; i++)
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600456 if (isprint(str[i]))
457 printable++;
458
459 if (printable <= len*7/8)
460 printable = 0;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500461
462 for (i = 0; i < len; i++)
463 if (printable)
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600464 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
465 (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500466 else
Andreas Dilgerd047e072006-06-21 00:01:42 -0400467 fprintf(out, "%02x ", (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500468}
469
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400470static void internal_dump_inode_extra(FILE *out,
471 const char *prefix EXT2FS_ATTR((unused)),
472 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500473 struct ext2_inode_large *inode)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500474{
475 struct ext2_ext_attr_entry *entry;
476 __u32 *magic;
477 char *start, *end;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500478 unsigned int storage_size;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500479
Takashi Sato8deb80a2006-03-18 21:43:46 -0500480 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500481 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
482 EXT2_GOOD_OLD_INODE_SIZE) {
483 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
484 inode->i_extra_isize);
485 return;
486 }
487 storage_size = EXT2_INODE_SIZE(current_fs->super) -
488 EXT2_GOOD_OLD_INODE_SIZE -
489 inode->i_extra_isize;
490 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
491 inode->i_extra_isize);
492 if (*magic == EXT2_EXT_ATTR_MAGIC) {
493 fprintf(out, "Extended attributes stored in inode body: \n");
494 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
495 start = (char *) magic + sizeof(__u32);
496 entry = (struct ext2_ext_attr_entry *) start;
497 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
498 struct ext2_ext_attr_entry *next =
499 EXT2_EXT_ATTR_NEXT(entry);
500 if (entry->e_value_size > storage_size ||
501 (char *) next >= end) {
502 fprintf(out, "invalid EA entry in inode\n");
503 return;
504 }
505 fprintf(out, " ");
506 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
507 entry->e_name_len);
508 fprintf(out, " = \"");
509 dump_xattr_string(out, start + entry->e_value_offs,
510 entry->e_value_size);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500511 fprintf(out, "\" (%u)\n", entry->e_value_size);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500512 entry = next;
513 }
514 }
515}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000517static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518{
519 struct list_blocks_struct lb;
520
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000521 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000523 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000525 lb.first = 1;
Theodore Ts'o43323be2008-02-07 14:37:17 -0500526 ext2fs_block_iterate2(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000527 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000528 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000529 if (lb.total)
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400530 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000531 fprintf(f,"\n");
532}
533
534
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000535void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000536 ext2_ino_t inode_num, struct ext2_inode *inode,
537 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000538{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000539 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000540 char frag, fsize;
541 int os = current_fs->super->s_creator_os;
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400542 struct ext2_inode_large *large_inode;
543 int is_large_inode = 0;
544
545 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
546 is_large_inode = 1;
547 large_inode = (struct ext2_inode_large *) inode;
548
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000549 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
550 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
551 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
552 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
553 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
554 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
555 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000557 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400558 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
559 prefix, inode->i_mode & 0777, inode->i_flags);
560 if (is_large_inode && large_inode->i_extra_isize >= 24) {
561 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
562 prefix, inode->i_generation, large_inode->i_version_hi,
563 inode->osd1.linux1.l_i_version);
564 } else {
565 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
566 inode->i_generation, inode->osd1.linux1.l_i_version);
567 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000568 fprintf(out, "%sUser: %5d Group: %5d Size: ",
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400569 prefix, inode_uid(*inode), inode_gid(*inode));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700570 if (LINUX_S_ISREG(inode->i_mode)) {
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400571 unsigned long long i_size = (inode->i_size |
572 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700573
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400574 fprintf(out, "%llu\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700575 } else
576 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500577 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000578 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000579 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
580 prefix,
581 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
582 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000583 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000584 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
585 prefix,
586 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500587 if (os == EXT2_OS_LINUX)
588 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
589 prefix, inode->i_links_count,
590 (((unsigned long long)
591 inode->osd2.linux2.l_i_blocks_hi << 32)) +
592 inode->i_blocks);
593 else
594 fprintf(out, "%sLinks: %d Blockcount: %u\n",
595 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000596 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000597 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000598 frag = inode->osd2.hurd2.h_i_frag;
599 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000600 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000601 default:
602 frag = fsize = 0;
603 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000604 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
605 prefix, inode->i_faddr, frag, fsize);
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400606 if (is_large_inode && large_inode->i_extra_isize >= 24) {
607 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
608 inode->i_ctime, large_inode->i_ctime_extra,
609 time_to_string(inode->i_ctime));
610 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
611 inode->i_atime, large_inode->i_atime_extra,
612 time_to_string(inode->i_atime));
613 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
614 inode->i_mtime, large_inode->i_mtime_extra,
615 time_to_string(inode->i_mtime));
616 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
617 large_inode->i_crtime, large_inode->i_crtime_extra,
618 time_to_string(large_inode->i_crtime));
619 } else {
620 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
621 time_to_string(inode->i_ctime));
622 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
623 time_to_string(inode->i_atime));
624 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
625 time_to_string(inode->i_mtime));
626 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000627 if (inode->i_dtime)
628 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
629 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500630 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
631 internal_dump_inode_extra(out, prefix, inode_num,
632 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500633 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000634 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
635 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500636 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
637 int major, minor;
638 const char *devnote;
639
640 if (inode->i_block[0]) {
641 major = (inode->i_block[0] >> 8) & 255;
642 minor = inode->i_block[0] & 255;
643 devnote = "";
644 } else {
645 major = (inode->i_block[1] & 0xfff00) >> 8;
646 minor = ((inode->i_block[1] & 0xff) |
647 ((inode->i_block[1] >> 12) & 0xfff00));
648 devnote = "(New-style) ";
649 }
650 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
651 devnote, major, minor, major, minor);
652 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000653 else if (do_dump_blocks)
654 dump_blocks(out, prefix, inode_num);
655}
656
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500657static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000658{
659 FILE *out;
660
661 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500662 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 close_pager(out);
664}
665
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666void do_stat(int argc, char *argv[])
667{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000668 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500669 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670
Theodore Ts'o64777392005-05-05 17:21:46 -0400671 if (check_fs_open(argv[0]))
Theodore Ts'o8363e352005-05-06 09:04:37 -0400672 return;
Theodore Ts'o64777392005-05-05 17:21:46 -0400673
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500674 inode_buf = (struct ext2_inode *)
675 malloc(EXT2_INODE_SIZE(current_fs->super));
676 if (!inode_buf) {
677 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000678 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500679 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500681 if (common_inode_args_process(argc, argv, &inode, 0)) {
682 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500683 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500684 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500686 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
687 EXT2_INODE_SIZE(current_fs->super))) {
688 free(inode_buf);
689 return;
690 }
691
692 dump_inode(inode, inode_buf);
693 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694 return;
695}
696
697void do_chroot(int argc, char *argv[])
698{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000699 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000700 int retval;
701
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500702 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000703 return;
704
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000705 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500707 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000708 return;
709 }
710 root = inode;
711}
712
713void do_clri(int argc, char *argv[])
714{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000715 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000716 struct ext2_inode inode_buf;
717
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500718 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719 return;
720
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500721 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500724 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726}
727
728void do_freei(int argc, char *argv[])
729{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000730 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000731
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500732 if (common_inode_args_process(argc, argv, &inode,
733 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000734 return;
735
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000736 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000738 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
739 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000740}
741
742void do_seti(int argc, char *argv[])
743{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000744 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500746 if (common_inode_args_process(argc, argv, &inode,
747 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000748 return;
749
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000750 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000751 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000752 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
753 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754}
755
756void do_testi(int argc, char *argv[])
757{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000758 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000759
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500760 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761 return;
762
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000763 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000764 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000765 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000766 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000767}
768
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769void do_freeb(int argc, char *argv[])
770{
771 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400772 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000773
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500774 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000775 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000776 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000777 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500778 while (count-- > 0) {
779 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500780 com_err(argv[0], 0, "Warning: block %u already clear",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500781 block);
782 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
783 block++;
784 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000785 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000786}
787
788void do_setb(int argc, char *argv[])
789{
790 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400791 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000792
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500793 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000794 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000795 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500797 while (count-- > 0) {
798 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500799 com_err(argv[0], 0, "Warning: block %u already set",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500800 block);
801 ext2fs_mark_block_bitmap(current_fs->block_map,block);
802 block++;
803 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000804 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000805}
806
807void do_testb(int argc, char *argv[])
808{
809 blk_t block;
Eric Sandeen3e419132007-04-10 15:40:04 -0400810 blk_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000811
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500812 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000813 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500814 while (count-- > 0) {
815 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500816 printf("Block %u marked in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500817 else
Takashi Sato8deb80a2006-03-18 21:43:46 -0500818 printf("Block %u not in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500819 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821}
822
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000823static void modify_u8(char *com, const char *prompt,
824 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000825{
826 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000827 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000828 char *tmp;
829
830 sprintf(buf, format, *val);
831 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400832 if (!fgets(buf, sizeof(buf), stdin))
833 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000834 if (buf[strlen (buf) - 1] == '\n')
835 buf[strlen (buf) - 1] = '\0';
836 if (!buf[0])
837 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000838 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000839 if (*tmp)
840 com_err(com, 0, "Bad value - %s", buf);
841 else
842 *val = v;
843}
844
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000845static void modify_u16(char *com, const char *prompt,
846 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000847{
848 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000849 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000850 char *tmp;
851
852 sprintf(buf, format, *val);
853 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400854 if (!fgets(buf, sizeof(buf), stdin))
855 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000856 if (buf[strlen (buf) - 1] == '\n')
857 buf[strlen (buf) - 1] = '\0';
858 if (!buf[0])
859 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000860 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000861 if (*tmp)
862 com_err(com, 0, "Bad value - %s", buf);
863 else
864 *val = v;
865}
866
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000867static void modify_u32(char *com, const char *prompt,
868 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000869{
870 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000871 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000872 char *tmp;
873
874 sprintf(buf, format, *val);
875 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +0400876 if (!fgets(buf, sizeof(buf), stdin))
877 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000878 if (buf[strlen (buf) - 1] == '\n')
879 buf[strlen (buf) - 1] = '\0';
880 if (!buf[0])
881 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000882 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000883 if (*tmp)
884 com_err(com, 0, "Bad value - %s", buf);
885 else
886 *val = v;
887}
888
889
890void do_modify_inode(int argc, char *argv[])
891{
892 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000893 ext2_ino_t inode_num;
894 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000895 unsigned char *frag, *fsize;
896 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500897 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000898 const char *hex_format = "0x%x";
899 const char *octal_format = "0%o";
900 const char *decimal_format = "%d";
Takashi Sato8deb80a2006-03-18 21:43:46 -0500901 const char *unsignedlong_format = "%lu";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000902
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500903 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000905
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500906 os = current_fs->super->s_creator_os;
907
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500908 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000910
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000911 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
912 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
913 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500914 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000915 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
916 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
917 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
918 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
919 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500920 if (os == EXT2_OS_LINUX)
921 modify_u16(argv[0], "Block count high", unsignedlong_format,
922 &inode.osd2.linux2.l_i_blocks_hi);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500923 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000924 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000925 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000926#if 0
927 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
928#endif
929 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000930 if (LINUX_S_ISDIR(inode.i_mode))
931 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
932 else
933 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000934
Theodore Ts'o5d171192006-11-11 06:32:03 -0500935 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000936 modify_u32(argv[0], "Translator Block",
937 decimal_format, &inode.osd1.hurd1.h_i_translator);
938
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000939 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000940 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000941 case EXT2_OS_HURD:
942 frag = &inode.osd2.hurd2.h_i_frag;
943 fsize = &inode.osd2.hurd2.h_i_fsize;
944 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000945 default:
946 frag = fsize = 0;
947 }
948 if (frag)
949 modify_u8(argv[0], "Fragment number", decimal_format, frag);
950 if (fsize)
951 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
952
Theodore Ts'o3839e651997-04-26 13:21:57 +0000953 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
954 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000955 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000957 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000958 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000959 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000960 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000961 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000962 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500963 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000964 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000965}
966
Theodore Ts'o3839e651997-04-26 13:21:57 +0000967void do_change_working_dir(int argc, char *argv[])
968{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000969 ext2_ino_t inode;
970 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000971
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500972 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973 return;
974
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000975 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500977 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000978 return;
979 }
980 cwd = inode;
981 return;
982}
983
Theodore Ts'o3839e651997-04-26 13:21:57 +0000984void do_print_working_directory(int argc, char *argv[])
985{
986 int retval;
987 char *pathname = NULL;
988
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500989 if (common_args_process(argc, argv, 1, 1,
990 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000991 return;
992
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000993 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000994 if (retval) {
995 com_err(argv[0], retval,
996 "while trying to get pathname of cwd");
997 }
Brian Behlendorf971fe052007-03-29 00:32:23 -0400998 printf("[pwd] INODE: %6u PATH: %s\n",
999 cwd, pathname ? pathname : "NULL");
1000 if (pathname) {
1001 free(pathname);
1002 pathname = NULL;
1003 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001004 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001005 if (retval) {
1006 com_err(argv[0], retval,
1007 "while trying to get pathname of root");
1008 }
Brian Behlendorf971fe052007-03-29 00:32:23 -04001009 printf("[root] INODE: %6u PATH: %s\n",
1010 root, pathname ? pathname : "NULL");
1011 if (pathname) {
1012 free(pathname);
1013 pathname = NULL;
1014 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001015 return;
1016}
1017
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001018/*
1019 * Given a mode, return the ext2 file type
1020 */
1021static int ext2_file_type(unsigned int mode)
1022{
1023 if (LINUX_S_ISREG(mode))
1024 return EXT2_FT_REG_FILE;
1025
1026 if (LINUX_S_ISDIR(mode))
1027 return EXT2_FT_DIR;
1028
1029 if (LINUX_S_ISCHR(mode))
1030 return EXT2_FT_CHRDEV;
1031
1032 if (LINUX_S_ISBLK(mode))
1033 return EXT2_FT_BLKDEV;
1034
1035 if (LINUX_S_ISLNK(mode))
1036 return EXT2_FT_SYMLINK;
1037
1038 if (LINUX_S_ISFIFO(mode))
1039 return EXT2_FT_FIFO;
1040
1041 if (LINUX_S_ISSOCK(mode))
1042 return EXT2_FT_SOCK;
1043
1044 return 0;
1045}
1046
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001047static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001048{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001049 ext2_ino_t ino;
1050 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001051 int retval;
1052 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001053 char *dest, *cp, *base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001054
1055 /*
1056 * Get the source inode
1057 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001058 ino = string_to_inode(sourcename);
1059 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001060 return;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001061 base_name = strrchr(sourcename, '/');
1062 if (base_name)
1063 base_name++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001064 else
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001065 base_name = sourcename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001066 /*
1067 * Figure out the destination. First see if it exists and is
1068 * a directory.
1069 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001070 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001071 dest = base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001072 else {
1073 /*
1074 * OK, it doesn't exist. See if it is
1075 * '<dir>/basename' or 'basename'
1076 */
1077 cp = strrchr(destname, '/');
1078 if (cp) {
1079 *cp = 0;
1080 dir = string_to_inode(destname);
1081 if (!dir)
1082 return;
1083 dest = cp+1;
1084 } else {
1085 dir = cwd;
1086 dest = destname;
1087 }
1088 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001089
1090 if (debugfs_read_inode(ino, &inode, sourcename))
1091 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001092
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001093 retval = ext2fs_link(current_fs, dir, dest, ino,
1094 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001095 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001096 com_err("make_link", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001097 return;
1098}
1099
1100
1101void do_link(int argc, char *argv[])
1102{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001103 if (common_args_process(argc, argv, 3, 3, "link",
1104 "<source file> <dest_name>", CHECK_FS_RW))
1105 return;
1106
1107 make_link(argv[1], argv[2]);
1108}
1109
1110static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001111 int blockcnt EXT2FS_ATTR((unused)),
1112 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001113{
1114 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001115
1116 block = *blocknr;
1117 ext2fs_block_alloc_stats(fs, block, +1);
1118 return 0;
1119}
1120
1121void do_undel(int argc, char *argv[])
1122{
1123 ext2_ino_t ino;
1124 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001125
Theodore Ts'ob026d532008-01-01 11:37:20 -05001126 if (common_args_process(argc, argv, 2, 3, "undelete",
1127 "<inode_num> [dest_name]",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001128 CHECK_FS_RW | CHECK_FS_BITMAPS))
1129 return;
1130
1131 ino = string_to_inode(argv[1]);
1132 if (!ino)
1133 return;
1134
1135 if (debugfs_read_inode(ino, &inode, argv[1]))
1136 return;
1137
1138 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1139 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001140 return;
1141 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001142
1143 /*
1144 * XXX this function doesn't handle changing the links count on the
1145 * parent directory when undeleting a directory.
1146 */
1147 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1148 inode.i_dtime = 0;
1149
1150 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001151 return;
1152
Theodore Ts'o43323be2008-02-07 14:37:17 -05001153 ext2fs_block_iterate(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001154 mark_blocks_proc, NULL);
1155
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001156 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001157
Theodore Ts'ob026d532008-01-01 11:37:20 -05001158 if (argc > 2)
1159 make_link(argv[1], argv[2]);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001160}
1161
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001162static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001163{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001164 int retval;
1165 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001166 char *base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001167
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001168 base_name = strrchr(filename, '/');
1169 if (base_name) {
1170 *base_name++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001171 dir = string_to_inode(filename);
1172 if (!dir)
1173 return;
1174 } else {
1175 dir = cwd;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001176 base_name = filename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001177 }
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001178 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001179 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001180 com_err("unlink_file_by_name", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001181 return;
1182}
1183
1184void do_unlink(int argc, char *argv[])
1185{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001186 if (common_args_process(argc, argv, 2, 2, "link",
1187 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001188 return;
1189
1190 unlink_file_by_name(argv[1]);
1191}
1192
1193void do_find_free_block(int argc, char *argv[])
1194{
Theodore Ts'o5aae7c22008-02-27 13:38:56 -05001195 blk_t free_blk, goal, first_free = 0;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001196 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001197 errcode_t retval;
1198 char *tmp;
1199
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001200 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1201 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001202 return;
1203 }
1204 if (check_fs_open(argv[0]))
1205 return;
1206
1207 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001208 count = strtol(argv[1],&tmp,0);
1209 if (*tmp) {
1210 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1211 return;
1212 }
1213 } else
1214 count = 1;
1215
1216 if (argc > 2) {
1217 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001218 if (*tmp) {
1219 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1220 return;
1221 }
1222 }
1223 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001224 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001225
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001226 printf("Free blocks found: ");
1227 free_blk = goal - 1;
1228 while (count-- > 0) {
1229 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1230 &free_blk);
Theodore Ts'o5aae7c22008-02-27 13:38:56 -05001231 if (first_free) {
1232 if (first_free == free_blk)
1233 break;
1234 } else
1235 first_free = free_blk;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001236 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001237 com_err("ext2fs_new_block", retval, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001238 return;
1239 } else
Takashi Sato8deb80a2006-03-18 21:43:46 -05001240 printf("%u ", free_blk);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001241 }
1242 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001243}
1244
1245void do_find_free_inode(int argc, char *argv[])
1246{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001247 ext2_ino_t free_inode, dir;
1248 int mode;
1249 int retval;
1250 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001251
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001252 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1253 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001254 return;
1255 }
1256 if (check_fs_open(argv[0]))
1257 return;
1258
1259 if (argc > 1) {
1260 dir = strtol(argv[1], &tmp, 0);
1261 if (*tmp) {
1262 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1263 return;
1264 }
1265 }
1266 else
1267 dir = root;
1268 if (argc > 2) {
1269 mode = strtol(argv[2], &tmp, 0);
1270 if (*tmp) {
1271 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1272 return;
1273 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001274 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001275 mode = 010755;
1276
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001277 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001278 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001279 com_err("ext2fs_new_inode", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001280 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001281 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001282}
1283
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001284static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001285{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001286 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001287 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001288 int got;
1289 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001290 char buf[8192];
1291 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001292
Theodore Ts'o5a513841997-10-25 22:41:14 +00001293 retval = ext2fs_file_open(current_fs, newfile,
1294 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001295 if (retval)
1296 return retval;
1297
Theodore Ts'o5a513841997-10-25 22:41:14 +00001298 while (1) {
1299 got = read(fd, buf, sizeof(buf));
1300 if (got == 0)
1301 break;
1302 if (got < 0) {
1303 retval = errno;
1304 goto fail;
1305 }
1306 ptr = buf;
1307 while (got > 0) {
1308 retval = ext2fs_file_write(e2_file, ptr,
1309 got, &written);
1310 if (retval)
1311 goto fail;
1312
1313 got -= written;
1314 ptr += written;
1315 }
1316 }
1317 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001318 return retval;
1319
1320fail:
1321 (void) ext2fs_file_close(e2_file);
1322 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001323}
1324
Theodore Ts'o5a513841997-10-25 22:41:14 +00001325
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001326void do_write(int argc, char *argv[])
1327{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001328 int fd;
1329 struct stat statbuf;
1330 ext2_ino_t newfile;
1331 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001332 struct ext2_inode inode;
1333
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001334 if (common_args_process(argc, argv, 3, 3, "write",
1335 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001336 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001337
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001338 fd = open(argv[1], O_RDONLY);
1339 if (fd < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001340 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001341 return;
1342 }
1343 if (fstat(fd, &statbuf) < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001344 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001345 close(fd);
1346 return;
1347 }
1348
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001349 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1350 if (retval == 0) {
1351 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1352 close(fd);
1353 return;
1354 }
1355
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001356 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001357 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001358 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001359 close(fd);
1360 return;
1361 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001362 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001363 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1364 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001365 if (retval == EXT2_ET_DIR_NO_SPACE) {
1366 retval = ext2fs_expand_dir(current_fs, cwd);
1367 if (retval) {
1368 com_err(argv[0], retval, "while expanding directory");
Manish Katiyar42621622008-08-23 21:41:25 +05301369 close(fd);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001370 return;
1371 }
1372 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1373 EXT2_FT_REG_FILE);
1374 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001375 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001376 com_err(argv[2], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001377 close(fd);
1378 return;
1379 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001380 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001381 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001382 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001383 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001384 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001385 inode.i_atime = inode.i_ctime = inode.i_mtime =
1386 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001387 inode.i_links_count = 1;
1388 inode.i_size = statbuf.st_size;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001389 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001390 close(fd);
1391 return;
1392 }
1393 if (LINUX_S_ISREG(inode.i_mode)) {
1394 retval = copy_file(fd, newfile);
1395 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001396 com_err("copy_file", retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001397 }
1398 close(fd);
1399}
1400
1401void do_mknod(int argc, char *argv[])
1402{
Theodore Ts'o54434922003-12-07 01:28:50 -05001403 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001404 ext2_ino_t newfile;
1405 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001406 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001407 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001408
1409 if (check_fs_open(argv[0]))
1410 return;
1411 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001412 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001413 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1414 return;
1415 }
1416 mode = minor = major = 0;
1417 switch (argv[2][0]) {
1418 case 'p':
1419 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001420 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001421 nr = 3;
1422 break;
1423 case 'c':
1424 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001425 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001426 nr = 5;
1427 break;
1428 case 'b':
1429 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001430 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001431 nr = 5;
1432 break;
1433 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001434 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001435 nr = 0;
1436 }
1437 if (nr == 5) {
1438 major = strtoul(argv[3], argv+3, 0);
1439 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001440 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001441 nr = 0;
1442 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001443 if (argc != nr)
1444 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001445 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001446 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001447 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001448 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001449 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001450 return;
1451 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001452 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001453 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001454 if (retval == EXT2_ET_DIR_NO_SPACE) {
1455 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001456 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001457 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001458 return;
1459 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001460 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1461 filetype);
1462 }
1463 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001464 com_err(argv[1], retval, 0);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001465 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001466 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001467 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001468 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001469 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1470 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001471 memset(&inode, 0, sizeof(inode));
1472 inode.i_mode = mode;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001473 inode.i_atime = inode.i_ctime = inode.i_mtime =
1474 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001475 if ((major < 256) && (minor < 256)) {
1476 inode.i_block[0] = major*256+minor;
1477 inode.i_block[1] = 0;
1478 } else {
1479 inode.i_block[0] = 0;
1480 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1481 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001482 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001483 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001484 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001485}
1486
Theodore Ts'o3839e651997-04-26 13:21:57 +00001487void do_mkdir(int argc, char *argv[])
1488{
1489 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001490 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001491 char *name;
1492 errcode_t retval;
1493
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001494 if (common_args_process(argc, argv, 2, 2, "mkdir",
1495 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001496 return;
1497
Theodore Ts'o3839e651997-04-26 13:21:57 +00001498 cp = strrchr(argv[1], '/');
1499 if (cp) {
1500 *cp = 0;
1501 parent = string_to_inode(argv[1]);
1502 if (!parent) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001503 com_err(argv[1], ENOENT, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001504 return;
1505 }
1506 name = cp+1;
1507 } else {
1508 parent = cwd;
1509 name = argv[1];
1510 }
1511
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001512try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001513 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001514 if (retval == EXT2_ET_DIR_NO_SPACE) {
1515 retval = ext2fs_expand_dir(current_fs, parent);
1516 if (retval) {
1517 com_err("argv[0]", retval, "while expanding directory");
1518 return;
1519 }
1520 goto try_again;
1521 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001522 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001523 com_err("ext2fs_mkdir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001524 return;
1525 }
1526
1527}
1528
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001529static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001530 int blockcnt EXT2FS_ATTR((unused)),
1531 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001532{
Theodore Ts'o34436892001-12-22 13:06:02 -05001533 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001534
1535 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001536 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001537 return 0;
1538}
1539
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001540static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001541{
1542 struct ext2_inode inode_buf;
1543
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001544 if (debugfs_read_inode(inode, &inode_buf, 0))
1545 return;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001546 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001547 if (debugfs_write_inode(inode, &inode_buf, 0))
1548 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001549 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1550 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001551
Theodore Ts'o43323be2008-02-07 14:37:17 -05001552 ext2fs_block_iterate(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001553 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001554 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001555 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1556 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001557}
1558
1559
1560void do_kill_file(int argc, char *argv[])
1561{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001562 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001563
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001564 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001565 return;
1566
Theodore Ts'o3839e651997-04-26 13:21:57 +00001567 kill_file_by_inode(inode_num);
1568}
1569
1570void do_rm(int argc, char *argv[])
1571{
1572 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001573 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001574 struct ext2_inode inode;
1575
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001576 if (common_args_process(argc, argv, 2, 2, "rm",
1577 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001578 return;
1579
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001580 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001581 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001582 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001583 return;
1584 }
1585
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001586 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001587 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001588
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001589 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001590 com_err(argv[0], 0, "file is a directory");
1591 return;
1592 }
1593
1594 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001595 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001596 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001597
1598 unlink_file_by_name(argv[1]);
1599 if (inode.i_links_count == 0)
1600 kill_file_by_inode(inode_num);
1601}
1602
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001603struct rd_struct {
1604 ext2_ino_t parent;
1605 int empty;
1606};
1607
Theodore Ts'o54434922003-12-07 01:28:50 -05001608static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1609 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001610 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001611 int offset EXT2FS_ATTR((unused)),
1612 int blocksize EXT2FS_ATTR((unused)),
1613 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001614 void *private)
1615{
1616 struct rd_struct *rds = (struct rd_struct *) private;
1617
1618 if (dirent->inode == 0)
1619 return 0;
1620 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1621 return 0;
1622 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1623 (dirent->name[1] == '.')) {
1624 rds->parent = dirent->inode;
1625 return 0;
1626 }
1627 rds->empty = 0;
1628 return 0;
1629}
1630
1631void do_rmdir(int argc, char *argv[])
1632{
1633 int retval;
1634 ext2_ino_t inode_num;
1635 struct ext2_inode inode;
1636 struct rd_struct rds;
1637
1638 if (common_args_process(argc, argv, 2, 2, "rmdir",
1639 "<filename>", CHECK_FS_RW))
1640 return;
1641
1642 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1643 if (retval) {
1644 com_err(argv[0], retval, "while trying to resolve filename");
1645 return;
1646 }
1647
1648 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1649 return;
1650
1651 if (!LINUX_S_ISDIR(inode.i_mode)) {
1652 com_err(argv[0], 0, "file is not a directory");
1653 return;
1654 }
1655
1656 rds.parent = 0;
1657 rds.empty = 1;
1658
1659 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1660 0, rmdir_proc, &rds);
1661 if (retval) {
1662 com_err(argv[0], retval, "while iterating over directory");
1663 return;
1664 }
1665 if (rds.empty == 0) {
1666 com_err(argv[0], 0, "directory not empty");
1667 return;
1668 }
1669
1670 inode.i_links_count = 0;
1671 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1672 return;
1673
1674 unlink_file_by_name(argv[1]);
1675 kill_file_by_inode(inode_num);
1676
1677 if (rds.parent) {
1678 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1679 return;
1680 if (inode.i_links_count > 1)
1681 inode.i_links_count--;
1682 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1683 return;
1684 }
1685}
1686
Theodore Ts'o54434922003-12-07 01:28:50 -05001687void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1688 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001689{
1690 FILE *out = stdout;
1691
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001692 if (current_fs)
1693 fprintf(out, "Open mode: read-%s\n",
1694 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001695 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001696 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001697}
1698
1699void do_expand_dir(int argc, char *argv[])
1700{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001701 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001702 int retval;
1703
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001704 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001705 return;
1706
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001707 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001708 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001709 com_err("ext2fs_expand_dir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001710 return;
1711}
1712
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001713void do_features(int argc, char *argv[])
1714{
1715 int i;
1716
1717 if (check_fs_open(argv[0]))
1718 return;
1719
1720 if ((argc != 1) && check_fs_read_write(argv[0]))
1721 return;
1722 for (i=1; i < argc; i++) {
1723 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001724 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001725 com_err(argv[0], 0, "Unknown feature: %s\n",
1726 argv[i]);
1727 else
1728 ext2fs_mark_super_dirty(current_fs);
1729 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001730 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001731}
1732
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001733void do_bmap(int argc, char *argv[])
1734{
1735 ext2_ino_t ino;
1736 blk_t blk, pblk;
1737 int err;
1738 errcode_t errcode;
1739
1740 if (common_args_process(argc, argv, 3, 3, argv[0],
1741 "<file> logical_blk", 0))
1742 return;
1743
1744 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001745 if (!ino)
1746 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001747 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1748
1749 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1750 if (errcode) {
1751 com_err("argv[0]", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -05001752 "while mapping logical block %u\n", blk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001753 return;
1754 }
Takashi Sato8deb80a2006-03-18 21:43:46 -05001755 printf("%u\n", pblk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001756}
1757
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001758void do_imap(int argc, char *argv[])
1759{
1760 ext2_ino_t ino;
1761 unsigned long group, block, block_nr, offset;
1762
1763 if (common_args_process(argc, argv, 2, 2, argv[0],
1764 "<file>", 0))
1765 return;
1766 ino = string_to_inode(argv[1]);
1767 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001768 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001769
1770 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1771 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1772 EXT2_INODE_SIZE(current_fs->super);
1773 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1774 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001775 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001776 group);
1777 return;
1778 }
1779 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1780 block;
1781 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1782
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001783 printf("Inode %d is part of block group %lu\n"
1784 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001785 block_nr, offset);
1786
1787}
1788
Theodore Ts'o4efae602005-09-24 21:56:38 -04001789void do_set_current_time(int argc, char *argv[])
1790{
Theodore Ts'o4efae602005-09-24 21:56:38 -04001791 time_t now;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001792
Theodore Ts'o4efae602005-09-24 21:56:38 -04001793 if (common_args_process(argc, argv, 2, 2, argv[0],
1794 "<time>", 0))
1795 return;
1796
1797 now = string_to_time(argv[1]);
1798 if (now == ((time_t) -1)) {
1799 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1800 argv[1]);
1801 return;
1802
1803 } else {
1804 printf("Setting current time to %s\n", time_to_string(now));
1805 current_fs->now = now;
1806 }
1807}
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001808
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001809static int source_file(const char *cmd_file, int sci_idx)
1810{
1811 FILE *f;
1812 char buf[256];
1813 char *cp;
1814 int exit_status = 0;
1815 int retval;
1816
1817 if (strcmp(cmd_file, "-") == 0)
1818 f = stdin;
1819 else {
1820 f = fopen(cmd_file, "r");
1821 if (!f) {
1822 perror(cmd_file);
1823 exit(1);
1824 }
1825 }
Theodore Ts'o2a7bfe82008-07-13 15:40:15 -04001826 fflush(stdout);
1827 fflush(stderr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001828 setbuf(stdout, NULL);
1829 setbuf(stderr, NULL);
1830 while (!feof(f)) {
1831 if (fgets(buf, sizeof(buf), f) == NULL)
1832 break;
1833 cp = strchr(buf, '\n');
1834 if (cp)
1835 *cp = 0;
1836 cp = strchr(buf, '\r');
1837 if (cp)
1838 *cp = 0;
1839 printf("debugfs: %s\n", buf);
1840 retval = ss_execute_line(sci_idx, buf);
1841 if (retval) {
1842 ss_perror(sci_idx, retval, buf);
1843 exit_status++;
1844 }
1845 }
1846 return exit_status;
1847}
1848
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001849int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001850{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001851 int retval;
1852 int sci_idx;
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001853 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 +00001854 int c;
Theodore Ts'ocf8272e2006-11-12 23:26:46 -05001855 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001856 char *request = 0;
1857 int exit_status = 0;
1858 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001859 blk_t superblock = 0;
1860 blk_t blocksize = 0;
1861 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001862 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001863
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001864 if (debug_prog_name == 0)
1865 debug_prog_name = "debugfs";
1866
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001867 add_error_table(&et_ext2_error_table);
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001868 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
1869 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001870
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001871 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001872 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001873 case 'R':
1874 request = optarg;
1875 break;
1876 case 'f':
1877 cmd_file = optarg;
1878 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001879 case 'd':
1880 data_filename = optarg;
1881 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001882 case 'i':
1883 open_flags |= EXT2_FLAG_IMAGE_FILE;
1884 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001885 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001886 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001887 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001888 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001889 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001890 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001891 break;
1892 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001893 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001894 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001895 break;
1896 case 'c':
1897 catastrophic = 1;
1898 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001899 case 'V':
1900 /* Print version number and exit */
1901 fprintf(stderr, "\tUsing %s\n",
1902 error_message(EXT2_ET_BASE));
1903 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001904 default:
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001905 com_err(argv[0], 0, usage, debug_prog_name);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001906 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001907 }
1908 }
1909 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001910 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001911 superblock, blocksize, catastrophic,
1912 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001913
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001914 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
Theodore Ts'o3839e651997-04-26 13:21:57 +00001915 &debug_cmds, &retval);
1916 if (retval) {
1917 ss_perror(sci_idx, retval, "creating invocation");
1918 exit(1);
1919 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001920 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001921
1922 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1923 if (retval) {
1924 ss_perror(sci_idx, retval, "adding standard requests");
1925 exit (1);
1926 }
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04001927 if (extra_cmds)
1928 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
1929 if (retval) {
1930 ss_perror(sci_idx, retval, "adding extra requests");
1931 exit (1);
1932 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001933 if (request) {
1934 retval = 0;
1935 retval = ss_execute_line(sci_idx, request);
1936 if (retval) {
1937 ss_perror(sci_idx, retval, request);
1938 exit_status++;
1939 }
1940 } else if (cmd_file) {
1941 exit_status = source_file(cmd_file, sci_idx);
1942 } else {
1943 ss_listen(sci_idx);
1944 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001945
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001946 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001947 close_filesystem();
1948
Theodore Ts'oa6d83022006-12-26 03:38:07 -05001949 remove_error_table(&et_ext2_error_table);
Theodore Ts'oe5973042000-01-18 17:58:34 +00001950 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001951}