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