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