blob: 80662b1bd217557c6fbff9b15053adc7a659763c [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.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04007 *
Theodore Ts'o3839e651997-04-26 13:21:57 +00008 * 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'oefc6f622008-08-27 23:07:54 -040019#else
Theodore Ts'o50e1e101997-04-26 13:58:21 +000020extern 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
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include "debugfs.h"
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000031#include "uuid/uuid.h"
Theodore Ts'of68aa411999-10-26 14:20:22 +000032#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000033
Theodore Ts'oea822ee2005-03-20 18:03:58 -050034#include <ext2fs/ext2_ext_attr.h>
35
Theodore Ts'o818180c1998-06-27 05:11:14 +000036#include "../version.h"
Andreas Dilger03efde82008-08-23 21:42:46 -060037#include "jfs_user.h"
Theodore Ts'o818180c1998-06-27 05:11:14 +000038
JP Abgralle0ed7402014-03-19 19:08:39 -070039#ifndef BUFSIZ
40#define BUFSIZ 8192
41#endif
42
43/* 64KiB is the minimium blksize to best minimize system call overhead. */
44#ifndef IO_BUFSIZE
45#define IO_BUFSIZE 64*1024
46#endif
47
48/* Block size for `st_blocks' */
49#ifndef S_BLKSIZE
50#define S_BLKSIZE 512
51#endif
52
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -040053ss_request_table *extra_cmds;
Theodore Ts'o2d328bb2008-03-17 23:17:13 -040054const char *debug_prog_name;
JP Abgralle0ed7402014-03-19 19:08:39 -070055int sci_idx;
Theodore Ts'o3839e651997-04-26 13:21:57 +000056
Theodore Ts'ob044c2e2001-01-11 15:26:39 +000057ext2_filsys current_fs = NULL;
58ext2_ino_t root, cwd;
Theodore Ts'o3839e651997-04-26 13:21:57 +000059
JP Abgralle0ed7402014-03-19 19:08:39 -070060static void open_filesystem(char *device, int open_flags, blk64_t superblock,
61 blk64_t blocksize, int catastrophic,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040062 char *data_filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +000063{
64 int retval;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040065 io_channel data_io = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000066
67 if (superblock != 0 && blocksize == 0) {
68 com_err(device, 0, "if you specify the superblock, you must also specify the block size");
69 current_fs = NULL;
70 return;
71 }
72
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040073 if (data_filename) {
74 if ((open_flags & EXT2_FLAG_IMAGE_FILE) == 0) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -040075 com_err(device, 0,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -040076 "The -d option is only valid when reading an e2image file");
77 current_fs = NULL;
78 return;
79 }
80 retval = unix_io_manager->open(data_filename, 0, &data_io);
81 if (retval) {
82 com_err(data_filename, 0, "while opening data source");
83 current_fs = NULL;
84 return;
85 }
86 }
87
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000088 if (catastrophic && (open_flags & EXT2_FLAG_RW)) {
89 com_err(device, 0,
90 "opening read-only because of catastrophic mode");
91 open_flags &= ~EXT2_FLAG_RW;
92 }
JP Abgralle0ed7402014-03-19 19:08:39 -070093 if (catastrophic)
94 open_flags |= EXT2_FLAG_SKIP_MMP;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040095
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +000096 retval = ext2fs_open(device, open_flags, superblock, blocksize,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000097 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000098 if (retval) {
99 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000100 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000101 return;
102 }
JP Abgralle0ed7402014-03-19 19:08:39 -0700103 current_fs->default_bitmap_type = EXT2FS_BMAP64_RBTREE;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000104
105 if (catastrophic)
106 com_err(device, 0, "catastrophic mode - not reading inode or group bitmaps");
107 else {
108 retval = ext2fs_read_inode_bitmap(current_fs);
109 if (retval) {
110 com_err(device, retval, "while reading inode bitmap");
111 goto errout;
112 }
113 retval = ext2fs_read_block_bitmap(current_fs);
114 if (retval) {
115 com_err(device, retval, "while reading block bitmap");
116 goto errout;
117 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 }
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400119
120 if (data_io) {
121 retval = ext2fs_set_data_io(current_fs, data_io);
122 if (retval) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400123 com_err(device, retval,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400124 "while setting data source");
125 goto errout;
126 }
127 }
128
Theodore Ts'o3839e651997-04-26 13:21:57 +0000129 root = cwd = EXT2_ROOT_INO;
130 return;
131
132errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000133 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000134 if (retval)
135 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000136 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000137}
138
139void do_open_filesys(int argc, char **argv)
140{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500141 int c, err;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000142 int catastrophic = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -0700143 blk64_t superblock = 0;
144 blk64_t blocksize = 0;
145 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
Theodore Ts'ob0d17e02004-11-29 17:35:58 -0500146 char *data_filename = 0;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400147
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400148 reset_getopt();
Theodore Ts'o0fd68e02010-09-24 10:12:54 -0400149 while ((c = getopt (argc, argv, "iwfecb:s:d:D")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +0000150 switch (c) {
Theodore Ts'o59cf7e02001-05-03 15:05:55 +0000151 case 'i':
152 open_flags |= EXT2_FLAG_IMAGE_FILE;
153 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000154 case 'w':
JP Abgralle0ed7402014-03-19 19:08:39 -0700155#ifdef READ_ONLY
156 goto print_usage;
157#else
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000158 open_flags |= EXT2_FLAG_RW;
JP Abgralle0ed7402014-03-19 19:08:39 -0700159#endif /* READ_ONLY */
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000160 break;
161 case 'f':
162 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 break;
Theodore Ts'o98eb44b2006-03-18 19:58:13 -0500164 case 'e':
165 open_flags |= EXT2_FLAG_EXCLUSIVE;
166 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000167 case 'c':
168 catastrophic = 1;
169 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400170 case 'd':
171 data_filename = optarg;
172 break;
Theodore Ts'o0fd68e02010-09-24 10:12:54 -0400173 case 'D':
174 open_flags |= EXT2_FLAG_DIRECT_IO;
175 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000176 case 'b':
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500177 blocksize = parse_ulong(optarg, argv[0],
178 "block size", &err);
179 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000180 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000181 break;
182 case 's':
JP Abgralle0ed7402014-03-19 19:08:39 -0700183 err = strtoblk(argv[0], optarg, &superblock);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500184 if (err)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000185 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000186 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000187 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500188 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000189 }
190 }
191 if (optind != argc-1) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500192 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193 }
194 if (check_fs_not_open(argv[0]))
195 return;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000196 open_filesystem(argv[optind], open_flags,
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400197 superblock, blocksize, catastrophic,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -0400198 data_filename);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500199 return;
200
201print_usage:
Zheng Liu50937752013-12-08 21:01:09 -0500202 fprintf(stderr, "%s: Usage: open [-s superblock] [-b blocksize] "
JP Abgralle0ed7402014-03-19 19:08:39 -0700203 "[-d image_filename] [-c] [-i] [-f] [-e] [-D] "
204#ifndef READ_ONLY
205 "[-w] "
206#endif
207 "<device>\n", argv[0]);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000208}
209
210void do_lcd(int argc, char **argv)
211{
Theodore Ts'o57a1cbb2007-03-07 08:09:10 -0500212 if (argc != 2) {
213 com_err(argv[0], 0, "Usage: %s %s", argv[0], "<native dir>");
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000214 return;
Theodore Ts'o57a1cbb2007-03-07 08:09:10 -0500215 }
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +0000216
217 if (chdir(argv[1]) == -1) {
218 com_err(argv[0], errno,
219 "while trying to change native directory to %s",
220 argv[1]);
221 return;
222 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000223}
224
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000225static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226{
227 int retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400228
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000229 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
230 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000231 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500232 com_err("ext2fs_write_inode_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000233 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000234 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
235 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000236 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500237 com_err("ext2fs_write_block_bitmap", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000238 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000239 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000240 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500241 com_err("ext2fs_close", retval, 0);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000242 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000243 return;
244}
245
246void do_close_filesys(int argc, char **argv)
247{
Theodore Ts'o6dce5322009-05-28 22:03:33 -0400248 int c;
249
250 if (check_fs_open(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000251 return;
Theodore Ts'o6dce5322009-05-28 22:03:33 -0400252
253 reset_getopt();
254 while ((c = getopt (argc, argv, "a")) != EOF) {
255 switch (c) {
256 case 'a':
257 current_fs->flags &= ~EXT2_FLAG_MASTER_SB_ONLY;
258 break;
259 default:
260 goto print_usage;
261 }
262 }
263
264 if (argc > optind) {
265 print_usage:
266 com_err(0, 0, "Usage: close_filesys [-a]");
267 return;
268 }
269
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 close_filesystem();
271}
272
JP Abgralle0ed7402014-03-19 19:08:39 -0700273#ifndef READ_ONLY
Theodore Ts'o3839e651997-04-26 13:21:57 +0000274void do_init_filesys(int argc, char **argv)
275{
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276 struct ext2_super_block param;
277 errcode_t retval;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500278 int err;
JP Abgralle0ed7402014-03-19 19:08:39 -0700279 blk64_t blocks;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400280
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500281 if (common_args_process(argc, argv, 3, 3, "initialize",
JP Abgralle0ed7402014-03-19 19:08:39 -0700282 "<device> <blocks>", CHECK_FS_NOTOPEN))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000283 return;
284
285 memset(&param, 0, sizeof(struct ext2_super_block));
JP Abgralle0ed7402014-03-19 19:08:39 -0700286 err = strtoblk(argv[0], argv[2], &blocks);
287 if (err)
288 return;
289 ext2fs_blocks_count_set(&param, blocks);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500290 if (err)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000292 retval = ext2fs_initialize(argv[1], 0, &param,
293 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000294 if (retval) {
295 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000296 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297 return;
298 }
299 root = cwd = EXT2_ROOT_INO;
300 return;
301}
302
Theodore Ts'o5dd8f962001-01-01 15:51:50 +0000303static void print_features(struct ext2_super_block * s, FILE *f)
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000304{
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000305 int i, j, printed=0;
Theodore Ts'o092c3de2001-05-14 11:20:48 +0000306 __u32 *mask = &s->s_feature_compat, m;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000307
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000308 fputs("Filesystem features:", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000309 for (i=0; i <3; i++,mask++) {
310 for (j=0,m=1; j < 32; j++, m<<=1) {
311 if (*mask & m) {
312 fprintf(f, " %s", e2p_feature2string(i, m));
313 printed++;
314 }
315 }
316 }
317 if (printed == 0)
Theodore Ts'o777ebb32001-05-13 02:45:15 +0000318 fputs("(none)", f);
319 fputs("\n", f);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000320}
JP Abgralle0ed7402014-03-19 19:08:39 -0700321#endif /* READ_ONLY */
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000322
JP Abgralle0ed7402014-03-19 19:08:39 -0700323static void print_bg_opts(ext2_filsys fs, dgrp_t group, int mask,
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400324 const char *str, int *first, FILE *f)
325{
JP Abgralle0ed7402014-03-19 19:08:39 -0700326 if (ext2fs_bg_flags_test(fs, group, mask)) {
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400327 if (*first) {
328 fputs(" [", f);
329 *first = 0;
330 } else
331 fputs(", ", f);
332 fputs(str, f);
333 }
334}
335
Theodore Ts'o3839e651997-04-26 13:21:57 +0000336void do_show_super_stats(int argc, char *argv[])
337{
JP Abgralle0ed7402014-03-19 19:08:39 -0700338 const char *units ="block";
Theodore Ts'o54434922003-12-07 01:28:50 -0500339 dgrp_t i;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000340 FILE *out;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000341 int c, header_only = 0;
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500342 int numdirs = 0, first, gdt_csum;
343
Theodore Ts'o88494bb2003-05-13 23:03:43 -0400344 reset_getopt();
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000345 while ((c = getopt (argc, argv, "h")) != EOF) {
346 switch (c) {
347 case 'h':
348 header_only++;
349 break;
350 default:
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500351 goto print_usage;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000352 }
353 }
354 if (optind != argc) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500355 goto print_usage;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000356 }
357 if (check_fs_open(argv[0]))
358 return;
359 out = open_pager();
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000360
JP Abgralle0ed7402014-03-19 19:08:39 -0700361 if (EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
362 EXT4_FEATURE_RO_COMPAT_BIGALLOC))
363 units = "cluster";
364
Theodore Ts'obd09eff2000-08-14 20:39:17 +0000365 list_super2(current_fs->super, out);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400366 for (i=0; i < current_fs->group_desc_count; i++)
JP Abgralle0ed7402014-03-19 19:08:39 -0700367 numdirs += ext2fs_bg_used_dirs_count(current_fs, i);
Theodore Ts'o34be9602002-07-15 16:56:41 -0400368 fprintf(out, "Directories: %d\n", numdirs);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400369
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000370 if (header_only) {
371 close_pager(out);
372 return;
373 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400374
Eric Sandeeneefa4d92009-01-29 17:46:28 -0500375 gdt_csum = EXT2_HAS_RO_COMPAT_FEATURE(current_fs->super,
376 EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
JP Abgralle0ed7402014-03-19 19:08:39 -0700377 for (i = 0; i < current_fs->group_desc_count; i++) {
378 fprintf(out, " Group %2d: block bitmap at %llu, "
379 "inode bitmap at %llu, "
380 "inode table at %llu\n"
381 " %u free %s%s, "
382 "%u free %s, "
383 "%u used %s%s",
384 i, ext2fs_block_bitmap_loc(current_fs, i),
385 ext2fs_inode_bitmap_loc(current_fs, i),
386 ext2fs_inode_table_loc(current_fs, i),
387 ext2fs_bg_free_blocks_count(current_fs, i), units,
388 ext2fs_bg_free_blocks_count(current_fs, i) != 1 ?
389 "s" : "",
390 ext2fs_bg_free_inodes_count(current_fs, i),
391 ext2fs_bg_free_inodes_count(current_fs, i) != 1 ?
392 "inodes" : "inode",
393 ext2fs_bg_used_dirs_count(current_fs, i),
394 ext2fs_bg_used_dirs_count(current_fs, i) != 1 ? "directories"
395 : "directory", gdt_csum ? ", " : "\n");
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500396 if (gdt_csum)
JP Abgralle0ed7402014-03-19 19:08:39 -0700397 fprintf(out, "%u unused %s\n",
398 ext2fs_bg_itable_unused(current_fs, i),
399 ext2fs_bg_itable_unused(current_fs, i) != 1 ?
400 "inodes" : "inode");
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400401 first = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700402 print_bg_opts(current_fs, i, EXT2_BG_INODE_UNINIT, "Inode not init",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400403 &first, out);
JP Abgralle0ed7402014-03-19 19:08:39 -0700404 print_bg_opts(current_fs, i, EXT2_BG_BLOCK_UNINIT, "Block not init",
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400405 &first, out);
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500406 if (gdt_csum) {
407 fprintf(out, "%sChecksum 0x%04x",
JP Abgralle0ed7402014-03-19 19:08:39 -0700408 first ? " [":", ", ext2fs_bg_checksum(current_fs, i));
Jose R. Santos8fdf2912007-10-21 21:03:57 -0500409 first = 0;
410 }
Theodore Ts'of5fa2002006-05-08 20:17:26 -0400411 if (!first)
412 fputs("]\n", out);
413 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000414 close_pager(out);
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500415 return;
416print_usage:
417 fprintf(stderr, "%s: Usage: show_super [-h]\n", argv[0]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000418}
419
JP Abgralle0ed7402014-03-19 19:08:39 -0700420#ifndef READ_ONLY
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400421void do_dirty_filesys(int argc EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500422 char **argv EXT2FS_ATTR((unused)))
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000423{
424 if (check_fs_open(argv[0]))
425 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000426 if (check_fs_read_write(argv[0]))
427 return;
428
429 if (argv[1] && !strcmp(argv[1], "-clean"))
430 current_fs->super->s_state |= EXT2_VALID_FS;
431 else
432 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000433 ext2fs_mark_super_dirty(current_fs);
434}
JP Abgralle0ed7402014-03-19 19:08:39 -0700435#endif /* READ_ONLY */
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000436
Theodore Ts'o3839e651997-04-26 13:21:57 +0000437struct list_blocks_struct {
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700438 FILE *f;
439 e2_blkcnt_t total;
JP Abgralle0ed7402014-03-19 19:08:39 -0700440 blk64_t first_block, last_block;
Andreas Dilger89e25cf2001-11-08 17:56:12 -0700441 e2_blkcnt_t first_bcnt, last_bcnt;
442 e2_blkcnt_t first;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000443};
444
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000445static void finish_range(struct list_blocks_struct *lb)
446{
447 if (lb->first_block == 0)
448 return;
449 if (lb->first)
450 lb->first = 0;
451 else
Theodore Ts'o80bfaa32000-08-18 15:08:37 +0000452 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000453 if (lb->first_block == lb->last_block)
JP Abgralle0ed7402014-03-19 19:08:39 -0700454 fprintf(lb->f, "(%lld):%llu",
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400455 (long long)lb->first_bcnt, lb->first_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000456 else
JP Abgralle0ed7402014-03-19 19:08:39 -0700457 fprintf(lb->f, "(%lld-%lld):%llu-%llu",
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400458 (long long)lb->first_bcnt, (long long)lb->last_bcnt,
459 lb->first_block, lb->last_block);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000460 lb->first_block = 0;
461}
462
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400463static int list_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
JP Abgralle0ed7402014-03-19 19:08:39 -0700464 blk64_t *blocknr, e2_blkcnt_t blockcnt,
465 blk64_t ref_block EXT2FS_ATTR((unused)),
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400466 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -0500467 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468{
469 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
470
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000471 lb->total++;
472 if (blockcnt >= 0) {
473 /*
474 * See if we can add on to the existing range (if it exists)
475 */
476 if (lb->first_block &&
477 (lb->last_block+1 == *blocknr) &&
478 (lb->last_bcnt+1 == blockcnt)) {
479 lb->last_block = *blocknr;
480 lb->last_bcnt = blockcnt;
481 return 0;
482 }
483 /*
484 * Start a new range.
485 */
486 finish_range(lb);
487 lb->first_block = lb->last_block = *blocknr;
488 lb->first_bcnt = lb->last_bcnt = blockcnt;
489 return 0;
490 }
491 /*
492 * Not a normal block. Always force a new range.
493 */
494 finish_range(lb);
495 if (lb->first)
496 lb->first = 0;
Theodore Ts'oa5eef732000-08-14 15:47:15 +0000497 else
Theodore Ts'o2c4a5402000-08-19 17:33:28 +0000498 fprintf(lb->f, ", ");
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000499 if (blockcnt == -1)
JP Abgralle0ed7402014-03-19 19:08:39 -0700500 fprintf(lb->f, "(IND):%llu", (unsigned long long) *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000501 else if (blockcnt == -2)
JP Abgralle0ed7402014-03-19 19:08:39 -0700502 fprintf(lb->f, "(DIND):%llu", (unsigned long long) *blocknr);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000503 else if (blockcnt == -3)
JP Abgralle0ed7402014-03-19 19:08:39 -0700504 fprintf(lb->f, "(TIND):%llu", (unsigned long long) *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 return 0;
506}
507
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500508static void dump_xattr_string(FILE *out, const char *str, int len)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500509{
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600510 int printable = 0;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500511 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400512
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600513 /* check: is string "printable enough?" */
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500514 for (i = 0; i < len; i++)
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600515 if (isprint(str[i]))
516 printable++;
517
518 if (printable <= len*7/8)
519 printable = 0;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500520
521 for (i = 0; i < len; i++)
522 if (printable)
Eric Sandeen290ac0e2008-01-29 21:30:46 -0600523 fprintf(out, isprint(str[i]) ? "%c" : "\\%03o",
524 (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500525 else
Andreas Dilgerd047e072006-06-21 00:01:42 -0400526 fprintf(out, "%02x ", (unsigned char)str[i]);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500527}
528
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400529static void internal_dump_inode_extra(FILE *out,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400530 const char *prefix EXT2FS_ATTR((unused)),
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400531 ext2_ino_t inode_num EXT2FS_ATTR((unused)),
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500532 struct ext2_inode_large *inode)
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500533{
534 struct ext2_ext_attr_entry *entry;
535 __u32 *magic;
536 char *start, *end;
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500537 unsigned int storage_size;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500538
Takashi Sato8deb80a2006-03-18 21:43:46 -0500539 fprintf(out, "Size of extra inode fields: %u\n", inode->i_extra_isize);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500540 if (inode->i_extra_isize > EXT2_INODE_SIZE(current_fs->super) -
541 EXT2_GOOD_OLD_INODE_SIZE) {
542 fprintf(stderr, "invalid inode->i_extra_isize (%u)\n",
543 inode->i_extra_isize);
544 return;
545 }
546 storage_size = EXT2_INODE_SIZE(current_fs->super) -
547 EXT2_GOOD_OLD_INODE_SIZE -
548 inode->i_extra_isize;
549 magic = (__u32 *)((char *)inode + EXT2_GOOD_OLD_INODE_SIZE +
550 inode->i_extra_isize);
551 if (*magic == EXT2_EXT_ATTR_MAGIC) {
552 fprintf(out, "Extended attributes stored in inode body: \n");
553 end = (char *) inode + EXT2_INODE_SIZE(current_fs->super);
554 start = (char *) magic + sizeof(__u32);
555 entry = (struct ext2_ext_attr_entry *) start;
556 while (!EXT2_EXT_IS_LAST_ENTRY(entry)) {
557 struct ext2_ext_attr_entry *next =
558 EXT2_EXT_ATTR_NEXT(entry);
559 if (entry->e_value_size > storage_size ||
560 (char *) next >= end) {
561 fprintf(out, "invalid EA entry in inode\n");
562 return;
563 }
564 fprintf(out, " ");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400565 dump_xattr_string(out, EXT2_EXT_ATTR_NAME(entry),
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500566 entry->e_name_len);
567 fprintf(out, " = \"");
568 dump_xattr_string(out, start + entry->e_value_offs,
569 entry->e_value_size);
Takashi Sato8deb80a2006-03-18 21:43:46 -0500570 fprintf(out, "\" (%u)\n", entry->e_value_size);
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500571 entry = next;
572 }
573 }
574}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000575
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000576static void dump_blocks(FILE *f, const char *prefix, ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000577{
578 struct list_blocks_struct lb;
579
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000580 fprintf(f, "%sBLOCKS:\n%s", prefix, prefix);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581 lb.total = 0;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000582 lb.first_block = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000583 lb.f = f;
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000584 lb.first = 1;
JP Abgralle0ed7402014-03-19 19:08:39 -0700585 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
586 list_blocks_proc, (void *)&lb);
Theodore Ts'o0a3db932000-08-14 17:06:05 +0000587 finish_range(&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 if (lb.total)
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400589 fprintf(f, "\n%sTOTAL: %lld\n", prefix, (long long)lb.total);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000590 fprintf(f,"\n");
591}
592
Theodore Ts'o187cb622009-07-26 22:29:47 -0400593static int int_log10(unsigned long long arg)
594{
595 int l = 0;
596
597 arg = arg / 10;
598 while (arg) {
599 l++;
600 arg = arg / 10;
601 }
602 return l;
603}
604
605#define DUMP_LEAF_EXTENTS 0x01
606#define DUMP_NODE_EXTENTS 0x02
607#define DUMP_EXTENT_TABLE 0x04
608
609static void dump_extents(FILE *f, const char *prefix, ext2_ino_t ino,
610 int flags, int logical_width, int physical_width)
611{
612 ext2_extent_handle_t handle;
613 struct ext2fs_extent extent;
614 struct ext2_extent_info info;
615 int op = EXT2_EXTENT_ROOT;
616 unsigned int printed = 0;
617 errcode_t errcode;
618
619 errcode = ext2fs_extent_open(current_fs, ino, &handle);
620 if (errcode)
621 return;
622
623 if (flags & DUMP_EXTENT_TABLE)
624 fprintf(f, "Level Entries %*s %*s Length Flags\n",
625 (logical_width*2)+3, "Logical",
626 (physical_width*2)+3, "Physical");
627 else
628 fprintf(f, "%sEXTENTS:\n%s", prefix, prefix);
629
630 while (1) {
631 errcode = ext2fs_extent_get(handle, op, &extent);
632
633 if (errcode)
634 break;
635
636 op = EXT2_EXTENT_NEXT;
637
638 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
639 continue;
640
641 if (extent.e_flags & EXT2_EXTENT_FLAGS_LEAF) {
642 if ((flags & DUMP_LEAF_EXTENTS) == 0)
643 continue;
644 } else {
645 if ((flags & DUMP_NODE_EXTENTS) == 0)
646 continue;
647 }
648
649 errcode = ext2fs_extent_get_info(handle, &info);
650 if (errcode)
651 continue;
652
653 if (!(extent.e_flags & EXT2_EXTENT_FLAGS_LEAF)) {
654 if (extent.e_flags & EXT2_EXTENT_FLAGS_SECOND_VISIT)
655 continue;
656
657 if (flags & DUMP_EXTENT_TABLE) {
658 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
659 "%*llu%*s %6u\n",
660 info.curr_level, info.max_depth,
661 info.curr_entry, info.num_entries,
662 logical_width,
663 extent.e_lblk,
664 logical_width,
665 extent.e_lblk + (extent.e_len - 1),
666 physical_width,
667 extent.e_pblk,
668 physical_width+3, "", extent.e_len);
669 continue;
670 }
671
Theodore Ts'oa9468442010-09-24 22:05:58 -0400672 fprintf(f, "%s(ETB%d):%lld",
Theodore Ts'oc4b87b82010-09-19 22:52:09 -0400673 printed ? ", " : "", info.curr_level,
Theodore Ts'o187cb622009-07-26 22:29:47 -0400674 extent.e_pblk);
675 printed = 1;
676 continue;
677 }
678
679 if (flags & DUMP_EXTENT_TABLE) {
680 fprintf(f, "%2d/%2d %3d/%3d %*llu - %*llu "
681 "%*llu - %*llu %6u %s\n",
682 info.curr_level, info.max_depth,
683 info.curr_entry, info.num_entries,
684 logical_width,
685 extent.e_lblk,
686 logical_width,
687 extent.e_lblk + (extent.e_len - 1),
688 physical_width,
689 extent.e_pblk,
690 physical_width,
691 extent.e_pblk + (extent.e_len - 1),
692 extent.e_len,
693 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
694 "Uninit" : "");
695 continue;
696 }
697
698 if (extent.e_len == 0)
699 continue;
700 else if (extent.e_len == 1)
701 fprintf(f,
Theodore Ts'oa9468442010-09-24 22:05:58 -0400702 "%s(%lld%s):%lld",
Theodore Ts'o187cb622009-07-26 22:29:47 -0400703 printed ? ", " : "",
704 extent.e_lblk,
705 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
Theodore Ts'oa9468442010-09-24 22:05:58 -0400706 "[u]" : "",
Theodore Ts'o187cb622009-07-26 22:29:47 -0400707 extent.e_pblk);
708 else
709 fprintf(f,
Theodore Ts'oa9468442010-09-24 22:05:58 -0400710 "%s(%lld-%lld%s):%lld-%lld",
Theodore Ts'o187cb622009-07-26 22:29:47 -0400711 printed ? ", " : "",
712 extent.e_lblk,
713 extent.e_lblk + (extent.e_len - 1),
714 extent.e_flags & EXT2_EXTENT_FLAGS_UNINIT ?
Theodore Ts'oa9468442010-09-24 22:05:58 -0400715 "[u]" : "",
Theodore Ts'o187cb622009-07-26 22:29:47 -0400716 extent.e_pblk,
717 extent.e_pblk + (extent.e_len - 1));
718 printed = 1;
719 }
720 if (printed)
721 fprintf(f, "\n");
722}
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723
Theodore Ts'o5e4f0702001-06-01 15:36:05 +0000724void internal_dump_inode(FILE *out, const char *prefix,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000725 ext2_ino_t inode_num, struct ext2_inode *inode,
726 int do_dump_blocks)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000727{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000728 const char *i_type;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000729 char frag, fsize;
730 int os = current_fs->super->s_creator_os;
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400731 struct ext2_inode_large *large_inode;
732 int is_large_inode = 0;
733
734 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
735 is_large_inode = 1;
736 large_inode = (struct ext2_inode_large *) inode;
737
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000738 if (LINUX_S_ISDIR(inode->i_mode)) i_type = "directory";
739 else if (LINUX_S_ISREG(inode->i_mode)) i_type = "regular";
740 else if (LINUX_S_ISLNK(inode->i_mode)) i_type = "symlink";
741 else if (LINUX_S_ISBLK(inode->i_mode)) i_type = "block special";
742 else if (LINUX_S_ISCHR(inode->i_mode)) i_type = "character special";
743 else if (LINUX_S_ISFIFO(inode->i_mode)) i_type = "FIFO";
744 else if (LINUX_S_ISSOCK(inode->i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000745 else i_type = "bad type";
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000746 fprintf(out, "%sInode: %u Type: %s ", prefix, inode_num, i_type);
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400747 fprintf(out, "%sMode: %04o Flags: 0x%x\n",
748 prefix, inode->i_mode & 0777, inode->i_flags);
749 if (is_large_inode && large_inode->i_extra_isize >= 24) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400750 fprintf(out, "%sGeneration: %u Version: 0x%08x:%08x\n",
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400751 prefix, inode->i_generation, large_inode->i_version_hi,
752 inode->osd1.linux1.l_i_version);
753 } else {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400754 fprintf(out, "%sGeneration: %u Version: 0x%08x\n", prefix,
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400755 inode->i_generation, inode->osd1.linux1.l_i_version);
756 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000757 fprintf(out, "%sUser: %5d Group: %5d Size: ",
Eric Sandeen5113a6e2007-05-08 00:10:54 -0400758 prefix, inode_uid(*inode), inode_gid(*inode));
JP Abgralle0ed7402014-03-19 19:08:39 -0700759 if (LINUX_S_ISREG(inode->i_mode))
760 fprintf(out, "%llu\n", EXT2_I_SIZE(inode));
761 else
Andreas Dilger1a6bb622001-11-08 17:52:26 -0700762 fprintf(out, "%d\n", inode->i_size);
Theodore Ts'o5d171192006-11-11 06:32:03 -0500763 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000764 fprintf(out,
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000765 "%sFile ACL: %d Directory ACL: %d Translator: %d\n",
766 prefix,
767 inode->i_file_acl, LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0,
768 inode->osd1.hurd1.h_i_translator);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000769 else
Theodore Ts'o81624c32009-04-18 09:40:26 -0400770 fprintf(out, "%sFile ACL: %llu Directory ACL: %d\n",
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000771 prefix,
Theodore Ts'o81624c32009-04-18 09:40:26 -0400772 inode->i_file_acl | ((long long)
773 (inode->osd2.linux2.l_i_file_acl_high) << 32),
774 LINUX_S_ISDIR(inode->i_mode) ? inode->i_dir_acl : 0);
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400775 if (os == EXT2_OS_LINUX)
Theodore Ts'o5d171192006-11-11 06:32:03 -0500776 fprintf(out, "%sLinks: %d Blockcount: %llu\n",
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400777 prefix, inode->i_links_count,
778 (((unsigned long long)
779 inode->osd2.linux2.l_i_blocks_hi << 32)) +
Theodore Ts'o5d171192006-11-11 06:32:03 -0500780 inode->i_blocks);
781 else
782 fprintf(out, "%sLinks: %d Blockcount: %u\n",
783 prefix, inode->i_links_count, inode->i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000784 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000785 case EXT2_OS_HURD:
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000786 frag = inode->osd2.hurd2.h_i_frag;
787 fsize = inode->osd2.hurd2.h_i_fsize;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000788 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000789 default:
790 frag = fsize = 0;
791 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000792 fprintf(out, "%sFragment: Address: %d Number: %d Size: %d\n",
793 prefix, inode->i_faddr, frag, fsize);
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400794 if (is_large_inode && large_inode->i_extra_isize >= 24) {
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400795 fprintf(out, "%s ctime: 0x%08x:%08x -- %s", prefix,
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400796 inode->i_ctime, large_inode->i_ctime_extra,
797 time_to_string(inode->i_ctime));
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400798 fprintf(out, "%s atime: 0x%08x:%08x -- %s", prefix,
Theodore Ts'oa16031c2008-05-15 22:17:06 -0400799 inode->i_atime, large_inode->i_atime_extra,
800 time_to_string(inode->i_atime));
801 fprintf(out, "%s mtime: 0x%08x:%08x -- %s", prefix,
802 inode->i_mtime, large_inode->i_mtime_extra,
803 time_to_string(inode->i_mtime));
804 fprintf(out, "%scrtime: 0x%08x:%08x -- %s", prefix,
805 large_inode->i_crtime, large_inode->i_crtime_extra,
806 time_to_string(large_inode->i_crtime));
807 } else {
808 fprintf(out, "%sctime: 0x%08x -- %s", prefix, inode->i_ctime,
809 time_to_string(inode->i_ctime));
810 fprintf(out, "%satime: 0x%08x -- %s", prefix, inode->i_atime,
811 time_to_string(inode->i_atime));
812 fprintf(out, "%smtime: 0x%08x -- %s", prefix, inode->i_mtime,
813 time_to_string(inode->i_mtime));
814 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400815 if (inode->i_dtime)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000816 fprintf(out, "%sdtime: 0x%08x -- %s", prefix, inode->i_dtime,
817 time_to_string(inode->i_dtime));
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500818 if (EXT2_INODE_SIZE(current_fs->super) > EXT2_GOOD_OLD_INODE_SIZE)
819 internal_dump_inode_extra(out, prefix, inode_num,
820 (struct ext2_inode_large *) inode);
Theodore Ts'o795afc42004-02-21 20:54:31 -0500821 if (LINUX_S_ISLNK(inode->i_mode) && ext2fs_inode_data_blocks(current_fs,inode) == 0)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000822 fprintf(out, "%sFast_link_dest: %.*s\n", prefix,
823 (int) inode->i_size, (char *)inode->i_block);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500824 else if (LINUX_S_ISBLK(inode->i_mode) || LINUX_S_ISCHR(inode->i_mode)) {
825 int major, minor;
826 const char *devnote;
827
828 if (inode->i_block[0]) {
829 major = (inode->i_block[0] >> 8) & 255;
830 minor = inode->i_block[0] & 255;
831 devnote = "";
832 } else {
833 major = (inode->i_block[1] & 0xfff00) >> 8;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400834 minor = ((inode->i_block[1] & 0xff) |
Theodore Ts'o2d107692004-02-23 22:30:54 -0500835 ((inode->i_block[1] >> 12) & 0xfff00));
836 devnote = "(New-style) ";
837 }
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400838 fprintf(out, "%sDevice major/minor number: %02d:%02d (hex %02x:%02x)\n",
Theodore Ts'o2d107692004-02-23 22:30:54 -0500839 devnote, major, minor, major, minor);
Theodore Ts'o187cb622009-07-26 22:29:47 -0400840 } else if (do_dump_blocks) {
841 if (inode->i_flags & EXT4_EXTENTS_FL)
842 dump_extents(out, prefix, inode_num,
Theodore Ts'oc4b87b82010-09-19 22:52:09 -0400843 DUMP_LEAF_EXTENTS|DUMP_NODE_EXTENTS, 0, 0);
Theodore Ts'o187cb622009-07-26 22:29:47 -0400844 else
845 dump_blocks(out, prefix, inode_num);
Theodore Ts'o2d107692004-02-23 22:30:54 -0500846 }
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000847}
848
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500849static void dump_inode(ext2_ino_t inode_num, struct ext2_inode *inode)
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000850{
851 FILE *out;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400852
Theodore Ts'oda81e3f2001-03-29 20:49:58 +0000853 out = open_pager();
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500854 internal_dump_inode(out, "", inode_num, inode, 1);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000855 close_pager(out);
856}
857
Theodore Ts'o3839e651997-04-26 13:21:57 +0000858void do_stat(int argc, char *argv[])
859{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000860 ext2_ino_t inode;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500861 struct ext2_inode * inode_buf;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000862
Theodore Ts'o64777392005-05-05 17:21:46 -0400863 if (check_fs_open(argv[0]))
Theodore Ts'o8363e352005-05-06 09:04:37 -0400864 return;
Theodore Ts'o64777392005-05-05 17:21:46 -0400865
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500866 inode_buf = (struct ext2_inode *)
867 malloc(EXT2_INODE_SIZE(current_fs->super));
868 if (!inode_buf) {
869 fprintf(stderr, "do_stat: can't allocate buffer\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000870 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500871 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000872
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500873 if (common_inode_args_process(argc, argv, &inode, 0)) {
874 free(inode_buf);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500875 return;
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500876 }
Theodore Ts'o3839e651997-04-26 13:21:57 +0000877
Theodore Ts'oea822ee2005-03-20 18:03:58 -0500878 if (debugfs_read_inode_full(inode, inode_buf, argv[0],
879 EXT2_INODE_SIZE(current_fs->super))) {
880 free(inode_buf);
881 return;
882 }
883
884 dump_inode(inode, inode_buf);
885 free(inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000886 return;
887}
888
JP Abgralle0ed7402014-03-19 19:08:39 -0700889void do_dump_extents(int argc, char **argv)
Theodore Ts'o187cb622009-07-26 22:29:47 -0400890{
891 struct ext2_inode inode;
892 ext2_ino_t ino;
893 FILE *out;
894 int c, flags = 0;
895 int logical_width;
896 int physical_width;
897
898 reset_getopt();
899 while ((c = getopt(argc, argv, "nl")) != EOF) {
900 switch (c) {
901 case 'n':
902 flags |= DUMP_NODE_EXTENTS;
903 break;
904 case 'l':
905 flags |= DUMP_LEAF_EXTENTS;
906 break;
907 }
908 }
909
JP Abgralle0ed7402014-03-19 19:08:39 -0700910 if (argc != optind + 1) {
Theodore Ts'o187cb622009-07-26 22:29:47 -0400911 com_err(0, 0, "Usage: dump_extents [-n] [-l] file");
912 return;
913 }
914
915 if (flags == 0)
916 flags = DUMP_NODE_EXTENTS | DUMP_LEAF_EXTENTS;
917 flags |= DUMP_EXTENT_TABLE;
918
919 if (check_fs_open(argv[0]))
920 return;
921
922 ino = string_to_inode(argv[optind]);
923 if (ino == 0)
924 return;
925
926 if (debugfs_read_inode(ino, &inode, argv[0]))
927 return;
928
929 if ((inode.i_flags & EXT4_EXTENTS_FL) == 0) {
930 fprintf(stderr, "%s: does not uses extent block maps\n",
931 argv[optind]);
932 return;
933 }
934
JP Abgralle0ed7402014-03-19 19:08:39 -0700935 logical_width = int_log10((EXT2_I_SIZE(&inode)+current_fs->blocksize-1)/
Theodore Ts'o187cb622009-07-26 22:29:47 -0400936 current_fs->blocksize) + 1;
937 if (logical_width < 5)
938 logical_width = 5;
JP Abgralle0ed7402014-03-19 19:08:39 -0700939 physical_width = int_log10(ext2fs_blocks_count(current_fs->super)) + 1;
Theodore Ts'o187cb622009-07-26 22:29:47 -0400940 if (physical_width < 5)
941 physical_width = 5;
942
943 out = open_pager();
944 dump_extents(out, "", ino, flags, logical_width, physical_width);
945 close_pager(out);
946 return;
947}
948
JP Abgralle0ed7402014-03-19 19:08:39 -0700949static int print_blocks_proc(ext2_filsys fs EXT2FS_ATTR((unused)),
950 blk64_t *blocknr,
951 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
952 blk64_t ref_block EXT2FS_ATTR((unused)),
953 int ref_offset EXT2FS_ATTR((unused)),
954 void *private EXT2FS_ATTR((unused)))
955{
956 printf("%llu ", *blocknr);
957 return 0;
958}
959
960void do_blocks(int argc, char *argv[])
961{
962 ext2_ino_t inode;
963
964 if (check_fs_open(argv[0]))
965 return;
966
967 if (common_inode_args_process(argc, argv, &inode, 0)) {
968 return;
969 }
970
971 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
972 print_blocks_proc, NULL);
973 fputc('\n', stdout);
974 return;
975}
976
Theodore Ts'o3839e651997-04-26 13:21:57 +0000977void do_chroot(int argc, char *argv[])
978{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000979 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000980 int retval;
981
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500982 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000983 return;
984
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000985 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000986 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -0500987 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000988 return;
989 }
990 root = inode;
991}
992
JP Abgralle0ed7402014-03-19 19:08:39 -0700993#ifndef READ_ONLY
Theodore Ts'o3839e651997-04-26 13:21:57 +0000994void do_clri(int argc, char *argv[])
995{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +0000996 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000997 struct ext2_inode inode_buf;
998
Theodore Ts'oe1018ee2002-01-03 04:55:25 -0500999 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001000 return;
1001
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001002 if (debugfs_read_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001003 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001004 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001005 if (debugfs_write_inode(inode, &inode_buf, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001006 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001007}
1008
1009void do_freei(int argc, char *argv[])
1010{
JP Abgralle0ed7402014-03-19 19:08:39 -07001011 unsigned int len = 1;
1012 int err = 0;
1013 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001014
JP Abgralle0ed7402014-03-19 19:08:39 -07001015 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1016 CHECK_FS_RW | CHECK_FS_BITMAPS))
1017 return;
1018 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001019 return;
1020
JP Abgralle0ed7402014-03-19 19:08:39 -07001021 inode = string_to_inode(argv[1]);
1022 if (!inode)
1023 return;
1024
1025 if (argc == 3) {
1026 len = parse_ulong(argv[2], argv[0], "length", &err);
1027 if (err)
1028 return;
1029 }
1030
1031 if (len == 1 &&
1032 !ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001033 com_err(argv[0], 0, "Warning: inode already clear");
JP Abgralle0ed7402014-03-19 19:08:39 -07001034 while (len-- > 0)
1035 ext2fs_unmark_inode_bitmap2(current_fs->inode_map, inode++);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001036 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001037}
1038
1039void do_seti(int argc, char *argv[])
1040{
JP Abgralle0ed7402014-03-19 19:08:39 -07001041 unsigned int len = 1;
1042 int err = 0;
1043 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001044
JP Abgralle0ed7402014-03-19 19:08:39 -07001045 if (common_args_process(argc, argv, 2, 3, argv[0], "<file> [num]",
1046 CHECK_FS_RW | CHECK_FS_BITMAPS))
1047 return;
1048 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001049 return;
1050
JP Abgralle0ed7402014-03-19 19:08:39 -07001051 inode = string_to_inode(argv[1]);
1052 if (!inode)
1053 return;
1054
1055 if (argc == 3) {
1056 len = parse_ulong(argv[2], argv[0], "length", &err);
1057 if (err)
1058 return;
1059 }
1060
1061 if ((len == 1) &&
1062 ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001063 com_err(argv[0], 0, "Warning: inode already set");
JP Abgralle0ed7402014-03-19 19:08:39 -07001064 while (len-- > 0)
1065 ext2fs_mark_inode_bitmap2(current_fs->inode_map, inode++);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001066 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001067}
JP Abgralle0ed7402014-03-19 19:08:39 -07001068#endif /* READ_ONLY */
Theodore Ts'o3839e651997-04-26 13:21:57 +00001069
1070void do_testi(int argc, char *argv[])
1071{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001072 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001073
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001074 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_BITMAPS))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001075 return;
1076
JP Abgralle0ed7402014-03-19 19:08:39 -07001077 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,inode))
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001078 printf("Inode %u is marked in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001079 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001080 printf("Inode %u is not in use\n", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001081}
1082
JP Abgralle0ed7402014-03-19 19:08:39 -07001083#ifndef READ_ONLY
Theodore Ts'o3839e651997-04-26 13:21:57 +00001084void do_freeb(int argc, char *argv[])
1085{
JP Abgralle0ed7402014-03-19 19:08:39 -07001086 blk64_t block;
1087 blk64_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001088
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001089 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001090 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001091 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001092 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001093 while (count-- > 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001094 if (!ext2fs_test_block_bitmap2(current_fs->block_map,block))
1095 com_err(argv[0], 0, "Warning: block %llu already clear",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001096 block);
JP Abgralle0ed7402014-03-19 19:08:39 -07001097 ext2fs_unmark_block_bitmap2(current_fs->block_map,block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001098 block++;
1099 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001100 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001101}
1102
1103void do_setb(int argc, char *argv[])
1104{
JP Abgralle0ed7402014-03-19 19:08:39 -07001105 blk64_t block;
1106 blk64_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001107
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001108 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001109 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001110 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001111 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001112 while (count-- > 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001113 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1114 com_err(argv[0], 0, "Warning: block %llu already set",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001115 block);
JP Abgralle0ed7402014-03-19 19:08:39 -07001116 ext2fs_mark_block_bitmap2(current_fs->block_map,block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001117 block++;
1118 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001119 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001120}
JP Abgralle0ed7402014-03-19 19:08:39 -07001121#endif /* READ_ONLY */
Theodore Ts'o3839e651997-04-26 13:21:57 +00001122
1123void do_testb(int argc, char *argv[])
1124{
JP Abgralle0ed7402014-03-19 19:08:39 -07001125 blk64_t block;
1126 blk64_t count = 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001127
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001128 if (common_block_args_process(argc, argv, &block, &count))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001129 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001130 while (count-- > 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001131 if (ext2fs_test_block_bitmap2(current_fs->block_map,block))
1132 printf("Block %llu marked in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001133 else
JP Abgralle0ed7402014-03-19 19:08:39 -07001134 printf("Block %llu not in use\n", block);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001135 block++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001136 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001137}
1138
JP Abgralle0ed7402014-03-19 19:08:39 -07001139#ifndef READ_ONLY
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001140static void modify_u8(char *com, const char *prompt,
1141 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001142{
1143 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001144 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001145 char *tmp;
1146
1147 sprintf(buf, format, *val);
1148 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +04001149 if (!fgets(buf, sizeof(buf), stdin))
1150 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001151 if (buf[strlen (buf) - 1] == '\n')
1152 buf[strlen (buf) - 1] = '\0';
1153 if (!buf[0])
1154 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001155 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001156 if (*tmp)
1157 com_err(com, 0, "Bad value - %s", buf);
1158 else
1159 *val = v;
1160}
1161
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001162static void modify_u16(char *com, const char *prompt,
1163 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001164{
1165 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001166 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001167 char *tmp;
1168
1169 sprintf(buf, format, *val);
1170 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +04001171 if (!fgets(buf, sizeof(buf), stdin))
1172 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001173 if (buf[strlen (buf) - 1] == '\n')
1174 buf[strlen (buf) - 1] = '\0';
1175 if (!buf[0])
1176 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001177 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001178 if (*tmp)
1179 com_err(com, 0, "Bad value - %s", buf);
1180 else
1181 *val = v;
1182}
1183
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001184static void modify_u32(char *com, const char *prompt,
1185 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001186{
1187 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001188 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001189 char *tmp;
1190
1191 sprintf(buf, format, *val);
1192 printf("%30s [%s] ", prompt, buf);
Dmitry V. Levind9039ae2007-10-20 22:08:40 +04001193 if (!fgets(buf, sizeof(buf), stdin))
1194 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001195 if (buf[strlen (buf) - 1] == '\n')
1196 buf[strlen (buf) - 1] = '\0';
1197 if (!buf[0])
1198 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +00001199 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001200 if (*tmp)
1201 com_err(com, 0, "Bad value - %s", buf);
1202 else
1203 *val = v;
1204}
1205
1206
1207void do_modify_inode(int argc, char *argv[])
1208{
1209 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001210 ext2_ino_t inode_num;
1211 int i;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001212 unsigned char *frag, *fsize;
1213 char buf[80];
Theodore Ts'o7380ac92002-03-05 01:57:53 -05001214 int os;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001215 const char *hex_format = "0x%x";
1216 const char *octal_format = "0%o";
1217 const char *decimal_format = "%d";
Takashi Sato8deb80a2006-03-18 21:43:46 -05001218 const char *unsignedlong_format = "%lu";
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001219
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001220 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001221 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001222
Theodore Ts'o7380ac92002-03-05 01:57:53 -05001223 os = current_fs->super->s_creator_os;
1224
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001225 if (debugfs_read_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001226 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001227
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001228 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
1229 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
1230 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
Takashi Sato8deb80a2006-03-18 21:43:46 -05001231 modify_u32(argv[0], "Size", unsignedlong_format, &inode.i_size);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001232 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
1233 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
1234 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
1235 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
1236 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
Theodore Ts'o5d171192006-11-11 06:32:03 -05001237 if (os == EXT2_OS_LINUX)
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001238 modify_u16(argv[0], "Block count high", unsignedlong_format,
Theodore Ts'o5d171192006-11-11 06:32:03 -05001239 &inode.osd2.linux2.l_i_blocks_hi);
Takashi Sato8deb80a2006-03-18 21:43:46 -05001240 modify_u32(argv[0], "Block count", unsignedlong_format, &inode.i_blocks);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001241 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
Theodore Ts'o3db93052000-12-30 20:26:31 +00001242 modify_u32(argv[0], "Generation", hex_format, &inode.i_generation);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001243#if 0
1244 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
1245#endif
1246 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +00001247 if (LINUX_S_ISDIR(inode.i_mode))
1248 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
1249 else
1250 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +00001251
Theodore Ts'o5d171192006-11-11 06:32:03 -05001252 if (os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +00001253 modify_u32(argv[0], "Translator Block",
1254 decimal_format, &inode.osd1.hurd1.h_i_translator);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001255
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001256 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001257 switch (os) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001258 case EXT2_OS_HURD:
1259 frag = &inode.osd2.hurd2.h_i_frag;
1260 fsize = &inode.osd2.hurd2.h_i_fsize;
1261 break;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001262 default:
1263 frag = fsize = 0;
1264 }
1265 if (frag)
1266 modify_u8(argv[0], "Fragment number", decimal_format, frag);
1267 if (fsize)
1268 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
1269
Theodore Ts'o3839e651997-04-26 13:21:57 +00001270 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
1271 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001272 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001273 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001274 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001275 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001276 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +00001277 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001278 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +00001279 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001280 if (debugfs_write_inode(inode_num, &inode, argv[1]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001281 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001282}
JP Abgralle0ed7402014-03-19 19:08:39 -07001283#endif /* READ_ONLY */
Theodore Ts'o3839e651997-04-26 13:21:57 +00001284
Theodore Ts'o3839e651997-04-26 13:21:57 +00001285void do_change_working_dir(int argc, char *argv[])
1286{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001287 ext2_ino_t inode;
1288 int retval;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001289
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001290 if (common_inode_args_process(argc, argv, &inode, 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291 return;
1292
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001293 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001294 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001295 com_err(argv[1], retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001296 return;
1297 }
1298 cwd = inode;
1299 return;
1300}
1301
Theodore Ts'o3839e651997-04-26 13:21:57 +00001302void do_print_working_directory(int argc, char *argv[])
1303{
1304 int retval;
1305 char *pathname = NULL;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001306
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001307 if (common_args_process(argc, argv, 1, 1,
1308 "print_working_directory", "", 0))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001309 return;
1310
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001311 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001312 if (retval) {
1313 com_err(argv[0], retval,
1314 "while trying to get pathname of cwd");
1315 }
Brian Behlendorf971fe052007-03-29 00:32:23 -04001316 printf("[pwd] INODE: %6u PATH: %s\n",
1317 cwd, pathname ? pathname : "NULL");
1318 if (pathname) {
1319 free(pathname);
1320 pathname = NULL;
1321 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001322 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001323 if (retval) {
1324 com_err(argv[0], retval,
1325 "while trying to get pathname of root");
1326 }
Brian Behlendorf971fe052007-03-29 00:32:23 -04001327 printf("[root] INODE: %6u PATH: %s\n",
1328 root, pathname ? pathname : "NULL");
1329 if (pathname) {
1330 free(pathname);
1331 pathname = NULL;
1332 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001333 return;
1334}
1335
JP Abgralle0ed7402014-03-19 19:08:39 -07001336#ifndef READ_ONLY
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001337static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001338{
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001339 ext2_ino_t ino;
1340 struct ext2_inode inode;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001341 int retval;
1342 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001343 char *dest, *cp, *base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001344
1345 /*
1346 * Get the source inode
1347 */
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001348 ino = string_to_inode(sourcename);
1349 if (!ino)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001350 return;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001351 base_name = strrchr(sourcename, '/');
1352 if (base_name)
1353 base_name++;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001354 else
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001355 base_name = sourcename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001356 /*
1357 * Figure out the destination. First see if it exists and is
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001358 * a directory.
Theodore Ts'o3839e651997-04-26 13:21:57 +00001359 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001360 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001361 dest = base_name;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001362 else {
1363 /*
1364 * OK, it doesn't exist. See if it is
1365 * '<dir>/basename' or 'basename'
1366 */
1367 cp = strrchr(destname, '/');
1368 if (cp) {
1369 *cp = 0;
1370 dir = string_to_inode(destname);
1371 if (!dir)
1372 return;
1373 dest = cp+1;
1374 } else {
1375 dir = cwd;
1376 dest = destname;
1377 }
1378 }
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001379
1380 if (debugfs_read_inode(ino, &inode, sourcename))
1381 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001382
1383 retval = ext2fs_link(current_fs, dir, dest, ino,
Theodore Ts'oabdf84f2004-03-20 16:54:15 -05001384 ext2_file_type(inode.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001385 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001386 com_err("make_link", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001387 return;
1388}
1389
1390
1391void do_link(int argc, char *argv[])
1392{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001393 if (common_args_process(argc, argv, 3, 3, "link",
1394 "<source file> <dest_name>", CHECK_FS_RW))
1395 return;
1396
1397 make_link(argv[1], argv[2]);
1398}
1399
JP Abgralle0ed7402014-03-19 19:08:39 -07001400static int mark_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1401 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1402 blk64_t ref_block EXT2FS_ATTR((unused)),
1403 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -05001404 void *private EXT2FS_ATTR((unused)))
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001405{
JP Abgralle0ed7402014-03-19 19:08:39 -07001406 blk64_t block;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001407
1408 block = *blocknr;
JP Abgralle0ed7402014-03-19 19:08:39 -07001409 ext2fs_block_alloc_stats2(fs, block, +1);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001410 return 0;
1411}
1412
1413void do_undel(int argc, char *argv[])
1414{
1415 ext2_ino_t ino;
1416 struct ext2_inode inode;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001417
Theodore Ts'ob026d532008-01-01 11:37:20 -05001418 if (common_args_process(argc, argv, 2, 3, "undelete",
1419 "<inode_num> [dest_name]",
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001420 CHECK_FS_RW | CHECK_FS_BITMAPS))
1421 return;
1422
1423 ino = string_to_inode(argv[1]);
1424 if (!ino)
1425 return;
1426
1427 if (debugfs_read_inode(ino, &inode, argv[1]))
1428 return;
1429
JP Abgralle0ed7402014-03-19 19:08:39 -07001430 if (ext2fs_test_inode_bitmap2(current_fs->inode_map, ino)) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001431 com_err(argv[1], 0, "Inode is not marked as deleted");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001432 return;
1433 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001434
1435 /*
1436 * XXX this function doesn't handle changing the links count on the
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001437 * parent directory when undeleting a directory.
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001438 */
1439 inode.i_links_count = LINUX_S_ISDIR(inode.i_mode) ? 2 : 1;
1440 inode.i_dtime = 0;
1441
1442 if (debugfs_write_inode(ino, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001443 return;
1444
JP Abgralle0ed7402014-03-19 19:08:39 -07001445 ext2fs_block_iterate3(current_fs, ino, BLOCK_FLAG_READ_ONLY, NULL,
1446 mark_blocks_proc, NULL);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001447
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001448 ext2fs_inode_alloc_stats2(current_fs, ino, +1, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001449
Theodore Ts'ob026d532008-01-01 11:37:20 -05001450 if (argc > 2)
1451 make_link(argv[1], argv[2]);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001452}
1453
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001454static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001455{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001456 int retval;
1457 ext2_ino_t dir;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001458 char *base_name;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001459
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001460 base_name = strrchr(filename, '/');
1461 if (base_name) {
1462 *base_name++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +00001463 dir = string_to_inode(filename);
1464 if (!dir)
1465 return;
1466 } else {
1467 dir = cwd;
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001468 base_name = filename;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001469 }
Theodore Ts'od4e0b1c2007-08-03 18:56:01 -04001470 retval = ext2fs_unlink(current_fs, dir, base_name, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001471 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001472 com_err("unlink_file_by_name", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001473 return;
1474}
1475
1476void do_unlink(int argc, char *argv[])
1477{
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001478 if (common_args_process(argc, argv, 2, 2, "link",
1479 "<pathname>", CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001480 return;
1481
1482 unlink_file_by_name(argv[1]);
1483}
JP Abgralle0ed7402014-03-19 19:08:39 -07001484#endif /* READ_ONLY */
Theodore Ts'o3839e651997-04-26 13:21:57 +00001485
1486void do_find_free_block(int argc, char *argv[])
1487{
JP Abgralle0ed7402014-03-19 19:08:39 -07001488 blk64_t free_blk, goal, first_free = 0;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001489 int count;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001490 errcode_t retval;
1491 char *tmp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001492
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001493 if ((argc > 3) || (argc==2 && *argv[1] == '?')) {
1494 com_err(argv[0], 0, "Usage: find_free_block [count [goal]]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001495 return;
1496 }
1497 if (check_fs_open(argv[0]))
1498 return;
1499
1500 if (argc > 1) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001501 count = strtol(argv[1],&tmp,0);
1502 if (*tmp) {
1503 com_err(argv[0], 0, "Bad count - %s", argv[1]);
1504 return;
1505 }
1506 } else
1507 count = 1;
1508
1509 if (argc > 2) {
1510 goal = strtol(argv[2], &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001511 if (*tmp) {
1512 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
1513 return;
1514 }
1515 }
1516 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001517 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001518
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001519 printf("Free blocks found: ");
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001520 free_blk = goal - 1;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001521 while (count-- > 0) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001522 retval = ext2fs_new_block2(current_fs, free_blk + 1, 0,
1523 &free_blk);
Theodore Ts'o5aae7c22008-02-27 13:38:56 -05001524 if (first_free) {
1525 if (first_free == free_blk)
1526 break;
1527 } else
1528 first_free = free_blk;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001529 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001530 com_err("ext2fs_new_block", retval, 0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001531 return;
1532 } else
JP Abgralle0ed7402014-03-19 19:08:39 -07001533 printf("%llu ", free_blk);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001534 }
1535 printf("\n");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001536}
1537
1538void do_find_free_inode(int argc, char *argv[])
1539{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001540 ext2_ino_t free_inode, dir;
1541 int mode;
1542 int retval;
1543 char *tmp;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001544
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001545 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
1546 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001547 return;
1548 }
1549 if (check_fs_open(argv[0]))
1550 return;
1551
1552 if (argc > 1) {
1553 dir = strtol(argv[1], &tmp, 0);
1554 if (*tmp) {
1555 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1556 return;
1557 }
1558 }
1559 else
1560 dir = root;
1561 if (argc > 2) {
1562 mode = strtol(argv[2], &tmp, 0);
1563 if (*tmp) {
1564 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1565 return;
1566 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001567 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001568 mode = 010755;
1569
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001570 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001571 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001572 com_err("ext2fs_new_inode", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001573 else
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001574 printf("Free inode found: %u\n", free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001575}
1576
JP Abgralle0ed7402014-03-19 19:08:39 -07001577#ifndef READ_ONLY
1578static errcode_t copy_file(int fd, ext2_ino_t newfile, int bufsize, int make_holes)
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001579{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001580 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001581 errcode_t retval;
Theodore Ts'ob7846402001-06-03 23:27:56 +00001582 int got;
1583 unsigned int written;
JP Abgralle0ed7402014-03-19 19:08:39 -07001584 char *buf;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001585 char *ptr;
JP Abgralle0ed7402014-03-19 19:08:39 -07001586 char *zero_buf;
1587 int cmp;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001588
Theodore Ts'o5a513841997-10-25 22:41:14 +00001589 retval = ext2fs_file_open(current_fs, newfile,
1590 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001591 if (retval)
1592 return retval;
1593
JP Abgralle0ed7402014-03-19 19:08:39 -07001594 retval = ext2fs_get_mem(bufsize, &buf);
1595 if (retval) {
1596 com_err("copy_file", retval, "can't allocate buffer\n");
1597 return retval;
1598 }
1599
1600 /* This is used for checking whether the whole block is zero */
1601 retval = ext2fs_get_memzero(bufsize, &zero_buf);
1602 if (retval) {
1603 com_err("copy_file", retval, "can't allocate buffer\n");
1604 ext2fs_free_mem(&buf);
1605 return retval;
1606 }
1607
Theodore Ts'o5a513841997-10-25 22:41:14 +00001608 while (1) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001609 got = read(fd, buf, bufsize);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001610 if (got == 0)
1611 break;
1612 if (got < 0) {
1613 retval = errno;
1614 goto fail;
1615 }
1616 ptr = buf;
JP Abgralle0ed7402014-03-19 19:08:39 -07001617
1618 /* Sparse copy */
1619 if (make_holes) {
1620 /* Check whether all is zero */
1621 cmp = memcmp(ptr, zero_buf, got);
1622 if (cmp == 0) {
1623 /* The whole block is zero, make a hole */
1624 retval = ext2fs_file_lseek(e2_file, got, EXT2_SEEK_CUR, NULL);
1625 if (retval)
1626 goto fail;
1627 got = 0;
1628 }
1629 }
1630
1631 /* Normal copy */
Theodore Ts'o5a513841997-10-25 22:41:14 +00001632 while (got > 0) {
1633 retval = ext2fs_file_write(e2_file, ptr,
1634 got, &written);
1635 if (retval)
1636 goto fail;
1637
1638 got -= written;
1639 ptr += written;
1640 }
1641 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001642 ext2fs_free_mem(&buf);
1643 ext2fs_free_mem(&zero_buf);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001644 retval = ext2fs_file_close(e2_file);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001645 return retval;
1646
1647fail:
JP Abgralle0ed7402014-03-19 19:08:39 -07001648 ext2fs_free_mem(&buf);
1649 ext2fs_free_mem(&zero_buf);
Theodore Ts'o5a513841997-10-25 22:41:14 +00001650 (void) ext2fs_file_close(e2_file);
1651 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001652}
1653
Theodore Ts'o5a513841997-10-25 22:41:14 +00001654
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001655void do_write(int argc, char *argv[])
1656{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001657 int fd;
1658 struct stat statbuf;
1659 ext2_ino_t newfile;
1660 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001661 struct ext2_inode inode;
JP Abgralle0ed7402014-03-19 19:08:39 -07001662 int bufsize = IO_BUFSIZE;
1663 int make_holes = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001664
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001665 if (common_args_process(argc, argv, 3, 3, "write",
1666 "<native file> <new file>", CHECK_FS_RW))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001667 return;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001668
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001669 fd = open(argv[1], O_RDONLY);
1670 if (fd < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001671 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001672 return;
1673 }
1674 if (fstat(fd, &statbuf) < 0) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001675 com_err(argv[1], errno, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001676 close(fd);
1677 return;
1678 }
1679
Theodore Ts'o1dd090f2002-10-31 11:53:49 -05001680 retval = ext2fs_namei(current_fs, root, cwd, argv[2], &newfile);
1681 if (retval == 0) {
1682 com_err(argv[0], 0, "The file '%s' already exists\n", argv[2]);
1683 close(fd);
1684 return;
1685 }
1686
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001687 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001688 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001689 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001690 close(fd);
1691 return;
1692 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001693 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001694 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1695 EXT2_FT_REG_FILE);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001696 if (retval == EXT2_ET_DIR_NO_SPACE) {
1697 retval = ext2fs_expand_dir(current_fs, cwd);
1698 if (retval) {
1699 com_err(argv[0], retval, "while expanding directory");
Manish Katiyar42621622008-08-23 21:41:25 +05301700 close(fd);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001701 return;
1702 }
1703 retval = ext2fs_link(current_fs, cwd, argv[2], newfile,
1704 EXT2_FT_REG_FILE);
1705 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001706 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001707 com_err(argv[2], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001708 close(fd);
1709 return;
1710 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001711 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001712 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001713 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001714 memset(&inode, 0, sizeof(inode));
Theodore Ts'o04df4912003-12-07 16:31:45 -05001715 inode.i_mode = (statbuf.st_mode & ~LINUX_S_IFMT) | LINUX_S_IFREG;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001716 inode.i_atime = inode.i_ctime = inode.i_mtime =
Theodore Ts'o4efae602005-09-24 21:56:38 -04001717 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001718 inode.i_links_count = 1;
1719 inode.i_size = statbuf.st_size;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001720 if (current_fs->super->s_feature_incompat &
JP Abgralle0ed7402014-03-19 19:08:39 -07001721 EXT3_FEATURE_INCOMPAT_EXTENTS) {
1722 int i;
1723 struct ext3_extent_header *eh;
1724
1725 eh = (struct ext3_extent_header *) &inode.i_block[0];
1726 eh->eh_depth = 0;
1727 eh->eh_entries = 0;
1728 eh->eh_magic = ext2fs_cpu_to_le16(EXT3_EXT_MAGIC);
1729 i = (sizeof(inode.i_block) - sizeof(*eh)) /
1730 sizeof(struct ext3_extent);
1731 eh->eh_max = ext2fs_cpu_to_le16(i);
Theodore Ts'oeaf8fed2008-08-27 18:38:47 -04001732 inode.i_flags |= EXT4_EXTENTS_FL;
JP Abgralle0ed7402014-03-19 19:08:39 -07001733 }
Theodore Ts'o030970e2005-03-20 20:05:22 -05001734 if (debugfs_write_new_inode(newfile, &inode, argv[0])) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001735 close(fd);
1736 return;
1737 }
1738 if (LINUX_S_ISREG(inode.i_mode)) {
JP Abgralle0ed7402014-03-19 19:08:39 -07001739 if (statbuf.st_blocks < statbuf.st_size / S_BLKSIZE) {
1740 make_holes = 1;
1741 /*
1742 * Use I/O blocksize as buffer size when
1743 * copying sparse files.
1744 */
1745 bufsize = statbuf.st_blksize;
1746 }
1747 retval = copy_file(fd, newfile, bufsize, make_holes);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001748 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001749 com_err("copy_file", retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001750 }
1751 close(fd);
1752}
1753
1754void do_mknod(int argc, char *argv[])
1755{
Theodore Ts'o54434922003-12-07 01:28:50 -05001756 unsigned long mode, major, minor;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001757 ext2_ino_t newfile;
1758 errcode_t retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001759 struct ext2_inode inode;
Theodore Ts'o54434922003-12-07 01:28:50 -05001760 int filetype, nr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001761
1762 if (check_fs_open(argv[0]))
1763 return;
1764 if (argc < 3 || argv[2][1]) {
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001765 usage:
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001766 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1767 return;
1768 }
1769 mode = minor = major = 0;
1770 switch (argv[2][0]) {
1771 case 'p':
1772 mode = LINUX_S_IFIFO;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001773 filetype = EXT2_FT_FIFO;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001774 nr = 3;
1775 break;
1776 case 'c':
1777 mode = LINUX_S_IFCHR;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001778 filetype = EXT2_FT_CHRDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001779 nr = 5;
1780 break;
1781 case 'b':
1782 mode = LINUX_S_IFBLK;
Theodore Ts'o085cb192001-05-09 06:09:12 +00001783 filetype = EXT2_FT_BLKDEV;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001784 nr = 5;
1785 break;
1786 default:
Theodore Ts'o085cb192001-05-09 06:09:12 +00001787 filetype = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001788 nr = 0;
1789 }
1790 if (nr == 5) {
1791 major = strtoul(argv[3], argv+3, 0);
1792 minor = strtoul(argv[4], argv+4, 0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001793 if (major > 65535 || minor > 65535 || argv[3][0] || argv[4][0])
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001794 nr = 0;
1795 }
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001796 if (argc != nr)
1797 goto usage;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001798 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001799 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001800 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001801 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001802 com_err(argv[0], retval, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001803 return;
1804 }
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001805 printf("Allocated inode: %u\n", newfile);
Theodore Ts'o085cb192001-05-09 06:09:12 +00001806 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, filetype);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001807 if (retval == EXT2_ET_DIR_NO_SPACE) {
1808 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001809 if (retval) {
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001810 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001811 return;
1812 }
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001813 retval = ext2fs_link(current_fs, cwd, argv[1], newfile,
1814 filetype);
1815 }
1816 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001817 com_err(argv[1], retval, 0);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001818 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001819 }
JP Abgralle0ed7402014-03-19 19:08:39 -07001820 if (ext2fs_test_inode_bitmap2(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001821 com_err(argv[0], 0, "Warning: inode already set");
JP Abgralle0ed7402014-03-19 19:08:39 -07001822 ext2fs_inode_alloc_stats2(current_fs, newfile, +1, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001823 memset(&inode, 0, sizeof(inode));
1824 inode.i_mode = mode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001825 inode.i_atime = inode.i_ctime = inode.i_mtime =
Theodore Ts'o4efae602005-09-24 21:56:38 -04001826 current_fs->now ? current_fs->now : time(0);
Theodore Ts'o2d107692004-02-23 22:30:54 -05001827 if ((major < 256) && (minor < 256)) {
1828 inode.i_block[0] = major*256+minor;
1829 inode.i_block[1] = 0;
1830 } else {
1831 inode.i_block[0] = 0;
1832 inode.i_block[1] = (minor & 0xff) | (major << 8) | ((minor & ~0xff) << 12);
1833 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001834 inode.i_links_count = 1;
Theodore Ts'o030970e2005-03-20 20:05:22 -05001835 if (debugfs_write_new_inode(newfile, &inode, argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001836 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001837}
1838
Theodore Ts'o3839e651997-04-26 13:21:57 +00001839void do_mkdir(int argc, char *argv[])
1840{
1841 char *cp;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001842 ext2_ino_t parent;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001843 char *name;
1844 errcode_t retval;
1845
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001846 if (common_args_process(argc, argv, 2, 2, "mkdir",
1847 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001848 return;
1849
Theodore Ts'o3839e651997-04-26 13:21:57 +00001850 cp = strrchr(argv[1], '/');
1851 if (cp) {
1852 *cp = 0;
1853 parent = string_to_inode(argv[1]);
1854 if (!parent) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001855 com_err(argv[1], ENOENT, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001856 return;
1857 }
1858 name = cp+1;
1859 } else {
1860 parent = cwd;
1861 name = argv[1];
1862 }
1863
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001864try_again:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001865 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001866 if (retval == EXT2_ET_DIR_NO_SPACE) {
1867 retval = ext2fs_expand_dir(current_fs, parent);
1868 if (retval) {
Manish Katiyar73b05422008-08-23 22:28:05 +05301869 com_err(argv[0], retval, "while expanding directory");
Theodore Ts'o2d9f0802003-12-11 11:54:48 -05001870 return;
1871 }
1872 goto try_again;
1873 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001874 if (retval) {
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05001875 com_err("ext2fs_mkdir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001876 return;
1877 }
1878
1879}
1880
JP Abgralle0ed7402014-03-19 19:08:39 -07001881static int release_blocks_proc(ext2_filsys fs, blk64_t *blocknr,
1882 e2_blkcnt_t blockcnt EXT2FS_ATTR((unused)),
1883 blk64_t ref_block EXT2FS_ATTR((unused)),
1884 int ref_offset EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -05001885 void *private EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001886{
JP Abgralle0ed7402014-03-19 19:08:39 -07001887 blk64_t block;
Theodore Ts'o34436892001-12-22 13:06:02 -05001888
1889 block = *blocknr;
JP Abgralle0ed7402014-03-19 19:08:39 -07001890 ext2fs_block_alloc_stats2(fs, block, -1);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001891 return 0;
1892}
1893
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001894static void kill_file_by_inode(ext2_ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001895{
1896 struct ext2_inode inode_buf;
1897
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001898 if (debugfs_read_inode(inode, &inode_buf, 0))
1899 return;
Theodore Ts'o4efae602005-09-24 21:56:38 -04001900 inode_buf.i_dtime = current_fs->now ? current_fs->now : time(0);
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001901 if (debugfs_write_inode(inode, &inode_buf, 0))
1902 return;
JP Abgralle0ed7402014-03-19 19:08:39 -07001903 if (!ext2fs_inode_has_valid_blocks2(current_fs, &inode_buf))
Theodore Ts'o9c92d842004-11-19 14:39:14 -05001904 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001905
JP Abgralle0ed7402014-03-19 19:08:39 -07001906 ext2fs_block_iterate3(current_fs, inode, BLOCK_FLAG_READ_ONLY, NULL,
1907 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001908 printf("\n");
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001909 ext2fs_inode_alloc_stats2(current_fs, inode, -1,
1910 LINUX_S_ISDIR(inode_buf.i_mode));
Theodore Ts'o3839e651997-04-26 13:21:57 +00001911}
1912
1913
1914void do_kill_file(int argc, char *argv[])
1915{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001916 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001917
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001918 if (common_inode_args_process(argc, argv, &inode_num, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001919 return;
1920
Theodore Ts'o3839e651997-04-26 13:21:57 +00001921 kill_file_by_inode(inode_num);
1922}
1923
1924void do_rm(int argc, char *argv[])
1925{
1926 int retval;
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00001927 ext2_ino_t inode_num;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001928 struct ext2_inode inode;
1929
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001930 if (common_args_process(argc, argv, 2, 2, "rm",
1931 "<filename>", CHECK_FS_RW))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001932 return;
1933
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001934 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001935 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001936 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001937 return;
1938 }
1939
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001940 if (debugfs_read_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001941 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001942
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001943 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001944 com_err(argv[0], 0, "file is a directory");
1945 return;
1946 }
1947
1948 --inode.i_links_count;
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05001949 if (debugfs_write_inode(inode_num, &inode, argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +00001950 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001951
1952 unlink_file_by_name(argv[1]);
1953 if (inode.i_links_count == 0)
1954 kill_file_by_inode(inode_num);
1955}
1956
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001957struct rd_struct {
1958 ext2_ino_t parent;
1959 int empty;
1960};
1961
Theodore Ts'o54434922003-12-07 01:28:50 -05001962static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1963 int entry EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001964 struct ext2_dir_entry *dirent,
Theodore Ts'o54434922003-12-07 01:28:50 -05001965 int offset EXT2FS_ATTR((unused)),
1966 int blocksize EXT2FS_ATTR((unused)),
1967 char *buf EXT2FS_ATTR((unused)),
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001968 void *private)
1969{
1970 struct rd_struct *rds = (struct rd_struct *) private;
1971
1972 if (dirent->inode == 0)
1973 return 0;
1974 if (((dirent->name_len&0xFF) == 1) && (dirent->name[0] == '.'))
1975 return 0;
1976 if (((dirent->name_len&0xFF) == 2) && (dirent->name[0] == '.') &&
1977 (dirent->name[1] == '.')) {
1978 rds->parent = dirent->inode;
1979 return 0;
1980 }
1981 rds->empty = 0;
1982 return 0;
1983}
Theodore Ts'oefc6f622008-08-27 23:07:54 -04001984
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04001985void do_rmdir(int argc, char *argv[])
1986{
1987 int retval;
1988 ext2_ino_t inode_num;
1989 struct ext2_inode inode;
1990 struct rd_struct rds;
1991
1992 if (common_args_process(argc, argv, 2, 2, "rmdir",
1993 "<filename>", CHECK_FS_RW))
1994 return;
1995
1996 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
1997 if (retval) {
1998 com_err(argv[0], retval, "while trying to resolve filename");
1999 return;
2000 }
2001
2002 if (debugfs_read_inode(inode_num, &inode, argv[0]))
2003 return;
2004
2005 if (!LINUX_S_ISDIR(inode.i_mode)) {
2006 com_err(argv[0], 0, "file is not a directory");
2007 return;
2008 }
2009
2010 rds.parent = 0;
2011 rds.empty = 1;
2012
2013 retval = ext2fs_dir_iterate2(current_fs, inode_num, 0,
2014 0, rmdir_proc, &rds);
2015 if (retval) {
2016 com_err(argv[0], retval, "while iterating over directory");
2017 return;
2018 }
2019 if (rds.empty == 0) {
2020 com_err(argv[0], 0, "directory not empty");
2021 return;
2022 }
2023
2024 inode.i_links_count = 0;
2025 if (debugfs_write_inode(inode_num, &inode, argv[0]))
2026 return;
2027
2028 unlink_file_by_name(argv[1]);
2029 kill_file_by_inode(inode_num);
2030
2031 if (rds.parent) {
2032 if (debugfs_read_inode(rds.parent, &inode, argv[0]))
2033 return;
2034 if (inode.i_links_count > 1)
2035 inode.i_links_count--;
2036 if (debugfs_write_inode(rds.parent, &inode, argv[0]))
2037 return;
2038 }
2039}
JP Abgralle0ed7402014-03-19 19:08:39 -07002040#endif /* READ_ONLY */
Theodore Ts'od7f64ae2002-07-09 01:27:05 -04002041
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002042void do_show_debugfs_params(int argc EXT2FS_ATTR((unused)),
Theodore Ts'o54434922003-12-07 01:28:50 -05002043 char *argv[] EXT2FS_ATTR((unused)))
Theodore Ts'o3839e651997-04-26 13:21:57 +00002044{
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002045 if (current_fs)
JP Abgralle0ed7402014-03-19 19:08:39 -07002046 printf("Open mode: read-%s\n",
2047 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
2048 printf("Filesystem in use: %s\n",
2049 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00002050}
2051
JP Abgralle0ed7402014-03-19 19:08:39 -07002052#ifndef READ_ONLY
Theodore Ts'o3839e651997-04-26 13:21:57 +00002053void do_expand_dir(int argc, char *argv[])
2054{
Theodore Ts'ob044c2e2001-01-11 15:26:39 +00002055 ext2_ino_t inode;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002056 int retval;
2057
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05002058 if (common_inode_args_process(argc, argv, &inode, CHECK_FS_RW))
Theodore Ts'o3839e651997-04-26 13:21:57 +00002059 return;
2060
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002061 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002062 if (retval)
Theodore Ts'o9b9a7802005-12-10 21:50:30 -05002063 com_err("ext2fs_expand_dir", retval, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002064 return;
2065}
2066
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00002067void do_features(int argc, char *argv[])
2068{
2069 int i;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002070
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00002071 if (check_fs_open(argv[0]))
2072 return;
2073
2074 if ((argc != 1) && check_fs_read_write(argv[0]))
2075 return;
2076 for (i=1; i < argc; i++) {
2077 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00002078 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00002079 com_err(argv[0], 0, "Unknown feature: %s\n",
2080 argv[i]);
2081 else
2082 ext2fs_mark_super_dirty(current_fs);
2083 }
Theodore Ts'o5dd8f962001-01-01 15:51:50 +00002084 print_features(current_fs->super, stdout);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00002085}
JP Abgralle0ed7402014-03-19 19:08:39 -07002086#endif /* READ_ONLY */
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00002087
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002088void do_bmap(int argc, char *argv[])
2089{
2090 ext2_ino_t ino;
JP Abgralle0ed7402014-03-19 19:08:39 -07002091 blk64_t blk, pblk;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002092 int err;
2093 errcode_t errcode;
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002094
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002095 if (common_args_process(argc, argv, 3, 3, argv[0],
2096 "<file> logical_blk", 0))
2097 return;
2098
2099 ino = string_to_inode(argv[1]);
Theodore Ts'obecf36f2003-05-05 11:35:04 -04002100 if (!ino)
2101 return;
JP Abgralle0ed7402014-03-19 19:08:39 -07002102 err = strtoblk(argv[0], argv[2], &blk);
2103 if (err)
2104 return;
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002105
JP Abgralle0ed7402014-03-19 19:08:39 -07002106 errcode = ext2fs_bmap2(current_fs, ino, 0, 0, 0, blk, 0, &pblk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002107 if (errcode) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002108 com_err(argv[0], errcode,
2109 "while mapping logical block %llu\n", blk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002110 return;
2111 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002112 printf("%llu\n", pblk);
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002113}
2114
Theodore Ts'obecf36f2003-05-05 11:35:04 -04002115void do_imap(int argc, char *argv[])
2116{
2117 ext2_ino_t ino;
2118 unsigned long group, block, block_nr, offset;
2119
2120 if (common_args_process(argc, argv, 2, 2, argv[0],
2121 "<file>", 0))
2122 return;
2123 ino = string_to_inode(argv[1]);
2124 if (!ino)
Theodore Ts'o88494bb2003-05-13 23:03:43 -04002125 return;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04002126
2127 group = (ino - 1) / EXT2_INODES_PER_GROUP(current_fs->super);
2128 offset = ((ino - 1) % EXT2_INODES_PER_GROUP(current_fs->super)) *
2129 EXT2_INODE_SIZE(current_fs->super);
2130 block = offset >> EXT2_BLOCK_SIZE_BITS(current_fs->super);
JP Abgralle0ed7402014-03-19 19:08:39 -07002131 if (!ext2fs_inode_table_loc(current_fs, (unsigned)group)) {
Theodore Ts'o54434922003-12-07 01:28:50 -05002132 com_err(argv[0], 0, "Inode table for group %lu is missing\n",
Theodore Ts'obecf36f2003-05-05 11:35:04 -04002133 group);
2134 return;
2135 }
JP Abgralle0ed7402014-03-19 19:08:39 -07002136 block_nr = ext2fs_inode_table_loc(current_fs, (unsigned)group) +
Theodore Ts'obecf36f2003-05-05 11:35:04 -04002137 block;
2138 offset &= (EXT2_BLOCK_SIZE(current_fs->super) - 1);
2139
Theodore Ts'o48e6e812003-07-06 00:36:48 -04002140 printf("Inode %d is part of block group %lu\n"
2141 "\tlocated at block %lu, offset 0x%04lx\n", ino, group,
Theodore Ts'obecf36f2003-05-05 11:35:04 -04002142 block_nr, offset);
2143
2144}
2145
JP Abgralle0ed7402014-03-19 19:08:39 -07002146#ifndef READ_ONLY
Theodore Ts'o4efae602005-09-24 21:56:38 -04002147void do_set_current_time(int argc, char *argv[])
2148{
Theodore Ts'o4efae602005-09-24 21:56:38 -04002149 time_t now;
Theodore Ts'obecf36f2003-05-05 11:35:04 -04002150
Theodore Ts'o4efae602005-09-24 21:56:38 -04002151 if (common_args_process(argc, argv, 2, 2, argv[0],
2152 "<time>", 0))
2153 return;
2154
2155 now = string_to_time(argv[1]);
2156 if (now == ((time_t) -1)) {
2157 com_err(argv[0], 0, "Couldn't parse argument as a time: %s\n",
2158 argv[1]);
2159 return;
2160
2161 } else {
2162 printf("Setting current time to %s\n", time_to_string(now));
2163 current_fs->now = now;
2164 }
2165}
JP Abgralle0ed7402014-03-19 19:08:39 -07002166#endif /* READ_ONLY */
Theodore Ts'ob38cd282002-05-11 22:13:20 -04002167
Andreas Dilger03efde82008-08-23 21:42:46 -06002168static int find_supp_feature(__u32 *supp, int feature_type, char *name)
2169{
2170 int compat, bit, ret;
2171 unsigned int feature_mask;
2172
2173 if (name) {
2174 if (feature_type == E2P_FS_FEATURE)
2175 ret = e2p_string2feature(name, &compat, &feature_mask);
2176 else
2177 ret = e2p_jrnl_string2feature(name, &compat,
2178 &feature_mask);
2179 if (ret)
2180 return ret;
2181
2182 if (!(supp[compat] & feature_mask))
2183 return 1;
2184 } else {
2185 for (compat = 0; compat < 3; compat++) {
2186 for (bit = 0, feature_mask = 1; bit < 32;
2187 bit++, feature_mask <<= 1) {
2188 if (supp[compat] & feature_mask) {
2189 if (feature_type == E2P_FS_FEATURE)
2190 fprintf(stdout, " %s",
2191 e2p_feature2string(compat,
2192 feature_mask));
2193 else
2194 fprintf(stdout, " %s",
2195 e2p_jrnl_feature2string(compat,
2196 feature_mask));
2197 }
2198 }
2199 }
2200 fprintf(stdout, "\n");
2201 }
2202
2203 return 0;
2204}
2205
2206void do_supported_features(int argc, char *argv[])
2207{
Theodore Ts'o42080a82009-07-11 23:23:16 -04002208 int ret;
Andreas Dilger03efde82008-08-23 21:42:46 -06002209 __u32 supp[3] = { EXT2_LIB_FEATURE_COMPAT_SUPP,
2210 EXT2_LIB_FEATURE_INCOMPAT_SUPP,
2211 EXT2_LIB_FEATURE_RO_COMPAT_SUPP };
2212 __u32 jrnl_supp[3] = { JFS_KNOWN_COMPAT_FEATURES,
2213 JFS_KNOWN_INCOMPAT_FEATURES,
2214 JFS_KNOWN_ROCOMPAT_FEATURES };
2215
2216 if (argc > 1) {
2217 ret = find_supp_feature(supp, E2P_FS_FEATURE, argv[1]);
2218 if (ret) {
2219 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE,
2220 argv[1]);
2221 }
2222 if (ret)
2223 com_err(argv[0], 0, "Unknown feature: %s\n", argv[1]);
2224 else
2225 fprintf(stdout, "Supported feature: %s\n", argv[1]);
2226 } else {
2227 fprintf(stdout, "Supported features:");
2228 ret = find_supp_feature(supp, E2P_FS_FEATURE, NULL);
2229 ret = find_supp_feature(jrnl_supp, E2P_JOURNAL_FEATURE, NULL);
2230 }
2231}
2232
JP Abgralle0ed7402014-03-19 19:08:39 -07002233#ifndef READ_ONLY
2234void do_punch(int argc, char *argv[])
2235{
2236 ext2_ino_t ino;
2237 blk64_t start, end;
2238 int err;
2239 errcode_t errcode;
2240
2241 if (common_args_process(argc, argv, 3, 4, argv[0],
2242 "<file> start_blk [end_blk]",
2243 CHECK_FS_RW | CHECK_FS_BITMAPS))
2244 return;
2245
2246 ino = string_to_inode(argv[1]);
2247 if (!ino)
2248 return;
2249 err = strtoblk(argv[0], argv[2], &start);
2250 if (err)
2251 return;
2252 if (argc == 4) {
2253 err = strtoblk(argv[0], argv[3], &end);
2254 if (err)
2255 return;
2256 } else
2257 end = ~0;
2258
2259 errcode = ext2fs_punch(current_fs, ino, 0, 0, start, end);
2260
2261 if (errcode) {
2262 com_err(argv[0], errcode,
2263 "while truncating inode %u from %llu to %llu\n", ino,
2264 (unsigned long long) start, (unsigned long long) end);
2265 return;
2266 }
2267}
2268#endif /* READ_ONLY */
2269
2270void do_symlink(int argc, char *argv[])
2271{
2272 char *cp;
2273 ext2_ino_t parent;
2274 char *name, *target;
2275 errcode_t retval;
2276
2277 if (common_args_process(argc, argv, 3, 3, "symlink",
2278 "<filename> <target>", CHECK_FS_RW))
2279 return;
2280
2281 cp = strrchr(argv[1], '/');
2282 if (cp) {
2283 *cp = 0;
2284 parent = string_to_inode(argv[1]);
2285 if (!parent) {
2286 com_err(argv[1], ENOENT, 0);
2287 return;
2288 }
2289 name = cp+1;
2290 } else {
2291 parent = cwd;
2292 name = argv[1];
2293 }
2294 target = argv[2];
2295
2296try_again:
2297 retval = ext2fs_symlink(current_fs, parent, 0, name, target);
2298 if (retval == EXT2_ET_DIR_NO_SPACE) {
2299 retval = ext2fs_expand_dir(current_fs, parent);
2300 if (retval) {
2301 com_err(argv[0], retval, "while expanding directory");
2302 return;
2303 }
2304 goto try_again;
2305 }
2306 if (retval) {
2307 com_err("ext2fs_symlink", retval, 0);
2308 return;
2309 }
2310
2311}
2312
2313void do_dump_mmp(int argc EXT2FS_ATTR((unused)), char *argv[])
2314{
2315 struct ext2_super_block *sb;
2316 struct mmp_struct *mmp_s;
2317 time_t t;
2318 errcode_t retval = 0;
2319
2320 if (check_fs_open(argv[0]))
2321 return;
2322
2323 sb = current_fs->super;
2324
2325 if (current_fs->mmp_buf == NULL) {
2326 retval = ext2fs_get_mem(current_fs->blocksize,
2327 &current_fs->mmp_buf);
2328 if (retval) {
2329 com_err(argv[0], retval, "allocating MMP buffer.\n");
2330 return;
2331 }
2332 }
2333
2334 mmp_s = current_fs->mmp_buf;
2335
2336 retval = ext2fs_mmp_read(current_fs, current_fs->super->s_mmp_block,
2337 current_fs->mmp_buf);
2338 if (retval) {
2339 com_err(argv[0], retval, "reading MMP block.\n");
2340 return;
2341 }
2342
2343 t = mmp_s->mmp_time;
2344 fprintf(stdout, "block_number: %llu\n", current_fs->super->s_mmp_block);
2345 fprintf(stdout, "update_interval: %d\n",
2346 current_fs->super->s_mmp_update_interval);
2347 fprintf(stdout, "check_interval: %d\n", mmp_s->mmp_check_interval);
2348 fprintf(stdout, "sequence: %08x\n", mmp_s->mmp_seq);
2349 fprintf(stdout, "time: %lld -- %s", mmp_s->mmp_time, ctime(&t));
2350 fprintf(stdout, "node_name: %s\n", mmp_s->mmp_nodename);
2351 fprintf(stdout, "device_name: %s\n", mmp_s->mmp_bdevname);
2352 fprintf(stdout, "magic: 0x%x\n", mmp_s->mmp_magic);
2353}
2354
2355static int source_file(const char *cmd_file, int ss_idx)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002356{
2357 FILE *f;
JP Abgralle0ed7402014-03-19 19:08:39 -07002358 char buf[BUFSIZ];
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002359 char *cp;
2360 int exit_status = 0;
2361 int retval;
2362
2363 if (strcmp(cmd_file, "-") == 0)
2364 f = stdin;
2365 else {
2366 f = fopen(cmd_file, "r");
2367 if (!f) {
2368 perror(cmd_file);
2369 exit(1);
2370 }
2371 }
Theodore Ts'o2a7bfe82008-07-13 15:40:15 -04002372 fflush(stdout);
2373 fflush(stderr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002374 setbuf(stdout, NULL);
2375 setbuf(stderr, NULL);
2376 while (!feof(f)) {
2377 if (fgets(buf, sizeof(buf), f) == NULL)
2378 break;
2379 cp = strchr(buf, '\n');
2380 if (cp)
2381 *cp = 0;
2382 cp = strchr(buf, '\r');
2383 if (cp)
2384 *cp = 0;
2385 printf("debugfs: %s\n", buf);
JP Abgralle0ed7402014-03-19 19:08:39 -07002386 retval = ss_execute_line(ss_idx, buf);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002387 if (retval) {
JP Abgralle0ed7402014-03-19 19:08:39 -07002388 ss_perror(ss_idx, retval, buf);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002389 exit_status++;
2390 }
2391 }
Peng Tao2d7ef232009-09-28 18:51:53 +08002392 if (f != stdin)
2393 fclose(f);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002394 return exit_status;
2395}
2396
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00002397int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00002398{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00002399 int retval;
JP Abgralle0ed7402014-03-19 19:08:39 -07002400 const char *usage =
2401 "Usage: %s [-b blocksize] [-s superblock] [-f cmd_file] "
2402 "[-R request] [-V] ["
2403#ifndef READ_ONLY
2404 "[-w] "
2405#endif
2406 "[-c] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00002407 int c;
JP Abgralle0ed7402014-03-19 19:08:39 -07002408 int open_flags = EXT2_FLAG_SOFTSUPP_FEATURES | EXT2_FLAG_64BITS;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002409 char *request = 0;
2410 int exit_status = 0;
2411 char *cmd_file = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07002412 blk64_t superblock = 0;
2413 blk64_t blocksize = 0;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00002414 int catastrophic = 0;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04002415 char *data_filename = 0;
JP Abgralle0ed7402014-03-19 19:08:39 -07002416#ifdef READ_ONLY
2417 const char *opt_string = "icR:f:b:s:Vd:D";
2418#else
2419 const char *opt_string = "iwcR:f:b:s:Vd:D";
2420#endif
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002421
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04002422 if (debug_prog_name == 0)
JP Abgralle0ed7402014-03-19 19:08:39 -07002423#ifdef READ_ONLY
2424 debug_prog_name = "rdebugfs";
2425#else
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04002426 debug_prog_name = "debugfs";
JP Abgralle0ed7402014-03-19 19:08:39 -07002427#endif
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002428 add_error_table(&et_ext2_error_table);
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04002429 fprintf (stderr, "%s %s (%s)\n", debug_prog_name,
2430 E2FSPROGS_VERSION, E2FSPROGS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002431
JP Abgralle0ed7402014-03-19 19:08:39 -07002432 while ((c = getopt (argc, argv, opt_string)) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00002433 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002434 case 'R':
2435 request = optarg;
2436 break;
2437 case 'f':
2438 cmd_file = optarg;
2439 break;
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04002440 case 'd':
2441 data_filename = optarg;
2442 break;
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00002443 case 'i':
2444 open_flags |= EXT2_FLAG_IMAGE_FILE;
2445 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07002446#ifndef READ_ONLY
Theodore Ts'o3839e651997-04-26 13:21:57 +00002447 case 'w':
Theodore Ts'o59cf7e02001-05-03 15:05:55 +00002448 open_flags |= EXT2_FLAG_RW;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002449 break;
JP Abgralle0ed7402014-03-19 19:08:39 -07002450#endif
Theodore Ts'o0fd68e02010-09-24 10:12:54 -04002451 case 'D':
2452 open_flags |= EXT2_FLAG_DIRECT_IO;
2453 break;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00002454 case 'b':
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002455 blocksize = parse_ulong(optarg, argv[0],
Theodore Ts'oe1018ee2002-01-03 04:55:25 -05002456 "block size", 0);
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00002457 break;
2458 case 's':
JP Abgralle0ed7402014-03-19 19:08:39 -07002459 retval = strtoblk(argv[0], optarg, &superblock);
2460 if (retval)
2461 return 1;
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00002462 break;
2463 case 'c':
2464 catastrophic = 1;
2465 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00002466 case 'V':
2467 /* Print version number and exit */
2468 fprintf(stderr, "\tUsing %s\n",
2469 error_message(EXT2_ET_BASE));
2470 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002471 default:
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04002472 com_err(argv[0], 0, usage, debug_prog_name);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00002473 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002474 }
2475 }
2476 if (optind < argc)
Theodore Ts'o2e8d40d2000-05-27 15:15:40 +00002477 open_filesystem(argv[optind], open_flags,
Theodore Ts'o1ad54a92004-07-28 21:11:48 -04002478 superblock, blocksize, catastrophic,
2479 data_filename);
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002480
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04002481 sci_idx = ss_create_invocation(debug_prog_name, "0.0", (char *) NULL,
Theodore Ts'o3839e651997-04-26 13:21:57 +00002482 &debug_cmds, &retval);
2483 if (retval) {
2484 ss_perror(sci_idx, retval, "creating invocation");
2485 exit(1);
2486 }
Theodore Ts'o3ae497e2003-03-16 06:26:25 -05002487 ss_get_readline(sci_idx);
Theodore Ts'o3839e651997-04-26 13:21:57 +00002488
2489 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
2490 if (retval) {
2491 ss_perror(sci_idx, retval, "adding standard requests");
2492 exit (1);
2493 }
Theodore Ts'o49ce6cb2007-08-31 13:39:23 -04002494 if (extra_cmds)
2495 ss_add_request_table (sci_idx, extra_cmds, 1, &retval);
2496 if (retval) {
2497 ss_perror(sci_idx, retval, "adding extra requests");
2498 exit (1);
2499 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002500 if (request) {
2501 retval = 0;
2502 retval = ss_execute_line(sci_idx, request);
2503 if (retval) {
2504 ss_perror(sci_idx, retval, request);
2505 exit_status++;
2506 }
2507 } else if (cmd_file) {
2508 exit_status = source_file(cmd_file, sci_idx);
2509 } else {
2510 ss_listen(sci_idx);
2511 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00002512
Theodore Ts'o6e9761c2009-06-15 03:52:30 -04002513 ss_delete_invocation(sci_idx);
2514
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00002515 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00002516 close_filesystem();
Theodore Ts'oefc6f622008-08-27 23:07:54 -04002517
Theodore Ts'oa6d83022006-12-26 03:38:07 -05002518 remove_error_table(&et_ext2_error_table);
Theodore Ts'oe5973042000-01-18 17:58:34 +00002519 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00002520}