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>
|
2002-06-21 06:18:05 +00:00
|
|
|
#include <sys/stdint.h>
|
2002-10-16 00:19:23 +00:00
|
|
|
#include <sys/kernel.h>
|
2000-07-11 22:07:57 +00:00
|
|
|
#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
|
|
|
|
2002-03-19 22:40:48 +00:00
|
|
|
static int cgaccount(int, struct vnode *, struct buf *, int);
|
2002-06-21 06:18:05 +00:00
|
|
|
static int expunge_ufs1(struct vnode *, struct inode *, struct fs *,
|
|
|
|
int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
|
|
|
|
ufs_lbn_t, int), int);
|
|
|
|
static int indiracct_ufs1(struct vnode *, struct vnode *, int,
|
|
|
|
ufs1_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
|
|
|
|
int (*)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *, struct fs *,
|
|
|
|
ufs_lbn_t, int), int);
|
|
|
|
static int fullacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
|
|
|
static int snapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
|
|
|
static int mapacct_ufs1(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
|
|
|
static int expunge_ufs2(struct vnode *, struct inode *, struct fs *,
|
|
|
|
int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
|
|
|
|
ufs_lbn_t, int), int);
|
|
|
|
static int indiracct_ufs2(struct vnode *, struct vnode *, int,
|
|
|
|
ufs2_daddr_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, ufs_lbn_t, struct fs *,
|
|
|
|
int (*)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *, struct fs *,
|
|
|
|
ufs_lbn_t, int), int);
|
|
|
|
static int fullacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
|
|
|
static int snapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
|
|
|
static int mapacct_ufs2(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
2002-03-19 22:40:48 +00:00
|
|
|
static int ffs_copyonwrite(struct vnode *, struct buf *);
|
2002-06-21 06:18:05 +00:00
|
|
|
static int readblock(struct buf *, ufs2_daddr_t);
|
2000-07-11 22:07:57 +00:00
|
|
|
|
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-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t numblks, blkno;
|
|
|
|
int error, cg, snaploc;
|
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;
|
2002-12-14 01:36:59 +00:00
|
|
|
long redo = 0, snaplistsize;
|
2001-05-04 05:49:28 +00:00
|
|
|
int32_t *lp;
|
2001-01-15 18:30:40 +00:00
|
|
|
void *space;
|
2002-12-14 01:36:59 +00:00
|
|
|
daddr_t *snapblklist;
|
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-11-30 19:00:51 +00:00
|
|
|
struct vnode *vp, *xvp, *nvp, *devvp;
|
2002-10-09 06:13:48 +00:00
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
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);
|
2002-11-30 19:00:51 +00:00
|
|
|
devvp = ip->i_devvp;
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* 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)),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_CLRBUF, &bp);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
ip->i_size = lblktosize(fs, (off_t)numblks);
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_size) = ip->i_size;
|
2000-07-11 22:07:57 +00:00
|
|
|
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-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, td->td_ucred, BA_METAONLY, &ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
2002-11-30 07:27:12 +00:00
|
|
|
bawrite(ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Allocate copies for the superblock and its summary information.
|
|
|
|
*/
|
2002-11-27 02:18:58 +00:00
|
|
|
error = UFS_BALLOC(vp, fs->fs_sblockloc, fs->fs_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++) {
|
2003-02-22 00:19:26 +00:00
|
|
|
error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
|
2001-12-14 00:15:06 +00:00
|
|
|
fs->fs_bsize, KERNCRED, 0, &nbp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
2002-11-30 07:27:12 +00:00
|
|
|
bawrite(nbp);
|
2001-12-14 00:15:06 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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);
|
2003-02-19 05:47:46 +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++) {
|
2003-02-22 00:19:26 +00:00
|
|
|
error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
|
2002-12-03 18:19:27 +00:00
|
|
|
fs->fs_bsize, KERNCRED, 0, &nbp);
|
|
|
|
if (error)
|
2001-12-14 00:15:06 +00:00
|
|
|
goto out;
|
|
|
|
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;
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_flags) = ip->i_flags;
|
2000-07-11 22:07:57 +00:00
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
/*
|
|
|
|
* Ensure that the snapshot is completely on disk.
|
2002-11-30 07:27:12 +00:00
|
|
|
* Since we have marked it as a snapshot it is safe to
|
|
|
|
* unlock it as no process will be allowed to write to it.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
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;
|
2002-11-30 07:27:12 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
|
|
|
* 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);
|
2002-10-25 00:20:37 +00:00
|
|
|
if ((error = vfs_write_suspend(vp->v_mount)) != 0) {
|
|
|
|
vn_start_write(NULL, &wrtmp, V_WAIT);
|
2002-11-30 07:27:12 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
2002-10-25 00:20:37 +00:00
|
|
|
goto out;
|
|
|
|
}
|
2000-07-11 22:07:57 +00:00
|
|
|
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-11-30 07:27:12 +00:00
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
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++;
|
2003-02-22 00:19:26 +00:00
|
|
|
error = UFS_BALLOC(vp, lfragtosize(fs, cgtod(fs, cg)),
|
2002-12-03 18:19:27 +00:00
|
|
|
fs->fs_bsize, KERNCRED, 0, &nbp);
|
|
|
|
if (error)
|
2000-07-11 22:07:57 +00:00
|
|
|
goto out1;
|
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.
|
|
|
|
*/
|
2002-11-27 02:18:58 +00:00
|
|
|
error = bread(vp, lblkno(fs, fs->fs_sblockloc), fs->fs_bsize,
|
2002-06-21 06:18:05 +00:00
|
|
|
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
|
|
|
}
|
2002-11-27 02:18:58 +00:00
|
|
|
loc = blkoff(fs, fs->fs_sblockloc);
|
2002-06-21 06:18:05 +00:00
|
|
|
copy_fs = (struct fs *)(sbp->b_data + loc);
|
2001-05-04 05:49:28 +00:00
|
|
|
bcopy(fs, copy_fs, fs->fs_sbsize);
|
|
|
|
if ((fs->fs_flags & (FS_UNCLEAN | FS_NEEDSFSCK)) == 0)
|
|
|
|
copy_fs->fs_clean = 1;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (fs->fs_sbsize < SBLOCKSIZE)
|
|
|
|
bzero(&sbp->b_data[loc + fs->fs_sbsize],
|
|
|
|
SBLOCKSIZE - fs->fs_sbsize);
|
2001-05-04 05:49:28 +00:00
|
|
|
size = blkroundup(fs, fs->fs_cssize);
|
|
|
|
if (fs->fs_contigsumsize > 0)
|
|
|
|
size += fs->fs_ncg * sizeof(int32_t);
|
2003-02-19 05:47:46 +00:00
|
|
|
space = malloc((u_long)size, M_UFSMNT, M_WAITOK);
|
2001-05-04 05:49:28 +00:00
|
|
|
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) {
|
2002-11-30 19:00:51 +00:00
|
|
|
if ((error = bread(devvp, fsbtodb(fs, fs->fs_csaddr + loc),
|
2001-05-04 05:49:28 +00:00
|
|
|
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.
|
2002-10-09 06:13:48 +00:00
|
|
|
* Note that we skip unlinked snapshot files as they will
|
|
|
|
* be handled separately below.
|
2002-02-02 01:42:44 +00:00
|
|
|
*/
|
|
|
|
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);
|
2002-09-25 02:47:49 +00:00
|
|
|
mp_fixme("Unlocked GETATTR.");
|
|
|
|
if (vrefcnt(xvp) == 0 || xvp->v_type == VNON ||
|
2002-10-09 06:13:48 +00:00
|
|
|
(VTOI(xvp)->i_flags & SF_SNAPSHOT) ||
|
2002-02-02 01:42:44 +00:00
|
|
|
(VOP_GETATTR(xvp, &vat, td->td_proc->p_ucred, td) == 0 &&
|
|
|
|
vat.va_nlink > 0)) {
|
|
|
|
mtx_lock(&mntvnode_mtx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (snapdebug)
|
|
|
|
vprint("ffs_snapshot: busy vnode", xvp);
|
2002-10-08 21:00:52 +00:00
|
|
|
if (vn_lock(xvp, LK_EXCLUSIVE, td) != 0)
|
2002-02-02 01:42:44 +00:00
|
|
|
goto loop;
|
|
|
|
xp = VTOI(xvp);
|
2003-02-22 00:29:51 +00:00
|
|
|
if (ffs_checkfreefile(copy_fs, vp, xp->i_number)) {
|
|
|
|
VOP_UNLOCK(xvp, 0, td);
|
|
|
|
continue;
|
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
/*
|
|
|
|
* 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) {
|
2002-06-21 06:18:05 +00:00
|
|
|
ffs_blkfree(copy_fs, vp, DIP(xp, i_db[loc]),
|
|
|
|
len, xp->i_number);
|
|
|
|
blkno = DIP(xp, i_db[loc]);
|
|
|
|
DIP(xp, i_db[loc]) = 0;
|
2002-02-02 01:42:44 +00:00
|
|
|
}
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
if (xp->i_ump->um_fstype == UFS1)
|
|
|
|
error = expunge_ufs1(vp, xp, copy_fs, fullacct_ufs1,
|
|
|
|
BLK_NOCOPY);
|
|
|
|
else
|
|
|
|
error = expunge_ufs2(vp, xp, copy_fs, fullacct_ufs2,
|
|
|
|
BLK_NOCOPY);
|
2002-02-02 01:42:44 +00:00
|
|
|
if (blkno)
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(xp, i_db[loc]) = blkno;
|
2002-02-02 01:42:44 +00:00
|
|
|
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);
|
2002-10-16 00:19:23 +00:00
|
|
|
/*
|
|
|
|
* If there already exist snapshots on this filesystem, grab a
|
|
|
|
* reference to their shared lock. If this is the first snapshot
|
|
|
|
* on this filesystem, we need to allocate a lock for the snapshots
|
|
|
|
* to share. In either case, acquire the snapshot lock and give
|
|
|
|
* up our original private lock.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
|
|
|
snaphead = &devvp->v_rdev->si_snapshots;
|
2002-10-16 00:19:23 +00:00
|
|
|
if ((xp = TAILQ_FIRST(snaphead)) != NULL) {
|
|
|
|
VI_LOCK(vp);
|
|
|
|
vp->v_vnlock = ITOV(xp)->v_vnlock;
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_UNLOCK(devvp);
|
2002-10-16 00:19:23 +00:00
|
|
|
} else {
|
|
|
|
struct lock *lkp;
|
|
|
|
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_UNLOCK(devvp);
|
2002-10-16 00:19:23 +00:00
|
|
|
MALLOC(lkp, struct lock *, sizeof(struct lock), M_UFSMNT,
|
2003-02-19 05:47:46 +00:00
|
|
|
M_WAITOK);
|
2002-10-16 00:19:23 +00:00
|
|
|
lockinit(lkp, PVFS, "snaplk", VLKTIMEOUT,
|
|
|
|
LK_CANRECURSE | LK_NOPAUSE);
|
|
|
|
VI_LOCK(vp);
|
|
|
|
vp->v_vnlock = lkp;
|
|
|
|
}
|
|
|
|
vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, td);
|
2002-11-30 19:00:51 +00:00
|
|
|
transferlockers(&vp->v_lock, vp->v_vnlock);
|
|
|
|
lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
|
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.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
2000-07-11 22:07:57 +00:00
|
|
|
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);
|
2002-10-16 00:19:23 +00:00
|
|
|
TAILQ_INSERT_TAIL(snaphead, ip, i_nextsnap);
|
2002-11-30 19:00:51 +00:00
|
|
|
devvp->v_rdev->si_copyonwrite = ffs_copyonwrite;
|
|
|
|
devvp->v_vflag |= VV_COPYONWRITE;
|
|
|
|
VI_UNLOCK(devvp);
|
2002-08-04 10:29:36 +00:00
|
|
|
ASSERT_VOP_LOCKED(vp, "ffs_snapshot vp");
|
|
|
|
vp->v_vflag |= VV_SYSTEM;
|
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);
|
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);
|
2002-11-15 22:36:57 +00:00
|
|
|
printf("%s: suspended %ld.%03ld sec, redo %ld of %d\n",
|
|
|
|
vp->v_mount->mnt_stat.f_mntonname, (long)endtime.tv_sec,
|
2001-12-14 00:15:06 +00:00
|
|
|
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.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
snaphead = &devvp->v_rdev->si_snapshots;
|
2002-02-02 01:42:44 +00:00
|
|
|
TAILQ_FOREACH(xp, snaphead, i_nextsnap) {
|
|
|
|
if (xp == ip)
|
|
|
|
break;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (xp->i_ump->um_fstype == UFS1)
|
|
|
|
error = expunge_ufs1(vp, xp, fs, snapacct_ufs1,
|
|
|
|
BLK_SNAP);
|
|
|
|
else
|
|
|
|
error = expunge_ufs2(vp, xp, fs, snapacct_ufs2,
|
|
|
|
BLK_SNAP);
|
|
|
|
if (error) {
|
2002-02-02 01:42:44 +00:00
|
|
|
fs->fs_snapinum[snaploc] = 0;
|
|
|
|
goto done;
|
2001-05-04 05:49:28 +00:00
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
}
|
2002-10-09 06:13:48 +00:00
|
|
|
/*
|
|
|
|
* Allocate the space for the list of preallocated snapshot blocks.
|
|
|
|
*/
|
2002-12-14 01:36:59 +00:00
|
|
|
snaplistsize = fs->fs_ncg + howmany(fs->fs_cssize, fs->fs_bsize) +
|
|
|
|
FSMAXSNAP + 1 /* superblock */ + 1 /* last block */ + 1 /* size */;
|
|
|
|
MALLOC(snapblklist, daddr_t *, snaplistsize * sizeof(daddr_t),
|
2003-02-19 05:47:46 +00:00
|
|
|
M_UFSMNT, M_WAITOK);
|
2002-12-15 19:25:59 +00:00
|
|
|
ip->i_snapblklist = &snapblklist[1];
|
2002-02-02 01:42:44 +00:00
|
|
|
/*
|
|
|
|
* Expunge the blocks used by the snapshots from the set of
|
2002-10-09 06:13:48 +00:00
|
|
|
* blocks marked as used in the snapshot bitmaps. Also, collect
|
2002-12-15 19:25:59 +00:00
|
|
|
* the list of allocated blocks in i_snapblklist.
|
2002-02-02 01:42:44 +00:00
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
if (ip->i_ump->um_fstype == UFS1)
|
|
|
|
error = expunge_ufs1(vp, ip, copy_fs, mapacct_ufs1, BLK_SNAP);
|
|
|
|
else
|
|
|
|
error = expunge_ufs2(vp, ip, copy_fs, mapacct_ufs2, BLK_SNAP);
|
|
|
|
if (error) {
|
2002-02-02 01:42:44 +00:00
|
|
|
fs->fs_snapinum[snaploc] = 0;
|
2002-12-14 01:36:59 +00:00
|
|
|
FREE(snapblklist, M_UFSMNT);
|
2002-02-02 01:42:44 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2002-12-15 19:25:59 +00:00
|
|
|
snaplistsize = ip->i_snapblklist - snapblklist;
|
2002-12-14 01:36:59 +00:00
|
|
|
snapblklist[0] = snaplistsize;
|
2002-12-15 19:25:59 +00:00
|
|
|
ip->i_snapblklist = 0;
|
2002-10-09 06:13:48 +00:00
|
|
|
/*
|
|
|
|
* Write out the list of allocated blocks to the end of the snapshot.
|
|
|
|
*/
|
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
2002-12-14 01:36:59 +00:00
|
|
|
aiov.iov_base = (void *)snapblklist;
|
|
|
|
aiov.iov_len = snaplistsize * sizeof(daddr_t);
|
2002-10-09 06:13:48 +00:00
|
|
|
auio.uio_resid = aiov.iov_len;;
|
|
|
|
auio.uio_offset = ip->i_size;
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_rw = UIO_WRITE;
|
|
|
|
auio.uio_td = td;
|
|
|
|
if ((error = VOP_WRITE(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
|
|
|
|
fs->fs_snapinum[snaploc] = 0;
|
2002-12-14 01:36:59 +00:00
|
|
|
FREE(snapblklist, M_UFSMNT);
|
2002-10-09 06:13:48 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2002-02-02 01:42:44 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2002-12-14 01:36:59 +00:00
|
|
|
FREE(snapblklist, M_UFSMNT);
|
2002-02-02 01:42:44 +00:00
|
|
|
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-12-14 01:36:59 +00:00
|
|
|
/*
|
|
|
|
* As this is the newest list, it is the most inclusive, so
|
|
|
|
* should replace the previous list.
|
|
|
|
*/
|
|
|
|
VI_LOCK(devvp);
|
|
|
|
FREE(devvp->v_rdev->si_snapblklist, M_UFSMNT);
|
|
|
|
devvp->v_rdev->si_snapblklist = snapblklist;
|
|
|
|
devvp->v_rdev->si_snaplistsize = snaplistsize;
|
|
|
|
VI_UNLOCK(devvp);
|
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:
|
This checkin reimplements the io-request priority hack in a way
that works in the new threaded kernel. It was commented out of
the disksort routine earlier this year for the reasons given in
kern/subr_disklabel.c (which is where this code used to reside
before it moved to kern/subr_disk.c):
----------------------------
revision 1.65
date: 2002/04/22 06:53:20; author: phk; state: Exp; lines: +5 -0
Comment out Kirks io-request priority hack until we can do this in a
civilized way which doesn't cause grief.
The problem is that it is not generally safe to cast a "struct bio
*" to a "struct buf *". Things like ccd, vinum, ata-raid and GEOM
constructs bio's which are not entrails of a struct buf.
Also, curthread may or may not have anything to do with the I/O request
at hand.
The correct solution can either be to tag struct bio's with a
priority derived from the requesting threads nice and have disksort
act on this field, this wouldn't address the "silly-seek syndrome"
where two equal processes bang the diskheads from one edge to the
other of the disk repeatedly.
Alternatively, and probably better: a sleep should be introduced
either at the time the I/O is requested or at the time it is completed
where we can be sure to sleep in the right thread.
The sleep also needs to be in constant timeunits, 1/hz can be practicaly
any sub-second size, at high HZ the current code practically doesn't
do anything.
----------------------------
As suggested in this comment, it is no longer located in the disk sort
routine, but rather now resides in spec_strategy where the disk operations
are being queued by the thread that is associated with the process that
is really requesting the I/O. At that point, the disk queues are not
visible, so the I/O for positively niced processes is always slowed
down whether or not there is other activity on the disk.
On the issue of scaling HZ, I believe that the current scheme is
better than using a fixed quantum of time. As machines and I/O
subsystems get faster, the resolution on the clock also rises.
So, ten years from now we will be slowing things down for shorter
periods of time, but the proportional effect on the system will
be about the same as it is today. So, I view this as a feature
rather than a drawback. Hence this patch sticks with using HZ.
Sponsored by: DARPA & NAI Labs.
Reviewed by: Poul-Henning Kamp <phk@critter.freebsd.dk>
2002-10-22 00:59:49 +00:00
|
|
|
if (saved_nice > 0)
|
|
|
|
td->td_ksegrp->kg_nice = saved_nice;
|
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;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t base, numblks;
|
|
|
|
int error, len, loc, indiroff;
|
2001-12-14 00:15:06 +00:00
|
|
|
|
|
|
|
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))
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_db[loc]) = BLK_NOCOPY;
|
|
|
|
else if (passno == 2 && DIP(ip, i_db[loc])== BLK_NOCOPY)
|
|
|
|
DIP(ip, i_db[loc]) = 0;
|
|
|
|
else if (passno == 1 && DIP(ip, i_db[loc])== BLK_NOCOPY)
|
2001-12-14 00:15:06 +00:00
|
|
|
panic("ffs_snapshot: lost direct block");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)(base + loc)),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
|
2001-12-14 00:15:06 +00:00
|
|
|
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)),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
|
2001-12-14 00:15:06 +00:00
|
|
|
if (error) {
|
|
|
|
brelse(bp);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
indiroff = 0;
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
if (ip->i_ump->um_fstype == UFS1) {
|
|
|
|
if (ffs_isblock(fs, cg_blksfree(cgp), loc))
|
|
|
|
((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
|
|
|
|
BLK_NOCOPY;
|
|
|
|
else if (passno == 2 && ((ufs1_daddr_t *)(ibp->b_data))
|
|
|
|
[indiroff] == BLK_NOCOPY)
|
|
|
|
((ufs1_daddr_t *)(ibp->b_data))[indiroff] = 0;
|
|
|
|
else if (passno == 1 && ((ufs1_daddr_t *)(ibp->b_data))
|
|
|
|
[indiroff] == BLK_NOCOPY)
|
|
|
|
panic("ffs_snapshot: lost indirect block");
|
|
|
|
continue;
|
|
|
|
}
|
2001-12-14 00:15:06 +00:00
|
|
|
if (ffs_isblock(fs, cg_blksfree(cgp), loc))
|
2002-06-21 06:18:05 +00:00
|
|
|
((ufs2_daddr_t *)(ibp->b_data))[indiroff] = BLK_NOCOPY;
|
2001-12-14 00:15:06 +00:00
|
|
|
else if (passno == 2 &&
|
2002-06-21 06:18:05 +00:00
|
|
|
((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
|
|
|
|
((ufs2_daddr_t *)(ibp->b_data))[indiroff] = 0;
|
2001-12-14 00:15:06 +00:00
|
|
|
else if (passno == 1 &&
|
2002-06-21 06:18:05 +00:00
|
|
|
((ufs2_daddr_t *)(ibp->b_data)) [indiroff] == BLK_NOCOPY)
|
2001-12-14 00:15:06 +00:00
|
|
|
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
|
2002-06-21 06:18:05 +00:00
|
|
|
* if the other snapshot holding them is freed. This code
|
|
|
|
* is reproduced once each for UFS1 and UFS2.
|
2001-05-04 05:49:28 +00:00
|
|
|
*/
|
|
|
|
static int
|
2002-06-21 06:18:05 +00:00
|
|
|
expunge_ufs1(snapvp, cancelip, fs, acctfunc, expungetype)
|
2002-02-02 01:42:44 +00:00
|
|
|
struct vnode *snapvp;
|
|
|
|
struct inode *cancelip;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *fs;
|
2002-06-21 06:18:05 +00:00
|
|
|
int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
2002-02-02 01:42:44 +00:00
|
|
|
int expungetype;
|
2001-05-04 05:49:28 +00:00
|
|
|
{
|
2002-06-21 06:18:05 +00:00
|
|
|
int i, error, indiroff;
|
|
|
|
ufs_lbn_t lbn, rlbn;
|
|
|
|
ufs2_daddr_t len, blkno, numblks, blksperindir;
|
|
|
|
struct ufs1_dinode *dip;
|
2002-02-02 01:42:44 +00:00
|
|
|
struct thread *td = curthread;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
/*
|
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) {
|
2002-12-03 18:19:27 +00:00
|
|
|
blkno = VTOI(snapvp)->i_din1->di_db[lbn];
|
2002-02-02 01:42:44 +00:00
|
|
|
} else {
|
|
|
|
td->td_proc->p_flag |= P_COWINPROGRESS;
|
|
|
|
error = UFS_BALLOC(snapvp, lblktosize(fs, (off_t)lbn),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
|
2002-02-02 01:42:44 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
indiroff = (lbn - NDADDR) % NINDIR(fs);
|
2002-06-21 06:18:05 +00:00
|
|
|
blkno = ((ufs1_daddr_t *)(bp->b_data))[indiroff];
|
2002-02-02 01:42:44 +00:00
|
|
|
bqrelse(bp);
|
|
|
|
}
|
2002-12-03 18:19:27 +00:00
|
|
|
if (blkno != 0) {
|
|
|
|
if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
|
|
|
|
return (error);
|
|
|
|
} else {
|
|
|
|
error = UFS_BALLOC(snapvp, lblktosize(fs, (off_t)lbn),
|
|
|
|
fs->fs_bsize, KERNCRED, 0, &bp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if ((error = readblock(bp, lbn)) != 0)
|
|
|
|
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.
|
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
dip = (struct ufs1_dinode *)bp->b_data +
|
|
|
|
ino_to_fsbo(fs, cancelip->i_number);
|
2002-02-02 01:42:44 +00:00
|
|
|
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;
|
2002-06-21 06:18:05 +00:00
|
|
|
bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs1_daddr_t));
|
2001-05-04 05:49:28 +00:00
|
|
|
bdwrite(bp);
|
2002-12-14 01:36:59 +00:00
|
|
|
/*
|
|
|
|
* Now go through and expunge all the blocks in the file
|
|
|
|
* using the function requested.
|
|
|
|
*/
|
|
|
|
numblks = howmany(cancelip->i_size, fs->fs_bsize);
|
|
|
|
if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_db[0],
|
|
|
|
&cancelip->i_din1->di_db[NDADDR], fs, 0, expungetype)))
|
|
|
|
return (error);
|
|
|
|
if ((error = (*acctfunc)(snapvp, &cancelip->i_din1->di_ib[0],
|
|
|
|
&cancelip->i_din1->di_ib[NIADDR], fs, -1, expungetype)))
|
|
|
|
return (error);
|
|
|
|
blksperindir = 1;
|
|
|
|
lbn = -NDADDR;
|
|
|
|
len = numblks - NDADDR;
|
|
|
|
rlbn = NDADDR;
|
|
|
|
for (i = 0; len > 0 && i < NIADDR; i++) {
|
|
|
|
error = indiracct_ufs1(snapvp, ITOV(cancelip), i,
|
|
|
|
cancelip->i_din1->di_ib[i], lbn, rlbn, len,
|
|
|
|
blksperindir, fs, acctfunc, expungetype);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
blksperindir *= NINDIR(fs);
|
|
|
|
lbn -= blksperindir + 1;
|
|
|
|
len -= blksperindir;
|
|
|
|
rlbn += blksperindir;
|
|
|
|
}
|
2001-05-04 05:49:28 +00:00
|
|
|
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
|
2002-06-21 06:18:05 +00:00
|
|
|
indiracct_ufs1(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
|
|
|
|
blksperindir, fs, acctfunc, expungetype)
|
2000-07-11 22:07:57 +00:00
|
|
|
struct vnode *snapvp;
|
|
|
|
struct vnode *cancelvp;
|
|
|
|
int level;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs1_daddr_t blkno;
|
|
|
|
ufs_lbn_t lbn;
|
|
|
|
ufs_lbn_t rlbn;
|
|
|
|
ufs_lbn_t remblks;
|
|
|
|
ufs_lbn_t blksperindir;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *fs;
|
2002-06-21 06:18:05 +00:00
|
|
|
int (*acctfunc)(struct vnode *, ufs1_daddr_t *, ufs1_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
2002-02-02 01:42:44 +00:00
|
|
|
int expungetype;
|
2000-07-11 22:07:57 +00:00
|
|
|
{
|
2002-06-21 06:18:05 +00:00
|
|
|
int error, num, i;
|
|
|
|
ufs_lbn_t subblksperindir;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct indir indirs[NIADDR + 2];
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs1_daddr_t last, *bap;
|
2000-07-11 22:07:57 +00:00
|
|
|
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);
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(bap, ufs1_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
|
2001-05-04 05:49:28 +00:00
|
|
|
bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
|
|
|
|
bqrelse(bp);
|
2002-12-14 01:36:59 +00:00
|
|
|
error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
|
|
|
|
level == 0 ? rlbn : -1, 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++) {
|
2002-06-21 06:18:05 +00:00
|
|
|
error = indiracct_ufs1(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
|
2002-06-21 06:18:05 +00:00
|
|
|
fullacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)
|
|
|
|
struct vnode *vp;
|
|
|
|
ufs1_daddr_t *oldblkp, *lastblkp;
|
|
|
|
struct fs *fs;
|
|
|
|
ufs_lbn_t lblkno;
|
|
|
|
int exptype; /* BLK_SNAP or BLK_NOCOPY */
|
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
|
|
|
if ((error = snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
|
|
|
|
return (error);
|
|
|
|
return (mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, exptype));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Identify a set of blocks allocated in a snapshot inode.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
snapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
|
2002-02-02 01:42:44 +00:00
|
|
|
struct vnode *vp;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs1_daddr_t *oldblkp, *lastblkp;
|
2002-02-02 01:42:44 +00:00
|
|
|
struct fs *fs;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs_lbn_t lblkno;
|
2002-02-02 01:42:44 +00:00
|
|
|
int expungetype; /* BLK_SNAP or BLK_NOCOPY */
|
2002-06-21 06:18:05 +00:00
|
|
|
{
|
|
|
|
struct inode *ip = VTOI(vp);
|
|
|
|
ufs1_daddr_t blkno, *blkp;
|
|
|
|
ufs_lbn_t lbn;
|
|
|
|
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_din1->di_db[lbn];
|
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
|
|
|
} else {
|
|
|
|
error = UFS_BALLOC(vp, lblktosize(fs, (off_t)lbn),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
|
2002-06-21 06:18:05 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
blkp = &((ufs1_daddr_t *)(ibp->b_data))
|
|
|
|
[(lbn - NDADDR) % NINDIR(fs)];
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* If we are expunging a snapshot vnode and we
|
|
|
|
* find a block marked BLK_NOCOPY, then it is
|
|
|
|
* one that has been allocated to this snapshot after
|
|
|
|
* we took our current snapshot and can be ignored.
|
|
|
|
*/
|
|
|
|
if (expungetype == BLK_SNAP && *blkp == BLK_NOCOPY) {
|
|
|
|
if (lbn >= NDADDR)
|
|
|
|
brelse(ibp);
|
|
|
|
} else {
|
|
|
|
if (*blkp != 0)
|
|
|
|
panic("snapacct: bad block");
|
|
|
|
*blkp = expungetype;
|
|
|
|
if (lbn >= NDADDR)
|
|
|
|
bdwrite(ibp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Account for a set of blocks allocated in a snapshot inode.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
mapacct_ufs1(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
|
|
|
|
struct vnode *vp;
|
|
|
|
ufs1_daddr_t *oldblkp, *lastblkp;
|
|
|
|
struct fs *fs;
|
|
|
|
ufs_lbn_t lblkno;
|
|
|
|
int expungetype;
|
|
|
|
{
|
|
|
|
ufs1_daddr_t blkno;
|
2002-10-09 06:13:48 +00:00
|
|
|
struct inode *ip;
|
2002-06-21 06:18:05 +00:00
|
|
|
ino_t inum;
|
2002-12-18 19:50:28 +00:00
|
|
|
int acctit;
|
2002-06-21 06:18:05 +00:00
|
|
|
|
2002-10-09 06:13:48 +00:00
|
|
|
ip = VTOI(vp);
|
|
|
|
inum = ip->i_number;
|
2002-12-18 19:50:28 +00:00
|
|
|
if (lblkno == -1)
|
|
|
|
acctit = 0;
|
|
|
|
else
|
|
|
|
acctit = 1;
|
2002-06-21 06:18:05 +00:00
|
|
|
for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
|
|
|
|
blkno = *oldblkp;
|
|
|
|
if (blkno == 0 || blkno == BLK_NOCOPY)
|
|
|
|
continue;
|
2002-12-18 19:50:28 +00:00
|
|
|
if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
|
2002-12-15 19:25:59 +00:00
|
|
|
*ip->i_snapblklist++ = lblkno;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (blkno == BLK_SNAP)
|
|
|
|
blkno = blkstofrags(fs, lblkno);
|
|
|
|
ffs_blkfree(fs, vp, blkno, fs->fs_bsize, inum);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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. This code
|
|
|
|
* is reproduced once each for UFS1 and UFS2.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
expunge_ufs2(snapvp, cancelip, fs, acctfunc, expungetype)
|
|
|
|
struct vnode *snapvp;
|
|
|
|
struct inode *cancelip;
|
|
|
|
struct fs *fs;
|
|
|
|
int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
|
|
|
int expungetype;
|
|
|
|
{
|
|
|
|
int i, error, indiroff;
|
|
|
|
ufs_lbn_t lbn, rlbn;
|
|
|
|
ufs2_daddr_t len, blkno, numblks, blksperindir;
|
|
|
|
struct ufs2_dinode *dip;
|
|
|
|
struct thread *td = curthread;
|
|
|
|
struct buf *bp;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare to expunge the inode. If its inode block has not
|
|
|
|
* yet been copied, then allocate and fill the copy.
|
|
|
|
*/
|
|
|
|
lbn = fragstoblks(fs, ino_to_fsba(fs, cancelip->i_number));
|
|
|
|
blkno = 0;
|
|
|
|
if (lbn < NDADDR) {
|
2002-12-03 18:19:27 +00:00
|
|
|
blkno = VTOI(snapvp)->i_din2->di_db[lbn];
|
2002-06-21 06:18:05 +00:00
|
|
|
} else {
|
|
|
|
td->td_proc->p_flag |= P_COWINPROGRESS;
|
|
|
|
error = UFS_BALLOC(snapvp, lblktosize(fs, (off_t)lbn),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &bp);
|
2002-06-21 06:18:05 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
indiroff = (lbn - NDADDR) % NINDIR(fs);
|
|
|
|
blkno = ((ufs2_daddr_t *)(bp->b_data))[indiroff];
|
|
|
|
bqrelse(bp);
|
|
|
|
}
|
2002-12-03 18:19:27 +00:00
|
|
|
if (blkno != 0) {
|
|
|
|
if ((error = bread(snapvp, lbn, fs->fs_bsize, KERNCRED, &bp)))
|
|
|
|
return (error);
|
|
|
|
} else {
|
|
|
|
error = UFS_BALLOC(snapvp, lblktosize(fs, (off_t)lbn),
|
|
|
|
fs->fs_bsize, KERNCRED, 0, &bp);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
if ((error = readblock(bp, lbn)) != 0)
|
|
|
|
return (error);
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
/*
|
|
|
|
* Set a snapshot inode to be a zero length file, regular files
|
|
|
|
* to be completely unallocated.
|
|
|
|
*/
|
|
|
|
dip = (struct ufs2_dinode *)bp->b_data +
|
|
|
|
ino_to_fsbo(fs, cancelip->i_number);
|
|
|
|
if (expungetype == BLK_NOCOPY)
|
|
|
|
dip->di_mode = 0;
|
|
|
|
dip->di_size = 0;
|
|
|
|
dip->di_blocks = 0;
|
|
|
|
dip->di_flags &= ~SF_SNAPSHOT;
|
|
|
|
bzero(&dip->di_db[0], (NDADDR + NIADDR) * sizeof(ufs2_daddr_t));
|
|
|
|
bdwrite(bp);
|
2002-12-14 01:36:59 +00:00
|
|
|
/*
|
|
|
|
* Now go through and expunge all the blocks in the file
|
|
|
|
* using the function requested.
|
|
|
|
*/
|
|
|
|
numblks = howmany(cancelip->i_size, fs->fs_bsize);
|
|
|
|
if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_db[0],
|
|
|
|
&cancelip->i_din2->di_db[NDADDR], fs, 0, expungetype)))
|
|
|
|
return (error);
|
|
|
|
if ((error = (*acctfunc)(snapvp, &cancelip->i_din2->di_ib[0],
|
|
|
|
&cancelip->i_din2->di_ib[NIADDR], fs, -1, expungetype)))
|
|
|
|
return (error);
|
|
|
|
blksperindir = 1;
|
|
|
|
lbn = -NDADDR;
|
|
|
|
len = numblks - NDADDR;
|
|
|
|
rlbn = NDADDR;
|
|
|
|
for (i = 0; len > 0 && i < NIADDR; i++) {
|
|
|
|
error = indiracct_ufs2(snapvp, ITOV(cancelip), i,
|
|
|
|
cancelip->i_din2->di_ib[i], lbn, rlbn, len,
|
|
|
|
blksperindir, fs, acctfunc, expungetype);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
blksperindir *= NINDIR(fs);
|
|
|
|
lbn -= blksperindir + 1;
|
|
|
|
len -= blksperindir;
|
|
|
|
rlbn += blksperindir;
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Descend an indirect block chain for vnode cancelvp accounting for all
|
|
|
|
* its indirect blocks in snapvp.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
indiracct_ufs2(snapvp, cancelvp, level, blkno, lbn, rlbn, remblks,
|
|
|
|
blksperindir, fs, acctfunc, expungetype)
|
|
|
|
struct vnode *snapvp;
|
|
|
|
struct vnode *cancelvp;
|
|
|
|
int level;
|
|
|
|
ufs2_daddr_t blkno;
|
|
|
|
ufs_lbn_t lbn;
|
|
|
|
ufs_lbn_t rlbn;
|
|
|
|
ufs_lbn_t remblks;
|
|
|
|
ufs_lbn_t blksperindir;
|
|
|
|
struct fs *fs;
|
|
|
|
int (*acctfunc)(struct vnode *, ufs2_daddr_t *, ufs2_daddr_t *,
|
|
|
|
struct fs *, ufs_lbn_t, int);
|
|
|
|
int expungetype;
|
|
|
|
{
|
|
|
|
int error, num, i;
|
|
|
|
ufs_lbn_t subblksperindir;
|
|
|
|
struct indir indirs[NIADDR + 2];
|
|
|
|
ufs2_daddr_t last, *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);
|
2003-02-19 05:47:46 +00:00
|
|
|
MALLOC(bap, ufs2_daddr_t *, fs->fs_bsize, M_DEVBUF, M_WAITOK);
|
2002-06-21 06:18:05 +00:00
|
|
|
bcopy(bp->b_data, (caddr_t)bap, fs->fs_bsize);
|
|
|
|
bqrelse(bp);
|
2002-12-14 01:36:59 +00:00
|
|
|
error = (*acctfunc)(snapvp, &bap[0], &bap[last], fs,
|
|
|
|
level == 0 ? rlbn : -1, expungetype);
|
2002-06-21 06:18:05 +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_ufs2(snapvp, cancelvp, level, bap[i], lbn,
|
|
|
|
rlbn, remblks, subblksperindir, fs, acctfunc, expungetype);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
rlbn += blksperindir;
|
|
|
|
lbn -= blksperindir;
|
|
|
|
remblks -= blksperindir;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
FREE(bap, M_DEVBUF);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do both snap accounting and map accounting.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fullacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)
|
|
|
|
struct vnode *vp;
|
|
|
|
ufs2_daddr_t *oldblkp, *lastblkp;
|
|
|
|
struct fs *fs;
|
|
|
|
ufs_lbn_t lblkno;
|
|
|
|
int exptype; /* BLK_SNAP or BLK_NOCOPY */
|
2002-02-02 01:42:44 +00:00
|
|
|
{
|
|
|
|
int error;
|
|
|
|
|
2002-06-21 06:18:05 +00:00
|
|
|
if ((error = snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype)))
|
2002-02-02 01:42:44 +00:00
|
|
|
return (error);
|
2002-06-21 06:18:05 +00:00
|
|
|
return (mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, exptype));
|
2002-02-02 01:42:44 +00:00
|
|
|
}
|
|
|
|
|
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-06-21 06:18:05 +00:00
|
|
|
snapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
|
2000-07-11 22:07:57 +00:00
|
|
|
struct vnode *vp;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t *oldblkp, *lastblkp;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *fs;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs_lbn_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);
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t blkno, *blkp;
|
|
|
|
ufs_lbn_t lbn;
|
2000-07-11 22:07:57 +00:00
|
|
|
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) {
|
2002-06-21 06:18:05 +00:00
|
|
|
blkp = &ip->i_din2->di_db[lbn];
|
2000-07-11 22:07:57 +00:00
|
|
|
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),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
return (error);
|
2002-06-21 06:18:05 +00:00
|
|
|
blkp = &((ufs2_daddr_t *)(ibp->b_data))
|
2000-07-11 22:07:57 +00:00
|
|
|
[(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-06-21 06:18:05 +00:00
|
|
|
mapacct_ufs2(vp, oldblkp, lastblkp, fs, lblkno, expungetype)
|
2001-05-04 05:49:28 +00:00
|
|
|
struct vnode *vp;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t *oldblkp, *lastblkp;
|
2001-05-04 05:49:28 +00:00
|
|
|
struct fs *fs;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs_lbn_t lblkno;
|
2002-02-02 01:42:44 +00:00
|
|
|
int expungetype;
|
2001-05-04 05:49:28 +00:00
|
|
|
{
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t blkno;
|
2002-10-09 06:13:48 +00:00
|
|
|
struct inode *ip;
|
2002-02-02 01:42:44 +00:00
|
|
|
ino_t inum;
|
2002-12-18 19:50:28 +00:00
|
|
|
int acctit;
|
2001-05-04 05:49:28 +00:00
|
|
|
|
2002-10-09 06:13:48 +00:00
|
|
|
ip = VTOI(vp);
|
|
|
|
inum = ip->i_number;
|
2002-12-18 19:50:28 +00:00
|
|
|
if (lblkno == -1)
|
|
|
|
acctit = 0;
|
|
|
|
else
|
|
|
|
acctit = 1;
|
2001-05-04 05:49:28 +00:00
|
|
|
for ( ; oldblkp < lastblkp; oldblkp++, lblkno++) {
|
|
|
|
blkno = *oldblkp;
|
|
|
|
if (blkno == 0 || blkno == BLK_NOCOPY)
|
|
|
|
continue;
|
2002-12-18 19:50:28 +00:00
|
|
|
if (acctit && expungetype == BLK_SNAP && blkno != BLK_SNAP)
|
2002-12-15 19:25:59 +00:00
|
|
|
*ip->i_snapblklist++ = lblkno;
|
2001-05-04 05:49:28 +00:00
|
|
|
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;
|
2002-12-14 01:36:59 +00:00
|
|
|
if (xp != NULL)
|
|
|
|
vrele(ITOV(ip));
|
|
|
|
else if (snapdebug)
|
2000-12-19 04:41:09 +00:00
|
|
|
printf("ffs_snapgone: lost snapshot vnode %d\n",
|
|
|
|
ip->i_number);
|
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;
|
2002-10-16 00:19:23 +00:00
|
|
|
struct lock *lkp;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct buf *ibp;
|
|
|
|
struct fs *fs;
|
2002-10-16 00:19:23 +00:00
|
|
|
struct thread *td = curthread;
|
2002-12-14 01:36:59 +00:00
|
|
|
ufs2_daddr_t numblks, blkno, dblk, *snapblklist;
|
2002-06-21 06:18:05 +00:00
|
|
|
int error, loc, last;
|
2000-07-11 22:07:57 +00:00
|
|
|
|
|
|
|
ip = VTOI(vp);
|
|
|
|
fs = ip->i_fs;
|
2002-11-30 19:00:51 +00:00
|
|
|
devvp = ip->i_devvp;
|
2000-07-11 22:07:57 +00:00
|
|
|
/*
|
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) {
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
|
|
|
lockmgr(&vp->v_lock, LK_INTERLOCK | LK_EXCLUSIVE,
|
|
|
|
VI_MTX(devvp), td);
|
|
|
|
VI_LOCK(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;
|
2002-11-30 19:00:51 +00:00
|
|
|
lkp = vp->v_vnlock;
|
|
|
|
vp->v_vnlock = &vp->v_lock;
|
|
|
|
lockmgr(lkp, LK_RELEASE, NULL, td);
|
|
|
|
if (TAILQ_FIRST(&devvp->v_rdev->si_snapshots) != 0) {
|
|
|
|
VI_UNLOCK(devvp);
|
|
|
|
} else {
|
2002-12-14 01:36:59 +00:00
|
|
|
snapblklist = devvp->v_rdev->si_snapblklist;
|
|
|
|
devvp->v_rdev->si_snapblklist = 0;
|
|
|
|
devvp->v_rdev->si_snaplistsize = 0;
|
2001-03-07 07:09:55 +00:00
|
|
|
devvp->v_rdev->si_copyonwrite = 0;
|
2002-08-04 10:29:36 +00:00
|
|
|
devvp->v_vflag &= ~VV_COPYONWRITE;
|
2002-11-30 19:00:51 +00:00
|
|
|
lockmgr(lkp, LK_DRAIN|LK_INTERLOCK, VI_MTX(devvp), td);
|
|
|
|
lockmgr(lkp, LK_RELEASE, NULL, td);
|
|
|
|
lockdestroy(lkp);
|
|
|
|
FREE(lkp, M_UFSMNT);
|
2002-12-14 01:36:59 +00:00
|
|
|
FREE(snapblklist, M_UFSMNT);
|
2001-03-07 07:09:55 +00:00
|
|
|
}
|
|
|
|
}
|
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++) {
|
2002-06-21 06:18:05 +00:00
|
|
|
dblk = DIP(ip, i_db[blkno]);
|
2001-05-08 07:29:03 +00:00
|
|
|
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(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))) {
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_blocks) -= btodb(fs->fs_bsize);
|
|
|
|
DIP(ip, i_db[blkno]) = 0;
|
2001-05-08 07:29:03 +00:00
|
|
|
}
|
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),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
continue;
|
2002-06-21 06:18:05 +00:00
|
|
|
if (fs->fs_size - blkno > NINDIR(fs))
|
2000-07-11 22:07:57 +00:00
|
|
|
last = NINDIR(fs);
|
2002-06-21 06:18:05 +00:00
|
|
|
else
|
|
|
|
last = fs->fs_size - blkno;
|
2000-07-11 22:07:57 +00:00
|
|
|
for (loc = 0; loc < last; loc++) {
|
2002-06-21 06:18:05 +00:00
|
|
|
if (ip->i_ump->um_fstype == UFS1) {
|
|
|
|
dblk = ((ufs1_daddr_t *)(ibp->b_data))[loc];
|
|
|
|
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
|
|
|
|
((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
|
|
|
|
else if ((dblk == blkstofrags(fs, blkno) &&
|
|
|
|
ffs_snapblkfree(fs, ip->i_devvp, dblk,
|
|
|
|
fs->fs_bsize, ip->i_number))) {
|
|
|
|
ip->i_din1->di_blocks -=
|
|
|
|
btodb(fs->fs_bsize);
|
|
|
|
((ufs1_daddr_t *)(ibp->b_data))[loc]= 0;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
dblk = ((ufs2_daddr_t *)(ibp->b_data))[loc];
|
2001-05-08 07:29:03 +00:00
|
|
|
if (dblk == BLK_NOCOPY || dblk == BLK_SNAP)
|
2002-06-21 06:18:05 +00:00
|
|
|
((ufs2_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))) {
|
2002-06-21 06:18:05 +00:00
|
|
|
ip->i_din2->di_blocks -= btodb(fs->fs_bsize);
|
|
|
|
((ufs2_daddr_t *)(ibp->b_data))[loc] = 0;
|
2001-05-08 07:29:03 +00:00
|
|
|
}
|
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;
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_flags) = ip->i_flags;
|
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;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t bno;
|
2000-07-11 22:07:57 +00:00
|
|
|
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;
|
2002-11-30 19:00:51 +00:00
|
|
|
struct vnode *vp = NULL;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs_lbn_t lbn;
|
|
|
|
ufs2_daddr_t blkno;
|
2002-11-30 19:00:51 +00:00
|
|
|
int indiroff = 0, snapshot_locked = 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-11-30 19:00:51 +00:00
|
|
|
retry:
|
|
|
|
VI_LOCK(devvp);
|
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) {
|
2002-06-21 06:18:05 +00:00
|
|
|
blkno = DIP(ip, i_db[lbn]);
|
2000-07-11 22:07:57 +00:00
|
|
|
} else {
|
2002-11-30 19:00:51 +00:00
|
|
|
if (snapshot_locked == 0 &&
|
2002-12-14 01:36:59 +00:00
|
|
|
lockmgr(vp->v_vnlock,
|
2002-11-30 19:00:51 +00:00
|
|
|
LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
|
|
|
|
VI_MTX(devvp), td) != 0)
|
|
|
|
goto retry;
|
2002-12-14 01:36:59 +00:00
|
|
|
snapshot_locked = 1;
|
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),
|
2002-06-23 06:12:22 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
2000-07-11 22:07:57 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
indiroff = (lbn - NDADDR) % NINDIR(fs);
|
2002-06-21 06:18:05 +00:00
|
|
|
if (ip->i_ump->um_fstype == UFS1)
|
|
|
|
blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
|
|
|
|
else
|
|
|
|
blkno=((ufs2_daddr_t *)(ibp->b_data))[indiroff];
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Check to see if block needs to be copied.
|
|
|
|
*/
|
2002-06-21 06:18:05 +00:00
|
|
|
if (blkno == 0) {
|
|
|
|
/*
|
|
|
|
* A block that we map is being freed. If it has not
|
|
|
|
* been claimed yet, we will claim or copy it (below).
|
|
|
|
*/
|
|
|
|
claimedblk = 1;
|
|
|
|
} else if (blkno == BLK_SNAP) {
|
|
|
|
/*
|
|
|
|
* No previous snapshot claimed the block,
|
2002-11-30 19:00:51 +00:00
|
|
|
* so it will be freed and become a BLK_NOCOPY
|
2002-06-21 06:18:05 +00:00
|
|
|
* (don't care) for us.
|
|
|
|
*/
|
2000-07-11 22:07:57 +00:00
|
|
|
if (claimedblk)
|
|
|
|
panic("snapblkfree: inconsistent block type");
|
2002-11-30 19:00:51 +00:00
|
|
|
if (snapshot_locked == 0 &&
|
|
|
|
lockmgr(vp->v_vnlock,
|
|
|
|
LK_INTERLOCK | LK_EXCLUSIVE | LK_NOWAIT,
|
|
|
|
VI_MTX(devvp), td) != 0) {
|
|
|
|
if (lbn >= NDADDR)
|
|
|
|
bqrelse(ibp);
|
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_SLEEPFAIL, td);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
snapshot_locked = 1;
|
2000-07-11 22:07:57 +00:00
|
|
|
if (lbn < NDADDR) {
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_db[lbn]) = BLK_NOCOPY;
|
2000-07-11 22:07:57 +00:00
|
|
|
ip->i_flag |= IN_CHANGE | IN_UPDATE;
|
2002-06-21 06:18:05 +00:00
|
|
|
} else if (ip->i_ump->um_fstype == UFS1) {
|
|
|
|
((ufs1_daddr_t *)(ibp->b_data))[indiroff] =
|
|
|
|
BLK_NOCOPY;
|
|
|
|
bdwrite(ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
} else {
|
2002-06-21 06:18:05 +00:00
|
|
|
((ufs2_daddr_t *)(ibp->b_data))[indiroff] =
|
2000-07-11 22:07:57 +00:00
|
|
|
BLK_NOCOPY;
|
|
|
|
bdwrite(ibp);
|
|
|
|
}
|
|
|
|
continue;
|
2002-06-21 06:18:05 +00:00
|
|
|
} else /* BLK_NOCOPY or default */ {
|
|
|
|
/*
|
|
|
|
* If the snapshot has already copied the block
|
|
|
|
* (default), or does not care about the block,
|
|
|
|
* it is not needed.
|
|
|
|
*/
|
|
|
|
if (lbn >= NDADDR)
|
|
|
|
bqrelse(ibp);
|
|
|
|
continue;
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
if (snapshot_locked == 0 &&
|
|
|
|
lockmgr(vp->v_vnlock,
|
|
|
|
LK_INTERLOCK | LK_EXCLUSIVE | LK_NOWAIT,
|
|
|
|
VI_MTX(devvp), td) != 0) {
|
|
|
|
if (lbn >= NDADDR)
|
|
|
|
bqrelse(ibp);
|
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_SLEEPFAIL, td);
|
|
|
|
goto retry;
|
|
|
|
}
|
|
|
|
snapshot_locked = 1;
|
2000-07-11 22:07:57 +00:00
|
|
|
if (size == fs->fs_bsize) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (snapdebug)
|
2002-06-23 18:17:27 +00:00
|
|
|
printf("%s %d lbn %jd from inum %d\n",
|
2002-06-21 06:18:05 +00:00
|
|
|
"Grabonremove: snapino", ip->i_number,
|
|
|
|
(intmax_t)lbn, inum);
|
2000-07-11 22:07:57 +00:00
|
|
|
#endif
|
|
|
|
if (lbn < NDADDR) {
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_db[lbn]) = bno;
|
|
|
|
} else if (ip->i_ump->um_fstype == UFS1) {
|
|
|
|
((ufs1_daddr_t *)(ibp->b_data))[indiroff] = bno;
|
|
|
|
bdwrite(ibp);
|
2000-07-11 22:07:57 +00:00
|
|
|
} else {
|
2002-06-21 06:18:05 +00:00
|
|
|
((ufs2_daddr_t *)(ibp->b_data))[indiroff] = bno;
|
2000-07-11 22:07:57 +00:00
|
|
|
bdwrite(ibp);
|
|
|
|
}
|
2002-06-21 06:18:05 +00:00
|
|
|
DIP(ip, i_blocks) += btodb(size);
|
2000-07-11 22:07:57 +00:00
|
|
|
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
|
|
|
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;
|
2002-11-30 19:00:51 +00:00
|
|
|
if (error)
|
2000-07-11 22:07:57 +00:00
|
|
|
break;
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (snapdebug)
|
2002-06-23 18:17:27 +00:00
|
|
|
printf("%s%d lbn %jd %s %d size %ld to blkno %jd\n",
|
2002-06-21 06:18:05 +00:00
|
|
|
"Copyonremove: snapino ", ip->i_number,
|
|
|
|
(intmax_t)lbn, "for inum", inum, size,
|
|
|
|
(intmax_t)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);
|
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);
|
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-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);
|
2002-11-30 19:00:51 +00:00
|
|
|
if (dopersistence && VTOI(vp)->i_effnlink > 0)
|
2001-09-12 08:38:13 +00:00
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
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.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
if (snapshot_locked)
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
|
|
|
else
|
|
|
|
VI_UNLOCK(devvp);
|
2000-07-11 22:07:57 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Associate snapshot files when mounting.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ffs_snapshot_mount(mp)
|
|
|
|
struct mount *mp;
|
|
|
|
{
|
|
|
|
struct ufsmount *ump = VFSTOUFS(mp);
|
2002-11-30 19:00:51 +00:00
|
|
|
struct vnode *devvp = ump->um_devvp;
|
2000-07-11 22:07:57 +00:00
|
|
|
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;
|
2002-10-16 00:19:23 +00:00
|
|
|
struct inode *ip, *xp;
|
2002-10-09 06:13:48 +00:00
|
|
|
struct uio auio;
|
|
|
|
struct iovec aiov;
|
2002-12-14 01:36:59 +00:00
|
|
|
void *snapblklist;
|
2002-10-09 06:13:48 +00:00
|
|
|
char *reason;
|
2002-12-14 01:36:59 +00:00
|
|
|
daddr_t snaplistsize;
|
2000-07-11 22:07:57 +00:00
|
|
|
int error, snaploc, loc;
|
|
|
|
|
2002-10-09 06:13:48 +00:00
|
|
|
/*
|
|
|
|
* XXX The following needs to be set before UFS_TRUNCATE or
|
|
|
|
* VOP_READ can be called.
|
|
|
|
*/
|
|
|
|
mp->mnt_stat.f_iosize = fs->fs_bsize;
|
|
|
|
/*
|
|
|
|
* Process each snapshot listed in the superblock.
|
|
|
|
*/
|
2002-12-14 01:36:59 +00:00
|
|
|
vp = NULL;
|
2002-11-30 19:00:51 +00:00
|
|
|
snaphead = &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)
|
2002-12-14 01:36:59 +00:00
|
|
|
break;
|
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);
|
2002-10-09 06:13:48 +00:00
|
|
|
if ((ip->i_flags & SF_SNAPSHOT) == 0 || ip->i_size ==
|
|
|
|
lblktosize(fs, howmany(fs->fs_size, fs->fs_frag))) {
|
|
|
|
if ((ip->i_flags & SF_SNAPSHOT) == 0) {
|
|
|
|
reason = "non-snapshot";
|
|
|
|
} else {
|
|
|
|
reason = "old format snapshot";
|
|
|
|
(void)UFS_TRUNCATE(vp, (off_t)0, 0, NOCRED, td);
|
|
|
|
(void)VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
|
|
|
}
|
|
|
|
printf("ffs_snapshot_mount: %s inode %d\n",
|
|
|
|
reason, fs->fs_snapinum[snaploc]);
|
2000-07-11 22:07:57 +00:00
|
|
|
vput(vp);
|
2002-12-14 01:36:59 +00:00
|
|
|
vp = NULL;
|
2000-07-11 22:07:57 +00:00
|
|
|
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;
|
|
|
|
}
|
2002-10-16 00:19:23 +00:00
|
|
|
/*
|
|
|
|
* If there already exist snapshots on this filesystem, grab a
|
|
|
|
* reference to their shared lock. If this is the first snapshot
|
|
|
|
* on this filesystem, we need to allocate a lock for the
|
|
|
|
* snapshots to share. In either case, acquire the snapshot
|
|
|
|
* lock and give up our original private lock.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
2002-10-16 00:19:23 +00:00
|
|
|
if ((xp = TAILQ_FIRST(snaphead)) != NULL) {
|
|
|
|
VI_LOCK(vp);
|
|
|
|
vp->v_vnlock = ITOV(xp)->v_vnlock;
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_UNLOCK(devvp);
|
2002-10-16 00:19:23 +00:00
|
|
|
} else {
|
|
|
|
struct lock *lkp;
|
|
|
|
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_UNLOCK(devvp);
|
2002-10-16 00:19:23 +00:00
|
|
|
MALLOC(lkp, struct lock *, sizeof(struct lock),
|
2003-02-19 05:47:46 +00:00
|
|
|
M_UFSMNT, M_WAITOK);
|
2002-10-16 00:19:23 +00:00
|
|
|
lockinit(lkp, PVFS, "snaplk", VLKTIMEOUT,
|
|
|
|
LK_CANRECURSE | LK_NOPAUSE);
|
|
|
|
VI_LOCK(vp);
|
|
|
|
vp->v_vnlock = lkp;
|
|
|
|
}
|
|
|
|
vn_lock(vp, LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY, td);
|
2002-11-30 19:00:51 +00:00
|
|
|
transferlockers(&vp->v_lock, vp->v_vnlock);
|
|
|
|
lockmgr(&vp->v_lock, LK_RELEASE, NULL, td);
|
2002-10-09 06:13:48 +00:00
|
|
|
/*
|
|
|
|
* Link it onto the active snapshot list.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
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);
|
2002-08-04 10:29:36 +00:00
|
|
|
vp->v_vflag |= VV_SYSTEM;
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_UNLOCK(devvp);
|
2001-09-12 08:38:13 +00:00
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
2002-12-14 01:36:59 +00:00
|
|
|
/*
|
|
|
|
* No usable snapshots found.
|
|
|
|
*/
|
|
|
|
if (vp == NULL)
|
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* Allocate the space for the block hints list. We always want to
|
|
|
|
* use the list from the newest snapshot.
|
|
|
|
*/
|
|
|
|
auio.uio_iov = &aiov;
|
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
aiov.iov_base = (void *)&snaplistsize;
|
|
|
|
aiov.iov_len = sizeof(snaplistsize);
|
|
|
|
auio.uio_resid = aiov.iov_len;
|
|
|
|
auio.uio_offset =
|
|
|
|
lblktosize(fs, howmany(fs->fs_size, fs->fs_frag));
|
|
|
|
auio.uio_segflg = UIO_SYSSPACE;
|
|
|
|
auio.uio_rw = UIO_READ;
|
|
|
|
auio.uio_td = td;
|
|
|
|
vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
|
|
|
|
if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
|
|
|
|
printf("ffs_snapshot_mount: read_1 failed %d\n", error);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MALLOC(snapblklist, void *, snaplistsize * sizeof(daddr_t),
|
2003-02-19 05:47:46 +00:00
|
|
|
M_UFSMNT, M_WAITOK);
|
2002-12-14 01:36:59 +00:00
|
|
|
auio.uio_iovcnt = 1;
|
|
|
|
aiov.iov_base = snapblklist;
|
|
|
|
aiov.iov_len = snaplistsize * sizeof (daddr_t);
|
|
|
|
auio.uio_resid = aiov.iov_len;
|
|
|
|
auio.uio_offset -= sizeof(snaplistsize);
|
|
|
|
if ((error = VOP_READ(vp, &auio, IO_UNIT, td->td_ucred)) != 0) {
|
|
|
|
printf("ffs_snapshot_mount: read_2 failed %d\n", error);
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
|
|
|
FREE(snapblklist, M_UFSMNT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
|
|
|
VI_LOCK(devvp);
|
|
|
|
ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_mount");
|
|
|
|
devvp->v_rdev->si_snaplistsize = snaplistsize;
|
|
|
|
devvp->v_rdev->si_snapblklist = (daddr_t *)snapblklist;
|
|
|
|
devvp->v_rdev->si_copyonwrite = ffs_copyonwrite;
|
|
|
|
devvp->v_vflag |= VV_COPYONWRITE;
|
|
|
|
VI_UNLOCK(devvp);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Disassociate snapshot files when unmounting.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ffs_snapshot_unmount(mp)
|
|
|
|
struct mount *mp;
|
|
|
|
{
|
2002-11-30 19:00:51 +00:00
|
|
|
struct vnode *devvp = VFSTOUFS(mp)->um_devvp;
|
|
|
|
struct snaphead *snaphead = &devvp->v_rdev->si_snapshots;
|
2002-10-16 00:19:23 +00:00
|
|
|
struct lock *lkp = NULL;
|
2000-07-11 22:07:57 +00:00
|
|
|
struct inode *xp;
|
2002-10-16 00:19:23 +00:00
|
|
|
struct vnode *vp;
|
2000-07-11 22:07:57 +00:00
|
|
|
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
2001-03-07 07:09:55 +00:00
|
|
|
while ((xp = TAILQ_FIRST(snaphead)) != 0) {
|
2002-10-16 00:19:23 +00:00
|
|
|
vp = ITOV(xp);
|
|
|
|
lkp = vp->v_vnlock;
|
|
|
|
vp->v_vnlock = &vp->v_lock;
|
2001-03-07 07:09:55 +00:00
|
|
|
TAILQ_REMOVE(snaphead, xp, i_nextsnap);
|
|
|
|
xp->i_nextsnap.tqe_prev = 0;
|
2002-11-30 19:00:51 +00:00
|
|
|
if (xp->i_effnlink > 0) {
|
|
|
|
VI_UNLOCK(devvp);
|
2002-10-16 00:19:23 +00:00
|
|
|
vrele(vp);
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
|
|
|
}
|
2002-10-16 00:19:23 +00:00
|
|
|
}
|
2002-12-14 01:36:59 +00:00
|
|
|
if (devvp->v_rdev->si_snapblklist != NULL) {
|
|
|
|
FREE(devvp->v_rdev->si_snapblklist, M_UFSMNT);
|
|
|
|
devvp->v_rdev->si_snapblklist = NULL;
|
|
|
|
devvp->v_rdev->si_snaplistsize = 0;
|
|
|
|
}
|
2002-10-16 00:19:23 +00:00
|
|
|
if (lkp != NULL) {
|
|
|
|
lockdestroy(lkp);
|
|
|
|
FREE(lkp, M_UFSMNT);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
2002-11-30 19:00:51 +00:00
|
|
|
ASSERT_VOP_LOCKED(devvp, "ffs_snapshot_unmount");
|
|
|
|
devvp->v_rdev->si_copyonwrite = 0;
|
|
|
|
devvp->v_vflag &= ~VV_COPYONWRITE;
|
|
|
|
VI_UNLOCK(devvp);
|
2000-07-11 22:07:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
{
|
2002-10-16 00:19:23 +00:00
|
|
|
struct snaphead *snaphead;
|
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;
|
2002-10-22 01:23:00 +00:00
|
|
|
struct vnode *vp = 0;
|
2002-12-14 01:36:59 +00:00
|
|
|
ufs2_daddr_t lbn, blkno, *snapblklist;
|
2002-10-22 01:23:00 +00:00
|
|
|
int lower, upper, mid, indiroff, snapshot_locked = 0, error = 0;
|
2000-07-11 22:07:57 +00:00
|
|
|
|
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");
|
2002-12-14 01:36:59 +00:00
|
|
|
/*
|
|
|
|
* First check to see if it is in the preallocated list.
|
|
|
|
* By doing this check we avoid several potential deadlocks.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
VI_LOCK(devvp);
|
2002-10-16 00:19:23 +00:00
|
|
|
snaphead = &devvp->v_rdev->si_snapshots;
|
|
|
|
ip = TAILQ_FIRST(snaphead);
|
|
|
|
fs = ip->i_fs;
|
|
|
|
lbn = fragstoblks(fs, dbtofsb(fs, bp->b_blkno));
|
2002-12-14 01:36:59 +00:00
|
|
|
snapblklist = devvp->v_rdev->si_snapblklist;
|
|
|
|
upper = devvp->v_rdev->si_snaplistsize - 1;
|
|
|
|
lower = 1;
|
|
|
|
while (lower <= upper) {
|
|
|
|
mid = (lower + upper) / 2;
|
|
|
|
if (snapblklist[mid] == lbn)
|
|
|
|
break;
|
|
|
|
if (snapblklist[mid] < lbn)
|
|
|
|
lower = mid + 1;
|
|
|
|
else
|
|
|
|
upper = mid - 1;
|
|
|
|
}
|
|
|
|
if (lower <= upper) {
|
|
|
|
VI_UNLOCK(devvp);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Not in the precomputed list, so check the snapshots.
|
|
|
|
*/
|
2002-11-30 19:00:51 +00:00
|
|
|
retry:
|
2002-10-16 00:19:23 +00:00
|
|
|
TAILQ_FOREACH(ip, snaphead, 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;
|
|
|
|
/*
|
2002-10-22 01:23:00 +00:00
|
|
|
* Check to see if block needs to be copied. We do not have
|
|
|
|
* to hold the snapshot lock while doing this lookup as it
|
|
|
|
* will never require any additional allocations for the
|
|
|
|
* snapshot inode.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
|
|
|
if (lbn < NDADDR) {
|
2002-06-21 06:18:05 +00:00
|
|
|
blkno = DIP(ip, i_db[lbn]);
|
2000-07-11 22:07:57 +00:00
|
|
|
} else {
|
2002-11-30 19:00:51 +00:00
|
|
|
if (snapshot_locked == 0 &&
|
2002-12-14 01:36:59 +00:00
|
|
|
lockmgr(vp->v_vnlock,
|
2002-11-30 19:00:51 +00:00
|
|
|
LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
|
|
|
|
VI_MTX(devvp), td) != 0) {
|
|
|
|
VI_LOCK(devvp);
|
|
|
|
goto retry;
|
|
|
|
}
|
2002-12-14 01:36:59 +00:00
|
|
|
snapshot_locked = 1;
|
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),
|
2002-10-16 00:19:23 +00:00
|
|
|
fs->fs_bsize, KERNCRED, BA_METAONLY, &ibp);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
2002-10-16 00:19:23 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2000-07-11 22:07:57 +00:00
|
|
|
indiroff = (lbn - NDADDR) % NINDIR(fs);
|
2002-06-21 06:18:05 +00:00
|
|
|
if (ip->i_ump->um_fstype == UFS1)
|
|
|
|
blkno=((ufs1_daddr_t *)(ibp->b_data))[indiroff];
|
|
|
|
else
|
|
|
|
blkno=((ufs2_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
|
2002-10-16 00:19:23 +00:00
|
|
|
if (blkno != 0)
|
2000-07-11 22:07:57 +00:00
|
|
|
continue;
|
|
|
|
/*
|
2002-10-22 01:23:00 +00:00
|
|
|
* Allocate the block into which to do the copy. Since
|
|
|
|
* multiple processes may all try to copy the same block,
|
|
|
|
* we have to recheck our need to do a copy if we sleep
|
|
|
|
* waiting for the lock.
|
|
|
|
*
|
|
|
|
* Because all snapshots on a filesystem share a single
|
|
|
|
* lock, we ensure that we will never be in competition
|
|
|
|
* with another process to allocate a block.
|
2000-07-11 22:07:57 +00:00
|
|
|
*/
|
2002-10-22 01:23:00 +00:00
|
|
|
if (snapshot_locked == 0 &&
|
2002-11-30 19:00:51 +00:00
|
|
|
lockmgr(vp->v_vnlock,
|
|
|
|
LK_INTERLOCK | LK_EXCLUSIVE | LK_SLEEPFAIL,
|
|
|
|
VI_MTX(devvp), td) != 0) {
|
|
|
|
VI_LOCK(devvp);
|
2002-10-22 01:23:00 +00:00
|
|
|
goto retry;
|
2002-11-30 19:00:51 +00:00
|
|
|
}
|
2002-10-22 01:23:00 +00:00
|
|
|
snapshot_locked = 1;
|
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),
|
2002-10-16 00:19:23 +00:00
|
|
|
fs->fs_bsize, KERNCRED, 0, &cbp);
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_proc->p_flag &= ~P_COWINPROGRESS;
|
2002-10-16 00:19:23 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
2000-07-11 22:07:57 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (snapdebug) {
|
2002-06-23 18:17:27 +00:00
|
|
|
printf("Copyonwrite: snapino %d lbn %jd for ",
|
2002-06-21 06:18:05 +00:00
|
|
|
ip->i_number, (intmax_t)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-06-23 18:17:27 +00:00
|
|
|
printf(" lblkno %jd to blkno %jd\n",
|
2002-06-21 06:18:05 +00:00
|
|
|
(intmax_t)bp->b_lblkno, (intmax_t)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);
|
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);
|
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-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);
|
2002-10-16 00:19:23 +00:00
|
|
|
if (dopersistence && VTOI(vp)->i_effnlink > 0)
|
2001-09-12 08:38:13 +00:00
|
|
|
(void) VOP_FSYNC(vp, KERNCRED, MNT_WAIT, td);
|
2001-04-25 08:11:18 +00:00
|
|
|
}
|
2002-10-22 01:23:00 +00:00
|
|
|
if (snapshot_locked)
|
|
|
|
VOP_UNLOCK(vp, 0, td);
|
2002-11-30 19:00:51 +00:00
|
|
|
else
|
|
|
|
VI_UNLOCK(devvp);
|
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;
|
2002-06-21 06:18:05 +00:00
|
|
|
ufs2_daddr_t lbn;
|
2000-07-11 22:07:57 +00:00
|
|
|
{
|
|
|
|
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));
|
|
|
|
}
|