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: src/sys/ufs/ffs/ffs_snapshot.c,v 1.136.2.6.2.1 2010/02/10 00:26:20 kensmith Exp $");
39
#include "opt_quota.h"
41
#include <sys/param.h>
42
#include <sys/systm.h>
43
#include <sys/kernel.h>
47
#include <sys/fcntl.h>
49
#include <sys/namei.h>
50
#include <sys/sched.h>
52
#include <sys/malloc.h>
53
#include <sys/mount.h>
54
#include <sys/resource.h>
55
#include <sys/resourcevar.h>
56
#include <sys/vnode.h>
58
#include <geom/geom.h>
60
#include <ufs/ufs/extattr.h>
61
#include <ufs/ufs/quota.h>
62
#include <ufs/ufs/ufsmount.h>
63
#include <ufs/ufs/inode.h>
64
#include <ufs/ufs/ufs_extern.h>
66
#include <ufs/ffs/fs.h>
67
#include <ufs/ffs/ffs_extern.h>
69
#define KERNCRED thread0.td_ucred
74
#ifdef NO_FFS_SNAPSHOT
76
ffs_snapshot(mp, snapfile)
84
ffs_snapblkfree(fs, devvp, bno, size, inum)
101
ffs_snapshot_mount(mp)
107
ffs_snapshot_unmount(mp)
119
ffs_copyonwrite(devvp, bp)
128
TAILQ_HEAD(snaphead, inode);
131
struct snaphead sn_head;
137
static int cgaccount(int, struct vnode *, struct buf *, int);
138
static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
139
int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
140
ufs_lbn_t, int), int);
141
static int indiracct_ufs1(struct vnode *, struct vnode *, int,
142
ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
143
int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
144
ufs_lbn_t, int), int);
145
static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
146
struct fs *, ufs_lbn_t, int);
147
static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
148
struct fs *, ufs_lbn_t, int);
149
static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
150
struct fs *, ufs_lbn_t, int);
151
static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
152
int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
153
ufs_lbn_t, int), int);
154
static int indiracct_ufs2(struct vnode *, struct vnode *, int,
155
ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
156
int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
157
ufs_lbn_t, int), int);
158
static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
159
struct fs *, ufs_lbn_t, int);
160
static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
161
struct fs *, ufs_lbn_t, int);
162
static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
163
struct fs *, ufs_lbn_t, int);
164
static int readblock(struct vnode *vp, struct buf *, ufs2_daddr_t);
165
static void try_free_snapdata(struct vnode *devvp, struct thread *td);
166
static int ffs_bp_snapblk(struct vnode *, struct buf *);
169
* To ensure the consistency of snapshots across crashes, we must
170
* synchronously write out copied blocks before allowing the
171
* originals to be modified. Because of the rather severe speed
172
* penalty that this imposes, the following flag allows this
173
* crash persistence to be disabled.
175
int dopersistence = 0;
178
#include <sys/sysctl.h>
179
SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
180
static int snapdebug = 0;
181
SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
182
int collectsnapstats = 0;
183
SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
188
* Create a snapshot file and initialize it for the filesystem.
191
ffs_snapshot(mp, snapfile)
195
ufs2_daddr_t numblks, blkno, *blkp, *snapblklist;
196
int error, cg, snaploc;
197
int i, size, len, loc;
199
struct timespec starttime = {0, 0}, endtime;
201
long redo = 0, snaplistsize = 0;
204
struct fs *copy_fs = NULL, *fs;
205
struct thread *td = curthread;
206
struct inode *ip, *xp;
207
struct buf *bp, *nbp, *ibp, *sbp = NULL;
211
struct vnode *vp, *xvp, *mvp, *devvp;
215
struct ufsmount *ump;
225
* Need to serialize access to snapshot code per filesystem.
228
* Assign a snapshot slot in the superblock.
231
for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
232
if (fs->fs_snapinum[snaploc] == 0)
235
if (snaploc == FSMAXSNAP)
238
* Create the snapshot file.
241
NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_SYSSPACE, snapfile, td);
242
if ((error = namei(&nd)) != 0)
244
if (nd.ni_vp != NULL) {
248
if (nd.ni_dvp->v_mount != mp)
251
NDFREE(&nd, NDF_ONLY_PNBUF);
252
if (nd.ni_dvp == nd.ni_vp)
260
vat.va_mode = S_IRUSR;
261
vat.va_vaflags |= VA_EXCLUSIVE;
262
if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
265
panic("ffs_snapshot: mount mismatch");
267
if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
268
NDFREE(&nd, NDF_ONLY_PNBUF);
270
if ((error = vn_start_write(NULL, &wrtmp,
271
V_XSLEEP | PCATCH)) != 0)
275
VOP_LEASE(nd.ni_dvp, td, KERNCRED, LEASE_WRITE);
276
error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
277
VOP_UNLOCK(nd.ni_dvp, 0, td);
279
NDFREE(&nd, NDF_ONLY_PNBUF);
280
vn_finished_write(wrtmp);
285
vp->v_vflag |= VV_SYSTEM;
289
* Allocate and copy the last block contents so as to be able
290
* to set size to that of the filesystem.
292
numblks = howmany(fs->fs_size, fs->fs_frag);
293
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
294
fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
297
ip->i_size = lblktosize(fs, (off_t)numblks);
298
DIP_SET(ip, i_size, ip->i_size);
299
ip->i_flag |= IN_CHANGE | IN_UPDATE;
300
error = readblock(vp, bp, numblks - 1);
305
* Preallocate critical data structures so that we can copy
306
* them in without further allocation after we suspend all
307
* operations on the filesystem. We would like to just release
308
* the allocated buffers without writing them since they will
309
* be filled in below once we are ready to go, but this upsets
310
* the soft update code, so we go ahead and write the new buffers.
312
* Allocate all indirect blocks and mark all of them as not
313
* needing to be copied.
315
for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
316
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
317
fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
323
* Allocate copies for the superblock and its summary information.
325
error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_sbsize, KERNCRED,
330
blkno = fragstoblks(fs, fs->fs_csaddr);
331
len = howmany(fs->fs_cssize, fs->fs_bsize);
332
for (loc = 0; loc < len; loc++) {
333
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
334
fs->fs_bsize, KERNCRED, 0, &nbp);
340
* Allocate all cylinder group blocks.
342
for (cg = 0; cg < fs->fs_ncg; cg++) {
343
error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
344
fs->fs_bsize, KERNCRED, 0, &nbp);
349
ffs_syncvnode(vp, MNT_WAIT);
352
* Copy all the cylinder group maps. Although the
353
* filesystem is still active, we hope that only a few
354
* cylinder groups will change between now and when we
355
* suspend operations. Thus, we will be able to quickly
356
* touch up the few cylinder groups that changed during
357
* the suspension period.
359
len = howmany(fs->fs_ncg, NBBY);
360
MALLOC(space, void *, len, M_DEVBUF, M_WAITOK|M_ZERO);
362
fs->fs_active = space;
364
for (cg = 0; cg < fs->fs_ncg; cg++) {
365
error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
366
fs->fs_bsize, KERNCRED, 0, &nbp);
369
error = cgaccount(cg, vp, nbp, 1);
372
ffs_syncvnode(vp, MNT_WAIT);
377
* Change inode to snapshot type file.
379
ip->i_flags |= SF_SNAPSHOT;
380
DIP_SET(ip, i_flags, ip->i_flags);
381
ip->i_flag |= IN_CHANGE | IN_UPDATE;
383
* Ensure that the snapshot is completely on disk.
384
* Since we have marked it as a snapshot it is safe to
385
* unlock it as no process will be allowed to write to it.
387
if ((error = ffs_syncvnode(vp, MNT_WAIT)) != 0)
389
VOP_UNLOCK(vp, 0, td);
391
* All allocations are done, so we can now snapshot the system.
393
* Recind nice scheduling while running with the filesystem suspended.
395
if (td->td_proc->p_nice > 0) {
401
saved_nice = p->p_nice;
407
* Suspend operation on filesystem.
410
vn_finished_write(wrtmp);
411
if ((error = vfs_write_suspend(vp->v_mount)) != 0) {
412
vn_start_write(NULL, &wrtmp, V_WAIT);
413
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
416
if (mp->mnt_kern_flag & MNTK_SUSPENDED)
418
vn_start_write(NULL, &wrtmp, V_WAIT);
420
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
421
if (ip->i_effnlink == 0) {
422
error = ENOENT; /* Snapshot file unlinked */
425
if (collectsnapstats)
426
nanotime(&starttime);
428
/* The last block might have changed. Copy it again to be sure. */
429
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
430
fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
433
error = readblock(vp, bp, numblks - 1);
434
bp->b_flags |= B_VALIDSUSPWRT;
439
* First, copy all the cylinder group maps that have changed.
441
for (cg = 0; cg < fs->fs_ncg; cg++) {
442
if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
445
error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
446
fs->fs_bsize, KERNCRED, 0, &nbp);
449
error = cgaccount(cg, vp, nbp, 2);
455
* Grab a copy of the superblock and its summary information.
456
* We delay writing it until the suspension is released below.
458
error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
465
loc = blkoff(fs, fs->fs_sblockloc);
466
copy_fs = (struct fs *)(sbp->b_data + loc);
467
bcopy(fs, copy_fs, fs->fs_sbsize);
468
if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
469
copy_fs->fs_clean = 1;
470
size = fs->fs_bsize < SBLOCKSIZE ? fs->fs_bsize : SBLOCKSIZE;
471
if (fs->fs_sbsize < size)
472
bzero(&sbp->b_data[loc + fs->fs_sbsize], size - fs->fs_sbsize);
473
size = blkroundup(fs, fs->fs_cssize);
474
if (fs->fs_contigsumsize > 0)
475
size += fs->fs_ncg * sizeof(int32_t);
476
space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
477
copy_fs->fs_csp = space;
478
bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
479
space = (char *)space + fs->fs_cssize;
480
loc = howmany(fs->fs_cssize, fs->fs_fsize);
481
i = fs->fs_frag - loc % fs->fs_frag;
482
len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
484
if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
485
len, KERNCRED, &bp)) != 0) {
487
free(copy_fs->fs_csp, M_UFSMNT);
492
bcopy(bp->b_data, space, (u_int)len);
493
space = (char *)space + len;
494
bp->b_flags |= B_INVAL | B_NOCACHE;
497
if (fs->fs_contigsumsize > 0) {
498
copy_fs->fs_maxcluster = lp = space;
499
for (i = 0; i < fs->fs_ncg; i++)
500
*lp++ = fs->fs_contigsumsize;
503
* We must check for active files that have been unlinked
504
* (e.g., with a zero link count). We have to expunge all
505
* trace of these files from the snapshot so that they are
506
* not reclaimed prematurely by fsck or unnecessarily dumped.
507
* We turn off the MNTK_SUSPENDED flag to avoid a panic from
508
* spec_strategy about writing on a suspended filesystem.
509
* Note that we skip unlinked snapshot files as they will
510
* be handled separately below.
512
* We also calculate the needed size for the snapshot list.
514
snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
515
FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
517
mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
519
MNT_VNODE_FOREACH(xvp, mp, mvp) {
522
if ((xvp->v_iflag & VI_DOOMED) ||
523
(xvp->v_usecount == 0 &&
524
(xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) ||
525
xvp->v_type == VNON ||
526
(VTOI(xvp)->i_flags & SF_SNAPSHOT)) {
532
* We can skip parent directory vnode because it must have
533
* this snapshot file in it.
535
if (xvp == nd.ni_dvp) {
541
if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0) {
543
MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
548
if (xvp->v_usecount == 0 &&
549
(xvp->v_iflag & (VI_OWEINACT | VI_DOINGINACT)) == 0) {
551
VOP_UNLOCK(xvp, 0, td);
558
vprint("ffs_snapshot: busy vnode", xvp);
559
if (VOP_GETATTR(xvp, &vat, td->td_ucred, td) == 0 &&
561
VOP_UNLOCK(xvp, 0, td);
567
if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
568
VOP_UNLOCK(xvp, 0, td);
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);
583
blkno = DIP(xp, i_db[loc]);
584
DIP_SET(xp, i_db[loc], 0);
588
if (xp->i_ump->um_fstype == UFS1)
589
error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
592
error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
595
DIP_SET(xp, i_db[loc], blkno);
597
error = ffs_freefile(ump, copy_fs, vp, xp->i_number,
599
VOP_UNLOCK(xvp, 0, td);
602
free(copy_fs->fs_csp, M_UFSMNT);
605
MNT_VNODE_FOREACH_ABORT(mp, mvp);
612
* If there already exist snapshots on this filesystem, grab a
613
* reference to their shared lock. If this is the first snapshot
614
* on this filesystem, we need to allocate a lock for the snapshots
615
* to share. In either case, acquire the snapshot lock and give
616
* up our original private lock.
619
sn = devvp->v_rdev->si_snapdata;
621
xp = TAILQ_FIRST(&sn->sn_head);
624
vp->v_vnlock = &sn->sn_lock;
627
sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
628
TAILQ_INIT(&sn->sn_head);
629
lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
630
LK_CANRECURSE | LK_NOSHARE);
632
vp->v_vnlock = &sn->sn_lock;
633
mp_fixme("si_snapdata setting is racey.");
634
devvp->v_rdev->si_snapdata = sn;
637
lockmgr(vp->v_vnlock, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY,
639
lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
641
* If this is the first snapshot on this filesystem, then we need
642
* to allocate the space for the list of preallocated snapshot blocks.
643
* This list will be refined below, but this preliminary one will
644
* keep us out of deadlock until the full one is ready.
647
MALLOC(snapblklist, daddr_t *, snaplistsize * sizeof(daddr_t),
649
blkp = &snapblklist[1];
650
*blkp++ = lblkno(fs, fs->fs_sblockloc);
651
blkno = fragstoblks(fs, fs->fs_csaddr);
652
for (cg = 0; cg < fs->fs_ncg; cg++) {
653
if (fragstoblks(fs, cgtod(fs, cg) > blkno))
655
*blkp++ = fragstoblks(fs, cgtod(fs, cg));
657
len = howmany(fs->fs_cssize, fs->fs_bsize);
658
for (loc = 0; loc < len; loc++)
659
*blkp++ = blkno + loc;
660
for (; cg < fs->fs_ncg; cg++)
661
*blkp++ = fragstoblks(fs, cgtod(fs, cg));
662
snapblklist[0] = blkp - snapblklist;
664
if (sn->sn_blklist != NULL)
665
panic("ffs_snapshot: non-empty list");
666
sn->sn_blklist = snapblklist;
667
sn->sn_listsize = blkp - snapblklist;
671
* Record snapshot inode. Since this is the newest snapshot,
672
* it must be placed at the end of the list.
675
fs->fs_snapinum[snaploc] = ip->i_number;
676
if (ip->i_nextsnap.tqe_prev != 0)
677
panic("ffs_snapshot: %d already on list", ip->i_number);
678
TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
679
devvp->v_vflag |= VV_COPYONWRITE;
681
ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
683
KASSERT((sn != NULL && sbp != NULL && error == 0) ||
684
(sn == NULL && sbp == NULL && error != 0),
685
("email phk@ and mckusick@"));
687
* Resume operation on filesystem.
689
vfs_write_resume(vp->v_mount);
690
vn_start_write(NULL, &wrtmp, V_WAIT);
691
if (collectsnapstats && starttime.tv_sec > 0) {
693
timespecsub(&endtime, &starttime);
694
printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
695
vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
696
endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
701
* Copy allocation information from all the snapshots in
702
* this snapshot and then expunge them from its view.
704
TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap) {
707
if (xp->i_ump->um_fstype == UFS1)
708
error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
711
error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
713
if (error == 0 && xp->i_effnlink == 0) {
714
error = ffs_freefile(ump,
721
fs->fs_snapinum[snaploc] = 0;
726
* Allocate space for the full list of preallocated snapshot blocks.
728
MALLOC(snapblklist, daddr_t *, snaplistsize * sizeof(daddr_t),
730
ip->i_snapblklist = &snapblklist[1];
732
* Expunge the blocks used by the snapshots from the set of
733
* blocks marked as used in the snapshot bitmaps. Also, collect
734
* the list of allocated blocks in i_snapblklist.
736
if (ip->i_ump->um_fstype == UFS1)
737
error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, BLK_SNAP);
739
error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, BLK_SNAP);
741
fs->fs_snapinum[snaploc] = 0;
742
FREE(snapblklist, M_UFSMNT);
745
if (snaplistsize < ip->i_snapblklist - snapblklist)
746
panic("ffs_snapshot: list too small");
747
snaplistsize = ip->i_snapblklist - snapblklist;
748
snapblklist[0] = snaplistsize;
749
ip->i_snapblklist = 0;
751
* Write out the list of allocated blocks to the end of the snapshot.
753
auio.uio_iov = &aiov;
755
aiov.iov_base = (void *)snapblklist;
756
aiov.iov_len = snaplistsize * sizeof(daddr_t);
757
auio.uio_resid = aiov.iov_len;;
758
auio.uio_offset = ip->i_size;
759
auio.uio_segflg = UIO_SYSSPACE;
760
auio.uio_rw = UIO_WRITE;
762
if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
763
fs->fs_snapinum[snaploc] = 0;
764
FREE(snapblklist, M_UFSMNT);
768
* Write the superblock and its summary information
771
blkno = fragstoblks(fs, fs->fs_csaddr);
772
len = howmany(fs->fs_cssize, fs->fs_bsize);
773
space = copy_fs->fs_csp;
774
for (loc = 0; loc < len; loc++) {
775
error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
778
fs->fs_snapinum[snaploc] = 0;
779
FREE(snapblklist, M_UFSMNT);
782
bcopy(space, nbp->b_data, fs->fs_bsize);
783
space = (char *)space + fs->fs_bsize;
787
* As this is the newest list, it is the most inclusive, so
788
* should replace the previous list.
791
space = sn->sn_blklist;
792
sn->sn_blklist = snapblklist;
793
sn->sn_listsize = snaplistsize;
796
FREE(space, M_UFSMNT);
798
* If another process is currently writing the buffer containing
799
* the inode for this snapshot then a deadlock can occur. Drop
800
* the snapshot lock until the buffer has been written.
802
VREF(vp); /* Protect against ffs_snapgone() */
803
VOP_UNLOCK(vp, 0, td);
804
(void) bread(ip->i_devvp,
805
fsbtodb(fs, ino_to_fsba(fs, ip->i_number)),
806
(int) fs->fs_bsize, NOCRED, &nbp);
808
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
809
if (ip->i_effnlink == 0)
810
error = ENOENT; /* Snapshot file unlinked */
812
vrele(vp); /* Drop extra reference */
814
FREE(copy_fs->fs_csp, M_UFSMNT);
817
NDFREE(&nd, NDF_ONLY_PNBUF);
818
if (saved_nice > 0) {
824
sched_nice(td->td_proc, saved_nice);
826
PROC_UNLOCK(td->td_proc);
829
if (fs->fs_active != 0) {
830
FREE(fs->fs_active, M_DEVBUF);
835
mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | (flag & ~MNT_QUOTA);
838
(void) ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
839
(void) ffs_syncvnode(vp, MNT_WAIT);
843
VOP_UNLOCK(vp, 0, td);
845
vn_finished_write(wrtmp);
846
process_deferred_inactive(mp);
851
* Copy a cylinder group map. All the unallocated blocks are marked
852
* BLK_NOCOPY so that the snapshot knows that it need not copy them
853
* if they are later written. If passno is one, then this is a first
854
* pass, so only setting needs to be done. If passno is 2, then this
855
* is a revision to a previous pass which must be undone as the
856
* replacement pass is done.
859
cgaccount(cg, vp, nbp, passno)
865
struct buf *bp, *ibp;
869
ufs2_daddr_t base, numblks;
870
int error, len, loc, indiroff;
874
error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
875
(int)fs->fs_cgsize, KERNCRED, &bp);
880
cgp = (struct cg *)bp->b_data;
881
if (!cg_chkmagic(cgp)) {
888
* Recomputation of summary information might not have been performed
889
* at mount time. Sync up summary information for current cylinder
890
* group while data is in memory to ensure that result of background
891
* fsck is slightly more consistent.
893
fs->fs_cs(fs, cg) = cgp->cg_cs;
894
UFS_UNLOCK(ip->i_ump);
895
bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
896
if (fs->fs_cgsize < fs->fs_bsize)
897
bzero(&nbp->b_data[fs->fs_cgsize],
898
fs->fs_bsize - fs->fs_cgsize);
899
cgp = (struct cg *)nbp->b_data;
902
nbp->b_flags |= B_VALIDSUSPWRT;
903
numblks = howmany(fs->fs_size, fs->fs_frag);
904
len = howmany(fs->fs_fpg, fs->fs_frag);
905
base = cgbase(fs, cg) / fs->fs_frag;
906
if (base + len >= numblks)
907
len = numblks - base - 1;
910
for ( ; loc < NDADDR; loc++) {
911
if (ffs_isblock(fs, cg_blksfree(cgp), loc))
912
DIP_SET(ip, i_db[loc], BLK_NOCOPY);
913
else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
914
DIP_SET(ip, i_db[loc], 0);
915
else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
916
panic("ffs_snapshot: lost direct block");
919
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
920
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
924
indiroff = (base + loc - NDADDR) % NINDIR(fs);
925
for ( ; loc < len; loc++, indiroff++) {
926
if (indiroff >= NINDIR(fs)) {
928
ibp->b_flags |= B_VALIDSUSPWRT;
930
error = UFS_BALLOC(vp,
931
lblktosize(fs, (off_t)(base + loc)),
932
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
938
if (ip->i_ump->um_fstype == UFS1) {
939
if (ffs_isblock(fs, cg_blksfree(cgp), loc))
940
((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
942
else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
943
[indiroff] == BLK_NOCOPY)
944
((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
945
else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
946
[indiroff] == BLK_NOCOPY)
947
panic("ffs_snapshot: lost indirect block");
950
if (ffs_isblock(fs, cg_blksfree(cgp), loc))
951
((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
952
else if (passno == 2 &&
953
((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
954
((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
955
else if (passno == 1 &&
956
((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
957
panic("ffs_snapshot: lost indirect block");
960
ibp->b_flags |= B_VALIDSUSPWRT;
966
* Before expunging a snapshot inode, note all the
967
* blocks that it claims with BLK_SNAP so that fsck will
968
* be able to account for those blocks properly and so
969
* that this snapshot knows that it need not copy them
970
* if the other snapshot holding them is freed. This code
971
* is reproduced once each for UFS1 and UFS2.
974
expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype)
975
struct vnode *snapvp;
976
struct inode *cancelip;
978
int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
979
struct fs *, ufs_lbn_t, int);
982
int i, error, indiroff;
984
ufs2_daddr_t len, blkno, numblks, blksperindir;
985
struct ufs1_dinode *dip;
986
struct thread *td = curthread;
990
* Prepare to expunge the inode. If its inode block has not
991
* yet been copied, then allocate and fill the copy.
993
lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
996
blkno = VTOI(snapvp)->i_din1->di_db[lbn];
998
td->td_pflags |= TDP_COWINPROGRESS;
999
error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1000
fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1001
td->td_pflags &= ~TDP_COWINPROGRESS;
1004
indiroff = (lbn - NDADDR) % NINDIR(fs);
1005
blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
1009
if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1012
error = ffs_balloc_ufs1(snapvp, lblktosize(fs, (off_t)lbn),
1013
fs->fs_bsize, KERNCRED, 0, &bp);
1016
if ((error = readblock(snapvp, bp, lbn)) != 0)
1020
* Set a snapshot inode to be a zero length file, regular files
1021
* or unlinked snapshots to be completely unallocated.
1023
dip = (struct ufs1_dinode *)bp->b_data +
1024
ino_to_fsbo(fs, cancelip->i_number);
1025
if (expungetype == BLK_NOCOPY || cancelip->i_effnlink == 0)
1029
dip->di_flags &= ~SF_SNAPSHOT;
1030
bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
1033
* Now go through and expunge all the blocks in the file
1034
* using the function requested.
1036
numblks = howmany(cancelip->i_size, fs->fs_bsize);
1037
if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
1038
&cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
1040
if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
1041
&cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
1045
len = numblks - NDADDR;
1047
for (i = 0; len > 0 && i < NIADDR; i++) {
1048
error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
1049
cancelip->i_din1->di_ib[i], lbn, rlbn, len,
1050
blksperindir, fs, acctfunc, expungetype);
1053
blksperindir *= NINDIR(fs);
1054
lbn -= blksperindir + 1;
1055
len -= blksperindir;
1056
rlbn += blksperindir;
1062
* Descend an indirect block chain for vnode cancelvp accounting for all
1063
* its indirect blocks in snapvp.
1066
indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1067
blksperindir, fs, acctfunc, expungetype)
1068
struct vnode *snapvp;
1069
struct vnode *cancelvp;
1075
ufs_lbn_t blksperindir;
1077
int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
1078
struct fs *, ufs_lbn_t, int);
1082
ufs_lbn_t subblksperindir;
1083
struct indir indirs[NIADDR + 2];
1084
ufs1_daddr_t last, *bap;
1088
if (expungetype == BLK_NOCOPY)
1090
panic("indiracct_ufs1: missing indir");
1092
if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1094
if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1095
panic("indiracct_ufs1: botched params");
1097
* We have to expand bread here since it will deadlock looking
1098
* up the block number for any blocks that are not in the cache.
1100
bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1101
bp->b_blkno = fsbtodb(fs, blkno);
1102
if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1103
(error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1108
* Account for the block pointers in this indirect block.
1110
last = howmany(remblks, blksperindir);
1111
if (last > NINDIR(fs))
1113
MALLOC(bap, ufs1_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
1114
bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1116
error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1117
level == 0 ? rlbn : -1, expungetype);
1118
if (error || level == 0)
1121
* Account for the block pointers in each of the indirect blocks
1122
* in the levels below us.
1124
subblksperindir = blksperindir / NINDIR(fs);
1125
for (lbn++, level--, i = 0; i < last; i++) {
1126
error = indiracct_ufs1(snapvp, cancelvp, level, bap[i], lbn,
1127
rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1130
rlbn += blksperindir;
1131
lbn -= blksperindir;
1132
remblks -= blksperindir;
1135
FREE(bap, M_DEVBUF);
1140
* Do both snap accounting and map accounting.
1143
fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1145
ufs1_daddr_t *oldblkp, *lastblkp;
1148
int exptype; /* BLK_SNAP or BLK_NOCOPY */
1152
if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1154
return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1158
* Identify a set of blocks allocated in a snapshot inode.
1161
snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1163
ufs1_daddr_t *oldblkp, *lastblkp;
1166
int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1168
struct inode *ip = VTOI(vp);
1169
ufs1_daddr_t blkno, *blkp;
1174
for ( ; oldblkp < lastblkp; oldblkp++) {
1176
if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1178
lbn = fragstoblks(fs, blkno);
1180
blkp = &ip->i_din1->di_db[lbn];
1181
ip->i_flag |= IN_CHANGE | IN_UPDATE;
1183
error = ffs_balloc_ufs1(vp, lblktosize(fs, (off_t)lbn),
1184
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1187
blkp = &((ufs1_daddr_t *)(ibp->b_data))
1188
[(lbn - NDADDR) % NINDIR(fs)];
1191
* If we are expunging a snapshot vnode and we
1192
* find a block marked BLK_NOCOPY, then it is
1193
* one that has been allocated to this snapshot after
1194
* we took our current snapshot and can be ignored.
1196
if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1201
panic("snapacct_ufs1: bad block");
1202
*blkp = expungetype;
1211
* Account for a set of blocks allocated in a snapshot inode.
1214
mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1216
ufs1_daddr_t *oldblkp, *lastblkp;
1227
inum = ip->i_number;
1232
for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1234
if (blkno == 0 || blkno == BLK_NOCOPY)
1236
if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1237
*ip->i_snapblklist++ = lblkno;
1238
if (blkno == BLK_SNAP)
1239
blkno = blkstofrags(fs, lblkno);
1240
ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1246
* Before expunging a snapshot inode, note all the
1247
* blocks that it claims with BLK_SNAP so that fsck will
1248
* be able to account for those blocks properly and so
1249
* that this snapshot knows that it need not copy them
1250
* if the other snapshot holding them is freed. This code
1251
* is reproduced once each for UFS1 and UFS2.
1254
expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype)
1255
struct vnode *snapvp;
1256
struct inode *cancelip;
1258
int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1259
struct fs *, ufs_lbn_t, int);
1262
int i, error, indiroff;
1263
ufs_lbn_t lbn, rlbn;
1264
ufs2_daddr_t len, blkno, numblks, blksperindir;
1265
struct ufs2_dinode *dip;
1266
struct thread *td = curthread;
1270
* Prepare to expunge the inode. If its inode block has not
1271
* yet been copied, then allocate and fill the copy.
1273
lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
1276
blkno = VTOI(snapvp)->i_din2->di_db[lbn];
1278
td->td_pflags |= TDP_COWINPROGRESS;
1279
error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1280
fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
1281
td->td_pflags &= ~TDP_COWINPROGRESS;
1284
indiroff = (lbn - NDADDR) % NINDIR(fs);
1285
blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
1289
if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
1292
error = ffs_balloc_ufs2(snapvp, lblktosize(fs, (off_t)lbn),
1293
fs->fs_bsize, KERNCRED, 0, &bp);
1296
if ((error = readblock(snapvp, bp, lbn)) != 0)
1300
* Set a snapshot inode to be a zero length file, regular files
1301
* to be completely unallocated.
1303
dip = (struct ufs2_dinode *)bp->b_data +
1304
ino_to_fsbo(fs, cancelip->i_number);
1305
if (expungetype == BLK_NOCOPY)
1309
dip->di_flags &= ~SF_SNAPSHOT;
1310
bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
1313
* Now go through and expunge all the blocks in the file
1314
* using the function requested.
1316
numblks = howmany(cancelip->i_size, fs->fs_bsize);
1317
if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
1318
&cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
1320
if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
1321
&cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
1325
len = numblks - NDADDR;
1327
for (i = 0; len > 0 && i < NIADDR; i++) {
1328
error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
1329
cancelip->i_din2->di_ib[i], lbn, rlbn, len,
1330
blksperindir, fs, acctfunc, expungetype);
1333
blksperindir *= NINDIR(fs);
1334
lbn -= blksperindir + 1;
1335
len -= blksperindir;
1336
rlbn += blksperindir;
1342
* Descend an indirect block chain for vnode cancelvp accounting for all
1343
* its indirect blocks in snapvp.
1346
indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
1347
blksperindir, fs, acctfunc, expungetype)
1348
struct vnode *snapvp;
1349
struct vnode *cancelvp;
1355
ufs_lbn_t blksperindir;
1357
int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
1358
struct fs *, ufs_lbn_t, int);
1362
ufs_lbn_t subblksperindir;
1363
struct indir indirs[NIADDR + 2];
1364
ufs2_daddr_t last, *bap;
1368
if (expungetype == BLK_NOCOPY)
1370
panic("indiracct_ufs2: missing indir");
1372
if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
1374
if (lbn != indirs[num - 1 - level].in_lbn || num < 2)
1375
panic("indiracct_ufs2: botched params");
1377
* We have to expand bread here since it will deadlock looking
1378
* up the block number for any blocks that are not in the cache.
1380
bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0, 0);
1381
bp->b_blkno = fsbtodb(fs, blkno);
1382
if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
1383
(error = readblock(cancelvp, bp, fragstoblks(fs, blkno)))) {
1388
* Account for the block pointers in this indirect block.
1390
last = howmany(remblks, blksperindir);
1391
if (last > NINDIR(fs))
1393
MALLOC(bap, ufs2_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
1394
bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
1396
error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
1397
level == 0 ? rlbn : -1, expungetype);
1398
if (error || level == 0)
1401
* Account for the block pointers in each of the indirect blocks
1402
* in the levels below us.
1404
subblksperindir = blksperindir / NINDIR(fs);
1405
for (lbn++, level--, i = 0; i < last; i++) {
1406
error = indiracct_ufs2(snapvp, cancelvp, level, bap[i], lbn,
1407
rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
1410
rlbn += blksperindir;
1411
lbn -= blksperindir;
1412
remblks -= blksperindir;
1415
FREE(bap, M_DEVBUF);
1420
* Do both snap accounting and map accounting.
1423
fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
1425
ufs2_daddr_t *oldblkp, *lastblkp;
1428
int exptype; /* BLK_SNAP or BLK_NOCOPY */
1432
if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
1434
return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
1438
* Identify a set of blocks allocated in a snapshot inode.
1441
snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1443
ufs2_daddr_t *oldblkp, *lastblkp;
1446
int expungetype; /* BLK_SNAP or BLK_NOCOPY */
1448
struct inode *ip = VTOI(vp);
1449
ufs2_daddr_t blkno, *blkp;
1454
for ( ; oldblkp < lastblkp; oldblkp++) {
1456
if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
1458
lbn = fragstoblks(fs, blkno);
1460
blkp = &ip->i_din2->di_db[lbn];
1461
ip->i_flag |= IN_CHANGE | IN_UPDATE;
1463
error = ffs_balloc_ufs2(vp, lblktosize(fs, (off_t)lbn),
1464
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1467
blkp = &((ufs2_daddr_t *)(ibp->b_data))
1468
[(lbn - NDADDR) % NINDIR(fs)];
1471
* If we are expunging a snapshot vnode and we
1472
* find a block marked BLK_NOCOPY, then it is
1473
* one that has been allocated to this snapshot after
1474
* we took our current snapshot and can be ignored.
1476
if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
1481
panic("snapacct_ufs2: bad block");
1482
*blkp = expungetype;
1491
* Account for a set of blocks allocated in a snapshot inode.
1494
mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
1496
ufs2_daddr_t *oldblkp, *lastblkp;
1507
inum = ip->i_number;
1512
for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
1514
if (blkno == 0 || blkno == BLK_NOCOPY)
1516
if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
1517
*ip->i_snapblklist++ = lblkno;
1518
if (blkno == BLK_SNAP)
1519
blkno = blkstofrags(fs, lblkno);
1520
ffs_blkfree(ip->i_ump, fs, vp, blkno, fs->fs_bsize, inum);
1526
* Decrement extra reference on snapshot when last name is removed.
1527
* It will not be freed until the last open reference goes away.
1536
struct snapdata *sn;
1537
struct ufsmount *ump;
1540
* Find snapshot in incore list.
1543
sn = ip->i_devvp->v_rdev->si_snapdata;
1545
TAILQ_FOREACH(xp, &sn->sn_head, i_nextsnap)
1551
printf("ffs_snapgone: lost snapshot vnode %d\n",
1554
* Delete snapshot inode from superblock. Keep list dense.
1559
for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
1560
if (fs->fs_snapinum[snaploc] == ip->i_number)
1562
if (snaploc < FSMAXSNAP) {
1563
for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
1564
if (fs->fs_snapinum[snaploc] == 0)
1566
fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
1568
fs->fs_snapinum[snaploc - 1] = 0;
1574
* Prepare a snapshot file for being removed.
1581
struct vnode *devvp;
1584
struct thread *td = curthread;
1585
ufs2_daddr_t numblks, blkno, dblk;
1586
int error, loc, last;
1587
struct snapdata *sn;
1591
devvp = ip->i_devvp;
1593
* If active, delete from incore list (this snapshot may
1594
* already have been in the process of being deleted, so
1595
* would not have been active).
1597
* Clear copy-on-write flag if last snapshot.
1600
if (ip->i_nextsnap.tqe_prev != 0) {
1601
sn = devvp->v_rdev->si_snapdata;
1602
TAILQ_REMOVE(&sn->sn_head, ip, i_nextsnap);
1603
ip->i_nextsnap.tqe_prev = 0;
1605
lockmgr(&vp->v_lock, LK_EXCLUSIVE, NULL, td);
1607
KASSERT(vp->v_vnlock == &sn->sn_lock,
1608
("ffs_snapremove: lost lock mutation"));
1609
vp->v_vnlock = &vp->v_lock;
1612
lockmgr(&sn->sn_lock, LK_RELEASE, NULL, td);
1613
try_free_snapdata(devvp, td);
1617
* Clear all BLK_NOCOPY fields. Pass any block claims to other
1618
* snapshots that want them (see ffs_snapblkfree below).
1620
for (blkno = 1; blkno < NDADDR; blkno++) {
1621
dblk = DIP(ip, i_db[blkno]);
1624
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1625
DIP_SET(ip, i_db[blkno], 0);
1626
else if ((dblk == blkstofrags(fs, blkno) &&
1627
ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
1629
DIP_SET(ip, i_blocks, DIP(ip, i_blocks) -
1630
btodb(fs->fs_bsize));
1631
DIP_SET(ip, i_db[blkno], 0);
1634
numblks = howmany(ip->i_size, fs->fs_bsize);
1635
for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
1636
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
1637
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1640
if (fs->fs_size - blkno > NINDIR(fs))
1643
last = fs->fs_size - blkno;
1644
for (loc = 0; loc < last; loc++) {
1645
if (ip->i_ump->um_fstype == UFS1) {
1646
dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
1649
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1650
((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1651
else if ((dblk == blkstofrags(fs, blkno) &&
1652
ffs_snapblkfree(fs, ip->i_devvp, dblk,
1653
fs->fs_bsize, ip->i_number))) {
1654
ip->i_din1->di_blocks -=
1655
btodb(fs->fs_bsize);
1656
((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
1660
dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
1663
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
1664
((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1665
else if ((dblk == blkstofrags(fs, blkno) &&
1666
ffs_snapblkfree(fs, ip->i_devvp, dblk,
1667
fs->fs_bsize, ip->i_number))) {
1668
ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
1669
((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
1675
* Clear snapshot flag and drop reference.
1677
ip->i_flags &= ~SF_SNAPSHOT;
1678
DIP_SET(ip, i_flags, ip->i_flags);
1679
ip->i_flag |= IN_CHANGE | IN_UPDATE;
1682
* Reenable disk quotas for ex-snapshot file.
1684
if (!getinoquota(ip))
1685
(void) chkdq(ip, DIP(ip, i_blocks), KERNCRED, FORCE);
1690
* Notification that a block is being freed. Return zero if the free
1691
* should be allowed to proceed. Return non-zero if the snapshot file
1692
* wants to claim the block. The block will be claimed if it is an
1693
* uncopied part of one of the snapshots. It will be freed if it is
1694
* either a BLK_NOCOPY or has already been copied in all of the snapshots.
1695
* If a fragment is being freed, then all snapshots that care about
1696
* it must make a copy since a snapshot file can only claim full sized
1697
* blocks. Note that if more than one snapshot file maps the block,
1698
* we can pick one at random to claim it. Since none of the snapshots
1699
* can change, we are assurred that they will all see the same unmodified
1700
* image. When deleting a snapshot file (see ffs_snapremove above), we
1701
* must push any of these claimed blocks to one of the other snapshots
1702
* that maps it. These claimed blocks are easily identified as they will
1703
* have a block number equal to their logical block number within the
1704
* snapshot. A copied block can never have this property because they
1705
* must always have been allocated from a BLK_NOCOPY location.
1708
ffs_snapblkfree(fs, devvp, bno, size, inum)
1710
struct vnode *devvp;
1715
struct buf *ibp, *cbp, *savedcbp = 0;
1716
struct thread *td = curthread;
1718
struct vnode *vp = NULL;
1721
int indiroff = 0, error = 0, claimedblk = 0;
1722
struct snapdata *sn;
1724
lbn = fragstoblks(fs, bno);
1727
sn = devvp->v_rdev->si_snapdata;
1732
if (lockmgr(&sn->sn_lock,
1733
LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
1734
VI_MTX(devvp), td) != 0)
1736
TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
1739
* Lookup block being written.
1742
blkno = DIP(ip, i_db[lbn]);
1744
td->td_pflags |= TDP_COWINPROGRESS;
1745
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1746
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
1747
td->td_pflags &= ~TDP_COWINPROGRESS;
1750
indiroff = (lbn - NDADDR) % NINDIR(fs);
1751
if (ip->i_ump->um_fstype == UFS1)
1752
blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
1754
blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
1757
* Check to see if block needs to be copied.
1761
* A block that we map is being freed. If it has not
1762
* been claimed yet, we will claim or copy it (below).
1765
} else if (blkno == BLK_SNAP) {
1767
* No previous snapshot claimed the block,
1768
* so it will be freed and become a BLK_NOCOPY
1769
* (don't care) for us.
1772
panic("snapblkfree: inconsistent block type");
1774
DIP_SET(ip, i_db[lbn], BLK_NOCOPY);
1775
ip->i_flag |= IN_CHANGE | IN_UPDATE;
1776
} else if (ip->i_ump->um_fstype == UFS1) {
1777
((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
1781
((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
1786
} else /* BLK_NOCOPY or default */ {
1788
* If the snapshot has already copied the block
1789
* (default), or does not care about the block,
1797
* If this is a full size block, we will just grab it
1798
* and assign it to the snapshot inode. Otherwise we
1799
* will proceed to copy it. See explanation for this
1800
* routine as to why only a single snapshot needs to
1803
if (size == fs->fs_bsize) {
1806
printf("%s %d lbn %jd from inum %d\n",
1807
"Grabonremove: snapino", ip->i_number,
1808
(intmax_t)lbn, inum);
1811
DIP_SET(ip, i_db[lbn], bno);
1812
} else if (ip->i_ump->um_fstype == UFS1) {
1813
((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
1816
((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
1819
DIP_SET(ip, i_blocks, DIP(ip, i_blocks) + btodb(size));
1820
ip->i_flag |= IN_CHANGE | IN_UPDATE;
1821
lockmgr(vp->v_vnlock, LK_RELEASE, NULL, td);
1827
* Allocate the block into which to do the copy. Note that this
1828
* allocation will never require any additional allocations for
1829
* the snapshot inode.
1831
td->td_pflags |= TDP_COWINPROGRESS;
1832
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
1833
fs->fs_bsize, KERNCRED, 0, &cbp);
1834
td->td_pflags &= ~TDP_COWINPROGRESS;
1839
printf("%s%d lbn %jd %s %d size %ld to blkno %jd\n",
1840
"Copyonremove: snapino ", ip->i_number,
1841
(intmax_t)lbn, "for inum", inum, size,
1842
(intmax_t)cbp->b_blkno);
1845
* If we have already read the old block contents, then
1846
* simply copy them to the new block. Note that we need
1847
* to synchronously write snapshots that have not been
1848
* unlinked, and hence will be visible after a crash,
1849
* to ensure their integrity.
1851
if (savedcbp != 0) {
1852
bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
1854
if (dopersistence && ip->i_effnlink > 0)
1855
(void) ffs_syncvnode(vp, MNT_WAIT);
1859
* Otherwise, read the old block contents into the buffer.
1861
if ((error = readblock(vp, cbp, lbn)) != 0) {
1862
bzero(cbp->b_data, fs->fs_bsize);
1864
if (dopersistence && ip->i_effnlink > 0)
1865
(void) ffs_syncvnode(vp, MNT_WAIT);
1871
* Note that we need to synchronously write snapshots that
1872
* have not been unlinked, and hence will be visible after
1873
* a crash, to ensure their integrity.
1876
vp = savedcbp->b_vp;
1878
if (dopersistence && VTOI(vp)->i_effnlink > 0)
1879
(void) ffs_syncvnode(vp, MNT_WAIT);
1882
* If we have been unable to allocate a block in which to do
1883
* the copy, then return non-zero so that the fragment will
1884
* not be freed. Although space will be lost, the snapshot
1885
* will stay consistent.
1887
lockmgr(vp->v_vnlock, LK_RELEASE, NULL, td);
1892
* Associate snapshot files when mounting.
1895
ffs_snapshot_mount(mp)
1898
struct ufsmount *ump = VFSTOUFS(mp);
1899
struct vnode *devvp = ump->um_devvp;
1900
struct fs *fs = ump->um_fs;
1901
struct thread *td = curthread;
1902
struct snapdata *sn;
1904
struct vnode *lastvp;
1910
daddr_t snaplistsize;
1911
int error, snaploc, loc;
1914
* XXX The following needs to be set before ffs_truncate or
1915
* VOP_READ can be called.
1917
mp->mnt_stat.f_iosize = fs->fs_bsize;
1919
* Process each snapshot listed in the superblock.
1923
sn = devvp->v_rdev->si_snapdata;
1924
for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
1925
if (fs->fs_snapinum[snaploc] == 0)
1927
if ((error = ffs_vget(mp, fs->fs_snapinum[snaploc],
1928
LK_EXCLUSIVE, &vp)) != 0){
1929
printf("ffs_snapshot_mount: vget failed %d\n", error);
1933
if ((ip->i_flags & SF_SNAPSHOT) == 0 || ip->i_size ==
1934
lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
1935
if ((ip->i_flags & SF_SNAPSHOT) == 0) {
1936
reason = "non-snapshot";
1938
reason = "old format snapshot";
1939
(void)ffs_truncate(vp, (off_t)0, 0, NOCRED, td);
1940
(void)ffs_syncvnode(vp, MNT_WAIT);
1942
printf("ffs_snapshot_mount: %s inode %d\n",
1943
reason, fs->fs_snapinum[snaploc]);
1946
for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
1947
if (fs->fs_snapinum[loc] == 0)
1949
fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
1951
fs->fs_snapinum[loc - 1] = 0;
1956
* If there already exist snapshots on this filesystem, grab a
1957
* reference to their shared lock. If this is the first snapshot
1958
* on this filesystem, we need to allocate a lock for the
1959
* snapshots to share. In either case, acquire the snapshot
1960
* lock and give up our original private lock.
1967
vp->v_vnlock = &sn->sn_lock;
1970
sn = malloc(sizeof *sn, M_UFSMNT, M_WAITOK | M_ZERO);
1971
TAILQ_INIT(&sn->sn_head);
1972
lockinit(&sn->sn_lock, PVFS, "snaplk", VLKTIMEOUT,
1973
LK_CANRECURSE | LK_NOSHARE);
1975
vp->v_vnlock = &sn->sn_lock;
1976
devvp->v_rdev->si_snapdata = sn;
1978
lockmgr(vp->v_vnlock, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY,
1980
lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
1982
* Link it onto the active snapshot list.
1985
if (ip->i_nextsnap.tqe_prev != 0)
1986
panic("ffs_snapshot_mount: %d already on list",
1989
TAILQ_INSERT_TAIL(&sn->sn_head, ip, i_nextsnap);
1990
vp->v_vflag |= VV_SYSTEM;
1992
VOP_UNLOCK(vp, 0, td);
1997
* No usable snapshots found.
2002
* Allocate the space for the block hints list. We always want to
2003
* use the list from the newest snapshot.
2005
auio.uio_iov = &aiov;
2006
auio.uio_iovcnt = 1;
2007
aiov.iov_base = (void *)&snaplistsize;
2008
aiov.iov_len = sizeof(snaplistsize);
2009
auio.uio_resid = aiov.iov_len;
2011
lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
2012
auio.uio_segflg = UIO_SYSSPACE;
2013
auio.uio_rw = UIO_READ;
2015
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
2016
if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2017
printf("ffs_snapshot_mount: read_1 failed %d\n", error);
2018
VOP_UNLOCK(vp, 0, td);
2021
MALLOC(snapblklist, void *, snaplistsize * sizeof(daddr_t),
2022
M_UFSMNT, M_WAITOK);
2023
auio.uio_iovcnt = 1;
2024
aiov.iov_base = snapblklist;
2025
aiov.iov_len = snaplistsize * sizeof (daddr_t);
2026
auio.uio_resid = aiov.iov_len;
2027
auio.uio_offset -= sizeof(snaplistsize);
2028
if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
2029
printf("ffs_snapshot_mount: read_2 failed %d\n", error);
2030
VOP_UNLOCK(vp, 0, td);
2031
FREE(snapblklist, M_UFSMNT);
2034
VOP_UNLOCK(vp, 0, td);
2036
ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
2037
sn->sn_listsize = snaplistsize;
2038
sn->sn_blklist = (daddr_t *)snapblklist;
2039
devvp->v_vflag |= VV_COPYONWRITE;
2044
* Disassociate snapshot files when unmounting.
2047
ffs_snapshot_unmount(mp)
2050
struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
2051
struct snapdata *sn;
2054
struct thread *td = curthread;
2057
sn = devvp->v_rdev->si_snapdata;
2058
while (sn != NULL && (xp = TAILQ_FIRST(&sn->sn_head)) != NULL) {
2060
TAILQ_REMOVE(&sn->sn_head, xp, i_nextsnap);
2061
xp->i_nextsnap.tqe_prev = 0;
2062
lockmgr(&sn->sn_lock,
2063
LK_INTERLOCK | LK_EXCLUSIVE,
2067
lockmgr(&vp->v_lock,
2068
LK_INTERLOCK | LK_EXCLUSIVE,
2071
KASSERT(vp->v_vnlock == &sn->sn_lock,
2072
("ffs_snapshot_unmount: lost lock mutation"));
2073
vp->v_vnlock = &vp->v_lock;
2075
lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
2076
lockmgr(&sn->sn_lock, LK_RELEASE, NULL, td);
2077
if (xp->i_effnlink > 0)
2080
sn = devvp->v_rdev->si_snapdata;
2082
try_free_snapdata(devvp, td);
2083
ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
2087
* Check the buffer block to be belong to device buffer that shall be
2088
* locked after snaplk. devvp shall be locked on entry, and will be
2089
* leaved locked upon exit.
2092
ffs_bp_snapblk(devvp, bp)
2093
struct vnode *devvp;
2096
struct snapdata *sn;
2098
ufs2_daddr_t lbn, *snapblklist;
2099
int lower, upper, mid;
2101
ASSERT_VI_LOCKED(devvp, "ffs_bp_snapblk");
2102
KASSERT(devvp->v_type == VCHR, ("Not a device %p", devvp));
2103
sn = devvp->v_rdev->si_snapdata;
2104
if (sn == NULL || TAILQ_FIRST(&sn->sn_head) == NULL)
2106
fs = TAILQ_FIRST(&sn->sn_head)->i_fs;
2107
lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2108
snapblklist = sn->sn_blklist;
2109
upper = sn->sn_listsize - 1;
2111
while (lower <= upper) {
2112
mid = (lower + upper) / 2;
2113
if (snapblklist[mid] == lbn)
2115
if (snapblklist[mid] < lbn)
2131
struct vnode *vp, *devvp;
2135
if (bo->bo_dirty.bv_cnt <= dirtybufthresh)
2140
devvp = bo->__bo_vnode;
2141
KASSERT(vp == devvp, ("devvp != vp %p %p", bo, bp));
2144
bp_bdskip = ffs_bp_snapblk(devvp, bp);
2148
if (bo->bo_dirty.bv_cnt > dirtybufthresh + 10 && !bp_bdskip) {
2149
(void) VOP_FSYNC(vp, MNT_NOWAIT, td);
2154
* Try to find a buffer to flush.
2156
TAILQ_FOREACH(nbp, &bo->bo_dirty.bv_hd, b_bobufs) {
2157
if ((nbp->b_vflags & BV_BKGRDINPROG) ||
2159
LK_EXCLUSIVE | LK_NOWAIT, NULL))
2162
panic("bdwrite: found ourselves");
2165
* Don't countdeps with the bo lock
2168
if (buf_countdeps(nbp, 0)) {
2175
if (!ffs_bp_snapblk(vp, nbp)) {
2176
if (BO_MTX(bo) != VI_MTX(vp)) {
2185
if (nbp->b_flags & B_CLUSTEROK) {
2186
vfs_bio_awrite(nbp);
2191
dirtybufferflushes++;
2200
* Check for need to copy block that is about to be written,
2201
* copying the block if necessary.
2204
ffs_copyonwrite(devvp, bp)
2205
struct vnode *devvp;
2208
struct snapdata *sn;
2209
struct buf *ibp, *cbp, *savedcbp = 0;
2210
struct thread *td = curthread;
2213
struct vnode *vp = 0;
2214
ufs2_daddr_t lbn, blkno, *snapblklist;
2215
int lower, upper, mid, indiroff, error = 0;
2216
int launched_async_io, prev_norunningbuf;
2217
long saved_runningbufspace;
2219
if (devvp != bp->b_vp && (VTOI(bp->b_vp)->i_flags & SF_SNAPSHOT) != 0)
2220
return (0); /* Update on a snapshot file */
2221
if (td->td_pflags & TDP_COWINPROGRESS)
2222
panic("ffs_copyonwrite: recursive call");
2224
* First check to see if it is in the preallocated list.
2225
* By doing this check we avoid several potential deadlocks.
2228
sn = devvp->v_rdev->si_snapdata;
2230
TAILQ_EMPTY(&sn->sn_head)) {
2232
return (0); /* No snapshot */
2234
ip = TAILQ_FIRST(&sn->sn_head);
2236
lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
2237
snapblklist = sn->sn_blklist;
2238
upper = sn->sn_listsize - 1;
2240
while (lower <= upper) {
2241
mid = (lower + upper) / 2;
2242
if (snapblklist[mid] == lbn)
2244
if (snapblklist[mid] < lbn)
2249
if (lower <= upper) {
2253
launched_async_io = 0;
2254
prev_norunningbuf = td->td_pflags & TDP_NORUNNINGBUF;
2256
* Since I/O on bp isn't yet in progress and it may be blocked
2257
* for a long time waiting on snaplk, back it out of
2258
* runningbufspace, possibly waking other threads waiting for space.
2260
saved_runningbufspace = bp->b_runningbufspace;
2261
if (saved_runningbufspace != 0)
2262
runningbufwakeup(bp);
2264
* Not in the precomputed list, so check the snapshots.
2266
while (lockmgr(&sn->sn_lock,
2267
LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
2268
VI_MTX(devvp), td) != 0) {
2270
sn = devvp->v_rdev->si_snapdata;
2272
TAILQ_EMPTY(&sn->sn_head)) {
2274
if (saved_runningbufspace != 0) {
2275
bp->b_runningbufspace = saved_runningbufspace;
2276
atomic_add_long(&runningbufspace,
2277
bp->b_runningbufspace);
2279
return (0); /* Snapshot gone */
2282
TAILQ_FOREACH(ip, &sn->sn_head, i_nextsnap) {
2285
* We ensure that everything of our own that needs to be
2286
* copied will be done at the time that ffs_snapshot is
2287
* called. Thus we can skip the check here which can
2288
* deadlock in doing the lookup in UFS_BALLOC.
2293
* Check to see if block needs to be copied. We do not have
2294
* to hold the snapshot lock while doing this lookup as it
2295
* will never require any additional allocations for the
2299
blkno = DIP(ip, i_db[lbn]);
2301
td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2302
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2303
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
2304
td->td_pflags &= ~TDP_COWINPROGRESS;
2307
indiroff = (lbn - NDADDR) % NINDIR(fs);
2308
if (ip->i_ump->um_fstype == UFS1)
2309
blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
2311
blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
2315
if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
2316
panic("ffs_copyonwrite: bad copy block");
2321
* Allocate the block into which to do the copy. Since
2322
* multiple processes may all try to copy the same block,
2323
* we have to recheck our need to do a copy if we sleep
2324
* waiting for the lock.
2326
* Because all snapshots on a filesystem share a single
2327
* lock, we ensure that we will never be in competition
2328
* with another process to allocate a block.
2330
td->td_pflags |= TDP_COWINPROGRESS | TDP_NORUNNINGBUF;
2331
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
2332
fs->fs_bsize, KERNCRED, 0, &cbp);
2333
td->td_pflags &= ~TDP_COWINPROGRESS;
2338
printf("Copyonwrite: snapino %d lbn %jd for ",
2339
ip->i_number, (intmax_t)lbn);
2340
if (bp->b_vp == devvp)
2341
printf("fs metadata");
2343
printf("inum %d", VTOI(bp->b_vp)->i_number);
2344
printf(" lblkno %jd to blkno %jd\n",
2345
(intmax_t)bp->b_lblkno, (intmax_t)cbp->b_blkno);
2349
* If we have already read the old block contents, then
2350
* simply copy them to the new block. Note that we need
2351
* to synchronously write snapshots that have not been
2352
* unlinked, and hence will be visible after a crash,
2353
* to ensure their integrity.
2355
if (savedcbp != 0) {
2356
bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
2358
if (dopersistence && ip->i_effnlink > 0)
2359
(void) ffs_syncvnode(vp, MNT_WAIT);
2361
launched_async_io = 1;
2365
* Otherwise, read the old block contents into the buffer.
2367
if ((error = readblock(vp, cbp, lbn)) != 0) {
2368
bzero(cbp->b_data, fs->fs_bsize);
2370
if (dopersistence && ip->i_effnlink > 0)
2371
(void) ffs_syncvnode(vp, MNT_WAIT);
2373
launched_async_io = 1;
2379
* Note that we need to synchronously write snapshots that
2380
* have not been unlinked, and hence will be visible after
2381
* a crash, to ensure their integrity.
2384
vp = savedcbp->b_vp;
2386
if (dopersistence && VTOI(vp)->i_effnlink > 0)
2387
(void) ffs_syncvnode(vp, MNT_WAIT);
2389
launched_async_io = 1;
2391
lockmgr(vp->v_vnlock, LK_RELEASE, NULL, td);
2392
td->td_pflags = (td->td_pflags & ~TDP_NORUNNINGBUF) |
2394
if (launched_async_io && (td->td_pflags & TDP_NORUNNINGBUF) == 0)
2395
waitrunningbufspace();
2397
* I/O on bp will now be started, so count it in runningbufspace.
2399
if (saved_runningbufspace != 0) {
2400
bp->b_runningbufspace = saved_runningbufspace;
2401
atomic_add_long(&runningbufspace, bp->b_runningbufspace);
2407
* Read the specified block into the given buffer.
2408
* Much of this boiler-plate comes from bwrite().
2411
readblock(vp, bp, lbn)
2416
struct inode *ip = VTOI(vp);
2419
bip = g_alloc_bio();
2420
bip->bio_cmd = BIO_READ;
2421
bip->bio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
2422
bip->bio_data = bp->b_data;
2423
bip->bio_length = bp->b_bcount;
2424
bip->bio_done = NULL;
2426
g_io_request(bip, ip->i_devvp->v_bufobj.bo_private);
2427
bp->b_error = biowait(bip, "snaprdb");
2429
return (bp->b_error);
2435
* Process file deletes that were deferred by ufs_inactive() due to
2436
* the file system being suspended. Transfer IN_LAZYACCESS into
2437
* IN_MODIFIED for vnodes that were accessed during suspension.
2440
process_deferred_inactive(struct mount *mp)
2442
struct vnode *vp, *mvp;
2448
(void) vn_start_secondary_write(NULL, &mp, V_WAIT);
2451
MNT_VNODE_FOREACH(vp, mp, mvp) {
2454
* IN_LAZYACCESS is checked here without holding any
2455
* vnode lock, but this flag is set only while holding
2458
if (vp->v_type == VNON || (vp->v_iflag & VI_DOOMED) != 0 ||
2459
((VTOI(vp)->i_flag & IN_LAZYACCESS) == 0 &&
2460
((vp->v_iflag & VI_OWEINACT) == 0 ||
2461
vp->v_usecount > 0))) {
2467
error = vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, td);
2471
if (error == ENOENT)
2472
continue; /* vnode recycled */
2473
MNT_VNODE_FOREACH_ABORT_ILOCKED(mp, mvp);
2477
if ((ip->i_flag & IN_LAZYACCESS) != 0) {
2478
ip->i_flag &= ~IN_LAZYACCESS;
2479
ip->i_flag |= IN_MODIFIED;
2482
if ((vp->v_iflag & VI_OWEINACT) == 0 || vp->v_usecount > 0) {
2484
VOP_UNLOCK(vp, 0, td);
2490
VNASSERT((vp->v_iflag & VI_DOINGINACT) == 0, vp,
2491
("process_deferred_inactive: "
2492
"recursed on VI_DOINGINACT"));
2493
vp->v_iflag |= VI_DOINGINACT;
2494
vp->v_iflag &= ~VI_OWEINACT;
2496
(void) VOP_INACTIVE(vp, td);
2498
VNASSERT(vp->v_iflag & VI_DOINGINACT, vp,
2499
("process_deferred_inactive: lost VI_DOINGINACT"));
2500
VNASSERT((vp->v_iflag & VI_OWEINACT) == 0, vp,
2501
("process_deferred_inactive: got VI_OWEINACT"));
2502
vp->v_iflag &= ~VI_DOINGINACT;
2504
VOP_UNLOCK(vp, 0, td);
2509
vn_finished_secondary_write(mp);
2512
#ifndef NO_FFS_SNAPSHOT
2514
/* Try to free snapdata associated with devvp */
2516
try_free_snapdata(struct vnode *devvp,
2519
struct snapdata *sn;
2520
ufs2_daddr_t *snapblklist;
2522
sn = devvp->v_rdev->si_snapdata;
2524
if (sn == NULL || TAILQ_FIRST(&sn->sn_head) != NULL ||
2525
(devvp->v_vflag & VV_COPYONWRITE) == 0) {
2530
devvp->v_rdev->si_snapdata = NULL;
2531
devvp->v_vflag &= ~VV_COPYONWRITE;
2532
snapblklist = sn->sn_blklist;
2533
sn->sn_blklist = NULL;
2534
sn->sn_listsize = 0;
2535
lockmgr(&sn->sn_lock, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp), td);
2536
lockmgr(&sn->sn_lock, LK_RELEASE, NULL, td);
2537
lockdestroy(&sn->sn_lock);
2539
if (snapblklist != NULL)
2540
FREE(snapblklist, M_UFSMNT);