xref: /freebsd/sys/ufs/ffs/ffs_snapshot.c (revision 7aa65846327fe5bc7e5961c2f7fd0c61f2ec0b01)
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 "opt_quota.h"
40 
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
44 #include <sys/conf.h>
45 #include <sys/bio.h>
46 #include <sys/buf.h>
47 #include <sys/fcntl.h>
48 #include <sys/proc.h>
49 #include <sys/namei.h>
50 #include <sys/sched.h>
51 #include <sys/stat.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/resource.h>
55 #include <sys/resourcevar.h>
56 #include <sys/vnode.h>
57 
58 #include <geom/geom.h>
59 
60 #include <ufs/ufs/extattr.h>
61 #include <ufs/ufs/quota.h>
62 #include <ufs/ufs/ufsmount.h>
63 #include <ufs/ufs/inode.h>
64 #include <ufs/ufs/ufs_extern.h>
65 
66 #include <ufs/ffs/fs.h>
67 #include <ufs/ffs/ffs_extern.h>
68 
69 #define KERNCRED thread0.td_ucred
70 #define DEBUG 1
71 
72 #include "opt_ffs.h"
73 
74 #ifdef NO_FFS_SNAPSHOT
75 int
76 ffs_snapshot(mp, snapfile)
77 	struct mount *mp;
78 	char *snapfile;
79 {
80 	return (EINVAL);
81 }
82 
83 int
84 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
85 	struct fs *fs;
86 	struct vnode *devvp;
87 	ufs2_daddr_t bno;
88 	long size;
89 	ino_t inum;
90 	enum vtype vtype;
91 	struct workhead *wkhd;
92 {
93 	return (EINVAL);
94 }
95 
96 void
97 ffs_snapremove(vp)
98 	struct vnode *vp;
99 {
100 }
101 
102 void
103 ffs_snapshot_mount(mp)
104 	struct mount *mp;
105 {
106 }
107 
108 void
109 ffs_snapshot_unmount(mp)
110 	struct mount *mp;
111 {
112 }
113 
114 void
115 ffs_snapgone(ip)
116 	struct inode *ip;
117 {
118 }
119 
120 int
121 ffs_copyonwrite(devvp, bp)
122 	struct vnode *devvp;
123 	struct buf *bp;
124 {
125 	return (EINVAL);
126 }
127 
128 void
129 ffs_sync_snap(mp, waitfor)
130 	struct mount *mp;
131 	int waitfor;
132 {
133 }
134 
135 #else
136 FEATURE(ffs_snapshot, "FFS snapshot support");
137 
138 LIST_HEAD(, snapdata) snapfree;
139 static struct mtx snapfree_lock;
140 MTX_SYSINIT(ffs_snapfree, &snapfree_lock, "snapdata free list", MTX_DEF);
141 
142 static int cgaccount(int, struct vnode *, struct buf *, int);
143 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
144     int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
145     ufs_lbn_t, int), int, int);
146 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
147     ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
148     int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
149     ufs_lbn_t, int), int);
150 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
151     struct fs *, ufs_lbn_t, int);
152 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
153     struct fs *, ufs_lbn_t, int);
154 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
155     struct fs *, ufs_lbn_t, int);
156 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
157     int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
158     ufs_lbn_t, int), int, int);
159 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
160     ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
161     int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
162     ufs_lbn_t, int), int);
163 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
164     struct fs *, ufs_lbn_t, int);
165 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
166     struct fs *, ufs_lbn_t, int);
167 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
168     struct fs *, ufs_lbn_t, int);
169 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
170 static void try_free_snapdata(struct vnode *devvp);
171 static struct snapdata *ffs_snapdata_acquire(struct vnode *devvp);
172 static int ffs_bp_snapblk(struct vnode *, struct buf *);
173 
174 /*
175  * To ensure the consistency of snapshots across crashes, we must
176  * synchronously write out copied blocks before allowing the
177  * originals to be modified. Because of the rather severe speed
178  * penalty that this imposes, the code normally only ensures
179  * persistence for the filesystem metadata contained within a
180  * snapshot. Setting the following flag allows this crash
181  * persistence to be enabled for file contents.
182  */
183 int dopersistence = 0;
184 
185 #ifdef DEBUG
186 #include <sys/sysctl.h>
187 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
188 static int snapdebug = 0;
189 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
190 int collectsnapstats = 0;
191 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
192 	0, "");
193 #endif /* DEBUG */
194 
195 /*
196  * Create a snapshot file and initialize it for the filesystem.
197  */
198 int
199 ffs_snapshot(mp, snapfile)
200 	struct mount *mp;
201 	char *snapfile;
202 {
203 	ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
204 	int error, cg, snaploc;
205 	int i, size, len, loc;
206 	ufs2_daddr_t blockno;
207 	uint64_t flag;
208 	struct timespec starttime = {0, 0}, endtime;
209 	char saved_nice = 0;
210 	long redo = 0, snaplistsize = 0;
211 	int32_t *lp;
212 	void *space;
213 	struct fs *copy_fs = NULL, *fs;
214 	struct thread *td = curthread;
215 	struct inode *ip, *xp;
216 	struct buf *bp, *nbp, *ibp;
217 	struct nameidata nd;
218 	struct mount *wrtmp;
219 	struct vattr vat;
220 	struct vnode *vp, *xvp, *mvp, *devvp;
221 	struct uio auio;
222 	struct iovec aiov;
223 	struct snapdata *sn;
224 	struct ufsmount *ump;
225 
226 	ump = VFSTOUFS(mp);
227 	fs = ump->um_fs;
228 	sn = NULL;
229 	/*
230 	 * At the moment, journaled soft updates cannot support
231 	 * taking snapshots.
232 	 */
233 	if (MOUNTEDSUJ(mp)) {
234 		vfs_mount_error(mp, "%s: Snapshots are not yet supported when "
235 		    "running with journaled soft updates", fs->fs_fsmnt);
236 		return (EOPNOTSUPP);
237 	}
238 	MNT_ILOCK(mp);
239 	flag = mp->mnt_flag;
240 	MNT_IUNLOCK(mp);
241 	/*
242 	 * Need to serialize access to snapshot code per filesystem.
243 	 */
244 	/*
245 	 * Assign a snapshot slot in the superblock.
246 	 */
247 	UFS_LOCK(ump);
248 	for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
249 		if (fs->fs_snapinum[snaploc] == 0)
250 			break;
251 	UFS_UNLOCK(ump);
252 	if (snaploc == FSMAXSNAP)
253 		return (ENOSPC);
254 	/*
255 	 * Create the snapshot file.
256 	 */
257 restart:
258 	NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE, snapfile, td);
259 	if ((error = namei(&nd)) != 0)
260 		return (error);
261 	if (nd.ni_vp != NULL) {
262 		vput(nd.ni_vp);
263 		error = EEXIST;
264 	}
265 	if (nd.ni_dvp->v_mount != mp)
266 		error = EXDEV;
267 	if (error) {
268 		NDFREE(&nd, NDF_ONLY_PNBUF);
269 		if (nd.ni_dvp == nd.ni_vp)
270 			vrele(nd.ni_dvp);
271 		else
272 			vput(nd.ni_dvp);
273 		return (error);
274 	}
275 	VATTR_NULL(&vat);
276 	vat.va_type = VREG;
277 	vat.va_mode = S_IRUSR;
278 	vat.va_vaflags |= VA_EXCLUSIVE;
279 	if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
280 		wrtmp = NULL;
281 	if (wrtmp != mp)
282 		panic("ffs_snapshot: mount mismatch");
283 	vfs_rel(wrtmp);
284 	if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
285 		NDFREE(&nd, NDF_ONLY_PNBUF);
286 		vput(nd.ni_dvp);
287 		if ((error = vn_start_write(NULL, &wrtmp,
288 		    V_XSLEEP | PCATCH)) != 0)
289 			return (error);
290 		goto restart;
291 	}
292 	error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
293 	VOP_UNLOCK(nd.ni_dvp, 0);
294 	if (error) {
295 		NDFREE(&nd, NDF_ONLY_PNBUF);
296 		vn_finished_write(wrtmp);
297 		vrele(nd.ni_dvp);
298 		return (error);
299 	}
300 	vp = nd.ni_vp;
301 	vp->v_vflag |= VV_SYSTEM;
302 	ip = VTOI(vp);
303 	devvp = ip->i_devvp;
304 	/*
305 	 * Allocate and copy the last block contents so as to be able
306 	 * to set size to that of the filesystem.
307 	 */
308 	numblks = howmany(fs->fs_size, fs->fs_frag);
309 	error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
310 	    fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
311 	if (error)
312 		goto out;
313 	ip->i_size = lblktosize(fs, (off_t)numblks);
314 	DIP_SET(ip, i_size, ip->i_size);
315 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
316 	error = readblock(vp, bp, numblks - 1);
317 	bawrite(bp);
318 	if (error != 0)
319 		goto out;
320 	/*
321 	 * Preallocate critical data structures so that we can copy
322 	 * them in without further allocation after we suspend all
323 	 * operations on the filesystem. We would like to just release
324 	 * the allocated buffers without writing them since they will
325 	 * be filled in below once we are ready to go, but this upsets
326 	 * the soft update code, so we go ahead and write the new buffers.
327 	 *
328 	 * Allocate all indirect blocks and mark all of them as not
329 	 * needing to be copied.
330 	 */
331 	for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
332 		error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
333 		    fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
334 		if (error)
335 			goto out;
336 		bawrite(ibp);
337 	}
338 	/*
339 	 * Allocate copies for the superblock and its summary information.
340 	 */
341 	error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
342 	    0, &nbp);
343 	if (error)
344 		goto out;
345 	bawrite(nbp);
346 	blkno = fragstoblks(fs, fs->fs_csaddr);
347 	len = howmany(fs->fs_cssize, fs->fs_bsize);
348 	for (loc = 0; loc < len; loc++) {
349 		error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
350 		    fs->fs_bsize, KERNCRED, 0, &nbp);
351 		if (error)
352 			goto out;
353 		bawrite(nbp);
354 	}
355 	/*
356 	 * Allocate all cylinder group blocks.
357 	 */
358 	for (cg = 0; cg < fs->fs_ncg; cg++) {
359 		error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
360 		    fs->fs_bsize, KERNCRED, 0, &nbp);
361 		if (error)
362 			goto out;
363 		bawrite(nbp);
364 		if (cg % 10 == 0)
365 			ffs_syncvnode(vp, MNT_WAIT, 0);
366 	}
367 	/*
368 	 * Copy all the cylinder group maps. Although the
369 	 * filesystem is still active, we hope that only a few
370 	 * cylinder groups will change between now and when we
371 	 * suspend operations. Thus, we will be able to quickly
372 	 * touch up the few cylinder groups that changed during
373 	 * the suspension period.
374 	 */
375 	len = howmany(fs->fs_ncg, NBBY);
376 	space = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
377 	UFS_LOCK(ump);
378 	fs->fs_active = space;
379 	UFS_UNLOCK(ump);
380 	for (cg = 0; cg < fs->fs_ncg; cg++) {
381 		error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
382 		    fs->fs_bsize, KERNCRED, 0, &nbp);
383 		if (error)
384 			goto out;
385 		error = cgaccount(cg, vp, nbp, 1);
386 		bawrite(nbp);
387 		if (cg % 10 == 0)
388 			ffs_syncvnode(vp, MNT_WAIT, 0);
389 		if (error)
390 			goto out;
391 	}
392 	/*
393 	 * Change inode to snapshot type file.
394 	 */
395 	ip->i_flags |= SF_SNAPSHOT;
396 	DIP_SET(ip, i_flags, ip->i_flags);
397 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
398 	/*
399 	 * Ensure that the snapshot is completely on disk.
400 	 * Since we have marked it as a snapshot it is safe to
401 	 * unlock it as no process will be allowed to write to it.
402 	 */
403 	if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
404 		goto out;
405 	VOP_UNLOCK(vp, 0);
406 	/*
407 	 * All allocations are done, so we can now snapshot the system.
408 	 *
409 	 * Recind nice scheduling while running with the filesystem suspended.
410 	 */
411 	if (td->td_proc->p_nice > 0) {
412 		struct proc *p;
413 
414 		p = td->td_proc;
415 		PROC_LOCK(p);
416 		saved_nice = p->p_nice;
417 		sched_nice(p, 0);
418 		PROC_UNLOCK(p);
419 	}
420 	/*
421 	 * Suspend operation on filesystem.
422 	 */
423 	for (;;) {
424 		vn_finished_write(wrtmp);
425 		if ((error = vfs_write_suspend(vp->v_mount)) != 0) {
426 			vn_start_write(NULL, &wrtmp, V_WAIT);
427 			vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
428 			goto out;
429 		}
430 		if (mp->mnt_kern_flag & MNTK_SUSPENDED)
431 			break;
432 		vn_start_write(NULL, &wrtmp, V_WAIT);
433 	}
434 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
435 	if (ip->i_effnlink == 0) {
436 		error = ENOENT;		/* Snapshot file unlinked */
437 		goto out1;
438 	}
439 	if (collectsnapstats)
440 		nanotime(&starttime);
441 
442 	/* The last block might have changed.  Copy it again to be sure. */
443 	error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
444 	    fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
445 	if (error != 0)
446 		goto out1;
447 	error = readblock(vp, bp, numblks - 1);
448 	bp->b_flags |= B_VALIDSUSPWRT;
449 	bawrite(bp);
450 	if (error != 0)
451 		goto out1;
452 	/*
453 	 * First, copy all the cylinder group maps that have changed.
454 	 */
455 	for (cg = 0; cg < fs->fs_ncg; cg++) {
456 		if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
457 			continue;
458 		redo++;
459 		error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
460 		    fs->fs_bsize, KERNCRED, 0, &nbp);
461 		if (error)
462 			goto out1;
463 		error = cgaccount(cg, vp, nbp, 2);
464 		bawrite(nbp);
465 		if (error)
466 			goto out1;
467 	}
468 	/*
469 	 * Grab a copy of the superblock and its summary information.
470 	 * We delay writing it until the suspension is released below.
471 	 */
472 	copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK);
473 	bcopy(fs, copy_fs, fs->fs_sbsize);
474 	if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
475 		copy_fs->fs_clean = 1;
476 	size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
477 	if (fs->fs_sbsize < size)
478 		bzero(&((char *)copy_fs)[fs->fs_sbsize],
479 		    size - fs->fs_sbsize);
480 	size = blkroundup(fs, fs->fs_cssize);
481 	if (fs->fs_contigsumsize > 0)
482 		size += fs->fs_ncg * sizeof(int32_t);
483 	space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
484 	copy_fs->fs_csp = space;
485 	bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
486 	space = (char *)space + fs->fs_cssize;
487 	loc = howmany(fs->fs_cssize, fs->fs_fsize);
488 	i = fs->fs_frag - loc % fs->fs_frag;
489 	len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
490 	if (len > 0) {
491 		if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
492 		    len, KERNCRED, &bp)) != 0) {
493 			brelse(bp);
494 			free(copy_fs->fs_csp, M_UFSMNT);
495 			free(copy_fs, M_UFSMNT);
496 			copy_fs = NULL;
497 			goto out1;
498 		}
499 		bcopy(bp->b_data, space, (u_int)len);
500 		space = (char *)space + len;
501 		bp->b_flags |= B_INVAL | B_NOCACHE;
502 		brelse(bp);
503 	}
504 	if (fs->fs_contigsumsize > 0) {
505 		copy_fs->fs_maxcluster = lp = space;
506 		for (i = 0; i < fs->fs_ncg; i++)
507 			*lp++ = fs->fs_contigsumsize;
508 	}
509 	/*
510 	 * We must check for active files that have been unlinked
511 	 * (e.g., with a zero link count). We have to expunge all
512 	 * trace of these files from the snapshot so that they are
513 	 * not reclaimed prematurely by fsck or unnecessarily dumped.
514 	 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
515 	 * spec_strategy about writing on a suspended filesystem.
516 	 * Note that we skip unlinked snapshot files as they will
517 	 * be handled separately below.
518 	 *
519 	 * We also calculate the needed size for the snapshot list.
520 	 */
521 	snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
522 	    FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
523 	MNT_ILOCK(mp);
524 	mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
525 	MNT_IUNLOCK(mp);
526 loop:
527 	MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
528 		if ((xvp->v_usecount == 0 &&
529 		     (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
530 		    xvp->v_type == VNON ||
531 		    IS_SNAPSHOT(VTOI(xvp))) {
532 			VI_UNLOCK(xvp);
533 			continue;
534 		}
535 		/*
536 		 * We can skip parent directory vnode because it must have
537 		 * this snapshot file in it.
538 		 */
539 		if (xvp == nd.ni_dvp) {
540 			VI_UNLOCK(xvp);
541 			continue;
542 		}
543 		vholdl(xvp);
544 		if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
545 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
546 			vdrop(xvp);
547 			goto loop;
548 		}
549 		VI_LOCK(xvp);
550 		if (xvp->v_usecount == 0 &&
551 		    (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
552 			VI_UNLOCK(xvp);
553 			VOP_UNLOCK(xvp, 0);
554 			vdrop(xvp);
555 			continue;
556 		}
557 		VI_UNLOCK(xvp);
558 		if (snapdebug)
559 			vprint("ffs_snapshot: busy vnode", xvp);
560 		if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
561 		    vat.va_nlink > 0) {
562 			VOP_UNLOCK(xvp, 0);
563 			vdrop(xvp);
564 			continue;
565 		}
566 		xp = VTOI(xvp);
567 		if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
568 			VOP_UNLOCK(xvp, 0);
569 			vdrop(xvp);
570 			continue;
571 		}
572 		/*
573 		 * If there is a fragment, clear it here.
574 		 */
575 		blkno = 0;
576 		loc = howmany(xp->i_size, fs->fs_bsize) - 1;
577 		if (loc < NDADDR) {
578 			len = fragroundup(fs, blkoff(fs, xp->i_size));
579 			if (len != 0 && len < fs->fs_bsize) {
580 				ffs_blkfree(ump, copy_fs, vp,
581 				    DIP(xp, i_db[loc]), len, xp->i_number,
582 				    xvp->v_type, NULL);
583 				blkno = DIP(xp, i_db[loc]);
584 				DIP_SET(xp, i_db[loc], 0);
585 			}
586 		}
587 		snaplistsize += 1;
588 		if (xp->i_ump->um_fstype == UFS1)
589 			error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
590 			    BLK_NOCOPY, 1);
591 		else
592 			error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
593 			    BLK_NOCOPY, 1);
594 		if (blkno)
595 			DIP_SET(xp, i_db[loc], blkno);
596 		if (!error)
597 			error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
598 			    xp->i_mode, NULL);
599 		VOP_UNLOCK(xvp, 0);
600 		vdrop(xvp);
601 		if (error) {
602 			free(copy_fs->fs_csp, M_UFSMNT);
603 			free(copy_fs, M_UFSMNT);
604 			copy_fs = NULL;
605 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
606 			goto out1;
607 		}
608 	}
609 	/*
610 	 * Erase the journal file from the snapshot.
611 	 */
612 	if (fs->fs_flags & FS_SUJ) {
613 		error = softdep_journal_lookup(mp, &xvp);
614 		if (error) {
615 			free(copy_fs->fs_csp, M_UFSMNT);
616 			free(copy_fs, M_UFSMNT);
617 			copy_fs = NULL;
618 			goto out1;
619 		}
620 		xp = VTOI(xvp);
621 		if (xp->i_ump->um_fstype == UFS1)
622 			error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
623 			    BLK_NOCOPY, 0);
624 		else
625 			error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
626 			    BLK_NOCOPY, 0);
627 		vput(xvp);
628 	}
629 	/*
630 	 * Acquire a lock on the snapdata structure, creating it if necessary.
631 	 */
632 	sn = ffs_snapdata_acquire(devvp);
633 	/*
634 	 * Change vnode to use shared snapshot lock instead of the original
635 	 * private lock.
636 	 */
637 	vp->v_vnlock = &sn->sn_lock;
638 	lockmgr(&vp->v_lock, LK_RELEASE, NULL);
639 	xp = TAILQ_FIRST(&sn->sn_head);
640 	/*
641 	 * If this is the first snapshot on this filesystem, then we need
642 	 * to allocate the space for the list of preallocated snapshot blocks.
643 	 * This list will be refined below, but this preliminary one will
644 	 * keep us out of deadlock until the full one is ready.
645 	 */
646 	if (xp == NULL) {
647 		snapblklist = malloc(snaplistsize * sizeof(daddr_t),
648 		    M_UFSMNT, M_WAITOK);
649 		blkp = &snapblklist[1];
650 		*blkp++ = lblkno(fs, fs->fs_sblockloc);
651 		blkno = fragstoblks(fs, fs->fs_csaddr);
652 		for (cg = 0; cg < fs->fs_ncg; cg++) {
653 			if (fragstoblks(fs, cgtod(fs, cg) > blkno))
654 				break;
655 			*blkp++ = fragstoblks(fs, cgtod(fs, cg));
656 		}
657 		len = howmany(fs->fs_cssize, fs->fs_bsize);
658 		for (loc = 0; loc < len; loc++)
659 			*blkp++ = blkno + loc;
660 		for (; cg < fs->fs_ncg; cg++)
661 			*blkp++ = fragstoblks(fs, cgtod(fs, cg));
662 		snapblklist[0] = blkp - snapblklist;
663 		VI_LOCK(devvp);
664 		if (sn->sn_blklist != NULL)
665 			panic("ffs_snapshot: non-empty list");
666 		sn->sn_blklist = snapblklist;
667 		sn->sn_listsize = blkp - snapblklist;
668 		VI_UNLOCK(devvp);
669 	}
670 	/*
671 	 * Record snapshot inode. Since this is the newest snapshot,
672 	 * it must be placed at the end of the list.
673 	 */
674 	VI_LOCK(devvp);
675 	fs->fs_snapinum[snaploc] = ip->i_number;
676 	if (ip->i_nextsnap.tqe_prev != 0)
677 		panic("ffs_snapshot: %d already on list", ip->i_number);
678 	TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
679 	devvp->v_vflag |= VV_COPYONWRITE;
680 	VI_UNLOCK(devvp);
681 	ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
682 out1:
683 	KASSERT((sn != NULL && copy_fs != NULL && error == 0) ||
684 		(sn == NULL && copy_fs == NULL && error != 0),
685 		("email phk@ and mckusick@"));
686 	/*
687 	 * Resume operation on filesystem.
688 	 */
689 	vfs_write_resume(vp->v_mount);
690 	vn_start_write(NULL, &wrtmp, V_WAIT);
691 	if (collectsnapstats && starttime.tv_sec > 0) {
692 		nanotime(&endtime);
693 		timespecsub(&endtime, &starttime);
694 		printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
695 		    vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
696 		    endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
697 	}
698 	if (copy_fs == NULL)
699 		goto out;
700 	/*
701 	 * Copy allocation information from all the snapshots in
702 	 * this snapshot and then expunge them from its view.
703 	 */
704 	TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
705 		if (xp == ip)
706 			break;
707 		if (xp->i_ump->um_fstype == UFS1)
708 			error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
709 			    BLK_SNAP, 0);
710 		else
711 			error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
712 			    BLK_SNAP, 0);
713 		if (error == 0 && xp->i_effnlink == 0) {
714 			error = ffs_freefile(ump,
715 					     copy_fs,
716 					     vp,
717 					     xp->i_number,
718 					     xp->i_mode, NULL);
719 		}
720 		if (error) {
721 			fs->fs_snapinum[snaploc] = 0;
722 			goto done;
723 		}
724 	}
725 	/*
726 	 * Allocate space for the full list of preallocated snapshot blocks.
727 	 */
728 	snapblklist = malloc(snaplistsize * sizeof(daddr_t),
729 	    M_UFSMNT, M_WAITOK);
730 	ip->i_snapblklist = &snapblklist[1];
731 	/*
732 	 * Expunge the blocks used by the snapshots from the set of
733 	 * blocks marked as used in the snapshot bitmaps. Also, collect
734 	 * the list of allocated blocks in i_snapblklist.
735 	 */
736 	if (ip->i_ump->um_fstype == UFS1)
737 		error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
738 		    BLK_SNAP, 0);
739 	else
740 		error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
741 		    BLK_SNAP, 0);
742 	if (error) {
743 		fs->fs_snapinum[snaploc] = 0;
744 		free(snapblklist, M_UFSMNT);
745 		goto done;
746 	}
747 	if (snaplistsize < ip->i_snapblklist - snapblklist)
748 		panic("ffs_snapshot: list too small");
749 	snaplistsize = ip->i_snapblklist - snapblklist;
750 	snapblklist[0] = snaplistsize;
751 	ip->i_snapblklist = 0;
752 	/*
753 	 * Write out the list of allocated blocks to the end of the snapshot.
754 	 */
755 	auio.uio_iov = &aiov;
756 	auio.uio_iovcnt = 1;
757 	aiov.iov_base = (void *)snapblklist;
758 	aiov.iov_len = snaplistsize * sizeof(daddr_t);
759 	auio.uio_resid = aiov.iov_len;
760 	auio.uio_offset = ip->i_size;
761 	auio.uio_segflg = UIO_SYSSPACE;
762 	auio.uio_rw = UIO_WRITE;
763 	auio.uio_td = td;
764 	if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
765 		fs->fs_snapinum[snaploc] = 0;
766 		free(snapblklist, M_UFSMNT);
767 		goto done;
768 	}
769 	/*
770 	 * Write the superblock and its summary information
771 	 * to the snapshot.
772 	 */
773 	blkno = fragstoblks(fs, fs->fs_csaddr);
774 	len = howmany(fs->fs_cssize, fs->fs_bsize);
775 	space = copy_fs->fs_csp;
776 	for (loc = 0; loc < len; loc++) {
777 		error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
778 		if (error) {
779 			brelse(nbp);
780 			fs->fs_snapinum[snaploc] = 0;
781 			free(snapblklist, M_UFSMNT);
782 			goto done;
783 		}
784 		bcopy(space, nbp->b_data, fs->fs_bsize);
785 		space = (char *)space + fs->fs_bsize;
786 		bawrite(nbp);
787 	}
788 	error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
789 	    KERNCRED, &nbp);
790 	if (error) {
791 		brelse(nbp);
792 	} else {
793 		loc = blkoff(fs, fs->fs_sblockloc);
794 		bcopy((char *)copy_fs, &nbp->b_data[loc], fs->fs_bsize);
795 		bawrite(nbp);
796 	}
797 	/*
798 	 * As this is the newest list, it is the most inclusive, so
799 	 * should replace the previous list.
800 	 */
801 	VI_LOCK(devvp);
802 	space = sn->sn_blklist;
803 	sn->sn_blklist = snapblklist;
804 	sn->sn_listsize = snaplistsize;
805 	VI_UNLOCK(devvp);
806 	if (space != NULL)
807 		free(space, M_UFSMNT);
808 	/*
809 	 * Preallocate all the direct blocks in the snapshot inode so
810 	 * that we never have to write the inode itself to commit an
811 	 * update to the contents of the snapshot. Note that once
812 	 * created, the size of the snapshot will never change, so
813 	 * there will never be a need to write the inode except to
814 	 * update the non-integrity-critical time fields and
815 	 * allocated-block count.
816 	 */
817 	for (blockno = 0; blockno < NDADDR; blockno++) {
818 		if (DIP(ip, i_db[blockno]) != 0)
819 			continue;
820 		error = UFS_BALLOC(vp, lblktosize(fs, blockno),
821 		    fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
822 		if (error)
823 			break;
824 		error = readblock(vp, bp, blockno);
825 		bawrite(bp);
826 		if (error != 0)
827 			break;
828 	}
829 done:
830 	free(copy_fs->fs_csp, M_UFSMNT);
831 	free(copy_fs, M_UFSMNT);
832 	copy_fs = NULL;
833 out:
834 	NDFREE(&nd, NDF_ONLY_PNBUF);
835 	if (saved_nice > 0) {
836 		struct proc *p;
837 
838 		p = td->td_proc;
839 		PROC_LOCK(p);
840 		sched_nice(td->td_proc, saved_nice);
841 		PROC_UNLOCK(td->td_proc);
842 	}
843 	UFS_LOCK(ump);
844 	if (fs->fs_active != 0) {
845 		free(fs->fs_active, M_DEVBUF);
846 		fs->fs_active = 0;
847 	}
848 	UFS_UNLOCK(ump);
849 	MNT_ILOCK(mp);
850 	mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
851 	MNT_IUNLOCK(mp);
852 	if (error)
853 		(void) ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
854 	(void) ffs_syncvnode(vp, MNT_WAIT, 0);
855 	if (error)
856 		vput(vp);
857 	else
858 		VOP_UNLOCK(vp, 0);
859 	vrele(nd.ni_dvp);
860 	vn_finished_write(wrtmp);
861 	process_deferred_inactive(mp);
862 	return (error);
863 }
864 
865 /*
866  * Copy a cylinder group map. All the unallocated blocks are marked
867  * BLK_NOCOPY so that the snapshot knows that it need not copy them
868  * if they are later written. If passno is one, then this is a first
869  * pass, so only setting needs to be done. If passno is 2, then this
870  * is a revision to a previous pass which must be undone as the
871  * replacement pass is done.
872  */
873 static int
874 cgaccount(cg, vp, nbp, passno)
875 	int cg;
876 	struct vnode *vp;
877 	struct buf *nbp;
878 	int passno;
879 {
880 	struct buf *bp, *ibp;
881 	struct inode *ip;
882 	struct cg *cgp;
883 	struct fs *fs;
884 	ufs2_daddr_t base, numblks;
885 	int error, len, loc, indiroff;
886 
887 	ip = VTOI(vp);
888 	fs = ip->i_fs;
889 	error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
890 		(int)fs->fs_cgsize, KERNCRED, &bp);
891 	if (error) {
892 		brelse(bp);
893 		return (error);
894 	}
895 	cgp = (struct cg *)bp->b_data;
896 	if (!cg_chkmagic(cgp)) {
897 		brelse(bp);
898 		return (EIO);
899 	}
900 	UFS_LOCK(ip->i_ump);
901 	ACTIVESET(fs, cg);
902 	/*
903 	 * Recomputation of summary information might not have been performed
904 	 * at mount time.  Sync up summary information for current cylinder
905 	 * group while data is in memory to ensure that result of background
906 	 * fsck is slightly more consistent.
907 	 */
908 	fs->fs_cs(fs, cg) = cgp->cg_cs;
909 	UFS_UNLOCK(ip->i_ump);
910 	bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
911 	if (fs->fs_cgsize < fs->fs_bsize)
912 		bzero(&nbp->b_data[fs->fs_cgsize],
913 		    fs->fs_bsize - fs->fs_cgsize);
914 	cgp = (struct cg *)nbp->b_data;
915 	bqrelse(bp);
916 	if (passno == 2)
917 		nbp->b_flags |= B_VALIDSUSPWRT;
918 	numblks = howmany(fs->fs_size, fs->fs_frag);
919 	len = howmany(fs->fs_fpg, fs->fs_frag);
920 	base = cgbase(fs, cg) / fs->fs_frag;
921 	if (base + len >= numblks)
922 		len = numblks - base - 1;
923 	loc = 0;
924 	if (base < NDADDR) {
925 		for ( ; loc < NDADDR; loc++) {
926 			if (ffs_isblock(fs, cg_blksfree(cgp), loc))
927 				DIP_SET(ip, i_db[loc], BLK_NOCOPY);
928 			else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
929 				DIP_SET(ip, i_db[loc], 0);
930 			else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
931 				panic("ffs_snapshot: lost direct block");
932 		}
933 	}
934 	error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
935 	    fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
936 	if (error) {
937 		return (error);
938 	}
939 	indiroff = (base + loc - NDADDR) % NINDIR(fs);
940 	for ( ; loc < len; loc++, indiroff++) {
941 		if (indiroff >= NINDIR(fs)) {
942 			if (passno == 2)
943 				ibp->b_flags |= B_VALIDSUSPWRT;
944 			bawrite(ibp);
945 			error = UFS_BALLOC(vp,
946 			    lblktosize(fs, (off_t)(base + loc)),
947 			    fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
948 			if (error) {
949 				return (error);
950 			}
951 			indiroff = 0;
952 		}
953 		if (ip->i_ump->um_fstype == UFS1) {
954 			if (ffs_isblock(fs, cg_blksfree(cgp), loc))
955 				((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
956 				    BLK_NOCOPY;
957 			else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
958 			    [indiroff] == BLK_NOCOPY)
959 				((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
960 			else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
961 			    [indiroff] == BLK_NOCOPY)
962 				panic("ffs_snapshot: lost indirect block");
963 			continue;
964 		}
965 		if (ffs_isblock(fs, cg_blksfree(cgp), loc))
966 			((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
967 		else if (passno == 2 &&
968 		    ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
969 			((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
970 		else if (passno == 1 &&
971 		    ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
972 			panic("ffs_snapshot: lost indirect block");
973 	}
974 	if (passno == 2)
975 		ibp->b_flags |= B_VALIDSUSPWRT;
976 	bdwrite(ibp);
977 	return (0);
978 }
979 
980 /*
981  * Before expunging a snapshot inode, note all the
982  * blocks that it claims with BLK_SNAP so that fsck will
983  * be able to account for those blocks properly and so
984  * that this snapshot knows that it need not copy them
985  * if the other snapshot holding them is freed. This code
986  * is reproduced once each for UFS1 and UFS2.
987  */
988 static int
989 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
990 	struct vnode *snapvp;
991 	struct inode *cancelip;
992 	struct fs *fs;
993 	int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
994 	    struct fs *, ufs_lbn_t, int);
995 	int expungetype;
996 	int clearmode;
997 {
998 	int i, error, indiroff;
999 	ufs_lbn_t lbn, rlbn;
1000 	ufs2_daddr_t len, blkno, numblks, blksperindir;
1001 	struct ufs1_dinode *dip;
1002 	struct thread *td = curthread;
1003 	struct buf *bp;
1004 
1005 	/*
1006 	 * Prepare to expunge the inode. If its inode block has not
1007 	 * yet been copied, then allocate and fill the copy.
1008 	 */
1009 	lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1010 	blkno = 0;
1011 	if (lbn < NDADDR) {
1012 		blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1013 	} else {
1014 		if (DOINGSOFTDEP(snapvp))
1015 			softdep_prealloc(snapvp, MNT_WAIT);
1016 		td->td_pflags |= TDP_COWINPROGRESS;
1017 		error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1018 		   fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1019 		td->td_pflags &= ~TDP_COWINPROGRESS;
1020 		if (error)
1021 			return (error);
1022 		indiroff = (lbn - NDADDR) % NINDIR(fs);
1023 		blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1024 		bqrelse(bp);
1025 	}
1026 	if (blkno != 0) {
1027 		if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1028 			return (error);
1029 	} else {
1030 		error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1031 		    fs->fs_bsize, KERNCRED, 0, &bp);
1032 		if (error)
1033 			return (error);
1034 		if ((error = readblock(snapvp, bp, lbn)) != 0)
1035 			return (error);
1036 	}
1037 	/*
1038 	 * Set a snapshot inode to be a zero length file, regular files
1039 	 * or unlinked snapshots to be completely unallocated.
1040 	 */
1041 	dip = (struct ufs1_dinode *)bp->b_data +
1042 	    ino_to_fsbo(fs, cancelip->i_number);
1043 	if (clearmode || cancelip->i_effnlink == 0)
1044 		dip->di_mode = 0;
1045 	dip->di_size = 0;
1046 	dip->di_blocks = 0;
1047 	dip->di_flags &= ~SF_SNAPSHOT;
1048 	bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1049 	bdwrite(bp);
1050 	/*
1051 	 * Now go through and expunge all the blocks in the file
1052 	 * using the function requested.
1053 	 */
1054 	numblks = howmany(cancelip->i_size, fs->fs_bsize);
1055 	if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1056 	    &cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
1057 		return (error);
1058 	if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1059 	    &cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
1060 		return (error);
1061 	blksperindir = 1;
1062 	lbn = -NDADDR;
1063 	len = numblks - NDADDR;
1064 	rlbn = NDADDR;
1065 	for (i = 0; len > 0 && i < NIADDR; i++) {
1066 		error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1067 		    cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1068 		    blksperindir, fs, acctfunc, expungetype);
1069 		if (error)
1070 			return (error);
1071 		blksperindir *= NINDIR(fs);
1072 		lbn -= blksperindir + 1;
1073 		len -= blksperindir;
1074 		rlbn += blksperindir;
1075 	}
1076 	return (0);
1077 }
1078 
1079 /*
1080  * Descend an indirect block chain for vnode cancelvp accounting for all
1081  * its indirect blocks in snapvp.
1082  */
1083 static int
1084 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1085 	    blksperindir, fs, acctfunc, expungetype)
1086 	struct vnode *snapvp;
1087 	struct vnode *cancelvp;
1088 	int level;
1089 	ufs1_daddr_t blkno;
1090 	ufs_lbn_t lbn;
1091 	ufs_lbn_t rlbn;
1092 	ufs_lbn_t remblks;
1093 	ufs_lbn_t blksperindir;
1094 	struct fs *fs;
1095 	int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1096 	    struct fs *, ufs_lbn_t, int);
1097 	int expungetype;
1098 {
1099 	int error, num, i;
1100 	ufs_lbn_t subblksperindir;
1101 	struct indir indirs[NIADDR + 2];
1102 	ufs1_daddr_t last, *bap;
1103 	struct buf *bp;
1104 
1105 	if (blkno == 0) {
1106 		if (expungetype == BLK_NOCOPY)
1107 			return (0);
1108 		panic("indiracct_ufs1: missing indir");
1109 	}
1110 	if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1111 		return (error);
1112 	if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1113 		panic("indiracct_ufs1: botched params");
1114 	/*
1115 	 * We have to expand bread here since it will deadlock looking
1116 	 * up the block number for any blocks that are not in the cache.
1117 	 */
1118 	bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1119 	bp->b_blkno = fsbtodb(fs, blkno);
1120 	if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1121 	    (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1122 		brelse(bp);
1123 		return (error);
1124 	}
1125 	/*
1126 	 * Account for the block pointers in this indirect block.
1127 	 */
1128 	last = howmany(remblks, blksperindir);
1129 	if (last > NINDIR(fs))
1130 		last = NINDIR(fs);
1131 	bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1132 	bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1133 	bqrelse(bp);
1134 	error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1135 	    level == 0 ? rlbn : -1, expungetype);
1136 	if (error || level == 0)
1137 		goto out;
1138 	/*
1139 	 * Account for the block pointers in each of the indirect blocks
1140 	 * in the levels below us.
1141 	 */
1142 	subblksperindir = blksperindir / NINDIR(fs);
1143 	for (lbn++, level--, i = 0; i < last; i++) {
1144 		error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1145 		    rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1146 		if (error)
1147 			goto out;
1148 		rlbn += blksperindir;
1149 		lbn -= blksperindir;
1150 		remblks -= blksperindir;
1151 	}
1152 out:
1153 	free(bap, M_DEVBUF);
1154 	return (error);
1155 }
1156 
1157 /*
1158  * Do both snap accounting and map accounting.
1159  */
1160 static int
1161 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1162 	struct vnode *vp;
1163 	ufs1_daddr_t *oldblkp, *lastblkp;
1164 	struct fs *fs;
1165 	ufs_lbn_t lblkno;
1166 	int exptype;	/* BLK_SNAP or BLK_NOCOPY */
1167 {
1168 	int error;
1169 
1170 	if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1171 		return (error);
1172 	return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1173 }
1174 
1175 /*
1176  * Identify a set of blocks allocated in a snapshot inode.
1177  */
1178 static int
1179 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1180 	struct vnode *vp;
1181 	ufs1_daddr_t *oldblkp, *lastblkp;
1182 	struct fs *fs;
1183 	ufs_lbn_t lblkno;
1184 	int expungetype;	/* BLK_SNAP or BLK_NOCOPY */
1185 {
1186 	struct inode *ip = VTOI(vp);
1187 	ufs1_daddr_t blkno, *blkp;
1188 	ufs_lbn_t lbn;
1189 	struct buf *ibp;
1190 	int error;
1191 
1192 	for ( ; oldblkp < lastblkp; oldblkp++) {
1193 		blkno = *oldblkp;
1194 		if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1195 			continue;
1196 		lbn = fragstoblks(fs, blkno);
1197 		if (lbn < NDADDR) {
1198 			blkp = &ip->i_din1->di_db[lbn];
1199 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
1200 		} else {
1201 			error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1202 			    fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1203 			if (error)
1204 				return (error);
1205 			blkp = &((ufs1_daddr_t *)(ibp->b_data))
1206 			    [(lbn - NDADDR) % NINDIR(fs)];
1207 		}
1208 		/*
1209 		 * If we are expunging a snapshot vnode and we
1210 		 * find a block marked BLK_NOCOPY, then it is
1211 		 * one that has been allocated to this snapshot after
1212 		 * we took our current snapshot and can be ignored.
1213 		 */
1214 		if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1215 			if (lbn >= NDADDR)
1216 				brelse(ibp);
1217 		} else {
1218 			if (*blkp != 0)
1219 				panic("snapacct_ufs1: bad block");
1220 			*blkp = expungetype;
1221 			if (lbn >= NDADDR)
1222 				bdwrite(ibp);
1223 		}
1224 	}
1225 	return (0);
1226 }
1227 
1228 /*
1229  * Account for a set of blocks allocated in a snapshot inode.
1230  */
1231 static int
1232 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1233 	struct vnode *vp;
1234 	ufs1_daddr_t *oldblkp, *lastblkp;
1235 	struct fs *fs;
1236 	ufs_lbn_t lblkno;
1237 	int expungetype;
1238 {
1239 	ufs1_daddr_t blkno;
1240 	struct inode *ip;
1241 	ino_t inum;
1242 	int acctit;
1243 
1244 	ip = VTOI(vp);
1245 	inum = ip->i_number;
1246 	if (lblkno == -1)
1247 		acctit = 0;
1248 	else
1249 		acctit = 1;
1250 	for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1251 		blkno = *oldblkp;
1252 		if (blkno == 0 || blkno == BLK_NOCOPY)
1253 			continue;
1254 		if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1255 			*ip->i_snapblklist++ = lblkno;
1256 		if (blkno == BLK_SNAP)
1257 			blkno = blkstofrags(fs, lblkno);
1258 		ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum,
1259 		    vp->v_type, NULL);
1260 	}
1261 	return (0);
1262 }
1263 
1264 /*
1265  * Before expunging a snapshot inode, note all the
1266  * blocks that it claims with BLK_SNAP so that fsck will
1267  * be able to account for those blocks properly and so
1268  * that this snapshot knows that it need not copy them
1269  * if the other snapshot holding them is freed. This code
1270  * is reproduced once each for UFS1 and UFS2.
1271  */
1272 static int
1273 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1274 	struct vnode *snapvp;
1275 	struct inode *cancelip;
1276 	struct fs *fs;
1277 	int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1278 	    struct fs *, ufs_lbn_t, int);
1279 	int expungetype;
1280 	int clearmode;
1281 {
1282 	int i, error, indiroff;
1283 	ufs_lbn_t lbn, rlbn;
1284 	ufs2_daddr_t len, blkno, numblks, blksperindir;
1285 	struct ufs2_dinode *dip;
1286 	struct thread *td = curthread;
1287 	struct buf *bp;
1288 
1289 	/*
1290 	 * Prepare to expunge the inode. If its inode block has not
1291 	 * yet been copied, then allocate and fill the copy.
1292 	 */
1293 	lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1294 	blkno = 0;
1295 	if (lbn < NDADDR) {
1296 		blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1297 	} else {
1298 		if (DOINGSOFTDEP(snapvp))
1299 			softdep_prealloc(snapvp, MNT_WAIT);
1300 		td->td_pflags |= TDP_COWINPROGRESS;
1301 		error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1302 		   fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1303 		td->td_pflags &= ~TDP_COWINPROGRESS;
1304 		if (error)
1305 			return (error);
1306 		indiroff = (lbn - NDADDR) % NINDIR(fs);
1307 		blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1308 		bqrelse(bp);
1309 	}
1310 	if (blkno != 0) {
1311 		if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1312 			return (error);
1313 	} else {
1314 		error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1315 		    fs->fs_bsize, KERNCRED, 0, &bp);
1316 		if (error)
1317 			return (error);
1318 		if ((error = readblock(snapvp, bp, lbn)) != 0)
1319 			return (error);
1320 	}
1321 	/*
1322 	 * Set a snapshot inode to be a zero length file, regular files
1323 	 * to be completely unallocated.
1324 	 */
1325 	dip = (struct ufs2_dinode *)bp->b_data +
1326 	    ino_to_fsbo(fs, cancelip->i_number);
1327 	if (clearmode || cancelip->i_effnlink == 0)
1328 		dip->di_mode = 0;
1329 	dip->di_size = 0;
1330 	dip->di_blocks = 0;
1331 	dip->di_flags &= ~SF_SNAPSHOT;
1332 	bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1333 	bdwrite(bp);
1334 	/*
1335 	 * Now go through and expunge all the blocks in the file
1336 	 * using the function requested.
1337 	 */
1338 	numblks = howmany(cancelip->i_size, fs->fs_bsize);
1339 	if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1340 	    &cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
1341 		return (error);
1342 	if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1343 	    &cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
1344 		return (error);
1345 	blksperindir = 1;
1346 	lbn = -NDADDR;
1347 	len = numblks - NDADDR;
1348 	rlbn = NDADDR;
1349 	for (i = 0; len > 0 && i < NIADDR; i++) {
1350 		error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1351 		    cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1352 		    blksperindir, fs, acctfunc, expungetype);
1353 		if (error)
1354 			return (error);
1355 		blksperindir *= NINDIR(fs);
1356 		lbn -= blksperindir + 1;
1357 		len -= blksperindir;
1358 		rlbn += blksperindir;
1359 	}
1360 	return (0);
1361 }
1362 
1363 /*
1364  * Descend an indirect block chain for vnode cancelvp accounting for all
1365  * its indirect blocks in snapvp.
1366  */
1367 static int
1368 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1369 	    blksperindir, fs, acctfunc, expungetype)
1370 	struct vnode *snapvp;
1371 	struct vnode *cancelvp;
1372 	int level;
1373 	ufs2_daddr_t blkno;
1374 	ufs_lbn_t lbn;
1375 	ufs_lbn_t rlbn;
1376 	ufs_lbn_t remblks;
1377 	ufs_lbn_t blksperindir;
1378 	struct fs *fs;
1379 	int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1380 	    struct fs *, ufs_lbn_t, int);
1381 	int expungetype;
1382 {
1383 	int error, num, i;
1384 	ufs_lbn_t subblksperindir;
1385 	struct indir indirs[NIADDR + 2];
1386 	ufs2_daddr_t last, *bap;
1387 	struct buf *bp;
1388 
1389 	if (blkno == 0) {
1390 		if (expungetype == BLK_NOCOPY)
1391 			return (0);
1392 		panic("indiracct_ufs2: missing indir");
1393 	}
1394 	if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1395 		return (error);
1396 	if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1397 		panic("indiracct_ufs2: botched params");
1398 	/*
1399 	 * We have to expand bread here since it will deadlock looking
1400 	 * up the block number for any blocks that are not in the cache.
1401 	 */
1402 	bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1403 	bp->b_blkno = fsbtodb(fs, blkno);
1404 	if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1405 	    (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1406 		brelse(bp);
1407 		return (error);
1408 	}
1409 	/*
1410 	 * Account for the block pointers in this indirect block.
1411 	 */
1412 	last = howmany(remblks, blksperindir);
1413 	if (last > NINDIR(fs))
1414 		last = NINDIR(fs);
1415 	bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1416 	bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1417 	bqrelse(bp);
1418 	error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1419 	    level == 0 ? rlbn : -1, expungetype);
1420 	if (error || level == 0)
1421 		goto out;
1422 	/*
1423 	 * Account for the block pointers in each of the indirect blocks
1424 	 * in the levels below us.
1425 	 */
1426 	subblksperindir = blksperindir / NINDIR(fs);
1427 	for (lbn++, level--, i = 0; i < last; i++) {
1428 		error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1429 		    rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1430 		if (error)
1431 			goto out;
1432 		rlbn += blksperindir;
1433 		lbn -= blksperindir;
1434 		remblks -= blksperindir;
1435 	}
1436 out:
1437 	free(bap, M_DEVBUF);
1438 	return (error);
1439 }
1440 
1441 /*
1442  * Do both snap accounting and map accounting.
1443  */
1444 static int
1445 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1446 	struct vnode *vp;
1447 	ufs2_daddr_t *oldblkp, *lastblkp;
1448 	struct fs *fs;
1449 	ufs_lbn_t lblkno;
1450 	int exptype;	/* BLK_SNAP or BLK_NOCOPY */
1451 {
1452 	int error;
1453 
1454 	if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1455 		return (error);
1456 	return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1457 }
1458 
1459 /*
1460  * Identify a set of blocks allocated in a snapshot inode.
1461  */
1462 static int
1463 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1464 	struct vnode *vp;
1465 	ufs2_daddr_t *oldblkp, *lastblkp;
1466 	struct fs *fs;
1467 	ufs_lbn_t lblkno;
1468 	int expungetype;	/* BLK_SNAP or BLK_NOCOPY */
1469 {
1470 	struct inode *ip = VTOI(vp);
1471 	ufs2_daddr_t blkno, *blkp;
1472 	ufs_lbn_t lbn;
1473 	struct buf *ibp;
1474 	int error;
1475 
1476 	for ( ; oldblkp < lastblkp; oldblkp++) {
1477 		blkno = *oldblkp;
1478 		if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1479 			continue;
1480 		lbn = fragstoblks(fs, blkno);
1481 		if (lbn < NDADDR) {
1482 			blkp = &ip->i_din2->di_db[lbn];
1483 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
1484 		} else {
1485 			error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1486 			    fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1487 			if (error)
1488 				return (error);
1489 			blkp = &((ufs2_daddr_t *)(ibp->b_data))
1490 			    [(lbn - NDADDR) % NINDIR(fs)];
1491 		}
1492 		/*
1493 		 * If we are expunging a snapshot vnode and we
1494 		 * find a block marked BLK_NOCOPY, then it is
1495 		 * one that has been allocated to this snapshot after
1496 		 * we took our current snapshot and can be ignored.
1497 		 */
1498 		if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1499 			if (lbn >= NDADDR)
1500 				brelse(ibp);
1501 		} else {
1502 			if (*blkp != 0)
1503 				panic("snapacct_ufs2: bad block");
1504 			*blkp = expungetype;
1505 			if (lbn >= NDADDR)
1506 				bdwrite(ibp);
1507 		}
1508 	}
1509 	return (0);
1510 }
1511 
1512 /*
1513  * Account for a set of blocks allocated in a snapshot inode.
1514  */
1515 static int
1516 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1517 	struct vnode *vp;
1518 	ufs2_daddr_t *oldblkp, *lastblkp;
1519 	struct fs *fs;
1520 	ufs_lbn_t lblkno;
1521 	int expungetype;
1522 {
1523 	ufs2_daddr_t blkno;
1524 	struct inode *ip;
1525 	ino_t inum;
1526 	int acctit;
1527 
1528 	ip = VTOI(vp);
1529 	inum = ip->i_number;
1530 	if (lblkno == -1)
1531 		acctit = 0;
1532 	else
1533 		acctit = 1;
1534 	for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1535 		blkno = *oldblkp;
1536 		if (blkno == 0 || blkno == BLK_NOCOPY)
1537 			continue;
1538 		if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1539 			*ip->i_snapblklist++ = lblkno;
1540 		if (blkno == BLK_SNAP)
1541 			blkno = blkstofrags(fs, lblkno);
1542 		ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum,
1543 		    vp->v_type, NULL);
1544 	}
1545 	return (0);
1546 }
1547 
1548 /*
1549  * Decrement extra reference on snapshot when last name is removed.
1550  * It will not be freed until the last open reference goes away.
1551  */
1552 void
1553 ffs_snapgone(ip)
1554 	struct inode *ip;
1555 {
1556 	struct inode *xp;
1557 	struct fs *fs;
1558 	int snaploc;
1559 	struct snapdata *sn;
1560 	struct ufsmount *ump;
1561 
1562 	/*
1563 	 * Find snapshot in incore list.
1564 	 */
1565 	xp = NULL;
1566 	sn = ip->i_devvp->v_rdev->si_snapdata;
1567 	if (sn != NULL)
1568 		TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1569 			if (xp == ip)
1570 				break;
1571 	if (xp != NULL)
1572 		vrele(ITOV(ip));
1573 	else if (snapdebug)
1574 		printf("ffs_snapgone: lost snapshot vnode %d\n",
1575 		    ip->i_number);
1576 	/*
1577 	 * Delete snapshot inode from superblock. Keep list dense.
1578 	 */
1579 	fs = ip->i_fs;
1580 	ump = ip->i_ump;
1581 	UFS_LOCK(ump);
1582 	for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1583 		if (fs->fs_snapinum[snaploc] == ip->i_number)
1584 			break;
1585 	if (snaploc < FSMAXSNAP) {
1586 		for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1587 			if (fs->fs_snapinum[snaploc] == 0)
1588 				break;
1589 			fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1590 		}
1591 		fs->fs_snapinum[snaploc - 1] = 0;
1592 	}
1593 	UFS_UNLOCK(ump);
1594 }
1595 
1596 /*
1597  * Prepare a snapshot file for being removed.
1598  */
1599 void
1600 ffs_snapremove(vp)
1601 	struct vnode *vp;
1602 {
1603 	struct inode *ip;
1604 	struct vnode *devvp;
1605 	struct buf *ibp;
1606 	struct fs *fs;
1607 	ufs2_daddr_t numblks, blkno, dblk;
1608 	int error, loc, last;
1609 	struct snapdata *sn;
1610 
1611 	ip = VTOI(vp);
1612 	fs = ip->i_fs;
1613 	devvp = ip->i_devvp;
1614 	/*
1615 	 * If active, delete from incore list (this snapshot may
1616 	 * already have been in the process of being deleted, so
1617 	 * would not have been active).
1618 	 *
1619 	 * Clear copy-on-write flag if last snapshot.
1620 	 */
1621 	VI_LOCK(devvp);
1622 	if (ip->i_nextsnap.tqe_prev != 0) {
1623 		sn = devvp->v_rdev->si_snapdata;
1624 		TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1625 		ip->i_nextsnap.tqe_prev = 0;
1626 		VI_UNLOCK(devvp);
1627 		lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1628 		KASSERT(vp->v_vnlock == &sn->sn_lock,
1629 			("ffs_snapremove: lost lock mutation"));
1630 		vp->v_vnlock = &vp->v_lock;
1631 		VI_LOCK(devvp);
1632 		lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1633 		try_free_snapdata(devvp);
1634 	} else
1635 		VI_UNLOCK(devvp);
1636 	/*
1637 	 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1638 	 * snapshots that want them (see ffs_snapblkfree below).
1639 	 */
1640 	for (blkno = 1; blkno < NDADDR; blkno++) {
1641 		dblk = DIP(ip, i_db[blkno]);
1642 		if (dblk == 0)
1643 			continue;
1644 		if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1645 			DIP_SET(ip, i_db[blkno], 0);
1646 		else if ((dblk == blkstofrags(fs, blkno) &&
1647 		     ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1648 		     ip->i_number, vp->v_type, NULL))) {
1649 			DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1650 			    btodb(fs->fs_bsize));
1651 			DIP_SET(ip, i_db[blkno], 0);
1652 		}
1653 	}
1654 	numblks = howmany(ip->i_size, fs->fs_bsize);
1655 	for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1656 		error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1657 		    fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1658 		if (error)
1659 			continue;
1660 		if (fs->fs_size - blkno > NINDIR(fs))
1661 			last = NINDIR(fs);
1662 		else
1663 			last = fs->fs_size - blkno;
1664 		for (loc = 0; loc < last; loc++) {
1665 			if (ip->i_ump->um_fstype == UFS1) {
1666 				dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1667 				if (dblk == 0)
1668 					continue;
1669 				if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1670 					((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1671 				else if ((dblk == blkstofrags(fs, blkno) &&
1672 				     ffs_snapblkfree(fs, ip->i_devvp, dblk,
1673 				     fs->fs_bsize, ip->i_number, vp->v_type,
1674 				     NULL))) {
1675 					ip->i_din1->di_blocks -=
1676 					    btodb(fs->fs_bsize);
1677 					((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1678 				}
1679 				continue;
1680 			}
1681 			dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1682 			if (dblk == 0)
1683 				continue;
1684 			if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1685 				((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1686 			else if ((dblk == blkstofrags(fs, blkno) &&
1687 			     ffs_snapblkfree(fs, ip->i_devvp, dblk,
1688 			     fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1689 				ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1690 				((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1691 			}
1692 		}
1693 		bawrite(ibp);
1694 	}
1695 	/*
1696 	 * Clear snapshot flag and drop reference.
1697 	 */
1698 	ip->i_flags &= ~SF_SNAPSHOT;
1699 	DIP_SET(ip, i_flags, ip->i_flags);
1700 	ip->i_flag |= IN_CHANGE | IN_UPDATE;
1701 	/*
1702 	 * The dirtied indirects must be written out before
1703 	 * softdep_setup_freeblocks() is called.  Otherwise indir_trunc()
1704 	 * may find indirect pointers using the magic BLK_* values.
1705 	 */
1706 	if (DOINGSOFTDEP(vp))
1707 		ffs_syncvnode(vp, MNT_WAIT, 0);
1708 #ifdef QUOTA
1709 	/*
1710 	 * Reenable disk quotas for ex-snapshot file.
1711 	 */
1712 	if (!getinoquota(ip))
1713 		(void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1714 #endif
1715 }
1716 
1717 /*
1718  * Notification that a block is being freed. Return zero if the free
1719  * should be allowed to proceed. Return non-zero if the snapshot file
1720  * wants to claim the block. The block will be claimed if it is an
1721  * uncopied part of one of the snapshots. It will be freed if it is
1722  * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1723  * If a fragment is being freed, then all snapshots that care about
1724  * it must make a copy since a snapshot file can only claim full sized
1725  * blocks. Note that if more than one snapshot file maps the block,
1726  * we can pick one at random to claim it. Since none of the snapshots
1727  * can change, we are assurred that they will all see the same unmodified
1728  * image. When deleting a snapshot file (see ffs_snapremove above), we
1729  * must push any of these claimed blocks to one of the other snapshots
1730  * that maps it. These claimed blocks are easily identified as they will
1731  * have a block number equal to their logical block number within the
1732  * snapshot. A copied block can never have this property because they
1733  * must always have been allocated from a BLK_NOCOPY location.
1734  */
1735 int
1736 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1737 	struct fs *fs;
1738 	struct vnode *devvp;
1739 	ufs2_daddr_t bno;
1740 	long size;
1741 	ino_t inum;
1742 	enum vtype vtype;
1743 	struct workhead *wkhd;
1744 {
1745 	struct buf *ibp, *cbp, *savedcbp = 0;
1746 	struct thread *td = curthread;
1747 	struct inode *ip;
1748 	struct vnode *vp = NULL;
1749 	ufs_lbn_t lbn;
1750 	ufs2_daddr_t blkno;
1751 	int indiroff = 0, error = 0, claimedblk = 0;
1752 	struct snapdata *sn;
1753 
1754 	lbn = fragstoblks(fs, bno);
1755 retry:
1756 	VI_LOCK(devvp);
1757 	sn = devvp->v_rdev->si_snapdata;
1758 	if (sn == NULL) {
1759 		VI_UNLOCK(devvp);
1760 		return (0);
1761 	}
1762 	if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1763 	    VI_MTX(devvp)) != 0)
1764 		goto retry;
1765 	TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1766 		vp = ITOV(ip);
1767 		if (DOINGSOFTDEP(vp))
1768 			softdep_prealloc(vp, MNT_WAIT);
1769 		/*
1770 		 * Lookup block being written.
1771 		 */
1772 		if (lbn < NDADDR) {
1773 			blkno = DIP(ip, i_db[lbn]);
1774 		} else {
1775 			td->td_pflags |= TDP_COWINPROGRESS;
1776 			error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1777 			    fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1778 			td->td_pflags &= ~TDP_COWINPROGRESS;
1779 			if (error)
1780 				break;
1781 			indiroff = (lbn - NDADDR) % NINDIR(fs);
1782 			if (ip->i_ump->um_fstype == UFS1)
1783 				blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1784 			else
1785 				blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1786 		}
1787 		/*
1788 		 * Check to see if block needs to be copied.
1789 		 */
1790 		if (blkno == 0) {
1791 			/*
1792 			 * A block that we map is being freed. If it has not
1793 			 * been claimed yet, we will claim or copy it (below).
1794 			 */
1795 			claimedblk = 1;
1796 		} else if (blkno == BLK_SNAP) {
1797 			/*
1798 			 * No previous snapshot claimed the block,
1799 			 * so it will be freed and become a BLK_NOCOPY
1800 			 * (don't care) for us.
1801 			 */
1802 			if (claimedblk)
1803 				panic("snapblkfree: inconsistent block type");
1804 			if (lbn < NDADDR) {
1805 				DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1806 				ip->i_flag |= IN_CHANGE | IN_UPDATE;
1807 			} else if (ip->i_ump->um_fstype == UFS1) {
1808 				((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1809 				    BLK_NOCOPY;
1810 				bdwrite(ibp);
1811 			} else {
1812 				((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1813 				    BLK_NOCOPY;
1814 				bdwrite(ibp);
1815 			}
1816 			continue;
1817 		} else /* BLK_NOCOPY or default */ {
1818 			/*
1819 			 * If the snapshot has already copied the block
1820 			 * (default), or does not care about the block,
1821 			 * it is not needed.
1822 			 */
1823 			if (lbn >= NDADDR)
1824 				bqrelse(ibp);
1825 			continue;
1826 		}
1827 		/*
1828 		 * If this is a full size block, we will just grab it
1829 		 * and assign it to the snapshot inode. Otherwise we
1830 		 * will proceed to copy it. See explanation for this
1831 		 * routine as to why only a single snapshot needs to
1832 		 * claim this block.
1833 		 */
1834 		if (size == fs->fs_bsize) {
1835 #ifdef DEBUG
1836 			if (snapdebug)
1837 				printf("%s %d lbn %jd from inum %d\n",
1838 				    "Grabonremove: snapino", ip->i_number,
1839 				    (intmax_t)lbn, inum);
1840 #endif
1841 			/*
1842 			 * If journaling is tracking this write we must add
1843 			 * the work to the inode or indirect being written.
1844 			 */
1845 			if (wkhd != NULL) {
1846 				if (lbn < NDADDR)
1847 					softdep_inode_append(ip,
1848 					    curthread->td_ucred, wkhd);
1849 				else
1850 					softdep_buf_append(ibp, wkhd);
1851 			}
1852 			if (lbn < NDADDR) {
1853 				DIP_SET(ip, i_db[lbn], bno);
1854 			} else if (ip->i_ump->um_fstype == UFS1) {
1855 				((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1856 				bdwrite(ibp);
1857 			} else {
1858 				((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1859 				bdwrite(ibp);
1860 			}
1861 			DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1862 			ip->i_flag |= IN_CHANGE | IN_UPDATE;
1863 			lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1864 			return (1);
1865 		}
1866 		if (lbn >= NDADDR)
1867 			bqrelse(ibp);
1868 		/*
1869 		 * Allocate the block into which to do the copy. Note that this
1870 		 * allocation will never require any additional allocations for
1871 		 * the snapshot inode.
1872 		 */
1873 		td->td_pflags |= TDP_COWINPROGRESS;
1874 		error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1875 		    fs->fs_bsize, KERNCRED, 0, &cbp);
1876 		td->td_pflags &= ~TDP_COWINPROGRESS;
1877 		if (error)
1878 			break;
1879 #ifdef DEBUG
1880 		if (snapdebug)
1881 			printf("%s%d lbn %jd %s %d size %ld to blkno %jd\n",
1882 			    "Copyonremove: snapino ", ip->i_number,
1883 			    (intmax_t)lbn, "for inum", inum, size,
1884 			    (intmax_t)cbp->b_blkno);
1885 #endif
1886 		/*
1887 		 * If we have already read the old block contents, then
1888 		 * simply copy them to the new block. Note that we need
1889 		 * to synchronously write snapshots that have not been
1890 		 * unlinked, and hence will be visible after a crash,
1891 		 * to ensure their integrity. At a minimum we ensure the
1892 		 * integrity of the filesystem metadata, but use the
1893 		 * dopersistence sysctl-setable flag to decide on the
1894 		 * persistence needed for file content data.
1895 		 */
1896 		if (savedcbp != 0) {
1897 			bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1898 			bawrite(cbp);
1899 			if ((vtype == VDIR || dopersistence) &&
1900 			    ip->i_effnlink > 0)
1901 				(void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1902 			continue;
1903 		}
1904 		/*
1905 		 * Otherwise, read the old block contents into the buffer.
1906 		 */
1907 		if ((error = readblock(vp, cbp, lbn)) != 0) {
1908 			bzero(cbp->b_data, fs->fs_bsize);
1909 			bawrite(cbp);
1910 			if ((vtype == VDIR || dopersistence) &&
1911 			    ip->i_effnlink > 0)
1912 				(void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1913 			break;
1914 		}
1915 		savedcbp = cbp;
1916 	}
1917 	/*
1918 	 * Note that we need to synchronously write snapshots that
1919 	 * have not been unlinked, and hence will be visible after
1920 	 * a crash, to ensure their integrity. At a minimum we
1921 	 * ensure the integrity of the filesystem metadata, but
1922 	 * use the dopersistence sysctl-setable flag to decide on
1923 	 * the persistence needed for file content data.
1924 	 */
1925 	if (savedcbp) {
1926 		vp = savedcbp->b_vp;
1927 		bawrite(savedcbp);
1928 		if ((vtype == VDIR || dopersistence) &&
1929 		    VTOI(vp)->i_effnlink > 0)
1930 			(void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1931 	}
1932 	/*
1933 	 * If we have been unable to allocate a block in which to do
1934 	 * the copy, then return non-zero so that the fragment will
1935 	 * not be freed. Although space will be lost, the snapshot
1936 	 * will stay consistent.
1937 	 */
1938 	if (error != 0 && wkhd != NULL)
1939 		softdep_freework(wkhd);
1940 	lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1941 	return (error);
1942 }
1943 
1944 /*
1945  * Associate snapshot files when mounting.
1946  */
1947 void
1948 ffs_snapshot_mount(mp)
1949 	struct mount *mp;
1950 {
1951 	struct ufsmount *ump = VFSTOUFS(mp);
1952 	struct vnode *devvp = ump->um_devvp;
1953 	struct fs *fs = ump->um_fs;
1954 	struct thread *td = curthread;
1955 	struct snapdata *sn;
1956 	struct vnode *vp;
1957 	struct vnode *lastvp;
1958 	struct inode *ip;
1959 	struct uio auio;
1960 	struct iovec aiov;
1961 	void *snapblklist;
1962 	char *reason;
1963 	daddr_t snaplistsize;
1964 	int error, snaploc, loc;
1965 
1966 	/*
1967 	 * XXX The following needs to be set before ffs_truncate or
1968 	 * VOP_READ can be called.
1969 	 */
1970 	mp->mnt_stat.f_iosize = fs->fs_bsize;
1971 	/*
1972 	 * Process each snapshot listed in the superblock.
1973 	 */
1974 	vp = NULL;
1975 	lastvp = NULL;
1976 	sn = NULL;
1977 	for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1978 		if (fs->fs_snapinum[snaploc] == 0)
1979 			break;
1980 		if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1981 		    LK_EXCLUSIVE, &vp)) != 0){
1982 			printf("ffs_snapshot_mount: vget failed %d\n", error);
1983 			continue;
1984 		}
1985 		ip = VTOI(vp);
1986 		if (!IS_SNAPSHOT(ip) || ip->i_size ==
1987 		    lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1988 			if (!IS_SNAPSHOT(ip)) {
1989 				reason = "non-snapshot";
1990 			} else {
1991 				reason = "old format snapshot";
1992 				(void)ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
1993 				(void)ffs_syncvnode(vp, MNT_WAIT, 0);
1994 			}
1995 			printf("ffs_snapshot_mount: %s inode %d\n",
1996 			    reason, fs->fs_snapinum[snaploc]);
1997 			vput(vp);
1998 			vp = NULL;
1999 			for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2000 				if (fs->fs_snapinum[loc] == 0)
2001 					break;
2002 				fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2003 			}
2004 			fs->fs_snapinum[loc - 1] = 0;
2005 			snaploc--;
2006 			continue;
2007 		}
2008 		/*
2009 		 * Acquire a lock on the snapdata structure, creating it if
2010 		 * necessary.
2011 		 */
2012 		sn = ffs_snapdata_acquire(devvp);
2013 		/*
2014 		 * Change vnode to use shared snapshot lock instead of the
2015 		 * original private lock.
2016 		 */
2017 		vp->v_vnlock = &sn->sn_lock;
2018 		lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2019 		/*
2020 		 * Link it onto the active snapshot list.
2021 		 */
2022 		VI_LOCK(devvp);
2023 		if (ip->i_nextsnap.tqe_prev != 0)
2024 			panic("ffs_snapshot_mount: %d already on list",
2025 			    ip->i_number);
2026 		else
2027 			TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2028 		vp->v_vflag |= VV_SYSTEM;
2029 		VI_UNLOCK(devvp);
2030 		VOP_UNLOCK(vp, 0);
2031 		lastvp = vp;
2032 	}
2033 	vp = lastvp;
2034 	/*
2035 	 * No usable snapshots found.
2036 	 */
2037 	if (sn == NULL || vp == NULL)
2038 		return;
2039 	/*
2040 	 * Allocate the space for the block hints list. We always want to
2041 	 * use the list from the newest snapshot.
2042 	 */
2043 	auio.uio_iov = &aiov;
2044 	auio.uio_iovcnt = 1;
2045 	aiov.iov_base = (void *)&snaplistsize;
2046 	aiov.iov_len = sizeof(snaplistsize);
2047 	auio.uio_resid = aiov.iov_len;
2048 	auio.uio_offset =
2049 	    lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2050 	auio.uio_segflg = UIO_SYSSPACE;
2051 	auio.uio_rw = UIO_READ;
2052 	auio.uio_td = td;
2053 	vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2054 	if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2055 		printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2056 		VOP_UNLOCK(vp, 0);
2057 		return;
2058 	}
2059 	snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2060 	    M_UFSMNT, M_WAITOK);
2061 	auio.uio_iovcnt = 1;
2062 	aiov.iov_base = snapblklist;
2063 	aiov.iov_len = snaplistsize * sizeof (daddr_t);
2064 	auio.uio_resid = aiov.iov_len;
2065 	auio.uio_offset -= sizeof(snaplistsize);
2066 	if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2067 		printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2068 		VOP_UNLOCK(vp, 0);
2069 		free(snapblklist, M_UFSMNT);
2070 		return;
2071 	}
2072 	VOP_UNLOCK(vp, 0);
2073 	VI_LOCK(devvp);
2074 	ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2075 	sn->sn_listsize = snaplistsize;
2076 	sn->sn_blklist = (daddr_t *)snapblklist;
2077 	devvp->v_vflag |= VV_COPYONWRITE;
2078 	VI_UNLOCK(devvp);
2079 }
2080 
2081 /*
2082  * Disassociate snapshot files when unmounting.
2083  */
2084 void
2085 ffs_snapshot_unmount(mp)
2086 	struct mount *mp;
2087 {
2088 	struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2089 	struct snapdata *sn;
2090 	struct inode *xp;
2091 	struct vnode *vp;
2092 
2093 	VI_LOCK(devvp);
2094 	sn = devvp->v_rdev->si_snapdata;
2095 	while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2096 		vp = ITOV(xp);
2097 		TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2098 		xp->i_nextsnap.tqe_prev = 0;
2099 		lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2100 		    VI_MTX(devvp));
2101 		lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2102 		KASSERT(vp->v_vnlock == &sn->sn_lock,
2103 		("ffs_snapshot_unmount: lost lock mutation"));
2104 		vp->v_vnlock = &vp->v_lock;
2105 		lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2106 		lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2107 		if (xp->i_effnlink > 0)
2108 			vrele(vp);
2109 		VI_LOCK(devvp);
2110 		sn = devvp->v_rdev->si_snapdata;
2111 	}
2112 	try_free_snapdata(devvp);
2113 	ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2114 }
2115 
2116 /*
2117  * Check the buffer block to be belong to device buffer that shall be
2118  * locked after snaplk. devvp shall be locked on entry, and will be
2119  * leaved locked upon exit.
2120  */
2121 static int
2122 ffs_bp_snapblk(devvp, bp)
2123 	struct vnode *devvp;
2124 	struct buf *bp;
2125 {
2126 	struct snapdata *sn;
2127 	struct fs *fs;
2128 	ufs2_daddr_t lbn, *snapblklist;
2129 	int lower, upper, mid;
2130 
2131 	ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2132 	KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2133 	sn = devvp->v_rdev->si_snapdata;
2134 	if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2135 		return (0);
2136 	fs = TAILQ_FIRST(&sn->sn_head)->i_fs;
2137 	lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2138 	snapblklist = sn->sn_blklist;
2139 	upper = sn->sn_listsize - 1;
2140 	lower = 1;
2141 	while (lower <= upper) {
2142 		mid = (lower + upper) / 2;
2143 		if (snapblklist[mid] == lbn)
2144 			break;
2145 		if (snapblklist[mid] < lbn)
2146 			lower = mid + 1;
2147 		else
2148 			upper = mid - 1;
2149 	}
2150 	if (lower <= upper)
2151 		return (1);
2152 	return (0);
2153 }
2154 
2155 void
2156 ffs_bdflush(bo, bp)
2157 	struct bufobj *bo;
2158 	struct buf *bp;
2159 {
2160 	struct thread *td;
2161 	struct vnode *vp, *devvp;
2162 	struct buf *nbp;
2163 	int bp_bdskip;
2164 
2165 	if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2166 		return;
2167 
2168 	td = curthread;
2169 	vp = bp->b_vp;
2170 	devvp = bo->__bo_vnode;
2171 	KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2172 
2173 	VI_LOCK(devvp);
2174 	bp_bdskip = ffs_bp_snapblk(devvp, bp);
2175 	if (bp_bdskip)
2176 		bdwriteskip++;
2177 	VI_UNLOCK(devvp);
2178 	if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2179 		(void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2180 		altbufferflushes++;
2181 	} else {
2182 		BO_LOCK(bo);
2183 		/*
2184 		 * Try to find a buffer to flush.
2185 		 */
2186 		TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2187 			if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2188 			    BUF_LOCK(nbp,
2189 				     LK_EXCLUSIVE | LK_NOWAIT, NULL))
2190 				continue;
2191 			if (bp == nbp)
2192 				panic("bdwrite: found ourselves");
2193 			BO_UNLOCK(bo);
2194 			/*
2195 			 * Don't countdeps with the bo lock
2196 			 * held.
2197 			 */
2198 			if (buf_countdeps(nbp, 0)) {
2199 				BO_LOCK(bo);
2200 				BUF_UNLOCK(nbp);
2201 				continue;
2202 			}
2203 			if (bp_bdskip) {
2204 				VI_LOCK(devvp);
2205 				if (!ffs_bp_snapblk(vp, nbp)) {
2206 					if (BO_MTX(bo) != VI_MTX(vp)) {
2207 						VI_UNLOCK(devvp);
2208 						BO_LOCK(bo);
2209 					}
2210 					BUF_UNLOCK(nbp);
2211 					continue;
2212 				}
2213 				VI_UNLOCK(devvp);
2214 			}
2215 			if (nbp->b_flags & B_CLUSTEROK) {
2216 				vfs_bio_awrite(nbp);
2217 			} else {
2218 				bremfree(nbp);
2219 				bawrite(nbp);
2220 			}
2221 			dirtybufferflushes++;
2222 			break;
2223 		}
2224 		if (nbp == NULL)
2225 			BO_UNLOCK(bo);
2226 	}
2227 }
2228 
2229 /*
2230  * Check for need to copy block that is about to be written,
2231  * copying the block if necessary.
2232  */
2233 int
2234 ffs_copyonwrite(devvp, bp)
2235 	struct vnode *devvp;
2236 	struct buf *bp;
2237 {
2238 	struct snapdata *sn;
2239 	struct buf *ibp, *cbp, *savedcbp = 0;
2240 	struct thread *td = curthread;
2241 	struct fs *fs;
2242 	struct inode *ip;
2243 	struct vnode *vp = 0;
2244 	ufs2_daddr_t lbn, blkno, *snapblklist;
2245 	int lower, upper, mid, indiroff, error = 0;
2246 	int launched_async_io, prev_norunningbuf;
2247 	long saved_runningbufspace;
2248 
2249 	if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2250 		return (0);		/* Update on a snapshot file */
2251 	if (td->td_pflags & TDP_COWINPROGRESS)
2252 		panic("ffs_copyonwrite: recursive call");
2253 	/*
2254 	 * First check to see if it is in the preallocated list.
2255 	 * By doing this check we avoid several potential deadlocks.
2256 	 */
2257 	VI_LOCK(devvp);
2258 	sn = devvp->v_rdev->si_snapdata;
2259 	if (sn == NULL ||
2260 	    TAILQ_EMPTY(&sn->sn_head)) {
2261 		VI_UNLOCK(devvp);
2262 		return (0);		/* No snapshot */
2263 	}
2264 	ip = TAILQ_FIRST(&sn->sn_head);
2265 	fs = ip->i_fs;
2266 	lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2267 	snapblklist = sn->sn_blklist;
2268 	upper = sn->sn_listsize - 1;
2269 	lower = 1;
2270 	while (lower <= upper) {
2271 		mid = (lower + upper) / 2;
2272 		if (snapblklist[mid] == lbn)
2273 			break;
2274 		if (snapblklist[mid] < lbn)
2275 			lower = mid + 1;
2276 		else
2277 			upper = mid - 1;
2278 	}
2279 	if (lower <= upper) {
2280 		VI_UNLOCK(devvp);
2281 		return (0);
2282 	}
2283 	launched_async_io = 0;
2284 	prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2285 	/*
2286 	 * Since I/O on bp isn't yet in progress and it may be blocked
2287 	 * for a long time waiting on snaplk, back it out of
2288 	 * runningbufspace, possibly waking other threads waiting for space.
2289 	 */
2290 	saved_runningbufspace = bp->b_runningbufspace;
2291 	if (saved_runningbufspace != 0)
2292 		runningbufwakeup(bp);
2293 	/*
2294 	 * Not in the precomputed list, so check the snapshots.
2295 	 */
2296 	while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2297 	    VI_MTX(devvp)) != 0) {
2298 		VI_LOCK(devvp);
2299 		sn = devvp->v_rdev->si_snapdata;
2300 		if (sn == NULL ||
2301 		    TAILQ_EMPTY(&sn->sn_head)) {
2302 			VI_UNLOCK(devvp);
2303 			if (saved_runningbufspace != 0) {
2304 				bp->b_runningbufspace = saved_runningbufspace;
2305 				atomic_add_long(&runningbufspace,
2306 					       bp->b_runningbufspace);
2307 			}
2308 			return (0);		/* Snapshot gone */
2309 		}
2310 	}
2311 	TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2312 		vp = ITOV(ip);
2313 		if (DOINGSOFTDEP(vp))
2314 			softdep_prealloc(vp, MNT_WAIT);
2315 		/*
2316 		 * We ensure that everything of our own that needs to be
2317 		 * copied will be done at the time that ffs_snapshot is
2318 		 * called. Thus we can skip the check here which can
2319 		 * deadlock in doing the lookup in UFS_BALLOC.
2320 		 */
2321 		if (bp->b_vp == vp)
2322 			continue;
2323 		/*
2324 		 * Check to see if block needs to be copied. We do not have
2325 		 * to hold the snapshot lock while doing this lookup as it
2326 		 * will never require any additional allocations for the
2327 		 * snapshot inode.
2328 		 */
2329 		if (lbn < NDADDR) {
2330 			blkno = DIP(ip, i_db[lbn]);
2331 		} else {
2332 			td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2333 			error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2334 			   fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2335 			td->td_pflags &= ~TDP_COWINPROGRESS;
2336 			if (error)
2337 				break;
2338 			indiroff = (lbn - NDADDR) % NINDIR(fs);
2339 			if (ip->i_ump->um_fstype == UFS1)
2340 				blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2341 			else
2342 				blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2343 			bqrelse(ibp);
2344 		}
2345 #ifdef INVARIANTS
2346 		if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2347 			panic("ffs_copyonwrite: bad copy block");
2348 #endif
2349 		if (blkno != 0)
2350 			continue;
2351 		/*
2352 		 * Allocate the block into which to do the copy. Since
2353 		 * multiple processes may all try to copy the same block,
2354 		 * we have to recheck our need to do a copy if we sleep
2355 		 * waiting for the lock.
2356 		 *
2357 		 * Because all snapshots on a filesystem share a single
2358 		 * lock, we ensure that we will never be in competition
2359 		 * with another process to allocate a block.
2360 		 */
2361 		td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2362 		error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2363 		    fs->fs_bsize, KERNCRED, 0, &cbp);
2364 		td->td_pflags &= ~TDP_COWINPROGRESS;
2365 		if (error)
2366 			break;
2367 #ifdef DEBUG
2368 		if (snapdebug) {
2369 			printf("Copyonwrite: snapino %d lbn %jd for ",
2370 			    ip->i_number, (intmax_t)lbn);
2371 			if (bp->b_vp == devvp)
2372 				printf("fs metadata");
2373 			else
2374 				printf("inum %d", VTOI(bp->b_vp)->i_number);
2375 			printf(" lblkno %jd to blkno %jd\n",
2376 			    (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2377 		}
2378 #endif
2379 		/*
2380 		 * If we have already read the old block contents, then
2381 		 * simply copy them to the new block. Note that we need
2382 		 * to synchronously write snapshots that have not been
2383 		 * unlinked, and hence will be visible after a crash,
2384 		 * to ensure their integrity. At a minimum we ensure the
2385 		 * integrity of the filesystem metadata, but use the
2386 		 * dopersistence sysctl-setable flag to decide on the
2387 		 * persistence needed for file content data.
2388 		 */
2389 		if (savedcbp != 0) {
2390 			bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2391 			bawrite(cbp);
2392 			if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2393 			    dopersistence) && ip->i_effnlink > 0)
2394 				(void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2395 			else
2396 				launched_async_io = 1;
2397 			continue;
2398 		}
2399 		/*
2400 		 * Otherwise, read the old block contents into the buffer.
2401 		 */
2402 		if ((error = readblock(vp, cbp, lbn)) != 0) {
2403 			bzero(cbp->b_data, fs->fs_bsize);
2404 			bawrite(cbp);
2405 			if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2406 			    dopersistence) && ip->i_effnlink > 0)
2407 				(void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2408 			else
2409 				launched_async_io = 1;
2410 			break;
2411 		}
2412 		savedcbp = cbp;
2413 	}
2414 	/*
2415 	 * Note that we need to synchronously write snapshots that
2416 	 * have not been unlinked, and hence will be visible after
2417 	 * a crash, to ensure their integrity. At a minimum we
2418 	 * ensure the integrity of the filesystem metadata, but
2419 	 * use the dopersistence sysctl-setable flag to decide on
2420 	 * the persistence needed for file content data.
2421 	 */
2422 	if (savedcbp) {
2423 		vp = savedcbp->b_vp;
2424 		bawrite(savedcbp);
2425 		if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2426 		    dopersistence) && VTOI(vp)->i_effnlink > 0)
2427 			(void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2428 		else
2429 			launched_async_io = 1;
2430 	}
2431 	lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2432 	td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2433 		prev_norunningbuf;
2434 	if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2435 		waitrunningbufspace();
2436 	/*
2437 	 * I/O on bp will now be started, so count it in runningbufspace.
2438 	 */
2439 	if (saved_runningbufspace != 0) {
2440 		bp->b_runningbufspace = saved_runningbufspace;
2441 		atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2442 	}
2443 	return (error);
2444 }
2445 
2446 /*
2447  * sync snapshots to force freework records waiting on snapshots to claim
2448  * blocks to free.
2449  */
2450 void
2451 ffs_sync_snap(mp, waitfor)
2452 	struct mount *mp;
2453 	int waitfor;
2454 {
2455 	struct snapdata *sn;
2456 	struct vnode *devvp;
2457 	struct vnode *vp;
2458 	struct inode *ip;
2459 
2460 	devvp = VFSTOUFS(mp)->um_devvp;
2461 	if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2462 		return;
2463 	for (;;) {
2464 		VI_LOCK(devvp);
2465 		sn = devvp->v_rdev->si_snapdata;
2466 		if (sn == NULL) {
2467 			VI_UNLOCK(devvp);
2468 			return;
2469 		}
2470 		if (lockmgr(&sn->sn_lock,
2471 		    LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2472 		    VI_MTX(devvp)) == 0)
2473 			break;
2474 	}
2475 	TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2476 		vp = ITOV(ip);
2477 		ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2478 	}
2479 	lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2480 }
2481 
2482 /*
2483  * Read the specified block into the given buffer.
2484  * Much of this boiler-plate comes from bwrite().
2485  */
2486 static int
2487 readblock(vp, bp, lbn)
2488 	struct vnode *vp;
2489 	struct buf *bp;
2490 	ufs2_daddr_t lbn;
2491 {
2492 	struct inode *ip = VTOI(vp);
2493 	struct bio *bip;
2494 
2495 	bip = g_alloc_bio();
2496 	bip->bio_cmd = BIO_READ;
2497 	bip->bio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
2498 	bip->bio_data = bp->b_data;
2499 	bip->bio_length = bp->b_bcount;
2500 	bip->bio_done = NULL;
2501 
2502 	g_io_request(bip, ip->i_devvp->v_bufobj.bo_private);
2503 	bp->b_error = biowait(bip, "snaprdb");
2504 	g_destroy_bio(bip);
2505 	return (bp->b_error);
2506 }
2507 
2508 #endif
2509 
2510 /*
2511  * Process file deletes that were deferred by ufs_inactive() due to
2512  * the file system being suspended. Transfer IN_LAZYACCESS into
2513  * IN_MODIFIED for vnodes that were accessed during suspension.
2514  */
2515 void
2516 process_deferred_inactive(struct mount *mp)
2517 {
2518 	struct vnode *vp, *mvp;
2519 	struct inode *ip;
2520 	struct thread *td;
2521 	int error;
2522 
2523 	td = curthread;
2524 	(void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2525  loop:
2526 	MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2527 		/*
2528 		 * IN_LAZYACCESS is checked here without holding any
2529 		 * vnode lock, but this flag is set only while holding
2530 		 * vnode interlock.
2531 		 */
2532 		if (vp->v_type == VNON ||
2533 		    ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2534 		    ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
2535 			VI_UNLOCK(vp);
2536 			continue;
2537 		}
2538 		vholdl(vp);
2539 		error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2540 		if (error != 0) {
2541 			vdrop(vp);
2542 			if (error == ENOENT)
2543 				continue;	/* vnode recycled */
2544 			MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2545 			goto loop;
2546 		}
2547 		ip = VTOI(vp);
2548 		if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2549 			ip->i_flag &= ~IN_LAZYACCESS;
2550 			ip->i_flag |= IN_MODIFIED;
2551 		}
2552 		VI_LOCK(vp);
2553 		if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2554 			VI_UNLOCK(vp);
2555 			VOP_UNLOCK(vp, 0);
2556 			vdrop(vp);
2557 			continue;
2558 		}
2559 		vinactive(vp, td);
2560 		VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2561 			 ("process_deferred_inactive: got VI_OWEINACT"));
2562 		VI_UNLOCK(vp);
2563 		VOP_UNLOCK(vp, 0);
2564 		vdrop(vp);
2565 	}
2566 	vn_finished_secondary_write(mp);
2567 }
2568 
2569 #ifndef NO_FFS_SNAPSHOT
2570 
2571 static struct snapdata *
2572 ffs_snapdata_alloc(void)
2573 {
2574 	struct snapdata *sn;
2575 
2576 	/*
2577 	 * Fetch a snapdata from the free list if there is one available.
2578 	 */
2579 	mtx_lock(&snapfree_lock);
2580 	sn = LIST_FIRST(&snapfree);
2581 	if (sn != NULL)
2582 		LIST_REMOVE(sn, sn_link);
2583 	mtx_unlock(&snapfree_lock);
2584 	if (sn != NULL)
2585 		return (sn);
2586 	/*
2587  	 * If there were no free snapdatas allocate one.
2588 	 */
2589 	sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2590 	TAILQ_INIT(&sn->sn_head);
2591 	lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2592 	    LK_CANRECURSE | LK_NOSHARE);
2593 	return (sn);
2594 }
2595 
2596 /*
2597  * The snapdata is never freed because we can not be certain that
2598  * there are no threads sleeping on the snap lock.  Persisting
2599  * them permanently avoids costly synchronization in ffs_lock().
2600  */
2601 static void
2602 ffs_snapdata_free(struct snapdata *sn)
2603 {
2604 	mtx_lock(&snapfree_lock);
2605 	LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2606 	mtx_unlock(&snapfree_lock);
2607 }
2608 
2609 /* Try to free snapdata associated with devvp */
2610 static void
2611 try_free_snapdata(struct vnode *devvp)
2612 {
2613 	struct snapdata *sn;
2614 	ufs2_daddr_t *snapblklist;
2615 
2616 	ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2617 	sn = devvp->v_rdev->si_snapdata;
2618 
2619 	if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2620 	    (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2621 		VI_UNLOCK(devvp);
2622 		return;
2623 	}
2624 
2625 	devvp->v_rdev->si_snapdata = NULL;
2626 	devvp->v_vflag &= ~VV_COPYONWRITE;
2627 	lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2628 	snapblklist = sn->sn_blklist;
2629 	sn->sn_blklist = NULL;
2630 	sn->sn_listsize = 0;
2631 	lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2632 	if (snapblklist != NULL)
2633 		free(snapblklist, M_UFSMNT);
2634 	ffs_snapdata_free(sn);
2635 }
2636 
2637 static struct snapdata *
2638 ffs_snapdata_acquire(struct vnode *devvp)
2639 {
2640 	struct snapdata *nsn;
2641 	struct snapdata *sn;
2642 
2643 	/*
2644  	 * Allocate a free snapdata.  This is done before acquiring the
2645 	 * devvp lock to avoid allocation while the devvp interlock is
2646 	 * held.
2647 	 */
2648 	nsn = ffs_snapdata_alloc();
2649 	/*
2650 	 * If there snapshots already exist on this filesystem grab a
2651 	 * reference to the shared lock.  Otherwise this is the first
2652 	 * snapshot on this filesystem and we need to use our
2653 	 * pre-allocated snapdata.
2654 	 */
2655 	VI_LOCK(devvp);
2656 	if (devvp->v_rdev->si_snapdata == NULL) {
2657 		devvp->v_rdev->si_snapdata = nsn;
2658 		nsn = NULL;
2659 	}
2660 	sn = devvp->v_rdev->si_snapdata;
2661 	/*
2662 	 * Acquire the snapshot lock.
2663 	 */
2664 	lockmgr(&sn->sn_lock,
2665 	    LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, VI_MTX(devvp));
2666 	/*
2667 	 * Free any unused snapdata.
2668 	 */
2669 	if (nsn != NULL)
2670 		ffs_snapdata_free(nsn);
2671 
2672 	return (sn);
2673 }
2674 
2675 #endif
2676