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