blob: dab49d4809fa894e2150e1bd0bcb6a234e6dcd7e [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'o818180c1998-06-27 05:11:14 +000036#include "../version.h"
37
Theodore Ts'o3839e651997-04-26 13:21:57 +000038extern ss_request_table debug_cmds;
39
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000040ext2_filsys current_fs = NULL;
41ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000042
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000043static void open_filesystem(char *device, int open_flags, blk_t superblock,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040044 blk_t blocksize, int catastrophic,
45 char *data_filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +000046{
47 int retval;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040048 io_channel data_io = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000049
50 if (superblock != 0 && blocksize == 0) {
51 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
52 current_fs = NULL;
53 return;
54 }
55
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040056 if (data_filename) {
57 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
58 com_err(device, 0,
59 "The -d option is only valid when reading an e2image file");
60 current_fs = NULL;
61 return;
62 }
63 retval = unix_io_manager->open(data_filename, 0, &data_io);
64 if (retval) {
65 com_err(data_filename, 0, "while opening data source");
66 current_fs = NULL;
67 return;
68 }
69 }
70
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000071 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
72 com_err(device, 0,
73 "opening read-only because of catastrophic mode");
74 open_flags &= ~EXT2_FLAG_RW;
75 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000076
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000077 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000078 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000079 if (retval) {
80 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000081 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000082 return;
83 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000084
85 if (catastrophic)
86 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
87 else {
88 retval = ext2fs_read_inode_bitmap(current_fs);
89 if (retval) {
90 com_err(device, retval, "while reading inode bitmap");
91 goto errout;
92 }
93 retval = ext2fs_read_block_bitmap(current_fs);
94 if (retval) {
95 com_err(device, retval, "while reading block bitmap");
96 goto errout;
97 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 }
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040099
100 if (data_io) {
101 retval = ext2fs_set_data_io(current_fs, data_io);
102 if (retval) {
103 com_err(device, retval,
104 "while setting data source");
105 goto errout;
106 }
107 }
108
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 root = cwd = EXT2_ROOT_INO;
110 return;
111
112errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000113 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 if (retval)
115 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000116 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000117}
118
119void do_open_filesys(int argc, char **argv)
120{
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000121 const char *usage = "Usage: open [-s superblock] [-b blocksize] [-c] [-w] <device>";
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500122 int c, err;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000123 int catastrophic = 0;
124 blk_t superblock = 0;
125 blk_t blocksize = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400126 int open_flags = 0;
127 char *data_filename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000128
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400129 reset_getopt();
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400130 while ((c = getopt (argc, argv, "iwfcb:s:d:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000131 switch (c) {
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000132 case 'i':
133 open_flags |= EXT2_FLAG_IMAGE_FILE;
134 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000135 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000136 open_flags |= EXT2_FLAG_RW;
137 break;
138 case 'f':
139 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000141 case 'c':
142 catastrophic = 1;
143 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400144 case 'd':
145 data_filename = optarg;
146 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000147 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500148 blocksize = parse_ulong(optarg, argv[0],
149 "block size", &err);
150 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000151 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000152 break;
153 case 's':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500154 superblock = parse_ulong(optarg, argv[0],
155 "superblock number", &err);
156 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000157 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000158 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 default:
160 com_err(argv[0], 0, usage);
161 return;
162 }
163 }
164 if (optind != argc-1) {
165 com_err(argv[0], 0, usage);
166 return;
167 }
168 if (check_fs_not_open(argv[0]))
169 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000170 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400171 superblock, blocksize, catastrophic,
172 data_filename);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000173}
174
175void do_lcd(int argc, char **argv)
176{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500177 if (common_args_process(argc, argv, 2, 2, "lcd",
178 "<native dir>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000179 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000180
181 if (chdir(argv[1]) == -1) {
182 com_err(argv[0], errno,
183 "while trying to change native directory to %s",
184 argv[1]);
185 return;
186 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187}
188
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000189static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000190{
191 int retval;
192
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000193 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
194 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000195 if (retval)
196 com_err("ext2fs_write_inode_bitmap", retval, "");
197 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000198 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
199 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000200 if (retval)
201 com_err("ext2fs_write_block_bitmap", retval, "");
202 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000203 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 if (retval)
205 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000206 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 return;
208}
209
210void do_close_filesys(int argc, char **argv)
211{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500212 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000213 return;
214 close_filesystem();
215}
216
217void do_init_filesys(int argc, char **argv)
218{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000219 struct ext2_super_block param;
220 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500221 int err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000222
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500223 if (common_args_process(argc, argv, 3, 3, "initialize",
224 "<device> <blocksize>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000225 return;
226
227 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400228 param.s_blocks_count = parse_ulong(argv[2], argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500229 "blocks count", &err);
230 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000231 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000232 retval = ext2fs_initialize(argv[1], 0, &param,
233 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000234 if (retval) {
235 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000236 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 return;
238 }
239 root = cwd = EXT2_ROOT_INO;
240 return;
241}
242
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000243static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000244{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000245 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000246 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000247
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000248 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000249 for (i=0; i <3; i++,mask++) {
250 for (j=0,m=1; j < 32; j++, m<<=1) {
251 if (*mask & m) {
252 fprintf(f, " %s", e2p_feature2string(i, m));
253 printed++;
254 }
255 }
256 }
257 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000258 fputs("(none)", f);
259 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000260}
261
Theodore Ts'o3839e651997-04-26 13:21:57 +0000262void do_show_super_stats(int argc, char *argv[])
263{
Theodore Ts'o54434922003-12-07 01:28:50 -0500264 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000265 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000266 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000267 int c, header_only = 0;
Theodore Ts'o34be9602002-07-15 16:56:41 -0400268 int numdirs = 0;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000269 const char *usage = "Usage: show_super [-h]";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400271 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000272 while ((c = getopt (argc, argv, "h")) != EOF) {
273 switch (c) {
274 case 'h':
275 header_only++;
276 break;
277 default:
278 com_err(argv[0], 0, usage);
279 return;
280 }
281 }
282 if (optind != argc) {
283 com_err(argv[0], 0, usage);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 return;
285 }
286 if (check_fs_open(argv[0]))
287 return;
288 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000289
290 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400291 for (i=0; i < current_fs->group_desc_count; i++)
292 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
293 fprintf(out, "Directories: %d\n", numdirs);
294
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000295 if (header_only) {
296 close_pager(out);
297 return;
298 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000299
300 gdp = &current_fs->group_desc[0];
301 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000302 fprintf(out, " Group %2d: block bitmap at %d, "
303 "inode bitmap at %d, "
304 "inode table at %d\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000305 " %d free %s, "
306 "%d free %s, "
307 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000308 i, gdp->bg_block_bitmap,
309 gdp->bg_inode_bitmap, gdp->bg_inode_table,
310 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000311 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000312 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000313 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000314 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000315 gdp->bg_used_dirs_count != 1 ? "directories"
316 : "directory");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000317 close_pager(out);
318}
319
Theodore Ts'o54434922003-12-07 01:28:50 -0500320void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
321 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000322{
323 if (check_fs_open(argv[0]))
324 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000325 if (check_fs_read_write(argv[0]))
326 return;
327
328 if (argv[1] && !strcmp(argv[1], "-clean"))
329 current_fs->super->s_state |= EXT2_VALID_FS;
330 else
331 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000332 ext2fs_mark_super_dirty(current_fs);
333}
334
Theodore Ts'o3839e651997-04-26 13:21:57 +0000335struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700336 FILE *f;
337 e2_blkcnt_t total;
338 blk_t first_block, last_block;
339 e2_blkcnt_t first_bcnt, last_bcnt;
340 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000341};
342
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000343static void finish_range(struct list_blocks_struct *lb)
344{
345 if (lb->first_block == 0)
346 return;
347 if (lb->first)
348 lb->first = 0;
349 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000350 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000351 if (lb->first_block == lb->last_block)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100352 fprintf(lb->f, "(%lld):%d", lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000353 else
Theodore Ts'oe8981882001-11-30 11:51:30 +0100354 fprintf(lb->f, "(%lld-%lld):%d-%d", lb->first_bcnt,
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000355 lb->last_bcnt, lb->first_block, lb->last_block);
356 lb->first_block = 0;
357}
358
Theodore Ts'o54434922003-12-07 01:28:50 -0500359static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
360 blk_t *blocknr, e2_blkcnt_t blockcnt,
361 blk_t ref_block EXT2FS_ATTR((unused)),
362 int ref_offset EXT2FS_ATTR((unused)),
363 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364{
365 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
366
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000367 lb->total++;
368 if (blockcnt >= 0) {
369 /*
370 * See if we can add on to the existing range (if it exists)
371 */
372 if (lb->first_block &&
373 (lb->last_block+1 == *blocknr) &&
374 (lb->last_bcnt+1 == blockcnt)) {
375 lb->last_block = *blocknr;
376 lb->last_bcnt = blockcnt;
377 return 0;
378 }
379 /*
380 * Start a new range.
381 */
382 finish_range(lb);
383 lb->first_block = lb->last_block = *blocknr;
384 lb->first_bcnt = lb->last_bcnt = blockcnt;
385 return 0;
386 }
387 /*
388 * Not a normal block. Always force a new range.
389 */
390 finish_range(lb);
391 if (lb->first)
392 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000393 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000394 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000395 if (blockcnt == -1)
396 fprintf(lb->f, "(IND):%d", *blocknr);
397 else if (blockcnt == -2)
398 fprintf(lb->f, "(DIND):%d", *blocknr);
399 else if (blockcnt == -3)
400 fprintf(lb->f, "(TIND):%d", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000401 return 0;
402}
403
404
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000405static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000406{
407 struct list_blocks_struct lb;
408
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000409 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000410 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000411 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000412 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000413 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700414 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000415 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000416 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000417 if (lb.total)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100418 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 fprintf(f,"\n");
420}
421
422
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000423void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000424 ext2_ino_t inode_num, struct ext2_inode *inode,
425 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000426{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000427 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000428 char frag, fsize;
429 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000431 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
432 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
433 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
434 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
435 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
436 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
437 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000439 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
440 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
441 prefix,
442 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
443 fprintf(out, "%sUser: %5d Group: %5d Size: ",
444 prefix, inode->i_uid, inode->i_gid);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700445 if (LINUX_S_ISREG(inode->i_mode)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000446 __u64 i_size = (inode->i_size |
447 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700448
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000449 fprintf(out, "%lld\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700450 } else
451 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000452 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000453 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000454 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
455 prefix,
456 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
457 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000458 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000459 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
460 prefix,
461 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
462 fprintf(out, "%sLinks: %d Blockcount: %d\n",
463 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000464 switch (os) {
465 case EXT2_OS_LINUX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000466 frag = inode->osd2.linux2.l_i_frag;
467 fsize = inode->osd2.linux2.l_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000468 break;
469 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000470 frag = inode->osd2.hurd2.h_i_frag;
471 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000472 break;
473 case EXT2_OS_MASIX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000474 frag = inode->osd2.masix2.m_i_frag;
475 fsize = inode->osd2.masix2.m_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000476 break;
477 default:
478 frag = fsize = 0;
479 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000480 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
481 prefix, inode->i_faddr, frag, fsize);
482 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
483 time_to_string(inode->i_ctime));
484 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
485 time_to_string(inode->i_atime));
486 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
487 time_to_string(inode->i_mtime));
488 if (inode->i_dtime)
489 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
490 time_to_string(inode->i_dtime));
Theodore Ts'o795afc42004-02-21 20:54:31 -0500491 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000492 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
493 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500494 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
495 int major, minor;
496 const char *devnote;
497
498 if (inode->i_block[0]) {
499 major = (inode->i_block[0] >> 8) & 255;
500 minor = inode->i_block[0] & 255;
501 devnote = "";
502 } else {
503 major = (inode->i_block[1] & 0xfff00) >> 8;
504 minor = ((inode->i_block[1] & 0xff) |
505 ((inode->i_block[1] >> 12) & 0xfff00));
506 devnote = "(New-style) ";
507 }
508 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
509 devnote, major, minor, major, minor);
510 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000511 else if (do_dump_blocks)
512 dump_blocks(out, prefix, inode_num);
513}
514
515static void dump_inode(ext2_ino_t inode_num, struct ext2_inode inode)
516{
517 FILE *out;
518
519 out = open_pager();
520 internal_dump_inode(out, "", inode_num, &inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000521 close_pager(out);
522}
523
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524void do_stat(int argc, char *argv[])
525{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000526 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 struct ext2_inode inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000528
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500529 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 return;
531
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500532 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
533 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000534
535 dump_inode(inode,inode_buf);
536 return;
537}
538
539void do_chroot(int argc, char *argv[])
540{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000541 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000542 int retval;
543
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500544 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000545 return;
546
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000547 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 if (retval) {
549 com_err(argv[1], retval, "");
550 return;
551 }
552 root = inode;
553}
554
555void do_clri(int argc, char *argv[])
556{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000557 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000558 struct ext2_inode inode_buf;
559
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500560 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000561 return;
562
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500563 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000564 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000565 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500566 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000567 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000568}
569
570void do_freei(int argc, char *argv[])
571{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000572 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000573
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500574 if (common_inode_args_process(argc, argv, &inode,
575 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000576 return;
577
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000578 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000579 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000580 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
581 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582}
583
584void do_seti(int argc, char *argv[])
585{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000586 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500588 if (common_inode_args_process(argc, argv, &inode,
589 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590 return;
591
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000592 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000593 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000594 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
595 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000596}
597
598void do_testi(int argc, char *argv[])
599{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000600 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000601
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500602 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603 return;
604
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000605 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000606 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000607 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000608 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609}
610
Theodore Ts'o3839e651997-04-26 13:21:57 +0000611void do_freeb(int argc, char *argv[])
612{
613 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500614 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500616 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000617 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000618 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500620 while (count-- > 0) {
621 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
622 com_err(argv[0], 0, "Warning: block %d already clear",
623 block);
624 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
625 block++;
626 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000627 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000628}
629
630void do_setb(int argc, char *argv[])
631{
632 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500633 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000634
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500635 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000636 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000637 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500639 while (count-- > 0) {
640 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
641 com_err(argv[0], 0, "Warning: block %d already set",
642 block);
643 ext2fs_mark_block_bitmap(current_fs->block_map,block);
644 block++;
645 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000646 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647}
648
649void do_testb(int argc, char *argv[])
650{
651 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500652 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500654 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000655 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500656 while (count-- > 0) {
657 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
658 printf("Block %d marked in use\n", block);
659 else
660 printf("Block %d not in use\n", block);
661 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000662 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663}
664
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000665static void modify_u8(char *com, const char *prompt,
666 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667{
668 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000669 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000670 char *tmp;
671
672 sprintf(buf, format, *val);
673 printf("%30s [%s] ", prompt, buf);
674 fgets(buf, sizeof(buf), stdin);
675 if (buf[strlen (buf) - 1] == '\n')
676 buf[strlen (buf) - 1] = '\0';
677 if (!buf[0])
678 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000679 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000680 if (*tmp)
681 com_err(com, 0, "Bad value - %s", buf);
682 else
683 *val = v;
684}
685
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000686static void modify_u16(char *com, const char *prompt,
687 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688{
689 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000690 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000691 char *tmp;
692
693 sprintf(buf, format, *val);
694 printf("%30s [%s] ", prompt, buf);
695 fgets(buf, sizeof(buf), stdin);
696 if (buf[strlen (buf) - 1] == '\n')
697 buf[strlen (buf) - 1] = '\0';
698 if (!buf[0])
699 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000700 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000701 if (*tmp)
702 com_err(com, 0, "Bad value - %s", buf);
703 else
704 *val = v;
705}
706
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000707static void modify_u32(char *com, const char *prompt,
708 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000709{
710 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000711 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 char *tmp;
713
714 sprintf(buf, format, *val);
715 printf("%30s [%s] ", prompt, buf);
716 fgets(buf, sizeof(buf), stdin);
717 if (buf[strlen (buf) - 1] == '\n')
718 buf[strlen (buf) - 1] = '\0';
719 if (!buf[0])
720 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000721 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722 if (*tmp)
723 com_err(com, 0, "Bad value - %s", buf);
724 else
725 *val = v;
726}
727
728
729void do_modify_inode(int argc, char *argv[])
730{
731 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000732 ext2_ino_t inode_num;
733 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000734 unsigned char *frag, *fsize;
735 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500736 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000737 const char *hex_format = "0x%x";
738 const char *octal_format = "0%o";
739 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000740
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500741 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000742 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500744 os = current_fs->super->s_creator_os;
745
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500746 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000748
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000749 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
750 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
751 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
752 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
753 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
754 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
755 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
756 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
757 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
758 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
759 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000760 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000761#if 0
762 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
763#endif
764 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000765 if (LINUX_S_ISDIR(inode.i_mode))
766 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
767 else
768 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000769
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000770 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000771 modify_u32(argv[0], "Translator Block",
772 decimal_format, &inode.osd1.hurd1.h_i_translator);
773
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000774 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000775 switch (os) {
776 case EXT2_OS_LINUX:
777 frag = &inode.osd2.linux2.l_i_frag;
778 fsize = &inode.osd2.linux2.l_i_fsize;
779 break;
780 case EXT2_OS_HURD:
781 frag = &inode.osd2.hurd2.h_i_frag;
782 fsize = &inode.osd2.hurd2.h_i_fsize;
783 break;
784 case EXT2_OS_MASIX:
785 frag = &inode.osd2.masix2.m_i_frag;
786 fsize = &inode.osd2.masix2.m_i_fsize;
787 break;
788 default:
789 frag = fsize = 0;
790 }
791 if (frag)
792 modify_u8(argv[0], "Fragment number", decimal_format, frag);
793 if (fsize)
794 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
795
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
797 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000798 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000800 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000801 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000802 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000803 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000804 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000805 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500806 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000807 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000808}
809
Theodore Ts'o3839e651997-04-26 13:21:57 +0000810void do_change_working_dir(int argc, char *argv[])
811{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000812 ext2_ino_t inode;
813 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000814
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500815 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000816 return;
817
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000818 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000819 if (retval) {
820 com_err(argv[1], retval, "");
821 return;
822 }
823 cwd = inode;
824 return;
825}
826
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827void do_print_working_directory(int argc, char *argv[])
828{
829 int retval;
830 char *pathname = NULL;
831
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500832 if (common_args_process(argc, argv, 1, 1,
833 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000834 return;
835
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000836 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000837 if (retval) {
838 com_err(argv[0], retval,
839 "while trying to get pathname of cwd");
840 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000841 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000842 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000843 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000844 if (retval) {
845 com_err(argv[0], retval,
846 "while trying to get pathname of root");
847 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000848 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000849 free(pathname);
850 return;
851}
852
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500853/*
854 * Given a mode, return the ext2 file type
855 */
856static int ext2_file_type(unsigned int mode)
857{
858 if (LINUX_S_ISREG(mode))
859 return EXT2_FT_REG_FILE;
860
861 if (LINUX_S_ISDIR(mode))
862 return EXT2_FT_DIR;
863
864 if (LINUX_S_ISCHR(mode))
865 return EXT2_FT_CHRDEV;
866
867 if (LINUX_S_ISBLK(mode))
868 return EXT2_FT_BLKDEV;
869
870 if (LINUX_S_ISLNK(mode))
871 return EXT2_FT_SYMLINK;
872
873 if (LINUX_S_ISFIFO(mode))
874 return EXT2_FT_FIFO;
875
876 if (LINUX_S_ISSOCK(mode))
877 return EXT2_FT_SOCK;
878
879 return 0;
880}
881
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000882static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000883{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500884 ext2_ino_t ino;
885 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000886 int retval;
887 ext2_ino_t dir;
888 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000889
890 /*
891 * Get the source inode
892 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500893 ino = string_to_inode(sourcename);
894 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000895 return;
896 basename = strrchr(sourcename, '/');
897 if (basename)
898 basename++;
899 else
900 basename = sourcename;
901 /*
902 * Figure out the destination. First see if it exists and is
903 * a directory.
904 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000905 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000906 dest = basename;
907 else {
908 /*
909 * OK, it doesn't exist. See if it is
910 * '<dir>/basename' or 'basename'
911 */
912 cp = strrchr(destname, '/');
913 if (cp) {
914 *cp = 0;
915 dir = string_to_inode(destname);
916 if (!dir)
917 return;
918 dest = cp+1;
919 } else {
920 dir = cwd;
921 dest = destname;
922 }
923 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500924
925 if (debugfs_read_inode(ino, &inode, sourcename))
926 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000927
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500928 retval = ext2fs_link(current_fs, dir, dest, ino,
929 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 if (retval)
931 com_err("make_link", retval, "");
932 return;
933}
934
935
936void do_link(int argc, char *argv[])
937{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500938 if (common_args_process(argc, argv, 3, 3, "link",
939 "<source file> <dest_name>", CHECK_FS_RW))
940 return;
941
942 make_link(argv[1], argv[2]);
943}
944
945static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -0500946 int blockcnt EXT2FS_ATTR((unused)),
947 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500948{
949 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500950
951 block = *blocknr;
952 ext2fs_block_alloc_stats(fs, block, +1);
953 return 0;
954}
955
956void do_undel(int argc, char *argv[])
957{
958 ext2_ino_t ino;
959 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500960
961 if (common_args_process(argc, argv, 3, 3, "undelete",
962 "<inode_num> <dest_name>",
963 CHECK_FS_RW | CHECK_FS_BITMAPS))
964 return;
965
966 ino = string_to_inode(argv[1]);
967 if (!ino)
968 return;
969
970 if (debugfs_read_inode(ino, &inode, argv[1]))
971 return;
972
973 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
974 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000975 return;
976 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500977
978 /*
979 * XXX this function doesn't handle changing the links count on the
980 * parent directory when undeleting a directory.
981 */
982 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
983 inode.i_dtime = 0;
984
985 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000986 return;
987
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500988 ext2fs_block_iterate(current_fs, ino, 0, NULL,
989 mark_blocks_proc, NULL);
990
Theodore Ts'od7f64ae2002-07-09 01:27:05 -0400991 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500992
Theodore Ts'o3839e651997-04-26 13:21:57 +0000993 make_link(argv[1], argv[2]);
994}
995
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000996static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000997{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000998 int retval;
999 ext2_ino_t dir;
1000 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001001
1002 basename = strrchr(filename, '/');
1003 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001004 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001005 dir = string_to_inode(filename);
1006 if (!dir)
1007 return;
1008 } else {
1009 dir = cwd;
1010 basename = filename;
1011 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001012 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001013 if (retval)
1014 com_err("unlink_file_by_name", retval, "");
1015 return;
1016}
1017
1018void do_unlink(int argc, char *argv[])
1019{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001020 if (common_args_process(argc, argv, 2, 2, "link",
1021 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001022 return;
1023
1024 unlink_file_by_name(argv[1]);
1025}
1026
1027void do_find_free_block(int argc, char *argv[])
1028{
1029 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001030 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001031 errcode_t retval;
1032 char *tmp;
1033
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001034 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1035 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001036 return;
1037 }
1038 if (check_fs_open(argv[0]))
1039 return;
1040
1041 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001042 count = strtol(argv[1],&tmp,0);
1043 if (*tmp) {
1044 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1045 return;
1046 }
1047 } else
1048 count = 1;
1049
1050 if (argc > 2) {
1051 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001052 if (*tmp) {
1053 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1054 return;
1055 }
1056 }
1057 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001058 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001059
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001060 printf("Free blocks found: ");
1061 free_blk = goal - 1;
1062 while (count-- > 0) {
1063 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1064 &free_blk);
1065 if (retval) {
1066 com_err("ext2fs_new_block", retval, "");
1067 return;
1068 } else
1069 printf("%d ", free_blk);
1070 }
1071 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001072}
1073
1074void do_find_free_inode(int argc, char *argv[])
1075{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001076 ext2_ino_t free_inode, dir;
1077 int mode;
1078 int retval;
1079 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001080
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001081 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1082 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001083 return;
1084 }
1085 if (check_fs_open(argv[0]))
1086 return;
1087
1088 if (argc > 1) {
1089 dir = strtol(argv[1], &tmp, 0);
1090 if (*tmp) {
1091 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1092 return;
1093 }
1094 }
1095 else
1096 dir = root;
1097 if (argc > 2) {
1098 mode = strtol(argv[2], &tmp, 0);
1099 if (*tmp) {
1100 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1101 return;
1102 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001103 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001104 mode = 010755;
1105
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001106 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001107 if (retval)
1108 com_err("ext2fs_new_inode", retval, "");
1109 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001110 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001111}
1112
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001113static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001114{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001115 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001116 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001117 int got;
1118 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001119 char buf[8192];
1120 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001121
Theodore Ts'o5a513841997-10-25 22:41:14 +00001122 retval = ext2fs_file_open(current_fs, newfile,
1123 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001124 if (retval)
1125 return retval;
1126
Theodore Ts'o5a513841997-10-25 22:41:14 +00001127 while (1) {
1128 got = read(fd, buf, sizeof(buf));
1129 if (got == 0)
1130 break;
1131 if (got < 0) {
1132 retval = errno;
1133 goto fail;
1134 }
1135 ptr = buf;
1136 while (got > 0) {
1137 retval = ext2fs_file_write(e2_file, ptr,
1138 got, &written);
1139 if (retval)
1140 goto fail;
1141
1142 got -= written;
1143 ptr += written;
1144 }
1145 }
1146 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001147 return retval;
1148
1149fail:
1150 (void) ext2fs_file_close(e2_file);
1151 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001152}
1153
Theodore Ts'o5a513841997-10-25 22:41:14 +00001154
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001155void do_write(int argc, char *argv[])
1156{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001157 int fd;
1158 struct stat statbuf;
1159 ext2_ino_t newfile;
1160 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001161 struct ext2_inode inode;
1162
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001163 if (common_args_process(argc, argv, 3, 3, "write",
1164 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001165 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001166
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001167 fd = open(argv[1], O_RDONLY);
1168 if (fd < 0) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001169 com_err(argv[1], errno, "");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001170 return;
1171 }
1172 if (fstat(fd, &statbuf) < 0) {
1173 com_err(argv[1], errno, "");
1174 close(fd);
1175 return;
1176 }
1177
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001178 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1179 if (retval == 0) {
1180 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1181 close(fd);
1182 return;
1183 }
1184
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001185 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001186 if (retval) {
1187 com_err(argv[0], retval, "");
1188 close(fd);
1189 return;
1190 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001191 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001192 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1193 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001194 if (retval == EXT2_ET_DIR_NO_SPACE) {
1195 retval = ext2fs_expand_dir(current_fs, cwd);
1196 if (retval) {
1197 com_err(argv[0], retval, "while expanding directory");
1198 return;
1199 }
1200 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1201 EXT2_FT_REG_FILE);
1202 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001203 if (retval) {
1204 com_err(argv[2], retval, "");
1205 close(fd);
1206 return;
1207 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001208 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001209 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001210 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001211 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001212 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001213 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1214 inode.i_links_count = 1;
1215 inode.i_size = statbuf.st_size;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001216 if (debugfs_write_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001217 close(fd);
1218 return;
1219 }
1220 if (LINUX_S_ISREG(inode.i_mode)) {
1221 retval = copy_file(fd, newfile);
1222 if (retval)
1223 com_err("copy_file", retval, "");
1224 }
1225 close(fd);
1226}
1227
1228void do_mknod(int argc, char *argv[])
1229{
Theodore Ts'o54434922003-12-07 01:28:50 -05001230 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001231 ext2_ino_t newfile;
1232 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001233 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001234 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001235
1236 if (check_fs_open(argv[0]))
1237 return;
1238 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001239 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001240 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1241 return;
1242 }
1243 mode = minor = major = 0;
1244 switch (argv[2][0]) {
1245 case 'p':
1246 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001247 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001248 nr = 3;
1249 break;
1250 case 'c':
1251 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001252 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001253 nr = 5;
1254 break;
1255 case 'b':
1256 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001257 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001258 nr = 5;
1259 break;
1260 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001261 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001262 nr = 0;
1263 }
1264 if (nr == 5) {
1265 major = strtoul(argv[3], argv+3, 0);
1266 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001267 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001268 nr = 0;
1269 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001270 if (argc != nr)
1271 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001272 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001273 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001274 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001275 if (retval) {
1276 com_err(argv[0], retval, "");
1277 return;
1278 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001279 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001280 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001281 if (retval == EXT2_ET_DIR_NO_SPACE) {
1282 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001283 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001284 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001285 return;
1286 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001287 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1288 filetype);
1289 }
1290 if (retval) {
1291 com_err(argv[1], retval, "");
1292 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001293 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001294 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001295 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001296 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1297 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001298 memset(&inode, 0, sizeof(inode));
1299 inode.i_mode = mode;
1300 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001301 if ((major < 256) && (minor < 256)) {
1302 inode.i_block[0] = major*256+minor;
1303 inode.i_block[1] = 0;
1304 } else {
1305 inode.i_block[0] = 0;
1306 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1307 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001308 inode.i_links_count = 1;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001309 if (debugfs_write_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001310 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001311}
1312
Theodore Ts'o3839e651997-04-26 13:21:57 +00001313void do_mkdir(int argc, char *argv[])
1314{
1315 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001316 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001317 char *name;
1318 errcode_t retval;
1319
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001320 if (common_args_process(argc, argv, 2, 2, "mkdir",
1321 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001322 return;
1323
Theodore Ts'o3839e651997-04-26 13:21:57 +00001324 cp = strrchr(argv[1], '/');
1325 if (cp) {
1326 *cp = 0;
1327 parent = string_to_inode(argv[1]);
1328 if (!parent) {
1329 com_err(argv[1], ENOENT, "");
1330 return;
1331 }
1332 name = cp+1;
1333 } else {
1334 parent = cwd;
1335 name = argv[1];
1336 }
1337
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001338try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001339 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001340 if (retval == EXT2_ET_DIR_NO_SPACE) {
1341 retval = ext2fs_expand_dir(current_fs, parent);
1342 if (retval) {
1343 com_err("argv[0]", retval, "while expanding directory");
1344 return;
1345 }
1346 goto try_again;
1347 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001348 if (retval) {
1349 com_err("ext2fs_mkdir", retval, "");
1350 return;
1351 }
1352
1353}
1354
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001355static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001356 int blockcnt EXT2FS_ATTR((unused)),
1357 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001358{
Theodore Ts'o34436892001-12-22 13:06:02 -05001359 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001360
1361 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001362 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001363 return 0;
1364}
1365
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001366static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001367{
1368 struct ext2_inode inode_buf;
1369
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001370 if (debugfs_read_inode(inode, &inode_buf, 0))
1371 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001372 inode_buf.i_dtime = time(NULL);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001373 if (debugfs_write_inode(inode, &inode_buf, 0))
1374 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001375 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1376 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001377
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001378 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1379 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001380 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001381 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1382 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001383}
1384
1385
1386void do_kill_file(int argc, char *argv[])
1387{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001388 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001389
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001390 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001391 return;
1392
Theodore Ts'o3839e651997-04-26 13:21:57 +00001393 kill_file_by_inode(inode_num);
1394}
1395
1396void do_rm(int argc, char *argv[])
1397{
1398 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001399 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001400 struct ext2_inode inode;
1401
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001402 if (common_args_process(argc, argv, 2, 2, "rm",
1403 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001404 return;
1405
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001406 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001407 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001408 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001409 return;
1410 }
1411
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001412 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001413 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001414
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001415 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001416 com_err(argv[0], 0, "file is a directory");
1417 return;
1418 }
1419
1420 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001421 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001422 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001423
1424 unlink_file_by_name(argv[1]);
1425 if (inode.i_links_count == 0)
1426 kill_file_by_inode(inode_num);
1427}
1428
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001429struct rd_struct {
1430 ext2_ino_t parent;
1431 int empty;
1432};
1433
Theodore Ts'o54434922003-12-07 01:28:50 -05001434static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1435 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001436 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001437 int offset EXT2FS_ATTR((unused)),
1438 int blocksize EXT2FS_ATTR((unused)),
1439 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001440 void *private)
1441{
1442 struct rd_struct *rds = (struct rd_struct *) private;
1443
1444 if (dirent->inode == 0)
1445 return 0;
1446 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1447 return 0;
1448 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1449 (dirent->name[1] == '.')) {
1450 rds->parent = dirent->inode;
1451 return 0;
1452 }
1453 rds->empty = 0;
1454 return 0;
1455}
1456
1457void do_rmdir(int argc, char *argv[])
1458{
1459 int retval;
1460 ext2_ino_t inode_num;
1461 struct ext2_inode inode;
1462 struct rd_struct rds;
1463
1464 if (common_args_process(argc, argv, 2, 2, "rmdir",
1465 "<filename>", CHECK_FS_RW))
1466 return;
1467
1468 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1469 if (retval) {
1470 com_err(argv[0], retval, "while trying to resolve filename");
1471 return;
1472 }
1473
1474 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1475 return;
1476
1477 if (!LINUX_S_ISDIR(inode.i_mode)) {
1478 com_err(argv[0], 0, "file is not a directory");
1479 return;
1480 }
1481
1482 rds.parent = 0;
1483 rds.empty = 1;
1484
1485 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1486 0, rmdir_proc, &rds);
1487 if (retval) {
1488 com_err(argv[0], retval, "while iterating over directory");
1489 return;
1490 }
1491 if (rds.empty == 0) {
1492 com_err(argv[0], 0, "directory not empty");
1493 return;
1494 }
1495
1496 inode.i_links_count = 0;
1497 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1498 return;
1499
1500 unlink_file_by_name(argv[1]);
1501 kill_file_by_inode(inode_num);
1502
1503 if (rds.parent) {
1504 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1505 return;
1506 if (inode.i_links_count > 1)
1507 inode.i_links_count--;
1508 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1509 return;
1510 }
1511}
1512
Theodore Ts'o54434922003-12-07 01:28:50 -05001513void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1514 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001515{
1516 FILE *out = stdout;
1517
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001518 if (current_fs)
1519 fprintf(out, "Open mode: read-%s\n",
1520 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001521 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001522 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001523}
1524
1525void do_expand_dir(int argc, char *argv[])
1526{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001527 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001528 int retval;
1529
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001530 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001531 return;
1532
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001533 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001534 if (retval)
1535 com_err("ext2fs_expand_dir", retval, "");
1536 return;
1537}
1538
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001539void do_features(int argc, char *argv[])
1540{
1541 int i;
1542
1543 if (check_fs_open(argv[0]))
1544 return;
1545
1546 if ((argc != 1) && check_fs_read_write(argv[0]))
1547 return;
1548 for (i=1; i < argc; i++) {
1549 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001550 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001551 com_err(argv[0], 0, "Unknown feature: %s\n",
1552 argv[i]);
1553 else
1554 ext2fs_mark_super_dirty(current_fs);
1555 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001556 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001557}
1558
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001559void do_bmap(int argc, char *argv[])
1560{
1561 ext2_ino_t ino;
1562 blk_t blk, pblk;
1563 int err;
1564 errcode_t errcode;
1565
1566 if (common_args_process(argc, argv, 3, 3, argv[0],
1567 "<file> logical_blk", 0))
1568 return;
1569
1570 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001571 if (!ino)
1572 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001573 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1574
1575 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1576 if (errcode) {
1577 com_err("argv[0]", errcode,
1578 "while mapping logical block %d\n", blk);
1579 return;
1580 }
1581 printf("%d\n", pblk);
1582}
1583
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001584void do_imap(int argc, char *argv[])
1585{
1586 ext2_ino_t ino;
1587 unsigned long group, block, block_nr, offset;
1588
1589 if (common_args_process(argc, argv, 2, 2, argv[0],
1590 "<file>", 0))
1591 return;
1592 ino = string_to_inode(argv[1]);
1593 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001594 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001595
1596 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1597 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1598 EXT2_INODE_SIZE(current_fs->super);
1599 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1600 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001601 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001602 group);
1603 return;
1604 }
1605 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1606 block;
1607 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1608
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001609 printf("Inode %d is part of block group %lu\n"
1610 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001611 block_nr, offset);
1612
1613}
1614
1615
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001616
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001617static int source_file(const char *cmd_file, int sci_idx)
1618{
1619 FILE *f;
1620 char buf[256];
1621 char *cp;
1622 int exit_status = 0;
1623 int retval;
1624
1625 if (strcmp(cmd_file, "-") == 0)
1626 f = stdin;
1627 else {
1628 f = fopen(cmd_file, "r");
1629 if (!f) {
1630 perror(cmd_file);
1631 exit(1);
1632 }
1633 }
1634 setbuf(stdout, NULL);
1635 setbuf(stderr, NULL);
1636 while (!feof(f)) {
1637 if (fgets(buf, sizeof(buf), f) == NULL)
1638 break;
1639 cp = strchr(buf, '\n');
1640 if (cp)
1641 *cp = 0;
1642 cp = strchr(buf, '\r');
1643 if (cp)
1644 *cp = 0;
1645 printf("debugfs: %s\n", buf);
1646 retval = ss_execute_line(sci_idx, buf);
1647 if (retval) {
1648 ss_perror(sci_idx, retval, buf);
1649 exit_status++;
1650 }
1651 }
1652 return exit_status;
1653}
1654
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001655int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001656{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001657 int retval;
1658 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001659 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 +00001660 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001661 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001662 char *request = 0;
1663 int exit_status = 0;
1664 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001665 blk_t superblock = 0;
1666 blk_t blocksize = 0;
1667 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001668 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001669
1670 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001671 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1672 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001673
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001674 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001675 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001676 case 'R':
1677 request = optarg;
1678 break;
1679 case 'f':
1680 cmd_file = optarg;
1681 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001682 case 'd':
1683 data_filename = optarg;
1684 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001685 case 'i':
1686 open_flags |= EXT2_FLAG_IMAGE_FILE;
1687 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001688 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001689 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001690 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001691 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001692 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001693 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001694 break;
1695 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001696 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001697 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001698 break;
1699 case 'c':
1700 catastrophic = 1;
1701 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001702 case 'V':
1703 /* Print version number and exit */
1704 fprintf(stderr, "\tUsing %s\n",
1705 error_message(EXT2_ET_BASE));
1706 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001707 default:
1708 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001709 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001710 }
1711 }
1712 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001713 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001714 superblock, blocksize, catastrophic,
1715 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001716
1717 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1718 &debug_cmds, &retval);
1719 if (retval) {
1720 ss_perror(sci_idx, retval, "creating invocation");
1721 exit(1);
1722 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001723 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001724
1725 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1726 if (retval) {
1727 ss_perror(sci_idx, retval, "adding standard requests");
1728 exit (1);
1729 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001730 if (request) {
1731 retval = 0;
1732 retval = ss_execute_line(sci_idx, request);
1733 if (retval) {
1734 ss_perror(sci_idx, retval, request);
1735 exit_status++;
1736 }
1737 } else if (cmd_file) {
1738 exit_status = source_file(cmd_file, sci_idx);
1739 } else {
1740 ss_listen(sci_idx);
1741 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001742
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001743 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001744 close_filesystem();
1745
Theodore Ts'oe5973042000-01-18 17:58:34 +00001746 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001747}