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>
47 #include <sys/gsb_crc32.h>
50 #include <sys/fcntl.h>
52 #include <sys/namei.h>
53 #include <sys/sched.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>
62 #include <geom/geom.h>
64 #include <ufs/ufs/extattr.h>
65 #include <ufs/ufs/quota.h>
66 #include <ufs/ufs/ufsmount.h>
67 #include <ufs/ufs/inode.h>
68 #include <ufs/ufs/ufs_extern.h>
70 #include <ufs/ffs/fs.h>
71 #include <ufs/ffs/ffs_extern.h>
73 #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,
196 #endif /* DIAGNOSTIC */
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;
212 long redo = 0, snaplistsize = 0;
215 struct fs *copy_fs = NULL, *fs;
216 struct thread *td = curthread;
217 struct inode *ip, *xp;
218 struct buf *bp, *nbp, *ibp;
222 struct vnode *vp, *xvp, *mvp, *devvp;
226 struct ufsmount *ump;
228 struct timespec starttime = {0, 0}, endtime;
235 * At the moment, journaled soft updates cannot support
238 if (MOUNTEDSUJ(mp)) {
239 vfs_mount_error(mp, "%s: Snapshots are not yet supported when "
240 "running with journaled soft updates", fs->fs_fsmnt);
247 * Need to serialize access to snapshot code per filesystem.
250 * Assign a snapshot slot in the superblock.
253 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
254 if (fs->fs_snapinum[snaploc] == 0)
257 if (snaploc == FSMAXSNAP)
260 * Create the snapshot file.
263 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF | NOCACHE, UIO_SYSSPACE,
265 if ((error = namei(&nd)) != 0)
267 if (nd.ni_vp != NULL) {
271 if (nd.ni_dvp->v_mount != mp)
274 NDFREE(&nd, NDF_ONLY_PNBUF);
275 if (nd.ni_dvp == nd.ni_vp)
283 vat.va_mode = S_IRUSR;
284 vat.va_vaflags |= VA_EXCLUSIVE;
285 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
288 panic("ffs_snapshot: mount mismatch");
290 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
291 NDFREE(&nd, NDF_ONLY_PNBUF);
293 if ((error = vn_start_write(NULL, &wrtmp,
294 V_XSLEEP | PCATCH)) != 0)
298 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
299 VOP_UNLOCK(nd.ni_dvp, 0);
301 NDFREE(&nd, NDF_ONLY_PNBUF);
302 vn_finished_write(wrtmp);
307 vnode_create_vobject(nd.ni_vp, fs->fs_size, td);
308 vp->v_vflag |= VV_SYSTEM;
310 devvp = ITODEVVP(ip);
312 * Allocate and copy the last block contents so as to be able
313 * to set size to that of the filesystem.
315 numblks = howmany(fs->fs_size, fs->fs_frag);
316 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
317 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
320 ip->i_size = lblktosize(fs, (off_t)numblks);
321 DIP_SET(ip, i_size, ip->i_size);
322 ip->i_flag |= IN_CHANGE | IN_UPDATE;
323 error = readblock(vp, bp, numblks - 1);
328 * Preallocate critical data structures so that we can copy
329 * them in without further allocation after we suspend all
330 * operations on the filesystem. We would like to just release
331 * the allocated buffers without writing them since they will
332 * be filled in below once we are ready to go, but this upsets
333 * the soft update code, so we go ahead and write the new buffers.
335 * Allocate all indirect blocks and mark all of them as not
336 * needing to be copied.
338 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
339 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
340 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
346 * Allocate copies for the superblock and its summary information.
348 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
353 blkno = fragstoblks(fs, fs->fs_csaddr);
354 len = howmany(fs->fs_cssize, fs->fs_bsize);
355 for (loc = 0; loc < len; loc++) {
356 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
357 fs->fs_bsize, KERNCRED, 0, &nbp);
363 * Allocate all cylinder group blocks.
365 for (cg = 0; cg < fs->fs_ncg; cg++) {
366 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
367 fs->fs_bsize, KERNCRED, 0, &nbp);
372 ffs_syncvnode(vp, MNT_WAIT, 0);
375 * Copy all the cylinder group maps. Although the
376 * filesystem is still active, we hope that only a few
377 * cylinder groups will change between now and when we
378 * suspend operations. Thus, we will be able to quickly
379 * touch up the few cylinder groups that changed during
380 * the suspension period.
382 len = howmany(fs->fs_ncg, NBBY);
383 space = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
385 fs->fs_active = space;
387 for (cg = 0; cg < fs->fs_ncg; cg++) {
388 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
389 fs->fs_bsize, KERNCRED, 0, &nbp);
392 error = cgaccount(cg, vp, nbp, 1);
395 ffs_syncvnode(vp, MNT_WAIT, 0);
400 * Change inode to snapshot type file.
402 ip->i_flags |= SF_SNAPSHOT;
403 DIP_SET(ip, i_flags, ip->i_flags);
404 ip->i_flag |= IN_CHANGE | IN_UPDATE;
406 * Ensure that the snapshot is completely on disk.
407 * Since we have marked it as a snapshot it is safe to
408 * unlock it as no process will be allowed to write to it.
410 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
414 * All allocations are done, so we can now snapshot the system.
416 * Recind nice scheduling while running with the filesystem suspended.
418 if (td->td_proc->p_nice > 0) {
423 saved_nice = p->p_nice;
428 * Suspend operation on filesystem.
431 vn_finished_write(wrtmp);
432 if ((error = vfs_write_suspend(vp->v_mount, 0)) != 0) {
433 vn_start_write(NULL, &wrtmp, V_WAIT);
434 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
437 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
439 vn_start_write(NULL, &wrtmp, V_WAIT);
441 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
442 if (ip->i_effnlink == 0) {
443 error = ENOENT; /* Snapshot file unlinked */
447 if (collectsnapstats)
448 nanotime(&starttime);
451 /* The last block might have changed. Copy it again to be sure. */
452 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
453 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
456 error = readblock(vp, bp, numblks - 1);
457 bp->b_flags |= B_VALIDSUSPWRT;
462 * First, copy all the cylinder group maps that have changed.
464 for (cg = 0; cg < fs->fs_ncg; cg++) {
465 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
468 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
469 fs->fs_bsize, KERNCRED, 0, &nbp);
472 error = cgaccount(cg, vp, nbp, 2);
478 * Grab a copy of the superblock and its summary information.
479 * We delay writing it until the suspension is released below.
481 copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK);
482 bcopy(fs, copy_fs, fs->fs_sbsize);
483 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
484 copy_fs->fs_clean = 1;
485 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
486 if (fs->fs_sbsize < size)
487 bzero(&((char *)copy_fs)[fs->fs_sbsize],
488 size - fs->fs_sbsize);
489 size = blkroundup(fs, fs->fs_cssize);
490 if (fs->fs_contigsumsize > 0)
491 size += fs->fs_ncg * sizeof(int32_t);
492 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
493 copy_fs->fs_csp = space;
494 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
495 space = (char *)space + fs->fs_cssize;
496 loc = howmany(fs->fs_cssize, fs->fs_fsize);
497 i = fs->fs_frag - loc % fs->fs_frag;
498 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
500 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
501 len, KERNCRED, &bp)) != 0) {
503 free(copy_fs->fs_csp, M_UFSMNT);
504 free(copy_fs, M_UFSMNT);
508 bcopy(bp->b_data, space, (u_int)len);
509 space = (char *)space + len;
510 bp->b_flags |= B_INVAL | B_NOCACHE;
513 if (fs->fs_contigsumsize > 0) {
514 copy_fs->fs_maxcluster = lp = space;
515 for (i = 0; i < fs->fs_ncg; i++)
516 *lp++ = fs->fs_contigsumsize;
519 * We must check for active files that have been unlinked
520 * (e.g., with a zero link count). We have to expunge all
521 * trace of these files from the snapshot so that they are
522 * not reclaimed prematurely by fsck or unnecessarily dumped.
523 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
524 * spec_strategy about writing on a suspended filesystem.
525 * Note that we skip unlinked snapshot files as they will
526 * be handled separately below.
528 * We also calculate the needed size for the snapshot list.
530 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
531 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
533 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
536 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
537 if ((xvp->v_usecount == 0 &&
538 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
539 xvp->v_type == VNON ||
540 IS_SNAPSHOT(VTOI(xvp))) {
545 * We can skip parent directory vnode because it must have
546 * this snapshot file in it.
548 if (xvp == nd.ni_dvp) {
553 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
554 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
559 if (xvp->v_usecount == 0 &&
560 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
569 vn_printf(xvp, "ffs_snapshot: busy vnode ");
571 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
578 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
584 * If there is a fragment, clear it here.
587 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
588 if (loc < UFS_NDADDR) {
589 len = fragroundup(fs, blkoff(fs, xp->i_size));
590 if (len != 0 && len < fs->fs_bsize) {
591 ffs_blkfree(ump, copy_fs, vp,
592 DIP(xp, i_db[loc]), len, xp->i_number,
593 xvp->v_type, NULL, SINGLETON_KEY);
594 blkno = DIP(xp, i_db[loc]);
595 DIP_SET(xp, i_db[loc], 0);
600 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
603 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
606 DIP_SET(xp, i_db[loc], blkno);
608 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
613 free(copy_fs->fs_csp, M_UFSMNT);
614 free(copy_fs, M_UFSMNT);
616 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
621 * Erase the journal file from the snapshot.
623 if (fs->fs_flags & FS_SUJ) {
624 error = softdep_journal_lookup(mp, &xvp);
626 free(copy_fs->fs_csp, M_UFSMNT);
627 free(copy_fs, M_UFSMNT);
633 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
636 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
641 * Acquire a lock on the snapdata structure, creating it if necessary.
643 sn = ffs_snapdata_acquire(devvp);
645 * Change vnode to use shared snapshot lock instead of the original
648 vp->v_vnlock = &sn->sn_lock;
649 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
650 xp = TAILQ_FIRST(&sn->sn_head);
652 * If this is the first snapshot on this filesystem, then we need
653 * to allocate the space for the list of preallocated snapshot blocks.
654 * This list will be refined below, but this preliminary one will
655 * keep us out of deadlock until the full one is ready.
658 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
660 blkp = &snapblklist[1];
661 *blkp++ = lblkno(fs, fs->fs_sblockloc);
662 blkno = fragstoblks(fs, fs->fs_csaddr);
663 for (cg = 0; cg < fs->fs_ncg; cg++) {
664 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
666 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
668 len = howmany(fs->fs_cssize, fs->fs_bsize);
669 for (loc = 0; loc < len; loc++)
670 *blkp++ = blkno + loc;
671 for (; cg < fs->fs_ncg; cg++)
672 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
673 snapblklist[0] = blkp - snapblklist;
675 if (sn->sn_blklist != NULL)
676 panic("ffs_snapshot: non-empty list");
677 sn->sn_blklist = snapblklist;
678 sn->sn_listsize = blkp - snapblklist;
682 * Record snapshot inode. Since this is the newest snapshot,
683 * it must be placed at the end of the list.
686 fs->fs_snapinum[snaploc] = ip->i_number;
687 if (ip->i_nextsnap.tqe_prev != 0)
688 panic("ffs_snapshot: %ju already on list",
689 (uintmax_t)ip->i_number);
690 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
691 devvp->v_vflag |= VV_COPYONWRITE;
693 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
695 KASSERT((sn != NULL && copy_fs != NULL && error == 0) ||
696 (sn == NULL && copy_fs == NULL && error != 0),
697 ("email phk@ and mckusick@"));
699 * Resume operation on filesystem.
701 vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR);
703 if (collectsnapstats && starttime.tv_sec > 0) {
705 timespecsub(&endtime, &starttime, &endtime);
706 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
707 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
708 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
714 * Copy allocation information from all the snapshots in
715 * this snapshot and then expunge them from its view.
717 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
721 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
724 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
726 if (error == 0 && xp->i_effnlink == 0) {
727 error = ffs_freefile(ump,
734 fs->fs_snapinum[snaploc] = 0;
739 * Allocate space for the full list of preallocated snapshot blocks.
741 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
743 ip->i_snapblklist = &snapblklist[1];
745 * Expunge the blocks used by the snapshots from the set of
746 * blocks marked as used in the snapshot bitmaps. Also, collect
747 * the list of allocated blocks in i_snapblklist.
750 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
753 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
756 fs->fs_snapinum[snaploc] = 0;
757 free(snapblklist, M_UFSMNT);
760 if (snaplistsize < ip->i_snapblklist - snapblklist)
761 panic("ffs_snapshot: list too small");
762 snaplistsize = ip->i_snapblklist - snapblklist;
763 snapblklist[0] = snaplistsize;
764 ip->i_snapblklist = 0;
766 * Write out the list of allocated blocks to the end of the snapshot.
768 auio.uio_iov = &aiov;
770 aiov.iov_base = (void *)snapblklist;
771 aiov.iov_len = snaplistsize * sizeof(daddr_t);
772 auio.uio_resid = aiov.iov_len;
773 auio.uio_offset = ip->i_size;
774 auio.uio_segflg = UIO_SYSSPACE;
775 auio.uio_rw = UIO_WRITE;
777 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
778 fs->fs_snapinum[snaploc] = 0;
779 free(snapblklist, M_UFSMNT);
783 * Write the superblock and its summary information
786 blkno = fragstoblks(fs, fs->fs_csaddr);
787 len = howmany(fs->fs_cssize, fs->fs_bsize);
788 space = copy_fs->fs_csp;
789 for (loc = 0; loc < len; loc++) {
790 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
792 fs->fs_snapinum[snaploc] = 0;
793 free(snapblklist, M_UFSMNT);
796 bcopy(space, nbp->b_data, fs->fs_bsize);
797 space = (char *)space + fs->fs_bsize;
800 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
805 loc = blkoff(fs, fs->fs_sblockloc);
806 copy_fs->fs_ckhash = ffs_calc_sbhash(copy_fs);
807 bcopy((char *)copy_fs, &nbp->b_data[loc], (u_int)fs->fs_sbsize);
811 * As this is the newest list, it is the most inclusive, so
812 * should replace the previous list.
815 space = sn->sn_blklist;
816 sn->sn_blklist = snapblklist;
817 sn->sn_listsize = snaplistsize;
820 free(space, M_UFSMNT);
822 * Preallocate all the direct blocks in the snapshot inode so
823 * that we never have to write the inode itself to commit an
824 * update to the contents of the snapshot. Note that once
825 * created, the size of the snapshot will never change, so
826 * there will never be a need to write the inode except to
827 * update the non-integrity-critical time fields and
828 * allocated-block count.
830 for (blockno = 0; blockno < UFS_NDADDR; blockno++) {
831 if (DIP(ip, i_db[blockno]) != 0)
833 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
834 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
837 error = readblock(vp, bp, blockno);
843 free(copy_fs->fs_csp, M_UFSMNT);
844 free(copy_fs, M_UFSMNT);
847 NDFREE(&nd, NDF_ONLY_PNBUF);
848 if (saved_nice > 0) {
853 sched_nice(td->td_proc, saved_nice);
854 PROC_UNLOCK(td->td_proc);
857 if (fs->fs_active != 0) {
858 free(fs->fs_active, M_DEVBUF);
863 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
866 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED);
867 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
873 vn_finished_write(wrtmp);
874 process_deferred_inactive(mp);
879 * Copy a cylinder group map. All the unallocated blocks are marked
880 * BLK_NOCOPY so that the snapshot knows that it need not copy them
881 * if they are later written. If passno is one, then this is a first
882 * pass, so only setting needs to be done. If passno is 2, then this
883 * is a revision to a previous pass which must be undone as the
884 * replacement pass is done.
887 cgaccount(cg, vp, nbp, passno)
893 struct buf *bp, *ibp;
897 ufs2_daddr_t base, numblks;
898 int error, len, loc, indiroff;
902 if ((error = ffs_getcg(fs, ITODEVVP(ip), cg, 0, &bp, &cgp)) != 0)
904 UFS_LOCK(ITOUMP(ip));
907 * Recomputation of summary information might not have been performed
908 * at mount time. Sync up summary information for current cylinder
909 * group while data is in memory to ensure that result of background
910 * fsck is slightly more consistent.
912 fs->fs_cs(fs, cg) = cgp->cg_cs;
913 UFS_UNLOCK(ITOUMP(ip));
914 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
915 if (fs->fs_cgsize < fs->fs_bsize)
916 bzero(&nbp->b_data[fs->fs_cgsize],
917 fs->fs_bsize - fs->fs_cgsize);
918 cgp = (struct cg *)nbp->b_data;
921 nbp->b_flags |= B_VALIDSUSPWRT;
922 numblks = howmany(fs->fs_size, fs->fs_frag);
923 len = howmany(fs->fs_fpg, fs->fs_frag);
924 base = cgbase(fs, cg) / fs->fs_frag;
925 if (base + len >= numblks)
926 len = numblks - base - 1;
928 if (base < UFS_NDADDR) {
929 for ( ; loc < UFS_NDADDR; loc++) {
930 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
931 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
932 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
933 DIP_SET(ip, i_db[loc], 0);
934 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
935 panic("ffs_snapshot: lost direct block");
938 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
939 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
943 indiroff = (base + loc - UFS_NDADDR) % NINDIR(fs);
944 for ( ; loc < len; loc++, indiroff++) {
945 if (indiroff >= NINDIR(fs)) {
947 ibp->b_flags |= B_VALIDSUSPWRT;
949 error = UFS_BALLOC(vp,
950 lblktosize(fs, (off_t)(base + loc)),
951 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
958 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
959 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
961 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
962 [indiroff] == BLK_NOCOPY)
963 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
964 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
965 [indiroff] == BLK_NOCOPY)
966 panic("ffs_snapshot: lost indirect block");
969 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
970 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
971 else if (passno == 2 &&
972 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
973 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
974 else if (passno == 1 &&
975 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
976 panic("ffs_snapshot: lost indirect block");
979 ibp->b_flags |= B_VALIDSUSPWRT;
983 * We have to calculate the crc32c here rather than just setting the
984 * BX_CYLGRP b_xflags because the allocation of the block for the
985 * the cylinder group map will always be a full size block (fs_bsize)
986 * even though the cylinder group may be smaller (fs_cgsize). The
987 * crc32c must be computed only over fs_cgsize whereas the BX_CYLGRP
988 * flag causes it to be computed over the size of the buffer.
990 if ((fs->fs_metackhash & CK_CYLGRP) != 0) {
991 ((struct cg *)nbp->b_data)->cg_ckhash = 0;
992 ((struct cg *)nbp->b_data)->cg_ckhash =
993 calculate_crc32c(~0L, nbp->b_data, fs->fs_cgsize);
999 * Before expunging a snapshot inode, note all the
1000 * blocks that it claims with BLK_SNAP so that fsck will
1001 * be able to account for those blocks properly and so
1002 * that this snapshot knows that it need not copy them
1003 * if the other snapshot holding them is freed. This code
1004 * is reproduced once each for UFS1 and UFS2.
1007 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1008 struct vnode *snapvp;
1009 struct inode *cancelip;
1011 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1012 struct fs *, ufs_lbn_t, int);
1016 int i, error, indiroff;
1017 ufs_lbn_t lbn, rlbn;
1018 ufs2_daddr_t len, blkno, numblks, blksperindir;
1019 struct ufs1_dinode *dip;
1020 struct thread *td = curthread;
1024 * Prepare to expunge the inode. If its inode block has not
1025 * yet been copied, then allocate and fill the copy.
1027 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1029 if (lbn < UFS_NDADDR) {
1030 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1032 if (DOINGSOFTDEP(snapvp))
1033 softdep_prealloc(snapvp, MNT_WAIT);
1034 td->td_pflags |= TDP_COWINPROGRESS;
1035 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1036 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1037 td->td_pflags &= ~TDP_COWINPROGRESS;
1040 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1041 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1045 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1048 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1049 fs->fs_bsize, KERNCRED, 0, &bp);
1052 if ((error = readblock(snapvp, bp, lbn)) != 0)
1056 * Set a snapshot inode to be a zero length file, regular files
1057 * or unlinked snapshots to be completely unallocated.
1059 dip = (struct ufs1_dinode *)bp->b_data +
1060 ino_to_fsbo(fs, cancelip->i_number);
1061 if (clearmode || cancelip->i_effnlink == 0)
1065 dip->di_flags &= ~SF_SNAPSHOT;
1066 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs1_daddr_t));
1069 * Now go through and expunge all the blocks in the file
1070 * using the function requested.
1072 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1073 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1074 &cancelip->i_din1->di_db[UFS_NDADDR], fs, 0, expungetype)))
1076 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1077 &cancelip->i_din1->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1081 len = numblks - UFS_NDADDR;
1083 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1084 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1085 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1086 blksperindir, fs, acctfunc, expungetype);
1089 blksperindir *= NINDIR(fs);
1090 lbn -= blksperindir + 1;
1091 len -= blksperindir;
1092 rlbn += blksperindir;
1098 * Descend an indirect block chain for vnode cancelvp accounting for all
1099 * its indirect blocks in snapvp.
1102 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1103 blksperindir, fs, acctfunc, expungetype)
1104 struct vnode *snapvp;
1105 struct vnode *cancelvp;
1111 ufs_lbn_t blksperindir;
1113 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1114 struct fs *, ufs_lbn_t, int);
1118 ufs_lbn_t subblksperindir;
1119 struct indir indirs[UFS_NIADDR + 2];
1120 ufs1_daddr_t last, *bap;
1124 if (expungetype == BLK_NOCOPY)
1126 panic("indiracct_ufs1: missing indir");
1128 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1130 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1131 panic("indiracct_ufs1: botched params");
1133 * We have to expand bread here since it will deadlock looking
1134 * up the block number for any blocks that are not in the cache.
1136 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1137 bp->b_blkno = fsbtodb(fs, blkno);
1138 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1139 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1144 * Account for the block pointers in this indirect block.
1146 last = howmany(remblks, blksperindir);
1147 if (last > NINDIR(fs))
1149 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1150 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1152 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1153 level == 0 ? rlbn : -1, expungetype);
1154 if (error || level == 0)
1157 * Account for the block pointers in each of the indirect blocks
1158 * in the levels below us.
1160 subblksperindir = blksperindir / NINDIR(fs);
1161 for (lbn++, level--, i = 0; i < last; i++) {
1162 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1163 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1166 rlbn += blksperindir;
1167 lbn -= blksperindir;
1168 remblks -= blksperindir;
1171 free(bap, M_DEVBUF);
1176 * Do both snap accounting and map accounting.
1179 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1181 ufs1_daddr_t *oldblkp, *lastblkp;
1184 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1188 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1190 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1194 * Identify a set of blocks allocated in a snapshot inode.
1197 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1199 ufs1_daddr_t *oldblkp, *lastblkp;
1202 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1204 struct inode *ip = VTOI(vp);
1205 ufs1_daddr_t blkno, *blkp;
1210 for ( ; oldblkp < lastblkp; oldblkp++) {
1212 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1214 lbn = fragstoblks(fs, blkno);
1215 if (lbn < UFS_NDADDR) {
1216 blkp = &ip->i_din1->di_db[lbn];
1217 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1219 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1220 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1223 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1224 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1227 * If we are expunging a snapshot vnode and we
1228 * find a block marked BLK_NOCOPY, then it is
1229 * one that has been allocated to this snapshot after
1230 * we took our current snapshot and can be ignored.
1232 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1233 if (lbn >= UFS_NDADDR)
1237 panic("snapacct_ufs1: bad block");
1238 *blkp = expungetype;
1239 if (lbn >= UFS_NDADDR)
1247 * Account for a set of blocks allocated in a snapshot inode.
1250 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1252 ufs1_daddr_t *oldblkp, *lastblkp;
1263 inum = ip->i_number;
1268 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1270 if (blkno == 0 || blkno == BLK_NOCOPY)
1272 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1273 *ip->i_snapblklist++ = lblkno;
1274 if (blkno == BLK_SNAP)
1275 blkno = blkstofrags(fs, lblkno);
1276 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1277 vp->v_type, NULL, SINGLETON_KEY);
1283 * Before expunging a snapshot inode, note all the
1284 * blocks that it claims with BLK_SNAP so that fsck will
1285 * be able to account for those blocks properly and so
1286 * that this snapshot knows that it need not copy them
1287 * if the other snapshot holding them is freed. This code
1288 * is reproduced once each for UFS1 and UFS2.
1291 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1292 struct vnode *snapvp;
1293 struct inode *cancelip;
1295 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1296 struct fs *, ufs_lbn_t, int);
1300 int i, error, indiroff;
1301 ufs_lbn_t lbn, rlbn;
1302 ufs2_daddr_t len, blkno, numblks, blksperindir;
1303 struct ufs2_dinode *dip;
1304 struct thread *td = curthread;
1308 * Prepare to expunge the inode. If its inode block has not
1309 * yet been copied, then allocate and fill the copy.
1311 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1313 if (lbn < UFS_NDADDR) {
1314 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1316 if (DOINGSOFTDEP(snapvp))
1317 softdep_prealloc(snapvp, MNT_WAIT);
1318 td->td_pflags |= TDP_COWINPROGRESS;
1319 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1320 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1321 td->td_pflags &= ~TDP_COWINPROGRESS;
1324 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1325 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1329 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1332 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1333 fs->fs_bsize, KERNCRED, 0, &bp);
1336 if ((error = readblock(snapvp, bp, lbn)) != 0)
1340 * Set a snapshot inode to be a zero length file, regular files
1341 * to be completely unallocated.
1343 dip = (struct ufs2_dinode *)bp->b_data +
1344 ino_to_fsbo(fs, cancelip->i_number);
1347 dip->di_flags &= ~SF_SNAPSHOT;
1348 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs2_daddr_t));
1349 if (clearmode || cancelip->i_effnlink == 0)
1352 ffs_update_dinode_ckhash(fs, dip);
1355 * Now go through and expunge all the blocks in the file
1356 * using the function requested.
1358 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1359 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1360 &cancelip->i_din2->di_db[UFS_NDADDR], fs, 0, expungetype)))
1362 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1363 &cancelip->i_din2->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1367 len = numblks - UFS_NDADDR;
1369 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1370 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1371 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1372 blksperindir, fs, acctfunc, expungetype);
1375 blksperindir *= NINDIR(fs);
1376 lbn -= blksperindir + 1;
1377 len -= blksperindir;
1378 rlbn += blksperindir;
1384 * Descend an indirect block chain for vnode cancelvp accounting for all
1385 * its indirect blocks in snapvp.
1388 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1389 blksperindir, fs, acctfunc, expungetype)
1390 struct vnode *snapvp;
1391 struct vnode *cancelvp;
1397 ufs_lbn_t blksperindir;
1399 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1400 struct fs *, ufs_lbn_t, int);
1404 ufs_lbn_t subblksperindir;
1405 struct indir indirs[UFS_NIADDR + 2];
1406 ufs2_daddr_t last, *bap;
1410 if (expungetype == BLK_NOCOPY)
1412 panic("indiracct_ufs2: missing indir");
1414 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1416 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1417 panic("indiracct_ufs2: botched params");
1419 * We have to expand bread here since it will deadlock looking
1420 * up the block number for any blocks that are not in the cache.
1422 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1423 bp->b_blkno = fsbtodb(fs, blkno);
1424 if ((bp->b_flags & B_CACHE) == 0 &&
1425 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1430 * Account for the block pointers in this indirect block.
1432 last = howmany(remblks, blksperindir);
1433 if (last > NINDIR(fs))
1435 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1436 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1438 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1439 level == 0 ? rlbn : -1, expungetype);
1440 if (error || level == 0)
1443 * Account for the block pointers in each of the indirect blocks
1444 * in the levels below us.
1446 subblksperindir = blksperindir / NINDIR(fs);
1447 for (lbn++, level--, i = 0; i < last; i++) {
1448 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1449 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1452 rlbn += blksperindir;
1453 lbn -= blksperindir;
1454 remblks -= blksperindir;
1457 free(bap, M_DEVBUF);
1462 * Do both snap accounting and map accounting.
1465 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1467 ufs2_daddr_t *oldblkp, *lastblkp;
1470 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1474 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1476 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1480 * Identify a set of blocks allocated in a snapshot inode.
1483 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1485 ufs2_daddr_t *oldblkp, *lastblkp;
1488 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1490 struct inode *ip = VTOI(vp);
1491 ufs2_daddr_t blkno, *blkp;
1496 for ( ; oldblkp < lastblkp; oldblkp++) {
1498 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1500 lbn = fragstoblks(fs, blkno);
1501 if (lbn < UFS_NDADDR) {
1502 blkp = &ip->i_din2->di_db[lbn];
1503 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1505 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1506 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1509 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1510 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1513 * If we are expunging a snapshot vnode and we
1514 * find a block marked BLK_NOCOPY, then it is
1515 * one that has been allocated to this snapshot after
1516 * we took our current snapshot and can be ignored.
1518 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1519 if (lbn >= UFS_NDADDR)
1523 panic("snapacct_ufs2: bad block");
1524 *blkp = expungetype;
1525 if (lbn >= UFS_NDADDR)
1533 * Account for a set of blocks allocated in a snapshot inode.
1536 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1538 ufs2_daddr_t *oldblkp, *lastblkp;
1549 inum = ip->i_number;
1554 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1556 if (blkno == 0 || blkno == BLK_NOCOPY)
1558 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1559 *ip->i_snapblklist++ = lblkno;
1560 if (blkno == BLK_SNAP)
1561 blkno = blkstofrags(fs, lblkno);
1562 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1563 vp->v_type, NULL, SINGLETON_KEY);
1569 * Decrement extra reference on snapshot when last name is removed.
1570 * It will not be freed until the last open reference goes away.
1579 struct snapdata *sn;
1580 struct ufsmount *ump;
1583 * Find snapshot in incore list.
1586 sn = ITODEVVP(ip)->v_rdev->si_snapdata;
1588 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1595 printf("ffs_snapgone: lost snapshot vnode %ju\n",
1596 (uintmax_t)ip->i_number);
1599 * Delete snapshot inode from superblock. Keep list dense.
1604 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1605 if (fs->fs_snapinum[snaploc] == ip->i_number)
1607 if (snaploc < FSMAXSNAP) {
1608 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1609 if (fs->fs_snapinum[snaploc] == 0)
1611 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1613 fs->fs_snapinum[snaploc - 1] = 0;
1619 * Prepare a snapshot file for being removed.
1626 struct vnode *devvp;
1629 ufs2_daddr_t numblks, blkno, dblk;
1630 int error, i, last, loc;
1631 struct snapdata *sn;
1635 devvp = ITODEVVP(ip);
1637 * If active, delete from incore list (this snapshot may
1638 * already have been in the process of being deleted, so
1639 * would not have been active).
1641 * Clear copy-on-write flag if last snapshot.
1644 if (ip->i_nextsnap.tqe_prev != 0) {
1645 sn = devvp->v_rdev->si_snapdata;
1646 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1647 ip->i_nextsnap.tqe_prev = 0;
1649 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1650 for (i = 0; i < sn->sn_lock.lk_recurse; i++)
1651 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1652 KASSERT(vp->v_vnlock == &sn->sn_lock,
1653 ("ffs_snapremove: lost lock mutation"));
1654 vp->v_vnlock = &vp->v_lock;
1656 while (sn->sn_lock.lk_recurse > 0)
1657 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1658 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1659 try_free_snapdata(devvp);
1663 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1664 * snapshots that want them (see ffs_snapblkfree below).
1666 for (blkno = 1; blkno < UFS_NDADDR; blkno++) {
1667 dblk = DIP(ip, i_db[blkno]);
1670 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1671 DIP_SET(ip, i_db[blkno], 0);
1672 else if ((dblk == blkstofrags(fs, blkno) &&
1673 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, fs->fs_bsize,
1674 ip->i_number, vp->v_type, NULL))) {
1675 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1676 btodb(fs->fs_bsize));
1677 DIP_SET(ip, i_db[blkno], 0);
1680 numblks = howmany(ip->i_size, fs->fs_bsize);
1681 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1682 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1683 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1686 if (fs->fs_size - blkno > NINDIR(fs))
1689 last = fs->fs_size - blkno;
1690 for (loc = 0; loc < last; loc++) {
1691 if (I_IS_UFS1(ip)) {
1692 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1695 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1696 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1697 else if ((dblk == blkstofrags(fs, blkno) &&
1698 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1699 fs->fs_bsize, ip->i_number, vp->v_type,
1701 ip->i_din1->di_blocks -=
1702 btodb(fs->fs_bsize);
1703 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1707 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1710 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1711 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1712 else if ((dblk == blkstofrags(fs, blkno) &&
1713 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1714 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1715 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1716 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1722 * Clear snapshot flag and drop reference.
1724 ip->i_flags &= ~SF_SNAPSHOT;
1725 DIP_SET(ip, i_flags, ip->i_flags);
1726 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1728 * The dirtied indirects must be written out before
1729 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1730 * may find indirect pointers using the magic BLK_* values.
1732 if (DOINGSOFTDEP(vp))
1733 ffs_syncvnode(vp, MNT_WAIT, 0);
1736 * Reenable disk quotas for ex-snapshot file.
1738 if (!getinoquota(ip))
1739 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1744 * Notification that a block is being freed. Return zero if the free
1745 * should be allowed to proceed. Return non-zero if the snapshot file
1746 * wants to claim the block. The block will be claimed if it is an
1747 * uncopied part of one of the snapshots. It will be freed if it is
1748 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1749 * If a fragment is being freed, then all snapshots that care about
1750 * it must make a copy since a snapshot file can only claim full sized
1751 * blocks. Note that if more than one snapshot file maps the block,
1752 * we can pick one at random to claim it. Since none of the snapshots
1753 * can change, we are assurred that they will all see the same unmodified
1754 * image. When deleting a snapshot file (see ffs_snapremove above), we
1755 * must push any of these claimed blocks to one of the other snapshots
1756 * that maps it. These claimed blocks are easily identified as they will
1757 * have a block number equal to their logical block number within the
1758 * snapshot. A copied block can never have this property because they
1759 * must always have been allocated from a BLK_NOCOPY location.
1762 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1764 struct vnode *devvp;
1769 struct workhead *wkhd;
1771 struct buf *ibp, *cbp, *savedcbp = NULL;
1772 struct thread *td = curthread;
1774 struct vnode *vp = NULL;
1777 int indiroff = 0, error = 0, claimedblk = 0;
1778 struct snapdata *sn;
1780 lbn = fragstoblks(fs, bno);
1783 sn = devvp->v_rdev->si_snapdata;
1788 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1789 VI_MTX(devvp)) != 0)
1791 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1793 if (DOINGSOFTDEP(vp))
1794 softdep_prealloc(vp, MNT_WAIT);
1796 * Lookup block being written.
1798 if (lbn < UFS_NDADDR) {
1799 blkno = DIP(ip, i_db[lbn]);
1801 td->td_pflags |= TDP_COWINPROGRESS;
1802 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1803 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1804 td->td_pflags &= ~TDP_COWINPROGRESS;
1807 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1809 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1811 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1814 * Check to see if block needs to be copied.
1818 * A block that we map is being freed. If it has not
1819 * been claimed yet, we will claim or copy it (below).
1822 } else if (blkno == BLK_SNAP) {
1824 * No previous snapshot claimed the block,
1825 * so it will be freed and become a BLK_NOCOPY
1826 * (don't care) for us.
1829 panic("snapblkfree: inconsistent block type");
1830 if (lbn < UFS_NDADDR) {
1831 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1832 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1833 } else if (I_IS_UFS1(ip)) {
1834 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1838 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1843 } else /* BLK_NOCOPY or default */ {
1845 * If the snapshot has already copied the block
1846 * (default), or does not care about the block,
1849 if (lbn >= UFS_NDADDR)
1854 * If this is a full size block, we will just grab it
1855 * and assign it to the snapshot inode. Otherwise we
1856 * will proceed to copy it. See explanation for this
1857 * routine as to why only a single snapshot needs to
1860 if (size == fs->fs_bsize) {
1863 printf("%s %ju lbn %jd from inum %ju\n",
1864 "Grabonremove: snapino",
1865 (uintmax_t)ip->i_number,
1866 (intmax_t)lbn, (uintmax_t)inum);
1869 * If journaling is tracking this write we must add
1870 * the work to the inode or indirect being written.
1873 if (lbn < UFS_NDADDR)
1874 softdep_inode_append(ip,
1875 curthread->td_ucred, wkhd);
1877 softdep_buf_append(ibp, wkhd);
1879 if (lbn < UFS_NDADDR) {
1880 DIP_SET(ip, i_db[lbn], bno);
1881 } else if (I_IS_UFS1(ip)) {
1882 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1885 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1888 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1889 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1890 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1893 if (lbn >= UFS_NDADDR)
1896 * Allocate the block into which to do the copy. Note that this
1897 * allocation will never require any additional allocations for
1898 * the snapshot inode.
1900 td->td_pflags |= TDP_COWINPROGRESS;
1901 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1902 fs->fs_bsize, KERNCRED, 0, &cbp);
1903 td->td_pflags &= ~TDP_COWINPROGRESS;
1908 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n",
1909 "Copyonremove: snapino ", (uintmax_t)ip->i_number,
1910 (intmax_t)lbn, "for inum", (uintmax_t)inum, size,
1911 (intmax_t)cbp->b_blkno);
1914 * If we have already read the old block contents, then
1915 * simply copy them to the new block. Note that we need
1916 * to synchronously write snapshots that have not been
1917 * unlinked, and hence will be visible after a crash,
1918 * to ensure their integrity. At a minimum we ensure the
1919 * integrity of the filesystem metadata, but use the
1920 * dopersistence sysctl-setable flag to decide on the
1921 * persistence needed for file content data.
1923 if (savedcbp != NULL) {
1924 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1926 if ((vtype == VDIR || dopersistence) &&
1928 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1932 * Otherwise, read the old block contents into the buffer.
1934 if ((error = readblock(vp, cbp, lbn)) != 0) {
1935 bzero(cbp->b_data, fs->fs_bsize);
1937 if ((vtype == VDIR || dopersistence) &&
1939 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1945 * Note that we need to synchronously write snapshots that
1946 * have not been unlinked, and hence will be visible after
1947 * a crash, to ensure their integrity. At a minimum we
1948 * ensure the integrity of the filesystem metadata, but
1949 * use the dopersistence sysctl-setable flag to decide on
1950 * the persistence needed for file content data.
1953 vp = savedcbp->b_vp;
1955 if ((vtype == VDIR || dopersistence) &&
1956 VTOI(vp)->i_effnlink > 0)
1957 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1960 * If we have been unable to allocate a block in which to do
1961 * the copy, then return non-zero so that the fragment will
1962 * not be freed. Although space will be lost, the snapshot
1963 * will stay consistent.
1965 if (error != 0 && wkhd != NULL)
1966 softdep_freework(wkhd);
1967 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1972 * Associate snapshot files when mounting.
1975 ffs_snapshot_mount(mp)
1978 struct ufsmount *ump = VFSTOUFS(mp);
1979 struct vnode *devvp = ump->um_devvp;
1980 struct fs *fs = ump->um_fs;
1981 struct thread *td = curthread;
1982 struct snapdata *sn;
1984 struct vnode *lastvp;
1990 daddr_t snaplistsize;
1991 int error, snaploc, loc;
1994 * XXX The following needs to be set before ffs_truncate or
1995 * VOP_READ can be called.
1997 mp->mnt_stat.f_iosize = fs->fs_bsize;
1999 * Process each snapshot listed in the superblock.
2004 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
2005 if (fs->fs_snapinum[snaploc] == 0)
2007 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
2008 LK_EXCLUSIVE, &vp)) != 0){
2009 printf("ffs_snapshot_mount: vget failed %d\n", error);
2013 if (vp->v_type != VREG) {
2014 reason = "non-file snapshot";
2015 } else if (!IS_SNAPSHOT(ip)) {
2016 reason = "non-snapshot";
2017 } else if (ip->i_size ==
2018 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
2019 reason = "old format snapshot";
2020 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED);
2021 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
2025 if (reason != NULL) {
2026 printf("ffs_snapshot_mount: %s inode %d\n",
2027 reason, fs->fs_snapinum[snaploc]);
2030 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2031 if (fs->fs_snapinum[loc] == 0)
2033 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2035 fs->fs_snapinum[loc - 1] = 0;
2040 * Acquire a lock on the snapdata structure, creating it if
2043 sn = ffs_snapdata_acquire(devvp);
2045 * Change vnode to use shared snapshot lock instead of the
2046 * original private lock.
2048 vp->v_vnlock = &sn->sn_lock;
2049 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2051 * Link it onto the active snapshot list.
2054 if (ip->i_nextsnap.tqe_prev != 0)
2055 panic("ffs_snapshot_mount: %ju already on list",
2056 (uintmax_t)ip->i_number);
2058 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2059 vp->v_vflag |= VV_SYSTEM;
2066 * No usable snapshots found.
2068 if (sn == NULL || vp == NULL)
2071 * Allocate the space for the block hints list. We always want to
2072 * use the list from the newest snapshot.
2074 auio.uio_iov = &aiov;
2075 auio.uio_iovcnt = 1;
2076 aiov.iov_base = (void *)&snaplistsize;
2077 aiov.iov_len = sizeof(snaplistsize);
2078 auio.uio_resid = aiov.iov_len;
2080 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2081 auio.uio_segflg = UIO_SYSSPACE;
2082 auio.uio_rw = UIO_READ;
2084 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2085 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2086 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2090 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2091 M_UFSMNT, M_WAITOK);
2092 auio.uio_iovcnt = 1;
2093 aiov.iov_base = snapblklist;
2094 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2095 auio.uio_resid = aiov.iov_len;
2096 auio.uio_offset -= sizeof(snaplistsize);
2097 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2098 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2100 free(snapblklist, M_UFSMNT);
2105 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2106 sn->sn_listsize = snaplistsize;
2107 sn->sn_blklist = (daddr_t *)snapblklist;
2108 devvp->v_vflag |= VV_COPYONWRITE;
2113 * Disassociate snapshot files when unmounting.
2116 ffs_snapshot_unmount(mp)
2119 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2120 struct snapdata *sn;
2125 sn = devvp->v_rdev->si_snapdata;
2126 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2128 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2129 xp->i_nextsnap.tqe_prev = 0;
2130 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2132 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2133 KASSERT(vp->v_vnlock == &sn->sn_lock,
2134 ("ffs_snapshot_unmount: lost lock mutation"));
2135 vp->v_vnlock = &vp->v_lock;
2136 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2137 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2138 if (xp->i_effnlink > 0)
2141 sn = devvp->v_rdev->si_snapdata;
2143 try_free_snapdata(devvp);
2144 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2148 * Check the buffer block to be belong to device buffer that shall be
2149 * locked after snaplk. devvp shall be locked on entry, and will be
2150 * leaved locked upon exit.
2153 ffs_bp_snapblk(devvp, bp)
2154 struct vnode *devvp;
2157 struct snapdata *sn;
2159 ufs2_daddr_t lbn, *snapblklist;
2160 int lower, upper, mid;
2162 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2163 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2164 sn = devvp->v_rdev->si_snapdata;
2165 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2167 fs = ITOFS(TAILQ_FIRST(&sn->sn_head));
2168 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2169 snapblklist = sn->sn_blklist;
2170 upper = sn->sn_listsize - 1;
2172 while (lower <= upper) {
2173 mid = (lower + upper) / 2;
2174 if (snapblklist[mid] == lbn)
2176 if (snapblklist[mid] < lbn)
2192 struct vnode *vp, *devvp;
2196 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2201 devvp = bo2vnode(bo);
2202 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2205 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2209 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2210 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2215 * Try to find a buffer to flush.
2217 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2218 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2220 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2223 panic("bdwrite: found ourselves");
2226 * Don't countdeps with the bo lock
2229 if (buf_countdeps(nbp, 0)) {
2236 if (!ffs_bp_snapblk(vp, nbp)) {
2244 if (nbp->b_flags & B_CLUSTEROK) {
2245 vfs_bio_awrite(nbp);
2250 dirtybufferflushes++;
2259 * Check for need to copy block that is about to be written,
2260 * copying the block if necessary.
2263 ffs_copyonwrite(devvp, bp)
2264 struct vnode *devvp;
2267 struct snapdata *sn;
2268 struct buf *ibp, *cbp, *savedcbp = NULL;
2269 struct thread *td = curthread;
2272 struct vnode *vp = NULL;
2273 ufs2_daddr_t lbn, blkno, *snapblklist;
2274 int lower, upper, mid, indiroff, error = 0;
2275 int launched_async_io, prev_norunningbuf;
2276 long saved_runningbufspace;
2278 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2279 return (0); /* Update on a snapshot file */
2280 if (td->td_pflags & TDP_COWINPROGRESS)
2281 panic("ffs_copyonwrite: recursive call");
2283 * First check to see if it is in the preallocated list.
2284 * By doing this check we avoid several potential deadlocks.
2287 sn = devvp->v_rdev->si_snapdata;
2289 TAILQ_EMPTY(&sn->sn_head)) {
2291 return (0); /* No snapshot */
2293 ip = TAILQ_FIRST(&sn->sn_head);
2295 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2296 snapblklist = sn->sn_blklist;
2297 upper = sn->sn_listsize - 1;
2299 while (lower <= upper) {
2300 mid = (lower + upper) / 2;
2301 if (snapblklist[mid] == lbn)
2303 if (snapblklist[mid] < lbn)
2308 if (lower <= upper) {
2312 launched_async_io = 0;
2313 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
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.
2319 saved_runningbufspace = bp->b_runningbufspace;
2320 if (saved_runningbufspace != 0)
2321 runningbufwakeup(bp);
2323 * Not in the precomputed list, so check the snapshots.
2325 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2326 VI_MTX(devvp)) != 0) {
2328 sn = devvp->v_rdev->si_snapdata;
2330 TAILQ_EMPTY(&sn->sn_head)) {
2332 if (saved_runningbufspace != 0) {
2333 bp->b_runningbufspace = saved_runningbufspace;
2334 atomic_add_long(&runningbufspace,
2335 bp->b_runningbufspace);
2337 return (0); /* Snapshot gone */
2340 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2342 if (DOINGSOFTDEP(vp))
2343 softdep_prealloc(vp, MNT_WAIT);
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.
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
2358 if (lbn < UFS_NDADDR) {
2359 blkno = DIP(ip, i_db[lbn]);
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;
2367 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
2369 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2371 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2375 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2376 panic("ffs_copyonwrite: bad copy block");
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.
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.
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;
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");
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);
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.
2419 if (savedcbp != NULL) {
2420 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
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);
2426 launched_async_io = 1;
2430 * Otherwise, read the old block contents into the buffer.
2432 if ((error = readblock(vp, cbp, lbn)) != 0) {
2433 bzero(cbp->b_data, fs->fs_bsize);
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);
2439 launched_async_io = 1;
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.
2453 vp = savedcbp->b_vp;
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);
2459 launched_async_io = 1;
2461 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2462 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2464 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2465 waitrunningbufspace();
2467 * I/O on bp will now be started, so count it in runningbufspace.
2469 if (saved_runningbufspace != 0) {
2470 bp->b_runningbufspace = saved_runningbufspace;
2471 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2477 * sync snapshots to force freework records waiting on snapshots to claim
2481 ffs_sync_snap(mp, waitfor)
2485 struct snapdata *sn;
2486 struct vnode *devvp;
2490 devvp = VFSTOUFS(mp)->um_devvp;
2491 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2495 sn = devvp->v_rdev->si_snapdata;
2500 if (lockmgr(&sn->sn_lock,
2501 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2502 VI_MTX(devvp)) == 0)
2505 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2507 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2509 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2513 * Read the specified block into the given buffer.
2514 * Much of this boiler-plate comes from bwrite().
2517 readblock(vp, bp, lbn)
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;
2536 g_io_request(bip, ITODEVVP(ip)->v_bufobj.bo_private);
2537 bp->b_error = biowait(bip, "snaprdb");
2539 return (bp->b_error);
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.
2550 process_deferred_inactive(struct mount *mp)
2552 struct vnode *vp, *mvp;
2558 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2560 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2562 * IN_LAZYACCESS is checked here without holding any
2563 * vnode lock, but this flag is set only while holding
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))) {
2573 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2576 if (error == ENOENT)
2577 continue; /* vnode recycled */
2578 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2582 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2583 ip->i_flag &= ~IN_LAZYACCESS;
2584 ip->i_flag |= IN_MODIFIED;
2587 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2594 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2595 ("process_deferred_inactive: got VI_OWEINACT"));
2600 vn_finished_secondary_write(mp);
2603 #ifndef NO_FFS_SNAPSHOT
2605 static struct snapdata *
2606 ffs_snapdata_alloc(void)
2608 struct snapdata *sn;
2611 * Fetch a snapdata from the free list if there is one available.
2613 mtx_lock(&snapfree_lock);
2614 sn = LIST_FIRST(&snapfree);
2616 LIST_REMOVE(sn, sn_link);
2617 mtx_unlock(&snapfree_lock);
2621 * If there were no free snapdatas allocate one.
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);
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().
2636 ffs_snapdata_free(struct snapdata *sn)
2638 mtx_lock(&snapfree_lock);
2639 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2640 mtx_unlock(&snapfree_lock);
2643 /* Try to free snapdata associated with devvp */
2645 try_free_snapdata(struct vnode *devvp)
2647 struct snapdata *sn;
2648 ufs2_daddr_t *snapblklist;
2650 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2651 sn = devvp->v_rdev->si_snapdata;
2653 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2654 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
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);
2671 static struct snapdata *
2672 ffs_snapdata_acquire(struct vnode *devvp)
2674 struct snapdata *nsn, *sn;
2678 * Allocate a free snapdata. This is done before acquiring the
2679 * devvp lock to avoid allocation while the devvp interlock is
2682 nsn = ffs_snapdata_alloc();
2686 sn = devvp->v_rdev->si_snapdata;
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.
2694 error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE |
2697 panic("leaked sn, lockmgr error %d", error);
2698 sn = devvp->v_rdev->si_snapdata = nsn;
2705 * There is a snapshots which already exists on this
2706 * filesystem, grab a reference to the common lock.
2708 error = lockmgr(&sn->sn_lock, LK_INTERLOCK |
2709 LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp));
2715 * Free any unused snapdata.
2718 ffs_snapdata_free(nsn);