2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2000 Marshall Kirk McKusick. All Rights Reserved.
6 * Further information about snapshots can be obtained from:
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
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
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.
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
35 * @(#)ffs_snapshot.c 8.11 (McKusick) 7/23/00
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_quota.h"
43 #include <sys/param.h>
44 #include <sys/kernel.h>
45 #include <sys/systm.h>
49 #include <sys/fcntl.h>
51 #include <sys/namei.h>
52 #include <sys/sched.h>
54 #include <sys/malloc.h>
55 #include <sys/mount.h>
56 #include <sys/resource.h>
57 #include <sys/resourcevar.h>
58 #include <sys/rwlock.h>
59 #include <sys/vnode.h>
61 #include <geom/geom.h>
63 #include <ufs/ufs/extattr.h>
64 #include <ufs/ufs/quota.h>
65 #include <ufs/ufs/ufsmount.h>
66 #include <ufs/ufs/inode.h>
67 #include <ufs/ufs/ufs_extern.h>
69 #include <ufs/ffs/fs.h>
70 #include <ufs/ffs/ffs_extern.h>
72 #define KERNCRED thread0.td_ucred
77 #ifdef NO_FFS_SNAPSHOT
79 ffs_snapshot(mp, snapfile)
87 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
94 struct workhead *wkhd;
106 ffs_snapshot_mount(mp)
112 ffs_snapshot_unmount(mp)
124 ffs_copyonwrite(devvp, bp)
132 ffs_sync_snap(mp, waitfor)
139 FEATURE(ffs_snapshot, "FFS snapshot support");
141 LIST_HEAD(, snapdata) snapfree;
142 static struct mtx snapfree_lock;
143 MTX_SYSINIT(ffs_snapfree, &snapfree_lock, "snapdata free list", MTX_DEF);
145 static int cgaccount(int, struct vnode *, struct buf *, int);
146 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
147 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
148 ufs_lbn_t, int), int, int);
149 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
150 ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
151 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
152 ufs_lbn_t, int), int);
153 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
154 struct fs *, ufs_lbn_t, int);
155 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
156 struct fs *, ufs_lbn_t, int);
157 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
158 struct fs *, ufs_lbn_t, int);
159 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
160 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
161 ufs_lbn_t, int), int, int);
162 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
163 ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
164 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
165 ufs_lbn_t, int), int);
166 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
167 struct fs *, ufs_lbn_t, int);
168 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
169 struct fs *, ufs_lbn_t, int);
170 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
171 struct fs *, ufs_lbn_t, int);
172 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
173 static void try_free_snapdata(struct vnode *devvp);
174 static struct snapdata *ffs_snapdata_acquire(struct vnode *devvp);
175 static int ffs_bp_snapblk(struct vnode *, struct buf *);
178 * To ensure the consistency of snapshots across crashes, we must
179 * synchronously write out copied blocks before allowing the
180 * originals to be modified. Because of the rather severe speed
181 * penalty that this imposes, the code normally only ensures
182 * persistence for the filesystem metadata contained within a
183 * snapshot. Setting the following flag allows this crash
184 * persistence to be enabled for file contents.
186 int dopersistence = 0;
189 #include <sys/sysctl.h>
190 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
191 static int snapdebug = 0;
192 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
193 int collectsnapstats = 0;
194 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
199 * Create a snapshot file and initialize it for the filesystem.
202 ffs_snapshot(mp, snapfile)
206 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
207 int error, cg, snaploc;
208 int i, size, len, loc;
209 ufs2_daddr_t blockno;
211 struct timespec starttime = {0, 0}, endtime;
213 long redo = 0, snaplistsize = 0;
216 struct fs *copy_fs = NULL, *fs;
217 struct thread *td = curthread;
218 struct inode *ip, *xp;
219 struct buf *bp, *nbp, *ibp;
223 struct vnode *vp, *xvp, *mvp, *devvp;
227 struct ufsmount *ump;
233 * At the moment, journaled soft updates cannot support
236 if (MOUNTEDSUJ(mp)) {
237 vfs_mount_error(mp, "%s: Snapshots are not yet supported when "
238 "running with journaled soft updates", fs->fs_fsmnt);
245 * Need to serialize access to snapshot code per filesystem.
248 * Assign a snapshot slot in the superblock.
251 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
252 if (fs->fs_snapinum[snaploc] == 0)
255 if (snaploc == FSMAXSNAP)
258 * Create the snapshot file.
261 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF | NOCACHE, UIO_SYSSPACE,
263 if ((error = namei(&nd)) != 0)
265 if (nd.ni_vp != NULL) {
269 if (nd.ni_dvp->v_mount != mp)
272 NDFREE(&nd, NDF_ONLY_PNBUF);
273 if (nd.ni_dvp == nd.ni_vp)
281 vat.va_mode = S_IRUSR;
282 vat.va_vaflags |= VA_EXCLUSIVE;
283 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
286 panic("ffs_snapshot: mount mismatch");
288 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
289 NDFREE(&nd, NDF_ONLY_PNBUF);
291 if ((error = vn_start_write(NULL, &wrtmp,
292 V_XSLEEP | PCATCH)) != 0)
296 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
297 VOP_UNLOCK(nd.ni_dvp, 0);
299 NDFREE(&nd, NDF_ONLY_PNBUF);
300 vn_finished_write(wrtmp);
305 vnode_create_vobject(nd.ni_vp, fs->fs_size, td);
306 vp->v_vflag |= VV_SYSTEM;
308 devvp = ITODEVVP(ip);
310 * Allocate and copy the last block contents so as to be able
311 * to set size to that of the filesystem.
313 numblks = howmany(fs->fs_size, fs->fs_frag);
314 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
315 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
318 ip->i_size = lblktosize(fs, (off_t)numblks);
319 DIP_SET(ip, i_size, ip->i_size);
320 ip->i_flag |= IN_SIZEMOD | IN_CHANGE | IN_UPDATE;
321 error = readblock(vp, bp, numblks - 1);
326 * Preallocate critical data structures so that we can copy
327 * them in without further allocation after we suspend all
328 * operations on the filesystem. We would like to just release
329 * the allocated buffers without writing them since they will
330 * be filled in below once we are ready to go, but this upsets
331 * the soft update code, so we go ahead and write the new buffers.
333 * Allocate all indirect blocks and mark all of them as not
334 * needing to be copied.
336 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
337 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
338 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
344 * Allocate copies for the superblock and its summary information.
346 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
351 blkno = fragstoblks(fs, fs->fs_csaddr);
352 len = howmany(fs->fs_cssize, fs->fs_bsize);
353 for (loc = 0; loc < len; loc++) {
354 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
355 fs->fs_bsize, KERNCRED, 0, &nbp);
361 * Allocate all cylinder group blocks.
363 for (cg = 0; cg < fs->fs_ncg; cg++) {
364 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
365 fs->fs_bsize, KERNCRED, 0, &nbp);
370 ffs_syncvnode(vp, MNT_WAIT, 0);
373 * Copy all the cylinder group maps. Although the
374 * filesystem is still active, we hope that only a few
375 * cylinder groups will change between now and when we
376 * suspend operations. Thus, we will be able to quickly
377 * touch up the few cylinder groups that changed during
378 * the suspension period.
380 len = howmany(fs->fs_ncg, NBBY);
381 space = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
383 fs->fs_active = space;
385 for (cg = 0; cg < fs->fs_ncg; cg++) {
386 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
387 fs->fs_bsize, KERNCRED, 0, &nbp);
390 error = cgaccount(cg, vp, nbp, 1);
393 ffs_syncvnode(vp, MNT_WAIT, 0);
398 * Change inode to snapshot type file.
400 ip->i_flags |= SF_SNAPSHOT;
401 DIP_SET(ip, i_flags, ip->i_flags);
402 ip->i_flag |= IN_CHANGE | IN_UPDATE;
404 * Ensure that the snapshot is completely on disk.
405 * Since we have marked it as a snapshot it is safe to
406 * unlock it as no process will be allowed to write to it.
408 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
412 * All allocations are done, so we can now snapshot the system.
414 * Recind nice scheduling while running with the filesystem suspended.
416 if (td->td_proc->p_nice > 0) {
421 saved_nice = p->p_nice;
426 * Suspend operation on filesystem.
429 vn_finished_write(wrtmp);
430 if ((error = vfs_write_suspend(vp->v_mount, 0)) != 0) {
431 vn_start_write(NULL, &wrtmp, V_WAIT);
432 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
435 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
437 vn_start_write(NULL, &wrtmp, V_WAIT);
439 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
440 if (ip->i_effnlink == 0) {
441 error = ENOENT; /* Snapshot file unlinked */
444 if (collectsnapstats)
445 nanotime(&starttime);
447 /* The last block might have changed. Copy it again to be sure. */
448 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
449 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
452 error = readblock(vp, bp, numblks - 1);
453 bp->b_flags |= B_VALIDSUSPWRT;
458 * First, copy all the cylinder group maps that have changed.
460 for (cg = 0; cg < fs->fs_ncg; cg++) {
461 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
464 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
465 fs->fs_bsize, KERNCRED, 0, &nbp);
468 error = cgaccount(cg, vp, nbp, 2);
474 * Grab a copy of the superblock and its summary information.
475 * We delay writing it until the suspension is released below.
477 copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK);
478 bcopy(fs, copy_fs, fs->fs_sbsize);
479 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
480 copy_fs->fs_clean = 1;
481 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
482 if (fs->fs_sbsize < size)
483 bzero(&((char *)copy_fs)[fs->fs_sbsize],
484 size - fs->fs_sbsize);
485 size = blkroundup(fs, fs->fs_cssize);
486 if (fs->fs_contigsumsize > 0)
487 size += fs->fs_ncg * sizeof(int32_t);
488 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
489 copy_fs->fs_csp = space;
490 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
491 space = (char *)space + fs->fs_cssize;
492 loc = howmany(fs->fs_cssize, fs->fs_fsize);
493 i = fs->fs_frag - loc % fs->fs_frag;
494 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
496 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
497 len, KERNCRED, &bp)) != 0) {
499 free(copy_fs->fs_csp, M_UFSMNT);
500 free(copy_fs, M_UFSMNT);
504 bcopy(bp->b_data, space, (u_int)len);
505 space = (char *)space + len;
506 bp->b_flags |= B_INVAL | B_NOCACHE;
509 if (fs->fs_contigsumsize > 0) {
510 copy_fs->fs_maxcluster = lp = space;
511 for (i = 0; i < fs->fs_ncg; i++)
512 *lp++ = fs->fs_contigsumsize;
515 * We must check for active files that have been unlinked
516 * (e.g., with a zero link count). We have to expunge all
517 * trace of these files from the snapshot so that they are
518 * not reclaimed prematurely by fsck or unnecessarily dumped.
519 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
520 * spec_strategy about writing on a suspended filesystem.
521 * Note that we skip unlinked snapshot files as they will
522 * be handled separately below.
524 * We also calculate the needed size for the snapshot list.
526 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
527 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
529 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
532 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
533 if ((xvp->v_usecount == 0 &&
534 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
535 xvp->v_type == VNON ||
536 IS_SNAPSHOT(VTOI(xvp))) {
541 * We can skip parent directory vnode because it must have
542 * this snapshot file in it.
544 if (xvp == nd.ni_dvp) {
549 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
550 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
555 if (xvp->v_usecount == 0 &&
556 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
564 vn_printf(xvp, "ffs_snapshot: busy vnode ");
565 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
572 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
578 * If there is a fragment, clear it here.
581 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
582 if (loc < UFS_NDADDR) {
583 len = fragroundup(fs, blkoff(fs, xp->i_size));
584 if (len != 0 && len < fs->fs_bsize) {
585 ffs_blkfree(ump, copy_fs, vp,
586 DIP(xp, i_db[loc]), len, xp->i_number,
587 xvp->v_type, NULL, SINGLETON_KEY);
588 blkno = DIP(xp, i_db[loc]);
589 DIP_SET(xp, i_db[loc], 0);
594 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
597 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
600 DIP_SET(xp, i_db[loc], blkno);
602 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
607 free(copy_fs->fs_csp, M_UFSMNT);
608 free(copy_fs, M_UFSMNT);
610 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
615 * Erase the journal file from the snapshot.
617 if (fs->fs_flags & FS_SUJ) {
618 error = softdep_journal_lookup(mp, &xvp);
620 free(copy_fs->fs_csp, M_UFSMNT);
621 free(copy_fs, M_UFSMNT);
627 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
630 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
635 * Acquire a lock on the snapdata structure, creating it if necessary.
637 sn = ffs_snapdata_acquire(devvp);
639 * Change vnode to use shared snapshot lock instead of the original
642 vp->v_vnlock = &sn->sn_lock;
643 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
644 xp = TAILQ_FIRST(&sn->sn_head);
646 * If this is the first snapshot on this filesystem, then we need
647 * to allocate the space for the list of preallocated snapshot blocks.
648 * This list will be refined below, but this preliminary one will
649 * keep us out of deadlock until the full one is ready.
652 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
654 blkp = &snapblklist[1];
655 *blkp++ = lblkno(fs, fs->fs_sblockloc);
656 blkno = fragstoblks(fs, fs->fs_csaddr);
657 for (cg = 0; cg < fs->fs_ncg; cg++) {
658 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
660 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
662 len = howmany(fs->fs_cssize, fs->fs_bsize);
663 for (loc = 0; loc < len; loc++)
664 *blkp++ = blkno + loc;
665 for (; cg < fs->fs_ncg; cg++)
666 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
667 snapblklist[0] = blkp - snapblklist;
669 if (sn->sn_blklist != NULL)
670 panic("ffs_snapshot: non-empty list");
671 sn->sn_blklist = snapblklist;
672 sn->sn_listsize = blkp - snapblklist;
676 * Record snapshot inode. Since this is the newest snapshot,
677 * it must be placed at the end of the list.
680 fs->fs_snapinum[snaploc] = ip->i_number;
681 if (ip->i_nextsnap.tqe_prev != 0)
682 panic("ffs_snapshot: %ju already on list",
683 (uintmax_t)ip->i_number);
684 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
685 devvp->v_vflag |= VV_COPYONWRITE;
687 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
689 KASSERT((sn != NULL && copy_fs != NULL && error == 0) ||
690 (sn == NULL && copy_fs == NULL && error != 0),
691 ("email phk@ and mckusick@"));
693 * Resume operation on filesystem.
695 vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR);
696 if (collectsnapstats && starttime.tv_sec > 0) {
698 timespecsub(&endtime, &starttime, &endtime);
699 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
700 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
701 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
706 * Copy allocation information from all the snapshots in
707 * this snapshot and then expunge them from its view.
709 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
713 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
716 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
718 if (error == 0 && xp->i_effnlink == 0) {
719 error = ffs_freefile(ump,
726 fs->fs_snapinum[snaploc] = 0;
731 * Allocate space for the full list of preallocated snapshot blocks.
733 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
735 ip->i_snapblklist = &snapblklist[1];
737 * Expunge the blocks used by the snapshots from the set of
738 * blocks marked as used in the snapshot bitmaps. Also, collect
739 * the list of allocated blocks in i_snapblklist.
742 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
745 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
748 fs->fs_snapinum[snaploc] = 0;
749 free(snapblklist, M_UFSMNT);
752 if (snaplistsize < ip->i_snapblklist - snapblklist)
753 panic("ffs_snapshot: list too small");
754 snaplistsize = ip->i_snapblklist - snapblklist;
755 snapblklist[0] = snaplistsize;
756 ip->i_snapblklist = 0;
758 * Write out the list of allocated blocks to the end of the snapshot.
760 auio.uio_iov = &aiov;
762 aiov.iov_base = (void *)snapblklist;
763 aiov.iov_len = snaplistsize * sizeof(daddr_t);
764 auio.uio_resid = aiov.iov_len;
765 auio.uio_offset = ip->i_size;
766 auio.uio_segflg = UIO_SYSSPACE;
767 auio.uio_rw = UIO_WRITE;
769 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
770 fs->fs_snapinum[snaploc] = 0;
771 free(snapblklist, M_UFSMNT);
775 * Write the superblock and its summary information
778 blkno = fragstoblks(fs, fs->fs_csaddr);
779 len = howmany(fs->fs_cssize, fs->fs_bsize);
780 space = copy_fs->fs_csp;
781 for (loc = 0; loc < len; loc++) {
782 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
785 fs->fs_snapinum[snaploc] = 0;
786 free(snapblklist, M_UFSMNT);
789 bcopy(space, nbp->b_data, fs->fs_bsize);
790 space = (char *)space + fs->fs_bsize;
793 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
798 loc = blkoff(fs, fs->fs_sblockloc);
799 bcopy((char *)copy_fs, &nbp->b_data[loc], (u_int)fs->fs_sbsize);
803 * As this is the newest list, it is the most inclusive, so
804 * should replace the previous list.
807 space = sn->sn_blklist;
808 sn->sn_blklist = snapblklist;
809 sn->sn_listsize = snaplistsize;
812 free(space, M_UFSMNT);
814 * Preallocate all the direct blocks in the snapshot inode so
815 * that we never have to write the inode itself to commit an
816 * update to the contents of the snapshot. Note that once
817 * created, the size of the snapshot will never change, so
818 * there will never be a need to write the inode except to
819 * update the non-integrity-critical time fields and
820 * allocated-block count.
822 for (blockno = 0; blockno < UFS_NDADDR; blockno++) {
823 if (DIP(ip, i_db[blockno]) != 0)
825 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
826 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
829 error = readblock(vp, bp, blockno);
835 free(copy_fs->fs_csp, M_UFSMNT);
836 free(copy_fs, M_UFSMNT);
839 NDFREE(&nd, NDF_ONLY_PNBUF);
840 if (saved_nice > 0) {
845 sched_nice(td->td_proc, saved_nice);
846 PROC_UNLOCK(td->td_proc);
849 if (fs->fs_active != 0) {
850 free(fs->fs_active, M_DEVBUF);
855 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
858 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED);
859 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
865 vn_finished_write(wrtmp);
866 process_deferred_inactive(mp);
871 * Copy a cylinder group map. All the unallocated blocks are marked
872 * BLK_NOCOPY so that the snapshot knows that it need not copy them
873 * if they are later written. If passno is one, then this is a first
874 * pass, so only setting needs to be done. If passno is 2, then this
875 * is a revision to a previous pass which must be undone as the
876 * replacement pass is done.
879 cgaccount(cg, vp, nbp, passno)
885 struct buf *bp, *ibp;
889 ufs2_daddr_t base, numblks;
890 int error, len, loc, indiroff;
894 if ((error = ffs_getcg(fs, ITODEVVP(ip), cg, &bp, &cgp)) != 0)
896 UFS_LOCK(ITOUMP(ip));
899 * Recomputation of summary information might not have been performed
900 * at mount time. Sync up summary information for current cylinder
901 * group while data is in memory to ensure that result of background
902 * fsck is slightly more consistent.
904 fs->fs_cs(fs, cg) = cgp->cg_cs;
905 UFS_UNLOCK(ITOUMP(ip));
906 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
907 if (fs->fs_cgsize < fs->fs_bsize)
908 bzero(&nbp->b_data[fs->fs_cgsize],
909 fs->fs_bsize - fs->fs_cgsize);
910 cgp = (struct cg *)nbp->b_data;
913 nbp->b_flags |= B_VALIDSUSPWRT;
914 numblks = howmany(fs->fs_size, fs->fs_frag);
915 len = howmany(fs->fs_fpg, fs->fs_frag);
916 base = cgbase(fs, cg) / fs->fs_frag;
917 if (base + len >= numblks)
918 len = numblks - base - 1;
920 if (base < UFS_NDADDR) {
921 for ( ; loc < UFS_NDADDR; loc++) {
922 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
923 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
924 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
925 DIP_SET(ip, i_db[loc], 0);
926 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
927 panic("ffs_snapshot: lost direct block");
930 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
931 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
935 indiroff = (base + loc - UFS_NDADDR) % NINDIR(fs);
936 for ( ; loc < len; loc++, indiroff++) {
937 if (indiroff >= NINDIR(fs)) {
939 ibp->b_flags |= B_VALIDSUSPWRT;
941 error = UFS_BALLOC(vp,
942 lblktosize(fs, (off_t)(base + loc)),
943 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
950 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
951 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
953 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
954 [indiroff] == BLK_NOCOPY)
955 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
956 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
957 [indiroff] == BLK_NOCOPY)
958 panic("ffs_snapshot: lost indirect block");
961 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
962 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
963 else if (passno == 2 &&
964 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
965 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
966 else if (passno == 1 &&
967 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
968 panic("ffs_snapshot: lost indirect block");
971 ibp->b_flags |= B_VALIDSUSPWRT;
975 * We have to calculate the crc32c here rather than just setting the
976 * BX_CYLGRP b_xflags because the allocation of the block for the
977 * the cylinder group map will always be a full size block (fs_bsize)
978 * even though the cylinder group may be smaller (fs_cgsize). The
979 * crc32c must be computed only over fs_cgsize whereas the BX_CYLGRP
980 * flag causes it to be computed over the size of the buffer.
982 if ((fs->fs_metackhash & CK_CYLGRP) != 0) {
983 ((struct cg *)nbp->b_data)->cg_ckhash = 0;
984 ((struct cg *)nbp->b_data)->cg_ckhash =
985 calculate_crc32c(~0L, nbp->b_data, fs->fs_cgsize);
991 * Before expunging a snapshot inode, note all the
992 * blocks that it claims with BLK_SNAP so that fsck will
993 * be able to account for those blocks properly and so
994 * that this snapshot knows that it need not copy them
995 * if the other snapshot holding them is freed. This code
996 * is reproduced once each for UFS1 and UFS2.
999 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1000 struct vnode *snapvp;
1001 struct inode *cancelip;
1003 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1004 struct fs *, ufs_lbn_t, int);
1008 int i, error, indiroff;
1009 ufs_lbn_t lbn, rlbn;
1010 ufs2_daddr_t len, blkno, numblks, blksperindir;
1011 struct ufs1_dinode *dip;
1012 struct thread *td = curthread;
1016 * Prepare to expunge the inode. If its inode block has not
1017 * yet been copied, then allocate and fill the copy.
1019 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1021 if (lbn < UFS_NDADDR) {
1022 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1024 if (DOINGSOFTDEP(snapvp))
1025 softdep_prealloc(snapvp, MNT_WAIT);
1026 td->td_pflags |= TDP_COWINPROGRESS;
1027 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1028 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1029 td->td_pflags &= ~TDP_COWINPROGRESS;
1032 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1033 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1037 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1040 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1041 fs->fs_bsize, KERNCRED, 0, &bp);
1044 if ((error = readblock(snapvp, bp, lbn)) != 0)
1048 * Set a snapshot inode to be a zero length file, regular files
1049 * or unlinked snapshots to be completely unallocated.
1051 dip = (struct ufs1_dinode *)bp->b_data +
1052 ino_to_fsbo(fs, cancelip->i_number);
1053 if (clearmode || cancelip->i_effnlink == 0)
1057 dip->di_flags &= ~SF_SNAPSHOT;
1058 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs1_daddr_t));
1061 * Now go through and expunge all the blocks in the file
1062 * using the function requested.
1064 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1065 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1066 &cancelip->i_din1->di_db[UFS_NDADDR], fs, 0, expungetype)))
1068 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1069 &cancelip->i_din1->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1073 len = numblks - UFS_NDADDR;
1075 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1076 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1077 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1078 blksperindir, fs, acctfunc, expungetype);
1081 blksperindir *= NINDIR(fs);
1082 lbn -= blksperindir + 1;
1083 len -= blksperindir;
1084 rlbn += blksperindir;
1090 * Descend an indirect block chain for vnode cancelvp accounting for all
1091 * its indirect blocks in snapvp.
1094 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1095 blksperindir, fs, acctfunc, expungetype)
1096 struct vnode *snapvp;
1097 struct vnode *cancelvp;
1103 ufs_lbn_t blksperindir;
1105 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1106 struct fs *, ufs_lbn_t, int);
1110 ufs_lbn_t subblksperindir;
1111 struct indir indirs[UFS_NIADDR + 2];
1112 ufs1_daddr_t last, *bap;
1116 if (expungetype == BLK_NOCOPY)
1118 panic("indiracct_ufs1: missing indir");
1120 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1122 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1123 panic("indiracct_ufs1: botched params");
1125 * We have to expand bread here since it will deadlock looking
1126 * up the block number for any blocks that are not in the cache.
1128 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1129 bp->b_blkno = fsbtodb(fs, blkno);
1130 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1131 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1136 * Account for the block pointers in this indirect block.
1138 last = howmany(remblks, blksperindir);
1139 if (last > NINDIR(fs))
1141 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1142 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1144 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1145 level == 0 ? rlbn : -1, expungetype);
1146 if (error || level == 0)
1149 * Account for the block pointers in each of the indirect blocks
1150 * in the levels below us.
1152 subblksperindir = blksperindir / NINDIR(fs);
1153 for (lbn++, level--, i = 0; i < last; i++) {
1154 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1155 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1158 rlbn += blksperindir;
1159 lbn -= blksperindir;
1160 remblks -= blksperindir;
1163 free(bap, M_DEVBUF);
1168 * Do both snap accounting and map accounting.
1171 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1173 ufs1_daddr_t *oldblkp, *lastblkp;
1176 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1180 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1182 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1186 * Identify a set of blocks allocated in a snapshot inode.
1189 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1191 ufs1_daddr_t *oldblkp, *lastblkp;
1194 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1196 struct inode *ip = VTOI(vp);
1197 ufs1_daddr_t blkno, *blkp;
1202 for ( ; oldblkp < lastblkp; oldblkp++) {
1204 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1206 lbn = fragstoblks(fs, blkno);
1207 if (lbn < UFS_NDADDR) {
1208 blkp = &ip->i_din1->di_db[lbn];
1209 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1211 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1212 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1215 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1216 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1219 * If we are expunging a snapshot vnode and we
1220 * find a block marked BLK_NOCOPY, then it is
1221 * one that has been allocated to this snapshot after
1222 * we took our current snapshot and can be ignored.
1224 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1225 if (lbn >= UFS_NDADDR)
1229 panic("snapacct_ufs1: bad block");
1230 *blkp = expungetype;
1231 if (lbn >= UFS_NDADDR)
1239 * Account for a set of blocks allocated in a snapshot inode.
1242 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1244 ufs1_daddr_t *oldblkp, *lastblkp;
1255 inum = ip->i_number;
1260 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1262 if (blkno == 0 || blkno == BLK_NOCOPY)
1264 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1265 *ip->i_snapblklist++ = lblkno;
1266 if (blkno == BLK_SNAP)
1267 blkno = blkstofrags(fs, lblkno);
1268 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1269 vp->v_type, NULL, SINGLETON_KEY);
1275 * Before expunging a snapshot inode, note all the
1276 * blocks that it claims with BLK_SNAP so that fsck will
1277 * be able to account for those blocks properly and so
1278 * that this snapshot knows that it need not copy them
1279 * if the other snapshot holding them is freed. This code
1280 * is reproduced once each for UFS1 and UFS2.
1283 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1284 struct vnode *snapvp;
1285 struct inode *cancelip;
1287 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1288 struct fs *, ufs_lbn_t, int);
1292 int i, error, indiroff;
1293 ufs_lbn_t lbn, rlbn;
1294 ufs2_daddr_t len, blkno, numblks, blksperindir;
1295 struct ufs2_dinode *dip;
1296 struct thread *td = curthread;
1300 * Prepare to expunge the inode. If its inode block has not
1301 * yet been copied, then allocate and fill the copy.
1303 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1305 if (lbn < UFS_NDADDR) {
1306 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1308 if (DOINGSOFTDEP(snapvp))
1309 softdep_prealloc(snapvp, MNT_WAIT);
1310 td->td_pflags |= TDP_COWINPROGRESS;
1311 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1312 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1313 td->td_pflags &= ~TDP_COWINPROGRESS;
1316 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1317 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1321 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1324 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1325 fs->fs_bsize, KERNCRED, 0, &bp);
1328 if ((error = readblock(snapvp, bp, lbn)) != 0)
1332 * Set a snapshot inode to be a zero length file, regular files
1333 * to be completely unallocated.
1335 dip = (struct ufs2_dinode *)bp->b_data +
1336 ino_to_fsbo(fs, cancelip->i_number);
1337 if (clearmode || cancelip->i_effnlink == 0)
1341 dip->di_flags &= ~SF_SNAPSHOT;
1342 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs2_daddr_t));
1345 * Now go through and expunge all the blocks in the file
1346 * using the function requested.
1348 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1349 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1350 &cancelip->i_din2->di_db[UFS_NDADDR], fs, 0, expungetype)))
1352 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1353 &cancelip->i_din2->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1357 len = numblks - UFS_NDADDR;
1359 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1360 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1361 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1362 blksperindir, fs, acctfunc, expungetype);
1365 blksperindir *= NINDIR(fs);
1366 lbn -= blksperindir + 1;
1367 len -= blksperindir;
1368 rlbn += blksperindir;
1374 * Descend an indirect block chain for vnode cancelvp accounting for all
1375 * its indirect blocks in snapvp.
1378 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1379 blksperindir, fs, acctfunc, expungetype)
1380 struct vnode *snapvp;
1381 struct vnode *cancelvp;
1387 ufs_lbn_t blksperindir;
1389 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1390 struct fs *, ufs_lbn_t, int);
1394 ufs_lbn_t subblksperindir;
1395 struct indir indirs[UFS_NIADDR + 2];
1396 ufs2_daddr_t last, *bap;
1400 if (expungetype == BLK_NOCOPY)
1402 panic("indiracct_ufs2: missing indir");
1404 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1406 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1407 panic("indiracct_ufs2: botched params");
1409 * We have to expand bread here since it will deadlock looking
1410 * up the block number for any blocks that are not in the cache.
1412 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1413 bp->b_blkno = fsbtodb(fs, blkno);
1414 if ((bp->b_flags & B_CACHE) == 0 &&
1415 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1420 * Account for the block pointers in this indirect block.
1422 last = howmany(remblks, blksperindir);
1423 if (last > NINDIR(fs))
1425 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1426 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1428 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1429 level == 0 ? rlbn : -1, expungetype);
1430 if (error || level == 0)
1433 * Account for the block pointers in each of the indirect blocks
1434 * in the levels below us.
1436 subblksperindir = blksperindir / NINDIR(fs);
1437 for (lbn++, level--, i = 0; i < last; i++) {
1438 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1439 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1442 rlbn += blksperindir;
1443 lbn -= blksperindir;
1444 remblks -= blksperindir;
1447 free(bap, M_DEVBUF);
1452 * Do both snap accounting and map accounting.
1455 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1457 ufs2_daddr_t *oldblkp, *lastblkp;
1460 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1464 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1466 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1470 * Identify a set of blocks allocated in a snapshot inode.
1473 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1475 ufs2_daddr_t *oldblkp, *lastblkp;
1478 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1480 struct inode *ip = VTOI(vp);
1481 ufs2_daddr_t blkno, *blkp;
1486 for ( ; oldblkp < lastblkp; oldblkp++) {
1488 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1490 lbn = fragstoblks(fs, blkno);
1491 if (lbn < UFS_NDADDR) {
1492 blkp = &ip->i_din2->di_db[lbn];
1493 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1495 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1496 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1499 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1500 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1503 * If we are expunging a snapshot vnode and we
1504 * find a block marked BLK_NOCOPY, then it is
1505 * one that has been allocated to this snapshot after
1506 * we took our current snapshot and can be ignored.
1508 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1509 if (lbn >= UFS_NDADDR)
1513 panic("snapacct_ufs2: bad block");
1514 *blkp = expungetype;
1515 if (lbn >= UFS_NDADDR)
1523 * Account for a set of blocks allocated in a snapshot inode.
1526 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1528 ufs2_daddr_t *oldblkp, *lastblkp;
1539 inum = ip->i_number;
1544 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1546 if (blkno == 0 || blkno == BLK_NOCOPY)
1548 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1549 *ip->i_snapblklist++ = lblkno;
1550 if (blkno == BLK_SNAP)
1551 blkno = blkstofrags(fs, lblkno);
1552 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1553 vp->v_type, NULL, SINGLETON_KEY);
1559 * Decrement extra reference on snapshot when last name is removed.
1560 * It will not be freed until the last open reference goes away.
1569 struct snapdata *sn;
1570 struct ufsmount *ump;
1573 * Find snapshot in incore list.
1576 sn = ITODEVVP(ip)->v_rdev->si_snapdata;
1578 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1584 printf("ffs_snapgone: lost snapshot vnode %ju\n",
1585 (uintmax_t)ip->i_number);
1587 * Delete snapshot inode from superblock. Keep list dense.
1592 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1593 if (fs->fs_snapinum[snaploc] == ip->i_number)
1595 if (snaploc < FSMAXSNAP) {
1596 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1597 if (fs->fs_snapinum[snaploc] == 0)
1599 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1601 fs->fs_snapinum[snaploc - 1] = 0;
1607 * Prepare a snapshot file for being removed.
1614 struct vnode *devvp;
1617 ufs2_daddr_t numblks, blkno, dblk;
1618 int error, i, last, loc;
1619 struct snapdata *sn;
1623 devvp = ITODEVVP(ip);
1625 * If active, delete from incore list (this snapshot may
1626 * already have been in the process of being deleted, so
1627 * would not have been active).
1629 * Clear copy-on-write flag if last snapshot.
1632 if (ip->i_nextsnap.tqe_prev != 0) {
1633 sn = devvp->v_rdev->si_snapdata;
1634 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1635 ip->i_nextsnap.tqe_prev = 0;
1637 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1638 for (i = 0; i < sn->sn_lock.lk_recurse; i++)
1639 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1640 KASSERT(vp->v_vnlock == &sn->sn_lock,
1641 ("ffs_snapremove: lost lock mutation"));
1642 vp->v_vnlock = &vp->v_lock;
1644 while (sn->sn_lock.lk_recurse > 0)
1645 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1646 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1647 try_free_snapdata(devvp);
1651 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1652 * snapshots that want them (see ffs_snapblkfree below).
1654 for (blkno = 1; blkno < UFS_NDADDR; blkno++) {
1655 dblk = DIP(ip, i_db[blkno]);
1658 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1659 DIP_SET(ip, i_db[blkno], 0);
1660 else if ((dblk == blkstofrags(fs, blkno) &&
1661 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, fs->fs_bsize,
1662 ip->i_number, vp->v_type, NULL))) {
1663 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1664 btodb(fs->fs_bsize));
1665 DIP_SET(ip, i_db[blkno], 0);
1668 numblks = howmany(ip->i_size, fs->fs_bsize);
1669 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1670 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1671 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1674 if (fs->fs_size - blkno > NINDIR(fs))
1677 last = fs->fs_size - blkno;
1678 for (loc = 0; loc < last; loc++) {
1679 if (I_IS_UFS1(ip)) {
1680 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1683 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1684 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1685 else if ((dblk == blkstofrags(fs, blkno) &&
1686 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1687 fs->fs_bsize, ip->i_number, vp->v_type,
1689 ip->i_din1->di_blocks -=
1690 btodb(fs->fs_bsize);
1691 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1695 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1698 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1699 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1700 else if ((dblk == blkstofrags(fs, blkno) &&
1701 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1702 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1703 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1704 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1710 * Clear snapshot flag and drop reference.
1712 ip->i_flags &= ~SF_SNAPSHOT;
1713 DIP_SET(ip, i_flags, ip->i_flags);
1714 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1716 * The dirtied indirects must be written out before
1717 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1718 * may find indirect pointers using the magic BLK_* values.
1720 if (DOINGSOFTDEP(vp))
1721 ffs_syncvnode(vp, MNT_WAIT, 0);
1724 * Reenable disk quotas for ex-snapshot file.
1726 if (!getinoquota(ip))
1727 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1732 * Notification that a block is being freed. Return zero if the free
1733 * should be allowed to proceed. Return non-zero if the snapshot file
1734 * wants to claim the block. The block will be claimed if it is an
1735 * uncopied part of one of the snapshots. It will be freed if it is
1736 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1737 * If a fragment is being freed, then all snapshots that care about
1738 * it must make a copy since a snapshot file can only claim full sized
1739 * blocks. Note that if more than one snapshot file maps the block,
1740 * we can pick one at random to claim it. Since none of the snapshots
1741 * can change, we are assurred that they will all see the same unmodified
1742 * image. When deleting a snapshot file (see ffs_snapremove above), we
1743 * must push any of these claimed blocks to one of the other snapshots
1744 * that maps it. These claimed blocks are easily identified as they will
1745 * have a block number equal to their logical block number within the
1746 * snapshot. A copied block can never have this property because they
1747 * must always have been allocated from a BLK_NOCOPY location.
1750 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1752 struct vnode *devvp;
1757 struct workhead *wkhd;
1759 struct buf *ibp, *cbp, *savedcbp = NULL;
1760 struct thread *td = curthread;
1762 struct vnode *vp = NULL;
1765 int indiroff = 0, error = 0, claimedblk = 0;
1766 struct snapdata *sn;
1768 lbn = fragstoblks(fs, bno);
1771 sn = devvp->v_rdev->si_snapdata;
1776 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1777 VI_MTX(devvp)) != 0)
1779 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1781 if (DOINGSOFTDEP(vp))
1782 softdep_prealloc(vp, MNT_WAIT);
1784 * Lookup block being written.
1786 if (lbn < UFS_NDADDR) {
1787 blkno = DIP(ip, i_db[lbn]);
1789 td->td_pflags |= TDP_COWINPROGRESS;
1790 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1791 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1792 td->td_pflags &= ~TDP_COWINPROGRESS;
1795 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1797 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1799 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1802 * Check to see if block needs to be copied.
1806 * A block that we map is being freed. If it has not
1807 * been claimed yet, we will claim or copy it (below).
1810 } else if (blkno == BLK_SNAP) {
1812 * No previous snapshot claimed the block,
1813 * so it will be freed and become a BLK_NOCOPY
1814 * (don't care) for us.
1817 panic("snapblkfree: inconsistent block type");
1818 if (lbn < UFS_NDADDR) {
1819 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1820 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1821 } else if (I_IS_UFS1(ip)) {
1822 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1826 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1831 } else /* BLK_NOCOPY or default */ {
1833 * If the snapshot has already copied the block
1834 * (default), or does not care about the block,
1837 if (lbn >= UFS_NDADDR)
1842 * If this is a full size block, we will just grab it
1843 * and assign it to the snapshot inode. Otherwise we
1844 * will proceed to copy it. See explanation for this
1845 * routine as to why only a single snapshot needs to
1848 if (size == fs->fs_bsize) {
1851 printf("%s %ju lbn %jd from inum %ju\n",
1852 "Grabonremove: snapino",
1853 (uintmax_t)ip->i_number,
1854 (intmax_t)lbn, (uintmax_t)inum);
1857 * If journaling is tracking this write we must add
1858 * the work to the inode or indirect being written.
1861 if (lbn < UFS_NDADDR)
1862 softdep_inode_append(ip,
1863 curthread->td_ucred, wkhd);
1865 softdep_buf_append(ibp, wkhd);
1867 if (lbn < UFS_NDADDR) {
1868 DIP_SET(ip, i_db[lbn], bno);
1869 } else if (I_IS_UFS1(ip)) {
1870 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1873 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1876 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1877 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1878 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1881 if (lbn >= UFS_NDADDR)
1884 * Allocate the block into which to do the copy. Note that this
1885 * allocation will never require any additional allocations for
1886 * the snapshot inode.
1888 td->td_pflags |= TDP_COWINPROGRESS;
1889 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1890 fs->fs_bsize, KERNCRED, 0, &cbp);
1891 td->td_pflags &= ~TDP_COWINPROGRESS;
1896 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n",
1897 "Copyonremove: snapino ", (uintmax_t)ip->i_number,
1898 (intmax_t)lbn, "for inum", (uintmax_t)inum, size,
1899 (intmax_t)cbp->b_blkno);
1902 * If we have already read the old block contents, then
1903 * simply copy them to the new block. Note that we need
1904 * to synchronously write snapshots that have not been
1905 * unlinked, and hence will be visible after a crash,
1906 * to ensure their integrity. At a minimum we ensure the
1907 * integrity of the filesystem metadata, but use the
1908 * dopersistence sysctl-setable flag to decide on the
1909 * persistence needed for file content data.
1911 if (savedcbp != NULL) {
1912 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1914 if ((vtype == VDIR || dopersistence) &&
1916 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1920 * Otherwise, read the old block contents into the buffer.
1922 if ((error = readblock(vp, cbp, lbn)) != 0) {
1923 bzero(cbp->b_data, fs->fs_bsize);
1925 if ((vtype == VDIR || dopersistence) &&
1927 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1933 * Note that we need to synchronously write snapshots that
1934 * have not been unlinked, and hence will be visible after
1935 * a crash, to ensure their integrity. At a minimum we
1936 * ensure the integrity of the filesystem metadata, but
1937 * use the dopersistence sysctl-setable flag to decide on
1938 * the persistence needed for file content data.
1941 vp = savedcbp->b_vp;
1943 if ((vtype == VDIR || dopersistence) &&
1944 VTOI(vp)->i_effnlink > 0)
1945 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1948 * If we have been unable to allocate a block in which to do
1949 * the copy, then return non-zero so that the fragment will
1950 * not be freed. Although space will be lost, the snapshot
1951 * will stay consistent.
1953 if (error != 0 && wkhd != NULL)
1954 softdep_freework(wkhd);
1955 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1960 * Associate snapshot files when mounting.
1963 ffs_snapshot_mount(mp)
1966 struct ufsmount *ump = VFSTOUFS(mp);
1967 struct vnode *devvp = ump->um_devvp;
1968 struct fs *fs = ump->um_fs;
1969 struct thread *td = curthread;
1970 struct snapdata *sn;
1972 struct vnode *lastvp;
1978 daddr_t snaplistsize;
1979 int error, snaploc, loc;
1982 * XXX The following needs to be set before ffs_truncate or
1983 * VOP_READ can be called.
1985 mp->mnt_stat.f_iosize = fs->fs_bsize;
1987 * Process each snapshot listed in the superblock.
1992 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1993 if (fs->fs_snapinum[snaploc] == 0)
1995 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1996 LK_EXCLUSIVE, &vp)) != 0){
1997 printf("ffs_snapshot_mount: vget failed %d\n", error);
2001 if (vp->v_type != VREG) {
2002 reason = "non-file snapshot";
2003 } else if (!IS_SNAPSHOT(ip)) {
2004 reason = "non-snapshot";
2005 } else if (ip->i_size ==
2006 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
2007 reason = "old format snapshot";
2008 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED);
2009 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
2013 if (reason != NULL) {
2014 printf("ffs_snapshot_mount: %s inode %d\n",
2015 reason, fs->fs_snapinum[snaploc]);
2018 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2019 if (fs->fs_snapinum[loc] == 0)
2021 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2023 fs->fs_snapinum[loc - 1] = 0;
2028 * Acquire a lock on the snapdata structure, creating it if
2031 sn = ffs_snapdata_acquire(devvp);
2033 * Change vnode to use shared snapshot lock instead of the
2034 * original private lock.
2036 vp->v_vnlock = &sn->sn_lock;
2037 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2039 * Link it onto the active snapshot list.
2042 if (ip->i_nextsnap.tqe_prev != 0)
2043 panic("ffs_snapshot_mount: %ju already on list",
2044 (uintmax_t)ip->i_number);
2046 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2047 vp->v_vflag |= VV_SYSTEM;
2054 * No usable snapshots found.
2056 if (sn == NULL || vp == NULL)
2059 * Allocate the space for the block hints list. We always want to
2060 * use the list from the newest snapshot.
2062 auio.uio_iov = &aiov;
2063 auio.uio_iovcnt = 1;
2064 aiov.iov_base = (void *)&snaplistsize;
2065 aiov.iov_len = sizeof(snaplistsize);
2066 auio.uio_resid = aiov.iov_len;
2068 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2069 auio.uio_segflg = UIO_SYSSPACE;
2070 auio.uio_rw = UIO_READ;
2072 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2073 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2074 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2078 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2079 M_UFSMNT, M_WAITOK);
2080 auio.uio_iovcnt = 1;
2081 aiov.iov_base = snapblklist;
2082 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2083 auio.uio_resid = aiov.iov_len;
2084 auio.uio_offset -= sizeof(snaplistsize);
2085 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2086 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2088 free(snapblklist, M_UFSMNT);
2093 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2094 sn->sn_listsize = snaplistsize;
2095 sn->sn_blklist = (daddr_t *)snapblklist;
2096 devvp->v_vflag |= VV_COPYONWRITE;
2101 * Disassociate snapshot files when unmounting.
2104 ffs_snapshot_unmount(mp)
2107 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2108 struct snapdata *sn;
2113 sn = devvp->v_rdev->si_snapdata;
2114 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2116 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2117 xp->i_nextsnap.tqe_prev = 0;
2118 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2120 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2121 KASSERT(vp->v_vnlock == &sn->sn_lock,
2122 ("ffs_snapshot_unmount: lost lock mutation"));
2123 vp->v_vnlock = &vp->v_lock;
2124 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2125 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2126 if (xp->i_effnlink > 0)
2129 sn = devvp->v_rdev->si_snapdata;
2131 try_free_snapdata(devvp);
2132 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2136 * Check the buffer block to be belong to device buffer that shall be
2137 * locked after snaplk. devvp shall be locked on entry, and will be
2138 * leaved locked upon exit.
2141 ffs_bp_snapblk(devvp, bp)
2142 struct vnode *devvp;
2145 struct snapdata *sn;
2147 ufs2_daddr_t lbn, *snapblklist;
2148 int lower, upper, mid;
2150 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2151 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2152 sn = devvp->v_rdev->si_snapdata;
2153 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2155 fs = ITOFS(TAILQ_FIRST(&sn->sn_head));
2156 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2157 snapblklist = sn->sn_blklist;
2158 upper = sn->sn_listsize - 1;
2160 while (lower <= upper) {
2161 mid = (lower + upper) / 2;
2162 if (snapblklist[mid] == lbn)
2164 if (snapblklist[mid] < lbn)
2180 struct vnode *vp, *devvp;
2184 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2189 devvp = bo2vnode(bo);
2190 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2193 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2197 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2198 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2203 * Try to find a buffer to flush.
2205 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2206 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2208 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2211 panic("bdwrite: found ourselves");
2214 * Don't countdeps with the bo lock
2217 if (buf_countdeps(nbp, 0)) {
2224 if (!ffs_bp_snapblk(vp, nbp)) {
2232 if (nbp->b_flags & B_CLUSTEROK) {
2233 vfs_bio_awrite(nbp);
2238 dirtybufferflushes++;
2247 * Check for need to copy block that is about to be written,
2248 * copying the block if necessary.
2251 ffs_copyonwrite(devvp, bp)
2252 struct vnode *devvp;
2255 struct snapdata *sn;
2256 struct buf *ibp, *cbp, *savedcbp = NULL;
2257 struct thread *td = curthread;
2260 struct vnode *vp = NULL;
2261 ufs2_daddr_t lbn, blkno, *snapblklist;
2262 int lower, upper, mid, indiroff, error = 0;
2263 int launched_async_io, prev_norunningbuf;
2264 long saved_runningbufspace;
2266 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2267 return (0); /* Update on a snapshot file */
2268 if (td->td_pflags & TDP_COWINPROGRESS)
2269 panic("ffs_copyonwrite: recursive call");
2271 * First check to see if it is in the preallocated list.
2272 * By doing this check we avoid several potential deadlocks.
2275 sn = devvp->v_rdev->si_snapdata;
2277 TAILQ_EMPTY(&sn->sn_head)) {
2279 return (0); /* No snapshot */
2281 ip = TAILQ_FIRST(&sn->sn_head);
2283 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2284 snapblklist = sn->sn_blklist;
2285 upper = sn->sn_listsize - 1;
2287 while (lower <= upper) {
2288 mid = (lower + upper) / 2;
2289 if (snapblklist[mid] == lbn)
2291 if (snapblklist[mid] < lbn)
2296 if (lower <= upper) {
2300 launched_async_io = 0;
2301 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2303 * Since I/O on bp isn't yet in progress and it may be blocked
2304 * for a long time waiting on snaplk, back it out of
2305 * runningbufspace, possibly waking other threads waiting for space.
2307 saved_runningbufspace = bp->b_runningbufspace;
2308 if (saved_runningbufspace != 0)
2309 runningbufwakeup(bp);
2311 * Not in the precomputed list, so check the snapshots.
2313 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2314 VI_MTX(devvp)) != 0) {
2316 sn = devvp->v_rdev->si_snapdata;
2318 TAILQ_EMPTY(&sn->sn_head)) {
2320 if (saved_runningbufspace != 0) {
2321 bp->b_runningbufspace = saved_runningbufspace;
2322 atomic_add_long(&runningbufspace,
2323 bp->b_runningbufspace);
2325 return (0); /* Snapshot gone */
2328 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2330 if (DOINGSOFTDEP(vp))
2331 softdep_prealloc(vp, MNT_WAIT);
2333 * We ensure that everything of our own that needs to be
2334 * copied will be done at the time that ffs_snapshot is
2335 * called. Thus we can skip the check here which can
2336 * deadlock in doing the lookup in UFS_BALLOC.
2341 * Check to see if block needs to be copied. We do not have
2342 * to hold the snapshot lock while doing this lookup as it
2343 * will never require any additional allocations for the
2346 if (lbn < UFS_NDADDR) {
2347 blkno = DIP(ip, i_db[lbn]);
2349 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2350 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2351 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2352 td->td_pflags &= ~TDP_COWINPROGRESS;
2355 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
2357 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2359 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2363 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2364 panic("ffs_copyonwrite: bad copy block");
2369 * Allocate the block into which to do the copy. Since
2370 * multiple processes may all try to copy the same block,
2371 * we have to recheck our need to do a copy if we sleep
2372 * waiting for the lock.
2374 * Because all snapshots on a filesystem share a single
2375 * lock, we ensure that we will never be in competition
2376 * with another process to allocate a block.
2378 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2379 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2380 fs->fs_bsize, KERNCRED, 0, &cbp);
2381 td->td_pflags &= ~TDP_COWINPROGRESS;
2386 printf("Copyonwrite: snapino %ju lbn %jd for ",
2387 (uintmax_t)ip->i_number, (intmax_t)lbn);
2388 if (bp->b_vp == devvp)
2389 printf("fs metadata");
2392 (uintmax_t)VTOI(bp->b_vp)->i_number);
2393 printf(" lblkno %jd to blkno %jd\n",
2394 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2398 * If we have already read the old block contents, then
2399 * simply copy them to the new block. Note that we need
2400 * to synchronously write snapshots that have not been
2401 * unlinked, and hence will be visible after a crash,
2402 * to ensure their integrity. At a minimum we ensure the
2403 * integrity of the filesystem metadata, but use the
2404 * dopersistence sysctl-setable flag to decide on the
2405 * persistence needed for file content data.
2407 if (savedcbp != NULL) {
2408 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2410 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2411 dopersistence) && ip->i_effnlink > 0)
2412 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2414 launched_async_io = 1;
2418 * Otherwise, read the old block contents into the buffer.
2420 if ((error = readblock(vp, cbp, lbn)) != 0) {
2421 bzero(cbp->b_data, fs->fs_bsize);
2423 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2424 dopersistence) && ip->i_effnlink > 0)
2425 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2427 launched_async_io = 1;
2433 * Note that we need to synchronously write snapshots that
2434 * have not been unlinked, and hence will be visible after
2435 * a crash, to ensure their integrity. At a minimum we
2436 * ensure the integrity of the filesystem metadata, but
2437 * use the dopersistence sysctl-setable flag to decide on
2438 * the persistence needed for file content data.
2441 vp = savedcbp->b_vp;
2443 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2444 dopersistence) && VTOI(vp)->i_effnlink > 0)
2445 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2447 launched_async_io = 1;
2449 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2450 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2452 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2453 waitrunningbufspace();
2455 * I/O on bp will now be started, so count it in runningbufspace.
2457 if (saved_runningbufspace != 0) {
2458 bp->b_runningbufspace = saved_runningbufspace;
2459 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2465 * sync snapshots to force freework records waiting on snapshots to claim
2469 ffs_sync_snap(mp, waitfor)
2473 struct snapdata *sn;
2474 struct vnode *devvp;
2478 devvp = VFSTOUFS(mp)->um_devvp;
2479 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2483 sn = devvp->v_rdev->si_snapdata;
2488 if (lockmgr(&sn->sn_lock,
2489 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2490 VI_MTX(devvp)) == 0)
2493 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2495 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2497 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2501 * Read the specified block into the given buffer.
2502 * Much of this boiler-plate comes from bwrite().
2505 readblock(vp, bp, lbn)
2517 bip = g_alloc_bio();
2518 bip->bio_cmd = BIO_READ;
2519 bip->bio_offset = dbtob(fsbtodb(fs, blkstofrags(fs, lbn)));
2520 bip->bio_data = bp->b_data;
2521 bip->bio_length = bp->b_bcount;
2522 bip->bio_done = NULL;
2524 g_io_request(bip, ITODEVVP(ip)->v_bufobj.bo_private);
2525 bp->b_error = biowait(bip, "snaprdb");
2527 return (bp->b_error);
2533 * Process file deletes that were deferred by ufs_inactive() due to
2534 * the file system being suspended. Transfer IN_LAZYACCESS into
2535 * IN_MODIFIED for vnodes that were accessed during suspension.
2538 process_deferred_inactive(struct mount *mp)
2540 struct vnode *vp, *mvp;
2546 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2548 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2550 * IN_LAZYACCESS is checked here without holding any
2551 * vnode lock, but this flag is set only while holding
2554 if (vp->v_type == VNON ||
2555 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2556 ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
2561 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2564 if (error == ENOENT)
2565 continue; /* vnode recycled */
2566 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2570 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2571 ip->i_flag &= ~IN_LAZYACCESS;
2572 ip->i_flag |= IN_MODIFIED;
2575 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2582 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2583 ("process_deferred_inactive: got VI_OWEINACT"));
2588 vn_finished_secondary_write(mp);
2591 #ifndef NO_FFS_SNAPSHOT
2593 static struct snapdata *
2594 ffs_snapdata_alloc(void)
2596 struct snapdata *sn;
2599 * Fetch a snapdata from the free list if there is one available.
2601 mtx_lock(&snapfree_lock);
2602 sn = LIST_FIRST(&snapfree);
2604 LIST_REMOVE(sn, sn_link);
2605 mtx_unlock(&snapfree_lock);
2609 * If there were no free snapdatas allocate one.
2611 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2612 TAILQ_INIT(&sn->sn_head);
2613 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2614 LK_CANRECURSE | LK_NOSHARE);
2619 * The snapdata is never freed because we can not be certain that
2620 * there are no threads sleeping on the snap lock. Persisting
2621 * them permanently avoids costly synchronization in ffs_lock().
2624 ffs_snapdata_free(struct snapdata *sn)
2626 mtx_lock(&snapfree_lock);
2627 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2628 mtx_unlock(&snapfree_lock);
2631 /* Try to free snapdata associated with devvp */
2633 try_free_snapdata(struct vnode *devvp)
2635 struct snapdata *sn;
2636 ufs2_daddr_t *snapblklist;
2638 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2639 sn = devvp->v_rdev->si_snapdata;
2641 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2642 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2647 devvp->v_rdev->si_snapdata = NULL;
2648 devvp->v_vflag &= ~VV_COPYONWRITE;
2649 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2650 snapblklist = sn->sn_blklist;
2651 sn->sn_blklist = NULL;
2652 sn->sn_listsize = 0;
2653 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2654 if (snapblklist != NULL)
2655 free(snapblklist, M_UFSMNT);
2656 ffs_snapdata_free(sn);
2659 static struct snapdata *
2660 ffs_snapdata_acquire(struct vnode *devvp)
2662 struct snapdata *nsn, *sn;
2666 * Allocate a free snapdata. This is done before acquiring the
2667 * devvp lock to avoid allocation while the devvp interlock is
2670 nsn = ffs_snapdata_alloc();
2674 sn = devvp->v_rdev->si_snapdata;
2677 * This is the first snapshot on this
2678 * filesystem and we use our pre-allocated
2679 * snapdata. Publish sn with the sn_lock
2680 * owned by us, to avoid the race.
2682 error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE |
2685 panic("leaked sn, lockmgr error %d", error);
2686 sn = devvp->v_rdev->si_snapdata = nsn;
2693 * There is a snapshots which already exists on this
2694 * filesystem, grab a reference to the common lock.
2696 error = lockmgr(&sn->sn_lock, LK_INTERLOCK |
2697 LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp));
2703 * Free any unused snapdata.
2706 ffs_snapdata_free(nsn);