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 try_free_snapdata(struct vnode *devvp, struct thread *td);
165 static int ffs_bp_snapblk(struct vnode *, struct buf *);
168 * To ensure the consistency of snapshots across crashes, we must
169 * synchronously write out copied blocks before allowing the
170 * originals to be modified. Because of the rather severe speed
171 * penalty that this imposes, the following flag allows this
172 * crash persistence to be disabled.
174 int dopersistence = 0;
177 #include <sys/sysctl.h>
178 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
179 static int snapdebug = 0;
180 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
181 int collectsnapstats = 0;
182 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
187 * Create a snapshot file and initialize it for the filesystem.
190 ffs_snapshot(mp, snapfile)
194 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
195 int error, cg, snaploc;
196 int i, size, len, loc;
198 struct timespec starttime = {0, 0}, endtime;
200 long redo = 0, snaplistsize = 0;
203 struct fs *copy_fs = NULL, *fs;
204 struct thread *td = curthread;
205 struct inode *ip, *xp;
206 struct buf *bp, *nbp, *ibp, *sbp = NULL;
210 struct vnode *vp, *xvp, *mvp, *devvp;
214 struct ufsmount *ump;
224 * Need to serialize access to snapshot code per filesystem.
227 * Assign a snapshot slot in the superblock.
230 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
231 if (fs->fs_snapinum[snaploc] == 0)
234 if (snaploc == FSMAXSNAP)
237 * Create the snapshot file.
240 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE, snapfile, td);
241 if ((error = namei(&nd)) != 0)
243 if (nd.ni_vp != NULL) {
247 if (nd.ni_dvp->v_mount != mp)
250 NDFREE(&nd, NDF_ONLY_PNBUF);
251 if (nd.ni_dvp == nd.ni_vp)
259 vat.va_mode = S_IRUSR;
260 vat.va_vaflags |= VA_EXCLUSIVE;
261 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
264 panic("ffs_snapshot: mount mismatch");
266 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
267 NDFREE(&nd, NDF_ONLY_PNBUF);
269 if ((error = vn_start_write(NULL, &wrtmp,
270 V_XSLEEP | PCATCH)) != 0)
274 VOP_LEASE(nd.ni_dvp, td, KERNCRED, LEASE_WRITE);
275 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
276 VOP_UNLOCK(nd.ni_dvp, 0, td);
278 NDFREE(&nd, NDF_ONLY_PNBUF);
279 vn_finished_write(wrtmp);
284 vp->v_vflag |= VV_SYSTEM;
288 * Allocate and copy the last block contents so as to be able
289 * to set size to that of the filesystem.
291 numblks = howmany(fs->fs_size, fs->fs_frag);
292 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
293 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
296 ip->i_size = lblktosize(fs, (off_t)numblks);
297 DIP_SET(ip, i_size, ip->i_size);
298 ip->i_flag |= IN_CHANGE | IN_UPDATE;
299 error = readblock(vp, bp, numblks - 1);
304 * Preallocate critical data structures so that we can copy
305 * them in without further allocation after we suspend all
306 * operations on the filesystem. We would like to just release
307 * the allocated buffers without writing them since they will
308 * be filled in below once we are ready to go, but this upsets
309 * the soft update code, so we go ahead and write the new buffers.
311 * Allocate all indirect blocks and mark all of them as not
312 * needing to be copied.
314 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
315 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
316 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
322 * Allocate copies for the superblock and its summary information.
324 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
329 blkno = fragstoblks(fs, fs->fs_csaddr);
330 len = howmany(fs->fs_cssize, fs->fs_bsize);
331 for (loc = 0; loc < len; loc++) {
332 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
333 fs->fs_bsize, KERNCRED, 0, &nbp);
339 * Allocate all cylinder group blocks.
341 for (cg = 0; cg < fs->fs_ncg; cg++) {
342 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
343 fs->fs_bsize, KERNCRED, 0, &nbp);
348 ffs_syncvnode(vp, MNT_WAIT);
351 * Copy all the cylinder group maps. Although the
352 * filesystem is still active, we hope that only a few
353 * cylinder groups will change between now and when we
354 * suspend operations. Thus, we will be able to quickly
355 * touch up the few cylinder groups that changed during
356 * the suspension period.
358 len = howmany(fs->fs_ncg, NBBY);
359 MALLOC(space, void *, len, M_DEVBUF, M_WAITOK|M_ZERO);
361 fs->fs_active = space;
363 for (cg = 0; cg < fs->fs_ncg; cg++) {
364 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
365 fs->fs_bsize, KERNCRED, 0, &nbp);
368 error = cgaccount(cg, vp, nbp, 1);
371 ffs_syncvnode(vp, MNT_WAIT);
376 * Change inode to snapshot type file.
378 ip->i_flags |= SF_SNAPSHOT;
379 DIP_SET(ip, i_flags, ip->i_flags);
380 ip->i_flag |= IN_CHANGE | IN_UPDATE;
382 * Ensure that the snapshot is completely on disk.
383 * Since we have marked it as a snapshot it is safe to
384 * unlock it as no process will be allowed to write to it.
386 if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
388 VOP_UNLOCK(vp, 0, td);
390 * All allocations are done, so we can now snapshot the system.
392 * Recind nice scheduling while running with the filesystem suspended.
394 if (td->td_proc->p_nice > 0) {
400 saved_nice = p->p_nice;
406 * Suspend operation on filesystem.
409 vn_finished_write(wrtmp);
410 if ((error = vfs_write_suspend(vp->v_mount)) != 0) {
411 vn_start_write(NULL, &wrtmp, V_WAIT);
412 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
415 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
417 vn_start_write(NULL, &wrtmp, V_WAIT);
419 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
420 if (ip->i_effnlink == 0) {
421 error = ENOENT; /* Snapshot file unlinked */
424 if (collectsnapstats)
425 nanotime(&starttime);
427 /* The last block might have changed. Copy it again to be sure. */
428 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
429 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
432 error = readblock(vp, bp, numblks - 1);
433 bp->b_flags |= B_VALIDSUSPWRT;
438 * First, copy all the cylinder group maps that have changed.
440 for (cg = 0; cg < fs->fs_ncg; cg++) {
441 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
444 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
445 fs->fs_bsize, KERNCRED, 0, &nbp);
448 error = cgaccount(cg, vp, nbp, 2);
454 * Grab a copy of the superblock and its summary information.
455 * We delay writing it until the suspension is released below.
457 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
464 loc = blkoff(fs, fs->fs_sblockloc);
465 copy_fs = (struct fs *)(sbp->b_data + loc);
466 bcopy(fs, copy_fs, fs->fs_sbsize);
467 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
468 copy_fs->fs_clean = 1;
469 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
470 if (fs->fs_sbsize < size)
471 bzero(&sbp->b_data[loc + fs->fs_sbsize], size - fs->fs_sbsize);
472 size = blkroundup(fs, fs->fs_cssize);
473 if (fs->fs_contigsumsize > 0)
474 size += fs->fs_ncg * sizeof(int32_t);
475 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
476 copy_fs->fs_csp = space;
477 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
478 space = (char *)space + fs->fs_cssize;
479 loc = howmany(fs->fs_cssize, fs->fs_fsize);
480 i = fs->fs_frag - loc % fs->fs_frag;
481 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
483 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
484 len, KERNCRED, &bp)) != 0) {
486 free(copy_fs->fs_csp, M_UFSMNT);
491 bcopy(bp->b_data, space, (u_int)len);
492 space = (char *)space + len;
493 bp->b_flags |= B_INVAL | B_NOCACHE;
496 if (fs->fs_contigsumsize > 0) {
497 copy_fs->fs_maxcluster = lp = space;
498 for (i = 0; i < fs->fs_ncg; i++)
499 *lp++ = fs->fs_contigsumsize;
502 * We must check for active files that have been unlinked
503 * (e.g., with a zero link count). We have to expunge all
504 * trace of these files from the snapshot so that they are
505 * not reclaimed prematurely by fsck or unnecessarily dumped.
506 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
507 * spec_strategy about writing on a suspended filesystem.
508 * Note that we skip unlinked snapshot files as they will
509 * be handled separately below.
511 * We also calculate the needed size for the snapshot list.
513 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
514 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
516 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
518 MNT_VNODE_FOREACH(xvp, mp, mvp) {
521 if ((xvp->v_iflag & VI_DOOMED) ||
522 (xvp->v_usecount == 0 &&
523 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
524 xvp->v_type == VNON ||
525 (VTOI(xvp)->i_flags & SF_SNAPSHOT)) {
531 * We can skip parent directory vnode because it must have
532 * this snapshot file in it.
534 if (xvp == nd.ni_dvp) {
540 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0) {
542 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
547 if (xvp->v_usecount == 0 &&
548 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
550 VOP_UNLOCK(xvp, 0, td);
557 vprint("ffs_snapshot: busy vnode", xvp);
558 if (VOP_GETATTR(xvp, &vat, td->td_ucred, td) == 0 &&
560 VOP_UNLOCK(xvp, 0, td);
566 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
567 VOP_UNLOCK(xvp, 0, td);
573 * If there is a fragment, clear it here.
576 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
578 len = fragroundup(fs, blkoff(fs, xp->i_size));
579 if (len != 0 && len < fs->fs_bsize) {
580 ffs_blkfree(ump, copy_fs, vp,
581 DIP(xp, i_db[loc]), len, xp->i_number);
582 blkno = DIP(xp, i_db[loc]);
583 DIP_SET(xp, i_db[loc], 0);
587 if (xp->i_ump->um_fstype == UFS1)
588 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
591 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
594 DIP_SET(xp, i_db[loc], blkno);
596 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
598 VOP_UNLOCK(xvp, 0, td);
601 free(copy_fs->fs_csp, M_UFSMNT);
604 MNT_VNODE_FOREACH_ABORT(mp, mvp);
611 * If there already exist snapshots on this filesystem, grab a
612 * reference to their shared lock. If this is the first snapshot
613 * on this filesystem, we need to allocate a lock for the snapshots
614 * to share. In either case, acquire the snapshot lock and give
615 * up our original private lock.
618 sn = devvp->v_rdev->si_snapdata;
620 xp = TAILQ_FIRST(&sn->sn_head);
623 vp->v_vnlock = &sn->sn_lock;
626 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
627 TAILQ_INIT(&sn->sn_head);
628 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
629 LK_CANRECURSE | LK_NOSHARE);
631 vp->v_vnlock = &sn->sn_lock;
632 mp_fixme("si_snapdata setting is racey.");
633 devvp->v_rdev->si_snapdata = sn;
636 lockmgr(vp->v_vnlock, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY,
638 lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
640 * If this is the first snapshot on this filesystem, then we need
641 * to allocate the space for the list of preallocated snapshot blocks.
642 * This list will be refined below, but this preliminary one will
643 * keep us out of deadlock until the full one is ready.
646 MALLOC(snapblklist, daddr_t *, snaplistsize * sizeof(daddr_t),
648 blkp = &snapblklist[1];
649 *blkp++ = lblkno(fs, fs->fs_sblockloc);
650 blkno = fragstoblks(fs, fs->fs_csaddr);
651 for (cg = 0; cg < fs->fs_ncg; cg++) {
652 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
654 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
656 len = howmany(fs->fs_cssize, fs->fs_bsize);
657 for (loc = 0; loc < len; loc++)
658 *blkp++ = blkno + loc;
659 for (; cg < fs->fs_ncg; cg++)
660 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
661 snapblklist[0] = blkp - snapblklist;
663 if (sn->sn_blklist != NULL)
664 panic("ffs_snapshot: non-empty list");
665 sn->sn_blklist = snapblklist;
666 sn->sn_listsize = blkp - snapblklist;
670 * Record snapshot inode. Since this is the newest snapshot,
671 * it must be placed at the end of the list.
674 fs->fs_snapinum[snaploc] = ip->i_number;
675 if (ip->i_nextsnap.tqe_prev != 0)
676 panic("ffs_snapshot: %d already on list", ip->i_number);
677 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
678 devvp->v_vflag |= VV_COPYONWRITE;
680 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
682 KASSERT((sn != NULL && sbp != NULL && error == 0) ||
683 (sn == NULL && sbp == NULL && error != 0),
684 ("email phk@ and mckusick@"));
686 * Resume operation on filesystem.
688 vfs_write_resume(vp->v_mount);
689 vn_start_write(NULL, &wrtmp, V_WAIT);
690 if (collectsnapstats && starttime.tv_sec > 0) {
692 timespecsub(&endtime, &starttime);
693 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
694 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
695 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
700 * Copy allocation information from all the snapshots in
701 * this snapshot and then expunge them from its view.
703 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
706 if (xp->i_ump->um_fstype == UFS1)
707 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
710 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
712 if (error == 0 && xp->i_effnlink == 0) {
713 error = ffs_freefile(ump,
720 fs->fs_snapinum[snaploc] = 0;
725 * Allocate space for the full list of preallocated snapshot blocks.
727 MALLOC(snapblklist, daddr_t *, snaplistsize * sizeof(daddr_t),
729 ip->i_snapblklist = &snapblklist[1];
731 * Expunge the blocks used by the snapshots from the set of
732 * blocks marked as used in the snapshot bitmaps. Also, collect
733 * the list of allocated blocks in i_snapblklist.
735 if (ip->i_ump->um_fstype == UFS1)
736 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, BLK_SNAP);
738 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, BLK_SNAP);
740 fs->fs_snapinum[snaploc] = 0;
741 FREE(snapblklist, M_UFSMNT);
744 if (snaplistsize < ip->i_snapblklist - snapblklist)
745 panic("ffs_snapshot: list too small");
746 snaplistsize = ip->i_snapblklist - snapblklist;
747 snapblklist[0] = snaplistsize;
748 ip->i_snapblklist = 0;
750 * Write out the list of allocated blocks to the end of the snapshot.
752 auio.uio_iov = &aiov;
754 aiov.iov_base = (void *)snapblklist;
755 aiov.iov_len = snaplistsize * sizeof(daddr_t);
756 auio.uio_resid = aiov.iov_len;;
757 auio.uio_offset = ip->i_size;
758 auio.uio_segflg = UIO_SYSSPACE;
759 auio.uio_rw = UIO_WRITE;
761 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
762 fs->fs_snapinum[snaploc] = 0;
763 FREE(snapblklist, M_UFSMNT);
767 * Write the superblock and its summary information
770 blkno = fragstoblks(fs, fs->fs_csaddr);
771 len = howmany(fs->fs_cssize, fs->fs_bsize);
772 space = copy_fs->fs_csp;
773 for (loc = 0; loc < len; loc++) {
774 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
777 fs->fs_snapinum[snaploc] = 0;
778 FREE(snapblklist, M_UFSMNT);
781 bcopy(space, nbp->b_data, fs->fs_bsize);
782 space = (char *)space + fs->fs_bsize;
786 * As this is the newest list, it is the most inclusive, so
787 * should replace the previous list.
790 space = sn->sn_blklist;
791 sn->sn_blklist = snapblklist;
792 sn->sn_listsize = snaplistsize;
795 FREE(space, M_UFSMNT);
797 * If another process is currently writing the buffer containing
798 * the inode for this snapshot then a deadlock can occur. Drop
799 * the snapshot lock until the buffer has been written.
801 VREF(vp); /* Protect against ffs_snapgone() */
802 VOP_UNLOCK(vp, 0, td);
803 (void) bread(ip->i_devvp,
804 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
805 (int) fs->fs_bsize, NOCRED, &nbp);
807 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
808 if (ip->i_effnlink == 0)
809 error = ENOENT; /* Snapshot file unlinked */
811 vrele(vp); /* Drop extra reference */
813 FREE(copy_fs->fs_csp, M_UFSMNT);
816 NDFREE(&nd, NDF_ONLY_PNBUF);
817 if (saved_nice > 0) {
823 sched_nice(td->td_proc, saved_nice);
825 PROC_UNLOCK(td->td_proc);
828 if (fs->fs_active != 0) {
829 FREE(fs->fs_active, M_DEVBUF);
834 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
837 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
838 (void) ffs_syncvnode(vp, MNT_WAIT);
842 VOP_UNLOCK(vp, 0, td);
844 vn_finished_write(wrtmp);
845 process_deferred_inactive(mp);
850 * Copy a cylinder group map. All the unallocated blocks are marked
851 * BLK_NOCOPY so that the snapshot knows that it need not copy them
852 * if they are later written. If passno is one, then this is a first
853 * pass, so only setting needs to be done. If passno is 2, then this
854 * is a revision to a previous pass which must be undone as the
855 * replacement pass is done.
858 cgaccount(cg, vp, nbp, passno)
864 struct buf *bp, *ibp;
868 ufs2_daddr_t base, numblks;
869 int error, len, loc, indiroff;
873 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
874 (int)fs->fs_cgsize, KERNCRED, &bp);
879 cgp = (struct cg *)bp->b_data;
880 if (!cg_chkmagic(cgp)) {
887 * Recomputation of summary information might not have been performed
888 * at mount time. Sync up summary information for current cylinder
889 * group while data is in memory to ensure that result of background
890 * fsck is slightly more consistent.
892 fs->fs_cs(fs, cg) = cgp->cg_cs;
893 UFS_UNLOCK(ip->i_ump);
894 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
895 if (fs->fs_cgsize < fs->fs_bsize)
896 bzero(&nbp->b_data[fs->fs_cgsize],
897 fs->fs_bsize - fs->fs_cgsize);
898 cgp = (struct cg *)nbp->b_data;
901 nbp->b_flags |= B_VALIDSUSPWRT;
902 numblks = howmany(fs->fs_size, fs->fs_frag);
903 len = howmany(fs->fs_fpg, fs->fs_frag);
904 base = cgbase(fs, cg) / fs->fs_frag;
905 if (base + len >= numblks)
906 len = numblks - base - 1;
909 for ( ; loc < NDADDR; loc++) {
910 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
911 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
912 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
913 DIP_SET(ip, i_db[loc], 0);
914 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
915 panic("ffs_snapshot: lost direct block");
918 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
919 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
923 indiroff = (base + loc - NDADDR) % NINDIR(fs);
924 for ( ; loc < len; loc++, indiroff++) {
925 if (indiroff >= NINDIR(fs)) {
927 ibp->b_flags |= B_VALIDSUSPWRT;
929 error = UFS_BALLOC(vp,
930 lblktosize(fs, (off_t)(base + loc)),
931 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
937 if (ip->i_ump->um_fstype == UFS1) {
938 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
939 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
941 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
942 [indiroff] == BLK_NOCOPY)
943 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
944 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
945 [indiroff] == BLK_NOCOPY)
946 panic("ffs_snapshot: lost indirect block");
949 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
950 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
951 else if (passno == 2 &&
952 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
953 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
954 else if (passno == 1 &&
955 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
956 panic("ffs_snapshot: lost indirect block");
959 ibp->b_flags |= B_VALIDSUSPWRT;
965 * Before expunging a snapshot inode, note all the
966 * blocks that it claims with BLK_SNAP so that fsck will
967 * be able to account for those blocks properly and so
968 * that this snapshot knows that it need not copy them
969 * if the other snapshot holding them is freed. This code
970 * is reproduced once each for UFS1 and UFS2.
973 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype)
974 struct vnode *snapvp;
975 struct inode *cancelip;
977 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
978 struct fs *, ufs_lbn_t, int);
981 int i, error, indiroff;
983 ufs2_daddr_t len, blkno, numblks, blksperindir;
984 struct ufs1_dinode *dip;
985 struct thread *td = curthread;
989 * Prepare to expunge the inode. If its inode block has not
990 * yet been copied, then allocate and fill the copy.
992 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
995 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
997 td->td_pflags |= TDP_COWINPROGRESS;
998 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
999 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1000 td->td_pflags &= ~TDP_COWINPROGRESS;
1003 indiroff = (lbn - NDADDR) % NINDIR(fs);
1004 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1008 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1011 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1012 fs->fs_bsize, KERNCRED, 0, &bp);
1015 if ((error = readblock(snapvp, bp, lbn)) != 0)
1019 * Set a snapshot inode to be a zero length file, regular files
1020 * or unlinked snapshots to be completely unallocated.
1022 dip = (struct ufs1_dinode *)bp->b_data +
1023 ino_to_fsbo(fs, cancelip->i_number);
1024 if (expungetype == BLK_NOCOPY || cancelip->i_effnlink == 0)
1028 dip->di_flags &= ~SF_SNAPSHOT;
1029 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1032 * Now go through and expunge all the blocks in the file
1033 * using the function requested.
1035 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1036 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1037 &cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
1039 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1040 &cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
1044 len = numblks - NDADDR;
1046 for (i = 0; len > 0 && i < NIADDR; i++) {
1047 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1048 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1049 blksperindir, fs, acctfunc, expungetype);
1052 blksperindir *= NINDIR(fs);
1053 lbn -= blksperindir + 1;
1054 len -= blksperindir;
1055 rlbn += blksperindir;
1061 * Descend an indirect block chain for vnode cancelvp accounting for all
1062 * its indirect blocks in snapvp.
1065 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1066 blksperindir, fs, acctfunc, expungetype)
1067 struct vnode *snapvp;
1068 struct vnode *cancelvp;
1074 ufs_lbn_t blksperindir;
1076 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1077 struct fs *, ufs_lbn_t, int);
1081 ufs_lbn_t subblksperindir;
1082 struct indir indirs[NIADDR + 2];
1083 ufs1_daddr_t last, *bap;
1087 if (expungetype == BLK_NOCOPY)
1089 panic("indiracct_ufs1: missing indir");
1091 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1093 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1094 panic("indiracct_ufs1: botched params");
1096 * We have to expand bread here since it will deadlock looking
1097 * up the block number for any blocks that are not in the cache.
1099 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1100 bp->b_blkno = fsbtodb(fs, blkno);
1101 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1102 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1107 * Account for the block pointers in this indirect block.
1109 last = howmany(remblks, blksperindir);
1110 if (last > NINDIR(fs))
1112 MALLOC(bap, ufs1_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
1113 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1115 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1116 level == 0 ? rlbn : -1, expungetype);
1117 if (error || level == 0)
1120 * Account for the block pointers in each of the indirect blocks
1121 * in the levels below us.
1123 subblksperindir = blksperindir / NINDIR(fs);
1124 for (lbn++, level--, i = 0; i < last; i++) {
1125 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1126 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1129 rlbn += blksperindir;
1130 lbn -= blksperindir;
1131 remblks -= blksperindir;
1134 FREE(bap, M_DEVBUF);
1139 * Do both snap accounting and map accounting.
1142 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1144 ufs1_daddr_t *oldblkp, *lastblkp;
1147 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1151 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1153 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1157 * Identify a set of blocks allocated in a snapshot inode.
1160 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1162 ufs1_daddr_t *oldblkp, *lastblkp;
1165 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1167 struct inode *ip = VTOI(vp);
1168 ufs1_daddr_t blkno, *blkp;
1173 for ( ; oldblkp < lastblkp; oldblkp++) {
1175 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1177 lbn = fragstoblks(fs, blkno);
1179 blkp = &ip->i_din1->di_db[lbn];
1180 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1182 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1183 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1186 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1187 [(lbn - NDADDR) % NINDIR(fs)];
1190 * If we are expunging a snapshot vnode and we
1191 * find a block marked BLK_NOCOPY, then it is
1192 * one that has been allocated to this snapshot after
1193 * we took our current snapshot and can be ignored.
1195 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1200 panic("snapacct_ufs1: bad block");
1201 *blkp = expungetype;
1210 * Account for a set of blocks allocated in a snapshot inode.
1213 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1215 ufs1_daddr_t *oldblkp, *lastblkp;
1226 inum = ip->i_number;
1231 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1233 if (blkno == 0 || blkno == BLK_NOCOPY)
1235 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1236 *ip->i_snapblklist++ = lblkno;
1237 if (blkno == BLK_SNAP)
1238 blkno = blkstofrags(fs, lblkno);
1239 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1245 * Before expunging a snapshot inode, note all the
1246 * blocks that it claims with BLK_SNAP so that fsck will
1247 * be able to account for those blocks properly and so
1248 * that this snapshot knows that it need not copy them
1249 * if the other snapshot holding them is freed. This code
1250 * is reproduced once each for UFS1 and UFS2.
1253 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype)
1254 struct vnode *snapvp;
1255 struct inode *cancelip;
1257 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1258 struct fs *, ufs_lbn_t, int);
1261 int i, error, indiroff;
1262 ufs_lbn_t lbn, rlbn;
1263 ufs2_daddr_t len, blkno, numblks, blksperindir;
1264 struct ufs2_dinode *dip;
1265 struct thread *td = curthread;
1269 * Prepare to expunge the inode. If its inode block has not
1270 * yet been copied, then allocate and fill the copy.
1272 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1275 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1277 td->td_pflags |= TDP_COWINPROGRESS;
1278 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1279 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1280 td->td_pflags &= ~TDP_COWINPROGRESS;
1283 indiroff = (lbn - NDADDR) % NINDIR(fs);
1284 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1288 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1291 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1292 fs->fs_bsize, KERNCRED, 0, &bp);
1295 if ((error = readblock(snapvp, bp, lbn)) != 0)
1299 * Set a snapshot inode to be a zero length file, regular files
1300 * to be completely unallocated.
1302 dip = (struct ufs2_dinode *)bp->b_data +
1303 ino_to_fsbo(fs, cancelip->i_number);
1304 if (expungetype == BLK_NOCOPY)
1308 dip->di_flags &= ~SF_SNAPSHOT;
1309 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1312 * Now go through and expunge all the blocks in the file
1313 * using the function requested.
1315 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1316 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1317 &cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
1319 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1320 &cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
1324 len = numblks - NDADDR;
1326 for (i = 0; len > 0 && i < NIADDR; i++) {
1327 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1328 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1329 blksperindir, fs, acctfunc, expungetype);
1332 blksperindir *= NINDIR(fs);
1333 lbn -= blksperindir + 1;
1334 len -= blksperindir;
1335 rlbn += blksperindir;
1341 * Descend an indirect block chain for vnode cancelvp accounting for all
1342 * its indirect blocks in snapvp.
1345 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1346 blksperindir, fs, acctfunc, expungetype)
1347 struct vnode *snapvp;
1348 struct vnode *cancelvp;
1354 ufs_lbn_t blksperindir;
1356 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1357 struct fs *, ufs_lbn_t, int);
1361 ufs_lbn_t subblksperindir;
1362 struct indir indirs[NIADDR + 2];
1363 ufs2_daddr_t last, *bap;
1367 if (expungetype == BLK_NOCOPY)
1369 panic("indiracct_ufs2: missing indir");
1371 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1373 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1374 panic("indiracct_ufs2: botched params");
1376 * We have to expand bread here since it will deadlock looking
1377 * up the block number for any blocks that are not in the cache.
1379 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1380 bp->b_blkno = fsbtodb(fs, blkno);
1381 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1382 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1387 * Account for the block pointers in this indirect block.
1389 last = howmany(remblks, blksperindir);
1390 if (last > NINDIR(fs))
1392 MALLOC(bap, ufs2_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
1393 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1395 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1396 level == 0 ? rlbn : -1, expungetype);
1397 if (error || level == 0)
1400 * Account for the block pointers in each of the indirect blocks
1401 * in the levels below us.
1403 subblksperindir = blksperindir / NINDIR(fs);
1404 for (lbn++, level--, i = 0; i < last; i++) {
1405 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1406 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1409 rlbn += blksperindir;
1410 lbn -= blksperindir;
1411 remblks -= blksperindir;
1414 FREE(bap, M_DEVBUF);
1419 * Do both snap accounting and map accounting.
1422 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1424 ufs2_daddr_t *oldblkp, *lastblkp;
1427 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1431 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1433 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1437 * Identify a set of blocks allocated in a snapshot inode.
1440 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1442 ufs2_daddr_t *oldblkp, *lastblkp;
1445 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1447 struct inode *ip = VTOI(vp);
1448 ufs2_daddr_t blkno, *blkp;
1453 for ( ; oldblkp < lastblkp; oldblkp++) {
1455 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1457 lbn = fragstoblks(fs, blkno);
1459 blkp = &ip->i_din2->di_db[lbn];
1460 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1462 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1463 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1466 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1467 [(lbn - NDADDR) % NINDIR(fs)];
1470 * If we are expunging a snapshot vnode and we
1471 * find a block marked BLK_NOCOPY, then it is
1472 * one that has been allocated to this snapshot after
1473 * we took our current snapshot and can be ignored.
1475 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1480 panic("snapacct_ufs2: bad block");
1481 *blkp = expungetype;
1490 * Account for a set of blocks allocated in a snapshot inode.
1493 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1495 ufs2_daddr_t *oldblkp, *lastblkp;
1506 inum = ip->i_number;
1511 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1513 if (blkno == 0 || blkno == BLK_NOCOPY)
1515 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1516 *ip->i_snapblklist++ = lblkno;
1517 if (blkno == BLK_SNAP)
1518 blkno = blkstofrags(fs, lblkno);
1519 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1525 * Decrement extra reference on snapshot when last name is removed.
1526 * It will not be freed until the last open reference goes away.
1535 struct snapdata *sn;
1536 struct ufsmount *ump;
1539 * Find snapshot in incore list.
1542 sn = ip->i_devvp->v_rdev->si_snapdata;
1544 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1550 printf("ffs_snapgone: lost snapshot vnode %d\n",
1553 * Delete snapshot inode from superblock. Keep list dense.
1558 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1559 if (fs->fs_snapinum[snaploc] == ip->i_number)
1561 if (snaploc < FSMAXSNAP) {
1562 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1563 if (fs->fs_snapinum[snaploc] == 0)
1565 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1567 fs->fs_snapinum[snaploc - 1] = 0;
1573 * Prepare a snapshot file for being removed.
1580 struct vnode *devvp;
1583 struct thread *td = curthread;
1584 ufs2_daddr_t numblks, blkno, dblk;
1585 int error, loc, last;
1586 struct snapdata *sn;
1590 devvp = ip->i_devvp;
1592 * If active, delete from incore list (this snapshot may
1593 * already have been in the process of being deleted, so
1594 * would not have been active).
1596 * Clear copy-on-write flag if last snapshot.
1599 if (ip->i_nextsnap.tqe_prev != 0) {
1600 sn = devvp->v_rdev->si_snapdata;
1601 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1602 ip->i_nextsnap.tqe_prev = 0;
1604 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL, td);
1606 KASSERT(vp->v_vnlock == &sn->sn_lock,
1607 ("ffs_snapremove: lost lock mutation"));
1608 vp->v_vnlock = &vp->v_lock;
1611 lockmgr(&sn->sn_lock, LK_RELEASE, NULL, td);
1612 try_free_snapdata(devvp, td);
1616 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1617 * snapshots that want them (see ffs_snapblkfree below).
1619 for (blkno = 1; blkno < NDADDR; blkno++) {
1620 dblk = DIP(ip, i_db[blkno]);
1623 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1624 DIP_SET(ip, i_db[blkno], 0);
1625 else if ((dblk == blkstofrags(fs, blkno) &&
1626 ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1628 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1629 btodb(fs->fs_bsize));
1630 DIP_SET(ip, i_db[blkno], 0);
1633 numblks = howmany(ip->i_size, fs->fs_bsize);
1634 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1635 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1636 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1639 if (fs->fs_size - blkno > NINDIR(fs))
1642 last = fs->fs_size - blkno;
1643 for (loc = 0; loc < last; loc++) {
1644 if (ip->i_ump->um_fstype == UFS1) {
1645 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1648 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1649 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1650 else if ((dblk == blkstofrags(fs, blkno) &&
1651 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1652 fs->fs_bsize, ip->i_number))) {
1653 ip->i_din1->di_blocks -=
1654 btodb(fs->fs_bsize);
1655 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1659 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1662 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1663 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1664 else if ((dblk == blkstofrags(fs, blkno) &&
1665 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1666 fs->fs_bsize, ip->i_number))) {
1667 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1668 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1674 * Clear snapshot flag and drop reference.
1676 ip->i_flags &= ~SF_SNAPSHOT;
1677 DIP_SET(ip, i_flags, ip->i_flags);
1678 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1681 * Reenable disk quotas for ex-snapshot file.
1683 if (!getinoquota(ip))
1684 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1689 * Notification that a block is being freed. Return zero if the free
1690 * should be allowed to proceed. Return non-zero if the snapshot file
1691 * wants to claim the block. The block will be claimed if it is an
1692 * uncopied part of one of the snapshots. It will be freed if it is
1693 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1694 * If a fragment is being freed, then all snapshots that care about
1695 * it must make a copy since a snapshot file can only claim full sized
1696 * blocks. Note that if more than one snapshot file maps the block,
1697 * we can pick one at random to claim it. Since none of the snapshots
1698 * can change, we are assurred that they will all see the same unmodified
1699 * image. When deleting a snapshot file (see ffs_snapremove above), we
1700 * must push any of these claimed blocks to one of the other snapshots
1701 * that maps it. These claimed blocks are easily identified as they will
1702 * have a block number equal to their logical block number within the
1703 * snapshot. A copied block can never have this property because they
1704 * must always have been allocated from a BLK_NOCOPY location.
1707 ffs_snapblkfree(fs, devvp, bno, size, inum)
1709 struct vnode *devvp;
1714 struct buf *ibp, *cbp, *savedcbp = 0;
1715 struct thread *td = curthread;
1717 struct vnode *vp = NULL;
1720 int indiroff = 0, error = 0, claimedblk = 0;
1721 struct snapdata *sn;
1723 lbn = fragstoblks(fs, bno);
1726 sn = devvp->v_rdev->si_snapdata;
1731 if (lockmgr(&sn->sn_lock,
1732 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1733 VI_MTX(devvp), td) != 0)
1735 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1738 * Lookup block being written.
1741 blkno = DIP(ip, i_db[lbn]);
1743 td->td_pflags |= TDP_COWINPROGRESS;
1744 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1745 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1746 td->td_pflags &= ~TDP_COWINPROGRESS;
1749 indiroff = (lbn - NDADDR) % NINDIR(fs);
1750 if (ip->i_ump->um_fstype == UFS1)
1751 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1753 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1756 * Check to see if block needs to be copied.
1760 * A block that we map is being freed. If it has not
1761 * been claimed yet, we will claim or copy it (below).
1764 } else if (blkno == BLK_SNAP) {
1766 * No previous snapshot claimed the block,
1767 * so it will be freed and become a BLK_NOCOPY
1768 * (don't care) for us.
1771 panic("snapblkfree: inconsistent block type");
1773 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1774 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1775 } else if (ip->i_ump->um_fstype == UFS1) {
1776 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1780 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1785 } else /* BLK_NOCOPY or default */ {
1787 * If the snapshot has already copied the block
1788 * (default), or does not care about the block,
1796 * If this is a full size block, we will just grab it
1797 * and assign it to the snapshot inode. Otherwise we
1798 * will proceed to copy it. See explanation for this
1799 * routine as to why only a single snapshot needs to
1802 if (size == fs->fs_bsize) {
1805 printf("%s %d lbn %jd from inum %d\n",
1806 "Grabonremove: snapino", ip->i_number,
1807 (intmax_t)lbn, inum);
1810 DIP_SET(ip, i_db[lbn], bno);
1811 } else if (ip->i_ump->um_fstype == UFS1) {
1812 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1815 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1818 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1819 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1820 lockmgr(vp->v_vnlock, LK_RELEASE, NULL, td);
1826 * Allocate the block into which to do the copy. Note that this
1827 * allocation will never require any additional allocations for
1828 * the snapshot inode.
1830 td->td_pflags |= TDP_COWINPROGRESS;
1831 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1832 fs->fs_bsize, KERNCRED, 0, &cbp);
1833 td->td_pflags &= ~TDP_COWINPROGRESS;
1838 printf("%s%d lbn %jd %s %d size %ld to blkno %jd\n",
1839 "Copyonremove: snapino ", ip->i_number,
1840 (intmax_t)lbn, "for inum", inum, size,
1841 (intmax_t)cbp->b_blkno);
1844 * If we have already read the old block contents, then
1845 * simply copy them to the new block. Note that we need
1846 * to synchronously write snapshots that have not been
1847 * unlinked, and hence will be visible after a crash,
1848 * to ensure their integrity.
1850 if (savedcbp != 0) {
1851 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1853 if (dopersistence && ip->i_effnlink > 0)
1854 (void) ffs_syncvnode(vp, MNT_WAIT);
1858 * Otherwise, read the old block contents into the buffer.
1860 if ((error = readblock(vp, cbp, lbn)) != 0) {
1861 bzero(cbp->b_data, fs->fs_bsize);
1863 if (dopersistence && ip->i_effnlink > 0)
1864 (void) ffs_syncvnode(vp, MNT_WAIT);
1870 * Note that we need to synchronously write snapshots that
1871 * have not been unlinked, and hence will be visible after
1872 * a crash, to ensure their integrity.
1875 vp = savedcbp->b_vp;
1877 if (dopersistence && VTOI(vp)->i_effnlink > 0)
1878 (void) ffs_syncvnode(vp, MNT_WAIT);
1881 * If we have been unable to allocate a block in which to do
1882 * the copy, then return non-zero so that the fragment will
1883 * not be freed. Although space will be lost, the snapshot
1884 * will stay consistent.
1886 lockmgr(vp->v_vnlock, LK_RELEASE, NULL, td);
1891 * Associate snapshot files when mounting.
1894 ffs_snapshot_mount(mp)
1897 struct ufsmount *ump = VFSTOUFS(mp);
1898 struct vnode *devvp = ump->um_devvp;
1899 struct fs *fs = ump->um_fs;
1900 struct thread *td = curthread;
1901 struct snapdata *sn;
1903 struct vnode *lastvp;
1909 daddr_t snaplistsize;
1910 int error, snaploc, loc;
1913 * XXX The following needs to be set before ffs_truncate or
1914 * VOP_READ can be called.
1916 mp->mnt_stat.f_iosize = fs->fs_bsize;
1918 * Process each snapshot listed in the superblock.
1922 sn = devvp->v_rdev->si_snapdata;
1923 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1924 if (fs->fs_snapinum[snaploc] == 0)
1926 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1927 LK_EXCLUSIVE, &vp)) != 0){
1928 printf("ffs_snapshot_mount: vget failed %d\n", error);
1932 if ((ip->i_flags & SF_SNAPSHOT) == 0 || ip->i_size ==
1933 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1934 if ((ip->i_flags & SF_SNAPSHOT) == 0) {
1935 reason = "non-snapshot";
1937 reason = "old format snapshot";
1938 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
1939 (void)ffs_syncvnode(vp, MNT_WAIT);
1941 printf("ffs_snapshot_mount: %s inode %d\n",
1942 reason, fs->fs_snapinum[snaploc]);
1945 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
1946 if (fs->fs_snapinum[loc] == 0)
1948 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
1950 fs->fs_snapinum[loc - 1] = 0;
1955 * If there already exist snapshots on this filesystem, grab a
1956 * reference to their shared lock. If this is the first snapshot
1957 * on this filesystem, we need to allocate a lock for the
1958 * snapshots to share. In either case, acquire the snapshot
1959 * lock and give up our original private lock.
1966 vp->v_vnlock = &sn->sn_lock;
1969 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
1970 TAILQ_INIT(&sn->sn_head);
1971 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
1972 LK_CANRECURSE | LK_NOSHARE);
1974 vp->v_vnlock = &sn->sn_lock;
1975 devvp->v_rdev->si_snapdata = sn;
1977 lockmgr(vp->v_vnlock, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY,
1979 lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
1981 * Link it onto the active snapshot list.
1984 if (ip->i_nextsnap.tqe_prev != 0)
1985 panic("ffs_snapshot_mount: %d already on list",
1988 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
1989 vp->v_vflag |= VV_SYSTEM;
1991 VOP_UNLOCK(vp, 0, td);
1996 * No usable snapshots found.
2001 * Allocate the space for the block hints list. We always want to
2002 * use the list from the newest snapshot.
2004 auio.uio_iov = &aiov;
2005 auio.uio_iovcnt = 1;
2006 aiov.iov_base = (void *)&snaplistsize;
2007 aiov.iov_len = sizeof(snaplistsize);
2008 auio.uio_resid = aiov.iov_len;
2010 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2011 auio.uio_segflg = UIO_SYSSPACE;
2012 auio.uio_rw = UIO_READ;
2014 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2015 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2016 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2017 VOP_UNLOCK(vp, 0, td);
2020 MALLOC(snapblklist, void *, snaplistsize * sizeof(daddr_t),
2021 M_UFSMNT, M_WAITOK);
2022 auio.uio_iovcnt = 1;
2023 aiov.iov_base = snapblklist;
2024 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2025 auio.uio_resid = aiov.iov_len;
2026 auio.uio_offset -= sizeof(snaplistsize);
2027 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2028 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2029 VOP_UNLOCK(vp, 0, td);
2030 FREE(snapblklist, M_UFSMNT);
2033 VOP_UNLOCK(vp, 0, td);
2035 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2036 sn->sn_listsize = snaplistsize;
2037 sn->sn_blklist = (daddr_t *)snapblklist;
2038 devvp->v_vflag |= VV_COPYONWRITE;
2043 * Disassociate snapshot files when unmounting.
2046 ffs_snapshot_unmount(mp)
2049 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2050 struct snapdata *sn;
2053 struct thread *td = curthread;
2056 sn = devvp->v_rdev->si_snapdata;
2057 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2059 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2060 xp->i_nextsnap.tqe_prev = 0;
2061 lockmgr(&sn->sn_lock,
2062 LK_INTERLOCK | LK_EXCLUSIVE,
2066 lockmgr(&vp->v_lock,
2067 LK_INTERLOCK | LK_EXCLUSIVE,
2070 KASSERT(vp->v_vnlock == &sn->sn_lock,
2071 ("ffs_snapshot_unmount: lost lock mutation"));
2072 vp->v_vnlock = &vp->v_lock;
2074 lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
2075 lockmgr(&sn->sn_lock, LK_RELEASE, NULL, td);
2076 if (xp->i_effnlink > 0)
2079 sn = devvp->v_rdev->si_snapdata;
2081 try_free_snapdata(devvp, td);
2082 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2086 * Check the buffer block to be belong to device buffer that shall be
2087 * locked after snaplk. devvp shall be locked on entry, and will be
2088 * leaved locked upon exit.
2091 ffs_bp_snapblk(devvp, bp)
2092 struct vnode *devvp;
2095 struct snapdata *sn;
2097 ufs2_daddr_t lbn, *snapblklist;
2098 int lower, upper, mid;
2100 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2101 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2102 sn = devvp->v_rdev->si_snapdata;
2103 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2105 fs = TAILQ_FIRST(&sn->sn_head)->i_fs;
2106 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2107 snapblklist = sn->sn_blklist;
2108 upper = sn->sn_listsize - 1;
2110 while (lower <= upper) {
2111 mid = (lower + upper) / 2;
2112 if (snapblklist[mid] == lbn)
2114 if (snapblklist[mid] < lbn)
2130 struct vnode *vp, *devvp;
2134 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2139 devvp = bo->__bo_vnode;
2140 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2143 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2147 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2148 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2153 * Try to find a buffer to flush.
2155 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2156 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2158 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2161 panic("bdwrite: found ourselves");
2164 * Don't countdeps with the bo lock
2167 if (buf_countdeps(nbp, 0)) {
2174 if (!ffs_bp_snapblk(vp, nbp)) {
2175 if (BO_MTX(bo) != VI_MTX(vp)) {
2184 if (nbp->b_flags & B_CLUSTEROK) {
2185 vfs_bio_awrite(nbp);
2190 dirtybufferflushes++;
2199 * Check for need to copy block that is about to be written,
2200 * copying the block if necessary.
2203 ffs_copyonwrite(devvp, bp)
2204 struct vnode *devvp;
2207 struct snapdata *sn;
2208 struct buf *ibp, *cbp, *savedcbp = 0;
2209 struct thread *td = curthread;
2212 struct vnode *vp = 0;
2213 ufs2_daddr_t lbn, blkno, *snapblklist;
2214 int lower, upper, mid, indiroff, error = 0;
2215 int launched_async_io, prev_norunningbuf;
2216 long saved_runningbufspace;
2218 if (devvp != bp->b_vp && (VTOI(bp->b_vp)->i_flags & SF_SNAPSHOT) != 0)
2219 return (0); /* Update on a snapshot file */
2220 if (td->td_pflags & TDP_COWINPROGRESS)
2221 panic("ffs_copyonwrite: recursive call");
2223 * First check to see if it is in the preallocated list.
2224 * By doing this check we avoid several potential deadlocks.
2227 sn = devvp->v_rdev->si_snapdata;
2229 TAILQ_EMPTY(&sn->sn_head)) {
2231 return (0); /* No snapshot */
2233 ip = TAILQ_FIRST(&sn->sn_head);
2235 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2236 snapblklist = sn->sn_blklist;
2237 upper = sn->sn_listsize - 1;
2239 while (lower <= upper) {
2240 mid = (lower + upper) / 2;
2241 if (snapblklist[mid] == lbn)
2243 if (snapblklist[mid] < lbn)
2248 if (lower <= upper) {
2252 launched_async_io = 0;
2253 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2255 * Since I/O on bp isn't yet in progress and it may be blocked
2256 * for a long time waiting on snaplk, back it out of
2257 * runningbufspace, possibly waking other threads waiting for space.
2259 saved_runningbufspace = bp->b_runningbufspace;
2260 if (saved_runningbufspace != 0)
2261 runningbufwakeup(bp);
2263 * Not in the precomputed list, so check the snapshots.
2265 while (lockmgr(&sn->sn_lock,
2266 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2267 VI_MTX(devvp), td) != 0) {
2269 sn = devvp->v_rdev->si_snapdata;
2271 TAILQ_EMPTY(&sn->sn_head)) {
2273 if (saved_runningbufspace != 0) {
2274 bp->b_runningbufspace = saved_runningbufspace;
2275 atomic_add_int(&runningbufspace,
2276 bp->b_runningbufspace);
2278 return (0); /* Snapshot gone */
2281 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2284 * We ensure that everything of our own that needs to be
2285 * copied will be done at the time that ffs_snapshot is
2286 * called. Thus we can skip the check here which can
2287 * deadlock in doing the lookup in UFS_BALLOC.
2292 * Check to see if block needs to be copied. We do not have
2293 * to hold the snapshot lock while doing this lookup as it
2294 * will never require any additional allocations for the
2298 blkno = DIP(ip, i_db[lbn]);
2300 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2301 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2302 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2303 td->td_pflags &= ~TDP_COWINPROGRESS;
2306 indiroff = (lbn - NDADDR) % NINDIR(fs);
2307 if (ip->i_ump->um_fstype == UFS1)
2308 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2310 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2314 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2315 panic("ffs_copyonwrite: bad copy block");
2320 * Allocate the block into which to do the copy. Since
2321 * multiple processes may all try to copy the same block,
2322 * we have to recheck our need to do a copy if we sleep
2323 * waiting for the lock.
2325 * Because all snapshots on a filesystem share a single
2326 * lock, we ensure that we will never be in competition
2327 * with another process to allocate a block.
2329 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2330 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2331 fs->fs_bsize, KERNCRED, 0, &cbp);
2332 td->td_pflags &= ~TDP_COWINPROGRESS;
2337 printf("Copyonwrite: snapino %d lbn %jd for ",
2338 ip->i_number, (intmax_t)lbn);
2339 if (bp->b_vp == devvp)
2340 printf("fs metadata");
2342 printf("inum %d", VTOI(bp->b_vp)->i_number);
2343 printf(" lblkno %jd to blkno %jd\n",
2344 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2348 * If we have already read the old block contents, then
2349 * simply copy them to the new block. Note that we need
2350 * to synchronously write snapshots that have not been
2351 * unlinked, and hence will be visible after a crash,
2352 * to ensure their integrity.
2354 if (savedcbp != 0) {
2355 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2357 if (dopersistence && ip->i_effnlink > 0)
2358 (void) ffs_syncvnode(vp, MNT_WAIT);
2360 launched_async_io = 1;
2364 * Otherwise, read the old block contents into the buffer.
2366 if ((error = readblock(vp, cbp, lbn)) != 0) {
2367 bzero(cbp->b_data, fs->fs_bsize);
2369 if (dopersistence && ip->i_effnlink > 0)
2370 (void) ffs_syncvnode(vp, MNT_WAIT);
2372 launched_async_io = 1;
2378 * Note that we need to synchronously write snapshots that
2379 * have not been unlinked, and hence will be visible after
2380 * a crash, to ensure their integrity.
2383 vp = savedcbp->b_vp;
2385 if (dopersistence && VTOI(vp)->i_effnlink > 0)
2386 (void) ffs_syncvnode(vp, MNT_WAIT);
2388 launched_async_io = 1;
2390 lockmgr(vp->v_vnlock, LK_RELEASE, NULL, td);
2391 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2393 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2394 waitrunningbufspace();
2396 * I/O on bp will now be started, so count it in runningbufspace.
2398 if (saved_runningbufspace != 0) {
2399 bp->b_runningbufspace = saved_runningbufspace;
2400 atomic_add_int(&runningbufspace, bp->b_runningbufspace);
2406 * Read the specified block into the given buffer.
2407 * Much of this boiler-plate comes from bwrite().
2410 readblock(vp, bp, lbn)
2415 struct inode *ip = VTOI(vp);
2418 bip = g_alloc_bio();
2419 bip->bio_cmd = BIO_READ;
2420 bip->bio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
2421 bip->bio_data = bp->b_data;
2422 bip->bio_length = bp->b_bcount;
2423 bip->bio_done = NULL;
2425 g_io_request(bip, ip->i_devvp->v_bufobj.bo_private);
2426 bp->b_error = biowait(bip, "snaprdb");
2428 return (bp->b_error);
2434 * Process file deletes that were deferred by ufs_inactive() due to
2435 * the file system being suspended. Transfer IN_LAZYACCESS into
2436 * IN_MODIFIED for vnodes that were accessed during suspension.
2439 process_deferred_inactive(struct mount *mp)
2441 struct vnode *vp, *mvp;
2447 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2450 MNT_VNODE_FOREACH(vp, mp, mvp) {
2453 * IN_LAZYACCESS is checked here without holding any
2454 * vnode lock, but this flag is set only while holding
2457 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED) != 0 ||
2458 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2459 ((vp->v_iflag & VI_OWEINACT) == 0 ||
2460 vp->v_usecount > 0))) {
2466 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
2470 if (error == ENOENT)
2471 continue; /* vnode recycled */
2472 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2476 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2477 ip->i_flag &= ~IN_LAZYACCESS;
2478 ip->i_flag |= IN_MODIFIED;
2481 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2483 VOP_UNLOCK(vp, 0, td);
2489 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2490 ("process_deferred_inactive: "
2491 "recursed on VI_DOINGINACT"));
2492 vp->v_iflag |= VI_DOINGINACT;
2493 vp->v_iflag &= ~VI_OWEINACT;
2495 (void) VOP_INACTIVE(vp, td);
2497 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2498 ("process_deferred_inactive: lost VI_DOINGINACT"));
2499 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2500 ("process_deferred_inactive: got VI_OWEINACT"));
2501 vp->v_iflag &= ~VI_DOINGINACT;
2503 VOP_UNLOCK(vp, 0, td);
2508 vn_finished_secondary_write(mp);
2511 #ifndef NO_FFS_SNAPSHOT
2513 /* Try to free snapdata associated with devvp */
2515 try_free_snapdata(struct vnode *devvp,
2518 struct snapdata *sn;
2519 ufs2_daddr_t *snapblklist;
2521 sn = devvp->v_rdev->si_snapdata;
2523 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2524 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2529 devvp->v_rdev->si_snapdata = NULL;
2530 devvp->v_vflag &= ~VV_COPYONWRITE;
2531 snapblklist = sn->sn_blklist;
2532 sn->sn_blklist = NULL;
2533 sn->sn_listsize = 0;
2534 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp), td);
2535 lockmgr(&sn->sn_lock, LK_RELEASE, NULL, td);
2536 lockdestroy(&sn->sn_lock);
2538 if (snapblklist != NULL)
2539 FREE(snapblklist, M_UFSMNT);