Home | History | Annotate | Line # | Download | only in dev
fss.c revision 1.9
      1 /*	$NetBSD: fss.c,v 1.9 2004/06/20 18:55:58 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Juergen Hannken-Illjes.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *	This product includes software developed by the NetBSD
     21  *	Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * File system snapshot disk driver.
     41  *
     42  * Block/character interface to the snapshot of a mounted file system.
     43  */
     44 
     45 #include <sys/cdefs.h>
     46 __KERNEL_RCSID(0, "$NetBSD: fss.c,v 1.9 2004/06/20 18:55:58 hannken Exp $");
     47 
     48 #include "fss.h"
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/namei.h>
     53 #include <sys/proc.h>
     54 #include <sys/errno.h>
     55 #include <sys/buf.h>
     56 #include <sys/malloc.h>
     57 #include <sys/ioctl.h>
     58 #include <sys/disklabel.h>
     59 #include <sys/device.h>
     60 #include <sys/disk.h>
     61 #include <sys/stat.h>
     62 #include <sys/mount.h>
     63 #include <sys/vnode.h>
     64 #include <sys/file.h>
     65 #include <sys/uio.h>
     66 #include <sys/conf.h>
     67 #include <sys/kthread.h>
     68 
     69 #include <miscfs/specfs/specdev.h>
     70 
     71 #include <dev/fssvar.h>
     72 
     73 #include <machine/stdarg.h>
     74 
     75 #ifdef DEBUG
     76 #define FSS_STATISTICS
     77 #endif
     78 
     79 #ifdef FSS_STATISTICS
     80 struct fss_stat {
     81 	u_int64_t	cow_calls;
     82 	u_int64_t	cow_copied;
     83 	u_int64_t	cow_cache_full;
     84 	u_int64_t	indir_read;
     85 	u_int64_t	indir_write;
     86 };
     87 
     88 static struct fss_stat fss_stat[NFSS];
     89 
     90 #define FSS_STAT_INC(sc, field)	\
     91 			do { \
     92 				fss_stat[sc->sc_unit].field++; \
     93 			} while (0)
     94 #define FSS_STAT_SET(sc, field, value) \
     95 			do { \
     96 				fss_stat[sc->sc_unit].field = value; \
     97 			} while (0)
     98 #define FSS_STAT_ADD(sc, field, value) \
     99 			do { \
    100 				fss_stat[sc->sc_unit].field += value; \
    101 			} while (0)
    102 #define FSS_STAT_VAL(sc, field) fss_stat[sc->sc_unit].field
    103 #define FSS_STAT_CLEAR(sc) \
    104 			do { \
    105 				memset(&fss_stat[sc->sc_unit], 0, \
    106 				    sizeof(struct fss_stat)); \
    107 			} while (0)
    108 #else /* FSS_STATISTICS */
    109 #define FSS_STAT_INC(sc, field)
    110 #define FSS_STAT_SET(sc, field, value)
    111 #define FSS_STAT_ADD(sc, field, value)
    112 #define FSS_STAT_CLEAR(sc)
    113 #endif /* FSS_STATISTICS */
    114 
    115 static struct fss_softc fss_softc[NFSS];
    116 
    117 void fssattach(int);
    118 
    119 dev_type_open(fss_open);
    120 dev_type_close(fss_close);
    121 dev_type_read(fss_read);
    122 dev_type_write(fss_write);
    123 dev_type_ioctl(fss_ioctl);
    124 dev_type_strategy(fss_strategy);
    125 dev_type_dump(fss_dump);
    126 dev_type_size(fss_size);
    127 
    128 static int fss_copy_on_write(void *, struct buf *);
    129 static inline void fss_error(struct fss_softc *, const char *, ...);
    130 static int fss_create_files(struct fss_softc *, struct fss_set *,
    131     off_t *, struct proc *);
    132 static int fss_create_snapshot(struct fss_softc *, struct fss_set *,
    133     struct proc *);
    134 static int fss_delete_snapshot(struct fss_softc *, struct proc *);
    135 static int fss_softc_alloc(struct fss_softc *);
    136 static void fss_softc_free(struct fss_softc *);
    137 static void fss_cluster_iodone(struct buf *);
    138 static void fss_read_cluster(struct fss_softc *, u_int32_t);
    139 static int fss_write_cluster(struct fss_cache *, u_int32_t);
    140 static void fss_bs_thread(void *);
    141 static int fss_bmap(struct fss_softc *, off_t, int,
    142     struct vnode **, daddr_t *, int *);
    143 static int fss_bs_io(struct fss_softc *, fss_io_type,
    144     u_int32_t, off_t, int, caddr_t);
    145 static u_int32_t *fss_bs_indir(struct fss_softc *, u_int32_t);
    146 
    147 const struct bdevsw fss_bdevsw = {
    148 	fss_open, fss_close, fss_strategy, fss_ioctl,
    149 	fss_dump, fss_size, D_DISK
    150 };
    151 
    152 const struct cdevsw fss_cdevsw = {
    153 	fss_open, fss_close, fss_read, fss_write, fss_ioctl,
    154 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    155 };
    156 
    157 void
    158 fssattach(int num)
    159 {
    160 	int i;
    161 	struct fss_softc *sc;
    162 
    163 	for (i = 0; i < NFSS; i++) {
    164 		sc = &fss_softc[i];
    165 		sc->sc_unit = i;
    166 		sc->sc_bdev = NODEV;
    167 		simple_lock_init(&sc->sc_slock);
    168 		bufq_alloc(&sc->sc_bufq, BUFQ_FCFS|BUFQ_SORT_RAWBLOCK);
    169 	}
    170 }
    171 
    172 int
    173 fss_open(dev_t dev, int flags, int mode, struct proc *p)
    174 {
    175 	struct fss_softc *sc;
    176 
    177 	if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
    178 		return ENODEV;
    179 
    180 	return 0;
    181 }
    182 
    183 int
    184 fss_close(dev_t dev, int flags, int mode, struct proc *p)
    185 {
    186 	struct fss_softc *sc;
    187 
    188 	if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
    189 		return ENODEV;
    190 
    191 	return 0;
    192 }
    193 
    194 void
    195 fss_strategy(struct buf *bp)
    196 {
    197 	int s;
    198 	struct fss_softc *sc;
    199 
    200 	sc = FSS_DEV_TO_SOFTC(bp->b_dev);
    201 
    202 	FSS_LOCK(sc, s);
    203 
    204 	if ((bp->b_flags & B_READ) != B_READ ||
    205 	    sc == NULL || !FSS_ISVALID(sc)) {
    206 
    207 		FSS_UNLOCK(sc, s);
    208 
    209 		bp->b_error = (sc == NULL ? ENODEV : EROFS);
    210 		bp->b_flags |= B_ERROR;
    211 		bp->b_resid = bp->b_bcount;
    212 		biodone(bp);
    213 		return;
    214 	}
    215 
    216 	bp->b_rawblkno = bp->b_blkno;
    217 	BUFQ_PUT(&sc->sc_bufq, bp);
    218 	wakeup(&sc->sc_bs_proc);
    219 
    220 	FSS_UNLOCK(sc, s);
    221 }
    222 
    223 int
    224 fss_read(dev_t dev, struct uio *uio, int flags)
    225 {
    226 	return physio(fss_strategy, NULL, dev, B_READ, minphys, uio);
    227 }
    228 
    229 int
    230 fss_write(dev_t dev, struct uio *uio, int flags)
    231 {
    232 	return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio);
    233 }
    234 
    235 int
    236 fss_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
    237 {
    238 	int s, error;
    239 	struct fss_softc *sc;
    240 	struct fss_set *fss = (struct fss_set *)data;
    241 	struct fss_get *fsg = (struct fss_get *)data;
    242 
    243 	if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
    244 		return ENODEV;
    245 
    246 	FSS_LOCK(sc, s);
    247 	while ((sc->sc_flags & FSS_EXCL) == FSS_EXCL) {
    248 		error = ltsleep(sc, PRIBIO|PCATCH, "fsslock", 0, &sc->sc_slock);
    249 		if (error) {
    250 			FSS_UNLOCK(sc, s);
    251 			return error;
    252 		}
    253 	}
    254 	sc->sc_flags |= FSS_EXCL;
    255 	FSS_UNLOCK(sc, s);
    256 
    257 	error = EINVAL;
    258 
    259 	switch (cmd) {
    260 	case FSSIOCSET:
    261 		if ((flag & FWRITE) == 0)
    262 			error = EPERM;
    263 		else if ((sc->sc_flags & FSS_ACTIVE) != 0)
    264 			error = EBUSY;
    265 		else
    266 			error = fss_create_snapshot(sc, fss, p);
    267 		break;
    268 
    269 	case FSSIOCCLR:
    270 		if ((flag & FWRITE) == 0)
    271 			error = EPERM;
    272 		else if ((sc->sc_flags & FSS_ACTIVE) == 0)
    273 			error = ENXIO;
    274 		else
    275 			error = fss_delete_snapshot(sc, p);
    276 		break;
    277 
    278 	case FSSIOCGET:
    279 		switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) {
    280 		case FSS_ACTIVE:
    281 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    282 			fsg->fsg_csize = FSS_CLSIZE(sc);
    283 			fsg->fsg_time = sc->sc_time;
    284 			fsg->fsg_mount_size = sc->sc_clcount;
    285 			fsg->fsg_bs_size = sc->sc_clnext;
    286 			error = 0;
    287 			break;
    288 		case FSS_PERSISTENT | FSS_ACTIVE:
    289 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    290 			fsg->fsg_csize = 0;
    291 			fsg->fsg_time = sc->sc_time;
    292 			fsg->fsg_mount_size = 0;
    293 			fsg->fsg_bs_size = 0;
    294 			error = 0;
    295 			break;
    296 		default:
    297 			error = ENXIO;
    298 			break;
    299 		}
    300 		break;
    301 	}
    302 
    303 	FSS_LOCK(sc, s);
    304 	sc->sc_flags &= ~FSS_EXCL;
    305 	FSS_UNLOCK(sc, s);
    306 	wakeup(sc);
    307 
    308 	return error;
    309 }
    310 
    311 int
    312 fss_size(dev_t dev)
    313 {
    314 	return -1;
    315 }
    316 
    317 int
    318 fss_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    319 {
    320 	return EROFS;
    321 }
    322 
    323 /*
    324  * An error occurred reading or writing the snapshot or backing store.
    325  * If it is the first error log to console.
    326  * The caller holds the simplelock.
    327  */
    328 static inline void
    329 fss_error(struct fss_softc *sc, const char *fmt, ...)
    330 {
    331 	va_list ap;
    332 
    333 	if ((sc->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE) {
    334 		va_start(ap, fmt);
    335 		printf("fss%d: snapshot invalid: ", sc->sc_unit);
    336 		vprintf(fmt, ap);
    337 		printf("\n");
    338 		va_end(ap);
    339 	}
    340 	if ((sc->sc_flags & FSS_ACTIVE) == FSS_ACTIVE)
    341 		sc->sc_flags |= FSS_ERROR;
    342 }
    343 
    344 /*
    345  * Allocate the variable sized parts of the softc and
    346  * fork the kernel thread.
    347  *
    348  * The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size
    349  * must be initialized.
    350  */
    351 static int
    352 fss_softc_alloc(struct fss_softc *sc)
    353 {
    354 	int i, len, error;
    355 
    356 	len = (sc->sc_clcount+NBBY-1)/NBBY;
    357 	sc->sc_copied = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    358 	if (sc->sc_copied == NULL)
    359 		return(ENOMEM);
    360 
    361 	len = sc->sc_cache_size*sizeof(struct fss_cache);
    362 	sc->sc_cache = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    363 	if (sc->sc_cache == NULL)
    364 		return(ENOMEM);
    365 
    366 	len = FSS_CLSIZE(sc);
    367 	for (i = 0; i < sc->sc_cache_size; i++) {
    368 		sc->sc_cache[i].fc_type = FSS_CACHE_FREE;
    369 		sc->sc_cache[i].fc_softc = sc;
    370 		sc->sc_cache[i].fc_xfercount = 0;
    371 		sc->sc_cache[i].fc_data = malloc(len, M_TEMP,
    372 		    M_WAITOK|M_CANFAIL);
    373 		if (sc->sc_cache[i].fc_data == NULL)
    374 			return(ENOMEM);
    375 	}
    376 
    377 	len = (sc->sc_indir_size+NBBY-1)/NBBY;
    378 	sc->sc_indir_valid = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    379 	if (sc->sc_indir_valid == NULL)
    380 		return(ENOMEM);
    381 
    382 	len = FSS_CLSIZE(sc);
    383 	sc->sc_indir_data = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    384 	if (sc->sc_indir_data == NULL)
    385 		return(ENOMEM);
    386 
    387 	if ((error = kthread_create1(fss_bs_thread, sc, &sc->sc_bs_proc,
    388 	    "fssbs%d", sc->sc_unit)) != 0)
    389 		return error;
    390 
    391 	sc->sc_flags |= FSS_BS_THREAD;
    392 	return 0;
    393 }
    394 
    395 /*
    396  * Free the variable sized parts of the softc.
    397  */
    398 static void
    399 fss_softc_free(struct fss_softc *sc)
    400 {
    401 	int s, i;
    402 
    403 	if ((sc->sc_flags & FSS_BS_THREAD) != 0) {
    404 		FSS_LOCK(sc, s);
    405 		sc->sc_flags &= ~FSS_BS_THREAD;
    406 		wakeup(&sc->sc_bs_proc);
    407 		while (sc->sc_bs_proc != NULL)
    408 			ltsleep(&sc->sc_bs_proc, PRIBIO, "fssthread", 0,
    409 			    &sc->sc_slock);
    410 		FSS_UNLOCK(sc, s);
    411 	}
    412 
    413 	if (sc->sc_copied != NULL)
    414 		free(sc->sc_copied, M_TEMP);
    415 	sc->sc_copied = NULL;
    416 
    417 	if (sc->sc_cache != NULL) {
    418 		for (i = 0; i < sc->sc_cache_size; i++)
    419 			if (sc->sc_cache[i].fc_data != NULL)
    420 				free(sc->sc_cache[i].fc_data, M_TEMP);
    421 		free(sc->sc_cache, M_TEMP);
    422 	}
    423 	sc->sc_cache = NULL;
    424 
    425 	if (sc->sc_indir_valid != NULL)
    426 		free(sc->sc_indir_valid, M_TEMP);
    427 	sc->sc_indir_valid = NULL;
    428 
    429 	if (sc->sc_indir_data != NULL)
    430 		free(sc->sc_indir_data, M_TEMP);
    431 	sc->sc_indir_data = NULL;
    432 }
    433 
    434 /*
    435  * Check if an unmount is ok. If forced, set this snapshot into ERROR state.
    436  */
    437 int
    438 fss_umount_hook(struct mount *mp, int forced)
    439 {
    440 	int i, s;
    441 
    442 	for (i = 0; i < NFSS; i++) {
    443 		FSS_LOCK(&fss_softc[i], s);
    444 		if ((fss_softc[i].sc_flags & FSS_ACTIVE) != 0 &&
    445 		    fss_softc[i].sc_mount == mp) {
    446 			if (forced)
    447 				fss_error(&fss_softc[i], "forced unmount");
    448 			else {
    449 				FSS_UNLOCK(&fss_softc[i], s);
    450 				return EBUSY;
    451 			}
    452 		}
    453 		FSS_UNLOCK(&fss_softc[i], s);
    454 	}
    455 
    456 	return 0;
    457 }
    458 
    459 /*
    460  * A buffer is written to the snapshotted block device. Copy to
    461  * backing store if needed.
    462  */
    463 static int
    464 fss_copy_on_write(void *v, struct buf *bp)
    465 {
    466 	int s;
    467 	u_int32_t cl, ch, c;
    468 	struct fss_softc *sc = v;
    469 
    470 	FSS_LOCK(sc, s);
    471 	if (!FSS_ISVALID(sc)) {
    472 		FSS_UNLOCK(sc, s);
    473 		return 0;
    474 	}
    475 
    476 	FSS_UNLOCK(sc, s);
    477 
    478 	FSS_STAT_INC(sc, cow_calls);
    479 
    480 	cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
    481 	ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
    482 
    483 	for (c = cl; c <= ch; c++)
    484 		fss_read_cluster(sc, c);
    485 
    486 	return 0;
    487 }
    488 
    489 /*
    490  * Lookup and open needed files.
    491  *
    492  * For file system internal snapshot initializes sc_mntname, sc_mount,
    493  * sc_bs_vp and sc_time.
    494  *
    495  * Otherwise returns dev and size of the underlying block device.
    496  * Initializes sc_mntname, sc_mount_vp, sc_bdev, sc_bs_vp and sc_mount
    497  */
    498 static int
    499 fss_create_files(struct fss_softc *sc, struct fss_set *fss,
    500     off_t *bsize, struct proc *p)
    501 {
    502 	int error, fsbsize;
    503 	struct timespec ts;
    504 	struct partinfo dpart;
    505 	struct vattr va;
    506 	struct nameidata nd;
    507 
    508 	/*
    509 	 * Get the mounted file system.
    510 	 */
    511 
    512 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_mount, p);
    513 	if ((error = namei(&nd)) != 0)
    514 		return error;
    515 
    516 	if ((nd.ni_vp->v_flag & VROOT) != VROOT) {
    517 		vrele(nd.ni_vp);
    518 		return EINVAL;
    519 	}
    520 
    521 	sc->sc_mount = nd.ni_vp->v_mount;
    522 	memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN);
    523 
    524 	vrele(nd.ni_vp);
    525 
    526 	/*
    527 	 * Check for file system internal snapshot.
    528 	 */
    529 
    530 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p);
    531 	if ((error = namei(&nd)) != 0)
    532 		return error;
    533 
    534 	if (nd.ni_vp->v_type == VREG && nd.ni_vp->v_mount == sc->sc_mount) {
    535 		vrele(nd.ni_vp);
    536 		sc->sc_flags |= FSS_PERSISTENT;
    537 
    538 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p);
    539 		if ((error = vn_open(&nd, FREAD, 0)) != 0)
    540 			return error;
    541 		sc->sc_bs_vp = nd.ni_vp;
    542 
    543 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    544 		if (fsbsize & (fsbsize-1))	/* No power of two */
    545 			return EINVAL;
    546 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32;
    547 		    sc->sc_bs_bshift++)
    548 			if (FSS_FSBSIZE(sc) == fsbsize)
    549 				break;
    550 		if (sc->sc_bs_bshift >= 32)
    551 			return EINVAL;
    552 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    553 
    554 		sc->sc_clshift = 0;
    555 
    556 		error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts);
    557 		TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts);
    558 
    559 		VOP_UNLOCK(sc->sc_bs_vp, 0);
    560 
    561 		return error;
    562 	}
    563 	vrele(nd.ni_vp);
    564 
    565 	/*
    566 	 * Get the block device it is mounted on.
    567 	 */
    568 
    569 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
    570 	    sc->sc_mount->mnt_stat.f_mntfromname, p);
    571 	if ((error = namei(&nd)) != 0)
    572 		return error;
    573 
    574 	if (nd.ni_vp->v_type != VBLK) {
    575 		vrele(nd.ni_vp);
    576 		return EINVAL;
    577 	}
    578 
    579 	error = VOP_IOCTL(nd.ni_vp, DIOCGPART, &dpart, FREAD, p->p_ucred, p);
    580 	if (error) {
    581 		vrele(nd.ni_vp);
    582 		return error;
    583 	}
    584 
    585 	sc->sc_mount_vp = nd.ni_vp;
    586 	sc->sc_bdev = nd.ni_vp->v_rdev;
    587 	*bsize = (off_t)dpart.disklab->d_secsize*dpart.part->p_size;
    588 	vrele(nd.ni_vp);
    589 
    590 	/*
    591 	 * Get the backing store
    592 	 */
    593 
    594 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, p);
    595 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
    596 		return error;
    597 	VOP_UNLOCK(nd.ni_vp, 0);
    598 
    599 	sc->sc_bs_vp = nd.ni_vp;
    600 
    601 	if (nd.ni_vp->v_type != VREG && nd.ni_vp->v_type != VCHR)
    602 		return EINVAL;
    603 
    604 	if (sc->sc_bs_vp->v_type == VREG) {
    605 		error = VOP_GETATTR(sc->sc_bs_vp, &va, p->p_ucred, p);
    606 		if (error != 0)
    607 			return error;
    608 		sc->sc_bs_size = va.va_size;
    609 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    610 		if (fsbsize & (fsbsize-1))	/* No power of two */
    611 			return EINVAL;
    612 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32;
    613 		    sc->sc_bs_bshift++)
    614 			if (FSS_FSBSIZE(sc) == fsbsize)
    615 				break;
    616 		if (sc->sc_bs_bshift >= 32)
    617 			return EINVAL;
    618 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    619 		sc->sc_flags |= FSS_BS_ALLOC;
    620 	} else {
    621 		sc->sc_bs_bshift = DEV_BSHIFT;
    622 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    623 		sc->sc_flags &= ~FSS_BS_ALLOC;
    624 	}
    625 
    626 	/*
    627 	 * As all IO to from/to the backing store goes through
    628 	 * VOP_STRATEGY() clean the buffer cache to prevent
    629 	 * cache incoherencies.
    630 	 */
    631 	if ((error = vinvalbuf(sc->sc_bs_vp, V_SAVE, p->p_ucred, p, 0, 0)) != 0)
    632 		return error;
    633 
    634 	return 0;
    635 }
    636 
    637 /*
    638  * Create a snapshot.
    639  */
    640 static int
    641 fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct proc *p)
    642 {
    643 	int len, error;
    644 	u_int32_t csize;
    645 	off_t bsize;
    646 
    647 	/*
    648 	 * Open needed files.
    649 	 */
    650 	if ((error = fss_create_files(sc, fss, &bsize, p)) != 0)
    651 		goto bad;
    652 
    653 	if (sc->sc_flags & FSS_PERSISTENT) {
    654 		fss_softc_alloc(sc);
    655 		sc->sc_flags |= FSS_ACTIVE;
    656 		return 0;
    657 	}
    658 
    659 	/*
    660 	 * Set cluster size. Must be a power of two and
    661 	 * a multiple of backing store block size.
    662 	 */
    663 	if (fss->fss_csize <= 0)
    664 		csize = MAXPHYS;
    665 	else
    666 		csize = fss->fss_csize;
    667 	if (bsize/csize > FSS_CLUSTER_MAX)
    668 		csize = bsize/FSS_CLUSTER_MAX+1;
    669 
    670 	for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32;
    671 	    sc->sc_clshift++)
    672 		if (FSS_CLSIZE(sc) >= csize)
    673 			break;
    674 	if (sc->sc_clshift >= 32) {
    675 		error = EINVAL;
    676 		goto bad;
    677 	}
    678 	sc->sc_clmask = FSS_CLSIZE(sc)-1;
    679 
    680 	/*
    681 	 * Set number of cache slots.
    682 	 */
    683 	if (FSS_CLSIZE(sc) <= 8192)
    684 		sc->sc_cache_size = 32;
    685 	else if (FSS_CLSIZE(sc) <= 65536)
    686 		sc->sc_cache_size = 8;
    687 	else
    688 		sc->sc_cache_size = 4;
    689 
    690 	/*
    691 	 * Set number of clusters and size of last cluster.
    692 	 */
    693 	sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1;
    694 	sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1;
    695 
    696 	/*
    697 	 * Set size of indirect table.
    698 	 */
    699 	len = sc->sc_clcount*sizeof(u_int32_t);
    700 	sc->sc_indir_size = FSS_BTOCL(sc, len)+1;
    701 	sc->sc_clnext = sc->sc_indir_size;
    702 	sc->sc_indir_cur = 0;
    703 
    704 	if ((error = fss_softc_alloc(sc)) != 0)
    705 		goto bad;
    706 
    707 	/*
    708 	 * Activate the snapshot.
    709 	 */
    710 
    711 	if ((error = vfs_write_suspend(sc->sc_mount, PUSER|PCATCH, 0)) != 0)
    712 		goto bad;
    713 
    714 	microtime(&sc->sc_time);
    715 
    716 	if (error == 0)
    717 		error = vn_cow_establish(sc->sc_mount_vp,
    718 		    fss_copy_on_write, sc);
    719 	if (error == 0)
    720 		sc->sc_flags |= FSS_ACTIVE;
    721 
    722 	vfs_write_resume(sc->sc_mount);
    723 
    724 	if (error != 0)
    725 		goto bad;
    726 
    727 #ifdef DEBUG
    728 	printf("fss%d: %s snapshot active\n", sc->sc_unit, sc->sc_mntname);
    729 	printf("fss%d: %u clusters of %u, %u cache slots, %u indir clusters\n",
    730 	    sc->sc_unit, sc->sc_clcount, FSS_CLSIZE(sc),
    731 	    sc->sc_cache_size, sc->sc_indir_size);
    732 #endif
    733 
    734 	return 0;
    735 
    736 bad:
    737 	fss_softc_free(sc);
    738 	if (sc->sc_bs_vp != NULL) {
    739 		if (sc->sc_flags & FSS_PERSISTENT)
    740 			vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p);
    741 		else
    742 			vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p);
    743 	}
    744 	sc->sc_bs_vp = NULL;
    745 
    746 	return error;
    747 }
    748 
    749 /*
    750  * Delete a snapshot.
    751  */
    752 static int
    753 fss_delete_snapshot(struct fss_softc *sc, struct proc *p)
    754 {
    755 	int s;
    756 
    757 	if ((sc->sc_flags & FSS_PERSISTENT) == 0)
    758 		vn_cow_disestablish(sc->sc_mount_vp, fss_copy_on_write, sc);
    759 
    760 	FSS_LOCK(sc, s);
    761 	sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR);
    762 	sc->sc_mount = NULL;
    763 	sc->sc_bdev = NODEV;
    764 	FSS_UNLOCK(sc, s);
    765 
    766 	fss_softc_free(sc);
    767 	if (sc->sc_flags & FSS_PERSISTENT)
    768 		vn_close(sc->sc_bs_vp, FREAD, p->p_ucred, p);
    769 	else
    770 		vn_close(sc->sc_bs_vp, FREAD|FWRITE, p->p_ucred, p);
    771 	sc->sc_bs_vp = NULL;
    772 	sc->sc_flags &= ~FSS_PERSISTENT;
    773 
    774 	FSS_STAT_CLEAR(sc);
    775 
    776 	return 0;
    777 }
    778 
    779 /*
    780  * Get the block address and number of contiguous blocks.
    781  * If the file contains a hole, try to allocate.
    782  * Backing store is locked by caller.
    783  */
    784 static int
    785 fss_bmap(struct fss_softc *sc, off_t start, int len,
    786     struct vnode **vpp, daddr_t *bnp, int *runp)
    787 {
    788 	int l, s, error;
    789 	struct buf *bp, **bpp;
    790 
    791 	if ((sc->sc_bs_vp->v_mount->mnt_flag & MNT_SOFTDEP) != 0)
    792 		bpp = &bp;
    793 	else
    794 		bpp = NULL;
    795 
    796 	error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp);
    797 	if ((error == 0 && *bnp != (daddr_t)-1) ||
    798 	    (sc->sc_flags & FSS_BS_ALLOC) == 0)
    799 		goto out;
    800 
    801 	if (start+len >= sc->sc_bs_size) {
    802 		error = ENOSPC;
    803 		goto out;
    804 	}
    805 
    806 	for (l = 0; l < len; l += FSS_FSBSIZE(sc)) {
    807 		error = VOP_BALLOC(sc->sc_bs_vp, start+l, FSS_FSBSIZE(sc),
    808 		    sc->sc_bs_proc->p_ucred, 0, bpp);
    809 		if (error)
    810 			goto out;
    811 
    812 		if (bpp == NULL)
    813 			continue;
    814 
    815 		s = splbio();
    816 		simple_lock(&bp->b_interlock);
    817 
    818 		if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_start)
    819 			(*bioops.io_start)(bp);
    820 		if (LIST_FIRST(&bp->b_dep) != NULL && bioops.io_complete)
    821 			(*bioops.io_complete)(bp);
    822 
    823 		bp->b_flags |= B_INVAL;
    824 		simple_unlock(&bp->b_interlock);
    825 		splx(s);
    826 
    827 		brelse(bp);
    828 	}
    829 
    830 	error = VOP_BMAP(sc->sc_bs_vp, FSS_BTOFSB(sc, start), vpp, bnp, runp);
    831 
    832 out:
    833 
    834 	if ((sc->sc_flags & FSS_PERSISTENT) == 0 &&
    835 	    error == 0 && *bnp == (daddr_t)-1)
    836 		error = ENOSPC;
    837 
    838 	return error;
    839 }
    840 
    841 /*
    842  * A read from the snapshotted block device has completed.
    843  */
    844 static void
    845 fss_cluster_iodone(struct buf *bp)
    846 {
    847 	int s;
    848 	struct fss_cache *scp = bp->b_private;
    849 
    850 	FSS_LOCK(scp->fc_softc, s);
    851 
    852 	if (bp->b_flags & B_EINTR)
    853 		fss_error(scp->fc_softc, "fs read interrupted");
    854 	if (bp->b_flags & B_ERROR)
    855 		fss_error(scp->fc_softc, "fs read error %d", bp->b_error);
    856 
    857 	if (bp->b_vp != NULL)
    858 		brelvp(bp);
    859 
    860 	if (--scp->fc_xfercount == 0)
    861 		wakeup(&scp->fc_data);
    862 
    863 	FSS_UNLOCK(scp->fc_softc, s);
    864 
    865 	s = splbio();
    866 	pool_put(&bufpool, bp);
    867 	splx(s);
    868 }
    869 
    870 /*
    871  * Read a cluster from the snapshotted block device to the cache.
    872  */
    873 static void
    874 fss_read_cluster(struct fss_softc *sc, u_int32_t cl)
    875 {
    876 	int s, todo, len;
    877 	caddr_t addr;
    878 	daddr_t dblk;
    879 	struct buf *bp;
    880 	struct fss_cache *scp, *scl;
    881 
    882 	/*
    883 	 * Get a free cache slot.
    884 	 */
    885 	scl = sc->sc_cache+sc->sc_cache_size;
    886 
    887 	FSS_LOCK(sc, s);
    888 
    889 restart:
    890 	if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) {
    891 		FSS_UNLOCK(sc, s);
    892 		return;
    893 	}
    894 
    895 	for (scp = sc->sc_cache; scp < scl; scp++)
    896 		if (scp->fc_type != FSS_CACHE_FREE &&
    897 		    scp->fc_cluster == cl) {
    898 			ltsleep(&scp->fc_type, PRIBIO, "cowwait2", 0,
    899 			    &sc->sc_slock);
    900 			goto restart;
    901 		}
    902 
    903 	for (scp = sc->sc_cache; scp < scl; scp++)
    904 		if (scp->fc_type == FSS_CACHE_FREE) {
    905 			scp->fc_type = FSS_CACHE_BUSY;
    906 			scp->fc_cluster = cl;
    907 			break;
    908 		}
    909 	if (scp >= scl) {
    910 		FSS_STAT_INC(sc, cow_cache_full);
    911 		ltsleep(&sc->sc_cache, PRIBIO, "cowwait3", 0, &sc->sc_slock);
    912 		goto restart;
    913 	}
    914 
    915 	FSS_UNLOCK(sc, s);
    916 
    917 	/*
    918 	 * Start the read.
    919 	 */
    920 	FSS_STAT_INC(sc, cow_copied);
    921 
    922 	dblk = btodb(FSS_CLTOB(sc, cl));
    923 	addr = scp->fc_data;
    924 	if (cl == sc->sc_clcount-1) {
    925 		todo = sc->sc_clresid;
    926 		memset(addr+todo, 0, FSS_CLSIZE(sc)-todo);
    927 	} else
    928 		todo = FSS_CLSIZE(sc);
    929 	while (todo > 0) {
    930 		len = todo;
    931 		if (len > MAXPHYS)
    932 			len = MAXPHYS;
    933 
    934 		s = splbio();
    935 		bp = pool_get(&bufpool, PR_WAITOK);
    936 		splx(s);
    937 
    938 		BUF_INIT(bp);
    939 		bp->b_flags = B_READ|B_CALL;
    940 		bp->b_bcount = len;
    941 		bp->b_bufsize = bp->b_bcount;
    942 		bp->b_error = 0;
    943 		bp->b_data = addr;
    944 		bp->b_blkno = bp->b_rawblkno = dblk;
    945 		bp->b_proc = NULL;
    946 		bp->b_dev = sc->sc_bdev;
    947 		bp->b_vp = NULLVP;
    948 		bp->b_private = scp;
    949 		bp->b_iodone = fss_cluster_iodone;
    950 
    951 		DEV_STRATEGY(bp);
    952 
    953 		FSS_LOCK(sc, s);
    954 		scp->fc_xfercount++;
    955 		FSS_UNLOCK(sc, s);
    956 
    957 		dblk += btodb(len);
    958 		addr += len;
    959 		todo -= len;
    960 	}
    961 
    962 	/*
    963 	 * Wait for all read requests to complete.
    964 	 */
    965 	FSS_LOCK(sc, s);
    966 	while (scp->fc_xfercount > 0)
    967 		ltsleep(&scp->fc_data, PRIBIO, "cowwait", 0, &sc->sc_slock);
    968 
    969 	scp->fc_type = FSS_CACHE_VALID;
    970 	setbit(sc->sc_copied, scp->fc_cluster);
    971 	FSS_UNLOCK(sc, s);
    972 
    973 	wakeup(&sc->sc_bs_proc);
    974 }
    975 
    976 /*
    977  * Write a cluster from the cache to the backing store.
    978  */
    979 static int
    980 fss_write_cluster(struct fss_cache *scp, u_int32_t cl)
    981 {
    982 	int s, error, todo, len, nra;
    983 	daddr_t nbn;
    984 	caddr_t addr;
    985 	off_t pos;
    986 	struct buf *bp;
    987 	struct vnode *vp;
    988 	struct fss_softc *sc;
    989 
    990 	error = 0;
    991 	sc = scp->fc_softc;
    992 
    993 	pos = FSS_CLTOB(sc, cl);
    994 	addr = scp->fc_data;
    995 	todo = FSS_CLSIZE(sc);
    996 
    997 	vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY);
    998 	simple_lock(&sc->sc_bs_vp->v_interlock);
    999 	error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(pos),
   1000 	    round_page(pos+todo), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
   1001 
   1002 	while (error == 0 && todo > 0) {
   1003 		error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra);
   1004 		if (error)
   1005 			break;
   1006 
   1007 		len = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos);
   1008 		if (len > todo)
   1009 			len = todo;
   1010 
   1011 		s = splbio();
   1012 		bp = pool_get(&bufpool, PR_WAITOK);
   1013 		splx(s);
   1014 
   1015 		BUF_INIT(bp);
   1016 		bp->b_flags = B_CALL;
   1017 		bp->b_bcount = len;
   1018 		bp->b_bufsize = bp->b_bcount;
   1019 		bp->b_error = 0;
   1020 		bp->b_data = addr;
   1021 		bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos));
   1022 		bp->b_proc = NULL;
   1023 		bp->b_vp = NULLVP;
   1024 		bp->b_private = scp;
   1025 		bp->b_iodone = fss_cluster_iodone;
   1026 		bgetvp(vp, bp);
   1027 		bp->b_vp->v_numoutput++;
   1028 
   1029 		BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1030 		VOP_STRATEGY(vp, bp);
   1031 
   1032 		FSS_LOCK(sc, s);
   1033 		scp->fc_xfercount++;
   1034 		FSS_UNLOCK(sc, s);
   1035 
   1036 		pos += len;
   1037 		addr += len;
   1038 		todo -= len;
   1039 	}
   1040 
   1041 	/*
   1042 	 * Wait for all write requests to complete.
   1043 	 */
   1044 	FSS_LOCK(sc, s);
   1045 	while (scp->fc_xfercount > 0)
   1046 		ltsleep(&scp->fc_data, PRIBIO, "bswwait", 0, &sc->sc_slock);
   1047 	FSS_UNLOCK(sc, s);
   1048 
   1049 	VOP_UNLOCK(sc->sc_bs_vp, 0);
   1050 
   1051 	return error;
   1052 }
   1053 
   1054 /*
   1055  * Read/write clusters from/to backing store.
   1056  * For persistent snapshots must be called with cl == 0. off is the
   1057  * offset into the snapshot.
   1058  */
   1059 static int
   1060 fss_bs_io(struct fss_softc *sc, fss_io_type rw,
   1061     u_int32_t cl, off_t off, int len, caddr_t data)
   1062 {
   1063 	int s, error, todo, count, nra;
   1064 	off_t pos;
   1065 	daddr_t nbn;
   1066 	struct buf *bp;
   1067 	struct vnode *vp;
   1068 
   1069 	todo = len;
   1070 	pos = FSS_CLTOB(sc, cl)+off;
   1071 	error = 0;
   1072 
   1073 	vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY);
   1074 	simple_lock(&sc->sc_bs_vp->v_interlock);
   1075 	error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(pos),
   1076 	    round_page(pos+todo), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
   1077 
   1078 	while (error == 0 && todo > 0) {
   1079 		error = fss_bmap(sc, pos, todo, &vp, &nbn, &nra);
   1080 		if (error)
   1081 			break;
   1082 
   1083 		count = FSS_FSBTOB(sc, nra+1)-FSS_FSBOFF(sc, pos);
   1084 		if (count > todo)
   1085 			count = todo;
   1086 
   1087 		if (nbn == (daddr_t)-1) {
   1088 			bzero(data, count);
   1089 			todo -= count;
   1090 			data += count;
   1091 			pos += count;
   1092 			continue;
   1093 		}
   1094 
   1095 		s = splbio();
   1096 		bp = pool_get(&bufpool, PR_WAITOK);
   1097 		splx(s);
   1098 
   1099 		BUF_INIT(bp);
   1100 		bp->b_flags = (rw == FSS_READ ? B_READ : 0);
   1101 		bp->b_bcount = count;
   1102 		bp->b_bufsize = bp->b_bcount;
   1103 		bp->b_error = 0;
   1104 		bp->b_data = data;
   1105 		bp->b_blkno = bp->b_rawblkno = nbn+btodb(FSS_FSBOFF(sc, pos));
   1106 		bp->b_proc = NULL;
   1107 		bp->b_vp = NULLVP;
   1108 		bgetvp(vp, bp);
   1109 		if ((bp->b_flags & B_READ) == 0)
   1110 			bp->b_vp->v_numoutput++;
   1111 
   1112 		if ((bp->b_flags & B_READ) == 0 || cl < sc->sc_indir_size)
   1113 			BIO_SETPRIO(bp, BPRIO_TIMECRITICAL);
   1114 		VOP_STRATEGY(vp, bp);
   1115 
   1116 		error = biowait(bp);
   1117 
   1118 		if (bp->b_vp != NULL)
   1119 			brelvp(bp);
   1120 
   1121 		s = splbio();
   1122 		pool_put(&bufpool, bp);
   1123 		splx(s);
   1124 
   1125 		if (error)
   1126 			break;
   1127 
   1128 		todo -= count;
   1129 		data += count;
   1130 		pos += count;
   1131 	}
   1132 
   1133 	VOP_UNLOCK(sc->sc_bs_vp, 0);
   1134 
   1135 	return error;
   1136 }
   1137 
   1138 /*
   1139  * Get a pointer to the indirect slot for this cluster.
   1140  */
   1141 static u_int32_t *
   1142 fss_bs_indir(struct fss_softc *sc, u_int32_t cl)
   1143 {
   1144 	u_int32_t icl;
   1145 	int ioff;
   1146 
   1147 	icl = cl/(FSS_CLSIZE(sc)/sizeof(u_int32_t));
   1148 	ioff = cl%(FSS_CLSIZE(sc)/sizeof(u_int32_t));
   1149 
   1150 	if (sc->sc_indir_cur == icl)
   1151 		return &sc->sc_indir_data[ioff];
   1152 
   1153 	if (sc->sc_indir_dirty) {
   1154 		FSS_STAT_INC(sc, indir_write);
   1155 		if (fss_bs_io(sc, FSS_WRITE, sc->sc_indir_cur, 0,
   1156 		    FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0)
   1157 			return NULL;
   1158 		setbit(sc->sc_indir_valid, sc->sc_indir_cur);
   1159 	}
   1160 
   1161 	sc->sc_indir_dirty = 0;
   1162 	sc->sc_indir_cur = icl;
   1163 
   1164 	if (isset(sc->sc_indir_valid, sc->sc_indir_cur)) {
   1165 		FSS_STAT_INC(sc, indir_read);
   1166 		if (fss_bs_io(sc, FSS_READ, sc->sc_indir_cur, 0,
   1167 		    FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0)
   1168 			return NULL;
   1169 	} else
   1170 		memset(sc->sc_indir_data, 0, FSS_CLSIZE(sc));
   1171 
   1172 	return &sc->sc_indir_data[ioff];
   1173 }
   1174 
   1175 /*
   1176  * The kernel thread (one for every active snapshot).
   1177  *
   1178  * After wakeup it cleans the cache and runs the I/O requests.
   1179  */
   1180 static void
   1181 fss_bs_thread(void *arg)
   1182 {
   1183 	int error, len, nfreed, nio, s;
   1184 	long off;
   1185 	caddr_t addr;
   1186 	u_int32_t c, cl, ch, *indirp;
   1187 	struct buf *bp, *nbp;
   1188 	struct fss_softc *sc;
   1189 	struct fss_cache *scp, *scl;
   1190 
   1191 	sc = arg;
   1192 
   1193 	scl = sc->sc_cache+sc->sc_cache_size;
   1194 
   1195 	s = splbio();
   1196 	nbp = pool_get(&bufpool, PR_WAITOK);
   1197 	splx(s);
   1198 
   1199 	nfreed = nio = 1;		/* Dont sleep the first time */
   1200 
   1201 	FSS_LOCK(sc, s);
   1202 
   1203 	for (;;) {
   1204 		if (nfreed == 0 && nio == 0)
   1205 			ltsleep(&sc->sc_bs_proc, PVM-1, "fssbs", 0,
   1206 			    &sc->sc_slock);
   1207 
   1208 		if ((sc->sc_flags & FSS_BS_THREAD) == 0) {
   1209 			sc->sc_bs_proc = NULL;
   1210 			wakeup(&sc->sc_bs_proc);
   1211 
   1212 			FSS_UNLOCK(sc, s);
   1213 
   1214 			s = splbio();
   1215 			pool_put(&bufpool, nbp);
   1216 			splx(s);
   1217 #ifdef FSS_STATISTICS
   1218 			if ((sc->sc_flags & FSS_PERSISTENT) == 0) {
   1219 				printf("fss%d: cow called %" PRId64 " times,"
   1220 				    " copied %" PRId64 " clusters,"
   1221 				    " cache full %" PRId64 " times\n",
   1222 				    sc->sc_unit,
   1223 				    FSS_STAT_VAL(sc, cow_calls),
   1224 				    FSS_STAT_VAL(sc, cow_copied),
   1225 				    FSS_STAT_VAL(sc, cow_cache_full));
   1226 				printf("fss%d: %" PRId64 " indir reads,"
   1227 				    " %" PRId64 " indir writes\n",
   1228 				    sc->sc_unit,
   1229 				    FSS_STAT_VAL(sc, indir_read),
   1230 				    FSS_STAT_VAL(sc, indir_write));
   1231 			}
   1232 #endif /* FSS_STATISTICS */
   1233 			kthread_exit(0);
   1234 		}
   1235 
   1236 		/*
   1237 		 * Process I/O requests (persistent)
   1238 		 */
   1239 
   1240 		if (sc->sc_flags & FSS_PERSISTENT) {
   1241 			nfreed = nio = 0;
   1242 
   1243 			if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL)
   1244 				continue;
   1245 
   1246 			nio++;
   1247 
   1248 			if (FSS_ISVALID(sc)) {
   1249 				FSS_UNLOCK(sc, s);
   1250 
   1251 				error = fss_bs_io(sc, FSS_READ, 0,
   1252 				    dbtob(bp->b_blkno), bp->b_bcount,
   1253 				    bp->b_data);
   1254 
   1255 				FSS_LOCK(sc, s);
   1256 			} else
   1257 				error = ENXIO;
   1258 
   1259 			if (error) {
   1260 				bp->b_error = error;
   1261 				bp->b_flags |= B_ERROR;
   1262 				bp->b_resid = bp->b_bcount;
   1263 			}
   1264 			biodone(bp);
   1265 
   1266 			continue;
   1267 		}
   1268 
   1269 		/*
   1270 		 * Clean the cache
   1271 		 */
   1272 		nfreed = 0;
   1273 		for (scp = sc->sc_cache; scp < scl; scp++) {
   1274 			if (scp->fc_type != FSS_CACHE_VALID)
   1275 				continue;
   1276 
   1277 			FSS_UNLOCK(sc, s);
   1278 
   1279 			indirp = fss_bs_indir(sc, scp->fc_cluster);
   1280 			if (indirp != NULL) {
   1281 				error = fss_write_cluster(scp, sc->sc_clnext);
   1282 			} else
   1283 				error = EIO;
   1284 
   1285 			FSS_LOCK(sc, s);
   1286 
   1287 			if (error == 0) {
   1288 				*indirp = sc->sc_clnext++;
   1289 				sc->sc_indir_dirty = 1;
   1290 			} else
   1291 				fss_error(sc, "write bs error %d", error);
   1292 
   1293 			scp->fc_type = FSS_CACHE_FREE;
   1294 			nfreed++;
   1295 			wakeup(&scp->fc_type);
   1296 		}
   1297 
   1298 		if (nfreed)
   1299 			wakeup(&sc->sc_cache);
   1300 
   1301 		/*
   1302 		 * Process I/O requests
   1303 		 */
   1304 		nio = 0;
   1305 
   1306 		if ((bp = BUFQ_GET(&sc->sc_bufq)) == NULL)
   1307 			continue;
   1308 
   1309 		nio++;
   1310 
   1311 		if (!FSS_ISVALID(sc)) {
   1312 			bp->b_error = ENXIO;
   1313 			bp->b_flags |= B_ERROR;
   1314 			bp->b_resid = bp->b_bcount;
   1315 			biodone(bp);
   1316 			continue;
   1317 		}
   1318 
   1319 		/*
   1320 		 * First read from the snapshotted block device.
   1321 		 * XXX Split to only read those parts that have not
   1322 		 * been saved to backing store?
   1323 		 */
   1324 
   1325 		FSS_UNLOCK(sc, s);
   1326 
   1327 		BUF_INIT(nbp);
   1328 		nbp->b_flags = B_READ;
   1329 		nbp->b_bcount = bp->b_bcount;
   1330 		nbp->b_bufsize = bp->b_bcount;
   1331 		nbp->b_error = 0;
   1332 		nbp->b_data = bp->b_data;
   1333 		nbp->b_blkno = nbp->b_rawblkno = bp->b_blkno;
   1334 		nbp->b_proc = bp->b_proc;
   1335 		nbp->b_dev = sc->sc_bdev;
   1336 		nbp->b_vp = NULLVP;
   1337 
   1338 		DEV_STRATEGY(nbp);
   1339 
   1340 		if (biowait(nbp) != 0) {
   1341 			bp->b_resid = bp->b_bcount;
   1342 			bp->b_error = nbp->b_error;
   1343 			bp->b_flags |= B_ERROR;
   1344 			biodone(bp);
   1345 			continue;
   1346 		}
   1347 
   1348 		cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
   1349 		off = FSS_CLOFF(sc, dbtob(bp->b_blkno));
   1350 		ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
   1351 		bp->b_resid = bp->b_bcount;
   1352 		addr = bp->b_data;
   1353 
   1354 		FSS_LOCK(sc, s);
   1355 
   1356 		/*
   1357 		 * Replace those parts that have been saved to backing store.
   1358 		 */
   1359 
   1360 		for (c = cl; c <= ch;
   1361 		    c++, off = 0, bp->b_resid -= len, addr += len) {
   1362 			len = FSS_CLSIZE(sc)-off;
   1363 			if (len > bp->b_resid)
   1364 				len = bp->b_resid;
   1365 
   1366 			if (isclr(sc->sc_copied, c))
   1367 				continue;
   1368 
   1369 			FSS_UNLOCK(sc, s);
   1370 
   1371 			indirp = fss_bs_indir(sc, c);
   1372 
   1373 			FSS_LOCK(sc, s);
   1374 
   1375 			if (indirp == NULL || *indirp == 0) {
   1376 				/*
   1377 				 * Not on backing store. Either in cache
   1378 				 * or hole in the snapshotted block device.
   1379 				 */
   1380 				for (scp = sc->sc_cache; scp < scl; scp++)
   1381 					if (scp->fc_type == FSS_CACHE_VALID &&
   1382 					    scp->fc_cluster == c)
   1383 						break;
   1384 				if (scp < scl)
   1385 					memcpy(addr, scp->fc_data+off, len);
   1386 				else
   1387 					memset(addr, 0, len);
   1388 				continue;
   1389 			}
   1390 			/*
   1391 			 * Read from backing store.
   1392 			 */
   1393 
   1394 			FSS_UNLOCK(sc, s);
   1395 
   1396 			if ((error = fss_bs_io(sc, FSS_READ, *indirp,
   1397 			    off, len, addr)) != 0) {
   1398 				bp->b_resid = bp->b_bcount;
   1399 				bp->b_error = error;
   1400 				bp->b_flags |= B_ERROR;
   1401 				break;
   1402 			}
   1403 
   1404 			FSS_LOCK(sc, s);
   1405 
   1406 		}
   1407 
   1408 		biodone(bp);
   1409 	}
   1410 }
   1411