Home | History | Annotate | Line # | Download | only in dev
fss.c revision 1.24
      1 /*	$NetBSD: fss.c,v 1.24 2006/04/13 15:56:53 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.24 2006/04/13 15:56:53 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 lwp *);
    132 static int fss_create_snapshot(struct fss_softc *, struct fss_set *,
    133     struct lwp *);
    134 static int fss_delete_snapshot(struct fss_softc *, struct lwp *);
    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 void fss_bs_thread(void *);
    140 static int fss_bs_io(struct fss_softc *, fss_io_type,
    141     u_int32_t, off_t, int, caddr_t);
    142 static u_int32_t *fss_bs_indir(struct fss_softc *, u_int32_t);
    143 
    144 const struct bdevsw fss_bdevsw = {
    145 	fss_open, fss_close, fss_strategy, fss_ioctl,
    146 	fss_dump, fss_size, D_DISK
    147 };
    148 
    149 const struct cdevsw fss_cdevsw = {
    150 	fss_open, fss_close, fss_read, fss_write, fss_ioctl,
    151 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    152 };
    153 
    154 void
    155 fssattach(int num)
    156 {
    157 	int i;
    158 	struct fss_softc *sc;
    159 
    160 	for (i = 0; i < NFSS; i++) {
    161 		sc = &fss_softc[i];
    162 		sc->sc_unit = i;
    163 		sc->sc_bdev = NODEV;
    164 		simple_lock_init(&sc->sc_slock);
    165 		lockinit(&sc->sc_lock, PRIBIO, "fsslock", 0, 0);
    166 		bufq_alloc(&sc->sc_bufq, "fcfs", 0);
    167 	}
    168 }
    169 
    170 int
    171 fss_open(dev_t dev, int flags, int mode, struct lwp *l)
    172 {
    173 	int s, mflag;
    174 	struct fss_softc *sc;
    175 
    176 	mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN);
    177 
    178 	if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
    179 		return ENODEV;
    180 
    181 	FSS_LOCK(sc, s);
    182 
    183 	sc->sc_flags |= mflag;
    184 
    185 	FSS_UNLOCK(sc, s);
    186 
    187 	return 0;
    188 }
    189 
    190 int
    191 fss_close(dev_t dev, int flags, int mode, struct lwp *l)
    192 {
    193 	int s, mflag, error;
    194 	struct fss_softc *sc;
    195 
    196 	mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN);
    197 
    198 	if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
    199 		return ENODEV;
    200 
    201 	FSS_LOCK(sc, s);
    202 
    203 	if ((sc->sc_flags & (FSS_CDEV_OPEN|FSS_BDEV_OPEN)) == mflag) {
    204 		if ((sc->sc_uflags & FSS_UNCONFIG_ON_CLOSE) != 0 &&
    205 		    (sc->sc_flags & FSS_ACTIVE) != 0) {
    206 			FSS_UNLOCK(sc, s);
    207 			error = fss_ioctl(dev, FSSIOCCLR, NULL, FWRITE, l);
    208 			if (error)
    209 				return error;
    210 			FSS_LOCK(sc, s);
    211 		}
    212 		sc->sc_uflags &= ~FSS_UNCONFIG_ON_CLOSE;
    213 	}
    214 
    215 	sc->sc_flags &= ~mflag;
    216 
    217 	FSS_UNLOCK(sc, s);
    218 
    219 	return 0;
    220 }
    221 
    222 void
    223 fss_strategy(struct buf *bp)
    224 {
    225 	int s;
    226 	struct fss_softc *sc;
    227 
    228 	sc = FSS_DEV_TO_SOFTC(bp->b_dev);
    229 
    230 	FSS_LOCK(sc, s);
    231 
    232 	if ((bp->b_flags & B_READ) != B_READ ||
    233 	    sc == NULL || !FSS_ISVALID(sc)) {
    234 
    235 		FSS_UNLOCK(sc, s);
    236 
    237 		bp->b_error = (sc == NULL ? ENODEV : EROFS);
    238 		bp->b_flags |= B_ERROR;
    239 		bp->b_resid = bp->b_bcount;
    240 		biodone(bp);
    241 		return;
    242 	}
    243 
    244 	bp->b_rawblkno = bp->b_blkno;
    245 	BUFQ_PUT(sc->sc_bufq, bp);
    246 	wakeup(&sc->sc_bs_proc);
    247 
    248 	FSS_UNLOCK(sc, s);
    249 }
    250 
    251 int
    252 fss_read(dev_t dev, struct uio *uio, int flags)
    253 {
    254 	return physio(fss_strategy, NULL, dev, B_READ, minphys, uio);
    255 }
    256 
    257 int
    258 fss_write(dev_t dev, struct uio *uio, int flags)
    259 {
    260 	return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio);
    261 }
    262 
    263 int
    264 fss_ioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct lwp *l)
    265 {
    266 	int error;
    267 	struct fss_softc *sc;
    268 	struct fss_set *fss = (struct fss_set *)data;
    269 	struct fss_get *fsg = (struct fss_get *)data;
    270 
    271 	if ((sc = FSS_DEV_TO_SOFTC(dev)) == NULL)
    272 		return ENODEV;
    273 
    274 	switch (cmd) {
    275 	case FSSIOCSET:
    276 		lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
    277 		if ((flag & FWRITE) == 0)
    278 			error = EPERM;
    279 		else if ((sc->sc_flags & FSS_ACTIVE) != 0)
    280 			error = EBUSY;
    281 		else
    282 			error = fss_create_snapshot(sc, fss, l);
    283 		lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
    284 		break;
    285 
    286 	case FSSIOCCLR:
    287 		lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
    288 		if ((flag & FWRITE) == 0)
    289 			error = EPERM;
    290 		else if ((sc->sc_flags & FSS_ACTIVE) == 0)
    291 			error = ENXIO;
    292 		else
    293 			error = fss_delete_snapshot(sc, l);
    294 		lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
    295 		break;
    296 
    297 	case FSSIOCGET:
    298 		lockmgr(&sc->sc_lock, LK_EXCLUSIVE, NULL);
    299 		switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) {
    300 		case FSS_ACTIVE:
    301 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    302 			fsg->fsg_csize = FSS_CLSIZE(sc);
    303 			fsg->fsg_time = sc->sc_time;
    304 			fsg->fsg_mount_size = sc->sc_clcount;
    305 			fsg->fsg_bs_size = sc->sc_clnext;
    306 			error = 0;
    307 			break;
    308 		case FSS_PERSISTENT | FSS_ACTIVE:
    309 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    310 			fsg->fsg_csize = 0;
    311 			fsg->fsg_time = sc->sc_time;
    312 			fsg->fsg_mount_size = 0;
    313 			fsg->fsg_bs_size = 0;
    314 			error = 0;
    315 			break;
    316 		default:
    317 			error = ENXIO;
    318 			break;
    319 		}
    320 		lockmgr(&sc->sc_lock, LK_RELEASE, NULL);
    321 		break;
    322 
    323 	case FSSIOFSET:
    324 		sc->sc_uflags = *(int *)data;
    325 		error = 0;
    326 		break;
    327 
    328 	case FSSIOFGET:
    329 		*(int *)data = sc->sc_uflags;
    330 		error = 0;
    331 		break;
    332 
    333 	default:
    334 		error = EINVAL;
    335 		break;
    336 	}
    337 
    338 	return error;
    339 }
    340 
    341 int
    342 fss_size(dev_t dev)
    343 {
    344 	return -1;
    345 }
    346 
    347 int
    348 fss_dump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
    349 {
    350 	return EROFS;
    351 }
    352 
    353 /*
    354  * An error occurred reading or writing the snapshot or backing store.
    355  * If it is the first error log to console.
    356  * The caller holds the simplelock.
    357  */
    358 static inline void
    359 fss_error(struct fss_softc *sc, const char *fmt, ...)
    360 {
    361 	va_list ap;
    362 
    363 	if ((sc->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE) {
    364 		va_start(ap, fmt);
    365 		printf("fss%d: snapshot invalid: ", sc->sc_unit);
    366 		vprintf(fmt, ap);
    367 		printf("\n");
    368 		va_end(ap);
    369 	}
    370 	if ((sc->sc_flags & FSS_ACTIVE) == FSS_ACTIVE)
    371 		sc->sc_flags |= FSS_ERROR;
    372 }
    373 
    374 /*
    375  * Allocate the variable sized parts of the softc and
    376  * fork the kernel thread.
    377  *
    378  * The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size
    379  * must be initialized.
    380  */
    381 static int
    382 fss_softc_alloc(struct fss_softc *sc)
    383 {
    384 	int i, len, error;
    385 
    386 	len = (sc->sc_clcount+NBBY-1)/NBBY;
    387 	sc->sc_copied = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    388 	if (sc->sc_copied == NULL)
    389 		return(ENOMEM);
    390 
    391 	len = sc->sc_cache_size*sizeof(struct fss_cache);
    392 	sc->sc_cache = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    393 	if (sc->sc_cache == NULL)
    394 		return(ENOMEM);
    395 
    396 	len = FSS_CLSIZE(sc);
    397 	for (i = 0; i < sc->sc_cache_size; i++) {
    398 		sc->sc_cache[i].fc_type = FSS_CACHE_FREE;
    399 		sc->sc_cache[i].fc_softc = sc;
    400 		sc->sc_cache[i].fc_xfercount = 0;
    401 		sc->sc_cache[i].fc_data = malloc(len, M_TEMP,
    402 		    M_WAITOK|M_CANFAIL);
    403 		if (sc->sc_cache[i].fc_data == NULL)
    404 			return(ENOMEM);
    405 	}
    406 
    407 	len = (sc->sc_indir_size+NBBY-1)/NBBY;
    408 	sc->sc_indir_valid = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    409 	if (sc->sc_indir_valid == NULL)
    410 		return(ENOMEM);
    411 
    412 	len = FSS_CLSIZE(sc);
    413 	sc->sc_indir_data = malloc(len, M_TEMP, M_ZERO|M_WAITOK|M_CANFAIL);
    414 	if (sc->sc_indir_data == NULL)
    415 		return(ENOMEM);
    416 
    417 	if ((error = kthread_create1(fss_bs_thread, sc, &sc->sc_bs_proc,
    418 	    "fssbs%d", sc->sc_unit)) != 0)
    419 		return error;
    420 
    421 	sc->sc_flags |= FSS_BS_THREAD;
    422 	return 0;
    423 }
    424 
    425 /*
    426  * Free the variable sized parts of the softc.
    427  */
    428 static void
    429 fss_softc_free(struct fss_softc *sc)
    430 {
    431 	int s, i;
    432 
    433 	if ((sc->sc_flags & FSS_BS_THREAD) != 0) {
    434 		FSS_LOCK(sc, s);
    435 		sc->sc_flags &= ~FSS_BS_THREAD;
    436 		wakeup(&sc->sc_bs_proc);
    437 		while (sc->sc_bs_proc != NULL)
    438 			ltsleep(&sc->sc_bs_proc, PRIBIO, "fssthread", 0,
    439 			    &sc->sc_slock);
    440 		FSS_UNLOCK(sc, s);
    441 	}
    442 
    443 	if (sc->sc_copied != NULL)
    444 		free(sc->sc_copied, M_TEMP);
    445 	sc->sc_copied = NULL;
    446 
    447 	if (sc->sc_cache != NULL) {
    448 		for (i = 0; i < sc->sc_cache_size; i++)
    449 			if (sc->sc_cache[i].fc_data != NULL)
    450 				free(sc->sc_cache[i].fc_data, M_TEMP);
    451 		free(sc->sc_cache, M_TEMP);
    452 	}
    453 	sc->sc_cache = NULL;
    454 
    455 	if (sc->sc_indir_valid != NULL)
    456 		free(sc->sc_indir_valid, M_TEMP);
    457 	sc->sc_indir_valid = NULL;
    458 
    459 	if (sc->sc_indir_data != NULL)
    460 		free(sc->sc_indir_data, M_TEMP);
    461 	sc->sc_indir_data = NULL;
    462 }
    463 
    464 /*
    465  * Check if an unmount is ok. If forced, set this snapshot into ERROR state.
    466  */
    467 int
    468 fss_umount_hook(struct mount *mp, int forced)
    469 {
    470 	int i, s;
    471 
    472 	for (i = 0; i < NFSS; i++) {
    473 		FSS_LOCK(&fss_softc[i], s);
    474 		if ((fss_softc[i].sc_flags & FSS_ACTIVE) != 0 &&
    475 		    fss_softc[i].sc_mount == mp) {
    476 			if (forced)
    477 				fss_error(&fss_softc[i], "forced unmount");
    478 			else {
    479 				FSS_UNLOCK(&fss_softc[i], s);
    480 				return EBUSY;
    481 			}
    482 		}
    483 		FSS_UNLOCK(&fss_softc[i], s);
    484 	}
    485 
    486 	return 0;
    487 }
    488 
    489 /*
    490  * A buffer is written to the snapshotted block device. Copy to
    491  * backing store if needed.
    492  */
    493 static int
    494 fss_copy_on_write(void *v, struct buf *bp)
    495 {
    496 	int s;
    497 	u_int32_t cl, ch, c;
    498 	struct fss_softc *sc = v;
    499 
    500 	FSS_LOCK(sc, s);
    501 	if (!FSS_ISVALID(sc)) {
    502 		FSS_UNLOCK(sc, s);
    503 		return 0;
    504 	}
    505 
    506 	FSS_UNLOCK(sc, s);
    507 
    508 	FSS_STAT_INC(sc, cow_calls);
    509 
    510 	cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
    511 	ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
    512 
    513 	for (c = cl; c <= ch; c++)
    514 		fss_read_cluster(sc, c);
    515 
    516 	return 0;
    517 }
    518 
    519 /*
    520  * Lookup and open needed files.
    521  *
    522  * For file system internal snapshot initializes sc_mntname, sc_mount,
    523  * sc_bs_vp and sc_time.
    524  *
    525  * Otherwise returns dev and size of the underlying block device.
    526  * Initializes sc_mntname, sc_mount_vp, sc_bdev, sc_bs_vp and sc_mount
    527  */
    528 static int
    529 fss_create_files(struct fss_softc *sc, struct fss_set *fss,
    530     off_t *bsize, struct lwp *l)
    531 {
    532 	int error, bits, fsbsize;
    533 	struct timespec ts;
    534 	struct partinfo dpart;
    535 	struct vattr va;
    536 	struct nameidata nd;
    537 
    538 	/*
    539 	 * Get the mounted file system.
    540 	 */
    541 
    542 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_mount, l);
    543 	if ((error = namei(&nd)) != 0)
    544 		return error;
    545 
    546 	if ((nd.ni_vp->v_flag & VROOT) != VROOT) {
    547 		vrele(nd.ni_vp);
    548 		return EINVAL;
    549 	}
    550 
    551 	sc->sc_mount = nd.ni_vp->v_mount;
    552 	memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN);
    553 
    554 	vrele(nd.ni_vp);
    555 
    556 	/*
    557 	 * Check for file system internal snapshot.
    558 	 */
    559 
    560 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, l);
    561 	if ((error = namei(&nd)) != 0)
    562 		return error;
    563 
    564 	if (nd.ni_vp->v_type == VREG && nd.ni_vp->v_mount == sc->sc_mount) {
    565 		vrele(nd.ni_vp);
    566 		sc->sc_flags |= FSS_PERSISTENT;
    567 
    568 		NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, l);
    569 		if ((error = vn_open(&nd, FREAD, 0)) != 0)
    570 			return error;
    571 		sc->sc_bs_vp = nd.ni_vp;
    572 
    573 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    574 		bits = sizeof(sc->sc_bs_bshift)*NBBY;
    575 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < bits;
    576 		    sc->sc_bs_bshift++)
    577 			if (FSS_FSBSIZE(sc) == fsbsize)
    578 				break;
    579 		if (sc->sc_bs_bshift >= bits) {
    580 			VOP_UNLOCK(sc->sc_bs_vp, 0);
    581 			return EINVAL;
    582 		}
    583 
    584 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    585 		sc->sc_clshift = 0;
    586 
    587 		error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts);
    588 		TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts);
    589 
    590 		VOP_UNLOCK(sc->sc_bs_vp, 0);
    591 
    592 		return error;
    593 	}
    594 	vrele(nd.ni_vp);
    595 
    596 	/*
    597 	 * Get the block device it is mounted on.
    598 	 */
    599 
    600 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
    601 	    sc->sc_mount->mnt_stat.f_mntfromname, l);
    602 	if ((error = namei(&nd)) != 0)
    603 		return error;
    604 
    605 	if (nd.ni_vp->v_type != VBLK) {
    606 		vrele(nd.ni_vp);
    607 		return EINVAL;
    608 	}
    609 
    610 	error = VOP_IOCTL(nd.ni_vp, DIOCGPART, &dpart, FREAD,
    611 	    l->l_proc->p_ucred, l);
    612 	if (error) {
    613 		vrele(nd.ni_vp);
    614 		return error;
    615 	}
    616 
    617 	sc->sc_mount_vp = nd.ni_vp;
    618 	sc->sc_bdev = nd.ni_vp->v_rdev;
    619 	*bsize = (off_t)dpart.disklab->d_secsize*dpart.part->p_size;
    620 	vrele(nd.ni_vp);
    621 
    622 	/*
    623 	 * Get the backing store
    624 	 */
    625 
    626 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore, l);
    627 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
    628 		return error;
    629 	VOP_UNLOCK(nd.ni_vp, 0);
    630 
    631 	sc->sc_bs_vp = nd.ni_vp;
    632 
    633 	if (nd.ni_vp->v_type != VREG && nd.ni_vp->v_type != VCHR)
    634 		return EINVAL;
    635 
    636 	if (sc->sc_bs_vp->v_type == VREG) {
    637 		error = VOP_GETATTR(sc->sc_bs_vp, &va, l->l_proc->p_ucred, l);
    638 		if (error != 0)
    639 			return error;
    640 		sc->sc_bs_size = va.va_size;
    641 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    642 		if (fsbsize & (fsbsize-1))	/* No power of two */
    643 			return EINVAL;
    644 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32;
    645 		    sc->sc_bs_bshift++)
    646 			if (FSS_FSBSIZE(sc) == fsbsize)
    647 				break;
    648 		if (sc->sc_bs_bshift >= 32)
    649 			return EINVAL;
    650 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    651 	} else {
    652 		sc->sc_bs_bshift = DEV_BSHIFT;
    653 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    654 	}
    655 
    656 	/*
    657 	 * As all IO to from/to the backing store goes through
    658 	 * VOP_STRATEGY() clean the buffer cache to prevent
    659 	 * cache incoherencies.
    660 	 */
    661 	if ((error = vinvalbuf(sc->sc_bs_vp, V_SAVE, l->l_proc->p_ucred, l, 0, 0)) != 0)
    662 		return error;
    663 
    664 	return 0;
    665 }
    666 
    667 /*
    668  * Create a snapshot.
    669  */
    670 static int
    671 fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct lwp *l)
    672 {
    673 	int len, error;
    674 	u_int32_t csize;
    675 	off_t bsize;
    676 
    677 	/*
    678 	 * Open needed files.
    679 	 */
    680 	if ((error = fss_create_files(sc, fss, &bsize, l)) != 0)
    681 		goto bad;
    682 
    683 	if (sc->sc_flags & FSS_PERSISTENT) {
    684 		fss_softc_alloc(sc);
    685 		sc->sc_flags |= FSS_ACTIVE;
    686 		return 0;
    687 	}
    688 
    689 	/*
    690 	 * Set cluster size. Must be a power of two and
    691 	 * a multiple of backing store block size.
    692 	 */
    693 	if (fss->fss_csize <= 0)
    694 		csize = MAXPHYS;
    695 	else
    696 		csize = fss->fss_csize;
    697 	if (bsize/csize > FSS_CLUSTER_MAX)
    698 		csize = bsize/FSS_CLUSTER_MAX+1;
    699 
    700 	for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32;
    701 	    sc->sc_clshift++)
    702 		if (FSS_CLSIZE(sc) >= csize)
    703 			break;
    704 	if (sc->sc_clshift >= 32) {
    705 		error = EINVAL;
    706 		goto bad;
    707 	}
    708 	sc->sc_clmask = FSS_CLSIZE(sc)-1;
    709 
    710 	/*
    711 	 * Set number of cache slots.
    712 	 */
    713 	if (FSS_CLSIZE(sc) <= 8192)
    714 		sc->sc_cache_size = 32;
    715 	else if (FSS_CLSIZE(sc) <= 65536)
    716 		sc->sc_cache_size = 8;
    717 	else
    718 		sc->sc_cache_size = 4;
    719 
    720 	/*
    721 	 * Set number of clusters and size of last cluster.
    722 	 */
    723 	sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1;
    724 	sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1;
    725 
    726 	/*
    727 	 * Set size of indirect table.
    728 	 */
    729 	len = sc->sc_clcount*sizeof(u_int32_t);
    730 	sc->sc_indir_size = FSS_BTOCL(sc, len)+1;
    731 	sc->sc_clnext = sc->sc_indir_size;
    732 	sc->sc_indir_cur = 0;
    733 
    734 	if ((error = fss_softc_alloc(sc)) != 0)
    735 		goto bad;
    736 
    737 	/*
    738 	 * Activate the snapshot.
    739 	 */
    740 
    741 	if ((error = vfs_write_suspend(sc->sc_mount, PUSER|PCATCH, 0)) != 0)
    742 		goto bad;
    743 
    744 	microtime(&sc->sc_time);
    745 
    746 	if (error == 0)
    747 		error = vn_cow_establish(sc->sc_mount_vp,
    748 		    fss_copy_on_write, sc);
    749 	if (error == 0)
    750 		sc->sc_flags |= FSS_ACTIVE;
    751 
    752 	vfs_write_resume(sc->sc_mount);
    753 
    754 	if (error != 0)
    755 		goto bad;
    756 
    757 #ifdef DEBUG
    758 	printf("fss%d: %s snapshot active\n", sc->sc_unit, sc->sc_mntname);
    759 	printf("fss%d: %u clusters of %u, %u cache slots, %u indir clusters\n",
    760 	    sc->sc_unit, sc->sc_clcount, FSS_CLSIZE(sc),
    761 	    sc->sc_cache_size, sc->sc_indir_size);
    762 #endif
    763 
    764 	return 0;
    765 
    766 bad:
    767 	fss_softc_free(sc);
    768 	if (sc->sc_bs_vp != NULL) {
    769 		if (sc->sc_flags & FSS_PERSISTENT)
    770 			vn_close(sc->sc_bs_vp, FREAD, l->l_proc->p_ucred, l);
    771 		else
    772 			vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_proc->p_ucred, l);
    773 	}
    774 	sc->sc_bs_vp = NULL;
    775 
    776 	return error;
    777 }
    778 
    779 /*
    780  * Delete a snapshot.
    781  */
    782 static int
    783 fss_delete_snapshot(struct fss_softc *sc, struct lwp *l)
    784 {
    785 	int s;
    786 
    787 	if ((sc->sc_flags & FSS_PERSISTENT) == 0)
    788 		vn_cow_disestablish(sc->sc_mount_vp, fss_copy_on_write, sc);
    789 
    790 	FSS_LOCK(sc, s);
    791 	sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR);
    792 	sc->sc_mount = NULL;
    793 	sc->sc_bdev = NODEV;
    794 	FSS_UNLOCK(sc, s);
    795 
    796 	fss_softc_free(sc);
    797 	if (sc->sc_flags & FSS_PERSISTENT)
    798 		vn_close(sc->sc_bs_vp, FREAD, l->l_proc->p_ucred, l);
    799 	else
    800 		vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_proc->p_ucred, l);
    801 	sc->sc_bs_vp = NULL;
    802 	sc->sc_flags &= ~FSS_PERSISTENT;
    803 
    804 	FSS_STAT_CLEAR(sc);
    805 
    806 	return 0;
    807 }
    808 
    809 /*
    810  * A read from the snapshotted block device has completed.
    811  */
    812 static void
    813 fss_cluster_iodone(struct buf *bp)
    814 {
    815 	int s;
    816 	struct fss_cache *scp = bp->b_private;
    817 
    818 	KASSERT(bp->b_vp == NULL);
    819 
    820 	FSS_LOCK(scp->fc_softc, s);
    821 
    822 	if (bp->b_flags & B_ERROR)
    823 		fss_error(scp->fc_softc, "fs read error %d", bp->b_error);
    824 
    825 	if (--scp->fc_xfercount == 0)
    826 		wakeup(&scp->fc_data);
    827 
    828 	FSS_UNLOCK(scp->fc_softc, s);
    829 
    830 	putiobuf(bp);
    831 }
    832 
    833 /*
    834  * Read a cluster from the snapshotted block device to the cache.
    835  */
    836 static void
    837 fss_read_cluster(struct fss_softc *sc, u_int32_t cl)
    838 {
    839 	int s, todo, len;
    840 	caddr_t addr;
    841 	daddr_t dblk;
    842 	struct buf *bp;
    843 	struct fss_cache *scp, *scl;
    844 
    845 	/*
    846 	 * Get a free cache slot.
    847 	 */
    848 	scl = sc->sc_cache+sc->sc_cache_size;
    849 
    850 	FSS_LOCK(sc, s);
    851 
    852 restart:
    853 	if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) {
    854 		FSS_UNLOCK(sc, s);
    855 		return;
    856 	}
    857 
    858 	for (scp = sc->sc_cache; scp < scl; scp++)
    859 		if (scp->fc_type != FSS_CACHE_FREE &&
    860 		    scp->fc_cluster == cl) {
    861 			ltsleep(&scp->fc_type, PRIBIO, "cowwait2", 0,
    862 			    &sc->sc_slock);
    863 			goto restart;
    864 		}
    865 
    866 	for (scp = sc->sc_cache; scp < scl; scp++)
    867 		if (scp->fc_type == FSS_CACHE_FREE) {
    868 			scp->fc_type = FSS_CACHE_BUSY;
    869 			scp->fc_cluster = cl;
    870 			break;
    871 		}
    872 	if (scp >= scl) {
    873 		FSS_STAT_INC(sc, cow_cache_full);
    874 		ltsleep(&sc->sc_cache, PRIBIO, "cowwait3", 0, &sc->sc_slock);
    875 		goto restart;
    876 	}
    877 
    878 	FSS_UNLOCK(sc, s);
    879 
    880 	/*
    881 	 * Start the read.
    882 	 */
    883 	FSS_STAT_INC(sc, cow_copied);
    884 
    885 	dblk = btodb(FSS_CLTOB(sc, cl));
    886 	addr = scp->fc_data;
    887 	if (cl == sc->sc_clcount-1) {
    888 		todo = sc->sc_clresid;
    889 		memset(addr+todo, 0, FSS_CLSIZE(sc)-todo);
    890 	} else
    891 		todo = FSS_CLSIZE(sc);
    892 	while (todo > 0) {
    893 		len = todo;
    894 		if (len > MAXPHYS)
    895 			len = MAXPHYS;
    896 
    897 		bp = getiobuf();
    898 		bp->b_flags = B_READ|B_CALL;
    899 		bp->b_bcount = len;
    900 		bp->b_bufsize = bp->b_bcount;
    901 		bp->b_error = 0;
    902 		bp->b_data = addr;
    903 		bp->b_blkno = dblk;
    904 		bp->b_proc = NULL;
    905 		bp->b_dev = sc->sc_bdev;
    906 		bp->b_vp = NULLVP;
    907 		bp->b_private = scp;
    908 		bp->b_iodone = fss_cluster_iodone;
    909 
    910 		DEV_STRATEGY(bp);
    911 
    912 		FSS_LOCK(sc, s);
    913 		scp->fc_xfercount++;
    914 		FSS_UNLOCK(sc, s);
    915 
    916 		dblk += btodb(len);
    917 		addr += len;
    918 		todo -= len;
    919 	}
    920 
    921 	/*
    922 	 * Wait for all read requests to complete.
    923 	 */
    924 	FSS_LOCK(sc, s);
    925 	while (scp->fc_xfercount > 0)
    926 		ltsleep(&scp->fc_data, PRIBIO, "cowwait", 0, &sc->sc_slock);
    927 
    928 	scp->fc_type = FSS_CACHE_VALID;
    929 	setbit(sc->sc_copied, scp->fc_cluster);
    930 	FSS_UNLOCK(sc, s);
    931 
    932 	wakeup(&sc->sc_bs_proc);
    933 }
    934 
    935 /*
    936  * Read/write clusters from/to backing store.
    937  * For persistent snapshots must be called with cl == 0. off is the
    938  * offset into the snapshot.
    939  */
    940 static int
    941 fss_bs_io(struct fss_softc *sc, fss_io_type rw,
    942     u_int32_t cl, off_t off, int len, caddr_t data)
    943 {
    944 	int error;
    945 
    946 	off += FSS_CLTOB(sc, cl);
    947 
    948 	vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY);
    949 
    950 	error = vn_rdwr((rw == FSS_READ ? UIO_READ : UIO_WRITE), sc->sc_bs_vp,
    951 	    data, len, off, UIO_SYSSPACE, IO_UNIT|IO_NODELOCKED,
    952 	    sc->sc_bs_proc->p_ucred, NULL, NULL);
    953 	if (error == 0) {
    954 		simple_lock(&sc->sc_bs_vp->v_interlock);
    955 		error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(off),
    956 		    round_page(off+len), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
    957 	}
    958 
    959 	VOP_UNLOCK(sc->sc_bs_vp, 0);
    960 
    961 	return error;
    962 }
    963 
    964 /*
    965  * Get a pointer to the indirect slot for this cluster.
    966  */
    967 static u_int32_t *
    968 fss_bs_indir(struct fss_softc *sc, u_int32_t cl)
    969 {
    970 	u_int32_t icl;
    971 	int ioff;
    972 
    973 	icl = cl/(FSS_CLSIZE(sc)/sizeof(u_int32_t));
    974 	ioff = cl%(FSS_CLSIZE(sc)/sizeof(u_int32_t));
    975 
    976 	if (sc->sc_indir_cur == icl)
    977 		return &sc->sc_indir_data[ioff];
    978 
    979 	if (sc->sc_indir_dirty) {
    980 		FSS_STAT_INC(sc, indir_write);
    981 		if (fss_bs_io(sc, FSS_WRITE, sc->sc_indir_cur, 0,
    982 		    FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0)
    983 			return NULL;
    984 		setbit(sc->sc_indir_valid, sc->sc_indir_cur);
    985 	}
    986 
    987 	sc->sc_indir_dirty = 0;
    988 	sc->sc_indir_cur = icl;
    989 
    990 	if (isset(sc->sc_indir_valid, sc->sc_indir_cur)) {
    991 		FSS_STAT_INC(sc, indir_read);
    992 		if (fss_bs_io(sc, FSS_READ, sc->sc_indir_cur, 0,
    993 		    FSS_CLSIZE(sc), (caddr_t)sc->sc_indir_data) != 0)
    994 			return NULL;
    995 	} else
    996 		memset(sc->sc_indir_data, 0, FSS_CLSIZE(sc));
    997 
    998 	return &sc->sc_indir_data[ioff];
    999 }
   1000 
   1001 /*
   1002  * The kernel thread (one for every active snapshot).
   1003  *
   1004  * After wakeup it cleans the cache and runs the I/O requests.
   1005  */
   1006 static void
   1007 fss_bs_thread(void *arg)
   1008 {
   1009 	int error, len, nfreed, nio, s;
   1010 	long off;
   1011 	caddr_t addr;
   1012 	u_int32_t c, cl, ch, *indirp;
   1013 	struct buf *bp, *nbp;
   1014 	struct fss_softc *sc;
   1015 	struct fss_cache *scp, *scl;
   1016 
   1017 	sc = arg;
   1018 
   1019 	scl = sc->sc_cache+sc->sc_cache_size;
   1020 
   1021 	nbp = getiobuf();
   1022 
   1023 	nfreed = nio = 1;		/* Dont sleep the first time */
   1024 
   1025 	FSS_LOCK(sc, s);
   1026 
   1027 	for (;;) {
   1028 		if (nfreed == 0 && nio == 0)
   1029 			ltsleep(&sc->sc_bs_proc, PVM-1, "fssbs", 0,
   1030 			    &sc->sc_slock);
   1031 
   1032 		if ((sc->sc_flags & FSS_BS_THREAD) == 0) {
   1033 			sc->sc_bs_proc = NULL;
   1034 			wakeup(&sc->sc_bs_proc);
   1035 
   1036 			FSS_UNLOCK(sc, s);
   1037 
   1038 			putiobuf(nbp);
   1039 #ifdef FSS_STATISTICS
   1040 			if ((sc->sc_flags & FSS_PERSISTENT) == 0) {
   1041 				printf("fss%d: cow called %" PRId64 " times,"
   1042 				    " copied %" PRId64 " clusters,"
   1043 				    " cache full %" PRId64 " times\n",
   1044 				    sc->sc_unit,
   1045 				    FSS_STAT_VAL(sc, cow_calls),
   1046 				    FSS_STAT_VAL(sc, cow_copied),
   1047 				    FSS_STAT_VAL(sc, cow_cache_full));
   1048 				printf("fss%d: %" PRId64 " indir reads,"
   1049 				    " %" PRId64 " indir writes\n",
   1050 				    sc->sc_unit,
   1051 				    FSS_STAT_VAL(sc, indir_read),
   1052 				    FSS_STAT_VAL(sc, indir_write));
   1053 			}
   1054 #endif /* FSS_STATISTICS */
   1055 			kthread_exit(0);
   1056 		}
   1057 
   1058 		/*
   1059 		 * Process I/O requests (persistent)
   1060 		 */
   1061 
   1062 		if (sc->sc_flags & FSS_PERSISTENT) {
   1063 			nfreed = nio = 0;
   1064 
   1065 			if ((bp = BUFQ_GET(sc->sc_bufq)) == NULL)
   1066 				continue;
   1067 
   1068 			nio++;
   1069 
   1070 			if (FSS_ISVALID(sc)) {
   1071 				FSS_UNLOCK(sc, s);
   1072 
   1073 				error = fss_bs_io(sc, FSS_READ, 0,
   1074 				    dbtob(bp->b_blkno), bp->b_bcount,
   1075 				    bp->b_data);
   1076 
   1077 				FSS_LOCK(sc, s);
   1078 			} else
   1079 				error = ENXIO;
   1080 
   1081 			if (error) {
   1082 				bp->b_error = error;
   1083 				bp->b_flags |= B_ERROR;
   1084 				bp->b_resid = bp->b_bcount;
   1085 			}
   1086 			biodone(bp);
   1087 
   1088 			continue;
   1089 		}
   1090 
   1091 		/*
   1092 		 * Clean the cache
   1093 		 */
   1094 		nfreed = 0;
   1095 		for (scp = sc->sc_cache; scp < scl; scp++) {
   1096 			if (scp->fc_type != FSS_CACHE_VALID)
   1097 				continue;
   1098 
   1099 			FSS_UNLOCK(sc, s);
   1100 
   1101 			indirp = fss_bs_indir(sc, scp->fc_cluster);
   1102 			if (indirp != NULL) {
   1103 				error = fss_bs_io(sc, FSS_WRITE, sc->sc_clnext,
   1104 				    0, FSS_CLSIZE(sc), scp->fc_data);
   1105 			} else
   1106 				error = EIO;
   1107 
   1108 			FSS_LOCK(sc, s);
   1109 
   1110 			if (error == 0) {
   1111 				*indirp = sc->sc_clnext++;
   1112 				sc->sc_indir_dirty = 1;
   1113 			} else
   1114 				fss_error(sc, "write bs error %d", error);
   1115 
   1116 			scp->fc_type = FSS_CACHE_FREE;
   1117 			nfreed++;
   1118 			wakeup(&scp->fc_type);
   1119 		}
   1120 
   1121 		if (nfreed)
   1122 			wakeup(&sc->sc_cache);
   1123 
   1124 		/*
   1125 		 * Process I/O requests
   1126 		 */
   1127 		nio = 0;
   1128 
   1129 		if ((bp = BUFQ_GET(sc->sc_bufq)) == NULL)
   1130 			continue;
   1131 
   1132 		nio++;
   1133 
   1134 		if (!FSS_ISVALID(sc)) {
   1135 			bp->b_error = ENXIO;
   1136 			bp->b_flags |= B_ERROR;
   1137 			bp->b_resid = bp->b_bcount;
   1138 			biodone(bp);
   1139 			continue;
   1140 		}
   1141 
   1142 		/*
   1143 		 * First read from the snapshotted block device.
   1144 		 * XXX Split to only read those parts that have not
   1145 		 * been saved to backing store?
   1146 		 */
   1147 
   1148 		FSS_UNLOCK(sc, s);
   1149 
   1150 		BUF_INIT(nbp);
   1151 		nbp->b_flags = B_READ;
   1152 		nbp->b_bcount = bp->b_bcount;
   1153 		nbp->b_bufsize = bp->b_bcount;
   1154 		nbp->b_error = 0;
   1155 		nbp->b_data = bp->b_data;
   1156 		nbp->b_blkno = bp->b_blkno;
   1157 		nbp->b_proc = bp->b_proc;
   1158 		nbp->b_dev = sc->sc_bdev;
   1159 		nbp->b_vp = NULLVP;
   1160 
   1161 		DEV_STRATEGY(nbp);
   1162 
   1163 		if (biowait(nbp) != 0) {
   1164 			bp->b_resid = bp->b_bcount;
   1165 			bp->b_error = nbp->b_error;
   1166 			bp->b_flags |= B_ERROR;
   1167 			biodone(bp);
   1168 			FSS_LOCK(sc, s);
   1169 			continue;
   1170 		}
   1171 
   1172 		cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
   1173 		off = FSS_CLOFF(sc, dbtob(bp->b_blkno));
   1174 		ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
   1175 		bp->b_resid = bp->b_bcount;
   1176 		addr = bp->b_data;
   1177 
   1178 		FSS_LOCK(sc, s);
   1179 
   1180 		/*
   1181 		 * Replace those parts that have been saved to backing store.
   1182 		 */
   1183 
   1184 		for (c = cl; c <= ch;
   1185 		    c++, off = 0, bp->b_resid -= len, addr += len) {
   1186 			len = FSS_CLSIZE(sc)-off;
   1187 			if (len > bp->b_resid)
   1188 				len = bp->b_resid;
   1189 
   1190 			if (isclr(sc->sc_copied, c))
   1191 				continue;
   1192 
   1193 			FSS_UNLOCK(sc, s);
   1194 
   1195 			indirp = fss_bs_indir(sc, c);
   1196 
   1197 			FSS_LOCK(sc, s);
   1198 
   1199 			if (indirp == NULL || *indirp == 0) {
   1200 				/*
   1201 				 * Not on backing store. Either in cache
   1202 				 * or hole in the snapshotted block device.
   1203 				 */
   1204 				for (scp = sc->sc_cache; scp < scl; scp++)
   1205 					if (scp->fc_type == FSS_CACHE_VALID &&
   1206 					    scp->fc_cluster == c)
   1207 						break;
   1208 				if (scp < scl)
   1209 					memcpy(addr, scp->fc_data+off, len);
   1210 				else
   1211 					memset(addr, 0, len);
   1212 				continue;
   1213 			}
   1214 			/*
   1215 			 * Read from backing store.
   1216 			 */
   1217 
   1218 			FSS_UNLOCK(sc, s);
   1219 
   1220 			if ((error = fss_bs_io(sc, FSS_READ, *indirp,
   1221 			    off, len, addr)) != 0) {
   1222 				bp->b_resid = bp->b_bcount;
   1223 				bp->b_error = error;
   1224 				bp->b_flags |= B_ERROR;
   1225 				FSS_LOCK(sc, s);
   1226 				break;
   1227 			}
   1228 
   1229 			FSS_LOCK(sc, s);
   1230 
   1231 		}
   1232 
   1233 		biodone(bp);
   1234 	}
   1235 }
   1236