blob: 0797a8897de652f61c1c1c118edcd92cf6e22f16 [file] [log] [blame]
Theodore Ts'o3839e651997-04-26 13:21:57 +00001/*
2 * debugfs.c --- a program which allows you to attach an ext2fs
3 * filesystem and play with it.
4 *
5 * Copyright (C) 1993 Theodore Ts'o. This file may be redistributed
6 * under the terms of the GNU Public License.
7 *
8 * Modifications by Robert Sanders <gt8134b@prism.gatech.edu>
9 */
10
11#include <stdio.h>
12#include <unistd.h>
13#include <stdlib.h>
14#include <ctype.h>
15#include <string.h>
16#include <time.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000017#ifdef HAVE_GETOPT_H
Theodore Ts'o3839e651997-04-26 13:21:57 +000018#include <getopt.h>
Theodore Ts'o50e1e101997-04-26 13:58:21 +000019#else
20extern int optind;
21extern char *optarg;
22#endif
23#ifdef HAVE_OPTRESET
24extern int optreset; /* defined by BSD, but not others */
25#endif
26#ifdef HAVE_ERRNO_H
27#include <errno.h>
28#endif
29#include <fcntl.h>
Theodore Ts'o3839e651997-04-26 13:21:57 +000030#include <sys/types.h>
31#include <sys/stat.h>
32
33#include "et/com_err.h"
34#include "ss/ss.h"
35#include "debugfs.h"
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000036#include "uuid/uuid.h"
Theodore Ts'o3839e651997-04-26 13:21:57 +000037
38extern ss_request_table debug_cmds;
39
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000040ext2_filsys current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000041ino_t root, cwd;
42
Theodore Ts'o50e1e101997-04-26 13:58:21 +000043static void open_filesystem(char *device, int open_flags)
Theodore Ts'o3839e651997-04-26 13:21:57 +000044{
45 int retval;
46
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000047 retval = ext2fs_open(device, open_flags, 0, 0,
48 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000049 if (retval) {
50 com_err(device, retval, "while opening filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000051 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000052 return;
53 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000054 retval = ext2fs_read_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000055 if (retval) {
56 com_err(device, retval, "while reading inode bitmap");
57 goto errout;
58 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000059 retval = ext2fs_read_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000060 if (retval) {
61 com_err(device, retval, "while reading block bitmap");
62 goto errout;
63 }
64 root = cwd = EXT2_ROOT_INO;
65 return;
66
67errout:
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000068 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +000069 if (retval)
70 com_err(device, retval, "while trying to close filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000071 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +000072}
73
74void do_open_filesys(int argc, char **argv)
75{
Theodore Ts'o50e1e101997-04-26 13:58:21 +000076 const char *usage = "Usage: open [-w] <device>";
Theodore Ts'o3839e651997-04-26 13:21:57 +000077 char c;
78 int open_flags = 0;
79
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +000080 optind = 0;
Theodore Ts'o50e1e101997-04-26 13:58:21 +000081#ifdef HAVE_OPTRESET
82 optreset = 1; /* Makes BSD getopt happy */
83#endif
Theodore Ts'o3839e651997-04-26 13:21:57 +000084 while ((c = getopt (argc, argv, "w")) != EOF) {
85 switch (c) {
86 case 'w':
87 open_flags = EXT2_FLAG_RW;
88 break;
89 default:
90 com_err(argv[0], 0, usage);
91 return;
92 }
93 }
94 if (optind != argc-1) {
95 com_err(argv[0], 0, usage);
96 return;
97 }
98 if (check_fs_not_open(argv[0]))
99 return;
100 open_filesystem(argv[optind], open_flags);
101}
102
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000103static void close_filesystem(NOARGS)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000104{
105 int retval;
106
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000107 if (current_fs->flags & EXT2_FLAG_IB_DIRTY) {
108 retval = ext2fs_write_inode_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000109 if (retval)
110 com_err("ext2fs_write_inode_bitmap", retval, "");
111 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000112 if (current_fs->flags & EXT2_FLAG_BB_DIRTY) {
113 retval = ext2fs_write_block_bitmap(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000114 if (retval)
115 com_err("ext2fs_write_block_bitmap", retval, "");
116 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000117 retval = ext2fs_close(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000118 if (retval)
119 com_err("ext2fs_close", retval, "");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000120 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000121 return;
122}
123
124void do_close_filesys(int argc, char **argv)
125{
126 if (argc > 1) {
127 com_err(argv[0], 0, "Usage: close_filesys");
128 return;
129 }
130 if (check_fs_open(argv[0]))
131 return;
132 close_filesystem();
133}
134
135void do_init_filesys(int argc, char **argv)
136{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000137 const char *usage = "Usage: initialize <device> <blocksize>";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000138 struct ext2_super_block param;
139 errcode_t retval;
140 char *tmp;
141
142 if (argc != 3) {
143 com_err(argv[0], 0, usage);
144 return;
145 }
146 if (check_fs_not_open(argv[0]))
147 return;
148
149 memset(&param, 0, sizeof(struct ext2_super_block));
150 param.s_blocks_count = strtoul(argv[2], &tmp, 0);
151 if (*tmp) {
152 com_err(argv[0], 0, "Bad blocks count - %s", argv[2]);
153 return;
154 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000155 retval = ext2fs_initialize(argv[1], 0, &param,
156 unix_io_manager, &current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000157 if (retval) {
158 com_err(argv[1], retval, "while initializing filesystem");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000159 current_fs = NULL;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000160 return;
161 }
162 root = cwd = EXT2_ROOT_INO;
163 return;
164}
165
166void do_show_super_stats(int argc, char *argv[])
167{
168 int i;
169 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000170 struct ext2fs_sb *sb;
171 struct ext2_group_desc *gdp;
172 char buf[80];
173 const char *none = "(none)";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000174
175 if (argc > 1) {
176 com_err(argv[0], 0, "Usage: show_super");
177 return;
178 }
179 if (check_fs_open(argv[0]))
180 return;
181 out = open_pager();
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000182 sb = (struct ext2fs_sb *) current_fs->super;
183 fprintf(out, "Filesystem is read-%s\n",
184 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
185 if (sb->s_volume_name[0]) {
186 memset(buf, 0, sizeof(buf));
187 strncpy(buf, sb->s_volume_name, sizeof(sb->s_volume_name));
188 } else
189 strcpy(buf, none);
190 fprintf(out, "Volume name = %s\n", buf);
191 if (sb->s_last_mounted[0]) {
192 memset(buf, 0, sizeof(buf));
193 strncpy(buf, sb->s_last_mounted, sizeof(sb->s_last_mounted));
194 } else
195 strcpy(buf, none);
196 fprintf(out, "Last mounted directory = %s\n", buf);
197 if (!uuid_is_null(sb->s_uuid))
198 uuid_unparse(sb->s_uuid, buf);
199 else
200 strcpy(buf, none);
201 fprintf(out, "Filesystem UUID = %s\n", buf);
202 fprintf(out, "Last mount time = %s", time_to_string(sb->s_mtime));
203 fprintf(out, "Last write time = %s", time_to_string(sb->s_wtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000204 fprintf(out, "Mount counts = %d (maximal = %d)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000205 sb->s_mnt_count, sb->s_max_mnt_count);
206 fputs ("Filesystem OS type = ", out);
207 switch (sb->s_creator_os) {
208 case EXT2_OS_LINUX: fputs ("Linux\n", out); break;
209 case EXT2_OS_HURD: fputs ("GNU\n", out); break;
210 case EXT2_OS_MASIX: fputs ("Masix\n", out); break;
211 default: fputs ("unknown\n", out);
212 }
213 fprintf(out, "Superblock size = %d\n",
214 sizeof(struct ext2_super_block));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000215 fprintf(out, "Block size = %d, fragment size = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000216 EXT2_BLOCK_SIZE(sb), EXT2_FRAG_SIZE(sb));
217 fprintf(out, "Inode size = %d\n", EXT2_INODE_SIZE(sb));
218 fprintf(out, "%d inodes, %d free\n", sb->s_inodes_count,
219 sb->s_free_inodes_count);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000220 fprintf(out, "%d blocks, %d free, %d reserved, first block = %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000221 sb->s_blocks_count, sb->s_free_blocks_count,
222 sb->s_r_blocks_count, sb->s_first_data_block);
223 fprintf(out, "%d blocks per group\n", sb->s_blocks_per_group);
224 fprintf(out, "%d fragments per group\n", sb->s_frags_per_group);
225 fprintf(out, "%d inodes per group\n", EXT2_INODES_PER_GROUP(sb));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000226 fprintf(out, "%ld group%s (%ld descriptors block%s)\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000227 current_fs->group_desc_count,
228 (current_fs->group_desc_count != 1) ? "s" : "",
229 current_fs->desc_blocks,
230 (current_fs->desc_blocks != 1) ? "s" : "");
231
232 gdp = &current_fs->group_desc[0];
233 for (i = 0; i < current_fs->group_desc_count; i++, gdp++)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000234 fprintf(out, " Group %2d: block bitmap at %d, "
235 "inode bitmap at %d, "
236 "inode table at %d\n"
Theodore Ts'o3839e651997-04-26 13:21:57 +0000237 " %d free block%s, "
238 "%d free inode%s, "
239 "%d used director%s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000240 i, gdp->bg_block_bitmap,
241 gdp->bg_inode_bitmap, gdp->bg_inode_table,
242 gdp->bg_free_blocks_count,
243 gdp->bg_free_blocks_count != 1 ? "s" : "",
244 gdp->bg_free_inodes_count,
245 gdp->bg_free_inodes_count != 1 ? "s" : "",
246 gdp->bg_used_dirs_count,
247 gdp->bg_used_dirs_count != 1 ? "ies" : "y");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000248 close_pager(out);
249}
250
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000251void do_dirty_filesys(int argc, char *argv[])
252{
253 if (check_fs_open(argv[0]))
254 return;
255
256 ext2fs_mark_super_dirty(current_fs);
257}
258
Theodore Ts'o3839e651997-04-26 13:21:57 +0000259struct list_blocks_struct {
260 FILE *f;
261 int total;
262};
263
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000264static int list_blocks_proc(ext2_filsys fs, blk_t *blocknr, int blockcnt,
265 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000266{
267 struct list_blocks_struct *lb = (struct list_blocks_struct *) private;
268
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000269 fprintf(lb->f, "%d ", *blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000270 lb->total++;
271 return 0;
272}
273
274
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000275static void dump_blocks(FILE *f, ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000276{
277 struct list_blocks_struct lb;
278
279 fprintf(f, "BLOCKS:\n");
280 lb.total = 0;
281 lb.f = f;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000282 ext2fs_block_iterate(current_fs, inode, 0, NULL,
283 list_blocks_proc, (void *)&lb);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000284 if (lb.total)
285 fprintf(f, "\nTOTAL: %d\n", lb.total);
286 fprintf(f,"\n");
287}
288
289
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000290static void dump_inode(ino_t inode_num, struct ext2_inode inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000291{
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000292 const char *i_type;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000293 FILE *out;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000294 char frag, fsize;
295 int os = current_fs->super->s_creator_os;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000296
297 out = open_pager();
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000298 if (LINUX_S_ISDIR(inode.i_mode)) i_type = "directory";
299 else if (LINUX_S_ISREG(inode.i_mode)) i_type = "regular";
300 else if (LINUX_S_ISLNK(inode.i_mode)) i_type = "symlink";
301 else if (LINUX_S_ISBLK(inode.i_mode)) i_type = "block special";
302 else if (LINUX_S_ISCHR(inode.i_mode)) i_type = "character special";
303 else if (LINUX_S_ISFIFO(inode.i_mode)) i_type = "FIFO";
304 else if (LINUX_S_ISSOCK(inode.i_mode)) i_type = "socket";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000305 else i_type = "bad type";
306 fprintf(out, "Inode: %ld Type: %s ", inode_num, i_type);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000307 fprintf(out, "Mode: %04o Flags: 0x%x Version: %d\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000308 inode.i_mode & 0777, inode.i_flags, inode.i_version);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000309 fprintf(out, "User: %5d Group: %5d Size: %d\n",
Theodore Ts'o3839e651997-04-26 13:21:57 +0000310 inode.i_uid, inode.i_gid, inode.i_size);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000311 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000312 fprintf(out,
313 "File ACL: %d Directory ACL: %d Translator: %d\n",
314 inode.i_file_acl, inode.i_dir_acl,
315 inode.osd1.hurd1.h_i_translator);
316 else
317 fprintf(out, "File ACL: %d Directory ACL: %d\n",
318 inode.i_file_acl, inode.i_dir_acl);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000319 fprintf(out, "Links: %d Blockcount: %d\n", inode.i_links_count,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000320 inode.i_blocks);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000321 switch (os) {
322 case EXT2_OS_LINUX:
323 frag = inode.osd2.linux2.l_i_frag;
324 fsize = inode.osd2.linux2.l_i_fsize;
325 break;
326 case EXT2_OS_HURD:
327 frag = inode.osd2.hurd2.h_i_frag;
328 fsize = inode.osd2.hurd2.h_i_fsize;
329 break;
330 case EXT2_OS_MASIX:
331 frag = inode.osd2.masix2.m_i_frag;
332 fsize = inode.osd2.masix2.m_i_fsize;
333 break;
334 default:
335 frag = fsize = 0;
336 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000337 fprintf(out, "Fragment: Address: %d Number: %d Size: %d\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000338 inode.i_faddr, frag, fsize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000339 fprintf(out, "ctime: 0x%08x -- %s", inode.i_ctime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000340 time_to_string(inode.i_ctime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000341 fprintf(out, "atime: 0x%08x -- %s", inode.i_atime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000342 time_to_string(inode.i_atime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000343 fprintf(out, "mtime: 0x%08x -- %s", inode.i_mtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000344 time_to_string(inode.i_mtime));
Theodore Ts'o3839e651997-04-26 13:21:57 +0000345 if (inode.i_dtime)
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000346 fprintf(out, "dtime: 0x%08x -- %s", inode.i_dtime,
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000347 time_to_string(inode.i_dtime));
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000348 if (LINUX_S_ISLNK(inode.i_mode) && inode.i_blocks == 0)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000349 fprintf(out, "Fast_link_dest: %s\n", (char *)inode.i_block);
350 else
351 dump_blocks(out, inode_num);
352 close_pager(out);
353}
354
355
356void do_stat(int argc, char *argv[])
357{
358 ino_t inode;
359 struct ext2_inode inode_buf;
360 int retval;
361
362 if (argc != 2) {
363 com_err(argv[0], 0, "Usage: stat <file>");
364 return;
365 }
366 if (check_fs_open(argv[0]))
367 return;
368 inode = string_to_inode(argv[1]);
369 if (!inode)
370 return;
371
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000372 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000373 if (retval)
374 {
375 com_err(argv[0], 0, "Reading inode");
376 return;
377 }
378
379 dump_inode(inode,inode_buf);
380 return;
381}
382
383void do_chroot(int argc, char *argv[])
384{
385 ino_t inode;
386 int retval;
387
388 if (argc != 2) {
389 com_err(argv[0], 0, "Usage: chroot <file>");
390 return;
391 }
392 if (check_fs_open(argv[0]))
393 return;
394 inode = string_to_inode(argv[1]);
395 if (!inode)
396 return;
397
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000398 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000399 if (retval) {
400 com_err(argv[1], retval, "");
401 return;
402 }
403 root = inode;
404}
405
406void do_clri(int argc, char *argv[])
407{
408 ino_t inode;
409 int retval;
410 struct ext2_inode inode_buf;
411
412 if (argc != 2) {
413 com_err(argv[0], 0, "Usage: clri <file>");
414 return;
415 }
416 if (check_fs_open(argv[0]))
417 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000418 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000419 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000420 inode = string_to_inode(argv[1]);
421 if (!inode)
422 return;
423
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000424 retval = ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000425 if (retval) {
426 com_err(argv[0], 0, "while trying to read inode %d", inode);
427 return;
428 }
429 memset(&inode_buf, 0, sizeof(inode_buf));
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000430 retval = ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000431 if (retval) {
432 com_err(argv[0], retval, "while trying to write inode %d",
433 inode);
434 return;
435 }
436}
437
438void do_freei(int argc, char *argv[])
439{
440 ino_t inode;
441
442 if (argc != 2) {
443 com_err(argv[0], 0, "Usage: freei <file>");
444 return;
445 }
446 if (check_fs_open(argv[0]))
447 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000448 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000449 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000450 inode = string_to_inode(argv[1]);
451 if (!inode)
452 return;
453
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000454 if (!ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000455 com_err(argv[0], 0, "Warning: inode already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000456 ext2fs_unmark_inode_bitmap(current_fs->inode_map,inode);
457 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000458}
459
460void do_seti(int argc, char *argv[])
461{
462 ino_t inode;
463
464 if (argc != 2) {
465 com_err(argv[0], 0, "Usage: seti <file>");
466 return;
467 }
468 if (check_fs_open(argv[0]))
469 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000470 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000471 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000472 inode = string_to_inode(argv[1]);
473 if (!inode)
474 return;
475
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000476 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000477 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000478 ext2fs_mark_inode_bitmap(current_fs->inode_map,inode);
479 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000480}
481
482void do_testi(int argc, char *argv[])
483{
484 ino_t inode;
485
486 if (argc != 2) {
487 com_err(argv[0], 0, "Usage: testi <file>");
488 return;
489 }
490 if (check_fs_open(argv[0]))
491 return;
492 inode = string_to_inode(argv[1]);
493 if (!inode)
494 return;
495
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000496 if (ext2fs_test_inode_bitmap(current_fs->inode_map,inode))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000497 printf("Inode %ld is marked in use\n", inode);
498 else
499 printf("Inode %ld is not in use\n", inode);
500}
501
502
503void do_freeb(int argc, char *argv[])
504{
505 blk_t block;
506 char *tmp;
507
508 if (argc != 2) {
509 com_err(argv[0], 0, "Usage: freeb <block>");
510 return;
511 }
512 if (check_fs_open(argv[0]))
513 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000514 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000515 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000516 block = strtoul(argv[1], &tmp, 0);
517 if (!block || *tmp) {
518 com_err(argv[0], 0, "No block 0");
519 return;
520 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000521 if (!ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000522 com_err(argv[0], 0, "Warning: block already clear");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000523 ext2fs_unmark_block_bitmap(current_fs->block_map,block);
524 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000525}
526
527void do_setb(int argc, char *argv[])
528{
529 blk_t block;
530 char *tmp;
531
532 if (argc != 2) {
533 com_err(argv[0], 0, "Usage: setb <block>");
534 return;
535 }
536 if (check_fs_open(argv[0]))
537 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000538 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000539 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000540 block = strtoul(argv[1], &tmp, 0);
541 if (!block || *tmp) {
542 com_err(argv[0], 0, "No block 0");
543 return;
544 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000545 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000546 com_err(argv[0], 0, "Warning: block already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000547 ext2fs_mark_block_bitmap(current_fs->block_map,block);
548 ext2fs_mark_bb_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000549}
550
551void do_testb(int argc, char *argv[])
552{
553 blk_t block;
554 char *tmp;
555
556 if (argc != 2) {
557 com_err(argv[0], 0, "Usage: testb <block>");
558 return;
559 }
560 if (check_fs_open(argv[0]))
561 return;
562 block = strtoul(argv[1], &tmp, 0);
563 if (!block || *tmp) {
564 com_err(argv[0], 0, "No block 0");
565 return;
566 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000567 if (ext2fs_test_block_bitmap(current_fs->block_map,block))
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000568 printf("Block %d marked in use\n", block);
569 else printf("Block %d not in use\n", block);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000570}
571
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000572static void modify_u8(char *com, const char *prompt,
573 const char *format, __u8 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000574{
575 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000576 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000577 char *tmp;
578
579 sprintf(buf, format, *val);
580 printf("%30s [%s] ", prompt, buf);
581 fgets(buf, sizeof(buf), stdin);
582 if (buf[strlen (buf) - 1] == '\n')
583 buf[strlen (buf) - 1] = '\0';
584 if (!buf[0])
585 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000586 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000587 if (*tmp)
588 com_err(com, 0, "Bad value - %s", buf);
589 else
590 *val = v;
591}
592
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000593static void modify_u16(char *com, const char *prompt,
594 const char *format, __u16 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000595{
596 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000597 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000598 char *tmp;
599
600 sprintf(buf, format, *val);
601 printf("%30s [%s] ", prompt, buf);
602 fgets(buf, sizeof(buf), stdin);
603 if (buf[strlen (buf) - 1] == '\n')
604 buf[strlen (buf) - 1] = '\0';
605 if (!buf[0])
606 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000607 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000608 if (*tmp)
609 com_err(com, 0, "Bad value - %s", buf);
610 else
611 *val = v;
612}
613
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000614static void modify_u32(char *com, const char *prompt,
615 const char *format, __u32 *val)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000616{
617 char buf[200];
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000618 unsigned long v;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000619 char *tmp;
620
621 sprintf(buf, format, *val);
622 printf("%30s [%s] ", prompt, buf);
623 fgets(buf, sizeof(buf), stdin);
624 if (buf[strlen (buf) - 1] == '\n')
625 buf[strlen (buf) - 1] = '\0';
626 if (!buf[0])
627 return;
Theodore Ts'o21c84b71997-04-29 16:15:03 +0000628 v = strtoul(buf, &tmp, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000629 if (*tmp)
630 com_err(com, 0, "Bad value - %s", buf);
631 else
632 *val = v;
633}
634
635
636void do_modify_inode(int argc, char *argv[])
637{
638 struct ext2_inode inode;
639 ino_t inode_num;
640 int i;
641 errcode_t retval;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000642 unsigned char *frag, *fsize;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000643 char buf[80];
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000644 int os = current_fs->super->s_creator_os;
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000645 const char *hex_format = "0x%x";
646 const char *octal_format = "0%o";
647 const char *decimal_format = "%d";
Theodore Ts'o3839e651997-04-26 13:21:57 +0000648
649 if (argc != 2) {
650 com_err(argv[0], 0, "Usage: modify_inode <file>");
651 return;
652 }
653 if (check_fs_open(argv[0]))
654 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000655 if (check_fs_read_write(argv[0]))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000656 return;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000657
658 inode_num = string_to_inode(argv[1]);
659 if (!inode_num)
660 return;
661
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000662 retval = ext2fs_read_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000663 if (retval) {
664 com_err(argv[1], retval, "while trying to read inode %d",
665 inode_num);
666 return;
667 }
668
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000669 modify_u16(argv[0], "Mode", octal_format, &inode.i_mode);
670 modify_u16(argv[0], "User ID", decimal_format, &inode.i_uid);
671 modify_u16(argv[0], "Group ID", decimal_format, &inode.i_gid);
672 modify_u32(argv[0], "Size", decimal_format, &inode.i_size);
673 modify_u32(argv[0], "Creation time", decimal_format, &inode.i_ctime);
674 modify_u32(argv[0], "Modification time", decimal_format, &inode.i_mtime);
675 modify_u32(argv[0], "Access time", decimal_format, &inode.i_atime);
676 modify_u32(argv[0], "Deletion time", decimal_format, &inode.i_dtime);
677 modify_u16(argv[0], "Link count", decimal_format, &inode.i_links_count);
678 modify_u32(argv[0], "Block count", decimal_format, &inode.i_blocks);
679 modify_u32(argv[0], "File flags", hex_format, &inode.i_flags);
680#if 0
681 modify_u32(argv[0], "Reserved1", decimal_format, &inode.i_reserved1);
682#endif
683 modify_u32(argv[0], "File acl", decimal_format, &inode.i_file_acl);
684 modify_u32(argv[0], "Directory acl", decimal_format, &inode.i_dir_acl);
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000685
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000686 if (current_fs->super->s_creator_os == EXT2_OS_HURD)
Theodore Ts'o62c06f71997-04-29 14:34:47 +0000687 modify_u32(argv[0], "Translator Block",
688 decimal_format, &inode.osd1.hurd1.h_i_translator);
689
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000690 modify_u32(argv[0], "Fragment address", decimal_format, &inode.i_faddr);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000691 switch (os) {
692 case EXT2_OS_LINUX:
693 frag = &inode.osd2.linux2.l_i_frag;
694 fsize = &inode.osd2.linux2.l_i_fsize;
695 break;
696 case EXT2_OS_HURD:
697 frag = &inode.osd2.hurd2.h_i_frag;
698 fsize = &inode.osd2.hurd2.h_i_fsize;
699 break;
700 case EXT2_OS_MASIX:
701 frag = &inode.osd2.masix2.m_i_frag;
702 fsize = &inode.osd2.masix2.m_i_fsize;
703 break;
704 default:
705 frag = fsize = 0;
706 }
707 if (frag)
708 modify_u8(argv[0], "Fragment number", decimal_format, frag);
709 if (fsize)
710 modify_u8(argv[0], "Fragment size", decimal_format, fsize);
711
Theodore Ts'o3839e651997-04-26 13:21:57 +0000712 for (i=0; i < EXT2_NDIR_BLOCKS; i++) {
713 sprintf(buf, "Direct Block #%d", i);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000714 modify_u32(argv[0], buf, decimal_format, &inode.i_block[i]);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000715 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000716 modify_u32(argv[0], "Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000717 &inode.i_block[EXT2_IND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000718 modify_u32(argv[0], "Double Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000719 &inode.i_block[EXT2_DIND_BLOCK]);
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000720 modify_u32(argv[0], "Triple Indirect Block", decimal_format,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000721 &inode.i_block[EXT2_TIND_BLOCK]);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000722 retval = ext2fs_write_inode(current_fs, inode_num, &inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000723 if (retval) {
724 com_err(argv[1], retval, "while trying to write inode %d",
725 inode_num);
726 return;
727 }
728}
729
730/*
731 * list directory
732 */
733
734struct list_dir_struct {
735 FILE *f;
736 int col;
737};
738
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000739static int list_dir_proc(struct ext2_dir_entry *dirent,
740 int offset,
741 int blocksize,
742 char *buf,
743 void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000744{
745 char name[EXT2_NAME_LEN];
746 char tmp[EXT2_NAME_LEN + 16];
747
748 struct list_dir_struct *ls = (struct list_dir_struct *) private;
749 int thislen;
750
751 thislen = (dirent->name_len < EXT2_NAME_LEN) ? dirent->name_len :
752 EXT2_NAME_LEN;
753 strncpy(name, dirent->name, thislen);
754 name[thislen] = '\0';
755
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000756 sprintf(tmp, "%d (%d) %s ", dirent->inode, dirent->rec_len, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000757 thislen = strlen(tmp);
758
759 if (ls->col + thislen > 80) {
760 fprintf(ls->f, "\n");
761 ls->col = 0;
762 }
763 fprintf(ls->f, "%s", tmp);
764 ls->col += thislen;
765
766 return 0;
767}
768
769void do_list_dir(int argc, char *argv[])
770{
771 ino_t inode;
772 int retval;
773 struct list_dir_struct ls;
774
775 if (argc > 2) {
776 com_err(argv[0], 0, "Usage: list_dir [pathname]");
777 return;
778 }
779 if (check_fs_open(argv[0]))
780 return;
781
782 if (argc == 2)
783 inode = string_to_inode(argv[1]);
784 else
785 inode = cwd;
786 if (!inode)
787 return;
788
789 ls.f = open_pager();
790 ls.col = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000791 retval = ext2fs_dir_iterate(current_fs, inode,
792 DIRENT_FLAG_INCLUDE_EMPTY,
Theodore Ts'o3839e651997-04-26 13:21:57 +0000793 0, list_dir_proc, &ls);
794 fprintf(ls.f, "\n");
795 close_pager(ls.f);
796 if (retval)
797 com_err(argv[1], retval, "");
798
799 return;
800}
801
802void do_change_working_dir(int argc, char *argv[])
803{
804 ino_t inode;
805 int retval;
806
807 if (argc != 2) {
808 com_err(argv[0], 0, "Usage: cd <file>");
809 return;
810 }
811 if (check_fs_open(argv[0]))
812 return;
813
814 inode = string_to_inode(argv[1]);
815 if (!inode)
816 return;
817
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000818 retval = ext2fs_check_directory(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000819 if (retval) {
820 com_err(argv[1], retval, "");
821 return;
822 }
823 cwd = inode;
824 return;
825}
826
Theodore Ts'o3839e651997-04-26 13:21:57 +0000827void do_print_working_directory(int argc, char *argv[])
828{
829 int retval;
830 char *pathname = NULL;
831
832 if (argc > 1) {
833 com_err(argv[0], 0, "Usage: print_working_directory");
834 return;
835 }
836 if (check_fs_open(argv[0]))
837 return;
838
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000839 retval = ext2fs_get_pathname(current_fs, cwd, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000840 if (retval) {
841 com_err(argv[0], retval,
842 "while trying to get pathname of cwd");
843 }
844 printf("[pwd] INODE: %6ld PATH: %s\n", cwd, pathname);
845 free(pathname);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000846 retval = ext2fs_get_pathname(current_fs, root, 0, &pathname);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000847 if (retval) {
848 com_err(argv[0], retval,
849 "while trying to get pathname of root");
850 }
851 printf("[root] INODE: %6ld PATH: %s\n", root, pathname);
852 free(pathname);
853 return;
854}
855
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000856static void make_link(char *sourcename, char *destname)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000857{
858 ino_t inode;
859 int retval;
860 ino_t dir;
861 char *dest, *cp, *basename;
862
863 /*
864 * Get the source inode
865 */
866 inode = string_to_inode(sourcename);
867 if (!inode)
868 return;
869 basename = strrchr(sourcename, '/');
870 if (basename)
871 basename++;
872 else
873 basename = sourcename;
874 /*
875 * Figure out the destination. First see if it exists and is
876 * a directory.
877 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000878 if (! (retval=ext2fs_namei(current_fs, root, cwd, destname, &dir)))
Theodore Ts'o3839e651997-04-26 13:21:57 +0000879 dest = basename;
880 else {
881 /*
882 * OK, it doesn't exist. See if it is
883 * '<dir>/basename' or 'basename'
884 */
885 cp = strrchr(destname, '/');
886 if (cp) {
887 *cp = 0;
888 dir = string_to_inode(destname);
889 if (!dir)
890 return;
891 dest = cp+1;
892 } else {
893 dir = cwd;
894 dest = destname;
895 }
896 }
897
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000898 retval = ext2fs_link(current_fs, dir, dest, inode, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000899 if (retval)
900 com_err("make_link", retval, "");
901 return;
902}
903
904
905void do_link(int argc, char *argv[])
906{
907 if (argc != 3) {
908 com_err(argv[0], 0, "Usage: link <source_file> <dest_name>");
909 return;
910 }
911 if (check_fs_open(argv[0]))
912 return;
913
914 make_link(argv[1], argv[2]);
915}
916
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000917static void unlink_file_by_name(char *filename)
Theodore Ts'o3839e651997-04-26 13:21:57 +0000918{
919 int retval;
920 ino_t dir;
921 char *basename;
922
923 basename = strrchr(filename, '/');
924 if (basename) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000925 *basename++ = '\0';
Theodore Ts'o3839e651997-04-26 13:21:57 +0000926 dir = string_to_inode(filename);
927 if (!dir)
928 return;
929 } else {
930 dir = cwd;
931 basename = filename;
932 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000933 retval = ext2fs_unlink(current_fs, dir, basename, 0, 0);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000934 if (retval)
935 com_err("unlink_file_by_name", retval, "");
936 return;
937}
938
939void do_unlink(int argc, char *argv[])
940{
941 if (argc != 2) {
942 com_err(argv[0], 0, "Usage: unlink <pathname>");
943 return;
944 }
945 if (check_fs_open(argv[0]))
946 return;
947
948 unlink_file_by_name(argv[1]);
949}
950
951void do_find_free_block(int argc, char *argv[])
952{
953 blk_t free_blk, goal;
954 errcode_t retval;
955 char *tmp;
956
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000957 if ((argc > 2) || (argc==2 && *argv[1] == '?')) {
958 com_err(argv[0], 0, "Usage: find_free_block [goal]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000959 return;
960 }
961 if (check_fs_open(argv[0]))
962 return;
963
964 if (argc > 1) {
965 goal = strtol(argv[1], &tmp, 0);
966 if (*tmp) {
967 com_err(argv[0], 0, "Bad goal - %s", argv[1]);
968 return;
969 }
970 }
971 else
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000972 goal = current_fs->super->s_first_data_block;
Theodore Ts'o3839e651997-04-26 13:21:57 +0000973
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +0000974 retval = ext2fs_new_block(current_fs, goal, 0, &free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000975 if (retval)
976 com_err("ext2fs_new_block", retval, "");
977 else
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000978 printf("Free block found: %d\n", free_blk);
Theodore Ts'o3839e651997-04-26 13:21:57 +0000979
980}
981
982void do_find_free_inode(int argc, char *argv[])
983{
984 ino_t free_inode, dir;
985 int mode;
986 int retval;
987 char *tmp;
988
Theodore Ts'o50e1e101997-04-26 13:58:21 +0000989 if (argc > 3 || (argc>1 && *argv[1] == '?')) {
990 com_err(argv[0], 0, "Usage: find_free_inode [dir] [mode]");
Theodore Ts'o3839e651997-04-26 13:21:57 +0000991 return;
992 }
993 if (check_fs_open(argv[0]))
994 return;
995
996 if (argc > 1) {
997 dir = strtol(argv[1], &tmp, 0);
998 if (*tmp) {
999 com_err(argv[0], 0, "Bad dir - %s", argv[1]);
1000 return;
1001 }
1002 }
1003 else
1004 dir = root;
1005 if (argc > 2) {
1006 mode = strtol(argv[2], &tmp, 0);
1007 if (*tmp) {
1008 com_err(argv[0], 0, "Bad mode - %s", argv[2]);
1009 return;
1010 }
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001011 } else
Theodore Ts'o3839e651997-04-26 13:21:57 +00001012 mode = 010755;
1013
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001014 retval = ext2fs_new_inode(current_fs, dir, mode, 0, &free_inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001015 if (retval)
1016 com_err("ext2fs_new_inode", retval, "");
1017 else
1018 printf("Free inode found: %ld\n", free_inode);
1019}
1020
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001021struct copy_file_struct {
1022 unsigned long size;
1023 int done, fd, blocks;
1024 errcode_t err;
1025};
1026
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001027static int copy_file_proc(ext2_filsys to_fs,
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001028 blk_t *blocknr,
1029 int blockcnt,
1030 void *private)
1031{
1032 struct copy_file_struct *cs = (struct copy_file_struct *) private;
1033 blk_t new_blk;
1034 static blk_t last_blk = 0;
1035 char *block;
1036 errcode_t retval;
1037 int group;
1038 int nr;
1039
1040 if (*blocknr) {
1041 new_blk = *blocknr;
1042 } else {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001043 retval = ext2fs_new_block(to_fs, last_blk, 0, &new_blk);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001044 if (retval) {
1045 cs->err = retval;
1046 return BLOCK_ABORT;
1047 }
1048 }
1049 last_blk = new_blk;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001050 block = malloc(to_fs->blocksize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001051 if (!block) {
1052 cs->err = ENOMEM;
1053 return BLOCK_ABORT;
1054 }
1055 if (blockcnt >= 0) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001056 nr = read(cs->fd, block, to_fs->blocksize);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001057 } else {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001058 nr = to_fs->blocksize;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001059 memset(block, 0, nr);
1060 }
1061 if (nr == 0) {
1062 cs->done = 1;
1063 return BLOCK_ABORT;
1064 }
1065 if (nr < 0) {
1066 cs->err = nr;
1067 return BLOCK_ABORT;
1068 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001069 retval = io_channel_write_blk(to_fs->io, new_blk, 1, block);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001070 if (retval) {
1071 cs->err = retval;
1072 return BLOCK_ABORT;
1073 }
1074 free(block);
1075 if (blockcnt >= 0)
1076 cs->size += nr;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001077 cs->blocks += to_fs->blocksize / 512;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001078 printf("%ld(%d) ", cs->size, blockcnt);
1079 fflush(stdout);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001080 if (nr < to_fs->blocksize) {
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001081 cs->done = 1;
1082 printf("\n");
1083 }
1084 *blocknr = new_blk;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001085 ext2fs_mark_block_bitmap(to_fs->block_map, new_blk);
1086 ext2fs_mark_bb_dirty(to_fs);
1087 group = ext2fs_group_of_blk(to_fs, new_blk);
1088 to_fs->group_desc[group].bg_free_blocks_count--;
1089 to_fs->super->s_free_blocks_count--;
1090 ext2fs_mark_super_dirty(to_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001091 if (cs->done)
1092 return (BLOCK_CHANGED | BLOCK_ABORT);
1093 else
1094 return BLOCK_CHANGED;
1095}
1096
1097static errcode_t copy_file(int fd, ino_t newfile)
1098{
1099 errcode_t retval;
1100 struct copy_file_struct cs;
1101 struct ext2_inode inode;
1102
1103 cs.fd = fd;
1104 cs.done = 0;
1105 cs.err = 0;
1106 cs.size = 0;
1107 cs.blocks = 0;
1108
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001109 retval = ext2fs_block_iterate(current_fs, newfile,
1110 BLOCK_FLAG_APPEND,
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001111 0, copy_file_proc, &cs);
1112
1113 if (cs.err)
1114 return cs.err;
1115 if (!cs.done)
1116 return EXT2_ET_EXPAND_DIR_ERR;
1117
1118 /*
1119 * Update the size and block count fields in the inode.
1120 */
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001121 retval = ext2fs_read_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001122 if (retval)
1123 return retval;
1124
1125 inode.i_blocks += cs.blocks;
1126
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001127 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001128 if (retval)
1129 return retval;
1130
1131 return 0;
1132}
1133
1134void do_write(int argc, char *argv[])
1135{
1136 int fd;
1137 struct stat statbuf;
1138 ino_t newfile;
1139 errcode_t retval;
1140 struct ext2_inode inode;
1141
1142 if (check_fs_open(argv[0]))
1143 return;
1144 if (argc != 3) {
1145 com_err(argv[0], 0, "Usage: write <nativefile> <newfile>");
1146 return;
1147 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001148 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001149 return;
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001150 fd = open(argv[1], O_RDONLY);
1151 if (fd < 0) {
1152 com_err(argv[1], fd, "");
1153 return;
1154 }
1155 if (fstat(fd, &statbuf) < 0) {
1156 com_err(argv[1], errno, "");
1157 close(fd);
1158 return;
1159 }
1160
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001161 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001162 if (retval) {
1163 com_err(argv[0], retval, "");
1164 close(fd);
1165 return;
1166 }
1167 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001168 retval = ext2fs_link(current_fs, cwd, argv[2], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001169 if (retval) {
1170 com_err(argv[2], retval, "");
1171 close(fd);
1172 return;
1173 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001174 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001175 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001176 ext2fs_mark_inode_bitmap(current_fs->inode_map,newfile);
1177 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001178 memset(&inode, 0, sizeof(inode));
1179 inode.i_mode = statbuf.st_mode;
1180 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1181 inode.i_links_count = 1;
1182 inode.i_size = statbuf.st_size;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001183 ext2fs_write_inode(current_fs, newfile, &inode);
1184 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001185 if (retval) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001186 com_err(argv[0], retval, "while trying to write inode %d",
1187 inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001188 close(fd);
1189 return;
1190 }
1191 if (LINUX_S_ISREG(inode.i_mode)) {
1192 retval = copy_file(fd, newfile);
1193 if (retval)
1194 com_err("copy_file", retval, "");
1195 }
1196 close(fd);
1197}
1198
1199void do_mknod(int argc, char *argv[])
1200{
1201 unsigned long mode, major, minor, nr;
1202 ino_t newfile;
1203 errcode_t retval;
1204 struct ext2_inode inode;
1205
1206 if (check_fs_open(argv[0]))
1207 return;
1208 if (argc < 3 || argv[2][1]) {
1209 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1210 return;
1211 }
1212 mode = minor = major = 0;
1213 switch (argv[2][0]) {
1214 case 'p':
1215 mode = LINUX_S_IFIFO;
1216 nr = 3;
1217 break;
1218 case 'c':
1219 mode = LINUX_S_IFCHR;
1220 nr = 5;
1221 break;
1222 case 'b':
1223 mode = LINUX_S_IFBLK;
1224 nr = 5;
1225 break;
1226 default:
1227 nr = 0;
1228 }
1229 if (nr == 5) {
1230 major = strtoul(argv[3], argv+3, 0);
1231 minor = strtoul(argv[4], argv+4, 0);
1232 if (major > 255 || minor > 255 || argv[3][0] || argv[4][0])
1233 nr = 0;
1234 }
1235 if (argc != nr) {
1236 com_err(argv[0], 0, "Usage: mknod <name> [p| [c|b] <major> <minor>]");
1237 return;
1238 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001239 if (check_fs_read_write(argv[0]))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001240 return;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001241 retval = ext2fs_new_inode(current_fs, cwd, 010755, 0, &newfile);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001242 if (retval) {
1243 com_err(argv[0], retval, "");
1244 return;
1245 }
1246 printf("Allocated inode: %ld\n", newfile);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001247 retval = ext2fs_link(current_fs, cwd, argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001248 if (retval) {
1249 if (retval == EXT2_ET_DIR_NO_SPACE) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001250 retval = ext2fs_expand_dir(current_fs, cwd);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001251 if (!retval)
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001252 retval = ext2fs_link(current_fs, cwd,
1253 argv[1], newfile, 0);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001254 }
1255 if (retval) {
1256 com_err(argv[1], retval, "");
1257 return;
1258 }
1259 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001260 if (ext2fs_test_inode_bitmap(current_fs->inode_map,newfile))
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001261 com_err(argv[0], 0, "Warning: inode already set");
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001262 ext2fs_mark_inode_bitmap(current_fs->inode_map, newfile);
1263 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001264 memset(&inode, 0, sizeof(inode));
1265 inode.i_mode = mode;
1266 inode.i_atime = inode.i_ctime = inode.i_mtime = time(NULL);
1267 inode.i_block[0] = major*256+minor;
1268 inode.i_links_count = 1;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001269 ext2fs_write_inode(current_fs, newfile, &inode);
1270 retval = ext2fs_write_inode(current_fs, newfile, &inode);
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001271 if (retval) {
1272 com_err(argv[0], retval, "while trying to write inode %d", inode);
1273 return;
1274 }
1275}
1276
Theodore Ts'o3839e651997-04-26 13:21:57 +00001277void do_mkdir(int argc, char *argv[])
1278{
1279 char *cp;
1280 ino_t parent;
1281 char *name;
1282 errcode_t retval;
1283
1284 if (check_fs_open(argv[0]))
1285 return;
1286
1287 if (argc != 2) {
1288 com_err(argv[0], 0, "Usage: mkdir <file>");
1289 return;
1290 }
1291
1292 cp = strrchr(argv[1], '/');
1293 if (cp) {
1294 *cp = 0;
1295 parent = string_to_inode(argv[1]);
1296 if (!parent) {
1297 com_err(argv[1], ENOENT, "");
1298 return;
1299 }
1300 name = cp+1;
1301 } else {
1302 parent = cwd;
1303 name = argv[1];
1304 }
1305
1306
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001307 retval = ext2fs_mkdir(current_fs, parent, 0, name);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001308 if (retval) {
1309 com_err("ext2fs_mkdir", retval, "");
1310 return;
1311 }
1312
1313}
1314
1315void do_rmdir(int argc, char *argv[])
1316{
1317 printf("Unimplemented\n");
1318}
1319
1320
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001321static int release_blocks_proc(ext2_filsys fs, blk_t *blocknr,
1322 int blockcnt, void *private)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001323{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001324 printf("%d ", *blocknr);
Theodore Ts'of3db3561997-04-26 13:34:30 +00001325 ext2fs_unmark_block_bitmap(fs->block_map,*blocknr);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001326 return 0;
1327}
1328
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001329static void kill_file_by_inode(ino_t inode)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001330{
1331 struct ext2_inode inode_buf;
1332
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001333 ext2fs_read_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001334 inode_buf.i_dtime = time(NULL);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001335 ext2fs_write_inode(current_fs, inode, &inode_buf);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001336
1337 printf("Kill file by inode %ld\n", inode);
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001338 ext2fs_block_iterate(current_fs, inode, 0, NULL,
1339 release_blocks_proc, NULL);
1340 ext2fs_unmark_inode_bitmap(current_fs->inode_map, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001341
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001342 ext2fs_mark_bb_dirty(current_fs);
1343 ext2fs_mark_ib_dirty(current_fs);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001344}
1345
1346
1347void do_kill_file(int argc, char *argv[])
1348{
1349 ino_t inode_num;
1350
1351 if (argc != 2) {
1352 com_err(argv[0], 0, "Usage: kill_file <file>");
1353 return;
1354 }
1355 if (check_fs_open(argv[0]))
1356 return;
1357
1358 inode_num = string_to_inode(argv[1]);
1359 if (!inode_num) {
1360 com_err(argv[0], 0, "Cannot find file");
1361 return;
1362 }
1363 kill_file_by_inode(inode_num);
1364}
1365
1366void do_rm(int argc, char *argv[])
1367{
1368 int retval;
1369 ino_t inode_num;
1370 struct ext2_inode inode;
1371
1372 if (argc != 2) {
1373 com_err(argv[0], 0, "Usage: rm <filename>");
1374 return;
1375 }
1376 if (check_fs_open(argv[0]))
1377 return;
1378
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001379 retval = ext2fs_namei(current_fs, root, cwd, argv[1], &inode_num);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001380 if (retval) {
1381 com_err(argv[0], 0, "Cannot find file");
1382 return;
1383 }
1384
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001385 retval = ext2fs_read_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001386 if (retval) {
1387 com_err(argv[0], retval, "while reading file's inode");
1388 return;
1389 }
1390
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001391 if (LINUX_S_ISDIR(inode.i_mode)) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001392 com_err(argv[0], 0, "file is a directory");
1393 return;
1394 }
1395
1396 --inode.i_links_count;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001397 retval = ext2fs_write_inode(current_fs,inode_num,&inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001398 if (retval) {
1399 com_err(argv[0], retval, "while writing inode");
1400 return;
1401 }
1402
1403 unlink_file_by_name(argv[1]);
1404 if (inode.i_links_count == 0)
1405 kill_file_by_inode(inode_num);
1406}
1407
1408void do_show_debugfs_params(int argc, char *argv[])
1409{
1410 FILE *out = stdout;
1411
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001412 if (current_fs)
1413 fprintf(out, "Open mode: read-%s\n",
1414 current_fs->flags & EXT2_FLAG_RW ? "write" : "only");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001415 fprintf(out, "Filesystem in use: %s\n",
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001416 current_fs ? current_fs->device_name : "--none--");
Theodore Ts'o3839e651997-04-26 13:21:57 +00001417}
1418
1419void do_expand_dir(int argc, char *argv[])
1420{
1421 ino_t inode;
1422 int retval;
1423
1424 if (argc != 2) {
1425 com_err(argv[0], 0, "Usage: expand_dir <file>");
1426 return;
1427 }
1428 if (check_fs_open(argv[0]))
1429 return;
1430 inode = string_to_inode(argv[1]);
1431 if (!inode)
1432 return;
1433
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001434 retval = ext2fs_expand_dir(current_fs, inode);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001435 if (retval)
1436 com_err("ext2fs_expand_dir", retval, "");
1437 return;
1438}
1439
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001440static int source_file(const char *cmd_file, int sci_idx)
1441{
1442 FILE *f;
1443 char buf[256];
1444 char *cp;
1445 int exit_status = 0;
1446 int retval;
1447
1448 if (strcmp(cmd_file, "-") == 0)
1449 f = stdin;
1450 else {
1451 f = fopen(cmd_file, "r");
1452 if (!f) {
1453 perror(cmd_file);
1454 exit(1);
1455 }
1456 }
1457 setbuf(stdout, NULL);
1458 setbuf(stderr, NULL);
1459 while (!feof(f)) {
1460 if (fgets(buf, sizeof(buf), f) == NULL)
1461 break;
1462 cp = strchr(buf, '\n');
1463 if (cp)
1464 *cp = 0;
1465 cp = strchr(buf, '\r');
1466 if (cp)
1467 *cp = 0;
1468 printf("debugfs: %s\n", buf);
1469 retval = ss_execute_line(sci_idx, buf);
1470 if (retval) {
1471 ss_perror(sci_idx, retval, buf);
1472 exit_status++;
1473 }
1474 }
1475 return exit_status;
1476}
1477
Theodore Ts'o3839e651997-04-26 13:21:57 +00001478void main(int argc, char **argv)
1479{
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001480 int retval;
1481 int sci_idx;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001482 const char *usage = "Usage: debugfs [-w] [device]";
Theodore Ts'o50e1e101997-04-26 13:58:21 +00001483 char c;
1484 int open_flags = 0;
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001485 char *request = 0;
1486 int exit_status = 0;
1487 char *cmd_file = 0;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001488
1489 initialize_ext2_error_table();
1490
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001491 while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
Theodore Ts'o3839e651997-04-26 13:21:57 +00001492 switch (c) {
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001493 case 'R':
1494 request = optarg;
1495 break;
1496 case 'f':
1497 cmd_file = optarg;
1498 break;
Theodore Ts'o3839e651997-04-26 13:21:57 +00001499 case 'w':
1500 open_flags = EXT2_FLAG_RW;
1501 break;
1502 default:
1503 com_err(argv[0], 0, usage);
1504 return;
1505 }
1506 }
1507 if (optind < argc)
1508 open_filesystem(argv[optind], open_flags);
1509
1510 sci_idx = ss_create_invocation("debugfs", "0.0", (char *) NULL,
1511 &debug_cmds, &retval);
1512 if (retval) {
1513 ss_perror(sci_idx, retval, "creating invocation");
1514 exit(1);
1515 }
1516
1517 (void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
1518 if (retval) {
1519 ss_perror(sci_idx, retval, "adding standard requests");
1520 exit (1);
1521 }
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001522 if (request) {
1523 retval = 0;
1524 retval = ss_execute_line(sci_idx, request);
1525 if (retval) {
1526 ss_perror(sci_idx, retval, request);
1527 exit_status++;
1528 }
1529 } else if (cmd_file) {
1530 exit_status = source_file(cmd_file, sci_idx);
1531 } else {
1532 ss_listen(sci_idx);
1533 }
Theodore Ts'o3839e651997-04-26 13:21:57 +00001534
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001535 if (current_fs)
Theodore Ts'o3839e651997-04-26 13:21:57 +00001536 close_filesystem();
1537
Theodore Ts'ofc6d9d51997-04-29 14:51:31 +00001538 exit(exit_status);
Theodore Ts'o3839e651997-04-26 13:21:57 +00001539}
1540