blob: c4fd51cd1939673bcad6c6d8f76016c0d5ad55cf [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
4 *
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
7 *
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
9 */
10
11#include <stdio.h>
12#include <unistd.h>
13#include <stdlib.h>
14#include <ctype.h>
15#include <string.h>
16#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#else
20extern int optind;
21extern char *optarg;
22#endif
23#ifdef HAVE_OPTRESET
24extern int optreset; /* defined by BSD, but not others */
25#endif
26#ifdef HAVE_ERRNO_H
27#include <errno.h>
28#endif
29#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <sys/types.h>
31#include <sys/stat.h>
32
33#include "et/com_err.h"
34#include "ss/ss.h"
35#include "debugfs.h"
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000036#include "uuid/uuid.h"
Theodore Ts'of68aa411999-10-26 14:20:22 +000037#include "e2p/e2p.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000038
Theodore Ts'o818180c1998-06-27 05:11:14 +000039#include "../version.h"
40
Theodore Ts'o3839e651997-04-26 13:21:57 +000041extern ss_request_table debug_cmds;
42
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000043ext2_filsys current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000044ino_t root, cwd;
45
Theodore Ts'o50e1e101997-04-26 13:58:21 +000046static void open_filesystem(char *device, int open_flags)
Theodore Ts'o3839e651997-04-26 13:21:57 +000047{
48 int retval;
49
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000050 retval = ext2fs_open(device, open_flags, 0, 0,
51 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 if (retval) {
53 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000054 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000055 return;
56 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000057 retval = ext2fs_read_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000058 if (retval) {
59 com_err(device, retval, "while reading inode bitmap");
60 goto errout;
61 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000062 retval = ext2fs_read_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000063 if (retval) {
64 com_err(device, retval, "while reading block bitmap");
65 goto errout;
66 }
67 root = cwd = EXT2_ROOT_INO;
68 return;
69
70errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000071 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000072 if (retval)
73 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000074 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000075}
76
77void do_open_filesys(int argc, char **argv)
78{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000079 const char *usage = "Usage: open [-w] <device>";
Theodore Ts'of1304811997-10-25 03:51:53 +000080 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +000081 int open_flags = 0;
82
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000083 optind = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000084#ifdef HAVE_OPTRESET
85 optreset = 1; /* Makes BSD getopt happy */
86#endif
Theodore Ts'od3aea7d1999-09-14 20:55:37 +000087 while ((c = getopt (argc, argv, "wf")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +000088 switch (c) {
89 case 'w':
Theodore Ts'od3aea7d1999-09-14 20:55:37 +000090 open_flags |= EXT2_FLAG_RW;
91 break;
92 case 'f':
93 open_flags |= EXT2_FLAG_FORCE;
Theodore Ts'o3839e651997-04-26 13:21:57 +000094 break;
95 default:
96 com_err(argv[0], 0, usage);
97 return;
98 }
99 }
100 if (optind != argc-1) {
101 com_err(argv[0], 0, usage);
102 return;
103 }
104 if (check_fs_not_open(argv[0]))
105 return;
106 open_filesystem(argv[optind], open_flags);
107}
108
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000109static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000110{
111 int retval;
112
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000113 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
114 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000115 if (retval)
116 com_err("ext2fs_write_inode_bitmap", retval, "");
117 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000118 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
119 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120 if (retval)
121 com_err("ext2fs_write_block_bitmap", retval, "");
122 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000123 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000124 if (retval)
125 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000126 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000127 return;
128}
129
130void do_close_filesys(int argc, char **argv)
131{
132 if (argc > 1) {
133 com_err(argv[0], 0, "Usage: close_filesys");
134 return;
135 }
136 if (check_fs_open(argv[0]))
137 return;
138 close_filesystem();
139}
140
141void do_init_filesys(int argc, char **argv)
142{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000143 const char *usage = "Usage: initialize <device> <blocksize>";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000144 struct ext2_super_block param;
145 errcode_t retval;
146 char *tmp;
147
148 if (argc != 3) {
149 com_err(argv[0], 0, usage);
150 return;
151 }
152 if (check_fs_not_open(argv[0]))
153 return;
154
155 memset(&param, 0, sizeof(struct ext2_super_block));
156 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
157 if (*tmp) {
158 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
159 return;
160 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000161 retval = ext2fs_initialize(argv[1], 0, &param,
162 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000163 if (retval) {
164 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000165 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000166 return;
167 }
168 root = cwd = EXT2_ROOT_INO;
169 return;
170}
171
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000172static void print_features(struct ext2fs_sb * s, FILE *f)
173{
174#ifdef EXT2_DYNAMIC_REV
175 int i, j, printed=0;
176__u32 *mask = &s->s_feature_compat, m;
177
178 printf ("Filesystem features:");
179 for (i=0; i <3; i++,mask++) {
180 for (j=0,m=1; j < 32; j++, m<<=1) {
181 if (*mask & m) {
182 fprintf(f, " %s", e2p_feature2string(i, m));
183 printed++;
184 }
185 }
186 }
187 if (printed == 0)
188 printf("(none)");
189 printf("\n");
190#endif
191}
192
Theodore Ts'o3839e651997-04-26 13:21:57 +0000193void do_show_super_stats(int argc, char *argv[])
194{
195 int i;
196 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000197 struct ext2fs_sb *sb;
198 struct ext2_group_desc *gdp;
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000199 int c, header_only = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000200 char buf[80];
201 const char *none = "(none)";
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000202 const char *usage = "Usage: show_super [-h]";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000203
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000204 optind = 0;
205#ifdef HAVE_OPTRESET
206 optreset = 1; /* Makes BSD getopt happy */
207#endif
208 while ((c = getopt (argc, argv, "h")) != EOF) {
209 switch (c) {
210 case 'h':
211 header_only++;
212 break;
213 default:
214 com_err(argv[0], 0, usage);
215 return;
216 }
217 }
218 if (optind != argc) {
219 com_err(argv[0], 0, usage);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000220 return;
221 }
222 if (check_fs_open(argv[0]))
223 return;
224 out = open_pager();
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000225 sb = (struct ext2fs_sb *) current_fs->super;
226 fprintf(out, "Filesystem is read-%s\n",
227 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
228 if (sb->s_volume_name[0]) {
229 memset(buf, 0, sizeof(buf));
230 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
231 } else
232 strcpy(buf, none);
233 fprintf(out, "Volume name = %s\n", buf);
234 if (sb->s_last_mounted[0]) {
235 memset(buf, 0, sizeof(buf));
236 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
237 } else
238 strcpy(buf, none);
239 fprintf(out, "Last mounted directory = %s\n", buf);
240 if (!uuid_is_null(sb->s_uuid))
241 uuid_unparse(sb->s_uuid, buf);
242 else
243 strcpy(buf, none);
244 fprintf(out, "Filesystem UUID = %s\n", buf);
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000245 print_features(sb, out);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000246 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
247 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 fprintf(out, "Mount counts = %d (maximal = %d)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000249 sb->s_mnt_count, sb->s_max_mnt_count);
250 fputs ("Filesystem OS type = ", out);
251 switch (sb->s_creator_os) {
252 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
253 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
254 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
255 default: fputs ("unknown\n", out);
256 }
257 fprintf(out, "Superblock size = %d\n",
258 sizeof(struct ext2_super_block));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259 fprintf(out, "Block size = %d, fragment size = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000260 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
261 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
262 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
263 sb->s_free_inodes_count);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000264 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000265 sb->s_blocks_count, sb->s_free_blocks_count,
266 sb->s_r_blocks_count, sb->s_first_data_block);
267 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
268 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
269 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000271 current_fs->group_desc_count,
272 (current_fs->group_desc_count != 1) ? "s" : "",
273 current_fs->desc_blocks,
274 (current_fs->desc_blocks != 1) ? "s" : "");
Theodore Ts'od3aea7d1999-09-14 20:55:37 +0000275
276 if (header_only) {
277 close_pager(out);
278 return;
279 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000280
281 gdp = &current_fs->group_desc[0];
282 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000283 fprintf(out, " Group %2d: block bitmap at %d, "
284 "inode bitmap at %d, "
285 "inode table at %d\n"
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 " %d free block%s, "
287 "%d free inode%s, "
288 "%d used director%s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000289 i, gdp->bg_block_bitmap,
290 gdp->bg_inode_bitmap, gdp->bg_inode_table,
291 gdp->bg_free_blocks_count,
292 gdp->bg_free_blocks_count != 1 ? "s" : "",
293 gdp->bg_free_inodes_count,
294 gdp->bg_free_inodes_count != 1 ? "s" : "",
295 gdp->bg_used_dirs_count,
296 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000297 close_pager(out);
298}
299
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000300void do_dirty_filesys(int argc, char **argv)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000301{
302 if (check_fs_open(argv[0]))
303 return;
Theodore Ts'o601002b1999-10-26 02:06:39 +0000304 if (check_fs_read_write(argv[0]))
305 return;
306
307 if (argv[1] && !strcmp(argv[1], "-clean"))
308 current_fs->super->s_state |= EXT2_VALID_FS;
309 else
310 current_fs->super->s_state &= ~EXT2_VALID_FS;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000311 ext2fs_mark_super_dirty(current_fs);
312}
313
Theodore Ts'o3839e651997-04-26 13:21:57 +0000314struct list_blocks_struct {
315 FILE *f;
316 int total;
317};
318
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000319static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
320 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000321{
322 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
323
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000324 fprintf(lb->f, "%d ", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000325 lb->total++;
326 return 0;
327}
328
329
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000330static void dump_blocks(FILE *f, ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000331{
332 struct list_blocks_struct lb;
333
334 fprintf(f, "BLOCKS:\n");
335 lb.total = 0;
336 lb.f = f;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000337 ext2fs_block_iterate(current_fs, inode, 0, NULL,
338 list_blocks_proc, (void *)&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000339 if (lb.total)
340 fprintf(f, "\nTOTAL: %d\n", lb.total);
341 fprintf(f,"\n");
342}
343
344
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000345static void dump_inode(ino_t inode_num, struct ext2_inode inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000346{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000347 const char *i_type;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000348 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000349 char frag, fsize;
350 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000351
352 out = open_pager();
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000353 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
354 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
355 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
356 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
357 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
358 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
359 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000360 else i_type = "bad type";
361 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
Theodore Ts'ob41d3601999-06-25 15:32:37 +0000362#if defined(HAVE_EXT2_INODE_VERSION)
Theodore Ts'o55376d81999-07-31 00:32:57 +0000363 fprintf(out, "Mode: %04o Flags: 0x%x Version/Generation: %d\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000364 inode.i_mode & 0777, inode.i_flags, inode.i_version);
Theodore Ts'ob41d3601999-06-25 15:32:37 +0000365#else
Theodore Ts'o55376d81999-07-31 00:32:57 +0000366 fprintf(out, "Mode: %04o Flags: 0x%x Version/Generation: %d\n",
Theodore Ts'ob41d3601999-06-25 15:32:37 +0000367 inode.i_mode & 0777, inode.i_flags, inode.i_generation);
368#endif
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000369 fprintf(out, "User: %5d Group: %5d Size: ",
370 inode.i_uid, inode.i_gid);
371 if (LINUX_S_ISDIR(inode.i_mode))
372 fprintf(out, "%d\n", inode.i_size);
373 else {
374 __u64 i_size = (inode.i_size |
375 ((unsigned long long)inode.i_size_high << 32));
376
377 fprintf(out, "%lld\n", i_size);
378 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000379 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000380 fprintf(out,
381 "File ACL: %d Directory ACL: %d Translator: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000382 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000383 inode.osd1.hurd1.h_i_translator);
384 else
385 fprintf(out, "File ACL: %d Directory ACL: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000386 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000387 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000388 inode.i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000389 switch (os) {
390 case EXT2_OS_LINUX:
391 frag = inode.osd2.linux2.l_i_frag;
392 fsize = inode.osd2.linux2.l_i_fsize;
393 break;
394 case EXT2_OS_HURD:
395 frag = inode.osd2.hurd2.h_i_frag;
396 fsize = inode.osd2.hurd2.h_i_fsize;
397 break;
398 case EXT2_OS_MASIX:
399 frag = inode.osd2.masix2.m_i_frag;
400 fsize = inode.osd2.masix2.m_i_fsize;
401 break;
402 default:
403 frag = fsize = 0;
404 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000405 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000406 inode.i_faddr, frag, fsize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000407 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000408 time_to_string(inode.i_ctime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000409 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000410 time_to_string(inode.i_atime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000411 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000412 time_to_string(inode.i_mtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000413 if (inode.i_dtime)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000414 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000415 time_to_string(inode.i_dtime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000416 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000417 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
418 else
419 dump_blocks(out, inode_num);
420 close_pager(out);
421}
422
423
424void do_stat(int argc, char *argv[])
425{
426 ino_t inode;
427 struct ext2_inode inode_buf;
428 int retval;
429
430 if (argc != 2) {
431 com_err(argv[0], 0, "Usage: stat <file>");
432 return;
433 }
434 if (check_fs_open(argv[0]))
435 return;
436 inode = string_to_inode(argv[1]);
437 if (!inode)
438 return;
439
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000440 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 if (retval)
442 {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000443 com_err(argv[0], retval, "while trying to read inode %d", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000444 return;
445 }
446
447 dump_inode(inode,inode_buf);
448 return;
449}
450
451void do_chroot(int argc, char *argv[])
452{
453 ino_t inode;
454 int retval;
455
456 if (argc != 2) {
457 com_err(argv[0], 0, "Usage: chroot <file>");
458 return;
459 }
460 if (check_fs_open(argv[0]))
461 return;
462 inode = string_to_inode(argv[1]);
463 if (!inode)
464 return;
465
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000466 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000467 if (retval) {
468 com_err(argv[1], retval, "");
469 return;
470 }
471 root = inode;
472}
473
474void do_clri(int argc, char *argv[])
475{
476 ino_t inode;
477 int retval;
478 struct ext2_inode inode_buf;
479
480 if (argc != 2) {
481 com_err(argv[0], 0, "Usage: clri <file>");
482 return;
483 }
484 if (check_fs_open(argv[0]))
485 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000486 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 inode = string_to_inode(argv[1]);
489 if (!inode)
490 return;
491
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000492 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000493 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000494 com_err(argv[0], retval, "while trying to read inode %d",
495 inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000496 return;
497 }
498 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000499 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000500 if (retval) {
501 com_err(argv[0], retval, "while trying to write inode %d",
502 inode);
503 return;
504 }
505}
506
507void do_freei(int argc, char *argv[])
508{
509 ino_t inode;
510
511 if (argc != 2) {
512 com_err(argv[0], 0, "Usage: freei <file>");
513 return;
514 }
515 if (check_fs_open(argv[0]))
516 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000517 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000518 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000519 inode = string_to_inode(argv[1]);
520 if (!inode)
521 return;
522
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000523 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000525 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
526 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527}
528
529void do_seti(int argc, char *argv[])
530{
531 ino_t inode;
532
533 if (argc != 2) {
534 com_err(argv[0], 0, "Usage: seti <file>");
535 return;
536 }
537 if (check_fs_open(argv[0]))
538 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000539 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000541 inode = string_to_inode(argv[1]);
542 if (!inode)
543 return;
544
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000545 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000547 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
548 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549}
550
551void do_testi(int argc, char *argv[])
552{
553 ino_t inode;
554
555 if (argc != 2) {
556 com_err(argv[0], 0, "Usage: testi <file>");
557 return;
558 }
559 if (check_fs_open(argv[0]))
560 return;
561 inode = string_to_inode(argv[1]);
562 if (!inode)
563 return;
564
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000565 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000566 printf("Inode %ld is marked in use\n", inode);
567 else
568 printf("Inode %ld is not in use\n", inode);
569}
570
571
572void do_freeb(int argc, char *argv[])
573{
574 blk_t block;
575 char *tmp;
576
577 if (argc != 2) {
578 com_err(argv[0], 0, "Usage: freeb <block>");
579 return;
580 }
581 if (check_fs_open(argv[0]))
582 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000583 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 block = strtoul(argv[1], &tmp, 0);
586 if (!block || *tmp) {
587 com_err(argv[0], 0, "No block 0");
588 return;
589 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000590 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000591 com_err(argv[0], 0, "Warning: block already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000592 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
593 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000594}
595
596void do_setb(int argc, char *argv[])
597{
598 blk_t block;
599 char *tmp;
600
601 if (argc != 2) {
602 com_err(argv[0], 0, "Usage: setb <block>");
603 return;
604 }
605 if (check_fs_open(argv[0]))
606 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000607 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 block = strtoul(argv[1], &tmp, 0);
610 if (!block || *tmp) {
611 com_err(argv[0], 0, "No block 0");
612 return;
613 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000614 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000615 com_err(argv[0], 0, "Warning: block already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000616 ext2fs_mark_block_bitmap(current_fs->block_map,block);
617 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618}
619
620void do_testb(int argc, char *argv[])
621{
622 blk_t block;
623 char *tmp;
624
625 if (argc != 2) {
626 com_err(argv[0], 0, "Usage: testb <block>");
627 return;
628 }
629 if (check_fs_open(argv[0]))
630 return;
631 block = strtoul(argv[1], &tmp, 0);
632 if (!block || *tmp) {
633 com_err(argv[0], 0, "No block 0");
634 return;
635 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000636 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000637 printf("Block %d marked in use\n", block);
638 else printf("Block %d not in use\n", block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639}
640
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000641static void modify_u8(char *com, const char *prompt,
642 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643{
644 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000645 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000646 char *tmp;
647
648 sprintf(buf, format, *val);
649 printf("%30s [%s] ", prompt, buf);
650 fgets(buf, sizeof(buf), stdin);
651 if (buf[strlen (buf) - 1] == '\n')
652 buf[strlen (buf) - 1] = '\0';
653 if (!buf[0])
654 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000655 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 if (*tmp)
657 com_err(com, 0, "Bad value - %s", buf);
658 else
659 *val = v;
660}
661
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000662static void modify_u16(char *com, const char *prompt,
663 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664{
665 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000666 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667 char *tmp;
668
669 sprintf(buf, format, *val);
670 printf("%30s [%s] ", prompt, buf);
671 fgets(buf, sizeof(buf), stdin);
672 if (buf[strlen (buf) - 1] == '\n')
673 buf[strlen (buf) - 1] = '\0';
674 if (!buf[0])
675 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000676 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000677 if (*tmp)
678 com_err(com, 0, "Bad value - %s", buf);
679 else
680 *val = v;
681}
682
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000683static void modify_u32(char *com, const char *prompt,
684 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000685{
686 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000687 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000688 char *tmp;
689
690 sprintf(buf, format, *val);
691 printf("%30s [%s] ", prompt, buf);
692 fgets(buf, sizeof(buf), stdin);
693 if (buf[strlen (buf) - 1] == '\n')
694 buf[strlen (buf) - 1] = '\0';
695 if (!buf[0])
696 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000697 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000698 if (*tmp)
699 com_err(com, 0, "Bad value - %s", buf);
700 else
701 *val = v;
702}
703
704
705void do_modify_inode(int argc, char *argv[])
706{
707 struct ext2_inode inode;
708 ino_t inode_num;
709 int i;
710 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000711 unsigned char *frag, *fsize;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 char buf[80];
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000713 int os = current_fs->super->s_creator_os;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000714 const char *hex_format = "0x%x";
715 const char *octal_format = "0%o";
716 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717
718 if (argc != 2) {
719 com_err(argv[0], 0, "Usage: modify_inode <file>");
720 return;
721 }
722 if (check_fs_open(argv[0]))
723 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000724 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726
727 inode_num = string_to_inode(argv[1]);
728 if (!inode_num)
729 return;
730
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000731 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000732 if (retval) {
733 com_err(argv[1], retval, "while trying to read inode %d",
734 inode_num);
735 return;
736 }
737
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000738 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
739 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
740 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
741 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
742 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
743 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
744 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
745 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
746 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
747 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
748 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
749#if 0
750 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
751#endif
752 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000753 if (LINUX_S_ISDIR(inode.i_mode))
754 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
755 else
756 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000757
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000758 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000759 modify_u32(argv[0], "Translator Block",
760 decimal_format, &inode.osd1.hurd1.h_i_translator);
761
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000762 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000763 switch (os) {
764 case EXT2_OS_LINUX:
765 frag = &inode.osd2.linux2.l_i_frag;
766 fsize = &inode.osd2.linux2.l_i_fsize;
767 break;
768 case EXT2_OS_HURD:
769 frag = &inode.osd2.hurd2.h_i_frag;
770 fsize = &inode.osd2.hurd2.h_i_fsize;
771 break;
772 case EXT2_OS_MASIX:
773 frag = &inode.osd2.masix2.m_i_frag;
774 fsize = &inode.osd2.masix2.m_i_fsize;
775 break;
776 default:
777 frag = fsize = 0;
778 }
779 if (frag)
780 modify_u8(argv[0], "Fragment number", decimal_format, frag);
781 if (fsize)
782 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
783
Theodore Ts'o3839e651997-04-26 13:21:57 +0000784 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
785 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000786 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000787 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000788 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000789 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000790 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000791 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000792 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000794 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000795 if (retval) {
796 com_err(argv[1], retval, "while trying to write inode %d",
797 inode_num);
798 return;
799 }
800}
801
Theodore Ts'o3839e651997-04-26 13:21:57 +0000802void do_change_working_dir(int argc, char *argv[])
803{
804 ino_t inode;
805 int retval;
806
807 if (argc != 2) {
808 com_err(argv[0], 0, "Usage: cd <file>");
809 return;
810 }
811 if (check_fs_open(argv[0]))
812 return;
813
814 inode = string_to_inode(argv[1]);
815 if (!inode)
816 return;
817
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000818 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000819 if (retval) {
820 com_err(argv[1], retval, "");
821 return;
822 }
823 cwd = inode;
824 return;
825}
826
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827void do_print_working_directory(int argc, char *argv[])
828{
829 int retval;
830 char *pathname = NULL;
831
832 if (argc > 1) {
833 com_err(argv[0], 0, "Usage: print_working_directory");
834 return;
835 }
836 if (check_fs_open(argv[0]))
837 return;
838
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000839 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000840 if (retval) {
841 com_err(argv[0], retval,
842 "while trying to get pathname of cwd");
843 }
844 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
845 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000846 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000847 if (retval) {
848 com_err(argv[0], retval,
849 "while trying to get pathname of root");
850 }
851 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
852 free(pathname);
853 return;
854}
855
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000856static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000857{
858 ino_t inode;
859 int retval;
860 ino_t dir;
861 char *dest, *cp, *basename;
862
863 /*
864 * Get the source inode
865 */
866 inode = string_to_inode(sourcename);
867 if (!inode)
868 return;
869 basename = strrchr(sourcename, '/');
870 if (basename)
871 basename++;
872 else
873 basename = sourcename;
874 /*
875 * Figure out the destination. First see if it exists and is
876 * a directory.
877 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000878 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000879 dest = basename;
880 else {
881 /*
882 * OK, it doesn't exist. See if it is
883 * '<dir>/basename' or 'basename'
884 */
885 cp = strrchr(destname, '/');
886 if (cp) {
887 *cp = 0;
888 dir = string_to_inode(destname);
889 if (!dir)
890 return;
891 dest = cp+1;
892 } else {
893 dir = cwd;
894 dest = destname;
895 }
896 }
897
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000898 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 if (retval)
900 com_err("make_link", retval, "");
901 return;
902}
903
904
905void do_link(int argc, char *argv[])
906{
907 if (argc != 3) {
908 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
909 return;
910 }
911 if (check_fs_open(argv[0]))
912 return;
913
914 make_link(argv[1], argv[2]);
915}
916
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000917static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918{
919 int retval;
920 ino_t dir;
921 char *basename;
922
923 basename = strrchr(filename, '/');
924 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000925 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000926 dir = string_to_inode(filename);
927 if (!dir)
928 return;
929 } else {
930 dir = cwd;
931 basename = filename;
932 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000933 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000934 if (retval)
935 com_err("unlink_file_by_name", retval, "");
936 return;
937}
938
939void do_unlink(int argc, char *argv[])
940{
941 if (argc != 2) {
942 com_err(argv[0], 0, "Usage: unlink <pathname>");
943 return;
944 }
945 if (check_fs_open(argv[0]))
946 return;
947
948 unlink_file_by_name(argv[1]);
949}
950
951void do_find_free_block(int argc, char *argv[])
952{
953 blk_t free_blk, goal;
954 errcode_t retval;
955 char *tmp;
956
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000957 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
958 com_err(argv[0], 0, "Usage: find_free_block [goal]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000959 return;
960 }
961 if (check_fs_open(argv[0]))
962 return;
963
964 if (argc > 1) {
965 goal = strtol(argv[1], &tmp, 0);
966 if (*tmp) {
967 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
968 return;
969 }
970 }
971 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000972 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000974 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000975 if (retval)
976 com_err("ext2fs_new_block", retval, "");
977 else
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000978 printf("Free block found: %d\n", free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000979
980}
981
982void do_find_free_inode(int argc, char *argv[])
983{
984 ino_t free_inode, dir;
985 int mode;
986 int retval;
987 char *tmp;
988
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000989 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
990 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000991 return;
992 }
993 if (check_fs_open(argv[0]))
994 return;
995
996 if (argc > 1) {
997 dir = strtol(argv[1], &tmp, 0);
998 if (*tmp) {
999 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1000 return;
1001 }
1002 }
1003 else
1004 dir = root;
1005 if (argc > 2) {
1006 mode = strtol(argv[2], &tmp, 0);
1007 if (*tmp) {
1008 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1009 return;
1010 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001011 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001012 mode = 010755;
1013
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001014 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001015 if (retval)
1016 com_err("ext2fs_new_inode", retval, "");
1017 else
1018 printf("Free inode found: %ld\n", free_inode);
1019}
1020
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001021static errcode_t copy_file(int fd, ino_t newfile)
1022{
Theodore Ts'o5a513841997-10-25 22:41:14 +00001023 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001024 errcode_t retval;
Theodore Ts'o4a31c481998-03-30 01:27:25 +00001025 unsigned int got, written;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001026 char buf[8192];
1027 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001028
Theodore Ts'o5a513841997-10-25 22:41:14 +00001029 retval = ext2fs_file_open(current_fs, newfile,
1030 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001031 if (retval)
1032 return retval;
1033
Theodore Ts'o5a513841997-10-25 22:41:14 +00001034 while (1) {
1035 got = read(fd, buf, sizeof(buf));
1036 if (got == 0)
1037 break;
1038 if (got < 0) {
1039 retval = errno;
1040 goto fail;
1041 }
1042 ptr = buf;
1043 while (got > 0) {
1044 retval = ext2fs_file_write(e2_file, ptr,
1045 got, &written);
1046 if (retval)
1047 goto fail;
1048
1049 got -= written;
1050 ptr += written;
1051 }
1052 }
1053 retval = ext2fs_file_close(e2_file);
1054
1055 return retval;
1056
1057fail:
1058 (void) ext2fs_file_close(e2_file);
1059 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001060}
1061
Theodore Ts'o5a513841997-10-25 22:41:14 +00001062
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001063void do_write(int argc, char *argv[])
1064{
1065 int fd;
1066 struct stat statbuf;
1067 ino_t newfile;
1068 errcode_t retval;
1069 struct ext2_inode inode;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001070 dgrp_t group;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001071
1072 if (check_fs_open(argv[0]))
1073 return;
1074 if (argc != 3) {
1075 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1076 return;
1077 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001078 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001079 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001080 fd = open(argv[1], O_RDONLY);
1081 if (fd < 0) {
1082 com_err(argv[1], fd, "");
1083 return;
1084 }
1085 if (fstat(fd, &statbuf) < 0) {
1086 com_err(argv[1], errno, "");
1087 close(fd);
1088 return;
1089 }
1090
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001091 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001092 if (retval) {
1093 com_err(argv[0], retval, "");
1094 close(fd);
1095 return;
1096 }
Theodore Ts'o5a513841997-10-25 22:41:14 +00001097 group = ext2fs_group_of_ino(current_fs, newfile);
1098 current_fs->group_desc[group].bg_free_inodes_count--;
1099 current_fs->super->s_free_inodes_count--;
1100 ext2fs_mark_super_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001101 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001102 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001103 if (retval) {
1104 com_err(argv[2], retval, "");
1105 close(fd);
1106 return;
1107 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001108 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001109 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001110 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1111 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001112 memset(&inode, 0, sizeof(inode));
1113 inode.i_mode = statbuf.st_mode;
1114 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1115 inode.i_links_count = 1;
1116 inode.i_size = statbuf.st_size;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001117 ext2fs_write_inode(current_fs, newfile, &inode);
1118 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001119 if (retval) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001120 com_err(argv[0], retval, "while trying to write inode %d",
1121 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001122 close(fd);
1123 return;
1124 }
1125 if (LINUX_S_ISREG(inode.i_mode)) {
1126 retval = copy_file(fd, newfile);
1127 if (retval)
1128 com_err("copy_file", retval, "");
1129 }
1130 close(fd);
1131}
1132
1133void do_mknod(int argc, char *argv[])
1134{
1135 unsigned long mode, major, minor, nr;
1136 ino_t newfile;
1137 errcode_t retval;
1138 struct ext2_inode inode;
1139
1140 if (check_fs_open(argv[0]))
1141 return;
1142 if (argc < 3 || argv[2][1]) {
1143 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1144 return;
1145 }
1146 mode = minor = major = 0;
1147 switch (argv[2][0]) {
1148 case 'p':
1149 mode = LINUX_S_IFIFO;
1150 nr = 3;
1151 break;
1152 case 'c':
1153 mode = LINUX_S_IFCHR;
1154 nr = 5;
1155 break;
1156 case 'b':
1157 mode = LINUX_S_IFBLK;
1158 nr = 5;
1159 break;
1160 default:
1161 nr = 0;
1162 }
1163 if (nr == 5) {
1164 major = strtoul(argv[3], argv+3, 0);
1165 minor = strtoul(argv[4], argv+4, 0);
1166 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1167 nr = 0;
1168 }
1169 if (argc != nr) {
1170 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1171 return;
1172 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001173 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001174 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001175 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001176 if (retval) {
1177 com_err(argv[0], retval, "");
1178 return;
1179 }
1180 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001181 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001182 if (retval) {
1183 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001184 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001185 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001186 retval = ext2fs_link(current_fs, cwd,
1187 argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001188 }
1189 if (retval) {
1190 com_err(argv[1], retval, "");
1191 return;
1192 }
1193 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001194 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001195 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001196 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1197 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001198 memset(&inode, 0, sizeof(inode));
1199 inode.i_mode = mode;
1200 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1201 inode.i_block[0] = major*256+minor;
1202 inode.i_links_count = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001203 ext2fs_write_inode(current_fs, newfile, &inode);
1204 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001205 if (retval) {
1206 com_err(argv[0], retval, "while trying to write inode %d", inode);
1207 return;
1208 }
1209}
1210
Theodore Ts'o3839e651997-04-26 13:21:57 +00001211void do_mkdir(int argc, char *argv[])
1212{
1213 char *cp;
1214 ino_t parent;
1215 char *name;
1216 errcode_t retval;
1217
1218 if (check_fs_open(argv[0]))
1219 return;
1220
1221 if (argc != 2) {
1222 com_err(argv[0], 0, "Usage: mkdir <file>");
1223 return;
1224 }
1225
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001226 if (check_fs_read_write(argv[0]))
1227 return;
1228
Theodore Ts'o3839e651997-04-26 13:21:57 +00001229 cp = strrchr(argv[1], '/');
1230 if (cp) {
1231 *cp = 0;
1232 parent = string_to_inode(argv[1]);
1233 if (!parent) {
1234 com_err(argv[1], ENOENT, "");
1235 return;
1236 }
1237 name = cp+1;
1238 } else {
1239 parent = cwd;
1240 name = argv[1];
1241 }
1242
1243
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001244 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001245 if (retval) {
1246 com_err("ext2fs_mkdir", retval, "");
1247 return;
1248 }
1249
1250}
1251
1252void do_rmdir(int argc, char *argv[])
1253{
1254 printf("Unimplemented\n");
1255}
1256
1257
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001258static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1259 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001260{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001261 printf("%d ", *blocknr);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001262 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001263 return 0;
1264}
1265
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001266static void kill_file_by_inode(ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001267{
1268 struct ext2_inode inode_buf;
1269
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001270 ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001271 inode_buf.i_dtime = time(NULL);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001272 ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001273
1274 printf("Kill file by inode %ld\n", inode);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001275 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1276 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001277 printf("\n");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001278 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001279
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001280 ext2fs_mark_bb_dirty(current_fs);
1281 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001282}
1283
1284
1285void do_kill_file(int argc, char *argv[])
1286{
1287 ino_t inode_num;
1288
1289 if (argc != 2) {
1290 com_err(argv[0], 0, "Usage: kill_file <file>");
1291 return;
1292 }
1293 if (check_fs_open(argv[0]))
1294 return;
1295
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001296 if (check_fs_read_write(argv[0]))
1297 return;
1298
Theodore Ts'o3839e651997-04-26 13:21:57 +00001299 inode_num = string_to_inode(argv[1]);
1300 if (!inode_num) {
1301 com_err(argv[0], 0, "Cannot find file");
1302 return;
1303 }
1304 kill_file_by_inode(inode_num);
1305}
1306
1307void do_rm(int argc, char *argv[])
1308{
1309 int retval;
1310 ino_t inode_num;
1311 struct ext2_inode inode;
1312
1313 if (argc != 2) {
1314 com_err(argv[0], 0, "Usage: rm <filename>");
1315 return;
1316 }
1317 if (check_fs_open(argv[0]))
1318 return;
1319
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001320 if (check_fs_read_write(argv[0]))
1321 return;
1322
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001323 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001324 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001325 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001326 return;
1327 }
1328
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001329 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001330 if (retval) {
1331 com_err(argv[0], retval, "while reading file's inode");
1332 return;
1333 }
1334
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001335 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001336 com_err(argv[0], 0, "file is a directory");
1337 return;
1338 }
1339
1340 --inode.i_links_count;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001341 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001342 if (retval) {
1343 com_err(argv[0], retval, "while writing inode");
1344 return;
1345 }
1346
1347 unlink_file_by_name(argv[1]);
1348 if (inode.i_links_count == 0)
1349 kill_file_by_inode(inode_num);
1350}
1351
1352void do_show_debugfs_params(int argc, char *argv[])
1353{
1354 FILE *out = stdout;
1355
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001356 if (current_fs)
1357 fprintf(out, "Open mode: read-%s\n",
1358 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001359 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001360 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001361}
1362
1363void do_expand_dir(int argc, char *argv[])
1364{
1365 ino_t inode;
1366 int retval;
1367
1368 if (argc != 2) {
1369 com_err(argv[0], 0, "Usage: expand_dir <file>");
1370 return;
1371 }
1372 if (check_fs_open(argv[0]))
1373 return;
1374 inode = string_to_inode(argv[1]);
1375 if (!inode)
1376 return;
1377
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001378 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001379 if (retval)
1380 com_err("ext2fs_expand_dir", retval, "");
1381 return;
1382}
1383
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001384void do_features(int argc, char *argv[])
1385{
1386 int i;
1387
1388 if (check_fs_open(argv[0]))
1389 return;
1390
1391 if ((argc != 1) && check_fs_read_write(argv[0]))
1392 return;
1393 for (i=1; i < argc; i++) {
1394 if (e2p_edit_feature(argv[i],
Theodore Ts'o06968e71999-10-23 03:17:10 +00001395 &current_fs->super->s_feature_compat, 0))
Theodore Ts'od3aea7d1999-09-14 20:55:37 +00001396 com_err(argv[0], 0, "Unknown feature: %s\n",
1397 argv[i]);
1398 else
1399 ext2fs_mark_super_dirty(current_fs);
1400 }
1401 print_features((struct ext2fs_sb *) current_fs->super, stdout);
1402}
1403
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001404static int source_file(const char *cmd_file, int sci_idx)
1405{
1406 FILE *f;
1407 char buf[256];
1408 char *cp;
1409 int exit_status = 0;
1410 int retval;
1411
1412 if (strcmp(cmd_file, "-") == 0)
1413 f = stdin;
1414 else {
1415 f = fopen(cmd_file, "r");
1416 if (!f) {
1417 perror(cmd_file);
1418 exit(1);
1419 }
1420 }
1421 setbuf(stdout, NULL);
1422 setbuf(stderr, NULL);
1423 while (!feof(f)) {
1424 if (fgets(buf, sizeof(buf), f) == NULL)
1425 break;
1426 cp = strchr(buf, '\n');
1427 if (cp)
1428 *cp = 0;
1429 cp = strchr(buf, '\r');
1430 if (cp)
1431 *cp = 0;
1432 printf("debugfs: %s\n", buf);
1433 retval = ss_execute_line(sci_idx, buf);
1434 if (retval) {
1435 ss_perror(sci_idx, retval, buf);
1436 exit_status++;
1437 }
1438 }
1439 return exit_status;
1440}
1441
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001442int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001443{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001444 int retval;
1445 int sci_idx;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001446 const char *usage = "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00001447 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001448 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001449 char *request = 0;
1450 int exit_status = 0;
1451 char *cmd_file = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001452
1453 initialize_ext2_error_table();
Theodore Ts'o818180c1998-06-27 05:11:14 +00001454 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1455 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1456 EXT2FS_VERSION, EXT2FS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001457
Theodore Ts'o818180c1998-06-27 05:11:14 +00001458 while ((c = getopt (argc, argv, "wR:f:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001459 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001460 case 'R':
1461 request = optarg;
1462 break;
1463 case 'f':
1464 cmd_file = optarg;
1465 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001466 case 'w':
1467 open_flags = EXT2_FLAG_RW;
1468 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001469 case 'V':
1470 /* Print version number and exit */
1471 fprintf(stderr, "\tUsing %s\n",
1472 error_message(EXT2_ET_BASE));
1473 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001474 default:
1475 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001476 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001477 }
1478 }
1479 if (optind < argc)
1480 open_filesystem(argv[optind], open_flags);
1481
1482 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1483 &debug_cmds, &retval);
1484 if (retval) {
1485 ss_perror(sci_idx, retval, "creating invocation");
1486 exit(1);
1487 }
1488
1489 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1490 if (retval) {
1491 ss_perror(sci_idx, retval, "adding standard requests");
1492 exit (1);
1493 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001494 if (request) {
1495 retval = 0;
1496 retval = ss_execute_line(sci_idx, request);
1497 if (retval) {
1498 ss_perror(sci_idx, retval, request);
1499 exit_status++;
1500 }
1501 } else if (cmd_file) {
1502 exit_status = source_file(cmd_file, sci_idx);
1503 } else {
1504 ss_listen(sci_idx);
1505 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001506
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001507 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001508 close_filesystem();
1509
Theodore Ts'oe5973042000-01-18 17:58:34 +00001510 return exit_status;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001511}