2004-06-20 22:55:58 +04:00
|
|
|
/* $NetBSD: fss.c,v 1.9 2004/06/20 18:55:58 hannken Exp $ */
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Juergen Hannken-Illjes.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``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 THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* File system snapshot disk driver.
|
|
|
|
*
|
|
|
|
* Block/character interface to the snapshot of a mounted file system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
2004-06-20 22:55:58 +04:00
|
|
|
__KERNEL_RCSID(0, "$NetBSD: fss.c,v 1.9 2004/06/20 18:55:58 hannken Exp $");
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
#include "fss.h"
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
|
|
|
#include <sys/namei.h>
|
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/errno.h>
|
|
|
|
#include <sys/buf.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/disklabel.h>
|
|
|
|
#include <sys/device.h>
|
|
|
|
#include <sys/disk.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mount.h>
|
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/file.h>
|
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <sys/kthread.h>
|
|
|
|
|
|
|
|
#include <miscfs/specfs/specdev.h>
|
|
|
|
|
|
|
|
#include <dev/fssvar.h>
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
#include <machine/stdarg.h>
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
#define FSS_STATISTICS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef FSS_STATISTICS
|
|
|
|
struct fss_stat {
|
|
|
|
u_int64_t cow_calls;
|
|
|
|
u_int64_t cow_copied;
|
|
|
|
u_int64_t cow_cache_full;
|
|
|
|
u_int64_t indir_read;
|
|
|
|
u_int64_t indir_write;
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct fss_stat fss_stat[NFSS];
|
|
|
|
|
|
|
|
#define FSS_STAT_INC(sc, field) \
|
|
|
|
do { \
|
|
|
|
fss_stat[sc->sc_unit].field++; \
|
|
|
|
} while (0)
|
|
|
|
#define FSS_STAT_SET(sc, field, value) \
|
|
|
|
do { \
|
|
|
|
fss_stat[sc->sc_unit].field = value; \
|
|
|
|
} while (0)
|
|
|
|
#define FSS_STAT_ADD(sc, field, value) \
|
|
|
|
do { \
|
|
|
|
fss_stat[sc->sc_unit].field += value; \
|
|
|
|
} while (0)
|
|
|
|
#define FSS_STAT_VAL(sc, field) fss_stat[sc->sc_unit].field
|
|
|
|
#define FSS_STAT_CLEAR(sc) \
|
|
|
|
do { \
|
|
|
|
memset(&fss_stat[sc->sc_unit], 0, \
|
|
|
|
sizeof(struct fss_stat)); \
|
|
|
|
} while (0)
|
|
|
|
#else /* FSS_STATISTICS */
|
|
|
|
#define FSS_STAT_INC(sc, field)
|
|
|
|
#define FSS_STAT_SET(sc, field, value)
|
|
|
|
#define FSS_STAT_ADD(sc, field, value)
|
|
|
|
#define FSS_STAT_CLEAR(sc)
|
|
|
|
#endif /* FSS_STATISTICS */
|
|
|
|
|
2004-02-14 03:00:56 +03:00
|
|
|
static struct fss_softc fss_softc[NFSS];
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
void fssattach(int);
|
|
|
|
|
|
|
|
dev_type_open(fss_open);
|
|
|
|
dev_type_close(fss_close);
|
|
|
|
dev_type_read(fss_read);
|
|
|
|
dev_type_write(fss_write);
|
|
|
|
dev_type_ioctl(fss_ioctl);
|
|
|
|
dev_type_strategy(fss_strategy);
|
|
|
|
dev_type_dump(fss_dump);
|
|
|
|
dev_type_size(fss_size);
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
static int fss_copy_on_write(void *, struct buf *);
|
2003-12-10 14:40:11 +03:00
|
|
|
static inline void fss_error(struct fss_softc *, const char *, ...);
|
2004-02-14 03:00:56 +03:00
|
|
|
static int fss_create_files(struct fss_softc *, struct fss_set *,
|
|
|
|
off_t *, struct proc *);
|
2003-12-10 14:40:11 +03:00
|
|
|
static int fss_create_snapshot(struct fss_softc *, struct fss_set *,
|
|
|
|
struct proc *);
|
|
|
|
static int fss_delete_snapshot(struct fss_softc *, struct proc *);
|
|
|
|
static int fss_softc_alloc(struct fss_softc *);
|
|
|
|
static void fss_softc_free(struct fss_softc *);
|
|
|
|
static void fss_cluster_iodone(struct buf *);
|
|
|
|
static void fss_read_cluster(struct fss_softc *, u_int32_t);
|
|
|
|
static int fss_write_cluster(struct fss_cache *, u_int32_t);
|
|
|
|
static void fss_bs_thread(void *);
|
2004-01-11 22:05:26 +03:00
|
|
|
static int fss_bmap(struct fss_softc *, off_t, int,
|
|
|
|
struct vnode **, daddr_t *, int *);
|
2003-12-10 14:40:11 +03:00
|
|
|
static int fss_bs_io(struct fss_softc *, fss_io_type,
|
2004-06-20 22:55:58 +04:00
|
|
|
u_int32_t, off_t, int, caddr_t);
|
2003-12-10 14:40:11 +03:00
|
|
|
static u_int32_t *fss_bs_indir(struct fss_softc *, u_int32_t);
|
|
|
|
|
|
|
|
const struct bdevsw fss_bdevsw = {
|
|
|
|
fss_open, fss_close, fss_strategy, fss_ioctl,
|
|
|
|
fss_dump, fss_size, D_DISK
|
|
|
|
};
|
|
|
|
|
|
|
|
const struct cdevsw fss_cdevsw = {
|
|
|
|
fss_open, fss_close, fss_read, fss_write, fss_ioctl,
|
|
|
|
nostop, notty, nopoll, nommap, nokqfilter, D_DISK
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
fssattach(int num)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct fss_softc *sc;
|
|
|
|
|
|
|
|
for (i = 0; i < NFSS; i++) {
|
|
|
|
sc = &fss_softc[i];
|
|
|
|
sc->sc_unit = i;
|
|
|
|
sc->sc_bdev = NODEV;
|
2004-01-11 22:05:26 +03:00
|
|
|
simple_lock_init(&sc->sc_slock);
|
2003-12-10 14:40:11 +03:00
|
|
|
bufq_alloc(&sc->sc_bufq, BUFQ_FCFS|BUFQ_SORT_RAWBLOCK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fss_open(dev_t dev, int flags, int mode, struct proc *p)
|
|
|
|
{
|
|
|
|
struct fss_softc *sc;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
|
|
|
|
return ENODEV;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fss_close(dev_t dev, int flags, int mode, struct proc *p)
|
|
|
|
{
|
|
|
|
struct fss_softc *sc;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
|
|
|
|
return ENODEV;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
fss_strategy(struct buf *bp)
|
|
|
|
{
|
2004-01-11 22:05:26 +03:00
|
|
|
int s;
|
2003-12-10 14:40:11 +03:00
|
|
|
struct fss_softc *sc;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
sc = FSS_DEV_TO_SOFTC(bp->b_dev);
|
|
|
|
|
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if ((bp->b_flags & B_READ) != B_READ ||
|
2004-01-11 22:05:26 +03:00
|
|
|
sc == NULL || !FSS_ISVALID(sc)) {
|
|
|
|
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
|
|
|
bp->b_error = (sc == NULL ? ENODEV : EROFS);
|
2003-12-10 14:40:11 +03:00
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bp->b_rawblkno = bp->b_blkno;
|
|
|
|
BUFQ_PUT(&sc->sc_bufq, bp);
|
|
|
|
wakeup(&sc->sc_bs_proc);
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
FSS_UNLOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fss_read(dev_t dev, struct uio *uio, int flags)
|
|
|
|
{
|
|
|
|
return physio(fss_strategy, NULL, dev, B_READ, minphys, uio);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fss_write(dev_t dev, struct uio *uio, int flags)
|
|
|
|
{
|
|
|
|
return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fss_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
|
|
|
|
{
|
2004-01-11 22:05:26 +03:00
|
|
|
int s, error;
|
2003-12-10 14:40:11 +03:00
|
|
|
struct fss_softc *sc;
|
|
|
|
struct fss_set *fss = (struct fss_set *)data;
|
|
|
|
struct fss_get *fsg = (struct fss_get *)data;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
|
|
|
|
return ENODEV;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
while ((sc->sc_flags & FSS_EXCL) == FSS_EXCL) {
|
|
|
|
error = ltsleep(sc, PRIBIO|PCATCH, "fsslock", 0, &sc->sc_slock);
|
|
|
|
if (error) {
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sc->sc_flags |= FSS_EXCL;
|
|
|
|
FSS_UNLOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
error = EINVAL;
|
|
|
|
|
|
|
|
switch (cmd) {
|
|
|
|
case FSSIOCSET:
|
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
error = EPERM;
|
|
|
|
else if ((sc->sc_flags & FSS_ACTIVE) != 0)
|
|
|
|
error = EBUSY;
|
|
|
|
else
|
|
|
|
error = fss_create_snapshot(sc, fss, p);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FSSIOCCLR:
|
|
|
|
if ((flag & FWRITE) == 0)
|
|
|
|
error = EPERM;
|
|
|
|
else if ((sc->sc_flags & FSS_ACTIVE) == 0)
|
|
|
|
error = ENXIO;
|
|
|
|
else
|
|
|
|
error = fss_delete_snapshot(sc, p);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case FSSIOCGET:
|
2004-05-25 18:54:55 +04:00
|
|
|
switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) {
|
|
|
|
case FSS_ACTIVE:
|
2003-12-10 14:40:11 +03:00
|
|
|
memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
|
2004-01-11 22:05:26 +03:00
|
|
|
fsg->fsg_csize = FSS_CLSIZE(sc);
|
2003-12-10 14:40:11 +03:00
|
|
|
fsg->fsg_time = sc->sc_time;
|
|
|
|
fsg->fsg_mount_size = sc->sc_clcount;
|
|
|
|
fsg->fsg_bs_size = sc->sc_clnext;
|
|
|
|
error = 0;
|
2004-05-25 18:54:55 +04:00
|
|
|
break;
|
|
|
|
case FSS_PERSISTENT | FSS_ACTIVE:
|
|
|
|
memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
|
|
|
|
fsg->fsg_csize = 0;
|
|
|
|
fsg->fsg_time = sc->sc_time;
|
|
|
|
fsg->fsg_mount_size = 0;
|
|
|
|
fsg->fsg_bs_size = 0;
|
|
|
|
error = 0;
|
|
|
|
break;
|
|
|
|
default:
|
2003-12-10 14:40:11 +03:00
|
|
|
error = ENXIO;
|
2004-05-25 18:54:55 +04:00
|
|
|
break;
|
|
|
|
}
|
2003-12-10 14:40:11 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
sc->sc_flags &= ~FSS_EXCL;
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
wakeup(sc);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fss_size(dev_t dev)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fss_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
|
|
|
|
{
|
2004-01-11 22:05:26 +03:00
|
|
|
return EROFS;
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-02-24 18:12:51 +03:00
|
|
|
* An error occurred reading or writing the snapshot or backing store.
|
2003-12-10 14:40:11 +03:00
|
|
|
* If it is the first error log to console.
|
2004-01-11 22:05:26 +03:00
|
|
|
* The caller holds the simplelock.
|
2003-12-10 14:40:11 +03:00
|
|
|
*/
|
|
|
|
static inline void
|
|
|
|
fss_error(struct fss_softc *sc, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if ((sc->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE) {
|
|
|
|
va_start(ap, fmt);
|
|
|
|
printf("fss%d: snapshot invalid: ", sc->sc_unit);
|
|
|
|
vprintf(fmt, ap);
|
|
|
|
printf("\n");
|
|
|
|
va_end(ap);
|
|
|
|
}
|
|
|
|
if ((sc->sc_flags & FSS_ACTIVE) == FSS_ACTIVE)
|
|
|
|
sc->sc_flags |= FSS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate the variable sized parts of the softc and
|
|
|
|
* fork the kernel thread.
|
|
|
|
*
|
2004-01-11 22:05:26 +03:00
|
|
|
* The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size
|
2003-12-10 14:40:11 +03:00
|
|
|
* must be initialized.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fss_softc_alloc(struct fss_softc *sc)
|
|
|
|
{
|
|
|
|
int i, len, error;
|
|
|
|
|
|
|
|
len = (sc->sc_clcount+NBBY-1)/NBBY;
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_copied = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
|
|
|
|
if (sc->sc_copied == NULL)
|
|
|
|
return(ENOMEM);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
len = sc->sc_cache_size*sizeof(struct fss_cache);
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_cache = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
|
|
|
|
if (sc->sc_cache == NULL)
|
|
|
|
return(ENOMEM);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
len = FSS_CLSIZE(sc);
|
2003-12-10 14:40:11 +03:00
|
|
|
for (i = 0; i < sc->sc_cache_size; i++) {
|
|
|
|
sc->sc_cache[i].fc_type = FSS_CACHE_FREE;
|
|
|
|
sc->sc_cache[i].fc_softc = sc;
|
|
|
|
sc->sc_cache[i].fc_xfercount = 0;
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_cache[i].fc_data = malloc(len, M_TEMP,
|
|
|
|
M_WAITOK|M_CANFAIL);
|
|
|
|
if (sc->sc_cache[i].fc_data == NULL)
|
|
|
|
return(ENOMEM);
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
len = (sc->sc_indir_size+NBBY-1)/NBBY;
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_indir_valid = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
|
|
|
|
if (sc->sc_indir_valid == NULL)
|
|
|
|
return(ENOMEM);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
len = FSS_CLSIZE(sc);
|
|
|
|
sc->sc_indir_data = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
|
|
|
|
if (sc->sc_indir_data == NULL)
|
|
|
|
return(ENOMEM);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
if ((error = kthread_create1(fss_bs_thread, sc, &sc->sc_bs_proc,
|
|
|
|
"fssbs%d", sc->sc_unit)) != 0)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
sc->sc_flags |= FSS_BS_THREAD;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free the variable sized parts of the softc.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fss_softc_free(struct fss_softc *sc)
|
|
|
|
{
|
2004-01-11 22:05:26 +03:00
|
|
|
int s, i;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
if ((sc->sc_flags & FSS_BS_THREAD) != 0) {
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_flags &= ~FSS_BS_THREAD;
|
|
|
|
wakeup(&sc->sc_bs_proc);
|
|
|
|
while (sc->sc_bs_proc != NULL)
|
2004-01-11 22:05:26 +03:00
|
|
|
ltsleep(&sc->sc_bs_proc, PRIBIO, "fssthread", 0,
|
|
|
|
&sc->sc_slock);
|
|
|
|
FSS_UNLOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (sc->sc_copied != NULL)
|
|
|
|
free(sc->sc_copied, M_TEMP);
|
|
|
|
sc->sc_copied = NULL;
|
|
|
|
|
|
|
|
if (sc->sc_cache != NULL) {
|
|
|
|
for (i = 0; i < sc->sc_cache_size; i++)
|
2004-01-11 22:05:26 +03:00
|
|
|
if (sc->sc_cache[i].fc_data != NULL)
|
|
|
|
free(sc->sc_cache[i].fc_data, M_TEMP);
|
2003-12-10 14:40:11 +03:00
|
|
|
free(sc->sc_cache, M_TEMP);
|
|
|
|
}
|
|
|
|
sc->sc_cache = NULL;
|
|
|
|
|
|
|
|
if (sc->sc_indir_valid != NULL)
|
|
|
|
free(sc->sc_indir_valid, M_TEMP);
|
|
|
|
sc->sc_indir_valid = NULL;
|
|
|
|
|
|
|
|
if (sc->sc_indir_data != NULL)
|
|
|
|
free(sc->sc_indir_data, M_TEMP);
|
|
|
|
sc->sc_indir_data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if an unmount is ok. If forced, set this snapshot into ERROR state.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
fss_umount_hook(struct mount *mp, int forced)
|
|
|
|
{
|
|
|
|
int i, s;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
for (i = 0; i < NFSS; i++) {
|
|
|
|
FSS_LOCK(&fss_softc[i], s);
|
2003-12-10 14:40:11 +03:00
|
|
|
if ((fss_softc[i].sc_flags & FSS_ACTIVE) != 0 &&
|
|
|
|
fss_softc[i].sc_mount == mp) {
|
|
|
|
if (forced)
|
|
|
|
fss_error(&fss_softc[i], "forced unmount");
|
|
|
|
else {
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(&fss_softc[i], s);
|
2003-12-10 14:40:11 +03:00
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
}
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(&fss_softc[i], s);
|
|
|
|
}
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A buffer is written to the snapshotted block device. Copy to
|
|
|
|
* backing store if needed.
|
|
|
|
*/
|
2004-05-25 18:54:55 +04:00
|
|
|
static int
|
2004-02-14 03:00:56 +03:00
|
|
|
fss_copy_on_write(void *v, struct buf *bp)
|
2003-12-10 14:40:11 +03:00
|
|
|
{
|
2004-01-11 22:05:26 +03:00
|
|
|
int s;
|
2003-12-10 14:40:11 +03:00
|
|
|
u_int32_t cl, ch, c;
|
2004-02-14 03:00:56 +03:00
|
|
|
struct fss_softc *sc = v;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
if (!FSS_ISVALID(sc)) {
|
|
|
|
FSS_UNLOCK(sc, s);
|
2004-05-25 18:54:55 +04:00
|
|
|
return 0;
|
2004-01-11 22:05:26 +03:00
|
|
|
}
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
FSS_STAT_INC(sc, cow_calls);
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
|
|
|
|
ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
for (c = cl; c <= ch; c++)
|
2003-12-10 14:40:11 +03:00
|
|
|
fss_read_cluster(sc, c);
|
2004-05-25 18:54:55 +04:00
|
|
|
|
|
|
|
return 0;
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Lookup and open needed files.
|
|
|
|
*
|
2004-05-25 18:54:55 +04:00
|
|
|
* For file system internal snapshot initializes sc_mntname, sc_mount,
|
|
|
|
* sc_bs_vp and sc_time.
|
|
|
|
*
|
|
|
|
* Otherwise returns dev and size of the underlying block device.
|
2004-02-14 03:00:56 +03:00
|
|
|
* Initializes sc_mntname, sc_mount_vp, sc_bdev, sc_bs_vp and sc_mount
|
2003-12-10 14:40:11 +03:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fss_create_files(struct fss_softc *sc, struct fss_set *fss,
|
2004-02-14 03:00:56 +03:00
|
|
|
off_t *bsize, struct proc *p)
|
2003-12-10 14:40:11 +03:00
|
|
|
{
|
2004-01-11 22:05:26 +03:00
|
|
|
int error, fsbsize;
|
2004-05-25 18:54:55 +04:00
|
|
|
struct timespec ts;
|
2003-12-13 21:59:29 +03:00
|
|
|
struct partinfo dpart;
|
2004-01-11 22:05:26 +03:00
|
|
|
struct vattr va;
|
2003-12-10 14:40:11 +03:00
|
|
|
struct nameidata nd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the mounted file system.
|
|
|
|
*/
|
|
|
|
|
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_mount, p);
|
|
|
|
if ((error = namei(&nd)) != 0)
|
|
|
|
return error;
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
if ((nd.ni_vp->v_flag & VROOT) != VROOT) {
|
|
|
|
vrele(nd.ni_vp);
|
2003-12-10 14:40:11 +03:00
|
|
|
return EINVAL;
|
2004-05-25 18:54:55 +04:00
|
|
|
}
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
sc->sc_mount = nd.ni_vp->v_mount;
|
2004-05-25 18:54:55 +04:00
|
|
|
memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN);
|
|
|
|
|
|
|
|
vrele(nd.ni_vp);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
/*
|
2004-05-25 18:54:55 +04:00
|
|
|
* Check for file system internal snapshot.
|
2003-12-10 14:40:11 +03:00
|
|
|
*/
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p);
|
|
|
|
if ((error = namei(&nd)) != 0)
|
|
|
|
return error;
|
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
if (nd.ni_vp->v_type == VREG && nd.ni_vp->v_mount == sc->sc_mount) {
|
2004-05-25 18:54:55 +04:00
|
|
|
vrele(nd.ni_vp);
|
|
|
|
sc->sc_flags |= FSS_PERSISTENT;
|
|
|
|
|
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p);
|
|
|
|
if ((error = vn_open(&nd, FREAD, 0)) != 0)
|
|
|
|
return error;
|
|
|
|
sc->sc_bs_vp = nd.ni_vp;
|
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
|
|
|
|
if (fsbsize & (fsbsize-1)) /* No power of two */
|
|
|
|
return EINVAL;
|
|
|
|
for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32;
|
|
|
|
sc->sc_bs_bshift++)
|
|
|
|
if (FSS_FSBSIZE(sc) == fsbsize)
|
|
|
|
break;
|
|
|
|
if (sc->sc_bs_bshift >= 32)
|
|
|
|
return EINVAL;
|
|
|
|
sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
|
|
|
|
|
|
|
|
sc->sc_clshift = 0;
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts);
|
|
|
|
TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts);
|
2004-06-20 22:55:58 +04:00
|
|
|
|
|
|
|
VOP_UNLOCK(sc->sc_bs_vp, 0);
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
vrele(nd.ni_vp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the block device it is mounted on.
|
|
|
|
*/
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
|
|
|
|
sc->sc_mount->mnt_stat.f_mntfromname, p);
|
2003-12-10 14:40:11 +03:00
|
|
|
if ((error = namei(&nd)) != 0)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
if (nd.ni_vp->v_type != VBLK) {
|
|
|
|
vrele(nd.ni_vp);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
2003-12-13 21:59:29 +03:00
|
|
|
error = VOP_IOCTL(nd.ni_vp, DIOCGPART, &dpart, FREAD, p->p_ucred, p);
|
|
|
|
if (error) {
|
2003-12-10 14:40:11 +03:00
|
|
|
vrele(nd.ni_vp);
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2004-02-14 03:00:56 +03:00
|
|
|
sc->sc_mount_vp = nd.ni_vp;
|
|
|
|
sc->sc_bdev = nd.ni_vp->v_rdev;
|
2003-12-13 21:59:29 +03:00
|
|
|
*bsize = (off_t)dpart.disklab->d_secsize*dpart.part->p_size;
|
2003-12-10 14:40:11 +03:00
|
|
|
vrele(nd.ni_vp);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the backing store
|
|
|
|
*/
|
|
|
|
|
|
|
|
NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p);
|
|
|
|
if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
|
|
|
|
return error;
|
|
|
|
VOP_UNLOCK(nd.ni_vp, 0);
|
|
|
|
|
|
|
|
sc->sc_bs_vp = nd.ni_vp;
|
|
|
|
|
|
|
|
if (nd.ni_vp->v_type != VREG && nd.ni_vp->v_type != VCHR)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
if (sc->sc_bs_vp->v_type == VREG) {
|
2004-01-11 22:05:26 +03:00
|
|
|
error = VOP_GETATTR(sc->sc_bs_vp, &va, p->p_ucred, p);
|
2003-12-10 14:40:11 +03:00
|
|
|
if (error != 0)
|
|
|
|
return error;
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_bs_size = va.va_size;
|
|
|
|
fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
|
|
|
|
if (fsbsize & (fsbsize-1)) /* No power of two */
|
|
|
|
return EINVAL;
|
|
|
|
for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32;
|
|
|
|
sc->sc_bs_bshift++)
|
|
|
|
if (FSS_FSBSIZE(sc) == fsbsize)
|
|
|
|
break;
|
|
|
|
if (sc->sc_bs_bshift >= 32)
|
|
|
|
return EINVAL;
|
|
|
|
sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
|
|
|
|
sc->sc_flags |= FSS_BS_ALLOC;
|
|
|
|
} else {
|
|
|
|
sc->sc_bs_bshift = DEV_BSHIFT;
|
|
|
|
sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
|
|
|
|
sc->sc_flags &= ~FSS_BS_ALLOC;
|
|
|
|
}
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* As all IO to from/to the backing store goes through
|
|
|
|
* VOP_STRATEGY() clean the buffer cache to prevent
|
|
|
|
* cache incoherencies.
|
|
|
|
*/
|
|
|
|
if ((error = vinvalbuf(sc->sc_bs_vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
|
|
|
|
return error;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a snapshot.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct proc *p)
|
|
|
|
{
|
|
|
|
int len, error;
|
2004-01-11 22:05:26 +03:00
|
|
|
u_int32_t csize;
|
2003-12-10 14:40:11 +03:00
|
|
|
off_t bsize;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open needed files.
|
|
|
|
*/
|
2004-02-14 03:00:56 +03:00
|
|
|
if ((error = fss_create_files(sc, fss, &bsize, p)) != 0)
|
2003-12-10 14:40:11 +03:00
|
|
|
goto bad;
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
if (sc->sc_flags & FSS_PERSISTENT) {
|
|
|
|
fss_softc_alloc(sc);
|
|
|
|
sc->sc_flags |= FSS_ACTIVE;
|
|
|
|
return 0;
|
2004-01-11 22:05:26 +03:00
|
|
|
}
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
/*
|
2004-01-11 22:05:26 +03:00
|
|
|
* Set cluster size. Must be a power of two and
|
|
|
|
* a multiple of backing store block size.
|
2003-12-10 14:40:11 +03:00
|
|
|
*/
|
2004-01-11 22:05:26 +03:00
|
|
|
if (fss->fss_csize <= 0)
|
|
|
|
csize = MAXPHYS;
|
|
|
|
else
|
|
|
|
csize = fss->fss_csize;
|
|
|
|
if (bsize/csize > FSS_CLUSTER_MAX)
|
|
|
|
csize = bsize/FSS_CLUSTER_MAX+1;
|
|
|
|
|
|
|
|
for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32;
|
|
|
|
sc->sc_clshift++)
|
|
|
|
if (FSS_CLSIZE(sc) >= csize)
|
|
|
|
break;
|
|
|
|
if (sc->sc_clshift >= 32) {
|
2003-12-10 14:40:11 +03:00
|
|
|
error = EINVAL;
|
|
|
|
goto bad;
|
2004-01-11 22:05:26 +03:00
|
|
|
}
|
|
|
|
sc->sc_clmask = FSS_CLSIZE(sc)-1;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
/*
|
|
|
|
* Set number of cache slots.
|
|
|
|
*/
|
|
|
|
if (FSS_CLSIZE(sc) <= 8192)
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_cache_size = 32;
|
2004-01-11 22:05:26 +03:00
|
|
|
else if (FSS_CLSIZE(sc) <= 65536)
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_cache_size = 8;
|
|
|
|
else
|
|
|
|
sc->sc_cache_size = 4;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
/*
|
|
|
|
* Set number of clusters and size of last cluster.
|
|
|
|
*/
|
|
|
|
sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1;
|
|
|
|
sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
/*
|
|
|
|
* Set size of indirect table.
|
|
|
|
*/
|
2003-12-10 14:40:11 +03:00
|
|
|
len = sc->sc_clcount*sizeof(u_int32_t);
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_indir_size = FSS_BTOCL(sc, len)+1;
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_clnext = sc->sc_indir_size;
|
|
|
|
sc->sc_indir_cur = 0;
|
|
|
|
|
|
|
|
if ((error = fss_softc_alloc(sc)) != 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Activate the snapshot.
|
|
|
|
*/
|
|
|
|
|
2004-01-10 20:16:38 +03:00
|
|
|
if ((error = vfs_write_suspend(sc->sc_mount, PUSER|PCATCH, 0)) != 0)
|
2003-12-10 14:40:11 +03:00
|
|
|
goto bad;
|
|
|
|
|
|
|
|
microtime(&sc->sc_time);
|
|
|
|
|
2004-02-14 03:00:56 +03:00
|
|
|
if (error == 0)
|
|
|
|
error = vn_cow_establish(sc->sc_mount_vp,
|
|
|
|
fss_copy_on_write, sc);
|
|
|
|
if (error == 0)
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_flags |= FSS_ACTIVE;
|
|
|
|
|
|
|
|
vfs_write_resume(sc->sc_mount);
|
|
|
|
|
|
|
|
if (error != 0)
|
|
|
|
goto bad;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("fss%d: %s snapshot active\n", sc->sc_unit, sc->sc_mntname);
|
|
|
|
printf("fss%d: %u clusters of %u, %u cache slots, %u indir clusters\n",
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_unit, sc->sc_clcount, FSS_CLSIZE(sc),
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_cache_size, sc->sc_indir_size);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
bad:
|
|
|
|
fss_softc_free(sc);
|
2004-05-25 18:54:55 +04:00
|
|
|
if (sc->sc_bs_vp != NULL) {
|
|
|
|
if (sc->sc_flags & FSS_PERSISTENT)
|
|
|
|
vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p);
|
|
|
|
else
|
|
|
|
vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p);
|
|
|
|
}
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_bs_vp = NULL;
|
|
|
|
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete a snapshot.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fss_delete_snapshot(struct fss_softc *sc, struct proc *p)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
if ((sc->sc_flags & FSS_PERSISTENT) == 0)
|
|
|
|
vn_cow_disestablish(sc->sc_mount_vp, fss_copy_on_write, sc);
|
2004-01-11 22:05:26 +03:00
|
|
|
|
2004-02-14 03:00:56 +03:00
|
|
|
FSS_LOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR);
|
|
|
|
sc->sc_mount = NULL;
|
|
|
|
sc->sc_bdev = NODEV;
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
fss_softc_free(sc);
|
2004-05-25 18:54:55 +04:00
|
|
|
if (sc->sc_flags & FSS_PERSISTENT)
|
|
|
|
vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p);
|
|
|
|
else
|
|
|
|
vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p);
|
2003-12-10 14:40:11 +03:00
|
|
|
sc->sc_bs_vp = NULL;
|
2004-05-25 18:54:55 +04:00
|
|
|
sc->sc_flags &= ~FSS_PERSISTENT;
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
FSS_STAT_CLEAR(sc);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-01-11 22:05:26 +03:00
|
|
|
* Get the block address and number of contiguous blocks.
|
|
|
|
* If the file contains a hole, try to allocate.
|
2004-06-20 22:55:58 +04:00
|
|
|
* Backing store is locked by caller.
|
2004-01-11 22:05:26 +03:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fss_bmap(struct fss_softc *sc, off_t start, int len,
|
|
|
|
struct vnode **vpp, daddr_t *bnp, int *runp)
|
2003-12-10 14:40:11 +03:00
|
|
|
{
|
2004-01-11 22:05:26 +03:00
|
|
|
int l, s, error;
|
|
|
|
struct buf *bp, **bpp;
|
|
|
|
|
|
|
|
if ((sc->sc_bs_vp->v_mount->mnt_flag & MNT_SOFTDEP) != 0)
|
|
|
|
bpp = &bp;
|
|
|
|
else
|
|
|
|
bpp = NULL;
|
|
|
|
|
|
|
|
error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp);
|
|
|
|
if ((error == 0 && *bnp != (daddr_t)-1) ||
|
|
|
|
(sc->sc_flags & FSS_BS_ALLOC) == 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (start+len >= sc->sc_bs_size) {
|
|
|
|
error = ENOSPC;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (l = 0; l < len; l += FSS_FSBSIZE(sc)) {
|
|
|
|
error = VOP_BALLOC(sc->sc_bs_vp, start+l, FSS_FSBSIZE(sc),
|
|
|
|
sc->sc_bs_proc->p_ucred, 0, bpp);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
if (bpp == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
simple_lock(&bp->b_interlock);
|
|
|
|
|
|
|
|
if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_start)
|
|
|
|
(*bioops.io_start)(bp);
|
|
|
|
if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete)
|
|
|
|
(*bioops.io_complete)(bp);
|
|
|
|
|
|
|
|
bp->b_flags |= B_INVAL;
|
|
|
|
simple_unlock(&bp->b_interlock);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
brelse(bp);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp);
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
if ((sc->sc_flags & FSS_PERSISTENT) == 0 &&
|
|
|
|
error == 0 && *bnp == (daddr_t)-1)
|
2004-01-11 22:05:26 +03:00
|
|
|
error = ENOSPC;
|
|
|
|
|
|
|
|
return error;
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* A read from the snapshotted block device has completed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fss_cluster_iodone(struct buf *bp)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct fss_cache *scp = bp->b_private;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(scp->fc_softc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
if (bp->b_flags & B_EINTR)
|
|
|
|
fss_error(scp->fc_softc, "fs read interrupted");
|
|
|
|
if (bp->b_flags & B_ERROR)
|
|
|
|
fss_error(scp->fc_softc, "fs read error %d", bp->b_error);
|
|
|
|
|
|
|
|
if (bp->b_vp != NULL)
|
|
|
|
brelvp(bp);
|
|
|
|
|
|
|
|
if (--scp->fc_xfercount == 0)
|
|
|
|
wakeup(&scp->fc_data);
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(scp->fc_softc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
s = splbio();
|
|
|
|
pool_put(&bufpool, bp);
|
2003-12-10 14:40:11 +03:00
|
|
|
splx(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a cluster from the snapshotted block device to the cache.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fss_read_cluster(struct fss_softc *sc, u_int32_t cl)
|
|
|
|
{
|
|
|
|
int s, todo, len;
|
|
|
|
caddr_t addr;
|
|
|
|
daddr_t dblk;
|
|
|
|
struct buf *bp;
|
|
|
|
struct fss_cache *scp, *scl;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a free cache slot.
|
|
|
|
*/
|
|
|
|
scl = sc->sc_cache+sc->sc_cache_size;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
restart:
|
2004-01-11 22:05:26 +03:00
|
|
|
if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) {
|
|
|
|
FSS_UNLOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (scp = sc->sc_cache; scp < scl; scp++)
|
|
|
|
if (scp->fc_type != FSS_CACHE_FREE &&
|
|
|
|
scp->fc_cluster == cl) {
|
2004-01-11 22:05:26 +03:00
|
|
|
ltsleep(&scp->fc_type, PRIBIO, "cowwait2", 0,
|
|
|
|
&sc->sc_slock);
|
2003-12-10 14:40:11 +03:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (scp = sc->sc_cache; scp < scl; scp++)
|
|
|
|
if (scp->fc_type == FSS_CACHE_FREE) {
|
|
|
|
scp->fc_type = FSS_CACHE_BUSY;
|
|
|
|
scp->fc_cluster = cl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (scp >= scl) {
|
|
|
|
FSS_STAT_INC(sc, cow_cache_full);
|
2004-01-11 22:05:26 +03:00
|
|
|
ltsleep(&sc->sc_cache, PRIBIO, "cowwait3", 0, &sc->sc_slock);
|
2003-12-10 14:40:11 +03:00
|
|
|
goto restart;
|
|
|
|
}
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
/*
|
|
|
|
* Start the read.
|
|
|
|
*/
|
|
|
|
FSS_STAT_INC(sc, cow_copied);
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
dblk = btodb(FSS_CLTOB(sc, cl));
|
2003-12-10 14:40:11 +03:00
|
|
|
addr = scp->fc_data;
|
|
|
|
if (cl == sc->sc_clcount-1) {
|
2004-01-11 22:05:26 +03:00
|
|
|
todo = sc->sc_clresid;
|
|
|
|
memset(addr+todo, 0, FSS_CLSIZE(sc)-todo);
|
2003-12-10 14:40:11 +03:00
|
|
|
} else
|
2004-01-11 22:05:26 +03:00
|
|
|
todo = FSS_CLSIZE(sc);
|
2003-12-10 14:40:11 +03:00
|
|
|
while (todo > 0) {
|
|
|
|
len = todo;
|
|
|
|
if (len > MAXPHYS)
|
|
|
|
len = MAXPHYS;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
bp = pool_get(&bufpool, PR_WAITOK);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
BUF_INIT(bp);
|
|
|
|
bp->b_flags = B_READ|B_CALL;
|
|
|
|
bp->b_bcount = len;
|
|
|
|
bp->b_bufsize = bp->b_bcount;
|
|
|
|
bp->b_error = 0;
|
|
|
|
bp->b_data = addr;
|
|
|
|
bp->b_blkno = bp->b_rawblkno = dblk;
|
|
|
|
bp->b_proc = NULL;
|
|
|
|
bp->b_dev = sc->sc_bdev;
|
|
|
|
bp->b_vp = NULLVP;
|
|
|
|
bp->b_private = scp;
|
|
|
|
bp->b_iodone = fss_cluster_iodone;
|
|
|
|
|
2004-01-25 21:06:48 +03:00
|
|
|
DEV_STRATEGY(bp);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
scp->fc_xfercount++;
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
dblk += btodb(len);
|
|
|
|
addr += len;
|
|
|
|
todo -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for all read requests to complete.
|
|
|
|
*/
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
while (scp->fc_xfercount > 0)
|
2004-01-11 22:05:26 +03:00
|
|
|
ltsleep(&scp->fc_data, PRIBIO, "cowwait", 0, &sc->sc_slock);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
scp->fc_type = FSS_CACHE_VALID;
|
|
|
|
setbit(sc->sc_copied, scp->fc_cluster);
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
wakeup(&sc->sc_bs_proc);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Write a cluster from the cache to the backing store.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fss_write_cluster(struct fss_cache *scp, u_int32_t cl)
|
|
|
|
{
|
|
|
|
int s, error, todo, len, nra;
|
|
|
|
daddr_t nbn;
|
|
|
|
caddr_t addr;
|
|
|
|
off_t pos;
|
|
|
|
struct buf *bp;
|
|
|
|
struct vnode *vp;
|
|
|
|
struct fss_softc *sc;
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
sc = scp->fc_softc;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
pos = FSS_CLTOB(sc, cl);
|
2003-12-10 14:40:11 +03:00
|
|
|
addr = scp->fc_data;
|
2004-01-11 22:05:26 +03:00
|
|
|
todo = FSS_CLSIZE(sc);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY);
|
|
|
|
simple_lock(&sc->sc_bs_vp->v_interlock);
|
|
|
|
error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(pos),
|
|
|
|
round_page(pos+todo), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
|
|
|
|
|
|
|
|
while (error == 0 && todo > 0) {
|
2004-01-11 22:05:26 +03:00
|
|
|
error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra);
|
2003-12-10 14:40:11 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
len = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos);
|
2003-12-10 14:40:11 +03:00
|
|
|
if (len > todo)
|
|
|
|
len = todo;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
bp = pool_get(&bufpool, PR_WAITOK);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
BUF_INIT(bp);
|
|
|
|
bp->b_flags = B_CALL;
|
|
|
|
bp->b_bcount = len;
|
|
|
|
bp->b_bufsize = bp->b_bcount;
|
|
|
|
bp->b_error = 0;
|
|
|
|
bp->b_data = addr;
|
2004-01-11 22:05:26 +03:00
|
|
|
bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos));
|
2003-12-10 14:40:11 +03:00
|
|
|
bp->b_proc = NULL;
|
|
|
|
bp->b_vp = NULLVP;
|
|
|
|
bp->b_private = scp;
|
|
|
|
bp->b_iodone = fss_cluster_iodone;
|
|
|
|
bgetvp(vp, bp);
|
|
|
|
bp->b_vp->v_numoutput++;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
|
2004-01-25 21:06:48 +03:00
|
|
|
VOP_STRATEGY(vp, bp);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
scp->fc_xfercount++;
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
pos += len;
|
|
|
|
addr += len;
|
|
|
|
todo -= len;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Wait for all write requests to complete.
|
|
|
|
*/
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
while (scp->fc_xfercount > 0)
|
2004-01-11 22:05:26 +03:00
|
|
|
ltsleep(&scp->fc_data, PRIBIO, "bswwait", 0, &sc->sc_slock);
|
|
|
|
FSS_UNLOCK(sc, s);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
VOP_UNLOCK(sc->sc_bs_vp, 0);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read/write clusters from/to backing store.
|
2004-06-20 22:55:58 +04:00
|
|
|
* For persistent snapshots must be called with cl == 0. off is the
|
|
|
|
* offset into the snapshot.
|
2003-12-10 14:40:11 +03:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
fss_bs_io(struct fss_softc *sc, fss_io_type rw,
|
2004-06-20 22:55:58 +04:00
|
|
|
u_int32_t cl, off_t off, int len, caddr_t data)
|
2003-12-10 14:40:11 +03:00
|
|
|
{
|
|
|
|
int s, error, todo, count, nra;
|
|
|
|
off_t pos;
|
|
|
|
daddr_t nbn;
|
|
|
|
struct buf *bp;
|
|
|
|
struct vnode *vp;
|
|
|
|
|
|
|
|
todo = len;
|
2004-01-11 22:05:26 +03:00
|
|
|
pos = FSS_CLTOB(sc, cl)+off;
|
2003-12-10 14:40:11 +03:00
|
|
|
error = 0;
|
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY);
|
|
|
|
simple_lock(&sc->sc_bs_vp->v_interlock);
|
|
|
|
error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(pos),
|
|
|
|
round_page(pos+todo), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
|
|
|
|
|
|
|
|
while (error == 0 && todo > 0) {
|
2004-01-11 22:05:26 +03:00
|
|
|
error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra);
|
2003-12-10 14:40:11 +03:00
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
count = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos);
|
2003-12-10 14:40:11 +03:00
|
|
|
if (count > todo)
|
|
|
|
count = todo;
|
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
if (nbn == (daddr_t)-1) {
|
|
|
|
bzero(data, count);
|
|
|
|
todo -= count;
|
|
|
|
data += count;
|
|
|
|
pos += count;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
s = splbio();
|
|
|
|
bp = pool_get(&bufpool, PR_WAITOK);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
BUF_INIT(bp);
|
|
|
|
bp->b_flags = (rw == FSS_READ ? B_READ : 0);
|
|
|
|
bp->b_bcount = count;
|
|
|
|
bp->b_bufsize = bp->b_bcount;
|
|
|
|
bp->b_error = 0;
|
|
|
|
bp->b_data = data;
|
2004-01-11 22:05:26 +03:00
|
|
|
bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos));
|
2003-12-10 14:40:11 +03:00
|
|
|
bp->b_proc = NULL;
|
|
|
|
bp->b_vp = NULLVP;
|
|
|
|
bgetvp(vp, bp);
|
|
|
|
if ((bp->b_flags & B_READ) == 0)
|
|
|
|
bp->b_vp->v_numoutput++;
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
if ((bp->b_flags & B_READ) == 0 || cl < sc->sc_indir_size)
|
|
|
|
BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
|
2004-01-25 21:06:48 +03:00
|
|
|
VOP_STRATEGY(vp, bp);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
error = biowait(bp);
|
|
|
|
|
|
|
|
if (bp->b_vp != NULL)
|
|
|
|
brelvp(bp);
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
pool_put(&bufpool, bp);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
break;
|
|
|
|
|
|
|
|
todo -= count;
|
|
|
|
data += count;
|
|
|
|
pos += count;
|
|
|
|
}
|
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
VOP_UNLOCK(sc->sc_bs_vp, 0);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a pointer to the indirect slot for this cluster.
|
|
|
|
*/
|
|
|
|
static u_int32_t *
|
|
|
|
fss_bs_indir(struct fss_softc *sc, u_int32_t cl)
|
|
|
|
{
|
|
|
|
u_int32_t icl;
|
|
|
|
int ioff;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
icl = cl/(FSS_CLSIZE(sc)/sizeof(u_int32_t));
|
|
|
|
ioff = cl%(FSS_CLSIZE(sc)/sizeof(u_int32_t));
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
if (sc->sc_indir_cur == icl)
|
|
|
|
return &sc->sc_indir_data[ioff];
|
|
|
|
|
|
|
|
if (sc->sc_indir_dirty) {
|
|
|
|
FSS_STAT_INC(sc, indir_write);
|
|
|
|
if (fss_bs_io(sc, FSS_WRITE, sc->sc_indir_cur, 0,
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0)
|
2003-12-10 14:40:11 +03:00
|
|
|
return NULL;
|
|
|
|
setbit(sc->sc_indir_valid, sc->sc_indir_cur);
|
|
|
|
}
|
|
|
|
|
|
|
|
sc->sc_indir_dirty = 0;
|
|
|
|
sc->sc_indir_cur = icl;
|
|
|
|
|
|
|
|
if (isset(sc->sc_indir_valid, sc->sc_indir_cur)) {
|
|
|
|
FSS_STAT_INC(sc, indir_read);
|
|
|
|
if (fss_bs_io(sc, FSS_READ, sc->sc_indir_cur, 0,
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0)
|
2003-12-10 14:40:11 +03:00
|
|
|
return NULL;
|
|
|
|
} else
|
2004-01-11 22:05:26 +03:00
|
|
|
memset(sc->sc_indir_data, 0, FSS_CLSIZE(sc));
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
return &sc->sc_indir_data[ioff];
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The kernel thread (one for every active snapshot).
|
|
|
|
*
|
|
|
|
* After wakeup it cleans the cache and runs the I/O requests.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
fss_bs_thread(void *arg)
|
|
|
|
{
|
|
|
|
int error, len, nfreed, nio, s;
|
|
|
|
long off;
|
|
|
|
caddr_t addr;
|
|
|
|
u_int32_t c, cl, ch, *indirp;
|
|
|
|
struct buf *bp, *nbp;
|
|
|
|
struct fss_softc *sc;
|
|
|
|
struct fss_cache *scp, *scl;
|
|
|
|
|
|
|
|
sc = arg;
|
|
|
|
|
|
|
|
scl = sc->sc_cache+sc->sc_cache_size;
|
|
|
|
|
|
|
|
s = splbio();
|
|
|
|
nbp = pool_get(&bufpool, PR_WAITOK);
|
|
|
|
splx(s);
|
|
|
|
|
|
|
|
nfreed = nio = 1; /* Dont sleep the first time */
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
for (;;) {
|
|
|
|
if (nfreed == 0 && nio == 0)
|
2004-01-11 22:05:26 +03:00
|
|
|
ltsleep(&sc->sc_bs_proc, PVM-1, "fssbs", 0,
|
|
|
|
&sc->sc_slock);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if ((sc->sc_flags & FSS_BS_THREAD) == 0) {
|
2004-01-11 22:05:26 +03:00
|
|
|
sc->sc_bs_proc = NULL;
|
|
|
|
wakeup(&sc->sc_bs_proc);
|
|
|
|
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
s = splbio();
|
|
|
|
pool_put(&bufpool, nbp);
|
|
|
|
splx(s);
|
|
|
|
#ifdef FSS_STATISTICS
|
2004-05-25 18:54:55 +04:00
|
|
|
if ((sc->sc_flags & FSS_PERSISTENT) == 0) {
|
|
|
|
printf("fss%d: cow called %" PRId64 " times,"
|
|
|
|
" copied %" PRId64 " clusters,"
|
|
|
|
" cache full %" PRId64 " times\n",
|
|
|
|
sc->sc_unit,
|
|
|
|
FSS_STAT_VAL(sc, cow_calls),
|
|
|
|
FSS_STAT_VAL(sc, cow_copied),
|
|
|
|
FSS_STAT_VAL(sc, cow_cache_full));
|
|
|
|
printf("fss%d: %" PRId64 " indir reads,"
|
|
|
|
" %" PRId64 " indir writes\n",
|
|
|
|
sc->sc_unit,
|
|
|
|
FSS_STAT_VAL(sc, indir_read),
|
|
|
|
FSS_STAT_VAL(sc, indir_write));
|
|
|
|
}
|
2003-12-10 14:40:11 +03:00
|
|
|
#endif /* FSS_STATISTICS */
|
|
|
|
kthread_exit(0);
|
|
|
|
}
|
|
|
|
|
2004-05-25 18:54:55 +04:00
|
|
|
/*
|
|
|
|
* Process I/O requests (persistent)
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (sc->sc_flags & FSS_PERSISTENT) {
|
|
|
|
nfreed = nio = 0;
|
|
|
|
|
|
|
|
if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nio++;
|
|
|
|
|
|
|
|
if (FSS_ISVALID(sc)) {
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2004-06-20 22:55:58 +04:00
|
|
|
error = fss_bs_io(sc, FSS_READ, 0,
|
|
|
|
dbtob(bp->b_blkno), bp->b_bcount,
|
|
|
|
bp->b_data);
|
2004-05-25 18:54:55 +04:00
|
|
|
|
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
} else
|
|
|
|
error = ENXIO;
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
bp->b_error = error;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
}
|
|
|
|
biodone(bp);
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
/*
|
|
|
|
* Clean the cache
|
|
|
|
*/
|
|
|
|
nfreed = 0;
|
|
|
|
for (scp = sc->sc_cache; scp < scl; scp++) {
|
|
|
|
if (scp->fc_type != FSS_CACHE_VALID)
|
|
|
|
continue;
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
indirp = fss_bs_indir(sc, scp->fc_cluster);
|
|
|
|
if (indirp != NULL) {
|
|
|
|
error = fss_write_cluster(scp, sc->sc_clnext);
|
|
|
|
} else
|
|
|
|
error = EIO;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if (error == 0) {
|
|
|
|
*indirp = sc->sc_clnext++;
|
|
|
|
sc->sc_indir_dirty = 1;
|
|
|
|
} else
|
|
|
|
fss_error(sc, "write bs error %d", error);
|
|
|
|
|
|
|
|
scp->fc_type = FSS_CACHE_FREE;
|
|
|
|
nfreed++;
|
|
|
|
wakeup(&scp->fc_type);
|
|
|
|
}
|
2004-01-11 22:05:26 +03:00
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if (nfreed)
|
|
|
|
wakeup(&sc->sc_cache);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process I/O requests
|
|
|
|
*/
|
|
|
|
nio = 0;
|
|
|
|
|
|
|
|
if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
nio++;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
if (!FSS_ISVALID(sc)) {
|
2003-12-10 14:40:11 +03:00
|
|
|
bp->b_error = ENXIO;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
biodone(bp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First read from the snapshotted block device.
|
|
|
|
* XXX Split to only read those parts that have not
|
|
|
|
* been saved to backing store?
|
|
|
|
*/
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
BUF_INIT(nbp);
|
|
|
|
nbp->b_flags = B_READ;
|
|
|
|
nbp->b_bcount = bp->b_bcount;
|
|
|
|
nbp->b_bufsize = bp->b_bcount;
|
|
|
|
nbp->b_error = 0;
|
|
|
|
nbp->b_data = bp->b_data;
|
|
|
|
nbp->b_blkno = nbp->b_rawblkno = bp->b_blkno;
|
|
|
|
nbp->b_proc = bp->b_proc;
|
|
|
|
nbp->b_dev = sc->sc_bdev;
|
|
|
|
nbp->b_vp = NULLVP;
|
|
|
|
|
2004-01-25 21:06:48 +03:00
|
|
|
DEV_STRATEGY(nbp);
|
2003-12-10 14:40:11 +03:00
|
|
|
|
|
|
|
if (biowait(nbp) != 0) {
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
bp->b_error = nbp->b_error;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
biodone(bp);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
|
|
|
|
off = FSS_CLOFF(sc, dbtob(bp->b_blkno));
|
|
|
|
ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
|
2003-12-10 14:40:11 +03:00
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
addr = bp->b_data;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
/*
|
|
|
|
* Replace those parts that have been saved to backing store.
|
|
|
|
*/
|
2004-01-11 22:05:26 +03:00
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
for (c = cl; c <= ch;
|
|
|
|
c++, off = 0, bp->b_resid -= len, addr += len) {
|
2004-01-11 22:05:26 +03:00
|
|
|
len = FSS_CLSIZE(sc)-off;
|
2003-12-10 14:40:11 +03:00
|
|
|
if (len > bp->b_resid)
|
|
|
|
len = bp->b_resid;
|
|
|
|
|
|
|
|
if (isclr(sc->sc_copied, c))
|
|
|
|
continue;
|
|
|
|
|
2004-01-11 22:05:26 +03:00
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
indirp = fss_bs_indir(sc, c);
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if (indirp == NULL || *indirp == 0) {
|
|
|
|
/*
|
|
|
|
* Not on backing store. Either in cache
|
|
|
|
* or hole in the snapshotted block device.
|
|
|
|
*/
|
|
|
|
for (scp = sc->sc_cache; scp < scl; scp++)
|
|
|
|
if (scp->fc_type == FSS_CACHE_VALID &&
|
|
|
|
scp->fc_cluster == c)
|
|
|
|
break;
|
|
|
|
if (scp < scl)
|
|
|
|
memcpy(addr, scp->fc_data+off, len);
|
|
|
|
else
|
|
|
|
memset(addr, 0, len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Read from backing store.
|
|
|
|
*/
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
FSS_UNLOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
if ((error = fss_bs_io(sc, FSS_READ, *indirp,
|
|
|
|
off, len, addr)) != 0) {
|
|
|
|
bp->b_resid = bp->b_bcount;
|
|
|
|
bp->b_error = error;
|
|
|
|
bp->b_flags |= B_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2004-01-11 22:05:26 +03:00
|
|
|
|
|
|
|
FSS_LOCK(sc, s);
|
|
|
|
|
2003-12-10 14:40:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
biodone(bp);
|
|
|
|
}
|
|
|
|
}
|