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