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 time.tv_sec > sblock.fs_time)
652 now = time.tv_sec;
653 else
654 now = sblock.fs_time;
655 inum = cg * sblock.fs_ipg;
656 lastvalidinum = inum + inosused - 1;
657 nextinum = inum;
658 lastinum = inum;
659 readcount = 0;
660 /* Flush old contents in case they have been updated */
661 flush(fswritefd, &inobuf);
662 inobuf.b_bno = 0;
663 if (inobuf.b_un.b_buf == NULL) {
664 inobufsize = blkroundup(&sblock,
665 MAX(INOBUFSIZE, sblock.fs_bsize));
666 initbarea(&inobuf, BT_INODES);
667 if ((inobuf.b_un.b_buf = Balloc((unsigned)inobufsize)) == NULL)
668 errx(EEXIT, "cannot allocate space for inode buffer");
669 }
670 fullcnt = inobufsize / ((sblock.fs_magic == FS_UFS1_MAGIC) ?
671 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode));
672 readpercg = inosused / fullcnt;
673 partialcnt = inosused % fullcnt;
674 partialsize = fragroundup(&sblock,
675 partialcnt * ((sblock.fs_magic == FS_UFS1_MAGIC) ?
676 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)));
677 if (partialcnt != 0) {
678 readpercg++;
679 } else {
680 partialcnt = fullcnt;
681 partialsize = inobufsize;
682 }
683 }
684
685 int
freeblock(struct inodesc * idesc)686 freeblock(struct inodesc *idesc)
687 {
688 struct dups *dlp;
689 struct bufarea *cgbp;
690 struct cg *cgp;
691 ufs2_daddr_t blkno;
692 long size, nfrags;
693
694 blkno = idesc->id_blkno;
695 if (idesc->id_type == SNAP) {
696 pfatal("clearing a snapshot dinode\n");
697 return (STOP);
698 }
699 size = lfragtosize(&sblock, idesc->id_numfrags);
700 if (snapblkfree(&sblock, blkno, size, idesc->id_number,
701 std_checkblkavail))
702 return (KEEPON);
703 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
704 if (chkrange(blkno, 1)) {
705 return (SKIP);
706 } else if (testbmap(blkno)) {
707 for (dlp = duplist; dlp; dlp = dlp->next) {
708 if (dlp->dup != blkno)
709 continue;
710 dlp->dup = duplist->dup;
711 dlp = duplist;
712 duplist = duplist->next;
713 free((char *)dlp);
714 break;
715 }
716 if (dlp == NULL) {
717 clrbmap(blkno);
718 n_blks--;
719 }
720 }
721 }
722 /*
723 * If all successfully returned, account for them.
724 */
725 if (nfrags == 0) {
726 cgbp = cglookup(dtog(&sblock, idesc->id_blkno));
727 cgp = cgbp->b_un.b_cg;
728 if (idesc->id_numfrags == sblock.fs_frag)
729 cgp->cg_cs.cs_nbfree++;
730 else
731 cgp->cg_cs.cs_nffree += idesc->id_numfrags;
732 cgdirty(cgbp);
733 }
734 return (KEEPON);
735 }
736
737 /*
738 * Prepare a snapshot file for being removed.
739 */
740 void
snapremove(ino_t inum)741 snapremove(ino_t inum)
742 {
743 struct inodesc idesc;
744 struct inode ip;
745 int i;
746
747 for (i = 0; i < snapcnt; i++)
748 if (snaplist[i].i_number == inum)
749 break;
750 if (i == snapcnt)
751 ginode(inum, &ip);
752 else
753 ip = snaplist[i];
754 if ((DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) == 0) {
755 printf("snapremove: inode %jd is not a snapshot\n",
756 (intmax_t)inum);
757 if (i == snapcnt)
758 irelse(&ip);
759 return;
760 }
761 if (debug)
762 printf("snapremove: remove %sactive snapshot %jd\n",
763 i == snapcnt ? "in" : "", (intmax_t)inum);
764 /*
765 * If on active snapshot list, remove it.
766 */
767 if (i < snapcnt) {
768 for (i++; i < FSMAXSNAP; i++) {
769 if (sblock.fs_snapinum[i] == 0)
770 break;
771 snaplist[i - 1] = snaplist[i];
772 sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i];
773 }
774 sblock.fs_snapinum[i - 1] = 0;
775 bzero(&snaplist[i - 1], sizeof(struct inode));
776 snapcnt--;
777 }
778 memset(&idesc, 0, sizeof(struct inodesc));
779 idesc.id_type = SNAP;
780 idesc.id_func = snapclean;
781 idesc.id_number = inum;
782 (void)ckinode(ip.i_dp, &idesc);
783 DIP_SET(ip.i_dp, di_flags, DIP(ip.i_dp, di_flags) & ~SF_SNAPSHOT);
784 inodirty(&ip);
785 irelse(&ip);
786 }
787
788 static int
snapclean(struct inodesc * idesc)789 snapclean(struct inodesc *idesc)
790 {
791 ufs2_daddr_t blkno;
792 struct bufarea *bp;
793 union dinode *dp;
794
795 blkno = idesc->id_blkno;
796 if (blkno == 0)
797 return (KEEPON);
798
799 dp = idesc->id_dp;
800 if (blkno == BLK_NOCOPY || blkno == BLK_SNAP) {
801 if (idesc->id_lbn < UFS_NDADDR) {
802 DIP_SET(dp, di_db[idesc->id_lbn], 0);
803 } else {
804 bp = idesc->id_bp;
805 IBLK_SET(bp, bp->b_index, 0);
806 dirty(bp);
807 }
808 }
809 return (KEEPON);
810 }
811
812 /*
813 * Notification that a block is being freed. Return zero if the free
814 * should be allowed to proceed. Return non-zero if the snapshot file
815 * wants to claim the block. The block will be claimed if it is an
816 * uncopied part of one of the snapshots. It will be freed if it is
817 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
818 * If a fragment is being freed, then all snapshots that care about
819 * it must make a copy since a snapshot file can only claim full sized
820 * blocks. Note that if more than one snapshot file maps the block,
821 * we can pick one at random to claim it. Since none of the snapshots
822 * can change, we are assurred that they will all see the same unmodified
823 * image. When deleting a snapshot file (see ino_trunc above), we
824 * must push any of these claimed blocks to one of the other snapshots
825 * that maps it. These claimed blocks are easily identified as they will
826 * have a block number equal to their logical block number within the
827 * snapshot. A copied block can never have this property because they
828 * must always have been allocated from a BLK_NOCOPY location.
829 */
830 int
snapblkfree(struct fs * fs,ufs2_daddr_t bno,long size,ino_t inum,ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t blkno,long frags))831 snapblkfree(struct fs *fs, ufs2_daddr_t bno, long size, ino_t inum,
832 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
833 {
834 union dinode *dp;
835 struct inode ip;
836 struct bufarea *snapbp;
837 ufs_lbn_t lbn;
838 ufs2_daddr_t blkno, relblkno;
839 int i, frags, claimedblk, copydone;
840
841 /* If no snapshots, nothing to do */
842 if (snapcnt == 0)
843 return (0);
844 if (debug)
845 printf("snapblkfree: in ino %jd free blkno %jd, size %jd\n",
846 (intmax_t)inum, (intmax_t)bno, (intmax_t)size);
847 relblkno = blknum(fs, bno);
848 lbn = fragstoblks(fs, relblkno);
849 /* Direct blocks are always pre-copied */
850 if (lbn < UFS_NDADDR)
851 return (0);
852 copydone = 0;
853 claimedblk = 0;
854 for (i = 0; i < snapcnt; i++) {
855 /*
856 * Lookup block being freed.
857 */
858 ip = snaplist[i];
859 dp = ip.i_dp;
860 blkno = ino_blkatoff(dp, inum != 0 ? inum : ip.i_number,
861 lbn, &frags, &snapbp);
862 /*
863 * Check to see if block needs to be copied.
864 */
865 if (blkno == 0) {
866 /*
867 * A block that we map is being freed. If it has not
868 * been claimed yet, we will claim or copy it (below).
869 */
870 claimedblk = 1;
871 } else if (blkno == BLK_SNAP) {
872 /*
873 * No previous snapshot claimed the block,
874 * so it will be freed and become a BLK_NOCOPY
875 * (don't care) for us.
876 */
877 if (claimedblk)
878 pfatal("snapblkfree: inconsistent block type");
879 IBLK_SET(snapbp, snapbp->b_index, BLK_NOCOPY);
880 dirty(snapbp);
881 brelse(snapbp);
882 continue;
883 } else /* BLK_NOCOPY or default */ {
884 /*
885 * If the snapshot has already copied the block
886 * (default), or does not care about the block,
887 * it is not needed.
888 */
889 brelse(snapbp);
890 continue;
891 }
892 /*
893 * If this is a full size block, we will just grab it
894 * and assign it to the snapshot inode. Otherwise we
895 * will proceed to copy it. See explanation for this
896 * routine as to why only a single snapshot needs to
897 * claim this block.
898 */
899 if (size == fs->fs_bsize) {
900 if (debug)
901 printf("Grabonremove snapshot %ju lbn %jd "
902 "from inum %ju\n", (intmax_t)ip.i_number,
903 (intmax_t)lbn, (uintmax_t)inum);
904 IBLK_SET(snapbp, snapbp->b_index, relblkno);
905 dirty(snapbp);
906 brelse(snapbp);
907 DIP_SET(dp, di_blocks,
908 DIP(dp, di_blocks) + btodb(size));
909 inodirty(&ip);
910 return (1);
911 }
912
913 /* First time through, read the contents of the old block. */
914 if (copydone == 0) {
915 copydone = 1;
916 if (blread(fsreadfd, copybuf, fsbtodb(fs, relblkno),
917 fs->fs_bsize) != 0) {
918 pfatal("Could not read snapshot %ju block "
919 "%jd\n", (intmax_t)ip.i_number,
920 (intmax_t)relblkno);
921 continue;
922 }
923 }
924 /*
925 * This allocation will never require any additional
926 * allocations for the snapshot inode.
927 */
928 blkno = allocblk(dtog(fs, relblkno), fs->fs_frag,
929 checkblkavail);
930 if (blkno == 0) {
931 pfatal("Could not allocate block for snapshot %ju\n",
932 (intmax_t)ip.i_number);
933 continue;
934 }
935 if (debug)
936 printf("Copyonremove: snapino %jd lbn %jd for inum %ju "
937 "size %ld new blkno %jd\n", (intmax_t)ip.i_number,
938 (intmax_t)lbn, (uintmax_t)inum, size,
939 (intmax_t)blkno);
940 blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize);
941 IBLK_SET(snapbp, snapbp->b_index, blkno);
942 dirty(snapbp);
943 brelse(snapbp);
944 DIP_SET(dp, di_blocks,
945 DIP(dp, di_blocks) + btodb(fs->fs_bsize));
946 inodirty(&ip);
947 }
948 return (0);
949 }
950
951 /*
952 * Notification that a block is being written. Return if the block
953 * is part of a snapshot as snapshots never track other snapshots.
954 * The block will be copied in all of the snapshots that are tracking
955 * it and have not yet copied it. Some buffers may hold more than one
956 * block. Here we need to check each block in the buffer.
957 */
958 void
copyonwrite(struct fs * fs,struct bufarea * bp,ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t blkno,long frags))959 copyonwrite(struct fs *fs, struct bufarea *bp,
960 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
961 {
962 ufs2_daddr_t copyblkno;
963 long i, numblks;
964
965 /* If no snapshots, nothing to do. */
966 if (snapcnt == 0)
967 return;
968 numblks = blkroundup(fs, bp->b_size) / fs->fs_bsize;
969 if (debug)
970 prtbuf(bp, "copyonwrite: checking %jd block%s in buffer",
971 (intmax_t)numblks, numblks > 1 ? "s" : "");
972 copyblkno = blknum(fs, dbtofsb(fs, bp->b_bno));
973 for (i = 0; i < numblks; i++) {
974 chkcopyonwrite(fs, copyblkno, checkblkavail);
975 copyblkno += fs->fs_frag;
976 }
977 }
978
979 static void
chkcopyonwrite(struct fs * fs,ufs2_daddr_t copyblkno,ufs2_daddr_t (* checkblkavail)(ufs2_daddr_t blkno,long frags))980 chkcopyonwrite(struct fs *fs, ufs2_daddr_t copyblkno,
981 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
982 {
983 struct inode ip;
984 union dinode *dp;
985 struct bufarea *snapbp;
986 ufs2_daddr_t blkno;
987 int i, frags, copydone;
988 ufs_lbn_t lbn;
989
990 lbn = fragstoblks(fs, copyblkno);
991 /* Direct blocks are always pre-copied */
992 if (lbn < UFS_NDADDR)
993 return;
994 copydone = 0;
995 for (i = 0; i < snapcnt; i++) {
996 /*
997 * Lookup block being freed.
998 */
999 ip = snaplist[i];
1000 dp = ip.i_dp;
1001 blkno = ino_blkatoff(dp, ip.i_number, lbn, &frags, &snapbp);
1002 /*
1003 * Check to see if block needs to be copied.
1004 */
1005 if (blkno != 0) {
1006 /*
1007 * A block that we have already copied or don't track.
1008 */
1009 brelse(snapbp);
1010 continue;
1011 }
1012 /* First time through, read the contents of the old block. */
1013 if (copydone == 0) {
1014 copydone = 1;
1015 if (blread(fsreadfd, copybuf, fsbtodb(fs, copyblkno),
1016 fs->fs_bsize) != 0) {
1017 pfatal("Could not read snapshot %ju block "
1018 "%jd\n", (intmax_t)ip.i_number,
1019 (intmax_t)copyblkno);
1020 continue;
1021 }
1022 }
1023 /*
1024 * This allocation will never require any additional
1025 * allocations for the snapshot inode.
1026 */
1027 if ((blkno = allocblk(dtog(fs, copyblkno), fs->fs_frag,
1028 checkblkavail)) == 0) {
1029 pfatal("Could not allocate block for snapshot %ju\n",
1030 (intmax_t)ip.i_number);
1031 continue;
1032 }
1033 if (debug)
1034 prtbuf(snapbp, "Copyonwrite: snapino %jd lbn %jd using "
1035 "blkno %ju setting in buffer",
1036 (intmax_t)ip.i_number, (intmax_t)lbn,
1037 (intmax_t)blkno);
1038 blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize);
1039 IBLK_SET(snapbp, snapbp->b_index, blkno);
1040 dirty(snapbp);
1041 brelse(snapbp);
1042 DIP_SET(dp, di_blocks,
1043 DIP(dp, di_blocks) + btodb(fs->fs_bsize));
1044 inodirty(&ip);
1045 }
1046 return;
1047 }
1048
1049 /*
1050 * Traverse an inode and check that its block count is correct
1051 * fixing it if necessary.
1052 */
1053 void
check_blkcnt(struct inode * ip)1054 check_blkcnt(struct inode *ip)
1055 {
1056 struct inodesc idesc;
1057 union dinode *dp;
1058 ufs2_daddr_t ndb;
1059 int j, ret, offset;
1060
1061 dp = ip->i_dp;
1062 memset(&idesc, 0, sizeof(struct inodesc));
1063 idesc.id_func = pass1check;
1064 idesc.id_number = ip->i_number;
1065 idesc.id_type = (DIP(dp, di_flags) & SF_SNAPSHOT) == 0 ? ADDR : SNAP;
1066 (void)ckinode(dp, &idesc);
1067 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
1068 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
1069 for (j = 0; j < UFS_NXADDR; j++) {
1070 if (--ndb == 0 &&
1071 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
1072 idesc.id_numfrags = numfrags(&sblock,
1073 fragroundup(&sblock, offset));
1074 else
1075 idesc.id_numfrags = sblock.fs_frag;
1076 if (dp->dp2.di_extb[j] == 0)
1077 continue;
1078 idesc.id_blkno = dp->dp2.di_extb[j];
1079 ret = (*idesc.id_func)(&idesc);
1080 if (ret & STOP)
1081 break;
1082 }
1083 }
1084 idesc.id_entryno *= btodb(sblock.fs_fsize);
1085 if (DIP(dp, di_blocks) != idesc.id_entryno) {
1086 if (!(sujrecovery && preen)) {
1087 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
1088 (u_long)idesc.id_number,
1089 (uintmax_t)DIP(dp, di_blocks),
1090 (uintmax_t)idesc.id_entryno);
1091 if (preen)
1092 printf(" (CORRECTED)\n");
1093 else if (reply("CORRECT") == 0)
1094 return;
1095 }
1096 if (bkgrdflag == 0) {
1097 DIP_SET(dp, di_blocks, idesc.id_entryno);
1098 inodirty(ip);
1099 } else {
1100 cmd.value = idesc.id_number;
1101 cmd.size = idesc.id_entryno - DIP(dp, di_blocks);
1102 if (debug)
1103 printf("adjblkcnt ino %ju amount %lld\n",
1104 (uintmax_t)cmd.value, (long long)cmd.size);
1105 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
1106 &cmd, sizeof cmd) == -1)
1107 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
1108 }
1109 }
1110 }
1111
1112 void
freeinodebuf(void)1113 freeinodebuf(void)
1114 {
1115 struct bufarea *bp;
1116 int i;
1117
1118 /*
1119 * Flush old contents in case they have been updated.
1120 */
1121 flush(fswritefd, &inobuf);
1122 if (inobuf.b_un.b_buf != NULL)
1123 free((char *)inobuf.b_un.b_buf);
1124 inobuf.b_un.b_buf = NULL;
1125 firstinum = lastinum = 0;
1126 /*
1127 * Reload the snapshot inodes in case any of them changed.
1128 */
1129 for (i = 0; i < snapcnt; i++) {
1130 bp = snaplist[i].i_bp;
1131 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, bp->b_bno,
1132 bp->b_size);
1133 }
1134 }
1135
1136 /*
1137 * Routines to maintain information about directory inodes.
1138 * This is built during the first pass and used during the
1139 * second and third passes.
1140 *
1141 * Enter inodes into the cache.
1142 */
1143 struct inoinfo *
cacheino(union dinode * dp,ino_t inumber)1144 cacheino(union dinode *dp, ino_t inumber)
1145 {
1146 struct inoinfo *inp;
1147 int i, blks;
1148
1149 if (getinoinfo(inumber) != NULL)
1150 pfatal("cacheino: duplicate entry for ino %jd\n",
1151 (intmax_t)inumber);
1152 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR)
1153 blks = UFS_NDADDR + UFS_NIADDR;
1154 else if (DIP(dp, di_size) > 0)
1155 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
1156 else
1157 blks = 1;
1158 inp = (struct inoinfo *)
1159 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
1160 if (inp == NULL)
1161 errx(EEXIT, "cannot increase directory list");
1162 SLIST_INSERT_HEAD(&inphash[inumber % dirhash], inp, i_hash);
1163 inp->i_flags = 0;
1164 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0;
1165 inp->i_dotdot = (ino_t)0;
1166 inp->i_number = inumber;
1167 inp->i_isize = DIP(dp, di_size);
1168 inp->i_depth = DIP(dp, di_dirdepth);
1169 inp->i_numblks = blks;
1170 for (i = 0; i < MIN(blks, UFS_NDADDR); i++)
1171 inp->i_blks[i] = DIP(dp, di_db[i]);
1172 if (blks > UFS_NDADDR)
1173 for (i = 0; i < UFS_NIADDR; i++)
1174 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]);
1175 if (inplast == listmax) {
1176 listmax += 100;
1177 inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
1178 listmax, sizeof(struct inoinfo *));
1179 if (inpsort == NULL)
1180 errx(EEXIT, "cannot increase directory list");
1181 }
1182 inpsort[inplast++] = inp;
1183 return (inp);
1184 }
1185
1186 /*
1187 * Look up an inode cache structure.
1188 */
1189 struct inoinfo *
getinoinfo(ino_t inumber)1190 getinoinfo(ino_t inumber)
1191 {
1192 struct inoinfo *inp;
1193
1194 SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) {
1195 if (inp->i_number != inumber)
1196 continue;
1197 return (inp);
1198 }
1199 return (NULL);
1200 }
1201
1202 /*
1203 * Remove an entry from the inode cache and disk-order sorted list.
1204 * Return 0 on success and 1 on failure.
1205 */
1206 int
removecachedino(ino_t inumber)1207 removecachedino(ino_t inumber)
1208 {
1209 struct inoinfo *inp, **inpp;
1210 char *listtype;
1211
1212 listtype = "hash";
1213 SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) {
1214 if (inp->i_number != inumber)
1215 continue;
1216 SLIST_REMOVE(&inphash[inumber % dirhash], inp, inoinfo, i_hash);
1217 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
1218 if (*inpp != inp)
1219 continue;
1220 *inpp = inpsort[inplast - 1];
1221 inplast--;
1222 free(inp);
1223 return (0);
1224 }
1225 listtype = "sort";
1226 break;
1227 }
1228 pfatal("removecachedino: entry for ino %jd not found on %s list\n",
1229 (intmax_t)inumber, listtype);
1230 return (1);
1231 }
1232
1233 /*
1234 * Clean up all the inode cache structure.
1235 */
1236 void
inocleanup(void)1237 inocleanup(void)
1238 {
1239 struct inoinfo **inpp;
1240
1241 if (inphash == NULL)
1242 return;
1243 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
1244 free((char *)(*inpp));
1245 free((char *)inphash);
1246 inphash = NULL;
1247 free((char *)inpsort);
1248 inpsort = NULL;
1249 }
1250
1251 void
inodirty(struct inode * ip)1252 inodirty(struct inode *ip)
1253 {
1254
1255 if (sblock.fs_magic == FS_UFS2_MAGIC)
1256 ffs_update_dinode_ckhash(&sblock,
1257 (struct ufs2_dinode *)ip->i_dp);
1258 dirty(ip->i_bp);
1259 }
1260
1261 void
clri(struct inodesc * idesc,const char * type,int flag)1262 clri(struct inodesc *idesc, const char *type, int flag)
1263 {
1264 union dinode *dp;
1265 struct inode ip;
1266
1267 ginode(idesc->id_number, &ip);
1268 dp = ip.i_dp;
1269 if (flag == 1) {
1270 pwarn("%s %s", type,
1271 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
1272 prtinode(&ip);
1273 printf("\n");
1274 }
1275 if (preen || reply("CLEAR") == 1) {
1276 if (preen)
1277 printf(" (CLEARED)\n");
1278 n_files--;
1279 if (bkgrdflag == 0) {
1280 if (idesc->id_type == SNAP) {
1281 snapremove(idesc->id_number);
1282 idesc->id_type = ADDR;
1283 }
1284 (void)ckinode(dp, idesc);
1285 inoinfo(idesc->id_number)->ino_state = USTATE;
1286 clearinode(dp);
1287 inodirty(&ip);
1288 } else {
1289 cmd.value = idesc->id_number;
1290 cmd.size = -DIP(dp, di_nlink);
1291 if (debug)
1292 printf("adjrefcnt ino %ld amt %lld\n",
1293 (long)cmd.value, (long long)cmd.size);
1294 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
1295 &cmd, sizeof cmd) == -1)
1296 rwerror("ADJUST INODE", cmd.value);
1297 }
1298 }
1299 irelse(&ip);
1300 }
1301
1302 int
findname(struct inodesc * idesc)1303 findname(struct inodesc *idesc)
1304 {
1305 struct direct *dirp = idesc->id_dirp;
1306
1307 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
1308 idesc->id_entryno++;
1309 return (KEEPON);
1310 }
1311 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
1312 return (STOP|FOUND);
1313 }
1314
1315 int
findino(struct inodesc * idesc)1316 findino(struct inodesc *idesc)
1317 {
1318 struct direct *dirp = idesc->id_dirp;
1319
1320 if (dirp->d_ino == 0)
1321 return (KEEPON);
1322 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
1323 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino < maxino) {
1324 idesc->id_parent = dirp->d_ino;
1325 return (STOP|FOUND);
1326 }
1327 return (KEEPON);
1328 }
1329
1330 int
clearentry(struct inodesc * idesc)1331 clearentry(struct inodesc *idesc)
1332 {
1333 struct direct *dirp = idesc->id_dirp;
1334
1335 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
1336 idesc->id_entryno++;
1337 return (KEEPON);
1338 }
1339 dirp->d_ino = 0;
1340 return (STOP|FOUND|ALTERED);
1341 }
1342
1343 void
prtinode(struct inode * ip)1344 prtinode(struct inode *ip)
1345 {
1346 char *p;
1347 union dinode *dp;
1348 struct passwd *pw;
1349 time_t t;
1350
1351 dp = ip->i_dp;
1352 printf(" I=%lu ", (u_long)ip->i_number);
1353 if (ip->i_number < UFS_ROOTINO || ip->i_number >= maxino)
1354 return;
1355 printf(" OWNER=");
1356 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
1357 printf("%s ", pw->pw_name);
1358 else
1359 printf("%u ", (unsigned)DIP(dp, di_uid));
1360 printf("MODE=%o\n", DIP(dp, di_mode));
1361 if (preen)
1362 printf("%s: ", cdevname);
1363 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
1364 t = DIP(dp, di_mtime);
1365 if ((p = ctime(&t)) != NULL)
1366 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
1367 }
1368
1369 void
blkerror(ino_t ino,const char * type,ufs2_daddr_t blk)1370 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
1371 {
1372
1373 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
1374 printf("\n");
1375 switch (inoinfo(ino)->ino_state) {
1376
1377 case FSTATE:
1378 case FZLINK:
1379 inoinfo(ino)->ino_state = FCLEAR;
1380 return;
1381
1382 case DSTATE:
1383 case DZLINK:
1384 inoinfo(ino)->ino_state = DCLEAR;
1385 return;
1386
1387 case FCLEAR:
1388 case DCLEAR:
1389 return;
1390
1391 default:
1392 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
1393 /* NOTREACHED */
1394 }
1395 }
1396
1397 /*
1398 * allocate an unused inode
1399 */
1400 ino_t
allocino(ino_t request,int type)1401 allocino(ino_t request, int type)
1402 {
1403 ino_t ino;
1404 struct inode ip;
1405 union dinode *dp;
1406 struct bufarea *cgbp;
1407 struct cg *cgp;
1408 int cg, anyino;
1409
1410 anyino = 0;
1411 if (request == 0) {
1412 request = UFS_ROOTINO;
1413 anyino = 1;
1414 } else if (inoinfo(request)->ino_state != USTATE)
1415 return (0);
1416 retry:
1417 for (ino = request; ino < maxino; ino++)
1418 if (inoinfo(ino)->ino_state == USTATE)
1419 break;
1420 if (ino >= maxino)
1421 return (0);
1422 cg = ino_to_cg(&sblock, ino);
1423 cgbp = cglookup(cg);
1424 cgp = cgbp->b_un.b_cg;
1425 if (!check_cgmagic(cg, cgbp)) {
1426 if (anyino == 0)
1427 return (0);
1428 request = (cg + 1) * sblock.fs_ipg;
1429 goto retry;
1430 }
1431 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
1432 cgp->cg_cs.cs_nifree--;
1433 switch (type & IFMT) {
1434 case IFDIR:
1435 inoinfo(ino)->ino_state = DSTATE;
1436 cgp->cg_cs.cs_ndir++;
1437 break;
1438 case IFREG:
1439 case IFLNK:
1440 inoinfo(ino)->ino_state = FSTATE;
1441 break;
1442 default:
1443 return (0);
1444 }
1445 cgdirty(cgbp);
1446 ginode(ino, &ip);
1447 dp = ip.i_dp;
1448 memset(dp, 0, ((sblock.fs_magic == FS_UFS1_MAGIC) ?
1449 sizeof(struct ufs1_dinode) : sizeof(struct ufs2_dinode)));
1450 DIP_SET(dp, di_db[0], allocblk(ino_to_cg(&sblock, ino), (long)1,
1451 std_checkblkavail));
1452 if (DIP(dp, di_db[0]) == 0) {
1453 inoinfo(ino)->ino_state = USTATE;
1454 inodirty(&ip);
1455 irelse(&ip);
1456 return (0);
1457 }
1458 DIP_SET(dp, di_mode, type);
1459 DIP_SET(dp, di_atime, time(NULL));
1460 DIP_SET(dp, di_ctime, DIP(dp, di_atime));
1461 DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
1462 DIP_SET(dp, di_size, sblock.fs_fsize);
1463 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
1464 n_files++;
1465 inodirty(&ip);
1466 irelse(&ip);
1467 inoinfo(ino)->ino_type = IFTODT(type);
1468 return (ino);
1469 }
1470
1471 /*
1472 * deallocate an inode
1473 */
1474 void
freeino(ino_t ino)1475 freeino(ino_t ino)
1476 {
1477 struct inodesc idesc;
1478 union dinode *dp;
1479 struct inode ip;
1480
1481 memset(&idesc, 0, sizeof(struct inodesc));
1482 idesc.id_type = ADDR;
1483 idesc.id_func = freeblock;
1484 idesc.id_number = ino;
1485 ginode(ino, &ip);
1486 dp = ip.i_dp;
1487 (void)ckinode(dp, &idesc);
1488 clearinode(dp);
1489 inodirty(&ip);
1490 irelse(&ip);
1491 inoinfo(ino)->ino_state = USTATE;
1492 n_files--;
1493 }
1494