blob: 992a2b724d401f1f564d965c93b3e354428ead78 [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;
41
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000042ext2_filsys current_fs = NULL;
43ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000044
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000045static void open_filesystem(char *device, int open_flags, blk_t superblock,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040046 blk_t blocksize, int catastrophic,
47 char *data_filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +000048{
49 int retval;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040050 io_channel data_io = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000051
52 if (superblock != 0 && blocksize == 0) {
53 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
54 current_fs = NULL;
55 return;
56 }
57
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040058 if (data_filename) {
59 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
60 com_err(device, 0,
61 "The -d option is only valid when reading an e2image file");
62 current_fs = NULL;
63 return;
64 }
65 retval = unix_io_manager->open(data_filename, 0, &data_io);
66 if (retval) {
67 com_err(data_filename, 0, "while opening data source");
68 current_fs = NULL;
69 return;
70 }
71 }
72
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000073 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
74 com_err(device, 0,
75 "opening read-only because of catastrophic mode");
76 open_flags &= ~EXT2_FLAG_RW;
77 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000078
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000079 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000080 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 if (retval) {
82 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000083 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 return;
85 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000086
87 if (catastrophic)
88 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
89 else {
90 retval = ext2fs_read_inode_bitmap(current_fs);
91 if (retval) {
92 com_err(device, retval, "while reading inode bitmap");
93 goto errout;
94 }
95 retval = ext2fs_read_block_bitmap(current_fs);
96 if (retval) {
97 com_err(device, retval, "while reading block bitmap");
98 goto errout;
99 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100 }
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400101
102 if (data_io) {
103 retval = ext2fs_set_data_io(current_fs, data_io);
104 if (retval) {
105 com_err(device, retval,
106 "while setting data source");
107 goto errout;
108 }
109 }
110
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 root = cwd = EXT2_ROOT_INO;
112 return;
113
114errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000115 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 if (retval)
117 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000118 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119}
120
121void do_open_filesys(int argc, char **argv)
122{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500123 int c, err;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000124 int catastrophic = 0;
125 blk_t superblock = 0;
126 blk_t blocksize = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400127 int open_flags = 0;
Theodore Ts'ob0d17e02004-11-29 17:35:58 -0500128 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400130 reset_getopt();
Theodore Ts'o98eb44b2006-03-18 19:58:13 -0500131 while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132 switch (c) {
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000133 case 'i':
134 open_flags |= EXT2_FLAG_IMAGE_FILE;
135 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000137 open_flags |= EXT2_FLAG_RW;
138 break;
139 case 'f':
140 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 break;
Theodore Ts'o98eb44b2006-03-18 19:58:13 -0500142 case 'e':
143 open_flags |= EXT2_FLAG_EXCLUSIVE;
144 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000145 case 'c':
146 catastrophic = 1;
147 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400148 case 'd':
149 data_filename = optarg;
150 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000151 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500152 blocksize = parse_ulong(optarg, argv[0],
153 "block size", &err);
154 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000155 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000156 break;
157 case 's':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500158 superblock = parse_ulong(optarg, argv[0],
159 "superblock number", &err);
160 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000161 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000162 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500164 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 }
166 }
167 if (optind != argc-1) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500168 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 }
170 if (check_fs_not_open(argv[0]))
171 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000172 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400173 superblock, blocksize, catastrophic,
174 data_filename);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500175 return;
176
177print_usage:
178 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
179 "[-c] [-w] <device>\n", argv[0]);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000180}
181
182void do_lcd(int argc, char **argv)
183{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500184 if (common_args_process(argc, argv, 2, 2, "lcd",
185 "<native dir>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000186 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000187
188 if (chdir(argv[1]) == -1) {
189 com_err(argv[0], errno,
190 "while trying to change native directory to %s",
191 argv[1]);
192 return;
193 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194}
195
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000196static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197{
198 int retval;
199
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000200 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
201 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500203 com_err("ext2fs_write_inode_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000205 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
206 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500208 com_err("ext2fs_write_block_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000210 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500212 com_err("ext2fs_close", retval, 0);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000213 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000214 return;
215}
216
217void do_close_filesys(int argc, char **argv)
218{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500219 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 return;
221 close_filesystem();
222}
223
224void do_init_filesys(int argc, char **argv)
225{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 struct ext2_super_block param;
227 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500228 int err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500230 if (common_args_process(argc, argv, 3, 3, "initialize",
231 "<device> <blocksize>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 return;
233
234 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400235 param.s_blocks_count = parse_ulong(argv[2], argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500236 "blocks count", &err);
237 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000239 retval = ext2fs_initialize(argv[1], 0, &param,
240 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 if (retval) {
242 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000243 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 return;
245 }
246 root = cwd = EXT2_ROOT_INO;
247 return;
248}
249
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000250static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000251{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000252 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000253 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000254
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000255 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000256 for (i=0; i <3; i++,mask++) {
257 for (j=0,m=1; j < 32; j++, m<<=1) {
258 if (*mask & m) {
259 fprintf(f, " %s", e2p_feature2string(i, m));
260 printed++;
261 }
262 }
263 }
264 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000265 fputs("(none)", f);
266 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000267}
268
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269void do_show_super_stats(int argc, char *argv[])
270{
Theodore Ts'o54434922003-12-07 01:28:50 -0500271 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000273 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000274 int c, header_only = 0;
Theodore Ts'o34be9602002-07-15 16:56:41 -0400275 int numdirs = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400277 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000278 while ((c = getopt (argc, argv, "h")) != EOF) {
279 switch (c) {
280 case 'h':
281 header_only++;
282 break;
283 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500284 goto print_usage;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000285 }
286 }
287 if (optind != argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500288 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289 }
290 if (check_fs_open(argv[0]))
291 return;
292 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000293
294 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400295 for (i=0; i < current_fs->group_desc_count; i++)
296 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
297 fprintf(out, "Directories: %d\n", numdirs);
298
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000299 if (header_only) {
300 close_pager(out);
301 return;
302 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000303
304 gdp = &current_fs->group_desc[0];
305 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000306 fprintf(out, " Group %2d: block bitmap at %d, "
307 "inode bitmap at %d, "
308 "inode table at %d\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000309 " %d free %s, "
310 "%d free %s, "
311 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000312 i, gdp->bg_block_bitmap,
313 gdp->bg_inode_bitmap, gdp->bg_inode_table,
314 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000315 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000316 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000317 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000318 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000319 gdp->bg_used_dirs_count != 1 ? "directories"
320 : "directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321 close_pager(out);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500322 return;
323print_usage:
324 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000325}
326
Theodore Ts'o54434922003-12-07 01:28:50 -0500327void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
328 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000329{
330 if (check_fs_open(argv[0]))
331 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000332 if (check_fs_read_write(argv[0]))
333 return;
334
335 if (argv[1] && !strcmp(argv[1], "-clean"))
336 current_fs->super->s_state |= EXT2_VALID_FS;
337 else
338 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000339 ext2fs_mark_super_dirty(current_fs);
340}
341
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700343 FILE *f;
344 e2_blkcnt_t total;
345 blk_t first_block, last_block;
346 e2_blkcnt_t first_bcnt, last_bcnt;
347 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348};
349
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000350static void finish_range(struct list_blocks_struct *lb)
351{
352 if (lb->first_block == 0)
353 return;
354 if (lb->first)
355 lb->first = 0;
356 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000357 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000358 if (lb->first_block == lb->last_block)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100359 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000360 else
Theodore Ts'oe8981882001-11-30 11:51:30 +0100361 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000362 lb->last_bcnt, lb->first_block, lb->last_block);
363 lb->first_block = 0;
364}
365
Theodore Ts'o54434922003-12-07 01:28:50 -0500366static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
367 blk_t *blocknr, e2_blkcnt_t blockcnt,
368 blk_t ref_block EXT2FS_ATTR((unused)),
369 int ref_offset EXT2FS_ATTR((unused)),
370 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000371{
372 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
373
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000374 lb->total++;
375 if (blockcnt >= 0) {
376 /*
377 * See if we can add on to the existing range (if it exists)
378 */
379 if (lb->first_block &&
380 (lb->last_block+1 == *blocknr) &&
381 (lb->last_bcnt+1 == blockcnt)) {
382 lb->last_block = *blocknr;
383 lb->last_bcnt = blockcnt;
384 return 0;
385 }
386 /*
387 * Start a new range.
388 */
389 finish_range(lb);
390 lb->first_block = lb->last_block = *blocknr;
391 lb->first_bcnt = lb->last_bcnt = blockcnt;
392 return 0;
393 }
394 /*
395 * Not a normal block. Always force a new range.
396 */
397 finish_range(lb);
398 if (lb->first)
399 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000400 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000401 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000402 if (blockcnt == -1)
403 fprintf(lb->f, "(IND):%d", *blocknr);
404 else if (blockcnt == -2)
405 fprintf(lb->f, "(DIND):%d", *blocknr);
406 else if (blockcnt == -3)
407 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000408 return 0;
409}
410
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500411static void dump_xattr_string(FILE *out, const char *str, int len)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500412{
413 int printable = 1;
414 int i;
415
416 /* check is string printable? */
417 for (i = 0; i < len; i++)
418 if (!isprint(str[i])) {
419 printable = 0;
420 break;
421 }
422
423 for (i = 0; i < len; i++)
424 if (printable)
425 fprintf(out, "%c", str[i]);
426 else
427 fprintf(out, "%02x ", str[i]);
428}
429
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500430static void internal_dump_inode_extra(FILE *out, const char *prefix,
431 ext2_ino_t inode_num,
432 struct ext2_inode_large *inode)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500433{
434 struct ext2_ext_attr_entry *entry;
435 __u32 *magic;
436 char *start, *end;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500437 unsigned int storage_size;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500438
Theodore Ts'oc9ae3f92005-03-21 01:08:10 -0500439 fprintf(out, "Size of extra inode fields: %d\n", inode->i_extra_isize);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500440 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
441 EXT2_GOOD_OLD_INODE_SIZE) {
442 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
443 inode->i_extra_isize);
444 return;
445 }
446 storage_size = EXT2_INODE_SIZE(current_fs->super) -
447 EXT2_GOOD_OLD_INODE_SIZE -
448 inode->i_extra_isize;
449 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
450 inode->i_extra_isize);
451 if (*magic == EXT2_EXT_ATTR_MAGIC) {
452 fprintf(out, "Extended attributes stored in inode body: \n");
453 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
454 start = (char *) magic + sizeof(__u32);
455 entry = (struct ext2_ext_attr_entry *) start;
456 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
457 struct ext2_ext_attr_entry *next =
458 EXT2_EXT_ATTR_NEXT(entry);
459 if (entry->e_value_size > storage_size ||
460 (char *) next >= end) {
461 fprintf(out, "invalid EA entry in inode\n");
462 return;
463 }
464 fprintf(out, " ");
465 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
466 entry->e_name_len);
467 fprintf(out, " = \"");
468 dump_xattr_string(out, start + entry->e_value_offs,
469 entry->e_value_size);
470 fprintf(out, "\" (%d)\n", entry->e_value_size);
471 entry = next;
472 }
473 }
474}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000475
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000476static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477{
478 struct list_blocks_struct lb;
479
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000480 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000482 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000484 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700485 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000486 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000487 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 if (lb.total)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100489 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490 fprintf(f,"\n");
491}
492
493
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000494void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000495 ext2_ino_t inode_num, struct ext2_inode *inode,
496 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000498 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000499 char frag, fsize;
500 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000501
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000502 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
503 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
504 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
505 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
506 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
507 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
508 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000510 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
511 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
512 prefix,
513 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
514 fprintf(out, "%sUser: %5d Group: %5d Size: ",
515 prefix, inode->i_uid, inode->i_gid);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700516 if (LINUX_S_ISREG(inode->i_mode)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000517 __u64 i_size = (inode->i_size |
518 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700519
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000520 fprintf(out, "%lld\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700521 } else
522 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000523 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000524 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000525 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
526 prefix,
527 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
528 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000529 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000530 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
531 prefix,
532 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
533 fprintf(out, "%sLinks: %d Blockcount: %d\n",
534 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000535 switch (os) {
536 case EXT2_OS_LINUX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000537 frag = inode->osd2.linux2.l_i_frag;
538 fsize = inode->osd2.linux2.l_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000539 break;
540 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000541 frag = inode->osd2.hurd2.h_i_frag;
542 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000543 break;
544 case EXT2_OS_MASIX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000545 frag = inode->osd2.masix2.m_i_frag;
546 fsize = inode->osd2.masix2.m_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000547 break;
548 default:
549 frag = fsize = 0;
550 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000551 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
552 prefix, inode->i_faddr, frag, fsize);
553 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
554 time_to_string(inode->i_ctime));
555 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
556 time_to_string(inode->i_atime));
557 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
558 time_to_string(inode->i_mtime));
559 if (inode->i_dtime)
560 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
561 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500562 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
563 internal_dump_inode_extra(out, prefix, inode_num,
564 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500565 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000566 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
567 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500568 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
569 int major, minor;
570 const char *devnote;
571
572 if (inode->i_block[0]) {
573 major = (inode->i_block[0] >> 8) & 255;
574 minor = inode->i_block[0] & 255;
575 devnote = "";
576 } else {
577 major = (inode->i_block[1] & 0xfff00) >> 8;
578 minor = ((inode->i_block[1] & 0xff) |
579 ((inode->i_block[1] >> 12) & 0xfff00));
580 devnote = "(New-style) ";
581 }
582 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
583 devnote, major, minor, major, minor);
584 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000585 else if (do_dump_blocks)
586 dump_blocks(out, prefix, inode_num);
587}
588
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500589static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000590{
591 FILE *out;
592
593 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500594 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595 close_pager(out);
596}
597
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598void do_stat(int argc, char *argv[])
599{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000600 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500601 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000602
Theodore Ts'o64777392005-05-05 17:21:46 -0400603 if (check_fs_open(argv[0]))
Theodore Ts'o8363e352005-05-06 09:04:37 -0400604 return;
Theodore Ts'o64777392005-05-05 17:21:46 -0400605
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500606 inode_buf = (struct ext2_inode *)
607 malloc(EXT2_INODE_SIZE(current_fs->super));
608 if (!inode_buf) {
609 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000610 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500611 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000612
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500613 if (common_inode_args_process(argc, argv, &inode, 0)) {
614 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500615 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500616 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500618 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
619 EXT2_INODE_SIZE(current_fs->super))) {
620 free(inode_buf);
621 return;
622 }
623
624 dump_inode(inode, inode_buf);
625 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626 return;
627}
628
629void do_chroot(int argc, char *argv[])
630{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000631 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 int retval;
633
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500634 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 return;
636
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000637 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500639 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000640 return;
641 }
642 root = inode;
643}
644
645void do_clri(int argc, char *argv[])
646{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000647 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648 struct ext2_inode inode_buf;
649
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500650 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 return;
652
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500653 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500656 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658}
659
660void do_freei(int argc, char *argv[])
661{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000662 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500664 if (common_inode_args_process(argc, argv, &inode,
665 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 return;
667
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000668 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000669 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000670 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
671 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000672}
673
674void do_seti(int argc, char *argv[])
675{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000676 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500678 if (common_inode_args_process(argc, argv, &inode,
679 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 return;
681
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000682 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000684 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
685 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000686}
687
688void do_testi(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, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000693 return;
694
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000695 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000696 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000697 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000698 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000699}
700
Theodore Ts'o3839e651997-04-26 13:21:57 +0000701void do_freeb(int argc, char *argv[])
702{
703 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500704 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000705
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500706 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000708 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500710 while (count-- > 0) {
711 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
712 com_err(argv[0], 0, "Warning: block %d already clear",
713 block);
714 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
715 block++;
716 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000717 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000718}
719
720void do_setb(int argc, char *argv[])
721{
722 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500723 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000724
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500725 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000727 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500729 while (count-- > 0) {
730 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
731 com_err(argv[0], 0, "Warning: block %d already set",
732 block);
733 ext2fs_mark_block_bitmap(current_fs->block_map,block);
734 block++;
735 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000736 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737}
738
739void do_testb(int argc, char *argv[])
740{
741 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500742 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500744 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500746 while (count-- > 0) {
747 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
748 printf("Block %d marked in use\n", block);
749 else
750 printf("Block %d not in use\n", block);
751 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000752 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000753}
754
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000755static void modify_u8(char *com, const char *prompt,
756 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000757{
758 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000759 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000760 char *tmp;
761
762 sprintf(buf, format, *val);
763 printf("%30s [%s] ", prompt, buf);
764 fgets(buf, sizeof(buf), stdin);
765 if (buf[strlen (buf) - 1] == '\n')
766 buf[strlen (buf) - 1] = '\0';
767 if (!buf[0])
768 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000769 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000770 if (*tmp)
771 com_err(com, 0, "Bad value - %s", buf);
772 else
773 *val = v;
774}
775
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000776static void modify_u16(char *com, const char *prompt,
777 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778{
779 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000780 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781 char *tmp;
782
783 sprintf(buf, format, *val);
784 printf("%30s [%s] ", prompt, buf);
785 fgets(buf, sizeof(buf), stdin);
786 if (buf[strlen (buf) - 1] == '\n')
787 buf[strlen (buf) - 1] = '\0';
788 if (!buf[0])
789 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000790 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000791 if (*tmp)
792 com_err(com, 0, "Bad value - %s", buf);
793 else
794 *val = v;
795}
796
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000797static void modify_u32(char *com, const char *prompt,
798 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799{
800 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000801 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000802 char *tmp;
803
804 sprintf(buf, format, *val);
805 printf("%30s [%s] ", prompt, buf);
806 fgets(buf, sizeof(buf), stdin);
807 if (buf[strlen (buf) - 1] == '\n')
808 buf[strlen (buf) - 1] = '\0';
809 if (!buf[0])
810 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000811 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000812 if (*tmp)
813 com_err(com, 0, "Bad value - %s", buf);
814 else
815 *val = v;
816}
817
818
819void do_modify_inode(int argc, char *argv[])
820{
821 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000822 ext2_ino_t inode_num;
823 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000824 unsigned char *frag, *fsize;
825 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500826 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000827 const char *hex_format = "0x%x";
828 const char *octal_format = "0%o";
829 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000830
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500831 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000832 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000833
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500834 os = current_fs->super->s_creator_os;
835
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500836 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000837 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000839 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
840 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
841 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
842 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
843 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
844 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
845 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
846 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
847 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
848 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
849 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000850 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000851#if 0
852 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
853#endif
854 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000855 if (LINUX_S_ISDIR(inode.i_mode))
856 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
857 else
858 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000859
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000860 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000861 modify_u32(argv[0], "Translator Block",
862 decimal_format, &inode.osd1.hurd1.h_i_translator);
863
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000864 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000865 switch (os) {
866 case EXT2_OS_LINUX:
867 frag = &inode.osd2.linux2.l_i_frag;
868 fsize = &inode.osd2.linux2.l_i_fsize;
869 break;
870 case EXT2_OS_HURD:
871 frag = &inode.osd2.hurd2.h_i_frag;
872 fsize = &inode.osd2.hurd2.h_i_fsize;
873 break;
874 case EXT2_OS_MASIX:
875 frag = &inode.osd2.masix2.m_i_frag;
876 fsize = &inode.osd2.masix2.m_i_fsize;
877 break;
878 default:
879 frag = fsize = 0;
880 }
881 if (frag)
882 modify_u8(argv[0], "Fragment number", decimal_format, frag);
883 if (fsize)
884 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
885
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
887 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000888 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000889 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000890 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000891 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000892 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000893 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000894 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500896 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000898}
899
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900void do_change_working_dir(int argc, char *argv[])
901{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000902 ext2_ino_t inode;
903 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000904
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500905 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 return;
907
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000908 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000909 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500910 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000911 return;
912 }
913 cwd = inode;
914 return;
915}
916
Theodore Ts'o3839e651997-04-26 13:21:57 +0000917void do_print_working_directory(int argc, char *argv[])
918{
919 int retval;
920 char *pathname = NULL;
921
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500922 if (common_args_process(argc, argv, 1, 1,
923 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000924 return;
925
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000926 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927 if (retval) {
928 com_err(argv[0], retval,
929 "while trying to get pathname of cwd");
930 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000931 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000932 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000933 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000934 if (retval) {
935 com_err(argv[0], retval,
936 "while trying to get pathname of root");
937 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000938 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939 free(pathname);
940 return;
941}
942
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500943/*
944 * Given a mode, return the ext2 file type
945 */
946static int ext2_file_type(unsigned int mode)
947{
948 if (LINUX_S_ISREG(mode))
949 return EXT2_FT_REG_FILE;
950
951 if (LINUX_S_ISDIR(mode))
952 return EXT2_FT_DIR;
953
954 if (LINUX_S_ISCHR(mode))
955 return EXT2_FT_CHRDEV;
956
957 if (LINUX_S_ISBLK(mode))
958 return EXT2_FT_BLKDEV;
959
960 if (LINUX_S_ISLNK(mode))
961 return EXT2_FT_SYMLINK;
962
963 if (LINUX_S_ISFIFO(mode))
964 return EXT2_FT_FIFO;
965
966 if (LINUX_S_ISSOCK(mode))
967 return EXT2_FT_SOCK;
968
969 return 0;
970}
971
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000972static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500974 ext2_ino_t ino;
975 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000976 int retval;
977 ext2_ino_t dir;
978 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000979
980 /*
981 * Get the source inode
982 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500983 ino = string_to_inode(sourcename);
984 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000985 return;
986 basename = strrchr(sourcename, '/');
987 if (basename)
988 basename++;
989 else
990 basename = sourcename;
991 /*
992 * Figure out the destination. First see if it exists and is
993 * a directory.
994 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000995 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000996 dest = basename;
997 else {
998 /*
999 * OK, it doesn't exist. See if it is
1000 * '<dir>/basename' or 'basename'
1001 */
1002 cp = strrchr(destname, '/');
1003 if (cp) {
1004 *cp = 0;
1005 dir = string_to_inode(destname);
1006 if (!dir)
1007 return;
1008 dest = cp+1;
1009 } else {
1010 dir = cwd;
1011 dest = destname;
1012 }
1013 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001014
1015 if (debugfs_read_inode(ino, &inode, sourcename))
1016 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001017
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001018 retval = ext2fs_link(current_fs, dir, dest, ino,
1019 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001020 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001021 com_err("make_link", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001022 return;
1023}
1024
1025
1026void do_link(int argc, char *argv[])
1027{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001028 if (common_args_process(argc, argv, 3, 3, "link",
1029 "<source file> <dest_name>", CHECK_FS_RW))
1030 return;
1031
1032 make_link(argv[1], argv[2]);
1033}
1034
1035static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001036 int blockcnt EXT2FS_ATTR((unused)),
1037 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001038{
1039 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001040
1041 block = *blocknr;
1042 ext2fs_block_alloc_stats(fs, block, +1);
1043 return 0;
1044}
1045
1046void do_undel(int argc, char *argv[])
1047{
1048 ext2_ino_t ino;
1049 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001050
1051 if (common_args_process(argc, argv, 3, 3, "undelete",
1052 "<inode_num> <dest_name>",
1053 CHECK_FS_RW | CHECK_FS_BITMAPS))
1054 return;
1055
1056 ino = string_to_inode(argv[1]);
1057 if (!ino)
1058 return;
1059
1060 if (debugfs_read_inode(ino, &inode, argv[1]))
1061 return;
1062
1063 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1064 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001065 return;
1066 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001067
1068 /*
1069 * XXX this function doesn't handle changing the links count on the
1070 * parent directory when undeleting a directory.
1071 */
1072 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1073 inode.i_dtime = 0;
1074
1075 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001076 return;
1077
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001078 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1079 mark_blocks_proc, NULL);
1080
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001081 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001082
Theodore Ts'o3839e651997-04-26 13:21:57 +00001083 make_link(argv[1], argv[2]);
1084}
1085
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001086static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001087{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001088 int retval;
1089 ext2_ino_t dir;
1090 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001091
1092 basename = strrchr(filename, '/');
1093 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001094 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001095 dir = string_to_inode(filename);
1096 if (!dir)
1097 return;
1098 } else {
1099 dir = cwd;
1100 basename = filename;
1101 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001102 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001103 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001104 com_err("unlink_file_by_name", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001105 return;
1106}
1107
1108void do_unlink(int argc, char *argv[])
1109{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001110 if (common_args_process(argc, argv, 2, 2, "link",
1111 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001112 return;
1113
1114 unlink_file_by_name(argv[1]);
1115}
1116
1117void do_find_free_block(int argc, char *argv[])
1118{
1119 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001120 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001121 errcode_t retval;
1122 char *tmp;
1123
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001124 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1125 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001126 return;
1127 }
1128 if (check_fs_open(argv[0]))
1129 return;
1130
1131 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001132 count = strtol(argv[1],&tmp,0);
1133 if (*tmp) {
1134 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1135 return;
1136 }
1137 } else
1138 count = 1;
1139
1140 if (argc > 2) {
1141 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001142 if (*tmp) {
1143 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1144 return;
1145 }
1146 }
1147 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001148 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001149
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001150 printf("Free blocks found: ");
1151 free_blk = goal - 1;
1152 while (count-- > 0) {
1153 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1154 &free_blk);
1155 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001156 com_err("ext2fs_new_block", retval, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001157 return;
1158 } else
1159 printf("%d ", free_blk);
1160 }
1161 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001162}
1163
1164void do_find_free_inode(int argc, char *argv[])
1165{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001166 ext2_ino_t free_inode, dir;
1167 int mode;
1168 int retval;
1169 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001170
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001171 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1172 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001173 return;
1174 }
1175 if (check_fs_open(argv[0]))
1176 return;
1177
1178 if (argc > 1) {
1179 dir = strtol(argv[1], &tmp, 0);
1180 if (*tmp) {
1181 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1182 return;
1183 }
1184 }
1185 else
1186 dir = root;
1187 if (argc > 2) {
1188 mode = strtol(argv[2], &tmp, 0);
1189 if (*tmp) {
1190 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1191 return;
1192 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001193 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001194 mode = 010755;
1195
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001196 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001197 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001198 com_err("ext2fs_new_inode", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001199 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001200 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001201}
1202
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001203static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001204{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001205 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001206 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001207 int got;
1208 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001209 char buf[8192];
1210 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001211
Theodore Ts'o5a513841997-10-25 22:41:14 +00001212 retval = ext2fs_file_open(current_fs, newfile,
1213 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001214 if (retval)
1215 return retval;
1216
Theodore Ts'o5a513841997-10-25 22:41:14 +00001217 while (1) {
1218 got = read(fd, buf, sizeof(buf));
1219 if (got == 0)
1220 break;
1221 if (got < 0) {
1222 retval = errno;
1223 goto fail;
1224 }
1225 ptr = buf;
1226 while (got > 0) {
1227 retval = ext2fs_file_write(e2_file, ptr,
1228 got, &written);
1229 if (retval)
1230 goto fail;
1231
1232 got -= written;
1233 ptr += written;
1234 }
1235 }
1236 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001237 return retval;
1238
1239fail:
1240 (void) ext2fs_file_close(e2_file);
1241 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001242}
1243
Theodore Ts'o5a513841997-10-25 22:41:14 +00001244
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001245void do_write(int argc, char *argv[])
1246{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001247 int fd;
1248 struct stat statbuf;
1249 ext2_ino_t newfile;
1250 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001251 struct ext2_inode inode;
1252
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001253 if (common_args_process(argc, argv, 3, 3, "write",
1254 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001255 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001256
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001257 fd = open(argv[1], O_RDONLY);
1258 if (fd < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001259 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001260 return;
1261 }
1262 if (fstat(fd, &statbuf) < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001263 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001264 close(fd);
1265 return;
1266 }
1267
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001268 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1269 if (retval == 0) {
1270 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1271 close(fd);
1272 return;
1273 }
1274
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001275 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001276 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001277 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001278 close(fd);
1279 return;
1280 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001281 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001282 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1283 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001284 if (retval == EXT2_ET_DIR_NO_SPACE) {
1285 retval = ext2fs_expand_dir(current_fs, cwd);
1286 if (retval) {
1287 com_err(argv[0], retval, "while expanding directory");
1288 return;
1289 }
1290 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1291 EXT2_FT_REG_FILE);
1292 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001293 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001294 com_err(argv[2], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001295 close(fd);
1296 return;
1297 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001298 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001299 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001300 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001301 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001302 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001303 inode.i_atime = inode.i_ctime = inode.i_mtime =
1304 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001305 inode.i_links_count = 1;
1306 inode.i_size = statbuf.st_size;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001307 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001308 close(fd);
1309 return;
1310 }
1311 if (LINUX_S_ISREG(inode.i_mode)) {
1312 retval = copy_file(fd, newfile);
1313 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001314 com_err("copy_file", retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001315 }
1316 close(fd);
1317}
1318
1319void do_mknod(int argc, char *argv[])
1320{
Theodore Ts'o54434922003-12-07 01:28:50 -05001321 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001322 ext2_ino_t newfile;
1323 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001324 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001325 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001326
1327 if (check_fs_open(argv[0]))
1328 return;
1329 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001330 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001331 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1332 return;
1333 }
1334 mode = minor = major = 0;
1335 switch (argv[2][0]) {
1336 case 'p':
1337 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001338 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001339 nr = 3;
1340 break;
1341 case 'c':
1342 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001343 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001344 nr = 5;
1345 break;
1346 case 'b':
1347 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001348 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001349 nr = 5;
1350 break;
1351 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001352 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001353 nr = 0;
1354 }
1355 if (nr == 5) {
1356 major = strtoul(argv[3], argv+3, 0);
1357 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001358 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001359 nr = 0;
1360 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001361 if (argc != nr)
1362 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001363 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001364 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001365 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001366 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001367 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001368 return;
1369 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001370 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001371 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001372 if (retval == EXT2_ET_DIR_NO_SPACE) {
1373 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001374 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001375 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001376 return;
1377 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001378 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1379 filetype);
1380 }
1381 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001382 com_err(argv[1], retval, 0);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001383 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001384 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001385 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001386 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001387 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1388 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001389 memset(&inode, 0, sizeof(inode));
1390 inode.i_mode = mode;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001391 inode.i_atime = inode.i_ctime = inode.i_mtime =
1392 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001393 if ((major < 256) && (minor < 256)) {
1394 inode.i_block[0] = major*256+minor;
1395 inode.i_block[1] = 0;
1396 } else {
1397 inode.i_block[0] = 0;
1398 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1399 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001400 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001401 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001402 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001403}
1404
Theodore Ts'o3839e651997-04-26 13:21:57 +00001405void do_mkdir(int argc, char *argv[])
1406{
1407 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001408 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001409 char *name;
1410 errcode_t retval;
1411
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001412 if (common_args_process(argc, argv, 2, 2, "mkdir",
1413 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001414 return;
1415
Theodore Ts'o3839e651997-04-26 13:21:57 +00001416 cp = strrchr(argv[1], '/');
1417 if (cp) {
1418 *cp = 0;
1419 parent = string_to_inode(argv[1]);
1420 if (!parent) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001421 com_err(argv[1], ENOENT, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001422 return;
1423 }
1424 name = cp+1;
1425 } else {
1426 parent = cwd;
1427 name = argv[1];
1428 }
1429
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001430try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001431 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001432 if (retval == EXT2_ET_DIR_NO_SPACE) {
1433 retval = ext2fs_expand_dir(current_fs, parent);
1434 if (retval) {
1435 com_err("argv[0]", retval, "while expanding directory");
1436 return;
1437 }
1438 goto try_again;
1439 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001440 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001441 com_err("ext2fs_mkdir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001442 return;
1443 }
1444
1445}
1446
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001447static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001448 int blockcnt EXT2FS_ATTR((unused)),
1449 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001450{
Theodore Ts'o34436892001-12-22 13:06:02 -05001451 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001452
1453 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001454 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001455 return 0;
1456}
1457
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001458static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001459{
1460 struct ext2_inode inode_buf;
1461
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001462 if (debugfs_read_inode(inode, &inode_buf, 0))
1463 return;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001464 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001465 if (debugfs_write_inode(inode, &inode_buf, 0))
1466 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001467 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1468 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001469
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001470 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1471 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001472 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001473 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1474 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001475}
1476
1477
1478void do_kill_file(int argc, char *argv[])
1479{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001480 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001481
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001482 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001483 return;
1484
Theodore Ts'o3839e651997-04-26 13:21:57 +00001485 kill_file_by_inode(inode_num);
1486}
1487
1488void do_rm(int argc, char *argv[])
1489{
1490 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001491 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001492 struct ext2_inode inode;
1493
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001494 if (common_args_process(argc, argv, 2, 2, "rm",
1495 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001496 return;
1497
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001498 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001499 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001500 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001501 return;
1502 }
1503
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001504 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001505 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001506
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001507 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001508 com_err(argv[0], 0, "file is a directory");
1509 return;
1510 }
1511
1512 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001513 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001514 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001515
1516 unlink_file_by_name(argv[1]);
1517 if (inode.i_links_count == 0)
1518 kill_file_by_inode(inode_num);
1519}
1520
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001521struct rd_struct {
1522 ext2_ino_t parent;
1523 int empty;
1524};
1525
Theodore Ts'o54434922003-12-07 01:28:50 -05001526static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1527 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001528 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001529 int offset EXT2FS_ATTR((unused)),
1530 int blocksize EXT2FS_ATTR((unused)),
1531 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001532 void *private)
1533{
1534 struct rd_struct *rds = (struct rd_struct *) private;
1535
1536 if (dirent->inode == 0)
1537 return 0;
1538 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1539 return 0;
1540 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1541 (dirent->name[1] == '.')) {
1542 rds->parent = dirent->inode;
1543 return 0;
1544 }
1545 rds->empty = 0;
1546 return 0;
1547}
1548
1549void do_rmdir(int argc, char *argv[])
1550{
1551 int retval;
1552 ext2_ino_t inode_num;
1553 struct ext2_inode inode;
1554 struct rd_struct rds;
1555
1556 if (common_args_process(argc, argv, 2, 2, "rmdir",
1557 "<filename>", CHECK_FS_RW))
1558 return;
1559
1560 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1561 if (retval) {
1562 com_err(argv[0], retval, "while trying to resolve filename");
1563 return;
1564 }
1565
1566 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1567 return;
1568
1569 if (!LINUX_S_ISDIR(inode.i_mode)) {
1570 com_err(argv[0], 0, "file is not a directory");
1571 return;
1572 }
1573
1574 rds.parent = 0;
1575 rds.empty = 1;
1576
1577 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1578 0, rmdir_proc, &rds);
1579 if (retval) {
1580 com_err(argv[0], retval, "while iterating over directory");
1581 return;
1582 }
1583 if (rds.empty == 0) {
1584 com_err(argv[0], 0, "directory not empty");
1585 return;
1586 }
1587
1588 inode.i_links_count = 0;
1589 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1590 return;
1591
1592 unlink_file_by_name(argv[1]);
1593 kill_file_by_inode(inode_num);
1594
1595 if (rds.parent) {
1596 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1597 return;
1598 if (inode.i_links_count > 1)
1599 inode.i_links_count--;
1600 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1601 return;
1602 }
1603}
1604
Theodore Ts'o54434922003-12-07 01:28:50 -05001605void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1606 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001607{
1608 FILE *out = stdout;
1609
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001610 if (current_fs)
1611 fprintf(out, "Open mode: read-%s\n",
1612 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001613 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001614 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001615}
1616
1617void do_expand_dir(int argc, char *argv[])
1618{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001619 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001620 int retval;
1621
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001622 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001623 return;
1624
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001625 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001626 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001627 com_err("ext2fs_expand_dir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001628 return;
1629}
1630
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001631void do_features(int argc, char *argv[])
1632{
1633 int i;
1634
1635 if (check_fs_open(argv[0]))
1636 return;
1637
1638 if ((argc != 1) && check_fs_read_write(argv[0]))
1639 return;
1640 for (i=1; i < argc; i++) {
1641 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001642 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001643 com_err(argv[0], 0, "Unknown feature: %s\n",
1644 argv[i]);
1645 else
1646 ext2fs_mark_super_dirty(current_fs);
1647 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001648 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001649}
1650
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001651void do_bmap(int argc, char *argv[])
1652{
1653 ext2_ino_t ino;
1654 blk_t blk, pblk;
1655 int err;
1656 errcode_t errcode;
1657
1658 if (common_args_process(argc, argv, 3, 3, argv[0],
1659 "<file> logical_blk", 0))
1660 return;
1661
1662 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001663 if (!ino)
1664 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001665 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1666
1667 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1668 if (errcode) {
1669 com_err("argv[0]", errcode,
1670 "while mapping logical block %d\n", blk);
1671 return;
1672 }
1673 printf("%d\n", pblk);
1674}
1675
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001676void do_imap(int argc, char *argv[])
1677{
1678 ext2_ino_t ino;
1679 unsigned long group, block, block_nr, offset;
1680
1681 if (common_args_process(argc, argv, 2, 2, argv[0],
1682 "<file>", 0))
1683 return;
1684 ino = string_to_inode(argv[1]);
1685 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001686 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001687
1688 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1689 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1690 EXT2_INODE_SIZE(current_fs->super);
1691 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1692 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001693 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001694 group);
1695 return;
1696 }
1697 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1698 block;
1699 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1700
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001701 printf("Inode %d is part of block group %lu\n"
1702 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001703 block_nr, offset);
1704
1705}
1706
Theodore Ts'o4efae602005-09-24 21:56:38 -04001707void do_set_current_time(int argc, char *argv[])
1708{
Theodore Ts'o4efae602005-09-24 21:56:38 -04001709 time_t now;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001710
Theodore Ts'o4efae602005-09-24 21:56:38 -04001711 if (common_args_process(argc, argv, 2, 2, argv[0],
1712 "<time>", 0))
1713 return;
1714
1715 now = string_to_time(argv[1]);
1716 if (now == ((time_t) -1)) {
1717 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1718 argv[1]);
1719 return;
1720
1721 } else {
1722 printf("Setting current time to %s\n", time_to_string(now));
1723 current_fs->now = now;
1724 }
1725}
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001726
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001727static int source_file(const char *cmd_file, int sci_idx)
1728{
1729 FILE *f;
1730 char buf[256];
1731 char *cp;
1732 int exit_status = 0;
1733 int retval;
1734
1735 if (strcmp(cmd_file, "-") == 0)
1736 f = stdin;
1737 else {
1738 f = fopen(cmd_file, "r");
1739 if (!f) {
1740 perror(cmd_file);
1741 exit(1);
1742 }
1743 }
1744 setbuf(stdout, NULL);
1745 setbuf(stderr, NULL);
1746 while (!feof(f)) {
1747 if (fgets(buf, sizeof(buf), f) == NULL)
1748 break;
1749 cp = strchr(buf, '\n');
1750 if (cp)
1751 *cp = 0;
1752 cp = strchr(buf, '\r');
1753 if (cp)
1754 *cp = 0;
1755 printf("debugfs: %s\n", buf);
1756 retval = ss_execute_line(sci_idx, buf);
1757 if (retval) {
1758 ss_perror(sci_idx, retval, buf);
1759 exit_status++;
1760 }
1761 }
1762 return exit_status;
1763}
1764
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001765int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001766{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001767 int retval;
1768 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001769 const char *usage = "Usage: debugfs [-b blocksize] [-s superblock] [-f cmd_file] [-R request] [-V] [[-w] [-c] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00001770 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001771 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001772 char *request = 0;
1773 int exit_status = 0;
1774 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001775 blk_t superblock = 0;
1776 blk_t blocksize = 0;
1777 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001778 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001779
1780 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001781 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1782 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001783
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001784 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001785 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001786 case 'R':
1787 request = optarg;
1788 break;
1789 case 'f':
1790 cmd_file = optarg;
1791 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001792 case 'd':
1793 data_filename = optarg;
1794 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001795 case 'i':
1796 open_flags |= EXT2_FLAG_IMAGE_FILE;
1797 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001798 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001799 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001800 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001801 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001802 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001803 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001804 break;
1805 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001806 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001807 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001808 break;
1809 case 'c':
1810 catastrophic = 1;
1811 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001812 case 'V':
1813 /* Print version number and exit */
1814 fprintf(stderr, "\tUsing %s\n",
1815 error_message(EXT2_ET_BASE));
1816 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001817 default:
1818 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001819 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001820 }
1821 }
1822 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001823 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001824 superblock, blocksize, catastrophic,
1825 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001826
1827 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1828 &debug_cmds, &retval);
1829 if (retval) {
1830 ss_perror(sci_idx, retval, "creating invocation");
1831 exit(1);
1832 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001833 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001834
1835 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1836 if (retval) {
1837 ss_perror(sci_idx, retval, "adding standard requests");
1838 exit (1);
1839 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001840 if (request) {
1841 retval = 0;
1842 retval = ss_execute_line(sci_idx, request);
1843 if (retval) {
1844 ss_perror(sci_idx, retval, request);
1845 exit_status++;
1846 }
1847 } else if (cmd_file) {
1848 exit_status = source_file(cmd_file, sci_idx);
1849 } else {
1850 ss_listen(sci_idx);
1851 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001852
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001853 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001854 close_filesystem();
1855
Theodore Ts'oe5973042000-01-18 17:58:34 +00001856 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001857}