blob: 2cbad64c52841d8945b94e95bcfe950ddb986d58 [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
Theodore Ts'o818180c1998-06-27 05:11:14 +000038#include "../version.h"
39
Theodore Ts'o3839e651997-04-26 13:21:57 +000040extern ss_request_table debug_cmds;
41
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000042ext2_filsys current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000043ino_t root, cwd;
44
Theodore Ts'o50e1e101997-04-26 13:58:21 +000045static void open_filesystem(char *device, int open_flags)
Theodore Ts'o3839e651997-04-26 13:21:57 +000046{
47 int retval;
48
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000049 retval = ext2fs_open(device, open_flags, 0, 0,
50 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000051 if (retval) {
52 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000053 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000054 return;
55 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000056 retval = ext2fs_read_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000057 if (retval) {
58 com_err(device, retval, "while reading inode bitmap");
59 goto errout;
60 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000061 retval = ext2fs_read_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000062 if (retval) {
63 com_err(device, retval, "while reading block bitmap");
64 goto errout;
65 }
66 root = cwd = EXT2_ROOT_INO;
67 return;
68
69errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000070 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000071 if (retval)
72 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000073 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000074}
75
76void do_open_filesys(int argc, char **argv)
77{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000078 const char *usage = "Usage: open [-w] <device>";
Theodore Ts'of1304811997-10-25 03:51:53 +000079 int c;
Theodore Ts'o3839e651997-04-26 13:21:57 +000080 int open_flags = 0;
81
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000082 optind = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000083#ifdef HAVE_OPTRESET
84 optreset = 1; /* Makes BSD getopt happy */
85#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000086 while ((c = getopt (argc, argv, "w")) != EOF) {
87 switch (c) {
88 case 'w':
89 open_flags = EXT2_FLAG_RW;
90 break;
91 default:
92 com_err(argv[0], 0, usage);
93 return;
94 }
95 }
96 if (optind != argc-1) {
97 com_err(argv[0], 0, usage);
98 return;
99 }
100 if (check_fs_not_open(argv[0]))
101 return;
102 open_filesystem(argv[optind], open_flags);
103}
104
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000105static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000106{
107 int retval;
108
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000109 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
110 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000111 if (retval)
112 com_err("ext2fs_write_inode_bitmap", retval, "");
113 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000114 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
115 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000116 if (retval)
117 com_err("ext2fs_write_block_bitmap", retval, "");
118 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000119 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000120 if (retval)
121 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000122 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000123 return;
124}
125
126void do_close_filesys(int argc, char **argv)
127{
128 if (argc > 1) {
129 com_err(argv[0], 0, "Usage: close_filesys");
130 return;
131 }
132 if (check_fs_open(argv[0]))
133 return;
134 close_filesystem();
135}
136
137void do_init_filesys(int argc, char **argv)
138{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000139 const char *usage = "Usage: initialize <device> <blocksize>";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000140 struct ext2_super_block param;
141 errcode_t retval;
142 char *tmp;
143
144 if (argc != 3) {
145 com_err(argv[0], 0, usage);
146 return;
147 }
148 if (check_fs_not_open(argv[0]))
149 return;
150
151 memset(&param, 0, sizeof(struct ext2_super_block));
152 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
153 if (*tmp) {
154 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
155 return;
156 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000157 retval = ext2fs_initialize(argv[1], 0, &param,
158 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000159 if (retval) {
160 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000161 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000162 return;
163 }
164 root = cwd = EXT2_ROOT_INO;
165 return;
166}
167
168void do_show_super_stats(int argc, char *argv[])
169{
170 int i;
171 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000172 struct ext2fs_sb *sb;
173 struct ext2_group_desc *gdp;
174 char buf[80];
175 const char *none = "(none)";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000176
177 if (argc > 1) {
178 com_err(argv[0], 0, "Usage: show_super");
179 return;
180 }
181 if (check_fs_open(argv[0]))
182 return;
183 out = open_pager();
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000184 sb = (struct ext2fs_sb *) current_fs->super;
185 fprintf(out, "Filesystem is read-%s\n",
186 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
187 if (sb->s_volume_name[0]) {
188 memset(buf, 0, sizeof(buf));
189 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
190 } else
191 strcpy(buf, none);
192 fprintf(out, "Volume name = %s\n", buf);
193 if (sb->s_last_mounted[0]) {
194 memset(buf, 0, sizeof(buf));
195 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
196 } else
197 strcpy(buf, none);
198 fprintf(out, "Last mounted directory = %s\n", buf);
199 if (!uuid_is_null(sb->s_uuid))
200 uuid_unparse(sb->s_uuid, buf);
201 else
202 strcpy(buf, none);
203 fprintf(out, "Filesystem UUID = %s\n", buf);
204 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
205 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000206 fprintf(out, "Mount counts = %d (maximal = %d)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000207 sb->s_mnt_count, sb->s_max_mnt_count);
208 fputs ("Filesystem OS type = ", out);
209 switch (sb->s_creator_os) {
210 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
211 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
212 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
213 default: fputs ("unknown\n", out);
214 }
215 fprintf(out, "Superblock size = %d\n",
216 sizeof(struct ext2_super_block));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000217 fprintf(out, "Block size = %d, fragment size = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000218 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
219 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
220 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
221 sb->s_free_inodes_count);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000222 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000223 sb->s_blocks_count, sb->s_free_blocks_count,
224 sb->s_r_blocks_count, sb->s_first_data_block);
225 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
226 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
227 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000228 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000229 current_fs->group_desc_count,
230 (current_fs->group_desc_count != 1) ? "s" : "",
231 current_fs->desc_blocks,
232 (current_fs->desc_blocks != 1) ? "s" : "");
233
234 gdp = &current_fs->group_desc[0];
235 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000236 fprintf(out, " Group %2d: block bitmap at %d, "
237 "inode bitmap at %d, "
238 "inode table at %d\n"
Theodore Ts'o3839e651997-04-26 13:21:57 +0000239 " %d free block%s, "
240 "%d free inode%s, "
241 "%d used director%s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000242 i, gdp->bg_block_bitmap,
243 gdp->bg_inode_bitmap, gdp->bg_inode_table,
244 gdp->bg_free_blocks_count,
245 gdp->bg_free_blocks_count != 1 ? "s" : "",
246 gdp->bg_free_inodes_count,
247 gdp->bg_free_inodes_count != 1 ? "s" : "",
248 gdp->bg_used_dirs_count,
249 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000250 close_pager(out);
251}
252
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000253void do_dirty_filesys(int argc, char **argv)
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000254{
255 if (check_fs_open(argv[0]))
256 return;
257
258 ext2fs_mark_super_dirty(current_fs);
259}
260
Theodore Ts'o3839e651997-04-26 13:21:57 +0000261struct list_blocks_struct {
262 FILE *f;
263 int total;
264};
265
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000266static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
267 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000268{
269 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
270
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000271 fprintf(lb->f, "%d ", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000272 lb->total++;
273 return 0;
274}
275
276
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000277static void dump_blocks(FILE *f, ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000278{
279 struct list_blocks_struct lb;
280
281 fprintf(f, "BLOCKS:\n");
282 lb.total = 0;
283 lb.f = f;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000284 ext2fs_block_iterate(current_fs, inode, 0, NULL,
285 list_blocks_proc, (void *)&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000286 if (lb.total)
287 fprintf(f, "\nTOTAL: %d\n", lb.total);
288 fprintf(f,"\n");
289}
290
291
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000292static void dump_inode(ino_t inode_num, struct ext2_inode inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000294 const char *i_type;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000295 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000296 char frag, fsize;
297 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000298
299 out = open_pager();
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000300 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
301 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
302 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
303 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
304 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
305 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
306 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000307 else i_type = "bad type";
308 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000309 fprintf(out, "Mode: %04o Flags: 0x%x Version: %d\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 inode.i_mode & 0777, inode.i_flags, inode.i_version);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000311 fprintf(out, "User: %5d Group: %5d Size: ",
312 inode.i_uid, inode.i_gid);
313 if (LINUX_S_ISDIR(inode.i_mode))
314 fprintf(out, "%d\n", inode.i_size);
315 else {
316 __u64 i_size = (inode.i_size |
317 ((unsigned long long)inode.i_size_high << 32));
318
319 fprintf(out, "%lld\n", i_size);
320 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000321 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000322 fprintf(out,
323 "File ACL: %d Directory ACL: %d Translator: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000324 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0,
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000325 inode.osd1.hurd1.h_i_translator);
326 else
327 fprintf(out, "File ACL: %d Directory ACL: %d\n",
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000328 inode.i_file_acl, LINUX_S_ISDIR(inode.i_mode) ? inode.i_dir_acl : 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000329 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000330 inode.i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000331 switch (os) {
332 case EXT2_OS_LINUX:
333 frag = inode.osd2.linux2.l_i_frag;
334 fsize = inode.osd2.linux2.l_i_fsize;
335 break;
336 case EXT2_OS_HURD:
337 frag = inode.osd2.hurd2.h_i_frag;
338 fsize = inode.osd2.hurd2.h_i_fsize;
339 break;
340 case EXT2_OS_MASIX:
341 frag = inode.osd2.masix2.m_i_frag;
342 fsize = inode.osd2.masix2.m_i_fsize;
343 break;
344 default:
345 frag = fsize = 0;
346 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000347 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000348 inode.i_faddr, frag, fsize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000349 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000350 time_to_string(inode.i_ctime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000351 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000352 time_to_string(inode.i_atime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000353 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000354 time_to_string(inode.i_mtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000355 if (inode.i_dtime)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000356 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000357 time_to_string(inode.i_dtime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000358 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000359 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
360 else
361 dump_blocks(out, inode_num);
362 close_pager(out);
363}
364
365
366void do_stat(int argc, char *argv[])
367{
368 ino_t inode;
369 struct ext2_inode inode_buf;
370 int retval;
371
372 if (argc != 2) {
373 com_err(argv[0], 0, "Usage: stat <file>");
374 return;
375 }
376 if (check_fs_open(argv[0]))
377 return;
378 inode = string_to_inode(argv[1]);
379 if (!inode)
380 return;
381
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000382 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000383 if (retval)
384 {
385 com_err(argv[0], 0, "Reading inode");
386 return;
387 }
388
389 dump_inode(inode,inode_buf);
390 return;
391}
392
393void do_chroot(int argc, char *argv[])
394{
395 ino_t inode;
396 int retval;
397
398 if (argc != 2) {
399 com_err(argv[0], 0, "Usage: chroot <file>");
400 return;
401 }
402 if (check_fs_open(argv[0]))
403 return;
404 inode = string_to_inode(argv[1]);
405 if (!inode)
406 return;
407
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000408 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000409 if (retval) {
410 com_err(argv[1], retval, "");
411 return;
412 }
413 root = inode;
414}
415
416void do_clri(int argc, char *argv[])
417{
418 ino_t inode;
419 int retval;
420 struct ext2_inode inode_buf;
421
422 if (argc != 2) {
423 com_err(argv[0], 0, "Usage: clri <file>");
424 return;
425 }
426 if (check_fs_open(argv[0]))
427 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000428 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000429 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000430 inode = string_to_inode(argv[1]);
431 if (!inode)
432 return;
433
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000434 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000435 if (retval) {
436 com_err(argv[0], 0, "while trying to read inode %d", inode);
437 return;
438 }
439 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000440 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000441 if (retval) {
442 com_err(argv[0], retval, "while trying to write inode %d",
443 inode);
444 return;
445 }
446}
447
448void do_freei(int argc, char *argv[])
449{
450 ino_t inode;
451
452 if (argc != 2) {
453 com_err(argv[0], 0, "Usage: freei <file>");
454 return;
455 }
456 if (check_fs_open(argv[0]))
457 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000458 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000459 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 inode = string_to_inode(argv[1]);
461 if (!inode)
462 return;
463
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000464 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000465 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000466 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
467 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000468}
469
470void do_seti(int argc, char *argv[])
471{
472 ino_t inode;
473
474 if (argc != 2) {
475 com_err(argv[0], 0, "Usage: seti <file>");
476 return;
477 }
478 if (check_fs_open(argv[0]))
479 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000480 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000481 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482 inode = string_to_inode(argv[1]);
483 if (!inode)
484 return;
485
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000486 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000487 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000488 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
489 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000490}
491
492void do_testi(int argc, char *argv[])
493{
494 ino_t inode;
495
496 if (argc != 2) {
497 com_err(argv[0], 0, "Usage: testi <file>");
498 return;
499 }
500 if (check_fs_open(argv[0]))
501 return;
502 inode = string_to_inode(argv[1]);
503 if (!inode)
504 return;
505
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000506 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000507 printf("Inode %ld is marked in use\n", inode);
508 else
509 printf("Inode %ld is not in use\n", inode);
510}
511
512
513void do_freeb(int argc, char *argv[])
514{
515 blk_t block;
516 char *tmp;
517
518 if (argc != 2) {
519 com_err(argv[0], 0, "Usage: freeb <block>");
520 return;
521 }
522 if (check_fs_open(argv[0]))
523 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000524 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 block = strtoul(argv[1], &tmp, 0);
527 if (!block || *tmp) {
528 com_err(argv[0], 0, "No block 0");
529 return;
530 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000531 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000532 com_err(argv[0], 0, "Warning: block already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000533 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
534 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000535}
536
537void do_setb(int argc, char *argv[])
538{
539 blk_t block;
540 char *tmp;
541
542 if (argc != 2) {
543 com_err(argv[0], 0, "Usage: setb <block>");
544 return;
545 }
546 if (check_fs_open(argv[0]))
547 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000548 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 block = strtoul(argv[1], &tmp, 0);
551 if (!block || *tmp) {
552 com_err(argv[0], 0, "No block 0");
553 return;
554 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000555 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000556 com_err(argv[0], 0, "Warning: block already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000557 ext2fs_mark_block_bitmap(current_fs->block_map,block);
558 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000559}
560
561void do_testb(int argc, char *argv[])
562{
563 blk_t block;
564 char *tmp;
565
566 if (argc != 2) {
567 com_err(argv[0], 0, "Usage: testb <block>");
568 return;
569 }
570 if (check_fs_open(argv[0]))
571 return;
572 block = strtoul(argv[1], &tmp, 0);
573 if (!block || *tmp) {
574 com_err(argv[0], 0, "No block 0");
575 return;
576 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000577 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000578 printf("Block %d marked in use\n", block);
579 else printf("Block %d not in use\n", block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000580}
581
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000582static void modify_u8(char *com, const char *prompt,
583 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000584{
585 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000586 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587 char *tmp;
588
589 sprintf(buf, format, *val);
590 printf("%30s [%s] ", prompt, buf);
591 fgets(buf, sizeof(buf), stdin);
592 if (buf[strlen (buf) - 1] == '\n')
593 buf[strlen (buf) - 1] = '\0';
594 if (!buf[0])
595 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000596 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000597 if (*tmp)
598 com_err(com, 0, "Bad value - %s", buf);
599 else
600 *val = v;
601}
602
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000603static void modify_u16(char *com, const char *prompt,
604 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000605{
606 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000607 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 char *tmp;
609
610 sprintf(buf, format, *val);
611 printf("%30s [%s] ", prompt, buf);
612 fgets(buf, sizeof(buf), stdin);
613 if (buf[strlen (buf) - 1] == '\n')
614 buf[strlen (buf) - 1] = '\0';
615 if (!buf[0])
616 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000617 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000618 if (*tmp)
619 com_err(com, 0, "Bad value - %s", buf);
620 else
621 *val = v;
622}
623
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000624static void modify_u32(char *com, const char *prompt,
625 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000626{
627 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000628 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 char *tmp;
630
631 sprintf(buf, format, *val);
632 printf("%30s [%s] ", prompt, buf);
633 fgets(buf, sizeof(buf), stdin);
634 if (buf[strlen (buf) - 1] == '\n')
635 buf[strlen (buf) - 1] = '\0';
636 if (!buf[0])
637 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000638 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000639 if (*tmp)
640 com_err(com, 0, "Bad value - %s", buf);
641 else
642 *val = v;
643}
644
645
646void do_modify_inode(int argc, char *argv[])
647{
648 struct ext2_inode inode;
649 ino_t inode_num;
650 int i;
651 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000652 unsigned char *frag, *fsize;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000653 char buf[80];
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000654 int os = current_fs->super->s_creator_os;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000655 const char *hex_format = "0x%x";
656 const char *octal_format = "0%o";
657 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000658
659 if (argc != 2) {
660 com_err(argv[0], 0, "Usage: modify_inode <file>");
661 return;
662 }
663 if (check_fs_open(argv[0]))
664 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000665 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000666 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667
668 inode_num = string_to_inode(argv[1]);
669 if (!inode_num)
670 return;
671
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000672 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000673 if (retval) {
674 com_err(argv[1], retval, "while trying to read inode %d",
675 inode_num);
676 return;
677 }
678
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000679 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
680 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
681 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
682 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
683 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
684 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
685 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
686 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
687 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
688 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
689 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
690#if 0
691 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
692#endif
693 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000694 if (LINUX_S_ISDIR(inode.i_mode))
695 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
696 else
697 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000698
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000699 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000700 modify_u32(argv[0], "Translator Block",
701 decimal_format, &inode.osd1.hurd1.h_i_translator);
702
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000703 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000704 switch (os) {
705 case EXT2_OS_LINUX:
706 frag = &inode.osd2.linux2.l_i_frag;
707 fsize = &inode.osd2.linux2.l_i_fsize;
708 break;
709 case EXT2_OS_HURD:
710 frag = &inode.osd2.hurd2.h_i_frag;
711 fsize = &inode.osd2.hurd2.h_i_fsize;
712 break;
713 case EXT2_OS_MASIX:
714 frag = &inode.osd2.masix2.m_i_frag;
715 fsize = &inode.osd2.masix2.m_i_fsize;
716 break;
717 default:
718 frag = fsize = 0;
719 }
720 if (frag)
721 modify_u8(argv[0], "Fragment number", decimal_format, frag);
722 if (fsize)
723 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
724
Theodore Ts'o3839e651997-04-26 13:21:57 +0000725 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
726 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000727 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000728 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000729 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000730 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000731 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000732 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000733 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000734 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000735 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000736 if (retval) {
737 com_err(argv[1], retval, "while trying to write inode %d",
738 inode_num);
739 return;
740 }
741}
742
Theodore Ts'o3839e651997-04-26 13:21:57 +0000743void do_change_working_dir(int argc, char *argv[])
744{
745 ino_t inode;
746 int retval;
747
748 if (argc != 2) {
749 com_err(argv[0], 0, "Usage: cd <file>");
750 return;
751 }
752 if (check_fs_open(argv[0]))
753 return;
754
755 inode = string_to_inode(argv[1]);
756 if (!inode)
757 return;
758
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000759 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000760 if (retval) {
761 com_err(argv[1], retval, "");
762 return;
763 }
764 cwd = inode;
765 return;
766}
767
Theodore Ts'o3839e651997-04-26 13:21:57 +0000768void do_print_working_directory(int argc, char *argv[])
769{
770 int retval;
771 char *pathname = NULL;
772
773 if (argc > 1) {
774 com_err(argv[0], 0, "Usage: print_working_directory");
775 return;
776 }
777 if (check_fs_open(argv[0]))
778 return;
779
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000780 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000781 if (retval) {
782 com_err(argv[0], retval,
783 "while trying to get pathname of cwd");
784 }
785 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
786 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000787 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000788 if (retval) {
789 com_err(argv[0], retval,
790 "while trying to get pathname of root");
791 }
792 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
793 free(pathname);
794 return;
795}
796
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000797static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000798{
799 ino_t inode;
800 int retval;
801 ino_t dir;
802 char *dest, *cp, *basename;
803
804 /*
805 * Get the source inode
806 */
807 inode = string_to_inode(sourcename);
808 if (!inode)
809 return;
810 basename = strrchr(sourcename, '/');
811 if (basename)
812 basename++;
813 else
814 basename = sourcename;
815 /*
816 * Figure out the destination. First see if it exists and is
817 * a directory.
818 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000819 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000820 dest = basename;
821 else {
822 /*
823 * OK, it doesn't exist. See if it is
824 * '<dir>/basename' or 'basename'
825 */
826 cp = strrchr(destname, '/');
827 if (cp) {
828 *cp = 0;
829 dir = string_to_inode(destname);
830 if (!dir)
831 return;
832 dest = cp+1;
833 } else {
834 dir = cwd;
835 dest = destname;
836 }
837 }
838
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000839 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000840 if (retval)
841 com_err("make_link", retval, "");
842 return;
843}
844
845
846void do_link(int argc, char *argv[])
847{
848 if (argc != 3) {
849 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
850 return;
851 }
852 if (check_fs_open(argv[0]))
853 return;
854
855 make_link(argv[1], argv[2]);
856}
857
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000858static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000859{
860 int retval;
861 ino_t dir;
862 char *basename;
863
864 basename = strrchr(filename, '/');
865 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000866 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000867 dir = string_to_inode(filename);
868 if (!dir)
869 return;
870 } else {
871 dir = cwd;
872 basename = filename;
873 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000874 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000875 if (retval)
876 com_err("unlink_file_by_name", retval, "");
877 return;
878}
879
880void do_unlink(int argc, char *argv[])
881{
882 if (argc != 2) {
883 com_err(argv[0], 0, "Usage: unlink <pathname>");
884 return;
885 }
886 if (check_fs_open(argv[0]))
887 return;
888
889 unlink_file_by_name(argv[1]);
890}
891
892void do_find_free_block(int argc, char *argv[])
893{
894 blk_t free_blk, goal;
895 errcode_t retval;
896 char *tmp;
897
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000898 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
899 com_err(argv[0], 0, "Usage: find_free_block [goal]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000900 return;
901 }
902 if (check_fs_open(argv[0]))
903 return;
904
905 if (argc > 1) {
906 goal = strtol(argv[1], &tmp, 0);
907 if (*tmp) {
908 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
909 return;
910 }
911 }
912 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000913 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000914
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000915 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000916 if (retval)
917 com_err("ext2fs_new_block", retval, "");
918 else
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000919 printf("Free block found: %d\n", free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000920
921}
922
923void do_find_free_inode(int argc, char *argv[])
924{
925 ino_t free_inode, dir;
926 int mode;
927 int retval;
928 char *tmp;
929
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000930 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
931 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000932 return;
933 }
934 if (check_fs_open(argv[0]))
935 return;
936
937 if (argc > 1) {
938 dir = strtol(argv[1], &tmp, 0);
939 if (*tmp) {
940 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
941 return;
942 }
943 }
944 else
945 dir = root;
946 if (argc > 2) {
947 mode = strtol(argv[2], &tmp, 0);
948 if (*tmp) {
949 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
950 return;
951 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000952 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000953 mode = 010755;
954
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000955 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000956 if (retval)
957 com_err("ext2fs_new_inode", retval, "");
958 else
959 printf("Free inode found: %ld\n", free_inode);
960}
961
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000962static errcode_t copy_file(int fd, ino_t newfile)
963{
Theodore Ts'o5a513841997-10-25 22:41:14 +0000964 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000965 errcode_t retval;
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000966 unsigned int got, written;
Theodore Ts'o5a513841997-10-25 22:41:14 +0000967 char buf[8192];
968 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000969
Theodore Ts'o5a513841997-10-25 22:41:14 +0000970 retval = ext2fs_file_open(current_fs, newfile,
971 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000972 if (retval)
973 return retval;
974
Theodore Ts'o5a513841997-10-25 22:41:14 +0000975 while (1) {
976 got = read(fd, buf, sizeof(buf));
977 if (got == 0)
978 break;
979 if (got < 0) {
980 retval = errno;
981 goto fail;
982 }
983 ptr = buf;
984 while (got > 0) {
985 retval = ext2fs_file_write(e2_file, ptr,
986 got, &written);
987 if (retval)
988 goto fail;
989
990 got -= written;
991 ptr += written;
992 }
993 }
994 retval = ext2fs_file_close(e2_file);
995
996 return retval;
997
998fail:
999 (void) ext2fs_file_close(e2_file);
1000 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001001}
1002
Theodore Ts'o5a513841997-10-25 22:41:14 +00001003
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001004void do_write(int argc, char *argv[])
1005{
1006 int fd;
1007 struct stat statbuf;
1008 ino_t newfile;
1009 errcode_t retval;
1010 struct ext2_inode inode;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001011 dgrp_t group;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001012
1013 if (check_fs_open(argv[0]))
1014 return;
1015 if (argc != 3) {
1016 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1017 return;
1018 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001019 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001020 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001021 fd = open(argv[1], O_RDONLY);
1022 if (fd < 0) {
1023 com_err(argv[1], fd, "");
1024 return;
1025 }
1026 if (fstat(fd, &statbuf) < 0) {
1027 com_err(argv[1], errno, "");
1028 close(fd);
1029 return;
1030 }
1031
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001032 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001033 if (retval) {
1034 com_err(argv[0], retval, "");
1035 close(fd);
1036 return;
1037 }
Theodore Ts'o5a513841997-10-25 22:41:14 +00001038 group = ext2fs_group_of_ino(current_fs, newfile);
1039 current_fs->group_desc[group].bg_free_inodes_count--;
1040 current_fs->super->s_free_inodes_count--;
1041 ext2fs_mark_super_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001042 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001043 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001044 if (retval) {
1045 com_err(argv[2], retval, "");
1046 close(fd);
1047 return;
1048 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001049 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001050 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001051 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1052 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001053 memset(&inode, 0, sizeof(inode));
1054 inode.i_mode = statbuf.st_mode;
1055 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1056 inode.i_links_count = 1;
1057 inode.i_size = statbuf.st_size;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001058 ext2fs_write_inode(current_fs, newfile, &inode);
1059 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001060 if (retval) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001061 com_err(argv[0], retval, "while trying to write inode %d",
1062 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001063 close(fd);
1064 return;
1065 }
1066 if (LINUX_S_ISREG(inode.i_mode)) {
1067 retval = copy_file(fd, newfile);
1068 if (retval)
1069 com_err("copy_file", retval, "");
1070 }
1071 close(fd);
1072}
1073
1074void do_mknod(int argc, char *argv[])
1075{
1076 unsigned long mode, major, minor, nr;
1077 ino_t newfile;
1078 errcode_t retval;
1079 struct ext2_inode inode;
1080
1081 if (check_fs_open(argv[0]))
1082 return;
1083 if (argc < 3 || argv[2][1]) {
1084 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1085 return;
1086 }
1087 mode = minor = major = 0;
1088 switch (argv[2][0]) {
1089 case 'p':
1090 mode = LINUX_S_IFIFO;
1091 nr = 3;
1092 break;
1093 case 'c':
1094 mode = LINUX_S_IFCHR;
1095 nr = 5;
1096 break;
1097 case 'b':
1098 mode = LINUX_S_IFBLK;
1099 nr = 5;
1100 break;
1101 default:
1102 nr = 0;
1103 }
1104 if (nr == 5) {
1105 major = strtoul(argv[3], argv+3, 0);
1106 minor = strtoul(argv[4], argv+4, 0);
1107 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1108 nr = 0;
1109 }
1110 if (argc != nr) {
1111 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1112 return;
1113 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001114 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001115 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001116 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001117 if (retval) {
1118 com_err(argv[0], retval, "");
1119 return;
1120 }
1121 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001122 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001123 if (retval) {
1124 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001125 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001126 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001127 retval = ext2fs_link(current_fs, cwd,
1128 argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001129 }
1130 if (retval) {
1131 com_err(argv[1], retval, "");
1132 return;
1133 }
1134 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001135 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001136 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001137 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1138 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001139 memset(&inode, 0, sizeof(inode));
1140 inode.i_mode = mode;
1141 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1142 inode.i_block[0] = major*256+minor;
1143 inode.i_links_count = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001144 ext2fs_write_inode(current_fs, newfile, &inode);
1145 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001146 if (retval) {
1147 com_err(argv[0], retval, "while trying to write inode %d", inode);
1148 return;
1149 }
1150}
1151
Theodore Ts'o3839e651997-04-26 13:21:57 +00001152void do_mkdir(int argc, char *argv[])
1153{
1154 char *cp;
1155 ino_t parent;
1156 char *name;
1157 errcode_t retval;
1158
1159 if (check_fs_open(argv[0]))
1160 return;
1161
1162 if (argc != 2) {
1163 com_err(argv[0], 0, "Usage: mkdir <file>");
1164 return;
1165 }
1166
1167 cp = strrchr(argv[1], '/');
1168 if (cp) {
1169 *cp = 0;
1170 parent = string_to_inode(argv[1]);
1171 if (!parent) {
1172 com_err(argv[1], ENOENT, "");
1173 return;
1174 }
1175 name = cp+1;
1176 } else {
1177 parent = cwd;
1178 name = argv[1];
1179 }
1180
1181
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001182 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001183 if (retval) {
1184 com_err("ext2fs_mkdir", retval, "");
1185 return;
1186 }
1187
1188}
1189
1190void do_rmdir(int argc, char *argv[])
1191{
1192 printf("Unimplemented\n");
1193}
1194
1195
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001196static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1197 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001198{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001199 printf("%d ", *blocknr);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001200 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001201 return 0;
1202}
1203
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001204static void kill_file_by_inode(ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001205{
1206 struct ext2_inode inode_buf;
1207
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001208 ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001209 inode_buf.i_dtime = time(NULL);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001210 ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001211
1212 printf("Kill file by inode %ld\n", inode);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001213 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1214 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001215 printf("\n");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001216 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001217
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001218 ext2fs_mark_bb_dirty(current_fs);
1219 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001220}
1221
1222
1223void do_kill_file(int argc, char *argv[])
1224{
1225 ino_t inode_num;
1226
1227 if (argc != 2) {
1228 com_err(argv[0], 0, "Usage: kill_file <file>");
1229 return;
1230 }
1231 if (check_fs_open(argv[0]))
1232 return;
1233
1234 inode_num = string_to_inode(argv[1]);
1235 if (!inode_num) {
1236 com_err(argv[0], 0, "Cannot find file");
1237 return;
1238 }
1239 kill_file_by_inode(inode_num);
1240}
1241
1242void do_rm(int argc, char *argv[])
1243{
1244 int retval;
1245 ino_t inode_num;
1246 struct ext2_inode inode;
1247
1248 if (argc != 2) {
1249 com_err(argv[0], 0, "Usage: rm <filename>");
1250 return;
1251 }
1252 if (check_fs_open(argv[0]))
1253 return;
1254
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001255 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001256 if (retval) {
1257 com_err(argv[0], 0, "Cannot find file");
1258 return;
1259 }
1260
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001261 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001262 if (retval) {
1263 com_err(argv[0], retval, "while reading file's inode");
1264 return;
1265 }
1266
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001267 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001268 com_err(argv[0], 0, "file is a directory");
1269 return;
1270 }
1271
1272 --inode.i_links_count;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001273 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001274 if (retval) {
1275 com_err(argv[0], retval, "while writing inode");
1276 return;
1277 }
1278
1279 unlink_file_by_name(argv[1]);
1280 if (inode.i_links_count == 0)
1281 kill_file_by_inode(inode_num);
1282}
1283
1284void do_show_debugfs_params(int argc, char *argv[])
1285{
1286 FILE *out = stdout;
1287
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001288 if (current_fs)
1289 fprintf(out, "Open mode: read-%s\n",
1290 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001291 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001292 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001293}
1294
1295void do_expand_dir(int argc, char *argv[])
1296{
1297 ino_t inode;
1298 int retval;
1299
1300 if (argc != 2) {
1301 com_err(argv[0], 0, "Usage: expand_dir <file>");
1302 return;
1303 }
1304 if (check_fs_open(argv[0]))
1305 return;
1306 inode = string_to_inode(argv[1]);
1307 if (!inode)
1308 return;
1309
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001310 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001311 if (retval)
1312 com_err("ext2fs_expand_dir", retval, "");
1313 return;
1314}
1315
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001316static int source_file(const char *cmd_file, int sci_idx)
1317{
1318 FILE *f;
1319 char buf[256];
1320 char *cp;
1321 int exit_status = 0;
1322 int retval;
1323
1324 if (strcmp(cmd_file, "-") == 0)
1325 f = stdin;
1326 else {
1327 f = fopen(cmd_file, "r");
1328 if (!f) {
1329 perror(cmd_file);
1330 exit(1);
1331 }
1332 }
1333 setbuf(stdout, NULL);
1334 setbuf(stderr, NULL);
1335 while (!feof(f)) {
1336 if (fgets(buf, sizeof(buf), f) == NULL)
1337 break;
1338 cp = strchr(buf, '\n');
1339 if (cp)
1340 *cp = 0;
1341 cp = strchr(buf, '\r');
1342 if (cp)
1343 *cp = 0;
1344 printf("debugfs: %s\n", buf);
1345 retval = ss_execute_line(sci_idx, buf);
1346 if (retval) {
1347 ss_perror(sci_idx, retval, buf);
1348 exit_status++;
1349 }
1350 }
1351 return exit_status;
1352}
1353
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001354int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001355{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001356 int retval;
1357 int sci_idx;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001358 const char *usage = "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00001359 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001360 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001361 char *request = 0;
1362 int exit_status = 0;
1363 char *cmd_file = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001364
1365 initialize_ext2_error_table();
Theodore Ts'o818180c1998-06-27 05:11:14 +00001366 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1367 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1368 EXT2FS_VERSION, EXT2FS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001369
Theodore Ts'o818180c1998-06-27 05:11:14 +00001370 while ((c = getopt (argc, argv, "wR:f:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001371 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001372 case 'R':
1373 request = optarg;
1374 break;
1375 case 'f':
1376 cmd_file = optarg;
1377 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001378 case 'w':
1379 open_flags = EXT2_FLAG_RW;
1380 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001381 case 'V':
1382 /* Print version number and exit */
1383 fprintf(stderr, "\tUsing %s\n",
1384 error_message(EXT2_ET_BASE));
1385 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001386 default:
1387 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001388 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001389 }
1390 }
1391 if (optind < argc)
1392 open_filesystem(argv[optind], open_flags);
1393
1394 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1395 &debug_cmds, &retval);
1396 if (retval) {
1397 ss_perror(sci_idx, retval, "creating invocation");
1398 exit(1);
1399 }
1400
1401 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1402 if (retval) {
1403 ss_perror(sci_idx, retval, "adding standard requests");
1404 exit (1);
1405 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001406 if (request) {
1407 retval = 0;
1408 retval = ss_execute_line(sci_idx, request);
1409 if (retval) {
1410 ss_perror(sci_idx, retval, request);
1411 exit_status++;
1412 }
1413 } else if (cmd_file) {
1414 exit_status = source_file(cmd_file, sci_idx);
1415 } else {
1416 ss_listen(sci_idx);
1417 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001418
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001419 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001420 close_filesystem();
1421
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001422 exit(exit_status);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001423}
1424