2 * Copyright 2000 Marshall Kirk McKusick. All Rights Reserved.
4 * Further information about snapshots can be obtained from:
6 * Marshall Kirk McKusick http://www.mckusick.com/softdep/
7 * 1614 Oxford Street mckusick@mckusick.com
8 * Berkeley, CA 94709-1608 +1-510-843-9542
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
21 * THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)ffs_snapshot.c 8.11 (McKusick) 7/23/00
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "opt_quota.h"
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
48 #include <sys/namei.h>
49 #include <sys/sched.h>
51 #include <sys/malloc.h>
52 #include <sys/mount.h>
53 #include <sys/resource.h>
54 #include <sys/resourcevar.h>
55 #include <sys/vnode.h>
57 #include <geom/geom.h>
59 #include <ufs/ufs/extattr.h>
60 #include <ufs/ufs/quota.h>
61 #include <ufs/ufs/ufsmount.h>
62 #include <ufs/ufs/inode.h>
63 #include <ufs/ufs/ufs_extern.h>
65 #include <ufs/ffs/fs.h>
66 #include <ufs/ffs/ffs_extern.h>
68 #define KERNCRED thread0.td_ucred
73 #ifdef NO_FFS_SNAPSHOT
75 ffs_snapshot(mp, snapfile)
83 ffs_snapblkfree(fs, devvp, bno, size, inum)
100 ffs_snapshot_mount(mp)
106 ffs_snapshot_unmount(mp)
118 ffs_copyonwrite(devvp, bp)
127 TAILQ_HEAD(snaphead, inode);
130 struct snaphead sn_head;
136 static int cgaccount(int, struct vnode *, struct buf *, int);
137 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
138 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
139 ufs_lbn_t, int), int);
140 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
141 ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
142 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
143 ufs_lbn_t, int), int);
144 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
145 struct fs *, ufs_lbn_t, int);
146 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
147 struct fs *, ufs_lbn_t, int);
148 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
149 struct fs *, ufs_lbn_t, int);
150 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
151 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
152 ufs_lbn_t, int), int);
153 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
154 ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
155 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
156 ufs_lbn_t, int), int);
157 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
158 struct fs *, ufs_lbn_t, int);
159 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
160 struct fs *, ufs_lbn_t, int);
161 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
162 struct fs *, ufs_lbn_t, int);
163 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
164 static void process_deferred_inactive(struct mount *);
165 static void try_free_snapdata(struct vnode *devvp, struct thread *td);
166 static int ffs_bp_snapblk(struct vnode *, struct buf *);
169 * To ensure the consistency of snapshots across crashes, we must
170 * synchronously write out copied blocks before allowing the
171 * originals to be modified. Because of the rather severe speed
172 * penalty that this imposes, the following flag allows this
173 * crash persistence to be disabled.
175 int dopersistence = 0;
178 #include <sys/sysctl.h>
179 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
180 static int snapdebug = 0;
181 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
182 int collectsnapstats = 0;
183 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
188 * Create a snapshot file and initialize it for the filesystem.
191 ffs_snapshot(mp, snapfile)
195 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
196 int error, cg, snaploc;
197 int i, size, len, loc;
199 struct timespec starttime = {0, 0}, endtime;
201 long redo = 0, snaplistsize = 0;
204 struct fs *copy_fs = NULL, *fs;
205 struct thread *td = curthread;
206 struct inode *ip, *xp;
207 struct buf *bp, *nbp, *ibp, *sbp = NULL;
211 struct vnode *vp, *xvp, *mvp, *devvp;
215 struct ufsmount *ump;
225 * Need to serialize access to snapshot code per filesystem.
228 * Assign a snapshot slot in the superblock.
231 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
232 if (fs->fs_snapinum[snaploc] == 0)
235 if (snaploc == FSMAXSNAP)
238 * Create the snapshot file.
241 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE, snapfile, td);
242 if ((error = namei(&nd)) != 0)
244 if (nd.ni_vp != NULL) {
248 if (nd.ni_dvp->v_mount != mp)
251 NDFREE(&nd, NDF_ONLY_PNBUF);
252 if (nd.ni_dvp == nd.ni_vp)
260 vat.va_mode = S_IRUSR;
261 vat.va_vaflags |= VA_EXCLUSIVE;
262 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
265 panic("ffs_snapshot: mount mismatch");
267 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
268 NDFREE(&nd, NDF_ONLY_PNBUF);
270 if ((error = vn_start_write(NULL, &wrtmp,
271 V_XSLEEP | PCATCH)) != 0)
275 VOP_LEASE(nd.ni_dvp, td, KERNCRED, LEASE_WRITE);
276 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
277 VOP_UNLOCK(nd.ni_dvp, 0);
279 NDFREE(&nd, NDF_ONLY_PNBUF);
280 vn_finished_write(wrtmp);
285 vp->v_vflag |= VV_SYSTEM;
289 * Allocate and copy the last block contents so as to be able
290 * to set size to that of the filesystem.
292 numblks = howmany(fs->fs_size, fs->fs_frag);
293 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
294 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
297 ip->i_size = lblktosize(fs, (off_t)numblks);
298 DIP_SET(ip, i_size, ip->i_size);
299 ip->i_flag |= IN_CHANGE | IN_UPDATE;
300 error = readblock(vp, bp, numblks - 1);
305 * Preallocate critical data structures so that we can copy
306 * them in without further allocation after we suspend all
307 * operations on the filesystem. We would like to just release
308 * the allocated buffers without writing them since they will
309 * be filled in below once we are ready to go, but this upsets
310 * the soft update code, so we go ahead and write the new buffers.
312 * Allocate all indirect blocks and mark all of them as not
313 * needing to be copied.
315 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
316 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
317 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
323 * Allocate copies for the superblock and its summary information.
325 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
330 blkno = fragstoblks(fs, fs->fs_csaddr);
331 len = howmany(fs->fs_cssize, fs->fs_bsize);
332 for (loc = 0; loc < len; loc++) {
333 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
334 fs->fs_bsize, KERNCRED, 0, &nbp);
340 * Allocate all cylinder group blocks.
342 for (cg = 0; cg < fs->fs_ncg; cg++) {
343 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
344 fs->fs_bsize, KERNCRED, 0, &nbp);
350 * Copy all the cylinder group maps. Although the
351 * filesystem is still active, we hope that only a few
352 * cylinder groups will change between now and when we
353 * suspend operations. Thus, we will be able to quickly
354 * touch up the few cylinder groups that changed during
355 * the suspension period.
357 len = howmany(fs->fs_ncg, NBBY);
358 MALLOC(space, void *, len, M_DEVBUF, M_WAITOK|M_ZERO);
360 fs->fs_active = space;
362 for (cg = 0; cg < fs->fs_ncg; cg++) {
363 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
364 fs->fs_bsize, KERNCRED, 0, &nbp);
367 error = cgaccount(cg, vp, nbp, 1);
373 * Change inode to snapshot type file.
375 ip->i_flags |= SF_SNAPSHOT;
376 DIP_SET(ip, i_flags, ip->i_flags);
377 ip->i_flag |= IN_CHANGE | IN_UPDATE;
379 * Ensure that the snapshot is completely on disk.
380 * Since we have marked it as a snapshot it is safe to
381 * unlock it as no process will be allowed to write to it.
383 if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
387 * All allocations are done, so we can now snapshot the system.
389 * Recind nice scheduling while running with the filesystem suspended.
391 if (td->td_proc->p_nice > 0) {
397 saved_nice = p->p_nice;
403 * Suspend operation on filesystem.
406 vn_finished_write(wrtmp);
407 if ((error = vfs_write_suspend(vp->v_mount)) != 0) {
408 vn_start_write(NULL, &wrtmp, V_WAIT);
409 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
412 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
414 vn_start_write(NULL, &wrtmp, V_WAIT);
416 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
417 if (ip->i_effnlink == 0) {
418 error = ENOENT; /* Snapshot file unlinked */
421 if (collectsnapstats)
422 nanotime(&starttime);
424 /* The last block might have changed. Copy it again to be sure. */
425 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
426 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
429 error = readblock(vp, bp, numblks - 1);
430 bp->b_flags |= B_VALIDSUSPWRT;
435 * First, copy all the cylinder group maps that have changed.
437 for (cg = 0; cg < fs->fs_ncg; cg++) {
438 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
441 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
442 fs->fs_bsize, KERNCRED, 0, &nbp);
445 error = cgaccount(cg, vp, nbp, 2);
451 * Grab a copy of the superblock and its summary information.
452 * We delay writing it until the suspension is released below.
454 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
461 loc = blkoff(fs, fs->fs_sblockloc);
462 copy_fs = (struct fs *)(sbp->b_data + loc);
463 bcopy(fs, copy_fs, fs->fs_sbsize);
464 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
465 copy_fs->fs_clean = 1;
466 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
467 if (fs->fs_sbsize < size)
468 bzero(&sbp->b_data[loc + fs->fs_sbsize], size - fs->fs_sbsize);
469 size = blkroundup(fs, fs->fs_cssize);
470 if (fs->fs_contigsumsize > 0)
471 size += fs->fs_ncg * sizeof(int32_t);
472 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
473 copy_fs->fs_csp = space;
474 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
475 space = (char *)space + fs->fs_cssize;
476 loc = howmany(fs->fs_cssize, fs->fs_fsize);
477 i = fs->fs_frag - loc % fs->fs_frag;
478 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
480 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
481 len, KERNCRED, &bp)) != 0) {
483 free(copy_fs->fs_csp, M_UFSMNT);
488 bcopy(bp->b_data, space, (u_int)len);
489 space = (char *)space + len;
490 bp->b_flags |= B_INVAL | B_NOCACHE;
493 if (fs->fs_contigsumsize > 0) {
494 copy_fs->fs_maxcluster = lp = space;
495 for (i = 0; i < fs->fs_ncg; i++)
496 *lp++ = fs->fs_contigsumsize;
499 * We must check for active files that have been unlinked
500 * (e.g., with a zero link count). We have to expunge all
501 * trace of these files from the snapshot so that they are
502 * not reclaimed prematurely by fsck or unnecessarily dumped.
503 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
504 * spec_strategy about writing on a suspended filesystem.
505 * Note that we skip unlinked snapshot files as they will
506 * be handled separately below.
508 * We also calculate the needed size for the snapshot list.
510 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
511 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
513 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
515 MNT_VNODE_FOREACH(xvp, mp, mvp) {
518 if ((xvp->v_iflag & VI_DOOMED) ||
519 (xvp->v_usecount == 0 &&
520 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
521 xvp->v_type == VNON ||
522 (VTOI(xvp)->i_flags & SF_SNAPSHOT)) {
528 * We can skip parent directory vnode because it must have
529 * this snapshot file in it.
531 if (xvp == nd.ni_dvp) {
537 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
539 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
544 if (xvp->v_usecount == 0 &&
545 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
554 vprint("ffs_snapshot: busy vnode", xvp);
555 if (VOP_GETATTR(xvp, &vat, td->td_ucred, td) == 0 &&
563 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
570 * If there is a fragment, clear it here.
573 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
575 len = fragroundup(fs, blkoff(fs, xp->i_size));
576 if (len != 0 && len < fs->fs_bsize) {
577 ffs_blkfree(ump, copy_fs, vp,
578 DIP(xp, i_db[loc]), len, xp->i_number);
579 blkno = DIP(xp, i_db[loc]);
580 DIP_SET(xp, i_db[loc], 0);
584 if (xp->i_ump->um_fstype == UFS1)
585 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
588 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
591 DIP_SET(xp, i_db[loc], blkno);
593 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
598 free(copy_fs->fs_csp, M_UFSMNT);
601 MNT_VNODE_FOREACH_ABORT(mp, mvp);
608 * If there already exist snapshots on this filesystem, grab a
609 * reference to their shared lock. If this is the first snapshot
610 * on this filesystem, we need to allocate a lock for the snapshots
611 * to share. In either case, acquire the snapshot lock and give
612 * up our original private lock.
615 sn = devvp->v_rdev->si_snapdata;
617 xp = TAILQ_FIRST(&sn->sn_head);
620 vp->v_vnlock = &sn->sn_lock;
623 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
624 TAILQ_INIT(&sn->sn_head);
625 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
626 LK_CANRECURSE | LK_NOSHARE);
628 vp->v_vnlock = &sn->sn_lock;
629 mp_fixme("si_snapdata setting is racey.");
630 devvp->v_rdev->si_snapdata = sn;
633 lockmgr(vp->v_vnlock, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY,
635 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
637 * If this is the first snapshot on this filesystem, then we need
638 * to allocate the space for the list of preallocated snapshot blocks.
639 * This list will be refined below, but this preliminary one will
640 * keep us out of deadlock until the full one is ready.
643 MALLOC(snapblklist, daddr_t *, snaplistsize * sizeof(daddr_t),
645 blkp = &snapblklist[1];
646 *blkp++ = lblkno(fs, fs->fs_sblockloc);
647 blkno = fragstoblks(fs, fs->fs_csaddr);
648 for (cg = 0; cg < fs->fs_ncg; cg++) {
649 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
651 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
653 len = howmany(fs->fs_cssize, fs->fs_bsize);
654 for (loc = 0; loc < len; loc++)
655 *blkp++ = blkno + loc;
656 for (; cg < fs->fs_ncg; cg++)
657 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
658 snapblklist[0] = blkp - snapblklist;
660 if (sn->sn_blklist != NULL)
661 panic("ffs_snapshot: non-empty list");
662 sn->sn_blklist = snapblklist;
663 sn->sn_listsize = blkp - snapblklist;
667 * Record snapshot inode. Since this is the newest snapshot,
668 * it must be placed at the end of the list.
671 fs->fs_snapinum[snaploc] = ip->i_number;
672 if (ip->i_nextsnap.tqe_prev != 0)
673 panic("ffs_snapshot: %d already on list", ip->i_number);
674 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
675 devvp->v_vflag |= VV_COPYONWRITE;
677 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
679 KASSERT((sn != NULL && sbp != NULL && error == 0) ||
680 (sn == NULL && sbp == NULL && error != 0),
681 ("email phk@ and mckusick@"));
683 * Resume operation on filesystem.
685 vfs_write_resume(vp->v_mount);
686 vn_start_write(NULL, &wrtmp, V_WAIT);
687 if (collectsnapstats && starttime.tv_sec > 0) {
689 timespecsub(&endtime, &starttime);
690 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
691 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
692 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
697 * Copy allocation information from all the snapshots in
698 * this snapshot and then expunge them from its view.
700 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
703 if (xp->i_ump->um_fstype == UFS1)
704 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
707 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
709 if (error == 0 && xp->i_effnlink == 0) {
710 error = ffs_freefile(ump,
717 fs->fs_snapinum[snaploc] = 0;
722 * Allocate space for the full list of preallocated snapshot blocks.
724 MALLOC(snapblklist, daddr_t *, snaplistsize * sizeof(daddr_t),
726 ip->i_snapblklist = &snapblklist[1];
728 * Expunge the blocks used by the snapshots from the set of
729 * blocks marked as used in the snapshot bitmaps. Also, collect
730 * the list of allocated blocks in i_snapblklist.
732 if (ip->i_ump->um_fstype == UFS1)
733 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, BLK_SNAP);
735 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, BLK_SNAP);
737 fs->fs_snapinum[snaploc] = 0;
738 FREE(snapblklist, M_UFSMNT);
741 if (snaplistsize < ip->i_snapblklist - snapblklist)
742 panic("ffs_snapshot: list too small");
743 snaplistsize = ip->i_snapblklist - snapblklist;
744 snapblklist[0] = snaplistsize;
745 ip->i_snapblklist = 0;
747 * Write out the list of allocated blocks to the end of the snapshot.
749 auio.uio_iov = &aiov;
751 aiov.iov_base = (void *)snapblklist;
752 aiov.iov_len = snaplistsize * sizeof(daddr_t);
753 auio.uio_resid = aiov.iov_len;;
754 auio.uio_offset = ip->i_size;
755 auio.uio_segflg = UIO_SYSSPACE;
756 auio.uio_rw = UIO_WRITE;
758 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
759 fs->fs_snapinum[snaploc] = 0;
760 FREE(snapblklist, M_UFSMNT);
764 * Write the superblock and its summary information
767 blkno = fragstoblks(fs, fs->fs_csaddr);
768 len = howmany(fs->fs_cssize, fs->fs_bsize);
769 space = copy_fs->fs_csp;
770 for (loc = 0; loc < len; loc++) {
771 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
774 fs->fs_snapinum[snaploc] = 0;
775 FREE(snapblklist, M_UFSMNT);
778 bcopy(space, nbp->b_data, fs->fs_bsize);
779 space = (char *)space + fs->fs_bsize;
783 * As this is the newest list, it is the most inclusive, so
784 * should replace the previous list.
787 space = sn->sn_blklist;
788 sn->sn_blklist = snapblklist;
789 sn->sn_listsize = snaplistsize;
792 FREE(space, M_UFSMNT);
794 * If another process is currently writing the buffer containing
795 * the inode for this snapshot then a deadlock can occur. Drop
796 * the snapshot lock until the buffer has been written.
798 VREF(vp); /* Protect against ffs_snapgone() */
800 (void) bread(ip->i_devvp,
801 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
802 (int) fs->fs_bsize, NOCRED, &nbp);
804 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
805 if (ip->i_effnlink == 0)
806 error = ENOENT; /* Snapshot file unlinked */
808 vrele(vp); /* Drop extra reference */
810 FREE(copy_fs->fs_csp, M_UFSMNT);
813 NDFREE(&nd, NDF_ONLY_PNBUF);
814 if (saved_nice > 0) {
820 sched_nice(td->td_proc, saved_nice);
822 PROC_UNLOCK(td->td_proc);
825 if (fs->fs_active != 0) {
826 FREE(fs->fs_active, M_DEVBUF);
831 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
834 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
835 (void) ffs_syncvnode(vp, MNT_WAIT);
841 vn_finished_write(wrtmp);
842 process_deferred_inactive(mp);
847 * Copy a cylinder group map. All the unallocated blocks are marked
848 * BLK_NOCOPY so that the snapshot knows that it need not copy them
849 * if they are later written. If passno is one, then this is a first
850 * pass, so only setting needs to be done. If passno is 2, then this
851 * is a revision to a previous pass which must be undone as the
852 * replacement pass is done.
855 cgaccount(cg, vp, nbp, passno)
861 struct buf *bp, *ibp;
865 ufs2_daddr_t base, numblks;
866 int error, len, loc, indiroff;
870 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
871 (int)fs->fs_cgsize, KERNCRED, &bp);
876 cgp = (struct cg *)bp->b_data;
877 if (!cg_chkmagic(cgp)) {
883 UFS_UNLOCK(ip->i_ump);
884 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
885 if (fs->fs_cgsize < fs->fs_bsize)
886 bzero(&nbp->b_data[fs->fs_cgsize],
887 fs->fs_bsize - fs->fs_cgsize);
888 cgp = (struct cg *)nbp->b_data;
891 nbp->b_flags |= B_VALIDSUSPWRT;
892 numblks = howmany(fs->fs_size, fs->fs_frag);
893 len = howmany(fs->fs_fpg, fs->fs_frag);
894 base = cgbase(fs, cg) / fs->fs_frag;
895 if (base + len >= numblks)
896 len = numblks - base - 1;
899 for ( ; loc < NDADDR; loc++) {
900 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
901 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
902 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
903 DIP_SET(ip, i_db[loc], 0);
904 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
905 panic("ffs_snapshot: lost direct block");
908 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
909 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
913 indiroff = (base + loc - NDADDR) % NINDIR(fs);
914 for ( ; loc < len; loc++, indiroff++) {
915 if (indiroff >= NINDIR(fs)) {
917 ibp->b_flags |= B_VALIDSUSPWRT;
919 error = UFS_BALLOC(vp,
920 lblktosize(fs, (off_t)(base + loc)),
921 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
927 if (ip->i_ump->um_fstype == UFS1) {
928 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
929 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
931 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
932 [indiroff] == BLK_NOCOPY)
933 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
934 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
935 [indiroff] == BLK_NOCOPY)
936 panic("ffs_snapshot: lost indirect block");
939 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
940 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
941 else if (passno == 2 &&
942 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
943 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
944 else if (passno == 1 &&
945 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
946 panic("ffs_snapshot: lost indirect block");
949 ibp->b_flags |= B_VALIDSUSPWRT;
955 * Before expunging a snapshot inode, note all the
956 * blocks that it claims with BLK_SNAP so that fsck will
957 * be able to account for those blocks properly and so
958 * that this snapshot knows that it need not copy them
959 * if the other snapshot holding them is freed. This code
960 * is reproduced once each for UFS1 and UFS2.
963 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype)
964 struct vnode *snapvp;
965 struct inode *cancelip;
967 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
968 struct fs *, ufs_lbn_t, int);
971 int i, error, indiroff;
973 ufs2_daddr_t len, blkno, numblks, blksperindir;
974 struct ufs1_dinode *dip;
975 struct thread *td = curthread;
979 * Prepare to expunge the inode. If its inode block has not
980 * yet been copied, then allocate and fill the copy.
982 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
985 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
987 td->td_pflags |= TDP_COWINPROGRESS;
988 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
989 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
990 td->td_pflags &= ~TDP_COWINPROGRESS;
993 indiroff = (lbn - NDADDR) % NINDIR(fs);
994 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
998 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1001 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1002 fs->fs_bsize, KERNCRED, 0, &bp);
1005 if ((error = readblock(snapvp, bp, lbn)) != 0)
1009 * Set a snapshot inode to be a zero length file, regular files
1010 * or unlinked snapshots to be completely unallocated.
1012 dip = (struct ufs1_dinode *)bp->b_data +
1013 ino_to_fsbo(fs, cancelip->i_number);
1014 if (expungetype == BLK_NOCOPY || cancelip->i_effnlink == 0)
1018 dip->di_flags &= ~SF_SNAPSHOT;
1019 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1022 * Now go through and expunge all the blocks in the file
1023 * using the function requested.
1025 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1026 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1027 &cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
1029 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1030 &cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
1034 len = numblks - NDADDR;
1036 for (i = 0; len > 0 && i < NIADDR; i++) {
1037 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1038 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1039 blksperindir, fs, acctfunc, expungetype);
1042 blksperindir *= NINDIR(fs);
1043 lbn -= blksperindir + 1;
1044 len -= blksperindir;
1045 rlbn += blksperindir;
1051 * Descend an indirect block chain for vnode cancelvp accounting for all
1052 * its indirect blocks in snapvp.
1055 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1056 blksperindir, fs, acctfunc, expungetype)
1057 struct vnode *snapvp;
1058 struct vnode *cancelvp;
1064 ufs_lbn_t blksperindir;
1066 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1067 struct fs *, ufs_lbn_t, int);
1071 ufs_lbn_t subblksperindir;
1072 struct indir indirs[NIADDR + 2];
1073 ufs1_daddr_t last, *bap;
1077 if (expungetype == BLK_NOCOPY)
1079 panic("indiracct_ufs1: missing indir");
1081 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1083 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1084 panic("indiracct_ufs1: botched params");
1086 * We have to expand bread here since it will deadlock looking
1087 * up the block number for any blocks that are not in the cache.
1089 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1090 bp->b_blkno = fsbtodb(fs, blkno);
1091 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1092 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1097 * Account for the block pointers in this indirect block.
1099 last = howmany(remblks, blksperindir);
1100 if (last > NINDIR(fs))
1102 MALLOC(bap, ufs1_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
1103 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1105 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1106 level == 0 ? rlbn : -1, expungetype);
1107 if (error || level == 0)
1110 * Account for the block pointers in each of the indirect blocks
1111 * in the levels below us.
1113 subblksperindir = blksperindir / NINDIR(fs);
1114 for (lbn++, level--, i = 0; i < last; i++) {
1115 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1116 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1119 rlbn += blksperindir;
1120 lbn -= blksperindir;
1121 remblks -= blksperindir;
1124 FREE(bap, M_DEVBUF);
1129 * Do both snap accounting and map accounting.
1132 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1134 ufs1_daddr_t *oldblkp, *lastblkp;
1137 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1141 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1143 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1147 * Identify a set of blocks allocated in a snapshot inode.
1150 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1152 ufs1_daddr_t *oldblkp, *lastblkp;
1155 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1157 struct inode *ip = VTOI(vp);
1158 ufs1_daddr_t blkno, *blkp;
1163 for ( ; oldblkp < lastblkp; oldblkp++) {
1165 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1167 lbn = fragstoblks(fs, blkno);
1169 blkp = &ip->i_din1->di_db[lbn];
1170 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1172 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1173 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1176 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1177 [(lbn - NDADDR) % NINDIR(fs)];
1180 * If we are expunging a snapshot vnode and we
1181 * find a block marked BLK_NOCOPY, then it is
1182 * one that has been allocated to this snapshot after
1183 * we took our current snapshot and can be ignored.
1185 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1190 panic("snapacct_ufs1: bad block");
1191 *blkp = expungetype;
1200 * Account for a set of blocks allocated in a snapshot inode.
1203 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1205 ufs1_daddr_t *oldblkp, *lastblkp;
1216 inum = ip->i_number;
1221 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1223 if (blkno == 0 || blkno == BLK_NOCOPY)
1225 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1226 *ip->i_snapblklist++ = lblkno;
1227 if (blkno == BLK_SNAP)
1228 blkno = blkstofrags(fs, lblkno);
1229 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1235 * Before expunging a snapshot inode, note all the
1236 * blocks that it claims with BLK_SNAP so that fsck will
1237 * be able to account for those blocks properly and so
1238 * that this snapshot knows that it need not copy them
1239 * if the other snapshot holding them is freed. This code
1240 * is reproduced once each for UFS1 and UFS2.
1243 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype)
1244 struct vnode *snapvp;
1245 struct inode *cancelip;
1247 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1248 struct fs *, ufs_lbn_t, int);
1251 int i, error, indiroff;
1252 ufs_lbn_t lbn, rlbn;
1253 ufs2_daddr_t len, blkno, numblks, blksperindir;
1254 struct ufs2_dinode *dip;
1255 struct thread *td = curthread;
1259 * Prepare to expunge the inode. If its inode block has not
1260 * yet been copied, then allocate and fill the copy.
1262 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1265 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1267 td->td_pflags |= TDP_COWINPROGRESS;
1268 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1269 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1270 td->td_pflags &= ~TDP_COWINPROGRESS;
1273 indiroff = (lbn - NDADDR) % NINDIR(fs);
1274 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1278 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1281 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1282 fs->fs_bsize, KERNCRED, 0, &bp);
1285 if ((error = readblock(snapvp, bp, lbn)) != 0)
1289 * Set a snapshot inode to be a zero length file, regular files
1290 * to be completely unallocated.
1292 dip = (struct ufs2_dinode *)bp->b_data +
1293 ino_to_fsbo(fs, cancelip->i_number);
1294 if (expungetype == BLK_NOCOPY)
1298 dip->di_flags &= ~SF_SNAPSHOT;
1299 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1302 * Now go through and expunge all the blocks in the file
1303 * using the function requested.
1305 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1306 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1307 &cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
1309 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1310 &cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
1314 len = numblks - NDADDR;
1316 for (i = 0; len > 0 && i < NIADDR; i++) {
1317 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1318 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1319 blksperindir, fs, acctfunc, expungetype);
1322 blksperindir *= NINDIR(fs);
1323 lbn -= blksperindir + 1;
1324 len -= blksperindir;
1325 rlbn += blksperindir;
1331 * Descend an indirect block chain for vnode cancelvp accounting for all
1332 * its indirect blocks in snapvp.
1335 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1336 blksperindir, fs, acctfunc, expungetype)
1337 struct vnode *snapvp;
1338 struct vnode *cancelvp;
1344 ufs_lbn_t blksperindir;
1346 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1347 struct fs *, ufs_lbn_t, int);
1351 ufs_lbn_t subblksperindir;
1352 struct indir indirs[NIADDR + 2];
1353 ufs2_daddr_t last, *bap;
1357 if (expungetype == BLK_NOCOPY)
1359 panic("indiracct_ufs2: missing indir");
1361 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1363 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1364 panic("indiracct_ufs2: botched params");
1366 * We have to expand bread here since it will deadlock looking
1367 * up the block number for any blocks that are not in the cache.
1369 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1370 bp->b_blkno = fsbtodb(fs, blkno);
1371 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1372 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1377 * Account for the block pointers in this indirect block.
1379 last = howmany(remblks, blksperindir);
1380 if (last > NINDIR(fs))
1382 MALLOC(bap, ufs2_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
1383 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1385 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1386 level == 0 ? rlbn : -1, expungetype);
1387 if (error || level == 0)
1390 * Account for the block pointers in each of the indirect blocks
1391 * in the levels below us.
1393 subblksperindir = blksperindir / NINDIR(fs);
1394 for (lbn++, level--, i = 0; i < last; i++) {
1395 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1396 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1399 rlbn += blksperindir;
1400 lbn -= blksperindir;
1401 remblks -= blksperindir;
1404 FREE(bap, M_DEVBUF);
1409 * Do both snap accounting and map accounting.
1412 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1414 ufs2_daddr_t *oldblkp, *lastblkp;
1417 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1421 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1423 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1427 * Identify a set of blocks allocated in a snapshot inode.
1430 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1432 ufs2_daddr_t *oldblkp, *lastblkp;
1435 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1437 struct inode *ip = VTOI(vp);
1438 ufs2_daddr_t blkno, *blkp;
1443 for ( ; oldblkp < lastblkp; oldblkp++) {
1445 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1447 lbn = fragstoblks(fs, blkno);
1449 blkp = &ip->i_din2->di_db[lbn];
1450 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1452 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1453 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1456 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1457 [(lbn - NDADDR) % NINDIR(fs)];
1460 * If we are expunging a snapshot vnode and we
1461 * find a block marked BLK_NOCOPY, then it is
1462 * one that has been allocated to this snapshot after
1463 * we took our current snapshot and can be ignored.
1465 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1470 panic("snapacct_ufs2: bad block");
1471 *blkp = expungetype;
1480 * Account for a set of blocks allocated in a snapshot inode.
1483 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1485 ufs2_daddr_t *oldblkp, *lastblkp;
1496 inum = ip->i_number;
1501 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1503 if (blkno == 0 || blkno == BLK_NOCOPY)
1505 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1506 *ip->i_snapblklist++ = lblkno;
1507 if (blkno == BLK_SNAP)
1508 blkno = blkstofrags(fs, lblkno);
1509 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1515 * Decrement extra reference on snapshot when last name is removed.
1516 * It will not be freed until the last open reference goes away.
1525 struct snapdata *sn;
1526 struct ufsmount *ump;
1529 * Find snapshot in incore list.
1532 sn = ip->i_devvp->v_rdev->si_snapdata;
1534 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1540 printf("ffs_snapgone: lost snapshot vnode %d\n",
1543 * Delete snapshot inode from superblock. Keep list dense.
1548 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1549 if (fs->fs_snapinum[snaploc] == ip->i_number)
1551 if (snaploc < FSMAXSNAP) {
1552 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1553 if (fs->fs_snapinum[snaploc] == 0)
1555 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1557 fs->fs_snapinum[snaploc - 1] = 0;
1563 * Prepare a snapshot file for being removed.
1570 struct vnode *devvp;
1573 struct thread *td = curthread;
1574 ufs2_daddr_t numblks, blkno, dblk;
1575 int error, loc, last;
1576 struct snapdata *sn;
1580 devvp = ip->i_devvp;
1582 * If active, delete from incore list (this snapshot may
1583 * already have been in the process of being deleted, so
1584 * would not have been active).
1586 * Clear copy-on-write flag if last snapshot.
1589 if (ip->i_nextsnap.tqe_prev != 0) {
1590 sn = devvp->v_rdev->si_snapdata;
1591 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1592 ip->i_nextsnap.tqe_prev = 0;
1594 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1596 KASSERT(vp->v_vnlock == &sn->sn_lock,
1597 ("ffs_snapremove: lost lock mutation"));
1598 vp->v_vnlock = &vp->v_lock;
1601 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1602 try_free_snapdata(devvp, td);
1606 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1607 * snapshots that want them (see ffs_snapblkfree below).
1609 for (blkno = 1; blkno < NDADDR; blkno++) {
1610 dblk = DIP(ip, i_db[blkno]);
1613 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1614 DIP_SET(ip, i_db[blkno], 0);
1615 else if ((dblk == blkstofrags(fs, blkno) &&
1616 ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1618 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1619 btodb(fs->fs_bsize));
1620 DIP_SET(ip, i_db[blkno], 0);
1623 numblks = howmany(ip->i_size, fs->fs_bsize);
1624 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1625 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1626 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1629 if (fs->fs_size - blkno > NINDIR(fs))
1632 last = fs->fs_size - blkno;
1633 for (loc = 0; loc < last; loc++) {
1634 if (ip->i_ump->um_fstype == UFS1) {
1635 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1638 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1639 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1640 else if ((dblk == blkstofrags(fs, blkno) &&
1641 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1642 fs->fs_bsize, ip->i_number))) {
1643 ip->i_din1->di_blocks -=
1644 btodb(fs->fs_bsize);
1645 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1649 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1652 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1653 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1654 else if ((dblk == blkstofrags(fs, blkno) &&
1655 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1656 fs->fs_bsize, ip->i_number))) {
1657 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1658 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1664 * Clear snapshot flag and drop reference.
1666 ip->i_flags &= ~SF_SNAPSHOT;
1667 DIP_SET(ip, i_flags, ip->i_flags);
1668 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1671 * Reenable disk quotas for ex-snapshot file.
1673 if (!getinoquota(ip))
1674 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1679 * Notification that a block is being freed. Return zero if the free
1680 * should be allowed to proceed. Return non-zero if the snapshot file
1681 * wants to claim the block. The block will be claimed if it is an
1682 * uncopied part of one of the snapshots. It will be freed if it is
1683 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1684 * If a fragment is being freed, then all snapshots that care about
1685 * it must make a copy since a snapshot file can only claim full sized
1686 * blocks. Note that if more than one snapshot file maps the block,
1687 * we can pick one at random to claim it. Since none of the snapshots
1688 * can change, we are assurred that they will all see the same unmodified
1689 * image. When deleting a snapshot file (see ffs_snapremove above), we
1690 * must push any of these claimed blocks to one of the other snapshots
1691 * that maps it. These claimed blocks are easily identified as they will
1692 * have a block number equal to their logical block number within the
1693 * snapshot. A copied block can never have this property because they
1694 * must always have been allocated from a BLK_NOCOPY location.
1697 ffs_snapblkfree(fs, devvp, bno, size, inum)
1699 struct vnode *devvp;
1704 struct buf *ibp, *cbp, *savedcbp = 0;
1705 struct thread *td = curthread;
1707 struct vnode *vp = NULL;
1710 int indiroff = 0, error = 0, claimedblk = 0;
1711 struct snapdata *sn;
1713 lbn = fragstoblks(fs, bno);
1716 sn = devvp->v_rdev->si_snapdata;
1721 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1722 VI_MTX(devvp)) != 0)
1724 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1727 * Lookup block being written.
1730 blkno = DIP(ip, i_db[lbn]);
1732 td->td_pflags |= TDP_COWINPROGRESS;
1733 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1734 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1735 td->td_pflags &= ~TDP_COWINPROGRESS;
1738 indiroff = (lbn - NDADDR) % NINDIR(fs);
1739 if (ip->i_ump->um_fstype == UFS1)
1740 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1742 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1745 * Check to see if block needs to be copied.
1749 * A block that we map is being freed. If it has not
1750 * been claimed yet, we will claim or copy it (below).
1753 } else if (blkno == BLK_SNAP) {
1755 * No previous snapshot claimed the block,
1756 * so it will be freed and become a BLK_NOCOPY
1757 * (don't care) for us.
1760 panic("snapblkfree: inconsistent block type");
1762 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1763 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1764 } else if (ip->i_ump->um_fstype == UFS1) {
1765 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1769 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1774 } else /* BLK_NOCOPY or default */ {
1776 * If the snapshot has already copied the block
1777 * (default), or does not care about the block,
1785 * If this is a full size block, we will just grab it
1786 * and assign it to the snapshot inode. Otherwise we
1787 * will proceed to copy it. See explanation for this
1788 * routine as to why only a single snapshot needs to
1791 if (size == fs->fs_bsize) {
1794 printf("%s %d lbn %jd from inum %d\n",
1795 "Grabonremove: snapino", ip->i_number,
1796 (intmax_t)lbn, inum);
1799 DIP_SET(ip, i_db[lbn], bno);
1800 } else if (ip->i_ump->um_fstype == UFS1) {
1801 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1804 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1807 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1808 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1809 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1815 * Allocate the block into which to do the copy. Note that this
1816 * allocation will never require any additional allocations for
1817 * the snapshot inode.
1819 td->td_pflags |= TDP_COWINPROGRESS;
1820 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1821 fs->fs_bsize, KERNCRED, 0, &cbp);
1822 td->td_pflags &= ~TDP_COWINPROGRESS;
1827 printf("%s%d lbn %jd %s %d size %ld to blkno %jd\n",
1828 "Copyonremove: snapino ", ip->i_number,
1829 (intmax_t)lbn, "for inum", inum, size,
1830 (intmax_t)cbp->b_blkno);
1833 * If we have already read the old block contents, then
1834 * simply copy them to the new block. Note that we need
1835 * to synchronously write snapshots that have not been
1836 * unlinked, and hence will be visible after a crash,
1837 * to ensure their integrity.
1839 if (savedcbp != 0) {
1840 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1842 if (dopersistence && ip->i_effnlink > 0)
1843 (void) ffs_syncvnode(vp, MNT_WAIT);
1847 * Otherwise, read the old block contents into the buffer.
1849 if ((error = readblock(vp, cbp, lbn)) != 0) {
1850 bzero(cbp->b_data, fs->fs_bsize);
1852 if (dopersistence && ip->i_effnlink > 0)
1853 (void) ffs_syncvnode(vp, MNT_WAIT);
1859 * Note that we need to synchronously write snapshots that
1860 * have not been unlinked, and hence will be visible after
1861 * a crash, to ensure their integrity.
1864 vp = savedcbp->b_vp;
1866 if (dopersistence && VTOI(vp)->i_effnlink > 0)
1867 (void) ffs_syncvnode(vp, MNT_WAIT);
1870 * If we have been unable to allocate a block in which to do
1871 * the copy, then return non-zero so that the fragment will
1872 * not be freed. Although space will be lost, the snapshot
1873 * will stay consistent.
1875 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1880 * Associate snapshot files when mounting.
1883 ffs_snapshot_mount(mp)
1886 struct ufsmount *ump = VFSTOUFS(mp);
1887 struct vnode *devvp = ump->um_devvp;
1888 struct fs *fs = ump->um_fs;
1889 struct thread *td = curthread;
1890 struct snapdata *sn;
1892 struct vnode *lastvp;
1898 daddr_t snaplistsize;
1899 int error, snaploc, loc;
1902 * XXX The following needs to be set before ffs_truncate or
1903 * VOP_READ can be called.
1905 mp->mnt_stat.f_iosize = fs->fs_bsize;
1907 * Process each snapshot listed in the superblock.
1911 sn = devvp->v_rdev->si_snapdata;
1912 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1913 if (fs->fs_snapinum[snaploc] == 0)
1915 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1916 LK_EXCLUSIVE, &vp)) != 0){
1917 printf("ffs_snapshot_mount: vget failed %d\n", error);
1921 if ((ip->i_flags & SF_SNAPSHOT) == 0 || ip->i_size ==
1922 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1923 if ((ip->i_flags & SF_SNAPSHOT) == 0) {
1924 reason = "non-snapshot";
1926 reason = "old format snapshot";
1927 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
1928 (void)ffs_syncvnode(vp, MNT_WAIT);
1930 printf("ffs_snapshot_mount: %s inode %d\n",
1931 reason, fs->fs_snapinum[snaploc]);
1934 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
1935 if (fs->fs_snapinum[loc] == 0)
1937 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
1939 fs->fs_snapinum[loc - 1] = 0;
1944 * If there already exist snapshots on this filesystem, grab a
1945 * reference to their shared lock. If this is the first snapshot
1946 * on this filesystem, we need to allocate a lock for the
1947 * snapshots to share. In either case, acquire the snapshot
1948 * lock and give up our original private lock.
1955 vp->v_vnlock = &sn->sn_lock;
1958 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
1959 TAILQ_INIT(&sn->sn_head);
1960 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
1961 LK_CANRECURSE | LK_NOSHARE);
1963 vp->v_vnlock = &sn->sn_lock;
1964 devvp->v_rdev->si_snapdata = sn;
1966 lockmgr(vp->v_vnlock, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY,
1968 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
1970 * Link it onto the active snapshot list.
1973 if (ip->i_nextsnap.tqe_prev != 0)
1974 panic("ffs_snapshot_mount: %d already on list",
1977 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
1978 vp->v_vflag |= VV_SYSTEM;
1985 * No usable snapshots found.
1990 * Allocate the space for the block hints list. We always want to
1991 * use the list from the newest snapshot.
1993 auio.uio_iov = &aiov;
1994 auio.uio_iovcnt = 1;
1995 aiov.iov_base = (void *)&snaplistsize;
1996 aiov.iov_len = sizeof(snaplistsize);
1997 auio.uio_resid = aiov.iov_len;
1999 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2000 auio.uio_segflg = UIO_SYSSPACE;
2001 auio.uio_rw = UIO_READ;
2003 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2004 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2005 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2009 MALLOC(snapblklist, void *, snaplistsize * sizeof(daddr_t),
2010 M_UFSMNT, M_WAITOK);
2011 auio.uio_iovcnt = 1;
2012 aiov.iov_base = snapblklist;
2013 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2014 auio.uio_resid = aiov.iov_len;
2015 auio.uio_offset -= sizeof(snaplistsize);
2016 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2017 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2019 FREE(snapblklist, M_UFSMNT);
2024 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2025 sn->sn_listsize = snaplistsize;
2026 sn->sn_blklist = (daddr_t *)snapblklist;
2027 devvp->v_vflag |= VV_COPYONWRITE;
2032 * Disassociate snapshot files when unmounting.
2035 ffs_snapshot_unmount(mp)
2038 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2039 struct snapdata *sn;
2042 struct thread *td = curthread;
2045 sn = devvp->v_rdev->si_snapdata;
2046 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2048 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2049 xp->i_nextsnap.tqe_prev = 0;
2050 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2053 lockmgr(&vp->v_lock, LK_INTERLOCK | LK_EXCLUSIVE, VI_MTX(vp));
2055 KASSERT(vp->v_vnlock == &sn->sn_lock,
2056 ("ffs_snapshot_unmount: lost lock mutation"));
2057 vp->v_vnlock = &vp->v_lock;
2059 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2060 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2061 if (xp->i_effnlink > 0)
2064 sn = devvp->v_rdev->si_snapdata;
2066 try_free_snapdata(devvp, td);
2067 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2071 * Check the buffer block to be belong to device buffer that shall be
2072 * locked after snaplk. devvp shall be locked on entry, and will be
2073 * leaved locked upon exit.
2076 ffs_bp_snapblk(devvp, bp)
2077 struct vnode *devvp;
2080 struct snapdata *sn;
2082 ufs2_daddr_t lbn, *snapblklist;
2083 int lower, upper, mid;
2085 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2086 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2087 sn = devvp->v_rdev->si_snapdata;
2088 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2090 fs = TAILQ_FIRST(&sn->sn_head)->i_fs;
2091 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2092 snapblklist = sn->sn_blklist;
2093 upper = sn->sn_listsize - 1;
2095 while (lower <= upper) {
2096 mid = (lower + upper) / 2;
2097 if (snapblklist[mid] == lbn)
2099 if (snapblklist[mid] < lbn)
2115 struct vnode *vp, *devvp;
2119 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2124 devvp = bo->__bo_vnode;
2125 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2128 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2132 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2133 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2138 * Try to find a buffer to flush.
2140 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2141 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2143 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2146 panic("bdwrite: found ourselves");
2149 * Don't countdeps with the bo lock
2152 if (buf_countdeps(nbp, 0)) {
2159 if (!ffs_bp_snapblk(vp, nbp)) {
2160 if (BO_MTX(bo) != VI_MTX(vp)) {
2169 if (nbp->b_flags & B_CLUSTEROK) {
2170 vfs_bio_awrite(nbp);
2175 dirtybufferflushes++;
2184 * Check for need to copy block that is about to be written,
2185 * copying the block if necessary.
2188 ffs_copyonwrite(devvp, bp)
2189 struct vnode *devvp;
2192 struct snapdata *sn;
2193 struct buf *ibp, *cbp, *savedcbp = 0;
2194 struct thread *td = curthread;
2197 struct vnode *vp = 0;
2198 ufs2_daddr_t lbn, blkno, *snapblklist;
2199 int lower, upper, mid, indiroff, error = 0;
2200 int launched_async_io, prev_norunningbuf;
2201 long saved_runningbufspace;
2203 if (devvp != bp->b_vp && (VTOI(bp->b_vp)->i_flags & SF_SNAPSHOT) != 0)
2204 return (0); /* Update on a snapshot file */
2205 if (td->td_pflags & TDP_COWINPROGRESS)
2206 panic("ffs_copyonwrite: recursive call");
2208 * First check to see if it is in the preallocated list.
2209 * By doing this check we avoid several potential deadlocks.
2212 sn = devvp->v_rdev->si_snapdata;
2214 TAILQ_EMPTY(&sn->sn_head)) {
2216 return (0); /* No snapshot */
2218 ip = TAILQ_FIRST(&sn->sn_head);
2220 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2221 snapblklist = sn->sn_blklist;
2222 upper = sn->sn_listsize - 1;
2224 while (lower <= upper) {
2225 mid = (lower + upper) / 2;
2226 if (snapblklist[mid] == lbn)
2228 if (snapblklist[mid] < lbn)
2233 if (lower <= upper) {
2237 launched_async_io = 0;
2238 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2240 * Since I/O on bp isn't yet in progress and it may be blocked
2241 * for a long time waiting on snaplk, back it out of
2242 * runningbufspace, possibly waking other threads waiting for space.
2244 saved_runningbufspace = bp->b_runningbufspace;
2245 if (saved_runningbufspace != 0)
2246 runningbufwakeup(bp);
2248 * Not in the precomputed list, so check the snapshots.
2250 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2251 VI_MTX(devvp)) != 0) {
2253 sn = devvp->v_rdev->si_snapdata;
2255 TAILQ_EMPTY(&sn->sn_head)) {
2257 if (saved_runningbufspace != 0) {
2258 bp->b_runningbufspace = saved_runningbufspace;
2259 atomic_add_int(&runningbufspace,
2260 bp->b_runningbufspace);
2262 return (0); /* Snapshot gone */
2265 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2268 * We ensure that everything of our own that needs to be
2269 * copied will be done at the time that ffs_snapshot is
2270 * called. Thus we can skip the check here which can
2271 * deadlock in doing the lookup in UFS_BALLOC.
2276 * Check to see if block needs to be copied. We do not have
2277 * to hold the snapshot lock while doing this lookup as it
2278 * will never require any additional allocations for the
2282 blkno = DIP(ip, i_db[lbn]);
2284 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2285 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2286 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2287 td->td_pflags &= ~TDP_COWINPROGRESS;
2290 indiroff = (lbn - NDADDR) % NINDIR(fs);
2291 if (ip->i_ump->um_fstype == UFS1)
2292 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2294 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2298 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2299 panic("ffs_copyonwrite: bad copy block");
2304 * Allocate the block into which to do the copy. Since
2305 * multiple processes may all try to copy the same block,
2306 * we have to recheck our need to do a copy if we sleep
2307 * waiting for the lock.
2309 * Because all snapshots on a filesystem share a single
2310 * lock, we ensure that we will never be in competition
2311 * with another process to allocate a block.
2313 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2314 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2315 fs->fs_bsize, KERNCRED, 0, &cbp);
2316 td->td_pflags &= ~TDP_COWINPROGRESS;
2321 printf("Copyonwrite: snapino %d lbn %jd for ",
2322 ip->i_number, (intmax_t)lbn);
2323 if (bp->b_vp == devvp)
2324 printf("fs metadata");
2326 printf("inum %d", VTOI(bp->b_vp)->i_number);
2327 printf(" lblkno %jd to blkno %jd\n",
2328 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2332 * If we have already read the old block contents, then
2333 * simply copy them to the new block. Note that we need
2334 * to synchronously write snapshots that have not been
2335 * unlinked, and hence will be visible after a crash,
2336 * to ensure their integrity.
2338 if (savedcbp != 0) {
2339 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2341 if (dopersistence && ip->i_effnlink > 0)
2342 (void) ffs_syncvnode(vp, MNT_WAIT);
2344 launched_async_io = 1;
2348 * Otherwise, read the old block contents into the buffer.
2350 if ((error = readblock(vp, cbp, lbn)) != 0) {
2351 bzero(cbp->b_data, fs->fs_bsize);
2353 if (dopersistence && ip->i_effnlink > 0)
2354 (void) ffs_syncvnode(vp, MNT_WAIT);
2356 launched_async_io = 1;
2362 * Note that we need to synchronously write snapshots that
2363 * have not been unlinked, and hence will be visible after
2364 * a crash, to ensure their integrity.
2367 vp = savedcbp->b_vp;
2369 if (dopersistence && VTOI(vp)->i_effnlink > 0)
2370 (void) ffs_syncvnode(vp, MNT_WAIT);
2372 launched_async_io = 1;
2374 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2375 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2377 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2378 waitrunningbufspace();
2380 * I/O on bp will now be started, so count it in runningbufspace.
2382 if (saved_runningbufspace != 0) {
2383 bp->b_runningbufspace = saved_runningbufspace;
2384 atomic_add_int(&runningbufspace, bp->b_runningbufspace);
2390 * Read the specified block into the given buffer.
2391 * Much of this boiler-plate comes from bwrite().
2394 readblock(vp, bp, lbn)
2399 struct inode *ip = VTOI(vp);
2402 bip = g_alloc_bio();
2403 bip->bio_cmd = BIO_READ;
2404 bip->bio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
2405 bip->bio_data = bp->b_data;
2406 bip->bio_length = bp->b_bcount;
2407 bip->bio_done = NULL;
2409 g_io_request(bip, ip->i_devvp->v_bufobj.bo_private);
2410 bp->b_error = biowait(bip, "snaprdb");
2412 return (bp->b_error);
2416 * Process file deletes that were deferred by ufs_inactive() due to
2417 * the file system being suspended. Transfer IN_LAZYACCESS into
2418 * IN_MODIFIED for vnodes that were accessed during suspension.
2421 process_deferred_inactive(struct mount *mp)
2423 struct vnode *vp, *mvp;
2429 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2432 MNT_VNODE_FOREACH(vp, mp, mvp) {
2435 * IN_LAZYACCESS is checked here without holding any
2436 * vnode lock, but this flag is set only while holding
2439 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED) != 0 ||
2440 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2441 ((vp->v_iflag & VI_OWEINACT) == 0 ||
2442 vp->v_usecount > 0))) {
2448 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2452 if (error == ENOENT)
2453 continue; /* vnode recycled */
2454 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2458 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2459 ip->i_flag &= ~IN_LAZYACCESS;
2460 ip->i_flag |= IN_MODIFIED;
2463 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2471 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2472 ("process_deferred_inactive: "
2473 "recursed on VI_DOINGINACT"));
2474 vp->v_iflag |= VI_DOINGINACT;
2475 vp->v_iflag &= ~VI_OWEINACT;
2477 (void) VOP_INACTIVE(vp, td);
2479 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2480 ("process_deferred_inactive: lost VI_DOINGINACT"));
2481 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2482 ("process_deferred_inactive: got VI_OWEINACT"));
2483 vp->v_iflag &= ~VI_DOINGINACT;
2490 vn_finished_secondary_write(mp);
2493 /* Try to free snapdata associated with devvp */
2495 try_free_snapdata(struct vnode *devvp,
2498 struct snapdata *sn;
2499 ufs2_daddr_t *snapblklist;
2501 sn = devvp->v_rdev->si_snapdata;
2503 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2504 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2509 devvp->v_rdev->si_snapdata = NULL;
2510 devvp->v_vflag &= ~VV_COPYONWRITE;
2511 snapblklist = sn->sn_blklist;
2512 sn->sn_blklist = NULL;
2513 sn->sn_listsize = 0;
2514 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2515 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2516 lockdestroy(&sn->sn_lock);
2518 if (snapblklist != NULL)
2519 FREE(snapblklist, M_UFSMNT);