xref: /freebsd/sbin/fsdb/fsdb.c (revision 7cc42f6d)
1 /*	$NetBSD: fsdb.c,v 1.2 1995/10/08 23:18:10 thorpej Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  *  Copyright (c) 1995 John T. Kohl
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. The name of the author may not be used to endorse or promote products
18  *     derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef lint
34 static const char rcsid[] =
35   "$FreeBSD$";
36 #endif /* not lint */
37 
38 #include <sys/param.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <grp.h>
42 #include <histedit.h>
43 #include <pwd.h>
44 #include <stdint.h>
45 #include <string.h>
46 #include <time.h>
47 #include <timeconv.h>
48 
49 #include <ufs/ufs/dinode.h>
50 #include <ufs/ufs/dir.h>
51 #include <ufs/ffs/fs.h>
52 
53 #include "fsdb.h"
54 #include "fsck.h"
55 
56 static void usage(void) __dead2;
57 int cmdloop(void);
58 static int compare_blk32(uint32_t *wantedblk, uint32_t curblk);
59 static int compare_blk64(uint64_t *wantedblk, uint64_t curblk);
60 static int founddatablk(uint64_t blk);
61 static int find_blks32(uint32_t *buf, int size, uint32_t *blknum);
62 static int find_blks64(uint64_t *buf, int size, uint64_t *blknum);
63 static int find_indirblks32(uint32_t blk, int ind_level, uint32_t *blknum);
64 static int find_indirblks64(uint64_t blk, int ind_level, uint64_t *blknum);
65 
66 static void
67 usage(void)
68 {
69 	fprintf(stderr, "usage: fsdb [-d] [-f] [-r] fsname\n");
70 	exit(1);
71 }
72 
73 /*
74  * We suck in lots of fsck code, and just pick & choose the stuff we want.
75  *
76  * fsreadfd is set up to read from the file system, fswritefd to write to
77  * the file system.
78  */
79 int
80 main(int argc, char *argv[])
81 {
82 	int ch, rval;
83 	char *fsys = NULL;
84 
85 	while (-1 != (ch = getopt(argc, argv, "fdr"))) {
86 		switch (ch) {
87 		case 'f':
88 			/* The -f option is left for historical
89 			 * reasons and has no meaning.
90 			 */
91 			break;
92 		case 'd':
93 			debug++;
94 			break;
95 		case 'r':
96 			nflag++; /* "no" in fsck, readonly for us */
97 			break;
98 		default:
99 			usage();
100 		}
101 	}
102 	argc -= optind;
103 	argv += optind;
104 	if (argc != 1)
105 		usage();
106 	else
107 		fsys = argv[0];
108 
109 	sblock_init();
110 	if (!setup(fsys))
111 		errx(1, "cannot set up file system `%s'", fsys);
112 	if (fswritefd < 0)
113 		nflag++;
114 	printf("%s file system `%s'\nLast Mounted on %s\n",
115 	       nflag? "Examining": "Editing", fsys, sblock.fs_fsmnt);
116 	rval = cmdloop();
117 	if (!nflag) {
118 		sblock.fs_clean = 0;	/* mark it dirty */
119 		sbdirty();
120 		ckfini(0);
121 		printf("*** FILE SYSTEM MARKED DIRTY\n");
122 		printf("*** BE SURE TO RUN FSCK TO CLEAN UP ANY DAMAGE\n");
123 		printf("*** IF IT WAS MOUNTED, RE-MOUNT WITH -u -o reload\n");
124 	}
125 	exit(rval);
126 }
127 
128 #define CMDFUNC(func) int func(int argc, char *argv[])
129 #define CMDFUNCSTART(func) int func(int argc, char *argv[])
130 
131 CMDFUNC(helpfn);
132 CMDFUNC(focus);				/* focus on inode */
133 CMDFUNC(active);			/* print active inode */
134 CMDFUNC(blocks);			/* print blocks for active inode */
135 CMDFUNC(focusname);			/* focus by name */
136 CMDFUNC(zapi);				/* clear inode */
137 CMDFUNC(uplink);			/* incr link */
138 CMDFUNC(downlink);			/* decr link */
139 CMDFUNC(linkcount);			/* set link count */
140 CMDFUNC(quit);				/* quit */
141 CMDFUNC(findblk);			/* find block */
142 CMDFUNC(ls);				/* list directory */
143 CMDFUNC(rm);				/* remove name */
144 CMDFUNC(ln);				/* add name */
145 CMDFUNC(newtype);			/* change type */
146 CMDFUNC(chmode);			/* change mode */
147 CMDFUNC(chlen);				/* change length */
148 CMDFUNC(chaflags);			/* change flags */
149 CMDFUNC(chgen);				/* change generation */
150 CMDFUNC(chowner);			/* change owner */
151 CMDFUNC(chgroup);			/* Change group */
152 CMDFUNC(back);				/* pop back to last ino */
153 CMDFUNC(chbtime);			/* Change btime */
154 CMDFUNC(chmtime);			/* Change mtime */
155 CMDFUNC(chctime);			/* Change ctime */
156 CMDFUNC(chatime);			/* Change atime */
157 CMDFUNC(chinum);			/* Change inode # of dirent */
158 CMDFUNC(chname);			/* Change dirname of dirent */
159 CMDFUNC(chsize);			/* Change size */
160 
161 struct cmdtable cmds[] = {
162 	{ "help", "Print out help", 1, 1, FL_RO, helpfn },
163 	{ "?", "Print out help", 1, 1, FL_RO, helpfn },
164 	{ "inode", "Set active inode to INUM", 2, 2, FL_RO, focus },
165 	{ "clri", "Clear inode INUM", 2, 2, FL_WR, zapi },
166 	{ "lookup", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
167 	{ "cd", "Set active inode by looking up NAME", 2, 2, FL_RO | FL_ST, focusname },
168 	{ "back", "Go to previous active inode", 1, 1, FL_RO, back },
169 	{ "active", "Print active inode", 1, 1, FL_RO, active },
170 	{ "print", "Print active inode", 1, 1, FL_RO, active },
171 	{ "blocks", "Print block numbers of active inode", 1, 1, FL_RO, blocks },
172 	{ "uplink", "Increment link count", 1, 1, FL_WR, uplink },
173 	{ "downlink", "Decrement link count", 1, 1, FL_WR, downlink },
174 	{ "linkcount", "Set link count to COUNT", 2, 2, FL_WR, linkcount },
175 	{ "findblk", "Find inode owning disk block(s)", 2, 33, FL_RO, findblk},
176 	{ "ls", "List current inode as directory", 1, 1, FL_RO, ls },
177 	{ "rm", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
178 	{ "del", "Remove NAME from current inode directory", 2, 2, FL_WR | FL_ST, rm },
179 	{ "ln", "Hardlink INO into current inode directory as NAME", 3, 3, FL_WR | FL_ST, ln },
180 	{ "chinum", "Change dir entry number INDEX to INUM", 3, 3, FL_WR, chinum },
181 	{ "chname", "Change dir entry number INDEX to NAME", 3, 3, FL_WR | FL_ST, chname },
182 	{ "chtype", "Change type of current inode to TYPE", 2, 2, FL_WR, newtype },
183 	{ "chmod", "Change mode of current inode to MODE", 2, 2, FL_WR, chmode },
184 	{ "chlen", "Change length of current inode to LENGTH", 2, 2, FL_WR, chlen },
185 	{ "chown", "Change owner of current inode to OWNER", 2, 2, FL_WR, chowner },
186 	{ "chgrp", "Change group of current inode to GROUP", 2, 2, FL_WR, chgroup },
187 	{ "chflags", "Change flags of current inode to FLAGS", 2, 2, FL_WR, chaflags },
188 	{ "chgen", "Change generation number of current inode to GEN", 2, 2, FL_WR, chgen },
189 	{ "chsize", "Change size of current inode to SIZE", 2, 2, FL_WR, chsize },
190 	{ "btime", "Change btime of current inode to BTIME", 2, 2, FL_WR, chbtime },
191 	{ "mtime", "Change mtime of current inode to MTIME", 2, 2, FL_WR, chmtime },
192 	{ "ctime", "Change ctime of current inode to CTIME", 2, 2, FL_WR, chctime },
193 	{ "atime", "Change atime of current inode to ATIME", 2, 2, FL_WR, chatime },
194 	{ "quit", "Exit", 1, 1, FL_RO, quit },
195 	{ "q", "Exit", 1, 1, FL_RO, quit },
196 	{ "exit", "Exit", 1, 1, FL_RO, quit },
197 	{ NULL, 0, 0, 0, 0, NULL },
198 };
199 
200 int
201 helpfn(int argc, char *argv[])
202 {
203     struct cmdtable *cmdtp;
204 
205     printf("Commands are:\n%-10s %5s %5s   %s\n",
206 	   "command", "min args", "max args", "what");
207 
208     for (cmdtp = cmds; cmdtp->cmd; cmdtp++)
209 	printf("%-10s %5u %5u   %s\n",
210 		cmdtp->cmd, cmdtp->minargc-1, cmdtp->maxargc-1, cmdtp->helptxt);
211     return 0;
212 }
213 
214 char *
215 prompt(EditLine *el)
216 {
217     static char pstring[64];
218     snprintf(pstring, sizeof(pstring), "fsdb (inum: %ju)> ",
219 	(uintmax_t)curinum);
220     return pstring;
221 }
222 
223 
224 int
225 cmdloop(void)
226 {
227     char *line;
228     const char *elline;
229     int cmd_argc, rval = 0, known;
230 #define scratch known
231     char **cmd_argv;
232     struct cmdtable *cmdp;
233     History *hist;
234     EditLine *elptr;
235     HistEvent he;
236 
237     curinode = ginode(UFS_ROOTINO);
238     curinum = UFS_ROOTINO;
239     printactive(0);
240 
241     hist = history_init();
242     history(hist, &he, H_SETSIZE, 100);	/* 100 elt history buffer */
243 
244     elptr = el_init("fsdb", stdin, stdout, stderr);
245     el_set(elptr, EL_EDITOR, "emacs");
246     el_set(elptr, EL_PROMPT, prompt);
247     el_set(elptr, EL_HIST, history, hist);
248     el_source(elptr, NULL);
249 
250     while ((elline = el_gets(elptr, &scratch)) != NULL && scratch != 0) {
251 	if (debug)
252 	    printf("command `%s'\n", elline);
253 
254 	history(hist, &he, H_ENTER, elline);
255 
256 	line = strdup(elline);
257 	cmd_argv = crack(line, &cmd_argc);
258 	/*
259 	 * el_parse returns -1 to signal that it's not been handled
260 	 * internally.
261 	 */
262 	if (el_parse(elptr, cmd_argc, (const char **)cmd_argv) != -1)
263 	    continue;
264 	if (cmd_argc) {
265 	    known = 0;
266 	    for (cmdp = cmds; cmdp->cmd; cmdp++) {
267 		if (!strcmp(cmdp->cmd, cmd_argv[0])) {
268 		    if ((cmdp->flags & FL_WR) == FL_WR && nflag)
269 			warnx("`%s' requires write access", cmd_argv[0]),
270 			    rval = 1;
271 		    else if (cmd_argc >= cmdp->minargc &&
272 			cmd_argc <= cmdp->maxargc)
273 			rval = (*cmdp->handler)(cmd_argc, cmd_argv);
274 		    else if (cmd_argc >= cmdp->minargc &&
275 			(cmdp->flags & FL_ST) == FL_ST) {
276 			strcpy(line, elline);
277 			cmd_argv = recrack(line, &cmd_argc, cmdp->maxargc);
278 			rval = (*cmdp->handler)(cmd_argc, cmd_argv);
279 		    } else
280 			rval = argcount(cmdp, cmd_argc, cmd_argv);
281 		    known = 1;
282 		    break;
283 		}
284 	    }
285 	    if (!known)
286 		warnx("unknown command `%s'", cmd_argv[0]), rval = 1;
287 	} else
288 	    rval = 0;
289 	free(line);
290 	if (rval < 0)
291 	    /* user typed "quit" */
292 	    return 0;
293 	if (rval)
294 	    warnx("rval was %d", rval);
295     }
296     el_end(elptr);
297     history_end(hist);
298     return rval;
299 }
300 
301 union dinode *curinode;
302 ino_t curinum, ocurrent;
303 
304 #define GETINUM(ac,inum)    inum = strtoul(argv[ac], &cp, 0); \
305 if (inum < UFS_ROOTINO || inum > maxino || cp == argv[ac] || *cp != '\0' ) { \
306 	printf("inode %ju out of range; range is [%ju,%ju]\n",		\
307 	    (uintmax_t)inum, (uintmax_t)UFS_ROOTINO, (uintmax_t)maxino);\
308 	return 1; \
309 }
310 
311 /*
312  * Focus on given inode number
313  */
314 CMDFUNCSTART(focus)
315 {
316     ino_t inum;
317     char *cp;
318 
319     GETINUM(1,inum);
320     curinode = ginode(inum);
321     ocurrent = curinum;
322     curinum = inum;
323     printactive(0);
324     return 0;
325 }
326 
327 CMDFUNCSTART(back)
328 {
329     curinum = ocurrent;
330     curinode = ginode(curinum);
331     printactive(0);
332     return 0;
333 }
334 
335 CMDFUNCSTART(zapi)
336 {
337     ino_t inum;
338     union dinode *dp;
339     char *cp;
340 
341     GETINUM(1,inum);
342     dp = ginode(inum);
343     clearinode(dp);
344     inodirty(dp);
345     if (curinode)			/* re-set after potential change */
346 	curinode = ginode(curinum);
347     return 0;
348 }
349 
350 CMDFUNCSTART(active)
351 {
352     printactive(0);
353     return 0;
354 }
355 
356 CMDFUNCSTART(blocks)
357 {
358     printactive(1);
359     return 0;
360 }
361 
362 CMDFUNCSTART(quit)
363 {
364     return -1;
365 }
366 
367 CMDFUNCSTART(uplink)
368 {
369     if (!checkactive())
370 	return 1;
371     DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) + 1);
372     printf("inode %ju link count now %d\n",
373 	(uintmax_t)curinum, DIP(curinode, di_nlink));
374     inodirty(curinode);
375     return 0;
376 }
377 
378 CMDFUNCSTART(downlink)
379 {
380     if (!checkactive())
381 	return 1;
382     DIP_SET(curinode, di_nlink, DIP(curinode, di_nlink) - 1);
383     printf("inode %ju link count now %d\n",
384 	(uintmax_t)curinum, DIP(curinode, di_nlink));
385     inodirty(curinode);
386     return 0;
387 }
388 
389 const char *typename[] = {
390     "unknown",
391     "fifo",
392     "char special",
393     "unregistered #3",
394     "directory",
395     "unregistered #5",
396     "blk special",
397     "unregistered #7",
398     "regular",
399     "unregistered #9",
400     "symlink",
401     "unregistered #11",
402     "socket",
403     "unregistered #13",
404     "whiteout",
405 };
406 
407 int diroff;
408 int slot;
409 
410 int
411 scannames(struct inodesc *idesc)
412 {
413 	struct direct *dirp = idesc->id_dirp;
414 
415 	printf("slot %d off %d ino %d reclen %d: %s, `%.*s'\n",
416 	       slot++, diroff, dirp->d_ino, dirp->d_reclen,
417 	       typename[dirp->d_type], dirp->d_namlen, dirp->d_name);
418 	diroff += dirp->d_reclen;
419 	return (KEEPON);
420 }
421 
422 CMDFUNCSTART(ls)
423 {
424     struct inodesc idesc;
425     checkactivedir();			/* let it go on anyway */
426 
427     slot = 0;
428     diroff = 0;
429     idesc.id_number = curinum;
430     idesc.id_func = scannames;
431     idesc.id_type = DATA;
432     idesc.id_fix = IGNORE;
433     ckinode(curinode, &idesc);
434     curinode = ginode(curinum);
435 
436     return 0;
437 }
438 
439 static int findblk_numtofind;
440 static int wantedblksize;
441 
442 CMDFUNCSTART(findblk)
443 {
444     ino_t inum, inosused;
445     uint32_t *wantedblk32;
446     uint64_t *wantedblk64;
447     struct bufarea *cgbp;
448     struct cg *cgp;
449     int c, i, is_ufs2;
450 
451     wantedblksize = (argc - 1);
452     is_ufs2 = sblock.fs_magic == FS_UFS2_MAGIC;
453     ocurrent = curinum;
454 
455     if (is_ufs2) {
456 	wantedblk64 = calloc(wantedblksize, sizeof(uint64_t));
457 	if (wantedblk64 == NULL)
458 	    err(1, "malloc");
459 	for (i = 1; i < argc; i++)
460 	    wantedblk64[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
461     } else {
462 	wantedblk32 = calloc(wantedblksize, sizeof(uint32_t));
463 	if (wantedblk32 == NULL)
464 	    err(1, "malloc");
465 	for (i = 1; i < argc; i++)
466 	    wantedblk32[i - 1] = dbtofsb(&sblock, strtoull(argv[i], NULL, 0));
467     }
468     findblk_numtofind = wantedblksize;
469     /*
470      * sblock.fs_ncg holds a number of cylinder groups.
471      * Iterate over all cylinder groups.
472      */
473     for (c = 0; c < sblock.fs_ncg; c++) {
474 	/*
475 	 * sblock.fs_ipg holds a number of inodes per cylinder group.
476 	 * Calculate a highest inode number for a given cylinder group.
477 	 */
478 	inum = c * sblock.fs_ipg;
479 	/* Read cylinder group. */
480 	cgbp = cglookup(c);
481 	cgp = cgbp->b_un.b_cg;
482 	/*
483 	 * Get a highest used inode number for a given cylinder group.
484 	 * For UFS1 all inodes initialized at the newfs stage.
485 	 */
486 	if (is_ufs2)
487 	    inosused = cgp->cg_initediblk;
488 	else
489 	    inosused = sblock.fs_ipg;
490 
491 	for (; inosused > 0; inum++, inosused--) {
492 	    /* Skip magic inodes: 0, UFS_WINO, UFS_ROOTINO. */
493 	    if (inum < UFS_ROOTINO)
494 		continue;
495 	    /*
496 	     * Check if the block we are looking for is just an inode block.
497 	     *
498 	     * ino_to_fsba() - get block containing inode from its number.
499 	     * INOPB() - get a number of inodes in one disk block.
500 	     */
501 	    if (is_ufs2 ?
502 		compare_blk64(wantedblk64, ino_to_fsba(&sblock, inum)) :
503 		compare_blk32(wantedblk32, ino_to_fsba(&sblock, inum))) {
504 		printf("block %llu: inode block (%ju-%ju)\n",
505 		    (unsigned long long)fsbtodb(&sblock,
506 			ino_to_fsba(&sblock, inum)),
507 		    (uintmax_t)(inum / INOPB(&sblock)) * INOPB(&sblock),
508 		    (uintmax_t)(inum / INOPB(&sblock) + 1) * INOPB(&sblock));
509 		findblk_numtofind--;
510 		if (findblk_numtofind == 0)
511 		    goto end;
512 	    }
513 	    /* Get on-disk inode aka dinode. */
514 	    curinum = inum;
515 	    curinode = ginode(inum);
516 	    /* Find IFLNK dinode with allocated data blocks. */
517 	    switch (DIP(curinode, di_mode) & IFMT) {
518 	    case IFDIR:
519 	    case IFREG:
520 		if (DIP(curinode, di_blocks) == 0)
521 		    continue;
522 		break;
523 	    case IFLNK:
524 		{
525 		    uint64_t size = DIP(curinode, di_size);
526 		    if (size > 0 && size < sblock.fs_maxsymlinklen &&
527 			DIP(curinode, di_blocks) == 0)
528 			continue;
529 		    else
530 			break;
531 		}
532 	    default:
533 		continue;
534 	    }
535 	    /* Look through direct data blocks. */
536 	    if (is_ufs2 ?
537 		find_blks64(curinode->dp2.di_db, UFS_NDADDR, wantedblk64) :
538 		find_blks32(curinode->dp1.di_db, UFS_NDADDR, wantedblk32))
539 		goto end;
540 	    for (i = 0; i < UFS_NIADDR; i++) {
541 		/*
542 		 * Does the block we are looking for belongs to the
543 		 * indirect blocks?
544 		 */
545 		if (is_ufs2 ?
546 		    compare_blk64(wantedblk64, curinode->dp2.di_ib[i]) :
547 		    compare_blk32(wantedblk32, curinode->dp1.di_ib[i]))
548 		    if (founddatablk(is_ufs2 ? curinode->dp2.di_ib[i] :
549 			curinode->dp1.di_ib[i]))
550 			goto end;
551 		/*
552 		 * Search through indirect, double and triple indirect
553 		 * data blocks.
554 		 */
555 		if (is_ufs2 ? (curinode->dp2.di_ib[i] != 0) :
556 		    (curinode->dp1.di_ib[i] != 0))
557 		    if (is_ufs2 ?
558 			find_indirblks64(curinode->dp2.di_ib[i], i,
559 			    wantedblk64) :
560 			find_indirblks32(curinode->dp1.di_ib[i], i,
561 			    wantedblk32))
562 			goto end;
563 	    }
564 	}
565     }
566 end:
567     curinum = ocurrent;
568     curinode = ginode(curinum);
569     if (is_ufs2)
570 	free(wantedblk64);
571     else
572 	free(wantedblk32);
573     return 0;
574 }
575 
576 static int
577 compare_blk32(uint32_t *wantedblk, uint32_t curblk)
578 {
579     int i;
580 
581     for (i = 0; i < wantedblksize; i++) {
582 	if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
583 	    wantedblk[i] = 0;
584 	    return 1;
585 	}
586     }
587     return 0;
588 }
589 
590 static int
591 compare_blk64(uint64_t *wantedblk, uint64_t curblk)
592 {
593     int i;
594 
595     for (i = 0; i < wantedblksize; i++) {
596 	if (wantedblk[i] != 0 && wantedblk[i] == curblk) {
597 	    wantedblk[i] = 0;
598 	    return 1;
599 	}
600     }
601     return 0;
602 }
603 
604 static int
605 founddatablk(uint64_t blk)
606 {
607 
608     printf("%llu: data block of inode %ju\n",
609 	(unsigned long long)fsbtodb(&sblock, blk), (uintmax_t)curinum);
610     findblk_numtofind--;
611     if (findblk_numtofind == 0)
612 	return 1;
613     return 0;
614 }
615 
616 static int
617 find_blks32(uint32_t *buf, int size, uint32_t *wantedblk)
618 {
619     int blk;
620     for (blk = 0; blk < size; blk++) {
621 	if (buf[blk] == 0)
622 	    continue;
623 	if (compare_blk32(wantedblk, buf[blk])) {
624 	    if (founddatablk(buf[blk]))
625 		return 1;
626 	}
627     }
628     return 0;
629 }
630 
631 static int
632 find_indirblks32(uint32_t blk, int ind_level, uint32_t *wantedblk)
633 {
634 #define MAXNINDIR      (MAXBSIZE / sizeof(uint32_t))
635     uint32_t idblk[MAXNINDIR];
636     int i;
637 
638     blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
639     if (ind_level <= 0) {
640 	if (find_blks32(idblk, sblock.fs_bsize / sizeof(uint32_t), wantedblk))
641 	    return 1;
642     } else {
643 	ind_level--;
644 	for (i = 0; i < sblock.fs_bsize / sizeof(uint32_t); i++) {
645 	    if (compare_blk32(wantedblk, idblk[i])) {
646 		if (founddatablk(idblk[i]))
647 		    return 1;
648 	    }
649 	    if (idblk[i] != 0)
650 		if (find_indirblks32(idblk[i], ind_level, wantedblk))
651 		    return 1;
652 	}
653     }
654 #undef MAXNINDIR
655     return 0;
656 }
657 
658 static int
659 find_blks64(uint64_t *buf, int size, uint64_t *wantedblk)
660 {
661     int blk;
662     for (blk = 0; blk < size; blk++) {
663 	if (buf[blk] == 0)
664 	    continue;
665 	if (compare_blk64(wantedblk, buf[blk])) {
666 	    if (founddatablk(buf[blk]))
667 		return 1;
668 	}
669     }
670     return 0;
671 }
672 
673 static int
674 find_indirblks64(uint64_t blk, int ind_level, uint64_t *wantedblk)
675 {
676 #define MAXNINDIR      (MAXBSIZE / sizeof(uint64_t))
677     uint64_t idblk[MAXNINDIR];
678     int i;
679 
680     blread(fsreadfd, (char *)idblk, fsbtodb(&sblock, blk), (int)sblock.fs_bsize);
681     if (ind_level <= 0) {
682 	if (find_blks64(idblk, sblock.fs_bsize / sizeof(uint64_t), wantedblk))
683 	    return 1;
684     } else {
685 	ind_level--;
686 	for (i = 0; i < sblock.fs_bsize / sizeof(uint64_t); i++) {
687 	    if (compare_blk64(wantedblk, idblk[i])) {
688 		if (founddatablk(idblk[i]))
689 		    return 1;
690 	    }
691 	    if (idblk[i] != 0)
692 		if (find_indirblks64(idblk[i], ind_level, wantedblk))
693 		    return 1;
694 	}
695     }
696 #undef MAXNINDIR
697     return 0;
698 }
699 
700 int findino(struct inodesc *idesc); /* from fsck */
701 static int dolookup(char *name);
702 
703 static int
704 dolookup(char *name)
705 {
706     struct inodesc idesc;
707 
708     if (!checkactivedir())
709 	    return 0;
710     idesc.id_number = curinum;
711     idesc.id_func = findino;
712     idesc.id_name = name;
713     idesc.id_type = DATA;
714     idesc.id_fix = IGNORE;
715     if (ckinode(curinode, &idesc) & FOUND) {
716 	curinum = idesc.id_parent;
717 	curinode = ginode(curinum);
718 	printactive(0);
719 	return 1;
720     } else {
721 	warnx("name `%s' not found in current inode directory", name);
722 	return 0;
723     }
724 }
725 
726 CMDFUNCSTART(focusname)
727 {
728     char *p, *val;
729 
730     if (!checkactive())
731 	return 1;
732 
733     ocurrent = curinum;
734 
735     if (argv[1][0] == '/') {
736 	curinum = UFS_ROOTINO;
737 	curinode = ginode(UFS_ROOTINO);
738     } else {
739 	if (!checkactivedir())
740 	    return 1;
741     }
742     for (p = argv[1]; p != NULL;) {
743 	while ((val = strsep(&p, "/")) != NULL && *val == '\0');
744 	if (val) {
745 	    printf("component `%s': ", val);
746 	    fflush(stdout);
747 	    if (!dolookup(val)) {
748 		curinode = ginode(curinum);
749 		return(1);
750 	    }
751 	}
752     }
753     return 0;
754 }
755 
756 CMDFUNCSTART(ln)
757 {
758     ino_t inum;
759     int rval;
760     char *cp;
761 
762     GETINUM(1,inum);
763 
764     if (!checkactivedir())
765 	return 1;
766     rval = makeentry(curinum, inum, argv[2]);
767     if (rval)
768 	    printf("Ino %ju entered as `%s'\n", (uintmax_t)inum, argv[2]);
769     else
770 	printf("could not enter name? weird.\n");
771     curinode = ginode(curinum);
772     return rval;
773 }
774 
775 CMDFUNCSTART(rm)
776 {
777     int rval;
778 
779     if (!checkactivedir())
780 	return 1;
781     rval = changeino(curinum, argv[1], 0);
782     if (rval & ALTERED) {
783 	printf("Name `%s' removed\n", argv[1]);
784 	return 0;
785     } else {
786 	printf("could not remove name ('%s')? weird.\n", argv[1]);
787 	return 1;
788     }
789 }
790 
791 long slotcount, desired;
792 
793 int
794 chinumfunc(struct inodesc *idesc)
795 {
796 	struct direct *dirp = idesc->id_dirp;
797 
798 	if (slotcount++ == desired) {
799 	    dirp->d_ino = idesc->id_parent;
800 	    return STOP|ALTERED|FOUND;
801 	}
802 	return KEEPON;
803 }
804 
805 CMDFUNCSTART(chinum)
806 {
807     char *cp;
808     ino_t inum;
809     struct inodesc idesc;
810 
811     slotcount = 0;
812     if (!checkactivedir())
813 	return 1;
814     GETINUM(2,inum);
815 
816     desired = strtol(argv[1], &cp, 0);
817     if (cp == argv[1] || *cp != '\0' || desired < 0) {
818 	printf("invalid slot number `%s'\n", argv[1]);
819 	return 1;
820     }
821 
822     idesc.id_number = curinum;
823     idesc.id_func = chinumfunc;
824     idesc.id_fix = IGNORE;
825     idesc.id_type = DATA;
826     idesc.id_parent = inum;		/* XXX convenient hiding place */
827 
828     if (ckinode(curinode, &idesc) & FOUND)
829 	return 0;
830     else {
831 	warnx("no %sth slot in current directory", argv[1]);
832 	return 1;
833     }
834 }
835 
836 int
837 chnamefunc(struct inodesc *idesc)
838 {
839 	struct direct *dirp = idesc->id_dirp;
840 	struct direct testdir;
841 
842 	if (slotcount++ == desired) {
843 	    /* will name fit? */
844 	    testdir.d_namlen = strlen(idesc->id_name);
845 	    if (DIRSIZ(NEWDIRFMT, &testdir) <= dirp->d_reclen) {
846 		dirp->d_namlen = testdir.d_namlen;
847 		strcpy(dirp->d_name, idesc->id_name);
848 		return STOP|ALTERED|FOUND;
849 	    } else
850 		return STOP|FOUND;	/* won't fit, so give up */
851 	}
852 	return KEEPON;
853 }
854 
855 CMDFUNCSTART(chname)
856 {
857     int rval;
858     char *cp;
859     struct inodesc idesc;
860 
861     slotcount = 0;
862     if (!checkactivedir())
863 	return 1;
864 
865     desired = strtoul(argv[1], &cp, 0);
866     if (cp == argv[1] || *cp != '\0') {
867 	printf("invalid slot number `%s'\n", argv[1]);
868 	return 1;
869     }
870 
871     idesc.id_number = curinum;
872     idesc.id_func = chnamefunc;
873     idesc.id_fix = IGNORE;
874     idesc.id_type = DATA;
875     idesc.id_name = argv[2];
876 
877     rval = ckinode(curinode, &idesc);
878     if ((rval & (FOUND|ALTERED)) == (FOUND|ALTERED))
879 	return 0;
880     else if (rval & FOUND) {
881 	warnx("new name `%s' does not fit in slot %s\n", argv[2], argv[1]);
882 	return 1;
883     } else {
884 	warnx("no %sth slot in current directory", argv[1]);
885 	return 1;
886     }
887 }
888 
889 struct typemap {
890     const char *typename;
891     int typebits;
892 } typenamemap[]  = {
893     {"file", IFREG},
894     {"dir", IFDIR},
895     {"socket", IFSOCK},
896     {"fifo", IFIFO},
897 };
898 
899 CMDFUNCSTART(newtype)
900 {
901     int type;
902     struct typemap *tp;
903 
904     if (!checkactive())
905 	return 1;
906     type = DIP(curinode, di_mode) & IFMT;
907     for (tp = typenamemap;
908 	 tp < &typenamemap[nitems(typenamemap)];
909 	 tp++) {
910 	if (!strcmp(argv[1], tp->typename)) {
911 	    printf("setting type to %s\n", tp->typename);
912 	    type = tp->typebits;
913 	    break;
914 	}
915     }
916     if (tp == &typenamemap[nitems(typenamemap)]) {
917 	warnx("type `%s' not known", argv[1]);
918 	warnx("try one of `file', `dir', `socket', `fifo'");
919 	return 1;
920     }
921     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~IFMT);
922     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | type);
923     inodirty(curinode);
924     printactive(0);
925     return 0;
926 }
927 
928 CMDFUNCSTART(chlen)
929 {
930     int rval = 1;
931     long len;
932     char *cp;
933 
934     if (!checkactive())
935 	return 1;
936 
937     len = strtol(argv[1], &cp, 0);
938     if (cp == argv[1] || *cp != '\0' || len < 0) {
939 	warnx("bad length `%s'", argv[1]);
940 	return 1;
941     }
942 
943     DIP_SET(curinode, di_size, len);
944     inodirty(curinode);
945     printactive(0);
946     return rval;
947 }
948 
949 CMDFUNCSTART(chmode)
950 {
951     int rval = 1;
952     long modebits;
953     char *cp;
954 
955     if (!checkactive())
956 	return 1;
957 
958     modebits = strtol(argv[1], &cp, 8);
959     if (cp == argv[1] || *cp != '\0' || (modebits & ~07777)) {
960 	warnx("bad modebits `%s'", argv[1]);
961 	return 1;
962     }
963 
964     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) & ~07777);
965     DIP_SET(curinode, di_mode, DIP(curinode, di_mode) | modebits);
966     inodirty(curinode);
967     printactive(0);
968     return rval;
969 }
970 
971 CMDFUNCSTART(chaflags)
972 {
973     int rval = 1;
974     u_long flags;
975     char *cp;
976 
977     if (!checkactive())
978 	return 1;
979 
980     flags = strtoul(argv[1], &cp, 0);
981     if (cp == argv[1] || *cp != '\0' ) {
982 	warnx("bad flags `%s'", argv[1]);
983 	return 1;
984     }
985 
986     if (flags > UINT_MAX) {
987 	warnx("flags set beyond 32-bit range of field (%lx)\n", flags);
988 	return(1);
989     }
990     DIP_SET(curinode, di_flags, flags);
991     inodirty(curinode);
992     printactive(0);
993     return rval;
994 }
995 
996 CMDFUNCSTART(chgen)
997 {
998     int rval = 1;
999     long gen;
1000     char *cp;
1001 
1002     if (!checkactive())
1003 	return 1;
1004 
1005     gen = strtol(argv[1], &cp, 0);
1006     if (cp == argv[1] || *cp != '\0' ) {
1007 	warnx("bad gen `%s'", argv[1]);
1008 	return 1;
1009     }
1010 
1011     if (gen > INT_MAX || gen < INT_MIN) {
1012 	warnx("gen set beyond 32-bit range of field (%lx)\n", gen);
1013 	return(1);
1014     }
1015     DIP_SET(curinode, di_gen, gen);
1016     inodirty(curinode);
1017     printactive(0);
1018     return rval;
1019 }
1020 
1021 CMDFUNCSTART(chsize)
1022 {
1023     int rval = 1;
1024     off_t size;
1025     char *cp;
1026 
1027     if (!checkactive())
1028 	return 1;
1029 
1030     size = strtoll(argv[1], &cp, 0);
1031     if (cp == argv[1] || *cp != '\0') {
1032 	warnx("bad size `%s'", argv[1]);
1033 	return 1;
1034     }
1035 
1036     if (size < 0) {
1037 	warnx("size set to negative (%jd)\n", (intmax_t)size);
1038 	return(1);
1039     }
1040     DIP_SET(curinode, di_size, size);
1041     inodirty(curinode);
1042     printactive(0);
1043     return rval;
1044 }
1045 
1046 CMDFUNCSTART(linkcount)
1047 {
1048     int rval = 1;
1049     int lcnt;
1050     char *cp;
1051 
1052     if (!checkactive())
1053 	return 1;
1054 
1055     lcnt = strtol(argv[1], &cp, 0);
1056     if (cp == argv[1] || *cp != '\0' ) {
1057 	warnx("bad link count `%s'", argv[1]);
1058 	return 1;
1059     }
1060     if (lcnt > USHRT_MAX || lcnt < 0) {
1061 	warnx("max link count is %d\n", USHRT_MAX);
1062 	return 1;
1063     }
1064 
1065     DIP_SET(curinode, di_nlink, lcnt);
1066     inodirty(curinode);
1067     printactive(0);
1068     return rval;
1069 }
1070 
1071 CMDFUNCSTART(chowner)
1072 {
1073     int rval = 1;
1074     unsigned long uid;
1075     char *cp;
1076     struct passwd *pwd;
1077 
1078     if (!checkactive())
1079 	return 1;
1080 
1081     uid = strtoul(argv[1], &cp, 0);
1082     if (cp == argv[1] || *cp != '\0' ) {
1083 	/* try looking up name */
1084 	if ((pwd = getpwnam(argv[1]))) {
1085 	    uid = pwd->pw_uid;
1086 	} else {
1087 	    warnx("bad uid `%s'", argv[1]);
1088 	    return 1;
1089 	}
1090     }
1091 
1092     DIP_SET(curinode, di_uid, uid);
1093     inodirty(curinode);
1094     printactive(0);
1095     return rval;
1096 }
1097 
1098 CMDFUNCSTART(chgroup)
1099 {
1100     int rval = 1;
1101     unsigned long gid;
1102     char *cp;
1103     struct group *grp;
1104 
1105     if (!checkactive())
1106 	return 1;
1107 
1108     gid = strtoul(argv[1], &cp, 0);
1109     if (cp == argv[1] || *cp != '\0' ) {
1110 	if ((grp = getgrnam(argv[1]))) {
1111 	    gid = grp->gr_gid;
1112 	} else {
1113 	    warnx("bad gid `%s'", argv[1]);
1114 	    return 1;
1115 	}
1116     }
1117 
1118     DIP_SET(curinode, di_gid, gid);
1119     inodirty(curinode);
1120     printactive(0);
1121     return rval;
1122 }
1123 
1124 int
1125 dotime(char *name, time_t *secp, int32_t *nsecp)
1126 {
1127     char *p, *val;
1128     struct tm t;
1129     int32_t nsec;
1130     p = strchr(name, '.');
1131     if (p) {
1132 	*p = '\0';
1133 	nsec = strtoul(++p, &val, 0);
1134 	if (val == p || *val != '\0' || nsec >= 1000000000 || nsec < 0) {
1135 		warnx("invalid nanoseconds");
1136 		goto badformat;
1137 	}
1138     } else
1139 	nsec = 0;
1140     if (strlen(name) != 14) {
1141 badformat:
1142 	warnx("date format: YYYYMMDDHHMMSS[.nsec]");
1143 	return 1;
1144     }
1145     *nsecp = nsec;
1146 
1147     for (p = name; *p; p++)
1148 	if (*p < '0' || *p > '9')
1149 	    goto badformat;
1150 
1151     p = name;
1152 #define VAL() ((*p++) - '0')
1153     t.tm_year = VAL();
1154     t.tm_year = VAL() + t.tm_year * 10;
1155     t.tm_year = VAL() + t.tm_year * 10;
1156     t.tm_year = VAL() + t.tm_year * 10 - 1900;
1157     t.tm_mon = VAL();
1158     t.tm_mon = VAL() + t.tm_mon * 10 - 1;
1159     t.tm_mday = VAL();
1160     t.tm_mday = VAL() + t.tm_mday * 10;
1161     t.tm_hour = VAL();
1162     t.tm_hour = VAL() + t.tm_hour * 10;
1163     t.tm_min = VAL();
1164     t.tm_min = VAL() + t.tm_min * 10;
1165     t.tm_sec = VAL();
1166     t.tm_sec = VAL() + t.tm_sec * 10;
1167     t.tm_isdst = -1;
1168 
1169     *secp = mktime(&t);
1170     if (*secp == -1) {
1171 	warnx("date/time out of range");
1172 	return 1;
1173     }
1174     return 0;
1175 }
1176 
1177 CMDFUNCSTART(chbtime)
1178 {
1179     time_t secs;
1180     int32_t nsecs;
1181 
1182     if (dotime(argv[1], &secs, &nsecs))
1183 	return 1;
1184     if (sblock.fs_magic == FS_UFS1_MAGIC)
1185 	return 1;
1186     curinode->dp2.di_birthtime = _time_to_time64(secs);
1187     curinode->dp2.di_birthnsec = nsecs;
1188     inodirty(curinode);
1189     printactive(0);
1190     return 0;
1191 }
1192 
1193 CMDFUNCSTART(chmtime)
1194 {
1195     time_t secs;
1196     int32_t nsecs;
1197 
1198     if (dotime(argv[1], &secs, &nsecs))
1199 	return 1;
1200     if (sblock.fs_magic == FS_UFS1_MAGIC)
1201 	curinode->dp1.di_mtime = _time_to_time32(secs);
1202     else
1203 	curinode->dp2.di_mtime = _time_to_time64(secs);
1204     DIP_SET(curinode, di_mtimensec, nsecs);
1205     inodirty(curinode);
1206     printactive(0);
1207     return 0;
1208 }
1209 
1210 CMDFUNCSTART(chatime)
1211 {
1212     time_t secs;
1213     int32_t nsecs;
1214 
1215     if (dotime(argv[1], &secs, &nsecs))
1216 	return 1;
1217     if (sblock.fs_magic == FS_UFS1_MAGIC)
1218 	curinode->dp1.di_atime = _time_to_time32(secs);
1219     else
1220 	curinode->dp2.di_atime = _time_to_time64(secs);
1221     DIP_SET(curinode, di_atimensec, nsecs);
1222     inodirty(curinode);
1223     printactive(0);
1224     return 0;
1225 }
1226 
1227 CMDFUNCSTART(chctime)
1228 {
1229     time_t secs;
1230     int32_t nsecs;
1231 
1232     if (dotime(argv[1], &secs, &nsecs))
1233 	return 1;
1234     if (sblock.fs_magic == FS_UFS1_MAGIC)
1235 	curinode->dp1.di_ctime = _time_to_time32(secs);
1236     else
1237 	curinode->dp2.di_ctime = _time_to_time64(secs);
1238     DIP_SET(curinode, di_ctimensec, nsecs);
1239     inodirty(curinode);
1240     printactive(0);
1241     return 0;
1242 }
1243