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