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>
63 #include <vm/vm_extern.h>
65 #include <geom/geom.h>
67 #include <ufs/ufs/extattr.h>
68 #include <ufs/ufs/quota.h>
69 #include <ufs/ufs/ufsmount.h>
70 #include <ufs/ufs/inode.h>
71 #include <ufs/ufs/ufs_extern.h>
73 #include <ufs/ffs/fs.h>
74 #include <ufs/ffs/ffs_extern.h>
76 #define KERNCRED thread0.td_ucred
80 #ifdef NO_FFS_SNAPSHOT
82 ffs_snapshot(mp, snapfile)
90 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
97 struct workhead *wkhd;
109 ffs_snapshot_mount(mp)
115 ffs_snapshot_unmount(mp)
127 ffs_copyonwrite(devvp, bp)
135 ffs_sync_snap(mp, waitfor)
142 FEATURE(ffs_snapshot, "FFS snapshot support");
144 LIST_HEAD(, snapdata) snapfree;
145 static struct mtx snapfree_lock;
146 MTX_SYSINIT(ffs_snapfree, &snapfree_lock, "snapdata free list", MTX_DEF);
148 static int cgaccount(int, struct vnode *, struct buf *, int);
149 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
150 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
151 ufs_lbn_t, int), int, int);
152 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
153 ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
154 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
155 ufs_lbn_t, int), int);
156 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
157 struct fs *, ufs_lbn_t, int);
158 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
159 struct fs *, ufs_lbn_t, int);
160 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
161 struct fs *, ufs_lbn_t, int);
162 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
163 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
164 ufs_lbn_t, int), int, int);
165 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
166 ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
167 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
168 ufs_lbn_t, int), int);
169 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
170 struct fs *, ufs_lbn_t, int);
171 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
172 struct fs *, ufs_lbn_t, int);
173 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
174 struct fs *, ufs_lbn_t, int);
175 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
176 static void try_free_snapdata(struct vnode *devvp);
177 static struct snapdata *ffs_snapdata_acquire(struct vnode *devvp);
178 static int ffs_bp_snapblk(struct vnode *, struct buf *);
181 * To ensure the consistency of snapshots across crashes, we must
182 * synchronously write out copied blocks before allowing the
183 * originals to be modified. Because of the rather severe speed
184 * penalty that this imposes, the code normally only ensures
185 * persistence for the filesystem metadata contained within a
186 * snapshot. Setting the following flag allows this crash
187 * persistence to be enabled for file contents.
189 int dopersistence = 0;
192 #include <sys/sysctl.h>
193 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
194 static int snapdebug = 0;
195 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
196 int collectsnapstats = 0;
197 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
199 #endif /* DIAGNOSTIC */
202 * Create a snapshot file and initialize it for the filesystem.
205 ffs_snapshot(mp, snapfile)
209 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
210 int error, cg, snaploc;
211 int i, size, len, loc;
212 ufs2_daddr_t blockno;
215 long redo = 0, snaplistsize = 0;
218 struct fs *copy_fs = NULL, *fs;
219 struct thread *td = curthread;
220 struct inode *ip, *xp;
221 struct buf *bp, *nbp, *ibp;
225 struct vnode *vp, *xvp, *mvp, *devvp;
229 struct ufsmount *ump;
231 struct timespec starttime = {0, 0}, endtime;
238 * At the moment, journaled soft updates cannot support
241 if (MOUNTEDSUJ(mp)) {
242 vfs_mount_error(mp, "%s: Snapshots are not yet supported when "
243 "running with journaled soft updates", fs->fs_fsmnt);
250 * Need to serialize access to snapshot code per filesystem.
253 * Assign a snapshot slot in the superblock.
256 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
257 if (fs->fs_snapinum[snaploc] == 0)
260 if (snaploc == FSMAXSNAP)
263 * Create the snapshot file.
266 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF | NOCACHE, UIO_SYSSPACE,
268 if ((error = namei(&nd)) != 0)
270 if (nd.ni_vp != NULL) {
274 if (nd.ni_dvp->v_mount != mp)
277 NDFREE(&nd, NDF_ONLY_PNBUF);
278 if (nd.ni_dvp == nd.ni_vp)
286 vat.va_mode = S_IRUSR;
287 vat.va_vaflags |= VA_EXCLUSIVE;
288 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
291 panic("ffs_snapshot: mount mismatch");
293 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
294 NDFREE(&nd, NDF_ONLY_PNBUF);
296 if ((error = vn_start_write(NULL, &wrtmp,
297 V_XSLEEP | PCATCH)) != 0)
301 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
303 VOP_VPUT_PAIR(nd.ni_dvp, NULL, true);
304 NDFREE(&nd, NDF_ONLY_PNBUF);
305 vn_finished_write(wrtmp);
306 if (error == ERELOOKUP)
312 VOP_VPUT_PAIR(nd.ni_dvp, &vp, false);
313 if (VN_IS_DOOMED(vp)) {
317 vnode_create_vobject(nd.ni_vp, fs->fs_size, td);
318 vp->v_vflag |= VV_SYSTEM;
320 devvp = ITODEVVP(ip);
322 * Calculate the size of the filesystem then allocate the block
323 * immediately following the last block of the filesystem that
324 * will contain the snapshot list. This operation allows us to
325 * set the size of the snapshot.
327 numblks = howmany(fs->fs_size, fs->fs_frag);
328 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)numblks),
329 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
333 ip->i_size = lblktosize(fs, (off_t)(numblks + 1));
334 vnode_pager_setsize(vp, ip->i_size);
335 DIP_SET(ip, i_size, ip->i_size);
336 UFS_INODE_SET_FLAG(ip, IN_SIZEMOD | IN_CHANGE | IN_UPDATE);
338 * Preallocate critical data structures so that we can copy
339 * them in without further allocation after we suspend all
340 * operations on the filesystem. We would like to just release
341 * the allocated buffers without writing them since they will
342 * be filled in below once we are ready to go, but this upsets
343 * the soft update code, so we go ahead and write the new buffers.
345 * Allocate all indirect blocks and mark all of them as not
346 * needing to be copied.
348 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
349 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
350 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
356 * Allocate copies for the superblock and its summary information.
358 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
363 blkno = fragstoblks(fs, fs->fs_csaddr);
364 len = howmany(fs->fs_cssize, fs->fs_bsize);
365 for (loc = 0; loc < len; loc++) {
366 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
367 fs->fs_bsize, KERNCRED, 0, &nbp);
373 * Allocate all cylinder group blocks.
375 for (cg = 0; cg < fs->fs_ncg; cg++) {
376 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
377 fs->fs_bsize, KERNCRED, 0, &nbp);
382 error = ffs_syncvnode(vp, MNT_WAIT, 0);
383 /* vp possibly reclaimed if unlocked */
389 * Copy all the cylinder group maps. Although the
390 * filesystem is still active, we hope that only a few
391 * cylinder groups will change between now and when we
392 * suspend operations. Thus, we will be able to quickly
393 * touch up the few cylinder groups that changed during
394 * the suspension period.
396 len = roundup2(howmany(fs->fs_ncg, NBBY), sizeof(int));
397 space = malloc(len, M_DEVBUF, M_WAITOK | M_ZERO);
399 fs->fs_active = space;
401 for (cg = 0; cg < fs->fs_ncg; cg++) {
402 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
403 fs->fs_bsize, KERNCRED, 0, &nbp);
406 error = cgaccount(cg, vp, nbp, 1);
408 if (cg % 10 == 0 && error == 0)
409 error = ffs_syncvnode(vp, MNT_WAIT, 0);
414 * Change inode to snapshot type file.
416 ip->i_flags |= SF_SNAPSHOT;
417 DIP_SET(ip, i_flags, ip->i_flags);
418 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
420 * Ensure that the snapshot is completely on disk.
421 * Since we have marked it as a snapshot it is safe to
422 * unlock it as no process will be allowed to write to it.
424 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
428 * All allocations are done, so we can now snapshot the system.
430 * Recind nice scheduling while running with the filesystem suspended.
432 if (td->td_proc->p_nice > 0) {
437 saved_nice = p->p_nice;
442 * Suspend operation on filesystem.
445 vn_finished_write(wrtmp);
446 if ((error = vfs_write_suspend(vp->v_mount, 0)) != 0) {
447 vn_start_write(NULL, &wrtmp, V_WAIT);
448 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
451 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
453 vn_start_write(NULL, &wrtmp, V_WAIT);
455 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
456 if (ip->i_effnlink == 0) {
457 error = ENOENT; /* Snapshot file unlinked */
461 if (collectsnapstats)
462 nanotime(&starttime);
466 * First, copy all the cylinder group maps that have changed.
468 for (cg = 0; cg < fs->fs_ncg; cg++) {
469 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
472 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
473 fs->fs_bsize, KERNCRED, 0, &nbp);
476 error = cgaccount(cg, vp, nbp, 2);
482 * Grab a copy of the superblock and its summary information.
483 * We delay writing it until the suspension is released below.
485 copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK);
486 bcopy(fs, copy_fs, fs->fs_sbsize);
487 copy_fs->fs_si = malloc(sizeof(struct fs_summary_info), M_UFSMNT,
489 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
490 copy_fs->fs_clean = 1;
491 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
492 if (fs->fs_sbsize < size)
493 bzero(&((char *)copy_fs)[fs->fs_sbsize],
494 size - fs->fs_sbsize);
495 size = blkroundup(fs, fs->fs_cssize);
496 if (fs->fs_contigsumsize > 0)
497 size += fs->fs_ncg * sizeof(int32_t);
498 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
499 copy_fs->fs_csp = space;
500 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
501 space = (char *)space + fs->fs_cssize;
502 loc = howmany(fs->fs_cssize, fs->fs_fsize);
503 i = fs->fs_frag - loc % fs->fs_frag;
504 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
506 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
507 len, KERNCRED, &bp)) != 0) {
511 bcopy(bp->b_data, space, (u_int)len);
512 space = (char *)space + len;
513 bp->b_flags |= B_INVAL | B_NOCACHE;
516 if (fs->fs_contigsumsize > 0) {
517 copy_fs->fs_maxcluster = lp = space;
518 for (i = 0; i < fs->fs_ncg; i++)
519 *lp++ = fs->fs_contigsumsize;
522 * We must check for active files that have been unlinked
523 * (e.g., with a zero link count). We have to expunge all
524 * trace of these files from the snapshot so that they are
525 * not reclaimed prematurely by fsck or unnecessarily dumped.
526 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
527 * spec_strategy about writing on a suspended filesystem.
528 * Note that we skip unlinked snapshot files as they will
529 * be handled separately below.
531 * We also calculate the size needed for the snapshot list.
532 * Initial number of entries is composed of:
533 * - one for each cylinder group map
534 * - one for each block used by superblock summary table
535 * - one for each snapshot inode block
536 * - one for the superblock
537 * - one for the snapshot list
538 * The direct block entries in the snapshot are always
539 * copied (see reason below). Note that the superblock and
540 * the first cylinder group will almost always be allocated
541 * in the direct blocks, but we add the slop for them in case
542 * they do not end up there. The snapshot list size may get
543 * expanded by one because of an update of an inode block for
544 * an unlinked but still open file when it is expunged.
546 * Because the direct block pointers are always copied, they
547 * are not added to the list. Instead ffs_copyonwrite()
548 * explicitly checks for them before checking the snapshot list.
550 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
551 FSMAXSNAP + /* superblock */ 1 + /* snaplist */ 1;
553 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
556 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
557 if ((xvp->v_usecount == 0 &&
558 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
559 xvp->v_type == VNON ||
560 IS_SNAPSHOT(VTOI(xvp))) {
565 * We can skip parent directory vnode because it must have
566 * this snapshot file in it.
568 if (xvp == nd.ni_dvp) {
573 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
574 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
579 if (xvp->v_usecount == 0 &&
580 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
589 vn_printf(xvp, "ffs_snapshot: busy vnode ");
591 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
598 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
604 * If there is a fragment, clear it here.
607 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
608 if (loc < UFS_NDADDR) {
609 len = fragroundup(fs, blkoff(fs, xp->i_size));
610 if (len != 0 && len < fs->fs_bsize) {
611 ffs_blkfree(ump, copy_fs, vp,
612 DIP(xp, i_db[loc]), len, xp->i_number,
613 xvp->v_type, NULL, SINGLETON_KEY);
614 blkno = DIP(xp, i_db[loc]);
615 DIP_SET(xp, i_db[loc], 0);
620 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
623 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
626 DIP_SET(xp, i_db[loc], blkno);
628 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
633 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
638 * Erase the journal file from the snapshot.
640 if (fs->fs_flags & FS_SUJ) {
641 error = softdep_journal_lookup(mp, &xvp);
646 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
649 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
654 * Acquire a lock on the snapdata structure, creating it if necessary.
656 sn = ffs_snapdata_acquire(devvp);
658 * Change vnode to use shared snapshot lock instead of the original
661 vp->v_vnlock = &sn->sn_lock;
662 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
663 xp = TAILQ_FIRST(&sn->sn_head);
665 * If this is the first snapshot on this filesystem, then we need
666 * to allocate the space for the list of preallocated snapshot blocks.
667 * This list will be refined below, but this preliminary one will
668 * keep us out of deadlock until the full one is ready.
671 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
673 blkp = &snapblklist[1];
674 *blkp++ = lblkno(fs, fs->fs_sblockloc);
675 blkno = fragstoblks(fs, fs->fs_csaddr);
676 for (cg = 0; cg < fs->fs_ncg; cg++) {
677 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
679 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
681 len = howmany(fs->fs_cssize, fs->fs_bsize);
682 for (loc = 0; loc < len; loc++)
683 *blkp++ = blkno + loc;
684 for (; cg < fs->fs_ncg; cg++)
685 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
686 snapblklist[0] = blkp - snapblklist;
688 if (sn->sn_blklist != NULL)
689 panic("ffs_snapshot: non-empty list");
690 sn->sn_blklist = snapblklist;
691 sn->sn_listsize = blkp - snapblklist;
695 * Preallocate all the direct blocks in the snapshot inode so
696 * that we never have to write the inode itself to commit an
697 * update to the contents of the snapshot. Note that once
698 * created, the size of the snapshot will never change, so
699 * there will never be a need to write the inode except to
700 * update the non-integrity-critical time fields and
701 * allocated-block count.
703 for (blockno = 0; blockno < UFS_NDADDR; blockno++) {
704 if (DIP(ip, i_db[blockno]) != 0)
706 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
707 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
710 error = readblock(vp, bp, blockno);
716 * Record snapshot inode. Since this is the newest snapshot,
717 * it must be placed at the end of the list.
720 fs->fs_snapinum[snaploc] = ip->i_number;
721 if (ip->i_nextsnap.tqe_prev != 0)
722 panic("ffs_snapshot: %ju already on list",
723 (uintmax_t)ip->i_number);
724 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
725 devvp->v_vflag |= VV_COPYONWRITE;
728 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
729 if (error != 0 && copy_fs != NULL) {
730 free(copy_fs->fs_csp, M_UFSMNT);
731 free(copy_fs->fs_si, M_UFSMNT);
732 free(copy_fs, M_UFSMNT);
735 KASSERT(error != 0 || (sn != NULL && copy_fs != NULL),
736 ("missing snapshot setup parameters"));
738 * Resume operation on filesystem.
740 vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR);
742 if (collectsnapstats && starttime.tv_sec > 0) {
744 timespecsub(&endtime, &starttime, &endtime);
745 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
746 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
747 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
753 * Copy allocation information from all the snapshots in
754 * this snapshot and then expunge them from its view.
756 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
760 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
763 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
765 if (error == 0 && xp->i_effnlink == 0) {
766 error = ffs_freefile(ump,
773 fs->fs_snapinum[snaploc] = 0;
778 * Allocate space for the full list of preallocated snapshot blocks.
780 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
782 ip->i_snapblklist = &snapblklist[1];
784 * Expunge the blocks used by the snapshots from the set of
785 * blocks marked as used in the snapshot bitmaps. Also, collect
786 * the list of allocated blocks in i_snapblklist.
789 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
792 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
795 fs->fs_snapinum[snaploc] = 0;
796 free(snapblklist, M_UFSMNT);
799 if (snaplistsize < ip->i_snapblklist - snapblklist)
800 panic("ffs_snapshot: list too small");
801 snaplistsize = ip->i_snapblklist - snapblklist;
802 snapblklist[0] = snaplistsize;
803 ip->i_snapblklist = 0;
805 * Write out the list of allocated blocks to the end of the snapshot.
807 auio.uio_iov = &aiov;
809 aiov.iov_base = (void *)snapblklist;
810 aiov.iov_len = snaplistsize * sizeof(daddr_t);
811 auio.uio_resid = aiov.iov_len;
812 auio.uio_offset = lblktosize(fs, (off_t)numblks);
813 auio.uio_segflg = UIO_SYSSPACE;
814 auio.uio_rw = UIO_WRITE;
816 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
817 fs->fs_snapinum[snaploc] = 0;
818 free(snapblklist, M_UFSMNT);
822 * Write the superblock and its summary information
825 blkno = fragstoblks(fs, fs->fs_csaddr);
826 len = howmany(fs->fs_cssize, fs->fs_bsize);
827 space = copy_fs->fs_csp;
828 for (loc = 0; loc < len; loc++) {
829 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
831 fs->fs_snapinum[snaploc] = 0;
832 free(snapblklist, M_UFSMNT);
835 bcopy(space, nbp->b_data, fs->fs_bsize);
836 space = (char *)space + fs->fs_bsize;
839 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
844 loc = blkoff(fs, fs->fs_sblockloc);
845 copy_fs->fs_fmod = 0;
846 copy_fs->fs_ckhash = ffs_calc_sbhash(copy_fs);
847 bcopy((char *)copy_fs, &nbp->b_data[loc], (u_int)fs->fs_sbsize);
851 * As this is the newest list, it is the most inclusive, so
852 * should replace the previous list.
855 space = sn->sn_blklist;
856 sn->sn_blklist = snapblklist;
857 sn->sn_listsize = snaplistsize;
860 free(space, M_UFSMNT);
862 free(copy_fs->fs_csp, M_UFSMNT);
863 free(copy_fs->fs_si, M_UFSMNT);
864 free(copy_fs, M_UFSMNT);
867 NDFREE(&nd, NDF_ONLY_PNBUF);
868 if (saved_nice > 0) {
873 sched_nice(td->td_proc, saved_nice);
874 PROC_UNLOCK(td->td_proc);
877 if (fs->fs_active != 0) {
878 free(fs->fs_active, M_DEVBUF);
883 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
886 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED);
887 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
893 vn_finished_write(wrtmp);
894 process_deferred_inactive(mp);
899 * Copy a cylinder group map. All the unallocated blocks are marked
900 * BLK_NOCOPY so that the snapshot knows that it need not copy them
901 * if they are later written. If passno is one, then this is a first
902 * pass, so only setting needs to be done. If passno is 2, then this
903 * is a revision to a previous pass which must be undone as the
904 * replacement pass is done.
907 cgaccount(cg, vp, nbp, passno)
913 struct buf *bp, *ibp;
917 ufs2_daddr_t base, numblks;
918 int error, len, loc, indiroff;
922 if ((error = ffs_getcg(fs, ITODEVVP(ip), cg, 0, &bp, &cgp)) != 0)
924 UFS_LOCK(ITOUMP(ip));
927 * Recomputation of summary information might not have been performed
928 * at mount time. Sync up summary information for current cylinder
929 * group while data is in memory to ensure that result of background
930 * fsck is slightly more consistent.
932 fs->fs_cs(fs, cg) = cgp->cg_cs;
933 UFS_UNLOCK(ITOUMP(ip));
934 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
935 if (fs->fs_cgsize < fs->fs_bsize)
936 bzero(&nbp->b_data[fs->fs_cgsize],
937 fs->fs_bsize - fs->fs_cgsize);
938 cgp = (struct cg *)nbp->b_data;
941 nbp->b_flags |= B_VALIDSUSPWRT;
942 numblks = howmany(fs->fs_size, fs->fs_frag);
943 len = howmany(fs->fs_fpg, fs->fs_frag);
944 base = cgbase(fs, cg) / fs->fs_frag;
945 if (base + len >= numblks)
946 len = numblks - base - 1;
948 if (base < UFS_NDADDR) {
949 for ( ; loc < UFS_NDADDR; loc++) {
950 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
951 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
952 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
953 DIP_SET(ip, i_db[loc], 0);
954 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
955 panic("ffs_snapshot: lost direct block");
958 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
959 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
963 indiroff = (base + loc - UFS_NDADDR) % NINDIR(fs);
964 for ( ; loc < len; loc++, indiroff++) {
965 if (indiroff >= NINDIR(fs)) {
967 ibp->b_flags |= B_VALIDSUSPWRT;
969 error = UFS_BALLOC(vp,
970 lblktosize(fs, (off_t)(base + loc)),
971 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
978 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
979 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
981 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
982 [indiroff] == BLK_NOCOPY)
983 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
984 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
985 [indiroff] == BLK_NOCOPY)
986 panic("ffs_snapshot: lost indirect block");
989 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
990 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
991 else if (passno == 2 &&
992 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
993 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
994 else if (passno == 1 &&
995 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
996 panic("ffs_snapshot: lost indirect block");
999 ibp->b_flags |= B_VALIDSUSPWRT;
1003 * We have to calculate the crc32c here rather than just setting the
1004 * BX_CYLGRP b_xflags because the allocation of the block for the
1005 * the cylinder group map will always be a full size block (fs_bsize)
1006 * even though the cylinder group may be smaller (fs_cgsize). The
1007 * crc32c must be computed only over fs_cgsize whereas the BX_CYLGRP
1008 * flag causes it to be computed over the size of the buffer.
1010 if ((fs->fs_metackhash & CK_CYLGRP) != 0) {
1011 ((struct cg *)nbp->b_data)->cg_ckhash = 0;
1012 ((struct cg *)nbp->b_data)->cg_ckhash =
1013 calculate_crc32c(~0L, nbp->b_data, fs->fs_cgsize);
1019 * Before expunging a snapshot inode, note all the
1020 * blocks that it claims with BLK_SNAP so that fsck will
1021 * be able to account for those blocks properly and so
1022 * that this snapshot knows that it need not copy them
1023 * if the other snapshot holding them is freed. This code
1024 * is reproduced once each for UFS1 and UFS2.
1027 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1028 struct vnode *snapvp;
1029 struct inode *cancelip;
1031 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1032 struct fs *, ufs_lbn_t, int);
1036 int i, error, indiroff;
1037 ufs_lbn_t lbn, rlbn;
1038 ufs2_daddr_t len, blkno, numblks, blksperindir;
1039 struct ufs1_dinode *dip;
1040 struct thread *td = curthread;
1044 * Prepare to expunge the inode. If its inode block has not
1045 * yet been copied, then allocate and fill the copy.
1047 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1049 if (lbn < UFS_NDADDR) {
1050 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1052 if (DOINGSOFTDEP(snapvp))
1053 softdep_prealloc(snapvp, MNT_WAIT);
1054 td->td_pflags |= TDP_COWINPROGRESS;
1055 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1056 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1057 td->td_pflags &= ~TDP_COWINPROGRESS;
1060 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1061 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1065 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1068 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1069 fs->fs_bsize, KERNCRED, 0, &bp);
1072 if ((error = readblock(snapvp, bp, lbn)) != 0)
1076 * Set a snapshot inode to be a zero length file, regular files
1077 * or unlinked snapshots to be completely unallocated.
1079 dip = (struct ufs1_dinode *)bp->b_data +
1080 ino_to_fsbo(fs, cancelip->i_number);
1081 if (clearmode || cancelip->i_effnlink == 0)
1085 dip->di_flags &= ~SF_SNAPSHOT;
1086 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs1_daddr_t));
1089 * Now go through and expunge all the blocks in the file
1090 * using the function requested.
1092 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1093 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1094 &cancelip->i_din1->di_db[UFS_NDADDR], fs, 0, expungetype)))
1096 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1097 &cancelip->i_din1->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1101 len = numblks - UFS_NDADDR;
1103 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1104 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1105 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1106 blksperindir, fs, acctfunc, expungetype);
1109 blksperindir *= NINDIR(fs);
1110 lbn -= blksperindir + 1;
1111 len -= blksperindir;
1112 rlbn += blksperindir;
1118 * Descend an indirect block chain for vnode cancelvp accounting for all
1119 * its indirect blocks in snapvp.
1122 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1123 blksperindir, fs, acctfunc, expungetype)
1124 struct vnode *snapvp;
1125 struct vnode *cancelvp;
1131 ufs_lbn_t blksperindir;
1133 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1134 struct fs *, ufs_lbn_t, int);
1138 ufs_lbn_t subblksperindir;
1139 struct indir indirs[UFS_NIADDR + 2];
1140 ufs1_daddr_t last, *bap;
1144 if (expungetype == BLK_NOCOPY)
1146 panic("indiracct_ufs1: missing indir");
1148 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1150 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1151 panic("indiracct_ufs1: botched params");
1153 * We have to expand bread here since it will deadlock looking
1154 * up the block number for any blocks that are not in the cache.
1156 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1157 bp->b_blkno = fsbtodb(fs, blkno);
1158 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1159 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1164 * Account for the block pointers in this indirect block.
1166 last = howmany(remblks, blksperindir);
1167 if (last > NINDIR(fs))
1169 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1170 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1172 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1173 level == 0 ? rlbn : -1, expungetype);
1174 if (error || level == 0)
1177 * Account for the block pointers in each of the indirect blocks
1178 * in the levels below us.
1180 subblksperindir = blksperindir / NINDIR(fs);
1181 for (lbn++, level--, i = 0; i < last; i++) {
1182 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1183 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1186 rlbn += blksperindir;
1187 lbn -= blksperindir;
1188 remblks -= blksperindir;
1191 free(bap, M_DEVBUF);
1196 * Do both snap accounting and map accounting.
1199 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1201 ufs1_daddr_t *oldblkp, *lastblkp;
1204 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1208 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1210 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1214 * Identify a set of blocks allocated in a snapshot inode.
1217 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1219 ufs1_daddr_t *oldblkp, *lastblkp;
1222 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1224 struct inode *ip = VTOI(vp);
1225 ufs1_daddr_t blkno, *blkp;
1230 for ( ; oldblkp < lastblkp; oldblkp++) {
1232 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1234 lbn = fragstoblks(fs, blkno);
1235 if (lbn < UFS_NDADDR) {
1236 blkp = &ip->i_din1->di_db[lbn];
1237 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1239 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1240 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1243 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1244 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1247 * If we are expunging a snapshot vnode and we
1248 * find a block marked BLK_NOCOPY, then it is
1249 * one that has been allocated to this snapshot after
1250 * we took our current snapshot and can be ignored.
1252 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1253 if (lbn >= UFS_NDADDR)
1257 panic("snapacct_ufs1: bad block");
1258 *blkp = expungetype;
1259 if (lbn >= UFS_NDADDR)
1267 * Account for a set of blocks allocated in a snapshot inode.
1270 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1272 ufs1_daddr_t *oldblkp, *lastblkp;
1283 inum = ip->i_number;
1288 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1290 if (blkno == 0 || blkno == BLK_NOCOPY)
1292 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1293 *ip->i_snapblklist++ = lblkno;
1294 if (blkno == BLK_SNAP)
1295 blkno = blkstofrags(fs, lblkno);
1296 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1297 vp->v_type, NULL, SINGLETON_KEY);
1303 * Before expunging a snapshot inode, note all the
1304 * blocks that it claims with BLK_SNAP so that fsck will
1305 * be able to account for those blocks properly and so
1306 * that this snapshot knows that it need not copy them
1307 * if the other snapshot holding them is freed. This code
1308 * is reproduced once each for UFS1 and UFS2.
1311 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1312 struct vnode *snapvp;
1313 struct inode *cancelip;
1315 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1316 struct fs *, ufs_lbn_t, int);
1320 int i, error, indiroff;
1321 ufs_lbn_t lbn, rlbn;
1322 ufs2_daddr_t len, blkno, numblks, blksperindir;
1323 struct ufs2_dinode *dip;
1324 struct thread *td = curthread;
1328 * Prepare to expunge the inode. If its inode block has not
1329 * yet been copied, then allocate and fill the copy.
1331 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1333 if (lbn < UFS_NDADDR) {
1334 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1336 if (DOINGSOFTDEP(snapvp))
1337 softdep_prealloc(snapvp, MNT_WAIT);
1338 td->td_pflags |= TDP_COWINPROGRESS;
1339 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1340 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1341 td->td_pflags &= ~TDP_COWINPROGRESS;
1344 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1345 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1349 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1352 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1353 fs->fs_bsize, KERNCRED, 0, &bp);
1356 if ((error = readblock(snapvp, bp, lbn)) != 0)
1360 * Set a snapshot inode to be a zero length file, regular files
1361 * to be completely unallocated.
1363 dip = (struct ufs2_dinode *)bp->b_data +
1364 ino_to_fsbo(fs, cancelip->i_number);
1367 dip->di_flags &= ~SF_SNAPSHOT;
1368 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs2_daddr_t));
1369 if (clearmode || cancelip->i_effnlink == 0)
1372 ffs_update_dinode_ckhash(fs, dip);
1375 * Now go through and expunge all the blocks in the file
1376 * using the function requested.
1378 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1379 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1380 &cancelip->i_din2->di_db[UFS_NDADDR], fs, 0, expungetype)))
1382 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1383 &cancelip->i_din2->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1387 len = numblks - UFS_NDADDR;
1389 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1390 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1391 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1392 blksperindir, fs, acctfunc, expungetype);
1395 blksperindir *= NINDIR(fs);
1396 lbn -= blksperindir + 1;
1397 len -= blksperindir;
1398 rlbn += blksperindir;
1404 * Descend an indirect block chain for vnode cancelvp accounting for all
1405 * its indirect blocks in snapvp.
1408 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1409 blksperindir, fs, acctfunc, expungetype)
1410 struct vnode *snapvp;
1411 struct vnode *cancelvp;
1417 ufs_lbn_t blksperindir;
1419 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1420 struct fs *, ufs_lbn_t, int);
1424 ufs_lbn_t subblksperindir;
1425 struct indir indirs[UFS_NIADDR + 2];
1426 ufs2_daddr_t last, *bap;
1430 if (expungetype == BLK_NOCOPY)
1432 panic("indiracct_ufs2: missing indir");
1434 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1436 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1437 panic("indiracct_ufs2: botched params");
1439 * We have to expand bread here since it will deadlock looking
1440 * up the block number for any blocks that are not in the cache.
1442 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1443 bp->b_blkno = fsbtodb(fs, blkno);
1444 if ((bp->b_flags & B_CACHE) == 0 &&
1445 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1450 * Account for the block pointers in this indirect block.
1452 last = howmany(remblks, blksperindir);
1453 if (last > NINDIR(fs))
1455 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1456 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1458 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1459 level == 0 ? rlbn : -1, expungetype);
1460 if (error || level == 0)
1463 * Account for the block pointers in each of the indirect blocks
1464 * in the levels below us.
1466 subblksperindir = blksperindir / NINDIR(fs);
1467 for (lbn++, level--, i = 0; i < last; i++) {
1468 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1469 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1472 rlbn += blksperindir;
1473 lbn -= blksperindir;
1474 remblks -= blksperindir;
1477 free(bap, M_DEVBUF);
1482 * Do both snap accounting and map accounting.
1485 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1487 ufs2_daddr_t *oldblkp, *lastblkp;
1490 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1494 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1496 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1500 * Identify a set of blocks allocated in a snapshot inode.
1503 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1505 ufs2_daddr_t *oldblkp, *lastblkp;
1508 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1510 struct inode *ip = VTOI(vp);
1511 ufs2_daddr_t blkno, *blkp;
1516 for ( ; oldblkp < lastblkp; oldblkp++) {
1518 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1520 lbn = fragstoblks(fs, blkno);
1521 if (lbn < UFS_NDADDR) {
1522 blkp = &ip->i_din2->di_db[lbn];
1523 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1525 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1526 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1529 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1530 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1533 * If we are expunging a snapshot vnode and we
1534 * find a block marked BLK_NOCOPY, then it is
1535 * one that has been allocated to this snapshot after
1536 * we took our current snapshot and can be ignored.
1538 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1539 if (lbn >= UFS_NDADDR)
1543 panic("snapacct_ufs2: bad block");
1544 *blkp = expungetype;
1545 if (lbn >= UFS_NDADDR)
1553 * Account for a set of blocks allocated in a snapshot inode.
1556 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1558 ufs2_daddr_t *oldblkp, *lastblkp;
1569 inum = ip->i_number;
1574 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1576 if (blkno == 0 || blkno == BLK_NOCOPY)
1578 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP &&
1579 lblkno >= UFS_NDADDR)
1580 *ip->i_snapblklist++ = lblkno;
1581 if (blkno == BLK_SNAP)
1582 blkno = blkstofrags(fs, lblkno);
1583 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1584 vp->v_type, NULL, SINGLETON_KEY);
1590 * Decrement extra reference on snapshot when last name is removed.
1591 * It will not be freed until the last open reference goes away.
1600 struct snapdata *sn;
1601 struct ufsmount *ump;
1604 * Find snapshot in incore list.
1607 sn = ITODEVVP(ip)->v_rdev->si_snapdata;
1609 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1616 printf("ffs_snapgone: lost snapshot vnode %ju\n",
1617 (uintmax_t)ip->i_number);
1620 * Delete snapshot inode from superblock. Keep list dense.
1625 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1626 if (fs->fs_snapinum[snaploc] == ip->i_number)
1628 if (snaploc < FSMAXSNAP) {
1629 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1630 if (fs->fs_snapinum[snaploc] == 0)
1632 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1634 fs->fs_snapinum[snaploc - 1] = 0;
1640 * Prepare a snapshot file for being removed.
1647 struct vnode *devvp;
1650 ufs2_daddr_t numblks, blkno, dblk;
1651 int error, i, last, loc;
1652 struct snapdata *sn;
1656 devvp = ITODEVVP(ip);
1658 * If active, delete from incore list (this snapshot may
1659 * already have been in the process of being deleted, so
1660 * would not have been active).
1662 * Clear copy-on-write flag if last snapshot.
1665 if (ip->i_nextsnap.tqe_prev != 0) {
1666 sn = devvp->v_rdev->si_snapdata;
1667 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1668 ip->i_nextsnap.tqe_prev = 0;
1670 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1671 for (i = 0; i < sn->sn_lock.lk_recurse; i++)
1672 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1673 KASSERT(vp->v_vnlock == &sn->sn_lock,
1674 ("ffs_snapremove: lost lock mutation"));
1675 vp->v_vnlock = &vp->v_lock;
1677 while (sn->sn_lock.lk_recurse > 0)
1678 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1679 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1680 try_free_snapdata(devvp);
1684 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1685 * snapshots that want them (see ffs_snapblkfree below).
1687 for (blkno = 1; blkno < UFS_NDADDR; blkno++) {
1688 dblk = DIP(ip, i_db[blkno]);
1691 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1692 DIP_SET(ip, i_db[blkno], 0);
1693 else if ((dblk == blkstofrags(fs, blkno) &&
1694 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, fs->fs_bsize,
1695 ip->i_number, vp->v_type, NULL))) {
1696 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1697 btodb(fs->fs_bsize));
1698 DIP_SET(ip, i_db[blkno], 0);
1701 numblks = howmany(ip->i_size, fs->fs_bsize);
1702 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1703 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1704 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1707 if (fs->fs_size - blkno > NINDIR(fs))
1710 last = fs->fs_size - blkno;
1711 for (loc = 0; loc < last; loc++) {
1712 if (I_IS_UFS1(ip)) {
1713 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1716 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1717 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1718 else if ((dblk == blkstofrags(fs, blkno) &&
1719 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1720 fs->fs_bsize, ip->i_number, vp->v_type,
1722 ip->i_din1->di_blocks -=
1723 btodb(fs->fs_bsize);
1724 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1728 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1731 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1732 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1733 else if ((dblk == blkstofrags(fs, blkno) &&
1734 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1735 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1736 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1737 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1743 * Clear snapshot flag and drop reference.
1745 ip->i_flags &= ~SF_SNAPSHOT;
1746 DIP_SET(ip, i_flags, ip->i_flags);
1747 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1749 * The dirtied indirects must be written out before
1750 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1751 * may find indirect pointers using the magic BLK_* values.
1753 if (DOINGSOFTDEP(vp))
1754 ffs_syncvnode(vp, MNT_WAIT, 0);
1757 * Reenable disk quotas for ex-snapshot file.
1759 if (!getinoquota(ip))
1760 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1765 * Notification that a block is being freed. Return zero if the free
1766 * should be allowed to proceed. Return non-zero if the snapshot file
1767 * wants to claim the block. The block will be claimed if it is an
1768 * uncopied part of one of the snapshots. It will be freed if it is
1769 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1770 * If a fragment is being freed, then all snapshots that care about
1771 * it must make a copy since a snapshot file can only claim full sized
1772 * blocks. Note that if more than one snapshot file maps the block,
1773 * we can pick one at random to claim it. Since none of the snapshots
1774 * can change, we are assurred that they will all see the same unmodified
1775 * image. When deleting a snapshot file (see ffs_snapremove above), we
1776 * must push any of these claimed blocks to one of the other snapshots
1777 * that maps it. These claimed blocks are easily identified as they will
1778 * have a block number equal to their logical block number within the
1779 * snapshot. A copied block can never have this property because they
1780 * must always have been allocated from a BLK_NOCOPY location.
1783 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1785 struct vnode *devvp;
1790 struct workhead *wkhd;
1792 struct buf *ibp, *cbp, *savedcbp = NULL;
1793 struct thread *td = curthread;
1795 struct vnode *vp = NULL;
1798 int indiroff = 0, error = 0, claimedblk = 0;
1799 struct snapdata *sn;
1801 lbn = fragstoblks(fs, bno);
1804 sn = devvp->v_rdev->si_snapdata;
1809 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1810 VI_MTX(devvp)) != 0)
1812 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1814 if (DOINGSOFTDEP(vp))
1815 softdep_prealloc(vp, MNT_WAIT);
1817 * Lookup block being written.
1819 if (lbn < UFS_NDADDR) {
1820 blkno = DIP(ip, i_db[lbn]);
1822 td->td_pflags |= TDP_COWINPROGRESS;
1823 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1824 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1825 td->td_pflags &= ~TDP_COWINPROGRESS;
1828 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1830 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1832 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1835 * Check to see if block needs to be copied.
1839 * A block that we map is being freed. If it has not
1840 * been claimed yet, we will claim or copy it (below).
1843 } else if (blkno == BLK_SNAP) {
1845 * No previous snapshot claimed the block,
1846 * so it will be freed and become a BLK_NOCOPY
1847 * (don't care) for us.
1850 panic("snapblkfree: inconsistent block type");
1851 if (lbn < UFS_NDADDR) {
1852 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1853 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1854 } else if (I_IS_UFS1(ip)) {
1855 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1859 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1864 } else /* BLK_NOCOPY or default */ {
1866 * If the snapshot has already copied the block
1867 * (default), or does not care about the block,
1870 if (lbn >= UFS_NDADDR)
1875 * If this is a full size block, we will just grab it
1876 * and assign it to the snapshot inode. Otherwise we
1877 * will proceed to copy it. See explanation for this
1878 * routine as to why only a single snapshot needs to
1881 if (size == fs->fs_bsize) {
1884 printf("%s %ju lbn %jd from inum %ju\n",
1885 "Grabonremove: snapino",
1886 (uintmax_t)ip->i_number,
1887 (intmax_t)lbn, (uintmax_t)inum);
1890 * If journaling is tracking this write we must add
1891 * the work to the inode or indirect being written.
1894 if (lbn < UFS_NDADDR)
1895 softdep_inode_append(ip,
1896 curthread->td_ucred, wkhd);
1898 softdep_buf_append(ibp, wkhd);
1900 if (lbn < UFS_NDADDR) {
1901 DIP_SET(ip, i_db[lbn], bno);
1902 } else if (I_IS_UFS1(ip)) {
1903 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1906 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1909 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1910 UFS_INODE_SET_FLAG(ip, IN_CHANGE | IN_UPDATE);
1911 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1914 if (lbn >= UFS_NDADDR)
1917 * Allocate the block into which to do the copy. Note that this
1918 * allocation will never require any additional allocations for
1919 * the snapshot inode.
1921 td->td_pflags |= TDP_COWINPROGRESS;
1922 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1923 fs->fs_bsize, KERNCRED, 0, &cbp);
1924 td->td_pflags &= ~TDP_COWINPROGRESS;
1929 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n",
1930 "Copyonremove: snapino ", (uintmax_t)ip->i_number,
1931 (intmax_t)lbn, "for inum", (uintmax_t)inum, size,
1932 (intmax_t)cbp->b_blkno);
1935 * If we have already read the old block contents, then
1936 * simply copy them to the new block. Note that we need
1937 * to synchronously write snapshots that have not been
1938 * unlinked, and hence will be visible after a crash,
1939 * to ensure their integrity. At a minimum we ensure the
1940 * integrity of the filesystem metadata, but use the
1941 * dopersistence sysctl-setable flag to decide on the
1942 * persistence needed for file content data.
1944 if (savedcbp != NULL) {
1945 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1947 if ((vtype == VDIR || dopersistence) &&
1949 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1953 * Otherwise, read the old block contents into the buffer.
1955 if ((error = readblock(vp, cbp, lbn)) != 0) {
1956 bzero(cbp->b_data, fs->fs_bsize);
1958 if ((vtype == VDIR || dopersistence) &&
1960 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1966 * Note that we need to synchronously write snapshots that
1967 * have not been unlinked, and hence will be visible after
1968 * a crash, to ensure their integrity. At a minimum we
1969 * ensure the integrity of the filesystem metadata, but
1970 * use the dopersistence sysctl-setable flag to decide on
1971 * the persistence needed for file content data.
1974 vp = savedcbp->b_vp;
1976 if ((vtype == VDIR || dopersistence) &&
1977 VTOI(vp)->i_effnlink > 0)
1978 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1981 * If we have been unable to allocate a block in which to do
1982 * the copy, then return non-zero so that the fragment will
1983 * not be freed. Although space will be lost, the snapshot
1984 * will stay consistent.
1986 if (error != 0 && wkhd != NULL)
1987 softdep_freework(wkhd);
1988 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1993 * Associate snapshot files when mounting.
1996 ffs_snapshot_mount(mp)
1999 struct ufsmount *ump = VFSTOUFS(mp);
2000 struct vnode *devvp = ump->um_devvp;
2001 struct fs *fs = ump->um_fs;
2002 struct thread *td = curthread;
2003 struct snapdata *sn;
2005 struct vnode *lastvp;
2011 daddr_t snaplistsize;
2012 int error, snaploc, loc;
2015 * XXX The following needs to be set before ffs_truncate or
2016 * VOP_READ can be called.
2018 mp->mnt_stat.f_iosize = fs->fs_bsize;
2020 * Process each snapshot listed in the superblock.
2025 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
2026 if (fs->fs_snapinum[snaploc] == 0)
2028 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
2029 LK_EXCLUSIVE, &vp)) != 0){
2030 printf("ffs_snapshot_mount: vget failed %d\n", error);
2034 if (vp->v_type != VREG) {
2035 reason = "non-file snapshot";
2036 } else if (!IS_SNAPSHOT(ip)) {
2037 reason = "non-snapshot";
2038 } else if (ip->i_size ==
2039 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
2040 reason = "old format snapshot";
2041 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED);
2042 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
2046 if (reason != NULL) {
2047 printf("ffs_snapshot_mount: %s inode %d\n",
2048 reason, fs->fs_snapinum[snaploc]);
2051 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2052 if (fs->fs_snapinum[loc] == 0)
2054 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2056 fs->fs_snapinum[loc - 1] = 0;
2061 * Acquire a lock on the snapdata structure, creating it if
2064 sn = ffs_snapdata_acquire(devvp);
2066 * Change vnode to use shared snapshot lock instead of the
2067 * original private lock.
2069 vp->v_vnlock = &sn->sn_lock;
2070 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2072 * Link it onto the active snapshot list.
2075 if (ip->i_nextsnap.tqe_prev != 0)
2076 panic("ffs_snapshot_mount: %ju already on list",
2077 (uintmax_t)ip->i_number);
2079 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2080 vp->v_vflag |= VV_SYSTEM;
2087 * No usable snapshots found.
2089 if (sn == NULL || vp == NULL)
2092 * Allocate the space for the block hints list. We always want to
2093 * use the list from the newest snapshot.
2095 auio.uio_iov = &aiov;
2096 auio.uio_iovcnt = 1;
2097 aiov.iov_base = (void *)&snaplistsize;
2098 aiov.iov_len = sizeof(snaplistsize);
2099 auio.uio_resid = aiov.iov_len;
2101 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2102 auio.uio_segflg = UIO_SYSSPACE;
2103 auio.uio_rw = UIO_READ;
2105 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2106 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2107 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2111 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2112 M_UFSMNT, M_WAITOK);
2113 auio.uio_iovcnt = 1;
2114 aiov.iov_base = snapblklist;
2115 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2116 auio.uio_resid = aiov.iov_len;
2117 auio.uio_offset -= sizeof(snaplistsize);
2118 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2119 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2121 free(snapblklist, M_UFSMNT);
2126 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2127 sn->sn_listsize = snaplistsize;
2128 sn->sn_blklist = (daddr_t *)snapblklist;
2129 devvp->v_vflag |= VV_COPYONWRITE;
2134 * Disassociate snapshot files when unmounting.
2137 ffs_snapshot_unmount(mp)
2140 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2141 struct snapdata *sn;
2146 sn = devvp->v_rdev->si_snapdata;
2147 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2149 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2150 xp->i_nextsnap.tqe_prev = 0;
2151 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2154 * Avoid LOR with above snapshot lock. The LK_NOWAIT should
2155 * never fail as the lock is currently unused. Rather than
2156 * panic, we recover by doing the blocking lock.
2158 if (lockmgr(&vp->v_lock, LK_EXCLUSIVE | LK_NOWAIT, NULL) != 0) {
2159 printf("ffs_snapshot_unmount: Unexpected LK_NOWAIT "
2161 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2163 KASSERT(vp->v_vnlock == &sn->sn_lock,
2164 ("ffs_snapshot_unmount: lost lock mutation"));
2165 vp->v_vnlock = &vp->v_lock;
2166 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2167 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2168 if (xp->i_effnlink > 0)
2171 sn = devvp->v_rdev->si_snapdata;
2173 try_free_snapdata(devvp);
2174 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2178 * Check the buffer block to be belong to device buffer that shall be
2179 * locked after snaplk. devvp shall be locked on entry, and will be
2180 * leaved locked upon exit.
2183 ffs_bp_snapblk(devvp, bp)
2184 struct vnode *devvp;
2187 struct snapdata *sn;
2189 ufs2_daddr_t lbn, *snapblklist;
2190 int lower, upper, mid;
2192 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2193 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2194 sn = devvp->v_rdev->si_snapdata;
2195 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2197 fs = ITOFS(TAILQ_FIRST(&sn->sn_head));
2198 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2199 snapblklist = sn->sn_blklist;
2200 upper = sn->sn_listsize - 1;
2202 while (lower <= upper) {
2203 mid = (lower + upper) / 2;
2204 if (snapblklist[mid] == lbn)
2206 if (snapblklist[mid] < lbn)
2222 struct vnode *vp, *devvp;
2226 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2231 devvp = bo2vnode(bo);
2232 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2235 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2239 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2240 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2245 * Try to find a buffer to flush.
2247 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2248 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2250 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2253 panic("bdwrite: found ourselves");
2256 * Don't countdeps with the bo lock
2259 if (buf_countdeps(nbp, 0)) {
2266 if (!ffs_bp_snapblk(vp, nbp)) {
2274 if (nbp->b_flags & B_CLUSTEROK) {
2275 vfs_bio_awrite(nbp);
2280 dirtybufferflushes++;
2289 * Check for need to copy block that is about to be written,
2290 * copying the block if necessary.
2293 ffs_copyonwrite(devvp, bp)
2294 struct vnode *devvp;
2297 struct snapdata *sn;
2298 struct buf *ibp, *cbp, *savedcbp = NULL;
2299 struct thread *td = curthread;
2302 struct vnode *vp = NULL;
2303 ufs2_daddr_t lbn, blkno, *snapblklist;
2304 int lower, upper, mid, indiroff, error = 0;
2305 int launched_async_io, prev_norunningbuf;
2306 long saved_runningbufspace;
2308 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2309 return (0); /* Update on a snapshot file */
2310 if (td->td_pflags & TDP_COWINPROGRESS)
2311 panic("ffs_copyonwrite: recursive call");
2313 * First check to see if it is in the preallocated list.
2314 * By doing this check we avoid several potential deadlocks.
2317 sn = devvp->v_rdev->si_snapdata;
2319 TAILQ_EMPTY(&sn->sn_head)) {
2321 return (0); /* No snapshot */
2323 ip = TAILQ_FIRST(&sn->sn_head);
2325 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2326 if (lbn < UFS_NDADDR) {
2328 return (0); /* Direct blocks are always copied */
2330 snapblklist = sn->sn_blklist;
2331 upper = sn->sn_listsize - 1;
2333 while (lower <= upper) {
2334 mid = (lower + upper) / 2;
2335 if (snapblklist[mid] == lbn)
2337 if (snapblklist[mid] < lbn)
2342 if (lower <= upper) {
2346 launched_async_io = 0;
2347 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2349 * Since I/O on bp isn't yet in progress and it may be blocked
2350 * for a long time waiting on snaplk, back it out of
2351 * runningbufspace, possibly waking other threads waiting for space.
2353 saved_runningbufspace = bp->b_runningbufspace;
2354 if (saved_runningbufspace != 0)
2355 runningbufwakeup(bp);
2357 * Not in the precomputed list, so check the snapshots.
2359 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2360 VI_MTX(devvp)) != 0) {
2362 sn = devvp->v_rdev->si_snapdata;
2364 TAILQ_EMPTY(&sn->sn_head)) {
2366 if (saved_runningbufspace != 0) {
2367 bp->b_runningbufspace = saved_runningbufspace;
2368 atomic_add_long(&runningbufspace,
2369 bp->b_runningbufspace);
2371 return (0); /* Snapshot gone */
2374 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2376 if (DOINGSOFTDEP(vp))
2377 softdep_prealloc(vp, MNT_WAIT);
2379 * We ensure that everything of our own that needs to be
2380 * copied will be done at the time that ffs_snapshot is
2381 * called. Thus we can skip the check here which can
2382 * deadlock in doing the lookup in UFS_BALLOC.
2387 * Check to see if block needs to be copied. We do not have
2388 * to hold the snapshot lock while doing this lookup as it
2389 * will never require any additional allocations for the
2392 if (lbn < UFS_NDADDR) {
2393 blkno = DIP(ip, i_db[lbn]);
2395 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2396 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2397 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2398 td->td_pflags &= ~TDP_COWINPROGRESS;
2401 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
2403 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2405 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2409 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2410 panic("ffs_copyonwrite: bad copy block");
2415 * Allocate the block into which to do the copy. Since
2416 * multiple processes may all try to copy the same block,
2417 * we have to recheck our need to do a copy if we sleep
2418 * waiting for the lock.
2420 * Because all snapshots on a filesystem share a single
2421 * lock, we ensure that we will never be in competition
2422 * with another process to allocate a block.
2424 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2425 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2426 fs->fs_bsize, KERNCRED, 0, &cbp);
2427 td->td_pflags &= ~TDP_COWINPROGRESS;
2432 printf("Copyonwrite: snapino %ju lbn %jd for ",
2433 (uintmax_t)ip->i_number, (intmax_t)lbn);
2434 if (bp->b_vp == devvp)
2435 printf("fs metadata");
2438 (uintmax_t)VTOI(bp->b_vp)->i_number);
2439 printf(" lblkno %jd to blkno %jd\n",
2440 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2444 * If we have already read the old block contents, then
2445 * simply copy them to the new block. Note that we need
2446 * to synchronously write snapshots that have not been
2447 * unlinked, and hence will be visible after a crash,
2448 * to ensure their integrity. At a minimum we ensure the
2449 * integrity of the filesystem metadata, but use the
2450 * dopersistence sysctl-setable flag to decide on the
2451 * persistence needed for file content data.
2453 if (savedcbp != NULL) {
2454 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2456 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2457 dopersistence) && ip->i_effnlink > 0)
2458 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2460 launched_async_io = 1;
2464 * Otherwise, read the old block contents into the buffer.
2466 if ((error = readblock(vp, cbp, lbn)) != 0) {
2467 bzero(cbp->b_data, fs->fs_bsize);
2469 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2470 dopersistence) && ip->i_effnlink > 0)
2471 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2473 launched_async_io = 1;
2479 * Note that we need to synchronously write snapshots that
2480 * have not been unlinked, and hence will be visible after
2481 * a crash, to ensure their integrity. At a minimum we
2482 * ensure the integrity of the filesystem metadata, but
2483 * use the dopersistence sysctl-setable flag to decide on
2484 * the persistence needed for file content data.
2487 vp = savedcbp->b_vp;
2489 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2490 dopersistence) && VTOI(vp)->i_effnlink > 0)
2491 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2493 launched_async_io = 1;
2495 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2496 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2498 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2499 waitrunningbufspace();
2501 * I/O on bp will now be started, so count it in runningbufspace.
2503 if (saved_runningbufspace != 0) {
2504 bp->b_runningbufspace = saved_runningbufspace;
2505 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2511 * sync snapshots to force freework records waiting on snapshots to claim
2515 ffs_sync_snap(mp, waitfor)
2519 struct snapdata *sn;
2520 struct vnode *devvp;
2524 devvp = VFSTOUFS(mp)->um_devvp;
2525 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2529 sn = devvp->v_rdev->si_snapdata;
2534 if (lockmgr(&sn->sn_lock,
2535 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2536 VI_MTX(devvp)) == 0)
2539 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2541 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2543 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2547 * Read the specified block into the given buffer.
2548 * Much of this boiler-plate comes from bwrite().
2551 readblock(vp, bp, lbn)
2563 bip = g_alloc_bio();
2564 bip->bio_cmd = BIO_READ;
2565 bip->bio_offset = dbtob(fsbtodb(fs, blkstofrags(fs, lbn)));
2566 bip->bio_data = bp->b_data;
2567 bip->bio_length = bp->b_bcount;
2568 bip->bio_done = NULL;
2570 g_io_request(bip, ITODEVVP(ip)->v_bufobj.bo_private);
2571 bp->b_error = biowait(bip, "snaprdb");
2573 return (bp->b_error);
2579 * Process file deletes that were deferred by ufs_inactive() due to
2580 * the file system being suspended. Transfer IN_LAZYACCESS into
2581 * IN_MODIFIED for vnodes that were accessed during suspension.
2584 process_deferred_inactive(struct mount *mp)
2586 struct vnode *vp, *mvp;
2590 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2592 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2594 * IN_LAZYACCESS is checked here without holding any
2595 * vnode lock, but this flag is set only while holding
2598 if (vp->v_type == VNON ||
2599 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2600 ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
2606 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2609 if (error == ENOENT)
2610 continue; /* vnode recycled */
2611 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2615 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2616 ip->i_flag &= ~IN_LAZYACCESS;
2617 UFS_INODE_SET_FLAG(ip, IN_MODIFIED);
2620 error = vinactive(vp);
2621 if (error == ERELOOKUP && vp->v_usecount == 0) {
2630 vn_finished_secondary_write(mp);
2633 #ifndef NO_FFS_SNAPSHOT
2635 static struct snapdata *
2636 ffs_snapdata_alloc(void)
2638 struct snapdata *sn;
2641 * Fetch a snapdata from the free list if there is one available.
2643 mtx_lock(&snapfree_lock);
2644 sn = LIST_FIRST(&snapfree);
2646 LIST_REMOVE(sn, sn_link);
2647 mtx_unlock(&snapfree_lock);
2651 * If there were no free snapdatas allocate one.
2653 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2654 TAILQ_INIT(&sn->sn_head);
2655 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2656 LK_CANRECURSE | LK_NOSHARE);
2661 * The snapdata is never freed because we can not be certain that
2662 * there are no threads sleeping on the snap lock. Persisting
2663 * them permanently avoids costly synchronization in ffs_lock().
2666 ffs_snapdata_free(struct snapdata *sn)
2668 mtx_lock(&snapfree_lock);
2669 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2670 mtx_unlock(&snapfree_lock);
2673 /* Try to free snapdata associated with devvp */
2675 try_free_snapdata(struct vnode *devvp)
2677 struct snapdata *sn;
2678 ufs2_daddr_t *snapblklist;
2680 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2681 sn = devvp->v_rdev->si_snapdata;
2683 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2684 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2689 devvp->v_rdev->si_snapdata = NULL;
2690 devvp->v_vflag &= ~VV_COPYONWRITE;
2691 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2692 snapblklist = sn->sn_blklist;
2693 sn->sn_blklist = NULL;
2694 sn->sn_listsize = 0;
2695 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2696 if (snapblklist != NULL)
2697 free(snapblklist, M_UFSMNT);
2698 ffs_snapdata_free(sn);
2701 static struct snapdata *
2702 ffs_snapdata_acquire(struct vnode *devvp)
2704 struct snapdata *nsn, *sn;
2708 * Allocate a free snapdata. This is done before acquiring the
2709 * devvp lock to avoid allocation while the devvp interlock is
2712 nsn = ffs_snapdata_alloc();
2716 sn = devvp->v_rdev->si_snapdata;
2719 * This is the first snapshot on this
2720 * filesystem and we use our pre-allocated
2721 * snapdata. Publish sn with the sn_lock
2722 * owned by us, to avoid the race.
2724 error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE |
2727 panic("leaked sn, lockmgr error %d", error);
2728 sn = devvp->v_rdev->si_snapdata = nsn;
2735 * There is a snapshots which already exists on this
2736 * filesystem, grab a reference to the common lock.
2738 error = lockmgr(&sn->sn_lock, LK_INTERLOCK |
2739 LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp));
2745 * Free any unused snapdata.
2748 ffs_snapdata_free(nsn);