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