blob: 7eb7dc6298a09120855f04e524b390e75bcf4e7c [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'o1ad54a92004-07-28 21:11:48 -0400131 while ((c = getopt (argc, argv, "iwfcb: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'o2e8d40d2000-05-27 15:15:40 +0000142 case 'c':
143 catastrophic = 1;
144 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400145 case 'd':
146 data_filename = optarg;
147 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000148 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500149 blocksize = parse_ulong(optarg, argv[0],
150 "block size", &err);
151 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000152 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000153 break;
154 case 's':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500155 superblock = parse_ulong(optarg, argv[0],
156 "superblock number", &err);
157 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000158 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000159 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500161 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 }
163 }
164 if (optind != argc-1) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500165 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166 }
167 if (check_fs_not_open(argv[0]))
168 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000169 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400170 superblock, blocksize, catastrophic,
171 data_filename);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500172 return;
173
174print_usage:
175 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
176 "[-c] [-w] <device>\n", argv[0]);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000177}
178
179void do_lcd(int argc, char **argv)
180{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500181 if (common_args_process(argc, argv, 2, 2, "lcd",
182 "<native dir>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000183 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000184
185 if (chdir(argv[1]) == -1) {
186 com_err(argv[0], errno,
187 "while trying to change native directory to %s",
188 argv[1]);
189 return;
190 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000191}
192
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000193static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194{
195 int retval;
196
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000197 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
198 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000199 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500200 com_err("ext2fs_write_inode_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000201 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000202 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
203 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500205 com_err("ext2fs_write_block_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000207 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000208 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500209 com_err("ext2fs_close", retval, 0);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000210 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 return;
212}
213
214void do_close_filesys(int argc, char **argv)
215{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500216 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000217 return;
218 close_filesystem();
219}
220
221void do_init_filesys(int argc, char **argv)
222{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223 struct ext2_super_block param;
224 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500225 int err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500227 if (common_args_process(argc, argv, 3, 3, "initialize",
228 "<device> <blocksize>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229 return;
230
231 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400232 param.s_blocks_count = parse_ulong(argv[2], argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500233 "blocks count", &err);
234 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000235 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000236 retval = ext2fs_initialize(argv[1], 0, &param,
237 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 if (retval) {
239 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000240 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 return;
242 }
243 root = cwd = EXT2_ROOT_INO;
244 return;
245}
246
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000247static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000248{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000249 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000250 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000251
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000252 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000253 for (i=0; i <3; i++,mask++) {
254 for (j=0,m=1; j < 32; j++, m<<=1) {
255 if (*mask & m) {
256 fprintf(f, " %s", e2p_feature2string(i, m));
257 printed++;
258 }
259 }
260 }
261 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000262 fputs("(none)", f);
263 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000264}
265
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266void do_show_super_stats(int argc, char *argv[])
267{
Theodore Ts'o54434922003-12-07 01:28:50 -0500268 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000269 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000270 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000271 int c, header_only = 0;
Theodore Ts'o34be9602002-07-15 16:56:41 -0400272 int numdirs = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000273
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400274 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000275 while ((c = getopt (argc, argv, "h")) != EOF) {
276 switch (c) {
277 case 'h':
278 header_only++;
279 break;
280 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500281 goto print_usage;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000282 }
283 }
284 if (optind != argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500285 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 }
287 if (check_fs_open(argv[0]))
288 return;
289 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000290
291 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400292 for (i=0; i < current_fs->group_desc_count; i++)
293 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
294 fprintf(out, "Directories: %d\n", numdirs);
295
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000296 if (header_only) {
297 close_pager(out);
298 return;
299 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000300
301 gdp = &current_fs->group_desc[0];
302 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000303 fprintf(out, " Group %2d: block bitmap at %d, "
304 "inode bitmap at %d, "
305 "inode table at %d\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000306 " %d free %s, "
307 "%d free %s, "
308 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000309 i, gdp->bg_block_bitmap,
310 gdp->bg_inode_bitmap, gdp->bg_inode_table,
311 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000312 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000313 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000314 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000315 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000316 gdp->bg_used_dirs_count != 1 ? "directories"
317 : "directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000318 close_pager(out);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500319 return;
320print_usage:
321 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000322}
323
Theodore Ts'o54434922003-12-07 01:28:50 -0500324void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
325 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000326{
327 if (check_fs_open(argv[0]))
328 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000329 if (check_fs_read_write(argv[0]))
330 return;
331
332 if (argv[1] && !strcmp(argv[1], "-clean"))
333 current_fs->super->s_state |= EXT2_VALID_FS;
334 else
335 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000336 ext2fs_mark_super_dirty(current_fs);
337}
338
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700340 FILE *f;
341 e2_blkcnt_t total;
342 blk_t first_block, last_block;
343 e2_blkcnt_t first_bcnt, last_bcnt;
344 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345};
346
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000347static void finish_range(struct list_blocks_struct *lb)
348{
349 if (lb->first_block == 0)
350 return;
351 if (lb->first)
352 lb->first = 0;
353 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000354 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000355 if (lb->first_block == lb->last_block)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100356 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000357 else
Theodore Ts'oe8981882001-11-30 11:51:30 +0100358 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000359 lb->last_bcnt, lb->first_block, lb->last_block);
360 lb->first_block = 0;
361}
362
Theodore Ts'o54434922003-12-07 01:28:50 -0500363static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
364 blk_t *blocknr, e2_blkcnt_t blockcnt,
365 blk_t ref_block EXT2FS_ATTR((unused)),
366 int ref_offset EXT2FS_ATTR((unused)),
367 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000368{
369 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
370
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000371 lb->total++;
372 if (blockcnt >= 0) {
373 /*
374 * See if we can add on to the existing range (if it exists)
375 */
376 if (lb->first_block &&
377 (lb->last_block+1 == *blocknr) &&
378 (lb->last_bcnt+1 == blockcnt)) {
379 lb->last_block = *blocknr;
380 lb->last_bcnt = blockcnt;
381 return 0;
382 }
383 /*
384 * Start a new range.
385 */
386 finish_range(lb);
387 lb->first_block = lb->last_block = *blocknr;
388 lb->first_bcnt = lb->last_bcnt = blockcnt;
389 return 0;
390 }
391 /*
392 * Not a normal block. Always force a new range.
393 */
394 finish_range(lb);
395 if (lb->first)
396 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000397 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000398 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000399 if (blockcnt == -1)
400 fprintf(lb->f, "(IND):%d", *blocknr);
401 else if (blockcnt == -2)
402 fprintf(lb->f, "(DIND):%d", *blocknr);
403 else if (blockcnt == -3)
404 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000405 return 0;
406}
407
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500408static void dump_xattr_string(FILE *out, const char *str, int len)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500409{
410 int printable = 1;
411 int i;
412
413 /* check is string printable? */
414 for (i = 0; i < len; i++)
415 if (!isprint(str[i])) {
416 printable = 0;
417 break;
418 }
419
420 for (i = 0; i < len; i++)
421 if (printable)
422 fprintf(out, "%c", str[i]);
423 else
424 fprintf(out, "%02x ", str[i]);
425}
426
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500427static void internal_dump_inode_extra(FILE *out, const char *prefix,
428 ext2_ino_t inode_num,
429 struct ext2_inode_large *inode)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500430{
431 struct ext2_ext_attr_entry *entry;
432 __u32 *magic;
433 char *start, *end;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500434 unsigned int storage_size;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500435
Theodore Ts'oc9ae3f92005-03-21 01:08:10 -0500436 fprintf(out, "Size of extra inode fields: %d\n", inode->i_extra_isize);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500437 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
438 EXT2_GOOD_OLD_INODE_SIZE) {
439 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
440 inode->i_extra_isize);
441 return;
442 }
443 storage_size = EXT2_INODE_SIZE(current_fs->super) -
444 EXT2_GOOD_OLD_INODE_SIZE -
445 inode->i_extra_isize;
446 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
447 inode->i_extra_isize);
448 if (*magic == EXT2_EXT_ATTR_MAGIC) {
449 fprintf(out, "Extended attributes stored in inode body: \n");
450 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
451 start = (char *) magic + sizeof(__u32);
452 entry = (struct ext2_ext_attr_entry *) start;
453 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
454 struct ext2_ext_attr_entry *next =
455 EXT2_EXT_ATTR_NEXT(entry);
456 if (entry->e_value_size > storage_size ||
457 (char *) next >= end) {
458 fprintf(out, "invalid EA entry in inode\n");
459 return;
460 }
461 fprintf(out, " ");
462 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
463 entry->e_name_len);
464 fprintf(out, " = \"");
465 dump_xattr_string(out, start + entry->e_value_offs,
466 entry->e_value_size);
467 fprintf(out, "\" (%d)\n", entry->e_value_size);
468 entry = next;
469 }
470 }
471}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000473static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000474{
475 struct list_blocks_struct lb;
476
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000477 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000478 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000479 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000481 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700482 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000483 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000484 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 if (lb.total)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100486 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 fprintf(f,"\n");
488}
489
490
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000491void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000492 ext2_ino_t inode_num, struct ext2_inode *inode,
493 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000494{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000495 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000496 char frag, fsize;
497 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000499 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
500 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
501 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
502 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
503 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
504 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
505 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000506 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000507 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
508 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
509 prefix,
510 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
511 fprintf(out, "%sUser: %5d Group: %5d Size: ",
512 prefix, inode->i_uid, inode->i_gid);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700513 if (LINUX_S_ISREG(inode->i_mode)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000514 __u64 i_size = (inode->i_size |
515 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700516
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000517 fprintf(out, "%lld\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700518 } else
519 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000520 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000521 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000522 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
523 prefix,
524 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
525 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000526 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000527 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
528 prefix,
529 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
530 fprintf(out, "%sLinks: %d Blockcount: %d\n",
531 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000532 switch (os) {
533 case EXT2_OS_LINUX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000534 frag = inode->osd2.linux2.l_i_frag;
535 fsize = inode->osd2.linux2.l_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000536 break;
537 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000538 frag = inode->osd2.hurd2.h_i_frag;
539 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000540 break;
541 case EXT2_OS_MASIX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000542 frag = inode->osd2.masix2.m_i_frag;
543 fsize = inode->osd2.masix2.m_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000544 break;
545 default:
546 frag = fsize = 0;
547 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000548 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
549 prefix, inode->i_faddr, frag, fsize);
550 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
551 time_to_string(inode->i_ctime));
552 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
553 time_to_string(inode->i_atime));
554 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
555 time_to_string(inode->i_mtime));
556 if (inode->i_dtime)
557 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
558 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500559 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
560 internal_dump_inode_extra(out, prefix, inode_num,
561 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500562 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000563 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
564 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500565 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
566 int major, minor;
567 const char *devnote;
568
569 if (inode->i_block[0]) {
570 major = (inode->i_block[0] >> 8) & 255;
571 minor = inode->i_block[0] & 255;
572 devnote = "";
573 } else {
574 major = (inode->i_block[1] & 0xfff00) >> 8;
575 minor = ((inode->i_block[1] & 0xff) |
576 ((inode->i_block[1] >> 12) & 0xfff00));
577 devnote = "(New-style) ";
578 }
579 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
580 devnote, major, minor, major, minor);
581 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000582 else if (do_dump_blocks)
583 dump_blocks(out, prefix, inode_num);
584}
585
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500586static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000587{
588 FILE *out;
589
590 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500591 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000592 close_pager(out);
593}
594
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595void do_stat(int argc, char *argv[])
596{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000597 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500598 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000599
Theodore Ts'o64777392005-05-05 17:21:46 -0400600 if (check_fs_open(argv[0]))
Theodore Ts'o8363e352005-05-06 09:04:37 -0400601 return;
Theodore Ts'o64777392005-05-05 17:21:46 -0400602
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500603 inode_buf = (struct ext2_inode *)
604 malloc(EXT2_INODE_SIZE(current_fs->super));
605 if (!inode_buf) {
606 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500608 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500610 if (common_inode_args_process(argc, argv, &inode, 0)) {
611 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500612 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500613 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000614
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500615 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
616 EXT2_INODE_SIZE(current_fs->super))) {
617 free(inode_buf);
618 return;
619 }
620
621 dump_inode(inode, inode_buf);
622 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623 return;
624}
625
626void do_chroot(int argc, char *argv[])
627{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000628 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 int retval;
630
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500631 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000632 return;
633
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000634 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000635 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500636 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 return;
638 }
639 root = inode;
640}
641
642void do_clri(int argc, char *argv[])
643{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000644 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000645 struct ext2_inode inode_buf;
646
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500647 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648 return;
649
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500650 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000652 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500653 if (debugfs_write_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}
656
657void do_freei(int argc, char *argv[])
658{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000659 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000660
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500661 if (common_inode_args_process(argc, argv, &inode,
662 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 return;
664
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000665 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000667 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
668 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000669}
670
671void do_seti(int argc, char *argv[])
672{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000673 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500675 if (common_inode_args_process(argc, argv, &inode,
676 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 return;
678
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000679 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000681 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
682 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000683}
684
685void do_testi(int argc, char *argv[])
686{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000687 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500689 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 return;
691
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000692 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000693 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000694 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000695 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000696}
697
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698void do_freeb(int argc, char *argv[])
699{
700 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500701 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000702
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500703 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000705 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000706 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500707 while (count-- > 0) {
708 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
709 com_err(argv[0], 0, "Warning: block %d already clear",
710 block);
711 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
712 block++;
713 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000714 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715}
716
717void do_setb(int argc, char *argv[])
718{
719 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500720 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000721
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500722 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000724 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500726 while (count-- > 0) {
727 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
728 com_err(argv[0], 0, "Warning: block %d already set",
729 block);
730 ext2fs_mark_block_bitmap(current_fs->block_map,block);
731 block++;
732 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000733 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000734}
735
736void do_testb(int argc, char *argv[])
737{
738 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500739 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000740
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500741 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000742 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500743 while (count-- > 0) {
744 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
745 printf("Block %d marked in use\n", block);
746 else
747 printf("Block %d not in use\n", block);
748 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000750}
751
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000752static void modify_u8(char *com, const char *prompt,
753 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000754{
755 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000756 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000757 char *tmp;
758
759 sprintf(buf, format, *val);
760 printf("%30s [%s] ", prompt, buf);
761 fgets(buf, sizeof(buf), stdin);
762 if (buf[strlen (buf) - 1] == '\n')
763 buf[strlen (buf) - 1] = '\0';
764 if (!buf[0])
765 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000766 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000767 if (*tmp)
768 com_err(com, 0, "Bad value - %s", buf);
769 else
770 *val = v;
771}
772
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000773static void modify_u16(char *com, const char *prompt,
774 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000775{
776 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000777 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778 char *tmp;
779
780 sprintf(buf, format, *val);
781 printf("%30s [%s] ", prompt, buf);
782 fgets(buf, sizeof(buf), stdin);
783 if (buf[strlen (buf) - 1] == '\n')
784 buf[strlen (buf) - 1] = '\0';
785 if (!buf[0])
786 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000787 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000788 if (*tmp)
789 com_err(com, 0, "Bad value - %s", buf);
790 else
791 *val = v;
792}
793
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000794static void modify_u32(char *com, const char *prompt,
795 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796{
797 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000798 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799 char *tmp;
800
801 sprintf(buf, format, *val);
802 printf("%30s [%s] ", prompt, buf);
803 fgets(buf, sizeof(buf), stdin);
804 if (buf[strlen (buf) - 1] == '\n')
805 buf[strlen (buf) - 1] = '\0';
806 if (!buf[0])
807 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000808 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000809 if (*tmp)
810 com_err(com, 0, "Bad value - %s", buf);
811 else
812 *val = v;
813}
814
815
816void do_modify_inode(int argc, char *argv[])
817{
818 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000819 ext2_ino_t inode_num;
820 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000821 unsigned char *frag, *fsize;
822 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500823 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000824 const char *hex_format = "0x%x";
825 const char *octal_format = "0%o";
826 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500828 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000829 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000830
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500831 os = current_fs->super->s_creator_os;
832
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500833 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000834 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000835
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000836 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
837 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
838 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
839 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
840 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
841 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
842 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
843 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
844 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
845 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
846 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000847 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000848#if 0
849 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
850#endif
851 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000852 if (LINUX_S_ISDIR(inode.i_mode))
853 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
854 else
855 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000856
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000857 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000858 modify_u32(argv[0], "Translator Block",
859 decimal_format, &inode.osd1.hurd1.h_i_translator);
860
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000861 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000862 switch (os) {
863 case EXT2_OS_LINUX:
864 frag = &inode.osd2.linux2.l_i_frag;
865 fsize = &inode.osd2.linux2.l_i_fsize;
866 break;
867 case EXT2_OS_HURD:
868 frag = &inode.osd2.hurd2.h_i_frag;
869 fsize = &inode.osd2.hurd2.h_i_fsize;
870 break;
871 case EXT2_OS_MASIX:
872 frag = &inode.osd2.masix2.m_i_frag;
873 fsize = &inode.osd2.masix2.m_i_fsize;
874 break;
875 default:
876 frag = fsize = 0;
877 }
878 if (frag)
879 modify_u8(argv[0], "Fragment number", decimal_format, frag);
880 if (fsize)
881 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
882
Theodore Ts'o3839e651997-04-26 13:21:57 +0000883 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
884 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000885 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000887 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000888 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000889 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000890 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000891 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000892 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500893 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000894 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895}
896
Theodore Ts'o3839e651997-04-26 13:21:57 +0000897void do_change_working_dir(int argc, char *argv[])
898{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000899 ext2_ino_t inode;
900 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000901
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500902 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000903 return;
904
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000905 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500907 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000908 return;
909 }
910 cwd = inode;
911 return;
912}
913
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914void do_print_working_directory(int argc, char *argv[])
915{
916 int retval;
917 char *pathname = NULL;
918
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500919 if (common_args_process(argc, argv, 1, 1,
920 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000921 return;
922
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000923 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000924 if (retval) {
925 com_err(argv[0], retval,
926 "while trying to get pathname of cwd");
927 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000928 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000929 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000930 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000931 if (retval) {
932 com_err(argv[0], retval,
933 "while trying to get pathname of root");
934 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000935 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000936 free(pathname);
937 return;
938}
939
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500940/*
941 * Given a mode, return the ext2 file type
942 */
943static int ext2_file_type(unsigned int mode)
944{
945 if (LINUX_S_ISREG(mode))
946 return EXT2_FT_REG_FILE;
947
948 if (LINUX_S_ISDIR(mode))
949 return EXT2_FT_DIR;
950
951 if (LINUX_S_ISCHR(mode))
952 return EXT2_FT_CHRDEV;
953
954 if (LINUX_S_ISBLK(mode))
955 return EXT2_FT_BLKDEV;
956
957 if (LINUX_S_ISLNK(mode))
958 return EXT2_FT_SYMLINK;
959
960 if (LINUX_S_ISFIFO(mode))
961 return EXT2_FT_FIFO;
962
963 if (LINUX_S_ISSOCK(mode))
964 return EXT2_FT_SOCK;
965
966 return 0;
967}
968
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000969static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000970{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500971 ext2_ino_t ino;
972 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000973 int retval;
974 ext2_ino_t dir;
975 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000976
977 /*
978 * Get the source inode
979 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500980 ino = string_to_inode(sourcename);
981 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000982 return;
983 basename = strrchr(sourcename, '/');
984 if (basename)
985 basename++;
986 else
987 basename = sourcename;
988 /*
989 * Figure out the destination. First see if it exists and is
990 * a directory.
991 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000992 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993 dest = basename;
994 else {
995 /*
996 * OK, it doesn't exist. See if it is
997 * '<dir>/basename' or 'basename'
998 */
999 cp = strrchr(destname, '/');
1000 if (cp) {
1001 *cp = 0;
1002 dir = string_to_inode(destname);
1003 if (!dir)
1004 return;
1005 dest = cp+1;
1006 } else {
1007 dir = cwd;
1008 dest = destname;
1009 }
1010 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001011
1012 if (debugfs_read_inode(ino, &inode, sourcename))
1013 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001014
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001015 retval = ext2fs_link(current_fs, dir, dest, ino,
1016 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001017 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001018 com_err("make_link", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001019 return;
1020}
1021
1022
1023void do_link(int argc, char *argv[])
1024{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001025 if (common_args_process(argc, argv, 3, 3, "link",
1026 "<source file> <dest_name>", CHECK_FS_RW))
1027 return;
1028
1029 make_link(argv[1], argv[2]);
1030}
1031
1032static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001033 int blockcnt EXT2FS_ATTR((unused)),
1034 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001035{
1036 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001037
1038 block = *blocknr;
1039 ext2fs_block_alloc_stats(fs, block, +1);
1040 return 0;
1041}
1042
1043void do_undel(int argc, char *argv[])
1044{
1045 ext2_ino_t ino;
1046 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001047
1048 if (common_args_process(argc, argv, 3, 3, "undelete",
1049 "<inode_num> <dest_name>",
1050 CHECK_FS_RW | CHECK_FS_BITMAPS))
1051 return;
1052
1053 ino = string_to_inode(argv[1]);
1054 if (!ino)
1055 return;
1056
1057 if (debugfs_read_inode(ino, &inode, argv[1]))
1058 return;
1059
1060 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1061 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001062 return;
1063 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001064
1065 /*
1066 * XXX this function doesn't handle changing the links count on the
1067 * parent directory when undeleting a directory.
1068 */
1069 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1070 inode.i_dtime = 0;
1071
1072 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001073 return;
1074
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001075 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1076 mark_blocks_proc, NULL);
1077
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001078 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001079
Theodore Ts'o3839e651997-04-26 13:21:57 +00001080 make_link(argv[1], argv[2]);
1081}
1082
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001083static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001084{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001085 int retval;
1086 ext2_ino_t dir;
1087 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001088
1089 basename = strrchr(filename, '/');
1090 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001091 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001092 dir = string_to_inode(filename);
1093 if (!dir)
1094 return;
1095 } else {
1096 dir = cwd;
1097 basename = filename;
1098 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001099 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001100 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001101 com_err("unlink_file_by_name", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001102 return;
1103}
1104
1105void do_unlink(int argc, char *argv[])
1106{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001107 if (common_args_process(argc, argv, 2, 2, "link",
1108 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001109 return;
1110
1111 unlink_file_by_name(argv[1]);
1112}
1113
1114void do_find_free_block(int argc, char *argv[])
1115{
1116 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001117 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001118 errcode_t retval;
1119 char *tmp;
1120
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001121 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1122 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001123 return;
1124 }
1125 if (check_fs_open(argv[0]))
1126 return;
1127
1128 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001129 count = strtol(argv[1],&tmp,0);
1130 if (*tmp) {
1131 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1132 return;
1133 }
1134 } else
1135 count = 1;
1136
1137 if (argc > 2) {
1138 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001139 if (*tmp) {
1140 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1141 return;
1142 }
1143 }
1144 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001145 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001146
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001147 printf("Free blocks found: ");
1148 free_blk = goal - 1;
1149 while (count-- > 0) {
1150 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1151 &free_blk);
1152 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001153 com_err("ext2fs_new_block", retval, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001154 return;
1155 } else
1156 printf("%d ", free_blk);
1157 }
1158 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001159}
1160
1161void do_find_free_inode(int argc, char *argv[])
1162{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001163 ext2_ino_t free_inode, dir;
1164 int mode;
1165 int retval;
1166 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001167
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001168 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1169 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001170 return;
1171 }
1172 if (check_fs_open(argv[0]))
1173 return;
1174
1175 if (argc > 1) {
1176 dir = strtol(argv[1], &tmp, 0);
1177 if (*tmp) {
1178 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1179 return;
1180 }
1181 }
1182 else
1183 dir = root;
1184 if (argc > 2) {
1185 mode = strtol(argv[2], &tmp, 0);
1186 if (*tmp) {
1187 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1188 return;
1189 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001190 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001191 mode = 010755;
1192
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001193 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001194 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001195 com_err("ext2fs_new_inode", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001196 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001197 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001198}
1199
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001200static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001201{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001202 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001203 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001204 int got;
1205 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001206 char buf[8192];
1207 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001208
Theodore Ts'o5a513841997-10-25 22:41:14 +00001209 retval = ext2fs_file_open(current_fs, newfile,
1210 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001211 if (retval)
1212 return retval;
1213
Theodore Ts'o5a513841997-10-25 22:41:14 +00001214 while (1) {
1215 got = read(fd, buf, sizeof(buf));
1216 if (got == 0)
1217 break;
1218 if (got < 0) {
1219 retval = errno;
1220 goto fail;
1221 }
1222 ptr = buf;
1223 while (got > 0) {
1224 retval = ext2fs_file_write(e2_file, ptr,
1225 got, &written);
1226 if (retval)
1227 goto fail;
1228
1229 got -= written;
1230 ptr += written;
1231 }
1232 }
1233 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001234 return retval;
1235
1236fail:
1237 (void) ext2fs_file_close(e2_file);
1238 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001239}
1240
Theodore Ts'o5a513841997-10-25 22:41:14 +00001241
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001242void do_write(int argc, char *argv[])
1243{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001244 int fd;
1245 struct stat statbuf;
1246 ext2_ino_t newfile;
1247 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001248 struct ext2_inode inode;
1249
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001250 if (common_args_process(argc, argv, 3, 3, "write",
1251 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001252 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001253
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001254 fd = open(argv[1], O_RDONLY);
1255 if (fd < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001256 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001257 return;
1258 }
1259 if (fstat(fd, &statbuf) < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001260 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001261 close(fd);
1262 return;
1263 }
1264
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001265 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1266 if (retval == 0) {
1267 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1268 close(fd);
1269 return;
1270 }
1271
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001272 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001273 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001274 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001275 close(fd);
1276 return;
1277 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001278 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001279 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1280 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001281 if (retval == EXT2_ET_DIR_NO_SPACE) {
1282 retval = ext2fs_expand_dir(current_fs, cwd);
1283 if (retval) {
1284 com_err(argv[0], retval, "while expanding directory");
1285 return;
1286 }
1287 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1288 EXT2_FT_REG_FILE);
1289 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001290 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001291 com_err(argv[2], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001292 close(fd);
1293 return;
1294 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001295 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001296 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001297 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001298 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001299 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001300 inode.i_atime = inode.i_ctime = inode.i_mtime =
1301 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001302 inode.i_links_count = 1;
1303 inode.i_size = statbuf.st_size;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001304 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001305 close(fd);
1306 return;
1307 }
1308 if (LINUX_S_ISREG(inode.i_mode)) {
1309 retval = copy_file(fd, newfile);
1310 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001311 com_err("copy_file", retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001312 }
1313 close(fd);
1314}
1315
1316void do_mknod(int argc, char *argv[])
1317{
Theodore Ts'o54434922003-12-07 01:28:50 -05001318 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001319 ext2_ino_t newfile;
1320 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001321 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001322 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001323
1324 if (check_fs_open(argv[0]))
1325 return;
1326 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001327 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001328 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1329 return;
1330 }
1331 mode = minor = major = 0;
1332 switch (argv[2][0]) {
1333 case 'p':
1334 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001335 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001336 nr = 3;
1337 break;
1338 case 'c':
1339 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001340 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001341 nr = 5;
1342 break;
1343 case 'b':
1344 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001345 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001346 nr = 5;
1347 break;
1348 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001349 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001350 nr = 0;
1351 }
1352 if (nr == 5) {
1353 major = strtoul(argv[3], argv+3, 0);
1354 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001355 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001356 nr = 0;
1357 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001358 if (argc != nr)
1359 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001360 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001361 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001362 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001363 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001364 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001365 return;
1366 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001367 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001368 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001369 if (retval == EXT2_ET_DIR_NO_SPACE) {
1370 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001371 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001372 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001373 return;
1374 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001375 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1376 filetype);
1377 }
1378 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001379 com_err(argv[1], retval, 0);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001380 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001381 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001382 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001383 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001384 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1385 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001386 memset(&inode, 0, sizeof(inode));
1387 inode.i_mode = mode;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001388 inode.i_atime = inode.i_ctime = inode.i_mtime =
1389 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001390 if ((major < 256) && (minor < 256)) {
1391 inode.i_block[0] = major*256+minor;
1392 inode.i_block[1] = 0;
1393 } else {
1394 inode.i_block[0] = 0;
1395 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1396 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001397 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001398 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001399 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001400}
1401
Theodore Ts'o3839e651997-04-26 13:21:57 +00001402void do_mkdir(int argc, char *argv[])
1403{
1404 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001405 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001406 char *name;
1407 errcode_t retval;
1408
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001409 if (common_args_process(argc, argv, 2, 2, "mkdir",
1410 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001411 return;
1412
Theodore Ts'o3839e651997-04-26 13:21:57 +00001413 cp = strrchr(argv[1], '/');
1414 if (cp) {
1415 *cp = 0;
1416 parent = string_to_inode(argv[1]);
1417 if (!parent) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001418 com_err(argv[1], ENOENT, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001419 return;
1420 }
1421 name = cp+1;
1422 } else {
1423 parent = cwd;
1424 name = argv[1];
1425 }
1426
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001427try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001428 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001429 if (retval == EXT2_ET_DIR_NO_SPACE) {
1430 retval = ext2fs_expand_dir(current_fs, parent);
1431 if (retval) {
1432 com_err("argv[0]", retval, "while expanding directory");
1433 return;
1434 }
1435 goto try_again;
1436 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001437 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001438 com_err("ext2fs_mkdir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001439 return;
1440 }
1441
1442}
1443
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001444static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001445 int blockcnt EXT2FS_ATTR((unused)),
1446 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001447{
Theodore Ts'o34436892001-12-22 13:06:02 -05001448 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001449
1450 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001451 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001452 return 0;
1453}
1454
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001455static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001456{
1457 struct ext2_inode inode_buf;
1458
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001459 if (debugfs_read_inode(inode, &inode_buf, 0))
1460 return;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001461 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001462 if (debugfs_write_inode(inode, &inode_buf, 0))
1463 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001464 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1465 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001466
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001467 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1468 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001469 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001470 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1471 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001472}
1473
1474
1475void do_kill_file(int argc, char *argv[])
1476{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001477 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001478
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001479 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001480 return;
1481
Theodore Ts'o3839e651997-04-26 13:21:57 +00001482 kill_file_by_inode(inode_num);
1483}
1484
1485void do_rm(int argc, char *argv[])
1486{
1487 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001488 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001489 struct ext2_inode inode;
1490
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001491 if (common_args_process(argc, argv, 2, 2, "rm",
1492 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001493 return;
1494
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001495 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001496 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001497 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001498 return;
1499 }
1500
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001501 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001502 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001503
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001504 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001505 com_err(argv[0], 0, "file is a directory");
1506 return;
1507 }
1508
1509 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001510 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001511 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001512
1513 unlink_file_by_name(argv[1]);
1514 if (inode.i_links_count == 0)
1515 kill_file_by_inode(inode_num);
1516}
1517
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001518struct rd_struct {
1519 ext2_ino_t parent;
1520 int empty;
1521};
1522
Theodore Ts'o54434922003-12-07 01:28:50 -05001523static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1524 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001525 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001526 int offset EXT2FS_ATTR((unused)),
1527 int blocksize EXT2FS_ATTR((unused)),
1528 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001529 void *private)
1530{
1531 struct rd_struct *rds = (struct rd_struct *) private;
1532
1533 if (dirent->inode == 0)
1534 return 0;
1535 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1536 return 0;
1537 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1538 (dirent->name[1] == '.')) {
1539 rds->parent = dirent->inode;
1540 return 0;
1541 }
1542 rds->empty = 0;
1543 return 0;
1544}
1545
1546void do_rmdir(int argc, char *argv[])
1547{
1548 int retval;
1549 ext2_ino_t inode_num;
1550 struct ext2_inode inode;
1551 struct rd_struct rds;
1552
1553 if (common_args_process(argc, argv, 2, 2, "rmdir",
1554 "<filename>", CHECK_FS_RW))
1555 return;
1556
1557 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1558 if (retval) {
1559 com_err(argv[0], retval, "while trying to resolve filename");
1560 return;
1561 }
1562
1563 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1564 return;
1565
1566 if (!LINUX_S_ISDIR(inode.i_mode)) {
1567 com_err(argv[0], 0, "file is not a directory");
1568 return;
1569 }
1570
1571 rds.parent = 0;
1572 rds.empty = 1;
1573
1574 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1575 0, rmdir_proc, &rds);
1576 if (retval) {
1577 com_err(argv[0], retval, "while iterating over directory");
1578 return;
1579 }
1580 if (rds.empty == 0) {
1581 com_err(argv[0], 0, "directory not empty");
1582 return;
1583 }
1584
1585 inode.i_links_count = 0;
1586 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1587 return;
1588
1589 unlink_file_by_name(argv[1]);
1590 kill_file_by_inode(inode_num);
1591
1592 if (rds.parent) {
1593 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1594 return;
1595 if (inode.i_links_count > 1)
1596 inode.i_links_count--;
1597 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1598 return;
1599 }
1600}
1601
Theodore Ts'o54434922003-12-07 01:28:50 -05001602void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1603 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001604{
1605 FILE *out = stdout;
1606
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001607 if (current_fs)
1608 fprintf(out, "Open mode: read-%s\n",
1609 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001610 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001611 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001612}
1613
1614void do_expand_dir(int argc, char *argv[])
1615{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001616 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001617 int retval;
1618
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001619 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001620 return;
1621
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001622 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001623 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001624 com_err("ext2fs_expand_dir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001625 return;
1626}
1627
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001628void do_features(int argc, char *argv[])
1629{
1630 int i;
1631
1632 if (check_fs_open(argv[0]))
1633 return;
1634
1635 if ((argc != 1) && check_fs_read_write(argv[0]))
1636 return;
1637 for (i=1; i < argc; i++) {
1638 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001639 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001640 com_err(argv[0], 0, "Unknown feature: %s\n",
1641 argv[i]);
1642 else
1643 ext2fs_mark_super_dirty(current_fs);
1644 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001645 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001646}
1647
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001648void do_bmap(int argc, char *argv[])
1649{
1650 ext2_ino_t ino;
1651 blk_t blk, pblk;
1652 int err;
1653 errcode_t errcode;
1654
1655 if (common_args_process(argc, argv, 3, 3, argv[0],
1656 "<file> logical_blk", 0))
1657 return;
1658
1659 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001660 if (!ino)
1661 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001662 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1663
1664 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1665 if (errcode) {
1666 com_err("argv[0]", errcode,
1667 "while mapping logical block %d\n", blk);
1668 return;
1669 }
1670 printf("%d\n", pblk);
1671}
1672
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001673void do_imap(int argc, char *argv[])
1674{
1675 ext2_ino_t ino;
1676 unsigned long group, block, block_nr, offset;
1677
1678 if (common_args_process(argc, argv, 2, 2, argv[0],
1679 "<file>", 0))
1680 return;
1681 ino = string_to_inode(argv[1]);
1682 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001683 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001684
1685 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1686 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1687 EXT2_INODE_SIZE(current_fs->super);
1688 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1689 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001690 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001691 group);
1692 return;
1693 }
1694 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1695 block;
1696 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1697
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001698 printf("Inode %d is part of block group %lu\n"
1699 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001700 block_nr, offset);
1701
1702}
1703
Theodore Ts'o4efae602005-09-24 21:56:38 -04001704void do_set_current_time(int argc, char *argv[])
1705{
Theodore Ts'o4efae602005-09-24 21:56:38 -04001706 time_t now;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001707
Theodore Ts'o4efae602005-09-24 21:56:38 -04001708 if (common_args_process(argc, argv, 2, 2, argv[0],
1709 "<time>", 0))
1710 return;
1711
1712 now = string_to_time(argv[1]);
1713 if (now == ((time_t) -1)) {
1714 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1715 argv[1]);
1716 return;
1717
1718 } else {
1719 printf("Setting current time to %s\n", time_to_string(now));
1720 current_fs->now = now;
1721 }
1722}
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001723
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001724static int source_file(const char *cmd_file, int sci_idx)
1725{
1726 FILE *f;
1727 char buf[256];
1728 char *cp;
1729 int exit_status = 0;
1730 int retval;
1731
1732 if (strcmp(cmd_file, "-") == 0)
1733 f = stdin;
1734 else {
1735 f = fopen(cmd_file, "r");
1736 if (!f) {
1737 perror(cmd_file);
1738 exit(1);
1739 }
1740 }
1741 setbuf(stdout, NULL);
1742 setbuf(stderr, NULL);
1743 while (!feof(f)) {
1744 if (fgets(buf, sizeof(buf), f) == NULL)
1745 break;
1746 cp = strchr(buf, '\n');
1747 if (cp)
1748 *cp = 0;
1749 cp = strchr(buf, '\r');
1750 if (cp)
1751 *cp = 0;
1752 printf("debugfs: %s\n", buf);
1753 retval = ss_execute_line(sci_idx, buf);
1754 if (retval) {
1755 ss_perror(sci_idx, retval, buf);
1756 exit_status++;
1757 }
1758 }
1759 return exit_status;
1760}
1761
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001762int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001763{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001764 int retval;
1765 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001766 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 +00001767 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001768 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001769 char *request = 0;
1770 int exit_status = 0;
1771 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001772 blk_t superblock = 0;
1773 blk_t blocksize = 0;
1774 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001775 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001776
1777 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001778 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1779 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001780
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001781 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001782 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001783 case 'R':
1784 request = optarg;
1785 break;
1786 case 'f':
1787 cmd_file = optarg;
1788 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001789 case 'd':
1790 data_filename = optarg;
1791 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001792 case 'i':
1793 open_flags |= EXT2_FLAG_IMAGE_FILE;
1794 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001795 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001796 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001797 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001798 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001799 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001800 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001801 break;
1802 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001803 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001804 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001805 break;
1806 case 'c':
1807 catastrophic = 1;
1808 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001809 case 'V':
1810 /* Print version number and exit */
1811 fprintf(stderr, "\tUsing %s\n",
1812 error_message(EXT2_ET_BASE));
1813 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001814 default:
1815 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001816 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001817 }
1818 }
1819 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001820 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001821 superblock, blocksize, catastrophic,
1822 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001823
1824 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1825 &debug_cmds, &retval);
1826 if (retval) {
1827 ss_perror(sci_idx, retval, "creating invocation");
1828 exit(1);
1829 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001830 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001831
1832 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1833 if (retval) {
1834 ss_perror(sci_idx, retval, "adding standard requests");
1835 exit (1);
1836 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001837 if (request) {
1838 retval = 0;
1839 retval = ss_execute_line(sci_idx, request);
1840 if (retval) {
1841 ss_perror(sci_idx, retval, request);
1842 exit_status++;
1843 }
1844 } else if (cmd_file) {
1845 exit_status = source_file(cmd_file, sci_idx);
1846 } else {
1847 ss_listen(sci_idx);
1848 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001849
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001850 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001851 close_filesystem();
1852
Theodore Ts'oe5973042000-01-18 17:58:34 +00001853 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001854}