xref: /freebsd/sbin/fsck_ffs/inode.c (revision 1111a44301da39d7b7459c784230e1405e8980f8)
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 #include <sys/param.h>
33 #include <sys/stat.h>
34 #include <sys/stdint.h>
35 #include <sys/sysctl.h>
36 
37 #include <ufs/ufs/dinode.h>
38 #include <ufs/ufs/dir.h>
39 #include <ufs/ffs/fs.h>
40 
41 #include <err.h>
42 #include <pwd.h>
43 #include <string.h>
44 #include <time.h>
45 
46 #include "fsck.h"
47 
48 struct bufarea *icachebp;	/* inode cache buffer */
49 static time_t now;		/* current time of day */
50 
51 static int iblock(struct inodesc *, off_t isize, int type);
52 static ufs2_daddr_t indir_blkatoff(ufs2_daddr_t, ino_t, ufs_lbn_t, ufs_lbn_t,
53     struct bufarea **);
54 static int snapclean(struct inodesc *idesc);
55 static void chkcopyonwrite(struct fs *, ufs2_daddr_t,
56     ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t, long));
57 
58 int
ckinode(union dinode * dp,struct inodesc * idesc)59 ckinode(union dinode *dp, struct inodesc *idesc)
60 {
61 	off_t remsize, sizepb;
62 	int i, offset, ret;
63 	struct inode ip;
64 	union dinode dino;
65 	ufs2_daddr_t ndb;
66 	mode_t mode;
67 	char pathbuf[MAXPATHLEN + 1];
68 
69 	if (idesc->id_fix != IGNORE)
70 		idesc->id_fix = DONTKNOW;
71 	idesc->id_dp = dp;
72 	idesc->id_lbn = -1;
73 	idesc->id_lballoc = -1;
74 	idesc->id_level = 0;
75 	idesc->id_entryno = 0;
76 	idesc->id_filesize = DIP(dp, di_size);
77 	mode = DIP(dp, di_mode) & IFMT;
78 	if (mode == IFBLK || mode == IFCHR || (mode == IFLNK &&
79 	    DIP(dp, di_size) < (unsigned)sblock.fs_maxsymlinklen))
80 		return (KEEPON);
81 	if (sblock.fs_magic == FS_UFS1_MAGIC)
82 		dino.dp1 = dp->dp1;
83 	else
84 		dino.dp2 = dp->dp2;
85 	if (DIP(&dino, di_size) < 0) {
86 		pfatal("NEGATIVE INODE SIZE %jd\n", DIP(&dino, di_size));
87 		return (STOP);
88 	}
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 				return (STOP);
116 			}
117 			continue;
118 		}
119 		idesc->id_blkno = DIP(&dino, di_db[i]);
120 		if (idesc->id_type != DATA)
121 			ret = (*idesc->id_func)(idesc);
122 		else
123 			ret = dirscan(idesc);
124 		if (ret & STOP)
125 			return (ret);
126 	}
127 	idesc->id_numfrags = sblock.fs_frag;
128 	remsize = DIP(&dino, di_size) - sblock.fs_bsize * UFS_NDADDR;
129 	sizepb = sblock.fs_bsize;
130 	for (i = 0; i < UFS_NIADDR; i++) {
131 		sizepb *= NINDIR(&sblock);
132 		idesc->id_level = i + 1;
133 		if (DIP(&dino, di_ib[i])) {
134 			idesc->id_blkno = DIP(&dino, di_ib[i]);
135 			ret = iblock(idesc, remsize, BT_LEVEL1 + i);
136 			if (ret & STOP)
137 				return (ret);
138 		} else if (remsize > 0) {
139 			idesc->id_lbn += sizepb / sblock.fs_bsize;
140 			if (idesc->id_type == DATA) {
141 				/* An empty block in a directory XXX */
142 				getpathname(pathbuf, idesc->id_number,
143 						idesc->id_number);
144 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
145 					pathbuf);
146 				if (reply("ADJUST LENGTH") == 1) {
147 					ginode(idesc->id_number, &ip);
148 					DIP_SET(ip.i_dp, di_size,
149 					    DIP(ip.i_dp, di_size) - remsize);
150 					remsize = 0;
151 					printf(
152 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
153 					rerun = 1;
154 					inodirty(&ip);
155 					irelse(&ip);
156 					break;
157 				}
158 			}
159 		}
160 		remsize -= sizepb;
161 	}
162 	return (KEEPON);
163 }
164 
165 static int
iblock(struct inodesc * idesc,off_t isize,int type)166 iblock(struct inodesc *idesc, off_t isize, int type)
167 {
168 	struct inode ip;
169 	struct bufarea *bp;
170 	int i, n, (*func)(struct inodesc *), nif;
171 	off_t sizepb;
172 	char buf[BUFSIZ];
173 	char pathbuf[MAXPATHLEN + 1];
174 
175 	if (idesc->id_type != DATA) {
176 		func = idesc->id_func;
177 		if (((n = (*func)(idesc)) & KEEPON) == 0)
178 			return (n);
179 	} else
180 		func = dirscan;
181 	bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
182 	if (bp->b_errs != 0) {
183 		brelse(bp);
184 		return (SKIP);
185 	}
186 	idesc->id_bp = bp;
187 	idesc->id_level--;
188 	for (sizepb = sblock.fs_bsize, i = 0; i < idesc->id_level; i++)
189 		sizepb *= NINDIR(&sblock);
190 	if (howmany(isize, sizepb) > NINDIR(&sblock))
191 		nif = NINDIR(&sblock);
192 	else
193 		nif = howmany(isize, sizepb);
194 	if (idesc->id_func == pass1check && nif < NINDIR(&sblock)) {
195 		for (i = nif; i < NINDIR(&sblock); i++) {
196 			if (IBLK(bp, i) == 0)
197 				continue;
198 			(void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
199 			    (u_long)idesc->id_number);
200 			if (preen) {
201 				pfatal("%s", buf);
202 			} else if (dofix(idesc, buf)) {
203 				IBLK_SET(bp, i, 0);
204 				dirty(bp);
205 			}
206 		}
207 		flush(fswritefd, bp);
208 	}
209 	for (i = 0; i < nif; i++) {
210 		if (IBLK(bp, i)) {
211 			idesc->id_blkno = IBLK(bp, i);
212 			bp->b_index = i;
213 			if (idesc->id_level == 0) {
214 				idesc->id_lbn++;
215 				n = (*func)(idesc);
216 			} else {
217 				n = iblock(idesc, isize, type - 1);
218 				idesc->id_level++;
219 			}
220 			if (n & STOP) {
221 				brelse(bp);
222 				return (n);
223 			}
224 		} else {
225 			idesc->id_lbn += sizepb / sblock.fs_bsize;
226 			if (idesc->id_type == DATA && isize > 0) {
227 				/* An empty block in a directory XXX */
228 				getpathname(pathbuf, idesc->id_number,
229 						idesc->id_number);
230 				pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
231 					pathbuf);
232 				if (reply("ADJUST LENGTH") == 1) {
233 					ginode(idesc->id_number, &ip);
234 					DIP_SET(ip.i_dp, di_size,
235 					    DIP(ip.i_dp, di_size) - isize);
236 					isize = 0;
237 					printf(
238 					    "YOU MUST RERUN FSCK AFTERWARDS\n");
239 					rerun = 1;
240 					inodirty(&ip);
241 					brelse(bp);
242 					return(STOP);
243 				}
244 			}
245 		}
246 		isize -= sizepb;
247 	}
248 	brelse(bp);
249 	return (KEEPON);
250 }
251 
252 /*
253  * Finds the disk block address at the specified lbn within the inode
254  * specified by dp.  This follows the whole tree and honors di_size and
255  * di_extsize so it is a true test of reachability.  The lbn may be
256  * negative if an extattr or indirect block is requested.
257  */
258 ufs2_daddr_t
ino_blkatoff(union dinode * dp,ino_t ino,ufs_lbn_t lbn,int * frags,struct bufarea ** bpp)259 ino_blkatoff(union dinode *dp, ino_t ino, ufs_lbn_t lbn, int *frags,
260     struct bufarea **bpp)
261 {
262 	ufs_lbn_t tmpval;
263 	ufs_lbn_t cur;
264 	ufs_lbn_t next;
265 	int i;
266 
267 	*frags = 0;
268 	if (bpp != NULL)
269 		*bpp = NULL;
270 	/*
271 	 * Handle extattr blocks first.
272 	 */
273 	if (lbn < 0 && lbn >= -UFS_NXADDR) {
274 		lbn = -1 - lbn;
275 		if (lbn > lblkno(&sblock, dp->dp2.di_extsize - 1))
276 			return (0);
277 		*frags = numfrags(&sblock,
278 		    sblksize(&sblock, dp->dp2.di_extsize, lbn));
279 		return (dp->dp2.di_extb[lbn]);
280 	}
281 	/*
282 	 * Now direct and indirect.
283 	 */
284 	if (DIP(dp, di_mode) == IFLNK &&
285 	    DIP(dp, di_size) < sblock.fs_maxsymlinklen)
286 		return (0);
287 	if (lbn >= 0 && lbn < UFS_NDADDR) {
288 		*frags = numfrags(&sblock,
289 		    sblksize(&sblock, DIP(dp, di_size), lbn));
290 		return (DIP(dp, di_db[lbn]));
291 	}
292 	*frags = sblock.fs_frag;
293 
294 	for (i = 0, tmpval = NINDIR(&sblock), cur = UFS_NDADDR; i < UFS_NIADDR;
295 	    i++, tmpval *= NINDIR(&sblock), cur = next) {
296 		next = cur + tmpval;
297 		if (lbn == -cur - i)
298 			return (DIP(dp, di_ib[i]));
299 		/*
300 		 * Determine whether the lbn in question is within this tree.
301 		 */
302 		if (lbn < 0 && -lbn >= next)
303 			continue;
304 		if (lbn > 0 && lbn >= next)
305 			continue;
306 		if (DIP(dp, di_ib[i]) == 0)
307 			return (0);
308 		return (indir_blkatoff(DIP(dp, di_ib[i]), ino, -cur - i, lbn,
309 		    bpp));
310 	}
311 	pfatal("lbn %jd not in ino %ju\n", lbn, (uintmax_t)ino);
312 	return (0);
313 }
314 
315 /*
316  * Fetch an indirect block to find the block at a given lbn.  The lbn
317  * may be negative to fetch a specific indirect block pointer or positive
318  * to fetch a specific block.
319  */
320 static ufs2_daddr_t
indir_blkatoff(ufs2_daddr_t blk,ino_t ino,ufs_lbn_t cur,ufs_lbn_t lbn,struct bufarea ** bpp)321 indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn,
322     struct bufarea **bpp)
323 {
324 	struct bufarea *bp;
325 	ufs_lbn_t lbnadd;
326 	ufs_lbn_t base;
327 	int i, level;
328 
329 	level = lbn_level(cur);
330 	if (level == -1)
331 		pfatal("Invalid indir lbn %jd in ino %ju\n",
332 		    lbn, (uintmax_t)ino);
333 	if (level == 0 && lbn < 0)
334 		pfatal("Invalid lbn %jd in ino %ju\n",
335 		    lbn, (uintmax_t)ino);
336 	lbnadd = 1;
337 	base = -(cur + level);
338 	for (i = level; i > 0; i--)
339 		lbnadd *= NINDIR(&sblock);
340 	if (lbn > 0)
341 		i = (lbn - base) / lbnadd;
342 	else
343 		i = (-lbn - base) / lbnadd;
344 	if (i < 0 || i >= NINDIR(&sblock)) {
345 		pfatal("Invalid indirect index %d produced by lbn %jd "
346 		    "in ino %ju\n", i, lbn, (uintmax_t)ino);
347 		return (0);
348 	}
349 	if (level == 0)
350 		cur = base + (i * lbnadd);
351 	else
352 		cur = -(base + (i * lbnadd)) - (level - 1);
353 	bp = getdatablk(blk, sblock.fs_bsize, BT_LEVEL1 + level);
354 	if (bp->b_errs != 0)
355 		return (0);
356 	blk = IBLK(bp, i);
357 	bp->b_index = i;
358 	if (cur == lbn || blk == 0) {
359 		if (bpp != NULL)
360 			*bpp = bp;
361 		else
362 			brelse(bp);
363 		return (blk);
364 	}
365 	brelse(bp);
366 	if (level == 0)
367 		pfatal("Invalid lbn %jd at level 0 for ino %ju\n", lbn,
368 		    (uintmax_t)ino);
369 	return (indir_blkatoff(blk, ino, cur, lbn, bpp));
370 }
371 
372 /*
373  * Check that a block in a legal block number.
374  * Return 0 if in range, 1 if out of range.
375  */
376 int
chkrange(ufs2_daddr_t blk,int cnt)377 chkrange(ufs2_daddr_t blk, int cnt)
378 {
379 	int c;
380 
381 	if (cnt <= 0 || blk <= 0 || blk >= maxfsblock ||
382 	    cnt > maxfsblock - blk) {
383 		if (debug)
384 			printf("out of range: blk %ld, offset %i, size %d\n",
385 			    (long)blk, (int)fragnum(&sblock, blk), cnt);
386 		return (1);
387 	}
388 	if (cnt > sblock.fs_frag ||
389 	    fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
390 		if (debug)
391 			printf("bad size: blk %ld, offset %i, size %d\n",
392 			    (long)blk, (int)fragnum(&sblock, blk), cnt);
393 		return (1);
394 	}
395 	c = dtog(&sblock, blk);
396 	if (blk < cgdmin(&sblock, c)) {
397 		if ((blk + cnt) > cgsblock(&sblock, c)) {
398 			if (debug) {
399 				printf("blk %ld < cgdmin %ld;",
400 				    (long)blk, (long)cgdmin(&sblock, c));
401 				printf(" blk + cnt %ld > cgsbase %ld\n",
402 				    (long)(blk + cnt),
403 				    (long)cgsblock(&sblock, c));
404 			}
405 			return (1);
406 		}
407 	} else {
408 		if ((blk + cnt) > cgbase(&sblock, c+1)) {
409 			if (debug)  {
410 				printf("blk %ld >= cgdmin %ld;",
411 				    (long)blk, (long)cgdmin(&sblock, c));
412 				printf(" blk + cnt %ld > sblock.fs_fpg %ld\n",
413 				    (long)(blk + cnt), (long)sblock.fs_fpg);
414 			}
415 			return (1);
416 		}
417 	}
418 	return (0);
419 }
420 
421 /*
422  * General purpose interface for reading inodes.
423  *
424  * firstinum and lastinum track contents of getnextino() cache (below).
425  */
426 static ino_t firstinum, lastinum;
427 static struct bufarea inobuf;
428 
429 void
ginode(ino_t inumber,struct inode * ip)430 ginode(ino_t inumber, struct inode *ip)
431 {
432 	ufs2_daddr_t iblk;
433 	union dinodep dpp;
434 	struct ufs2_dinode *dp;
435 
436 	if (inumber < UFS_ROOTINO || inumber >= maxino)
437 		errx(EEXIT, "bad inode number %ju to ginode",
438 		    (uintmax_t)inumber);
439 	ip->i_number = inumber;
440 	if (inumber >= firstinum && inumber < lastinum) {
441 		/* contents in getnextino() cache */
442 		ip->i_bp = &inobuf;
443 		inobuf.b_refcnt++;
444 		inobuf.b_index = firstinum;
445 	} else if (icachebp != NULL &&
446 	    inumber >= icachebp->b_index &&
447 	    inumber < icachebp->b_index + INOPB(&sblock)) {
448 		/* take an additional reference for the returned inode */
449 		icachebp->b_refcnt++;
450 		ip->i_bp = icachebp;
451 	} else {
452 		iblk = ino_to_fsba(&sblock, inumber);
453 		/* release our cache-hold reference on old icachebp */
454 		if (icachebp != NULL)
455 			brelse(icachebp);
456 		icachebp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
457 		if (icachebp->b_errs != 0) {
458 			icachebp = NULL;
459 			ip->i_bp = NULL;
460 			ip->i_dp = &zino;
461 			return;
462 		}
463 		/* take a cache-hold reference on new icachebp */
464 		icachebp->b_refcnt++;
465 		icachebp->b_index = rounddown(inumber, INOPB(&sblock));
466 		ip->i_bp = icachebp;
467 	}
468 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
469 		ip->i_dp = (union dinode *)
470 		    &ip->i_bp->b_un.b_dinode1[inumber - ip->i_bp->b_index];
471 		dpp.dp1 = (struct ufs1_dinode *)ip->i_dp;
472 		if (ffs_oldfscompat_inode_read(&sblock, dpp, now))
473 			inodirty(ip);
474 		return;
475 	}
476 	ip->i_dp = (union dinode *)
477 	    &ip->i_bp->b_un.b_dinode2[inumber - ip->i_bp->b_index];
478 	dpp.dp2 = dp = (struct ufs2_dinode *)ip->i_dp;
479 	/* Do not check hash of inodes being created */
480 	if (dp->di_mode != 0 && ffs_verify_dinode_ckhash(&sblock, dp)) {
481 		pwarn("INODE CHECK-HASH FAILED");
482 		prtinode(ip);
483 		if (preen || reply("FIX") != 0) {
484 			if (preen)
485 				printf(" (FIXED)\n");
486 			ffs_update_dinode_ckhash(&sblock, dp);
487 			inodirty(ip);
488 		}
489 	}
490 	if (ffs_oldfscompat_inode_read(&sblock, dpp, now))
491 		inodirty(ip);
492 }
493 
494 /*
495  * Release a held inode.
496  */
497 void
irelse(struct inode * ip)498 irelse(struct inode *ip)
499 {
500 
501 	/* Check for failed inode read */
502 	if (ip->i_bp == NULL)
503 		return;
504 	if (debug && sblock.fs_magic == FS_UFS2_MAGIC &&
505 	    ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)ip->i_dp)) {
506 		pwarn("irelse: releasing inode with bad check-hash");
507 		prtinode(ip);
508 	}
509 	if (ip->i_bp->b_refcnt <= 0)
510 		pfatal("irelse: releasing unreferenced ino %ju\n",
511 		    (uintmax_t) ip->i_number);
512 	brelse(ip->i_bp);
513 }
514 
515 /*
516  * Special purpose version of ginode used to optimize first pass
517  * over all the inodes in numerical order.
518  */
519 static ino_t nextinum, lastvalidinum;
520 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
521 
522 union dinode *
getnextinode(ino_t inumber,int rebuiltcg)523 getnextinode(ino_t inumber, int rebuiltcg)
524 {
525 	int j;
526 	long size;
527 	mode_t mode;
528 	ufs2_daddr_t ndb, blk;
529 	union dinode *dp;
530 	union dinodep dpp;
531 	struct inode ip;
532 	static caddr_t nextinop;
533 
534 	if (inumber != nextinum++ || inumber > lastvalidinum)
535 		errx(EEXIT, "bad inode number %ju to nextinode",
536 		    (uintmax_t)inumber);
537 	if (inumber >= lastinum) {
538 		readcount++;
539 		firstinum = lastinum;
540 		blk = ino_to_fsba(&sblock, lastinum);
541 		if (readcount % readpercg == 0) {
542 			size = partialsize;
543 			lastinum += partialcnt;
544 		} else {
545 			size = inobufsize;
546 			lastinum += fullcnt;
547 		}
548 		/*
549 		 * Flush old contents in case they have been updated.
550 		 * If getblk encounters an error, it will already have zeroed
551 		 * out the buffer, so we do not need to do so here.
552 		 */
553 		if (inobuf.b_refcnt != 0)
554 			pfatal("Non-zero getnextinode() ref count %d\n",
555 			    inobuf.b_refcnt);
556 		flush(fswritefd, &inobuf);
557 		getblk(&inobuf, blk, size);
558 		nextinop = inobuf.b_un.b_buf;
559 	}
560 	dp = (union dinode *)nextinop;
561 	if (sblock.fs_magic == FS_UFS1_MAGIC) {
562 		nextinop += sizeof(struct ufs1_dinode);
563 		dpp.dp1 = (struct ufs1_dinode *)dp;
564 	} else {
565 		nextinop += sizeof(struct ufs2_dinode);
566 		dpp.dp2 = (struct ufs2_dinode *)dp;
567 	}
568 	if ((ckhashadd & CK_INODE) != 0) {
569 		ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
570 		dirty(&inobuf);
571 	}
572 	if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
573 		pwarn("INODE CHECK-HASH FAILED");
574 		ip.i_bp = NULL;
575 		ip.i_dp = dp;
576 		ip.i_number = inumber;
577 		prtinode(&ip);
578 		if (preen || reply("FIX") != 0) {
579 			if (preen)
580 				printf(" (FIXED)\n");
581 			ffs_update_dinode_ckhash(&sblock,
582 			    (struct ufs2_dinode *)dp);
583 			dirty(&inobuf);
584 		}
585 	}
586 	if (ffs_oldfscompat_inode_read(&sblock, dpp, now))
587 		dirty(&inobuf);
588 	if (rebuiltcg && (char *)dp == inobuf.b_un.b_buf) {
589 		/*
590 		 * Try to determine if we have reached the end of the
591 		 * allocated inodes.
592 		 */
593 		mode = DIP(dp, di_mode) & IFMT;
594 		if (mode == 0) {
595 			if (memcmp(dp->dp2.di_db, zino.dp2.di_db,
596 				UFS_NDADDR * sizeof(ufs2_daddr_t)) ||
597 			      memcmp(dp->dp2.di_ib, zino.dp2.di_ib,
598 				UFS_NIADDR * sizeof(ufs2_daddr_t)) ||
599 			      dp->dp2.di_mode || dp->dp2.di_size)
600 				return (NULL);
601 			return (dp);
602 		}
603 		if (!ftypeok(dp))
604 			return (NULL);
605 		ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
606 		if (ndb < 0)
607 			return (NULL);
608 		if (mode == IFBLK || mode == IFCHR)
609 			ndb++;
610 		if (mode == IFLNK) {
611 			/*
612 			 * Fake ndb value so direct/indirect block checks below
613 			 * will detect any garbage after symlink string.
614 			 */
615 			if (DIP(dp, di_size) < (off_t)sblock.fs_maxsymlinklen) {
616 				ndb = howmany(DIP(dp, di_size),
617 				    sizeof(ufs2_daddr_t));
618 				if (ndb > UFS_NDADDR) {
619 					j = ndb - UFS_NDADDR;
620 					for (ndb = 1; j > 1; j--)
621 						ndb *= NINDIR(&sblock);
622 					ndb += UFS_NDADDR;
623 				}
624 			}
625 		}
626 		for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++)
627 			if (DIP(dp, di_db[j]) != 0)
628 				return (NULL);
629 		for (j = 0, ndb -= UFS_NDADDR; ndb > 0; j++)
630 			ndb /= NINDIR(&sblock);
631 		for (; j < UFS_NIADDR; j++)
632 			if (DIP(dp, di_ib[j]) != 0)
633 				return (NULL);
634 	}
635 	return (dp);
636 }
637 
638 void
setinodebuf(int cg,ino_t inosused)639 setinodebuf(int cg, ino_t inosused)
640 {
641 	struct timespec time;
642 	ino_t inum;
643 
644 	/*
645 	 * Get the current value of the present time.
646 	 * This will happen before each cylinder group is scanned.
647 	 * If for some reason getting the time fails, we will use
648 	 * the last time that the superblock was updated.
649 	 */
650 	if (clock_gettime(CLOCK_REALTIME_FAST, &time) == 0)
651 		now = time.tv_sec;
652 	else
653 		now = sblock.fs_time;
654 	inum = cg * sblock.fs_ipg;
655 	lastvalidinum = inum + inosused - 1;
656 	nextinum = inum;
657 	lastinum = inum;
658 	readcount = 0;
659 	/* Flush old contents in case they have been updated */
660 	flush(fswritefd, &inobuf);
661 	inobuf.b_bno = 0;
662 	if (inobuf.b_un.b_buf == NULL) {
663 		inobufsize = blkroundup(&sblock,
664 		    MAX(INOBUFSIZE, sblock.fs_bsize));
665 		initbarea(&inobuf, BT_INODES);
666 		if ((inobuf.b_un.b_buf = Balloc((unsigned)inobufsize)) == NULL)
667 			errx(EEXIT, "cannot allocate space for inode buffer");
668 	}
669 	fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
670 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
671 	readpercg = inosused / fullcnt;
672 	partialcnt = inosused % fullcnt;
673 	partialsize = fragroundup(&sblock,
674 	    partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
675 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)));
676 	if (partialcnt != 0) {
677 		readpercg++;
678 	} else {
679 		partialcnt = fullcnt;
680 		partialsize = inobufsize;
681 	}
682 }
683 
684 int
freeblock(struct inodesc * idesc)685 freeblock(struct inodesc *idesc)
686 {
687 	struct dups *dlp;
688 	struct bufarea *cgbp;
689 	struct cg *cgp;
690 	ufs2_daddr_t blkno;
691 	long size, nfrags;
692 
693 	blkno = idesc->id_blkno;
694 	if (idesc->id_type == SNAP) {
695 		pfatal("clearing a snapshot dinode\n");
696 		return (STOP);
697 	}
698 	size = lfragtosize(&sblock, idesc->id_numfrags);
699 	if (snapblkfree(&sblock, blkno, size, idesc->id_number,
700 	    std_checkblkavail))
701 		return (KEEPON);
702 	for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
703 		if (chkrange(blkno, 1)) {
704 			return (SKIP);
705 		} else if (testbmap(blkno)) {
706 			for (dlp = duplist; dlp; dlp = dlp->next) {
707 				if (dlp->dup != blkno)
708 					continue;
709 				dlp->dup = duplist->dup;
710 				dlp = duplist;
711 				duplist = duplist->next;
712 				free((char *)dlp);
713 				break;
714 			}
715 			if (dlp == NULL) {
716 				clrbmap(blkno);
717 				n_blks--;
718 			}
719 		}
720 	}
721 	/*
722 	 * If all successfully returned, account for them.
723 	 */
724 	if (nfrags == 0) {
725 		cgbp = cglookup(dtog(&sblock, idesc->id_blkno));
726 		cgp = cgbp->b_un.b_cg;
727 		if (idesc->id_numfrags == sblock.fs_frag)
728 			cgp->cg_cs.cs_nbfree++;
729 		else
730 			cgp->cg_cs.cs_nffree += idesc->id_numfrags;
731 		cgdirty(cgbp);
732 	}
733 	return (KEEPON);
734 }
735 
736 /*
737  * Prepare a snapshot file for being removed.
738  */
739 void
snapremove(ino_t inum)740 snapremove(ino_t inum)
741 {
742 	struct inodesc idesc;
743 	struct inode ip;
744 	int i;
745 
746 	for (i = 0; i < snapcnt; i++)
747 		if (snaplist[i].i_number == inum)
748 			break;
749 	if (i == snapcnt)
750 		ginode(inum, &ip);
751 	else
752 		ip = snaplist[i];
753 	if ((DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) == 0) {
754 		printf("snapremove: inode %jd is not a snapshot\n",
755 		    (intmax_t)inum);
756 		if (i == snapcnt)
757 			irelse(&ip);
758 		return;
759 	}
760 	if (debug)
761 		printf("snapremove: remove %sactive snapshot %jd\n",
762 		    i == snapcnt ? "in" : "", (intmax_t)inum);
763 	/*
764 	 * If on active snapshot list, remove it.
765 	 */
766 	if (i < snapcnt) {
767 		for (i++; i < FSMAXSNAP; i++) {
768 			if (sblock.fs_snapinum[i] == 0)
769 				break;
770 			snaplist[i - 1] = snaplist[i];
771 			sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i];
772 		}
773 		sblock.fs_snapinum[i - 1] = 0;
774 		bzero(&snaplist[i - 1], sizeof(struct inode));
775 		snapcnt--;
776 	}
777 	memset(&idesc, 0, sizeof(struct inodesc));
778 	idesc.id_type = SNAP;
779 	idesc.id_func = snapclean;
780 	idesc.id_number = inum;
781 	(void)ckinode(ip.i_dp, &idesc);
782 	DIP_SET(ip.i_dp, di_flags, DIP(ip.i_dp, di_flags) & ~SF_SNAPSHOT);
783 	inodirty(&ip);
784 	irelse(&ip);
785 }
786 
787 static int
snapclean(struct inodesc * idesc)788 snapclean(struct inodesc *idesc)
789 {
790 	ufs2_daddr_t blkno;
791 	struct bufarea *bp;
792 	union dinode *dp;
793 
794 	blkno = idesc->id_blkno;
795 	if (blkno == 0)
796 		return (KEEPON);
797 
798 	dp = idesc->id_dp;
799 	if (blkno == BLK_NOCOPY || blkno == BLK_SNAP) {
800 		if (idesc->id_lbn < UFS_NDADDR) {
801 			DIP_SET(dp, di_db[idesc->id_lbn], 0);
802 		} else {
803 			bp = idesc->id_bp;
804 			IBLK_SET(bp, bp->b_index, 0);
805 			dirty(bp);
806 		}
807 	}
808 	return (KEEPON);
809 }
810 
811 /*
812  * Notification that a block is being freed. Return zero if the free
813  * should be allowed to proceed. Return non-zero if the snapshot file
814  * wants to claim the block. The block will be claimed if it is an
815  * uncopied part of one of the snapshots. It will be freed if it is
816  * either a BLK_NOCOPY or has already been copied in all of the snapshots.
817  * If a fragment is being freed, then all snapshots that care about
818  * it must make a copy since a snapshot file can only claim full sized
819  * blocks. Note that if more than one snapshot file maps the block,
820  * we can pick one at random to claim it. Since none of the snapshots
821  * can change, we are assurred that they will all see the same unmodified
822  * image. When deleting a snapshot file (see ino_trunc above), we
823  * must push any of these claimed blocks to one of the other snapshots
824  * that maps it. These claimed blocks are easily identified as they will
825  * have a block number equal to their logical block number within the
826  * snapshot. A copied block can never have this property because they
827  * must always have been allocated from a BLK_NOCOPY location.
828  */
829 int
snapblkfree(struct fs * fs,ufs2_daddr_t bno,long size,ino_t inum,ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t blkno,long frags))830 snapblkfree(struct fs *fs, ufs2_daddr_t bno, long size, ino_t inum,
831 	ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
832 {
833 	union dinode *dp;
834 	struct inode ip;
835 	struct bufarea *snapbp;
836 	ufs_lbn_t lbn;
837 	ufs2_daddr_t blkno, relblkno;
838 	int i, frags, claimedblk, copydone;
839 
840 	/* If no snapshots, nothing to do */
841 	if (snapcnt == 0)
842 		return (0);
843 	if (debug)
844 		printf("snapblkfree: in ino %jd free blkno %jd, size %jd\n",
845 		    (intmax_t)inum, (intmax_t)bno, (intmax_t)size);
846 	relblkno = blknum(fs, bno);
847 	lbn = fragstoblks(fs, relblkno);
848 	/* Direct blocks are always pre-copied */
849 	if (lbn < UFS_NDADDR)
850 		return (0);
851 	copydone = 0;
852 	claimedblk = 0;
853 	for (i = 0; i < snapcnt; i++) {
854 		/*
855 		 * Lookup block being freed.
856 		 */
857 		ip = snaplist[i];
858 		dp = ip.i_dp;
859 		blkno = ino_blkatoff(dp, inum != 0 ? inum : ip.i_number,
860 		    lbn, &frags, &snapbp);
861 		/*
862 		 * Check to see if block needs to be copied.
863 		 */
864 		if (blkno == 0) {
865 			/*
866 			 * A block that we map is being freed. If it has not
867 			 * been claimed yet, we will claim or copy it (below).
868 			 */
869 			claimedblk = 1;
870 		} else if (blkno == BLK_SNAP) {
871 			/*
872 			 * No previous snapshot claimed the block,
873 			 * so it will be freed and become a BLK_NOCOPY
874 			 * (don't care) for us.
875 			 */
876 			if (claimedblk)
877 				pfatal("snapblkfree: inconsistent block type");
878 			IBLK_SET(snapbp, snapbp->b_index, BLK_NOCOPY);
879 			dirty(snapbp);
880 			brelse(snapbp);
881 			continue;
882 		} else /* BLK_NOCOPY or default */ {
883 			/*
884 			 * If the snapshot has already copied the block
885 			 * (default), or does not care about the block,
886 			 * it is not needed.
887 			 */
888 			brelse(snapbp);
889 			continue;
890 		}
891 		/*
892 		 * If this is a full size block, we will just grab it
893 		 * and assign it to the snapshot inode. Otherwise we
894 		 * will proceed to copy it. See explanation for this
895 		 * routine as to why only a single snapshot needs to
896 		 * claim this block.
897 		 */
898 		if (size == fs->fs_bsize) {
899 			if (debug)
900 				printf("Grabonremove snapshot %ju lbn %jd "
901 				    "from inum %ju\n", (intmax_t)ip.i_number,
902 				    (intmax_t)lbn, (uintmax_t)inum);
903 			IBLK_SET(snapbp, snapbp->b_index, relblkno);
904 			dirty(snapbp);
905 			brelse(snapbp);
906 			DIP_SET(dp, di_blocks,
907 			    DIP(dp, di_blocks) + btodb(size));
908 			inodirty(&ip);
909 			return (1);
910 		}
911 
912 		/* First time through, read the contents of the old block. */
913 		if (copydone == 0) {
914 			copydone = 1;
915 			if (blread(fsreadfd, copybuf, fsbtodb(fs, relblkno),
916 			    fs->fs_bsize) != 0) {
917 				pfatal("Could not read snapshot %ju block "
918 				    "%jd\n", (intmax_t)ip.i_number,
919 				    (intmax_t)relblkno);
920 				continue;
921 			}
922 		}
923 		/*
924 		 * This allocation will never require any additional
925 		 * allocations for the snapshot inode.
926 		 */
927 		blkno = allocblk(dtog(fs, relblkno), fs->fs_frag,
928 		    checkblkavail);
929 		if (blkno == 0) {
930 			pfatal("Could not allocate block for snapshot %ju\n",
931 			    (intmax_t)ip.i_number);
932 			continue;
933 		}
934 		if (debug)
935 			printf("Copyonremove: snapino %jd lbn %jd for inum %ju "
936 			    "size %ld new blkno %jd\n", (intmax_t)ip.i_number,
937 			    (intmax_t)lbn, (uintmax_t)inum, size,
938 			    (intmax_t)blkno);
939 		blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize);
940 		IBLK_SET(snapbp, snapbp->b_index, blkno);
941 		dirty(snapbp);
942 		brelse(snapbp);
943 		DIP_SET(dp, di_blocks,
944 		    DIP(dp, di_blocks) + btodb(fs->fs_bsize));
945 		inodirty(&ip);
946 	}
947 	return (0);
948 }
949 
950 /*
951  * Notification that a block is being written. Return if the block
952  * is part of a snapshot as snapshots never track other snapshots.
953  * The block will be copied in all of the snapshots that are tracking
954  * it and have not yet copied it. Some buffers may hold more than one
955  * block. Here we need to check each block in the buffer.
956  */
957 void
copyonwrite(struct fs * fs,struct bufarea * bp,ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t blkno,long frags))958 copyonwrite(struct fs *fs, struct bufarea *bp,
959 	ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
960 {
961 	ufs2_daddr_t copyblkno;
962 	long i, numblks;
963 
964 	/* If no snapshots, nothing to do. */
965 	if (snapcnt == 0)
966 		return;
967 	numblks = blkroundup(fs, bp->b_size) / fs->fs_bsize;
968 	if (debug)
969 		prtbuf(bp, "copyonwrite: checking %jd block%s in buffer",
970 		    (intmax_t)numblks, numblks > 1 ? "s" : "");
971 	copyblkno = blknum(fs, dbtofsb(fs, bp->b_bno));
972 	for (i = 0; i < numblks; i++) {
973 		chkcopyonwrite(fs, copyblkno, checkblkavail);
974 		copyblkno += fs->fs_frag;
975 	}
976 }
977 
978 static void
chkcopyonwrite(struct fs * fs,ufs2_daddr_t copyblkno,ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t blkno,long frags))979 chkcopyonwrite(struct fs *fs, ufs2_daddr_t copyblkno,
980 	ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
981 {
982 	struct inode ip;
983 	union dinode *dp;
984 	struct bufarea *snapbp;
985 	ufs2_daddr_t blkno;
986 	int i, frags, copydone;
987 	ufs_lbn_t lbn;
988 
989 	lbn = fragstoblks(fs, copyblkno);
990 	/* Direct blocks are always pre-copied */
991 	if (lbn < UFS_NDADDR)
992 		return;
993 	copydone = 0;
994 	for (i = 0; i < snapcnt; i++) {
995 		/*
996 		 * Lookup block being freed.
997 		 */
998 		ip = snaplist[i];
999 		dp = ip.i_dp;
1000 		blkno = ino_blkatoff(dp, ip.i_number, lbn, &frags, &snapbp);
1001 		/*
1002 		 * Check to see if block needs to be copied.
1003 		 */
1004 		if (blkno != 0) {
1005 			/*
1006 			 * A block that we have already copied or don't track.
1007 			 */
1008 			brelse(snapbp);
1009 			continue;
1010 		}
1011 		/* First time through, read the contents of the old block. */
1012 		if (copydone == 0) {
1013 			copydone = 1;
1014 			if (blread(fsreadfd, copybuf, fsbtodb(fs, copyblkno),
1015 			    fs->fs_bsize) != 0) {
1016 				pfatal("Could not read snapshot %ju block "
1017 				    "%jd\n", (intmax_t)ip.i_number,
1018 				    (intmax_t)copyblkno);
1019 				continue;
1020 			}
1021 		}
1022 		/*
1023 		 * This allocation will never require any additional
1024 		 * allocations for the snapshot inode.
1025 		 */
1026 		if ((blkno = allocblk(dtog(fs, copyblkno), fs->fs_frag,
1027 		    checkblkavail)) == 0) {
1028 			pfatal("Could not allocate block for snapshot %ju\n",
1029 			    (intmax_t)ip.i_number);
1030 			continue;
1031 		}
1032 		if (debug)
1033 			prtbuf(snapbp, "Copyonwrite: snapino %jd lbn %jd using "
1034 			    "blkno %ju setting in buffer",
1035 			    (intmax_t)ip.i_number, (intmax_t)lbn,
1036 			    (intmax_t)blkno);
1037 		blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize);
1038 		IBLK_SET(snapbp, snapbp->b_index, blkno);
1039 		dirty(snapbp);
1040 		brelse(snapbp);
1041 		DIP_SET(dp, di_blocks,
1042 		    DIP(dp, di_blocks) + btodb(fs->fs_bsize));
1043 		inodirty(&ip);
1044 	}
1045 	return;
1046 }
1047 
1048 /*
1049  * Traverse an inode and check that its block count is correct
1050  * fixing it if necessary.
1051  */
1052 void
check_blkcnt(struct inode * ip)1053 check_blkcnt(struct inode *ip)
1054 {
1055 	struct inodesc idesc;
1056 	union dinode *dp;
1057 	ufs2_daddr_t ndb;
1058 	int j, ret, offset;
1059 
1060 	dp = ip->i_dp;
1061 	memset(&idesc, 0, sizeof(struct inodesc));
1062 	idesc.id_func = pass1check;
1063 	idesc.id_number = ip->i_number;
1064 	idesc.id_type = (DIP(dp, di_flags) & SF_SNAPSHOT) == 0 ? ADDR : SNAP;
1065 	(void)ckinode(dp, &idesc);
1066 	if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
1067 		ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
1068 		for (j = 0; j < UFS_NXADDR; j++) {
1069 			if (--ndb == 0 &&
1070 			    (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
1071 				idesc.id_numfrags = numfrags(&sblock,
1072 				    fragroundup(&sblock, offset));
1073 			else
1074 				idesc.id_numfrags = sblock.fs_frag;
1075 			if (dp->dp2.di_extb[j] == 0)
1076 				continue;
1077 			idesc.id_blkno = dp->dp2.di_extb[j];
1078 			ret = (*idesc.id_func)(&idesc);
1079 			if (ret & STOP)
1080 				break;
1081 		}
1082 	}
1083 	idesc.id_entryno *= btodb(sblock.fs_fsize);
1084 	if (DIP(dp, di_blocks) != idesc.id_entryno) {
1085 		if (!(sujrecovery && preen)) {
1086 			pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
1087 			    (u_long)idesc.id_number,
1088 			    (uintmax_t)DIP(dp, di_blocks),
1089 			    (uintmax_t)idesc.id_entryno);
1090 			if (preen)
1091 				printf(" (CORRECTED)\n");
1092 			else if (reply("CORRECT") == 0)
1093 				return;
1094 		}
1095 		if (bkgrdflag == 0) {
1096 			DIP_SET(dp, di_blocks, idesc.id_entryno);
1097 			inodirty(ip);
1098 		} else {
1099 			cmd.value = idesc.id_number;
1100 			cmd.size = idesc.id_entryno - DIP(dp, di_blocks);
1101 			if (debug)
1102 				printf("adjblkcnt ino %ju amount %lld\n",
1103 				    (uintmax_t)cmd.value, (long long)cmd.size);
1104 			if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
1105 			    &cmd, sizeof cmd) == -1)
1106 				rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
1107 		}
1108 	}
1109 }
1110 
1111 void
freeinodebuf(void)1112 freeinodebuf(void)
1113 {
1114 	struct bufarea *bp;
1115 	int i;
1116 
1117 	/*
1118 	 * Flush old contents in case they have been updated.
1119 	 */
1120 	flush(fswritefd, &inobuf);
1121 	if (inobuf.b_un.b_buf != NULL)
1122 		free((char *)inobuf.b_un.b_buf);
1123 	inobuf.b_un.b_buf = NULL;
1124 	firstinum = lastinum = 0;
1125 	/*
1126 	 * Reload the snapshot inodes in case any of them changed.
1127 	 */
1128 	for (i = 0; i < snapcnt; i++) {
1129 		bp = snaplist[i].i_bp;
1130 		bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, bp->b_bno,
1131 		    bp->b_size);
1132 	}
1133 }
1134 
1135 /*
1136  * Routines to maintain information about directory inodes.
1137  * This is built during the first pass and used during the
1138  * second and third passes.
1139  *
1140  * Enter inodes into the cache.
1141  */
1142 struct inoinfo *
cacheino(union dinode * dp,ino_t inumber)1143 cacheino(union dinode *dp, ino_t inumber)
1144 {
1145 	struct inoinfo *inp;
1146 	int i, blks;
1147 
1148 	if (getinoinfo(inumber) != NULL)
1149 		pfatal("cacheino: duplicate entry for ino %jd\n",
1150 		    (intmax_t)inumber);
1151 	if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR)
1152 		blks = UFS_NDADDR + UFS_NIADDR;
1153 	else if (DIP(dp, di_size) > 0)
1154 		blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
1155 	else
1156 		blks = 1;
1157 	inp = (struct inoinfo *)
1158 		Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
1159 	if (inp == NULL)
1160 		errx(EEXIT, "cannot increase directory list");
1161 	SLIST_INSERT_HEAD(&inphash[inumber % dirhash], inp, i_hash);
1162 	inp->i_flags = 0;
1163 	inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0;
1164 	inp->i_dotdot = (ino_t)0;
1165 	inp->i_number = inumber;
1166 	inp->i_isize = DIP(dp, di_size);
1167 	inp->i_depth = DIP(dp, di_dirdepth);
1168 	inp->i_numblks = blks;
1169 	for (i = 0; i < MIN(blks, UFS_NDADDR); i++)
1170 		inp->i_blks[i] = DIP(dp, di_db[i]);
1171 	if (blks > UFS_NDADDR)
1172 		for (i = 0; i < UFS_NIADDR; i++)
1173 			inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]);
1174 	if (inplast == listmax) {
1175 		listmax += 100;
1176 		inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
1177 		    listmax, sizeof(struct inoinfo *));
1178 		if (inpsort == NULL)
1179 			errx(EEXIT, "cannot increase directory list");
1180 	}
1181 	inpsort[inplast++] = inp;
1182 	return (inp);
1183 }
1184 
1185 /*
1186  * Look up an inode cache structure.
1187  */
1188 struct inoinfo *
getinoinfo(ino_t inumber)1189 getinoinfo(ino_t inumber)
1190 {
1191 	struct inoinfo *inp;
1192 
1193 	SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) {
1194 		if (inp->i_number != inumber)
1195 			continue;
1196 		return (inp);
1197 	}
1198 	return (NULL);
1199 }
1200 
1201 /*
1202  * Remove an entry from the inode cache and disk-order sorted list.
1203  * Return 0 on success and 1 on failure.
1204  */
1205 int
removecachedino(ino_t inumber)1206 removecachedino(ino_t inumber)
1207 {
1208 	struct inoinfo *inp, **inpp;
1209 	char *listtype;
1210 
1211 	listtype = "hash";
1212 	SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) {
1213 		if (inp->i_number != inumber)
1214 			continue;
1215 		SLIST_REMOVE(&inphash[inumber % dirhash], inp, inoinfo, i_hash);
1216 		for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
1217 			if (*inpp != inp)
1218 				continue;
1219 			*inpp = inpsort[inplast - 1];
1220 			inplast--;
1221 			free(inp);
1222 			return (0);
1223 		}
1224 		listtype = "sort";
1225 		break;
1226 	}
1227 	pfatal("removecachedino: entry for ino %jd not found on %s list\n",
1228 	    (intmax_t)inumber, listtype);
1229 	return (1);
1230 }
1231 
1232 /*
1233  * Clean up all the inode cache structure.
1234  */
1235 void
inocleanup(void)1236 inocleanup(void)
1237 {
1238 	struct inoinfo **inpp;
1239 
1240 	if (inphash == NULL)
1241 		return;
1242 	for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
1243 		free((char *)(*inpp));
1244 	free((char *)inphash);
1245 	inphash = NULL;
1246 	free((char *)inpsort);
1247 	inpsort = NULL;
1248 }
1249 
1250 void
inodirty(struct inode * ip)1251 inodirty(struct inode *ip)
1252 {
1253 
1254 	if (sblock.fs_magic == FS_UFS2_MAGIC)
1255 		ffs_update_dinode_ckhash(&sblock,
1256 		    (struct ufs2_dinode *)ip->i_dp);
1257 	dirty(ip->i_bp);
1258 }
1259 
1260 void
clri(struct inodesc * idesc,const char * type,int flag)1261 clri(struct inodesc *idesc, const char *type, int flag)
1262 {
1263 	union dinode *dp;
1264 	struct inode ip;
1265 
1266 	ginode(idesc->id_number, &ip);
1267 	dp = ip.i_dp;
1268 	if (flag == 1) {
1269 		pwarn("%s %s", type,
1270 		    (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
1271 		prtinode(&ip);
1272 		printf("\n");
1273 	}
1274 	if (preen || reply("CLEAR") == 1) {
1275 		if (preen)
1276 			printf(" (CLEARED)\n");
1277 		n_files--;
1278 		if (bkgrdflag == 0) {
1279 			if (idesc->id_type == SNAP) {
1280 				snapremove(idesc->id_number);
1281 				idesc->id_type = ADDR;
1282 			}
1283 			(void)ckinode(dp, idesc);
1284 			inoinfo(idesc->id_number)->ino_state = USTATE;
1285 			clearinode(dp);
1286 			inodirty(&ip);
1287 		} else {
1288 			cmd.value = idesc->id_number;
1289 			cmd.size = -DIP(dp, di_nlink);
1290 			if (debug)
1291 				printf("adjrefcnt ino %ld amt %lld\n",
1292 				    (long)cmd.value, (long long)cmd.size);
1293 			if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
1294 			    &cmd, sizeof cmd) == -1)
1295 				rwerror("ADJUST INODE", cmd.value);
1296 		}
1297 	}
1298 	irelse(&ip);
1299 }
1300 
1301 int
findname(struct inodesc * idesc)1302 findname(struct inodesc *idesc)
1303 {
1304 	struct direct *dirp = idesc->id_dirp;
1305 
1306 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
1307 		idesc->id_entryno++;
1308 		return (KEEPON);
1309 	}
1310 	memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
1311 	return (STOP|FOUND);
1312 }
1313 
1314 int
findino(struct inodesc * idesc)1315 findino(struct inodesc *idesc)
1316 {
1317 	struct direct *dirp = idesc->id_dirp;
1318 
1319 	if (dirp->d_ino == 0)
1320 		return (KEEPON);
1321 	if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
1322 	    dirp->d_ino >= UFS_ROOTINO && dirp->d_ino < maxino) {
1323 		idesc->id_parent = dirp->d_ino;
1324 		return (STOP|FOUND);
1325 	}
1326 	return (KEEPON);
1327 }
1328 
1329 int
clearentry(struct inodesc * idesc)1330 clearentry(struct inodesc *idesc)
1331 {
1332 	struct direct *dirp = idesc->id_dirp;
1333 
1334 	if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
1335 		idesc->id_entryno++;
1336 		return (KEEPON);
1337 	}
1338 	dirp->d_ino = 0;
1339 	return (STOP|FOUND|ALTERED);
1340 }
1341 
1342 void
prtinode(struct inode * ip)1343 prtinode(struct inode *ip)
1344 {
1345 	char *p;
1346 	union dinode *dp;
1347 	struct passwd *pw;
1348 	time_t t;
1349 
1350 	dp = ip->i_dp;
1351 	printf(" I=%lu ", (u_long)ip->i_number);
1352 	if (ip->i_number < UFS_ROOTINO || ip->i_number >= maxino)
1353 		return;
1354 	printf(" OWNER=");
1355 	if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
1356 		printf("%s ", pw->pw_name);
1357 	else
1358 		printf("%u ", (unsigned)DIP(dp, di_uid));
1359 	printf("MODE=%o\n", DIP(dp, di_mode));
1360 	if (preen)
1361 		printf("%s: ", cdevname);
1362 	printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
1363 	t = DIP(dp, di_mtime);
1364 	if ((p = ctime(&t)) != NULL)
1365 		printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
1366 }
1367 
1368 void
blkerror(ino_t ino,const char * type,ufs2_daddr_t blk)1369 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
1370 {
1371 
1372 	pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
1373 	printf("\n");
1374 	switch (inoinfo(ino)->ino_state) {
1375 
1376 	case FSTATE:
1377 	case FZLINK:
1378 		inoinfo(ino)->ino_state = FCLEAR;
1379 		return;
1380 
1381 	case DSTATE:
1382 	case DZLINK:
1383 		inoinfo(ino)->ino_state = DCLEAR;
1384 		return;
1385 
1386 	case FCLEAR:
1387 	case DCLEAR:
1388 		return;
1389 
1390 	default:
1391 		errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
1392 		/* NOTREACHED */
1393 	}
1394 }
1395 
1396 /*
1397  * allocate an unused inode
1398  */
1399 ino_t
allocino(ino_t request,int type)1400 allocino(ino_t request, int type)
1401 {
1402 	ino_t ino;
1403 	struct inode ip;
1404 	union dinode *dp;
1405 	struct bufarea *cgbp;
1406 	struct cg *cgp;
1407 	int cg, anyino;
1408 
1409 	anyino = 0;
1410 	if (request == 0) {
1411 		request = UFS_ROOTINO;
1412 		anyino = 1;
1413 	} else if (inoinfo(request)->ino_state != USTATE)
1414 		return (0);
1415 retry:
1416 	for (ino = request; ino < maxino; ino++)
1417 		if (inoinfo(ino)->ino_state == USTATE)
1418 			break;
1419 	if (ino >= maxino)
1420 		return (0);
1421 	cg = ino_to_cg(&sblock, ino);
1422 	cgbp = cglookup(cg);
1423 	cgp = cgbp->b_un.b_cg;
1424 	if (!check_cgmagic(cg, cgbp)) {
1425 		if (anyino == 0)
1426 			return (0);
1427 		request = (cg + 1) * sblock.fs_ipg;
1428 		goto retry;
1429 	}
1430 	setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
1431 	cgp->cg_cs.cs_nifree--;
1432 	switch (type & IFMT) {
1433 	case IFDIR:
1434 		inoinfo(ino)->ino_state = DSTATE;
1435 		cgp->cg_cs.cs_ndir++;
1436 		break;
1437 	case IFREG:
1438 	case IFLNK:
1439 		inoinfo(ino)->ino_state = FSTATE;
1440 		break;
1441 	default:
1442 		return (0);
1443 	}
1444 	cgdirty(cgbp);
1445 	ginode(ino, &ip);
1446 	dp = ip.i_dp;
1447 	memset(dp, 0, ((sblock.fs_magic == FS_UFS1_MAGIC) ?
1448 	    sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)));
1449 	DIP_SET(dp, di_db[0], allocblk(ino_to_cg(&sblock, ino), (long)1,
1450 	    std_checkblkavail));
1451 	if (DIP(dp, di_db[0]) == 0) {
1452 		inoinfo(ino)->ino_state = USTATE;
1453 		inodirty(&ip);
1454 		irelse(&ip);
1455 		return (0);
1456 	}
1457 	DIP_SET(dp, di_mode, type);
1458 	DIP_SET(dp, di_atime, time(NULL));
1459 	DIP_SET(dp, di_ctime, DIP(dp, di_atime));
1460 	DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
1461 	DIP_SET(dp, di_size, sblock.fs_fsize);
1462 	DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
1463 	n_files++;
1464 	inodirty(&ip);
1465 	irelse(&ip);
1466 	inoinfo(ino)->ino_type = IFTODT(type);
1467 	return (ino);
1468 }
1469 
1470 /*
1471  * deallocate an inode
1472  */
1473 void
freeino(ino_t ino)1474 freeino(ino_t ino)
1475 {
1476 	struct inodesc idesc;
1477 	union dinode *dp;
1478 	struct inode ip;
1479 
1480 	memset(&idesc, 0, sizeof(struct inodesc));
1481 	idesc.id_type = ADDR;
1482 	idesc.id_func = freeblock;
1483 	idesc.id_number = ino;
1484 	ginode(ino, &ip);
1485 	dp = ip.i_dp;
1486 	(void)ckinode(dp, &idesc);
1487 	clearinode(dp);
1488 	inodirty(&ip);
1489 	irelse(&ip);
1490 	inoinfo(ino)->ino_state = USTATE;
1491 	n_files--;
1492 }
1493