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>
47 #include <sys/fcntl.h>
49 #include <sys/namei.h>
50 #include <sys/sched.h>
52 #include <sys/malloc.h>
53 #include <sys/mount.h>
54 #include <sys/resource.h>
55 #include <sys/resourcevar.h>
56 #include <sys/vnode.h>
58 #include <geom/geom.h>
60 #include <ufs/ufs/extattr.h>
61 #include <ufs/ufs/quota.h>
62 #include <ufs/ufs/ufsmount.h>
63 #include <ufs/ufs/inode.h>
64 #include <ufs/ufs/ufs_extern.h>
66 #include <ufs/ffs/fs.h>
67 #include <ufs/ffs/ffs_extern.h>
69 #define KERNCRED thread0.td_ucred
74 #ifdef NO_FFS_SNAPSHOT
76 ffs_snapshot(mp, snapfile)
84 ffs_snapblkfree(fs, devvp, bno, size, inum)
101 ffs_snapshot_mount(mp)
107 ffs_snapshot_unmount(mp)
119 ffs_copyonwrite(devvp, bp)
128 TAILQ_HEAD(snaphead, inode);
131 LIST_ENTRY(snapdata) sn_link;
132 struct snaphead sn_head;
138 LIST_HEAD(, snapdata) snapfree;
139 static struct mtx snapfree_lock;
140 MTX_SYSINIT(ffs_snapfree, &snapfree_lock, "snapdata free list", MTX_DEF);
142 static int cgaccount(int, struct vnode *, struct buf *, int);
143 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
144 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
145 ufs_lbn_t, int), int);
146 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
147 ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
148 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
149 ufs_lbn_t, int), int);
150 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
151 struct fs *, ufs_lbn_t, int);
152 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
153 struct fs *, ufs_lbn_t, int);
154 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
155 struct fs *, ufs_lbn_t, int);
156 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
157 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
158 ufs_lbn_t, int), int);
159 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
160 ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
161 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
162 ufs_lbn_t, int), int);
163 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
164 struct fs *, ufs_lbn_t, int);
165 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
166 struct fs *, ufs_lbn_t, int);
167 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
168 struct fs *, ufs_lbn_t, int);
169 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
170 static void try_free_snapdata(struct vnode *devvp);
171 static struct snapdata *ffs_snapdata_acquire(struct vnode *devvp);
172 static int ffs_bp_snapblk(struct vnode *, struct buf *);
175 * To ensure the consistency of snapshots across crashes, we must
176 * synchronously write out copied blocks before allowing the
177 * originals to be modified. Because of the rather severe speed
178 * penalty that this imposes, the following flag allows this
179 * crash persistence to be disabled.
181 int dopersistence = 0;
184 #include <sys/sysctl.h>
185 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
186 static int snapdebug = 0;
187 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
188 int collectsnapstats = 0;
189 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
194 * Create a snapshot file and initialize it for the filesystem.
197 ffs_snapshot(mp, snapfile)
201 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
202 int error, cg, snaploc;
203 int i, size, len, loc;
205 struct timespec starttime = {0, 0}, endtime;
207 long redo = 0, snaplistsize = 0;
210 struct fs *copy_fs = NULL, *fs;
211 struct thread *td = curthread;
212 struct inode *ip, *xp;
213 struct buf *bp, *nbp, *ibp, *sbp = NULL;
217 struct vnode *vp, *xvp, *mvp, *devvp;
221 struct ufsmount *ump;
231 * Need to serialize access to snapshot code per filesystem.
234 * Assign a snapshot slot in the superblock.
237 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
238 if (fs->fs_snapinum[snaploc] == 0)
241 if (snaploc == FSMAXSNAP)
244 * Create the snapshot file.
247 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE, snapfile, td);
248 if ((error = namei(&nd)) != 0)
250 if (nd.ni_vp != NULL) {
254 if (nd.ni_dvp->v_mount != mp)
257 NDFREE(&nd, NDF_ONLY_PNBUF);
258 if (nd.ni_dvp == nd.ni_vp)
266 vat.va_mode = S_IRUSR;
267 vat.va_vaflags |= VA_EXCLUSIVE;
268 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
271 panic("ffs_snapshot: mount mismatch");
273 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
274 NDFREE(&nd, NDF_ONLY_PNBUF);
276 if ((error = vn_start_write(NULL, &wrtmp,
277 V_XSLEEP | PCATCH)) != 0)
281 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
282 VOP_UNLOCK(nd.ni_dvp, 0);
284 NDFREE(&nd, NDF_ONLY_PNBUF);
285 vn_finished_write(wrtmp);
290 vp->v_vflag |= VV_SYSTEM;
294 * Allocate and copy the last block contents so as to be able
295 * to set size to that of the filesystem.
297 numblks = howmany(fs->fs_size, fs->fs_frag);
298 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
299 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
302 ip->i_size = lblktosize(fs, (off_t)numblks);
303 DIP_SET(ip, i_size, ip->i_size);
304 ip->i_flag |= IN_CHANGE | IN_UPDATE;
305 error = readblock(vp, bp, numblks - 1);
310 * Preallocate critical data structures so that we can copy
311 * them in without further allocation after we suspend all
312 * operations on the filesystem. We would like to just release
313 * the allocated buffers without writing them since they will
314 * be filled in below once we are ready to go, but this upsets
315 * the soft update code, so we go ahead and write the new buffers.
317 * Allocate all indirect blocks and mark all of them as not
318 * needing to be copied.
320 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
321 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
322 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
328 * Allocate copies for the superblock and its summary information.
330 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
335 blkno = fragstoblks(fs, fs->fs_csaddr);
336 len = howmany(fs->fs_cssize, fs->fs_bsize);
337 for (loc = 0; loc < len; loc++) {
338 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
339 fs->fs_bsize, KERNCRED, 0, &nbp);
345 * Allocate all cylinder group blocks.
347 for (cg = 0; cg < fs->fs_ncg; cg++) {
348 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
349 fs->fs_bsize, KERNCRED, 0, &nbp);
354 ffs_syncvnode(vp, MNT_WAIT);
357 * Copy all the cylinder group maps. Although the
358 * filesystem is still active, we hope that only a few
359 * cylinder groups will change between now and when we
360 * suspend operations. Thus, we will be able to quickly
361 * touch up the few cylinder groups that changed during
362 * the suspension period.
364 len = howmany(fs->fs_ncg, NBBY);
365 space = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
367 fs->fs_active = space;
369 for (cg = 0; cg < fs->fs_ncg; cg++) {
370 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
371 fs->fs_bsize, KERNCRED, 0, &nbp);
374 error = cgaccount(cg, vp, nbp, 1);
377 ffs_syncvnode(vp, MNT_WAIT);
382 * Change inode to snapshot type file.
384 ip->i_flags |= SF_SNAPSHOT;
385 DIP_SET(ip, i_flags, ip->i_flags);
386 ip->i_flag |= IN_CHANGE | IN_UPDATE;
388 * Ensure that the snapshot is completely on disk.
389 * Since we have marked it as a snapshot it is safe to
390 * unlock it as no process will be allowed to write to it.
392 if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
396 * All allocations are done, so we can now snapshot the system.
398 * Recind nice scheduling while running with the filesystem suspended.
400 if (td->td_proc->p_nice > 0) {
405 saved_nice = p->p_nice;
410 * Suspend operation on filesystem.
413 vn_finished_write(wrtmp);
414 if ((error = vfs_write_suspend(vp->v_mount)) != 0) {
415 vn_start_write(NULL, &wrtmp, V_WAIT);
416 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
419 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
421 vn_start_write(NULL, &wrtmp, V_WAIT);
423 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
424 if (ip->i_effnlink == 0) {
425 error = ENOENT; /* Snapshot file unlinked */
428 if (collectsnapstats)
429 nanotime(&starttime);
431 /* The last block might have changed. Copy it again to be sure. */
432 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
433 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
436 error = readblock(vp, bp, numblks - 1);
437 bp->b_flags |= B_VALIDSUSPWRT;
442 * First, copy all the cylinder group maps that have changed.
444 for (cg = 0; cg < fs->fs_ncg; cg++) {
445 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
448 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
449 fs->fs_bsize, KERNCRED, 0, &nbp);
452 error = cgaccount(cg, vp, nbp, 2);
458 * Grab a copy of the superblock and its summary information.
459 * We delay writing it until the suspension is released below.
461 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
468 loc = blkoff(fs, fs->fs_sblockloc);
469 copy_fs = (struct fs *)(sbp->b_data + loc);
470 bcopy(fs, copy_fs, fs->fs_sbsize);
471 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
472 copy_fs->fs_clean = 1;
473 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
474 if (fs->fs_sbsize < size)
475 bzero(&sbp->b_data[loc + fs->fs_sbsize], size - fs->fs_sbsize);
476 size = blkroundup(fs, fs->fs_cssize);
477 if (fs->fs_contigsumsize > 0)
478 size += fs->fs_ncg * sizeof(int32_t);
479 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
480 copy_fs->fs_csp = space;
481 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
482 space = (char *)space + fs->fs_cssize;
483 loc = howmany(fs->fs_cssize, fs->fs_fsize);
484 i = fs->fs_frag - loc % fs->fs_frag;
485 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
487 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
488 len, KERNCRED, &bp)) != 0) {
490 free(copy_fs->fs_csp, M_UFSMNT);
495 bcopy(bp->b_data, space, (u_int)len);
496 space = (char *)space + len;
497 bp->b_flags |= B_INVAL | B_NOCACHE;
500 if (fs->fs_contigsumsize > 0) {
501 copy_fs->fs_maxcluster = lp = space;
502 for (i = 0; i < fs->fs_ncg; i++)
503 *lp++ = fs->fs_contigsumsize;
506 * We must check for active files that have been unlinked
507 * (e.g., with a zero link count). We have to expunge all
508 * trace of these files from the snapshot so that they are
509 * not reclaimed prematurely by fsck or unnecessarily dumped.
510 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
511 * spec_strategy about writing on a suspended filesystem.
512 * Note that we skip unlinked snapshot files as they will
513 * be handled separately below.
515 * We also calculate the needed size for the snapshot list.
517 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
518 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
520 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
522 MNT_VNODE_FOREACH(xvp, mp, mvp) {
525 if ((xvp->v_iflag & VI_DOOMED) ||
526 (xvp->v_usecount == 0 &&
527 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
528 xvp->v_type == VNON ||
529 (VTOI(xvp)->i_flags & SF_SNAPSHOT)) {
535 * We can skip parent directory vnode because it must have
536 * this snapshot file in it.
538 if (xvp == nd.ni_dvp) {
544 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
546 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
551 if (xvp->v_usecount == 0 &&
552 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
561 vprint("ffs_snapshot: busy vnode", xvp);
562 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
570 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
577 * If there is a fragment, clear it here.
580 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
582 len = fragroundup(fs, blkoff(fs, xp->i_size));
583 if (len != 0 && len < fs->fs_bsize) {
584 ffs_blkfree(ump, copy_fs, vp,
585 DIP(xp, i_db[loc]), len, xp->i_number);
586 blkno = DIP(xp, i_db[loc]);
587 DIP_SET(xp, i_db[loc], 0);
591 if (xp->i_ump->um_fstype == UFS1)
592 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
595 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
598 DIP_SET(xp, i_db[loc], blkno);
600 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
605 free(copy_fs->fs_csp, M_UFSMNT);
608 MNT_VNODE_FOREACH_ABORT(mp, mvp);
615 * Acquire a lock on the snapdata structure, creating it if necessary.
617 sn = ffs_snapdata_acquire(devvp);
619 * Change vnode to use shared snapshot lock instead of the original
622 vp->v_vnlock = &sn->sn_lock;
623 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
624 xp = TAILQ_FIRST(&sn->sn_head);
626 * If this is the first snapshot on this filesystem, then we need
627 * to allocate the space for the list of preallocated snapshot blocks.
628 * This list will be refined below, but this preliminary one will
629 * keep us out of deadlock until the full one is ready.
632 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
634 blkp = &snapblklist[1];
635 *blkp++ = lblkno(fs, fs->fs_sblockloc);
636 blkno = fragstoblks(fs, fs->fs_csaddr);
637 for (cg = 0; cg < fs->fs_ncg; cg++) {
638 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
640 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
642 len = howmany(fs->fs_cssize, fs->fs_bsize);
643 for (loc = 0; loc < len; loc++)
644 *blkp++ = blkno + loc;
645 for (; cg < fs->fs_ncg; cg++)
646 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
647 snapblklist[0] = blkp - snapblklist;
649 if (sn->sn_blklist != NULL)
650 panic("ffs_snapshot: non-empty list");
651 sn->sn_blklist = snapblklist;
652 sn->sn_listsize = blkp - snapblklist;
656 * Record snapshot inode. Since this is the newest snapshot,
657 * it must be placed at the end of the list.
660 fs->fs_snapinum[snaploc] = ip->i_number;
661 if (ip->i_nextsnap.tqe_prev != 0)
662 panic("ffs_snapshot: %d already on list", ip->i_number);
663 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
664 devvp->v_vflag |= VV_COPYONWRITE;
666 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
668 KASSERT((sn != NULL && sbp != NULL && error == 0) ||
669 (sn == NULL && sbp == NULL && error != 0),
670 ("email phk@ and mckusick@"));
672 * Resume operation on filesystem.
674 vfs_write_resume(vp->v_mount);
675 vn_start_write(NULL, &wrtmp, V_WAIT);
676 if (collectsnapstats && starttime.tv_sec > 0) {
678 timespecsub(&endtime, &starttime);
679 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
680 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
681 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
686 * Copy allocation information from all the snapshots in
687 * this snapshot and then expunge them from its view.
689 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
692 if (xp->i_ump->um_fstype == UFS1)
693 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
696 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
698 if (error == 0 && xp->i_effnlink == 0) {
699 error = ffs_freefile(ump,
706 fs->fs_snapinum[snaploc] = 0;
711 * Allocate space for the full list of preallocated snapshot blocks.
713 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
715 ip->i_snapblklist = &snapblklist[1];
717 * Expunge the blocks used by the snapshots from the set of
718 * blocks marked as used in the snapshot bitmaps. Also, collect
719 * the list of allocated blocks in i_snapblklist.
721 if (ip->i_ump->um_fstype == UFS1)
722 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, BLK_SNAP);
724 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, BLK_SNAP);
726 fs->fs_snapinum[snaploc] = 0;
727 free(snapblklist, M_UFSMNT);
730 if (snaplistsize < ip->i_snapblklist - snapblklist)
731 panic("ffs_snapshot: list too small");
732 snaplistsize = ip->i_snapblklist - snapblklist;
733 snapblklist[0] = snaplistsize;
734 ip->i_snapblklist = 0;
736 * Write out the list of allocated blocks to the end of the snapshot.
738 auio.uio_iov = &aiov;
740 aiov.iov_base = (void *)snapblklist;
741 aiov.iov_len = snaplistsize * sizeof(daddr_t);
742 auio.uio_resid = aiov.iov_len;
743 auio.uio_offset = ip->i_size;
744 auio.uio_segflg = UIO_SYSSPACE;
745 auio.uio_rw = UIO_WRITE;
747 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
748 fs->fs_snapinum[snaploc] = 0;
749 free(snapblklist, M_UFSMNT);
753 * Write the superblock and its summary information
756 blkno = fragstoblks(fs, fs->fs_csaddr);
757 len = howmany(fs->fs_cssize, fs->fs_bsize);
758 space = copy_fs->fs_csp;
759 for (loc = 0; loc < len; loc++) {
760 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
763 fs->fs_snapinum[snaploc] = 0;
764 free(snapblklist, M_UFSMNT);
767 bcopy(space, nbp->b_data, fs->fs_bsize);
768 space = (char *)space + fs->fs_bsize;
772 * As this is the newest list, it is the most inclusive, so
773 * should replace the previous list.
776 space = sn->sn_blklist;
777 sn->sn_blklist = snapblklist;
778 sn->sn_listsize = snaplistsize;
781 free(space, M_UFSMNT);
783 * If another process is currently writing the buffer containing
784 * the inode for this snapshot then a deadlock can occur. Drop
785 * the snapshot lock until the buffer has been written.
787 VREF(vp); /* Protect against ffs_snapgone() */
789 (void) bread(ip->i_devvp,
790 fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
791 (int) fs->fs_bsize, NOCRED, &nbp);
793 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
794 if (ip->i_effnlink == 0)
795 error = ENOENT; /* Snapshot file unlinked */
797 vrele(vp); /* Drop extra reference */
799 free(copy_fs->fs_csp, M_UFSMNT);
802 NDFREE(&nd, NDF_ONLY_PNBUF);
803 if (saved_nice > 0) {
808 sched_nice(td->td_proc, saved_nice);
809 PROC_UNLOCK(td->td_proc);
812 if (fs->fs_active != 0) {
813 free(fs->fs_active, M_DEVBUF);
818 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
821 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
822 (void) ffs_syncvnode(vp, MNT_WAIT);
828 vn_finished_write(wrtmp);
829 process_deferred_inactive(mp);
834 * Copy a cylinder group map. All the unallocated blocks are marked
835 * BLK_NOCOPY so that the snapshot knows that it need not copy them
836 * if they are later written. If passno is one, then this is a first
837 * pass, so only setting needs to be done. If passno is 2, then this
838 * is a revision to a previous pass which must be undone as the
839 * replacement pass is done.
842 cgaccount(cg, vp, nbp, passno)
848 struct buf *bp, *ibp;
852 ufs2_daddr_t base, numblks;
853 int error, len, loc, indiroff;
857 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
858 (int)fs->fs_cgsize, KERNCRED, &bp);
863 cgp = (struct cg *)bp->b_data;
864 if (!cg_chkmagic(cgp)) {
871 * Recomputation of summary information might not have been performed
872 * at mount time. Sync up summary information for current cylinder
873 * group while data is in memory to ensure that result of background
874 * fsck is slightly more consistent.
876 fs->fs_cs(fs, cg) = cgp->cg_cs;
877 UFS_UNLOCK(ip->i_ump);
878 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
879 if (fs->fs_cgsize < fs->fs_bsize)
880 bzero(&nbp->b_data[fs->fs_cgsize],
881 fs->fs_bsize - fs->fs_cgsize);
882 cgp = (struct cg *)nbp->b_data;
885 nbp->b_flags |= B_VALIDSUSPWRT;
886 numblks = howmany(fs->fs_size, fs->fs_frag);
887 len = howmany(fs->fs_fpg, fs->fs_frag);
888 base = cgbase(fs, cg) / fs->fs_frag;
889 if (base + len >= numblks)
890 len = numblks - base - 1;
893 for ( ; loc < NDADDR; loc++) {
894 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
895 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
896 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
897 DIP_SET(ip, i_db[loc], 0);
898 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
899 panic("ffs_snapshot: lost direct block");
902 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
903 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
907 indiroff = (base + loc - NDADDR) % NINDIR(fs);
908 for ( ; loc < len; loc++, indiroff++) {
909 if (indiroff >= NINDIR(fs)) {
911 ibp->b_flags |= B_VALIDSUSPWRT;
913 error = UFS_BALLOC(vp,
914 lblktosize(fs, (off_t)(base + loc)),
915 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
921 if (ip->i_ump->um_fstype == UFS1) {
922 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
923 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
925 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
926 [indiroff] == BLK_NOCOPY)
927 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
928 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
929 [indiroff] == BLK_NOCOPY)
930 panic("ffs_snapshot: lost indirect block");
933 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
934 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
935 else if (passno == 2 &&
936 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
937 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
938 else if (passno == 1 &&
939 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
940 panic("ffs_snapshot: lost indirect block");
943 ibp->b_flags |= B_VALIDSUSPWRT;
949 * Before expunging a snapshot inode, note all the
950 * blocks that it claims with BLK_SNAP so that fsck will
951 * be able to account for those blocks properly and so
952 * that this snapshot knows that it need not copy them
953 * if the other snapshot holding them is freed. This code
954 * is reproduced once each for UFS1 and UFS2.
957 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype)
958 struct vnode *snapvp;
959 struct inode *cancelip;
961 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
962 struct fs *, ufs_lbn_t, int);
965 int i, error, indiroff;
967 ufs2_daddr_t len, blkno, numblks, blksperindir;
968 struct ufs1_dinode *dip;
969 struct thread *td = curthread;
973 * Prepare to expunge the inode. If its inode block has not
974 * yet been copied, then allocate and fill the copy.
976 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
979 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
981 td->td_pflags |= TDP_COWINPROGRESS;
982 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
983 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
984 td->td_pflags &= ~TDP_COWINPROGRESS;
987 indiroff = (lbn - NDADDR) % NINDIR(fs);
988 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
992 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
995 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
996 fs->fs_bsize, KERNCRED, 0, &bp);
999 if ((error = readblock(snapvp, bp, lbn)) != 0)
1003 * Set a snapshot inode to be a zero length file, regular files
1004 * or unlinked snapshots to be completely unallocated.
1006 dip = (struct ufs1_dinode *)bp->b_data +
1007 ino_to_fsbo(fs, cancelip->i_number);
1008 if (expungetype == BLK_NOCOPY || cancelip->i_effnlink == 0)
1012 dip->di_flags &= ~SF_SNAPSHOT;
1013 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1016 * Now go through and expunge all the blocks in the file
1017 * using the function requested.
1019 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1020 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1021 &cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
1023 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1024 &cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
1028 len = numblks - NDADDR;
1030 for (i = 0; len > 0 && i < NIADDR; i++) {
1031 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1032 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1033 blksperindir, fs, acctfunc, expungetype);
1036 blksperindir *= NINDIR(fs);
1037 lbn -= blksperindir + 1;
1038 len -= blksperindir;
1039 rlbn += blksperindir;
1045 * Descend an indirect block chain for vnode cancelvp accounting for all
1046 * its indirect blocks in snapvp.
1049 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1050 blksperindir, fs, acctfunc, expungetype)
1051 struct vnode *snapvp;
1052 struct vnode *cancelvp;
1058 ufs_lbn_t blksperindir;
1060 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1061 struct fs *, ufs_lbn_t, int);
1065 ufs_lbn_t subblksperindir;
1066 struct indir indirs[NIADDR + 2];
1067 ufs1_daddr_t last, *bap;
1071 if (expungetype == BLK_NOCOPY)
1073 panic("indiracct_ufs1: missing indir");
1075 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1077 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1078 panic("indiracct_ufs1: botched params");
1080 * We have to expand bread here since it will deadlock looking
1081 * up the block number for any blocks that are not in the cache.
1083 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1084 bp->b_blkno = fsbtodb(fs, blkno);
1085 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1086 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1091 * Account for the block pointers in this indirect block.
1093 last = howmany(remblks, blksperindir);
1094 if (last > NINDIR(fs))
1096 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1097 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1099 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1100 level == 0 ? rlbn : -1, expungetype);
1101 if (error || level == 0)
1104 * Account for the block pointers in each of the indirect blocks
1105 * in the levels below us.
1107 subblksperindir = blksperindir / NINDIR(fs);
1108 for (lbn++, level--, i = 0; i < last; i++) {
1109 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1110 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1113 rlbn += blksperindir;
1114 lbn -= blksperindir;
1115 remblks -= blksperindir;
1118 free(bap, M_DEVBUF);
1123 * Do both snap accounting and map accounting.
1126 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1128 ufs1_daddr_t *oldblkp, *lastblkp;
1131 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1135 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1137 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1141 * Identify a set of blocks allocated in a snapshot inode.
1144 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1146 ufs1_daddr_t *oldblkp, *lastblkp;
1149 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1151 struct inode *ip = VTOI(vp);
1152 ufs1_daddr_t blkno, *blkp;
1157 for ( ; oldblkp < lastblkp; oldblkp++) {
1159 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1161 lbn = fragstoblks(fs, blkno);
1163 blkp = &ip->i_din1->di_db[lbn];
1164 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1166 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1167 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1170 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1171 [(lbn - NDADDR) % NINDIR(fs)];
1174 * If we are expunging a snapshot vnode and we
1175 * find a block marked BLK_NOCOPY, then it is
1176 * one that has been allocated to this snapshot after
1177 * we took our current snapshot and can be ignored.
1179 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1184 panic("snapacct_ufs1: bad block");
1185 *blkp = expungetype;
1194 * Account for a set of blocks allocated in a snapshot inode.
1197 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1199 ufs1_daddr_t *oldblkp, *lastblkp;
1210 inum = ip->i_number;
1215 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1217 if (blkno == 0 || blkno == BLK_NOCOPY)
1219 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1220 *ip->i_snapblklist++ = lblkno;
1221 if (blkno == BLK_SNAP)
1222 blkno = blkstofrags(fs, lblkno);
1223 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1229 * Before expunging a snapshot inode, note all the
1230 * blocks that it claims with BLK_SNAP so that fsck will
1231 * be able to account for those blocks properly and so
1232 * that this snapshot knows that it need not copy them
1233 * if the other snapshot holding them is freed. This code
1234 * is reproduced once each for UFS1 and UFS2.
1237 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype)
1238 struct vnode *snapvp;
1239 struct inode *cancelip;
1241 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1242 struct fs *, ufs_lbn_t, int);
1245 int i, error, indiroff;
1246 ufs_lbn_t lbn, rlbn;
1247 ufs2_daddr_t len, blkno, numblks, blksperindir;
1248 struct ufs2_dinode *dip;
1249 struct thread *td = curthread;
1253 * Prepare to expunge the inode. If its inode block has not
1254 * yet been copied, then allocate and fill the copy.
1256 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1259 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1261 td->td_pflags |= TDP_COWINPROGRESS;
1262 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1263 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1264 td->td_pflags &= ~TDP_COWINPROGRESS;
1267 indiroff = (lbn - NDADDR) % NINDIR(fs);
1268 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1272 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1275 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1276 fs->fs_bsize, KERNCRED, 0, &bp);
1279 if ((error = readblock(snapvp, bp, lbn)) != 0)
1283 * Set a snapshot inode to be a zero length file, regular files
1284 * to be completely unallocated.
1286 dip = (struct ufs2_dinode *)bp->b_data +
1287 ino_to_fsbo(fs, cancelip->i_number);
1288 if (expungetype == BLK_NOCOPY)
1292 dip->di_flags &= ~SF_SNAPSHOT;
1293 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1296 * Now go through and expunge all the blocks in the file
1297 * using the function requested.
1299 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1300 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1301 &cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
1303 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1304 &cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
1308 len = numblks - NDADDR;
1310 for (i = 0; len > 0 && i < NIADDR; i++) {
1311 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1312 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1313 blksperindir, fs, acctfunc, expungetype);
1316 blksperindir *= NINDIR(fs);
1317 lbn -= blksperindir + 1;
1318 len -= blksperindir;
1319 rlbn += blksperindir;
1325 * Descend an indirect block chain for vnode cancelvp accounting for all
1326 * its indirect blocks in snapvp.
1329 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1330 blksperindir, fs, acctfunc, expungetype)
1331 struct vnode *snapvp;
1332 struct vnode *cancelvp;
1338 ufs_lbn_t blksperindir;
1340 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1341 struct fs *, ufs_lbn_t, int);
1345 ufs_lbn_t subblksperindir;
1346 struct indir indirs[NIADDR + 2];
1347 ufs2_daddr_t last, *bap;
1351 if (expungetype == BLK_NOCOPY)
1353 panic("indiracct_ufs2: missing indir");
1355 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1357 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1358 panic("indiracct_ufs2: botched params");
1360 * We have to expand bread here since it will deadlock looking
1361 * up the block number for any blocks that are not in the cache.
1363 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1364 bp->b_blkno = fsbtodb(fs, blkno);
1365 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1366 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1371 * Account for the block pointers in this indirect block.
1373 last = howmany(remblks, blksperindir);
1374 if (last > NINDIR(fs))
1376 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1377 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1379 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1380 level == 0 ? rlbn : -1, expungetype);
1381 if (error || level == 0)
1384 * Account for the block pointers in each of the indirect blocks
1385 * in the levels below us.
1387 subblksperindir = blksperindir / NINDIR(fs);
1388 for (lbn++, level--, i = 0; i < last; i++) {
1389 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1390 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1393 rlbn += blksperindir;
1394 lbn -= blksperindir;
1395 remblks -= blksperindir;
1398 free(bap, M_DEVBUF);
1403 * Do both snap accounting and map accounting.
1406 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1408 ufs2_daddr_t *oldblkp, *lastblkp;
1411 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1415 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1417 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1421 * Identify a set of blocks allocated in a snapshot inode.
1424 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1426 ufs2_daddr_t *oldblkp, *lastblkp;
1429 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1431 struct inode *ip = VTOI(vp);
1432 ufs2_daddr_t blkno, *blkp;
1437 for ( ; oldblkp < lastblkp; oldblkp++) {
1439 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1441 lbn = fragstoblks(fs, blkno);
1443 blkp = &ip->i_din2->di_db[lbn];
1444 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1446 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1447 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1450 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1451 [(lbn - NDADDR) % NINDIR(fs)];
1454 * If we are expunging a snapshot vnode and we
1455 * find a block marked BLK_NOCOPY, then it is
1456 * one that has been allocated to this snapshot after
1457 * we took our current snapshot and can be ignored.
1459 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1464 panic("snapacct_ufs2: bad block");
1465 *blkp = expungetype;
1474 * Account for a set of blocks allocated in a snapshot inode.
1477 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1479 ufs2_daddr_t *oldblkp, *lastblkp;
1490 inum = ip->i_number;
1495 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1497 if (blkno == 0 || blkno == BLK_NOCOPY)
1499 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1500 *ip->i_snapblklist++ = lblkno;
1501 if (blkno == BLK_SNAP)
1502 blkno = blkstofrags(fs, lblkno);
1503 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1509 * Decrement extra reference on snapshot when last name is removed.
1510 * It will not be freed until the last open reference goes away.
1519 struct snapdata *sn;
1520 struct ufsmount *ump;
1523 * Find snapshot in incore list.
1526 sn = ip->i_devvp->v_rdev->si_snapdata;
1528 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1534 printf("ffs_snapgone: lost snapshot vnode %d\n",
1537 * Delete snapshot inode from superblock. Keep list dense.
1542 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1543 if (fs->fs_snapinum[snaploc] == ip->i_number)
1545 if (snaploc < FSMAXSNAP) {
1546 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1547 if (fs->fs_snapinum[snaploc] == 0)
1549 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1551 fs->fs_snapinum[snaploc - 1] = 0;
1557 * Prepare a snapshot file for being removed.
1564 struct vnode *devvp;
1567 ufs2_daddr_t numblks, blkno, dblk;
1568 int error, loc, last;
1569 struct snapdata *sn;
1573 devvp = ip->i_devvp;
1575 * If active, delete from incore list (this snapshot may
1576 * already have been in the process of being deleted, so
1577 * would not have been active).
1579 * Clear copy-on-write flag if last snapshot.
1582 if (ip->i_nextsnap.tqe_prev != 0) {
1583 sn = devvp->v_rdev->si_snapdata;
1584 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1585 ip->i_nextsnap.tqe_prev = 0;
1587 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1588 KASSERT(vp->v_vnlock == &sn->sn_lock,
1589 ("ffs_snapremove: lost lock mutation"));
1590 vp->v_vnlock = &vp->v_lock;
1592 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1593 try_free_snapdata(devvp);
1597 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1598 * snapshots that want them (see ffs_snapblkfree below).
1600 for (blkno = 1; blkno < NDADDR; blkno++) {
1601 dblk = DIP(ip, i_db[blkno]);
1604 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1605 DIP_SET(ip, i_db[blkno], 0);
1606 else if ((dblk == blkstofrags(fs, blkno) &&
1607 ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1609 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1610 btodb(fs->fs_bsize));
1611 DIP_SET(ip, i_db[blkno], 0);
1614 numblks = howmany(ip->i_size, fs->fs_bsize);
1615 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1616 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1617 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1620 if (fs->fs_size - blkno > NINDIR(fs))
1623 last = fs->fs_size - blkno;
1624 for (loc = 0; loc < last; loc++) {
1625 if (ip->i_ump->um_fstype == UFS1) {
1626 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1629 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1630 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1631 else if ((dblk == blkstofrags(fs, blkno) &&
1632 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1633 fs->fs_bsize, ip->i_number))) {
1634 ip->i_din1->di_blocks -=
1635 btodb(fs->fs_bsize);
1636 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1640 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1643 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1644 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1645 else if ((dblk == blkstofrags(fs, blkno) &&
1646 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1647 fs->fs_bsize, ip->i_number))) {
1648 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1649 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1655 * Clear snapshot flag and drop reference.
1657 ip->i_flags &= ~SF_SNAPSHOT;
1658 DIP_SET(ip, i_flags, ip->i_flags);
1659 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1662 * Reenable disk quotas for ex-snapshot file.
1664 if (!getinoquota(ip))
1665 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1670 * Notification that a block is being freed. Return zero if the free
1671 * should be allowed to proceed. Return non-zero if the snapshot file
1672 * wants to claim the block. The block will be claimed if it is an
1673 * uncopied part of one of the snapshots. It will be freed if it is
1674 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1675 * If a fragment is being freed, then all snapshots that care about
1676 * it must make a copy since a snapshot file can only claim full sized
1677 * blocks. Note that if more than one snapshot file maps the block,
1678 * we can pick one at random to claim it. Since none of the snapshots
1679 * can change, we are assurred that they will all see the same unmodified
1680 * image. When deleting a snapshot file (see ffs_snapremove above), we
1681 * must push any of these claimed blocks to one of the other snapshots
1682 * that maps it. These claimed blocks are easily identified as they will
1683 * have a block number equal to their logical block number within the
1684 * snapshot. A copied block can never have this property because they
1685 * must always have been allocated from a BLK_NOCOPY location.
1688 ffs_snapblkfree(fs, devvp, bno, size, inum)
1690 struct vnode *devvp;
1695 struct buf *ibp, *cbp, *savedcbp = 0;
1696 struct thread *td = curthread;
1698 struct vnode *vp = NULL;
1701 int indiroff = 0, error = 0, claimedblk = 0;
1702 struct snapdata *sn;
1704 lbn = fragstoblks(fs, bno);
1707 sn = devvp->v_rdev->si_snapdata;
1712 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1713 VI_MTX(devvp)) != 0)
1715 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1718 * Lookup block being written.
1721 blkno = DIP(ip, i_db[lbn]);
1723 td->td_pflags |= TDP_COWINPROGRESS;
1724 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1725 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1726 td->td_pflags &= ~TDP_COWINPROGRESS;
1729 indiroff = (lbn - NDADDR) % NINDIR(fs);
1730 if (ip->i_ump->um_fstype == UFS1)
1731 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1733 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1736 * Check to see if block needs to be copied.
1740 * A block that we map is being freed. If it has not
1741 * been claimed yet, we will claim or copy it (below).
1744 } else if (blkno == BLK_SNAP) {
1746 * No previous snapshot claimed the block,
1747 * so it will be freed and become a BLK_NOCOPY
1748 * (don't care) for us.
1751 panic("snapblkfree: inconsistent block type");
1753 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1754 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1755 } else if (ip->i_ump->um_fstype == UFS1) {
1756 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1760 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1765 } else /* BLK_NOCOPY or default */ {
1767 * If the snapshot has already copied the block
1768 * (default), or does not care about the block,
1776 * If this is a full size block, we will just grab it
1777 * and assign it to the snapshot inode. Otherwise we
1778 * will proceed to copy it. See explanation for this
1779 * routine as to why only a single snapshot needs to
1782 if (size == fs->fs_bsize) {
1785 printf("%s %d lbn %jd from inum %d\n",
1786 "Grabonremove: snapino", ip->i_number,
1787 (intmax_t)lbn, inum);
1790 DIP_SET(ip, i_db[lbn], bno);
1791 } else if (ip->i_ump->um_fstype == UFS1) {
1792 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1795 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1798 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1799 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1800 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1806 * Allocate the block into which to do the copy. Note that this
1807 * allocation will never require any additional allocations for
1808 * the snapshot inode.
1810 td->td_pflags |= TDP_COWINPROGRESS;
1811 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1812 fs->fs_bsize, KERNCRED, 0, &cbp);
1813 td->td_pflags &= ~TDP_COWINPROGRESS;
1818 printf("%s%d lbn %jd %s %d size %ld to blkno %jd\n",
1819 "Copyonremove: snapino ", ip->i_number,
1820 (intmax_t)lbn, "for inum", inum, size,
1821 (intmax_t)cbp->b_blkno);
1824 * If we have already read the old block contents, then
1825 * simply copy them to the new block. Note that we need
1826 * to synchronously write snapshots that have not been
1827 * unlinked, and hence will be visible after a crash,
1828 * to ensure their integrity.
1830 if (savedcbp != 0) {
1831 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1833 if (dopersistence && ip->i_effnlink > 0)
1834 (void) ffs_syncvnode(vp, MNT_WAIT);
1838 * Otherwise, read the old block contents into the buffer.
1840 if ((error = readblock(vp, cbp, lbn)) != 0) {
1841 bzero(cbp->b_data, fs->fs_bsize);
1843 if (dopersistence && ip->i_effnlink > 0)
1844 (void) ffs_syncvnode(vp, MNT_WAIT);
1850 * Note that we need to synchronously write snapshots that
1851 * have not been unlinked, and hence will be visible after
1852 * a crash, to ensure their integrity.
1855 vp = savedcbp->b_vp;
1857 if (dopersistence && VTOI(vp)->i_effnlink > 0)
1858 (void) ffs_syncvnode(vp, MNT_WAIT);
1861 * If we have been unable to allocate a block in which to do
1862 * the copy, then return non-zero so that the fragment will
1863 * not be freed. Although space will be lost, the snapshot
1864 * will stay consistent.
1866 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1871 * Associate snapshot files when mounting.
1874 ffs_snapshot_mount(mp)
1877 struct ufsmount *ump = VFSTOUFS(mp);
1878 struct vnode *devvp = ump->um_devvp;
1879 struct fs *fs = ump->um_fs;
1880 struct thread *td = curthread;
1881 struct snapdata *sn;
1883 struct vnode *lastvp;
1889 daddr_t snaplistsize;
1890 int error, snaploc, loc;
1893 * XXX The following needs to be set before ffs_truncate or
1894 * VOP_READ can be called.
1896 mp->mnt_stat.f_iosize = fs->fs_bsize;
1898 * Process each snapshot listed in the superblock.
1903 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1904 if (fs->fs_snapinum[snaploc] == 0)
1906 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1907 LK_EXCLUSIVE, &vp)) != 0){
1908 printf("ffs_snapshot_mount: vget failed %d\n", error);
1912 if ((ip->i_flags & SF_SNAPSHOT) == 0 || ip->i_size ==
1913 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1914 if ((ip->i_flags & SF_SNAPSHOT) == 0) {
1915 reason = "non-snapshot";
1917 reason = "old format snapshot";
1918 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
1919 (void)ffs_syncvnode(vp, MNT_WAIT);
1921 printf("ffs_snapshot_mount: %s inode %d\n",
1922 reason, fs->fs_snapinum[snaploc]);
1925 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
1926 if (fs->fs_snapinum[loc] == 0)
1928 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
1930 fs->fs_snapinum[loc - 1] = 0;
1935 * Acquire a lock on the snapdata structure, creating it if
1938 sn = ffs_snapdata_acquire(devvp);
1940 * Change vnode to use shared snapshot lock instead of the
1941 * original private lock.
1943 vp->v_vnlock = &sn->sn_lock;
1944 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
1946 * Link it onto the active snapshot list.
1949 if (ip->i_nextsnap.tqe_prev != 0)
1950 panic("ffs_snapshot_mount: %d already on list",
1953 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
1954 vp->v_vflag |= VV_SYSTEM;
1961 * No usable snapshots found.
1963 if (sn == NULL || vp == NULL)
1966 * Allocate the space for the block hints list. We always want to
1967 * use the list from the newest snapshot.
1969 auio.uio_iov = &aiov;
1970 auio.uio_iovcnt = 1;
1971 aiov.iov_base = (void *)&snaplistsize;
1972 aiov.iov_len = sizeof(snaplistsize);
1973 auio.uio_resid = aiov.iov_len;
1975 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
1976 auio.uio_segflg = UIO_SYSSPACE;
1977 auio.uio_rw = UIO_READ;
1979 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1980 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
1981 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
1985 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
1986 M_UFSMNT, M_WAITOK);
1987 auio.uio_iovcnt = 1;
1988 aiov.iov_base = snapblklist;
1989 aiov.iov_len = snaplistsize * sizeof (daddr_t);
1990 auio.uio_resid = aiov.iov_len;
1991 auio.uio_offset -= sizeof(snaplistsize);
1992 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
1993 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
1995 free(snapblklist, M_UFSMNT);
2000 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2001 sn->sn_listsize = snaplistsize;
2002 sn->sn_blklist = (daddr_t *)snapblklist;
2003 devvp->v_vflag |= VV_COPYONWRITE;
2008 * Disassociate snapshot files when unmounting.
2011 ffs_snapshot_unmount(mp)
2014 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2015 struct snapdata *sn;
2020 sn = devvp->v_rdev->si_snapdata;
2021 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2023 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2024 xp->i_nextsnap.tqe_prev = 0;
2025 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2027 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2028 KASSERT(vp->v_vnlock == &sn->sn_lock,
2029 ("ffs_snapshot_unmount: lost lock mutation"));
2030 vp->v_vnlock = &vp->v_lock;
2031 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2032 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2033 if (xp->i_effnlink > 0)
2036 sn = devvp->v_rdev->si_snapdata;
2038 try_free_snapdata(devvp);
2039 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2043 * Check the buffer block to be belong to device buffer that shall be
2044 * locked after snaplk. devvp shall be locked on entry, and will be
2045 * leaved locked upon exit.
2048 ffs_bp_snapblk(devvp, bp)
2049 struct vnode *devvp;
2052 struct snapdata *sn;
2054 ufs2_daddr_t lbn, *snapblklist;
2055 int lower, upper, mid;
2057 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2058 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2059 sn = devvp->v_rdev->si_snapdata;
2060 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2062 fs = TAILQ_FIRST(&sn->sn_head)->i_fs;
2063 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2064 snapblklist = sn->sn_blklist;
2065 upper = sn->sn_listsize - 1;
2067 while (lower <= upper) {
2068 mid = (lower + upper) / 2;
2069 if (snapblklist[mid] == lbn)
2071 if (snapblklist[mid] < lbn)
2087 struct vnode *vp, *devvp;
2091 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2096 devvp = bo->__bo_vnode;
2097 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2100 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2104 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2105 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2110 * Try to find a buffer to flush.
2112 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2113 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2115 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2118 panic("bdwrite: found ourselves");
2121 * Don't countdeps with the bo lock
2124 if (buf_countdeps(nbp, 0)) {
2131 if (!ffs_bp_snapblk(vp, nbp)) {
2132 if (BO_MTX(bo) != VI_MTX(vp)) {
2141 if (nbp->b_flags & B_CLUSTEROK) {
2142 vfs_bio_awrite(nbp);
2147 dirtybufferflushes++;
2156 * Check for need to copy block that is about to be written,
2157 * copying the block if necessary.
2160 ffs_copyonwrite(devvp, bp)
2161 struct vnode *devvp;
2164 struct snapdata *sn;
2165 struct buf *ibp, *cbp, *savedcbp = 0;
2166 struct thread *td = curthread;
2169 struct vnode *vp = 0;
2170 ufs2_daddr_t lbn, blkno, *snapblklist;
2171 int lower, upper, mid, indiroff, error = 0;
2172 int launched_async_io, prev_norunningbuf;
2173 long saved_runningbufspace;
2175 if (devvp != bp->b_vp && (VTOI(bp->b_vp)->i_flags & SF_SNAPSHOT) != 0)
2176 return (0); /* Update on a snapshot file */
2177 if (td->td_pflags & TDP_COWINPROGRESS)
2178 panic("ffs_copyonwrite: recursive call");
2180 * First check to see if it is in the preallocated list.
2181 * By doing this check we avoid several potential deadlocks.
2184 sn = devvp->v_rdev->si_snapdata;
2186 TAILQ_EMPTY(&sn->sn_head)) {
2188 return (0); /* No snapshot */
2190 ip = TAILQ_FIRST(&sn->sn_head);
2192 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2193 snapblklist = sn->sn_blklist;
2194 upper = sn->sn_listsize - 1;
2196 while (lower <= upper) {
2197 mid = (lower + upper) / 2;
2198 if (snapblklist[mid] == lbn)
2200 if (snapblklist[mid] < lbn)
2205 if (lower <= upper) {
2209 launched_async_io = 0;
2210 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2212 * Since I/O on bp isn't yet in progress and it may be blocked
2213 * for a long time waiting on snaplk, back it out of
2214 * runningbufspace, possibly waking other threads waiting for space.
2216 saved_runningbufspace = bp->b_runningbufspace;
2217 if (saved_runningbufspace != 0)
2218 runningbufwakeup(bp);
2220 * Not in the precomputed list, so check the snapshots.
2222 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2223 VI_MTX(devvp)) != 0) {
2225 sn = devvp->v_rdev->si_snapdata;
2227 TAILQ_EMPTY(&sn->sn_head)) {
2229 if (saved_runningbufspace != 0) {
2230 bp->b_runningbufspace = saved_runningbufspace;
2231 atomic_add_long(&runningbufspace,
2232 bp->b_runningbufspace);
2234 return (0); /* Snapshot gone */
2237 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2240 * We ensure that everything of our own that needs to be
2241 * copied will be done at the time that ffs_snapshot is
2242 * called. Thus we can skip the check here which can
2243 * deadlock in doing the lookup in UFS_BALLOC.
2248 * Check to see if block needs to be copied. We do not have
2249 * to hold the snapshot lock while doing this lookup as it
2250 * will never require any additional allocations for the
2254 blkno = DIP(ip, i_db[lbn]);
2256 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2257 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2258 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2259 td->td_pflags &= ~TDP_COWINPROGRESS;
2262 indiroff = (lbn - NDADDR) % NINDIR(fs);
2263 if (ip->i_ump->um_fstype == UFS1)
2264 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2266 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2270 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2271 panic("ffs_copyonwrite: bad copy block");
2276 * Allocate the block into which to do the copy. Since
2277 * multiple processes may all try to copy the same block,
2278 * we have to recheck our need to do a copy if we sleep
2279 * waiting for the lock.
2281 * Because all snapshots on a filesystem share a single
2282 * lock, we ensure that we will never be in competition
2283 * with another process to allocate a block.
2285 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2286 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2287 fs->fs_bsize, KERNCRED, 0, &cbp);
2288 td->td_pflags &= ~TDP_COWINPROGRESS;
2293 printf("Copyonwrite: snapino %d lbn %jd for ",
2294 ip->i_number, (intmax_t)lbn);
2295 if (bp->b_vp == devvp)
2296 printf("fs metadata");
2298 printf("inum %d", VTOI(bp->b_vp)->i_number);
2299 printf(" lblkno %jd to blkno %jd\n",
2300 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2304 * If we have already read the old block contents, then
2305 * simply copy them to the new block. Note that we need
2306 * to synchronously write snapshots that have not been
2307 * unlinked, and hence will be visible after a crash,
2308 * to ensure their integrity.
2310 if (savedcbp != 0) {
2311 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2313 if (dopersistence && ip->i_effnlink > 0)
2314 (void) ffs_syncvnode(vp, MNT_WAIT);
2316 launched_async_io = 1;
2320 * Otherwise, read the old block contents into the buffer.
2322 if ((error = readblock(vp, cbp, lbn)) != 0) {
2323 bzero(cbp->b_data, fs->fs_bsize);
2325 if (dopersistence && ip->i_effnlink > 0)
2326 (void) ffs_syncvnode(vp, MNT_WAIT);
2328 launched_async_io = 1;
2334 * Note that we need to synchronously write snapshots that
2335 * have not been unlinked, and hence will be visible after
2336 * a crash, to ensure their integrity.
2339 vp = savedcbp->b_vp;
2341 if (dopersistence && VTOI(vp)->i_effnlink > 0)
2342 (void) ffs_syncvnode(vp, MNT_WAIT);
2344 launched_async_io = 1;
2346 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2347 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2349 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2350 waitrunningbufspace();
2352 * I/O on bp will now be started, so count it in runningbufspace.
2354 if (saved_runningbufspace != 0) {
2355 bp->b_runningbufspace = saved_runningbufspace;
2356 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2362 * Read the specified block into the given buffer.
2363 * Much of this boiler-plate comes from bwrite().
2366 readblock(vp, bp, lbn)
2371 struct inode *ip = VTOI(vp);
2374 bip = g_alloc_bio();
2375 bip->bio_cmd = BIO_READ;
2376 bip->bio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
2377 bip->bio_data = bp->b_data;
2378 bip->bio_length = bp->b_bcount;
2379 bip->bio_done = NULL;
2381 g_io_request(bip, ip->i_devvp->v_bufobj.bo_private);
2382 bp->b_error = biowait(bip, "snaprdb");
2384 return (bp->b_error);
2390 * Process file deletes that were deferred by ufs_inactive() due to
2391 * the file system being suspended. Transfer IN_LAZYACCESS into
2392 * IN_MODIFIED for vnodes that were accessed during suspension.
2395 process_deferred_inactive(struct mount *mp)
2397 struct vnode *vp, *mvp;
2403 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2406 MNT_VNODE_FOREACH(vp, mp, mvp) {
2409 * IN_LAZYACCESS is checked here without holding any
2410 * vnode lock, but this flag is set only while holding
2413 if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED) != 0 ||
2414 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2415 ((vp->v_iflag & VI_OWEINACT) == 0 ||
2416 vp->v_usecount > 0))) {
2422 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2426 if (error == ENOENT)
2427 continue; /* vnode recycled */
2428 MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2432 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2433 ip->i_flag &= ~IN_LAZYACCESS;
2434 ip->i_flag |= IN_MODIFIED;
2437 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2445 VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2446 ("process_deferred_inactive: "
2447 "recursed on VI_DOINGINACT"));
2448 vp->v_iflag |= VI_DOINGINACT;
2449 vp->v_iflag &= ~VI_OWEINACT;
2451 (void) VOP_INACTIVE(vp, td);
2453 VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2454 ("process_deferred_inactive: lost VI_DOINGINACT"));
2455 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2456 ("process_deferred_inactive: got VI_OWEINACT"));
2457 vp->v_iflag &= ~VI_DOINGINACT;
2464 vn_finished_secondary_write(mp);
2467 #ifndef NO_FFS_SNAPSHOT
2469 static struct snapdata *
2470 ffs_snapdata_alloc(void)
2472 struct snapdata *sn;
2475 * Fetch a snapdata from the free list if there is one available.
2477 mtx_lock(&snapfree_lock);
2478 sn = LIST_FIRST(&snapfree);
2480 LIST_REMOVE(sn, sn_link);
2481 mtx_unlock(&snapfree_lock);
2485 * If there were no free snapdatas allocate one.
2487 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2488 TAILQ_INIT(&sn->sn_head);
2489 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2490 LK_CANRECURSE | LK_NOSHARE);
2495 * The snapdata is never freed because we can not be certain that
2496 * there are no threads sleeping on the snap lock. Persisting
2497 * them permanently avoids costly synchronization in ffs_lock().
2500 ffs_snapdata_free(struct snapdata *sn)
2502 mtx_lock(&snapfree_lock);
2503 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2504 mtx_unlock(&snapfree_lock);
2507 /* Try to free snapdata associated with devvp */
2509 try_free_snapdata(struct vnode *devvp)
2511 struct snapdata *sn;
2512 ufs2_daddr_t *snapblklist;
2514 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2515 sn = devvp->v_rdev->si_snapdata;
2517 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2518 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2523 devvp->v_rdev->si_snapdata = NULL;
2524 devvp->v_vflag &= ~VV_COPYONWRITE;
2525 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2526 snapblklist = sn->sn_blklist;
2527 sn->sn_blklist = NULL;
2528 sn->sn_listsize = 0;
2529 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2530 if (snapblklist != NULL)
2531 free(snapblklist, M_UFSMNT);
2532 ffs_snapdata_free(sn);
2535 static struct snapdata *
2536 ffs_snapdata_acquire(struct vnode *devvp)
2538 struct snapdata *nsn;
2539 struct snapdata *sn;
2542 * Allocate a free snapdata. This is done before acquiring the
2543 * devvp lock to avoid allocation while the devvp interlock is
2546 nsn = ffs_snapdata_alloc();
2548 * If there snapshots already exist on this filesystem grab a
2549 * reference to the shared lock. Otherwise this is the first
2550 * snapshot on this filesystem and we need to use our
2551 * pre-allocated snapdata.
2554 if (devvp->v_rdev->si_snapdata == NULL) {
2555 devvp->v_rdev->si_snapdata = nsn;
2558 sn = devvp->v_rdev->si_snapdata;
2560 * Acquire the snapshot lock.
2562 lockmgr(&sn->sn_lock,
2563 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, VI_MTX(devvp));
2565 * Free any unused snapdata.
2568 ffs_snapdata_free(nsn);