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