Home | History | Annotate | Line # | Download | only in ffs
ffs_snapshot.c revision 1.72
      1 /*	$NetBSD: ffs_snapshot.c,v 1.72 2008/07/30 10:09:30 hannken Exp $	*/
      2 
      3 /*
      4  * Copyright 2000 Marshall Kirk McKusick. All Rights Reserved.
      5  *
      6  * Further information about snapshots can be obtained from:
      7  *
      8  *	Marshall Kirk McKusick		http://www.mckusick.com/softdep/
      9  *	1614 Oxford Street		mckusick (at) mckusick.com
     10  *	Berkeley, CA 94709-1608		+1-510-843-9542
     11  *	USA
     12  *
     13  * Redistribution and use in source and binary forms, with or without
     14  * modification, are permitted provided that the following conditions
     15  * are met:
     16  *
     17  * 1. Redistributions of source code must retain the above copyright
     18  *    notice, this list of conditions and the following disclaimer.
     19  * 2. Redistributions in binary form must reproduce the above copyright
     20  *    notice, this list of conditions and the following disclaimer in the
     21  *    documentation and/or other materials provided with the distribution.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY
     24  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     26  * DISCLAIMED.  IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR
     27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  *
     35  *	@(#)ffs_snapshot.c	8.11 (McKusick) 7/23/00
     36  *
     37  *	from FreeBSD: ffs_snapshot.c,v 1.79 2004/02/13 02:02:06 kuriyama Exp
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: ffs_snapshot.c,v 1.72 2008/07/30 10:09:30 hannken Exp $");
     42 
     43 #if defined(_KERNEL_OPT)
     44 #include "opt_ffs.h"
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/kernel.h>
     49 #include <sys/systm.h>
     50 #include <sys/conf.h>
     51 #include <sys/buf.h>
     52 #include <sys/proc.h>
     53 #include <sys/namei.h>
     54 #include <sys/sched.h>
     55 #include <sys/stat.h>
     56 #include <sys/malloc.h>
     57 #include <sys/mount.h>
     58 #include <sys/resource.h>
     59 #include <sys/resourcevar.h>
     60 #include <sys/vnode.h>
     61 #include <sys/kauth.h>
     62 #include <sys/fstrans.h>
     63 
     64 #include <miscfs/specfs/specdev.h>
     65 
     66 #include <ufs/ufs/quota.h>
     67 #include <ufs/ufs/ufsmount.h>
     68 #include <ufs/ufs/inode.h>
     69 #include <ufs/ufs/ufs_extern.h>
     70 #include <ufs/ufs/ufs_bswap.h>
     71 
     72 #include <ufs/ffs/fs.h>
     73 #include <ufs/ffs/ffs_extern.h>
     74 
     75 #include <uvm/uvm.h>
     76 
     77 /* FreeBSD -> NetBSD conversion */
     78 #define KERNCRED	lwp0.l_cred
     79 #define ufs1_daddr_t	int32_t
     80 #define ufs2_daddr_t	int64_t
     81 #define ufs_lbn_t	daddr_t
     82 #define VI_MTX(v)	(&(v)->v_interlock)
     83 #define VI_LOCK(v)	mutex_enter(&(v)->v_interlock)
     84 #define VI_UNLOCK(v)	mutex_exit(&(v)->v_interlock)
     85 #define MNT_ILOCK(v)	mutex_enter(&mntvnode_lock)
     86 #define MNT_IUNLOCK(v)	mutex_exit(&mntvnode_lock)
     87 
     88 #if !defined(FFS_NO_SNAPSHOT)
     89 static int cgaccount(int, struct vnode *, void *, int);
     90 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
     91     int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
     92     ufs_lbn_t, int), int);
     93 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
     94     ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
     95     int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
     96     ufs_lbn_t, int), int);
     97 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
     98     struct fs *, ufs_lbn_t, int);
     99 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
    100     struct fs *, ufs_lbn_t, int);
    101 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
    102     struct fs *, ufs_lbn_t, int);
    103 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
    104     int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
    105     ufs_lbn_t, int), int);
    106 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
    107     ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
    108     int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
    109     ufs_lbn_t, int), int);
    110 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
    111     struct fs *, ufs_lbn_t, int);
    112 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
    113     struct fs *, ufs_lbn_t, int);
    114 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
    115     struct fs *, ufs_lbn_t, int);
    116 #endif /* !defined(FFS_NO_SNAPSHOT) */
    117 
    118 static int ffs_copyonwrite(void *, struct buf *, bool);
    119 static int snapblkaddr(struct vnode *, daddr_t, daddr_t *);
    120 static int rwfsblk(struct vnode *, int, void *, ufs2_daddr_t);
    121 static int wrsnapblk(struct vnode *, void *, ufs2_daddr_t);
    122 static inline ufs2_daddr_t db_get(struct inode *, int);
    123 static inline void db_assign(struct inode *, int, ufs2_daddr_t);
    124 static inline ufs2_daddr_t idb_get(struct inode *, void *, int);
    125 static inline void idb_assign(struct inode *, void *, int, ufs2_daddr_t);
    126 
    127 struct snap_info {
    128 	kmutex_t si_lock;			/* Lock this snapinfo */
    129 	struct vnlock si_vnlock;		/* Snapshot vnode common lock */
    130 	TAILQ_HEAD(inodelst, inode) si_snapshots; /* List of active snapshots */
    131 	daddr_t *si_snapblklist;		/* Snapshot block hints list */
    132 	uint32_t si_gen;			/* Incremented on change */
    133 };
    134 
    135 #ifdef DEBUG
    136 static int snapdebug = 0;
    137 #endif
    138 
    139 int
    140 ffs_snapshot_init(struct ufsmount *ump)
    141 {
    142 	struct snap_info *si;
    143 
    144 	si = ump->um_snapinfo = kmem_alloc(sizeof(*si), KM_SLEEP);
    145 	if (si == NULL)
    146 		return ENOMEM;
    147 
    148 	TAILQ_INIT(&si->si_snapshots);
    149 	mutex_init(&si->si_lock, MUTEX_DEFAULT, IPL_NONE);
    150 	rw_init(&si->si_vnlock.vl_lock);
    151 	si->si_vnlock.vl_canrecurse = 1;
    152 	si->si_vnlock.vl_recursecnt = 0;
    153 	si->si_gen = 0;
    154 	si->si_snapblklist = NULL;
    155 
    156 	return 0;
    157 }
    158 
    159 void
    160 ffs_snapshot_fini(struct ufsmount *ump)
    161 {
    162 	struct snap_info *si;
    163 
    164 	si = ump->um_snapinfo;
    165 	ump->um_snapinfo = NULL;
    166 
    167 	KASSERT(TAILQ_EMPTY(&si->si_snapshots));
    168 	mutex_destroy(&si->si_lock);
    169 	rw_destroy(&si->si_vnlock.vl_lock);
    170 	KASSERT(si->si_snapblklist == NULL);
    171 	kmem_free(si, sizeof(*si));
    172 }
    173 
    174 /*
    175  * Create a snapshot file and initialize it for the filesystem.
    176  * Vnode is locked on entry and return.
    177  */
    178 int
    179 ffs_snapshot(struct mount *mp, struct vnode *vp,
    180     struct timespec *ctime)
    181 {
    182 #if defined(FFS_NO_SNAPSHOT)
    183 	return EOPNOTSUPP;
    184 }
    185 #else /* defined(FFS_NO_SNAPSHOT) */
    186 	ufs2_daddr_t numblks, blkno, *blkp, snaplistsize = 0, *snapblklist;
    187 	int error, ns, cg, snaploc;
    188 	int i, size, len, loc;
    189 	int flag = mp->mnt_flag;
    190 	struct timeval starttime;
    191 #ifdef DEBUG
    192 	struct timeval endtime;
    193 #endif
    194 	struct timespec ts;
    195 	long redo = 0;
    196 	int32_t *lp;
    197 	void *space;
    198 	void *sbbuf = NULL;
    199 	struct fs *copy_fs = NULL, *fs = VFSTOUFS(mp)->um_fs;
    200 	struct lwp *l = curlwp;
    201 	struct inode *ip, *xp;
    202 	struct buf *bp, *ibp, *nbp;
    203 	struct vattr vat;
    204 	struct vnode *xvp, *mvp, *devvp;
    205 	struct snap_info *si;
    206 
    207 	ns = UFS_FSNEEDSWAP(fs);
    208 	si = VFSTOUFS(mp)->um_snapinfo;
    209 
    210 	/*
    211 	 * Need to serialize access to snapshot code per filesystem.
    212 	 */
    213 	/*
    214 	 * If the vnode already is a snapshot, return.
    215 	 */
    216 	if (VTOI(vp)->i_flags & SF_SNAPSHOT) {
    217 		if (ctime) {
    218 			ctime->tv_sec = DIP(VTOI(vp), mtime);
    219 			ctime->tv_nsec = DIP(VTOI(vp), mtimensec);
    220 		}
    221 		return 0;
    222 	}
    223 	/*
    224 	 * Check mount, exclusive reference and owner.
    225 	 */
    226 	if (vp->v_mount != mp)
    227 		return EXDEV;
    228 	if (vp->v_usecount != 1 || vp->v_writecount != 0)
    229 		return EBUSY;
    230 	if (kauth_authorize_generic(l->l_cred, KAUTH_GENERIC_ISSUSER,
    231 	    NULL) != 0 &&
    232 	    VTOI(vp)->i_uid != kauth_cred_geteuid(l->l_cred))
    233 		return EACCES;
    234 
    235 	if (vp->v_size != 0) {
    236 		error = ffs_truncate(vp, 0, 0, NOCRED);
    237 		if (error)
    238 			return error;
    239 	}
    240 	/*
    241 	 * Assign a snapshot slot in the superblock.
    242 	 */
    243 	for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
    244 		if (fs->fs_snapinum[snaploc] == 0)
    245 			break;
    246 	if (snaploc == FSMAXSNAP)
    247 		return (ENOSPC);
    248 	ip = VTOI(vp);
    249 	devvp = ip->i_devvp;
    250 	/*
    251 	 * Write an empty list of preallocated blocks to the end of
    252 	 * the snapshot to set size to at least that of the filesystem.
    253 	 */
    254 	numblks = howmany(fs->fs_size, fs->fs_frag);
    255 	blkno = 1;
    256 	blkno = ufs_rw64(blkno, ns);
    257 	error = vn_rdwr(UIO_WRITE, vp,
    258 	    (void *)&blkno, sizeof(blkno), lblktosize(fs, (off_t)numblks),
    259 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, l->l_cred, NULL, NULL);
    260 	if (error)
    261 		goto out;
    262 	/*
    263 	 * Preallocate critical data structures so that we can copy
    264 	 * them in without further allocation after we suspend all
    265 	 * operations on the filesystem. We would like to just release
    266 	 * the allocated buffers without writing them since they will
    267 	 * be filled in below once we are ready to go, but this upsets
    268 	 * the soft update code, so we go ahead and write the new buffers.
    269 	 *
    270 	 * Allocate all indirect blocks and mark all of them as not
    271 	 * needing to be copied.
    272 	 */
    273 	for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
    274 		error = ffs_balloc(vp, lblktosize(fs, (off_t)blkno),
    275 		    fs->fs_bsize, l->l_cred, B_METAONLY, &ibp);
    276 		if (error)
    277 			goto out;
    278 		if (DOINGSOFTDEP(vp))
    279 			bawrite(ibp);
    280 		else
    281 			brelse(ibp, 0);
    282 	}
    283 	/*
    284 	 * Allocate copies for the superblock and its summary information.
    285 	 */
    286 	error = ffs_balloc(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
    287 	    0, &nbp);
    288 	if (error)
    289 		goto out;
    290 	bawrite(nbp);
    291 	blkno = fragstoblks(fs, fs->fs_csaddr);
    292 	len = howmany(fs->fs_cssize, fs->fs_bsize);
    293 	for (loc = 0; loc < len; loc++) {
    294 		error = ffs_balloc(vp, lblktosize(fs, (off_t)(blkno + loc)),
    295 		    fs->fs_bsize, KERNCRED, 0, &nbp);
    296 		if (error)
    297 			goto out;
    298 		bawrite(nbp);
    299 	}
    300 	/*
    301 	 * Copy all the cylinder group maps. Although the
    302 	 * filesystem is still active, we hope that only a few
    303 	 * cylinder groups will change between now and when we
    304 	 * suspend operations. Thus, we will be able to quickly
    305 	 * touch up the few cylinder groups that changed during
    306 	 * the suspension period.
    307 	 */
    308 	len = howmany(fs->fs_ncg, NBBY);
    309 	fs->fs_active = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
    310 	for (cg = 0; cg < fs->fs_ncg; cg++) {
    311 		if ((error = ffs_balloc(vp, lfragtosize(fs, cgtod(fs, cg)),
    312 		    fs->fs_bsize, KERNCRED, 0, &nbp)) != 0)
    313 			goto out;
    314 		error = cgaccount(cg, vp, nbp->b_data, 1);
    315 		bawrite(nbp);
    316 		if (error)
    317 			goto out;
    318 	}
    319 	/*
    320 	 * Change inode to snapshot type file.
    321 	 */
    322 	ip->i_flags |= SF_SNAPSHOT;
    323 	DIP_ASSIGN(ip, flags, ip->i_flags);
    324 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    325 	/*
    326 	 * Ensure that the snapshot is completely on disk.
    327 	 * Since we have marked it as a snapshot it is safe to
    328 	 * unlock it as no process will be allowed to write to it.
    329 	 */
    330 	if ((error = VOP_FSYNC(vp, KERNCRED, FSYNC_WAIT, 0, 0)) != 0)
    331 		goto out;
    332 	VOP_UNLOCK(vp, 0);
    333 	/*
    334 	 * All allocations are done, so we can now snapshot the system.
    335 	 *
    336 	 * Suspend operation on filesystem.
    337 	 */
    338 	if ((error = vfs_suspend(vp->v_mount, 0)) != 0) {
    339 		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    340 		goto out;
    341 	}
    342 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
    343 	getmicrotime(&starttime);
    344 	/*
    345 	 * First, copy all the cylinder group maps that have changed.
    346 	 */
    347 	for (cg = 0; cg < fs->fs_ncg; cg++) {
    348 		if (ACTIVECG_ISSET(fs, cg))
    349 			continue;
    350 		redo++;
    351 		if ((error = ffs_balloc(vp, lfragtosize(fs, cgtod(fs, cg)),
    352 		    fs->fs_bsize, KERNCRED, 0, &nbp)) != 0)
    353 			goto out1;
    354 		error = cgaccount(cg, vp, nbp->b_data, 2);
    355 		bawrite(nbp);
    356 		if (error)
    357 			goto out1;
    358 	}
    359 	/*
    360 	 * Grab a copy of the superblock and its summary information.
    361 	 * We delay writing it until the suspension is released below.
    362 	 */
    363 	sbbuf = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
    364 	loc = blkoff(fs, fs->fs_sblockloc);
    365 	if (loc > 0)
    366 		memset(sbbuf, 0, loc);
    367 	copy_fs = (struct fs *)((char *)sbbuf + loc);
    368 	bcopy(fs, copy_fs, fs->fs_sbsize);
    369 	size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
    370 	if (fs->fs_sbsize < size)
    371 		memset((char *)sbbuf + loc + fs->fs_sbsize, 0,
    372 		    size - fs->fs_sbsize);
    373 	size = blkroundup(fs, fs->fs_cssize);
    374 	if (fs->fs_contigsumsize > 0)
    375 		size += fs->fs_ncg * sizeof(int32_t);
    376 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
    377 	copy_fs->fs_csp = space;
    378 	bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
    379 	space = (char *)space + fs->fs_cssize;
    380 	loc = howmany(fs->fs_cssize, fs->fs_fsize);
    381 	i = fs->fs_frag - loc % fs->fs_frag;
    382 	len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
    383 	if (len > 0) {
    384 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
    385 		    len, KERNCRED, 0, &bp)) != 0) {
    386 			brelse(bp, 0);
    387 			free(copy_fs->fs_csp, M_UFSMNT);
    388 			goto out1;
    389 		}
    390 		bcopy(bp->b_data, space, (u_int)len);
    391 		space = (char *)space + len;
    392 		brelse(bp, BC_INVAL | BC_NOCACHE);
    393 	}
    394 	if (fs->fs_contigsumsize > 0) {
    395 		copy_fs->fs_maxcluster = lp = space;
    396 		for (i = 0; i < fs->fs_ncg; i++)
    397 			*lp++ = fs->fs_contigsumsize;
    398 	}
    399 	/*
    400 	 * We must check for active files that have been unlinked
    401 	 * (e.g., with a zero link count). We have to expunge all
    402 	 * trace of these files from the snapshot so that they are
    403 	 * not reclaimed prematurely by fsck or unnecessarily dumped.
    404 	 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
    405 	 * spec_strategy about writing on a suspended filesystem.
    406 	 * Note that we skip unlinked snapshot files as they will
    407 	 * be handled separately below.
    408 	 *
    409 	 * We also calculate the needed size for the snapshot list.
    410 	 */
    411 	snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
    412 	    FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
    413 	/* Allocate a marker vnode */
    414 	if ((mvp = vnalloc(mp)) == NULL) {
    415 		error = ENOMEM;
    416 		goto out1;
    417 	}
    418 	MNT_ILOCK(mp);
    419 	/*
    420 	 * NOTE: not using the TAILQ_FOREACH here since in this loop vgone()
    421 	 * and vclean() can be called indirectly
    422 	 */
    423 	for (xvp = TAILQ_FIRST(&mp->mnt_vnodelist); xvp; xvp = vunmark(mvp)) {
    424 		vmark(mvp, xvp);
    425 		/*
    426 		 * Make sure this vnode wasn't reclaimed in getnewvnode().
    427 		 * Start over if it has (it won't be on the list anymore).
    428 		 */
    429 		if (xvp->v_mount != mp || vismarker(xvp))
    430 			continue;
    431 		VI_LOCK(xvp);
    432 		if ((xvp->v_iflag & VI_XLOCK) ||
    433 		    xvp->v_usecount == 0 || xvp->v_type == VNON ||
    434 		    VTOI(xvp) == NULL ||
    435 		    (VTOI(xvp)->i_flags & SF_SNAPSHOT)) {
    436 			VI_UNLOCK(xvp);
    437 			continue;
    438 		}
    439 		MNT_IUNLOCK(mp);
    440 		/*
    441 		 * XXXAD should increase vnode ref count to prevent it
    442 		 * disappearing or being recycled.
    443 		 */
    444 		VI_UNLOCK(xvp);
    445 #ifdef DEBUG
    446 		if (snapdebug)
    447 			vprint("ffs_snapshot: busy vnode", xvp);
    448 #endif
    449 		if (VOP_GETATTR(xvp, &vat, l->l_cred) == 0 &&
    450 		    vat.va_nlink > 0) {
    451 			MNT_ILOCK(mp);
    452 			continue;
    453 		}
    454 		xp = VTOI(xvp);
    455 		if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
    456 			MNT_ILOCK(mp);
    457 			continue;
    458 		}
    459 		/*
    460 		 * If there is a fragment, clear it here.
    461 		 */
    462 		blkno = 0;
    463 		loc = howmany(xp->i_size, fs->fs_bsize) - 1;
    464 		if (loc < NDADDR) {
    465 			len = fragroundup(fs, blkoff(fs, xp->i_size));
    466 			if (len > 0 && len < fs->fs_bsize) {
    467 				ffs_blkfree(copy_fs, vp, db_get(xp, loc),
    468 				    len, xp->i_number);
    469 				blkno = db_get(xp, loc);
    470 				db_assign(xp, loc, 0);
    471 			}
    472 		}
    473 		snaplistsize += 1;
    474 		if (xp->i_ump->um_fstype == UFS1)
    475 			error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
    476 			    BLK_NOCOPY);
    477 		else
    478 			error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
    479 			    BLK_NOCOPY);
    480 		if (blkno)
    481 			db_assign(xp, loc, blkno);
    482 		if (!error)
    483 			error = ffs_freefile(copy_fs, vp, xp->i_number,
    484 			    xp->i_mode);
    485 		if (error) {
    486 			free(copy_fs->fs_csp, M_UFSMNT);
    487 			(void)vunmark(mvp);
    488 			goto out1;
    489 		}
    490 		MNT_ILOCK(mp);
    491 	}
    492 	MNT_IUNLOCK(mp);
    493 	vnfree(mvp);
    494 	/*
    495 	 * Acquire the snapshot lock and give up our original private lock.
    496 	 */
    497 	VI_LOCK(vp);
    498 	vp->v_vnlock = &si->si_vnlock;
    499 	vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY);
    500 	vlockmgr(&vp->v_lock, LK_RELEASE);
    501 	/*
    502 	 * If this is the first snapshot on this filesystem, then we need
    503 	 * to allocate the space for the list of preallocated snapshot blocks.
    504 	 * This list will be refined below, but this preliminary one will
    505 	 * keep us out of deadlock until the full one is ready.
    506 	 */
    507 	mutex_enter(&si->si_lock);
    508 	if ((xp = TAILQ_FIRST(&si->si_snapshots)) == NULL) {
    509 		mutex_exit(&si->si_lock);
    510 		snapblklist = malloc(
    511 		    snaplistsize * sizeof(ufs2_daddr_t), M_UFSMNT, M_WAITOK);
    512 		blkp = &snapblklist[1];
    513 		*blkp++ = lblkno(fs, fs->fs_sblockloc);
    514 		blkno = fragstoblks(fs, fs->fs_csaddr);
    515 		for (cg = 0; cg < fs->fs_ncg; cg++) {
    516 			if (fragstoblks(fs, cgtod(fs, cg)) > blkno)
    517 				break;
    518 			*blkp++ = fragstoblks(fs, cgtod(fs, cg));
    519 		}
    520 		len = howmany(fs->fs_cssize, fs->fs_bsize);
    521 		for (loc = 0; loc < len; loc++)
    522 			*blkp++ = blkno + loc;
    523 		for (; cg < fs->fs_ncg; cg++)
    524 			*blkp++ = fragstoblks(fs, cgtod(fs, cg));
    525 		snapblklist[0] = blkp - snapblklist;
    526 		mutex_enter(&si->si_lock);
    527 		if (si->si_snapblklist != NULL)
    528 			panic("ffs_snapshot: non-empty list");
    529 		si->si_snapblklist = snapblklist;
    530 	}
    531 	/*
    532 	 * Record snapshot inode. Since this is the newest snapshot,
    533 	 * it must be placed at the end of the list.
    534 	 */
    535 	fs->fs_snapinum[snaploc] = ip->i_number;
    536 	if (ip->i_nextsnap.tqe_prev != 0)
    537 		panic("ffs_snapshot: %llu already on list",
    538 		    (unsigned long long)ip->i_number);
    539 	TAILQ_INSERT_TAIL(&si->si_snapshots, ip, i_nextsnap);
    540 	if (xp == NULL)
    541 		fscow_establish(mp, ffs_copyonwrite, devvp);
    542 	si->si_gen++;
    543 	mutex_exit(&si->si_lock);
    544 	vp->v_vflag |= VV_SYSTEM;
    545 out1:
    546 	/*
    547 	 * Resume operation on filesystem.
    548 	 */
    549 	vfs_resume(vp->v_mount);
    550 	/*
    551 	 * Set the mtime to the time the snapshot has been taken.
    552 	 */
    553 	TIMEVAL_TO_TIMESPEC(&starttime, &ts);
    554 	if (ctime)
    555 		*ctime = ts;
    556 	DIP_ASSIGN(ip, mtime, ts.tv_sec);
    557 	DIP_ASSIGN(ip, mtimensec, ts.tv_nsec);
    558 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
    559 
    560 #ifdef DEBUG
    561 	if (starttime.tv_sec > 0) {
    562 		getmicrotime(&endtime);
    563 		timersub(&endtime, &starttime, &endtime);
    564 		printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
    565 		    vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
    566 		    endtime.tv_usec / 1000, redo, fs->fs_ncg);
    567 	}
    568 #endif
    569 	if (error)
    570 		goto out;
    571 	/*
    572 	 * Copy allocation information from all the snapshots in
    573 	 * this snapshot and then expunge them from its view.
    574 	 */
    575 	TAILQ_FOREACH(xp, &si->si_snapshots, i_nextsnap) {
    576 		if (xp == ip)
    577 			break;
    578 		if (xp->i_ump->um_fstype == UFS1)
    579 			error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
    580 			    BLK_SNAP);
    581 		else
    582 			error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
    583 			    BLK_SNAP);
    584 		if (error == 0 && xp->i_ffs_effnlink == 0)
    585 			error = ffs_freefile(copy_fs, vp,
    586 			    xp->i_number, xp->i_mode);
    587 		if (error) {
    588 			fs->fs_snapinum[snaploc] = 0;
    589 			goto done;
    590 		}
    591 	}
    592 	/*
    593 	 * Allocate space for the full list of preallocated snapshot blocks.
    594 	 */
    595 	snapblklist = malloc(snaplistsize * sizeof(ufs2_daddr_t),
    596 	    M_UFSMNT, M_WAITOK);
    597 	ip->i_snapblklist = &snapblklist[1];
    598 	/*
    599 	 * Expunge the blocks used by the snapshots from the set of
    600 	 * blocks marked as used in the snapshot bitmaps. Also, collect
    601 	 * the list of allocated blocks in i_snapblklist.
    602 	 */
    603 	if (ip->i_ump->um_fstype == UFS1)
    604 		error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, BLK_SNAP);
    605 	else
    606 		error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, BLK_SNAP);
    607 	if (error) {
    608 		fs->fs_snapinum[snaploc] = 0;
    609 		FREE(snapblklist, M_UFSMNT);
    610 		goto done;
    611 	}
    612 	if (snaplistsize < ip->i_snapblklist - snapblklist)
    613 		panic("ffs_snapshot: list too small");
    614 	snaplistsize = ip->i_snapblklist - snapblklist;
    615 	snapblklist[0] = snaplistsize;
    616 	ip->i_snapblklist = &snapblklist[0];
    617 	/*
    618 	 * Write out the list of allocated blocks to the end of the snapshot.
    619 	 */
    620 	for (i = 0; i < snaplistsize; i++)
    621 		snapblklist[i] = ufs_rw64(snapblklist[i], ns);
    622 	error = vn_rdwr(UIO_WRITE, vp, (void *)snapblklist,
    623 	    snaplistsize*sizeof(ufs2_daddr_t), lblktosize(fs, (off_t)numblks),
    624 	    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT, l->l_cred, NULL, NULL);
    625 	for (i = 0; i < snaplistsize; i++)
    626 		snapblklist[i] = ufs_rw64(snapblklist[i], ns);
    627 	if (error) {
    628 		fs->fs_snapinum[snaploc] = 0;
    629 		FREE(snapblklist, M_UFSMNT);
    630 		goto done;
    631 	}
    632 	/*
    633 	 * Write the superblock and its summary information
    634 	 * to the snapshot.
    635 	 */
    636 	blkno = fragstoblks(fs, fs->fs_csaddr);
    637 	len = howmany(fs->fs_cssize, fs->fs_bsize);
    638 	space = copy_fs->fs_csp;
    639 #ifdef FFS_EI
    640 	if (ns) {
    641 		ffs_sb_swap(copy_fs, copy_fs);
    642 		ffs_csum_swap(space, space, fs->fs_cssize);
    643 	}
    644 #endif
    645 	for (loc = 0; loc < len; loc++) {
    646 		error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, 0, &nbp);
    647 		if (error) {
    648 			brelse(nbp, 0);
    649 			fs->fs_snapinum[snaploc] = 0;
    650 			FREE(snapblklist, M_UFSMNT);
    651 			goto done;
    652 		}
    653 		bcopy(space, nbp->b_data, fs->fs_bsize);
    654 		space = (char *)space + fs->fs_bsize;
    655 		bawrite(nbp);
    656 	}
    657 	/*
    658 	 * As this is the newest list, it is the most inclusive, so
    659 	 * should replace the previous list. If this is the first snapshot
    660 	 * free the preliminary list.
    661 	 */
    662 	mutex_enter(&si->si_lock);
    663 	space = si->si_snapblklist;
    664 	si->si_snapblklist = snapblklist;
    665 	if (TAILQ_FIRST(&si->si_snapshots) == ip)
    666 		FREE(space, M_UFSMNT);
    667 	si->si_gen++;
    668 	mutex_exit(&si->si_lock);
    669 done:
    670 	free(copy_fs->fs_csp, M_UFSMNT);
    671 	if (!error) {
    672 		error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
    673 		    KERNCRED, 0, &nbp);
    674 		if (error) {
    675 			brelse(nbp, 0);
    676 			fs->fs_snapinum[snaploc] = 0;
    677 		}
    678 		bcopy(sbbuf, nbp->b_data, fs->fs_bsize);
    679 		bawrite(nbp);
    680 	}
    681 out:
    682 	/*
    683 	 * Invalidate and free all pages on the snapshot vnode.
    684 	 * All data has been written through the buffer cache.
    685 	 * Clean all dirty buffers now to avoid UBC inconsistencies.
    686 	 */
    687 	if (!error) {
    688 		mutex_enter(&vp->v_interlock);
    689 		error = VOP_PUTPAGES(vp, 0, 0,
    690 		    PGO_ALLPAGES|PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
    691 	}
    692 	if (!error) {
    693 		mutex_enter(&bufcache_lock);
    694 		for (bp = LIST_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) {
    695 			nbp = LIST_NEXT(bp, b_vnbufs);
    696 			if (bp->b_lblkno < 0)
    697 				continue;
    698 			KASSERT((bp->b_cflags & BC_BUSY) == 0);
    699 			bp->b_cflags |= BC_BUSY;
    700 			mutex_exit(&bufcache_lock);
    701 			if (DOINGSOFTDEP(vp)) {
    702 				bp->b_cflags |= BC_VFLUSH | BC_NOCACHE;
    703 				error = bwrite(bp);
    704 			} else {
    705 				error = rwfsblk(vp, B_WRITE, bp->b_data,
    706 				    fragstoblks(fs, dbtofsb(fs, bp->b_blkno)));
    707 				brelse(bp, BC_INVAL | BC_VFLUSH);
    708 			}
    709 			mutex_enter(&bufcache_lock);
    710 			if (error)
    711 				break;
    712 			nbp = LIST_FIRST(&vp->v_dirtyblkhd);
    713 		}
    714 		mutex_exit(&bufcache_lock);
    715 	}
    716 	if (sbbuf)
    717 		free(sbbuf, M_UFSMNT);
    718 	if (fs->fs_active != 0) {
    719 		FREE(fs->fs_active, M_DEVBUF);
    720 		fs->fs_active = 0;
    721 	}
    722 	mp->mnt_flag = flag;
    723 	if (error)
    724 		(void) ffs_truncate(vp, (off_t)0, 0, NOCRED);
    725 	else
    726 		vref(vp);
    727 	return (error);
    728 }
    729 
    730 /*
    731  * Copy a cylinder group map. All the unallocated blocks are marked
    732  * BLK_NOCOPY so that the snapshot knows that it need not copy them
    733  * if they are later written. If passno is one, then this is a first
    734  * pass, so only setting needs to be done. If passno is 2, then this
    735  * is a revision to a previous pass which must be undone as the
    736  * replacement pass is done.
    737  */
    738 static int
    739 cgaccount(int cg, struct vnode *vp, void *data, int passno)
    740 {
    741 	struct buf *bp, *ibp;
    742 	struct inode *ip;
    743 	struct cg *cgp;
    744 	struct fs *fs;
    745 	ufs2_daddr_t base, numblks;
    746 	int error, len, loc, ns, indiroff;
    747 
    748 	ip = VTOI(vp);
    749 	fs = ip->i_fs;
    750 	ns = UFS_FSNEEDSWAP(fs);
    751 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
    752 		(int)fs->fs_cgsize, KERNCRED, 0, &bp);
    753 	if (error) {
    754 		brelse(bp, 0);
    755 		return (error);
    756 	}
    757 	cgp = (struct cg *)bp->b_data;
    758 	if (!cg_chkmagic(cgp, ns)) {
    759 		brelse(bp, 0);
    760 		return (EIO);
    761 	}
    762 	ACTIVECG_SET(fs, cg);
    763 
    764 	bcopy(bp->b_data, data, fs->fs_cgsize);
    765 	brelse(bp, 0);
    766 	if (fs->fs_cgsize < fs->fs_bsize)
    767 		memset((char *)data + fs->fs_cgsize, 0,
    768 		    fs->fs_bsize - fs->fs_cgsize);
    769 	numblks = howmany(fs->fs_size, fs->fs_frag);
    770 	len = howmany(fs->fs_fpg, fs->fs_frag);
    771 	base = cg * fs->fs_fpg / fs->fs_frag;
    772 	if (base + len >= numblks)
    773 		len = numblks - base - 1;
    774 	loc = 0;
    775 	if (base < NDADDR) {
    776 		for ( ; loc < NDADDR; loc++) {
    777 			if (ffs_isblock(fs, cg_blksfree(cgp, ns), loc))
    778 				db_assign(ip, loc, BLK_NOCOPY);
    779 			else if (db_get(ip, loc) == BLK_NOCOPY) {
    780 				if (passno == 2)
    781 					db_assign(ip, loc, 0);
    782 				else if (passno == 1)
    783 					panic("ffs_snapshot: lost direct block");
    784 			}
    785 		}
    786 	}
    787 	if ((error = ffs_balloc(vp, lblktosize(fs, (off_t)(base + loc)),
    788 	    fs->fs_bsize, KERNCRED, B_METAONLY, &ibp)) != 0)
    789 		return (error);
    790 	indiroff = (base + loc - NDADDR) % NINDIR(fs);
    791 	for ( ; loc < len; loc++, indiroff++) {
    792 		if (indiroff >= NINDIR(fs)) {
    793 			bawrite(ibp);
    794 			if ((error = ffs_balloc(vp,
    795 			    lblktosize(fs, (off_t)(base + loc)),
    796 			    fs->fs_bsize, KERNCRED, B_METAONLY, &ibp)) != 0)
    797 				return (error);
    798 			indiroff = 0;
    799 		}
    800 		if (ffs_isblock(fs, cg_blksfree(cgp, ns), loc))
    801 			idb_assign(ip, ibp->b_data, indiroff, BLK_NOCOPY);
    802 		else if (idb_get(ip, ibp->b_data, indiroff) == BLK_NOCOPY) {
    803 			if (passno == 2)
    804 				idb_assign(ip, ibp->b_data, indiroff, 0);
    805 			else if (passno == 1)
    806 				panic("ffs_snapshot: lost indirect block");
    807 		}
    808 	}
    809 	bdwrite(ibp);
    810 	return (0);
    811 }
    812 
    813 /*
    814  * Before expunging a snapshot inode, note all the
    815  * blocks that it claims with BLK_SNAP so that fsck will
    816  * be able to account for those blocks properly and so
    817  * that this snapshot knows that it need not copy them
    818  * if the other snapshot holding them is freed. This code
    819  * is reproduced once each for UFS1 and UFS2.
    820  */
    821 static int
    822 expunge_ufs1(struct vnode *snapvp, struct inode *cancelip, struct fs *fs,
    823     int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
    824 		    struct fs *, ufs_lbn_t, int),
    825     int expungetype)
    826 {
    827 	int i, error, ns;
    828 	ufs_lbn_t lbn, rlbn;
    829 	ufs2_daddr_t len, blkno, numblks, blksperindir;
    830 	struct ufs1_dinode *dip;
    831 	struct buf *bp;
    832 
    833 	ns = UFS_FSNEEDSWAP(fs);
    834 	/*
    835 	 * Prepare to expunge the inode. If its inode block has not
    836 	 * yet been copied, then allocate and fill the copy.
    837 	 */
    838 	lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
    839 	error = snapblkaddr(snapvp, lbn, &blkno);
    840 	if (error)
    841 		return error;
    842 	if (blkno != 0) {
    843 		error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED,
    844 		    B_MODIFY, &bp);
    845 	} else {
    846 		error = ffs_balloc(snapvp, lblktosize(fs, (off_t)lbn),
    847 		    fs->fs_bsize, KERNCRED, 0, &bp);
    848 		if (! error)
    849 			error = rwfsblk(snapvp, B_READ, bp->b_data, lbn);
    850 	}
    851 	if (error)
    852 		return error;
    853 	/*
    854 	 * Set a snapshot inode to be a zero length file, regular files
    855 	 * or unlinked snapshots to be completely unallocated.
    856 	 */
    857 	dip = (struct ufs1_dinode *)bp->b_data +
    858 	    ino_to_fsbo(fs, cancelip->i_number);
    859 	if (expungetype == BLK_NOCOPY || cancelip->i_ffs_effnlink == 0)
    860 		dip->di_mode = 0;
    861 	dip->di_size = 0;
    862 	dip->di_blocks = 0;
    863 	dip->di_flags =
    864 	    ufs_rw32(ufs_rw32(dip->di_flags, ns) & ~SF_SNAPSHOT, ns);
    865 	bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
    866 	bdwrite(bp);
    867 	/*
    868 	 * Now go through and expunge all the blocks in the file
    869 	 * using the function requested.
    870 	 */
    871 	numblks = howmany(cancelip->i_size, fs->fs_bsize);
    872 	if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs1_db[0],
    873 	    &cancelip->i_ffs1_db[NDADDR], fs, 0, expungetype)))
    874 		return (error);
    875 	if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs1_ib[0],
    876 	    &cancelip->i_ffs1_ib[NIADDR], fs, -1, expungetype)))
    877 		return (error);
    878 	blksperindir = 1;
    879 	lbn = -NDADDR;
    880 	len = numblks - NDADDR;
    881 	rlbn = NDADDR;
    882 	for (i = 0; len > 0 && i < NIADDR; i++) {
    883 		error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
    884 		    ufs_rw32(cancelip->i_ffs1_ib[i], ns), lbn, rlbn, len,
    885 		    blksperindir, fs, acctfunc, expungetype);
    886 		if (error)
    887 			return (error);
    888 		blksperindir *= NINDIR(fs);
    889 		lbn -= blksperindir + 1;
    890 		len -= blksperindir;
    891 		rlbn += blksperindir;
    892 	}
    893 	return (0);
    894 }
    895 
    896 /*
    897  * Descend an indirect block chain for vnode cancelvp accounting for all
    898  * its indirect blocks in snapvp.
    899  */
    900 static int
    901 indiracct_ufs1(struct vnode *snapvp, struct vnode *cancelvp, int level,
    902     ufs1_daddr_t blkno, ufs_lbn_t lbn, ufs_lbn_t rlbn, ufs_lbn_t remblks,
    903     ufs_lbn_t blksperindir, struct fs *fs,
    904     int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
    905 		    struct fs *, ufs_lbn_t, int),
    906     int expungetype)
    907 {
    908 	int error, ns, num, i;
    909 	ufs_lbn_t subblksperindir;
    910 	struct indir indirs[NIADDR + 2];
    911 	ufs1_daddr_t last, *bap;
    912 	struct buf *bp;
    913 
    914 	ns = UFS_FSNEEDSWAP(fs);
    915 
    916 	if (blkno == 0) {
    917 		if (expungetype == BLK_NOCOPY)
    918 			return (0);
    919 		panic("indiracct_ufs1: missing indir");
    920 	}
    921 	if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
    922 		return (error);
    923 	if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
    924 		panic("indiracct_ufs1: botched params");
    925 	/*
    926 	 * We have to expand bread here since it will deadlock looking
    927 	 * up the block number for any blocks that are not in the cache.
    928 	 */
    929 	error = ffs_getblk(cancelvp, lbn, fsbtodb(fs, blkno), fs->fs_bsize,
    930 	    false, &bp);
    931 	if (error)
    932 		return error;
    933 	if ((bp->b_oflags & (BO_DONE | BO_DELWRI)) == 0 && (error =
    934 	    rwfsblk(bp->b_vp, B_READ, bp->b_data, fragstoblks(fs, blkno)))) {
    935 		brelse(bp, 0);
    936 		return (error);
    937 	}
    938 	/*
    939 	 * Account for the block pointers in this indirect block.
    940 	 */
    941 	last = howmany(remblks, blksperindir);
    942 	if (last > NINDIR(fs))
    943 		last = NINDIR(fs);
    944 	bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
    945 	bcopy(bp->b_data, (void *)bap, fs->fs_bsize);
    946 	brelse(bp, 0);
    947 	error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
    948 	    level == 0 ? rlbn : -1, expungetype);
    949 	if (error || level == 0)
    950 		goto out;
    951 	/*
    952 	 * Account for the block pointers in each of the indirect blocks
    953 	 * in the levels below us.
    954 	 */
    955 	subblksperindir = blksperindir / NINDIR(fs);
    956 	for (lbn++, level--, i = 0; i < last; i++) {
    957 		error = indiracct_ufs1(snapvp, cancelvp, level,
    958 		    ufs_rw32(bap[i], ns), lbn, rlbn, remblks, subblksperindir,
    959 		    fs, acctfunc, expungetype);
    960 		if (error)
    961 			goto out;
    962 		rlbn += blksperindir;
    963 		lbn -= blksperindir;
    964 		remblks -= blksperindir;
    965 	}
    966 out:
    967 	FREE(bap, M_DEVBUF);
    968 	return (error);
    969 }
    970 
    971 /*
    972  * Do both snap accounting and map accounting.
    973  */
    974 static int
    975 fullacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
    976     struct fs *fs, ufs_lbn_t lblkno,
    977     int exptype /* BLK_SNAP or BLK_NOCOPY */)
    978 {
    979 	int error;
    980 
    981 	if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
    982 		return (error);
    983 	return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
    984 }
    985 
    986 /*
    987  * Identify a set of blocks allocated in a snapshot inode.
    988  */
    989 static int
    990 snapacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
    991     struct fs *fs, ufs_lbn_t lblkno,
    992     int expungetype /* BLK_SNAP or BLK_NOCOPY */)
    993 {
    994 	struct inode *ip = VTOI(vp);
    995 	ufs1_daddr_t blkno, *blkp;
    996 	ufs_lbn_t lbn;
    997 	struct buf *ibp;
    998 	int error, ns;
    999 
   1000 	ns = UFS_FSNEEDSWAP(fs);
   1001 
   1002 	for ( ; oldblkp < lastblkp; oldblkp++) {
   1003 		blkno = ufs_rw32(*oldblkp, ns);
   1004 		if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
   1005 			continue;
   1006 		lbn = fragstoblks(fs, blkno);
   1007 		if (lbn < NDADDR) {
   1008 			blkp = &ip->i_ffs1_db[lbn];
   1009 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1010 		} else {
   1011 			error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
   1012 			    fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
   1013 			if (error)
   1014 				return (error);
   1015 			blkp = &((ufs1_daddr_t *)(ibp->b_data))
   1016 			    [(lbn - NDADDR) % NINDIR(fs)];
   1017 		}
   1018 		/*
   1019 		 * If we are expunging a snapshot vnode and we
   1020 		 * find a block marked BLK_NOCOPY, then it is
   1021 		 * one that has been allocated to this snapshot after
   1022 		 * we took our current snapshot and can be ignored.
   1023 		 */
   1024 		blkno = ufs_rw32(*blkp, ns);
   1025 		if (expungetype == BLK_SNAP && blkno == BLK_NOCOPY) {
   1026 			if (lbn >= NDADDR)
   1027 				brelse(ibp, 0);
   1028 		} else {
   1029 			if (blkno != 0)
   1030 				panic("snapacct_ufs1: bad block");
   1031 			*blkp = ufs_rw32(expungetype, ns);
   1032 			if (lbn >= NDADDR)
   1033 				bdwrite(ibp);
   1034 		}
   1035 	}
   1036 	return (0);
   1037 }
   1038 
   1039 /*
   1040  * Account for a set of blocks allocated in a snapshot inode.
   1041  */
   1042 static int
   1043 mapacct_ufs1(struct vnode *vp, ufs1_daddr_t *oldblkp, ufs1_daddr_t *lastblkp,
   1044     struct fs *fs, ufs_lbn_t lblkno, int expungetype)
   1045 {
   1046 	ufs1_daddr_t blkno;
   1047 	struct inode *ip;
   1048 	ino_t inum;
   1049 	int acctit, ns;
   1050 
   1051 	ns = UFS_FSNEEDSWAP(fs);
   1052 	ip = VTOI(vp);
   1053 	inum = ip->i_number;
   1054 	if (lblkno == -1)
   1055 		acctit = 0;
   1056 	else
   1057 		acctit = 1;
   1058 	for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
   1059 		blkno = ufs_rw32(*oldblkp, ns);
   1060 		if (blkno == 0 || blkno == BLK_NOCOPY)
   1061 			continue;
   1062 		if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
   1063 			*ip->i_snapblklist++ = lblkno;
   1064 		if (blkno == BLK_SNAP)
   1065 			blkno = blkstofrags(fs, lblkno);
   1066 		ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
   1067 	}
   1068 	return (0);
   1069 }
   1070 
   1071 /*
   1072  * Before expunging a snapshot inode, note all the
   1073  * blocks that it claims with BLK_SNAP so that fsck will
   1074  * be able to account for those blocks properly and so
   1075  * that this snapshot knows that it need not copy them
   1076  * if the other snapshot holding them is freed. This code
   1077  * is reproduced once each for UFS1 and UFS2.
   1078  */
   1079 static int
   1080 expunge_ufs2(struct vnode *snapvp, struct inode *cancelip, struct fs *fs,
   1081     int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
   1082 		    struct fs *, ufs_lbn_t, int),
   1083     int expungetype)
   1084 {
   1085 	int i, error, ns;
   1086 	ufs_lbn_t lbn, rlbn;
   1087 	ufs2_daddr_t len, blkno, numblks, blksperindir;
   1088 	struct ufs2_dinode *dip;
   1089 	struct buf *bp;
   1090 
   1091 	ns = UFS_FSNEEDSWAP(fs);
   1092 	/*
   1093 	 * Prepare to expunge the inode. If its inode block has not
   1094 	 * yet been copied, then allocate and fill the copy.
   1095 	 */
   1096 	lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
   1097 	error = snapblkaddr(snapvp, lbn, &blkno);
   1098 	if (error)
   1099 		return error;
   1100 	if (blkno != 0) {
   1101 		error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED,
   1102 		    B_MODIFY, &bp);
   1103 	} else {
   1104 		error = ffs_balloc(snapvp, lblktosize(fs, (off_t)lbn),
   1105 		    fs->fs_bsize, KERNCRED, 0, &bp);
   1106 		if (! error)
   1107 			error = rwfsblk(snapvp, B_READ, bp->b_data, lbn);
   1108 		}
   1109 	if (error)
   1110 		return error;
   1111 	/*
   1112 	 * Set a snapshot inode to be a zero length file, regular files
   1113 	 * or unlinked snapshots to be completely unallocated.
   1114 	 */
   1115 	dip = (struct ufs2_dinode *)bp->b_data +
   1116 	    ino_to_fsbo(fs, cancelip->i_number);
   1117 	if (expungetype == BLK_NOCOPY || cancelip->i_ffs_effnlink == 0)
   1118 		dip->di_mode = 0;
   1119 	dip->di_size = 0;
   1120 	dip->di_blocks = 0;
   1121 	dip->di_flags =
   1122 	    ufs_rw32(ufs_rw32(dip->di_flags, ns) & ~SF_SNAPSHOT, ns);
   1123 	bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
   1124 	bdwrite(bp);
   1125 	/*
   1126 	 * Now go through and expunge all the blocks in the file
   1127 	 * using the function requested.
   1128 	 */
   1129 	numblks = howmany(cancelip->i_size, fs->fs_bsize);
   1130 	if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs2_db[0],
   1131 	    &cancelip->i_ffs2_db[NDADDR], fs, 0, expungetype)))
   1132 		return (error);
   1133 	if ((error = (*acctfunc)(snapvp, &cancelip->i_ffs2_ib[0],
   1134 	    &cancelip->i_ffs2_ib[NIADDR], fs, -1, expungetype)))
   1135 		return (error);
   1136 	blksperindir = 1;
   1137 	lbn = -NDADDR;
   1138 	len = numblks - NDADDR;
   1139 	rlbn = NDADDR;
   1140 	for (i = 0; len > 0 && i < NIADDR; i++) {
   1141 		error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
   1142 		    ufs_rw64(cancelip->i_ffs2_ib[i], ns), lbn, rlbn, len,
   1143 		    blksperindir, fs, acctfunc, expungetype);
   1144 		if (error)
   1145 			return (error);
   1146 		blksperindir *= NINDIR(fs);
   1147 		lbn -= blksperindir + 1;
   1148 		len -= blksperindir;
   1149 		rlbn += blksperindir;
   1150 	}
   1151 	return (0);
   1152 }
   1153 
   1154 /*
   1155  * Descend an indirect block chain for vnode cancelvp accounting for all
   1156  * its indirect blocks in snapvp.
   1157  */
   1158 static int
   1159 indiracct_ufs2(struct vnode *snapvp, struct vnode *cancelvp, int level,
   1160     ufs2_daddr_t blkno, ufs_lbn_t lbn, ufs_lbn_t rlbn, ufs_lbn_t remblks,
   1161     ufs_lbn_t blksperindir, struct fs *fs,
   1162     int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
   1163 		    struct fs *, ufs_lbn_t, int),
   1164     int expungetype)
   1165 {
   1166 	int error, ns, num, i;
   1167 	ufs_lbn_t subblksperindir;
   1168 	struct indir indirs[NIADDR + 2];
   1169 	ufs2_daddr_t last, *bap;
   1170 	struct buf *bp;
   1171 
   1172 	ns = UFS_FSNEEDSWAP(fs);
   1173 
   1174 	if (blkno == 0) {
   1175 		if (expungetype == BLK_NOCOPY)
   1176 			return (0);
   1177 		panic("indiracct_ufs2: missing indir");
   1178 	}
   1179 	if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
   1180 		return (error);
   1181 	if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
   1182 		panic("indiracct_ufs2: botched params");
   1183 	/*
   1184 	 * We have to expand bread here since it will deadlock looking
   1185 	 * up the block number for any blocks that are not in the cache.
   1186 	 */
   1187 	error = ffs_getblk(cancelvp, lbn, fsbtodb(fs, blkno), fs->fs_bsize,
   1188 	    false, &bp);
   1189 	if (error)
   1190 		return error;
   1191 	if ((bp->b_oflags & (BO_DONE | BO_DELWRI)) == 0 && (error =
   1192 	    rwfsblk(bp->b_vp, B_READ, bp->b_data, fragstoblks(fs, blkno)))) {
   1193 		brelse(bp, 0);
   1194 		return (error);
   1195 	}
   1196 	/*
   1197 	 * Account for the block pointers in this indirect block.
   1198 	 */
   1199 	last = howmany(remblks, blksperindir);
   1200 	if (last > NINDIR(fs))
   1201 		last = NINDIR(fs);
   1202 	bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
   1203 	bcopy(bp->b_data, (void *)bap, fs->fs_bsize);
   1204 	brelse(bp, 0);
   1205 	error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
   1206 	    level == 0 ? rlbn : -1, expungetype);
   1207 	if (error || level == 0)
   1208 		goto out;
   1209 	/*
   1210 	 * Account for the block pointers in each of the indirect blocks
   1211 	 * in the levels below us.
   1212 	 */
   1213 	subblksperindir = blksperindir / NINDIR(fs);
   1214 	for (lbn++, level--, i = 0; i < last; i++) {
   1215 		error = indiracct_ufs2(snapvp, cancelvp, level,
   1216 		    ufs_rw64(bap[i], ns), lbn, rlbn, remblks, subblksperindir,
   1217 		    fs, acctfunc, expungetype);
   1218 		if (error)
   1219 			goto out;
   1220 		rlbn += blksperindir;
   1221 		lbn -= blksperindir;
   1222 		remblks -= blksperindir;
   1223 	}
   1224 out:
   1225 	FREE(bap, M_DEVBUF);
   1226 	return (error);
   1227 }
   1228 
   1229 /*
   1230  * Do both snap accounting and map accounting.
   1231  */
   1232 static int
   1233 fullacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
   1234     struct fs *fs, ufs_lbn_t lblkno,
   1235     int exptype /* BLK_SNAP or BLK_NOCOPY */)
   1236 {
   1237 	int error;
   1238 
   1239 	if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
   1240 		return (error);
   1241 	return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
   1242 }
   1243 
   1244 /*
   1245  * Identify a set of blocks allocated in a snapshot inode.
   1246  */
   1247 static int
   1248 snapacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
   1249     struct fs *fs, ufs_lbn_t lblkno,
   1250     int expungetype /* BLK_SNAP or BLK_NOCOPY */)
   1251 {
   1252 	struct inode *ip = VTOI(vp);
   1253 	ufs2_daddr_t blkno, *blkp;
   1254 	ufs_lbn_t lbn;
   1255 	struct buf *ibp;
   1256 	int error, ns;
   1257 
   1258 	ns = UFS_FSNEEDSWAP(fs);
   1259 
   1260 	for ( ; oldblkp < lastblkp; oldblkp++) {
   1261 		blkno = ufs_rw64(*oldblkp, ns);
   1262 		if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
   1263 			continue;
   1264 		lbn = fragstoblks(fs, blkno);
   1265 		if (lbn < NDADDR) {
   1266 			blkp = &ip->i_ffs2_db[lbn];
   1267 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1268 		} else {
   1269 			error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
   1270 			    fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
   1271 			if (error)
   1272 				return (error);
   1273 			blkp = &((ufs2_daddr_t *)(ibp->b_data))
   1274 			    [(lbn - NDADDR) % NINDIR(fs)];
   1275 		}
   1276 		/*
   1277 		 * If we are expunging a snapshot vnode and we
   1278 		 * find a block marked BLK_NOCOPY, then it is
   1279 		 * one that has been allocated to this snapshot after
   1280 		 * we took our current snapshot and can be ignored.
   1281 		 */
   1282 		blkno = ufs_rw64(*blkp, ns);
   1283 		if (expungetype == BLK_SNAP && blkno == BLK_NOCOPY) {
   1284 			if (lbn >= NDADDR)
   1285 				brelse(ibp, 0);
   1286 		} else {
   1287 			if (blkno != 0)
   1288 				panic("snapacct_ufs2: bad block");
   1289 			*blkp = ufs_rw64(expungetype, ns);
   1290 			if (lbn >= NDADDR)
   1291 				bdwrite(ibp);
   1292 		}
   1293 	}
   1294 	return (0);
   1295 }
   1296 
   1297 /*
   1298  * Account for a set of blocks allocated in a snapshot inode.
   1299  */
   1300 static int
   1301 mapacct_ufs2(struct vnode *vp, ufs2_daddr_t *oldblkp, ufs2_daddr_t *lastblkp,
   1302     struct fs *fs, ufs_lbn_t lblkno, int expungetype)
   1303 {
   1304 	ufs2_daddr_t blkno;
   1305 	struct inode *ip;
   1306 	ino_t inum;
   1307 	int acctit, ns;
   1308 
   1309 	ns = UFS_FSNEEDSWAP(fs);
   1310 	ip = VTOI(vp);
   1311 	inum = ip->i_number;
   1312 	if (lblkno == -1)
   1313 		acctit = 0;
   1314 	else
   1315 		acctit = 1;
   1316 	for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
   1317 		blkno = ufs_rw64(*oldblkp, ns);
   1318 		if (blkno == 0 || blkno == BLK_NOCOPY)
   1319 			continue;
   1320 		if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
   1321 			*ip->i_snapblklist++ = lblkno;
   1322 		if (blkno == BLK_SNAP)
   1323 			blkno = blkstofrags(fs, lblkno);
   1324 		ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
   1325 	}
   1326 	return (0);
   1327 }
   1328 #endif /* defined(FFS_NO_SNAPSHOT) */
   1329 
   1330 /*
   1331  * Decrement extra reference on snapshot when last name is removed.
   1332  * It will not be freed until the last open reference goes away.
   1333  */
   1334 void
   1335 ffs_snapgone(struct inode *ip)
   1336 {
   1337 	struct mount *mp = ip->i_devvp->v_specmountpoint;
   1338 	struct inode *xp;
   1339 	struct fs *fs;
   1340 	struct snap_info *si;
   1341 	int snaploc;
   1342 
   1343 	si = VFSTOUFS(mp)->um_snapinfo;
   1344 
   1345 	/*
   1346 	 * Find snapshot in incore list.
   1347 	 */
   1348 	mutex_enter(&si->si_lock);
   1349 	TAILQ_FOREACH(xp, &si->si_snapshots, i_nextsnap)
   1350 		if (xp == ip)
   1351 			break;
   1352 	mutex_exit(&si->si_lock);
   1353 	if (xp != NULL)
   1354 		vrele(ITOV(ip));
   1355 #ifdef DEBUG
   1356 	else if (snapdebug)
   1357 		printf("ffs_snapgone: lost snapshot vnode %llu\n",
   1358 		    (unsigned long long)ip->i_number);
   1359 #endif
   1360 	/*
   1361 	 * Delete snapshot inode from superblock. Keep list dense.
   1362 	 */
   1363 	mutex_enter(&si->si_lock);
   1364 	fs = ip->i_fs;
   1365 	for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
   1366 		if (fs->fs_snapinum[snaploc] == ip->i_number)
   1367 			break;
   1368 	if (snaploc < FSMAXSNAP) {
   1369 		for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
   1370 			if (fs->fs_snapinum[snaploc] == 0)
   1371 				break;
   1372 			fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
   1373 		}
   1374 		fs->fs_snapinum[snaploc - 1] = 0;
   1375 	}
   1376 	si->si_gen++;
   1377 	mutex_exit(&si->si_lock);
   1378 }
   1379 
   1380 /*
   1381  * Prepare a snapshot file for being removed.
   1382  */
   1383 void
   1384 ffs_snapremove(struct vnode *vp)
   1385 {
   1386 	struct inode *ip = VTOI(vp), *xp;
   1387 	struct vnode *devvp = ip->i_devvp;
   1388 	struct fs *fs = ip->i_fs;
   1389 	struct mount *mp = devvp->v_specmountpoint;
   1390 	struct vnlock *lkp;
   1391 	struct buf *ibp;
   1392 	struct snap_info *si;
   1393 	ufs2_daddr_t numblks, blkno, dblk;
   1394 	int error, ns, loc, last;
   1395 
   1396 	si = VFSTOUFS(mp)->um_snapinfo;
   1397 	ns = UFS_FSNEEDSWAP(fs);
   1398 	/*
   1399 	 * If active, delete from incore list (this snapshot may
   1400 	 * already have been in the process of being deleted, so
   1401 	 * would not have been active).
   1402 	 *
   1403 	 * Clear copy-on-write flag if last snapshot.
   1404 	 */
   1405 	if (ip->i_nextsnap.tqe_prev != 0) {
   1406 		mutex_enter(&si->si_lock);
   1407 		vlockmgr(&vp->v_lock, LK_EXCLUSIVE);
   1408 		TAILQ_REMOVE(&si->si_snapshots, ip, i_nextsnap);
   1409 		ip->i_nextsnap.tqe_prev = 0;
   1410 		lkp = vp->v_vnlock;
   1411 		KASSERT(lkp == &si->si_vnlock);
   1412 		vp->v_vnlock = &vp->v_lock;
   1413 		vlockmgr(lkp, LK_RELEASE);
   1414 		if (TAILQ_FIRST(&si->si_snapshots) != 0) {
   1415 			/* Roll back the list of preallocated blocks. */
   1416 			xp = TAILQ_LAST(&si->si_snapshots, inodelst);
   1417 			si->si_snapblklist = xp->i_snapblklist;
   1418 		} else {
   1419 			si->si_snapblklist = 0;
   1420 			si->si_gen++;
   1421 			mutex_exit(&si->si_lock);
   1422 			fscow_disestablish(mp, ffs_copyonwrite, devvp);
   1423 			mutex_enter(&si->si_lock);
   1424 		}
   1425 		si->si_gen++;
   1426 		mutex_exit(&si->si_lock);
   1427 		FREE(ip->i_snapblklist, M_UFSMNT);
   1428 		ip->i_snapblklist = NULL;
   1429 	}
   1430 	/*
   1431 	 * Clear all BLK_NOCOPY fields. Pass any block claims to other
   1432 	 * snapshots that want them (see ffs_snapblkfree below).
   1433 	 */
   1434 	for (blkno = 1; blkno < NDADDR; blkno++) {
   1435 		dblk = db_get(ip, blkno);
   1436 		if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
   1437 			db_assign(ip, blkno, 0);
   1438 		else if ((dblk == blkstofrags(fs, blkno) &&
   1439 		     ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
   1440 		     ip->i_number))) {
   1441 			DIP_ADD(ip, blocks, -btodb(fs->fs_bsize));
   1442 			db_assign(ip, blkno, 0);
   1443 		}
   1444 	}
   1445 	numblks = howmany(ip->i_size, fs->fs_bsize);
   1446 	for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
   1447 		error = ffs_balloc(vp, lblktosize(fs, (off_t)blkno),
   1448 		    fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
   1449 		if (error)
   1450 			continue;
   1451 		if (fs->fs_size - blkno > NINDIR(fs))
   1452 			last = NINDIR(fs);
   1453 		else
   1454 			last = fs->fs_size - blkno;
   1455 		for (loc = 0; loc < last; loc++) {
   1456 			dblk = idb_get(ip, ibp->b_data, loc);
   1457 			if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
   1458 				idb_assign(ip, ibp->b_data, loc, 0);
   1459 			else if (dblk == blkstofrags(fs, blkno) &&
   1460 			    ffs_snapblkfree(fs, ip->i_devvp, dblk,
   1461 			    fs->fs_bsize, ip->i_number)) {
   1462 				DIP_ADD(ip, blocks, -btodb(fs->fs_bsize));
   1463 				idb_assign(ip, ibp->b_data, loc, 0);
   1464 			}
   1465 		}
   1466 		bawrite(ibp);
   1467 	}
   1468 	/*
   1469 	 * Clear snapshot flag and drop reference.
   1470 	 */
   1471 	ip->i_flags &= ~SF_SNAPSHOT;
   1472 	DIP_ASSIGN(ip, flags, ip->i_flags);
   1473 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1474 }
   1475 
   1476 /*
   1477  * Notification that a block is being freed. Return zero if the free
   1478  * should be allowed to proceed. Return non-zero if the snapshot file
   1479  * wants to claim the block. The block will be claimed if it is an
   1480  * uncopied part of one of the snapshots. It will be freed if it is
   1481  * either a BLK_NOCOPY or has already been copied in all of the snapshots.
   1482  * If a fragment is being freed, then all snapshots that care about
   1483  * it must make a copy since a snapshot file can only claim full sized
   1484  * blocks. Note that if more than one snapshot file maps the block,
   1485  * we can pick one at random to claim it. Since none of the snapshots
   1486  * can change, we are assurred that they will all see the same unmodified
   1487  * image. When deleting a snapshot file (see ffs_snapremove above), we
   1488  * must push any of these claimed blocks to one of the other snapshots
   1489  * that maps it. These claimed blocks are easily identified as they will
   1490  * have a block number equal to their logical block number within the
   1491  * snapshot. A copied block can never have this property because they
   1492  * must always have been allocated from a BLK_NOCOPY location.
   1493  */
   1494 int
   1495 ffs_snapblkfree(struct fs *fs, struct vnode *devvp, ufs2_daddr_t bno,
   1496     long size, ino_t inum)
   1497 {
   1498 	struct mount *mp = devvp->v_specmountpoint;
   1499 	struct buf *ibp;
   1500 	struct inode *ip;
   1501 	struct vnode *vp = NULL;
   1502 	struct snap_info *si;
   1503 	void *saved_data = NULL;
   1504 	ufs_lbn_t lbn;
   1505 	ufs2_daddr_t blkno;
   1506 	uint32_t gen;
   1507 	int indiroff = 0, snapshot_locked = 0, error = 0, claimedblk = 0;
   1508 
   1509 	si = VFSTOUFS(mp)->um_snapinfo;
   1510 	lbn = fragstoblks(fs, bno);
   1511 	mutex_enter(&si->si_lock);
   1512 retry:
   1513 	gen = si->si_gen;
   1514 	TAILQ_FOREACH(ip, &si->si_snapshots, i_nextsnap) {
   1515 		vp = ITOV(ip);
   1516 		if (snapshot_locked == 0) {
   1517 			if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
   1518 				mutex_exit(&si->si_lock);
   1519 				kpause("snaplock", false, 1, NULL);
   1520 				mutex_enter(&si->si_lock);
   1521 				goto retry;
   1522 			}
   1523 			snapshot_locked = 1;
   1524 			if (gen != si->si_gen)
   1525 				goto retry;
   1526 		}
   1527 		/*
   1528 		 * Lookup block being written.
   1529 		 */
   1530 		if (lbn < NDADDR) {
   1531 			blkno = db_get(ip, lbn);
   1532 		} else {
   1533 			mutex_exit(&si->si_lock);
   1534 			error = ffs_balloc(vp, lblktosize(fs, (off_t)lbn),
   1535 			    fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
   1536 			if (error) {
   1537 				mutex_enter(&si->si_lock);
   1538 				break;
   1539 			}
   1540 			indiroff = (lbn - NDADDR) % NINDIR(fs);
   1541 			blkno = idb_get(ip, ibp->b_data, indiroff);
   1542 			mutex_enter(&si->si_lock);
   1543 			if (gen != si->si_gen) {
   1544 				brelse(ibp, 0);
   1545 				goto retry;
   1546 			}
   1547 		}
   1548 		/*
   1549 		 * Check to see if block needs to be copied.
   1550 		 */
   1551 		if (blkno == 0) {
   1552 			/*
   1553 			 * A block that we map is being freed. If it has not
   1554 			 * been claimed yet, we will claim or copy it (below).
   1555 			 */
   1556 			claimedblk = 1;
   1557 		} else if (blkno == BLK_SNAP) {
   1558 			/*
   1559 			 * No previous snapshot claimed the block,
   1560 			 * so it will be freed and become a BLK_NOCOPY
   1561 			 * (don't care) for us.
   1562 			 */
   1563 			if (claimedblk)
   1564 				panic("snapblkfree: inconsistent block type");
   1565 			if (lbn < NDADDR) {
   1566 				db_assign(ip, lbn, BLK_NOCOPY);
   1567 				ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1568 			} else {
   1569 				idb_assign(ip, ibp->b_data, indiroff,
   1570 				    BLK_NOCOPY);
   1571 				mutex_exit(&si->si_lock);
   1572 				if (ip->i_ffs_effnlink > 0)
   1573 					bwrite(ibp);
   1574 				else
   1575 					bdwrite(ibp);
   1576 				mutex_enter(&si->si_lock);
   1577 				if (gen != si->si_gen)
   1578 					goto retry;
   1579 			}
   1580 			continue;
   1581 		} else /* BLK_NOCOPY or default */ {
   1582 			/*
   1583 			 * If the snapshot has already copied the block
   1584 			 * (default), or does not care about the block,
   1585 			 * it is not needed.
   1586 			 */
   1587 			if (lbn >= NDADDR)
   1588 				brelse(ibp, 0);
   1589 			continue;
   1590 		}
   1591 		/*
   1592 		 * If this is a full size block, we will just grab it
   1593 		 * and assign it to the snapshot inode. Otherwise we
   1594 		 * will proceed to copy it. See explanation for this
   1595 		 * routine as to why only a single snapshot needs to
   1596 		 * claim this block.
   1597 		 */
   1598 		if (size == fs->fs_bsize) {
   1599 #ifdef DEBUG
   1600 			if (snapdebug)
   1601 				printf("%s %llu lbn %" PRId64
   1602 				    "from inum %llu\n",
   1603 				    "Grabonremove: snapino",
   1604 				    (unsigned long long)ip->i_number,
   1605 				    lbn, (unsigned long long)inum);
   1606 #endif
   1607 			mutex_exit(&si->si_lock);
   1608 			if (lbn < NDADDR) {
   1609 				db_assign(ip, lbn, bno);
   1610 			} else {
   1611 				idb_assign(ip, ibp->b_data, indiroff, bno);
   1612 				if (ip->i_ffs_effnlink > 0)
   1613 					bwrite(ibp);
   1614 				else
   1615 					bdwrite(ibp);
   1616 			}
   1617 			DIP_ADD(ip, blocks, btodb(size));
   1618 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
   1619 			VOP_UNLOCK(vp, 0);
   1620 			return (1);
   1621 		}
   1622 		if (lbn >= NDADDR)
   1623 			brelse(ibp, 0);
   1624 #ifdef DEBUG
   1625 		if (snapdebug)
   1626 			printf("%s%llu lbn %" PRId64 " %s %llu size %ld\n",
   1627 			    "Copyonremove: snapino ",
   1628 			    (unsigned long long)ip->i_number,
   1629 			    lbn, "for inum", (unsigned long long)inum, size);
   1630 #endif
   1631 		/*
   1632 		 * If we have already read the old block contents, then
   1633 		 * simply copy them to the new block. Note that we need
   1634 		 * to synchronously write snapshots that have not been
   1635 		 * unlinked, and hence will be visible after a crash,
   1636 		 * to ensure their integrity.
   1637 		 */
   1638 		mutex_exit(&si->si_lock);
   1639 		if (saved_data == NULL) {
   1640 			saved_data = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
   1641 			error = rwfsblk(vp, B_READ, saved_data, lbn);
   1642 			if (error) {
   1643 				free(saved_data, M_UFSMNT);
   1644 				saved_data = NULL;
   1645 				mutex_enter(&si->si_lock);
   1646 				break;
   1647 			}
   1648 		}
   1649 		error = wrsnapblk(vp, saved_data, lbn);
   1650 		mutex_enter(&si->si_lock);
   1651 		if (error)
   1652 			break;
   1653 		if (gen != si->si_gen)
   1654 			goto retry;
   1655 	}
   1656 	mutex_exit(&si->si_lock);
   1657 	if (saved_data)
   1658 		free(saved_data, M_UFSMNT);
   1659 	/*
   1660 	 * If we have been unable to allocate a block in which to do
   1661 	 * the copy, then return non-zero so that the fragment will
   1662 	 * not be freed. Although space will be lost, the snapshot
   1663 	 * will stay consistent.
   1664 	 */
   1665 	if (snapshot_locked)
   1666 		VOP_UNLOCK(vp, 0);
   1667 	return (error);
   1668 }
   1669 
   1670 /*
   1671  * Associate snapshot files when mounting.
   1672  */
   1673 void
   1674 ffs_snapshot_mount(struct mount *mp)
   1675 {
   1676 	struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
   1677 	struct fs *fs = VFSTOUFS(mp)->um_fs;
   1678 	struct lwp *l = curlwp;
   1679 	struct vnode *vp;
   1680 	struct inode *ip, *xp;
   1681 	struct snap_info *si;
   1682 	ufs2_daddr_t snaplistsize, *snapblklist;
   1683 	int i, error, ns, snaploc, loc;
   1684 
   1685 	/*
   1686 	 * No persistent snapshots on apple ufs file systems.
   1687 	 */
   1688 	if (UFS_MPISAPPLEUFS(VFSTOUFS(mp)))
   1689 		return;
   1690 
   1691 	si = VFSTOUFS(mp)->um_snapinfo;
   1692 	ns = UFS_FSNEEDSWAP(fs);
   1693 	/*
   1694 	 * XXX The following needs to be set before ffs_truncate or
   1695 	 * VOP_READ can be called.
   1696 	 */
   1697 	mp->mnt_stat.f_iosize = fs->fs_bsize;
   1698 	/*
   1699 	 * Process each snapshot listed in the superblock.
   1700 	 */
   1701 	vp = NULL;
   1702 	mutex_enter(&si->si_lock);
   1703 	for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
   1704 		if (fs->fs_snapinum[snaploc] == 0)
   1705 			break;
   1706 		if ((error = VFS_VGET(mp, fs->fs_snapinum[snaploc],
   1707 		    &vp)) != 0) {
   1708 			printf("ffs_snapshot_mount: vget failed %d\n", error);
   1709 			continue;
   1710 		}
   1711 		ip = VTOI(vp);
   1712 		if ((ip->i_flags & SF_SNAPSHOT) == 0) {
   1713 			printf("ffs_snapshot_mount: non-snapshot inode %d\n",
   1714 			    fs->fs_snapinum[snaploc]);
   1715 			vput(vp);
   1716 			vp = NULL;
   1717 			for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
   1718 				if (fs->fs_snapinum[loc] == 0)
   1719 					break;
   1720 				fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
   1721 			}
   1722 			fs->fs_snapinum[loc - 1] = 0;
   1723 			snaploc--;
   1724 			continue;
   1725 		}
   1726 
   1727 		/*
   1728 		 * Read the block hints list. Use an empty list on
   1729 		 * read errors.
   1730 		 */
   1731 		error = vn_rdwr(UIO_READ, vp,
   1732 		    (void *)&snaplistsize, sizeof(snaplistsize),
   1733 		    lblktosize(fs, howmany(fs->fs_size, fs->fs_frag)),
   1734 		    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
   1735 		    l->l_cred, NULL, NULL);
   1736 		if (error) {
   1737 			printf("ffs_snapshot_mount: read_1 failed %d\n", error);
   1738 			snaplistsize = 1;
   1739 		} else
   1740 			snaplistsize = ufs_rw64(snaplistsize, ns);
   1741 		snapblklist = malloc(
   1742 		    snaplistsize * sizeof(ufs2_daddr_t), M_UFSMNT, M_WAITOK);
   1743 		if (error)
   1744 			snapblklist[0] = 1;
   1745 		else {
   1746 			error = vn_rdwr(UIO_READ, vp, (void *)snapblklist,
   1747 			    snaplistsize * sizeof(ufs2_daddr_t),
   1748 			    lblktosize(fs, howmany(fs->fs_size, fs->fs_frag)),
   1749 			    UIO_SYSSPACE, IO_NODELOCKED|IO_UNIT,
   1750 			    l->l_cred, NULL, NULL);
   1751 			for (i = 0; i < snaplistsize; i++)
   1752 				snapblklist[i] = ufs_rw64(snapblklist[i], ns);
   1753 			if (error) {
   1754 				printf("ffs_snapshot_mount: read_2 failed %d\n",
   1755 				    error);
   1756 				snapblklist[0] = 1;
   1757 			}
   1758 		}
   1759 		ip->i_snapblklist = &snapblklist[0];
   1760 
   1761 		/*
   1762 		 * Acquire the snapshot lock and give up our original
   1763 		 * private lock.
   1764 		 */
   1765 		VI_LOCK(vp);
   1766 		vp->v_vnlock = &si->si_vnlock;
   1767 		vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY);
   1768 		vlockmgr(&vp->v_lock, LK_RELEASE);
   1769 		/*
   1770 		 * Link it onto the active snapshot list.
   1771 		 */
   1772 		if (ip->i_nextsnap.tqe_prev != 0)
   1773 			panic("ffs_snapshot_mount: %llu already on list",
   1774 			    (unsigned long long)ip->i_number);
   1775 		else
   1776 			TAILQ_INSERT_TAIL(&si->si_snapshots, ip, i_nextsnap);
   1777 		vp->v_vflag |= VV_SYSTEM;
   1778 		VOP_UNLOCK(vp, 0);
   1779 	}
   1780 	/*
   1781 	 * No usable snapshots found.
   1782 	 */
   1783 	if (vp == NULL) {
   1784 		mutex_exit(&si->si_lock);
   1785 		return;
   1786 	}
   1787 	/*
   1788 	 * Attach the block hints list. We always want to
   1789 	 * use the list from the newest snapshot.
   1790 	*/
   1791 	xp = TAILQ_LAST(&si->si_snapshots, inodelst);
   1792 	si->si_snapblklist = xp->i_snapblklist;
   1793 	fscow_establish(mp, ffs_copyonwrite, devvp);
   1794 	si->si_gen++;
   1795 	mutex_exit(&si->si_lock);
   1796 }
   1797 
   1798 /*
   1799  * Disassociate snapshot files when unmounting.
   1800  */
   1801 void
   1802 ffs_snapshot_unmount(struct mount *mp)
   1803 {
   1804 	struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
   1805 	struct inode *xp;
   1806 	struct vnode *vp = NULL;
   1807 	struct snap_info *si;
   1808 
   1809 	si = VFSTOUFS(mp)->um_snapinfo;
   1810 	mutex_enter(&si->si_lock);
   1811 	while ((xp = TAILQ_FIRST(&si->si_snapshots)) != 0) {
   1812 		vp = ITOV(xp);
   1813 		vp->v_vnlock = &vp->v_lock;
   1814 		TAILQ_REMOVE(&si->si_snapshots, xp, i_nextsnap);
   1815 		xp->i_nextsnap.tqe_prev = 0;
   1816 		if (xp->i_snapblklist == si->si_snapblklist)
   1817 			si->si_snapblklist = NULL;
   1818 		FREE(xp->i_snapblklist, M_UFSMNT);
   1819 		if (xp->i_ffs_effnlink > 0) {
   1820 			si->si_gen++;
   1821 			mutex_exit(&si->si_lock);
   1822 			vrele(vp);
   1823 			mutex_enter(&si->si_lock);
   1824 		}
   1825 	}
   1826 	if (vp)
   1827 		fscow_disestablish(mp, ffs_copyonwrite, devvp);
   1828 	si->si_gen++;
   1829 	mutex_exit(&si->si_lock);
   1830 }
   1831 
   1832 /*
   1833  * Lookup a snapshots data block address.
   1834  * Simpler than UFS_BALLOC() as we know all metadata is already allocated.
   1835  */
   1836 static int
   1837 snapblkaddr(struct vnode *vp, daddr_t lbn, daddr_t *res)
   1838 {
   1839 	struct indir indirs[NIADDR + 2];
   1840 	struct inode *ip = VTOI(vp);
   1841 	struct fs *fs = ip->i_fs;
   1842 	struct buf *bp;
   1843 	int error, num;
   1844 
   1845 	KASSERT(lbn >= 0);
   1846 
   1847 	if (lbn < NDADDR) {
   1848 		*res = db_get(ip, lbn);
   1849 		return 0;
   1850 	}
   1851 	if ((error = ufs_getlbns(vp, lbn, indirs, &num)) != 0)
   1852 		return error;
   1853 	error = bread(vp, indirs[num-1].in_lbn, fs->fs_bsize, NOCRED, 0, &bp);
   1854 	if (error == 0)
   1855 		*res = idb_get(ip, bp->b_data, indirs[num-1].in_off);
   1856 	brelse(bp, 0);
   1857 
   1858 	return error;
   1859 }
   1860 
   1861 /*
   1862  * Check for need to copy block that is about to be written,
   1863  * copying the block if necessary.
   1864  */
   1865 static int
   1866 ffs_copyonwrite(void *v, struct buf *bp, bool data_valid)
   1867 {
   1868 	struct fs *fs;
   1869 	struct inode *ip;
   1870 	struct vnode *devvp = v, *vp = NULL;
   1871 	struct mount *mp = devvp->v_specmountpoint;
   1872 	struct snap_info *si;
   1873 	void *saved_data = NULL;
   1874 	ufs2_daddr_t lbn, blkno, *snapblklist;
   1875 	uint32_t gen;
   1876 	int lower, upper, mid, ns, snapshot_locked = 0, error = 0;
   1877 
   1878 	/*
   1879 	 * Check for valid snapshots.
   1880 	 */
   1881 	si = VFSTOUFS(mp)->um_snapinfo;
   1882 	mutex_enter(&si->si_lock);
   1883 	ip = TAILQ_FIRST(&si->si_snapshots);
   1884 	if (ip == NULL) {
   1885 		mutex_exit(&si->si_lock);
   1886 		return 0;
   1887 	}
   1888 	/*
   1889 	 * First check to see if it is in the preallocated list.
   1890 	 * By doing this check we avoid several potential deadlocks.
   1891 	 */
   1892 	fs = ip->i_fs;
   1893 	ns = UFS_FSNEEDSWAP(fs);
   1894 	lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
   1895 	snapblklist = si->si_snapblklist;
   1896 	upper = si->si_snapblklist[0] - 1;
   1897 	lower = 1;
   1898 	while (lower <= upper) {
   1899 		mid = (lower + upper) / 2;
   1900 		if (snapblklist[mid] == lbn)
   1901 			break;
   1902 		if (snapblklist[mid] < lbn)
   1903 			lower = mid + 1;
   1904 		else
   1905 			upper = mid - 1;
   1906 	}
   1907 	if (lower <= upper) {
   1908 		mutex_exit(&si->si_lock);
   1909 		return 0;
   1910 	}
   1911 	/*
   1912 	 * Not in the precomputed list, so check the snapshots.
   1913 	 */
   1914 	 if (data_valid && bp->b_bcount == fs->fs_bsize)
   1915 		saved_data = bp->b_data;
   1916 retry:
   1917 	gen = si->si_gen;
   1918 	TAILQ_FOREACH(ip, &si->si_snapshots, i_nextsnap) {
   1919 		vp = ITOV(ip);
   1920 		/*
   1921 		 * We ensure that everything of our own that needs to be
   1922 		 * copied will be done at the time that ffs_snapshot is
   1923 		 * called. Thus we can skip the check here which can
   1924 		 * deadlock in doing the lookup in ffs_balloc.
   1925 		 */
   1926 		if (bp->b_vp == vp)
   1927 			continue;
   1928 		/*
   1929 		 * Check to see if block needs to be copied.
   1930 		 */
   1931 		if (lbn < NDADDR) {
   1932 			blkno = db_get(ip, lbn);
   1933 		} else {
   1934 			mutex_exit(&si->si_lock);
   1935 			if ((error = snapblkaddr(vp, lbn, &blkno)) != 0) {
   1936 				mutex_enter(&si->si_lock);
   1937 				break;
   1938 			}
   1939 			mutex_enter(&si->si_lock);
   1940 			if (gen != si->si_gen)
   1941 				goto retry;
   1942 		}
   1943 #ifdef DIAGNOSTIC
   1944 		if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
   1945 			panic("ffs_copyonwrite: bad copy block");
   1946 #endif
   1947 		if (blkno != 0)
   1948 			continue;
   1949 
   1950 		if (snapshot_locked == 0) {
   1951 			if (VOP_LOCK(vp, LK_EXCLUSIVE | LK_NOWAIT) != 0) {
   1952 				mutex_exit(&si->si_lock);
   1953 				kpause("snaplock", false, 1, NULL);
   1954 				mutex_enter(&si->si_lock);
   1955 				goto retry;
   1956 			}
   1957 			snapshot_locked = 1;
   1958 			if (gen != si->si_gen)
   1959 				goto retry;
   1960 
   1961 			/* Check again if block still needs to be copied */
   1962 			if (lbn < NDADDR) {
   1963 				blkno = db_get(ip, lbn);
   1964 			} else {
   1965 				mutex_exit(&si->si_lock);
   1966 				if ((error = snapblkaddr(vp, lbn, &blkno)) != 0) {
   1967 					mutex_enter(&si->si_lock);
   1968 					break;
   1969 				}
   1970 				mutex_enter(&si->si_lock);
   1971 				if (gen != si->si_gen)
   1972 					goto retry;
   1973 			}
   1974 
   1975 			if (blkno != 0)
   1976 				continue;
   1977 		}
   1978 		/*
   1979 		 * Allocate the block into which to do the copy. Since
   1980 		 * multiple processes may all try to copy the same block,
   1981 		 * we have to recheck our need to do a copy if we sleep
   1982 		 * waiting for the lock.
   1983 		 *
   1984 		 * Because all snapshots on a filesystem share a single
   1985 		 * lock, we ensure that we will never be in competition
   1986 		 * with another process to allocate a block.
   1987 		 */
   1988 #ifdef DEBUG
   1989 		if (snapdebug) {
   1990 			printf("Copyonwrite: snapino %llu lbn %" PRId64 " for ",
   1991 			    (unsigned long long)ip->i_number, lbn);
   1992 			if (bp->b_vp == devvp)
   1993 				printf("fs metadata");
   1994 			else
   1995 				printf("inum %llu", (unsigned long long)
   1996 				    VTOI(bp->b_vp)->i_number);
   1997 			printf(" lblkno %" PRId64 "\n", bp->b_lblkno);
   1998 		}
   1999 #endif
   2000 		/*
   2001 		 * If we have already read the old block contents, then
   2002 		 * simply copy them to the new block. Note that we need
   2003 		 * to synchronously write snapshots that have not been
   2004 		 * unlinked, and hence will be visible after a crash,
   2005 		 * to ensure their integrity.
   2006 		 */
   2007 		mutex_exit(&si->si_lock);
   2008 		if (saved_data == NULL) {
   2009 			saved_data = malloc(fs->fs_bsize, M_UFSMNT, M_WAITOK);
   2010 			error = rwfsblk(vp, B_READ, saved_data, lbn);
   2011 			if (error) {
   2012 				free(saved_data, M_UFSMNT);
   2013 				saved_data = NULL;
   2014 				mutex_enter(&si->si_lock);
   2015 				break;
   2016 			}
   2017 		}
   2018 		error = wrsnapblk(vp, saved_data, lbn);
   2019 		mutex_enter(&si->si_lock);
   2020 		if (error)
   2021 			break;
   2022 		if (gen != si->si_gen)
   2023 			goto retry;
   2024 	}
   2025 	/*
   2026 	 * Note that we need to synchronously write snapshots that
   2027 	 * have not been unlinked, and hence will be visible after
   2028 	 * a crash, to ensure their integrity.
   2029 	 */
   2030 	mutex_exit(&si->si_lock);
   2031 	if (saved_data && saved_data != bp->b_data)
   2032 		free(saved_data, M_UFSMNT);
   2033 	if (snapshot_locked)
   2034 		VOP_UNLOCK(vp, 0);
   2035 	return error;
   2036 }
   2037 
   2038 /*
   2039  * Read or write the specified block of the filesystem vp resides on
   2040  * from or to the disk bypassing UBC and the buffer cache.
   2041  */
   2042 static int
   2043 rwfsblk(struct vnode *vp, int flags, void *data, ufs2_daddr_t lbn)
   2044 {
   2045 	int error;
   2046 	struct inode *ip = VTOI(vp);
   2047 	struct fs *fs = ip->i_fs;
   2048 	struct buf *nbp;
   2049 
   2050 	nbp = getiobuf(NULL, true);
   2051 	nbp->b_flags = flags;
   2052 	nbp->b_bcount = nbp->b_bufsize = fs->fs_bsize;
   2053 	nbp->b_error = 0;
   2054 	nbp->b_data = data;
   2055 	nbp->b_blkno = nbp->b_rawblkno = fsbtodb(fs, blkstofrags(fs, lbn));
   2056 	nbp->b_proc = NULL;
   2057 	nbp->b_dev = ip->i_devvp->v_rdev;
   2058 	SET(nbp->b_cflags, BC_BUSY);	/* mark buffer busy */
   2059 
   2060 	bdev_strategy(nbp);
   2061 
   2062 	error = biowait(nbp);
   2063 
   2064 	putiobuf(nbp);
   2065 
   2066 	return error;
   2067 }
   2068 
   2069 /*
   2070  * Write the specified block to a snapshot.
   2071  */
   2072 static int
   2073 wrsnapblk(struct vnode *vp, void *data, ufs2_daddr_t lbn)
   2074 {
   2075 	struct inode *ip = VTOI(vp);
   2076 	struct fs *fs = ip->i_fs;
   2077 	off_t offset;
   2078 	int error, flags;
   2079 
   2080 	offset = lblktosize(fs, (off_t)lbn);
   2081 	flags = IO_NODELOCKED|IO_UNIT;
   2082 	if (ip->i_ffs_effnlink > 0)
   2083 		flags |= IO_SYNC;
   2084 	error = vn_rdwr(UIO_WRITE, vp, data, fs->fs_bsize, offset,
   2085 	    UIO_SYSSPACE, flags, curlwp->l_cred, NULL, NULL);
   2086 
   2087 	if (!error && curlwp == uvm.pagedaemon_lwp) {
   2088 		mutex_enter(&vp->v_interlock);
   2089 		error = VOP_PUTPAGES(vp,
   2090 		    trunc_page(offset), round_page(offset+fs->fs_bsize),
   2091 		    PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
   2092 	}
   2093 
   2094 	return error;
   2095 }
   2096 
   2097 /*
   2098  * Get/Put direct block from inode or buffer containing disk addresses. Take
   2099  * care for fs type (UFS1/UFS2) and byte swapping. These functions should go
   2100  * into a global include.
   2101  */
   2102 static inline ufs2_daddr_t
   2103 db_get(struct inode *ip, int loc)
   2104 {
   2105 	if (ip->i_ump->um_fstype == UFS1)
   2106 		return ufs_rw32(ip->i_ffs1_db[loc], UFS_IPNEEDSWAP(ip));
   2107 	else
   2108 		return ufs_rw64(ip->i_ffs2_db[loc], UFS_IPNEEDSWAP(ip));
   2109 }
   2110 
   2111 static inline void
   2112 db_assign(struct inode *ip, int loc, ufs2_daddr_t val)
   2113 {
   2114 	if (ip->i_ump->um_fstype == UFS1)
   2115 		ip->i_ffs1_db[loc] = ufs_rw32(val, UFS_IPNEEDSWAP(ip));
   2116 	else
   2117 		ip->i_ffs2_db[loc] = ufs_rw64(val, UFS_IPNEEDSWAP(ip));
   2118 }
   2119 
   2120 static inline ufs2_daddr_t
   2121 idb_get(struct inode *ip, void *bf, int loc)
   2122 {
   2123 	if (ip->i_ump->um_fstype == UFS1)
   2124 		return ufs_rw32(((ufs1_daddr_t *)(bf))[loc],
   2125 		    UFS_IPNEEDSWAP(ip));
   2126 	else
   2127 		return ufs_rw64(((ufs2_daddr_t *)(bf))[loc],
   2128 		    UFS_IPNEEDSWAP(ip));
   2129 }
   2130 
   2131 static inline void
   2132 idb_assign(struct inode *ip, void *bf, int loc, ufs2_daddr_t val)
   2133 {
   2134 	if (ip->i_ump->um_fstype == UFS1)
   2135 		((ufs1_daddr_t *)(bf))[loc] =
   2136 		    ufs_rw32(val, UFS_IPNEEDSWAP(ip));
   2137 	else
   2138 		((ufs2_daddr_t *)(bf))[loc] =
   2139 		    ufs_rw64(val, UFS_IPNEEDSWAP(ip));
   2140 }
   2141