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