xref: /freebsd/sbin/fsck_ffs/inode.c (revision 69c5fa5cd1ec9b09ed88a086607a8a0993818db9)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 #if 0
33 #ifndef lint
34 static const char sccsid[] = "@(#)inode.c	8.8 (Berkeley) 4/28/95";
35 #endif /* not lint */
36 #endif
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include <sys/param.h>
41 #include <sys/stdint.h>
42 #include <sys/sysctl.h>
43 
44 #include <ufs/ufs/dinode.h>
45 #include <ufs/ufs/dir.h>
46 #include <ufs/ffs/fs.h>
47 
48 #include <err.h>
49 #include <pwd.h>
50 #include <string.h>
51 #include <time.h>
52 #include <libufs.h>
53 
54 #include "fsck.h"
55 
56 struct bufarea *icachebp;	/* inode cache buffer */
57 
58 static int iblock(struct inodesc *, off_t isize, int type);
59 static ufs2_daddr_t indir_blkatoff(ufs2_daddr_t, ino_t, ufs_lbn_t, ufs_lbn_t,
60     struct bufarea **);
61 
62 int
63 ckinode(union dinode *dp, struct inodesc *idesc)
64 {
65 	off_t remsize, sizepb;
66 	int i, offset, ret;
67 	struct inode ip;
68 	union dinode dino;
69 	ufs2_daddr_t ndb;
70 	mode_t mode;
71 	char pathbuf[MAXPATHLEN + 1];
72 
73 	if (idesc->id_fix != IGNORE)
74 		idesc->id_fix = DONTKNOW;
75 	idesc->id_dp = dp;
76 	idesc->id_lbn = -1;
77 	idesc->id_lballoc = -1;
78 	idesc->id_level = 0;
79 	idesc->id_entryno = 0;
80 	idesc->id_filesize = DIP(dp, di_size);
81 	mode = DIP(dp, di_mode) & IFMT;
82 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
83 	    DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
84 		return (KEEPON);
85 	if (sblock.fs_magic == FS_UFS1_MAGIC)
86 		dino.dp1 = dp->dp1;
87 	else
88 		dino.dp2 = dp->dp2;
89 	ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
90 	for (i = 0; i < UFS_NDADDR; i++) {
91 		idesc->id_lbn++;
92 		if (--ndb == 0 &&
93 		    (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
94 			idesc->id_numfrags =
95 				numfrags(&sblock, fragroundup(&sblock, offset));
96 		else
97 			idesc->id_numfrags = sblock.fs_frag;
98 		if (DIP(&dino, di_db[i]) == 0) {
99 			if (idesc->id_type == DATA && ndb >= 0) {
100 				/* An empty block in a directory XXX */
101 				getpathname(pathbuf, idesc->id_number,
102 						idesc->id_number);
103 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
104 					pathbuf);
105 				if (reply("ADJUST LENGTH") == 1) {
106 					ginode(idesc->id_number, &ip);
107 					DIP_SET(ip.i_dp, di_size,
108 					    i * sblock.fs_bsize);
109 					printf(
110 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
111 					rerun = 1;
112 					inodirty(&ip);
113 					irelse(&ip);
114 				}
115 			}
116 			continue;
117 		}
118 		idesc->id_blkno = DIP(&dino, di_db[i]);
119 		if (idesc->id_type != DATA)
120 			ret = (*idesc->id_func)(idesc);
121 		else
122 			ret = dirscan(idesc);
123 		if (ret & STOP)
124 			return (ret);
125 	}
126 	idesc->id_numfrags = sblock.fs_frag;
127 	remsize = DIP(&dino, di_size) - sblock.fs_bsize * UFS_NDADDR;
128 	sizepb = sblock.fs_bsize;
129 	for (i = 0; i < UFS_NIADDR; i++) {
130 		sizepb *= NINDIR(&sblock);
131 		idesc->id_level = i + 1;
132 		if (DIP(&dino, di_ib[i])) {
133 			idesc->id_blkno = DIP(&dino, di_ib[i]);
134 			ret = iblock(idesc, remsize, BT_LEVEL1 + i);
135 			if (ret & STOP)
136 				return (ret);
137 		} else if (remsize > 0) {
138 			idesc->id_lbn += sizepb / sblock.fs_bsize;
139 			if (idesc->id_type == DATA) {
140 				/* An empty block in a directory XXX */
141 				getpathname(pathbuf, idesc->id_number,
142 						idesc->id_number);
143 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
144 					pathbuf);
145 				if (reply("ADJUST LENGTH") == 1) {
146 					ginode(idesc->id_number, &ip);
147 					DIP_SET(ip.i_dp, di_size,
148 					    DIP(ip.i_dp, di_size) - remsize);
149 					remsize = 0;
150 					printf(
151 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
152 					rerun = 1;
153 					inodirty(&ip);
154 					irelse(&ip);
155 					break;
156 				}
157 			}
158 		}
159 		remsize -= sizepb;
160 	}
161 	return (KEEPON);
162 }
163 
164 static int
165 iblock(struct inodesc *idesc, off_t isize, int type)
166 {
167 	struct inode ip;
168 	struct bufarea *bp;
169 	int i, n, (*func)(struct inodesc *), nif;
170 	off_t sizepb;
171 	char buf[BUFSIZ];
172 	char pathbuf[MAXPATHLEN + 1];
173 
174 	if (idesc->id_type != DATA) {
175 		func = idesc->id_func;
176 		if (((n = (*func)(idesc)) & KEEPON) == 0)
177 			return (n);
178 	} else
179 		func = dirscan;
180 	bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
181 	if (bp->b_errs != 0) {
182 		brelse(bp);
183 		return (SKIP);
184 	}
185 	idesc->id_bp = bp;
186 	idesc->id_level--;
187 	for (sizepb = sblock.fs_bsize, i = 0; i < idesc->id_level; i++)
188 		sizepb *= NINDIR(&sblock);
189 	if (howmany(isize, sizepb) > NINDIR(&sblock))
190 		nif = NINDIR(&sblock);
191 	else
192 		nif = howmany(isize, sizepb);
193 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
194 		for (i = nif; i < NINDIR(&sblock); i++) {
195 			if (IBLK(bp, i) == 0)
196 				continue;
197 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
198 			    (u_long)idesc->id_number);
199 			if (preen) {
200 				pfatal("%s", buf);
201 			} else if (dofix(idesc, buf)) {
202 				IBLK_SET(bp, i, 0);
203 				dirty(bp);
204 			}
205 		}
206 		flush(fswritefd, bp);
207 	}
208 	for (i = 0; i < nif; i++) {
209 		if (IBLK(bp, i)) {
210 			idesc->id_blkno = IBLK(bp, i);
211 			bp->b_index = i;
212 			if (idesc->id_level == 0) {
213 				idesc->id_lbn++;
214 				n = (*func)(idesc);
215 			} else {
216 				n = iblock(idesc, isize, type);
217 				idesc->id_level++;
218 			}
219 			if (n & STOP) {
220 				brelse(bp);
221 				return (n);
222 			}
223 		} else {
224 			idesc->id_lbn += sizepb / sblock.fs_bsize;
225 			if (idesc->id_type == DATA && isize > 0) {
226 				/* An empty block in a directory XXX */
227 				getpathname(pathbuf, idesc->id_number,
228 						idesc->id_number);
229 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
230 					pathbuf);
231 				if (reply("ADJUST LENGTH") == 1) {
232 					ginode(idesc->id_number, &ip);
233 					DIP_SET(ip.i_dp, di_size,
234 					    DIP(ip.i_dp, di_size) - isize);
235 					isize = 0;
236 					printf(
237 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
238 					rerun = 1;
239 					inodirty(&ip);
240 					brelse(bp);
241 					return(STOP);
242 				}
243 			}
244 		}
245 		isize -= sizepb;
246 	}
247 	brelse(bp);
248 	return (KEEPON);
249 }
250 
251 /*
252  * Finds the disk block address at the specified lbn within the inode
253  * specified by dp.  This follows the whole tree and honors di_size and
254  * di_extsize so it is a true test of reachability.  The lbn may be
255  * negative if an extattr or indirect block is requested.
256  */
257 ufs2_daddr_t
258 ino_blkatoff(union dinode *dp, ino_t ino, ufs_lbn_t lbn, int *frags,
259     struct bufarea **bpp)
260 {
261 	ufs_lbn_t tmpval;
262 	ufs_lbn_t cur;
263 	ufs_lbn_t next;
264 	int i;
265 
266 	*frags = 0;
267 	/*
268 	 * Handle extattr blocks first.
269 	 */
270 	if (lbn < 0 && lbn >= -UFS_NXADDR) {
271 		lbn = -1 - lbn;
272 		if (lbn > lblkno(&sblock, dp->dp2.di_extsize - 1))
273 			return (0);
274 		*frags = numfrags(&sblock,
275 		    sblksize(&sblock, dp->dp2.di_extsize, lbn));
276 		return (dp->dp2.di_extb[lbn]);
277 	}
278 	/*
279 	 * Now direct and indirect.
280 	 */
281 	if (DIP(dp, di_mode) == IFLNK &&
282 	    DIP(dp, di_size) < sblock.fs_maxsymlinklen)
283 		return (0);
284 	if (lbn >= 0 && lbn < UFS_NDADDR) {
285 		*frags = numfrags(&sblock,
286 		    sblksize(&sblock, DIP(dp, di_size), lbn));
287 		return (DIP(dp, di_db[lbn]));
288 	}
289 	*frags = sblock.fs_frag;
290 
291 	for (i = 0, tmpval = NINDIR(&sblock), cur = UFS_NDADDR; i < UFS_NIADDR;
292 	    i++, tmpval *= NINDIR(&sblock), cur = next) {
293 		next = cur + tmpval;
294 		if (lbn == -cur - i)
295 			return (DIP(dp, di_ib[i]));
296 		/*
297 		 * Determine whether the lbn in question is within this tree.
298 		 */
299 		if (lbn < 0 && -lbn >= next)
300 			continue;
301 		if (lbn > 0 && lbn >= next)
302 			continue;
303 		return (indir_blkatoff(DIP(dp, di_ib[i]), ino, -cur - i, lbn,
304 		    bpp));
305 	}
306 	pfatal("lbn %jd not in ino %ju\n", lbn, (uintmax_t)ino);
307 	return (0);
308 }
309 
310 /*
311  * Fetch an indirect block to find the block at a given lbn.  The lbn
312  * may be negative to fetch a specific indirect block pointer or positive
313  * to fetch a specific block.
314  */
315 static ufs2_daddr_t
316 indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn,
317     struct bufarea **bpp)
318 {
319 	struct bufarea *bp;
320 	ufs_lbn_t lbnadd;
321 	ufs_lbn_t base;
322 	int i, level;
323 
324 	if (blk == 0)
325 		return (0);
326 	level = lbn_level(cur);
327 	if (level == -1)
328 		pfatal("Invalid indir lbn %jd in ino %ju\n",
329 		    lbn, (uintmax_t)ino);
330 	if (level == 0 && lbn < 0)
331 		pfatal("Invalid lbn %jd in ino %ju\n",
332 		    lbn, (uintmax_t)ino);
333 	lbnadd = 1;
334 	base = -(cur + level);
335 	for (i = level; i > 0; i--)
336 		lbnadd *= NINDIR(&sblock);
337 	if (lbn > 0)
338 		i = (lbn - base) / lbnadd;
339 	else
340 		i = (-lbn - base) / lbnadd;
341 	if (i < 0 || i >= NINDIR(&sblock)) {
342 		pfatal("Invalid indirect index %d produced by lbn %jd "
343 		    "in ino %ju\n", i, lbn, (uintmax_t)ino);
344 		return (0);
345 	}
346 	if (level == 0)
347 		cur = base + (i * lbnadd);
348 	else
349 		cur = -(base + (i * lbnadd)) - (level - 1);
350 	bp = getdatablk(blk, sblock.fs_bsize, BT_LEVEL1 + level);
351 	if (bp->b_errs != 0)
352 		return (0);
353 	blk = IBLK(bp, i);
354 	bp->b_index = i;
355 	if (bpp != NULL)
356 		*bpp = bp;
357 	else
358 		brelse(bp);
359 	if (cur == lbn)
360 		return (blk);
361 	if (level == 0)
362 		pfatal("Invalid lbn %jd at level 0 for ino %ju\n", lbn,
363 		    (uintmax_t)ino);
364 	return (indir_blkatoff(blk, ino, cur, lbn, bpp));
365 }
366 
367 /*
368  * Check that a block in a legal block number.
369  * Return 0 if in range, 1 if out of range.
370  */
371 int
372 chkrange(ufs2_daddr_t blk, int cnt)
373 {
374 	int c;
375 
376 	if (cnt <= 0 || blk <= 0 || blk > maxfsblock ||
377 	    cnt - 1 > maxfsblock - blk)
378 		return (1);
379 	if (cnt > sblock.fs_frag ||
380 	    fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
381 		if (debug)
382 			printf("bad size: blk %ld, offset %i, size %d\n",
383 			    (long)blk, (int)fragnum(&sblock, blk), cnt);
384 		return (1);
385 	}
386 	c = dtog(&sblock, blk);
387 	if (blk < cgdmin(&sblock, c)) {
388 		if ((blk + cnt) > cgsblock(&sblock, c)) {
389 			if (debug) {
390 				printf("blk %ld < cgdmin %ld;",
391 				    (long)blk, (long)cgdmin(&sblock, c));
392 				printf(" blk + cnt %ld > cgsbase %ld\n",
393 				    (long)(blk + cnt),
394 				    (long)cgsblock(&sblock, c));
395 			}
396 			return (1);
397 		}
398 	} else {
399 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
400 			if (debug)  {
401 				printf("blk %ld >= cgdmin %ld;",
402 				    (long)blk, (long)cgdmin(&sblock, c));
403 				printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
404 				    (long)(blk + cnt), (long)sblock.fs_fpg);
405 			}
406 			return (1);
407 		}
408 	}
409 	return (0);
410 }
411 
412 /*
413  * General purpose interface for reading inodes.
414  */
415 void
416 ginode(ino_t inumber, struct inode *ip)
417 {
418 	ufs2_daddr_t iblk;
419 	static ino_t startinum = -1;
420 
421 	if (inumber < UFS_ROOTINO || inumber > maxino)
422 		errx(EEXIT, "bad inode number %ju to ginode",
423 		    (uintmax_t)inumber);
424 	ip->i_number = inumber;
425 	if (startinum != -1 &&
426 	    inumber >= startinum && inumber < startinum + INOPB(&sblock)) {
427 		/* take an additional reference for the returned inode */
428 		icachebp->b_refcnt++;
429 	} else {
430 		iblk = ino_to_fsba(&sblock, inumber);
431 		/* release our cache-hold reference on old icachebp */
432 		if (icachebp != NULL)
433 			brelse(icachebp);
434 		icachebp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
435 		if (icachebp->b_errs != 0) {
436 			ip->i_bp = NULL;
437 			ip->i_dp = &zino;
438 			return;
439 		}
440 		startinum = rounddown(inumber, INOPB(&sblock));
441 		/* take a cache-hold reference on new icachebp */
442 		icachebp->b_refcnt++;
443 		icachebp->b_index = startinum;
444 	}
445 	ip->i_bp = icachebp;
446 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
447 		ip->i_dp = (union dinode *)
448 		    &icachebp->b_un.b_dinode1[inumber % INOPB(&sblock)];
449 		return;
450 	}
451 	ip->i_dp = (union dinode *)
452 	    &icachebp->b_un.b_dinode2[inumber % INOPB(&sblock)];
453 	if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)ip->i_dp)) {
454 		pwarn("INODE CHECK-HASH FAILED");
455 		prtinode(ip);
456 		if (preen || reply("FIX") != 0) {
457 			if (preen)
458 				printf(" (FIXED)\n");
459 			ffs_update_dinode_ckhash(&sblock,
460 			    (struct ufs2_dinode *)ip->i_dp);
461 			inodirty(ip);
462 		}
463 	}
464 }
465 
466 /*
467  * Release a held inode.
468  */
469 void
470 irelse(struct inode *ip)
471 {
472 
473 	if (ip->i_bp->b_refcnt <= 0)
474 		pfatal("irelse: releasing unreferenced ino %ju\n",
475 		    (uintmax_t) ip->i_number);
476 	brelse(ip->i_bp);
477 }
478 
479 /*
480  * Special purpose version of ginode used to optimize first pass
481  * over all the inodes in numerical order.
482  */
483 static ino_t nextino, lastinum, lastvalidinum;
484 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
485 static struct bufarea inobuf;
486 
487 union dinode *
488 getnextinode(ino_t inumber, int rebuildcg)
489 {
490 	int j;
491 	long size;
492 	mode_t mode;
493 	ufs2_daddr_t ndb, blk;
494 	union dinode *dp;
495 	struct inode ip;
496 	static caddr_t nextinop;
497 
498 	if (inumber != nextino++ || inumber > lastvalidinum)
499 		errx(EEXIT, "bad inode number %ju to nextinode",
500 		    (uintmax_t)inumber);
501 	if (inumber >= lastinum) {
502 		readcount++;
503 		blk = ino_to_fsba(&sblock, lastinum);
504 		if (readcount % readpercg == 0) {
505 			size = partialsize;
506 			lastinum += partialcnt;
507 		} else {
508 			size = inobufsize;
509 			lastinum += fullcnt;
510 		}
511 		/*
512 		 * Flush old contents in case they have been updated.
513 		 * If getblk encounters an error, it will already have zeroed
514 		 * out the buffer, so we do not need to do so here.
515 		 */
516 		flush(fswritefd, &inobuf);
517 		getblk(&inobuf, blk, size);
518 		nextinop = inobuf.b_un.b_buf;
519 	}
520 	dp = (union dinode *)nextinop;
521 	if (sblock.fs_magic == FS_UFS1_MAGIC)
522 		nextinop += sizeof(struct ufs1_dinode);
523 	else
524 		nextinop += sizeof(struct ufs2_dinode);
525 	if ((ckhashadd & CK_INODE) != 0) {
526 		ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
527 		dirty(&inobuf);
528 	}
529 	if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
530 		pwarn("INODE CHECK-HASH FAILED");
531 		ip.i_bp = NULL;
532 		ip.i_dp = dp;
533 		ip.i_number = inumber;
534 		prtinode(&ip);
535 		if (preen || reply("FIX") != 0) {
536 			if (preen)
537 				printf(" (FIXED)\n");
538 			ffs_update_dinode_ckhash(&sblock,
539 			    (struct ufs2_dinode *)dp);
540 			dirty(&inobuf);
541 		}
542 	}
543 	if (rebuildcg && (char *)dp == inobuf.b_un.b_buf) {
544 		/*
545 		 * Try to determine if we have reached the end of the
546 		 * allocated inodes.
547 		 */
548 		mode = DIP(dp, di_mode) & IFMT;
549 		if (mode == 0) {
550 			if (memcmp(dp->dp2.di_db, zino.dp2.di_db,
551 				UFS_NDADDR * sizeof(ufs2_daddr_t)) ||
552 			      memcmp(dp->dp2.di_ib, zino.dp2.di_ib,
553 				UFS_NIADDR * sizeof(ufs2_daddr_t)) ||
554 			      dp->dp2.di_mode || dp->dp2.di_size)
555 				return (NULL);
556 			return (dp);
557 		}
558 		if (!ftypeok(dp))
559 			return (NULL);
560 		ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
561 		if (ndb < 0)
562 			return (NULL);
563 		if (mode == IFBLK || mode == IFCHR)
564 			ndb++;
565 		if (mode == IFLNK) {
566 			/*
567 			 * Fake ndb value so direct/indirect block checks below
568 			 * will detect any garbage after symlink string.
569 			 */
570 			if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
571 				ndb = howmany(DIP(dp, di_size),
572 				    sizeof(ufs2_daddr_t));
573 				if (ndb > UFS_NDADDR) {
574 					j = ndb - UFS_NDADDR;
575 					for (ndb = 1; j > 1; j--)
576 						ndb *= NINDIR(&sblock);
577 					ndb += UFS_NDADDR;
578 				}
579 			}
580 		}
581 		for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++)
582 			if (DIP(dp, di_db[j]) != 0)
583 				return (NULL);
584 		for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++)
585 			ndb /= NINDIR(&sblock);
586 		for (; j < UFS_NIADDR; j++)
587 			if (DIP(dp, di_ib[j]) != 0)
588 				return (NULL);
589 	}
590 	return (dp);
591 }
592 
593 void
594 setinodebuf(int cg, ino_t inosused)
595 {
596 	ino_t inum;
597 
598 	inum = cg * sblock.fs_ipg;
599 	lastvalidinum = inum + inosused - 1;
600 	nextino = inum;
601 	lastinum = inum;
602 	readcount = 0;
603 	if (inobuf.b_un.b_buf == NULL) {
604 		inobufsize = blkroundup(&sblock,
605 		    MAX(INOBUFSIZE, sblock.fs_bsize));
606 		initbarea(&inobuf, BT_INODES);
607 		if ((inobuf.b_un.b_buf = Malloc((unsigned)inobufsize)) == NULL)
608 			errx(EEXIT, "cannot allocate space for inode buffer");
609 	}
610 	fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
611 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
612 	readpercg = inosused / fullcnt;
613 	partialcnt = inosused % fullcnt;
614 	partialsize = fragroundup(&sblock,
615 	    partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
616 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)));
617 	if (partialcnt != 0) {
618 		readpercg++;
619 	} else {
620 		partialcnt = fullcnt;
621 		partialsize = inobufsize;
622 	}
623 }
624 
625 int
626 freeblock(struct inodesc *idesc)
627 {
628 	struct dups *dlp;
629 	ufs2_daddr_t blkno;
630 	long nfrags, res;
631 
632 	res = KEEPON;
633 	blkno = idesc->id_blkno;
634 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
635 		if (chkrange(blkno, 1)) {
636 			res = SKIP;
637 		} else if (testbmap(blkno)) {
638 			for (dlp = duplist; dlp; dlp = dlp->next) {
639 				if (dlp->dup != blkno)
640 					continue;
641 				dlp->dup = duplist->dup;
642 				dlp = duplist;
643 				duplist = duplist->next;
644 				free((char *)dlp);
645 				break;
646 			}
647 			if (dlp == NULL) {
648 				clrbmap(blkno);
649 				n_blks--;
650 			}
651 		}
652 	}
653 	return (res);
654 }
655 
656 void
657 freeinodebuf(void)
658 {
659 
660 	/*
661 	 * Flush old contents in case they have been updated.
662 	 */
663 	flush(fswritefd, &inobuf);
664 	if (inobuf.b_un.b_buf != NULL)
665 		free((char *)inobuf.b_un.b_buf);
666 	inobuf.b_un.b_buf = NULL;
667 }
668 
669 /*
670  * Routines to maintain information about directory inodes.
671  * This is built during the first pass and used during the
672  * second and third passes.
673  *
674  * Enter inodes into the cache.
675  */
676 void
677 cacheino(union dinode *dp, ino_t inumber)
678 {
679 	struct inoinfo *inp, **inpp;
680 	int i, blks;
681 
682 	if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR)
683 		blks = UFS_NDADDR + UFS_NIADDR;
684 	else if (DIP(dp, di_size) > 0)
685 		blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
686 	else
687 		blks = 1;
688 	inp = (struct inoinfo *)
689 		Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
690 	if (inp == NULL)
691 		errx(EEXIT, "cannot increase directory list");
692 	inpp = &inphead[inumber % dirhash];
693 	inp->i_nexthash = *inpp;
694 	*inpp = inp;
695 	inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0;
696 	inp->i_dotdot = (ino_t)0;
697 	inp->i_number = inumber;
698 	inp->i_isize = DIP(dp, di_size);
699 	inp->i_numblks = blks;
700 	for (i = 0; i < MIN(blks, UFS_NDADDR); i++)
701 		inp->i_blks[i] = DIP(dp, di_db[i]);
702 	if (blks > UFS_NDADDR)
703 		for (i = 0; i < UFS_NIADDR; i++)
704 			inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]);
705 	if (inplast == listmax) {
706 		listmax += 100;
707 		inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
708 		    listmax, sizeof(struct inoinfo *));
709 		if (inpsort == NULL)
710 			errx(EEXIT, "cannot increase directory list");
711 	}
712 	inpsort[inplast++] = inp;
713 }
714 
715 /*
716  * Look up an inode cache structure.
717  */
718 struct inoinfo *
719 getinoinfo(ino_t inumber)
720 {
721 	struct inoinfo *inp;
722 
723 	for (inp = inphead[inumber % dirhash]; inp; inp = inp->i_nexthash) {
724 		if (inp->i_number != inumber)
725 			continue;
726 		return (inp);
727 	}
728 	errx(EEXIT, "cannot find inode %ju", (uintmax_t)inumber);
729 	return ((struct inoinfo *)0);
730 }
731 
732 /*
733  * Clean up all the inode cache structure.
734  */
735 void
736 inocleanup(void)
737 {
738 	struct inoinfo **inpp;
739 
740 	if (inphead == NULL)
741 		return;
742 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
743 		free((char *)(*inpp));
744 	free((char *)inphead);
745 	free((char *)inpsort);
746 	inphead = inpsort = NULL;
747 }
748 
749 void
750 inodirty(struct inode *ip)
751 {
752 
753 	if (sblock.fs_magic == FS_UFS2_MAGIC)
754 		ffs_update_dinode_ckhash(&sblock,
755 		    (struct ufs2_dinode *)ip->i_dp);
756 	dirty(ip->i_bp);
757 }
758 
759 void
760 clri(struct inodesc *idesc, const char *type, int flag)
761 {
762 	union dinode *dp;
763 	struct inode ip;
764 
765 	ginode(idesc->id_number, &ip);
766 	dp = ip.i_dp;
767 	if (flag == 1) {
768 		pwarn("%s %s", type,
769 		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
770 		prtinode(&ip);
771 		printf("\n");
772 	}
773 	if (preen || reply("CLEAR") == 1) {
774 		if (preen)
775 			printf(" (CLEARED)\n");
776 		n_files--;
777 		if (bkgrdflag == 0) {
778 			(void)ckinode(dp, idesc);
779 			inoinfo(idesc->id_number)->ino_state = USTATE;
780 			clearinode(dp);
781 			inodirty(&ip);
782 		} else {
783 			cmd.value = idesc->id_number;
784 			cmd.size = -DIP(dp, di_nlink);
785 			if (debug)
786 				printf("adjrefcnt ino %ld amt %lld\n",
787 				    (long)cmd.value, (long long)cmd.size);
788 			if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
789 			    &cmd, sizeof cmd) == -1)
790 				rwerror("ADJUST INODE", cmd.value);
791 		}
792 	}
793 	irelse(&ip);
794 }
795 
796 int
797 findname(struct inodesc *idesc)
798 {
799 	struct direct *dirp = idesc->id_dirp;
800 
801 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
802 		idesc->id_entryno++;
803 		return (KEEPON);
804 	}
805 	memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
806 	return (STOP|FOUND);
807 }
808 
809 int
810 findino(struct inodesc *idesc)
811 {
812 	struct direct *dirp = idesc->id_dirp;
813 
814 	if (dirp->d_ino == 0)
815 		return (KEEPON);
816 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
817 	    dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) {
818 		idesc->id_parent = dirp->d_ino;
819 		return (STOP|FOUND);
820 	}
821 	return (KEEPON);
822 }
823 
824 int
825 clearentry(struct inodesc *idesc)
826 {
827 	struct direct *dirp = idesc->id_dirp;
828 
829 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
830 		idesc->id_entryno++;
831 		return (KEEPON);
832 	}
833 	dirp->d_ino = 0;
834 	return (STOP|FOUND|ALTERED);
835 }
836 
837 void
838 prtinode(struct inode *ip)
839 {
840 	char *p;
841 	union dinode *dp;
842 	struct passwd *pw;
843 	time_t t;
844 
845 	dp = ip->i_dp;
846 	printf(" I=%lu ", (u_long)ip->i_number);
847 	if (ip->i_number < UFS_ROOTINO || ip->i_number > maxino)
848 		return;
849 	printf(" OWNER=");
850 	if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
851 		printf("%s ", pw->pw_name);
852 	else
853 		printf("%u ", (unsigned)DIP(dp, di_uid));
854 	printf("MODE=%o\n", DIP(dp, di_mode));
855 	if (preen)
856 		printf("%s: ", cdevname);
857 	printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
858 	t = DIP(dp, di_mtime);
859 	p = ctime(&t);
860 	printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
861 }
862 
863 void
864 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
865 {
866 
867 	pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
868 	printf("\n");
869 	switch (inoinfo(ino)->ino_state) {
870 
871 	case FSTATE:
872 	case FZLINK:
873 		inoinfo(ino)->ino_state = FCLEAR;
874 		return;
875 
876 	case DSTATE:
877 	case DZLINK:
878 		inoinfo(ino)->ino_state = DCLEAR;
879 		return;
880 
881 	case FCLEAR:
882 	case DCLEAR:
883 		return;
884 
885 	default:
886 		errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
887 		/* NOTREACHED */
888 	}
889 }
890 
891 /*
892  * allocate an unused inode
893  */
894 ino_t
895 allocino(ino_t request, int type)
896 {
897 	ino_t ino;
898 	struct inode ip;
899 	union dinode *dp;
900 	struct bufarea *cgbp;
901 	struct cg *cgp;
902 	int cg;
903 
904 	if (request == 0)
905 		request = UFS_ROOTINO;
906 	else if (inoinfo(request)->ino_state != USTATE)
907 		return (0);
908 	for (ino = request; ino < maxino; ino++)
909 		if (inoinfo(ino)->ino_state == USTATE)
910 			break;
911 	if (ino == maxino)
912 		return (0);
913 	cg = ino_to_cg(&sblock, ino);
914 	cgbp = cglookup(cg);
915 	cgp = cgbp->b_un.b_cg;
916 	if (!check_cgmagic(cg, cgbp, 0))
917 		return (0);
918 	setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
919 	cgp->cg_cs.cs_nifree--;
920 	switch (type & IFMT) {
921 	case IFDIR:
922 		inoinfo(ino)->ino_state = DSTATE;
923 		cgp->cg_cs.cs_ndir++;
924 		break;
925 	case IFREG:
926 	case IFLNK:
927 		inoinfo(ino)->ino_state = FSTATE;
928 		break;
929 	default:
930 		return (0);
931 	}
932 	cgdirty(cgbp);
933 	ginode(ino, &ip);
934 	dp = ip.i_dp;
935 	DIP_SET(dp, di_db[0], allocblk((long)1));
936 	if (DIP(dp, di_db[0]) == 0) {
937 		inoinfo(ino)->ino_state = USTATE;
938 		irelse(&ip);
939 		return (0);
940 	}
941 	DIP_SET(dp, di_mode, type);
942 	DIP_SET(dp, di_flags, 0);
943 	DIP_SET(dp, di_atime, time(NULL));
944 	DIP_SET(dp, di_ctime, DIP(dp, di_atime));
945 	DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
946 	DIP_SET(dp, di_mtimensec, 0);
947 	DIP_SET(dp, di_ctimensec, 0);
948 	DIP_SET(dp, di_atimensec, 0);
949 	DIP_SET(dp, di_size, sblock.fs_fsize);
950 	DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
951 	n_files++;
952 	inodirty(&ip);
953 	irelse(&ip);
954 	inoinfo(ino)->ino_type = IFTODT(type);
955 	return (ino);
956 }
957 
958 /*
959  * deallocate an inode
960  */
961 void
962 freeino(ino_t ino)
963 {
964 	struct inodesc idesc;
965 	union dinode *dp;
966 	struct inode ip;
967 
968 	memset(&idesc, 0, sizeof(struct inodesc));
969 	idesc.id_type = inoinfo(ino)->ino_idtype;
970 	idesc.id_func = freeblock;
971 	idesc.id_number = ino;
972 	ginode(ino, &ip);
973 	dp = ip.i_dp;
974 	(void)ckinode(dp, &idesc);
975 	clearinode(dp);
976 	inodirty(&ip);
977 	irelse(&ip);
978 	inoinfo(ino)->ino_state = USTATE;
979 	n_files--;
980 }
981