blob: 232d0bef64e91fe09e3500801ce6e0ad6e74c18c [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'o3839e651997-04-26 13:21:57 +000037
38extern ss_request_table debug_cmds;
39
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000040ext2_filsys current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000041ino_t root, cwd;
42
Theodore Ts'o50e1e101997-04-26 13:58:21 +000043static void open_filesystem(char *device, int open_flags)
Theodore Ts'o3839e651997-04-26 13:21:57 +000044{
45 int retval;
46
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000047 retval = ext2fs_open(device, open_flags, 0, 0,
48 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 if (retval) {
50 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000051 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 return;
53 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000054 retval = ext2fs_read_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000055 if (retval) {
56 com_err(device, retval, "while reading inode bitmap");
57 goto errout;
58 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000059 retval = ext2fs_read_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 if (retval) {
61 com_err(device, retval, "while reading block bitmap");
62 goto errout;
63 }
64 root = cwd = EXT2_ROOT_INO;
65 return;
66
67errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000068 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000069 if (retval)
70 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000071 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000072}
73
74void do_open_filesys(int argc, char **argv)
75{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000076 const char *usage = "Usage: open [-w] <device>";
Theodore Ts'of1304811997-10-25 03:51:53 +000077 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +000078 int open_flags = 0;
79
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000080 optind = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000081#ifdef HAVE_OPTRESET
82 optreset = 1; /* Makes BSD getopt happy */
83#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 while ((c = getopt (argc, argv, "w")) != EOF) {
85 switch (c) {
86 case 'w':
87 open_flags = EXT2_FLAG_RW;
88 break;
89 default:
90 com_err(argv[0], 0, usage);
91 return;
92 }
93 }
94 if (optind != argc-1) {
95 com_err(argv[0], 0, usage);
96 return;
97 }
98 if (check_fs_not_open(argv[0]))
99 return;
100 open_filesystem(argv[optind], open_flags);
101}
102
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000103static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104{
105 int retval;
106
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000107 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
108 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 if (retval)
110 com_err("ext2fs_write_inode_bitmap", retval, "");
111 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000112 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
113 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 if (retval)
115 com_err("ext2fs_write_block_bitmap", retval, "");
116 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000117 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 if (retval)
119 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000120 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121 return;
122}
123
124void do_close_filesys(int argc, char **argv)
125{
126 if (argc > 1) {
127 com_err(argv[0], 0, "Usage: close_filesys");
128 return;
129 }
130 if (check_fs_open(argv[0]))
131 return;
132 close_filesystem();
133}
134
135void do_init_filesys(int argc, char **argv)
136{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000137 const char *usage = "Usage: initialize <device> <blocksize>";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138 struct ext2_super_block param;
139 errcode_t retval;
140 char *tmp;
141
142 if (argc != 3) {
143 com_err(argv[0], 0, usage);
144 return;
145 }
146 if (check_fs_not_open(argv[0]))
147 return;
148
149 memset(&param, 0, sizeof(struct ext2_super_block));
150 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
151 if (*tmp) {
152 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
153 return;
154 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000155 retval = ext2fs_initialize(argv[1], 0, &param,
156 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 if (retval) {
158 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000159 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 return;
161 }
162 root = cwd = EXT2_ROOT_INO;
163 return;
164}
165
166void do_show_super_stats(int argc, char *argv[])
167{
168 int i;
169 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000170 struct ext2fs_sb *sb;
171 struct ext2_group_desc *gdp;
172 char buf[80];
173 const char *none = "(none)";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174
175 if (argc > 1) {
176 com_err(argv[0], 0, "Usage: show_super");
177 return;
178 }
179 if (check_fs_open(argv[0]))
180 return;
181 out = open_pager();
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000182 sb = (struct ext2fs_sb *) current_fs->super;
183 fprintf(out, "Filesystem is read-%s\n",
184 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
185 if (sb->s_volume_name[0]) {
186 memset(buf, 0, sizeof(buf));
187 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
188 } else
189 strcpy(buf, none);
190 fprintf(out, "Volume name = %s\n", buf);
191 if (sb->s_last_mounted[0]) {
192 memset(buf, 0, sizeof(buf));
193 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
194 } else
195 strcpy(buf, none);
196 fprintf(out, "Last mounted directory = %s\n", buf);
197 if (!uuid_is_null(sb->s_uuid))
198 uuid_unparse(sb->s_uuid, buf);
199 else
200 strcpy(buf, none);
201 fprintf(out, "Filesystem UUID = %s\n", buf);
202 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
203 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 fprintf(out, "Mount counts = %d (maximal = %d)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000205 sb->s_mnt_count, sb->s_max_mnt_count);
206 fputs ("Filesystem OS type = ", out);
207 switch (sb->s_creator_os) {
208 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
209 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
210 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
211 default: fputs ("unknown\n", out);
212 }
213 fprintf(out, "Superblock size = %d\n",
214 sizeof(struct ext2_super_block));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000215 fprintf(out, "Block size = %d, fragment size = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000216 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
217 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
218 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
219 sb->s_free_inodes_count);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000220 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000221 sb->s_blocks_count, sb->s_free_blocks_count,
222 sb->s_r_blocks_count, sb->s_first_data_block);
223 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
224 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
225 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000227 current_fs->group_desc_count,
228 (current_fs->group_desc_count != 1) ? "s" : "",
229 current_fs->desc_blocks,
230 (current_fs->desc_blocks != 1) ? "s" : "");
231
232 gdp = &current_fs->group_desc[0];
233 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000234 fprintf(out, " Group %2d: block bitmap at %d, "
235 "inode bitmap at %d, "
236 "inode table at %d\n"
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 " %d free block%s, "
238 "%d free inode%s, "
239 "%d used director%s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000240 i, gdp->bg_block_bitmap,
241 gdp->bg_inode_bitmap, gdp->bg_inode_table,
242 gdp->bg_free_blocks_count,
243 gdp->bg_free_blocks_count != 1 ? "s" : "",
244 gdp->bg_free_inodes_count,
245 gdp->bg_free_inodes_count != 1 ? "s" : "",
246 gdp->bg_used_dirs_count,
247 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 close_pager(out);
249}
250
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000251void do_dirty_filesys(int argc, char *argv[])
252{
253 if (check_fs_open(argv[0]))
254 return;
255
256 ext2fs_mark_super_dirty(current_fs);
257}
258
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259struct list_blocks_struct {
260 FILE *f;
261 int total;
262};
263
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000264static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
265 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266{
267 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
268
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000269 fprintf(lb->f, "%d ", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 lb->total++;
271 return 0;
272}
273
274
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000275static void dump_blocks(FILE *f, ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276{
277 struct list_blocks_struct lb;
278
279 fprintf(f, "BLOCKS:\n");
280 lb.total = 0;
281 lb.f = f;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000282 ext2fs_block_iterate(current_fs, inode, 0, NULL,
283 list_blocks_proc, (void *)&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 if (lb.total)
285 fprintf(f, "\nTOTAL: %d\n", lb.total);
286 fprintf(f,"\n");
287}
288
289
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000290static void dump_inode(ino_t inode_num, struct ext2_inode inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000292 const char *i_type;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000294 char frag, fsize;
295 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296
297 out = open_pager();
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000298 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
299 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
300 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
301 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
302 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
303 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
304 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 else i_type = "bad type";
306 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000307 fprintf(out, "Mode: %04o Flags: 0x%x Version: %d\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 inode.i_mode & 0777, inode.i_flags, inode.i_version);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000309 fprintf(out, "User: %5d Group: %5d Size: ",
310 inode.i_uid, inode.i_gid);
311 if (LINUX_S_ISDIR(inode.i_mode))
312 fprintf(out, "%d\n", inode.i_size);
313 else {
314 __u64 i_size = (inode.i_size |
315 ((unsigned long long)inode.i_size_high << 32));
316
317 fprintf(out, "%lld\n", i_size);
318 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000319 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000320 fprintf(out,
321 "File ACL: %d Directory ACL: %d Translator: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000322 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000323 inode.osd1.hurd1.h_i_translator);
324 else
325 fprintf(out, "File ACL: %d Directory ACL: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000326 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000327 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000328 inode.i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000329 switch (os) {
330 case EXT2_OS_LINUX:
331 frag = inode.osd2.linux2.l_i_frag;
332 fsize = inode.osd2.linux2.l_i_fsize;
333 break;
334 case EXT2_OS_HURD:
335 frag = inode.osd2.hurd2.h_i_frag;
336 fsize = inode.osd2.hurd2.h_i_fsize;
337 break;
338 case EXT2_OS_MASIX:
339 frag = inode.osd2.masix2.m_i_frag;
340 fsize = inode.osd2.masix2.m_i_fsize;
341 break;
342 default:
343 frag = fsize = 0;
344 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000345 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000346 inode.i_faddr, frag, fsize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000347 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000348 time_to_string(inode.i_ctime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000349 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000350 time_to_string(inode.i_atime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000351 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000352 time_to_string(inode.i_mtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000353 if (inode.i_dtime)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000354 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000355 time_to_string(inode.i_dtime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000356 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000357 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
358 else
359 dump_blocks(out, inode_num);
360 close_pager(out);
361}
362
363
364void do_stat(int argc, char *argv[])
365{
366 ino_t inode;
367 struct ext2_inode inode_buf;
368 int retval;
369
370 if (argc != 2) {
371 com_err(argv[0], 0, "Usage: stat <file>");
372 return;
373 }
374 if (check_fs_open(argv[0]))
375 return;
376 inode = string_to_inode(argv[1]);
377 if (!inode)
378 return;
379
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000380 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000381 if (retval)
382 {
383 com_err(argv[0], 0, "Reading inode");
384 return;
385 }
386
387 dump_inode(inode,inode_buf);
388 return;
389}
390
391void do_chroot(int argc, char *argv[])
392{
393 ino_t inode;
394 int retval;
395
396 if (argc != 2) {
397 com_err(argv[0], 0, "Usage: chroot <file>");
398 return;
399 }
400 if (check_fs_open(argv[0]))
401 return;
402 inode = string_to_inode(argv[1]);
403 if (!inode)
404 return;
405
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000406 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000407 if (retval) {
408 com_err(argv[1], retval, "");
409 return;
410 }
411 root = inode;
412}
413
414void do_clri(int argc, char *argv[])
415{
416 ino_t inode;
417 int retval;
418 struct ext2_inode inode_buf;
419
420 if (argc != 2) {
421 com_err(argv[0], 0, "Usage: clri <file>");
422 return;
423 }
424 if (check_fs_open(argv[0]))
425 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000426 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000427 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000428 inode = string_to_inode(argv[1]);
429 if (!inode)
430 return;
431
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000432 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000433 if (retval) {
434 com_err(argv[0], 0, "while trying to read inode %d", inode);
435 return;
436 }
437 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000438 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000439 if (retval) {
440 com_err(argv[0], retval, "while trying to write inode %d",
441 inode);
442 return;
443 }
444}
445
446void do_freei(int argc, char *argv[])
447{
448 ino_t inode;
449
450 if (argc != 2) {
451 com_err(argv[0], 0, "Usage: freei <file>");
452 return;
453 }
454 if (check_fs_open(argv[0]))
455 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000456 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000457 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458 inode = string_to_inode(argv[1]);
459 if (!inode)
460 return;
461
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000462 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000463 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000464 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
465 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466}
467
468void do_seti(int argc, char *argv[])
469{
470 ino_t inode;
471
472 if (argc != 2) {
473 com_err(argv[0], 0, "Usage: seti <file>");
474 return;
475 }
476 if (check_fs_open(argv[0]))
477 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000478 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000479 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480 inode = string_to_inode(argv[1]);
481 if (!inode)
482 return;
483
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000484 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000485 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000486 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
487 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488}
489
490void do_testi(int argc, char *argv[])
491{
492 ino_t inode;
493
494 if (argc != 2) {
495 com_err(argv[0], 0, "Usage: testi <file>");
496 return;
497 }
498 if (check_fs_open(argv[0]))
499 return;
500 inode = string_to_inode(argv[1]);
501 if (!inode)
502 return;
503
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000504 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000505 printf("Inode %ld is marked in use\n", inode);
506 else
507 printf("Inode %ld is not in use\n", inode);
508}
509
510
511void do_freeb(int argc, char *argv[])
512{
513 blk_t block;
514 char *tmp;
515
516 if (argc != 2) {
517 com_err(argv[0], 0, "Usage: freeb <block>");
518 return;
519 }
520 if (check_fs_open(argv[0]))
521 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000522 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000523 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000524 block = strtoul(argv[1], &tmp, 0);
525 if (!block || *tmp) {
526 com_err(argv[0], 0, "No block 0");
527 return;
528 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000529 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000530 com_err(argv[0], 0, "Warning: block already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000531 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
532 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533}
534
535void do_setb(int argc, char *argv[])
536{
537 blk_t block;
538 char *tmp;
539
540 if (argc != 2) {
541 com_err(argv[0], 0, "Usage: setb <block>");
542 return;
543 }
544 if (check_fs_open(argv[0]))
545 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000546 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000547 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000548 block = strtoul(argv[1], &tmp, 0);
549 if (!block || *tmp) {
550 com_err(argv[0], 0, "No block 0");
551 return;
552 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000553 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000554 com_err(argv[0], 0, "Warning: block already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000555 ext2fs_mark_block_bitmap(current_fs->block_map,block);
556 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557}
558
559void do_testb(int argc, char *argv[])
560{
561 blk_t block;
562 char *tmp;
563
564 if (argc != 2) {
565 com_err(argv[0], 0, "Usage: testb <block>");
566 return;
567 }
568 if (check_fs_open(argv[0]))
569 return;
570 block = strtoul(argv[1], &tmp, 0);
571 if (!block || *tmp) {
572 com_err(argv[0], 0, "No block 0");
573 return;
574 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000575 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000576 printf("Block %d marked in use\n", block);
577 else printf("Block %d not in use\n", block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000578}
579
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000580static void modify_u8(char *com, const char *prompt,
581 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000582{
583 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000584 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585 char *tmp;
586
587 sprintf(buf, format, *val);
588 printf("%30s [%s] ", prompt, buf);
589 fgets(buf, sizeof(buf), stdin);
590 if (buf[strlen (buf) - 1] == '\n')
591 buf[strlen (buf) - 1] = '\0';
592 if (!buf[0])
593 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000594 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595 if (*tmp)
596 com_err(com, 0, "Bad value - %s", buf);
597 else
598 *val = v;
599}
600
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000601static void modify_u16(char *com, const char *prompt,
602 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000603{
604 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000605 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606 char *tmp;
607
608 sprintf(buf, format, *val);
609 printf("%30s [%s] ", prompt, buf);
610 fgets(buf, sizeof(buf), stdin);
611 if (buf[strlen (buf) - 1] == '\n')
612 buf[strlen (buf) - 1] = '\0';
613 if (!buf[0])
614 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000615 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616 if (*tmp)
617 com_err(com, 0, "Bad value - %s", buf);
618 else
619 *val = v;
620}
621
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000622static void modify_u32(char *com, const char *prompt,
623 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000624{
625 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000626 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627 char *tmp;
628
629 sprintf(buf, format, *val);
630 printf("%30s [%s] ", prompt, buf);
631 fgets(buf, sizeof(buf), stdin);
632 if (buf[strlen (buf) - 1] == '\n')
633 buf[strlen (buf) - 1] = '\0';
634 if (!buf[0])
635 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000636 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000637 if (*tmp)
638 com_err(com, 0, "Bad value - %s", buf);
639 else
640 *val = v;
641}
642
643
644void do_modify_inode(int argc, char *argv[])
645{
646 struct ext2_inode inode;
647 ino_t inode_num;
648 int i;
649 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000650 unsigned char *frag, *fsize;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000651 char buf[80];
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000652 int os = current_fs->super->s_creator_os;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000653 const char *hex_format = "0x%x";
654 const char *octal_format = "0%o";
655 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656
657 if (argc != 2) {
658 com_err(argv[0], 0, "Usage: modify_inode <file>");
659 return;
660 }
661 if (check_fs_open(argv[0]))
662 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000663 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000664 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000665
666 inode_num = string_to_inode(argv[1]);
667 if (!inode_num)
668 return;
669
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000670 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000671 if (retval) {
672 com_err(argv[1], retval, "while trying to read inode %d",
673 inode_num);
674 return;
675 }
676
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000677 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
678 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
679 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
680 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
681 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
682 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
683 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
684 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
685 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
686 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
687 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
688#if 0
689 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
690#endif
691 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000692 if (LINUX_S_ISDIR(inode.i_mode))
693 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
694 else
695 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000696
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000697 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000698 modify_u32(argv[0], "Translator Block",
699 decimal_format, &inode.osd1.hurd1.h_i_translator);
700
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000701 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000702 switch (os) {
703 case EXT2_OS_LINUX:
704 frag = &inode.osd2.linux2.l_i_frag;
705 fsize = &inode.osd2.linux2.l_i_fsize;
706 break;
707 case EXT2_OS_HURD:
708 frag = &inode.osd2.hurd2.h_i_frag;
709 fsize = &inode.osd2.hurd2.h_i_fsize;
710 break;
711 case EXT2_OS_MASIX:
712 frag = &inode.osd2.masix2.m_i_frag;
713 fsize = &inode.osd2.masix2.m_i_fsize;
714 break;
715 default:
716 frag = fsize = 0;
717 }
718 if (frag)
719 modify_u8(argv[0], "Fragment number", decimal_format, frag);
720 if (fsize)
721 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
722
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
724 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000725 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000727 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000729 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000731 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000732 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000733 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000734 if (retval) {
735 com_err(argv[1], retval, "while trying to write inode %d",
736 inode_num);
737 return;
738 }
739}
740
Theodore Ts'o3839e651997-04-26 13:21:57 +0000741void do_change_working_dir(int argc, char *argv[])
742{
743 ino_t inode;
744 int retval;
745
746 if (argc != 2) {
747 com_err(argv[0], 0, "Usage: cd <file>");
748 return;
749 }
750 if (check_fs_open(argv[0]))
751 return;
752
753 inode = string_to_inode(argv[1]);
754 if (!inode)
755 return;
756
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000757 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000758 if (retval) {
759 com_err(argv[1], retval, "");
760 return;
761 }
762 cwd = inode;
763 return;
764}
765
Theodore Ts'o3839e651997-04-26 13:21:57 +0000766void do_print_working_directory(int argc, char *argv[])
767{
768 int retval;
769 char *pathname = NULL;
770
771 if (argc > 1) {
772 com_err(argv[0], 0, "Usage: print_working_directory");
773 return;
774 }
775 if (check_fs_open(argv[0]))
776 return;
777
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000778 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000779 if (retval) {
780 com_err(argv[0], retval,
781 "while trying to get pathname of cwd");
782 }
783 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
784 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000785 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000786 if (retval) {
787 com_err(argv[0], retval,
788 "while trying to get pathname of root");
789 }
790 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
791 free(pathname);
792 return;
793}
794
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000795static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000796{
797 ino_t inode;
798 int retval;
799 ino_t dir;
800 char *dest, *cp, *basename;
801
802 /*
803 * Get the source inode
804 */
805 inode = string_to_inode(sourcename);
806 if (!inode)
807 return;
808 basename = strrchr(sourcename, '/');
809 if (basename)
810 basename++;
811 else
812 basename = sourcename;
813 /*
814 * Figure out the destination. First see if it exists and is
815 * a directory.
816 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000817 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000818 dest = basename;
819 else {
820 /*
821 * OK, it doesn't exist. See if it is
822 * '<dir>/basename' or 'basename'
823 */
824 cp = strrchr(destname, '/');
825 if (cp) {
826 *cp = 0;
827 dir = string_to_inode(destname);
828 if (!dir)
829 return;
830 dest = cp+1;
831 } else {
832 dir = cwd;
833 dest = destname;
834 }
835 }
836
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000837 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000838 if (retval)
839 com_err("make_link", retval, "");
840 return;
841}
842
843
844void do_link(int argc, char *argv[])
845{
846 if (argc != 3) {
847 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
848 return;
849 }
850 if (check_fs_open(argv[0]))
851 return;
852
853 make_link(argv[1], argv[2]);
854}
855
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000856static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000857{
858 int retval;
859 ino_t dir;
860 char *basename;
861
862 basename = strrchr(filename, '/');
863 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000864 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000865 dir = string_to_inode(filename);
866 if (!dir)
867 return;
868 } else {
869 dir = cwd;
870 basename = filename;
871 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000872 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000873 if (retval)
874 com_err("unlink_file_by_name", retval, "");
875 return;
876}
877
878void do_unlink(int argc, char *argv[])
879{
880 if (argc != 2) {
881 com_err(argv[0], 0, "Usage: unlink <pathname>");
882 return;
883 }
884 if (check_fs_open(argv[0]))
885 return;
886
887 unlink_file_by_name(argv[1]);
888}
889
890void do_find_free_block(int argc, char *argv[])
891{
892 blk_t free_blk, goal;
893 errcode_t retval;
894 char *tmp;
895
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000896 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
897 com_err(argv[0], 0, "Usage: find_free_block [goal]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000898 return;
899 }
900 if (check_fs_open(argv[0]))
901 return;
902
903 if (argc > 1) {
904 goal = strtol(argv[1], &tmp, 0);
905 if (*tmp) {
906 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
907 return;
908 }
909 }
910 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000911 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000912
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000913 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914 if (retval)
915 com_err("ext2fs_new_block", retval, "");
916 else
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000917 printf("Free block found: %d\n", free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918
919}
920
921void do_find_free_inode(int argc, char *argv[])
922{
923 ino_t free_inode, dir;
924 int mode;
925 int retval;
926 char *tmp;
927
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000928 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
929 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000930 return;
931 }
932 if (check_fs_open(argv[0]))
933 return;
934
935 if (argc > 1) {
936 dir = strtol(argv[1], &tmp, 0);
937 if (*tmp) {
938 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
939 return;
940 }
941 }
942 else
943 dir = root;
944 if (argc > 2) {
945 mode = strtol(argv[2], &tmp, 0);
946 if (*tmp) {
947 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
948 return;
949 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000950 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000951 mode = 010755;
952
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000953 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000954 if (retval)
955 com_err("ext2fs_new_inode", retval, "");
956 else
957 printf("Free inode found: %ld\n", free_inode);
958}
959
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000960static errcode_t copy_file(int fd, ino_t newfile)
961{
Theodore Ts'o5a513841997-10-25 22:41:14 +0000962 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000963 errcode_t retval;
Theodore Ts'o5a513841997-10-25 22:41:14 +0000964 int got, written;
965 char buf[8192];
966 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000967
Theodore Ts'o5a513841997-10-25 22:41:14 +0000968 retval = ext2fs_file_open(current_fs, newfile,
969 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000970 if (retval)
971 return retval;
972
Theodore Ts'o5a513841997-10-25 22:41:14 +0000973 while (1) {
974 got = read(fd, buf, sizeof(buf));
975 if (got == 0)
976 break;
977 if (got < 0) {
978 retval = errno;
979 goto fail;
980 }
981 ptr = buf;
982 while (got > 0) {
983 retval = ext2fs_file_write(e2_file, ptr,
984 got, &written);
985 if (retval)
986 goto fail;
987
988 got -= written;
989 ptr += written;
990 }
991 }
992 retval = ext2fs_file_close(e2_file);
993
994 return retval;
995
996fail:
997 (void) ext2fs_file_close(e2_file);
998 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000999}
1000
Theodore Ts'o5a513841997-10-25 22:41:14 +00001001
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001002void do_write(int argc, char *argv[])
1003{
1004 int fd;
1005 struct stat statbuf;
1006 ino_t newfile;
1007 errcode_t retval;
1008 struct ext2_inode inode;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001009 dgrp_t group;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001010
1011 if (check_fs_open(argv[0]))
1012 return;
1013 if (argc != 3) {
1014 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1015 return;
1016 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001017 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001018 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001019 fd = open(argv[1], O_RDONLY);
1020 if (fd < 0) {
1021 com_err(argv[1], fd, "");
1022 return;
1023 }
1024 if (fstat(fd, &statbuf) < 0) {
1025 com_err(argv[1], errno, "");
1026 close(fd);
1027 return;
1028 }
1029
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001030 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001031 if (retval) {
1032 com_err(argv[0], retval, "");
1033 close(fd);
1034 return;
1035 }
Theodore Ts'o5a513841997-10-25 22:41:14 +00001036 group = ext2fs_group_of_ino(current_fs, newfile);
1037 current_fs->group_desc[group].bg_free_inodes_count--;
1038 current_fs->super->s_free_inodes_count--;
1039 ext2fs_mark_super_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001040 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001041 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001042 if (retval) {
1043 com_err(argv[2], retval, "");
1044 close(fd);
1045 return;
1046 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001047 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001048 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001049 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1050 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001051 memset(&inode, 0, sizeof(inode));
1052 inode.i_mode = statbuf.st_mode;
1053 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1054 inode.i_links_count = 1;
1055 inode.i_size = statbuf.st_size;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001056 ext2fs_write_inode(current_fs, newfile, &inode);
1057 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001058 if (retval) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001059 com_err(argv[0], retval, "while trying to write inode %d",
1060 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001061 close(fd);
1062 return;
1063 }
1064 if (LINUX_S_ISREG(inode.i_mode)) {
1065 retval = copy_file(fd, newfile);
1066 if (retval)
1067 com_err("copy_file", retval, "");
1068 }
1069 close(fd);
1070}
1071
1072void do_mknod(int argc, char *argv[])
1073{
1074 unsigned long mode, major, minor, nr;
1075 ino_t newfile;
1076 errcode_t retval;
1077 struct ext2_inode inode;
1078
1079 if (check_fs_open(argv[0]))
1080 return;
1081 if (argc < 3 || argv[2][1]) {
1082 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1083 return;
1084 }
1085 mode = minor = major = 0;
1086 switch (argv[2][0]) {
1087 case 'p':
1088 mode = LINUX_S_IFIFO;
1089 nr = 3;
1090 break;
1091 case 'c':
1092 mode = LINUX_S_IFCHR;
1093 nr = 5;
1094 break;
1095 case 'b':
1096 mode = LINUX_S_IFBLK;
1097 nr = 5;
1098 break;
1099 default:
1100 nr = 0;
1101 }
1102 if (nr == 5) {
1103 major = strtoul(argv[3], argv+3, 0);
1104 minor = strtoul(argv[4], argv+4, 0);
1105 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1106 nr = 0;
1107 }
1108 if (argc != nr) {
1109 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1110 return;
1111 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001112 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001113 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001114 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001115 if (retval) {
1116 com_err(argv[0], retval, "");
1117 return;
1118 }
1119 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001120 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001121 if (retval) {
1122 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001123 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001124 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001125 retval = ext2fs_link(current_fs, cwd,
1126 argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001127 }
1128 if (retval) {
1129 com_err(argv[1], retval, "");
1130 return;
1131 }
1132 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001133 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001134 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001135 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1136 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001137 memset(&inode, 0, sizeof(inode));
1138 inode.i_mode = mode;
1139 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1140 inode.i_block[0] = major*256+minor;
1141 inode.i_links_count = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001142 ext2fs_write_inode(current_fs, newfile, &inode);
1143 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001144 if (retval) {
1145 com_err(argv[0], retval, "while trying to write inode %d", inode);
1146 return;
1147 }
1148}
1149
Theodore Ts'o3839e651997-04-26 13:21:57 +00001150void do_mkdir(int argc, char *argv[])
1151{
1152 char *cp;
1153 ino_t parent;
1154 char *name;
1155 errcode_t retval;
1156
1157 if (check_fs_open(argv[0]))
1158 return;
1159
1160 if (argc != 2) {
1161 com_err(argv[0], 0, "Usage: mkdir <file>");
1162 return;
1163 }
1164
1165 cp = strrchr(argv[1], '/');
1166 if (cp) {
1167 *cp = 0;
1168 parent = string_to_inode(argv[1]);
1169 if (!parent) {
1170 com_err(argv[1], ENOENT, "");
1171 return;
1172 }
1173 name = cp+1;
1174 } else {
1175 parent = cwd;
1176 name = argv[1];
1177 }
1178
1179
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001180 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001181 if (retval) {
1182 com_err("ext2fs_mkdir", retval, "");
1183 return;
1184 }
1185
1186}
1187
1188void do_rmdir(int argc, char *argv[])
1189{
1190 printf("Unimplemented\n");
1191}
1192
1193
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001194static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1195 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001196{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001197 printf("%d ", *blocknr);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001198 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001199 return 0;
1200}
1201
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001202static void kill_file_by_inode(ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001203{
1204 struct ext2_inode inode_buf;
1205
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001206 ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001207 inode_buf.i_dtime = time(NULL);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001208 ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001209
1210 printf("Kill file by inode %ld\n", inode);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001211 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1212 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001213 printf("\n");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001214 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001215
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001216 ext2fs_mark_bb_dirty(current_fs);
1217 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001218}
1219
1220
1221void do_kill_file(int argc, char *argv[])
1222{
1223 ino_t inode_num;
1224
1225 if (argc != 2) {
1226 com_err(argv[0], 0, "Usage: kill_file <file>");
1227 return;
1228 }
1229 if (check_fs_open(argv[0]))
1230 return;
1231
1232 inode_num = string_to_inode(argv[1]);
1233 if (!inode_num) {
1234 com_err(argv[0], 0, "Cannot find file");
1235 return;
1236 }
1237 kill_file_by_inode(inode_num);
1238}
1239
1240void do_rm(int argc, char *argv[])
1241{
1242 int retval;
1243 ino_t inode_num;
1244 struct ext2_inode inode;
1245
1246 if (argc != 2) {
1247 com_err(argv[0], 0, "Usage: rm <filename>");
1248 return;
1249 }
1250 if (check_fs_open(argv[0]))
1251 return;
1252
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001253 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001254 if (retval) {
1255 com_err(argv[0], 0, "Cannot find file");
1256 return;
1257 }
1258
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001259 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001260 if (retval) {
1261 com_err(argv[0], retval, "while reading file's inode");
1262 return;
1263 }
1264
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001265 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001266 com_err(argv[0], 0, "file is a directory");
1267 return;
1268 }
1269
1270 --inode.i_links_count;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001271 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001272 if (retval) {
1273 com_err(argv[0], retval, "while writing inode");
1274 return;
1275 }
1276
1277 unlink_file_by_name(argv[1]);
1278 if (inode.i_links_count == 0)
1279 kill_file_by_inode(inode_num);
1280}
1281
1282void do_show_debugfs_params(int argc, char *argv[])
1283{
1284 FILE *out = stdout;
1285
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001286 if (current_fs)
1287 fprintf(out, "Open mode: read-%s\n",
1288 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001289 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001290 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291}
1292
1293void do_expand_dir(int argc, char *argv[])
1294{
1295 ino_t inode;
1296 int retval;
1297
1298 if (argc != 2) {
1299 com_err(argv[0], 0, "Usage: expand_dir <file>");
1300 return;
1301 }
1302 if (check_fs_open(argv[0]))
1303 return;
1304 inode = string_to_inode(argv[1]);
1305 if (!inode)
1306 return;
1307
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001308 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001309 if (retval)
1310 com_err("ext2fs_expand_dir", retval, "");
1311 return;
1312}
1313
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001314static int source_file(const char *cmd_file, int sci_idx)
1315{
1316 FILE *f;
1317 char buf[256];
1318 char *cp;
1319 int exit_status = 0;
1320 int retval;
1321
1322 if (strcmp(cmd_file, "-") == 0)
1323 f = stdin;
1324 else {
1325 f = fopen(cmd_file, "r");
1326 if (!f) {
1327 perror(cmd_file);
1328 exit(1);
1329 }
1330 }
1331 setbuf(stdout, NULL);
1332 setbuf(stderr, NULL);
1333 while (!feof(f)) {
1334 if (fgets(buf, sizeof(buf), f) == NULL)
1335 break;
1336 cp = strchr(buf, '\n');
1337 if (cp)
1338 *cp = 0;
1339 cp = strchr(buf, '\r');
1340 if (cp)
1341 *cp = 0;
1342 printf("debugfs: %s\n", buf);
1343 retval = ss_execute_line(sci_idx, buf);
1344 if (retval) {
1345 ss_perror(sci_idx, retval, buf);
1346 exit_status++;
1347 }
1348 }
1349 return exit_status;
1350}
1351
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001352int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001353{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001354 int retval;
1355 int sci_idx;
Theodore Ts'o521e3681997-04-29 17:48:10 +00001356 const char *usage = "Usage: debugfs [[-w] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00001357 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001358 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001359 char *request = 0;
1360 int exit_status = 0;
1361 char *cmd_file = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001362
1363 initialize_ext2_error_table();
1364
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001365 while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001366 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001367 case 'R':
1368 request = optarg;
1369 break;
1370 case 'f':
1371 cmd_file = optarg;
1372 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001373 case 'w':
1374 open_flags = EXT2_FLAG_RW;
1375 break;
1376 default:
1377 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001378 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001379 }
1380 }
1381 if (optind < argc)
1382 open_filesystem(argv[optind], open_flags);
1383
1384 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1385 &debug_cmds, &retval);
1386 if (retval) {
1387 ss_perror(sci_idx, retval, "creating invocation");
1388 exit(1);
1389 }
1390
1391 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1392 if (retval) {
1393 ss_perror(sci_idx, retval, "adding standard requests");
1394 exit (1);
1395 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001396 if (request) {
1397 retval = 0;
1398 retval = ss_execute_line(sci_idx, request);
1399 if (retval) {
1400 ss_perror(sci_idx, retval, request);
1401 exit_status++;
1402 }
1403 } else if (cmd_file) {
1404 exit_status = source_file(cmd_file, sci_idx);
1405 } else {
1406 ss_listen(sci_idx);
1407 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001408
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001409 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001410 close_filesystem();
1411
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001412 exit(exit_status);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001413}
1414