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