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