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