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/rwlock.h>
57 #include <sys/vnode.h>
59 #include <geom/geom.h>
61 #include <ufs/ufs/extattr.h>
62 #include <ufs/ufs/quota.h>
63 #include <ufs/ufs/ufsmount.h>
64 #include <ufs/ufs/inode.h>
65 #include <ufs/ufs/ufs_extern.h>
67 #include <ufs/ffs/fs.h>
68 #include <ufs/ffs/ffs_extern.h>
70 #define KERNCRED thread0.td_ucred
75 #ifdef NO_FFS_SNAPSHOT
77 ffs_snapshot(mp, snapfile)
85 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
92 struct workhead *wkhd;
104 ffs_snapshot_mount(mp)
110 ffs_snapshot_unmount(mp)
122 ffs_copyonwrite(devvp, bp)
130 ffs_sync_snap(mp, waitfor)
137 FEATURE(ffs_snapshot, "FFS snapshot support");
139 LIST_HEAD(, snapdata) snapfree;
140 static struct mtx snapfree_lock;
141 MTX_SYSINIT(ffs_snapfree, &snapfree_lock, "snapdata free list", MTX_DEF);
143 static int cgaccount(int, struct vnode *, struct buf *, int);
144 static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
145 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
146 ufs_lbn_t, int), int, int);
147 static int indiracct_ufs1(struct vnode *, struct vnode *, int,
148 ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
149 int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
150 ufs_lbn_t, int), int);
151 static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
152 struct fs *, ufs_lbn_t, int);
153 static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
154 struct fs *, ufs_lbn_t, int);
155 static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
156 struct fs *, ufs_lbn_t, int);
157 static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
158 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
159 ufs_lbn_t, int), int, int);
160 static int indiracct_ufs2(struct vnode *, struct vnode *, int,
161 ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
162 int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
163 ufs_lbn_t, int), int);
164 static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
165 struct fs *, ufs_lbn_t, int);
166 static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
167 struct fs *, ufs_lbn_t, int);
168 static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
169 struct fs *, ufs_lbn_t, int);
170 static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
171 static void try_free_snapdata(struct vnode *devvp);
172 static struct snapdata *ffs_snapdata_acquire(struct vnode *devvp);
173 static int ffs_bp_snapblk(struct vnode *, struct buf *);
176 * To ensure the consistency of snapshots across crashes, we must
177 * synchronously write out copied blocks before allowing the
178 * originals to be modified. Because of the rather severe speed
179 * penalty that this imposes, the code normally only ensures
180 * persistence for the filesystem metadata contained within a
181 * snapshot. Setting the following flag allows this crash
182 * persistence to be enabled for file contents.
184 int dopersistence = 0;
187 #include <sys/sysctl.h>
188 SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
189 static int snapdebug = 0;
190 SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
191 int collectsnapstats = 0;
192 SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
197 * Create a snapshot file and initialize it for the filesystem.
200 ffs_snapshot(mp, snapfile)
204 ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
205 int error, cg, snaploc;
206 int i, size, len, loc;
207 ufs2_daddr_t blockno;
209 struct timespec starttime = {0, 0}, endtime;
211 long redo = 0, snaplistsize = 0;
214 struct fs *copy_fs = NULL, *fs;
215 struct thread *td = curthread;
216 struct inode *ip, *xp;
217 struct buf *bp, *nbp, *ibp;
221 struct vnode *vp, *xvp, *mvp, *devvp;
225 struct ufsmount *ump;
231 * At the moment, journaled soft updates cannot support
234 if (MOUNTEDSUJ(mp)) {
235 vfs_mount_error(mp, "%s: Snapshots are not yet supported when "
236 "running with journaled soft updates", fs->fs_fsmnt);
243 * Need to serialize access to snapshot code per filesystem.
246 * Assign a snapshot slot in the superblock.
249 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
250 if (fs->fs_snapinum[snaploc] == 0)
253 if (snaploc == FSMAXSNAP)
256 * Create the snapshot file.
259 NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE, snapfile, td);
260 if ((error = namei(&nd)) != 0)
262 if (nd.ni_vp != NULL) {
266 if (nd.ni_dvp->v_mount != mp)
269 NDFREE(&nd, NDF_ONLY_PNBUF);
270 if (nd.ni_dvp == nd.ni_vp)
278 vat.va_mode = S_IRUSR;
279 vat.va_vaflags |= VA_EXCLUSIVE;
280 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
283 panic("ffs_snapshot: mount mismatch");
285 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
286 NDFREE(&nd, NDF_ONLY_PNBUF);
288 if ((error = vn_start_write(NULL, &wrtmp,
289 V_XSLEEP | PCATCH)) != 0)
293 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
294 VOP_UNLOCK(nd.ni_dvp, 0);
296 NDFREE(&nd, NDF_ONLY_PNBUF);
297 vn_finished_write(wrtmp);
302 vp->v_vflag |= VV_SYSTEM;
306 * Allocate and copy the last block contents so as to be able
307 * to set size to that of the filesystem.
309 numblks = howmany(fs->fs_size, fs->fs_frag);
310 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
311 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
314 ip->i_size = lblktosize(fs, (off_t)numblks);
315 DIP_SET(ip, i_size, ip->i_size);
316 ip->i_flag |= IN_CHANGE | IN_UPDATE;
317 error = readblock(vp, bp, numblks - 1);
322 * Preallocate critical data structures so that we can copy
323 * them in without further allocation after we suspend all
324 * operations on the filesystem. We would like to just release
325 * the allocated buffers without writing them since they will
326 * be filled in below once we are ready to go, but this upsets
327 * the soft update code, so we go ahead and write the new buffers.
329 * Allocate all indirect blocks and mark all of them as not
330 * needing to be copied.
332 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
333 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
334 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
340 * Allocate copies for the superblock and its summary information.
342 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
347 blkno = fragstoblks(fs, fs->fs_csaddr);
348 len = howmany(fs->fs_cssize, fs->fs_bsize);
349 for (loc = 0; loc < len; loc++) {
350 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
351 fs->fs_bsize, KERNCRED, 0, &nbp);
357 * Allocate all cylinder group blocks.
359 for (cg = 0; cg < fs->fs_ncg; cg++) {
360 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
361 fs->fs_bsize, KERNCRED, 0, &nbp);
366 ffs_syncvnode(vp, MNT_WAIT, 0);
369 * Copy all the cylinder group maps. Although the
370 * filesystem is still active, we hope that only a few
371 * cylinder groups will change between now and when we
372 * suspend operations. Thus, we will be able to quickly
373 * touch up the few cylinder groups that changed during
374 * the suspension period.
376 len = howmany(fs->fs_ncg, NBBY);
377 space = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
379 fs->fs_active = space;
381 for (cg = 0; cg < fs->fs_ncg; cg++) {
382 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
383 fs->fs_bsize, KERNCRED, 0, &nbp);
386 error = cgaccount(cg, vp, nbp, 1);
389 ffs_syncvnode(vp, MNT_WAIT, 0);
394 * Change inode to snapshot type file.
396 ip->i_flags |= SF_SNAPSHOT;
397 DIP_SET(ip, i_flags, ip->i_flags);
398 ip->i_flag |= IN_CHANGE | IN_UPDATE;
400 * Ensure that the snapshot is completely on disk.
401 * Since we have marked it as a snapshot it is safe to
402 * unlock it as no process will be allowed to write to it.
404 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
408 * All allocations are done, so we can now snapshot the system.
410 * Recind nice scheduling while running with the filesystem suspended.
412 if (td->td_proc->p_nice > 0) {
417 saved_nice = p->p_nice;
422 * Suspend operation on filesystem.
425 vn_finished_write(wrtmp);
426 if ((error = vfs_write_suspend(vp->v_mount, 0)) != 0) {
427 vn_start_write(NULL, &wrtmp, V_WAIT);
428 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
431 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
433 vn_start_write(NULL, &wrtmp, V_WAIT);
435 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
436 if (ip->i_effnlink == 0) {
437 error = ENOENT; /* Snapshot file unlinked */
440 if (collectsnapstats)
441 nanotime(&starttime);
443 /* The last block might have changed. Copy it again to be sure. */
444 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
445 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
448 error = readblock(vp, bp, numblks - 1);
449 bp->b_flags |= B_VALIDSUSPWRT;
454 * First, copy all the cylinder group maps that have changed.
456 for (cg = 0; cg < fs->fs_ncg; cg++) {
457 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
460 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
461 fs->fs_bsize, KERNCRED, 0, &nbp);
464 error = cgaccount(cg, vp, nbp, 2);
470 * Grab a copy of the superblock and its summary information.
471 * We delay writing it until the suspension is released below.
473 copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK);
474 bcopy(fs, copy_fs, fs->fs_sbsize);
475 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
476 copy_fs->fs_clean = 1;
477 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
478 if (fs->fs_sbsize < size)
479 bzero(&((char *)copy_fs)[fs->fs_sbsize],
480 size - fs->fs_sbsize);
481 size = blkroundup(fs, fs->fs_cssize);
482 if (fs->fs_contigsumsize > 0)
483 size += fs->fs_ncg * sizeof(int32_t);
484 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
485 copy_fs->fs_csp = space;
486 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
487 space = (char *)space + fs->fs_cssize;
488 loc = howmany(fs->fs_cssize, fs->fs_fsize);
489 i = fs->fs_frag - loc % fs->fs_frag;
490 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
492 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
493 len, KERNCRED, &bp)) != 0) {
495 free(copy_fs->fs_csp, M_UFSMNT);
496 free(copy_fs, M_UFSMNT);
500 bcopy(bp->b_data, space, (u_int)len);
501 space = (char *)space + len;
502 bp->b_flags |= B_INVAL | B_NOCACHE;
505 if (fs->fs_contigsumsize > 0) {
506 copy_fs->fs_maxcluster = lp = space;
507 for (i = 0; i < fs->fs_ncg; i++)
508 *lp++ = fs->fs_contigsumsize;
511 * We must check for active files that have been unlinked
512 * (e.g., with a zero link count). We have to expunge all
513 * trace of these files from the snapshot so that they are
514 * not reclaimed prematurely by fsck or unnecessarily dumped.
515 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
516 * spec_strategy about writing on a suspended filesystem.
517 * Note that we skip unlinked snapshot files as they will
518 * be handled separately below.
520 * We also calculate the needed size for the snapshot list.
522 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
523 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
525 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
528 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
529 if ((xvp->v_usecount == 0 &&
530 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
531 xvp->v_type == VNON ||
532 IS_SNAPSHOT(VTOI(xvp))) {
537 * We can skip parent directory vnode because it must have
538 * this snapshot file in it.
540 if (xvp == nd.ni_dvp) {
545 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
546 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
551 if (xvp->v_usecount == 0 &&
552 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
560 vprint("ffs_snapshot: busy vnode", xvp);
561 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
568 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
574 * If there is a fragment, clear it here.
577 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
579 len = fragroundup(fs, blkoff(fs, xp->i_size));
580 if (len != 0 && len < fs->fs_bsize) {
581 ffs_blkfree(ump, copy_fs, vp,
582 DIP(xp, i_db[loc]), len, xp->i_number,
584 blkno = DIP(xp, i_db[loc]);
585 DIP_SET(xp, i_db[loc], 0);
589 if (xp->i_ump->um_fstype == UFS1)
590 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
593 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
596 DIP_SET(xp, i_db[loc], blkno);
598 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
603 free(copy_fs->fs_csp, M_UFSMNT);
604 free(copy_fs, M_UFSMNT);
606 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
611 * Erase the journal file from the snapshot.
613 if (fs->fs_flags & FS_SUJ) {
614 error = softdep_journal_lookup(mp, &xvp);
616 free(copy_fs->fs_csp, M_UFSMNT);
617 free(copy_fs, M_UFSMNT);
622 if (xp->i_ump->um_fstype == UFS1)
623 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
626 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
631 * Acquire a lock on the snapdata structure, creating it if necessary.
633 sn = ffs_snapdata_acquire(devvp);
635 * Change vnode to use shared snapshot lock instead of the original
638 vp->v_vnlock = &sn->sn_lock;
639 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
640 xp = TAILQ_FIRST(&sn->sn_head);
642 * If this is the first snapshot on this filesystem, then we need
643 * to allocate the space for the list of preallocated snapshot blocks.
644 * This list will be refined below, but this preliminary one will
645 * keep us out of deadlock until the full one is ready.
648 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
650 blkp = &snapblklist[1];
651 *blkp++ = lblkno(fs, fs->fs_sblockloc);
652 blkno = fragstoblks(fs, fs->fs_csaddr);
653 for (cg = 0; cg < fs->fs_ncg; cg++) {
654 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
656 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
658 len = howmany(fs->fs_cssize, fs->fs_bsize);
659 for (loc = 0; loc < len; loc++)
660 *blkp++ = blkno + loc;
661 for (; cg < fs->fs_ncg; cg++)
662 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
663 snapblklist[0] = blkp - snapblklist;
665 if (sn->sn_blklist != NULL)
666 panic("ffs_snapshot: non-empty list");
667 sn->sn_blklist = snapblklist;
668 sn->sn_listsize = blkp - snapblklist;
672 * Record snapshot inode. Since this is the newest snapshot,
673 * it must be placed at the end of the list.
676 fs->fs_snapinum[snaploc] = ip->i_number;
677 if (ip->i_nextsnap.tqe_prev != 0)
678 panic("ffs_snapshot: %ju already on list",
679 (uintmax_t)ip->i_number);
680 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
681 devvp->v_vflag |= VV_COPYONWRITE;
683 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
685 KASSERT((sn != NULL && copy_fs != NULL && error == 0) ||
686 (sn == NULL && copy_fs == NULL && error != 0),
687 ("email phk@ and mckusick@"));
689 * Resume operation on filesystem.
691 vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR);
692 if (collectsnapstats && starttime.tv_sec > 0) {
694 timespecsub(&endtime, &starttime);
695 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
696 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
697 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
702 * Copy allocation information from all the snapshots in
703 * this snapshot and then expunge them from its view.
705 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
708 if (xp->i_ump->um_fstype == UFS1)
709 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
712 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
714 if (error == 0 && xp->i_effnlink == 0) {
715 error = ffs_freefile(ump,
722 fs->fs_snapinum[snaploc] = 0;
727 * Allocate space for the full list of preallocated snapshot blocks.
729 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
731 ip->i_snapblklist = &snapblklist[1];
733 * Expunge the blocks used by the snapshots from the set of
734 * blocks marked as used in the snapshot bitmaps. Also, collect
735 * the list of allocated blocks in i_snapblklist.
737 if (ip->i_ump->um_fstype == UFS1)
738 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
741 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
744 fs->fs_snapinum[snaploc] = 0;
745 free(snapblklist, M_UFSMNT);
748 if (snaplistsize < ip->i_snapblklist - snapblklist)
749 panic("ffs_snapshot: list too small");
750 snaplistsize = ip->i_snapblklist - snapblklist;
751 snapblklist[0] = snaplistsize;
752 ip->i_snapblklist = 0;
754 * Write out the list of allocated blocks to the end of the snapshot.
756 auio.uio_iov = &aiov;
758 aiov.iov_base = (void *)snapblklist;
759 aiov.iov_len = snaplistsize * sizeof(daddr_t);
760 auio.uio_resid = aiov.iov_len;
761 auio.uio_offset = ip->i_size;
762 auio.uio_segflg = UIO_SYSSPACE;
763 auio.uio_rw = UIO_WRITE;
765 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
766 fs->fs_snapinum[snaploc] = 0;
767 free(snapblklist, M_UFSMNT);
771 * Write the superblock and its summary information
774 blkno = fragstoblks(fs, fs->fs_csaddr);
775 len = howmany(fs->fs_cssize, fs->fs_bsize);
776 space = copy_fs->fs_csp;
777 for (loc = 0; loc < len; loc++) {
778 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
781 fs->fs_snapinum[snaploc] = 0;
782 free(snapblklist, M_UFSMNT);
785 bcopy(space, nbp->b_data, fs->fs_bsize);
786 space = (char *)space + fs->fs_bsize;
789 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
794 loc = blkoff(fs, fs->fs_sblockloc);
795 bcopy((char *)copy_fs, &nbp->b_data[loc], (u_int)fs->fs_sbsize);
799 * As this is the newest list, it is the most inclusive, so
800 * should replace the previous list.
803 space = sn->sn_blklist;
804 sn->sn_blklist = snapblklist;
805 sn->sn_listsize = snaplistsize;
808 free(space, M_UFSMNT);
810 * Preallocate all the direct blocks in the snapshot inode so
811 * that we never have to write the inode itself to commit an
812 * update to the contents of the snapshot. Note that once
813 * created, the size of the snapshot will never change, so
814 * there will never be a need to write the inode except to
815 * update the non-integrity-critical time fields and
816 * allocated-block count.
818 for (blockno = 0; blockno < NDADDR; blockno++) {
819 if (DIP(ip, i_db[blockno]) != 0)
821 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
822 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
825 error = readblock(vp, bp, blockno);
831 free(copy_fs->fs_csp, M_UFSMNT);
832 free(copy_fs, M_UFSMNT);
835 NDFREE(&nd, NDF_ONLY_PNBUF);
836 if (saved_nice > 0) {
841 sched_nice(td->td_proc, saved_nice);
842 PROC_UNLOCK(td->td_proc);
845 if (fs->fs_active != 0) {
846 free(fs->fs_active, M_DEVBUF);
851 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
854 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED);
855 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
861 vn_finished_write(wrtmp);
862 process_deferred_inactive(mp);
867 * Copy a cylinder group map. All the unallocated blocks are marked
868 * BLK_NOCOPY so that the snapshot knows that it need not copy them
869 * if they are later written. If passno is one, then this is a first
870 * pass, so only setting needs to be done. If passno is 2, then this
871 * is a revision to a previous pass which must be undone as the
872 * replacement pass is done.
875 cgaccount(cg, vp, nbp, passno)
881 struct buf *bp, *ibp;
885 ufs2_daddr_t base, numblks;
886 int error, len, loc, indiroff;
890 error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
891 (int)fs->fs_cgsize, KERNCRED, &bp);
896 cgp = (struct cg *)bp->b_data;
897 if (!cg_chkmagic(cgp)) {
904 * Recomputation of summary information might not have been performed
905 * at mount time. Sync up summary information for current cylinder
906 * group while data is in memory to ensure that result of background
907 * fsck is slightly more consistent.
909 fs->fs_cs(fs, cg) = cgp->cg_cs;
910 UFS_UNLOCK(ip->i_ump);
911 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
912 if (fs->fs_cgsize < fs->fs_bsize)
913 bzero(&nbp->b_data[fs->fs_cgsize],
914 fs->fs_bsize - fs->fs_cgsize);
915 cgp = (struct cg *)nbp->b_data;
918 nbp->b_flags |= B_VALIDSUSPWRT;
919 numblks = howmany(fs->fs_size, fs->fs_frag);
920 len = howmany(fs->fs_fpg, fs->fs_frag);
921 base = cgbase(fs, cg) / fs->fs_frag;
922 if (base + len >= numblks)
923 len = numblks - base - 1;
926 for ( ; loc < NDADDR; loc++) {
927 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
928 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
929 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
930 DIP_SET(ip, i_db[loc], 0);
931 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
932 panic("ffs_snapshot: lost direct block");
935 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
936 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
940 indiroff = (base + loc - NDADDR) % NINDIR(fs);
941 for ( ; loc < len; loc++, indiroff++) {
942 if (indiroff >= NINDIR(fs)) {
944 ibp->b_flags |= B_VALIDSUSPWRT;
946 error = UFS_BALLOC(vp,
947 lblktosize(fs, (off_t)(base + loc)),
948 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
954 if (ip->i_ump->um_fstype == UFS1) {
955 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
956 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
958 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
959 [indiroff] == BLK_NOCOPY)
960 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
961 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
962 [indiroff] == BLK_NOCOPY)
963 panic("ffs_snapshot: lost indirect block");
966 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
967 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
968 else if (passno == 2 &&
969 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
970 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
971 else if (passno == 1 &&
972 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
973 panic("ffs_snapshot: lost indirect block");
976 ibp->b_flags |= B_VALIDSUSPWRT;
982 * Before expunging a snapshot inode, note all the
983 * blocks that it claims with BLK_SNAP so that fsck will
984 * be able to account for those blocks properly and so
985 * that this snapshot knows that it need not copy them
986 * if the other snapshot holding them is freed. This code
987 * is reproduced once each for UFS1 and UFS2.
990 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
991 struct vnode *snapvp;
992 struct inode *cancelip;
994 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
995 struct fs *, ufs_lbn_t, int);
999 int i, error, indiroff;
1000 ufs_lbn_t lbn, rlbn;
1001 ufs2_daddr_t len, blkno, numblks, blksperindir;
1002 struct ufs1_dinode *dip;
1003 struct thread *td = curthread;
1007 * Prepare to expunge the inode. If its inode block has not
1008 * yet been copied, then allocate and fill the copy.
1010 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1013 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1015 if (DOINGSOFTDEP(snapvp))
1016 softdep_prealloc(snapvp, MNT_WAIT);
1017 td->td_pflags |= TDP_COWINPROGRESS;
1018 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1019 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1020 td->td_pflags &= ~TDP_COWINPROGRESS;
1023 indiroff = (lbn - NDADDR) % NINDIR(fs);
1024 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1028 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1031 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1032 fs->fs_bsize, KERNCRED, 0, &bp);
1035 if ((error = readblock(snapvp, bp, lbn)) != 0)
1039 * Set a snapshot inode to be a zero length file, regular files
1040 * or unlinked snapshots to be completely unallocated.
1042 dip = (struct ufs1_dinode *)bp->b_data +
1043 ino_to_fsbo(fs, cancelip->i_number);
1044 if (clearmode || cancelip->i_effnlink == 0)
1048 dip->di_flags &= ~SF_SNAPSHOT;
1049 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1052 * Now go through and expunge all the blocks in the file
1053 * using the function requested.
1055 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1056 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1057 &cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
1059 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1060 &cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
1064 len = numblks - NDADDR;
1066 for (i = 0; len > 0 && i < NIADDR; i++) {
1067 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1068 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1069 blksperindir, fs, acctfunc, expungetype);
1072 blksperindir *= NINDIR(fs);
1073 lbn -= blksperindir + 1;
1074 len -= blksperindir;
1075 rlbn += blksperindir;
1081 * Descend an indirect block chain for vnode cancelvp accounting for all
1082 * its indirect blocks in snapvp.
1085 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1086 blksperindir, fs, acctfunc, expungetype)
1087 struct vnode *snapvp;
1088 struct vnode *cancelvp;
1094 ufs_lbn_t blksperindir;
1096 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1097 struct fs *, ufs_lbn_t, int);
1101 ufs_lbn_t subblksperindir;
1102 struct indir indirs[NIADDR + 2];
1103 ufs1_daddr_t last, *bap;
1107 if (expungetype == BLK_NOCOPY)
1109 panic("indiracct_ufs1: missing indir");
1111 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1113 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1114 panic("indiracct_ufs1: botched params");
1116 * We have to expand bread here since it will deadlock looking
1117 * up the block number for any blocks that are not in the cache.
1119 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1120 bp->b_blkno = fsbtodb(fs, blkno);
1121 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1122 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1127 * Account for the block pointers in this indirect block.
1129 last = howmany(remblks, blksperindir);
1130 if (last > NINDIR(fs))
1132 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1133 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1135 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1136 level == 0 ? rlbn : -1, expungetype);
1137 if (error || level == 0)
1140 * Account for the block pointers in each of the indirect blocks
1141 * in the levels below us.
1143 subblksperindir = blksperindir / NINDIR(fs);
1144 for (lbn++, level--, i = 0; i < last; i++) {
1145 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1146 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1149 rlbn += blksperindir;
1150 lbn -= blksperindir;
1151 remblks -= blksperindir;
1154 free(bap, M_DEVBUF);
1159 * Do both snap accounting and map accounting.
1162 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1164 ufs1_daddr_t *oldblkp, *lastblkp;
1167 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1171 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1173 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1177 * Identify a set of blocks allocated in a snapshot inode.
1180 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1182 ufs1_daddr_t *oldblkp, *lastblkp;
1185 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1187 struct inode *ip = VTOI(vp);
1188 ufs1_daddr_t blkno, *blkp;
1193 for ( ; oldblkp < lastblkp; oldblkp++) {
1195 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1197 lbn = fragstoblks(fs, blkno);
1199 blkp = &ip->i_din1->di_db[lbn];
1200 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1202 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1203 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1206 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1207 [(lbn - NDADDR) % NINDIR(fs)];
1210 * If we are expunging a snapshot vnode and we
1211 * find a block marked BLK_NOCOPY, then it is
1212 * one that has been allocated to this snapshot after
1213 * we took our current snapshot and can be ignored.
1215 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1220 panic("snapacct_ufs1: bad block");
1221 *blkp = expungetype;
1230 * Account for a set of blocks allocated in a snapshot inode.
1233 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1235 ufs1_daddr_t *oldblkp, *lastblkp;
1246 inum = ip->i_number;
1251 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1253 if (blkno == 0 || blkno == BLK_NOCOPY)
1255 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1256 *ip->i_snapblklist++ = lblkno;
1257 if (blkno == BLK_SNAP)
1258 blkno = blkstofrags(fs, lblkno);
1259 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum,
1266 * Before expunging a snapshot inode, note all the
1267 * blocks that it claims with BLK_SNAP so that fsck will
1268 * be able to account for those blocks properly and so
1269 * that this snapshot knows that it need not copy them
1270 * if the other snapshot holding them is freed. This code
1271 * is reproduced once each for UFS1 and UFS2.
1274 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1275 struct vnode *snapvp;
1276 struct inode *cancelip;
1278 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1279 struct fs *, ufs_lbn_t, int);
1283 int i, error, indiroff;
1284 ufs_lbn_t lbn, rlbn;
1285 ufs2_daddr_t len, blkno, numblks, blksperindir;
1286 struct ufs2_dinode *dip;
1287 struct thread *td = curthread;
1291 * Prepare to expunge the inode. If its inode block has not
1292 * yet been copied, then allocate and fill the copy.
1294 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1297 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1299 if (DOINGSOFTDEP(snapvp))
1300 softdep_prealloc(snapvp, MNT_WAIT);
1301 td->td_pflags |= TDP_COWINPROGRESS;
1302 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1303 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1304 td->td_pflags &= ~TDP_COWINPROGRESS;
1307 indiroff = (lbn - NDADDR) % NINDIR(fs);
1308 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1312 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1315 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1316 fs->fs_bsize, KERNCRED, 0, &bp);
1319 if ((error = readblock(snapvp, bp, lbn)) != 0)
1323 * Set a snapshot inode to be a zero length file, regular files
1324 * to be completely unallocated.
1326 dip = (struct ufs2_dinode *)bp->b_data +
1327 ino_to_fsbo(fs, cancelip->i_number);
1328 if (clearmode || cancelip->i_effnlink == 0)
1332 dip->di_flags &= ~SF_SNAPSHOT;
1333 bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1336 * Now go through and expunge all the blocks in the file
1337 * using the function requested.
1339 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1340 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1341 &cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
1343 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1344 &cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
1348 len = numblks - NDADDR;
1350 for (i = 0; len > 0 && i < NIADDR; i++) {
1351 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1352 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1353 blksperindir, fs, acctfunc, expungetype);
1356 blksperindir *= NINDIR(fs);
1357 lbn -= blksperindir + 1;
1358 len -= blksperindir;
1359 rlbn += blksperindir;
1365 * Descend an indirect block chain for vnode cancelvp accounting for all
1366 * its indirect blocks in snapvp.
1369 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1370 blksperindir, fs, acctfunc, expungetype)
1371 struct vnode *snapvp;
1372 struct vnode *cancelvp;
1378 ufs_lbn_t blksperindir;
1380 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1381 struct fs *, ufs_lbn_t, int);
1385 ufs_lbn_t subblksperindir;
1386 struct indir indirs[NIADDR + 2];
1387 ufs2_daddr_t last, *bap;
1391 if (expungetype == BLK_NOCOPY)
1393 panic("indiracct_ufs2: missing indir");
1395 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1397 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1398 panic("indiracct_ufs2: botched params");
1400 * We have to expand bread here since it will deadlock looking
1401 * up the block number for any blocks that are not in the cache.
1403 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1404 bp->b_blkno = fsbtodb(fs, blkno);
1405 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1406 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1411 * Account for the block pointers in this indirect block.
1413 last = howmany(remblks, blksperindir);
1414 if (last > NINDIR(fs))
1416 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1417 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1419 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1420 level == 0 ? rlbn : -1, expungetype);
1421 if (error || level == 0)
1424 * Account for the block pointers in each of the indirect blocks
1425 * in the levels below us.
1427 subblksperindir = blksperindir / NINDIR(fs);
1428 for (lbn++, level--, i = 0; i < last; i++) {
1429 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1430 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1433 rlbn += blksperindir;
1434 lbn -= blksperindir;
1435 remblks -= blksperindir;
1438 free(bap, M_DEVBUF);
1443 * Do both snap accounting and map accounting.
1446 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1448 ufs2_daddr_t *oldblkp, *lastblkp;
1451 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1455 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1457 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1461 * Identify a set of blocks allocated in a snapshot inode.
1464 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1466 ufs2_daddr_t *oldblkp, *lastblkp;
1469 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1471 struct inode *ip = VTOI(vp);
1472 ufs2_daddr_t blkno, *blkp;
1477 for ( ; oldblkp < lastblkp; oldblkp++) {
1479 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1481 lbn = fragstoblks(fs, blkno);
1483 blkp = &ip->i_din2->di_db[lbn];
1484 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1486 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1487 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1490 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1491 [(lbn - NDADDR) % NINDIR(fs)];
1494 * If we are expunging a snapshot vnode and we
1495 * find a block marked BLK_NOCOPY, then it is
1496 * one that has been allocated to this snapshot after
1497 * we took our current snapshot and can be ignored.
1499 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1504 panic("snapacct_ufs2: bad block");
1505 *blkp = expungetype;
1514 * Account for a set of blocks allocated in a snapshot inode.
1517 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1519 ufs2_daddr_t *oldblkp, *lastblkp;
1530 inum = ip->i_number;
1535 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1537 if (blkno == 0 || blkno == BLK_NOCOPY)
1539 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1540 *ip->i_snapblklist++ = lblkno;
1541 if (blkno == BLK_SNAP)
1542 blkno = blkstofrags(fs, lblkno);
1543 ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum,
1550 * Decrement extra reference on snapshot when last name is removed.
1551 * It will not be freed until the last open reference goes away.
1560 struct snapdata *sn;
1561 struct ufsmount *ump;
1564 * Find snapshot in incore list.
1567 sn = ip->i_devvp->v_rdev->si_snapdata;
1569 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1575 printf("ffs_snapgone: lost snapshot vnode %ju\n",
1576 (uintmax_t)ip->i_number);
1578 * Delete snapshot inode from superblock. Keep list dense.
1583 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1584 if (fs->fs_snapinum[snaploc] == ip->i_number)
1586 if (snaploc < FSMAXSNAP) {
1587 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1588 if (fs->fs_snapinum[snaploc] == 0)
1590 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1592 fs->fs_snapinum[snaploc - 1] = 0;
1598 * Prepare a snapshot file for being removed.
1605 struct vnode *devvp;
1608 ufs2_daddr_t numblks, blkno, dblk;
1609 int error, loc, last;
1610 struct snapdata *sn;
1614 devvp = ip->i_devvp;
1616 * If active, delete from incore list (this snapshot may
1617 * already have been in the process of being deleted, so
1618 * would not have been active).
1620 * Clear copy-on-write flag if last snapshot.
1623 if (ip->i_nextsnap.tqe_prev != 0) {
1624 sn = devvp->v_rdev->si_snapdata;
1625 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1626 ip->i_nextsnap.tqe_prev = 0;
1628 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1629 KASSERT(vp->v_vnlock == &sn->sn_lock,
1630 ("ffs_snapremove: lost lock mutation"));
1631 vp->v_vnlock = &vp->v_lock;
1633 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1634 try_free_snapdata(devvp);
1638 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1639 * snapshots that want them (see ffs_snapblkfree below).
1641 for (blkno = 1; blkno < NDADDR; blkno++) {
1642 dblk = DIP(ip, i_db[blkno]);
1645 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1646 DIP_SET(ip, i_db[blkno], 0);
1647 else if ((dblk == blkstofrags(fs, blkno) &&
1648 ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1649 ip->i_number, vp->v_type, NULL))) {
1650 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1651 btodb(fs->fs_bsize));
1652 DIP_SET(ip, i_db[blkno], 0);
1655 numblks = howmany(ip->i_size, fs->fs_bsize);
1656 for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1657 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1658 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1661 if (fs->fs_size - blkno > NINDIR(fs))
1664 last = fs->fs_size - blkno;
1665 for (loc = 0; loc < last; loc++) {
1666 if (ip->i_ump->um_fstype == UFS1) {
1667 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1670 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1671 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1672 else if ((dblk == blkstofrags(fs, blkno) &&
1673 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1674 fs->fs_bsize, ip->i_number, vp->v_type,
1676 ip->i_din1->di_blocks -=
1677 btodb(fs->fs_bsize);
1678 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1682 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1685 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1686 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1687 else if ((dblk == blkstofrags(fs, blkno) &&
1688 ffs_snapblkfree(fs, ip->i_devvp, dblk,
1689 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1690 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1691 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1697 * Clear snapshot flag and drop reference.
1699 ip->i_flags &= ~SF_SNAPSHOT;
1700 DIP_SET(ip, i_flags, ip->i_flags);
1701 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1703 * The dirtied indirects must be written out before
1704 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1705 * may find indirect pointers using the magic BLK_* values.
1707 if (DOINGSOFTDEP(vp))
1708 ffs_syncvnode(vp, MNT_WAIT, 0);
1711 * Reenable disk quotas for ex-snapshot file.
1713 if (!getinoquota(ip))
1714 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1719 * Notification that a block is being freed. Return zero if the free
1720 * should be allowed to proceed. Return non-zero if the snapshot file
1721 * wants to claim the block. The block will be claimed if it is an
1722 * uncopied part of one of the snapshots. It will be freed if it is
1723 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1724 * If a fragment is being freed, then all snapshots that care about
1725 * it must make a copy since a snapshot file can only claim full sized
1726 * blocks. Note that if more than one snapshot file maps the block,
1727 * we can pick one at random to claim it. Since none of the snapshots
1728 * can change, we are assurred that they will all see the same unmodified
1729 * image. When deleting a snapshot file (see ffs_snapremove above), we
1730 * must push any of these claimed blocks to one of the other snapshots
1731 * that maps it. These claimed blocks are easily identified as they will
1732 * have a block number equal to their logical block number within the
1733 * snapshot. A copied block can never have this property because they
1734 * must always have been allocated from a BLK_NOCOPY location.
1737 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1739 struct vnode *devvp;
1744 struct workhead *wkhd;
1746 struct buf *ibp, *cbp, *savedcbp = NULL;
1747 struct thread *td = curthread;
1749 struct vnode *vp = NULL;
1752 int indiroff = 0, error = 0, claimedblk = 0;
1753 struct snapdata *sn;
1755 lbn = fragstoblks(fs, bno);
1758 sn = devvp->v_rdev->si_snapdata;
1763 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1764 VI_MTX(devvp)) != 0)
1766 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1768 if (DOINGSOFTDEP(vp))
1769 softdep_prealloc(vp, MNT_WAIT);
1771 * Lookup block being written.
1774 blkno = DIP(ip, i_db[lbn]);
1776 td->td_pflags |= TDP_COWINPROGRESS;
1777 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1778 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1779 td->td_pflags &= ~TDP_COWINPROGRESS;
1782 indiroff = (lbn - NDADDR) % NINDIR(fs);
1783 if (ip->i_ump->um_fstype == UFS1)
1784 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1786 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1789 * Check to see if block needs to be copied.
1793 * A block that we map is being freed. If it has not
1794 * been claimed yet, we will claim or copy it (below).
1797 } else if (blkno == BLK_SNAP) {
1799 * No previous snapshot claimed the block,
1800 * so it will be freed and become a BLK_NOCOPY
1801 * (don't care) for us.
1804 panic("snapblkfree: inconsistent block type");
1806 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1807 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1808 } else if (ip->i_ump->um_fstype == UFS1) {
1809 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1813 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1818 } else /* BLK_NOCOPY or default */ {
1820 * If the snapshot has already copied the block
1821 * (default), or does not care about the block,
1829 * If this is a full size block, we will just grab it
1830 * and assign it to the snapshot inode. Otherwise we
1831 * will proceed to copy it. See explanation for this
1832 * routine as to why only a single snapshot needs to
1835 if (size == fs->fs_bsize) {
1838 printf("%s %ju lbn %jd from inum %ju\n",
1839 "Grabonremove: snapino",
1840 (uintmax_t)ip->i_number,
1841 (intmax_t)lbn, (uintmax_t)inum);
1844 * If journaling is tracking this write we must add
1845 * the work to the inode or indirect being written.
1849 softdep_inode_append(ip,
1850 curthread->td_ucred, wkhd);
1852 softdep_buf_append(ibp, wkhd);
1855 DIP_SET(ip, i_db[lbn], bno);
1856 } else if (ip->i_ump->um_fstype == UFS1) {
1857 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1860 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1863 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1864 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1865 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1871 * Allocate the block into which to do the copy. Note that this
1872 * allocation will never require any additional allocations for
1873 * the snapshot inode.
1875 td->td_pflags |= TDP_COWINPROGRESS;
1876 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1877 fs->fs_bsize, KERNCRED, 0, &cbp);
1878 td->td_pflags &= ~TDP_COWINPROGRESS;
1883 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n",
1884 "Copyonremove: snapino ", (uintmax_t)ip->i_number,
1885 (intmax_t)lbn, "for inum", (uintmax_t)inum, size,
1886 (intmax_t)cbp->b_blkno);
1889 * If we have already read the old block contents, then
1890 * simply copy them to the new block. Note that we need
1891 * to synchronously write snapshots that have not been
1892 * unlinked, and hence will be visible after a crash,
1893 * to ensure their integrity. At a minimum we ensure the
1894 * integrity of the filesystem metadata, but use the
1895 * dopersistence sysctl-setable flag to decide on the
1896 * persistence needed for file content data.
1898 if (savedcbp != 0) {
1899 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1901 if ((vtype == VDIR || dopersistence) &&
1903 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1907 * Otherwise, read the old block contents into the buffer.
1909 if ((error = readblock(vp, cbp, lbn)) != 0) {
1910 bzero(cbp->b_data, fs->fs_bsize);
1912 if ((vtype == VDIR || dopersistence) &&
1914 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1920 * Note that we need to synchronously write snapshots that
1921 * have not been unlinked, and hence will be visible after
1922 * a crash, to ensure their integrity. At a minimum we
1923 * ensure the integrity of the filesystem metadata, but
1924 * use the dopersistence sysctl-setable flag to decide on
1925 * the persistence needed for file content data.
1928 vp = savedcbp->b_vp;
1930 if ((vtype == VDIR || dopersistence) &&
1931 VTOI(vp)->i_effnlink > 0)
1932 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1935 * If we have been unable to allocate a block in which to do
1936 * the copy, then return non-zero so that the fragment will
1937 * not be freed. Although space will be lost, the snapshot
1938 * will stay consistent.
1940 if (error != 0 && wkhd != NULL)
1941 softdep_freework(wkhd);
1942 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1947 * Associate snapshot files when mounting.
1950 ffs_snapshot_mount(mp)
1953 struct ufsmount *ump = VFSTOUFS(mp);
1954 struct vnode *devvp = ump->um_devvp;
1955 struct fs *fs = ump->um_fs;
1956 struct thread *td = curthread;
1957 struct snapdata *sn;
1959 struct vnode *lastvp;
1965 daddr_t snaplistsize;
1966 int error, snaploc, loc;
1969 * XXX The following needs to be set before ffs_truncate or
1970 * VOP_READ can be called.
1972 mp->mnt_stat.f_iosize = fs->fs_bsize;
1974 * Process each snapshot listed in the superblock.
1979 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1980 if (fs->fs_snapinum[snaploc] == 0)
1982 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1983 LK_EXCLUSIVE, &vp)) != 0){
1984 printf("ffs_snapshot_mount: vget failed %d\n", error);
1988 if (!IS_SNAPSHOT(ip) || ip->i_size ==
1989 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1990 if (!IS_SNAPSHOT(ip)) {
1991 reason = "non-snapshot";
1993 reason = "old format snapshot";
1994 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED);
1995 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
1997 printf("ffs_snapshot_mount: %s inode %d\n",
1998 reason, fs->fs_snapinum[snaploc]);
2001 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
2002 if (fs->fs_snapinum[loc] == 0)
2004 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2006 fs->fs_snapinum[loc - 1] = 0;
2011 * Acquire a lock on the snapdata structure, creating it if
2014 sn = ffs_snapdata_acquire(devvp);
2016 * Change vnode to use shared snapshot lock instead of the
2017 * original private lock.
2019 vp->v_vnlock = &sn->sn_lock;
2020 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2022 * Link it onto the active snapshot list.
2025 if (ip->i_nextsnap.tqe_prev != 0)
2026 panic("ffs_snapshot_mount: %ju already on list",
2027 (uintmax_t)ip->i_number);
2029 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2030 vp->v_vflag |= VV_SYSTEM;
2037 * No usable snapshots found.
2039 if (sn == NULL || vp == NULL)
2042 * Allocate the space for the block hints list. We always want to
2043 * use the list from the newest snapshot.
2045 auio.uio_iov = &aiov;
2046 auio.uio_iovcnt = 1;
2047 aiov.iov_base = (void *)&snaplistsize;
2048 aiov.iov_len = sizeof(snaplistsize);
2049 auio.uio_resid = aiov.iov_len;
2051 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2052 auio.uio_segflg = UIO_SYSSPACE;
2053 auio.uio_rw = UIO_READ;
2055 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2056 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2057 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2061 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2062 M_UFSMNT, M_WAITOK);
2063 auio.uio_iovcnt = 1;
2064 aiov.iov_base = snapblklist;
2065 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2066 auio.uio_resid = aiov.iov_len;
2067 auio.uio_offset -= sizeof(snaplistsize);
2068 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2069 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2071 free(snapblklist, M_UFSMNT);
2076 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2077 sn->sn_listsize = snaplistsize;
2078 sn->sn_blklist = (daddr_t *)snapblklist;
2079 devvp->v_vflag |= VV_COPYONWRITE;
2084 * Disassociate snapshot files when unmounting.
2087 ffs_snapshot_unmount(mp)
2090 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2091 struct snapdata *sn;
2096 sn = devvp->v_rdev->si_snapdata;
2097 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2099 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2100 xp->i_nextsnap.tqe_prev = 0;
2101 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2103 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2104 KASSERT(vp->v_vnlock == &sn->sn_lock,
2105 ("ffs_snapshot_unmount: lost lock mutation"));
2106 vp->v_vnlock = &vp->v_lock;
2107 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2108 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2109 if (xp->i_effnlink > 0)
2112 sn = devvp->v_rdev->si_snapdata;
2114 try_free_snapdata(devvp);
2115 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2119 * Check the buffer block to be belong to device buffer that shall be
2120 * locked after snaplk. devvp shall be locked on entry, and will be
2121 * leaved locked upon exit.
2124 ffs_bp_snapblk(devvp, bp)
2125 struct vnode *devvp;
2128 struct snapdata *sn;
2130 ufs2_daddr_t lbn, *snapblklist;
2131 int lower, upper, mid;
2133 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2134 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2135 sn = devvp->v_rdev->si_snapdata;
2136 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2138 fs = TAILQ_FIRST(&sn->sn_head)->i_fs;
2139 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2140 snapblklist = sn->sn_blklist;
2141 upper = sn->sn_listsize - 1;
2143 while (lower <= upper) {
2144 mid = (lower + upper) / 2;
2145 if (snapblklist[mid] == lbn)
2147 if (snapblklist[mid] < lbn)
2163 struct vnode *vp, *devvp;
2167 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2172 devvp = bo->__bo_vnode;
2173 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2176 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2180 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2181 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2186 * Try to find a buffer to flush.
2188 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2189 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2191 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2194 panic("bdwrite: found ourselves");
2197 * Don't countdeps with the bo lock
2200 if (buf_countdeps(nbp, 0)) {
2207 if (!ffs_bp_snapblk(vp, nbp)) {
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 = NULL;
2240 struct thread *td = curthread;
2243 struct vnode *vp = NULL;
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 %ju lbn %jd for ",
2370 (uintmax_t)ip->i_number, (intmax_t)lbn);
2371 if (bp->b_vp == devvp)
2372 printf("fs metadata");
2375 (uintmax_t)VTOI(bp->b_vp)->i_number);
2376 printf(" lblkno %jd to blkno %jd\n",
2377 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2381 * If we have already read the old block contents, then
2382 * simply copy them to the new block. Note that we need
2383 * to synchronously write snapshots that have not been
2384 * unlinked, and hence will be visible after a crash,
2385 * to ensure their integrity. At a minimum we ensure the
2386 * integrity of the filesystem metadata, but use the
2387 * dopersistence sysctl-setable flag to decide on the
2388 * persistence needed for file content data.
2390 if (savedcbp != 0) {
2391 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2393 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2394 dopersistence) && ip->i_effnlink > 0)
2395 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2397 launched_async_io = 1;
2401 * Otherwise, read the old block contents into the buffer.
2403 if ((error = readblock(vp, cbp, lbn)) != 0) {
2404 bzero(cbp->b_data, fs->fs_bsize);
2406 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2407 dopersistence) && ip->i_effnlink > 0)
2408 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2410 launched_async_io = 1;
2416 * Note that we need to synchronously write snapshots that
2417 * have not been unlinked, and hence will be visible after
2418 * a crash, to ensure their integrity. At a minimum we
2419 * ensure the integrity of the filesystem metadata, but
2420 * use the dopersistence sysctl-setable flag to decide on
2421 * the persistence needed for file content data.
2424 vp = savedcbp->b_vp;
2426 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2427 dopersistence) && VTOI(vp)->i_effnlink > 0)
2428 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2430 launched_async_io = 1;
2432 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2433 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2435 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2436 waitrunningbufspace();
2438 * I/O on bp will now be started, so count it in runningbufspace.
2440 if (saved_runningbufspace != 0) {
2441 bp->b_runningbufspace = saved_runningbufspace;
2442 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2448 * sync snapshots to force freework records waiting on snapshots to claim
2452 ffs_sync_snap(mp, waitfor)
2456 struct snapdata *sn;
2457 struct vnode *devvp;
2461 devvp = VFSTOUFS(mp)->um_devvp;
2462 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2466 sn = devvp->v_rdev->si_snapdata;
2471 if (lockmgr(&sn->sn_lock,
2472 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2473 VI_MTX(devvp)) == 0)
2476 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2478 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2480 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2484 * Read the specified block into the given buffer.
2485 * Much of this boiler-plate comes from bwrite().
2488 readblock(vp, bp, lbn)
2493 struct inode *ip = VTOI(vp);
2496 bip = g_alloc_bio();
2497 bip->bio_cmd = BIO_READ;
2498 bip->bio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
2499 bip->bio_data = bp->b_data;
2500 bip->bio_length = bp->b_bcount;
2501 bip->bio_done = NULL;
2503 g_io_request(bip, ip->i_devvp->v_bufobj.bo_private);
2504 bp->b_error = biowait(bip, "snaprdb");
2506 return (bp->b_error);
2512 * Process file deletes that were deferred by ufs_inactive() due to
2513 * the file system being suspended. Transfer IN_LAZYACCESS into
2514 * IN_MODIFIED for vnodes that were accessed during suspension.
2517 process_deferred_inactive(struct mount *mp)
2519 struct vnode *vp, *mvp;
2525 (void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2527 MNT_VNODE_FOREACH_ALL(vp, mp, mvp) {
2529 * IN_LAZYACCESS is checked here without holding any
2530 * vnode lock, but this flag is set only while holding
2533 if (vp->v_type == VNON ||
2534 ((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2535 ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0))) {
2540 error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK);
2543 if (error == ENOENT)
2544 continue; /* vnode recycled */
2545 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
2549 if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2550 ip->i_flag &= ~IN_LAZYACCESS;
2551 ip->i_flag |= IN_MODIFIED;
2554 if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2561 VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2562 ("process_deferred_inactive: got VI_OWEINACT"));
2567 vn_finished_secondary_write(mp);
2570 #ifndef NO_FFS_SNAPSHOT
2572 static struct snapdata *
2573 ffs_snapdata_alloc(void)
2575 struct snapdata *sn;
2578 * Fetch a snapdata from the free list if there is one available.
2580 mtx_lock(&snapfree_lock);
2581 sn = LIST_FIRST(&snapfree);
2583 LIST_REMOVE(sn, sn_link);
2584 mtx_unlock(&snapfree_lock);
2588 * If there were no free snapdatas allocate one.
2590 sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
2591 TAILQ_INIT(&sn->sn_head);
2592 lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
2593 LK_CANRECURSE | LK_NOSHARE);
2598 * The snapdata is never freed because we can not be certain that
2599 * there are no threads sleeping on the snap lock. Persisting
2600 * them permanently avoids costly synchronization in ffs_lock().
2603 ffs_snapdata_free(struct snapdata *sn)
2605 mtx_lock(&snapfree_lock);
2606 LIST_INSERT_HEAD(&snapfree, sn, sn_link);
2607 mtx_unlock(&snapfree_lock);
2610 /* Try to free snapdata associated with devvp */
2612 try_free_snapdata(struct vnode *devvp)
2614 struct snapdata *sn;
2615 ufs2_daddr_t *snapblklist;
2617 ASSERT_VI_LOCKED(devvp, "try_free_snapdata");
2618 sn = devvp->v_rdev->si_snapdata;
2620 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2621 (devvp->v_vflag & VV_COPYONWRITE) == 0) {
2626 devvp->v_rdev->si_snapdata = NULL;
2627 devvp->v_vflag &= ~VV_COPYONWRITE;
2628 lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp));
2629 snapblklist = sn->sn_blklist;
2630 sn->sn_blklist = NULL;
2631 sn->sn_listsize = 0;
2632 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2633 if (snapblklist != NULL)
2634 free(snapblklist, M_UFSMNT);
2635 ffs_snapdata_free(sn);
2638 static struct snapdata *
2639 ffs_snapdata_acquire(struct vnode *devvp)
2641 struct snapdata *nsn;
2642 struct snapdata *sn;
2645 * Allocate a free snapdata. This is done before acquiring the
2646 * devvp lock to avoid allocation while the devvp interlock is
2649 nsn = ffs_snapdata_alloc();
2651 * If there snapshots already exist on this filesystem grab a
2652 * reference to the shared lock. Otherwise this is the first
2653 * snapshot on this filesystem and we need to use our
2654 * pre-allocated snapdata.
2657 if (devvp->v_rdev->si_snapdata == NULL) {
2658 devvp->v_rdev->si_snapdata = nsn;
2661 sn = devvp->v_rdev->si_snapdata;
2663 * Acquire the snapshot lock.
2665 lockmgr(&sn->sn_lock,
2666 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, VI_MTX(devvp));
2668 * Free any unused snapdata.
2671 ffs_snapdata_free(nsn);