Home | History | Annotate | Line # | Download | only in dev
fss.c revision 1.61
      1 /*	$NetBSD: fss.c,v 1.61 2008/11/05 19:30:57 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  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * File system snapshot disk driver.
     34  *
     35  * Block/character interface to the snapshot of a mounted file system.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: fss.c,v 1.61 2008/11/05 19:30:57 hannken Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/namei.h>
     44 #include <sys/proc.h>
     45 #include <sys/errno.h>
     46 #include <sys/malloc.h>
     47 #include <sys/buf.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/disklabel.h>
     50 #include <sys/device.h>
     51 #include <sys/disk.h>
     52 #include <sys/stat.h>
     53 #include <sys/mount.h>
     54 #include <sys/vnode.h>
     55 #include <sys/file.h>
     56 #include <sys/uio.h>
     57 #include <sys/conf.h>
     58 #include <sys/kthread.h>
     59 #include <sys/fstrans.h>
     60 #include <sys/simplelock.h>
     61 
     62 #include <miscfs/specfs/specdev.h>
     63 
     64 #include <dev/fssvar.h>
     65 
     66 #include <uvm/uvm.h>
     67 
     68 void fssattach(int);
     69 
     70 dev_type_open(fss_open);
     71 dev_type_close(fss_close);
     72 dev_type_read(fss_read);
     73 dev_type_write(fss_write);
     74 dev_type_ioctl(fss_ioctl);
     75 dev_type_strategy(fss_strategy);
     76 dev_type_dump(fss_dump);
     77 dev_type_size(fss_size);
     78 
     79 static void fss_unmount_hook(struct mount *);
     80 static int fss_copy_on_write(void *, struct buf *, bool);
     81 static inline void fss_error(struct fss_softc *, const char *);
     82 static int fss_create_files(struct fss_softc *, struct fss_set *,
     83     off_t *, struct lwp *);
     84 static int fss_create_snapshot(struct fss_softc *, struct fss_set *,
     85     struct lwp *);
     86 static int fss_delete_snapshot(struct fss_softc *, struct lwp *);
     87 static int fss_softc_alloc(struct fss_softc *);
     88 static void fss_softc_free(struct fss_softc *);
     89 static int fss_read_cluster(struct fss_softc *, u_int32_t);
     90 static void fss_bs_thread(void *);
     91 static int fss_bs_io(struct fss_softc *, fss_io_type,
     92     u_int32_t, off_t, int, void *);
     93 static u_int32_t *fss_bs_indir(struct fss_softc *, u_int32_t);
     94 
     95 static kmutex_t fss_device_lock;	/* Protect all units. */
     96 static int fss_num_attached = 0;	/* Number of attached devices. */
     97 static struct vfs_hooks fss_vfs_hooks = {
     98 	.vh_unmount = fss_unmount_hook
     99 };
    100 
    101 const struct bdevsw fss_bdevsw = {
    102 	fss_open, fss_close, fss_strategy, fss_ioctl,
    103 	fss_dump, fss_size, D_DISK | D_MPSAFE
    104 };
    105 
    106 const struct cdevsw fss_cdevsw = {
    107 	fss_open, fss_close, fss_read, fss_write, fss_ioctl,
    108 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK | D_MPSAFE
    109 };
    110 
    111 static int fss_match(device_t, cfdata_t, void *);
    112 static void fss_attach(device_t, device_t, void *);
    113 static int fss_detach(device_t, int);
    114 
    115 CFATTACH_DECL_NEW(fss, sizeof(struct fss_softc),
    116     fss_match, fss_attach, fss_detach, NULL);
    117 extern struct cfdriver fss_cd;
    118 
    119 void
    120 fssattach(int num)
    121 {
    122 
    123 	mutex_init(&fss_device_lock, MUTEX_DEFAULT, IPL_NONE);
    124 	if (config_cfattach_attach(fss_cd.cd_name, &fss_ca))
    125 		aprint_error("%s: unable to register\n", fss_cd.cd_name);
    126 }
    127 
    128 static int
    129 fss_match(device_t self, cfdata_t cfdata, void *aux)
    130 {
    131 	return 1;
    132 }
    133 
    134 static void
    135 fss_attach(device_t parent, device_t self, void *aux)
    136 {
    137 	struct fss_softc *sc = device_private(self);
    138 
    139 	sc->sc_dev = self;
    140 	sc->sc_bdev = NODEV;
    141 	mutex_init(&sc->sc_slock, MUTEX_DEFAULT, IPL_NONE);
    142 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
    143 	cv_init(&sc->sc_work_cv, "fssbs");
    144 	cv_init(&sc->sc_cache_cv, "cowwait");
    145 	bufq_alloc(&sc->sc_bufq, "fcfs", 0);
    146 	sc->sc_dkdev = malloc(sizeof(*sc->sc_dkdev), M_DEVBUF, M_WAITOK);
    147 	sc->sc_dkdev->dk_info = NULL;
    148 	disk_init(sc->sc_dkdev, device_xname(self), NULL);
    149 	if (!pmf_device_register(self, NULL, NULL))
    150 		aprint_error_dev(self, "couldn't establish power handler\n");
    151 
    152 	if (fss_num_attached++ == 0)
    153 		vfs_hooks_attach(&fss_vfs_hooks);
    154 }
    155 
    156 static int
    157 fss_detach(device_t self, int flags)
    158 {
    159 	struct fss_softc *sc = device_private(self);
    160 
    161 	if (sc->sc_flags & FSS_ACTIVE)
    162 		return EBUSY;
    163 
    164 	if (--fss_num_attached == 0)
    165 		vfs_hooks_detach(&fss_vfs_hooks);
    166 
    167 	pmf_device_deregister(self);
    168 	mutex_destroy(&sc->sc_slock);
    169 	mutex_destroy(&sc->sc_lock);
    170 	cv_destroy(&sc->sc_work_cv);
    171 	cv_destroy(&sc->sc_cache_cv);
    172 	bufq_drain(sc->sc_bufq);
    173 	bufq_free(sc->sc_bufq);
    174 	disk_destroy(sc->sc_dkdev);
    175 	free(sc->sc_dkdev, M_DEVBUF);
    176 
    177 	return 0;
    178 }
    179 
    180 int
    181 fss_open(dev_t dev, int flags, int mode, struct lwp *l)
    182 {
    183 	int mflag;
    184 	cfdata_t cf;
    185 	struct fss_softc *sc;
    186 
    187 	mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN);
    188 
    189 	mutex_enter(&fss_device_lock);
    190 
    191 	sc = device_lookup_private(&fss_cd, minor(dev));
    192 	if (sc == NULL) {
    193 		cf = malloc(sizeof(*cf), M_DEVBUF, M_WAITOK);
    194 		cf->cf_name = fss_cd.cd_name;
    195 		cf->cf_atname = fss_cd.cd_name;
    196 		cf->cf_unit = minor(dev);
    197 		cf->cf_fstate = FSTATE_STAR;
    198 		sc = device_private(config_attach_pseudo(cf));
    199 		if (sc == NULL)
    200 			return ENOMEM;
    201 	}
    202 
    203 	mutex_enter(&sc->sc_slock);
    204 
    205 	sc->sc_flags |= mflag;
    206 
    207 	mutex_exit(&sc->sc_slock);
    208 	mutex_exit(&fss_device_lock);
    209 
    210 	return 0;
    211 }
    212 
    213 int
    214 fss_close(dev_t dev, int flags, int mode, struct lwp *l)
    215 {
    216 	int mflag, error;
    217 	cfdata_t cf;
    218 	struct fss_softc *sc = device_lookup_private(&fss_cd, minor(dev));
    219 
    220 	mflag = (mode == S_IFCHR ? FSS_CDEV_OPEN : FSS_BDEV_OPEN);
    221 	error = 0;
    222 
    223 restart:
    224 	mutex_enter(&sc->sc_slock);
    225 	if ((sc->sc_flags & (FSS_CDEV_OPEN|FSS_BDEV_OPEN)) != mflag) {
    226 		sc->sc_flags &= ~mflag;
    227 		mutex_exit(&sc->sc_slock);
    228 		return 0;
    229 	}
    230 	if ((sc->sc_flags & FSS_ACTIVE) != 0 &&
    231 	    (sc->sc_uflags & FSS_UNCONFIG_ON_CLOSE) != 0) {
    232 		sc->sc_uflags &= ~FSS_UNCONFIG_ON_CLOSE;
    233 		mutex_exit(&sc->sc_slock);
    234 		error = fss_ioctl(dev, FSSIOCCLR, NULL, FWRITE, l);
    235 		goto restart;
    236 	}
    237 	if ((sc->sc_flags & FSS_ACTIVE) != 0) {
    238 		mutex_exit(&sc->sc_slock);
    239 		return error;
    240 	}
    241 	if (! mutex_tryenter(&fss_device_lock)) {
    242 		mutex_exit(&sc->sc_slock);
    243 		goto restart;
    244 	}
    245 
    246 	KASSERT((sc->sc_flags & FSS_ACTIVE) == 0);
    247 	KASSERT((sc->sc_flags & (FSS_CDEV_OPEN|FSS_BDEV_OPEN)) == mflag);
    248 	mutex_exit(&sc->sc_slock);
    249 	cf = device_cfdata(sc->sc_dev);
    250 	error = config_detach(sc->sc_dev, DETACH_QUIET);
    251 	if (! error)
    252 		free(cf, M_DEVBUF);
    253 	mutex_exit(&fss_device_lock);
    254 
    255 	return error;
    256 }
    257 
    258 void
    259 fss_strategy(struct buf *bp)
    260 {
    261 	const bool write = ((bp->b_flags & B_READ) != B_READ);
    262 	struct fss_softc *sc = device_lookup_private(&fss_cd, minor(bp->b_dev));
    263 
    264 	mutex_enter(&sc->sc_slock);
    265 
    266 	if (write || !FSS_ISVALID(sc)) {
    267 
    268 		mutex_exit(&sc->sc_slock);
    269 
    270 		bp->b_error = (write ? EROFS : ENXIO);
    271 		bp->b_resid = bp->b_bcount;
    272 		biodone(bp);
    273 		return;
    274 	}
    275 
    276 	bp->b_rawblkno = bp->b_blkno;
    277 	BUFQ_PUT(sc->sc_bufq, bp);
    278 	cv_signal(&sc->sc_work_cv);
    279 
    280 	mutex_exit(&sc->sc_slock);
    281 }
    282 
    283 int
    284 fss_read(dev_t dev, struct uio *uio, int flags)
    285 {
    286 	return physio(fss_strategy, NULL, dev, B_READ, minphys, uio);
    287 }
    288 
    289 int
    290 fss_write(dev_t dev, struct uio *uio, int flags)
    291 {
    292 	return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio);
    293 }
    294 
    295 int
    296 fss_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    297 {
    298 	int error;
    299 	struct fss_softc *sc = device_lookup_private(&fss_cd, minor(dev));
    300 	struct fss_set *fss = (struct fss_set *)data;
    301 	struct fss_get *fsg = (struct fss_get *)data;
    302 
    303 	switch (cmd) {
    304 	case FSSIOCSET:
    305 		mutex_enter(&sc->sc_lock);
    306 		if ((flag & FWRITE) == 0)
    307 			error = EPERM;
    308 		else if ((sc->sc_flags & FSS_ACTIVE) != 0)
    309 			error = EBUSY;
    310 		else
    311 			error = fss_create_snapshot(sc, fss, l);
    312 		mutex_exit(&sc->sc_lock);
    313 		break;
    314 
    315 	case FSSIOCCLR:
    316 		mutex_enter(&sc->sc_lock);
    317 		if ((flag & FWRITE) == 0)
    318 			error = EPERM;
    319 		else if ((sc->sc_flags & FSS_ACTIVE) == 0)
    320 			error = ENXIO;
    321 		else
    322 			error = fss_delete_snapshot(sc, l);
    323 		mutex_exit(&sc->sc_lock);
    324 		break;
    325 
    326 	case FSSIOCGET:
    327 		mutex_enter(&sc->sc_lock);
    328 		switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) {
    329 		case FSS_ACTIVE:
    330 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    331 			fsg->fsg_csize = FSS_CLSIZE(sc);
    332 			fsg->fsg_time = sc->sc_time;
    333 			fsg->fsg_mount_size = sc->sc_clcount;
    334 			fsg->fsg_bs_size = sc->sc_clnext;
    335 			error = 0;
    336 			break;
    337 		case FSS_PERSISTENT | FSS_ACTIVE:
    338 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    339 			fsg->fsg_csize = 0;
    340 			fsg->fsg_time = sc->sc_time;
    341 			fsg->fsg_mount_size = 0;
    342 			fsg->fsg_bs_size = 0;
    343 			error = 0;
    344 			break;
    345 		default:
    346 			error = ENXIO;
    347 			break;
    348 		}
    349 		mutex_exit(&sc->sc_lock);
    350 		break;
    351 
    352 	case FSSIOFSET:
    353 		mutex_enter(&sc->sc_slock);
    354 		sc->sc_uflags = *(int *)data;
    355 		mutex_exit(&sc->sc_slock);
    356 		error = 0;
    357 		break;
    358 
    359 	case FSSIOFGET:
    360 		mutex_enter(&sc->sc_slock);
    361 		*(int *)data = sc->sc_uflags;
    362 		mutex_exit(&sc->sc_slock);
    363 		error = 0;
    364 		break;
    365 
    366 	default:
    367 		error = EINVAL;
    368 		break;
    369 	}
    370 
    371 	return error;
    372 }
    373 
    374 int
    375 fss_size(dev_t dev)
    376 {
    377 	return -1;
    378 }
    379 
    380 int
    381 fss_dump(dev_t dev, daddr_t blkno, void *va,
    382     size_t size)
    383 {
    384 	return EROFS;
    385 }
    386 
    387 /*
    388  * An error occurred reading or writing the snapshot or backing store.
    389  * If it is the first error log to console.
    390  * The caller holds the mutex.
    391  */
    392 static inline void
    393 fss_error(struct fss_softc *sc, const char *msg)
    394 {
    395 
    396 	if ((sc->sc_flags & (FSS_ACTIVE|FSS_ERROR)) == FSS_ACTIVE)
    397 		aprint_error_dev(sc->sc_dev, "snapshot invalid: %s\n", msg);
    398 	if ((sc->sc_flags & FSS_ACTIVE) == FSS_ACTIVE)
    399 		sc->sc_flags |= FSS_ERROR;
    400 }
    401 
    402 /*
    403  * Allocate the variable sized parts of the softc and
    404  * fork the kernel thread.
    405  *
    406  * The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size
    407  * must be initialized.
    408  */
    409 static int
    410 fss_softc_alloc(struct fss_softc *sc)
    411 {
    412 	int i, error;
    413 
    414 	if ((sc->sc_flags & FSS_PERSISTENT) == 0) {
    415 		sc->sc_copied =
    416 		    kmem_zalloc(howmany(sc->sc_clcount, NBBY), KM_SLEEP);
    417 		if (sc->sc_copied == NULL)
    418 			return(ENOMEM);
    419 
    420 		sc->sc_cache = kmem_alloc(sc->sc_cache_size *
    421 		    sizeof(struct fss_cache), KM_SLEEP);
    422 		if (sc->sc_cache == NULL)
    423 			return(ENOMEM);
    424 
    425 		for (i = 0; i < sc->sc_cache_size; i++) {
    426 			sc->sc_cache[i].fc_type = FSS_CACHE_FREE;
    427 			sc->sc_cache[i].fc_data =
    428 			    kmem_alloc(FSS_CLSIZE(sc), KM_SLEEP);
    429 			if (sc->sc_cache[i].fc_data == NULL)
    430 				return(ENOMEM);
    431 			cv_init(&sc->sc_cache[i].fc_state_cv, "cowwait1");
    432 		}
    433 
    434 		sc->sc_indir_valid =
    435 		    kmem_zalloc(howmany(sc->sc_indir_size, NBBY), KM_SLEEP);
    436 		if (sc->sc_indir_valid == NULL)
    437 			return(ENOMEM);
    438 
    439 		sc->sc_indir_data = kmem_zalloc(FSS_CLSIZE(sc), KM_SLEEP);
    440 		if (sc->sc_indir_data == NULL)
    441 			return(ENOMEM);
    442 	} else {
    443 		sc->sc_copied = NULL;
    444 		sc->sc_cache = NULL;
    445 		sc->sc_indir_valid = NULL;
    446 		sc->sc_indir_data = NULL;
    447 	}
    448 
    449 	if ((error = kthread_create(PRI_BIO, 0, NULL, fss_bs_thread, sc,
    450 	    &sc->sc_bs_lwp, device_xname(sc->sc_dev))) != 0)
    451 		return error;
    452 
    453 	sc->sc_flags |= FSS_BS_THREAD;
    454 
    455 	disk_attach(sc->sc_dkdev);
    456 
    457 	return 0;
    458 }
    459 
    460 /*
    461  * Free the variable sized parts of the softc.
    462  */
    463 static void
    464 fss_softc_free(struct fss_softc *sc)
    465 {
    466 	int i;
    467 
    468 	if ((sc->sc_flags & FSS_BS_THREAD) != 0) {
    469 		mutex_enter(&sc->sc_slock);
    470 		sc->sc_flags &= ~FSS_BS_THREAD;
    471 		cv_signal(&sc->sc_work_cv);
    472 		while (sc->sc_bs_lwp != NULL)
    473 			kpause("fssdetach", false, 1, &sc->sc_slock);
    474 		mutex_exit(&sc->sc_slock);
    475 	}
    476 
    477 	disk_detach(sc->sc_dkdev);
    478 
    479 	if (sc->sc_copied != NULL)
    480 		kmem_free(sc->sc_copied, howmany(sc->sc_clcount, NBBY));
    481 	sc->sc_copied = NULL;
    482 
    483 	if (sc->sc_cache != NULL) {
    484 		for (i = 0; i < sc->sc_cache_size; i++)
    485 			if (sc->sc_cache[i].fc_data != NULL) {
    486 				cv_destroy(&sc->sc_cache[i].fc_state_cv);
    487 				kmem_free(sc->sc_cache[i].fc_data,
    488 				    FSS_CLSIZE(sc));
    489 			}
    490 		kmem_free(sc->sc_cache,
    491 		    sc->sc_cache_size*sizeof(struct fss_cache));
    492 	}
    493 	sc->sc_cache = NULL;
    494 
    495 	if (sc->sc_indir_valid != NULL)
    496 		kmem_free(sc->sc_indir_valid, howmany(sc->sc_indir_size, NBBY));
    497 	sc->sc_indir_valid = NULL;
    498 
    499 	if (sc->sc_indir_data != NULL)
    500 		kmem_free(sc->sc_indir_data, FSS_CLSIZE(sc));
    501 	sc->sc_indir_data = NULL;
    502 }
    503 
    504 /*
    505  * Set all active snapshots on this file system into ERROR state.
    506  */
    507 static void
    508 fss_unmount_hook(struct mount *mp)
    509 {
    510 	int i;
    511 	struct fss_softc *sc;
    512 
    513 	mutex_enter(&fss_device_lock);
    514 	for (i = 0; i < fss_cd.cd_ndevs; i++) {
    515 		if ((sc = device_lookup_private(&fss_cd, i)) == NULL)
    516 			continue;
    517 		mutex_enter(&sc->sc_slock);
    518 		if ((sc->sc_flags & FSS_ACTIVE) != 0 &&
    519 		    sc->sc_mount == mp)
    520 			fss_error(sc, "forced unmount");
    521 		mutex_exit(&sc->sc_slock);
    522 	}
    523 	mutex_exit(&fss_device_lock);
    524 }
    525 
    526 /*
    527  * A buffer is written to the snapshotted block device. Copy to
    528  * backing store if needed.
    529  */
    530 static int
    531 fss_copy_on_write(void *v, struct buf *bp, bool data_valid)
    532 {
    533 	int error;
    534 	u_int32_t cl, ch, c;
    535 	struct fss_softc *sc = v;
    536 
    537 	mutex_enter(&sc->sc_slock);
    538 	if (!FSS_ISVALID(sc)) {
    539 		mutex_exit(&sc->sc_slock);
    540 		return 0;
    541 	}
    542 
    543 	cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
    544 	ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
    545 	error = 0;
    546 	if (curlwp == uvm.pagedaemon_lwp) {
    547 		for (c = cl; c <= ch; c++)
    548 			if (isclr(sc->sc_copied, c)) {
    549 				error = ENOMEM;
    550 				break;
    551 			}
    552 	}
    553 	mutex_exit(&sc->sc_slock);
    554 
    555 	if (error == 0)
    556 		for (c = cl; c <= ch; c++) {
    557 			error = fss_read_cluster(sc, c);
    558 			if (error)
    559 				break;
    560 		}
    561 
    562 	return error;
    563 }
    564 
    565 /*
    566  * Lookup and open needed files.
    567  *
    568  * For file system internal snapshot initializes sc_mntname, sc_mount,
    569  * sc_bs_vp and sc_time.
    570  *
    571  * Otherwise returns dev and size of the underlying block device.
    572  * Initializes sc_mntname, sc_mount, sc_bdev, sc_bs_vp and sc_mount
    573  */
    574 static int
    575 fss_create_files(struct fss_softc *sc, struct fss_set *fss,
    576     off_t *bsize, struct lwp *l)
    577 {
    578 	int error, bits, fsbsize;
    579 	struct timespec ts;
    580 	struct partinfo dpart;
    581 	struct vattr va;
    582 	struct nameidata nd;
    583 
    584 	/*
    585 	 * Get the mounted file system.
    586 	 */
    587 
    588 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_mount);
    589 	if ((error = namei(&nd)) != 0)
    590 		return error;
    591 
    592 	if ((nd.ni_vp->v_vflag & VV_ROOT) != VV_ROOT) {
    593 		vrele(nd.ni_vp);
    594 		return EINVAL;
    595 	}
    596 
    597 	sc->sc_mount = nd.ni_vp->v_mount;
    598 	memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN);
    599 
    600 	vrele(nd.ni_vp);
    601 
    602 	/*
    603 	 * Check for file system internal snapshot.
    604 	 */
    605 
    606 	NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, fss->fss_bstore);
    607 	if ((error = namei(&nd)) != 0)
    608 		return error;
    609 
    610 	if (nd.ni_vp->v_type == VREG && nd.ni_vp->v_mount == sc->sc_mount) {
    611 		sc->sc_flags |= FSS_PERSISTENT;
    612 		sc->sc_bs_vp = nd.ni_vp;
    613 
    614 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    615 		bits = sizeof(sc->sc_bs_bshift)*NBBY;
    616 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < bits;
    617 		    sc->sc_bs_bshift++)
    618 			if (FSS_FSBSIZE(sc) == fsbsize)
    619 				break;
    620 		if (sc->sc_bs_bshift >= bits) {
    621 			VOP_UNLOCK(sc->sc_bs_vp, 0);
    622 			return EINVAL;
    623 		}
    624 
    625 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    626 		sc->sc_clshift = 0;
    627 
    628 		error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts);
    629 		TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts);
    630 
    631 		VOP_UNLOCK(sc->sc_bs_vp, 0);
    632 
    633 		return error;
    634 	}
    635 	vput(nd.ni_vp);
    636 
    637 	/*
    638 	 * Get the block device it is mounted on.
    639 	 */
    640 
    641 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE,
    642 	    sc->sc_mount->mnt_stat.f_mntfromname);
    643 	if ((error = namei(&nd)) != 0)
    644 		return error;
    645 
    646 	if (nd.ni_vp->v_type != VBLK) {
    647 		vrele(nd.ni_vp);
    648 		return EINVAL;
    649 	}
    650 
    651 	sc->sc_bdev = nd.ni_vp->v_rdev;
    652 	vrele(nd.ni_vp);
    653 
    654 	/*
    655 	 * Get the block device size.
    656 	 */
    657 
    658 	error = bdev_ioctl(sc->sc_bdev, DIOCGPART, &dpart, FREAD, l);
    659 	if (error)
    660 		return error;
    661 
    662 	*bsize = (off_t)dpart.disklab->d_secsize*dpart.part->p_size;
    663 
    664 	/*
    665 	 * Get the backing store
    666 	 */
    667 
    668 	NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, fss->fss_bstore);
    669 	if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0)
    670 		return error;
    671 	VOP_UNLOCK(nd.ni_vp, 0);
    672 
    673 	sc->sc_bs_vp = nd.ni_vp;
    674 
    675 	if (nd.ni_vp->v_type != VREG && nd.ni_vp->v_type != VCHR)
    676 		return EINVAL;
    677 
    678 	if (sc->sc_bs_vp->v_type == VREG) {
    679 		error = VOP_GETATTR(sc->sc_bs_vp, &va, l->l_cred);
    680 		if (error != 0)
    681 			return error;
    682 		sc->sc_bs_size = va.va_size;
    683 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    684 		if (fsbsize & (fsbsize-1))	/* No power of two */
    685 			return EINVAL;
    686 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32;
    687 		    sc->sc_bs_bshift++)
    688 			if (FSS_FSBSIZE(sc) == fsbsize)
    689 				break;
    690 		if (sc->sc_bs_bshift >= 32)
    691 			return EINVAL;
    692 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    693 	} else {
    694 		sc->sc_bs_bshift = DEV_BSHIFT;
    695 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    696 	}
    697 
    698 	/*
    699 	 * As all IO to from/to the backing store goes through
    700 	 * VOP_STRATEGY() clean the buffer cache to prevent
    701 	 * cache incoherencies.
    702 	 */
    703 	if ((error = vinvalbuf(sc->sc_bs_vp, V_SAVE, l->l_cred, l, 0, 0)) != 0)
    704 		return error;
    705 
    706 	return 0;
    707 }
    708 
    709 /*
    710  * Create a snapshot.
    711  */
    712 static int
    713 fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct lwp *l)
    714 {
    715 	int len, error;
    716 	u_int32_t csize;
    717 	off_t bsize;
    718 
    719 	bsize = 0;	/* XXX gcc */
    720 
    721 	/*
    722 	 * Open needed files.
    723 	 */
    724 	if ((error = fss_create_files(sc, fss, &bsize, l)) != 0)
    725 		goto bad;
    726 
    727 	if (sc->sc_flags & FSS_PERSISTENT) {
    728 		fss_softc_alloc(sc);
    729 		sc->sc_flags |= FSS_ACTIVE;
    730 		return 0;
    731 	}
    732 
    733 	/*
    734 	 * Set cluster size. Must be a power of two and
    735 	 * a multiple of backing store block size.
    736 	 */
    737 	if (fss->fss_csize <= 0)
    738 		csize = MAXPHYS;
    739 	else
    740 		csize = fss->fss_csize;
    741 	if (bsize/csize > FSS_CLUSTER_MAX)
    742 		csize = bsize/FSS_CLUSTER_MAX+1;
    743 
    744 	for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32;
    745 	    sc->sc_clshift++)
    746 		if (FSS_CLSIZE(sc) >= csize)
    747 			break;
    748 	if (sc->sc_clshift >= 32) {
    749 		error = EINVAL;
    750 		goto bad;
    751 	}
    752 	sc->sc_clmask = FSS_CLSIZE(sc)-1;
    753 
    754 	/*
    755 	 * Set number of cache slots.
    756 	 */
    757 	if (FSS_CLSIZE(sc) <= 8192)
    758 		sc->sc_cache_size = 32;
    759 	else if (FSS_CLSIZE(sc) <= 65536)
    760 		sc->sc_cache_size = 8;
    761 	else
    762 		sc->sc_cache_size = 4;
    763 
    764 	/*
    765 	 * Set number of clusters and size of last cluster.
    766 	 */
    767 	sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1;
    768 	sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1;
    769 
    770 	/*
    771 	 * Set size of indirect table.
    772 	 */
    773 	len = sc->sc_clcount*sizeof(u_int32_t);
    774 	sc->sc_indir_size = FSS_BTOCL(sc, len)+1;
    775 	sc->sc_clnext = sc->sc_indir_size;
    776 	sc->sc_indir_cur = 0;
    777 
    778 	if ((error = fss_softc_alloc(sc)) != 0)
    779 		goto bad;
    780 
    781 	/*
    782 	 * Activate the snapshot.
    783 	 */
    784 
    785 	if ((error = vfs_suspend(sc->sc_mount, 0)) != 0)
    786 		goto bad;
    787 
    788 	microtime(&sc->sc_time);
    789 
    790 	if (error == 0)
    791 		error = fscow_establish(sc->sc_mount,
    792 		    fss_copy_on_write, sc);
    793 	if (error == 0)
    794 		sc->sc_flags |= FSS_ACTIVE;
    795 
    796 	vfs_resume(sc->sc_mount);
    797 
    798 	if (error != 0)
    799 		goto bad;
    800 
    801 	aprint_debug_dev(sc->sc_dev, "%s snapshot active\n", sc->sc_mntname);
    802 	aprint_debug_dev(sc->sc_dev,
    803 	    "%u clusters of %u, %u cache slots, %u indir clusters\n",
    804 	    sc->sc_clcount, FSS_CLSIZE(sc),
    805 	    sc->sc_cache_size, sc->sc_indir_size);
    806 
    807 	return 0;
    808 
    809 bad:
    810 	fss_softc_free(sc);
    811 	if (sc->sc_bs_vp != NULL) {
    812 		if (sc->sc_flags & FSS_PERSISTENT)
    813 			vn_close(sc->sc_bs_vp, FREAD, l->l_cred);
    814 		else
    815 			vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_cred);
    816 	}
    817 	sc->sc_bs_vp = NULL;
    818 
    819 	return error;
    820 }
    821 
    822 /*
    823  * Delete a snapshot.
    824  */
    825 static int
    826 fss_delete_snapshot(struct fss_softc *sc, struct lwp *l)
    827 {
    828 
    829 	if ((sc->sc_flags & FSS_PERSISTENT) == 0)
    830 		fscow_disestablish(sc->sc_mount, fss_copy_on_write, sc);
    831 
    832 	mutex_enter(&sc->sc_slock);
    833 	sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR);
    834 	sc->sc_mount = NULL;
    835 	sc->sc_bdev = NODEV;
    836 	mutex_exit(&sc->sc_slock);
    837 
    838 	fss_softc_free(sc);
    839 	if (sc->sc_flags & FSS_PERSISTENT)
    840 		vn_close(sc->sc_bs_vp, FREAD, l->l_cred);
    841 	else
    842 		vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_cred);
    843 	sc->sc_bs_vp = NULL;
    844 	sc->sc_flags &= ~FSS_PERSISTENT;
    845 
    846 	return 0;
    847 }
    848 
    849 /*
    850  * Read a cluster from the snapshotted block device to the cache.
    851  */
    852 static int
    853 fss_read_cluster(struct fss_softc *sc, u_int32_t cl)
    854 {
    855 	int error, todo, offset, len;
    856 	daddr_t dblk;
    857 	struct buf *bp, *mbp;
    858 	struct fss_cache *scp, *scl;
    859 
    860 	/*
    861 	 * Get a free cache slot.
    862 	 */
    863 	scl = sc->sc_cache+sc->sc_cache_size;
    864 
    865 	mutex_enter(&sc->sc_slock);
    866 
    867 restart:
    868 	if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) {
    869 		mutex_exit(&sc->sc_slock);
    870 		return 0;
    871 	}
    872 
    873 	for (scp = sc->sc_cache; scp < scl; scp++)
    874 		if (scp->fc_cluster == cl) {
    875 			if (scp->fc_type == FSS_CACHE_VALID) {
    876 				mutex_exit(&sc->sc_slock);
    877 				return 0;
    878 			} else if (scp->fc_type == FSS_CACHE_BUSY) {
    879 				cv_wait(&scp->fc_state_cv, &sc->sc_slock);
    880 				goto restart;
    881 			}
    882 		}
    883 
    884 	for (scp = sc->sc_cache; scp < scl; scp++)
    885 		if (scp->fc_type == FSS_CACHE_FREE) {
    886 			scp->fc_type = FSS_CACHE_BUSY;
    887 			scp->fc_cluster = cl;
    888 			break;
    889 		}
    890 	if (scp >= scl) {
    891 		cv_wait(&sc->sc_cache_cv, &sc->sc_slock);
    892 		goto restart;
    893 	}
    894 
    895 	mutex_exit(&sc->sc_slock);
    896 
    897 	/*
    898 	 * Start the read.
    899 	 */
    900 	dblk = btodb(FSS_CLTOB(sc, cl));
    901 	if (cl == sc->sc_clcount-1) {
    902 		todo = sc->sc_clresid;
    903 		memset((char *)scp->fc_data + todo, 0, FSS_CLSIZE(sc) - todo);
    904 	} else
    905 		todo = FSS_CLSIZE(sc);
    906 	offset = 0;
    907 	mbp = getiobuf(NULL, true);
    908 	mbp->b_bufsize = todo;
    909 	mbp->b_data = scp->fc_data;
    910 	mbp->b_resid = mbp->b_bcount = todo;
    911 	mbp->b_flags = B_READ;
    912 	mbp->b_cflags = BC_BUSY;
    913 	mbp->b_dev = sc->sc_bdev;
    914 	while (todo > 0) {
    915 		len = todo;
    916 		if (len > MAXPHYS)
    917 			len = MAXPHYS;
    918 		if (btodb(FSS_CLTOB(sc, cl)) == dblk && len == todo)
    919 			bp = mbp;
    920 		else {
    921 			bp = getiobuf(NULL, true);
    922 			nestiobuf_setup(mbp, bp, offset, len);
    923 		}
    924 		bp->b_lblkno = 0;
    925 		bp->b_blkno = dblk;
    926 		bdev_strategy(bp);
    927 		dblk += btodb(len);
    928 		offset += len;
    929 		todo -= len;
    930 	}
    931 	error = biowait(mbp);
    932 	putiobuf(mbp);
    933 
    934 	mutex_enter(&sc->sc_slock);
    935 	scp->fc_type = (error ? FSS_CACHE_FREE : FSS_CACHE_VALID);
    936 	cv_broadcast(&scp->fc_state_cv);
    937 	if (error == 0) {
    938 		setbit(sc->sc_copied, scp->fc_cluster);
    939 		cv_signal(&sc->sc_work_cv);
    940 	}
    941 	mutex_exit(&sc->sc_slock);
    942 
    943 	return error;
    944 }
    945 
    946 /*
    947  * Read/write clusters from/to backing store.
    948  * For persistent snapshots must be called with cl == 0. off is the
    949  * offset into the snapshot.
    950  */
    951 static int
    952 fss_bs_io(struct fss_softc *sc, fss_io_type rw,
    953     u_int32_t cl, off_t off, int len, void *data)
    954 {
    955 	int error;
    956 
    957 	off += FSS_CLTOB(sc, cl);
    958 
    959 	vn_lock(sc->sc_bs_vp, LK_EXCLUSIVE|LK_RETRY);
    960 
    961 	error = vn_rdwr((rw == FSS_READ ? UIO_READ : UIO_WRITE), sc->sc_bs_vp,
    962 	    data, len, off, UIO_SYSSPACE, IO_UNIT|IO_NODELOCKED,
    963 	    sc->sc_bs_lwp->l_cred, NULL, NULL);
    964 	if (error == 0) {
    965 		mutex_enter(&sc->sc_bs_vp->v_interlock);
    966 		error = VOP_PUTPAGES(sc->sc_bs_vp, trunc_page(off),
    967 		    round_page(off+len), PGO_CLEANIT|PGO_SYNCIO|PGO_FREE);
    968 	}
    969 
    970 	VOP_UNLOCK(sc->sc_bs_vp, 0);
    971 
    972 	return error;
    973 }
    974 
    975 /*
    976  * Get a pointer to the indirect slot for this cluster.
    977  */
    978 static u_int32_t *
    979 fss_bs_indir(struct fss_softc *sc, u_int32_t cl)
    980 {
    981 	u_int32_t icl;
    982 	int ioff;
    983 
    984 	icl = cl/(FSS_CLSIZE(sc)/sizeof(u_int32_t));
    985 	ioff = cl%(FSS_CLSIZE(sc)/sizeof(u_int32_t));
    986 
    987 	if (sc->sc_indir_cur == icl)
    988 		return &sc->sc_indir_data[ioff];
    989 
    990 	if (sc->sc_indir_dirty) {
    991 		if (fss_bs_io(sc, FSS_WRITE, sc->sc_indir_cur, 0,
    992 		    FSS_CLSIZE(sc), (void *)sc->sc_indir_data) != 0)
    993 			return NULL;
    994 		setbit(sc->sc_indir_valid, sc->sc_indir_cur);
    995 	}
    996 
    997 	sc->sc_indir_dirty = 0;
    998 	sc->sc_indir_cur = icl;
    999 
   1000 	if (isset(sc->sc_indir_valid, sc->sc_indir_cur)) {
   1001 		if (fss_bs_io(sc, FSS_READ, sc->sc_indir_cur, 0,
   1002 		    FSS_CLSIZE(sc), (void *)sc->sc_indir_data) != 0)
   1003 			return NULL;
   1004 	} else
   1005 		memset(sc->sc_indir_data, 0, FSS_CLSIZE(sc));
   1006 
   1007 	return &sc->sc_indir_data[ioff];
   1008 }
   1009 
   1010 /*
   1011  * The kernel thread (one for every active snapshot).
   1012  *
   1013  * After wakeup it cleans the cache and runs the I/O requests.
   1014  */
   1015 static void
   1016 fss_bs_thread(void *arg)
   1017 {
   1018 	bool thread_idle, is_valid;
   1019 	int error, i, todo, len, crotor, is_read;
   1020 	long off;
   1021 	char *addr;
   1022 	u_int32_t c, cl, ch, *indirp;
   1023 	struct buf *bp, *nbp;
   1024 	struct fss_softc *sc;
   1025 	struct fss_cache *scp, *scl;
   1026 
   1027 	sc = arg;
   1028 	scl = sc->sc_cache+sc->sc_cache_size;
   1029 	crotor = 0;
   1030 	thread_idle = false;
   1031 
   1032 	mutex_enter(&sc->sc_slock);
   1033 
   1034 	for (;;) {
   1035 		if (thread_idle)
   1036 			cv_wait(&sc->sc_work_cv, &sc->sc_slock);
   1037 		thread_idle = true;
   1038 		if ((sc->sc_flags & FSS_BS_THREAD) == 0) {
   1039 			sc->sc_bs_lwp = NULL;
   1040 			mutex_exit(&sc->sc_slock);
   1041 			kthread_exit(0);
   1042 		}
   1043 
   1044 		/*
   1045 		 * Process I/O requests (persistent)
   1046 		 */
   1047 
   1048 		if (sc->sc_flags & FSS_PERSISTENT) {
   1049 			if ((bp = BUFQ_GET(sc->sc_bufq)) == NULL)
   1050 				continue;
   1051 			is_valid = FSS_ISVALID(sc);
   1052 			is_read = (bp->b_flags & B_READ);
   1053 			thread_idle = false;
   1054 			mutex_exit(&sc->sc_slock);
   1055 
   1056 			if (is_valid) {
   1057 				disk_busy(sc->sc_dkdev);
   1058 				error = fss_bs_io(sc, FSS_READ, 0,
   1059 				    dbtob(bp->b_blkno), bp->b_bcount,
   1060 				    bp->b_data);
   1061 				disk_unbusy(sc->sc_dkdev,
   1062 				    (error ? 0 : bp->b_bcount), is_read);
   1063 			} else
   1064 				error = ENXIO;
   1065 
   1066 			bp->b_error = error;
   1067 			bp->b_resid = (error ? bp->b_bcount : 0);
   1068 			biodone(bp);
   1069 
   1070 			mutex_enter(&sc->sc_slock);
   1071 			continue;
   1072 		}
   1073 
   1074 		/*
   1075 		 * Clean the cache
   1076 		 */
   1077 		for (i = 0; i < sc->sc_cache_size; i++) {
   1078 			crotor = (crotor + 1) % sc->sc_cache_size;
   1079 			scp = sc->sc_cache + crotor;
   1080 			if (scp->fc_type != FSS_CACHE_VALID)
   1081 				continue;
   1082 			mutex_exit(&sc->sc_slock);
   1083 
   1084 			thread_idle = false;
   1085 			indirp = fss_bs_indir(sc, scp->fc_cluster);
   1086 			if (indirp != NULL) {
   1087 				error = fss_bs_io(sc, FSS_WRITE, sc->sc_clnext,
   1088 				    0, FSS_CLSIZE(sc), scp->fc_data);
   1089 			} else
   1090 				error = EIO;
   1091 
   1092 			mutex_enter(&sc->sc_slock);
   1093 			if (error == 0) {
   1094 				*indirp = sc->sc_clnext++;
   1095 				sc->sc_indir_dirty = 1;
   1096 			} else
   1097 				fss_error(sc, "write error on backing store");
   1098 
   1099 			scp->fc_type = FSS_CACHE_FREE;
   1100 			cv_signal(&sc->sc_cache_cv);
   1101 			break;
   1102 		}
   1103 
   1104 		/*
   1105 		 * Process I/O requests
   1106 		 */
   1107 		if ((bp = BUFQ_GET(sc->sc_bufq)) == NULL)
   1108 			continue;
   1109 		is_valid = FSS_ISVALID(sc);
   1110 		is_read = (bp->b_flags & B_READ);
   1111 		thread_idle = false;
   1112 
   1113 		if (!is_valid) {
   1114 			mutex_exit(&sc->sc_slock);
   1115 
   1116 			bp->b_error = ENXIO;
   1117 			bp->b_resid = bp->b_bcount;
   1118 			biodone(bp);
   1119 
   1120 			mutex_enter(&sc->sc_slock);
   1121 			continue;
   1122 		}
   1123 
   1124 		disk_busy(sc->sc_dkdev);
   1125 
   1126 		/*
   1127 		 * First read from the snapshotted block device unless
   1128 		 * this request is completely covered by backing store.
   1129 		 */
   1130 
   1131 		cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
   1132 		off = FSS_CLOFF(sc, dbtob(bp->b_blkno));
   1133 		ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
   1134 		error = 0;
   1135 		bp->b_resid = 0;
   1136 		bp->b_error = 0;
   1137 		for (c = cl; c <= ch; c++) {
   1138 			if (isset(sc->sc_copied, c))
   1139 				continue;
   1140 			mutex_exit(&sc->sc_slock);
   1141 
   1142 			/* Not on backing store, read from device. */
   1143 			nbp = getiobuf(NULL, true);
   1144 			nbp->b_flags = B_READ;
   1145 			nbp->b_resid = nbp->b_bcount = bp->b_bcount;
   1146 			nbp->b_bufsize = bp->b_bcount;
   1147 			nbp->b_data = bp->b_data;
   1148 			nbp->b_blkno = bp->b_blkno;
   1149 			nbp->b_lblkno = 0;
   1150 			nbp->b_dev = sc->sc_bdev;
   1151 			SET(nbp->b_cflags, BC_BUSY);	/* mark buffer busy */
   1152 
   1153 			bdev_strategy(nbp);
   1154 
   1155 			error = biowait(nbp);
   1156 			if (error != 0) {
   1157 				bp->b_resid = bp->b_bcount;
   1158 				bp->b_error = nbp->b_error;
   1159 				disk_unbusy(sc->sc_dkdev, 0, is_read);
   1160 				biodone(bp);
   1161 			}
   1162 			putiobuf(nbp);
   1163 
   1164 			mutex_enter(&sc->sc_slock);
   1165 			break;
   1166 		}
   1167 		if (error)
   1168 			continue;
   1169 
   1170 		/*
   1171 		 * Replace those parts that have been saved to backing store.
   1172 		 */
   1173 
   1174 		addr = bp->b_data;
   1175 		todo = bp->b_bcount;
   1176 		for (c = cl; c <= ch; c++, off = 0, todo -= len, addr += len) {
   1177 			len = FSS_CLSIZE(sc)-off;
   1178 			if (len > todo)
   1179 				len = todo;
   1180 			if (isclr(sc->sc_copied, c))
   1181 				continue;
   1182 			mutex_exit(&sc->sc_slock);
   1183 
   1184 			indirp = fss_bs_indir(sc, c);
   1185 			if (indirp == NULL || *indirp == 0) {
   1186 				/*
   1187 				 * Not on backing store. Either in cache
   1188 				 * or hole in the snapshotted block device.
   1189 				 */
   1190 
   1191 				mutex_enter(&sc->sc_slock);
   1192 				for (scp = sc->sc_cache; scp < scl; scp++)
   1193 					if (scp->fc_type == FSS_CACHE_VALID &&
   1194 					    scp->fc_cluster == c)
   1195 						break;
   1196 				if (scp < scl)
   1197 					memcpy(addr, (char *)scp->fc_data+off,
   1198 					    len);
   1199 				else
   1200 					memset(addr, 0, len);
   1201 				continue;
   1202 			}
   1203 
   1204 			/*
   1205 			 * Read from backing store.
   1206 			 */
   1207 			error =
   1208 			    fss_bs_io(sc, FSS_READ, *indirp, off, len, addr);
   1209 
   1210 			mutex_enter(&sc->sc_slock);
   1211 			if (error) {
   1212 				bp->b_resid = bp->b_bcount;
   1213 				bp->b_error = error;
   1214 				break;
   1215 			}
   1216 		}
   1217 		mutex_exit(&sc->sc_slock);
   1218 
   1219 		disk_unbusy(sc->sc_dkdev, (error ? 0 : bp->b_bcount), is_read);
   1220 		biodone(bp);
   1221 
   1222 		mutex_enter(&sc->sc_slock);
   1223 	}
   1224 }
   1225 
   1226 #ifdef _MODULE
   1227 
   1228 #include <sys/module.h>
   1229 
   1230 MODULE(MODULE_CLASS_DRIVER, fss, NULL);
   1231 CFDRIVER_DECL(fss, DV_DISK, NULL);
   1232 
   1233 static int
   1234 fss_modcmd(modcmd_t cmd, void *arg)
   1235 {
   1236 	int bmajor = -1, cmajor = -1,  error = 0;
   1237 
   1238 	switch (cmd) {
   1239 	case MODULE_CMD_INIT:
   1240 		mutex_init(&fss_device_lock, MUTEX_DEFAULT, IPL_NONE);
   1241 		error = config_cfdriver_attach(&fss_cd);
   1242 		if (error) {
   1243 			mutex_destroy(&fss_device_lock);
   1244 			break;
   1245 		}
   1246 		error = config_cfattach_attach(fss_cd.cd_name, &fss_ca);
   1247 		if (error) {
   1248 			config_cfdriver_detach(&fss_cd);
   1249 			mutex_destroy(&fss_device_lock);
   1250 			break;
   1251 		}
   1252 		error = devsw_attach(fss_cd.cd_name,
   1253 		    &fss_bdevsw, &bmajor, &fss_cdevsw, &cmajor);
   1254 		if (error) {
   1255 			config_cfattach_detach(fss_cd.cd_name, &fss_ca);
   1256 			config_cfdriver_detach(&fss_cd);
   1257 			mutex_destroy(&fss_device_lock);
   1258 			break;
   1259 		}
   1260 		break;
   1261 
   1262 	case MODULE_CMD_FINI:
   1263 		error = config_cfattach_detach(fss_cd.cd_name, &fss_ca);
   1264 		if (error)
   1265 			break;
   1266 		config_cfdriver_detach(&fss_cd);
   1267 		devsw_detach(&fss_bdevsw, &fss_cdevsw);
   1268 		mutex_destroy(&fss_device_lock);
   1269 		break;
   1270 
   1271 	default:
   1272 		error = ENOTTY;
   1273 		break;
   1274 	}
   1275 
   1276 	return error;
   1277 }
   1278 
   1279 #endif /* _MODULE */
   1280