xref: /netbsd-src/sbin/fsck_ext2fs/inode.c (revision 466a16a118933bd295a8a104f095714fadf9cf68)
1 /*	$NetBSD: inode.c,v 1.27 2008/11/24 17:41:29 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 1980, 1986, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1997 Manuel Bouyer.
34  *
35  * Redistribution and use in source and binary forms, with or without
36  * modification, are permitted provided that the following conditions
37  * are met:
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgement:
45  *	This product includes software developed by Manuel Bouyer.
46  * 4. The name of the author may not be used to endorse or promote products
47  *    derived from this software without specific prior written permission.
48  *
49  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
50  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
52  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
53  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
54  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
55  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
56  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
57  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
58  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 #ifndef lint
63 #if 0
64 static char sccsid[] = "@(#)inode.c	8.5 (Berkeley) 2/8/95";
65 #else
66 __RCSID("$NetBSD: inode.c,v 1.27 2008/11/24 17:41:29 tsutsui Exp $");
67 #endif
68 #endif /* not lint */
69 
70 #include <sys/param.h>
71 #include <sys/time.h>
72 #include <ufs/ext2fs/ext2fs_dinode.h>
73 #include <ufs/ext2fs/ext2fs_dir.h>
74 #include <ufs/ext2fs/ext2fs.h>
75 
76 #include <ufs/ufs/dinode.h> /* for IFMT & friends */
77 #ifndef SMALL
78 #include <pwd.h>
79 #endif
80 #include <stdio.h>
81 #include <stdlib.h>
82 #include <string.h>
83 #include <time.h>
84 
85 #include "fsck.h"
86 #include "fsutil.h"
87 #include "extern.h"
88 
89 /*
90  * CG is stored in fs byte order in memory, so we can't use ino_to_fsba
91  * here.
92  */
93 
94 #define fsck_ino_to_fsba(fs, x)                      \
95 	(fs2h32((fs)->e2fs_gd[ino_to_cg(fs, x)].ext2bgd_i_tables) + \
96 	(((x)-1) % (fs)->e2fs.e2fs_ipg)/(fs)->e2fs_ipb)
97 
98 
99 static ino_t startinum;
100 
101 static int iblock(struct inodesc *, long, u_int64_t);
102 
103 static int setlarge(void);
104 
105 static int
106 setlarge(void)
107 {
108 	if (sblock.e2fs.e2fs_rev < E2FS_REV1) {
109 		pfatal("LARGE FILES UNSUPPORTED ON REVISION 0 FILESYSTEMS");
110 		return 0;
111 	}
112 	if (!(sblock.e2fs.e2fs_features_rocompat & EXT2F_ROCOMPAT_LARGEFILE)) {
113 		if (preen)
114 			pwarn("SETTING LARGE FILE INDICATOR\n");
115 		else if (!reply("SET LARGE FILE INDICATOR"))
116 			return 0;
117 		sblock.e2fs.e2fs_features_rocompat |= EXT2F_ROCOMPAT_LARGEFILE;
118 		sbdirty();
119 	}
120 	return 1;
121 }
122 
123 u_int64_t
124 inosize(struct ext2fs_dinode *dp)
125 {
126 	u_int64_t size = fs2h32(dp->e2di_size);
127 
128 	if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG)
129 		size |= (u_int64_t)fs2h32(dp->e2di_dacl) << 32;
130 	if (size > INT32_MAX)
131 		(void)setlarge();
132 	return size;
133 }
134 
135 void
136 inossize(struct ext2fs_dinode *dp, u_int64_t size)
137 {
138 	if ((fs2h16(dp->e2di_mode) & IFMT) == IFREG) {
139 		dp->e2di_dacl = h2fs32(size >> 32);
140 		if (size > INT32_MAX)
141 			if (!setlarge())
142 				return;
143 	} else if (size > INT32_MAX) {
144 		pfatal("TRYING TO SET FILESIZE TO %llu ON MODE %x FILE\n",
145 		    (unsigned long long)size, fs2h16(dp->e2di_mode) & IFMT);
146 		return;
147 	}
148 	dp->e2di_size = h2fs32(size);
149 }
150 
151 int
152 ckinode(struct ext2fs_dinode *dp, struct inodesc *idesc)
153 {
154 	u_int32_t *ap;
155 	long ret, n, ndb;
156 	struct ext2fs_dinode dino;
157 	u_int64_t remsize, sizepb;
158 	mode_t mode;
159 	char pathbuf[MAXPATHLEN + 1];
160 
161 	if (idesc->id_fix != IGNORE)
162 		idesc->id_fix = DONTKNOW;
163 	idesc->id_entryno = 0;
164 	idesc->id_filesize = inosize(dp);
165 	mode = fs2h16(dp->e2di_mode) & IFMT;
166 	if (mode == IFBLK || mode == IFCHR || mode == IFIFO ||
167 	    (mode == IFLNK && (inosize(dp) < EXT2_MAXSYMLINKLEN)))
168 		return (KEEPON);
169 	dino = *dp;
170 	ndb = howmany(inosize(&dino), sblock.e2fs_bsize);
171 	for (ap = &dino.e2di_blocks[0]; ap < &dino.e2di_blocks[NDADDR];
172 	    ap++,ndb--) {
173 		idesc->id_numfrags = 1;
174 		if (*ap == 0) {
175 			if (idesc->id_type == DATA && ndb > 0) {
176 				/* An empty block in a directory XXX */
177 				getpathname(pathbuf, sizeof(pathbuf),
178 				    idesc->id_number, idesc->id_number);
179 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
180 				    pathbuf);
181 				if (reply("ADJUST LENGTH") == 1) {
182 					dp = ginode(idesc->id_number);
183 					inossize(dp,
184 					    (ap - &dino.e2di_blocks[0]) *
185 					    sblock.e2fs_bsize);
186 					printf(
187 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
188 					rerun = 1;
189 					inodirty();
190 				}
191 			}
192 			continue;
193 		}
194 		idesc->id_blkno = fs2h32(*ap);
195 		if (idesc->id_type == ADDR)
196 			ret = (*idesc->id_func)(idesc);
197 		else
198 			ret = dirscan(idesc);
199 		if (ret & STOP)
200 			return (ret);
201 	}
202 	idesc->id_numfrags = 1;
203 	remsize = inosize(&dino) - sblock.e2fs_bsize * NDADDR;
204 	sizepb = sblock.e2fs_bsize;
205 	for (ap = &dino.e2di_blocks[NDADDR], n = 1; n <= NIADDR; ap++, n++) {
206 		if (*ap) {
207 			idesc->id_blkno = fs2h32(*ap);
208 			ret = iblock(idesc, n, remsize);
209 			if (ret & STOP)
210 				return (ret);
211 		} else {
212 			if (idesc->id_type == DATA && remsize > 0) {
213 				/* An empty block in a directory XXX */
214 				getpathname(pathbuf, sizeof(pathbuf),
215 				    idesc->id_number, idesc->id_number);
216 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
217 				    pathbuf);
218 				if (reply("ADJUST LENGTH") == 1) {
219 					dp = ginode(idesc->id_number);
220 					inossize(dp, inosize(dp) - remsize);
221 					remsize = 0;
222 					printf(
223 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
224 					rerun = 1;
225 					inodirty();
226 					break;
227 				}
228 			}
229 		}
230 		sizepb *= NINDIR(&sblock);
231 		remsize -= sizepb;
232 	}
233 	return (KEEPON);
234 }
235 
236 static int
237 iblock(struct inodesc *idesc, long ilevel, u_int64_t isize)
238 {
239 	/* XXX ondisk32 */
240 	int32_t *ap;
241 	int32_t *aplim;
242 	struct bufarea *bp;
243 	int i, n, (*func)(struct inodesc *), nif;
244 	u_int64_t sizepb;
245 	char buf[BUFSIZ];
246 	char pathbuf[MAXPATHLEN + 1];
247 	struct ext2fs_dinode *dp;
248 
249 	if (idesc->id_type == ADDR) {
250 		func = idesc->id_func;
251 		if (((n = (*func)(idesc)) & KEEPON) == 0)
252 			return (n);
253 	} else
254 		func = dirscan;
255 	if (chkrange(idesc->id_blkno, idesc->id_numfrags))
256 		return (SKIP);
257 	bp = getdatablk(idesc->id_blkno, sblock.e2fs_bsize);
258 	ilevel--;
259 	for (sizepb = sblock.e2fs_bsize, i = 0; i < ilevel; i++)
260 		sizepb *= NINDIR(&sblock);
261 	if (isize > sizepb * NINDIR(&sblock))
262 		nif = NINDIR(&sblock);
263 	else
264 		nif = howmany(isize, sizepb);
265 	if (idesc->id_func == pass1check &&
266 		nif < NINDIR(&sblock)) {
267 		aplim = &bp->b_un.b_indir[NINDIR(&sblock)];
268 		for (ap = &bp->b_un.b_indir[nif]; ap < aplim; ap++) {
269 			if (*ap == 0)
270 				continue;
271 			(void)snprintf(buf, sizeof(buf),
272 			    "PARTIALLY TRUNCATED INODE I=%llu",
273 			    (unsigned long long)idesc->id_number);
274 			if (dofix(idesc, buf)) {
275 				*ap = 0;
276 				dirty(bp);
277 			}
278 		}
279 		flush(fswritefd, bp);
280 	}
281 	aplim = &bp->b_un.b_indir[nif];
282 	for (ap = bp->b_un.b_indir; ap < aplim; ap++) {
283 		if (*ap) {
284 			idesc->id_blkno = fs2h32(*ap);
285 			if (ilevel == 0)
286 				n = (*func)(idesc);
287 			else
288 				n = iblock(idesc, ilevel, isize);
289 			if (n & STOP) {
290 				bp->b_flags &= ~B_INUSE;
291 				return (n);
292 			}
293 		} else {
294 			if (idesc->id_type == DATA && isize > 0) {
295 				/* An empty block in a directory XXX */
296 				getpathname(pathbuf, sizeof(pathbuf),
297 				    idesc->id_number, idesc->id_number);
298 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
299 				    pathbuf);
300 				if (reply("ADJUST LENGTH") == 1) {
301 					dp = ginode(idesc->id_number);
302 					inossize(dp, inosize(dp) - isize);
303 					isize = 0;
304 					printf(
305 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
306 					rerun = 1;
307 					inodirty();
308 					bp->b_flags &= ~B_INUSE;
309 					return(STOP);
310 				}
311 			}
312 		}
313 		isize -= sizepb;
314 	}
315 	bp->b_flags &= ~B_INUSE;
316 	return (KEEPON);
317 }
318 
319 /*
320  * Check that a block in a legal block number.
321  * Return 0 if in range, 1 if out of range.
322  */
323 int
324 chkrange(daddr_t blk, int cnt)
325 {
326 	int c, overh;
327 
328 	if ((unsigned int)(blk + cnt) > maxfsblock)
329 		return (1);
330 	c = dtog(&sblock, blk);
331 	overh = cgoverhead(c);
332 	if (blk < sblock.e2fs.e2fs_bpg * c + overh +
333 	    sblock.e2fs.e2fs_first_dblock) {
334 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * c + overh +
335 		    sblock.e2fs.e2fs_first_dblock) {
336 			if (debug) {
337 				printf("blk %lld < cgdmin %d;",
338 				    (long long)blk,
339 				    sblock.e2fs.e2fs_bpg * c + overh +
340 				    sblock.e2fs.e2fs_first_dblock);
341 				printf(" blk + cnt %lld > cgsbase %d\n",
342 				    (long long)(blk + cnt),
343 				    sblock.e2fs.e2fs_bpg * c +
344 				    overh + sblock.e2fs.e2fs_first_dblock);
345 			}
346 			return (1);
347 		}
348 	} else {
349 		if ((blk + cnt) > sblock.e2fs.e2fs_bpg * (c + 1) + overh +
350 		    sblock.e2fs.e2fs_first_dblock) {
351 			if (debug)  {
352 				printf("blk %lld >= cgdmin %d;",
353 				    (long long)blk,
354 				    sblock.e2fs.e2fs_bpg * c + overh +
355 				    sblock.e2fs.e2fs_first_dblock);
356 				printf(" blk + cnt %lld > cgdmax %d\n",
357 				    (long long)(blk+cnt),
358 				    sblock.e2fs.e2fs_bpg * (c + 1) +
359 				    overh + sblock.e2fs.e2fs_first_dblock);
360 			}
361 			return (1);
362 		}
363 	}
364 	return (0);
365 }
366 
367 /*
368  * General purpose interface for reading inodes.
369  */
370 struct ext2fs_dinode *
371 ginode(ino_t inumber)
372 {
373 	daddr_t iblk;
374 
375 	if ((inumber < EXT2_FIRSTINO &&
376 	     inumber != EXT2_ROOTINO &&
377 	     !(inumber == EXT2_RESIZEINO &&
378 	       (sblock.e2fs.e2fs_features_compat & EXT2F_COMPAT_RESIZE) != 0))
379 		|| inumber > maxino)
380 		errexit("bad inode number %llu to ginode",
381 		    (unsigned long long)inumber);
382 	if (startinum == 0 ||
383 	    inumber < startinum || inumber >= startinum + sblock.e2fs_ipb) {
384 		iblk = fsck_ino_to_fsba(&sblock, inumber);
385 		if (pbp != 0)
386 			pbp->b_flags &= ~B_INUSE;
387 		pbp = getdatablk(iblk, sblock.e2fs_bsize);
388 		startinum = ((inumber -1) / sblock.e2fs_ipb) * sblock.e2fs_ipb + 1;
389 	}
390 	return (&pbp->b_un.b_dinode[(inumber-1) % sblock.e2fs_ipb]);
391 }
392 
393 /*
394  * Special purpose version of ginode used to optimize first pass
395  * over all the inodes in numerical order.
396  */
397 ino_t nextino, lastinum;
398 long readcnt, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
399 struct ext2fs_dinode *inodebuf;
400 
401 struct ext2fs_dinode *
402 getnextinode(ino_t inumber)
403 {
404 	long size;
405 	daddr_t dblk;
406 	static struct ext2fs_dinode *dp;
407 
408 	if (inumber != nextino++ || inumber > maxino)
409 		errexit("bad inode number %llu to nextinode",
410 		    (unsigned long long)inumber);
411 	if (inumber >= lastinum) {
412 		readcnt++;
413 		dblk = fsbtodb(&sblock, fsck_ino_to_fsba(&sblock, lastinum));
414 		if (readcnt % readpercg == 0) {
415 			size = partialsize;
416 			lastinum += partialcnt;
417 		} else {
418 			size = inobufsize;
419 			lastinum += fullcnt;
420 		}
421 		(void)bread(fsreadfd, (char *)inodebuf, dblk, size);
422 		dp = inodebuf;
423 	}
424 	return (dp++);
425 }
426 
427 void
428 resetinodebuf(void)
429 {
430 
431 	startinum = 0;
432 	nextino = 1;
433 	lastinum = 1;
434 	readcnt = 0;
435 	inobufsize = blkroundup(&sblock, INOBUFSIZE);
436 	fullcnt = inobufsize / sizeof(struct ext2fs_dinode);
437 	readpercg = sblock.e2fs.e2fs_ipg / fullcnt;
438 	partialcnt = sblock.e2fs.e2fs_ipg % fullcnt;
439 	partialsize = partialcnt * sizeof(struct ext2fs_dinode);
440 	if (partialcnt != 0) {
441 		readpercg++;
442 	} else {
443 		partialcnt = fullcnt;
444 		partialsize = inobufsize;
445 	}
446 	if (inodebuf == NULL &&
447 	    (inodebuf = malloc((unsigned int)inobufsize)) == NULL)
448 		errexit("Cannot allocate space for inode buffer");
449 	while (nextino < EXT2_ROOTINO)
450 		(void)getnextinode(nextino);
451 }
452 
453 void
454 freeinodebuf(void)
455 {
456 
457 	if (inodebuf != NULL)
458 		free(inodebuf);
459 	inodebuf = NULL;
460 }
461 
462 /*
463  * Routines to maintain information about directory inodes.
464  * This is built during the first pass and used during the
465  * second and third passes.
466  *
467  * Enter inodes into the cache.
468  */
469 void
470 cacheino(struct ext2fs_dinode *dp, ino_t inumber)
471 {
472 	struct inoinfo *inp;
473 	struct inoinfo **inpp;
474 	unsigned int blks;
475 
476 	blks = howmany(inosize(dp), sblock.e2fs_bsize);
477 	if (blks > NDADDR)
478 		blks = NDADDR + NIADDR;
479 	/* XXX ondisk32 */
480 	inp = malloc(sizeof(*inp) + (blks - 1) * sizeof(int32_t));
481 	if (inp == NULL)
482 		return;
483 	inpp = &inphead[inumber % numdirs];
484 	inp->i_nexthash = *inpp;
485 	*inpp = inp;
486 	inp->i_child = inp->i_sibling = inp->i_parentp = 0;
487 	if (inumber == EXT2_ROOTINO)
488 		inp->i_parent = EXT2_ROOTINO;
489 	else
490 		inp->i_parent = (ino_t)0;
491 	inp->i_dotdot = (ino_t)0;
492 	inp->i_number = inumber;
493 	inp->i_isize = inosize(dp);
494 	/* XXX ondisk32 */
495 	inp->i_numblks = blks * sizeof(int32_t);
496 	memcpy(&inp->i_blks[0], &dp->e2di_blocks[0], (size_t)inp->i_numblks);
497 	if (inplast == listmax) {
498 		listmax += 100;
499 		inpsort = (struct inoinfo **)realloc((char *)inpsort,
500 		    (unsigned int)listmax * sizeof(struct inoinfo *));
501 		if (inpsort == NULL)
502 			errexit("cannot increase directory list");
503 	}
504 	inpsort[inplast++] = inp;
505 }
506 
507 /*
508  * Look up an inode cache structure.
509  */
510 struct inoinfo *
511 getinoinfo(ino_t inumber)
512 {
513 	struct inoinfo *inp;
514 
515 	for (inp = inphead[inumber % numdirs]; inp; inp = inp->i_nexthash) {
516 		if (inp->i_number != inumber)
517 			continue;
518 		return (inp);
519 	}
520 	errexit("cannot find inode %llu", (unsigned long long)inumber);
521 	return ((struct inoinfo *)0);
522 }
523 
524 /*
525  * Clean up all the inode cache structure.
526  */
527 void
528 inocleanup(void)
529 {
530 	struct inoinfo **inpp;
531 
532 	if (inphead == NULL)
533 		return;
534 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
535 		free(*inpp);
536 	free(inphead);
537 	free(inpsort);
538 	inphead = inpsort = NULL;
539 }
540 
541 void
542 inodirty(void)
543 {
544 
545 	dirty(pbp);
546 }
547 
548 void
549 clri(struct inodesc *idesc, const char *type, int flag)
550 {
551 	struct ext2fs_dinode *dp;
552 
553 	dp = ginode(idesc->id_number);
554 	if (flag == 1) {
555 		pwarn("%s %s", type,
556 		    (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
557 		pinode(idesc->id_number);
558 	}
559 	if (preen || reply("CLEAR") == 1) {
560 		if (preen)
561 			printf(" (CLEARED)\n");
562 		n_files--;
563 		(void)ckinode(dp, idesc);
564 		clearinode(dp);
565 		statemap[idesc->id_number] = USTATE;
566 		inodirty();
567 	}
568 }
569 
570 int
571 findname(struct inodesc *idesc)
572 {
573 	struct ext2fs_direct *dirp = idesc->id_dirp;
574 	u_int16_t namlen = dirp->e2d_namlen;
575 	/* from utilities.c namebuf[] variable */
576 	char *buf = __UNCONST(idesc->id_name);
577 	if (namlen > MAXPATHLEN) {
578 		/* XXX: Prevent overflow but don't fix */
579 		namlen = MAXPATHLEN;
580 	}
581 
582 	if (fs2h32(dirp->e2d_ino) != idesc->id_parent)
583 		return (KEEPON);
584 	(void)memcpy(buf, dirp->e2d_name, (size_t)namlen);
585 	buf[namlen] = '\0';
586 	return (STOP|FOUND);
587 }
588 
589 int
590 findino(struct inodesc *idesc)
591 {
592 	struct ext2fs_direct *dirp = idesc->id_dirp;
593 	u_int32_t ino = fs2h32(dirp->e2d_ino);
594 
595 	if (ino == 0)
596 		return (KEEPON);
597 	if (strcmp(dirp->e2d_name, idesc->id_name) == 0 &&
598 	    (ino == EXT2_ROOTINO || ino >= EXT2_FIRSTINO)
599 		&& ino <= maxino) {
600 		idesc->id_parent = ino;
601 		return (STOP|FOUND);
602 	}
603 	return (KEEPON);
604 }
605 
606 void
607 pinode(ino_t ino)
608 {
609 	struct ext2fs_dinode *dp;
610 	char *p;
611 	struct passwd *pw;
612 	time_t t;
613 	uid_t uid;
614 
615 	printf(" I=%llu ", (unsigned long long)ino);
616 	if ((ino < EXT2_FIRSTINO && ino != EXT2_ROOTINO) || ino > maxino)
617 		return;
618 	dp = ginode(ino);
619 	uid = fs2h16(dp->e2di_uid);
620 	if (sblock.e2fs.e2fs_rev > E2FS_REV0)
621 		uid |= fs2h16(dp->e2di_uid_high) << 16;
622 	printf(" OWNER=");
623 #ifndef SMALL
624 	if (Uflag && (pw = getpwuid(uid)) != 0)
625 		printf("%s ", pw->pw_name);
626 	else
627 #endif
628 		printf("%u ", (unsigned int)uid);
629 	printf("MODE=%o\n", fs2h16(dp->e2di_mode));
630 	if (preen)
631 		printf("%s: ", cdevname());
632 	printf("SIZE=%llu ", (long long)inosize(dp));
633 	t = fs2h32(dp->e2di_mtime);
634 	p = ctime(&t);
635 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
636 }
637 
638 void
639 blkerror(ino_t ino, const char *type, daddr_t blk)
640 {
641 
642 	pfatal("%lld %s I=%llu", (long long)blk, type, (unsigned long long)ino);
643 	printf("\n");
644 	switch (statemap[ino]) {
645 
646 	case FSTATE:
647 		statemap[ino] = FCLEAR;
648 		return;
649 
650 	case DSTATE:
651 		statemap[ino] = DCLEAR;
652 		return;
653 
654 	case FCLEAR:
655 	case DCLEAR:
656 		return;
657 
658 	default:
659 		errexit("BAD STATE %d TO BLKERR", statemap[ino]);
660 		/* NOTREACHED */
661 	}
662 }
663 
664 /*
665  * allocate an unused inode
666  */
667 ino_t
668 allocino(ino_t request, int type)
669 {
670 	ino_t ino;
671 	struct ext2fs_dinode *dp;
672 	time_t t;
673 
674 	if (request == 0)
675 		request = EXT2_ROOTINO;
676 	else if (statemap[request] != USTATE)
677 		return (0);
678 	for (ino = request; ino < maxino; ino++) {
679 		if ((ino > EXT2_ROOTINO) && (ino < EXT2_FIRSTINO))
680 			continue;
681 		if (statemap[ino] == USTATE)
682 			break;
683 	}
684 	if (ino == maxino)
685 		return (0);
686 	switch (type & IFMT) {
687 	case IFDIR:
688 		statemap[ino] = DSTATE;
689 		break;
690 	case IFREG:
691 	case IFLNK:
692 		statemap[ino] = FSTATE;
693 		break;
694 	default:
695 		return (0);
696 	}
697 	dp = ginode(ino);
698 	dp->e2di_blocks[0] = h2fs32(allocblk());
699 	if (dp->e2di_blocks[0] == 0) {
700 		statemap[ino] = USTATE;
701 		return (0);
702 	}
703 	dp->e2di_mode = h2fs16(type);
704 	(void)time(&t);
705 	dp->e2di_atime = h2fs32(t);
706 	dp->e2di_mtime = dp->e2di_ctime = dp->e2di_atime;
707 	dp->e2di_dtime = 0;
708 	inossize(dp, sblock.e2fs_bsize);
709 	dp->e2di_nblock = h2fs32(btodb(sblock.e2fs_bsize));
710 	n_files++;
711 	inodirty();
712 	typemap[ino] = E2IFTODT(type);
713 	return (ino);
714 }
715 
716 /*
717  * deallocate an inode
718  */
719 void
720 freeino(ino_t ino)
721 {
722 	struct inodesc idesc;
723 	struct ext2fs_dinode *dp;
724 
725 	memset(&idesc, 0, sizeof(struct inodesc));
726 	idesc.id_type = ADDR;
727 	idesc.id_func = pass4check;
728 	idesc.id_number = ino;
729 	dp = ginode(ino);
730 	(void)ckinode(dp, &idesc);
731 	clearinode(dp);
732 	inodirty();
733 	statemap[ino] = USTATE;
734 	n_files--;
735 }
736