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