blob: ab10934415bd1f0362a63592a5a3d0b625e420b7 [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 {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000385 com_err(argv[0], retval, "while trying to read inode %d", inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000386 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) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +0000436 com_err(argv[0], retval, "while trying to read inode %d",
437 inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000438 return;
439 }
440 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000441 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000442 if (retval) {
443 com_err(argv[0], retval, "while trying to write inode %d",
444 inode);
445 return;
446 }
447}
448
449void do_freei(int argc, char *argv[])
450{
451 ino_t inode;
452
453 if (argc != 2) {
454 com_err(argv[0], 0, "Usage: freei <file>");
455 return;
456 }
457 if (check_fs_open(argv[0]))
458 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000459 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000460 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000461 inode = string_to_inode(argv[1]);
462 if (!inode)
463 return;
464
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000465 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000466 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000467 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
468 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000469}
470
471void do_seti(int argc, char *argv[])
472{
473 ino_t inode;
474
475 if (argc != 2) {
476 com_err(argv[0], 0, "Usage: seti <file>");
477 return;
478 }
479 if (check_fs_open(argv[0]))
480 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000481 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000482 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000483 inode = string_to_inode(argv[1]);
484 if (!inode)
485 return;
486
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000487 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000488 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000489 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
490 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000491}
492
493void do_testi(int argc, char *argv[])
494{
495 ino_t inode;
496
497 if (argc != 2) {
498 com_err(argv[0], 0, "Usage: testi <file>");
499 return;
500 }
501 if (check_fs_open(argv[0]))
502 return;
503 inode = string_to_inode(argv[1]);
504 if (!inode)
505 return;
506
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000507 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000508 printf("Inode %ld is marked in use\n", inode);
509 else
510 printf("Inode %ld is not in use\n", inode);
511}
512
513
514void do_freeb(int argc, char *argv[])
515{
516 blk_t block;
517 char *tmp;
518
519 if (argc != 2) {
520 com_err(argv[0], 0, "Usage: freeb <block>");
521 return;
522 }
523 if (check_fs_open(argv[0]))
524 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000525 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000526 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000527 block = strtoul(argv[1], &tmp, 0);
528 if (!block || *tmp) {
529 com_err(argv[0], 0, "No block 0");
530 return;
531 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000532 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000533 com_err(argv[0], 0, "Warning: block already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000534 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
535 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000536}
537
538void do_setb(int argc, char *argv[])
539{
540 blk_t block;
541 char *tmp;
542
543 if (argc != 2) {
544 com_err(argv[0], 0, "Usage: setb <block>");
545 return;
546 }
547 if (check_fs_open(argv[0]))
548 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000549 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000550 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000551 block = strtoul(argv[1], &tmp, 0);
552 if (!block || *tmp) {
553 com_err(argv[0], 0, "No block 0");
554 return;
555 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000556 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000557 com_err(argv[0], 0, "Warning: block already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000558 ext2fs_mark_block_bitmap(current_fs->block_map,block);
559 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000560}
561
562void do_testb(int argc, char *argv[])
563{
564 blk_t block;
565 char *tmp;
566
567 if (argc != 2) {
568 com_err(argv[0], 0, "Usage: testb <block>");
569 return;
570 }
571 if (check_fs_open(argv[0]))
572 return;
573 block = strtoul(argv[1], &tmp, 0);
574 if (!block || *tmp) {
575 com_err(argv[0], 0, "No block 0");
576 return;
577 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000578 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000579 printf("Block %d marked in use\n", block);
580 else printf("Block %d not in use\n", block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000581}
582
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000583static void modify_u8(char *com, const char *prompt,
584 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000585{
586 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000587 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000588 char *tmp;
589
590 sprintf(buf, format, *val);
591 printf("%30s [%s] ", prompt, buf);
592 fgets(buf, sizeof(buf), stdin);
593 if (buf[strlen (buf) - 1] == '\n')
594 buf[strlen (buf) - 1] = '\0';
595 if (!buf[0])
596 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000597 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 if (*tmp)
599 com_err(com, 0, "Bad value - %s", buf);
600 else
601 *val = v;
602}
603
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000604static void modify_u16(char *com, const char *prompt,
605 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000606{
607 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000608 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000609 char *tmp;
610
611 sprintf(buf, format, *val);
612 printf("%30s [%s] ", prompt, buf);
613 fgets(buf, sizeof(buf), stdin);
614 if (buf[strlen (buf) - 1] == '\n')
615 buf[strlen (buf) - 1] = '\0';
616 if (!buf[0])
617 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000618 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 if (*tmp)
620 com_err(com, 0, "Bad value - %s", buf);
621 else
622 *val = v;
623}
624
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000625static void modify_u32(char *com, const char *prompt,
626 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000627{
628 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000629 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000630 char *tmp;
631
632 sprintf(buf, format, *val);
633 printf("%30s [%s] ", prompt, buf);
634 fgets(buf, sizeof(buf), stdin);
635 if (buf[strlen (buf) - 1] == '\n')
636 buf[strlen (buf) - 1] = '\0';
637 if (!buf[0])
638 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000639 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000640 if (*tmp)
641 com_err(com, 0, "Bad value - %s", buf);
642 else
643 *val = v;
644}
645
646
647void do_modify_inode(int argc, char *argv[])
648{
649 struct ext2_inode inode;
650 ino_t inode_num;
651 int i;
652 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000653 unsigned char *frag, *fsize;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000654 char buf[80];
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000655 int os = current_fs->super->s_creator_os;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000656 const char *hex_format = "0x%x";
657 const char *octal_format = "0%o";
658 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000659
660 if (argc != 2) {
661 com_err(argv[0], 0, "Usage: modify_inode <file>");
662 return;
663 }
664 if (check_fs_open(argv[0]))
665 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000666 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000667 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000668
669 inode_num = string_to_inode(argv[1]);
670 if (!inode_num)
671 return;
672
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000673 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000674 if (retval) {
675 com_err(argv[1], retval, "while trying to read inode %d",
676 inode_num);
677 return;
678 }
679
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000680 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
681 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
682 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
683 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
684 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
685 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
686 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
687 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
688 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
689 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
690 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
691#if 0
692 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
693#endif
694 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
Theodore Ts'o36a43d61998-03-24 16:17:51 +0000695 if (LINUX_S_ISDIR(inode.i_mode))
696 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
697 else
698 modify_u32(argv[0], "High 32bits of size", decimal_format, &inode.i_size_high);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000699
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000700 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000701 modify_u32(argv[0], "Translator Block",
702 decimal_format, &inode.osd1.hurd1.h_i_translator);
703
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000704 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000705 switch (os) {
706 case EXT2_OS_LINUX:
707 frag = &inode.osd2.linux2.l_i_frag;
708 fsize = &inode.osd2.linux2.l_i_fsize;
709 break;
710 case EXT2_OS_HURD:
711 frag = &inode.osd2.hurd2.h_i_frag;
712 fsize = &inode.osd2.hurd2.h_i_fsize;
713 break;
714 case EXT2_OS_MASIX:
715 frag = &inode.osd2.masix2.m_i_frag;
716 fsize = &inode.osd2.masix2.m_i_fsize;
717 break;
718 default:
719 frag = fsize = 0;
720 }
721 if (frag)
722 modify_u8(argv[0], "Fragment number", decimal_format, frag);
723 if (fsize)
724 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
725
Theodore Ts'o3839e651997-04-26 13:21:57 +0000726 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
727 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000728 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000729 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000730 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000731 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000732 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000733 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000734 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000735 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000736 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000737 if (retval) {
738 com_err(argv[1], retval, "while trying to write inode %d",
739 inode_num);
740 return;
741 }
742}
743
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744void do_change_working_dir(int argc, char *argv[])
745{
746 ino_t inode;
747 int retval;
748
749 if (argc != 2) {
750 com_err(argv[0], 0, "Usage: cd <file>");
751 return;
752 }
753 if (check_fs_open(argv[0]))
754 return;
755
756 inode = string_to_inode(argv[1]);
757 if (!inode)
758 return;
759
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000760 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000761 if (retval) {
762 com_err(argv[1], retval, "");
763 return;
764 }
765 cwd = inode;
766 return;
767}
768
Theodore Ts'o3839e651997-04-26 13:21:57 +0000769void do_print_working_directory(int argc, char *argv[])
770{
771 int retval;
772 char *pathname = NULL;
773
774 if (argc > 1) {
775 com_err(argv[0], 0, "Usage: print_working_directory");
776 return;
777 }
778 if (check_fs_open(argv[0]))
779 return;
780
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000781 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000782 if (retval) {
783 com_err(argv[0], retval,
784 "while trying to get pathname of cwd");
785 }
786 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
787 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000788 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000789 if (retval) {
790 com_err(argv[0], retval,
791 "while trying to get pathname of root");
792 }
793 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
794 free(pathname);
795 return;
796}
797
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000798static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000799{
800 ino_t inode;
801 int retval;
802 ino_t dir;
803 char *dest, *cp, *basename;
804
805 /*
806 * Get the source inode
807 */
808 inode = string_to_inode(sourcename);
809 if (!inode)
810 return;
811 basename = strrchr(sourcename, '/');
812 if (basename)
813 basename++;
814 else
815 basename = sourcename;
816 /*
817 * Figure out the destination. First see if it exists and is
818 * a directory.
819 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000820 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000821 dest = basename;
822 else {
823 /*
824 * OK, it doesn't exist. See if it is
825 * '<dir>/basename' or 'basename'
826 */
827 cp = strrchr(destname, '/');
828 if (cp) {
829 *cp = 0;
830 dir = string_to_inode(destname);
831 if (!dir)
832 return;
833 dest = cp+1;
834 } else {
835 dir = cwd;
836 dest = destname;
837 }
838 }
839
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000840 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000841 if (retval)
842 com_err("make_link", retval, "");
843 return;
844}
845
846
847void do_link(int argc, char *argv[])
848{
849 if (argc != 3) {
850 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
851 return;
852 }
853 if (check_fs_open(argv[0]))
854 return;
855
856 make_link(argv[1], argv[2]);
857}
858
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000859static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000860{
861 int retval;
862 ino_t dir;
863 char *basename;
864
865 basename = strrchr(filename, '/');
866 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000867 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000868 dir = string_to_inode(filename);
869 if (!dir)
870 return;
871 } else {
872 dir = cwd;
873 basename = filename;
874 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000875 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000876 if (retval)
877 com_err("unlink_file_by_name", retval, "");
878 return;
879}
880
881void do_unlink(int argc, char *argv[])
882{
883 if (argc != 2) {
884 com_err(argv[0], 0, "Usage: unlink <pathname>");
885 return;
886 }
887 if (check_fs_open(argv[0]))
888 return;
889
890 unlink_file_by_name(argv[1]);
891}
892
893void do_find_free_block(int argc, char *argv[])
894{
895 blk_t free_blk, goal;
896 errcode_t retval;
897 char *tmp;
898
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000899 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
900 com_err(argv[0], 0, "Usage: find_free_block [goal]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000901 return;
902 }
903 if (check_fs_open(argv[0]))
904 return;
905
906 if (argc > 1) {
907 goal = strtol(argv[1], &tmp, 0);
908 if (*tmp) {
909 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
910 return;
911 }
912 }
913 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000914 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000915
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000916 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000917 if (retval)
918 com_err("ext2fs_new_block", retval, "");
919 else
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000920 printf("Free block found: %d\n", free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000921
922}
923
924void do_find_free_inode(int argc, char *argv[])
925{
926 ino_t free_inode, dir;
927 int mode;
928 int retval;
929 char *tmp;
930
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000931 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
932 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000933 return;
934 }
935 if (check_fs_open(argv[0]))
936 return;
937
938 if (argc > 1) {
939 dir = strtol(argv[1], &tmp, 0);
940 if (*tmp) {
941 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
942 return;
943 }
944 }
945 else
946 dir = root;
947 if (argc > 2) {
948 mode = strtol(argv[2], &tmp, 0);
949 if (*tmp) {
950 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
951 return;
952 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000953 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +0000954 mode = 010755;
955
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000956 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000957 if (retval)
958 com_err("ext2fs_new_inode", retval, "");
959 else
960 printf("Free inode found: %ld\n", free_inode);
961}
962
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000963static errcode_t copy_file(int fd, ino_t newfile)
964{
Theodore Ts'o5a513841997-10-25 22:41:14 +0000965 ext2_file_t e2_file;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000966 errcode_t retval;
Theodore Ts'o4a31c481998-03-30 01:27:25 +0000967 unsigned int got, written;
Theodore Ts'o5a513841997-10-25 22:41:14 +0000968 char buf[8192];
969 char *ptr;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000970
Theodore Ts'o5a513841997-10-25 22:41:14 +0000971 retval = ext2fs_file_open(current_fs, newfile,
972 EXT2_FILE_WRITE, &e2_file);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000973 if (retval)
974 return retval;
975
Theodore Ts'o5a513841997-10-25 22:41:14 +0000976 while (1) {
977 got = read(fd, buf, sizeof(buf));
978 if (got == 0)
979 break;
980 if (got < 0) {
981 retval = errno;
982 goto fail;
983 }
984 ptr = buf;
985 while (got > 0) {
986 retval = ext2fs_file_write(e2_file, ptr,
987 got, &written);
988 if (retval)
989 goto fail;
990
991 got -= written;
992 ptr += written;
993 }
994 }
995 retval = ext2fs_file_close(e2_file);
996
997 return retval;
998
999fail:
1000 (void) ext2fs_file_close(e2_file);
1001 return retval;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001002}
1003
Theodore Ts'o5a513841997-10-25 22:41:14 +00001004
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001005void do_write(int argc, char *argv[])
1006{
1007 int fd;
1008 struct stat statbuf;
1009 ino_t newfile;
1010 errcode_t retval;
1011 struct ext2_inode inode;
Theodore Ts'o5a513841997-10-25 22:41:14 +00001012 dgrp_t group;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001013
1014 if (check_fs_open(argv[0]))
1015 return;
1016 if (argc != 3) {
1017 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1018 return;
1019 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001020 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001021 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001022 fd = open(argv[1], O_RDONLY);
1023 if (fd < 0) {
1024 com_err(argv[1], fd, "");
1025 return;
1026 }
1027 if (fstat(fd, &statbuf) < 0) {
1028 com_err(argv[1], errno, "");
1029 close(fd);
1030 return;
1031 }
1032
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001033 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001034 if (retval) {
1035 com_err(argv[0], retval, "");
1036 close(fd);
1037 return;
1038 }
Theodore Ts'o5a513841997-10-25 22:41:14 +00001039 group = ext2fs_group_of_ino(current_fs, newfile);
1040 current_fs->group_desc[group].bg_free_inodes_count--;
1041 current_fs->super->s_free_inodes_count--;
1042 ext2fs_mark_super_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001043 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001044 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001045 if (retval) {
1046 com_err(argv[2], retval, "");
1047 close(fd);
1048 return;
1049 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001050 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001051 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001052 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1053 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001054 memset(&inode, 0, sizeof(inode));
1055 inode.i_mode = statbuf.st_mode;
1056 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1057 inode.i_links_count = 1;
1058 inode.i_size = statbuf.st_size;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001059 ext2fs_write_inode(current_fs, newfile, &inode);
1060 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001061 if (retval) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001062 com_err(argv[0], retval, "while trying to write inode %d",
1063 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001064 close(fd);
1065 return;
1066 }
1067 if (LINUX_S_ISREG(inode.i_mode)) {
1068 retval = copy_file(fd, newfile);
1069 if (retval)
1070 com_err("copy_file", retval, "");
1071 }
1072 close(fd);
1073}
1074
1075void do_mknod(int argc, char *argv[])
1076{
1077 unsigned long mode, major, minor, nr;
1078 ino_t newfile;
1079 errcode_t retval;
1080 struct ext2_inode inode;
1081
1082 if (check_fs_open(argv[0]))
1083 return;
1084 if (argc < 3 || argv[2][1]) {
1085 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1086 return;
1087 }
1088 mode = minor = major = 0;
1089 switch (argv[2][0]) {
1090 case 'p':
1091 mode = LINUX_S_IFIFO;
1092 nr = 3;
1093 break;
1094 case 'c':
1095 mode = LINUX_S_IFCHR;
1096 nr = 5;
1097 break;
1098 case 'b':
1099 mode = LINUX_S_IFBLK;
1100 nr = 5;
1101 break;
1102 default:
1103 nr = 0;
1104 }
1105 if (nr == 5) {
1106 major = strtoul(argv[3], argv+3, 0);
1107 minor = strtoul(argv[4], argv+4, 0);
1108 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1109 nr = 0;
1110 }
1111 if (argc != nr) {
1112 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1113 return;
1114 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001115 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001116 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001117 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001118 if (retval) {
1119 com_err(argv[0], retval, "");
1120 return;
1121 }
1122 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001123 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001124 if (retval) {
1125 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001126 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001127 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001128 retval = ext2fs_link(current_fs, cwd,
1129 argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001130 }
1131 if (retval) {
1132 com_err(argv[1], retval, "");
1133 return;
1134 }
1135 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001136 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001137 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001138 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1139 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001140 memset(&inode, 0, sizeof(inode));
1141 inode.i_mode = mode;
1142 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1143 inode.i_block[0] = major*256+minor;
1144 inode.i_links_count = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001145 ext2fs_write_inode(current_fs, newfile, &inode);
1146 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001147 if (retval) {
1148 com_err(argv[0], retval, "while trying to write inode %d", inode);
1149 return;
1150 }
1151}
1152
Theodore Ts'o3839e651997-04-26 13:21:57 +00001153void do_mkdir(int argc, char *argv[])
1154{
1155 char *cp;
1156 ino_t parent;
1157 char *name;
1158 errcode_t retval;
1159
1160 if (check_fs_open(argv[0]))
1161 return;
1162
1163 if (argc != 2) {
1164 com_err(argv[0], 0, "Usage: mkdir <file>");
1165 return;
1166 }
1167
1168 cp = strrchr(argv[1], '/');
1169 if (cp) {
1170 *cp = 0;
1171 parent = string_to_inode(argv[1]);
1172 if (!parent) {
1173 com_err(argv[1], ENOENT, "");
1174 return;
1175 }
1176 name = cp+1;
1177 } else {
1178 parent = cwd;
1179 name = argv[1];
1180 }
1181
1182
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001183 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001184 if (retval) {
1185 com_err("ext2fs_mkdir", retval, "");
1186 return;
1187 }
1188
1189}
1190
1191void do_rmdir(int argc, char *argv[])
1192{
1193 printf("Unimplemented\n");
1194}
1195
1196
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001197static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1198 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001199{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001200 printf("%d ", *blocknr);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001201 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001202 return 0;
1203}
1204
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001205static void kill_file_by_inode(ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001206{
1207 struct ext2_inode inode_buf;
1208
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001209 ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001210 inode_buf.i_dtime = time(NULL);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001211 ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001212
1213 printf("Kill file by inode %ld\n", inode);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001214 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1215 release_blocks_proc, NULL);
Theodore Ts'o521e3681997-04-29 17:48:10 +00001216 printf("\n");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001217 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001218
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001219 ext2fs_mark_bb_dirty(current_fs);
1220 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001221}
1222
1223
1224void do_kill_file(int argc, char *argv[])
1225{
1226 ino_t inode_num;
1227
1228 if (argc != 2) {
1229 com_err(argv[0], 0, "Usage: kill_file <file>");
1230 return;
1231 }
1232 if (check_fs_open(argv[0]))
1233 return;
1234
1235 inode_num = string_to_inode(argv[1]);
1236 if (!inode_num) {
1237 com_err(argv[0], 0, "Cannot find file");
1238 return;
1239 }
1240 kill_file_by_inode(inode_num);
1241}
1242
1243void do_rm(int argc, char *argv[])
1244{
1245 int retval;
1246 ino_t inode_num;
1247 struct ext2_inode inode;
1248
1249 if (argc != 2) {
1250 com_err(argv[0], 0, "Usage: rm <filename>");
1251 return;
1252 }
1253 if (check_fs_open(argv[0]))
1254 return;
1255
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001256 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001257 if (retval) {
Theodore Ts'o91d6d481998-08-01 01:03:39 +00001258 com_err(argv[0], retval, "while trying to resolve filename");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001259 return;
1260 }
1261
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001262 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001263 if (retval) {
1264 com_err(argv[0], retval, "while reading file's inode");
1265 return;
1266 }
1267
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001268 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001269 com_err(argv[0], 0, "file is a directory");
1270 return;
1271 }
1272
1273 --inode.i_links_count;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001274 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001275 if (retval) {
1276 com_err(argv[0], retval, "while writing inode");
1277 return;
1278 }
1279
1280 unlink_file_by_name(argv[1]);
1281 if (inode.i_links_count == 0)
1282 kill_file_by_inode(inode_num);
1283}
1284
1285void do_show_debugfs_params(int argc, char *argv[])
1286{
1287 FILE *out = stdout;
1288
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001289 if (current_fs)
1290 fprintf(out, "Open mode: read-%s\n",
1291 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001292 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001293 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001294}
1295
1296void do_expand_dir(int argc, char *argv[])
1297{
1298 ino_t inode;
1299 int retval;
1300
1301 if (argc != 2) {
1302 com_err(argv[0], 0, "Usage: expand_dir <file>");
1303 return;
1304 }
1305 if (check_fs_open(argv[0]))
1306 return;
1307 inode = string_to_inode(argv[1]);
1308 if (!inode)
1309 return;
1310
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001311 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001312 if (retval)
1313 com_err("ext2fs_expand_dir", retval, "");
1314 return;
1315}
1316
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001317static int source_file(const char *cmd_file, int sci_idx)
1318{
1319 FILE *f;
1320 char buf[256];
1321 char *cp;
1322 int exit_status = 0;
1323 int retval;
1324
1325 if (strcmp(cmd_file, "-") == 0)
1326 f = stdin;
1327 else {
1328 f = fopen(cmd_file, "r");
1329 if (!f) {
1330 perror(cmd_file);
1331 exit(1);
1332 }
1333 }
1334 setbuf(stdout, NULL);
1335 setbuf(stderr, NULL);
1336 while (!feof(f)) {
1337 if (fgets(buf, sizeof(buf), f) == NULL)
1338 break;
1339 cp = strchr(buf, '\n');
1340 if (cp)
1341 *cp = 0;
1342 cp = strchr(buf, '\r');
1343 if (cp)
1344 *cp = 0;
1345 printf("debugfs: %s\n", buf);
1346 retval = ss_execute_line(sci_idx, buf);
1347 if (retval) {
1348 ss_perror(sci_idx, retval, buf);
1349 exit_status++;
1350 }
1351 }
1352 return exit_status;
1353}
1354
Theodore Ts'oa8859ca1997-09-16 02:08:28 +00001355int main(int argc, char **argv)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001356{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001357 int retval;
1358 int sci_idx;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001359 const char *usage = "Usage: debugfs [-f cmd_file] [-R request] [-V] [[-w] device]";
Theodore Ts'of1304811997-10-25 03:51:53 +00001360 int c;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001361 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001362 char *request = 0;
1363 int exit_status = 0;
1364 char *cmd_file = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001365
1366 initialize_ext2_error_table();
Theodore Ts'o818180c1998-06-27 05:11:14 +00001367 fprintf (stderr, "debugfs %s, %s for EXT2 FS %s, %s\n",
1368 E2FSPROGS_VERSION, E2FSPROGS_DATE,
1369 EXT2FS_VERSION, EXT2FS_DATE);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001370
Theodore Ts'o818180c1998-06-27 05:11:14 +00001371 while ((c = getopt (argc, argv, "wR:f:V")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001372 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001373 case 'R':
1374 request = optarg;
1375 break;
1376 case 'f':
1377 cmd_file = optarg;
1378 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001379 case 'w':
1380 open_flags = EXT2_FLAG_RW;
1381 break;
Theodore Ts'o818180c1998-06-27 05:11:14 +00001382 case 'V':
1383 /* Print version number and exit */
1384 fprintf(stderr, "\tUsing %s\n",
1385 error_message(EXT2_ET_BASE));
1386 exit(0);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001387 default:
1388 com_err(argv[0], 0, usage);
Theodore Ts'ob4ac9cc1997-10-15 01:54:48 +00001389 return 1;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001390 }
1391 }
1392 if (optind < argc)
1393 open_filesystem(argv[optind], open_flags);
1394
1395 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1396 &debug_cmds, &retval);
1397 if (retval) {
1398 ss_perror(sci_idx, retval, "creating invocation");
1399 exit(1);
1400 }
1401
1402 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1403 if (retval) {
1404 ss_perror(sci_idx, retval, "adding standard requests");
1405 exit (1);
1406 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001407 if (request) {
1408 retval = 0;
1409 retval = ss_execute_line(sci_idx, request);
1410 if (retval) {
1411 ss_perror(sci_idx, retval, request);
1412 exit_status++;
1413 }
1414 } else if (cmd_file) {
1415 exit_status = source_file(cmd_file, sci_idx);
1416 } else {
1417 ss_listen(sci_idx);
1418 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001419
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001420 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001421 close_filesystem();
1422
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001423 exit(exit_status);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001424}
1425