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 | NOCACHE, UIO_SYSSPACE,
261 if ((error = namei(&nd)) != 0)
263 if (nd.ni_vp != NULL) {
267 if (nd.ni_dvp->v_mount != mp)
270 NDFREE(&nd, NDF_ONLY_PNBUF);
271 if (nd.ni_dvp == nd.ni_vp)
279 vat.va_mode = S_IRUSR;
280 vat.va_vaflags |= VA_EXCLUSIVE;
281 if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
284 panic("ffs_snapshot: mount mismatch");
286 if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
287 NDFREE(&nd, NDF_ONLY_PNBUF);
289 if ((error = vn_start_write(NULL, &wrtmp,
290 V_XSLEEP | PCATCH)) != 0)
294 error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
295 VOP_UNLOCK(nd.ni_dvp, 0);
297 NDFREE(&nd, NDF_ONLY_PNBUF);
298 vn_finished_write(wrtmp);
303 vp->v_vflag |= VV_SYSTEM;
305 devvp = ITODEVVP(ip);
307 * Allocate and copy the last block contents so as to be able
308 * to set size to that of the filesystem.
310 numblks = howmany(fs->fs_size, fs->fs_frag);
311 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
312 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
315 ip->i_size = lblktosize(fs, (off_t)numblks);
316 DIP_SET(ip, i_size, ip->i_size);
317 ip->i_flag |= IN_CHANGE | IN_UPDATE;
318 error = readblock(vp, bp, numblks - 1);
323 * Preallocate critical data structures so that we can copy
324 * them in without further allocation after we suspend all
325 * operations on the filesystem. We would like to just release
326 * the allocated buffers without writing them since they will
327 * be filled in below once we are ready to go, but this upsets
328 * the soft update code, so we go ahead and write the new buffers.
330 * Allocate all indirect blocks and mark all of them as not
331 * needing to be copied.
333 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
334 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
335 fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
341 * Allocate copies for the superblock and its summary information.
343 error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
348 blkno = fragstoblks(fs, fs->fs_csaddr);
349 len = howmany(fs->fs_cssize, fs->fs_bsize);
350 for (loc = 0; loc < len; loc++) {
351 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
352 fs->fs_bsize, KERNCRED, 0, &nbp);
358 * Allocate all cylinder group blocks.
360 for (cg = 0; cg < fs->fs_ncg; cg++) {
361 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
362 fs->fs_bsize, KERNCRED, 0, &nbp);
367 ffs_syncvnode(vp, MNT_WAIT, 0);
370 * Copy all the cylinder group maps. Although the
371 * filesystem is still active, we hope that only a few
372 * cylinder groups will change between now and when we
373 * suspend operations. Thus, we will be able to quickly
374 * touch up the few cylinder groups that changed during
375 * the suspension period.
377 len = howmany(fs->fs_ncg, NBBY);
378 space = malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
380 fs->fs_active = space;
382 for (cg = 0; cg < fs->fs_ncg; cg++) {
383 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
384 fs->fs_bsize, KERNCRED, 0, &nbp);
387 error = cgaccount(cg, vp, nbp, 1);
390 ffs_syncvnode(vp, MNT_WAIT, 0);
395 * Change inode to snapshot type file.
397 ip->i_flags |= SF_SNAPSHOT;
398 DIP_SET(ip, i_flags, ip->i_flags);
399 ip->i_flag |= IN_CHANGE | IN_UPDATE;
401 * Ensure that the snapshot is completely on disk.
402 * Since we have marked it as a snapshot it is safe to
403 * unlock it as no process will be allowed to write to it.
405 if ((error = ffs_syncvnode(vp, MNT_WAIT, 0)) != 0)
409 * All allocations are done, so we can now snapshot the system.
411 * Recind nice scheduling while running with the filesystem suspended.
413 if (td->td_proc->p_nice > 0) {
418 saved_nice = p->p_nice;
423 * Suspend operation on filesystem.
426 vn_finished_write(wrtmp);
427 if ((error = vfs_write_suspend(vp->v_mount, 0)) != 0) {
428 vn_start_write(NULL, &wrtmp, V_WAIT);
429 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
432 if (mp->mnt_kern_flag & MNTK_SUSPENDED)
434 vn_start_write(NULL, &wrtmp, V_WAIT);
436 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
437 if (ip->i_effnlink == 0) {
438 error = ENOENT; /* Snapshot file unlinked */
441 if (collectsnapstats)
442 nanotime(&starttime);
444 /* The last block might have changed. Copy it again to be sure. */
445 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
446 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
449 error = readblock(vp, bp, numblks - 1);
450 bp->b_flags |= B_VALIDSUSPWRT;
455 * First, copy all the cylinder group maps that have changed.
457 for (cg = 0; cg < fs->fs_ncg; cg++) {
458 if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
461 error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
462 fs->fs_bsize, KERNCRED, 0, &nbp);
465 error = cgaccount(cg, vp, nbp, 2);
471 * Grab a copy of the superblock and its summary information.
472 * We delay writing it until the suspension is released below.
474 copy_fs = malloc((u_long)fs->fs_bsize, M_UFSMNT, M_WAITOK);
475 bcopy(fs, copy_fs, fs->fs_sbsize);
476 if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
477 copy_fs->fs_clean = 1;
478 size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
479 if (fs->fs_sbsize < size)
480 bzero(&((char *)copy_fs)[fs->fs_sbsize],
481 size - fs->fs_sbsize);
482 size = blkroundup(fs, fs->fs_cssize);
483 if (fs->fs_contigsumsize > 0)
484 size += fs->fs_ncg * sizeof(int32_t);
485 space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
486 copy_fs->fs_csp = space;
487 bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
488 space = (char *)space + fs->fs_cssize;
489 loc = howmany(fs->fs_cssize, fs->fs_fsize);
490 i = fs->fs_frag - loc % fs->fs_frag;
491 len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
493 if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
494 len, KERNCRED, &bp)) != 0) {
496 free(copy_fs->fs_csp, M_UFSMNT);
497 free(copy_fs, M_UFSMNT);
501 bcopy(bp->b_data, space, (u_int)len);
502 space = (char *)space + len;
503 bp->b_flags |= B_INVAL | B_NOCACHE;
506 if (fs->fs_contigsumsize > 0) {
507 copy_fs->fs_maxcluster = lp = space;
508 for (i = 0; i < fs->fs_ncg; i++)
509 *lp++ = fs->fs_contigsumsize;
512 * We must check for active files that have been unlinked
513 * (e.g., with a zero link count). We have to expunge all
514 * trace of these files from the snapshot so that they are
515 * not reclaimed prematurely by fsck or unnecessarily dumped.
516 * We turn off the MNTK_SUSPENDED flag to avoid a panic from
517 * spec_strategy about writing on a suspended filesystem.
518 * Note that we skip unlinked snapshot files as they will
519 * be handled separately below.
521 * We also calculate the needed size for the snapshot list.
523 snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
524 FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
526 mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
529 MNT_VNODE_FOREACH_ALL(xvp, mp, mvp) {
530 if ((xvp->v_usecount == 0 &&
531 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
532 xvp->v_type == VNON ||
533 IS_SNAPSHOT(VTOI(xvp))) {
538 * We can skip parent directory vnode because it must have
539 * this snapshot file in it.
541 if (xvp == nd.ni_dvp) {
546 if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK) != 0) {
547 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
552 if (xvp->v_usecount == 0 &&
553 (xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
561 vn_printf(xvp, "ffs_snapshot: busy vnode ");
562 if (VOP_GETATTR(xvp, &vat, td->td_ucred) == 0 &&
569 if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
575 * If there is a fragment, clear it here.
578 loc = howmany(xp->i_size, fs->fs_bsize) - 1;
579 if (loc < UFS_NDADDR) {
580 len = fragroundup(fs, blkoff(fs, xp->i_size));
581 if (len != 0 && len < fs->fs_bsize) {
582 ffs_blkfree(ump, copy_fs, vp,
583 DIP(xp, i_db[loc]), len, xp->i_number,
585 blkno = DIP(xp, i_db[loc]);
586 DIP_SET(xp, i_db[loc], 0);
591 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
594 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
597 DIP_SET(xp, i_db[loc], blkno);
599 error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
604 free(copy_fs->fs_csp, M_UFSMNT);
605 free(copy_fs, M_UFSMNT);
607 MNT_VNODE_FOREACH_ALL_ABORT(mp, mvp);
612 * Erase the journal file from the snapshot.
614 if (fs->fs_flags & FS_SUJ) {
615 error = softdep_journal_lookup(mp, &xvp);
617 free(copy_fs->fs_csp, M_UFSMNT);
618 free(copy_fs, M_UFSMNT);
624 error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
627 error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
632 * Acquire a lock on the snapdata structure, creating it if necessary.
634 sn = ffs_snapdata_acquire(devvp);
636 * Change vnode to use shared snapshot lock instead of the original
639 vp->v_vnlock = &sn->sn_lock;
640 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
641 xp = TAILQ_FIRST(&sn->sn_head);
643 * If this is the first snapshot on this filesystem, then we need
644 * to allocate the space for the list of preallocated snapshot blocks.
645 * This list will be refined below, but this preliminary one will
646 * keep us out of deadlock until the full one is ready.
649 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
651 blkp = &snapblklist[1];
652 *blkp++ = lblkno(fs, fs->fs_sblockloc);
653 blkno = fragstoblks(fs, fs->fs_csaddr);
654 for (cg = 0; cg < fs->fs_ncg; cg++) {
655 if (fragstoblks(fs, cgtod(fs, cg) > blkno))
657 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
659 len = howmany(fs->fs_cssize, fs->fs_bsize);
660 for (loc = 0; loc < len; loc++)
661 *blkp++ = blkno + loc;
662 for (; cg < fs->fs_ncg; cg++)
663 *blkp++ = fragstoblks(fs, cgtod(fs, cg));
664 snapblklist[0] = blkp - snapblklist;
666 if (sn->sn_blklist != NULL)
667 panic("ffs_snapshot: non-empty list");
668 sn->sn_blklist = snapblklist;
669 sn->sn_listsize = blkp - snapblklist;
673 * Record snapshot inode. Since this is the newest snapshot,
674 * it must be placed at the end of the list.
677 fs->fs_snapinum[snaploc] = ip->i_number;
678 if (ip->i_nextsnap.tqe_prev != 0)
679 panic("ffs_snapshot: %ju already on list",
680 (uintmax_t)ip->i_number);
681 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
682 devvp->v_vflag |= VV_COPYONWRITE;
684 ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
686 KASSERT((sn != NULL && copy_fs != NULL && error == 0) ||
687 (sn == NULL && copy_fs == NULL && error != 0),
688 ("email phk@ and mckusick@"));
690 * Resume operation on filesystem.
692 vfs_write_resume(vp->v_mount, VR_START_WRITE | VR_NO_SUSPCLR);
693 if (collectsnapstats && starttime.tv_sec > 0) {
695 timespecsub(&endtime, &starttime);
696 printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
697 vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
698 endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
703 * Copy allocation information from all the snapshots in
704 * this snapshot and then expunge them from its view.
706 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
710 error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
713 error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
715 if (error == 0 && xp->i_effnlink == 0) {
716 error = ffs_freefile(ump,
723 fs->fs_snapinum[snaploc] = 0;
728 * Allocate space for the full list of preallocated snapshot blocks.
730 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
732 ip->i_snapblklist = &snapblklist[1];
734 * Expunge the blocks used by the snapshots from the set of
735 * blocks marked as used in the snapshot bitmaps. Also, collect
736 * the list of allocated blocks in i_snapblklist.
739 error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1,
742 error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2,
745 fs->fs_snapinum[snaploc] = 0;
746 free(snapblklist, M_UFSMNT);
749 if (snaplistsize < ip->i_snapblklist - snapblklist)
750 panic("ffs_snapshot: list too small");
751 snaplistsize = ip->i_snapblklist - snapblklist;
752 snapblklist[0] = snaplistsize;
753 ip->i_snapblklist = 0;
755 * Write out the list of allocated blocks to the end of the snapshot.
757 auio.uio_iov = &aiov;
759 aiov.iov_base = (void *)snapblklist;
760 aiov.iov_len = snaplistsize * sizeof(daddr_t);
761 auio.uio_resid = aiov.iov_len;
762 auio.uio_offset = ip->i_size;
763 auio.uio_segflg = UIO_SYSSPACE;
764 auio.uio_rw = UIO_WRITE;
766 if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
767 fs->fs_snapinum[snaploc] = 0;
768 free(snapblklist, M_UFSMNT);
772 * Write the superblock and its summary information
775 blkno = fragstoblks(fs, fs->fs_csaddr);
776 len = howmany(fs->fs_cssize, fs->fs_bsize);
777 space = copy_fs->fs_csp;
778 for (loc = 0; loc < len; loc++) {
779 error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
782 fs->fs_snapinum[snaploc] = 0;
783 free(snapblklist, M_UFSMNT);
786 bcopy(space, nbp->b_data, fs->fs_bsize);
787 space = (char *)space + fs->fs_bsize;
790 error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
795 loc = blkoff(fs, fs->fs_sblockloc);
796 bcopy((char *)copy_fs, &nbp->b_data[loc], (u_int)fs->fs_sbsize);
800 * As this is the newest list, it is the most inclusive, so
801 * should replace the previous list.
804 space = sn->sn_blklist;
805 sn->sn_blklist = snapblklist;
806 sn->sn_listsize = snaplistsize;
809 free(space, M_UFSMNT);
811 * Preallocate all the direct blocks in the snapshot inode so
812 * that we never have to write the inode itself to commit an
813 * update to the contents of the snapshot. Note that once
814 * created, the size of the snapshot will never change, so
815 * there will never be a need to write the inode except to
816 * update the non-integrity-critical time fields and
817 * allocated-block count.
819 for (blockno = 0; blockno < UFS_NDADDR; blockno++) {
820 if (DIP(ip, i_db[blockno]) != 0)
822 error = UFS_BALLOC(vp, lblktosize(fs, blockno),
823 fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
826 error = readblock(vp, bp, blockno);
832 free(copy_fs->fs_csp, M_UFSMNT);
833 free(copy_fs, M_UFSMNT);
836 NDFREE(&nd, NDF_ONLY_PNBUF);
837 if (saved_nice > 0) {
842 sched_nice(td->td_proc, saved_nice);
843 PROC_UNLOCK(td->td_proc);
846 if (fs->fs_active != 0) {
847 free(fs->fs_active, M_DEVBUF);
852 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
855 (void) ffs_truncate(vp, (off_t)0, 0, NOCRED);
856 (void) ffs_syncvnode(vp, MNT_WAIT, 0);
862 vn_finished_write(wrtmp);
863 process_deferred_inactive(mp);
868 * Copy a cylinder group map. All the unallocated blocks are marked
869 * BLK_NOCOPY so that the snapshot knows that it need not copy them
870 * if they are later written. If passno is one, then this is a first
871 * pass, so only setting needs to be done. If passno is 2, then this
872 * is a revision to a previous pass which must be undone as the
873 * replacement pass is done.
876 cgaccount(cg, vp, nbp, passno)
882 struct buf *bp, *ibp;
886 ufs2_daddr_t base, numblks;
887 int error, len, loc, indiroff;
891 if ((error = ffs_getcg(fs, ITODEVVP(ip), cg, &bp, &cgp)) != 0)
893 UFS_LOCK(ITOUMP(ip));
896 * Recomputation of summary information might not have been performed
897 * at mount time. Sync up summary information for current cylinder
898 * group while data is in memory to ensure that result of background
899 * fsck is slightly more consistent.
901 fs->fs_cs(fs, cg) = cgp->cg_cs;
902 UFS_UNLOCK(ITOUMP(ip));
903 bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
904 if (fs->fs_cgsize < fs->fs_bsize)
905 bzero(&nbp->b_data[fs->fs_cgsize],
906 fs->fs_bsize - fs->fs_cgsize);
907 cgp = (struct cg *)nbp->b_data;
910 nbp->b_flags |= B_VALIDSUSPWRT;
911 numblks = howmany(fs->fs_size, fs->fs_frag);
912 len = howmany(fs->fs_fpg, fs->fs_frag);
913 base = cgbase(fs, cg) / fs->fs_frag;
914 if (base + len >= numblks)
915 len = numblks - base - 1;
917 if (base < UFS_NDADDR) {
918 for ( ; loc < UFS_NDADDR; loc++) {
919 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
920 DIP_SET(ip, i_db[loc], BLK_NOCOPY);
921 else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
922 DIP_SET(ip, i_db[loc], 0);
923 else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
924 panic("ffs_snapshot: lost direct block");
927 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
928 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
932 indiroff = (base + loc - UFS_NDADDR) % NINDIR(fs);
933 for ( ; loc < len; loc++, indiroff++) {
934 if (indiroff >= NINDIR(fs)) {
936 ibp->b_flags |= B_VALIDSUSPWRT;
938 error = UFS_BALLOC(vp,
939 lblktosize(fs, (off_t)(base + loc)),
940 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
947 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
948 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
950 else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
951 [indiroff] == BLK_NOCOPY)
952 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
953 else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
954 [indiroff] == BLK_NOCOPY)
955 panic("ffs_snapshot: lost indirect block");
958 if (ffs_isblock(fs, cg_blksfree(cgp), loc))
959 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
960 else if (passno == 2 &&
961 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
962 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
963 else if (passno == 1 &&
964 ((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
965 panic("ffs_snapshot: lost indirect block");
968 ibp->b_flags |= B_VALIDSUSPWRT;
974 * Before expunging a snapshot inode, note all the
975 * blocks that it claims with BLK_SNAP so that fsck will
976 * be able to account for those blocks properly and so
977 * that this snapshot knows that it need not copy them
978 * if the other snapshot holding them is freed. This code
979 * is reproduced once each for UFS1 and UFS2.
982 expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
983 struct vnode *snapvp;
984 struct inode *cancelip;
986 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
987 struct fs *, ufs_lbn_t, int);
991 int i, error, indiroff;
993 ufs2_daddr_t len, blkno, numblks, blksperindir;
994 struct ufs1_dinode *dip;
995 struct thread *td = curthread;
999 * Prepare to expunge the inode. If its inode block has not
1000 * yet been copied, then allocate and fill the copy.
1002 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1004 if (lbn < UFS_NDADDR) {
1005 blkno = VTOI(snapvp)->i_din1->di_db[lbn];
1007 if (DOINGSOFTDEP(snapvp))
1008 softdep_prealloc(snapvp, MNT_WAIT);
1009 td->td_pflags |= TDP_COWINPROGRESS;
1010 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1011 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1012 td->td_pflags &= ~TDP_COWINPROGRESS;
1015 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1016 blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1020 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1023 error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1024 fs->fs_bsize, KERNCRED, 0, &bp);
1027 if ((error = readblock(snapvp, bp, lbn)) != 0)
1031 * Set a snapshot inode to be a zero length file, regular files
1032 * or unlinked snapshots to be completely unallocated.
1034 dip = (struct ufs1_dinode *)bp->b_data +
1035 ino_to_fsbo(fs, cancelip->i_number);
1036 if (clearmode || cancelip->i_effnlink == 0)
1040 dip->di_flags &= ~SF_SNAPSHOT;
1041 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs1_daddr_t));
1044 * Now go through and expunge all the blocks in the file
1045 * using the function requested.
1047 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1048 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1049 &cancelip->i_din1->di_db[UFS_NDADDR], fs, 0, expungetype)))
1051 if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1052 &cancelip->i_din1->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1056 len = numblks - UFS_NDADDR;
1058 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1059 error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1060 cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1061 blksperindir, fs, acctfunc, expungetype);
1064 blksperindir *= NINDIR(fs);
1065 lbn -= blksperindir + 1;
1066 len -= blksperindir;
1067 rlbn += blksperindir;
1073 * Descend an indirect block chain for vnode cancelvp accounting for all
1074 * its indirect blocks in snapvp.
1077 indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1078 blksperindir, fs, acctfunc, expungetype)
1079 struct vnode *snapvp;
1080 struct vnode *cancelvp;
1086 ufs_lbn_t blksperindir;
1088 int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1089 struct fs *, ufs_lbn_t, int);
1093 ufs_lbn_t subblksperindir;
1094 struct indir indirs[UFS_NIADDR + 2];
1095 ufs1_daddr_t last, *bap;
1099 if (expungetype == BLK_NOCOPY)
1101 panic("indiracct_ufs1: missing indir");
1103 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1105 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1106 panic("indiracct_ufs1: botched params");
1108 * We have to expand bread here since it will deadlock looking
1109 * up the block number for any blocks that are not in the cache.
1111 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1112 bp->b_blkno = fsbtodb(fs, blkno);
1113 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1114 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1119 * Account for the block pointers in this indirect block.
1121 last = howmany(remblks, blksperindir);
1122 if (last > NINDIR(fs))
1124 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1125 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1127 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1128 level == 0 ? rlbn : -1, expungetype);
1129 if (error || level == 0)
1132 * Account for the block pointers in each of the indirect blocks
1133 * in the levels below us.
1135 subblksperindir = blksperindir / NINDIR(fs);
1136 for (lbn++, level--, i = 0; i < last; i++) {
1137 error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1138 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1141 rlbn += blksperindir;
1142 lbn -= blksperindir;
1143 remblks -= blksperindir;
1146 free(bap, M_DEVBUF);
1151 * Do both snap accounting and map accounting.
1154 fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1156 ufs1_daddr_t *oldblkp, *lastblkp;
1159 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1163 if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1165 return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1169 * Identify a set of blocks allocated in a snapshot inode.
1172 snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1174 ufs1_daddr_t *oldblkp, *lastblkp;
1177 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1179 struct inode *ip = VTOI(vp);
1180 ufs1_daddr_t blkno, *blkp;
1185 for ( ; oldblkp < lastblkp; oldblkp++) {
1187 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1189 lbn = fragstoblks(fs, blkno);
1190 if (lbn < UFS_NDADDR) {
1191 blkp = &ip->i_din1->di_db[lbn];
1192 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1194 error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1195 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1198 blkp = &((ufs1_daddr_t *)(ibp->b_data))
1199 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1202 * If we are expunging a snapshot vnode and we
1203 * find a block marked BLK_NOCOPY, then it is
1204 * one that has been allocated to this snapshot after
1205 * we took our current snapshot and can be ignored.
1207 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1208 if (lbn >= UFS_NDADDR)
1212 panic("snapacct_ufs1: bad block");
1213 *blkp = expungetype;
1214 if (lbn >= UFS_NDADDR)
1222 * Account for a set of blocks allocated in a snapshot inode.
1225 mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1227 ufs1_daddr_t *oldblkp, *lastblkp;
1238 inum = ip->i_number;
1243 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1245 if (blkno == 0 || blkno == BLK_NOCOPY)
1247 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1248 *ip->i_snapblklist++ = lblkno;
1249 if (blkno == BLK_SNAP)
1250 blkno = blkstofrags(fs, lblkno);
1251 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1258 * Before expunging a snapshot inode, note all the
1259 * blocks that it claims with BLK_SNAP so that fsck will
1260 * be able to account for those blocks properly and so
1261 * that this snapshot knows that it need not copy them
1262 * if the other snapshot holding them is freed. This code
1263 * is reproduced once each for UFS1 and UFS2.
1266 expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype, clearmode)
1267 struct vnode *snapvp;
1268 struct inode *cancelip;
1270 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1271 struct fs *, ufs_lbn_t, int);
1275 int i, error, indiroff;
1276 ufs_lbn_t lbn, rlbn;
1277 ufs2_daddr_t len, blkno, numblks, blksperindir;
1278 struct ufs2_dinode *dip;
1279 struct thread *td = curthread;
1283 * Prepare to expunge the inode. If its inode block has not
1284 * yet been copied, then allocate and fill the copy.
1286 lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1288 if (lbn < UFS_NDADDR) {
1289 blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1291 if (DOINGSOFTDEP(snapvp))
1292 softdep_prealloc(snapvp, MNT_WAIT);
1293 td->td_pflags |= TDP_COWINPROGRESS;
1294 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1295 fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1296 td->td_pflags &= ~TDP_COWINPROGRESS;
1299 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1300 blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1304 if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1307 error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1308 fs->fs_bsize, KERNCRED, 0, &bp);
1311 if ((error = readblock(snapvp, bp, lbn)) != 0)
1315 * Set a snapshot inode to be a zero length file, regular files
1316 * to be completely unallocated.
1318 dip = (struct ufs2_dinode *)bp->b_data +
1319 ino_to_fsbo(fs, cancelip->i_number);
1320 if (clearmode || cancelip->i_effnlink == 0)
1324 dip->di_flags &= ~SF_SNAPSHOT;
1325 bzero(&dip->di_db[0], (UFS_NDADDR + UFS_NIADDR) * sizeof(ufs2_daddr_t));
1328 * Now go through and expunge all the blocks in the file
1329 * using the function requested.
1331 numblks = howmany(cancelip->i_size, fs->fs_bsize);
1332 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1333 &cancelip->i_din2->di_db[UFS_NDADDR], fs, 0, expungetype)))
1335 if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1336 &cancelip->i_din2->di_ib[UFS_NIADDR], fs, -1, expungetype)))
1340 len = numblks - UFS_NDADDR;
1342 for (i = 0; len > 0 && i < UFS_NIADDR; i++) {
1343 error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1344 cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1345 blksperindir, fs, acctfunc, expungetype);
1348 blksperindir *= NINDIR(fs);
1349 lbn -= blksperindir + 1;
1350 len -= blksperindir;
1351 rlbn += blksperindir;
1357 * Descend an indirect block chain for vnode cancelvp accounting for all
1358 * its indirect blocks in snapvp.
1361 indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1362 blksperindir, fs, acctfunc, expungetype)
1363 struct vnode *snapvp;
1364 struct vnode *cancelvp;
1370 ufs_lbn_t blksperindir;
1372 int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1373 struct fs *, ufs_lbn_t, int);
1377 ufs_lbn_t subblksperindir;
1378 struct indir indirs[UFS_NIADDR + 2];
1379 ufs2_daddr_t last, *bap;
1383 if (expungetype == BLK_NOCOPY)
1385 panic("indiracct_ufs2: missing indir");
1387 if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1389 if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1390 panic("indiracct_ufs2: botched params");
1392 * We have to expand bread here since it will deadlock looking
1393 * up the block number for any blocks that are not in the cache.
1395 bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1396 bp->b_blkno = fsbtodb(fs, blkno);
1397 if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1398 (error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1403 * Account for the block pointers in this indirect block.
1405 last = howmany(remblks, blksperindir);
1406 if (last > NINDIR(fs))
1408 bap = malloc(fs->fs_bsize, M_DEVBUF, M_WAITOK);
1409 bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1411 error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1412 level == 0 ? rlbn : -1, expungetype);
1413 if (error || level == 0)
1416 * Account for the block pointers in each of the indirect blocks
1417 * in the levels below us.
1419 subblksperindir = blksperindir / NINDIR(fs);
1420 for (lbn++, level--, i = 0; i < last; i++) {
1421 error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1422 rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1425 rlbn += blksperindir;
1426 lbn -= blksperindir;
1427 remblks -= blksperindir;
1430 free(bap, M_DEVBUF);
1435 * Do both snap accounting and map accounting.
1438 fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1440 ufs2_daddr_t *oldblkp, *lastblkp;
1443 int exptype; /* BLK_SNAP or BLK_NOCOPY */
1447 if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1449 return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1453 * Identify a set of blocks allocated in a snapshot inode.
1456 snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1458 ufs2_daddr_t *oldblkp, *lastblkp;
1461 int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1463 struct inode *ip = VTOI(vp);
1464 ufs2_daddr_t blkno, *blkp;
1469 for ( ; oldblkp < lastblkp; oldblkp++) {
1471 if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1473 lbn = fragstoblks(fs, blkno);
1474 if (lbn < UFS_NDADDR) {
1475 blkp = &ip->i_din2->di_db[lbn];
1476 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1478 error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1479 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1482 blkp = &((ufs2_daddr_t *)(ibp->b_data))
1483 [(lbn - UFS_NDADDR) % NINDIR(fs)];
1486 * If we are expunging a snapshot vnode and we
1487 * find a block marked BLK_NOCOPY, then it is
1488 * one that has been allocated to this snapshot after
1489 * we took our current snapshot and can be ignored.
1491 if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1492 if (lbn >= UFS_NDADDR)
1496 panic("snapacct_ufs2: bad block");
1497 *blkp = expungetype;
1498 if (lbn >= UFS_NDADDR)
1506 * Account for a set of blocks allocated in a snapshot inode.
1509 mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1511 ufs2_daddr_t *oldblkp, *lastblkp;
1522 inum = ip->i_number;
1527 for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1529 if (blkno == 0 || blkno == BLK_NOCOPY)
1531 if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1532 *ip->i_snapblklist++ = lblkno;
1533 if (blkno == BLK_SNAP)
1534 blkno = blkstofrags(fs, lblkno);
1535 ffs_blkfree(ITOUMP(ip), fs, vp, blkno, fs->fs_bsize, inum,
1542 * Decrement extra reference on snapshot when last name is removed.
1543 * It will not be freed until the last open reference goes away.
1552 struct snapdata *sn;
1553 struct ufsmount *ump;
1556 * Find snapshot in incore list.
1559 sn = ITODEVVP(ip)->v_rdev->si_snapdata;
1561 TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1567 printf("ffs_snapgone: lost snapshot vnode %ju\n",
1568 (uintmax_t)ip->i_number);
1570 * Delete snapshot inode from superblock. Keep list dense.
1575 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1576 if (fs->fs_snapinum[snaploc] == ip->i_number)
1578 if (snaploc < FSMAXSNAP) {
1579 for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1580 if (fs->fs_snapinum[snaploc] == 0)
1582 fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1584 fs->fs_snapinum[snaploc - 1] = 0;
1590 * Prepare a snapshot file for being removed.
1597 struct vnode *devvp;
1600 ufs2_daddr_t numblks, blkno, dblk;
1601 int error, i, last, loc;
1602 struct snapdata *sn;
1606 devvp = ITODEVVP(ip);
1608 * If active, delete from incore list (this snapshot may
1609 * already have been in the process of being deleted, so
1610 * would not have been active).
1612 * Clear copy-on-write flag if last snapshot.
1615 if (ip->i_nextsnap.tqe_prev != 0) {
1616 sn = devvp->v_rdev->si_snapdata;
1617 TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1618 ip->i_nextsnap.tqe_prev = 0;
1620 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1621 for (i = 0; i < sn->sn_lock.lk_recurse; i++)
1622 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
1623 KASSERT(vp->v_vnlock == &sn->sn_lock,
1624 ("ffs_snapremove: lost lock mutation"));
1625 vp->v_vnlock = &vp->v_lock;
1627 while (sn->sn_lock.lk_recurse > 0)
1628 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1629 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1630 try_free_snapdata(devvp);
1634 * Clear all BLK_NOCOPY fields. Pass any block claims to other
1635 * snapshots that want them (see ffs_snapblkfree below).
1637 for (blkno = 1; blkno < UFS_NDADDR; blkno++) {
1638 dblk = DIP(ip, i_db[blkno]);
1641 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1642 DIP_SET(ip, i_db[blkno], 0);
1643 else if ((dblk == blkstofrags(fs, blkno) &&
1644 ffs_snapblkfree(fs, ITODEVVP(ip), dblk, fs->fs_bsize,
1645 ip->i_number, vp->v_type, NULL))) {
1646 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1647 btodb(fs->fs_bsize));
1648 DIP_SET(ip, i_db[blkno], 0);
1651 numblks = howmany(ip->i_size, fs->fs_bsize);
1652 for (blkno = UFS_NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1653 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1654 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1657 if (fs->fs_size - blkno > NINDIR(fs))
1660 last = fs->fs_size - blkno;
1661 for (loc = 0; loc < last; loc++) {
1662 if (I_IS_UFS1(ip)) {
1663 dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1666 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1667 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1668 else if ((dblk == blkstofrags(fs, blkno) &&
1669 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1670 fs->fs_bsize, ip->i_number, vp->v_type,
1672 ip->i_din1->di_blocks -=
1673 btodb(fs->fs_bsize);
1674 ((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1678 dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1681 if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1682 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1683 else if ((dblk == blkstofrags(fs, blkno) &&
1684 ffs_snapblkfree(fs, ITODEVVP(ip), dblk,
1685 fs->fs_bsize, ip->i_number, vp->v_type, NULL))) {
1686 ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1687 ((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1693 * Clear snapshot flag and drop reference.
1695 ip->i_flags &= ~SF_SNAPSHOT;
1696 DIP_SET(ip, i_flags, ip->i_flags);
1697 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1699 * The dirtied indirects must be written out before
1700 * softdep_setup_freeblocks() is called. Otherwise indir_trunc()
1701 * may find indirect pointers using the magic BLK_* values.
1703 if (DOINGSOFTDEP(vp))
1704 ffs_syncvnode(vp, MNT_WAIT, 0);
1707 * Reenable disk quotas for ex-snapshot file.
1709 if (!getinoquota(ip))
1710 (void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1715 * Notification that a block is being freed. Return zero if the free
1716 * should be allowed to proceed. Return non-zero if the snapshot file
1717 * wants to claim the block. The block will be claimed if it is an
1718 * uncopied part of one of the snapshots. It will be freed if it is
1719 * either a BLK_NOCOPY or has already been copied in all of the snapshots.
1720 * If a fragment is being freed, then all snapshots that care about
1721 * it must make a copy since a snapshot file can only claim full sized
1722 * blocks. Note that if more than one snapshot file maps the block,
1723 * we can pick one at random to claim it. Since none of the snapshots
1724 * can change, we are assurred that they will all see the same unmodified
1725 * image. When deleting a snapshot file (see ffs_snapremove above), we
1726 * must push any of these claimed blocks to one of the other snapshots
1727 * that maps it. These claimed blocks are easily identified as they will
1728 * have a block number equal to their logical block number within the
1729 * snapshot. A copied block can never have this property because they
1730 * must always have been allocated from a BLK_NOCOPY location.
1733 ffs_snapblkfree(fs, devvp, bno, size, inum, vtype, wkhd)
1735 struct vnode *devvp;
1740 struct workhead *wkhd;
1742 struct buf *ibp, *cbp, *savedcbp = NULL;
1743 struct thread *td = curthread;
1745 struct vnode *vp = NULL;
1748 int indiroff = 0, error = 0, claimedblk = 0;
1749 struct snapdata *sn;
1751 lbn = fragstoblks(fs, bno);
1754 sn = devvp->v_rdev->si_snapdata;
1759 if (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1760 VI_MTX(devvp)) != 0)
1762 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1764 if (DOINGSOFTDEP(vp))
1765 softdep_prealloc(vp, MNT_WAIT);
1767 * Lookup block being written.
1769 if (lbn < UFS_NDADDR) {
1770 blkno = DIP(ip, i_db[lbn]);
1772 td->td_pflags |= TDP_COWINPROGRESS;
1773 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1774 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1775 td->td_pflags &= ~TDP_COWINPROGRESS;
1778 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
1780 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1782 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1785 * Check to see if block needs to be copied.
1789 * A block that we map is being freed. If it has not
1790 * been claimed yet, we will claim or copy it (below).
1793 } else if (blkno == BLK_SNAP) {
1795 * No previous snapshot claimed the block,
1796 * so it will be freed and become a BLK_NOCOPY
1797 * (don't care) for us.
1800 panic("snapblkfree: inconsistent block type");
1801 if (lbn < UFS_NDADDR) {
1802 DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1803 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1804 } else if (I_IS_UFS1(ip)) {
1805 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1809 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1814 } else /* BLK_NOCOPY or default */ {
1816 * If the snapshot has already copied the block
1817 * (default), or does not care about the block,
1820 if (lbn >= UFS_NDADDR)
1825 * If this is a full size block, we will just grab it
1826 * and assign it to the snapshot inode. Otherwise we
1827 * will proceed to copy it. See explanation for this
1828 * routine as to why only a single snapshot needs to
1831 if (size == fs->fs_bsize) {
1834 printf("%s %ju lbn %jd from inum %ju\n",
1835 "Grabonremove: snapino",
1836 (uintmax_t)ip->i_number,
1837 (intmax_t)lbn, (uintmax_t)inum);
1840 * If journaling is tracking this write we must add
1841 * the work to the inode or indirect being written.
1844 if (lbn < UFS_NDADDR)
1845 softdep_inode_append(ip,
1846 curthread->td_ucred, wkhd);
1848 softdep_buf_append(ibp, wkhd);
1850 if (lbn < UFS_NDADDR) {
1851 DIP_SET(ip, i_db[lbn], bno);
1852 } else if (I_IS_UFS1(ip)) {
1853 ((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1856 ((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1859 DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1860 ip->i_flag |= IN_CHANGE | IN_UPDATE;
1861 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
1864 if (lbn >= UFS_NDADDR)
1867 * Allocate the block into which to do the copy. Note that this
1868 * allocation will never require any additional allocations for
1869 * the snapshot inode.
1871 td->td_pflags |= TDP_COWINPROGRESS;
1872 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1873 fs->fs_bsize, KERNCRED, 0, &cbp);
1874 td->td_pflags &= ~TDP_COWINPROGRESS;
1879 printf("%s%ju lbn %jd %s %ju size %ld to blkno %jd\n",
1880 "Copyonremove: snapino ", (uintmax_t)ip->i_number,
1881 (intmax_t)lbn, "for inum", (uintmax_t)inum, size,
1882 (intmax_t)cbp->b_blkno);
1885 * If we have already read the old block contents, then
1886 * simply copy them to the new block. Note that we need
1887 * to synchronously write snapshots that have not been
1888 * unlinked, and hence will be visible after a crash,
1889 * to ensure their integrity. At a minimum we ensure the
1890 * integrity of the filesystem metadata, but use the
1891 * dopersistence sysctl-setable flag to decide on the
1892 * persistence needed for file content data.
1894 if (savedcbp != NULL) {
1895 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1897 if ((vtype == VDIR || dopersistence) &&
1899 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1903 * Otherwise, read the old block contents into the buffer.
1905 if ((error = readblock(vp, cbp, lbn)) != 0) {
1906 bzero(cbp->b_data, fs->fs_bsize);
1908 if ((vtype == VDIR || dopersistence) &&
1910 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1916 * Note that we need to synchronously write snapshots that
1917 * have not been unlinked, and hence will be visible after
1918 * a crash, to ensure their integrity. At a minimum we
1919 * ensure the integrity of the filesystem metadata, but
1920 * use the dopersistence sysctl-setable flag to decide on
1921 * the persistence needed for file content data.
1924 vp = savedcbp->b_vp;
1926 if ((vtype == VDIR || dopersistence) &&
1927 VTOI(vp)->i_effnlink > 0)
1928 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
1931 * If we have been unable to allocate a block in which to do
1932 * the copy, then return non-zero so that the fragment will
1933 * not be freed. Although space will be lost, the snapshot
1934 * will stay consistent.
1936 if (error != 0 && wkhd != NULL)
1937 softdep_freework(wkhd);
1938 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
1943 * Associate snapshot files when mounting.
1946 ffs_snapshot_mount(mp)
1949 struct ufsmount *ump = VFSTOUFS(mp);
1950 struct vnode *devvp = ump->um_devvp;
1951 struct fs *fs = ump->um_fs;
1952 struct thread *td = curthread;
1953 struct snapdata *sn;
1955 struct vnode *lastvp;
1961 daddr_t snaplistsize;
1962 int error, snaploc, loc;
1965 * XXX The following needs to be set before ffs_truncate or
1966 * VOP_READ can be called.
1968 mp->mnt_stat.f_iosize = fs->fs_bsize;
1970 * Process each snapshot listed in the superblock.
1975 for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1976 if (fs->fs_snapinum[snaploc] == 0)
1978 if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1979 LK_EXCLUSIVE, &vp)) != 0){
1980 printf("ffs_snapshot_mount: vget failed %d\n", error);
1984 if (!IS_SNAPSHOT(ip) || ip->i_size ==
1985 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1986 if (!IS_SNAPSHOT(ip)) {
1987 reason = "non-snapshot";
1989 reason = "old format snapshot";
1990 (void)ffs_truncate(vp, (off_t)0, 0, NOCRED);
1991 (void)ffs_syncvnode(vp, MNT_WAIT, 0);
1993 printf("ffs_snapshot_mount: %s inode %d\n",
1994 reason, fs->fs_snapinum[snaploc]);
1997 for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
1998 if (fs->fs_snapinum[loc] == 0)
2000 fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
2002 fs->fs_snapinum[loc - 1] = 0;
2007 * Acquire a lock on the snapdata structure, creating it if
2010 sn = ffs_snapdata_acquire(devvp);
2012 * Change vnode to use shared snapshot lock instead of the
2013 * original private lock.
2015 vp->v_vnlock = &sn->sn_lock;
2016 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2018 * Link it onto the active snapshot list.
2021 if (ip->i_nextsnap.tqe_prev != 0)
2022 panic("ffs_snapshot_mount: %ju already on list",
2023 (uintmax_t)ip->i_number);
2025 TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
2026 vp->v_vflag |= VV_SYSTEM;
2033 * No usable snapshots found.
2035 if (sn == NULL || vp == NULL)
2038 * Allocate the space for the block hints list. We always want to
2039 * use the list from the newest snapshot.
2041 auio.uio_iov = &aiov;
2042 auio.uio_iovcnt = 1;
2043 aiov.iov_base = (void *)&snaplistsize;
2044 aiov.iov_len = sizeof(snaplistsize);
2045 auio.uio_resid = aiov.iov_len;
2047 lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2048 auio.uio_segflg = UIO_SYSSPACE;
2049 auio.uio_rw = UIO_READ;
2051 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
2052 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2053 printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2057 snapblklist = malloc(snaplistsize * sizeof(daddr_t),
2058 M_UFSMNT, M_WAITOK);
2059 auio.uio_iovcnt = 1;
2060 aiov.iov_base = snapblklist;
2061 aiov.iov_len = snaplistsize * sizeof (daddr_t);
2062 auio.uio_resid = aiov.iov_len;
2063 auio.uio_offset -= sizeof(snaplistsize);
2064 if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2065 printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2067 free(snapblklist, M_UFSMNT);
2072 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2073 sn->sn_listsize = snaplistsize;
2074 sn->sn_blklist = (daddr_t *)snapblklist;
2075 devvp->v_vflag |= VV_COPYONWRITE;
2080 * Disassociate snapshot files when unmounting.
2083 ffs_snapshot_unmount(mp)
2086 struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2087 struct snapdata *sn;
2092 sn = devvp->v_rdev->si_snapdata;
2093 while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2095 TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2096 xp->i_nextsnap.tqe_prev = 0;
2097 lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE,
2099 lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL);
2100 KASSERT(vp->v_vnlock == &sn->sn_lock,
2101 ("ffs_snapshot_unmount: lost lock mutation"));
2102 vp->v_vnlock = &vp->v_lock;
2103 lockmgr(&vp->v_lock, LK_RELEASE, NULL);
2104 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2105 if (xp->i_effnlink > 0)
2108 sn = devvp->v_rdev->si_snapdata;
2110 try_free_snapdata(devvp);
2111 ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2115 * Check the buffer block to be belong to device buffer that shall be
2116 * locked after snaplk. devvp shall be locked on entry, and will be
2117 * leaved locked upon exit.
2120 ffs_bp_snapblk(devvp, bp)
2121 struct vnode *devvp;
2124 struct snapdata *sn;
2126 ufs2_daddr_t lbn, *snapblklist;
2127 int lower, upper, mid;
2129 ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2130 KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2131 sn = devvp->v_rdev->si_snapdata;
2132 if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2134 fs = ITOFS(TAILQ_FIRST(&sn->sn_head));
2135 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2136 snapblklist = sn->sn_blklist;
2137 upper = sn->sn_listsize - 1;
2139 while (lower <= upper) {
2140 mid = (lower + upper) / 2;
2141 if (snapblklist[mid] == lbn)
2143 if (snapblklist[mid] < lbn)
2159 struct vnode *vp, *devvp;
2163 if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2168 devvp = bo2vnode(bo);
2169 KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2172 bp_bdskip = ffs_bp_snapblk(devvp, bp);
2176 if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2177 (void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2182 * Try to find a buffer to flush.
2184 TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2185 if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2187 LK_EXCLUSIVE | LK_NOWAIT, NULL))
2190 panic("bdwrite: found ourselves");
2193 * Don't countdeps with the bo lock
2196 if (buf_countdeps(nbp, 0)) {
2203 if (!ffs_bp_snapblk(vp, nbp)) {
2211 if (nbp->b_flags & B_CLUSTEROK) {
2212 vfs_bio_awrite(nbp);
2217 dirtybufferflushes++;
2226 * Check for need to copy block that is about to be written,
2227 * copying the block if necessary.
2230 ffs_copyonwrite(devvp, bp)
2231 struct vnode *devvp;
2234 struct snapdata *sn;
2235 struct buf *ibp, *cbp, *savedcbp = NULL;
2236 struct thread *td = curthread;
2239 struct vnode *vp = NULL;
2240 ufs2_daddr_t lbn, blkno, *snapblklist;
2241 int lower, upper, mid, indiroff, error = 0;
2242 int launched_async_io, prev_norunningbuf;
2243 long saved_runningbufspace;
2245 if (devvp != bp->b_vp && IS_SNAPSHOT(VTOI(bp->b_vp)))
2246 return (0); /* Update on a snapshot file */
2247 if (td->td_pflags & TDP_COWINPROGRESS)
2248 panic("ffs_copyonwrite: recursive call");
2250 * First check to see if it is in the preallocated list.
2251 * By doing this check we avoid several potential deadlocks.
2254 sn = devvp->v_rdev->si_snapdata;
2256 TAILQ_EMPTY(&sn->sn_head)) {
2258 return (0); /* No snapshot */
2260 ip = TAILQ_FIRST(&sn->sn_head);
2262 lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2263 snapblklist = sn->sn_blklist;
2264 upper = sn->sn_listsize - 1;
2266 while (lower <= upper) {
2267 mid = (lower + upper) / 2;
2268 if (snapblklist[mid] == lbn)
2270 if (snapblklist[mid] < lbn)
2275 if (lower <= upper) {
2279 launched_async_io = 0;
2280 prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2282 * Since I/O on bp isn't yet in progress and it may be blocked
2283 * for a long time waiting on snaplk, back it out of
2284 * runningbufspace, possibly waking other threads waiting for space.
2286 saved_runningbufspace = bp->b_runningbufspace;
2287 if (saved_runningbufspace != 0)
2288 runningbufwakeup(bp);
2290 * Not in the precomputed list, so check the snapshots.
2292 while (lockmgr(&sn->sn_lock, LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2293 VI_MTX(devvp)) != 0) {
2295 sn = devvp->v_rdev->si_snapdata;
2297 TAILQ_EMPTY(&sn->sn_head)) {
2299 if (saved_runningbufspace != 0) {
2300 bp->b_runningbufspace = saved_runningbufspace;
2301 atomic_add_long(&runningbufspace,
2302 bp->b_runningbufspace);
2304 return (0); /* Snapshot gone */
2307 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2309 if (DOINGSOFTDEP(vp))
2310 softdep_prealloc(vp, MNT_WAIT);
2312 * We ensure that everything of our own that needs to be
2313 * copied will be done at the time that ffs_snapshot is
2314 * called. Thus we can skip the check here which can
2315 * deadlock in doing the lookup in UFS_BALLOC.
2320 * Check to see if block needs to be copied. We do not have
2321 * to hold the snapshot lock while doing this lookup as it
2322 * will never require any additional allocations for the
2325 if (lbn < UFS_NDADDR) {
2326 blkno = DIP(ip, i_db[lbn]);
2328 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2329 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2330 fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2331 td->td_pflags &= ~TDP_COWINPROGRESS;
2334 indiroff = (lbn - UFS_NDADDR) % NINDIR(fs);
2336 blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2338 blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2342 if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2343 panic("ffs_copyonwrite: bad copy block");
2348 * Allocate the block into which to do the copy. Since
2349 * multiple processes may all try to copy the same block,
2350 * we have to recheck our need to do a copy if we sleep
2351 * waiting for the lock.
2353 * Because all snapshots on a filesystem share a single
2354 * lock, we ensure that we will never be in competition
2355 * with another process to allocate a block.
2357 td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2358 error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2359 fs->fs_bsize, KERNCRED, 0, &cbp);
2360 td->td_pflags &= ~TDP_COWINPROGRESS;
2365 printf("Copyonwrite: snapino %ju lbn %jd for ",
2366 (uintmax_t)ip->i_number, (intmax_t)lbn);
2367 if (bp->b_vp == devvp)
2368 printf("fs metadata");
2371 (uintmax_t)VTOI(bp->b_vp)->i_number);
2372 printf(" lblkno %jd to blkno %jd\n",
2373 (intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2377 * If we have already read the old block contents, then
2378 * simply copy them to the new block. Note that we need
2379 * to synchronously write snapshots that have not been
2380 * unlinked, and hence will be visible after a crash,
2381 * to ensure their integrity. At a minimum we ensure the
2382 * integrity of the filesystem metadata, but use the
2383 * dopersistence sysctl-setable flag to decide on the
2384 * persistence needed for file content data.
2386 if (savedcbp != NULL) {
2387 bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2389 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2390 dopersistence) && ip->i_effnlink > 0)
2391 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2393 launched_async_io = 1;
2397 * Otherwise, read the old block contents into the buffer.
2399 if ((error = readblock(vp, cbp, lbn)) != 0) {
2400 bzero(cbp->b_data, fs->fs_bsize);
2402 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2403 dopersistence) && ip->i_effnlink > 0)
2404 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2406 launched_async_io = 1;
2412 * Note that we need to synchronously write snapshots that
2413 * have not been unlinked, and hence will be visible after
2414 * a crash, to ensure their integrity. At a minimum we
2415 * ensure the integrity of the filesystem metadata, but
2416 * use the dopersistence sysctl-setable flag to decide on
2417 * the persistence needed for file content data.
2420 vp = savedcbp->b_vp;
2422 if ((devvp == bp->b_vp || bp->b_vp->v_type == VDIR ||
2423 dopersistence) && VTOI(vp)->i_effnlink > 0)
2424 (void) ffs_syncvnode(vp, MNT_WAIT, NO_INO_UPDT);
2426 launched_async_io = 1;
2428 lockmgr(vp->v_vnlock, LK_RELEASE, NULL);
2429 td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2431 if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2432 waitrunningbufspace();
2434 * I/O on bp will now be started, so count it in runningbufspace.
2436 if (saved_runningbufspace != 0) {
2437 bp->b_runningbufspace = saved_runningbufspace;
2438 atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2444 * sync snapshots to force freework records waiting on snapshots to claim
2448 ffs_sync_snap(mp, waitfor)
2452 struct snapdata *sn;
2453 struct vnode *devvp;
2457 devvp = VFSTOUFS(mp)->um_devvp;
2458 if ((devvp->v_vflag & VV_COPYONWRITE) == 0)
2462 sn = devvp->v_rdev->si_snapdata;
2467 if (lockmgr(&sn->sn_lock,
2468 LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2469 VI_MTX(devvp)) == 0)
2472 TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2474 ffs_syncvnode(vp, waitfor, NO_INO_UPDT);
2476 lockmgr(&sn->sn_lock, LK_RELEASE, NULL);
2480 * Read the specified block into the given buffer.
2481 * Much of this boiler-plate comes from bwrite().
2484 readblock(vp, bp, lbn)
2489 struct inode *ip = VTOI(vp);
2496 bip = g_alloc_bio();
2497 bip->bio_cmd = BIO_READ;
2498 bip->bio_offset = dbtob(fsbtodb(fs, blkstofrags(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, ITODEVVP(ip)->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, *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();
2653 sn = devvp->v_rdev->si_snapdata;
2656 * This is the first snapshot on this
2657 * filesystem and we use our pre-allocated
2658 * snapdata. Publish sn with the sn_lock
2659 * owned by us, to avoid the race.
2661 error = lockmgr(&nsn->sn_lock, LK_EXCLUSIVE |
2664 panic("leaked sn, lockmgr error %d", error);
2665 sn = devvp->v_rdev->si_snapdata = nsn;
2672 * There is a snapshots which already exists on this
2673 * filesystem, grab a reference to the common lock.
2675 error = lockmgr(&sn->sn_lock, LK_INTERLOCK |
2676 LK_EXCLUSIVE | LK_SLEEPFAIL, VI_MTX(devvp));
2682 * Free any unused snapdata.
2685 ffs_snapdata_free(nsn);