blob: 25d52d756a0619d6b329ad36ec5233ec76a9cf53 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
4 *
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
7 *
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
9 */
10
11#include <stdio.h>
12#include <unistd.h>
13#include <stdlib.h>
14#include <ctype.h>
15#include <string.h>
16#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#else
20extern int optind;
21extern char *optarg;
22#endif
Theodore Ts'o50e1e101997-04-26 13:58:21 +000023#ifdef HAVE_ERRNO_H
24#include <errno.h>
25#endif
26#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000027#include <sys/types.h>
28#include <sys/stat.h>
29
30#include "et/com_err.h"
31#include "ss/ss.h"
32#include "debugfs.h"
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000033#include "uuid/uuid.h"
Theodore Ts'of68aa411999-10-26 14:20:22 +000034#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000035
Theodore Ts'oea822ee2005-03-20 18:03:58 -050036#include <ext2fs/ext2_ext_attr.h>
37
Theodore Ts'o818180c1998-06-27 05:11:14 +000038#include "../version.h"
39
Theodore Ts'o3839e651997-04-26 13:21:57 +000040extern ss_request_table debug_cmds;
41
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000042ext2_filsys current_fs = NULL;
43ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000044
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000045static void open_filesystem(char *device, int open_flags, blk_t superblock,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040046 blk_t blocksize, int catastrophic,
47 char *data_filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +000048{
49 int retval;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040050 io_channel data_io = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000051
52 if (superblock != 0 && blocksize == 0) {
53 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
54 current_fs = NULL;
55 return;
56 }
57
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040058 if (data_filename) {
59 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
60 com_err(device, 0,
61 "The -d option is only valid when reading an e2image file");
62 current_fs = NULL;
63 return;
64 }
65 retval = unix_io_manager->open(data_filename, 0, &data_io);
66 if (retval) {
67 com_err(data_filename, 0, "while opening data source");
68 current_fs = NULL;
69 return;
70 }
71 }
72
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000073 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
74 com_err(device, 0,
75 "opening read-only because of catastrophic mode");
76 open_flags &= ~EXT2_FLAG_RW;
77 }
Theodore Ts'o3839e651997-04-26 13:21:57 +000078
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000079 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000080 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 if (retval) {
82 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000083 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 return;
85 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000086
87 if (catastrophic)
88 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
89 else {
90 retval = ext2fs_read_inode_bitmap(current_fs);
91 if (retval) {
92 com_err(device, retval, "while reading inode bitmap");
93 goto errout;
94 }
95 retval = ext2fs_read_block_bitmap(current_fs);
96 if (retval) {
97 com_err(device, retval, "while reading block bitmap");
98 goto errout;
99 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000100 }
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400101
102 if (data_io) {
103 retval = ext2fs_set_data_io(current_fs, data_io);
104 if (retval) {
105 com_err(device, retval,
106 "while setting data source");
107 goto errout;
108 }
109 }
110
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 root = cwd = EXT2_ROOT_INO;
112 return;
113
114errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000115 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 if (retval)
117 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000118 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000119}
120
121void do_open_filesys(int argc, char **argv)
122{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500123 int c, err;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000124 int catastrophic = 0;
125 blk_t superblock = 0;
126 blk_t blocksize = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400127 int open_flags = 0;
Theodore Ts'ob0d17e02004-11-29 17:35:58 -0500128 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400130 reset_getopt();
Theodore Ts'o98eb44b2006-03-18 19:58:13 -0500131 while ((c = getopt (argc, argv, "iwfecb:s:d:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000132 switch (c) {
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000133 case 'i':
134 open_flags |= EXT2_FLAG_IMAGE_FILE;
135 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000136 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000137 open_flags |= EXT2_FLAG_RW;
138 break;
139 case 'f':
140 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000141 break;
Theodore Ts'o98eb44b2006-03-18 19:58:13 -0500142 case 'e':
143 open_flags |= EXT2_FLAG_EXCLUSIVE;
144 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000145 case 'c':
146 catastrophic = 1;
147 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400148 case 'd':
149 data_filename = optarg;
150 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000151 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500152 blocksize = parse_ulong(optarg, argv[0],
153 "block size", &err);
154 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000155 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000156 break;
157 case 's':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500158 superblock = parse_ulong(optarg, argv[0],
159 "superblock number", &err);
160 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000161 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000162 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500164 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000165 }
166 }
167 if (optind != argc-1) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500168 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000169 }
170 if (check_fs_not_open(argv[0]))
171 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000172 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400173 superblock, blocksize, catastrophic,
174 data_filename);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500175 return;
176
177print_usage:
178 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
179 "[-c] [-w] <device>\n", argv[0]);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000180}
181
182void do_lcd(int argc, char **argv)
183{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500184 if (common_args_process(argc, argv, 2, 2, "lcd",
185 "<native dir>", 0))
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000186 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000187
188 if (chdir(argv[1]) == -1) {
189 com_err(argv[0], errno,
190 "while trying to change native directory to %s",
191 argv[1]);
192 return;
193 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000194}
195
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000196static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000197{
198 int retval;
199
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000200 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
201 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000202 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500203 com_err("ext2fs_write_inode_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000205 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
206 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000207 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500208 com_err("ext2fs_write_block_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000209 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000210 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000211 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500212 com_err("ext2fs_close", retval, 0);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000213 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000214 return;
215}
216
217void do_close_filesys(int argc, char **argv)
218{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500219 if (common_args_process(argc, argv, 1, 1, "close_filesys", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 return;
221 close_filesystem();
222}
223
224void do_init_filesys(int argc, char **argv)
225{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 struct ext2_super_block param;
227 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500228 int err;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000229
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500230 if (common_args_process(argc, argv, 3, 3, "initialize",
231 "<device> <blocksize>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000232 return;
233
234 memset(&param, 0, sizeof(struct ext2_super_block));
Theodore Ts'ob38cd282002-05-11 22:13:20 -0400235 param.s_blocks_count = parse_ulong(argv[2], argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500236 "blocks count", &err);
237 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000239 retval = ext2fs_initialize(argv[1], 0, &param,
240 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000241 if (retval) {
242 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000243 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000244 return;
245 }
246 root = cwd = EXT2_ROOT_INO;
247 return;
248}
249
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000250static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000251{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000252 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000253 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000254
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000255 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000256 for (i=0; i <3; i++,mask++) {
257 for (j=0,m=1; j < 32; j++, m<<=1) {
258 if (*mask & m) {
259 fprintf(f, " %s", e2p_feature2string(i, m));
260 printed++;
261 }
262 }
263 }
264 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000265 fputs("(none)", f);
266 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000267}
268
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400269static void print_bg_opts(struct ext2_group_desc *gdp, int mask,
270 const char *str, int *first, FILE *f)
271{
272 if (gdp->bg_flags & mask) {
273 if (*first) {
274 fputs(" [", f);
275 *first = 0;
276 } else
277 fputs(", ", f);
278 fputs(str, f);
279 }
280}
281
Theodore Ts'o3839e651997-04-26 13:21:57 +0000282void do_show_super_stats(int argc, char *argv[])
283{
Theodore Ts'o54434922003-12-07 01:28:50 -0500284 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000285 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000286 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000287 int c, header_only = 0;
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400288 int numdirs = 0, first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000289
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400290 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000291 while ((c = getopt (argc, argv, "h")) != EOF) {
292 switch (c) {
293 case 'h':
294 header_only++;
295 break;
296 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500297 goto print_usage;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000298 }
299 }
300 if (optind != argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500301 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000302 }
303 if (check_fs_open(argv[0]))
304 return;
305 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000306
307 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400308 for (i=0; i < current_fs->group_desc_count; i++)
309 numdirs += current_fs->group_desc[i].bg_used_dirs_count;
310 fprintf(out, "Directories: %d\n", numdirs);
311
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000312 if (header_only) {
313 close_pager(out);
314 return;
315 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000316
317 gdp = &current_fs->group_desc[0];
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400318 for (i = 0; i < current_fs->group_desc_count; i++, gdp++) {
Takashi Sato8deb80a2006-03-18 21:43:46 -0500319 fprintf(out, " Group %2d: block bitmap at %u, "
320 "inode bitmap at %u, "
321 "inode table at %u\n"
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000322 " %d free %s, "
323 "%d free %s, "
324 "%d used %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000325 i, gdp->bg_block_bitmap,
326 gdp->bg_inode_bitmap, gdp->bg_inode_table,
327 gdp->bg_free_blocks_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000328 gdp->bg_free_blocks_count != 1 ? "blocks" : "block",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000329 gdp->bg_free_inodes_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000330 gdp->bg_free_inodes_count != 1 ? "inodes" : "inode",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000331 gdp->bg_used_dirs_count,
Theodore Ts'ob74d1d82001-01-12 17:23:52 +0000332 gdp->bg_used_dirs_count != 1 ? "directories"
333 : "directory");
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400334 first = 1;
335 print_bg_opts(gdp, EXT2_BG_INODE_UNINIT, "Inode not init",
336 &first, out);
337 print_bg_opts(gdp, EXT2_BG_BLOCK_UNINIT, "Block not init",
338 &first, out);
339 if (!first)
340 fputs("]\n", out);
341 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000342 close_pager(out);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500343 return;
344print_usage:
345 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346}
347
Theodore Ts'o54434922003-12-07 01:28:50 -0500348void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
349 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000350{
351 if (check_fs_open(argv[0]))
352 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000353 if (check_fs_read_write(argv[0]))
354 return;
355
356 if (argv[1] && !strcmp(argv[1], "-clean"))
357 current_fs->super->s_state |= EXT2_VALID_FS;
358 else
359 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000360 ext2fs_mark_super_dirty(current_fs);
361}
362
Theodore Ts'o3839e651997-04-26 13:21:57 +0000363struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700364 FILE *f;
365 e2_blkcnt_t total;
366 blk_t first_block, last_block;
367 e2_blkcnt_t first_bcnt, last_bcnt;
368 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000369};
370
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000371static void finish_range(struct list_blocks_struct *lb)
372{
373 if (lb->first_block == 0)
374 return;
375 if (lb->first)
376 lb->first = 0;
377 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000378 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000379 if (lb->first_block == lb->last_block)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500380 fprintf(lb->f, "(%lld):%u", lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000381 else
Takashi Sato8deb80a2006-03-18 21:43:46 -0500382 fprintf(lb->f, "(%lld-%lld):%u-%u", lb->first_bcnt,
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000383 lb->last_bcnt, lb->first_block, lb->last_block);
384 lb->first_block = 0;
385}
386
Theodore Ts'o54434922003-12-07 01:28:50 -0500387static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
388 blk_t *blocknr, e2_blkcnt_t blockcnt,
389 blk_t ref_block EXT2FS_ATTR((unused)),
390 int ref_offset EXT2FS_ATTR((unused)),
391 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000392{
393 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
394
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000395 lb->total++;
396 if (blockcnt >= 0) {
397 /*
398 * See if we can add on to the existing range (if it exists)
399 */
400 if (lb->first_block &&
401 (lb->last_block+1 == *blocknr) &&
402 (lb->last_bcnt+1 == blockcnt)) {
403 lb->last_block = *blocknr;
404 lb->last_bcnt = blockcnt;
405 return 0;
406 }
407 /*
408 * Start a new range.
409 */
410 finish_range(lb);
411 lb->first_block = lb->last_block = *blocknr;
412 lb->first_bcnt = lb->last_bcnt = blockcnt;
413 return 0;
414 }
415 /*
416 * Not a normal block. Always force a new range.
417 */
418 finish_range(lb);
419 if (lb->first)
420 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000421 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000422 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000423 if (blockcnt == -1)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500424 fprintf(lb->f, "(IND):%u", *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000425 else if (blockcnt == -2)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500426 fprintf(lb->f, "(DIND):%u", *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000427 else if (blockcnt == -3)
Takashi Sato8deb80a2006-03-18 21:43:46 -0500428 fprintf(lb->f, "(TIND):%u", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429 return 0;
430}
431
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500432static void dump_xattr_string(FILE *out, const char *str, int len)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500433{
434 int printable = 1;
435 int i;
436
437 /* check is string printable? */
438 for (i = 0; i < len; i++)
439 if (!isprint(str[i])) {
440 printable = 0;
441 break;
442 }
443
444 for (i = 0; i < len; i++)
445 if (printable)
446 fprintf(out, "%c", str[i]);
447 else
448 fprintf(out, "%02x ", str[i]);
449}
450
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500451static void internal_dump_inode_extra(FILE *out, const char *prefix,
452 ext2_ino_t inode_num,
453 struct ext2_inode_large *inode)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500454{
455 struct ext2_ext_attr_entry *entry;
456 __u32 *magic;
457 char *start, *end;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500458 unsigned int storage_size;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500459
Takashi Sato8deb80a2006-03-18 21:43:46 -0500460 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500461 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
462 EXT2_GOOD_OLD_INODE_SIZE) {
463 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
464 inode->i_extra_isize);
465 return;
466 }
467 storage_size = EXT2_INODE_SIZE(current_fs->super) -
468 EXT2_GOOD_OLD_INODE_SIZE -
469 inode->i_extra_isize;
470 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
471 inode->i_extra_isize);
472 if (*magic == EXT2_EXT_ATTR_MAGIC) {
473 fprintf(out, "Extended attributes stored in inode body: \n");
474 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
475 start = (char *) magic + sizeof(__u32);
476 entry = (struct ext2_ext_attr_entry *) start;
477 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
478 struct ext2_ext_attr_entry *next =
479 EXT2_EXT_ATTR_NEXT(entry);
480 if (entry->e_value_size > storage_size ||
481 (char *) next >= end) {
482 fprintf(out, "invalid EA entry in inode\n");
483 return;
484 }
485 fprintf(out, " ");
486 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
487 entry->e_name_len);
488 fprintf(out, " = \"");
489 dump_xattr_string(out, start + entry->e_value_offs,
490 entry->e_value_size);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500491 fprintf(out, "\" (%u)\n", entry->e_value_size);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500492 entry = next;
493 }
494 }
495}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000497static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000498{
499 struct list_blocks_struct lb;
500
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000501 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000502 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000503 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000504 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000505 lb.first = 1;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700506 ext2fs_block_iterate2(current_fs, inode, 0, NULL,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000507 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000508 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000509 if (lb.total)
Theodore Ts'oe8981882001-11-30 11:51:30 +0100510 fprintf(f, "\n%sTOTAL: %lld\n", prefix, lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000511 fprintf(f,"\n");
512}
513
514
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000515void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000516 ext2_ino_t inode_num, struct ext2_inode *inode,
517 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000519 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000520 char frag, fsize;
521 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000523 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
524 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
525 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
526 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
527 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
528 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
529 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000531 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
532 fprintf(out, "%sMode: %04o Flags: 0x%x Generation: %u\n",
533 prefix,
534 inode->i_mode & 0777, inode->i_flags, inode->i_generation);
535 fprintf(out, "%sUser: %5d Group: %5d Size: ",
536 prefix, inode->i_uid, inode->i_gid);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700537 if (LINUX_S_ISREG(inode->i_mode)) {
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000538 __u64 i_size = (inode->i_size |
539 ((unsigned long long)inode->i_size_high << 32));
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700540
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000541 fprintf(out, "%lld\n", i_size);
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700542 } else
543 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000544 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000545 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000546 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
547 prefix,
548 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
549 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000550 else
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000551 fprintf(out, "%sFile ACL: %d Directory ACL: %d\n",
552 prefix,
553 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500554 fprintf(out, "%sLinks: %d Blockcount: %u\n",
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000555 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000556 switch (os) {
557 case EXT2_OS_LINUX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000558 frag = inode->osd2.linux2.l_i_frag;
559 fsize = inode->osd2.linux2.l_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000560 break;
561 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000562 frag = inode->osd2.hurd2.h_i_frag;
563 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000564 break;
565 case EXT2_OS_MASIX:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000566 frag = inode->osd2.masix2.m_i_frag;
567 fsize = inode->osd2.masix2.m_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000568 break;
569 default:
570 frag = fsize = 0;
571 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000572 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
573 prefix, inode->i_faddr, frag, fsize);
574 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
575 time_to_string(inode->i_ctime));
576 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
577 time_to_string(inode->i_atime));
578 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
579 time_to_string(inode->i_mtime));
580 if (inode->i_dtime)
581 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
582 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500583 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
584 internal_dump_inode_extra(out, prefix, inode_num,
585 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500586 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000587 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
588 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500589 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
590 int major, minor;
591 const char *devnote;
592
593 if (inode->i_block[0]) {
594 major = (inode->i_block[0] >> 8) & 255;
595 minor = inode->i_block[0] & 255;
596 devnote = "";
597 } else {
598 major = (inode->i_block[1] & 0xfff00) >> 8;
599 minor = ((inode->i_block[1] & 0xff) |
600 ((inode->i_block[1] >> 12) & 0xfff00));
601 devnote = "(New-style) ";
602 }
603 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
604 devnote, major, minor, major, minor);
605 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000606 else if (do_dump_blocks)
607 dump_blocks(out, prefix, inode_num);
608}
609
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500610static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000611{
612 FILE *out;
613
614 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500615 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 close_pager(out);
617}
618
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619void do_stat(int argc, char *argv[])
620{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000621 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500622 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000623
Theodore Ts'o64777392005-05-05 17:21:46 -0400624 if (check_fs_open(argv[0]))
Theodore Ts'o8363e352005-05-06 09:04:37 -0400625 return;
Theodore Ts'o64777392005-05-05 17:21:46 -0400626
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500627 inode_buf = (struct ext2_inode *)
628 malloc(EXT2_INODE_SIZE(current_fs->super));
629 if (!inode_buf) {
630 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000631 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500632 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000633
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500634 if (common_inode_args_process(argc, argv, &inode, 0)) {
635 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500636 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500637 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000638
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500639 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
640 EXT2_INODE_SIZE(current_fs->super))) {
641 free(inode_buf);
642 return;
643 }
644
645 dump_inode(inode, inode_buf);
646 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000647 return;
648}
649
650void do_chroot(int argc, char *argv[])
651{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000652 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653 int retval;
654
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500655 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 return;
657
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000658 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500660 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000661 return;
662 }
663 root = inode;
664}
665
666void do_clri(int argc, char *argv[])
667{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000668 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000669 struct ext2_inode inode_buf;
670
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500671 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000672 return;
673
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500674 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000675 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000676 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500677 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000678 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000679}
680
681void do_freei(int argc, char *argv[])
682{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000683 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000684
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500685 if (common_inode_args_process(argc, argv, &inode,
686 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000687 return;
688
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000689 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000690 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000691 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
692 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000693}
694
695void do_seti(int argc, char *argv[])
696{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000697 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500699 if (common_inode_args_process(argc, argv, &inode,
700 CHECK_FS_RW | CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000701 return;
702
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000703 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000704 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000705 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
706 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000707}
708
709void do_testi(int argc, char *argv[])
710{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000711 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500713 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000714 return;
715
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000716 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000717 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000718 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000719 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000720}
721
Theodore Ts'o3839e651997-04-26 13:21:57 +0000722void do_freeb(int argc, char *argv[])
723{
724 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500725 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500727 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000729 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500731 while (count-- > 0) {
732 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500733 com_err(argv[0], 0, "Warning: block %u already clear",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500734 block);
735 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
736 block++;
737 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000738 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000739}
740
741void do_setb(int argc, char *argv[])
742{
743 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500744 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500746 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000747 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000748 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000749 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500750 while (count-- > 0) {
751 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500752 com_err(argv[0], 0, "Warning: block %u already set",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500753 block);
754 ext2fs_mark_block_bitmap(current_fs->block_map,block);
755 block++;
756 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000757 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758}
759
760void do_testb(int argc, char *argv[])
761{
762 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500763 int count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000764
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500765 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500767 while (count-- > 0) {
768 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Takashi Sato8deb80a2006-03-18 21:43:46 -0500769 printf("Block %u marked in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500770 else
Takashi Sato8deb80a2006-03-18 21:43:46 -0500771 printf("Block %u not in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500772 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000773 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000774}
775
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000776static void modify_u8(char *com, const char *prompt,
777 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000778{
779 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000780 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781 char *tmp;
782
783 sprintf(buf, format, *val);
784 printf("%30s [%s] ", prompt, buf);
785 fgets(buf, sizeof(buf), stdin);
786 if (buf[strlen (buf) - 1] == '\n')
787 buf[strlen (buf) - 1] = '\0';
788 if (!buf[0])
789 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000790 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000791 if (*tmp)
792 com_err(com, 0, "Bad value - %s", buf);
793 else
794 *val = v;
795}
796
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000797static void modify_u16(char *com, const char *prompt,
798 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799{
800 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000801 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000802 char *tmp;
803
804 sprintf(buf, format, *val);
805 printf("%30s [%s] ", prompt, buf);
806 fgets(buf, sizeof(buf), stdin);
807 if (buf[strlen (buf) - 1] == '\n')
808 buf[strlen (buf) - 1] = '\0';
809 if (!buf[0])
810 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000811 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000812 if (*tmp)
813 com_err(com, 0, "Bad value - %s", buf);
814 else
815 *val = v;
816}
817
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000818static void modify_u32(char *com, const char *prompt,
819 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820{
821 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000822 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000823 char *tmp;
824
825 sprintf(buf, format, *val);
826 printf("%30s [%s] ", prompt, buf);
827 fgets(buf, sizeof(buf), stdin);
828 if (buf[strlen (buf) - 1] == '\n')
829 buf[strlen (buf) - 1] = '\0';
830 if (!buf[0])
831 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000832 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000833 if (*tmp)
834 com_err(com, 0, "Bad value - %s", buf);
835 else
836 *val = v;
837}
838
839
840void do_modify_inode(int argc, char *argv[])
841{
842 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000843 ext2_ino_t inode_num;
844 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000845 unsigned char *frag, *fsize;
846 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500847 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000848 const char *hex_format = "0x%x";
849 const char *octal_format = "0%o";
850 const char *decimal_format = "%d";
Takashi Sato8deb80a2006-03-18 21:43:46 -0500851 const char *unsignedlong_format = "%lu";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000852
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500853 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000854 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855
Theodore Ts'o7380ac92002-03-05 01:57:53 -0500856 os = current_fs->super->s_creator_os;
857
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500858 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000859 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000860
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000861 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
862 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
863 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500864 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000865 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
866 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
867 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
868 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
869 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500870 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000871 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +0000872 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000873#if 0
874 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
875#endif
876 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000877 if (LINUX_S_ISDIR(inode.i_mode))
878 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
879 else
880 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000881
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000882 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000883 modify_u32(argv[0], "Translator Block",
884 decimal_format, &inode.osd1.hurd1.h_i_translator);
885
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000886 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000887 switch (os) {
888 case EXT2_OS_LINUX:
889 frag = &inode.osd2.linux2.l_i_frag;
890 fsize = &inode.osd2.linux2.l_i_fsize;
891 break;
892 case EXT2_OS_HURD:
893 frag = &inode.osd2.hurd2.h_i_frag;
894 fsize = &inode.osd2.hurd2.h_i_fsize;
895 break;
896 case EXT2_OS_MASIX:
897 frag = &inode.osd2.masix2.m_i_frag;
898 fsize = &inode.osd2.masix2.m_i_fsize;
899 break;
900 default:
901 frag = fsize = 0;
902 }
903 if (frag)
904 modify_u8(argv[0], "Fragment number", decimal_format, frag);
905 if (fsize)
906 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
907
Theodore Ts'o3839e651997-04-26 13:21:57 +0000908 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
909 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000910 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000911 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000912 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000913 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000914 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000915 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000916 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000917 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500918 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000919 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000920}
921
Theodore Ts'o3839e651997-04-26 13:21:57 +0000922void do_change_working_dir(int argc, char *argv[])
923{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000924 ext2_ino_t inode;
925 int retval;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000926
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500927 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000928 return;
929
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000930 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000931 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500932 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000933 return;
934 }
935 cwd = inode;
936 return;
937}
938
Theodore Ts'o3839e651997-04-26 13:21:57 +0000939void do_print_working_directory(int argc, char *argv[])
940{
941 int retval;
942 char *pathname = NULL;
943
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500944 if (common_args_process(argc, argv, 1, 1,
945 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000946 return;
947
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000948 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000949 if (retval) {
950 com_err(argv[0], retval,
951 "while trying to get pathname of cwd");
952 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000953 printf("[pwd] INODE: %6u PATH: %s\n", cwd, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000954 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000955 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956 if (retval) {
957 com_err(argv[0], retval,
958 "while trying to get pathname of root");
959 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000960 printf("[root] INODE: %6u PATH: %s\n", root, pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000961 free(pathname);
962 return;
963}
964
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500965/*
966 * Given a mode, return the ext2 file type
967 */
968static int ext2_file_type(unsigned int mode)
969{
970 if (LINUX_S_ISREG(mode))
971 return EXT2_FT_REG_FILE;
972
973 if (LINUX_S_ISDIR(mode))
974 return EXT2_FT_DIR;
975
976 if (LINUX_S_ISCHR(mode))
977 return EXT2_FT_CHRDEV;
978
979 if (LINUX_S_ISBLK(mode))
980 return EXT2_FT_BLKDEV;
981
982 if (LINUX_S_ISLNK(mode))
983 return EXT2_FT_SYMLINK;
984
985 if (LINUX_S_ISFIFO(mode))
986 return EXT2_FT_FIFO;
987
988 if (LINUX_S_ISSOCK(mode))
989 return EXT2_FT_SOCK;
990
991 return 0;
992}
993
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000994static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000995{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -0500996 ext2_ino_t ino;
997 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000998 int retval;
999 ext2_ino_t dir;
1000 char *dest, *cp, *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001001
1002 /*
1003 * Get the source inode
1004 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001005 ino = string_to_inode(sourcename);
1006 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001007 return;
1008 basename = strrchr(sourcename, '/');
1009 if (basename)
1010 basename++;
1011 else
1012 basename = sourcename;
1013 /*
1014 * Figure out the destination. First see if it exists and is
1015 * a directory.
1016 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001017 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001018 dest = basename;
1019 else {
1020 /*
1021 * OK, it doesn't exist. See if it is
1022 * '<dir>/basename' or 'basename'
1023 */
1024 cp = strrchr(destname, '/');
1025 if (cp) {
1026 *cp = 0;
1027 dir = string_to_inode(destname);
1028 if (!dir)
1029 return;
1030 dest = cp+1;
1031 } else {
1032 dir = cwd;
1033 dest = destname;
1034 }
1035 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001036
1037 if (debugfs_read_inode(ino, &inode, sourcename))
1038 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001039
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001040 retval = ext2fs_link(current_fs, dir, dest, ino,
1041 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001042 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001043 com_err("make_link", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001044 return;
1045}
1046
1047
1048void do_link(int argc, char *argv[])
1049{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001050 if (common_args_process(argc, argv, 3, 3, "link",
1051 "<source file> <dest_name>", CHECK_FS_RW))
1052 return;
1053
1054 make_link(argv[1], argv[2]);
1055}
1056
1057static int mark_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001058 int blockcnt EXT2FS_ATTR((unused)),
1059 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001060{
1061 blk_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001062
1063 block = *blocknr;
1064 ext2fs_block_alloc_stats(fs, block, +1);
1065 return 0;
1066}
1067
1068void do_undel(int argc, char *argv[])
1069{
1070 ext2_ino_t ino;
1071 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001072
1073 if (common_args_process(argc, argv, 3, 3, "undelete",
1074 "<inode_num> <dest_name>",
1075 CHECK_FS_RW | CHECK_FS_BITMAPS))
1076 return;
1077
1078 ino = string_to_inode(argv[1]);
1079 if (!ino)
1080 return;
1081
1082 if (debugfs_read_inode(ino, &inode, argv[1]))
1083 return;
1084
1085 if (ext2fs_test_inode_bitmap(current_fs->inode_map, ino)) {
1086 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001087 return;
1088 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001089
1090 /*
1091 * XXX this function doesn't handle changing the links count on the
1092 * parent directory when undeleting a directory.
1093 */
1094 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1095 inode.i_dtime = 0;
1096
1097 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001098 return;
1099
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001100 ext2fs_block_iterate(current_fs, ino, 0, NULL,
1101 mark_blocks_proc, NULL);
1102
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001103 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001104
Theodore Ts'o3839e651997-04-26 13:21:57 +00001105 make_link(argv[1], argv[2]);
1106}
1107
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001108static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001109{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001110 int retval;
1111 ext2_ino_t dir;
1112 char *basename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001113
1114 basename = strrchr(filename, '/');
1115 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001116 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001117 dir = string_to_inode(filename);
1118 if (!dir)
1119 return;
1120 } else {
1121 dir = cwd;
1122 basename = filename;
1123 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001124 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001125 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001126 com_err("unlink_file_by_name", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001127 return;
1128}
1129
1130void do_unlink(int argc, char *argv[])
1131{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001132 if (common_args_process(argc, argv, 2, 2, "link",
1133 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001134 return;
1135
1136 unlink_file_by_name(argv[1]);
1137}
1138
1139void do_find_free_block(int argc, char *argv[])
1140{
1141 blk_t free_blk, goal;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001142 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001143 errcode_t retval;
1144 char *tmp;
1145
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001146 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1147 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001148 return;
1149 }
1150 if (check_fs_open(argv[0]))
1151 return;
1152
1153 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001154 count = strtol(argv[1],&tmp,0);
1155 if (*tmp) {
1156 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1157 return;
1158 }
1159 } else
1160 count = 1;
1161
1162 if (argc > 2) {
1163 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001164 if (*tmp) {
1165 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1166 return;
1167 }
1168 }
1169 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001170 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001171
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001172 printf("Free blocks found: ");
1173 free_blk = goal - 1;
1174 while (count-- > 0) {
1175 retval = ext2fs_new_block(current_fs, free_blk + 1, 0,
1176 &free_blk);
1177 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001178 com_err("ext2fs_new_block", retval, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001179 return;
1180 } else
Takashi Sato8deb80a2006-03-18 21:43:46 -05001181 printf("%u ", free_blk);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001182 }
1183 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001184}
1185
1186void do_find_free_inode(int argc, char *argv[])
1187{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001188 ext2_ino_t free_inode, dir;
1189 int mode;
1190 int retval;
1191 char *tmp;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001192
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001193 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1194 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001195 return;
1196 }
1197 if (check_fs_open(argv[0]))
1198 return;
1199
1200 if (argc > 1) {
1201 dir = strtol(argv[1], &tmp, 0);
1202 if (*tmp) {
1203 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1204 return;
1205 }
1206 }
1207 else
1208 dir = root;
1209 if (argc > 2) {
1210 mode = strtol(argv[2], &tmp, 0);
1211 if (*tmp) {
1212 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1213 return;
1214 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001215 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001216 mode = 010755;
1217
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001218 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001219 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001220 com_err("ext2fs_new_inode", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001221 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001222 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001223}
1224
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001225static errcode_t copy_file(int fd, ext2_ino_t newfile)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001226{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001227 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001228 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001229 int got;
1230 unsigned int written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001231 char buf[8192];
1232 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001233
Theodore Ts'o5a513841997-10-25 22:41:14 +00001234 retval = ext2fs_file_open(current_fs, newfile,
1235 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001236 if (retval)
1237 return retval;
1238
Theodore Ts'o5a513841997-10-25 22:41:14 +00001239 while (1) {
1240 got = read(fd, buf, sizeof(buf));
1241 if (got == 0)
1242 break;
1243 if (got < 0) {
1244 retval = errno;
1245 goto fail;
1246 }
1247 ptr = buf;
1248 while (got > 0) {
1249 retval = ext2fs_file_write(e2_file, ptr,
1250 got, &written);
1251 if (retval)
1252 goto fail;
1253
1254 got -= written;
1255 ptr += written;
1256 }
1257 }
1258 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001259 return retval;
1260
1261fail:
1262 (void) ext2fs_file_close(e2_file);
1263 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001264}
1265
Theodore Ts'o5a513841997-10-25 22:41:14 +00001266
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001267void do_write(int argc, char *argv[])
1268{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001269 int fd;
1270 struct stat statbuf;
1271 ext2_ino_t newfile;
1272 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001273 struct ext2_inode inode;
1274
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001275 if (common_args_process(argc, argv, 3, 3, "write",
1276 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001277 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001278
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001279 fd = open(argv[1], O_RDONLY);
1280 if (fd < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001281 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001282 return;
1283 }
1284 if (fstat(fd, &statbuf) < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001285 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001286 close(fd);
1287 return;
1288 }
1289
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001290 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1291 if (retval == 0) {
1292 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1293 close(fd);
1294 return;
1295 }
1296
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001297 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001298 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001299 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001300 close(fd);
1301 return;
1302 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001303 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001304 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1305 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001306 if (retval == EXT2_ET_DIR_NO_SPACE) {
1307 retval = ext2fs_expand_dir(current_fs, cwd);
1308 if (retval) {
1309 com_err(argv[0], retval, "while expanding directory");
1310 return;
1311 }
1312 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1313 EXT2_FT_REG_FILE);
1314 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001315 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001316 com_err(argv[2], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001317 close(fd);
1318 return;
1319 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001320 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001321 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001322 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001323 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001324 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001325 inode.i_atime = inode.i_ctime = inode.i_mtime =
1326 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001327 inode.i_links_count = 1;
1328 inode.i_size = statbuf.st_size;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001329 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001330 close(fd);
1331 return;
1332 }
1333 if (LINUX_S_ISREG(inode.i_mode)) {
1334 retval = copy_file(fd, newfile);
1335 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001336 com_err("copy_file", retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001337 }
1338 close(fd);
1339}
1340
1341void do_mknod(int argc, char *argv[])
1342{
Theodore Ts'o54434922003-12-07 01:28:50 -05001343 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001344 ext2_ino_t newfile;
1345 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001346 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001347 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001348
1349 if (check_fs_open(argv[0]))
1350 return;
1351 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001352 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001353 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1354 return;
1355 }
1356 mode = minor = major = 0;
1357 switch (argv[2][0]) {
1358 case 'p':
1359 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001360 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001361 nr = 3;
1362 break;
1363 case 'c':
1364 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001365 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001366 nr = 5;
1367 break;
1368 case 'b':
1369 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001370 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001371 nr = 5;
1372 break;
1373 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001374 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001375 nr = 0;
1376 }
1377 if (nr == 5) {
1378 major = strtoul(argv[3], argv+3, 0);
1379 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001380 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001381 nr = 0;
1382 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001383 if (argc != nr)
1384 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001385 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001386 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001387 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001388 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001389 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001390 return;
1391 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001392 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001393 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001394 if (retval == EXT2_ET_DIR_NO_SPACE) {
1395 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001396 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001397 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001398 return;
1399 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001400 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1401 filetype);
1402 }
1403 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001404 com_err(argv[1], retval, 0);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001405 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001406 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001407 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001408 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001409 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1410 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001411 memset(&inode, 0, sizeof(inode));
1412 inode.i_mode = mode;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001413 inode.i_atime = inode.i_ctime = inode.i_mtime =
1414 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001415 if ((major < 256) && (minor < 256)) {
1416 inode.i_block[0] = major*256+minor;
1417 inode.i_block[1] = 0;
1418 } else {
1419 inode.i_block[0] = 0;
1420 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1421 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001422 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001423 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001424 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001425}
1426
Theodore Ts'o3839e651997-04-26 13:21:57 +00001427void do_mkdir(int argc, char *argv[])
1428{
1429 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001430 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001431 char *name;
1432 errcode_t retval;
1433
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001434 if (common_args_process(argc, argv, 2, 2, "mkdir",
1435 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001436 return;
1437
Theodore Ts'o3839e651997-04-26 13:21:57 +00001438 cp = strrchr(argv[1], '/');
1439 if (cp) {
1440 *cp = 0;
1441 parent = string_to_inode(argv[1]);
1442 if (!parent) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001443 com_err(argv[1], ENOENT, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001444 return;
1445 }
1446 name = cp+1;
1447 } else {
1448 parent = cwd;
1449 name = argv[1];
1450 }
1451
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001452try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001453 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001454 if (retval == EXT2_ET_DIR_NO_SPACE) {
1455 retval = ext2fs_expand_dir(current_fs, parent);
1456 if (retval) {
1457 com_err("argv[0]", retval, "while expanding directory");
1458 return;
1459 }
1460 goto try_again;
1461 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001462 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001463 com_err("ext2fs_mkdir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001464 return;
1465 }
1466
1467}
1468
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001469static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
Theodore Ts'o54434922003-12-07 01:28:50 -05001470 int blockcnt EXT2FS_ATTR((unused)),
1471 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001472{
Theodore Ts'o34436892001-12-22 13:06:02 -05001473 blk_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001474
1475 block = *blocknr;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001476 ext2fs_block_alloc_stats(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001477 return 0;
1478}
1479
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001480static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001481{
1482 struct ext2_inode inode_buf;
1483
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001484 if (debugfs_read_inode(inode, &inode_buf, 0))
1485 return;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001486 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001487 if (debugfs_write_inode(inode, &inode_buf, 0))
1488 return;
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001489 if (!ext2fs_inode_has_valid_blocks(&inode_buf))
1490 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001491
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001492 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1493 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001494 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001495 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1496 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001497}
1498
1499
1500void do_kill_file(int argc, char *argv[])
1501{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001502 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001503
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001504 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001505 return;
1506
Theodore Ts'o3839e651997-04-26 13:21:57 +00001507 kill_file_by_inode(inode_num);
1508}
1509
1510void do_rm(int argc, char *argv[])
1511{
1512 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001513 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001514 struct ext2_inode inode;
1515
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001516 if (common_args_process(argc, argv, 2, 2, "rm",
1517 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001518 return;
1519
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001520 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001521 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001522 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001523 return;
1524 }
1525
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001526 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001527 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001528
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001529 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001530 com_err(argv[0], 0, "file is a directory");
1531 return;
1532 }
1533
1534 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001535 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001536 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001537
1538 unlink_file_by_name(argv[1]);
1539 if (inode.i_links_count == 0)
1540 kill_file_by_inode(inode_num);
1541}
1542
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001543struct rd_struct {
1544 ext2_ino_t parent;
1545 int empty;
1546};
1547
Theodore Ts'o54434922003-12-07 01:28:50 -05001548static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1549 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001550 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001551 int offset EXT2FS_ATTR((unused)),
1552 int blocksize EXT2FS_ATTR((unused)),
1553 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001554 void *private)
1555{
1556 struct rd_struct *rds = (struct rd_struct *) private;
1557
1558 if (dirent->inode == 0)
1559 return 0;
1560 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1561 return 0;
1562 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1563 (dirent->name[1] == '.')) {
1564 rds->parent = dirent->inode;
1565 return 0;
1566 }
1567 rds->empty = 0;
1568 return 0;
1569}
1570
1571void do_rmdir(int argc, char *argv[])
1572{
1573 int retval;
1574 ext2_ino_t inode_num;
1575 struct ext2_inode inode;
1576 struct rd_struct rds;
1577
1578 if (common_args_process(argc, argv, 2, 2, "rmdir",
1579 "<filename>", CHECK_FS_RW))
1580 return;
1581
1582 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1583 if (retval) {
1584 com_err(argv[0], retval, "while trying to resolve filename");
1585 return;
1586 }
1587
1588 if (debugfs_read_inode(inode_num, &inode, argv[0]))
1589 return;
1590
1591 if (!LINUX_S_ISDIR(inode.i_mode)) {
1592 com_err(argv[0], 0, "file is not a directory");
1593 return;
1594 }
1595
1596 rds.parent = 0;
1597 rds.empty = 1;
1598
1599 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
1600 0, rmdir_proc, &rds);
1601 if (retval) {
1602 com_err(argv[0], retval, "while iterating over directory");
1603 return;
1604 }
1605 if (rds.empty == 0) {
1606 com_err(argv[0], 0, "directory not empty");
1607 return;
1608 }
1609
1610 inode.i_links_count = 0;
1611 if (debugfs_write_inode(inode_num, &inode, argv[0]))
1612 return;
1613
1614 unlink_file_by_name(argv[1]);
1615 kill_file_by_inode(inode_num);
1616
1617 if (rds.parent) {
1618 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
1619 return;
1620 if (inode.i_links_count > 1)
1621 inode.i_links_count--;
1622 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
1623 return;
1624 }
1625}
1626
Theodore Ts'o54434922003-12-07 01:28:50 -05001627void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
1628 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001629{
1630 FILE *out = stdout;
1631
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001632 if (current_fs)
1633 fprintf(out, "Open mode: read-%s\n",
1634 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001635 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001636 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001637}
1638
1639void do_expand_dir(int argc, char *argv[])
1640{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001641 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001642 int retval;
1643
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001644 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001645 return;
1646
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001647 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001648 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001649 com_err("ext2fs_expand_dir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001650 return;
1651}
1652
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001653void do_features(int argc, char *argv[])
1654{
1655 int i;
1656
1657 if (check_fs_open(argv[0]))
1658 return;
1659
1660 if ((argc != 1) && check_fs_read_write(argv[0]))
1661 return;
1662 for (i=1; i < argc; i++) {
1663 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001664 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001665 com_err(argv[0], 0, "Unknown feature: %s\n",
1666 argv[i]);
1667 else
1668 ext2fs_mark_super_dirty(current_fs);
1669 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00001670 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001671}
1672
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001673void do_bmap(int argc, char *argv[])
1674{
1675 ext2_ino_t ino;
1676 blk_t blk, pblk;
1677 int err;
1678 errcode_t errcode;
1679
1680 if (common_args_process(argc, argv, 3, 3, argv[0],
1681 "<file> logical_blk", 0))
1682 return;
1683
1684 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001685 if (!ino)
1686 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001687 blk = parse_ulong(argv[2], argv[0], "logical_block", &err);
1688
1689 errcode = ext2fs_bmap(current_fs, ino, 0, 0, 0, blk, &pblk);
1690 if (errcode) {
1691 com_err("argv[0]", errcode,
Takashi Sato8deb80a2006-03-18 21:43:46 -05001692 "while mapping logical block %u\n", blk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001693 return;
1694 }
Takashi Sato8deb80a2006-03-18 21:43:46 -05001695 printf("%u\n", pblk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001696}
1697
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001698void do_imap(int argc, char *argv[])
1699{
1700 ext2_ino_t ino;
1701 unsigned long group, block, block_nr, offset;
1702
1703 if (common_args_process(argc, argv, 2, 2, argv[0],
1704 "<file>", 0))
1705 return;
1706 ino = string_to_inode(argv[1]);
1707 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04001708 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001709
1710 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
1711 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
1712 EXT2_INODE_SIZE(current_fs->super);
1713 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
1714 if (!current_fs->group_desc[(unsigned)group].bg_inode_table) {
Theodore Ts'o54434922003-12-07 01:28:50 -05001715 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001716 group);
1717 return;
1718 }
1719 block_nr = current_fs->group_desc[(unsigned)group].bg_inode_table +
1720 block;
1721 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
1722
Theodore Ts'o48e6e812003-07-06 00:36:48 -04001723 printf("Inode %d is part of block group %lu\n"
1724 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001725 block_nr, offset);
1726
1727}
1728
Theodore Ts'o4efae602005-09-24 21:56:38 -04001729void do_set_current_time(int argc, char *argv[])
1730{
Theodore Ts'o4efae602005-09-24 21:56:38 -04001731 time_t now;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04001732
Theodore Ts'o4efae602005-09-24 21:56:38 -04001733 if (common_args_process(argc, argv, 2, 2, argv[0],
1734 "<time>", 0))
1735 return;
1736
1737 now = string_to_time(argv[1]);
1738 if (now == ((time_t) -1)) {
1739 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
1740 argv[1]);
1741 return;
1742
1743 } else {
1744 printf("Setting current time to %s\n", time_to_string(now));
1745 current_fs->now = now;
1746 }
1747}
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001748
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001749static int source_file(const char *cmd_file, int sci_idx)
1750{
1751 FILE *f;
1752 char buf[256];
1753 char *cp;
1754 int exit_status = 0;
1755 int retval;
1756
1757 if (strcmp(cmd_file, "-") == 0)
1758 f = stdin;
1759 else {
1760 f = fopen(cmd_file, "r");
1761 if (!f) {
1762 perror(cmd_file);
1763 exit(1);
1764 }
1765 }
1766 setbuf(stdout, NULL);
1767 setbuf(stderr, NULL);
1768 while (!feof(f)) {
1769 if (fgets(buf, sizeof(buf), f) == NULL)
1770 break;
1771 cp = strchr(buf, '\n');
1772 if (cp)
1773 *cp = 0;
1774 cp = strchr(buf, '\r');
1775 if (cp)
1776 *cp = 0;
1777 printf("debugfs: %s\n", buf);
1778 retval = ss_execute_line(sci_idx, buf);
1779 if (retval) {
1780 ss_perror(sci_idx, retval, buf);
1781 exit_status++;
1782 }
1783 }
1784 return exit_status;
1785}
1786
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001787int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001788{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001789 int retval;
1790 int sci_idx;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001791 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 +00001792 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001793 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001794 char *request = 0;
1795 int exit_status = 0;
1796 char *cmd_file = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001797 blk_t superblock = 0;
1798 blk_t blocksize = 0;
1799 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001800 char *data_filename = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001801
1802 initialize_ext2_error_table();
Theodore Ts'o0f8973f2001-08-27 12:44:23 -04001803 fprintf (stderr, "debugfs %s (%s)\n", E2FSPROGS_VERSION,
1804 E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001805
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001806 while ((c = getopt (argc, argv, "iwcR:f:b:s:Vd:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001807 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001808 case 'R':
1809 request = optarg;
1810 break;
1811 case 'f':
1812 cmd_file = optarg;
1813 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001814 case 'd':
1815 data_filename = optarg;
1816 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001817 case 'i':
1818 open_flags |= EXT2_FLAG_IMAGE_FILE;
1819 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001820 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00001821 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001822 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001823 case 'b':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001824 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001825 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001826 break;
1827 case 's':
Theodore Ts'ob38cd282002-05-11 22:13:20 -04001828 superblock = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001829 "superblock number", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001830 break;
1831 case 'c':
1832 catastrophic = 1;
1833 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001834 case 'V':
1835 /* Print version number and exit */
1836 fprintf(stderr, "\tUsing %s\n",
1837 error_message(EXT2_ET_BASE));
1838 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001839 default:
1840 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001841 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001842 }
1843 }
1844 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00001845 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04001846 superblock, blocksize, catastrophic,
1847 data_filename);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001848
1849 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1850 &debug_cmds, &retval);
1851 if (retval) {
1852 ss_perror(sci_idx, retval, "creating invocation");
1853 exit(1);
1854 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05001855 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001856
1857 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1858 if (retval) {
1859 ss_perror(sci_idx, retval, "adding standard requests");
1860 exit (1);
1861 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001862 if (request) {
1863 retval = 0;
1864 retval = ss_execute_line(sci_idx, request);
1865 if (retval) {
1866 ss_perror(sci_idx, retval, request);
1867 exit_status++;
1868 }
1869 } else if (cmd_file) {
1870 exit_status = source_file(cmd_file, sci_idx);
1871 } else {
1872 ss_listen(sci_idx);
1873 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001874
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001875 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001876 close_filesystem();
1877
Theodore Ts'oe5973042000-01-18 17:58:34 +00001878 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001879}