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, vtype, wkhd)
91 struct workhead *wkhd;
103 ffs_snapshot_mount(mp)
109 ffs_snapshot_unmount(mp)
121 ffs_copyonwrite(devvp, bp)
129 ffs_sync_snap(mp, waitfor)
136 FEATURE(ffs_snapshot, "FFS snapshot support");
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, 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, 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 code normally only ensures
179 * persistence for the filesystem metadata contained within a
180 * snapshot. Setting the following flag allows this crash
181 * persistence to be enabled for file contents.
183 int dopersistence = 0;
186 #include <sys/sysctl.h>
187 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
188 static int snapdebug = 0;
189 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
190 int collectsnapstats = 0;
191 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
196 * Create a snapshot file and initialize it for the filesystem.
199 ffs_snapshot(mp, snapfile)
203 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
204 int error, cg, snaploc;
205 int i, size, len, loc;
206 ufs2_daddr_t blockno;
208 struct timespec starttime = {0, 0}, endtime;
210 long redo = 0, snaplistsize = 0;
213 struct fs *copy_fs = NULL, *fs;
214 struct thread *td = curthread;
215 struct inode *ip, *xp;
216 struct buf *bp, *nbp, *ibp;
220 struct vnode *vp, *xvp, *mvp, *devvp;
224 struct ufsmount *ump;
230 * At the moment, journaled soft updates cannot support
233 if (MOUNTEDSUJ(mp)) {
234 vfs_mount_error(mp, "%s: Snapshots are not yet supported when "
235 "running with journaled soft updates", fs->fs_fsmnt);
242 * Need to serialize access to snapshot code per filesystem.
245 * Assign a snapshot slot in the superblock.
248 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
249 if (fs->fs_snapinum[snaploc] == 0)
252 if (snaploc == FSMAXSNAP)
255 * Create the snapshot file.
258 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE, snapfile, td);
259 if ((error = namei(&nd)) != 0)
261 if (nd.ni_vp != NULL) {
265 if (nd.ni_dvp->v_mount != mp)
268 NDFREE(&nd, NDF_ONLY_PNBUF);
269 if (nd.ni_dvp == nd.ni_vp)
277 vat.va_mode = S_IRUSR;
278 vat.va_vaflags |= VA_EXCLUSIVE;
279 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
282 panic("ffs_snapshot: mount mismatch");
284 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
285 NDFREE(&nd, NDF_ONLY_PNBUF);
287 if ((error = vn_start_write(NULL, &wrtmp,
288 V_XSLEEP | PCATCH)) != 0)
292 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
293 VOP_UNLOCK(nd.ni_dvp, 0);
295 NDFREE(&nd, NDF_ONLY_PNBUF);
296 vn_finished_write(wrtmp);
301 vp->v_vflag |= VV_SYSTEM;
305 * Allocate and copy the last block contents so as to be able
306 * to set size to that of the filesystem.
308 numblks = howmany(fs->fs_size, fs->fs_frag);
309 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
310 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
313 ip->i_size = lblktosize(fs, (off_t)numblks);
314 DIP_SET(ip, i_size, ip->i_size);
315 ip->i_flag |= IN_CHANGE | IN_UPDATE;
316 error = readblock(vp, bp, numblks - 1);
321 * Preallocate critical data structures so that we can copy
322 * them in without further allocation after we suspend all
323 * operations on the filesystem. We would like to just release
324 * the allocated buffers without writing them since they will
325 * be filled in below once we are ready to go, but this upsets
326 * the soft update code, so we go ahead and write the new buffers.
328 * Allocate all indirect blocks and mark all of them as not
329 * needing to be copied.
331 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
332 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
333 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
339 * Allocate copies for the superblock and its summary information.
341 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
346 blkno = fragstoblks(fs, fs->fs_csaddr);
347 len = howmany(fs->fs_cssize, fs->fs_bsize);
348 for (loc = 0; loc < len; loc++) {
349 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
350 fs->fs_bsize, KERNCRED, 0, &nbp);
356 * Allocate all cylinder group blocks.
358 for (cg = 0; cg < fs->fs_ncg; cg++) {
359 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
360 fs->fs_bsize, KERNCRED, 0, &nbp);
365 ffs_syncvnode(vp, MNT_WAIT, 0);
368 * Copy all the cylinder group maps. Although the
369 * filesystem is still active, we hope that only a few
370 * cylinder groups will change between now and when we
371 * suspend operations. Thus, we will be able to quickly
372 * touch up the few cylinder groups that changed during
373 * the suspension period.
375 len = howmany(fs->fs_ncg, NBBY);
376 space = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
378 fs->fs_active = space;
380 for (cg = 0; cg < fs->fs_ncg; cg++) {
381 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
382 fs->fs_bsize, KERNCRED, 0, &nbp);
385 error = cgaccount(cg, vp, nbp, 1);
388 ffs_syncvnode(vp, MNT_WAIT, 0);
393 * Change inode to snapshot type file.
395 ip->i_flags |= SF_SNAPSHOT;
396 DIP_SET(ip, i_flags, ip->i_flags);
397 ip->i_flag |= IN_CHANGE | IN_UPDATE;
399 * Ensure that the snapshot is completely on disk.
400 * Since we have marked it as a snapshot it is safe to
401 * unlock it as no process will be allowed to write to it.
403 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
407 * All allocations are done, so we can now snapshot the system.
409 * Recind nice scheduling while running with the filesystem suspended.
411 if (td->td_proc->p_nice > 0) {
416 saved_nice = p->p_nice;
421 * Suspend operation on filesystem.
424 vn_finished_write(wrtmp);
425 if ((error = vfs_write_suspend(vp->v_mount)) != 0) {
426 vn_start_write(NULL, &wrtmp, V_WAIT);
427 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
430 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
432 vn_start_write(NULL, &wrtmp, V_WAIT);
434 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
435 if (ip->i_effnlink == 0) {
436 error = ENOENT; /* Snapshot file unlinked */
439 if (collectsnapstats)
440 nanotime(&starttime);
442 /* The last block might have changed. Copy it again to be sure. */
443 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
444 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
447 error = readblock(vp, bp, numblks - 1);
448 bp->b_flags |= B_VALIDSUSPWRT;
453 * First, copy all the cylinder group maps that have changed.
455 for (cg = 0; cg < fs->fs_ncg; cg++) {
456 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
459 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
460 fs->fs_bsize, KERNCRED, 0, &nbp);
463 error = cgaccount(cg, vp, nbp, 2);
469 * Grab a copy of the superblock and its summary information.
470 * We delay writing it until the suspension is released below.
472 copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK);
473 bcopy(fs, copy_fs, fs->fs_sbsize);
474 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
475 copy_fs->fs_clean = 1;
476 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
477 if (fs->fs_sbsize < size)
478 bzero(&((char *)copy_fs)[fs->fs_sbsize],
479 size - fs->fs_sbsize);
480 size = blkroundup(fs, fs->fs_cssize);
481 if (fs->fs_contigsumsize > 0)
482 size += fs->fs_ncg * sizeof(int32_t);
483 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
484 copy_fs->fs_csp = space;
485 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
486 space = (char *)space + fs->fs_cssize;
487 loc = howmany(fs->fs_cssize, fs->fs_fsize);
488 i = fs->fs_frag - loc % fs->fs_frag;
489 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
491 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
492 len, KERNCRED, &bp)) != 0) {
494 free(copy_fs->fs_csp, M_UFSMNT);
495 free(copy_fs, M_UFSMNT);
499 bcopy(bp->b_data, space, (u_int)len);
500 space = (char *)space + len;
501 bp->b_flags |= B_INVAL | B_NOCACHE;
504 if (fs->fs_contigsumsize > 0) {
505 copy_fs->fs_maxcluster = lp = space;
506 for (i = 0; i < fs->fs_ncg; i++)
507 *lp++ = fs->fs_contigsumsize;
510 * We must check for active files that have been unlinked
511 * (e.g., with a zero link count). We have to expunge all
512 * trace of these files from the snapshot so that they are
513 * not reclaimed prematurely by fsck or unnecessarily dumped.
514 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
515 * spec_strategy about writing on a suspended filesystem.
516 * Note that we skip unlinked snapshot files as they will
517 * be handled separately below.
519 * We also calculate the needed size for the snapshot list.
521 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
522 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
524 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
527 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
528 if ((xvp->v_usecount == 0 &&
529 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
530 xvp->v_type == VNON ||
531 IS_SNAPSHOT(VTOI(xvp))) {
536 * We can skip parent directory vnode because it must have
537 * this snapshot file in it.
539 if (xvp == nd.ni_dvp) {
544 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
545 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
550 if (xvp->v_usecount == 0 &&
551 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
559 vprint("ffs_snapshot: busy vnode", xvp);
560 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
567 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
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,
583 blkno = DIP(xp, i_db[loc]);
584 DIP_SET(xp, i_db[loc], 0);
588 if (xp->i_ump->um_fstype == UFS1)
589 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
592 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
595 DIP_SET(xp, i_db[loc], blkno);
597 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
602 free(copy_fs->fs_csp, M_UFSMNT);
603 free(copy_fs, M_UFSMNT);
605 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
610 * Erase the journal file from the snapshot.
612 if (fs->fs_flags & FS_SUJ) {
613 error = softdep_journal_lookup(mp, &xvp);
615 free(copy_fs->fs_csp, M_UFSMNT);
616 free(copy_fs, M_UFSMNT);
621 if (xp->i_ump->um_fstype == UFS1)
622 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
625 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
630 * Acquire a lock on the snapdata structure, creating it if necessary.
632 sn = ffs_snapdata_acquire(devvp);
634 * Change vnode to use shared snapshot lock instead of the original
637 vp->v_vnlock = &sn->sn_lock;
638 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
639 xp = TAILQ_FIRST(&sn->sn_head);
641 * If this is the first snapshot on this filesystem, then we need
642 * to allocate the space for the list of preallocated snapshot blocks.
643 * This list will be refined below, but this preliminary one will
644 * keep us out of deadlock until the full one is ready.
647 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
649 blkp = &snapblklist[1];
650 *blkp++ = lblkno(fs, fs->fs_sblockloc);
651 blkno = fragstoblks(fs, fs->fs_csaddr);
652 for (cg = 0; cg < fs->fs_ncg; cg++) {
653 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
655 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
657 len = howmany(fs->fs_cssize, fs->fs_bsize);
658 for (loc = 0; loc < len; loc++)
659 *blkp++ = blkno + loc;
660 for (; cg < fs->fs_ncg; cg++)
661 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
662 snapblklist[0] = blkp - snapblklist;
664 if (sn->sn_blklist != NULL)
665 panic("ffs_snapshot: non-empty list");
666 sn->sn_blklist = snapblklist;
667 sn->sn_listsize = blkp - snapblklist;
671 * Record snapshot inode. Since this is the newest snapshot,
672 * it must be placed at the end of the list.
675 fs->fs_snapinum[snaploc] = ip->i_number;
676 if (ip->i_nextsnap.tqe_prev != 0)
677 panic("ffs_snapshot: %d already on list", ip->i_number);
678 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
679 devvp->v_vflag |= VV_COPYONWRITE;
681 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
683 KASSERT((sn != NULL && copy_fs != NULL && error == 0) ||
684 (sn == NULL && copy_fs == NULL && error != 0),
685 ("email phk@ and mckusick@"));
687 * Resume operation on filesystem.
689 vfs_write_resume(vp->v_mount);
690 vn_start_write(NULL, &wrtmp, V_WAIT);
691 if (collectsnapstats && starttime.tv_sec > 0) {
693 timespecsub(&endtime, &starttime);
694 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
695 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
696 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
701 * Copy allocation information from all the snapshots in
702 * this snapshot and then expunge them from its view.
704 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
707 if (xp->i_ump->um_fstype == UFS1)
708 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
711 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
713 if (error == 0 && xp->i_effnlink == 0) {
714 error = ffs_freefile(ump,
721 fs->fs_snapinum[snaploc] = 0;
726 * Allocate space for the full list of preallocated snapshot blocks.
728 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
730 ip->i_snapblklist = &snapblklist[1];
732 * Expunge the blocks used by the snapshots from the set of
733 * blocks marked as used in the snapshot bitmaps. Also, collect
734 * the list of allocated blocks in i_snapblklist.
736 if (ip->i_ump->um_fstype == UFS1)
737 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
740 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
743 fs->fs_snapinum[snaploc] = 0;
744 free(snapblklist, M_UFSMNT);
747 if (snaplistsize < ip->i_snapblklist - snapblklist)
748 panic("ffs_snapshot: list too small");
749 snaplistsize = ip->i_snapblklist - snapblklist;
750 snapblklist[0] = snaplistsize;
751 ip->i_snapblklist = 0;
753 * Write out the list of allocated blocks to the end of the snapshot.
755 auio.uio_iov = &aiov;
757 aiov.iov_base = (void *)snapblklist;
758 aiov.iov_len = snaplistsize * sizeof(daddr_t);
759 auio.uio_resid = aiov.iov_len;
760 auio.uio_offset = ip->i_size;
761 auio.uio_segflg = UIO_SYSSPACE;
762 auio.uio_rw = UIO_WRITE;
764 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
765 fs->fs_snapinum[snaploc] = 0;
766 free(snapblklist, M_UFSMNT);
770 * Write the superblock and its summary information
773 blkno = fragstoblks(fs, fs->fs_csaddr);
774 len = howmany(fs->fs_cssize, fs->fs_bsize);
775 space = copy_fs->fs_csp;
776 for (loc = 0; loc < len; loc++) {
777 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
780 fs->fs_snapinum[snaploc] = 0;
781 free(snapblklist, M_UFSMNT);
784 bcopy(space, nbp->b_data, fs->fs_bsize);
785 space = (char *)space + fs->fs_bsize;
788 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
793 loc = blkoff(fs, fs->fs_sblockloc);
794 bcopy((char *)copy_fs, &nbp->b_data[loc], fs->fs_bsize);
798 * As this is the newest list, it is the most inclusive, so
799 * should replace the previous list.
802 space = sn->sn_blklist;
803 sn->sn_blklist = snapblklist;
804 sn->sn_listsize = snaplistsize;
807 free(space, M_UFSMNT);
809 * Preallocate all the direct blocks in the snapshot inode so
810 * that we never have to write the inode itself to commit an
811 * update to the contents of the snapshot. Note that once
812 * created, the size of the snapshot will never change, so
813 * there will never be a need to write the inode except to
814 * update the non-integrity-critical time fields and
815 * allocated-block count.
817 for (blockno = 0; blockno < NDADDR; blockno++) {
818 if (DIP(ip, i_db[blockno]) != 0)
820 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
821 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
824 error = readblock(vp, bp, blockno);
830 free(copy_fs->fs_csp, M_UFSMNT);
831 free(copy_fs, M_UFSMNT);
834 NDFREE(&nd, NDF_ONLY_PNBUF);
835 if (saved_nice > 0) {
840 sched_nice(td->td_proc, saved_nice);
841 PROC_UNLOCK(td->td_proc);
844 if (fs->fs_active != 0) {
845 free(fs->fs_active, M_DEVBUF);
850 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
853 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
854 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
860 vn_finished_write(wrtmp);
861 process_deferred_inactive(mp);
866 * Copy a cylinder group map. All the unallocated blocks are marked
867 * BLK_NOCOPY so that the snapshot knows that it need not copy them
868 * if they are later written. If passno is one, then this is a first
869 * pass, so only setting needs to be done. If passno is 2, then this
870 * is a revision to a previous pass which must be undone as the
871 * replacement pass is done.
874 cgaccount(cg, vp, nbp, passno)
880 struct buf *bp, *ibp;
884 ufs2_daddr_t base, numblks;
885 int error, len, loc, indiroff;
889 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
890 (int)fs->fs_cgsize, KERNCRED, &bp);
895 cgp = (struct cg *)bp->b_data;
896 if (!cg_chkmagic(cgp)) {
903 * Recomputation of summary information might not have been performed
904 * at mount time. Sync up summary information for current cylinder
905 * group while data is in memory to ensure that result of background
906 * fsck is slightly more consistent.
908 fs->fs_cs(fs, cg) = cgp->cg_cs;
909 UFS_UNLOCK(ip->i_ump);
910 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
911 if (fs->fs_cgsize < fs->fs_bsize)
912 bzero(&nbp->b_data[fs->fs_cgsize],
913 fs->fs_bsize - fs->fs_cgsize);
914 cgp = (struct cg *)nbp->b_data;
917 nbp->b_flags |= B_VALIDSUSPWRT;
918 numblks = howmany(fs->fs_size, fs->fs_frag);
919 len = howmany(fs->fs_fpg, fs->fs_frag);
920 base = cgbase(fs, cg) / fs->fs_frag;
921 if (base + len >= numblks)
922 len = numblks - base - 1;
925 for ( ; loc < NDADDR; loc++) {
926 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
927 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
928 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
929 DIP_SET(ip, i_db[loc], 0);
930 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
931 panic("ffs_snapshot: lost direct block");
934 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
935 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
939 indiroff = (base + loc - NDADDR) % NINDIR(fs);
940 for ( ; loc < len; loc++, indiroff++) {
941 if (indiroff >= NINDIR(fs)) {
943 ibp->b_flags |= B_VALIDSUSPWRT;
945 error = UFS_BALLOC(vp,
946 lblktosize(fs, (off_t)(base + loc)),
947 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
953 if (ip->i_ump->um_fstype == UFS1) {
954 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
955 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
957 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
958 [indiroff] == BLK_NOCOPY)
959 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
960 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
961 [indiroff] == BLK_NOCOPY)
962 panic("ffs_snapshot: lost indirect block");
965 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
966 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
967 else if (passno == 2 &&
968 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
969 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
970 else if (passno == 1 &&
971 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
972 panic("ffs_snapshot: lost indirect block");
975 ibp->b_flags |= B_VALIDSUSPWRT;
981 * Before expunging a snapshot inode, note all the
982 * blocks that it claims with BLK_SNAP so that fsck will
983 * be able to account for those blocks properly and so
984 * that this snapshot knows that it need not copy them
985 * if the other snapshot holding them is freed. This code
986 * is reproduced once each for UFS1 and UFS2.
989 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
990 struct vnode *snapvp;
991 struct inode *cancelip;
993 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
994 struct fs *, ufs_lbn_t, int);
998 int i, error, indiroff;
1000 ufs2_daddr_t len, blkno, numblks, blksperindir;
1001 struct ufs1_dinode *dip;
1002 struct thread *td = curthread;
1006 * Prepare to expunge the inode. If its inode block has not
1007 * yet been copied, then allocate and fill the copy.
1009 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1012 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1014 if (DOINGSOFTDEP(snapvp))
1015 softdep_prealloc(snapvp, MNT_WAIT);
1016 td->td_pflags |= TDP_COWINPROGRESS;
1017 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1018 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1019 td->td_pflags &= ~TDP_COWINPROGRESS;
1022 indiroff = (lbn - NDADDR) % NINDIR(fs);
1023 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1027 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1030 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1031 fs->fs_bsize, KERNCRED, 0, &bp);
1034 if ((error = readblock(snapvp, bp, lbn)) != 0)
1038 * Set a snapshot inode to be a zero length file, regular files
1039 * or unlinked snapshots to be completely unallocated.
1041 dip = (struct ufs1_dinode *)bp->b_data +
1042 ino_to_fsbo(fs, cancelip->i_number);
1043 if (clearmode || cancelip->i_effnlink == 0)
1047 dip->di_flags &= ~SF_SNAPSHOT;
1048 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1051 * Now go through and expunge all the blocks in the file
1052 * using the function requested.
1054 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1055 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1056 &cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
1058 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1059 &cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
1063 len = numblks - NDADDR;
1065 for (i = 0; len > 0 && i < NIADDR; i++) {
1066 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1067 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1068 blksperindir, fs, acctfunc, expungetype);
1071 blksperindir *= NINDIR(fs);
1072 lbn -= blksperindir + 1;
1073 len -= blksperindir;
1074 rlbn += blksperindir;
1080 * Descend an indirect block chain for vnode cancelvp accounting for all
1081 * its indirect blocks in snapvp.
1084 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1085 blksperindir, fs, acctfunc, expungetype)
1086 struct vnode *snapvp;
1087 struct vnode *cancelvp;
1093 ufs_lbn_t blksperindir;
1095 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1096 struct fs *, ufs_lbn_t, int);
1100 ufs_lbn_t subblksperindir;
1101 struct indir indirs[NIADDR + 2];
1102 ufs1_daddr_t last, *bap;
1106 if (expungetype == BLK_NOCOPY)
1108 panic("indiracct_ufs1: missing indir");
1110 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1112 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1113 panic("indiracct_ufs1: botched params");
1115 * We have to expand bread here since it will deadlock looking
1116 * up the block number for any blocks that are not in the cache.
1118 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1119 bp->b_blkno = fsbtodb(fs, blkno);
1120 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1121 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1126 * Account for the block pointers in this indirect block.
1128 last = howmany(remblks, blksperindir);
1129 if (last > NINDIR(fs))
1131 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1132 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1134 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1135 level == 0 ? rlbn : -1, expungetype);
1136 if (error || level == 0)
1139 * Account for the block pointers in each of the indirect blocks
1140 * in the levels below us.
1142 subblksperindir = blksperindir / NINDIR(fs);
1143 for (lbn++, level--, i = 0; i < last; i++) {
1144 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1145 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1148 rlbn += blksperindir;
1149 lbn -= blksperindir;
1150 remblks -= blksperindir;
1153 free(bap, M_DEVBUF);
1158 * Do both snap accounting and map accounting.
1161 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1163 ufs1_daddr_t *oldblkp, *lastblkp;
1166 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1170 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1172 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1176 * Identify a set of blocks allocated in a snapshot inode.
1179 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1181 ufs1_daddr_t *oldblkp, *lastblkp;
1184 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1186 struct inode *ip = VTOI(vp);
1187 ufs1_daddr_t blkno, *blkp;
1192 for ( ; oldblkp < lastblkp; oldblkp++) {
1194 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1196 lbn = fragstoblks(fs, blkno);
1198 blkp = &ip->i_din1->di_db[lbn];
1199 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1201 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1202 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1205 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1206 [(lbn - NDADDR) % NINDIR(fs)];
1209 * If we are expunging a snapshot vnode and we
1210 * find a block marked BLK_NOCOPY, then it is
1211 * one that has been allocated to this snapshot after
1212 * we took our current snapshot and can be ignored.
1214 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1219 panic("snapacct_ufs1: bad block");
1220 *blkp = expungetype;
1229 * Account for a set of blocks allocated in a snapshot inode.
1232 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1234 ufs1_daddr_t *oldblkp, *lastblkp;
1245 inum = ip->i_number;
1250 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1252 if (blkno == 0 || blkno == BLK_NOCOPY)
1254 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1255 *ip->i_snapblklist++ = lblkno;
1256 if (blkno == BLK_SNAP)
1257 blkno = blkstofrags(fs, lblkno);
1258 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum,
1265 * Before expunging a snapshot inode, note all the
1266 * blocks that it claims with BLK_SNAP so that fsck will
1267 * be able to account for those blocks properly and so
1268 * that this snapshot knows that it need not copy them
1269 * if the other snapshot holding them is freed. This code
1270 * is reproduced once each for UFS1 and UFS2.
1273 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1274 struct vnode *snapvp;
1275 struct inode *cancelip;
1277 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1278 struct fs *, ufs_lbn_t, int);
1282 int i, error, indiroff;
1283 ufs_lbn_t lbn, rlbn;
1284 ufs2_daddr_t len, blkno, numblks, blksperindir;
1285 struct ufs2_dinode *dip;
1286 struct thread *td = curthread;
1290 * Prepare to expunge the inode. If its inode block has not
1291 * yet been copied, then allocate and fill the copy.
1293 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1296 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1298 if (DOINGSOFTDEP(snapvp))
1299 softdep_prealloc(snapvp, MNT_WAIT);
1300 td->td_pflags |= TDP_COWINPROGRESS;
1301 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1302 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1303 td->td_pflags &= ~TDP_COWINPROGRESS;
1306 indiroff = (lbn - NDADDR) % NINDIR(fs);
1307 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1311 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1314 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1315 fs->fs_bsize, KERNCRED, 0, &bp);
1318 if ((error = readblock(snapvp, bp, lbn)) != 0)
1322 * Set a snapshot inode to be a zero length file, regular files
1323 * to be completely unallocated.
1325 dip = (struct ufs2_dinode *)bp->b_data +
1326 ino_to_fsbo(fs, cancelip->i_number);
1327 if (clearmode || cancelip->i_effnlink == 0)
1331 dip->di_flags &= ~SF_SNAPSHOT;
1332 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1335 * Now go through and expunge all the blocks in the file
1336 * using the function requested.
1338 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1339 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1340 &cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
1342 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1343 &cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
1347 len = numblks - NDADDR;
1349 for (i = 0; len > 0 && i < NIADDR; i++) {
1350 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1351 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1352 blksperindir, fs, acctfunc, expungetype);
1355 blksperindir *= NINDIR(fs);
1356 lbn -= blksperindir + 1;
1357 len -= blksperindir;
1358 rlbn += blksperindir;
1364 * Descend an indirect block chain for vnode cancelvp accounting for all
1365 * its indirect blocks in snapvp.
1368 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1369 blksperindir, fs, acctfunc, expungetype)
1370 struct vnode *snapvp;
1371 struct vnode *cancelvp;
1377 ufs_lbn_t blksperindir;
1379 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1380 struct fs *, ufs_lbn_t, int);
1384 ufs_lbn_t subblksperindir;
1385 struct indir indirs[NIADDR + 2];
1386 ufs2_daddr_t last, *bap;
1390 if (expungetype == BLK_NOCOPY)
1392 panic("indiracct_ufs2: missing indir");
1394 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1396 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1397 panic("indiracct_ufs2: botched params");
1399 * We have to expand bread here since it will deadlock looking
1400 * up the block number for any blocks that are not in the cache.
1402 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1403 bp->b_blkno = fsbtodb(fs, blkno);
1404 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1405 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1410 * Account for the block pointers in this indirect block.
1412 last = howmany(remblks, blksperindir);
1413 if (last > NINDIR(fs))
1415 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1416 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1418 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1419 level == 0 ? rlbn : -1, expungetype);
1420 if (error || level == 0)
1423 * Account for the block pointers in each of the indirect blocks
1424 * in the levels below us.
1426 subblksperindir = blksperindir / NINDIR(fs);
1427 for (lbn++, level--, i = 0; i < last; i++) {
1428 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1429 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1432 rlbn += blksperindir;
1433 lbn -= blksperindir;
1434 remblks -= blksperindir;
1437 free(bap, M_DEVBUF);
1442 * Do both snap accounting and map accounting.
1445 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1447 ufs2_daddr_t *oldblkp, *lastblkp;
1450 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1454 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1456 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1460 * Identify a set of blocks allocated in a snapshot inode.
1463 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1465 ufs2_daddr_t *oldblkp, *lastblkp;
1468 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1470 struct inode *ip = VTOI(vp);
1471 ufs2_daddr_t blkno, *blkp;
1476 for ( ; oldblkp < lastblkp; oldblkp++) {
1478 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1480 lbn = fragstoblks(fs, blkno);
1482 blkp = &ip->i_din2->di_db[lbn];
1483 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1485 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1486 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1489 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1490 [(lbn - NDADDR) % NINDIR(fs)];
1493 * If we are expunging a snapshot vnode and we
1494 * find a block marked BLK_NOCOPY, then it is
1495 * one that has been allocated to this snapshot after
1496 * we took our current snapshot and can be ignored.
1498 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1503 panic("snapacct_ufs2: bad block");
1504 *blkp = expungetype;
1513 * Account for a set of blocks allocated in a snapshot inode.
1516 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1518 ufs2_daddr_t *oldblkp, *lastblkp;
1529 inum = ip->i_number;
1534 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1536 if (blkno == 0 || blkno == BLK_NOCOPY)
1538 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1539 *ip->i_snapblklist++ = lblkno;
1540 if (blkno == BLK_SNAP)
1541 blkno = blkstofrags(fs, lblkno);
1542 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum,
1549 * Decrement extra reference on snapshot when last name is removed.
1550 * It will not be freed until the last open reference goes away.
1559 struct snapdata *sn;
1560 struct ufsmount *ump;
1563 * Find snapshot in incore list.
1566 sn = ip->i_devvp->v_rdev->si_snapdata;
1568 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1574 printf("ffs_snapgone: lost snapshot vnode %d\n",
1577 * Delete snapshot inode from superblock. Keep list dense.
1582 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1583 if (fs->fs_snapinum[snaploc] == ip->i_number)
1585 if (snaploc < FSMAXSNAP) {
1586 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1587 if (fs->fs_snapinum[snaploc] == 0)
1589 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1591 fs->fs_snapinum[snaploc - 1] = 0;
1597 * Prepare a snapshot file for being removed.
1604 struct vnode *devvp;
1607 ufs2_daddr_t numblks, blkno, dblk;
1608 int error, loc, last;
1609 struct snapdata *sn;
1613 devvp = ip->i_devvp;
1615 * If active, delete from incore list (this snapshot may
1616 * already have been in the process of being deleted, so
1617 * would not have been active).
1619 * Clear copy-on-write flag if last snapshot.
1622 if (ip->i_nextsnap.tqe_prev != 0) {
1623 sn = devvp->v_rdev->si_snapdata;
1624 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1625 ip->i_nextsnap.tqe_prev = 0;
1627 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1628 KASSERT(vp->v_vnlock == &sn->sn_lock,
1629 ("ffs_snapremove: lost lock mutation"));
1630 vp->v_vnlock = &vp->v_lock;
1632 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1633 try_free_snapdata(devvp);
1637 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1638 * snapshots that want them (see ffs_snapblkfree below).
1640 for (blkno = 1; blkno < NDADDR; blkno++) {
1641 dblk = DIP(ip, i_db[blkno]);
1644 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1645 DIP_SET(ip, i_db[blkno], 0);
1646 else if ((dblk == blkstofrags(fs, blkno) &&
1647 ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1648 ip->i_number, vp->v_type, NULL))) {
1649 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1650 btodb(fs->fs_bsize));
1651 DIP_SET(ip, i_db[blkno], 0);
1654 numblks = howmany(ip->i_size, fs->fs_bsize);
1655 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1656 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1657 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1660 if (fs->fs_size - blkno > NINDIR(fs))
1663 last = fs->fs_size - blkno;
1664 for (loc = 0; loc < last; loc++) {
1665 if (ip->i_ump->um_fstype == UFS1) {
1666 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1669 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1670 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1671 else if ((dblk == blkstofrags(fs, blkno) &&
1672 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1673 fs->fs_bsize, ip->i_number, vp->v_type,
1675 ip->i_din1->di_blocks -=
1676 btodb(fs->fs_bsize);
1677 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1681 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1684 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1685 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1686 else if ((dblk == blkstofrags(fs, blkno) &&
1687 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1688 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1689 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1690 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1696 * Clear snapshot flag and drop reference.
1698 ip->i_flags &= ~SF_SNAPSHOT;
1699 DIP_SET(ip, i_flags, ip->i_flags);
1700 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1702 * The dirtied indirects must be written out before
1703 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1704 * may find indirect pointers using the magic BLK_* values.
1706 if (DOINGSOFTDEP(vp))
1707 ffs_syncvnode(vp, MNT_WAIT, 0);
1710 * Reenable disk quotas for ex-snapshot file.
1712 if (!getinoquota(ip))
1713 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1718 * Notification that a block is being freed. Return zero if the free
1719 * should be allowed to proceed. Return non-zero if the snapshot file
1720 * wants to claim the block. The block will be claimed if it is an
1721 * uncopied part of one of the snapshots. It will be freed if it is
1722 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1723 * If a fragment is being freed, then all snapshots that care about
1724 * it must make a copy since a snapshot file can only claim full sized
1725 * blocks. Note that if more than one snapshot file maps the block,
1726 * we can pick one at random to claim it. Since none of the snapshots
1727 * can change, we are assurred that they will all see the same unmodified
1728 * image. When deleting a snapshot file (see ffs_snapremove above), we
1729 * must push any of these claimed blocks to one of the other snapshots
1730 * that maps it. These claimed blocks are easily identified as they will
1731 * have a block number equal to their logical block number within the
1732 * snapshot. A copied block can never have this property because they
1733 * must always have been allocated from a BLK_NOCOPY location.
1736 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1738 struct vnode *devvp;
1743 struct workhead *wkhd;
1745 struct buf *ibp, *cbp, *savedcbp = 0;
1746 struct thread *td = curthread;
1748 struct vnode *vp = NULL;
1751 int indiroff = 0, error = 0, claimedblk = 0;
1752 struct snapdata *sn;
1754 lbn = fragstoblks(fs, bno);
1757 sn = devvp->v_rdev->si_snapdata;
1762 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1763 VI_MTX(devvp)) != 0)
1765 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1767 if (DOINGSOFTDEP(vp))
1768 softdep_prealloc(vp, MNT_WAIT);
1770 * Lookup block being written.
1773 blkno = DIP(ip, i_db[lbn]);
1775 td->td_pflags |= TDP_COWINPROGRESS;
1776 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1777 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1778 td->td_pflags &= ~TDP_COWINPROGRESS;
1781 indiroff = (lbn - NDADDR) % NINDIR(fs);
1782 if (ip->i_ump->um_fstype == UFS1)
1783 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1785 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1788 * Check to see if block needs to be copied.
1792 * A block that we map is being freed. If it has not
1793 * been claimed yet, we will claim or copy it (below).
1796 } else if (blkno == BLK_SNAP) {
1798 * No previous snapshot claimed the block,
1799 * so it will be freed and become a BLK_NOCOPY
1800 * (don't care) for us.
1803 panic("snapblkfree: inconsistent block type");
1805 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1806 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1807 } else if (ip->i_ump->um_fstype == UFS1) {
1808 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1812 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1817 } else /* BLK_NOCOPY or default */ {
1819 * If the snapshot has already copied the block
1820 * (default), or does not care about the block,
1828 * If this is a full size block, we will just grab it
1829 * and assign it to the snapshot inode. Otherwise we
1830 * will proceed to copy it. See explanation for this
1831 * routine as to why only a single snapshot needs to
1834 if (size == fs->fs_bsize) {
1837 printf("%s %d lbn %jd from inum %d\n",
1838 "Grabonremove: snapino", ip->i_number,
1839 (intmax_t)lbn, inum);
1842 * If journaling is tracking this write we must add
1843 * the work to the inode or indirect being written.
1847 softdep_inode_append(ip,
1848 curthread->td_ucred, wkhd);
1850 softdep_buf_append(ibp, wkhd);
1853 DIP_SET(ip, i_db[lbn], bno);
1854 } else if (ip->i_ump->um_fstype == UFS1) {
1855 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1858 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1861 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1862 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1863 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1869 * Allocate the block into which to do the copy. Note that this
1870 * allocation will never require any additional allocations for
1871 * the snapshot inode.
1873 td->td_pflags |= TDP_COWINPROGRESS;
1874 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1875 fs->fs_bsize, KERNCRED, 0, &cbp);
1876 td->td_pflags &= ~TDP_COWINPROGRESS;
1881 printf("%s%d lbn %jd %s %d size %ld to blkno %jd\n",
1882 "Copyonremove: snapino ", ip->i_number,
1883 (intmax_t)lbn, "for inum", inum, size,
1884 (intmax_t)cbp->b_blkno);
1887 * If we have already read the old block contents, then
1888 * simply copy them to the new block. Note that we need
1889 * to synchronously write snapshots that have not been
1890 * unlinked, and hence will be visible after a crash,
1891 * to ensure their integrity. At a minimum we ensure the
1892 * integrity of the filesystem metadata, but use the
1893 * dopersistence sysctl-setable flag to decide on the
1894 * persistence needed for file content data.
1896 if (savedcbp != 0) {
1897 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1899 if ((vtype == VDIR || dopersistence) &&
1901 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1905 * Otherwise, read the old block contents into the buffer.
1907 if ((error = readblock(vp, cbp, lbn)) != 0) {
1908 bzero(cbp->b_data, fs->fs_bsize);
1910 if ((vtype == VDIR || dopersistence) &&
1912 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1918 * Note that we need to synchronously write snapshots that
1919 * have not been unlinked, and hence will be visible after
1920 * a crash, to ensure their integrity. At a minimum we
1921 * ensure the integrity of the filesystem metadata, but
1922 * use the dopersistence sysctl-setable flag to decide on
1923 * the persistence needed for file content data.
1926 vp = savedcbp->b_vp;
1928 if ((vtype == VDIR || dopersistence) &&
1929 VTOI(vp)->i_effnlink > 0)
1930 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1933 * If we have been unable to allocate a block in which to do
1934 * the copy, then return non-zero so that the fragment will
1935 * not be freed. Although space will be lost, the snapshot
1936 * will stay consistent.
1938 if (error != 0 && wkhd != NULL)
1939 softdep_freework(wkhd);
1940 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1945 * Associate snapshot files when mounting.
1948 ffs_snapshot_mount(mp)
1951 struct ufsmount *ump = VFSTOUFS(mp);
1952 struct vnode *devvp = ump->um_devvp;
1953 struct fs *fs = ump->um_fs;
1954 struct thread *td = curthread;
1955 struct snapdata *sn;
1957 struct vnode *lastvp;
1963 daddr_t snaplistsize;
1964 int error, snaploc, loc;
1967 * XXX The following needs to be set before ffs_truncate or
1968 * VOP_READ can be called.
1970 mp->mnt_stat.f_iosize = fs->fs_bsize;
1972 * Process each snapshot listed in the superblock.
1977 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1978 if (fs->fs_snapinum[snaploc] == 0)
1980 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1981 LK_EXCLUSIVE, &vp)) != 0){
1982 printf("ffs_snapshot_mount: vget failed %d\n", error);
1986 if (!IS_SNAPSHOT(ip) || ip->i_size ==
1987 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1988 if (!IS_SNAPSHOT(ip)) {
1989 reason = "non-snapshot";
1991 reason = "old format snapshot";
1992 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
1993 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
1995 printf("ffs_snapshot_mount: %s inode %d\n",
1996 reason, fs->fs_snapinum[snaploc]);
1999 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2000 if (fs->fs_snapinum[loc] == 0)
2002 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2004 fs->fs_snapinum[loc - 1] = 0;
2009 * Acquire a lock on the snapdata structure, creating it if
2012 sn = ffs_snapdata_acquire(devvp);
2014 * Change vnode to use shared snapshot lock instead of the
2015 * original private lock.
2017 vp->v_vnlock = &sn->sn_lock;
2018 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2020 * Link it onto the active snapshot list.
2023 if (ip->i_nextsnap.tqe_prev != 0)
2024 panic("ffs_snapshot_mount: %d already on list",
2027 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2028 vp->v_vflag |= VV_SYSTEM;
2035 * No usable snapshots found.
2037 if (sn == NULL || vp == NULL)
2040 * Allocate the space for the block hints list. We always want to
2041 * use the list from the newest snapshot.
2043 auio.uio_iov = &aiov;
2044 auio.uio_iovcnt = 1;
2045 aiov.iov_base = (void *)&snaplistsize;
2046 aiov.iov_len = sizeof(snaplistsize);
2047 auio.uio_resid = aiov.iov_len;
2049 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2050 auio.uio_segflg = UIO_SYSSPACE;
2051 auio.uio_rw = UIO_READ;
2053 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2054 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2055 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2059 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2060 M_UFSMNT, M_WAITOK);
2061 auio.uio_iovcnt = 1;
2062 aiov.iov_base = snapblklist;
2063 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2064 auio.uio_resid = aiov.iov_len;
2065 auio.uio_offset -= sizeof(snaplistsize);
2066 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2067 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2069 free(snapblklist, M_UFSMNT);
2074 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2075 sn->sn_listsize = snaplistsize;
2076 sn->sn_blklist = (daddr_t *)snapblklist;
2077 devvp->v_vflag |= VV_COPYONWRITE;
2082 * Disassociate snapshot files when unmounting.
2085 ffs_snapshot_unmount(mp)
2088 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2089 struct snapdata *sn;
2094 sn = devvp->v_rdev->si_snapdata;
2095 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2097 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2098 xp->i_nextsnap.tqe_prev = 0;
2099 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2101 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2102 KASSERT(vp->v_vnlock == &sn->sn_lock,
2103 ("ffs_snapshot_unmount: lost lock mutation"));
2104 vp->v_vnlock = &vp->v_lock;
2105 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2106 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2107 if (xp->i_effnlink > 0)
2110 sn = devvp->v_rdev->si_snapdata;
2112 try_free_snapdata(devvp);
2113 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2117 * Check the buffer block to be belong to device buffer that shall be
2118 * locked after snaplk. devvp shall be locked on entry, and will be
2119 * leaved locked upon exit.
2122 ffs_bp_snapblk(devvp, bp)
2123 struct vnode *devvp;
2126 struct snapdata *sn;
2128 ufs2_daddr_t lbn, *snapblklist;
2129 int lower, upper, mid;
2131 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2132 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2133 sn = devvp->v_rdev->si_snapdata;
2134 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2136 fs = TAILQ_FIRST(&sn->sn_head)->i_fs;
2137 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2138 snapblklist = sn->sn_blklist;
2139 upper = sn->sn_listsize - 1;
2141 while (lower <= upper) {
2142 mid = (lower + upper) / 2;
2143 if (snapblklist[mid] == lbn)
2145 if (snapblklist[mid] < lbn)
2161 struct vnode *vp, *devvp;
2165 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2170 devvp = bo->__bo_vnode;
2171 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2174 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2178 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2179 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2184 * Try to find a buffer to flush.
2186 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2187 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2189 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2192 panic("bdwrite: found ourselves");
2195 * Don't countdeps with the bo lock
2198 if (buf_countdeps(nbp, 0)) {
2205 if (!ffs_bp_snapblk(vp, nbp)) {
2206 if (BO_MTX(bo) != VI_MTX(vp)) {
2215 if (nbp->b_flags & B_CLUSTEROK) {
2216 vfs_bio_awrite(nbp);
2221 dirtybufferflushes++;
2230 * Check for need to copy block that is about to be written,
2231 * copying the block if necessary.
2234 ffs_copyonwrite(devvp, bp)
2235 struct vnode *devvp;
2238 struct snapdata *sn;
2239 struct buf *ibp, *cbp, *savedcbp = 0;
2240 struct thread *td = curthread;
2243 struct vnode *vp = 0;
2244 ufs2_daddr_t lbn, blkno, *snapblklist;
2245 int lower, upper, mid, indiroff, error = 0;
2246 int launched_async_io, prev_norunningbuf;
2247 long saved_runningbufspace;
2249 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2250 return (0); /* Update on a snapshot file */
2251 if (td->td_pflags & TDP_COWINPROGRESS)
2252 panic("ffs_copyonwrite: recursive call");
2254 * First check to see if it is in the preallocated list.
2255 * By doing this check we avoid several potential deadlocks.
2258 sn = devvp->v_rdev->si_snapdata;
2260 TAILQ_EMPTY(&sn->sn_head)) {
2262 return (0); /* No snapshot */
2264 ip = TAILQ_FIRST(&sn->sn_head);
2266 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2267 snapblklist = sn->sn_blklist;
2268 upper = sn->sn_listsize - 1;
2270 while (lower <= upper) {
2271 mid = (lower + upper) / 2;
2272 if (snapblklist[mid] == lbn)
2274 if (snapblklist[mid] < lbn)
2279 if (lower <= upper) {
2283 launched_async_io = 0;
2284 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2286 * Since I/O on bp isn't yet in progress and it may be blocked
2287 * for a long time waiting on snaplk, back it out of
2288 * runningbufspace, possibly waking other threads waiting for space.
2290 saved_runningbufspace = bp->b_runningbufspace;
2291 if (saved_runningbufspace != 0)
2292 runningbufwakeup(bp);
2294 * Not in the precomputed list, so check the snapshots.
2296 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2297 VI_MTX(devvp)) != 0) {
2299 sn = devvp->v_rdev->si_snapdata;
2301 TAILQ_EMPTY(&sn->sn_head)) {
2303 if (saved_runningbufspace != 0) {
2304 bp->b_runningbufspace = saved_runningbufspace;
2305 atomic_add_long(&runningbufspace,
2306 bp->b_runningbufspace);
2308 return (0); /* Snapshot gone */
2311 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2313 if (DOINGSOFTDEP(vp))
2314 softdep_prealloc(vp, MNT_WAIT);
2316 * We ensure that everything of our own that needs to be
2317 * copied will be done at the time that ffs_snapshot is
2318 * called. Thus we can skip the check here which can
2319 * deadlock in doing the lookup in UFS_BALLOC.
2324 * Check to see if block needs to be copied. We do not have
2325 * to hold the snapshot lock while doing this lookup as it
2326 * will never require any additional allocations for the
2330 blkno = DIP(ip, i_db[lbn]);
2332 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2333 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2334 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2335 td->td_pflags &= ~TDP_COWINPROGRESS;
2338 indiroff = (lbn - NDADDR) % NINDIR(fs);
2339 if (ip->i_ump->um_fstype == UFS1)
2340 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2342 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2346 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2347 panic("ffs_copyonwrite: bad copy block");
2352 * Allocate the block into which to do the copy. Since
2353 * multiple processes may all try to copy the same block,
2354 * we have to recheck our need to do a copy if we sleep
2355 * waiting for the lock.
2357 * Because all snapshots on a filesystem share a single
2358 * lock, we ensure that we will never be in competition
2359 * with another process to allocate a block.
2361 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2362 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2363 fs->fs_bsize, KERNCRED, 0, &cbp);
2364 td->td_pflags &= ~TDP_COWINPROGRESS;
2369 printf("Copyonwrite: snapino %d lbn %jd for ",
2370 ip->i_number, (intmax_t)lbn);
2371 if (bp->b_vp == devvp)
2372 printf("fs metadata");
2374 printf("inum %d", VTOI(bp->b_vp)->i_number);
2375 printf(" lblkno %jd to blkno %jd\n",
2376 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2380 * If we have already read the old block contents, then
2381 * simply copy them to the new block. Note that we need
2382 * to synchronously write snapshots that have not been
2383 * unlinked, and hence will be visible after a crash,
2384 * to ensure their integrity. At a minimum we ensure the
2385 * integrity of the filesystem metadata, but use the
2386 * dopersistence sysctl-setable flag to decide on the
2387 * persistence needed for file content data.
2389 if (savedcbp != 0) {
2390 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2392 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2393 dopersistence) && ip->i_effnlink > 0)
2394 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2396 launched_async_io = 1;
2400 * Otherwise, read the old block contents into the buffer.
2402 if ((error = readblock(vp, cbp, lbn)) != 0) {
2403 bzero(cbp->b_data, fs->fs_bsize);
2405 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2406 dopersistence) && ip->i_effnlink > 0)
2407 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2409 launched_async_io = 1;
2415 * Note that we need to synchronously write snapshots that
2416 * have not been unlinked, and hence will be visible after
2417 * a crash, to ensure their integrity. At a minimum we
2418 * ensure the integrity of the filesystem metadata, but
2419 * use the dopersistence sysctl-setable flag to decide on
2420 * the persistence needed for file content data.
2423 vp = savedcbp->b_vp;
2425 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2426 dopersistence) && VTOI(vp)->i_effnlink > 0)
2427 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2429 launched_async_io = 1;
2431 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2432 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2434 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2435 waitrunningbufspace();
2437 * I/O on bp will now be started, so count it in runningbufspace.
2439 if (saved_runningbufspace != 0) {
2440 bp->b_runningbufspace = saved_runningbufspace;
2441 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2447 * sync snapshots to force freework records waiting on snapshots to claim
2451 ffs_sync_snap(mp, waitfor)
2455 struct snapdata *sn;
2456 struct vnode *devvp;
2460 devvp = VFSTOUFS(mp)->um_devvp;
2461 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2465 sn = devvp->v_rdev->si_snapdata;
2470 if (lockmgr(&sn->sn_lock,
2471 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2472 VI_MTX(devvp)) == 0)
2475 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2477 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2479 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2483 * Read the specified block into the given buffer.
2484 * Much of this boiler-plate comes from bwrite().
2487 readblock(vp, bp, lbn)
2492 struct inode *ip = VTOI(vp);
2495 bip = g_alloc_bio();
2496 bip->bio_cmd = BIO_READ;
2497 bip->bio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
2498 bip->bio_data = bp->b_data;
2499 bip->bio_length = bp->b_bcount;
2500 bip->bio_done = NULL;
2502 g_io_request(bip, ip->i_devvp->v_bufobj.bo_private);
2503 bp->b_error = biowait(bip, "snaprdb");
2505 return (bp->b_error);
2511 * Process file deletes that were deferred by ufs_inactive() due to
2512 * the file system being suspended. Transfer IN_LAZYACCESS into
2513 * IN_MODIFIED for vnodes that were accessed during suspension.
2516 process_deferred_inactive(struct mount *mp)
2518 struct vnode *vp, *mvp;
2524 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2526 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2528 * IN_LAZYACCESS is checked here without holding any
2529 * vnode lock, but this flag is set only while holding
2532 if (vp->v_type == VNON ||
2533 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2534 ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
2539 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2542 if (error == ENOENT)
2543 continue; /* vnode recycled */
2544 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2548 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2549 ip->i_flag &= ~IN_LAZYACCESS;
2550 ip->i_flag |= IN_MODIFIED;
2553 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2560 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2561 ("process_deferred_inactive: got VI_OWEINACT"));
2566 vn_finished_secondary_write(mp);
2569 #ifndef NO_FFS_SNAPSHOT
2571 static struct snapdata *
2572 ffs_snapdata_alloc(void)
2574 struct snapdata *sn;
2577 * Fetch a snapdata from the free list if there is one available.
2579 mtx_lock(&snapfree_lock);
2580 sn = LIST_FIRST(&snapfree);
2582 LIST_REMOVE(sn, sn_link);
2583 mtx_unlock(&snapfree_lock);
2587 * If there were no free snapdatas allocate one.
2589 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2590 TAILQ_INIT(&sn->sn_head);
2591 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2592 LK_CANRECURSE | LK_NOSHARE);
2597 * The snapdata is never freed because we can not be certain that
2598 * there are no threads sleeping on the snap lock. Persisting
2599 * them permanently avoids costly synchronization in ffs_lock().
2602 ffs_snapdata_free(struct snapdata *sn)
2604 mtx_lock(&snapfree_lock);
2605 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2606 mtx_unlock(&snapfree_lock);
2609 /* Try to free snapdata associated with devvp */
2611 try_free_snapdata(struct vnode *devvp)
2613 struct snapdata *sn;
2614 ufs2_daddr_t *snapblklist;
2616 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2617 sn = devvp->v_rdev->si_snapdata;
2619 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2620 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2625 devvp->v_rdev->si_snapdata = NULL;
2626 devvp->v_vflag &= ~VV_COPYONWRITE;
2627 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2628 snapblklist = sn->sn_blklist;
2629 sn->sn_blklist = NULL;
2630 sn->sn_listsize = 0;
2631 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2632 if (snapblklist != NULL)
2633 free(snapblklist, M_UFSMNT);
2634 ffs_snapdata_free(sn);
2637 static struct snapdata *
2638 ffs_snapdata_acquire(struct vnode *devvp)
2640 struct snapdata *nsn;
2641 struct snapdata *sn;
2644 * Allocate a free snapdata. This is done before acquiring the
2645 * devvp lock to avoid allocation while the devvp interlock is
2648 nsn = ffs_snapdata_alloc();
2650 * If there snapshots already exist on this filesystem grab a
2651 * reference to the shared lock. Otherwise this is the first
2652 * snapshot on this filesystem and we need to use our
2653 * pre-allocated snapdata.
2656 if (devvp->v_rdev->si_snapdata == NULL) {
2657 devvp->v_rdev->si_snapdata = nsn;
2660 sn = devvp->v_rdev->si_snapdata;
2662 * Acquire the snapshot lock.
2664 lockmgr(&sn->sn_lock,
2665 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, VI_MTX(devvp));
2667 * Free any unused snapdata.
2670 ffs_snapdata_free(nsn);