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);
793 fs->fs_snapinum[snaploc] = 0;
794 free(snapblklist, M_UFSMNT);
797 bcopy(space, nbp->b_data, fs->fs_bsize);
798 space = (char *)space + fs->fs_bsize;
801 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
806 loc = blkoff(fs, fs->fs_sblockloc);
807 copy_fs->fs_ckhash = ffs_calc_sbhash(copy_fs);
808 bcopy((char *)copy_fs, &nbp->b_data[loc], (u_int)fs->fs_sbsize);
812 * As this is the newest list, it is the most inclusive, so
813 * should replace the previous list.
816 space = sn->sn_blklist;
817 sn->sn_blklist = snapblklist;
818 sn->sn_listsize = snaplistsize;
821 free(space, M_UFSMNT);
823 * Preallocate all the direct blocks in the snapshot inode so
824 * that we never have to write the inode itself to commit an
825 * update to the contents of the snapshot. Note that once
826 * created, the size of the snapshot will never change, so
827 * there will never be a need to write the inode except to
828 * update the non-integrity-critical time fields and
829 * allocated-block count.
831 for (blockno = 0; blockno < UFS_NDADDR; blockno++) {
832 if (DIP(ip, i_db[blockno]) != 0)
834 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
835 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
838 error = readblock(vp, bp, blockno);
844 free(copy_fs->fs_csp, M_UFSMNT);
845 free(copy_fs, M_UFSMNT);
848 NDFREE(&nd, NDF_ONLY_PNBUF);
849 if (saved_nice > 0) {
854 sched_nice(td->td_proc, saved_nice);
855 PROC_UNLOCK(td->td_proc);
858 if (fs->fs_active != 0) {
859 free(fs->fs_active, M_DEVBUF);
864 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
867 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED);
868 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
874 vn_finished_write(wrtmp);
875 process_deferred_inactive(mp);
880 * Copy a cylinder group map. All the unallocated blocks are marked
881 * BLK_NOCOPY so that the snapshot knows that it need not copy them
882 * if they are later written. If passno is one, then this is a first
883 * pass, so only setting needs to be done. If passno is 2, then this
884 * is a revision to a previous pass which must be undone as the
885 * replacement pass is done.
888 cgaccount(cg, vp, nbp, passno)
894 struct buf *bp, *ibp;
898 ufs2_daddr_t base, numblks;
899 int error, len, loc, indiroff;
903 if ((error = ffs_getcg(fs, ITODEVVP(ip), cg, &bp, &cgp)) != 0)
905 UFS_LOCK(ITOUMP(ip));
908 * Recomputation of summary information might not have been performed
909 * at mount time. Sync up summary information for current cylinder
910 * group while data is in memory to ensure that result of background
911 * fsck is slightly more consistent.
913 fs->fs_cs(fs, cg) = cgp->cg_cs;
914 UFS_UNLOCK(ITOUMP(ip));
915 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
916 if (fs->fs_cgsize < fs->fs_bsize)
917 bzero(&nbp->b_data[fs->fs_cgsize],
918 fs->fs_bsize - fs->fs_cgsize);
919 cgp = (struct cg *)nbp->b_data;
922 nbp->b_flags |= B_VALIDSUSPWRT;
923 numblks = howmany(fs->fs_size, fs->fs_frag);
924 len = howmany(fs->fs_fpg, fs->fs_frag);
925 base = cgbase(fs, cg) / fs->fs_frag;
926 if (base + len >= numblks)
927 len = numblks - base - 1;
929 if (base < UFS_NDADDR) {
930 for ( ; loc < UFS_NDADDR; loc++) {
931 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
932 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
933 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
934 DIP_SET(ip, i_db[loc], 0);
935 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
936 panic("ffs_snapshot: lost direct block");
939 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
940 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
944 indiroff = (base + loc - UFS_NDADDR) % NINDIR(fs);
945 for ( ; loc < len; loc++, indiroff++) {
946 if (indiroff >= NINDIR(fs)) {
948 ibp->b_flags |= B_VALIDSUSPWRT;
950 error = UFS_BALLOC(vp,
951 lblktosize(fs, (off_t)(base + loc)),
952 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
959 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
960 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
962 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
963 [indiroff] == BLK_NOCOPY)
964 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
965 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
966 [indiroff] == BLK_NOCOPY)
967 panic("ffs_snapshot: lost indirect block");
970 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
971 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
972 else if (passno == 2 &&
973 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
974 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
975 else if (passno == 1 &&
976 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
977 panic("ffs_snapshot: lost indirect block");
980 ibp->b_flags |= B_VALIDSUSPWRT;
984 * We have to calculate the crc32c here rather than just setting the
985 * BX_CYLGRP b_xflags because the allocation of the block for the
986 * the cylinder group map will always be a full size block (fs_bsize)
987 * even though the cylinder group may be smaller (fs_cgsize). The
988 * crc32c must be computed only over fs_cgsize whereas the BX_CYLGRP
989 * flag causes it to be computed over the size of the buffer.
991 if ((fs->fs_metackhash & CK_CYLGRP) != 0) {
992 ((struct cg *)nbp->b_data)->cg_ckhash = 0;
993 ((struct cg *)nbp->b_data)->cg_ckhash =
994 calculate_crc32c(~0L, nbp->b_data, fs->fs_cgsize);
1000 * Before expunging a snapshot inode, note all the
1001 * blocks that it claims with BLK_SNAP so that fsck will
1002 * be able to account for those blocks properly and so
1003 * that this snapshot knows that it need not copy them
1004 * if the other snapshot holding them is freed. This code
1005 * is reproduced once each for UFS1 and UFS2.
1008 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1009 struct vnode *snapvp;
1010 struct inode *cancelip;
1012 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1013 struct fs *, ufs_lbn_t, int);
1017 int i, error, indiroff;
1018 ufs_lbn_t lbn, rlbn;
1019 ufs2_daddr_t len, blkno, numblks, blksperindir;
1020 struct ufs1_dinode *dip;
1021 struct thread *td = curthread;
1025 * Prepare to expunge the inode. If its inode block has not
1026 * yet been copied, then allocate and fill the copy.
1028 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1030 if (lbn < UFS_NDADDR) {
1031 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1033 if (DOINGSOFTDEP(snapvp))
1034 softdep_prealloc(snapvp, MNT_WAIT);
1035 td->td_pflags |= TDP_COWINPROGRESS;
1036 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1037 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1038 td->td_pflags &= ~TDP_COWINPROGRESS;
1041 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1042 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1046 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1049 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1050 fs->fs_bsize, KERNCRED, 0, &bp);
1053 if ((error = readblock(snapvp, bp, lbn)) != 0)
1057 * Set a snapshot inode to be a zero length file, regular files
1058 * or unlinked snapshots to be completely unallocated.
1060 dip = (struct ufs1_dinode *)bp->b_data +
1061 ino_to_fsbo(fs, cancelip->i_number);
1062 if (clearmode || cancelip->i_effnlink == 0)
1066 dip->di_flags &= ~SF_SNAPSHOT;
1067 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs1_daddr_t));
1070 * Now go through and expunge all the blocks in the file
1071 * using the function requested.
1073 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1074 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1075 &cancelip->i_din1->di_db[UFS_NDADDR], fs, 0, expungetype)))
1077 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1078 &cancelip->i_din1->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1082 len = numblks - UFS_NDADDR;
1084 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1085 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1086 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1087 blksperindir, fs, acctfunc, expungetype);
1090 blksperindir *= NINDIR(fs);
1091 lbn -= blksperindir + 1;
1092 len -= blksperindir;
1093 rlbn += blksperindir;
1099 * Descend an indirect block chain for vnode cancelvp accounting for all
1100 * its indirect blocks in snapvp.
1103 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1104 blksperindir, fs, acctfunc, expungetype)
1105 struct vnode *snapvp;
1106 struct vnode *cancelvp;
1112 ufs_lbn_t blksperindir;
1114 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1115 struct fs *, ufs_lbn_t, int);
1119 ufs_lbn_t subblksperindir;
1120 struct indir indirs[UFS_NIADDR + 2];
1121 ufs1_daddr_t last, *bap;
1125 if (expungetype == BLK_NOCOPY)
1127 panic("indiracct_ufs1: missing indir");
1129 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1131 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1132 panic("indiracct_ufs1: botched params");
1134 * We have to expand bread here since it will deadlock looking
1135 * up the block number for any blocks that are not in the cache.
1137 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1138 bp->b_blkno = fsbtodb(fs, blkno);
1139 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1140 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1145 * Account for the block pointers in this indirect block.
1147 last = howmany(remblks, blksperindir);
1148 if (last > NINDIR(fs))
1150 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1151 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1153 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1154 level == 0 ? rlbn : -1, expungetype);
1155 if (error || level == 0)
1158 * Account for the block pointers in each of the indirect blocks
1159 * in the levels below us.
1161 subblksperindir = blksperindir / NINDIR(fs);
1162 for (lbn++, level--, i = 0; i < last; i++) {
1163 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1164 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1167 rlbn += blksperindir;
1168 lbn -= blksperindir;
1169 remblks -= blksperindir;
1172 free(bap, M_DEVBUF);
1177 * Do both snap accounting and map accounting.
1180 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1182 ufs1_daddr_t *oldblkp, *lastblkp;
1185 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1189 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1191 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1195 * Identify a set of blocks allocated in a snapshot inode.
1198 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1200 ufs1_daddr_t *oldblkp, *lastblkp;
1203 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1205 struct inode *ip = VTOI(vp);
1206 ufs1_daddr_t blkno, *blkp;
1211 for ( ; oldblkp < lastblkp; oldblkp++) {
1213 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1215 lbn = fragstoblks(fs, blkno);
1216 if (lbn < UFS_NDADDR) {
1217 blkp = &ip->i_din1->di_db[lbn];
1218 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1220 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1221 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1224 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1225 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1228 * If we are expunging a snapshot vnode and we
1229 * find a block marked BLK_NOCOPY, then it is
1230 * one that has been allocated to this snapshot after
1231 * we took our current snapshot and can be ignored.
1233 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1234 if (lbn >= UFS_NDADDR)
1238 panic("snapacct_ufs1: bad block");
1239 *blkp = expungetype;
1240 if (lbn >= UFS_NDADDR)
1248 * Account for a set of blocks allocated in a snapshot inode.
1251 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1253 ufs1_daddr_t *oldblkp, *lastblkp;
1264 inum = ip->i_number;
1269 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1271 if (blkno == 0 || blkno == BLK_NOCOPY)
1273 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1274 *ip->i_snapblklist++ = lblkno;
1275 if (blkno == BLK_SNAP)
1276 blkno = blkstofrags(fs, lblkno);
1277 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1278 vp->v_type, NULL, SINGLETON_KEY);
1284 * Before expunging a snapshot inode, note all the
1285 * blocks that it claims with BLK_SNAP so that fsck will
1286 * be able to account for those blocks properly and so
1287 * that this snapshot knows that it need not copy them
1288 * if the other snapshot holding them is freed. This code
1289 * is reproduced once each for UFS1 and UFS2.
1292 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1293 struct vnode *snapvp;
1294 struct inode *cancelip;
1296 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1297 struct fs *, ufs_lbn_t, int);
1301 int i, error, indiroff;
1302 ufs_lbn_t lbn, rlbn;
1303 ufs2_daddr_t len, blkno, numblks, blksperindir;
1304 struct ufs2_dinode *dip;
1305 struct thread *td = curthread;
1309 * Prepare to expunge the inode. If its inode block has not
1310 * yet been copied, then allocate and fill the copy.
1312 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1314 if (lbn < UFS_NDADDR) {
1315 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1317 if (DOINGSOFTDEP(snapvp))
1318 softdep_prealloc(snapvp, MNT_WAIT);
1319 td->td_pflags |= TDP_COWINPROGRESS;
1320 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1321 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1322 td->td_pflags &= ~TDP_COWINPROGRESS;
1325 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1326 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1330 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1333 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1334 fs->fs_bsize, KERNCRED, 0, &bp);
1337 if ((error = readblock(snapvp, bp, lbn)) != 0)
1341 * Set a snapshot inode to be a zero length file, regular files
1342 * to be completely unallocated.
1344 dip = (struct ufs2_dinode *)bp->b_data +
1345 ino_to_fsbo(fs, cancelip->i_number);
1348 dip->di_flags &= ~SF_SNAPSHOT;
1349 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs2_daddr_t));
1350 if (clearmode || cancelip->i_effnlink == 0)
1353 ffs_update_dinode_ckhash(fs, dip);
1356 * Now go through and expunge all the blocks in the file
1357 * using the function requested.
1359 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1360 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1361 &cancelip->i_din2->di_db[UFS_NDADDR], fs, 0, expungetype)))
1363 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1364 &cancelip->i_din2->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1368 len = numblks - UFS_NDADDR;
1370 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1371 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1372 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1373 blksperindir, fs, acctfunc, expungetype);
1376 blksperindir *= NINDIR(fs);
1377 lbn -= blksperindir + 1;
1378 len -= blksperindir;
1379 rlbn += blksperindir;
1385 * Descend an indirect block chain for vnode cancelvp accounting for all
1386 * its indirect blocks in snapvp.
1389 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1390 blksperindir, fs, acctfunc, expungetype)
1391 struct vnode *snapvp;
1392 struct vnode *cancelvp;
1398 ufs_lbn_t blksperindir;
1400 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1401 struct fs *, ufs_lbn_t, int);
1405 ufs_lbn_t subblksperindir;
1406 struct indir indirs[UFS_NIADDR + 2];
1407 ufs2_daddr_t last, *bap;
1411 if (expungetype == BLK_NOCOPY)
1413 panic("indiracct_ufs2: missing indir");
1415 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1417 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1418 panic("indiracct_ufs2: botched params");
1420 * We have to expand bread here since it will deadlock looking
1421 * up the block number for any blocks that are not in the cache.
1423 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1424 bp->b_blkno = fsbtodb(fs, blkno);
1425 if ((bp->b_flags & B_CACHE) == 0 &&
1426 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1431 * Account for the block pointers in this indirect block.
1433 last = howmany(remblks, blksperindir);
1434 if (last > NINDIR(fs))
1436 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1437 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1439 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1440 level == 0 ? rlbn : -1, expungetype);
1441 if (error || level == 0)
1444 * Account for the block pointers in each of the indirect blocks
1445 * in the levels below us.
1447 subblksperindir = blksperindir / NINDIR(fs);
1448 for (lbn++, level--, i = 0; i < last; i++) {
1449 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1450 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1453 rlbn += blksperindir;
1454 lbn -= blksperindir;
1455 remblks -= blksperindir;
1458 free(bap, M_DEVBUF);
1463 * Do both snap accounting and map accounting.
1466 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1468 ufs2_daddr_t *oldblkp, *lastblkp;
1471 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1475 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1477 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1481 * Identify a set of blocks allocated in a snapshot inode.
1484 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1486 ufs2_daddr_t *oldblkp, *lastblkp;
1489 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1491 struct inode *ip = VTOI(vp);
1492 ufs2_daddr_t blkno, *blkp;
1497 for ( ; oldblkp < lastblkp; oldblkp++) {
1499 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1501 lbn = fragstoblks(fs, blkno);
1502 if (lbn < UFS_NDADDR) {
1503 blkp = &ip->i_din2->di_db[lbn];
1504 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1506 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1507 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1510 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1511 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1514 * If we are expunging a snapshot vnode and we
1515 * find a block marked BLK_NOCOPY, then it is
1516 * one that has been allocated to this snapshot after
1517 * we took our current snapshot and can be ignored.
1519 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1520 if (lbn >= UFS_NDADDR)
1524 panic("snapacct_ufs2: bad block");
1525 *blkp = expungetype;
1526 if (lbn >= UFS_NDADDR)
1534 * Account for a set of blocks allocated in a snapshot inode.
1537 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1539 ufs2_daddr_t *oldblkp, *lastblkp;
1550 inum = ip->i_number;
1555 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1557 if (blkno == 0 || blkno == BLK_NOCOPY)
1559 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1560 *ip->i_snapblklist++ = lblkno;
1561 if (blkno == BLK_SNAP)
1562 blkno = blkstofrags(fs, lblkno);
1563 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1564 vp->v_type, NULL, SINGLETON_KEY);
1570 * Decrement extra reference on snapshot when last name is removed.
1571 * It will not be freed until the last open reference goes away.
1580 struct snapdata *sn;
1581 struct ufsmount *ump;
1584 * Find snapshot in incore list.
1587 sn = ITODEVVP(ip)->v_rdev->si_snapdata;
1589 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1596 printf("ffs_snapgone: lost snapshot vnode %ju\n",
1597 (uintmax_t)ip->i_number);
1600 * Delete snapshot inode from superblock. Keep list dense.
1605 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1606 if (fs->fs_snapinum[snaploc] == ip->i_number)
1608 if (snaploc < FSMAXSNAP) {
1609 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1610 if (fs->fs_snapinum[snaploc] == 0)
1612 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1614 fs->fs_snapinum[snaploc - 1] = 0;
1620 * Prepare a snapshot file for being removed.
1627 struct vnode *devvp;
1630 ufs2_daddr_t numblks, blkno, dblk;
1631 int error, i, last, loc;
1632 struct snapdata *sn;
1636 devvp = ITODEVVP(ip);
1638 * If active, delete from incore list (this snapshot may
1639 * already have been in the process of being deleted, so
1640 * would not have been active).
1642 * Clear copy-on-write flag if last snapshot.
1645 if (ip->i_nextsnap.tqe_prev != 0) {
1646 sn = devvp->v_rdev->si_snapdata;
1647 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1648 ip->i_nextsnap.tqe_prev = 0;
1650 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1651 for (i = 0; i < sn->sn_lock.lk_recurse; i++)
1652 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1653 KASSERT(vp->v_vnlock == &sn->sn_lock,
1654 ("ffs_snapremove: lost lock mutation"));
1655 vp->v_vnlock = &vp->v_lock;
1657 while (sn->sn_lock.lk_recurse > 0)
1658 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1659 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1660 try_free_snapdata(devvp);
1664 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1665 * snapshots that want them (see ffs_snapblkfree below).
1667 for (blkno = 1; blkno < UFS_NDADDR; blkno++) {
1668 dblk = DIP(ip, i_db[blkno]);
1671 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1672 DIP_SET(ip, i_db[blkno], 0);
1673 else if ((dblk == blkstofrags(fs, blkno) &&
1674 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, fs->fs_bsize,
1675 ip->i_number, vp->v_type, NULL))) {
1676 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1677 btodb(fs->fs_bsize));
1678 DIP_SET(ip, i_db[blkno], 0);
1681 numblks = howmany(ip->i_size, fs->fs_bsize);
1682 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1683 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1684 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1687 if (fs->fs_size - blkno > NINDIR(fs))
1690 last = fs->fs_size - blkno;
1691 for (loc = 0; loc < last; loc++) {
1692 if (I_IS_UFS1(ip)) {
1693 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1696 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1697 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1698 else if ((dblk == blkstofrags(fs, blkno) &&
1699 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1700 fs->fs_bsize, ip->i_number, vp->v_type,
1702 ip->i_din1->di_blocks -=
1703 btodb(fs->fs_bsize);
1704 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1708 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1711 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1712 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1713 else if ((dblk == blkstofrags(fs, blkno) &&
1714 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1715 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1716 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1717 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1723 * Clear snapshot flag and drop reference.
1725 ip->i_flags &= ~SF_SNAPSHOT;
1726 DIP_SET(ip, i_flags, ip->i_flags);
1727 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1729 * The dirtied indirects must be written out before
1730 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1731 * may find indirect pointers using the magic BLK_* values.
1733 if (DOINGSOFTDEP(vp))
1734 ffs_syncvnode(vp, MNT_WAIT, 0);
1737 * Reenable disk quotas for ex-snapshot file.
1739 if (!getinoquota(ip))
1740 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1745 * Notification that a block is being freed. Return zero if the free
1746 * should be allowed to proceed. Return non-zero if the snapshot file
1747 * wants to claim the block. The block will be claimed if it is an
1748 * uncopied part of one of the snapshots. It will be freed if it is
1749 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1750 * If a fragment is being freed, then all snapshots that care about
1751 * it must make a copy since a snapshot file can only claim full sized
1752 * blocks. Note that if more than one snapshot file maps the block,
1753 * we can pick one at random to claim it. Since none of the snapshots
1754 * can change, we are assurred that they will all see the same unmodified
1755 * image. When deleting a snapshot file (see ffs_snapremove above), we
1756 * must push any of these claimed blocks to one of the other snapshots
1757 * that maps it. These claimed blocks are easily identified as they will
1758 * have a block number equal to their logical block number within the
1759 * snapshot. A copied block can never have this property because they
1760 * must always have been allocated from a BLK_NOCOPY location.
1763 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1765 struct vnode *devvp;
1770 struct workhead *wkhd;
1772 struct buf *ibp, *cbp, *savedcbp = NULL;
1773 struct thread *td = curthread;
1775 struct vnode *vp = NULL;
1778 int indiroff = 0, error = 0, claimedblk = 0;
1779 struct snapdata *sn;
1781 lbn = fragstoblks(fs, bno);
1784 sn = devvp->v_rdev->si_snapdata;
1789 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1790 VI_MTX(devvp)) != 0)
1792 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1794 if (DOINGSOFTDEP(vp))
1795 softdep_prealloc(vp, MNT_WAIT);
1797 * Lookup block being written.
1799 if (lbn < UFS_NDADDR) {
1800 blkno = DIP(ip, i_db[lbn]);
1802 td->td_pflags |= TDP_COWINPROGRESS;
1803 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1804 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1805 td->td_pflags &= ~TDP_COWINPROGRESS;
1808 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1810 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1812 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1815 * Check to see if block needs to be copied.
1819 * A block that we map is being freed. If it has not
1820 * been claimed yet, we will claim or copy it (below).
1823 } else if (blkno == BLK_SNAP) {
1825 * No previous snapshot claimed the block,
1826 * so it will be freed and become a BLK_NOCOPY
1827 * (don't care) for us.
1830 panic("snapblkfree: inconsistent block type");
1831 if (lbn < UFS_NDADDR) {
1832 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1833 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1834 } else if (I_IS_UFS1(ip)) {
1835 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1839 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1844 } else /* BLK_NOCOPY or default */ {
1846 * If the snapshot has already copied the block
1847 * (default), or does not care about the block,
1850 if (lbn >= UFS_NDADDR)
1855 * If this is a full size block, we will just grab it
1856 * and assign it to the snapshot inode. Otherwise we
1857 * will proceed to copy it. See explanation for this
1858 * routine as to why only a single snapshot needs to
1861 if (size == fs->fs_bsize) {
1864 printf("%s %ju lbn %jd from inum %ju\n",
1865 "Grabonremove: snapino",
1866 (uintmax_t)ip->i_number,
1867 (intmax_t)lbn, (uintmax_t)inum);
1870 * If journaling is tracking this write we must add
1871 * the work to the inode or indirect being written.
1874 if (lbn < UFS_NDADDR)
1875 softdep_inode_append(ip,
1876 curthread->td_ucred, wkhd);
1878 softdep_buf_append(ibp, wkhd);
1880 if (lbn < UFS_NDADDR) {
1881 DIP_SET(ip, i_db[lbn], bno);
1882 } else if (I_IS_UFS1(ip)) {
1883 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1886 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1889 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1890 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1891 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1894 if (lbn >= UFS_NDADDR)
1897 * Allocate the block into which to do the copy. Note that this
1898 * allocation will never require any additional allocations for
1899 * the snapshot inode.
1901 td->td_pflags |= TDP_COWINPROGRESS;
1902 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1903 fs->fs_bsize, KERNCRED, 0, &cbp);
1904 td->td_pflags &= ~TDP_COWINPROGRESS;
1909 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n",
1910 "Copyonremove: snapino ", (uintmax_t)ip->i_number,
1911 (intmax_t)lbn, "for inum", (uintmax_t)inum, size,
1912 (intmax_t)cbp->b_blkno);
1915 * If we have already read the old block contents, then
1916 * simply copy them to the new block. Note that we need
1917 * to synchronously write snapshots that have not been
1918 * unlinked, and hence will be visible after a crash,
1919 * to ensure their integrity. At a minimum we ensure the
1920 * integrity of the filesystem metadata, but use the
1921 * dopersistence sysctl-setable flag to decide on the
1922 * persistence needed for file content data.
1924 if (savedcbp != NULL) {
1925 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1927 if ((vtype == VDIR || dopersistence) &&
1929 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1933 * Otherwise, read the old block contents into the buffer.
1935 if ((error = readblock(vp, cbp, lbn)) != 0) {
1936 bzero(cbp->b_data, fs->fs_bsize);
1938 if ((vtype == VDIR || dopersistence) &&
1940 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1946 * Note that we need to synchronously write snapshots that
1947 * have not been unlinked, and hence will be visible after
1948 * a crash, to ensure their integrity. At a minimum we
1949 * ensure the integrity of the filesystem metadata, but
1950 * use the dopersistence sysctl-setable flag to decide on
1951 * the persistence needed for file content data.
1954 vp = savedcbp->b_vp;
1956 if ((vtype == VDIR || dopersistence) &&
1957 VTOI(vp)->i_effnlink > 0)
1958 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1961 * If we have been unable to allocate a block in which to do
1962 * the copy, then return non-zero so that the fragment will
1963 * not be freed. Although space will be lost, the snapshot
1964 * will stay consistent.
1966 if (error != 0 && wkhd != NULL)
1967 softdep_freework(wkhd);
1968 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1973 * Associate snapshot files when mounting.
1976 ffs_snapshot_mount(mp)
1979 struct ufsmount *ump = VFSTOUFS(mp);
1980 struct vnode *devvp = ump->um_devvp;
1981 struct fs *fs = ump->um_fs;
1982 struct thread *td = curthread;
1983 struct snapdata *sn;
1985 struct vnode *lastvp;
1991 daddr_t snaplistsize;
1992 int error, snaploc, loc;
1995 * XXX The following needs to be set before ffs_truncate or
1996 * VOP_READ can be called.
1998 mp->mnt_stat.f_iosize = fs->fs_bsize;
2000 * Process each snapshot listed in the superblock.
2005 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
2006 if (fs->fs_snapinum[snaploc] == 0)
2008 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
2009 LK_EXCLUSIVE, &vp)) != 0){
2010 printf("ffs_snapshot_mount: vget failed %d\n", error);
2014 if (vp->v_type != VREG) {
2015 reason = "non-file snapshot";
2016 } else if (!IS_SNAPSHOT(ip)) {
2017 reason = "non-snapshot";
2018 } else if (ip->i_size ==
2019 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
2020 reason = "old format snapshot";
2021 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED);
2022 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
2026 if (reason != NULL) {
2027 printf("ffs_snapshot_mount: %s inode %d\n",
2028 reason, fs->fs_snapinum[snaploc]);
2031 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2032 if (fs->fs_snapinum[loc] == 0)
2034 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2036 fs->fs_snapinum[loc - 1] = 0;
2041 * Acquire a lock on the snapdata structure, creating it if
2044 sn = ffs_snapdata_acquire(devvp);
2046 * Change vnode to use shared snapshot lock instead of the
2047 * original private lock.
2049 vp->v_vnlock = &sn->sn_lock;
2050 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2052 * Link it onto the active snapshot list.
2055 if (ip->i_nextsnap.tqe_prev != 0)
2056 panic("ffs_snapshot_mount: %ju already on list",
2057 (uintmax_t)ip->i_number);
2059 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2060 vp->v_vflag |= VV_SYSTEM;
2067 * No usable snapshots found.
2069 if (sn == NULL || vp == NULL)
2072 * Allocate the space for the block hints list. We always want to
2073 * use the list from the newest snapshot.
2075 auio.uio_iov = &aiov;
2076 auio.uio_iovcnt = 1;
2077 aiov.iov_base = (void *)&snaplistsize;
2078 aiov.iov_len = sizeof(snaplistsize);
2079 auio.uio_resid = aiov.iov_len;
2081 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2082 auio.uio_segflg = UIO_SYSSPACE;
2083 auio.uio_rw = UIO_READ;
2085 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2086 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2087 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2091 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2092 M_UFSMNT, M_WAITOK);
2093 auio.uio_iovcnt = 1;
2094 aiov.iov_base = snapblklist;
2095 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2096 auio.uio_resid = aiov.iov_len;
2097 auio.uio_offset -= sizeof(snaplistsize);
2098 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2099 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2101 free(snapblklist, M_UFSMNT);
2106 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2107 sn->sn_listsize = snaplistsize;
2108 sn->sn_blklist = (daddr_t *)snapblklist;
2109 devvp->v_vflag |= VV_COPYONWRITE;
2114 * Disassociate snapshot files when unmounting.
2117 ffs_snapshot_unmount(mp)
2120 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2121 struct snapdata *sn;
2126 sn = devvp->v_rdev->si_snapdata;
2127 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2129 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2130 xp->i_nextsnap.tqe_prev = 0;
2131 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2133 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2134 KASSERT(vp->v_vnlock == &sn->sn_lock,
2135 ("ffs_snapshot_unmount: lost lock mutation"));
2136 vp->v_vnlock = &vp->v_lock;
2137 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2138 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2139 if (xp->i_effnlink > 0)
2142 sn = devvp->v_rdev->si_snapdata;
2144 try_free_snapdata(devvp);
2145 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2149 * Check the buffer block to be belong to device buffer that shall be
2150 * locked after snaplk. devvp shall be locked on entry, and will be
2151 * leaved locked upon exit.
2154 ffs_bp_snapblk(devvp, bp)
2155 struct vnode *devvp;
2158 struct snapdata *sn;
2160 ufs2_daddr_t lbn, *snapblklist;
2161 int lower, upper, mid;
2163 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2164 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2165 sn = devvp->v_rdev->si_snapdata;
2166 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2168 fs = ITOFS(TAILQ_FIRST(&sn->sn_head));
2169 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2170 snapblklist = sn->sn_blklist;
2171 upper = sn->sn_listsize - 1;
2173 while (lower <= upper) {
2174 mid = (lower + upper) / 2;
2175 if (snapblklist[mid] == lbn)
2177 if (snapblklist[mid] < lbn)
2193 struct vnode *vp, *devvp;
2197 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2202 devvp = bo2vnode(bo);
2203 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2206 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2210 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2211 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2216 * Try to find a buffer to flush.
2218 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2219 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2221 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2224 panic("bdwrite: found ourselves");
2227 * Don't countdeps with the bo lock
2230 if (buf_countdeps(nbp, 0)) {
2237 if (!ffs_bp_snapblk(vp, nbp)) {
2245 if (nbp->b_flags & B_CLUSTEROK) {
2246 vfs_bio_awrite(nbp);
2251 dirtybufferflushes++;
2260 * Check for need to copy block that is about to be written,
2261 * copying the block if necessary.
2264 ffs_copyonwrite(devvp, bp)
2265 struct vnode *devvp;
2268 struct snapdata *sn;
2269 struct buf *ibp, *cbp, *savedcbp = NULL;
2270 struct thread *td = curthread;
2273 struct vnode *vp = NULL;
2274 ufs2_daddr_t lbn, blkno, *snapblklist;
2275 int lower, upper, mid, indiroff, error = 0;
2276 int launched_async_io, prev_norunningbuf;
2277 long saved_runningbufspace;
2279 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2280 return (0); /* Update on a snapshot file */
2281 if (td->td_pflags & TDP_COWINPROGRESS)
2282 panic("ffs_copyonwrite: recursive call");
2284 * First check to see if it is in the preallocated list.
2285 * By doing this check we avoid several potential deadlocks.
2288 sn = devvp->v_rdev->si_snapdata;
2290 TAILQ_EMPTY(&sn->sn_head)) {
2292 return (0); /* No snapshot */
2294 ip = TAILQ_FIRST(&sn->sn_head);
2296 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2297 snapblklist = sn->sn_blklist;
2298 upper = sn->sn_listsize - 1;
2300 while (lower <= upper) {
2301 mid = (lower + upper) / 2;
2302 if (snapblklist[mid] == lbn)
2304 if (snapblklist[mid] < lbn)
2309 if (lower <= upper) {
2313 launched_async_io = 0;
2314 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2316 * Since I/O on bp isn't yet in progress and it may be blocked
2317 * for a long time waiting on snaplk, back it out of
2318 * runningbufspace, possibly waking other threads waiting for space.
2320 saved_runningbufspace = bp->b_runningbufspace;
2321 if (saved_runningbufspace != 0)
2322 runningbufwakeup(bp);
2324 * Not in the precomputed list, so check the snapshots.
2326 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2327 VI_MTX(devvp)) != 0) {
2329 sn = devvp->v_rdev->si_snapdata;
2331 TAILQ_EMPTY(&sn->sn_head)) {
2333 if (saved_runningbufspace != 0) {
2334 bp->b_runningbufspace = saved_runningbufspace;
2335 atomic_add_long(&runningbufspace,
2336 bp->b_runningbufspace);
2338 return (0); /* Snapshot gone */
2341 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2343 if (DOINGSOFTDEP(vp))
2344 softdep_prealloc(vp, MNT_WAIT);
2346 * We ensure that everything of our own that needs to be
2347 * copied will be done at the time that ffs_snapshot is
2348 * called. Thus we can skip the check here which can
2349 * deadlock in doing the lookup in UFS_BALLOC.
2354 * Check to see if block needs to be copied. We do not have
2355 * to hold the snapshot lock while doing this lookup as it
2356 * will never require any additional allocations for the
2359 if (lbn < UFS_NDADDR) {
2360 blkno = DIP(ip, i_db[lbn]);
2362 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2363 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2364 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2365 td->td_pflags &= ~TDP_COWINPROGRESS;
2368 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
2370 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2372 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2376 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2377 panic("ffs_copyonwrite: bad copy block");
2382 * Allocate the block into which to do the copy. Since
2383 * multiple processes may all try to copy the same block,
2384 * we have to recheck our need to do a copy if we sleep
2385 * waiting for the lock.
2387 * Because all snapshots on a filesystem share a single
2388 * lock, we ensure that we will never be in competition
2389 * with another process to allocate a block.
2391 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2392 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2393 fs->fs_bsize, KERNCRED, 0, &cbp);
2394 td->td_pflags &= ~TDP_COWINPROGRESS;
2399 printf("Copyonwrite: snapino %ju lbn %jd for ",
2400 (uintmax_t)ip->i_number, (intmax_t)lbn);
2401 if (bp->b_vp == devvp)
2402 printf("fs metadata");
2405 (uintmax_t)VTOI(bp->b_vp)->i_number);
2406 printf(" lblkno %jd to blkno %jd\n",
2407 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2411 * If we have already read the old block contents, then
2412 * simply copy them to the new block. Note that we need
2413 * to synchronously write snapshots that have not been
2414 * unlinked, and hence will be visible after a crash,
2415 * to ensure their integrity. At a minimum we ensure the
2416 * integrity of the filesystem metadata, but use the
2417 * dopersistence sysctl-setable flag to decide on the
2418 * persistence needed for file content data.
2420 if (savedcbp != NULL) {
2421 bcopy(savedcbp->b_data, 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;
2431 * Otherwise, read the old block contents into the buffer.
2433 if ((error = readblock(vp, cbp, lbn)) != 0) {
2434 bzero(cbp->b_data, fs->fs_bsize);
2436 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2437 dopersistence) && ip->i_effnlink > 0)
2438 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2440 launched_async_io = 1;
2446 * Note that we need to synchronously write snapshots that
2447 * have not been unlinked, and hence will be visible after
2448 * a crash, to ensure their integrity. At a minimum we
2449 * ensure the integrity of the filesystem metadata, but
2450 * use the dopersistence sysctl-setable flag to decide on
2451 * the persistence needed for file content data.
2454 vp = savedcbp->b_vp;
2456 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2457 dopersistence) && VTOI(vp)->i_effnlink > 0)
2458 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2460 launched_async_io = 1;
2462 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2463 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2465 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2466 waitrunningbufspace();
2468 * I/O on bp will now be started, so count it in runningbufspace.
2470 if (saved_runningbufspace != 0) {
2471 bp->b_runningbufspace = saved_runningbufspace;
2472 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2478 * sync snapshots to force freework records waiting on snapshots to claim
2482 ffs_sync_snap(mp, waitfor)
2486 struct snapdata *sn;
2487 struct vnode *devvp;
2491 devvp = VFSTOUFS(mp)->um_devvp;
2492 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2496 sn = devvp->v_rdev->si_snapdata;
2501 if (lockmgr(&sn->sn_lock,
2502 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2503 VI_MTX(devvp)) == 0)
2506 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2508 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2510 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2514 * Read the specified block into the given buffer.
2515 * Much of this boiler-plate comes from bwrite().
2518 readblock(vp, bp, lbn)
2530 bip = g_alloc_bio();
2531 bip->bio_cmd = BIO_READ;
2532 bip->bio_offset = dbtob(fsbtodb(fs, blkstofrags(fs, lbn)));
2533 bip->bio_data = bp->b_data;
2534 bip->bio_length = bp->b_bcount;
2535 bip->bio_done = NULL;
2537 g_io_request(bip, ITODEVVP(ip)->v_bufobj.bo_private);
2538 bp->b_error = biowait(bip, "snaprdb");
2540 return (bp->b_error);
2546 * Process file deletes that were deferred by ufs_inactive() due to
2547 * the file system being suspended. Transfer IN_LAZYACCESS into
2548 * IN_MODIFIED for vnodes that were accessed during suspension.
2551 process_deferred_inactive(struct mount *mp)
2553 struct vnode *vp, *mvp;
2559 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2561 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2563 * IN_LAZYACCESS is checked here without holding any
2564 * vnode lock, but this flag is set only while holding
2567 if (vp->v_type == VNON ||
2568 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2569 ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
2574 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2577 if (error == ENOENT)
2578 continue; /* vnode recycled */
2579 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2583 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2584 ip->i_flag &= ~IN_LAZYACCESS;
2585 ip->i_flag |= IN_MODIFIED;
2588 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2595 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2596 ("process_deferred_inactive: got VI_OWEINACT"));
2601 vn_finished_secondary_write(mp);
2604 #ifndef NO_FFS_SNAPSHOT
2606 static struct snapdata *
2607 ffs_snapdata_alloc(void)
2609 struct snapdata *sn;
2612 * Fetch a snapdata from the free list if there is one available.
2614 mtx_lock(&snapfree_lock);
2615 sn = LIST_FIRST(&snapfree);
2617 LIST_REMOVE(sn, sn_link);
2618 mtx_unlock(&snapfree_lock);
2622 * If there were no free snapdatas allocate one.
2624 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2625 TAILQ_INIT(&sn->sn_head);
2626 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2627 LK_CANRECURSE | LK_NOSHARE);
2632 * The snapdata is never freed because we can not be certain that
2633 * there are no threads sleeping on the snap lock. Persisting
2634 * them permanently avoids costly synchronization in ffs_lock().
2637 ffs_snapdata_free(struct snapdata *sn)
2639 mtx_lock(&snapfree_lock);
2640 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2641 mtx_unlock(&snapfree_lock);
2644 /* Try to free snapdata associated with devvp */
2646 try_free_snapdata(struct vnode *devvp)
2648 struct snapdata *sn;
2649 ufs2_daddr_t *snapblklist;
2651 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2652 sn = devvp->v_rdev->si_snapdata;
2654 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2655 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2660 devvp->v_rdev->si_snapdata = NULL;
2661 devvp->v_vflag &= ~VV_COPYONWRITE;
2662 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2663 snapblklist = sn->sn_blklist;
2664 sn->sn_blklist = NULL;
2665 sn->sn_listsize = 0;
2666 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2667 if (snapblklist != NULL)
2668 free(snapblklist, M_UFSMNT);
2669 ffs_snapdata_free(sn);
2672 static struct snapdata *
2673 ffs_snapdata_acquire(struct vnode *devvp)
2675 struct snapdata *nsn, *sn;
2679 * Allocate a free snapdata. This is done before acquiring the
2680 * devvp lock to avoid allocation while the devvp interlock is
2683 nsn = ffs_snapdata_alloc();
2687 sn = devvp->v_rdev->si_snapdata;
2690 * This is the first snapshot on this
2691 * filesystem and we use our pre-allocated
2692 * snapdata. Publish sn with the sn_lock
2693 * owned by us, to avoid the race.
2695 error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE |
2698 panic("leaked sn, lockmgr error %d", error);
2699 sn = devvp->v_rdev->si_snapdata = nsn;
2706 * There is a snapshots which already exists on this
2707 * filesystem, grab a reference to the common lock.
2709 error = lockmgr(&sn->sn_lock, LK_INTERLOCK |
2710 LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp));
2716 * Free any unused snapdata.
2719 ffs_snapdata_free(nsn);