2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1986, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static const char sccsid[] = "@(#)inode.c 8.8 (Berkeley) 4/28/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/stdint.h>
43 #include <sys/sysctl.h>
45 #include <ufs/ufs/dinode.h>
46 #include <ufs/ufs/dir.h>
47 #include <ufs/ffs/fs.h>
57 struct bufarea *icachebp; /* inode cache buffer */
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,
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));
67 ckinode(union dinode *dp, struct inodesc *idesc)
69 off_t remsize, sizepb;
75 char pathbuf[MAXPATHLEN + 1];
77 if (idesc->id_fix != IGNORE)
78 idesc->id_fix = DONTKNOW;
81 idesc->id_lballoc = -1;
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))
89 if (sblock.fs_magic == FS_UFS1_MAGIC)
93 ndb = howmany(DIP(&dino, di_size), sblock.fs_bsize);
94 for (i = 0; i < UFS_NDADDR; i++) {
97 (offset = blkoff(&sblock, DIP(&dino, di_size))) != 0)
99 numfrags(&sblock, fragroundup(&sblock, offset));
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,
107 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
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);
114 "YOU MUST RERUN FSCK AFTERWARDS\n");
122 idesc->id_blkno = DIP(&dino, di_db[i]);
123 if (idesc->id_type != DATA)
124 ret = (*idesc->id_func)(idesc);
126 ret = dirscan(idesc);
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);
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,
147 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
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);
155 "YOU MUST RERUN FSCK AFTERWARDS\n");
169 iblock(struct inodesc *idesc, off_t isize, int type)
173 int i, n, (*func)(struct inodesc *), nif;
176 char pathbuf[MAXPATHLEN + 1];
178 if (idesc->id_type != DATA) {
179 func = idesc->id_func;
180 if (((n = (*func)(idesc)) & KEEPON) == 0)
184 bp = getdatablk(idesc->id_blkno, sblock.fs_bsize, type);
185 if (bp->b_errs != 0) {
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);
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)
201 (void)sprintf(buf, "PARTIALLY TRUNCATED INODE I=%lu",
202 (u_long)idesc->id_number);
205 } else if (dofix(idesc, buf)) {
210 flush(fswritefd, bp);
212 for (i = 0; i < nif; i++) {
214 idesc->id_blkno = IBLK(bp, i);
216 if (idesc->id_level == 0) {
220 n = iblock(idesc, isize, type - 1);
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,
233 pfatal("DIRECTORY %s: CONTAINS EMPTY BLOCKS",
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);
241 "YOU MUST RERUN FSCK AFTERWARDS\n");
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.
262 ino_blkatoff(union dinode *dp, ino_t ino, ufs_lbn_t lbn, int *frags,
263 struct bufarea **bpp)
274 * Handle extattr blocks first.
276 if (lbn < 0 && lbn >= -UFS_NXADDR) {
278 if (lbn > lblkno(&sblock, dp->dp2.di_extsize - 1))
280 *frags = numfrags(&sblock,
281 sblksize(&sblock, dp->dp2.di_extsize, lbn));
282 return (dp->dp2.di_extb[lbn]);
285 * Now direct and indirect.
287 if (DIP(dp, di_mode) == IFLNK &&
288 DIP(dp, di_size) < sblock.fs_maxsymlinklen)
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]));
295 *frags = sblock.fs_frag;
297 for (i = 0, tmpval = NINDIR(&sblock), cur = UFS_NDADDR; i < UFS_NIADDR;
298 i++, tmpval *= NINDIR(&sblock), cur = next) {
301 return (DIP(dp, di_ib[i]));
303 * Determine whether the lbn in question is within this tree.
305 if (lbn < 0 && -lbn >= next)
307 if (lbn > 0 && lbn >= next)
309 if (DIP(dp, di_ib[i]) == 0)
311 return (indir_blkatoff(DIP(dp, di_ib[i]), ino, -cur - i, lbn,
314 pfatal("lbn %jd not in ino %ju\n", lbn, (uintmax_t)ino);
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.
324 indir_blkatoff(ufs2_daddr_t blk, ino_t ino, ufs_lbn_t cur, ufs_lbn_t lbn,
325 struct bufarea **bpp)
332 level = lbn_level(cur);
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);
340 base = -(cur + level);
341 for (i = level; i > 0; i--)
342 lbnadd *= NINDIR(&sblock);
344 i = (lbn - base) / lbnadd;
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);
353 cur = base + (i * lbnadd);
355 cur = -(base + (i * lbnadd)) - (level - 1);
356 bp = getdatablk(blk, sblock.fs_bsize, BT_LEVEL1 + level);
361 if (cur == lbn || blk == 0) {
370 pfatal("Invalid lbn %jd at level 0 for ino %ju\n", lbn,
372 return (indir_blkatoff(blk, ino, cur, lbn, bpp));
376 * Check that a block in a legal block number.
377 * Return 0 if in range, 1 if out of range.
380 chkrange(ufs2_daddr_t blk, int cnt)
384 if (cnt <= 0 || blk <= 0 || blk >= maxfsblock ||
385 cnt > maxfsblock - blk) {
387 printf("out of range: blk %ld, offset %i, size %d\n",
388 (long)blk, (int)fragnum(&sblock, blk), cnt);
391 if (cnt > sblock.fs_frag ||
392 fragnum(&sblock, blk) + cnt > sblock.fs_frag) {
394 printf("bad size: blk %ld, offset %i, size %d\n",
395 (long)blk, (int)fragnum(&sblock, blk), cnt);
398 c = dtog(&sblock, blk);
399 if (blk < cgdmin(&sblock, c)) {
400 if ((blk + cnt) > cgsblock(&sblock, c)) {
402 printf("blk %ld < cgdmin %ld;",
403 (long)blk, (long)cgdmin(&sblock, c));
404 printf(" blk + cnt %ld > cgsbase %ld\n",
406 (long)cgsblock(&sblock, c));
411 if ((blk + cnt) > cgbase(&sblock, c+1)) {
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);
425 * General purpose interface for reading inodes.
427 * firstinum and lastinum track contents of getnextino() cache (below).
429 static ino_t firstinum, lastinum;
430 static struct bufarea inobuf;
433 ginode(ino_t inumber, struct inode *ip)
437 if (inumber < UFS_ROOTINO || inumber > maxino)
438 errx(EEXIT, "bad inode number %ju to ginode",
440 ip->i_number = inumber;
441 if (inumber >= firstinum && inumber < lastinum) {
442 /* contents in getnextino() cache */
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++;
453 iblk = ino_to_fsba(&sblock, inumber);
454 /* release our cache-hold reference on old icachebp */
455 if (icachebp != NULL)
457 icachebp = getdatablk(iblk, sblock.fs_bsize, BT_INODES);
458 if (icachebp->b_errs != 0) {
464 /* take a cache-hold reference on new icachebp */
465 icachebp->b_refcnt++;
466 icachebp->b_index = rounddown(inumber, INOPB(&sblock));
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];
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");
479 if (preen || reply("FIX") != 0) {
481 printf(" (FIXED)\n");
482 ffs_update_dinode_ckhash(&sblock,
483 (struct ufs2_dinode *)ip->i_dp);
490 * Release a held inode.
493 irelse(struct inode *ip)
496 /* Check for failed inode read */
497 if (ip->i_bp == NULL)
499 if (ip->i_bp->b_refcnt <= 0)
500 pfatal("irelse: releasing unreferenced ino %ju\n",
501 (uintmax_t) ip->i_number);
506 * Special purpose version of ginode used to optimize first pass
507 * over all the inodes in numerical order.
509 static ino_t nextinum, lastvalidinum;
510 static long readcount, readpercg, fullcnt, inobufsize, partialcnt, partialsize;
513 getnextinode(ino_t inumber, int rebuiltcg)
518 ufs2_daddr_t ndb, blk;
521 static caddr_t nextinop;
523 if (inumber != nextinum++ || inumber > lastvalidinum)
524 errx(EEXIT, "bad inode number %ju to nextinode",
526 if (inumber >= lastinum) {
528 firstinum = lastinum;
529 blk = ino_to_fsba(&sblock, lastinum);
530 if (readcount % readpercg == 0) {
532 lastinum += partialcnt;
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.
542 if (inobuf.b_refcnt != 0)
543 pfatal("Non-zero getnextinode() ref count %d\n",
545 flush(fswritefd, &inobuf);
546 getblk(&inobuf, blk, size);
547 nextinop = inobuf.b_un.b_buf;
549 dp = (union dinode *)nextinop;
550 if (sblock.fs_magic == FS_UFS1_MAGIC)
551 nextinop += sizeof(struct ufs1_dinode);
553 nextinop += sizeof(struct ufs2_dinode);
554 if ((ckhashadd & CK_INODE) != 0) {
555 ffs_update_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp);
558 if (ffs_verify_dinode_ckhash(&sblock, (struct ufs2_dinode *)dp) != 0) {
559 pwarn("INODE CHECK-HASH FAILED");
562 ip.i_number = inumber;
564 if (preen || reply("FIX") != 0) {
566 printf(" (FIXED)\n");
567 ffs_update_dinode_ckhash(&sblock,
568 (struct ufs2_dinode *)dp);
572 if (rebuiltcg && (char *)dp == inobuf.b_un.b_buf) {
574 * Try to determine if we have reached the end of the
577 mode = DIP(dp, di_mode) & IFMT;
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)
589 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize);
592 if (mode == IFBLK || mode == IFCHR)
596 * Fake ndb value so direct/indirect block checks below
597 * will detect any garbage after symlink string.
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);
610 for (j = ndb; ndb < UFS_NDADDR && j < UFS_NDADDR; j++)
611 if (DIP(dp, di_db[j]) != 0)
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)
623 setinodebuf(int cg, ino_t inosused)
627 inum = cg * sblock.fs_ipg;
628 lastvalidinum = inum + inosused - 1;
632 /* Flush old contents in case they have been updated */
633 flush(fswritefd, &inobuf);
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");
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) {
652 partialcnt = fullcnt;
653 partialsize = inobufsize;
658 freeblock(struct inodesc *idesc)
661 struct bufarea *cgbp;
666 blkno = idesc->id_blkno;
667 if (idesc->id_type == SNAP) {
668 pfatal("clearing a snapshot dinode\n");
671 size = lfragtosize(&sblock, idesc->id_numfrags);
672 if (snapblkfree(&sblock, blkno, size, idesc->id_number,
675 for (nfrags = idesc->id_numfrags; nfrags > 0; blkno++, nfrags--) {
676 if (chkrange(blkno, 1)) {
678 } else if (testbmap(blkno)) {
679 for (dlp = duplist; dlp; dlp = dlp->next) {
680 if (dlp->dup != blkno)
682 dlp->dup = duplist->dup;
684 duplist = duplist->next;
695 * If all successfully returned, account for them.
698 cgbp = cglookup(dtog(&sblock, idesc->id_blkno));
699 cgp = cgbp->b_un.b_cg;
700 if (idesc->id_numfrags == sblock.fs_frag)
701 cgp->cg_cs.cs_nbfree++;
703 cgp->cg_cs.cs_nffree += idesc->id_numfrags;
710 * Prepare a snapshot file for being removed.
713 snapremove(ino_t inum)
715 struct inodesc idesc;
719 for (i = 0; i < snapcnt; i++)
720 if (snaplist[i].i_number == inum)
726 if ((DIP(ip.i_dp, di_flags) & SF_SNAPSHOT) == 0) {
727 printf("snapremove: inode %jd is not a snapshot\n",
734 printf("snapremove: remove %sactive snapshot %jd\n",
735 i == snapcnt ? "in" : "", (intmax_t)inum);
737 * If on active snapshot list, remove it.
740 for (i++; i < FSMAXSNAP; i++) {
741 if (sblock.fs_snapinum[i] == 0)
743 snaplist[i - 1] = snaplist[i];
744 sblock.fs_snapinum[i - 1] = sblock.fs_snapinum[i];
746 sblock.fs_snapinum[i - 1] = 0;
747 bzero(&snaplist[i - 1], sizeof(struct inode));
750 memset(&idesc, 0, sizeof(struct inodesc));
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);
761 snapclean(struct inodesc *idesc)
767 blkno = idesc->id_blkno;
772 if (blkno == BLK_NOCOPY || blkno == BLK_SNAP) {
773 if (idesc->id_lbn < UFS_NDADDR) {
774 DIP_SET(dp, di_db[idesc->id_lbn], 0);
777 IBLK_SET(bp, bp->b_index, 0);
785 * Notification that a block is being freed. Return zero if the free
786 * should be allowed to proceed. Return non-zero if the snapshot file
787 * wants to claim the block. The block will be claimed if it is an
788 * uncopied part of one of the snapshots. It will be freed if it is
789 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
790 * If a fragment is being freed, then all snapshots that care about
791 * it must make a copy since a snapshot file can only claim full sized
792 * blocks. Note that if more than one snapshot file maps the block,
793 * we can pick one at random to claim it. Since none of the snapshots
794 * can change, we are assurred that they will all see the same unmodified
795 * image. When deleting a snapshot file (see ino_trunc above), we
796 * must push any of these claimed blocks to one of the other snapshots
797 * that maps it. These claimed blocks are easily identified as they will
798 * have a block number equal to their logical block number within the
799 * snapshot. A copied block can never have this property because they
800 * must always have been allocated from a BLK_NOCOPY location.
803 snapblkfree(struct fs *fs, ufs2_daddr_t bno, long size, ino_t inum,
804 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
808 struct bufarea *snapbp;
810 ufs2_daddr_t blkno, relblkno;
811 int i, frags, claimedblk, copydone;
813 /* If no snapshots, nothing to do */
817 printf("snapblkfree: in ino %jd free blkno %jd, size %jd\n",
818 (intmax_t)inum, (intmax_t)bno, (intmax_t)size);
819 relblkno = blknum(fs, bno);
820 lbn = fragstoblks(fs, relblkno);
821 /* Direct blocks are always pre-copied */
822 if (lbn < UFS_NDADDR)
826 for (i = 0; i < snapcnt; i++) {
828 * Lookup block being freed.
832 blkno = ino_blkatoff(dp, inum != 0 ? inum : ip.i_number,
833 lbn, &frags, &snapbp);
835 * Check to see if block needs to be copied.
839 * A block that we map is being freed. If it has not
840 * been claimed yet, we will claim or copy it (below).
843 } else if (blkno == BLK_SNAP) {
845 * No previous snapshot claimed the block,
846 * so it will be freed and become a BLK_NOCOPY
847 * (don't care) for us.
850 pfatal("snapblkfree: inconsistent block type");
851 IBLK_SET(snapbp, snapbp->b_index, BLK_NOCOPY);
855 } else /* BLK_NOCOPY or default */ {
857 * If the snapshot has already copied the block
858 * (default), or does not care about the block,
865 * If this is a full size block, we will just grab it
866 * and assign it to the snapshot inode. Otherwise we
867 * will proceed to copy it. See explanation for this
868 * routine as to why only a single snapshot needs to
871 if (size == fs->fs_bsize) {
873 printf("Grabonremove snapshot %ju lbn %jd "
874 "from inum %ju\n", (intmax_t)ip.i_number,
875 (intmax_t)lbn, (uintmax_t)inum);
876 IBLK_SET(snapbp, snapbp->b_index, relblkno);
879 DIP_SET(dp, di_blocks,
880 DIP(dp, di_blocks) + btodb(size));
885 /* First time through, read the contents of the old block. */
888 if (blread(fsreadfd, copybuf, fsbtodb(fs, relblkno),
889 fs->fs_bsize) != 0) {
890 pfatal("Could not read snapshot %ju block "
891 "%jd\n", (intmax_t)ip.i_number,
897 * This allocation will never require any additional
898 * allocations for the snapshot inode.
900 blkno = allocblk(dtog(fs, relblkno), fs->fs_frag,
903 pfatal("Could not allocate block for snapshot %ju\n",
904 (intmax_t)ip.i_number);
908 printf("Copyonremove: snapino %jd lbn %jd for inum %ju "
909 "size %ld new blkno %jd\n", (intmax_t)ip.i_number,
910 (intmax_t)lbn, (uintmax_t)inum, size,
912 blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize);
913 IBLK_SET(snapbp, snapbp->b_index, blkno);
916 DIP_SET(dp, di_blocks,
917 DIP(dp, di_blocks) + btodb(fs->fs_bsize));
924 * Notification that a block is being written. Return if the block
925 * is part of a snapshot as snapshots never track other snapshots.
926 * The block will be copied in all of the snapshots that are tracking
927 * it and have not yet copied it. Some buffers may hold more than one
928 * block. Here we need to check each block in the buffer.
931 copyonwrite(struct fs *fs, struct bufarea *bp,
932 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
934 ufs2_daddr_t copyblkno;
937 /* If no snapshots, nothing to do. */
940 numblks = blkroundup(fs, bp->b_size) / fs->fs_bsize;
942 prtbuf(bp, "copyonwrite: checking %jd block%s in buffer",
943 (intmax_t)numblks, numblks > 1 ? "s" : "");
944 copyblkno = blknum(fs, dbtofsb(fs, bp->b_bno));
945 for (i = 0; i < numblks; i++) {
946 chkcopyonwrite(fs, copyblkno, checkblkavail);
947 copyblkno += fs->fs_frag;
952 chkcopyonwrite(struct fs *fs, ufs2_daddr_t copyblkno,
953 ufs2_daddr_t (*checkblkavail)(ufs2_daddr_t blkno, long frags))
957 struct bufarea *snapbp;
959 int i, frags, copydone;
962 lbn = fragstoblks(fs, copyblkno);
963 /* Direct blocks are always pre-copied */
964 if (lbn < UFS_NDADDR)
967 for (i = 0; i < snapcnt; i++) {
969 * Lookup block being freed.
973 blkno = ino_blkatoff(dp, ip.i_number, lbn, &frags, &snapbp);
975 * Check to see if block needs to be copied.
979 * A block that we have already copied or don't track.
984 /* First time through, read the contents of the old block. */
987 if (blread(fsreadfd, copybuf, fsbtodb(fs, copyblkno),
988 fs->fs_bsize) != 0) {
989 pfatal("Could not read snapshot %ju block "
990 "%jd\n", (intmax_t)ip.i_number,
991 (intmax_t)copyblkno);
996 * This allocation will never require any additional
997 * allocations for the snapshot inode.
999 if ((blkno = allocblk(dtog(fs, copyblkno), fs->fs_frag,
1000 checkblkavail)) == 0) {
1001 pfatal("Could not allocate block for snapshot %ju\n",
1002 (intmax_t)ip.i_number);
1006 prtbuf(snapbp, "Copyonwrite: snapino %jd lbn %jd using "
1007 "blkno %ju setting in buffer",
1008 (intmax_t)ip.i_number, (intmax_t)lbn,
1010 blwrite(fswritefd, copybuf, fsbtodb(fs, blkno), fs->fs_bsize);
1011 IBLK_SET(snapbp, snapbp->b_index, blkno);
1014 DIP_SET(dp, di_blocks,
1015 DIP(dp, di_blocks) + btodb(fs->fs_bsize));
1022 * Traverse an inode and check that its block count is correct
1023 * fixing it if necessary.
1026 check_blkcnt(struct inode *ip)
1028 struct inodesc idesc;
1034 memset(&idesc, 0, sizeof(struct inodesc));
1035 idesc.id_func = pass1check;
1036 idesc.id_number = ip->i_number;
1037 idesc.id_type = (DIP(dp, di_flags) & SF_SNAPSHOT) == 0 ? ADDR : SNAP;
1038 (void)ckinode(dp, &idesc);
1039 if (sblock.fs_magic == FS_UFS2_MAGIC && dp->dp2.di_extsize > 0) {
1040 ndb = howmany(dp->dp2.di_extsize, sblock.fs_bsize);
1041 for (j = 0; j < UFS_NXADDR; j++) {
1043 (offset = blkoff(&sblock, dp->dp2.di_extsize)) != 0)
1044 idesc.id_numfrags = numfrags(&sblock,
1045 fragroundup(&sblock, offset));
1047 idesc.id_numfrags = sblock.fs_frag;
1048 if (dp->dp2.di_extb[j] == 0)
1050 idesc.id_blkno = dp->dp2.di_extb[j];
1051 ret = (*idesc.id_func)(&idesc);
1056 idesc.id_entryno *= btodb(sblock.fs_fsize);
1057 if (DIP(dp, di_blocks) != idesc.id_entryno) {
1058 if (!(sujrecovery && preen)) {
1059 pwarn("INCORRECT BLOCK COUNT I=%lu (%ju should be %ju)",
1060 (u_long)idesc.id_number,
1061 (uintmax_t)DIP(dp, di_blocks),
1062 (uintmax_t)idesc.id_entryno);
1064 printf(" (CORRECTED)\n");
1065 else if (reply("CORRECT") == 0)
1068 if (bkgrdflag == 0) {
1069 DIP_SET(dp, di_blocks, idesc.id_entryno);
1072 cmd.value = idesc.id_number;
1073 cmd.size = idesc.id_entryno - DIP(dp, di_blocks);
1075 printf("adjblkcnt ino %ju amount %lld\n",
1076 (uintmax_t)cmd.value, (long long)cmd.size);
1077 if (sysctl(adjblkcnt, MIBSIZE, 0, 0,
1078 &cmd, sizeof cmd) == -1)
1079 rwerror("ADJUST INODE BLOCK COUNT", cmd.value);
1091 * Flush old contents in case they have been updated.
1093 flush(fswritefd, &inobuf);
1094 if (inobuf.b_un.b_buf != NULL)
1095 free((char *)inobuf.b_un.b_buf);
1096 inobuf.b_un.b_buf = NULL;
1097 firstinum = lastinum = 0;
1099 * Reload the snapshot inodes in case any of them changed.
1101 for (i = 0; i < snapcnt; i++) {
1102 bp = snaplist[i].i_bp;
1103 bp->b_errs = blread(fsreadfd, bp->b_un.b_buf, bp->b_bno,
1109 * Routines to maintain information about directory inodes.
1110 * This is built during the first pass and used during the
1111 * second and third passes.
1113 * Enter inodes into the cache.
1116 cacheino(union dinode *dp, ino_t inumber)
1118 struct inoinfo *inp;
1121 if (getinoinfo(inumber) != NULL)
1122 pfatal("cacheino: duplicate entry for ino %jd\n",
1124 if (howmany(DIP(dp, di_size), sblock.fs_bsize) > UFS_NDADDR)
1125 blks = UFS_NDADDR + UFS_NIADDR;
1126 else if (DIP(dp, di_size) > 0)
1127 blks = howmany(DIP(dp, di_size), sblock.fs_bsize);
1130 inp = (struct inoinfo *)
1131 Malloc(sizeof(*inp) + (blks - 1) * sizeof(ufs2_daddr_t));
1133 errx(EEXIT, "cannot increase directory list");
1134 SLIST_INSERT_HEAD(&inphash[inumber % dirhash], inp, i_hash);
1136 inp->i_parent = inumber == UFS_ROOTINO ? UFS_ROOTINO : (ino_t)0;
1137 inp->i_dotdot = (ino_t)0;
1138 inp->i_number = inumber;
1139 inp->i_isize = DIP(dp, di_size);
1140 inp->i_depth = DIP(dp, di_dirdepth);
1141 inp->i_numblks = blks;
1142 for (i = 0; i < MIN(blks, UFS_NDADDR); i++)
1143 inp->i_blks[i] = DIP(dp, di_db[i]);
1144 if (blks > UFS_NDADDR)
1145 for (i = 0; i < UFS_NIADDR; i++)
1146 inp->i_blks[UFS_NDADDR + i] = DIP(dp, di_ib[i]);
1147 if (inplast == listmax) {
1149 inpsort = (struct inoinfo **)reallocarray((char *)inpsort,
1150 listmax, sizeof(struct inoinfo *));
1151 if (inpsort == NULL)
1152 errx(EEXIT, "cannot increase directory list");
1154 inpsort[inplast++] = inp;
1159 * Look up an inode cache structure.
1162 getinoinfo(ino_t inumber)
1164 struct inoinfo *inp;
1166 SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) {
1167 if (inp->i_number != inumber)
1175 * Remove an entry from the inode cache and disk-order sorted list.
1176 * Return 0 on success and 1 on failure.
1179 removecachedino(ino_t inumber)
1181 struct inoinfo *inp, **inpp;
1185 SLIST_FOREACH(inp, &inphash[inumber % dirhash], i_hash) {
1186 if (inp->i_number != inumber)
1188 SLIST_REMOVE(&inphash[inumber % dirhash], inp, inoinfo, i_hash);
1189 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--) {
1192 *inpp = inpsort[inplast - 1];
1200 pfatal("removecachedino: entry for ino %jd not found on %s list\n",
1201 (intmax_t)inumber, listtype);
1206 * Clean up all the inode cache structure.
1211 struct inoinfo **inpp;
1213 if (inphash == NULL)
1215 for (inpp = &inpsort[inplast - 1]; inpp >= inpsort; inpp--)
1216 free((char *)(*inpp));
1217 free((char *)inphash);
1219 free((char *)inpsort);
1224 inodirty(struct inode *ip)
1227 if (sblock.fs_magic == FS_UFS2_MAGIC)
1228 ffs_update_dinode_ckhash(&sblock,
1229 (struct ufs2_dinode *)ip->i_dp);
1234 clri(struct inodesc *idesc, const char *type, int flag)
1239 ginode(idesc->id_number, &ip);
1242 pwarn("%s %s", type,
1243 (DIP(dp, di_mode) & IFMT) == IFDIR ? "DIR" : "FILE");
1247 if (preen || reply("CLEAR") == 1) {
1249 printf(" (CLEARED)\n");
1251 if (bkgrdflag == 0) {
1252 if (idesc->id_type == SNAP) {
1253 snapremove(idesc->id_number);
1254 idesc->id_type = ADDR;
1256 (void)ckinode(dp, idesc);
1257 inoinfo(idesc->id_number)->ino_state = USTATE;
1261 cmd.value = idesc->id_number;
1262 cmd.size = -DIP(dp, di_nlink);
1264 printf("adjrefcnt ino %ld amt %lld\n",
1265 (long)cmd.value, (long long)cmd.size);
1266 if (sysctl(adjrefcnt, MIBSIZE, 0, 0,
1267 &cmd, sizeof cmd) == -1)
1268 rwerror("ADJUST INODE", cmd.value);
1275 findname(struct inodesc *idesc)
1277 struct direct *dirp = idesc->id_dirp;
1279 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
1280 idesc->id_entryno++;
1283 memmove(idesc->id_name, dirp->d_name, (size_t)dirp->d_namlen + 1);
1284 return (STOP|FOUND);
1288 findino(struct inodesc *idesc)
1290 struct direct *dirp = idesc->id_dirp;
1292 if (dirp->d_ino == 0)
1294 if (strcmp(dirp->d_name, idesc->id_name) == 0 &&
1295 dirp->d_ino >= UFS_ROOTINO && dirp->d_ino <= maxino) {
1296 idesc->id_parent = dirp->d_ino;
1297 return (STOP|FOUND);
1303 clearentry(struct inodesc *idesc)
1305 struct direct *dirp = idesc->id_dirp;
1307 if (dirp->d_ino != idesc->id_parent || idesc->id_entryno < 2) {
1308 idesc->id_entryno++;
1312 return (STOP|FOUND|ALTERED);
1316 prtinode(struct inode *ip)
1324 printf(" I=%lu ", (u_long)ip->i_number);
1325 if (ip->i_number < UFS_ROOTINO || ip->i_number > maxino)
1328 if ((pw = getpwuid((int)DIP(dp, di_uid))) != NULL)
1329 printf("%s ", pw->pw_name);
1331 printf("%u ", (unsigned)DIP(dp, di_uid));
1332 printf("MODE=%o\n", DIP(dp, di_mode));
1334 printf("%s: ", cdevname);
1335 printf("SIZE=%ju ", (uintmax_t)DIP(dp, di_size));
1336 t = DIP(dp, di_mtime);
1337 if ((p = ctime(&t)) != NULL)
1338 printf("MTIME=%12.12s %4.4s ", &p[4], &p[20]);
1342 blkerror(ino_t ino, const char *type, ufs2_daddr_t blk)
1345 pfatal("%jd %s I=%ju", (intmax_t)blk, type, (uintmax_t)ino);
1347 switch (inoinfo(ino)->ino_state) {
1351 inoinfo(ino)->ino_state = FCLEAR;
1356 inoinfo(ino)->ino_state = DCLEAR;
1364 errx(EEXIT, "BAD STATE %d TO BLKERR", inoinfo(ino)->ino_state);
1370 * allocate an unused inode
1373 allocino(ino_t request, int type)
1378 struct bufarea *cgbp;
1384 request = UFS_ROOTINO;
1386 } else if (inoinfo(request)->ino_state != USTATE)
1389 for (ino = request; ino < maxino; ino++)
1390 if (inoinfo(ino)->ino_state == USTATE)
1394 cg = ino_to_cg(&sblock, ino);
1395 cgbp = cglookup(cg);
1396 cgp = cgbp->b_un.b_cg;
1397 if (!check_cgmagic(cg, cgbp)) {
1400 request = (cg + 1) * sblock.fs_ipg;
1403 setbit(cg_inosused(cgp), ino % sblock.fs_ipg);
1404 cgp->cg_cs.cs_nifree--;
1405 switch (type & IFMT) {
1407 inoinfo(ino)->ino_state = DSTATE;
1408 cgp->cg_cs.cs_ndir++;
1412 inoinfo(ino)->ino_state = FSTATE;
1420 DIP_SET(dp, di_db[0], allocblk(ino_to_cg(&sblock, ino), (long)1,
1421 std_checkblkavail));
1422 if (DIP(dp, di_db[0]) == 0) {
1423 inoinfo(ino)->ino_state = USTATE;
1427 DIP_SET(dp, di_mode, type);
1428 DIP_SET(dp, di_flags, 0);
1429 DIP_SET(dp, di_atime, time(NULL));
1430 DIP_SET(dp, di_ctime, DIP(dp, di_atime));
1431 DIP_SET(dp, di_mtime, DIP(dp, di_ctime));
1432 DIP_SET(dp, di_mtimensec, 0);
1433 DIP_SET(dp, di_ctimensec, 0);
1434 DIP_SET(dp, di_atimensec, 0);
1435 DIP_SET(dp, di_size, sblock.fs_fsize);
1436 DIP_SET(dp, di_blocks, btodb(sblock.fs_fsize));
1440 inoinfo(ino)->ino_type = IFTODT(type);
1445 * deallocate an inode
1450 struct inodesc idesc;
1454 memset(&idesc, 0, sizeof(struct inodesc));
1455 idesc.id_type = ADDR;
1456 idesc.id_func = freeblock;
1457 idesc.id_number = ino;
1460 (void)ckinode(dp, &idesc);
1464 inoinfo(ino)->ino_state = USTATE;