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