2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2000 Marshall Kirk McKusick. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Further information about snapshots can be obtained from:
|
|
|
|
*
|
|
|
|
* Marshall Kirk McKusick http://www.mckusick.com/softdep/
|
|
|
|
* 1614 Oxford Street mckusick@mckusick.com
|
|
|
|
* Berkeley, CA 94709-1608 +1-510-843-9542
|
|
|
|
* USA
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY MARSHALL KIRK MCKUSICK ``AS IS'' AND ANY
|
|
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL MARSHALL KIRK MCKUSICK BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
2000-07-24 05:28:33 +00:00
|
|
|
* @(#)ffs_snapshot.c 8.11 (McKusick) 7/23/00
|
2000-07-11 22:07:57 +00:00
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2001-03-07 07:09:55 +00:00
|
|
|
#include <sys/conf.h>
|
2000-07-11 22:07:57 +00:00
|
|
|
#include <sys/bio.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/resourcevar.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
|
|
|
|
#include <ufs/ufs/extattr.h>
|
|
|
|
#include <ufs/ufs/quota.h>
|
|
|
|
#include <ufs/ufs/ufsmount.h>
|
|
|
|
#include <ufs/ufs/inode.h>
|
|
|
|
#include <ufs/ufs/ufs_extern.h>
|
|
|
|
|
|
|
|
#include <ufs/ffs/fs.h>
|
|
|
|
#include <ufs/ffs/ffs_extern.h>
|
|
|
|
|
2002-02-27 19:18:10 +00:00
|
|
|
#define KERNCRED thread0.td_ucred
|
2000-09-17 19:41:26 +00:00
|
|
|
#define DEBUG 1
|
2000-07-11 22:07:57 +00:00
|
|
|
|
2001-12-14 00:15:06 +00:00
|
|
|
static int cgaccount __P((int, struct vnode *, struct buf *, int));
|
2001-05-04 05:49:28 +00:00
|
|
|
static int expunge __P((struct vnode *, struct inode *, struct fs *,
|
|
|
|
int (*) __P((struct vnode *, ufs_daddr_t *, ufs_daddr_t *, struct fs *,
|
2002-02-02 01:42:44 +00:00
|
|
|
ufs_daddr_t, int)), int));
|
2000-07-11 22:07:57 +00:00
|
|
|
static int indiracct __P((struct vnode *, struct vnode *, int, ufs_daddr_t,
|
2001-05-04 05:49:28 +00:00
|
|
|
int, int, int, int, struct fs *, int (*) __P((struct vnode *,
|
2002-02-02 01:42:44 +00:00
|
|
|
ufs_daddr_t *, ufs_daddr_t *, struct fs *, ufs_daddr_t, int)), int));
|
|
|
|
static int fullacct __P((struct vnode *, ufs_daddr_t *, ufs_daddr_t *,
|
|
|
|
struct fs *, ufs_daddr_t, int));
|
2001-05-04 05:49:28 +00:00
|
|
|
static int snapacct __P((struct vnode *, ufs_daddr_t *, ufs_daddr_t *,
|
2002-02-02 01:42:44 +00:00
|
|
|
struct fs *, ufs_daddr_t, int));
|
2001-05-04 05:49:28 +00:00
|
|
|
static int mapacct __P((struct vnode *, ufs_daddr_t *, ufs_daddr_t *,
|
2002-02-02 01:42:44 +00:00
|
|
|
struct fs *, ufs_daddr_t, int));
|
2001-03-07 07:09:55 +00:00
|
|
|
static int ffs_copyonwrite __P((struct vnode *, struct buf *));
|
2000-07-11 22:07:57 +00:00
|
|
|
static int readblock __P((struct buf *, daddr_t));
|
|
|
|
|
2001-05-14 17:16:49 +00:00
|
|
|
/*
|
|
|
|
* To ensure the consistency of snapshots across crashes, we must
|
|
|
|
* synchronously write out copied blocks before allowing the
|
|
|
|
* originals to be modified. Because of the rather severe speed
|
|
|
|
* penalty that this imposes, the following flag allows this
|
|
|
|
* crash persistence to be disabled.
|
|
|
|
*/
|
|
|
|
int dopersistence = 0;
|
|
|
|
|
2000-07-11 22:07:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
#include <sys/sysctl.h>
|
2001-05-14 17:16:49 +00:00
|
|
|
SYSCTL_INT(_debug, OID_AUTO, dopersistence, CTLFLAG_RW, &dopersistence, 0, "");
|
2000-07-11 22:07:57 +00:00
|
|
|
int snapdebug = 0;
|
|
|
|
SYSCTL_INT(_debug, OID_AUTO, snapdebug, CTLFLAG_RW, &snapdebug, 0, "");
|
2001-12-14 00:15:06 +00:00
|
|
|
int collectsnapstats = 0;
|
|
|
|
SYSCTL_INT(_debug, OID_AUTO, collectsnapstats, CTLFLAG_RW, &collectsnapstats,
|
|
|
|
0, "");
|
2000-07-11 22:07:57 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a snapshot file and initialize it for the filesystem.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ffs_snapshot(mp, snapfile)
|
|
|
|
struct mount *mp;
|
|
|
|
char *snapfile;
|
|
|
|
{
|
2002-02-02 01:42:44 +00:00
|
|
|
ufs_daddr_t blkno;
|
2001-12-14 00:15:06 +00:00
|
|
|
int error, cg, snaploc, numblks;
|
2002-02-02 01:42:44 +00:00
|
|
|
int i, size, len, loc;
|
2001-05-04 05:49:28 +00:00
|
|
|
int flag = mp->mnt_flag;
|
2001-12-14 00:15:06 +00:00
|
|
|
struct timespec starttime = {0, 0}, endtime;
|
|
|
|
char saved_nice = 0;
|
|
|
|
long redo = 0;
|
2001-05-04 05:49:28 +00:00
|
|
|
int32_t *lp;
|
2001-01-15 18:30:40 +00:00
|
|
|
void *space;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *copy_fs = NULL, *fs = VFSTOUFS(mp)->um_fs;
|
2001-03-07 07:09:55 +00:00
|
|
|
struct snaphead *snaphead;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread;
|
2001-03-07 07:09:55 +00:00
|
|
|
struct inode *ip, *xp;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct buf *bp, *nbp, *ibp, *sbp = NULL;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct nameidata nd;
|
|
|
|
struct mount *wrtmp;
|
|
|
|
struct vattr vat;
|
2002-02-02 01:42:44 +00:00
|
|
|
struct vnode *vp, *xvp, *nvp;
|
2000-07-11 22:07:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Need to serialize access to snapshot code per filesystem.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* Assign a snapshot slot in the superblock.
|
|
|
|
*/
|
|
|
|
for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
|
|
|
|
if (fs->fs_snapinum[snaploc] == 0)
|
|
|
|
break;
|
|
|
|
if (snaploc == FSMAXSNAP)
|
|
|
|
return (ENOSPC);
|
|
|
|
/*
|
|
|
|
* Create the snapshot file.
|
|
|
|
*/
|
|
|
|
restart:
|
2001-09-12 08:38:13 +00:00
|
|
|
NDINIT(&nd, CREATE, LOCKPARENT | LOCKLEAF, UIO_USERSPACE, snapfile, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
if ((error = namei(&nd)) != 0)
|
|
|
|
return (error);
|
|
|
|
if (nd.ni_vp != NULL) {
|
|
|
|
vput(nd.ni_vp);
|
|
|
|
error = EEXIST;
|
|
|
|
}
|
|
|
|
if (nd.ni_dvp->v_mount != mp)
|
|
|
|
error = EXDEV;
|
|
|
|
if (error) {
|
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
|
|
|
if (nd.ni_dvp == nd.ni_vp)
|
|
|
|
vrele(nd.ni_dvp);
|
|
|
|
else
|
|
|
|
vput(nd.ni_dvp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
VATTR_NULL(&vat);
|
|
|
|
vat.va_type = VREG;
|
|
|
|
vat.va_mode = S_IRUSR;
|
|
|
|
vat.va_vaflags |= VA_EXCLUSIVE;
|
|
|
|
if (VOP_GETWRITEMOUNT(nd.ni_dvp, &wrtmp))
|
|
|
|
wrtmp = NULL;
|
|
|
|
if (wrtmp != mp)
|
|
|
|
panic("ffs_snapshot: mount mismatch");
|
2000-07-12 00:27:27 +00:00
|
|
|
if (vn_start_write(NULL, &wrtmp, V_NOWAIT) != 0) {
|
2000-07-11 22:07:57 +00:00
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
|
|
|
vput(nd.ni_dvp);
|
2000-07-12 00:27:27 +00:00
|
|
|
if ((error = vn_start_write(NULL, &wrtmp,
|
|
|
|
V_XSLEEP | PCATCH)) != 0)
|
2000-07-11 22:07:57 +00:00
|
|
|
return (error);
|
|
|
|
goto restart;
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_LEASE(nd.ni_dvp, td, KERNCRED, LEASE_WRITE);
|
2000-07-11 22:07:57 +00:00
|
|
|
error = VOP_CREATE(nd.ni_dvp, &nd.ni_vp, &nd.ni_cnd, &vat);
|
|
|
|
vput(nd.ni_dvp);
|
|
|
|
if (error) {
|
|
|
|
NDFREE(&nd, NDF_ONLY_PNBUF);
|
|
|
|
vn_finished_write(wrtmp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
vp = nd.ni_vp;
|
|
|
|
ip = VTOI(vp);
|
|
|
|
/*
|
|
|
|
* Allocate and copy the last block contents so as to be able
|
|
|
|
* to set size to that of the filesystem.
|
|
|
|
*/
|
|
|
|
numblks = howmany(fs->fs_size, fs->fs_frag);
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(numblks - 1)),
|
2000-07-11 22:07:57 +00:00
|
|
|
fs->fs_bsize, KERNCRED, B_CLRBUF, &bp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
ip->i_size = lblktosize(fs, (off_t)numblks);
|
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
if ((error = readblock(bp, numblks - 1)) != 0)
|
|
|
|
goto out;
|
|
|
|
bawrite(bp);
|
|
|
|
/*
|
|
|
|
* Preallocate critical data structures so that we can copy
|
|
|
|
* them in without further allocation after we suspend all
|
|
|
|
* operations on the filesystem. We would like to just release
|
|
|
|
* the allocated buffers without writing them since they will
|
|
|
|
* be filled in below once we are ready to go, but this upsets
|
|
|
|
* the soft update code, so we go ahead and write the new buffers.
|
|
|
|
*
|
2001-04-26 00:50:53 +00:00
|
|
|
* Allocate all indirect blocks and mark all of them as not
|
|
|
|
* needing to be copied.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
|
|
|
for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
|
2002-02-27 18:32:23 +00:00
|
|
|
fs->fs_bsize, td->td_ucred, B_METAONLY, &ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
bdwrite(ibp);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Allocate copies for the superblock and its summary information.
|
|
|
|
*/
|
2001-05-08 07:29:03 +00:00
|
|
|
error = UFS_BALLOC(vp, (off_t)(SBOFF), SBSIZE, KERNCRED, 0, &nbp);
|
2001-05-04 05:49:28 +00:00
|
|
|
if (error)
|
2000-07-11 22:07:57 +00:00
|
|
|
goto out;
|
|
|
|
bawrite(nbp);
|
|
|
|
blkno = fragstoblks(fs, fs->fs_csaddr);
|
|
|
|
len = howmany(fs->fs_cssize, fs->fs_bsize);
|
|
|
|
for (loc = 0; loc < len; loc++) {
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(blkno + loc)),
|
2000-07-11 22:07:57 +00:00
|
|
|
fs->fs_bsize, KERNCRED, 0, &nbp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
bawrite(nbp);
|
|
|
|
}
|
2001-12-14 00:15:06 +00:00
|
|
|
/*
|
|
|
|
* Allocate all cylinder group blocks.
|
|
|
|
*/
|
|
|
|
for (cg = 0; cg < fs->fs_ncg; cg++) {
|
|
|
|
error = UFS_BALLOC(vp, (off_t)(cgtod(fs, cg)) << fs->fs_fshift,
|
|
|
|
fs->fs_bsize, KERNCRED, 0, &nbp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
bdwrite(nbp);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Copy all the cylinder group maps. Although the
|
|
|
|
* filesystem is still active, we hope that only a few
|
|
|
|
* cylinder groups will change between now and when we
|
|
|
|
* suspend operations. Thus, we will be able to quickly
|
|
|
|
* touch up the few cylinder groups that changed during
|
|
|
|
* the suspension period.
|
|
|
|
*/
|
2002-01-17 08:33:32 +00:00
|
|
|
len = howmany(fs->fs_ncg, NBBY);
|
2001-12-18 18:05:17 +00:00
|
|
|
MALLOC(fs->fs_active, int *, len, M_DEVBUF, M_WAITOK);
|
2001-12-14 00:15:06 +00:00
|
|
|
bzero(fs->fs_active, len);
|
|
|
|
for (cg = 0; cg < fs->fs_ncg; cg++) {
|
|
|
|
error = bread(vp, fragstoblks(fs, cgtod(fs, cg)), fs->fs_bsize,
|
|
|
|
KERNCRED, &nbp);
|
2002-02-02 01:42:44 +00:00
|
|
|
if (error) {
|
|
|
|
brelse(nbp);
|
2001-12-14 00:15:06 +00:00
|
|
|
goto out;
|
2002-02-02 01:42:44 +00:00
|
|
|
}
|
2001-12-14 00:15:06 +00:00
|
|
|
error = cgaccount(cg, vp, nbp, 1);
|
|
|
|
bawrite(nbp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Change inode to snapshot type file.
|
|
|
|
*/
|
2000-07-26 23:07:01 +00:00
|
|
|
ip->i_flags |= SF_SNAPSHOT;
|
2000-07-11 22:07:57 +00:00
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
/*
|
|
|
|
* Ensure that the snapshot is completely on disk.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
if ((error = VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td)) != 0)
|
2000-07-11 22:07:57 +00:00
|
|
|
goto out;
|
|
|
|
/*
|
|
|
|
* All allocations are done, so we can now snapshot the system.
|
|
|
|
*
|
2001-12-14 00:15:06 +00:00
|
|
|
* Recind nice scheduling while running with the filesystem suspended.
|
|
|
|
*/
|
|
|
|
if (td->td_ksegrp->kg_nice > 0) {
|
|
|
|
saved_nice = td->td_ksegrp->kg_nice;
|
|
|
|
td->td_ksegrp->kg_nice = 0;
|
|
|
|
}
|
|
|
|
/*
|
2000-07-11 22:07:57 +00:00
|
|
|
* Suspend operation on filesystem.
|
|
|
|
*/
|
|
|
|
for (;;) {
|
|
|
|
vn_finished_write(wrtmp);
|
|
|
|
vfs_write_suspend(vp->v_mount);
|
|
|
|
if (mp->mnt_kern_flag & MNTK_SUSPENDED)
|
|
|
|
break;
|
2000-07-12 00:27:27 +00:00
|
|
|
vn_start_write(NULL, &wrtmp, V_WAIT);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
if (collectsnapstats)
|
|
|
|
nanotime(&starttime);
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
2001-12-14 00:15:06 +00:00
|
|
|
* First, copy all the cylinder group maps that have changed.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
|
|
|
for (cg = 0; cg < fs->fs_ncg; cg++) {
|
2001-12-18 18:05:17 +00:00
|
|
|
if ((ACTIVECGNUM(fs, cg) & ACTIVECGOFF(cg)) != 0)
|
2001-12-14 00:15:06 +00:00
|
|
|
continue;
|
|
|
|
redo++;
|
2000-07-11 22:07:57 +00:00
|
|
|
error = bread(vp, fragstoblks(fs, cgtod(fs, cg)), fs->fs_bsize,
|
|
|
|
KERNCRED, &nbp);
|
2002-02-02 01:42:44 +00:00
|
|
|
if (error) {
|
|
|
|
brelse(nbp);
|
2000-07-11 22:07:57 +00:00
|
|
|
goto out1;
|
2002-02-02 01:42:44 +00:00
|
|
|
}
|
2001-12-14 00:15:06 +00:00
|
|
|
error = cgaccount(cg, vp, nbp, 2);
|
2002-01-17 08:33:32 +00:00
|
|
|
bawrite(nbp);
|
2001-12-14 00:15:06 +00:00
|
|
|
if (error)
|
2000-07-11 22:07:57 +00:00
|
|
|
goto out1;
|
|
|
|
}
|
2001-05-04 05:49:28 +00:00
|
|
|
/*
|
|
|
|
* Grab a copy of the superblock and its summary information.
|
|
|
|
* We delay writing it until the suspension is released below.
|
|
|
|
*/
|
|
|
|
error = bread(vp, lblkno(fs, SBOFF), fs->fs_bsize, KERNCRED, &sbp);
|
2002-02-02 01:42:44 +00:00
|
|
|
if (error) {
|
|
|
|
brelse(sbp);
|
|
|
|
sbp = NULL;
|
2001-05-04 05:49:28 +00:00
|
|
|
goto out1;
|
2002-02-02 01:42:44 +00:00
|
|
|
}
|
2001-05-04 05:49:28 +00:00
|
|
|
copy_fs = (struct fs *)(sbp->b_data + blkoff(fs, SBOFF));
|
|
|
|
bcopy(fs, copy_fs, fs->fs_sbsize);
|
|
|
|
if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
|
|
|
|
copy_fs->fs_clean = 1;
|
|
|
|
if (fs->fs_sbsize < SBSIZE)
|
|
|
|
bzero(&sbp->b_data[blkoff(fs, SBOFF) + fs->fs_sbsize],
|
|
|
|
SBSIZE - fs->fs_sbsize);
|
|
|
|
size = blkroundup(fs, fs->fs_cssize);
|
|
|
|
if (fs->fs_contigsumsize > 0)
|
|
|
|
size += fs->fs_ncg * sizeof(int32_t);
|
|
|
|
space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
|
|
|
|
copy_fs->fs_csp = space;
|
|
|
|
bcopy(fs->fs_csp, copy_fs->fs_csp, fs->fs_cssize);
|
|
|
|
(char *)space += fs->fs_cssize;
|
|
|
|
loc = howmany(fs->fs_cssize, fs->fs_fsize);
|
2001-05-08 07:29:03 +00:00
|
|
|
i = fs->fs_frag - loc % fs->fs_frag;
|
|
|
|
len = (i == fs->fs_frag) ? 0 : i * fs->fs_fsize;
|
|
|
|
if (len > 0) {
|
2001-05-04 05:49:28 +00:00
|
|
|
if ((error = bread(ip->i_devvp,
|
|
|
|
fsbtodb(fs, fs->fs_csaddr + loc),
|
|
|
|
len, KERNCRED, &bp)) != 0) {
|
2002-02-02 01:42:44 +00:00
|
|
|
brelse(bp);
|
2001-05-04 05:49:28 +00:00
|
|
|
free(copy_fs->fs_csp, M_UFSMNT);
|
2002-02-02 01:42:44 +00:00
|
|
|
bawrite(sbp);
|
|
|
|
sbp = NULL;
|
2000-07-11 22:07:57 +00:00
|
|
|
goto out1;
|
2001-05-04 05:49:28 +00:00
|
|
|
}
|
|
|
|
bcopy(bp->b_data, space, (u_int)len);
|
|
|
|
(char *)space += len;
|
|
|
|
bp->b_flags |= B_INVAL | B_NOCACHE;
|
|
|
|
brelse(bp);
|
|
|
|
}
|
|
|
|
if (fs->fs_contigsumsize > 0) {
|
|
|
|
copy_fs->fs_maxcluster = lp = space;
|
|
|
|
for (i = 0; i < fs->fs_ncg; i++)
|
|
|
|
*lp++ = fs->fs_contigsumsize;
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
/*
|
|
|
|
* We must check for active files that have been unlinked
|
|
|
|
* (e.g., with a zero link count). We have to expunge all
|
|
|
|
* trace of these files from the snapshot so that they are
|
|
|
|
* not reclaimed prematurely by fsck or unnecessarily dumped.
|
|
|
|
* We turn off the MNTK_SUSPENDED flag to avoid a panic from
|
|
|
|
* spec_strategy about writing on a suspended filesystem.
|
|
|
|
*/
|
|
|
|
mp->mnt_kern_flag &= ~MNTK_SUSPENDED;
|
|
|
|
mtx_lock(&mntvnode_mtx);
|
|
|
|
loop:
|
|
|
|
for (xvp = TAILQ_FIRST(&mp->mnt_nvnodelist); xvp; xvp = nvp) {
|
|
|
|
/*
|
|
|
|
* Make sure this vnode wasn't reclaimed in getnewvnode().
|
|
|
|
* Start over if it has (it won't be on the list anymore).
|
|
|
|
*/
|
|
|
|
if (xvp->v_mount != mp)
|
|
|
|
goto loop;
|
|
|
|
nvp = TAILQ_NEXT(xvp, v_nmntvnodes);
|
|
|
|
mtx_unlock(&mntvnode_mtx);
|
|
|
|
mtx_lock(&xvp->v_interlock);
|
|
|
|
if (xvp->v_usecount == 0 || xvp->v_type == VNON ||
|
|
|
|
(VOP_GETATTR(xvp, &vat, td->td_proc->p_ucred, td) == 0 &&
|
|
|
|
vat.va_nlink > 0)) {
|
|
|
|
mtx_unlock(&xvp->v_interlock);
|
|
|
|
mtx_lock(&mntvnode_mtx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (snapdebug)
|
|
|
|
vprint("ffs_snapshot: busy vnode", xvp);
|
|
|
|
if (vn_lock(xvp, LK_EXCLUSIVE | LK_INTERLOCK, td) != 0)
|
|
|
|
goto loop;
|
|
|
|
xp = VTOI(xvp);
|
|
|
|
/*
|
|
|
|
* If there is a fragment, clear it here.
|
|
|
|
*/
|
|
|
|
blkno = 0;
|
|
|
|
loc = howmany(xp->i_size, fs->fs_bsize) - 1;
|
|
|
|
if (loc < NDADDR) {
|
|
|
|
len = fragroundup(fs, blkoff(fs, xp->i_size));
|
|
|
|
if (len < fs->fs_bsize) {
|
|
|
|
ffs_blkfree(copy_fs, vp, xp->i_db[loc], len,
|
|
|
|
xp->i_number);
|
|
|
|
blkno = xp->i_db[loc];
|
|
|
|
xp->i_db[loc] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error = expunge(vp, xp, copy_fs, fullacct, BLK_NOCOPY);
|
|
|
|
if (blkno)
|
|
|
|
xp->i_db[loc] = blkno;
|
|
|
|
if (!error)
|
|
|
|
error = ffs_freefile(copy_fs, vp, xp->i_number,
|
|
|
|
xp->i_mode);
|
|
|
|
VOP_UNLOCK(xvp, 0, td);
|
|
|
|
if (error) {
|
|
|
|
free(copy_fs->fs_csp, M_UFSMNT);
|
|
|
|
bawrite(sbp);
|
|
|
|
sbp = NULL;
|
|
|
|
goto out1;
|
|
|
|
}
|
|
|
|
mtx_lock(&mntvnode_mtx);
|
|
|
|
}
|
|
|
|
mtx_unlock(&mntvnode_mtx);
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Record snapshot inode. Since this is the newest snapshot,
|
|
|
|
* it must be placed at the end of the list.
|
|
|
|
*/
|
|
|
|
fs->fs_snapinum[snaploc] = ip->i_number;
|
2001-03-07 07:09:55 +00:00
|
|
|
if (ip->i_nextsnap.tqe_prev != 0)
|
2000-07-11 22:07:57 +00:00
|
|
|
panic("ffs_snapshot: %d already on list", ip->i_number);
|
2001-12-14 00:15:06 +00:00
|
|
|
snaphead = &ip->i_devvp->v_rdev->si_snapshots;
|
2001-03-07 07:09:55 +00:00
|
|
|
TAILQ_INSERT_TAIL(snaphead, ip, i_nextsnap);
|
|
|
|
ip->i_devvp->v_rdev->si_copyonwrite = ffs_copyonwrite;
|
|
|
|
ip->i_devvp->v_flag |= VCOPYONWRITE;
|
2000-07-11 22:07:57 +00:00
|
|
|
vp->v_flag |= VSYSTEM;
|
2001-12-14 00:15:06 +00:00
|
|
|
out1:
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Resume operation on filesystem.
|
|
|
|
*/
|
|
|
|
vfs_write_resume(vp->v_mount);
|
2001-12-14 00:15:06 +00:00
|
|
|
if (saved_nice > 0)
|
|
|
|
td->td_ksegrp->kg_nice = saved_nice;
|
2000-07-12 00:27:27 +00:00
|
|
|
vn_start_write(NULL, &wrtmp, V_WAIT);
|
2001-12-14 00:15:06 +00:00
|
|
|
if (collectsnapstats && starttime.tv_sec > 0) {
|
|
|
|
nanotime(&endtime);
|
|
|
|
timespecsub(&endtime, &starttime);
|
|
|
|
printf("%s: suspended %d.%03ld sec, redo %ld of %d\n",
|
|
|
|
vp->v_mount->mnt_stat.f_mntonname, endtime.tv_sec,
|
|
|
|
endtime.tv_nsec / 1000000, redo, fs->fs_ncg);
|
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
if (sbp == NULL)
|
|
|
|
goto out;
|
|
|
|
/*
|
|
|
|
* Copy allocation information from all the snapshots in
|
|
|
|
* this snapshot and then expunge them from its view.
|
|
|
|
*/
|
|
|
|
snaphead = &ip->i_devvp->v_rdev->si_snapshots;
|
|
|
|
TAILQ_FOREACH(xp, snaphead, i_nextsnap) {
|
|
|
|
if (xp == ip)
|
|
|
|
break;
|
|
|
|
if ((error = expunge(vp, xp, fs, snapacct, BLK_SNAP)) != 0) {
|
|
|
|
fs->fs_snapinum[snaploc] = 0;
|
|
|
|
goto done;
|
2001-05-04 05:49:28 +00:00
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Expunge the blocks used by the snapshots from the set of
|
|
|
|
* blocks marked as used in the snapshot bitmaps.
|
|
|
|
*/
|
|
|
|
if ((error = expunge(vp, ip, copy_fs, mapacct, BLK_SNAP)) != 0) {
|
|
|
|
fs->fs_snapinum[snaploc] = 0;
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Write the superblock and its summary information
|
|
|
|
* to the snapshot.
|
|
|
|
*/
|
|
|
|
blkno = fragstoblks(fs, fs->fs_csaddr);
|
|
|
|
len = howmany(fs->fs_cssize, fs->fs_bsize);
|
|
|
|
space = copy_fs->fs_csp;
|
|
|
|
for (loc = 0; loc < len; loc++) {
|
|
|
|
error = bread(vp, blkno + loc, fs->fs_bsize, KERNCRED, &nbp);
|
|
|
|
if (error) {
|
|
|
|
brelse(nbp);
|
|
|
|
fs->fs_snapinum[snaploc] = 0;
|
|
|
|
goto done;
|
2001-05-04 05:49:28 +00:00
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
bcopy(space, nbp->b_data, fs->fs_bsize);
|
|
|
|
space = (char *)space + fs->fs_bsize;
|
|
|
|
bawrite(nbp);
|
2001-05-04 05:49:28 +00:00
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
done:
|
|
|
|
free(copy_fs->fs_csp, M_UFSMNT);
|
|
|
|
bawrite(sbp);
|
2000-07-11 22:07:57 +00:00
|
|
|
out:
|
2001-12-14 00:15:06 +00:00
|
|
|
if (fs->fs_active != 0) {
|
|
|
|
FREE(fs->fs_active, M_DEVBUF);
|
|
|
|
fs->fs_active = 0;
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
mp->mnt_flag = flag;
|
2001-05-04 05:49:28 +00:00
|
|
|
if (error)
|
2001-09-12 08:38:13 +00:00
|
|
|
(void) UFS_TRUNCATE(vp, (off_t)0, 0, NOCRED, td);
|
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
vput(vp);
|
|
|
|
else
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
vn_finished_write(wrtmp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2001-12-14 00:15:06 +00:00
|
|
|
/*
|
|
|
|
* Copy a cylinder group map. All the unallocated blocks are marked
|
|
|
|
* BLK_NOCOPY so that the snapshot knows that it need not copy them
|
2002-03-15 18:49:47 +00:00
|
|
|
* if they are later written. If passno is one, then this is a first
|
|
|
|
* pass, so only setting needs to be done. If passno is 2, then this
|
2001-12-14 00:15:06 +00:00
|
|
|
* is a revision to a previous pass which must be undone as the
|
|
|
|
* replacement pass is done.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
cgaccount(cg, vp, nbp, passno)
|
|
|
|
int cg;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct buf *nbp;
|
|
|
|
int passno;
|
|
|
|
{
|
|
|
|
struct buf *bp, *ibp;
|
|
|
|
struct inode *ip;
|
|
|
|
struct cg *cgp;
|
|
|
|
struct fs *fs;
|
|
|
|
int error, numblks, base, len, loc, indiroff;
|
|
|
|
|
|
|
|
ip = VTOI(vp);
|
|
|
|
fs = ip->i_fs;
|
|
|
|
error = bread(ip->i_devvp, fsbtodb(fs, cgtod(fs, cg)),
|
|
|
|
(int)fs->fs_cgsize, KERNCRED, &bp);
|
|
|
|
if (error) {
|
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
cgp = (struct cg *)bp->b_data;
|
|
|
|
if (!cg_chkmagic(cgp)) {
|
|
|
|
brelse(bp);
|
|
|
|
return (EIO);
|
|
|
|
}
|
2001-12-18 18:05:17 +00:00
|
|
|
atomic_set_int(&ACTIVECGNUM(fs, cg), ACTIVECGOFF(cg));
|
2001-12-14 00:15:06 +00:00
|
|
|
bcopy(bp->b_data, nbp->b_data, fs->fs_cgsize);
|
|
|
|
if (fs->fs_cgsize < fs->fs_bsize)
|
|
|
|
bzero(&nbp->b_data[fs->fs_cgsize],
|
|
|
|
fs->fs_bsize - fs->fs_cgsize);
|
|
|
|
if (passno == 2)
|
|
|
|
nbp->b_flags |= B_VALIDSUSPWRT;
|
|
|
|
numblks = howmany(fs->fs_size, fs->fs_frag);
|
|
|
|
len = howmany(fs->fs_fpg, fs->fs_frag);
|
|
|
|
base = cg * fs->fs_fpg / fs->fs_frag;
|
|
|
|
if (base + len >= numblks)
|
|
|
|
len = numblks - base - 1;
|
|
|
|
loc = 0;
|
|
|
|
if (base < NDADDR) {
|
|
|
|
for ( ; loc < NDADDR; loc++) {
|
|
|
|
if (ffs_isblock(fs, cg_blksfree(cgp), loc))
|
|
|
|
ip->i_db[loc] = BLK_NOCOPY;
|
|
|
|
else if (passno == 2 && ip->i_db[loc] == BLK_NOCOPY)
|
|
|
|
ip->i_db[loc] = 0;
|
|
|
|
else if (passno == 1 && ip->i_db[loc] == BLK_NOCOPY)
|
|
|
|
panic("ffs_snapshot: lost direct block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
|
|
|
|
fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
|
|
|
|
if (error) {
|
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
indiroff = (base + loc - NDADDR) % NINDIR(fs);
|
|
|
|
for ( ; loc < len; loc++, indiroff++) {
|
|
|
|
if (indiroff >= NINDIR(fs)) {
|
|
|
|
if (passno == 2)
|
|
|
|
ibp->b_flags |= B_VALIDSUSPWRT;
|
|
|
|
bawrite(ibp);
|
|
|
|
error = UFS_BALLOC(vp,
|
|
|
|
lblktosize(fs, (off_t)(base + loc)),
|
|
|
|
fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
|
|
|
|
if (error) {
|
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
indiroff = 0;
|
|
|
|
}
|
|
|
|
if (ffs_isblock(fs, cg_blksfree(cgp), loc))
|
|
|
|
((ufs_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
|
|
|
|
else if (passno == 2 &&
|
|
|
|
((ufs_daddr_t *)(ibp->b_data))[indiroff] == BLK_NOCOPY)
|
|
|
|
((ufs_daddr_t *)(ibp->b_data))[indiroff] = 0;
|
|
|
|
else if (passno == 1 &&
|
|
|
|
((ufs_daddr_t *)(ibp->b_data))[indiroff] == BLK_NOCOPY)
|
|
|
|
panic("ffs_snapshot: lost indirect block");
|
|
|
|
}
|
|
|
|
bqrelse(bp);
|
|
|
|
if (passno == 2)
|
|
|
|
ibp->b_flags |= B_VALIDSUSPWRT;
|
|
|
|
bdwrite(ibp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-05-04 05:49:28 +00:00
|
|
|
/*
|
|
|
|
* Before expunging a snapshot inode, note all the
|
|
|
|
* blocks that it claims with BLK_SNAP so that fsck will
|
|
|
|
* be able to account for those blocks properly and so
|
|
|
|
* that this snapshot knows that it need not copy them
|
|
|
|
* if the other snapshot holding them is freed.
|
|
|
|
*/
|
|
|
|
static int
|
2002-02-02 01:42:44 +00:00
|
|
|
expunge(snapvp, cancelip, fs, acctfunc, expungetype)
|
|
|
|
struct vnode *snapvp;
|
|
|
|
struct inode *cancelip;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *fs;
|
|
|
|
int (*acctfunc) __P((struct vnode *, ufs_daddr_t *, ufs_daddr_t *,
|
2002-02-02 01:42:44 +00:00
|
|
|
struct fs *, ufs_daddr_t, int));
|
|
|
|
int expungetype;
|
2001-05-04 05:49:28 +00:00
|
|
|
{
|
|
|
|
int i, len, error, numblks, blksperindir;
|
2002-02-02 01:42:44 +00:00
|
|
|
ufs_daddr_t lbn, rlbn, blkno, indiroff;
|
|
|
|
struct thread *td = curthread;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct dinode *dip;
|
|
|
|
struct buf *bp;
|
|
|
|
|
2002-02-02 01:42:44 +00:00
|
|
|
numblks = howmany(cancelip->i_size, fs->fs_bsize);
|
|
|
|
if ((error = (*acctfunc)(snapvp, &cancelip->i_db[0],
|
|
|
|
&cancelip->i_ib[NIADDR], fs, 0, expungetype)))
|
2001-05-04 05:49:28 +00:00
|
|
|
return (error);
|
|
|
|
blksperindir = 1;
|
|
|
|
lbn = -NDADDR;
|
|
|
|
len = numblks - NDADDR;
|
|
|
|
rlbn = NDADDR;
|
|
|
|
for (i = 0; len > 0 && i < NIADDR; i++) {
|
2002-02-02 01:42:44 +00:00
|
|
|
error = indiracct(snapvp, ITOV(cancelip), i, cancelip->i_ib[i],
|
|
|
|
lbn, rlbn, len, blksperindir, fs, acctfunc, expungetype);
|
2001-05-04 05:49:28 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
blksperindir *= NINDIR(fs);
|
|
|
|
lbn -= blksperindir + 1;
|
|
|
|
len -= blksperindir;
|
|
|
|
rlbn += blksperindir;
|
|
|
|
}
|
|
|
|
/*
|
2002-02-02 01:42:44 +00:00
|
|
|
* Prepare to expunge the inode. If its inode block has not
|
|
|
|
* yet been copied, then allocate and fill the copy.
|
2001-05-04 05:49:28 +00:00
|
|
|
*/
|
2002-02-02 01:42:44 +00:00
|
|
|
lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
|
|
|
|
blkno = 0;
|
|
|
|
if (lbn < NDADDR) {
|
|
|
|
blkno = cancelip->i_db[lbn];
|
|
|
|
} else {
|
|
|
|
td->td_proc->p_flag |= P_COWINPROGRESS;
|
|
|
|
error = UFS_BALLOC(snapvp, lblktosize(fs, (off_t)lbn),
|
|
|
|
fs->fs_bsize, KERNCRED, B_METAONLY, &bp);
|
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
indiroff = (lbn - NDADDR) % NINDIR(fs);
|
|
|
|
blkno = ((ufs_daddr_t *)(bp->b_data))[indiroff];
|
|
|
|
bqrelse(bp);
|
|
|
|
}
|
|
|
|
error = UFS_BALLOC(snapvp, lblktosize(fs, (off_t)lbn),
|
|
|
|
fs->fs_bsize, KERNCRED, 0, &bp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if (blkno == 0 && (error = readblock(bp, lbn)))
|
2001-05-04 05:49:28 +00:00
|
|
|
return (error);
|
2002-02-02 01:42:44 +00:00
|
|
|
/*
|
|
|
|
* Set a snapshot inode to be a zero length file, regular files
|
|
|
|
* to be completely unallocated.
|
|
|
|
*/
|
|
|
|
dip = (struct dinode *)bp->b_data + ino_to_fsbo(fs, cancelip->i_number);
|
|
|
|
if (expungetype == BLK_NOCOPY)
|
|
|
|
dip->di_mode = 0;
|
2001-05-04 05:49:28 +00:00
|
|
|
dip->di_size = 0;
|
|
|
|
dip->di_blocks = 0;
|
|
|
|
dip->di_flags &= ~SF_SNAPSHOT;
|
|
|
|
bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs_daddr_t));
|
|
|
|
bdwrite(bp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Descend an indirect block chain for vnode cancelvp accounting for all
|
|
|
|
* its indirect blocks in snapvp.
|
|
|
|
*/
|
|
|
|
static int
|
2001-05-04 05:49:28 +00:00
|
|
|
indiracct(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks, blksperindir, fs,
|
2002-02-02 01:42:44 +00:00
|
|
|
acctfunc, expungetype)
|
2000-07-11 22:07:57 +00:00
|
|
|
struct vnode *snapvp;
|
|
|
|
struct vnode *cancelvp;
|
|
|
|
int level;
|
|
|
|
ufs_daddr_t blkno;
|
|
|
|
int lbn;
|
|
|
|
int rlbn;
|
|
|
|
int remblks;
|
|
|
|
int blksperindir;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *fs;
|
|
|
|
int (*acctfunc) __P((struct vnode *, ufs_daddr_t *, ufs_daddr_t *,
|
2002-02-02 01:42:44 +00:00
|
|
|
struct fs *, ufs_daddr_t, int));
|
|
|
|
int expungetype;
|
2000-07-11 22:07:57 +00:00
|
|
|
{
|
|
|
|
int subblksperindir, error, last, num, i;
|
|
|
|
struct indir indirs[NIADDR + 2];
|
|
|
|
ufs_daddr_t *bap;
|
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
if ((error = ufs_getlbns(cancelvp, rlbn, indirs, &num)) != 0)
|
|
|
|
return (error);
|
|
|
|
if (lbn != indirs[num - 1 - level].in_lbn || blkno == 0 || num < 2)
|
|
|
|
panic("indiracct: botched params");
|
|
|
|
/*
|
|
|
|
* We have to expand bread here since it will deadlock looking
|
|
|
|
* up the block number for any blocks that are not in the cache.
|
|
|
|
*/
|
|
|
|
bp = getblk(cancelvp, lbn, fs->fs_bsize, 0, 0);
|
|
|
|
bp->b_blkno = fsbtodb(fs, blkno);
|
|
|
|
if ((bp->b_flags & (B_DONE | B_DELWRI)) == 0 &&
|
|
|
|
(error = readblock(bp, fragstoblks(fs, blkno)))) {
|
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Account for the block pointers in this indirect block.
|
|
|
|
*/
|
|
|
|
last = howmany(remblks, blksperindir);
|
|
|
|
if (last > NINDIR(fs))
|
|
|
|
last = NINDIR(fs);
|
2001-05-04 05:49:28 +00:00
|
|
|
MALLOC(bap, ufs_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
|
|
|
|
bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
|
|
|
|
bqrelse(bp);
|
2002-02-02 01:42:44 +00:00
|
|
|
error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs, rlbn, expungetype);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error || level == 0)
|
|
|
|
goto out;
|
|
|
|
/*
|
|
|
|
* Account for the block pointers in each of the indirect blocks
|
|
|
|
* in the levels below us.
|
|
|
|
*/
|
|
|
|
subblksperindir = blksperindir / NINDIR(fs);
|
|
|
|
for (lbn++, level--, i = 0; i < last; i++) {
|
|
|
|
error = indiracct(snapvp, cancelvp, level, bap[i], lbn,
|
2002-02-02 01:42:44 +00:00
|
|
|
rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
rlbn += blksperindir;
|
|
|
|
lbn -= blksperindir;
|
|
|
|
remblks -= blksperindir;
|
|
|
|
}
|
|
|
|
out:
|
2001-05-04 05:49:28 +00:00
|
|
|
FREE(bap, M_DEVBUF);
|
2000-07-11 22:07:57 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2002-02-02 01:42:44 +00:00
|
|
|
/*
|
|
|
|
* Do both snap accounting and map accounting.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fullacct(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
|
|
|
|
struct vnode *vp;
|
|
|
|
ufs_daddr_t *oldblkp, *lastblkp;
|
|
|
|
struct fs *fs;
|
|
|
|
ufs_daddr_t lblkno;
|
|
|
|
int expungetype; /* BLK_SNAP or BLK_NOCOPY */
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if ((error = snapacct(vp, oldblkp, lastblkp, fs, lblkno, expungetype)))
|
|
|
|
return (error);
|
|
|
|
return (mapacct(vp, oldblkp, lastblkp, fs, lblkno, expungetype));
|
|
|
|
}
|
|
|
|
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
2001-12-14 00:15:06 +00:00
|
|
|
* Identify a set of blocks allocated in a snapshot inode.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
|
|
|
static int
|
2002-02-02 01:42:44 +00:00
|
|
|
snapacct(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
|
2000-07-11 22:07:57 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
ufs_daddr_t *oldblkp, *lastblkp;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *fs;
|
|
|
|
ufs_daddr_t lblkno;
|
2002-02-02 01:42:44 +00:00
|
|
|
int expungetype; /* BLK_SNAP or BLK_NOCOPY */
|
2000-07-11 22:07:57 +00:00
|
|
|
{
|
|
|
|
struct inode *ip = VTOI(vp);
|
|
|
|
ufs_daddr_t lbn, blkno, *blkp;
|
|
|
|
struct buf *ibp;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
for ( ; oldblkp < lastblkp; oldblkp++) {
|
|
|
|
blkno = *oldblkp;
|
|
|
|
if (blkno == 0 || blkno == BLK_NOCOPY || blkno == BLK_SNAP)
|
|
|
|
continue;
|
|
|
|
lbn = fragstoblks(fs, blkno);
|
|
|
|
if (lbn < NDADDR) {
|
|
|
|
blkp = &ip->i_db[lbn];
|
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
} else {
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
|
2000-07-11 22:07:57 +00:00
|
|
|
fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
blkp = &((ufs_daddr_t *)(ibp->b_data))
|
|
|
|
[(lbn - NDADDR) % NINDIR(fs)];
|
|
|
|
}
|
2001-12-14 00:15:06 +00:00
|
|
|
/*
|
2002-02-02 01:42:44 +00:00
|
|
|
* If we are expunging a snapshot vnode and we
|
|
|
|
* find a block marked BLK_NOCOPY, then it is
|
2001-12-14 00:15:06 +00:00
|
|
|
* one that has been allocated to this snapshot after
|
|
|
|
* we took our current snapshot and can be ignored.
|
|
|
|
*/
|
2002-02-02 01:42:44 +00:00
|
|
|
if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
|
2001-12-14 00:15:06 +00:00
|
|
|
if (lbn >= NDADDR)
|
|
|
|
brelse(ibp);
|
|
|
|
} else {
|
|
|
|
if (*blkp != 0)
|
|
|
|
panic("snapacct: bad block");
|
2002-02-02 01:42:44 +00:00
|
|
|
*blkp = expungetype;
|
2001-12-14 00:15:06 +00:00
|
|
|
if (lbn >= NDADDR)
|
|
|
|
bdwrite(ibp);
|
2000-07-24 05:28:33 +00:00
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2001-05-04 05:49:28 +00:00
|
|
|
/*
|
|
|
|
* Account for a set of blocks allocated in a snapshot inode.
|
|
|
|
*/
|
|
|
|
static int
|
2002-02-02 01:42:44 +00:00
|
|
|
mapacct(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
|
2001-05-04 05:49:28 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
ufs_daddr_t *oldblkp, *lastblkp;
|
|
|
|
struct fs *fs;
|
|
|
|
ufs_daddr_t lblkno;
|
2002-02-02 01:42:44 +00:00
|
|
|
int expungetype;
|
2001-05-04 05:49:28 +00:00
|
|
|
{
|
2002-02-02 01:42:44 +00:00
|
|
|
ufs_daddr_t blkno;
|
|
|
|
ino_t inum;
|
2001-05-04 05:49:28 +00:00
|
|
|
|
2002-02-02 01:42:44 +00:00
|
|
|
inum = VTOI(vp)->i_number;
|
2001-05-04 05:49:28 +00:00
|
|
|
for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
|
|
|
|
blkno = *oldblkp;
|
|
|
|
if (blkno == 0 || blkno == BLK_NOCOPY)
|
|
|
|
continue;
|
|
|
|
if (blkno == BLK_SNAP)
|
|
|
|
blkno = blkstofrags(fs, lblkno);
|
2002-02-02 01:42:44 +00:00
|
|
|
ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
|
2001-05-04 05:49:28 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2000-12-19 04:41:09 +00:00
|
|
|
/*
|
|
|
|
* Decrement extra reference on snapshot when last name is removed.
|
|
|
|
* It will not be freed until the last open reference goes away.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ffs_snapgone(ip)
|
|
|
|
struct inode *ip;
|
|
|
|
{
|
|
|
|
struct inode *xp;
|
2001-03-21 04:05:20 +00:00
|
|
|
struct fs *fs;
|
|
|
|
int snaploc;
|
2000-12-19 04:41:09 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find snapshot in incore list.
|
|
|
|
*/
|
2001-03-07 07:09:55 +00:00
|
|
|
TAILQ_FOREACH(xp, &ip->i_devvp->v_rdev->si_snapshots, i_nextsnap)
|
|
|
|
if (xp == ip)
|
2000-12-19 04:41:09 +00:00
|
|
|
break;
|
|
|
|
if (xp == 0)
|
|
|
|
printf("ffs_snapgone: lost snapshot vnode %d\n",
|
|
|
|
ip->i_number);
|
|
|
|
else
|
|
|
|
vrele(ITOV(ip));
|
2001-03-21 04:05:20 +00:00
|
|
|
/*
|
|
|
|
* Delete snapshot inode from superblock. Keep list dense.
|
|
|
|
*/
|
|
|
|
fs = ip->i_fs;
|
|
|
|
for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++)
|
|
|
|
if (fs->fs_snapinum[snaploc] == ip->i_number)
|
|
|
|
break;
|
|
|
|
if (snaploc < FSMAXSNAP) {
|
|
|
|
for (snaploc++; snaploc < FSMAXSNAP; snaploc++) {
|
|
|
|
if (fs->fs_snapinum[snaploc] == 0)
|
|
|
|
break;
|
|
|
|
fs->fs_snapinum[snaploc - 1] = fs->fs_snapinum[snaploc];
|
|
|
|
}
|
|
|
|
fs->fs_snapinum[snaploc - 1] = 0;
|
|
|
|
}
|
2000-12-19 04:41:09 +00:00
|
|
|
}
|
|
|
|
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Prepare a snapshot file for being removed.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ffs_snapremove(vp)
|
|
|
|
struct vnode *vp;
|
|
|
|
{
|
2001-03-07 07:09:55 +00:00
|
|
|
struct inode *ip;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct vnode *devvp;
|
|
|
|
struct buf *ibp;
|
|
|
|
struct fs *fs;
|
|
|
|
ufs_daddr_t blkno, dblk;
|
2001-05-08 07:29:03 +00:00
|
|
|
int error, numblks, loc, last;
|
2000-07-11 22:07:57 +00:00
|
|
|
|
|
|
|
ip = VTOI(vp);
|
|
|
|
fs = ip->i_fs;
|
|
|
|
/*
|
2001-04-25 08:11:18 +00:00
|
|
|
* If active, delete from incore list (this snapshot may
|
|
|
|
* already have been in the process of being deleted, so
|
|
|
|
* would not have been active).
|
|
|
|
*
|
2000-07-11 22:07:57 +00:00
|
|
|
* Clear copy-on-write flag if last snapshot.
|
|
|
|
*/
|
2001-04-25 08:11:18 +00:00
|
|
|
if (ip->i_nextsnap.tqe_prev != 0) {
|
|
|
|
devvp = ip->i_devvp;
|
2001-03-07 07:09:55 +00:00
|
|
|
TAILQ_REMOVE(&devvp->v_rdev->si_snapshots, ip, i_nextsnap);
|
|
|
|
ip->i_nextsnap.tqe_prev = 0;
|
|
|
|
if (TAILQ_FIRST(&devvp->v_rdev->si_snapshots) == 0) {
|
|
|
|
devvp->v_rdev->si_copyonwrite = 0;
|
|
|
|
devvp->v_flag &= ~VCOPYONWRITE;
|
|
|
|
}
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Clear all BLK_NOCOPY fields. Pass any block claims to other
|
|
|
|
* snapshots that want them (see ffs_snapblkfree below).
|
|
|
|
*/
|
|
|
|
for (blkno = 1; blkno < NDADDR; blkno++) {
|
|
|
|
dblk = ip->i_db[blkno];
|
2001-05-08 07:29:03 +00:00
|
|
|
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
|
2000-07-11 22:07:57 +00:00
|
|
|
ip->i_db[blkno] = 0;
|
2001-05-08 07:29:03 +00:00
|
|
|
else if ((dblk == blkstofrags(fs, blkno) &&
|
2002-02-02 01:42:44 +00:00
|
|
|
ffs_snapblkfree(fs, ip->i_devvp, dblk, fs->fs_bsize,
|
|
|
|
ip->i_number))) {
|
2001-05-08 07:29:03 +00:00
|
|
|
ip->i_blocks -= btodb(fs->fs_bsize);
|
|
|
|
ip->i_db[blkno] = 0;
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
2001-05-08 07:29:03 +00:00
|
|
|
numblks = howmany(ip->i_size, fs->fs_bsize);
|
|
|
|
for (blkno = NDADDR; blkno < numblks; blkno += NINDIR(fs)) {
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)blkno),
|
2000-07-11 22:07:57 +00:00
|
|
|
fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
|
|
|
|
if (error)
|
|
|
|
continue;
|
|
|
|
if ((last = fs->fs_size - blkno) > NINDIR(fs))
|
|
|
|
last = NINDIR(fs);
|
|
|
|
for (loc = 0; loc < last; loc++) {
|
|
|
|
dblk = ((ufs_daddr_t *)(ibp->b_data))[loc];
|
2001-05-08 07:29:03 +00:00
|
|
|
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
|
2000-07-11 22:07:57 +00:00
|
|
|
((ufs_daddr_t *)(ibp->b_data))[loc] = 0;
|
2001-05-08 07:29:03 +00:00
|
|
|
else if ((dblk == blkstofrags(fs, blkno) &&
|
2002-02-02 01:42:44 +00:00
|
|
|
ffs_snapblkfree(fs, ip->i_devvp, dblk,
|
|
|
|
fs->fs_bsize, ip->i_number))) {
|
2001-05-08 07:29:03 +00:00
|
|
|
ip->i_blocks -= btodb(fs->fs_bsize);
|
|
|
|
((ufs_daddr_t *)(ibp->b_data))[loc] = 0;
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
bawrite(ibp);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Clear snapshot flag and drop reference.
|
|
|
|
*/
|
2000-07-26 23:07:01 +00:00
|
|
|
ip->i_flags &= ~SF_SNAPSHOT;
|
2000-07-11 22:07:57 +00:00
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Notification that a block is being freed. Return zero if the free
|
|
|
|
* should be allowed to proceed. Return non-zero if the snapshot file
|
|
|
|
* wants to claim the block. The block will be claimed if it is an
|
|
|
|
* uncopied part of one of the snapshots. It will be freed if it is
|
|
|
|
* either a BLK_NOCOPY or has already been copied in all of the snapshots.
|
|
|
|
* If a fragment is being freed, then all snapshots that care about
|
|
|
|
* it must make a copy since a snapshot file can only claim full sized
|
|
|
|
* blocks. Note that if more than one snapshot file maps the block,
|
|
|
|
* we can pick one at random to claim it. Since none of the snapshots
|
|
|
|
* can change, we are assurred that they will all see the same unmodified
|
|
|
|
* image. When deleting a snapshot file (see ffs_snapremove above), we
|
|
|
|
* must push any of these claimed blocks to one of the other snapshots
|
|
|
|
* that maps it. These claimed blocks are easily identified as they will
|
|
|
|
* have a block number equal to their logical block number within the
|
|
|
|
* snapshot. A copied block can never have this property because they
|
|
|
|
* must always have been allocated from a BLK_NOCOPY location.
|
|
|
|
*/
|
|
|
|
int
|
2002-02-02 01:42:44 +00:00
|
|
|
ffs_snapblkfree(fs, devvp, bno, size, inum)
|
|
|
|
struct fs *fs;
|
|
|
|
struct vnode *devvp;
|
2000-07-11 22:07:57 +00:00
|
|
|
ufs_daddr_t bno;
|
|
|
|
long size;
|
2002-02-02 01:42:44 +00:00
|
|
|
ino_t inum;
|
2000-07-11 22:07:57 +00:00
|
|
|
{
|
|
|
|
struct buf *ibp, *cbp, *savedcbp = 0;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct inode *ip;
|
|
|
|
struct vnode *vp;
|
|
|
|
ufs_daddr_t lbn, blkno;
|
|
|
|
int indiroff = 0, error = 0, claimedblk = 0;
|
2001-03-07 07:09:55 +00:00
|
|
|
struct snaphead *snaphead;
|
2000-07-11 22:07:57 +00:00
|
|
|
|
|
|
|
lbn = fragstoblks(fs, bno);
|
2002-02-02 01:42:44 +00:00
|
|
|
snaphead = &devvp->v_rdev->si_snapshots;
|
2001-03-07 07:09:55 +00:00
|
|
|
TAILQ_FOREACH(ip, snaphead, i_nextsnap) {
|
2000-07-11 22:07:57 +00:00
|
|
|
vp = ITOV(ip);
|
|
|
|
/*
|
|
|
|
* Lookup block being written.
|
|
|
|
*/
|
|
|
|
if (lbn < NDADDR) {
|
|
|
|
blkno = ip->i_db[lbn];
|
|
|
|
} else {
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
|
|
|
td->td_proc->p_flag |= P_COWINPROGRESS;
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
|
2000-07-11 22:07:57 +00:00
|
|
|
fs->fs_bsize, KERNCRED, B_METAONLY, &ibp);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
indiroff = (lbn - NDADDR) % NINDIR(fs);
|
|
|
|
blkno = ((ufs_daddr_t *)(ibp->b_data))[indiroff];
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check to see if block needs to be copied.
|
|
|
|
*/
|
|
|
|
switch (blkno) {
|
|
|
|
/*
|
|
|
|
* If the snapshot has already copied the block (default),
|
|
|
|
* or does not care about the block, it is not needed.
|
|
|
|
*/
|
|
|
|
default:
|
|
|
|
case BLK_NOCOPY:
|
|
|
|
if (lbn >= NDADDR)
|
2000-07-24 05:28:33 +00:00
|
|
|
bqrelse(ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* No previous snapshot claimed the block, so it will be
|
|
|
|
* freed and become a BLK_NOCOPY (don't care) for us.
|
|
|
|
*/
|
|
|
|
case BLK_SNAP:
|
|
|
|
if (claimedblk)
|
|
|
|
panic("snapblkfree: inconsistent block type");
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (lbn < NDADDR) {
|
|
|
|
ip->i_db[lbn] = BLK_NOCOPY;
|
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
} else {
|
|
|
|
((ufs_daddr_t *)(ibp->b_data))[indiroff] =
|
|
|
|
BLK_NOCOPY;
|
|
|
|
bdwrite(ibp);
|
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
continue;
|
|
|
|
/*
|
|
|
|
* A block that we map is being freed. If it has not been
|
|
|
|
* claimed yet, we will claim or copy it (below).
|
|
|
|
*/
|
|
|
|
case 0:
|
|
|
|
claimedblk = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If this is a full size block, we will just grab it
|
|
|
|
* and assign it to the snapshot inode. Otherwise we
|
|
|
|
* will proceed to copy it. See explanation for this
|
|
|
|
* routine as to why only a single snapshot needs to
|
|
|
|
* claim this block.
|
|
|
|
*/
|
|
|
|
if (size == fs->fs_bsize) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (snapdebug)
|
|
|
|
printf("%s %d lbn %d from inum %d\n",
|
|
|
|
"Grabonremove: snapino", ip->i_number, lbn,
|
2002-02-02 01:42:44 +00:00
|
|
|
inum);
|
2000-07-11 22:07:57 +00:00
|
|
|
#endif
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (lbn < NDADDR) {
|
|
|
|
ip->i_db[lbn] = bno;
|
|
|
|
} else {
|
|
|
|
((ufs_daddr_t *)(ibp->b_data))[indiroff] = bno;
|
|
|
|
bdwrite(ibp);
|
|
|
|
}
|
|
|
|
ip->i_blocks += btodb(size);
|
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (lbn >= NDADDR)
|
2000-07-24 05:28:33 +00:00
|
|
|
bqrelse(ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Allocate the block into which to do the copy. Note that this
|
|
|
|
* allocation will never require any additional allocations for
|
|
|
|
* the snapshot inode.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
|
|
|
td->td_proc->p_flag |= P_COWINPROGRESS;
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
|
2000-07-11 22:07:57 +00:00
|
|
|
fs->fs_bsize, KERNCRED, 0, &cbp);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
2001-04-25 08:11:18 +00:00
|
|
|
if (error) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
break;
|
2001-04-25 08:11:18 +00:00
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (snapdebug)
|
2002-03-19 04:09:21 +00:00
|
|
|
printf(
|
|
|
|
"Copyonremove: snapino %lu lbn %ld for inum %lu size %ld to blkno %lld\n",
|
|
|
|
(unsigned long)ip->i_number, (long)lbn,
|
|
|
|
(unsigned long)inum, size, (long long)cbp->b_blkno);
|
2000-07-11 22:07:57 +00:00
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* If we have already read the old block contents, then
|
2001-04-25 08:11:18 +00:00
|
|
|
* simply copy them to the new block. Note that we need
|
|
|
|
* to synchronously write snapshots that have not been
|
|
|
|
* unlinked, and hence will be visible after a crash,
|
|
|
|
* to ensure their integrity.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
|
|
|
if (savedcbp != 0) {
|
|
|
|
bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
|
|
|
|
bawrite(cbp);
|
2001-05-14 17:16:49 +00:00
|
|
|
if (dopersistence && ip->i_effnlink > 0)
|
2001-09-12 08:38:13 +00:00
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Otherwise, read the old block contents into the buffer.
|
|
|
|
*/
|
2001-04-25 08:11:18 +00:00
|
|
|
if ((error = readblock(cbp, lbn)) != 0) {
|
|
|
|
bzero(cbp->b_data, fs->fs_bsize);
|
|
|
|
bawrite(cbp);
|
2001-05-14 17:16:49 +00:00
|
|
|
if (dopersistence && ip->i_effnlink > 0)
|
2001-09-12 08:38:13 +00:00
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
break;
|
2001-04-25 08:11:18 +00:00
|
|
|
}
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
savedcbp = cbp;
|
|
|
|
}
|
2001-04-25 08:11:18 +00:00
|
|
|
/*
|
|
|
|
* Note that we need to synchronously write snapshots that
|
|
|
|
* have not been unlinked, and hence will be visible after
|
|
|
|
* a crash, to ensure their integrity.
|
|
|
|
*/
|
|
|
|
if (savedcbp) {
|
|
|
|
vp = savedcbp->b_vp;
|
2000-07-11 22:07:57 +00:00
|
|
|
bawrite(savedcbp);
|
2001-05-14 17:16:49 +00:00
|
|
|
if (dopersistence && VTOI(vp)->i_effnlink > 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2001-05-11 07:12:03 +00:00
|
|
|
}
|
2001-04-25 08:11:18 +00:00
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* If we have been unable to allocate a block in which to do
|
|
|
|
* the copy, then return non-zero so that the fragment will
|
|
|
|
* not be freed. Although space will be lost, the snapshot
|
|
|
|
* will stay consistent.
|
|
|
|
*/
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Associate snapshot files when mounting.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ffs_snapshot_mount(mp)
|
|
|
|
struct mount *mp;
|
|
|
|
{
|
|
|
|
struct ufsmount *ump = VFSTOUFS(mp);
|
|
|
|
struct fs *fs = ump->um_fs;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread;
|
2001-03-07 07:09:55 +00:00
|
|
|
struct snaphead *snaphead;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct vnode *vp;
|
2001-03-07 07:09:55 +00:00
|
|
|
struct inode *ip;
|
2000-07-11 22:07:57 +00:00
|
|
|
int error, snaploc, loc;
|
|
|
|
|
2001-03-07 07:09:55 +00:00
|
|
|
snaphead = &ump->um_devvp->v_rdev->si_snapshots;
|
2000-07-11 22:07:57 +00:00
|
|
|
for (snaploc = 0; snaploc < FSMAXSNAP; snaploc++) {
|
|
|
|
if (fs->fs_snapinum[snaploc] == 0)
|
|
|
|
return;
|
2002-03-17 01:25:47 +00:00
|
|
|
if ((error = VFS_VGET(mp, fs->fs_snapinum[snaploc],
|
|
|
|
LK_EXCLUSIVE, &vp)) != 0){
|
2000-07-11 22:07:57 +00:00
|
|
|
printf("ffs_snapshot_mount: vget failed %d\n", error);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ip = VTOI(vp);
|
|
|
|
if ((ip->i_flags & SF_SNAPSHOT) == 0) {
|
|
|
|
printf("ffs_snapshot_mount: non-snapshot inode %d\n",
|
|
|
|
fs->fs_snapinum[snaploc]);
|
|
|
|
vput(vp);
|
|
|
|
for (loc = snaploc + 1; loc < FSMAXSNAP; loc++) {
|
|
|
|
if (fs->fs_snapinum[loc] == 0)
|
|
|
|
break;
|
|
|
|
fs->fs_snapinum[loc - 1] = fs->fs_snapinum[loc];
|
|
|
|
}
|
|
|
|
fs->fs_snapinum[loc - 1] = 0;
|
|
|
|
snaploc--;
|
|
|
|
continue;
|
|
|
|
}
|
2001-03-07 07:09:55 +00:00
|
|
|
if (ip->i_nextsnap.tqe_prev != 0)
|
2000-07-11 22:07:57 +00:00
|
|
|
panic("ffs_snapshot_mount: %d already on list",
|
|
|
|
ip->i_number);
|
2001-03-07 07:09:55 +00:00
|
|
|
else
|
|
|
|
TAILQ_INSERT_TAIL(snaphead, ip, i_nextsnap);
|
2000-07-11 22:07:57 +00:00
|
|
|
vp->v_flag |= VSYSTEM;
|
2001-03-07 07:09:55 +00:00
|
|
|
ump->um_devvp->v_rdev->si_copyonwrite = ffs_copyonwrite;
|
2000-07-11 22:07:57 +00:00
|
|
|
ump->um_devvp->v_flag |= VCOPYONWRITE;
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disassociate snapshot files when unmounting.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ffs_snapshot_unmount(mp)
|
|
|
|
struct mount *mp;
|
|
|
|
{
|
|
|
|
struct ufsmount *ump = VFSTOUFS(mp);
|
2001-03-07 07:09:55 +00:00
|
|
|
struct snaphead *snaphead = &ump->um_devvp->v_rdev->si_snapshots;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct inode *xp;
|
|
|
|
|
2001-03-07 07:09:55 +00:00
|
|
|
while ((xp = TAILQ_FIRST(snaphead)) != 0) {
|
|
|
|
TAILQ_REMOVE(snaphead, xp, i_nextsnap);
|
|
|
|
xp->i_nextsnap.tqe_prev = 0;
|
2000-07-26 23:07:01 +00:00
|
|
|
if (xp->i_effnlink > 0)
|
|
|
|
vrele(ITOV(xp));
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
2001-03-07 07:09:55 +00:00
|
|
|
ump->um_devvp->v_rdev->si_copyonwrite = 0;
|
2000-07-11 22:07:57 +00:00
|
|
|
ump->um_devvp->v_flag &= ~VCOPYONWRITE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for need to copy block that is about to be written,
|
|
|
|
* copying the block if necessary.
|
|
|
|
*/
|
2001-03-07 07:09:55 +00:00
|
|
|
static int
|
|
|
|
ffs_copyonwrite(devvp, bp)
|
|
|
|
struct vnode *devvp;
|
|
|
|
struct buf *bp;
|
2000-07-11 22:07:57 +00:00
|
|
|
{
|
2001-03-07 07:09:55 +00:00
|
|
|
struct buf *ibp, *cbp, *savedcbp = 0;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread;
|
2001-03-07 07:09:55 +00:00
|
|
|
struct fs *fs;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct inode *ip;
|
|
|
|
struct vnode *vp;
|
|
|
|
ufs_daddr_t lbn, blkno;
|
|
|
|
int indiroff, error = 0;
|
|
|
|
|
2001-03-07 07:09:55 +00:00
|
|
|
fs = TAILQ_FIRST(&devvp->v_rdev->si_snapshots)->i_fs;
|
2000-07-11 22:07:57 +00:00
|
|
|
lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
|
2001-09-12 08:38:13 +00:00
|
|
|
if (td->td_proc->p_flag & P_COWINPROGRESS)
|
2000-07-11 22:07:57 +00:00
|
|
|
panic("ffs_copyonwrite: recursive call");
|
2001-03-07 07:09:55 +00:00
|
|
|
TAILQ_FOREACH(ip, &devvp->v_rdev->si_snapshots, i_nextsnap) {
|
2000-07-11 22:07:57 +00:00
|
|
|
vp = ITOV(ip);
|
|
|
|
/*
|
|
|
|
* We ensure that everything of our own that needs to be
|
|
|
|
* copied will be done at the time that ffs_snapshot is
|
|
|
|
* called. Thus we can skip the check here which can
|
2001-04-29 12:36:52 +00:00
|
|
|
* deadlock in doing the lookup in UFS_BALLOC.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
|
|
|
if (bp->b_vp == vp)
|
|
|
|
continue;
|
|
|
|
/*
|
2000-07-24 05:28:33 +00:00
|
|
|
* Check to see if block needs to be copied. We have to
|
2001-04-29 12:36:52 +00:00
|
|
|
* be able to do the UFS_BALLOC without blocking, otherwise
|
2000-07-24 05:28:33 +00:00
|
|
|
* we may get in a deadlock with another process also
|
|
|
|
* trying to allocate. If we find outselves unable to
|
|
|
|
* get the buffer lock, we unlock the snapshot vnode,
|
|
|
|
* sleep briefly, and try again.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
2000-07-24 05:28:33 +00:00
|
|
|
retry:
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (lbn < NDADDR) {
|
|
|
|
blkno = ip->i_db[lbn];
|
|
|
|
} else {
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag |= P_COWINPROGRESS;
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
|
2000-07-24 05:28:33 +00:00
|
|
|
fs->fs_bsize, KERNCRED, B_METAONLY | B_NOWAIT, &ibp);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
2000-07-24 05:28:33 +00:00
|
|
|
if (error) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-24 05:28:33 +00:00
|
|
|
if (error != EWOULDBLOCK)
|
|
|
|
break;
|
2002-02-11 20:37:54 +00:00
|
|
|
tsleep(vp, td->td_ksegrp->kg_user_pri, "nap", 1);
|
2000-07-24 05:28:33 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
indiroff = (lbn - NDADDR) % NINDIR(fs);
|
|
|
|
blkno = ((ufs_daddr_t *)(ibp->b_data))[indiroff];
|
2000-07-24 05:28:33 +00:00
|
|
|
bqrelse(ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
#ifdef DIAGNOSTIC
|
|
|
|
if (blkno == BLK_SNAP && bp->b_lblkno >= 0)
|
|
|
|
panic("ffs_copyonwrite: bad copy block");
|
|
|
|
#endif
|
2000-07-24 05:28:33 +00:00
|
|
|
if (blkno != 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
continue;
|
2000-07-24 05:28:33 +00:00
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* Allocate the block into which to do the copy. Note that this
|
|
|
|
* allocation will never require any additional allocations for
|
|
|
|
* the snapshot inode.
|
|
|
|
*/
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag |= P_COWINPROGRESS;
|
2001-04-29 12:36:52 +00:00
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
|
2000-07-24 05:28:33 +00:00
|
|
|
fs->fs_bsize, KERNCRED, B_NOWAIT, &cbp);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
2000-07-24 05:28:33 +00:00
|
|
|
if (error) {
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-24 05:28:33 +00:00
|
|
|
if (error != EWOULDBLOCK)
|
|
|
|
break;
|
2002-02-11 20:37:54 +00:00
|
|
|
tsleep(vp, td->td_ksegrp->kg_user_pri, "nap", 1);
|
2000-07-24 05:28:33 +00:00
|
|
|
goto retry;
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (snapdebug) {
|
|
|
|
printf("Copyonwrite: snapino %d lbn %d for ",
|
|
|
|
ip->i_number, lbn);
|
2001-03-07 07:09:55 +00:00
|
|
|
if (bp->b_vp == devvp)
|
2000-07-11 22:07:57 +00:00
|
|
|
printf("fs metadata");
|
|
|
|
else
|
|
|
|
printf("inum %d", VTOI(bp->b_vp)->i_number);
|
2002-03-19 04:09:21 +00:00
|
|
|
printf(" lblkno %lld to blkno %lld\n",
|
|
|
|
(long long)bp->b_lblkno, (long long)cbp->b_blkno);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/*
|
|
|
|
* If we have already read the old block contents, then
|
2001-04-25 08:11:18 +00:00
|
|
|
* simply copy them to the new block. Note that we need
|
|
|
|
* to synchronously write snapshots that have not been
|
|
|
|
* unlinked, and hence will be visible after a crash,
|
|
|
|
* to ensure their integrity.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
|
|
|
if (savedcbp != 0) {
|
|
|
|
bcopy(savedcbp->b_data, cbp->b_data, fs->fs_bsize);
|
|
|
|
bawrite(cbp);
|
2001-05-14 17:16:49 +00:00
|
|
|
if (dopersistence && ip->i_effnlink > 0)
|
2001-09-12 08:38:13 +00:00
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Otherwise, read the old block contents into the buffer.
|
|
|
|
*/
|
2001-04-25 08:11:18 +00:00
|
|
|
if ((error = readblock(cbp, lbn)) != 0) {
|
|
|
|
bzero(cbp->b_data, fs->fs_bsize);
|
|
|
|
bawrite(cbp);
|
2001-05-14 17:16:49 +00:00
|
|
|
if (dopersistence && ip->i_effnlink > 0)
|
2001-09-12 08:38:13 +00:00
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
break;
|
2001-04-25 08:11:18 +00:00
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
savedcbp = cbp;
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
2001-04-25 08:11:18 +00:00
|
|
|
/*
|
|
|
|
* Note that we need to synchronously write snapshots that
|
|
|
|
* have not been unlinked, and hence will be visible after
|
|
|
|
* a crash, to ensure their integrity.
|
|
|
|
*/
|
|
|
|
if (savedcbp) {
|
|
|
|
vp = savedcbp->b_vp;
|
2000-07-11 22:07:57 +00:00
|
|
|
bawrite(savedcbp);
|
2001-05-14 17:16:49 +00:00
|
|
|
if (dopersistence && VTOI(vp)->i_effnlink > 0) {
|
2001-09-12 08:38:13 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2001-05-14 17:16:49 +00:00
|
|
|
}
|
2001-04-25 08:11:18 +00:00
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the specified block into the given buffer.
|
|
|
|
* Much of this boiler-plate comes from bwrite().
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
readblock(bp, lbn)
|
|
|
|
struct buf *bp;
|
|
|
|
daddr_t lbn;
|
|
|
|
{
|
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
2001-09-12 08:38:13 +00:00
|
|
|
struct thread *td = curthread;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct inode *ip = VTOI(bp->b_vp);
|
|
|
|
|
|
|
|
aiov.iov_base = bp->b_data;
|
|
|
|
aiov.iov_len = bp->b_bcount;
|
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
auio.uio_offset = dbtob(fsbtodb(ip->i_fs, blkstofrags(ip->i_fs, lbn)));
|
|
|
|
auio.uio_resid = bp->b_bcount;
|
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
2001-09-12 08:38:13 +00:00
|
|
|
auio.uio_td = td;
|
2000-07-11 22:07:57 +00:00
|
|
|
return (physio(ip->i_devvp->v_rdev, &auio, 0));
|
|
|
|
}
|