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