Home | History | Annotate | Line # | Download | only in dev
fss.c revision 1.104
      1 /*	$NetBSD: fss.c,v 1.104 2018/01/23 22:42:29 pgoyette 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.104 2018/01/23 22:42:29 pgoyette 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 *, size_t *);
     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 
    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 		bp->b_error = (write ? EROFS : ENXIO);
    284 		goto done;
    285 	}
    286 	/* Check bounds for non-persistent snapshots. */
    287 	if ((sc->sc_flags & FSS_PERSISTENT) == 0 &&
    288 	    bounds_check_with_mediasize(bp, DEV_BSIZE,
    289 	    btodb(FSS_CLTOB(sc, sc->sc_clcount - 1) + sc->sc_clresid)) <= 0)
    290 		goto done;
    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 	return;
    298 
    299 done:
    300 	mutex_exit(&sc->sc_slock);
    301 	bp->b_resid = bp->b_bcount;
    302 	biodone(bp);
    303 }
    304 
    305 int
    306 fss_read(dev_t dev, struct uio *uio, int flags)
    307 {
    308 	return physio(fss_strategy, NULL, dev, B_READ, minphys, uio);
    309 }
    310 
    311 int
    312 fss_write(dev_t dev, struct uio *uio, int flags)
    313 {
    314 	return physio(fss_strategy, NULL, dev, B_WRITE, minphys, uio);
    315 }
    316 
    317 int
    318 fss_ioctl(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
    319 {
    320 	int error;
    321 	struct fss_softc *sc = device_lookup_private(&fss_cd, minor(dev));
    322 	struct fss_set _fss;
    323 	struct fss_set *fss = (struct fss_set *)data;
    324 	struct fss_set50 *fss50 = (struct fss_set50 *)data;
    325 	struct fss_get *fsg = (struct fss_get *)data;
    326 #ifndef _LP64
    327 	struct fss_get50 *fsg50 = (struct fss_get50 *)data;
    328 #endif
    329 
    330 	switch (cmd) {
    331 	case FSSIOCSET50:
    332 		fss = &_fss;
    333 		fss->fss_mount = fss50->fss_mount;
    334 		fss->fss_bstore = fss50->fss_bstore;
    335 		fss->fss_csize = fss50->fss_csize;
    336 		fss->fss_flags = 0;
    337 		/* Fall through */
    338 	case FSSIOCSET:
    339 		mutex_enter(&sc->sc_lock);
    340 		if ((flag & FWRITE) == 0)
    341 			error = EPERM;
    342 		else if ((sc->sc_flags & FSS_ACTIVE) != 0)
    343 			error = EBUSY;
    344 		else
    345 			error = fss_create_snapshot(sc, fss, l);
    346 		if (error == 0)
    347 			sc->sc_uflags = fss->fss_flags;
    348 		mutex_exit(&sc->sc_lock);
    349 		break;
    350 
    351 	case FSSIOCCLR:
    352 		mutex_enter(&sc->sc_lock);
    353 		if ((flag & FWRITE) == 0)
    354 			error = EPERM;
    355 		else if ((sc->sc_flags & FSS_ACTIVE) == 0)
    356 			error = ENXIO;
    357 		else
    358 			error = fss_delete_snapshot(sc, l);
    359 		mutex_exit(&sc->sc_lock);
    360 		break;
    361 
    362 #ifndef _LP64
    363 	case FSSIOCGET50:
    364 		mutex_enter(&sc->sc_lock);
    365 		switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) {
    366 		case FSS_ACTIVE:
    367 			memcpy(fsg50->fsg_mount, sc->sc_mntname, MNAMELEN);
    368 			fsg50->fsg_csize = FSS_CLSIZE(sc);
    369 			timeval_to_timeval50(&sc->sc_time, &fsg50->fsg_time);
    370 			fsg50->fsg_mount_size = sc->sc_clcount;
    371 			fsg50->fsg_bs_size = sc->sc_clnext;
    372 			error = 0;
    373 			break;
    374 		case FSS_PERSISTENT | FSS_ACTIVE:
    375 			memcpy(fsg50->fsg_mount, sc->sc_mntname, MNAMELEN);
    376 			fsg50->fsg_csize = 0;
    377 			timeval_to_timeval50(&sc->sc_time, &fsg50->fsg_time);
    378 			fsg50->fsg_mount_size = 0;
    379 			fsg50->fsg_bs_size = 0;
    380 			error = 0;
    381 			break;
    382 		default:
    383 			error = ENXIO;
    384 			break;
    385 		}
    386 		mutex_exit(&sc->sc_lock);
    387 		break;
    388 #endif /* _LP64 */
    389 
    390 	case FSSIOCGET:
    391 		mutex_enter(&sc->sc_lock);
    392 		switch (sc->sc_flags & (FSS_PERSISTENT | FSS_ACTIVE)) {
    393 		case FSS_ACTIVE:
    394 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    395 			fsg->fsg_csize = FSS_CLSIZE(sc);
    396 			fsg->fsg_time = sc->sc_time;
    397 			fsg->fsg_mount_size = sc->sc_clcount;
    398 			fsg->fsg_bs_size = sc->sc_clnext;
    399 			error = 0;
    400 			break;
    401 		case FSS_PERSISTENT | FSS_ACTIVE:
    402 			memcpy(fsg->fsg_mount, sc->sc_mntname, MNAMELEN);
    403 			fsg->fsg_csize = 0;
    404 			fsg->fsg_time = sc->sc_time;
    405 			fsg->fsg_mount_size = 0;
    406 			fsg->fsg_bs_size = 0;
    407 			error = 0;
    408 			break;
    409 		default:
    410 			error = ENXIO;
    411 			break;
    412 		}
    413 		mutex_exit(&sc->sc_lock);
    414 		break;
    415 
    416 	case FSSIOFSET:
    417 		mutex_enter(&sc->sc_slock);
    418 		sc->sc_uflags = *(int *)data;
    419 		mutex_exit(&sc->sc_slock);
    420 		error = 0;
    421 		break;
    422 
    423 	case FSSIOFGET:
    424 		mutex_enter(&sc->sc_slock);
    425 		*(int *)data = sc->sc_uflags;
    426 		mutex_exit(&sc->sc_slock);
    427 		error = 0;
    428 		break;
    429 
    430 	default:
    431 		error = EINVAL;
    432 		break;
    433 	}
    434 
    435 	return error;
    436 }
    437 
    438 int
    439 fss_size(dev_t dev)
    440 {
    441 	return -1;
    442 }
    443 
    444 int
    445 fss_dump(dev_t dev, daddr_t blkno, void *va,
    446     size_t size)
    447 {
    448 	return EROFS;
    449 }
    450 
    451 /*
    452  * An error occurred reading or writing the snapshot or backing store.
    453  * If it is the first error log to console and disestablish cow handler.
    454  * The caller holds the mutex.
    455  */
    456 static inline void
    457 fss_error(struct fss_softc *sc, const char *msg)
    458 {
    459 
    460 	if ((sc->sc_flags & (FSS_ACTIVE | FSS_ERROR)) != FSS_ACTIVE)
    461 		return;
    462 
    463 	aprint_error_dev(sc->sc_dev, "snapshot invalid: %s\n", msg);
    464 	if ((sc->sc_flags & FSS_PERSISTENT) == 0)
    465 		fscow_disestablish(sc->sc_mount, fss_copy_on_write, sc);
    466 	sc->sc_flags |= FSS_ERROR;
    467 }
    468 
    469 /*
    470  * Allocate the variable sized parts of the softc and
    471  * fork the kernel thread.
    472  *
    473  * The fields sc_clcount, sc_clshift, sc_cache_size and sc_indir_size
    474  * must be initialized.
    475  */
    476 static int
    477 fss_softc_alloc(struct fss_softc *sc)
    478 {
    479 	int i, error;
    480 
    481 	if ((sc->sc_flags & FSS_PERSISTENT) == 0) {
    482 		sc->sc_copied =
    483 		    kmem_zalloc(howmany(sc->sc_clcount, NBBY), KM_SLEEP);
    484 		sc->sc_cache = kmem_alloc(sc->sc_cache_size *
    485 		    sizeof(struct fss_cache), KM_SLEEP);
    486 		for (i = 0; i < sc->sc_cache_size; i++) {
    487 			sc->sc_cache[i].fc_type = FSS_CACHE_FREE;
    488 			sc->sc_cache[i].fc_data =
    489 			    kmem_alloc(FSS_CLSIZE(sc), KM_SLEEP);
    490 			cv_init(&sc->sc_cache[i].fc_state_cv, "cowwait1");
    491 		}
    492 
    493 		sc->sc_indir_valid =
    494 		    kmem_zalloc(howmany(sc->sc_indir_size, NBBY), KM_SLEEP);
    495 		sc->sc_indir_data = kmem_zalloc(FSS_CLSIZE(sc), KM_SLEEP);
    496 	} else {
    497 		sc->sc_copied = NULL;
    498 		sc->sc_cache = NULL;
    499 		sc->sc_indir_valid = NULL;
    500 		sc->sc_indir_data = NULL;
    501 	}
    502 
    503 	sc->sc_flags |= FSS_BS_THREAD;
    504 	if ((error = kthread_create(PRI_BIO, KTHREAD_MUSTJOIN, NULL,
    505 	    fss_bs_thread, sc, &sc->sc_bs_lwp,
    506 	    "%s", device_xname(sc->sc_dev))) != 0) {
    507 		sc->sc_flags &= ~FSS_BS_THREAD;
    508 		return error;
    509 	}
    510 
    511 	disk_attach(sc->sc_dkdev);
    512 
    513 	return 0;
    514 }
    515 
    516 /*
    517  * Free the variable sized parts of the softc.
    518  */
    519 static void
    520 fss_softc_free(struct fss_softc *sc)
    521 {
    522 	int i;
    523 
    524 	if ((sc->sc_flags & FSS_BS_THREAD) != 0) {
    525 		mutex_enter(&sc->sc_slock);
    526 		sc->sc_flags &= ~FSS_BS_THREAD;
    527 		cv_signal(&sc->sc_work_cv);
    528 		mutex_exit(&sc->sc_slock);
    529 		kthread_join(sc->sc_bs_lwp);
    530 
    531 		disk_detach(sc->sc_dkdev);
    532 	}
    533 
    534 	if (sc->sc_copied != NULL)
    535 		kmem_free(sc->sc_copied, howmany(sc->sc_clcount, NBBY));
    536 	sc->sc_copied = NULL;
    537 
    538 	if (sc->sc_cache != NULL) {
    539 		for (i = 0; i < sc->sc_cache_size; i++)
    540 			if (sc->sc_cache[i].fc_data != NULL) {
    541 				cv_destroy(&sc->sc_cache[i].fc_state_cv);
    542 				kmem_free(sc->sc_cache[i].fc_data,
    543 				    FSS_CLSIZE(sc));
    544 			}
    545 		kmem_free(sc->sc_cache,
    546 		    sc->sc_cache_size*sizeof(struct fss_cache));
    547 	}
    548 	sc->sc_cache = NULL;
    549 
    550 	if (sc->sc_indir_valid != NULL)
    551 		kmem_free(sc->sc_indir_valid, howmany(sc->sc_indir_size, NBBY));
    552 	sc->sc_indir_valid = NULL;
    553 
    554 	if (sc->sc_indir_data != NULL)
    555 		kmem_free(sc->sc_indir_data, FSS_CLSIZE(sc));
    556 	sc->sc_indir_data = NULL;
    557 }
    558 
    559 /*
    560  * Set all active snapshots on this file system into ERROR state.
    561  */
    562 static void
    563 fss_unmount_hook(struct mount *mp)
    564 {
    565 	int i;
    566 	struct fss_softc *sc;
    567 
    568 	mutex_enter(&fss_device_lock);
    569 	for (i = 0; i < fss_cd.cd_ndevs; i++) {
    570 		if ((sc = device_lookup_private(&fss_cd, i)) == NULL)
    571 			continue;
    572 		mutex_enter(&sc->sc_slock);
    573 		if ((sc->sc_flags & FSS_ACTIVE) != 0 && sc->sc_mount == mp)
    574 			fss_error(sc, "forced by unmount");
    575 		mutex_exit(&sc->sc_slock);
    576 	}
    577 	mutex_exit(&fss_device_lock);
    578 }
    579 
    580 /*
    581  * A buffer is written to the snapshotted block device. Copy to
    582  * backing store if needed.
    583  */
    584 static int
    585 fss_copy_on_write(void *v, struct buf *bp, bool data_valid)
    586 {
    587 	int error;
    588 	u_int32_t cl, ch, c;
    589 	struct fss_softc *sc = v;
    590 
    591 	mutex_enter(&sc->sc_slock);
    592 	if (!FSS_ISVALID(sc)) {
    593 		mutex_exit(&sc->sc_slock);
    594 		return 0;
    595 	}
    596 
    597 	cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
    598 	ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
    599 	error = 0;
    600 	if (curlwp == uvm.pagedaemon_lwp) {
    601 		for (c = cl; c <= ch; c++)
    602 			if (isclr(sc->sc_copied, c)) {
    603 				error = ENOMEM;
    604 				break;
    605 			}
    606 	}
    607 	mutex_exit(&sc->sc_slock);
    608 
    609 	if (error == 0)
    610 		for (c = cl; c <= ch; c++) {
    611 			error = fss_read_cluster(sc, c);
    612 			if (error)
    613 				break;
    614 		}
    615 
    616 	return error;
    617 }
    618 
    619 /*
    620  * Lookup and open needed files.
    621  *
    622  * For file system internal snapshot initializes sc_mntname, sc_mount,
    623  * sc_bs_vp and sc_time.
    624  *
    625  * Otherwise returns dev and size of the underlying block device.
    626  * Initializes sc_mntname, sc_mount, sc_bdev, sc_bs_vp and sc_mount
    627  */
    628 static int
    629 fss_create_files(struct fss_softc *sc, struct fss_set *fss,
    630     off_t *bsize, struct lwp *l)
    631 {
    632 	int error, bits, fsbsize;
    633 	uint64_t numsec;
    634 	unsigned int secsize;
    635 	struct timespec ts;
    636 	/* nd -> nd2 to reduce mistakes while updating only some namei calls */
    637 	struct pathbuf *pb2;
    638 	struct nameidata nd2;
    639 	struct vnode *vp;
    640 
    641 	/*
    642 	 * Get the mounted file system.
    643 	 */
    644 
    645 	error = namei_simple_user(fss->fss_mount,
    646 				NSM_FOLLOW_NOEMULROOT, &vp);
    647 	if (error != 0)
    648 		return error;
    649 
    650 	if ((vp->v_vflag & VV_ROOT) != VV_ROOT) {
    651 		vrele(vp);
    652 		return EINVAL;
    653 	}
    654 
    655 	sc->sc_mount = vp->v_mount;
    656 	memcpy(sc->sc_mntname, sc->sc_mount->mnt_stat.f_mntonname, MNAMELEN);
    657 
    658 	vrele(vp);
    659 
    660 	/*
    661 	 * Check for file system internal snapshot.
    662 	 */
    663 
    664 	error = namei_simple_user(fss->fss_bstore,
    665 				NSM_FOLLOW_NOEMULROOT, &vp);
    666 	if (error != 0)
    667 		return error;
    668 
    669 	if (vp->v_type == VREG && vp->v_mount == sc->sc_mount) {
    670 		sc->sc_flags |= FSS_PERSISTENT;
    671 		sc->sc_bs_vp = vp;
    672 
    673 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    674 		bits = sizeof(sc->sc_bs_bshift)*NBBY;
    675 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < bits;
    676 		    sc->sc_bs_bshift++)
    677 			if (FSS_FSBSIZE(sc) == fsbsize)
    678 				break;
    679 		if (sc->sc_bs_bshift >= bits)
    680 			return EINVAL;
    681 
    682 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    683 		sc->sc_clshift = 0;
    684 
    685 		if ((fss->fss_flags & FSS_UNLINK_ON_CREATE) != 0) {
    686 			error = do_sys_unlink(fss->fss_bstore, UIO_USERSPACE);
    687 			if (error)
    688 				return error;
    689 		}
    690 		error = vn_lock(vp, LK_EXCLUSIVE);
    691 		if (error != 0)
    692 			return error;
    693 		error = VFS_SNAPSHOT(sc->sc_mount, sc->sc_bs_vp, &ts);
    694 		TIMESPEC_TO_TIMEVAL(&sc->sc_time, &ts);
    695 
    696 		VOP_UNLOCK(sc->sc_bs_vp);
    697 
    698 		return error;
    699 	}
    700 	vrele(vp);
    701 
    702 	/*
    703 	 * Get the block device it is mounted on and its size.
    704 	 */
    705 
    706 	error = spec_node_lookup_by_mount(sc->sc_mount, &vp);
    707 	if (error)
    708 		return error;
    709 	sc->sc_bdev = vp->v_rdev;
    710 
    711 	error = getdisksize(vp, &numsec, &secsize);
    712 	vrele(vp);
    713 	if (error)
    714 		return error;
    715 
    716 	*bsize = (off_t)numsec*secsize;
    717 
    718 	/*
    719 	 * Get the backing store
    720 	 */
    721 
    722 	error = pathbuf_copyin(fss->fss_bstore, &pb2);
    723 	if (error) {
    724  		return error;
    725 	}
    726 	NDINIT(&nd2, LOOKUP, FOLLOW, pb2);
    727 	if ((error = vn_open(&nd2, FREAD|FWRITE, 0)) != 0) {
    728 		pathbuf_destroy(pb2);
    729 		return error;
    730 	}
    731 	VOP_UNLOCK(nd2.ni_vp);
    732 
    733 	sc->sc_bs_vp = nd2.ni_vp;
    734 
    735 	if (nd2.ni_vp->v_type != VREG && nd2.ni_vp->v_type != VCHR) {
    736 		pathbuf_destroy(pb2);
    737 		return EINVAL;
    738 	}
    739 	pathbuf_destroy(pb2);
    740 
    741 	if ((fss->fss_flags & FSS_UNLINK_ON_CREATE) != 0) {
    742 		error = do_sys_unlink(fss->fss_bstore, UIO_USERSPACE);
    743 		if (error)
    744 			return error;
    745 	}
    746 	if (sc->sc_bs_vp->v_type == VREG) {
    747 		fsbsize = sc->sc_bs_vp->v_mount->mnt_stat.f_iosize;
    748 		if (fsbsize & (fsbsize-1))	/* No power of two */
    749 			return EINVAL;
    750 		for (sc->sc_bs_bshift = 1; sc->sc_bs_bshift < 32;
    751 		    sc->sc_bs_bshift++)
    752 			if (FSS_FSBSIZE(sc) == fsbsize)
    753 				break;
    754 		if (sc->sc_bs_bshift >= 32)
    755 			return EINVAL;
    756 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    757 	} else {
    758 		sc->sc_bs_bshift = DEV_BSHIFT;
    759 		sc->sc_bs_bmask = FSS_FSBSIZE(sc)-1;
    760 	}
    761 
    762 	return 0;
    763 }
    764 
    765 /*
    766  * Create a snapshot.
    767  */
    768 static int
    769 fss_create_snapshot(struct fss_softc *sc, struct fss_set *fss, struct lwp *l)
    770 {
    771 	int len, error;
    772 	u_int32_t csize;
    773 	off_t bsize;
    774 
    775 	bsize = 0;	/* XXX gcc */
    776 
    777 	/*
    778 	 * Open needed files.
    779 	 */
    780 	if ((error = fss_create_files(sc, fss, &bsize, l)) != 0)
    781 		goto bad;
    782 
    783 	if (sc->sc_flags & FSS_PERSISTENT) {
    784 		fss_softc_alloc(sc);
    785 		sc->sc_flags |= FSS_ACTIVE;
    786 		return 0;
    787 	}
    788 
    789 	/*
    790 	 * Set cluster size. Must be a power of two and
    791 	 * a multiple of backing store block size.
    792 	 */
    793 	if (fss->fss_csize <= 0)
    794 		csize = MAXPHYS;
    795 	else
    796 		csize = fss->fss_csize;
    797 	if (bsize/csize > FSS_CLUSTER_MAX)
    798 		csize = bsize/FSS_CLUSTER_MAX+1;
    799 
    800 	for (sc->sc_clshift = sc->sc_bs_bshift; sc->sc_clshift < 32;
    801 	    sc->sc_clshift++)
    802 		if (FSS_CLSIZE(sc) >= csize)
    803 			break;
    804 	if (sc->sc_clshift >= 32) {
    805 		error = EINVAL;
    806 		goto bad;
    807 	}
    808 	sc->sc_clmask = FSS_CLSIZE(sc)-1;
    809 
    810 	/*
    811 	 * Set number of cache slots.
    812 	 */
    813 	if (FSS_CLSIZE(sc) <= 8192)
    814 		sc->sc_cache_size = 32;
    815 	else if (FSS_CLSIZE(sc) <= 65536)
    816 		sc->sc_cache_size = 8;
    817 	else
    818 		sc->sc_cache_size = 4;
    819 
    820 	/*
    821 	 * Set number of clusters and size of last cluster.
    822 	 */
    823 	sc->sc_clcount = FSS_BTOCL(sc, bsize-1)+1;
    824 	sc->sc_clresid = FSS_CLOFF(sc, bsize-1)+1;
    825 
    826 	/*
    827 	 * Set size of indirect table.
    828 	 */
    829 	len = sc->sc_clcount*sizeof(u_int32_t);
    830 	sc->sc_indir_size = FSS_BTOCL(sc, len)+1;
    831 	sc->sc_clnext = sc->sc_indir_size;
    832 	sc->sc_indir_cur = 0;
    833 
    834 	if ((error = fss_softc_alloc(sc)) != 0)
    835 		goto bad;
    836 
    837 	/*
    838 	 * Activate the snapshot.
    839 	 */
    840 
    841 	if ((error = vfs_suspend(sc->sc_mount, 0)) != 0)
    842 		goto bad;
    843 
    844 	microtime(&sc->sc_time);
    845 
    846 	vrele_flush(sc->sc_mount);
    847 	error = VFS_SYNC(sc->sc_mount, MNT_WAIT, curlwp->l_cred);
    848 	if (error == 0)
    849 		error = fscow_establish(sc->sc_mount, fss_copy_on_write, sc);
    850 	if (error == 0)
    851 		sc->sc_flags |= FSS_ACTIVE;
    852 
    853 	vfs_resume(sc->sc_mount);
    854 
    855 	if (error != 0)
    856 		goto bad;
    857 
    858 	aprint_debug_dev(sc->sc_dev, "%s snapshot active\n", sc->sc_mntname);
    859 	aprint_debug_dev(sc->sc_dev,
    860 	    "%u clusters of %u, %u cache slots, %u indir clusters\n",
    861 	    sc->sc_clcount, FSS_CLSIZE(sc),
    862 	    sc->sc_cache_size, sc->sc_indir_size);
    863 
    864 	return 0;
    865 
    866 bad:
    867 	fss_softc_free(sc);
    868 	if (sc->sc_bs_vp != NULL) {
    869 		if (sc->sc_flags & FSS_PERSISTENT)
    870 			vrele(sc->sc_bs_vp);
    871 		else
    872 			vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_cred);
    873 	}
    874 	sc->sc_bs_vp = NULL;
    875 
    876 	return error;
    877 }
    878 
    879 /*
    880  * Delete a snapshot.
    881  */
    882 static int
    883 fss_delete_snapshot(struct fss_softc *sc, struct lwp *l)
    884 {
    885 
    886 	if ((sc->sc_flags & (FSS_PERSISTENT | FSS_ERROR)) == 0)
    887 		fscow_disestablish(sc->sc_mount, fss_copy_on_write, sc);
    888 
    889 	mutex_enter(&sc->sc_slock);
    890 	sc->sc_flags &= ~(FSS_ACTIVE|FSS_ERROR);
    891 	sc->sc_mount = NULL;
    892 	sc->sc_bdev = NODEV;
    893 	mutex_exit(&sc->sc_slock);
    894 
    895 	fss_softc_free(sc);
    896 	if (sc->sc_flags & FSS_PERSISTENT)
    897 		vrele(sc->sc_bs_vp);
    898 	else
    899 		vn_close(sc->sc_bs_vp, FREAD|FWRITE, l->l_cred);
    900 	sc->sc_bs_vp = NULL;
    901 	sc->sc_flags &= ~FSS_PERSISTENT;
    902 
    903 	return 0;
    904 }
    905 
    906 /*
    907  * Read a cluster from the snapshotted block device to the cache.
    908  */
    909 static int
    910 fss_read_cluster(struct fss_softc *sc, u_int32_t cl)
    911 {
    912 	int error, todo, offset, len;
    913 	daddr_t dblk;
    914 	struct buf *bp, *mbp;
    915 	struct fss_cache *scp, *scl;
    916 
    917 	/*
    918 	 * Get a free cache slot.
    919 	 */
    920 	scl = sc->sc_cache+sc->sc_cache_size;
    921 
    922 	mutex_enter(&sc->sc_slock);
    923 
    924 restart:
    925 	if (isset(sc->sc_copied, cl) || !FSS_ISVALID(sc)) {
    926 		mutex_exit(&sc->sc_slock);
    927 		return 0;
    928 	}
    929 
    930 	for (scp = sc->sc_cache; scp < scl; scp++)
    931 		if (scp->fc_cluster == cl) {
    932 			if (scp->fc_type == FSS_CACHE_VALID) {
    933 				mutex_exit(&sc->sc_slock);
    934 				return 0;
    935 			} else if (scp->fc_type == FSS_CACHE_BUSY) {
    936 				cv_wait(&scp->fc_state_cv, &sc->sc_slock);
    937 				goto restart;
    938 			}
    939 		}
    940 
    941 	for (scp = sc->sc_cache; scp < scl; scp++)
    942 		if (scp->fc_type == FSS_CACHE_FREE) {
    943 			scp->fc_type = FSS_CACHE_BUSY;
    944 			scp->fc_cluster = cl;
    945 			break;
    946 		}
    947 	if (scp >= scl) {
    948 		cv_wait(&sc->sc_cache_cv, &sc->sc_slock);
    949 		goto restart;
    950 	}
    951 
    952 	mutex_exit(&sc->sc_slock);
    953 
    954 	/*
    955 	 * Start the read.
    956 	 */
    957 	dblk = btodb(FSS_CLTOB(sc, cl));
    958 	if (cl == sc->sc_clcount-1) {
    959 		todo = sc->sc_clresid;
    960 		memset((char *)scp->fc_data + todo, 0, FSS_CLSIZE(sc) - todo);
    961 	} else
    962 		todo = FSS_CLSIZE(sc);
    963 	offset = 0;
    964 	mbp = getiobuf(NULL, true);
    965 	mbp->b_bufsize = todo;
    966 	mbp->b_data = scp->fc_data;
    967 	mbp->b_resid = mbp->b_bcount = todo;
    968 	mbp->b_flags = B_READ;
    969 	mbp->b_cflags = BC_BUSY;
    970 	mbp->b_dev = sc->sc_bdev;
    971 	while (todo > 0) {
    972 		len = todo;
    973 		if (len > MAXPHYS)
    974 			len = MAXPHYS;
    975 		if (btodb(FSS_CLTOB(sc, cl)) == dblk && len == todo)
    976 			bp = mbp;
    977 		else {
    978 			bp = getiobuf(NULL, true);
    979 			nestiobuf_setup(mbp, bp, offset, len);
    980 		}
    981 		bp->b_lblkno = 0;
    982 		bp->b_blkno = dblk;
    983 		bdev_strategy(bp);
    984 		dblk += btodb(len);
    985 		offset += len;
    986 		todo -= len;
    987 	}
    988 	error = biowait(mbp);
    989 	if (error == 0 && mbp->b_resid != 0)
    990 		error = EIO;
    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, size_t *resid)
   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, resid, 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, NULL) != 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, NULL) != 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 	size_t resid;
   1084 	struct buf *bp, *nbp;
   1085 	struct fss_softc *sc;
   1086 	struct fss_cache *scp, *scl;
   1087 
   1088 	sc = arg;
   1089 	scl = sc->sc_cache+sc->sc_cache_size;
   1090 	crotor = 0;
   1091 	thread_idle = false;
   1092 
   1093 	mutex_enter(&sc->sc_slock);
   1094 
   1095 	for (;;) {
   1096 		if (thread_idle)
   1097 			cv_wait(&sc->sc_work_cv, &sc->sc_slock);
   1098 		thread_idle = true;
   1099 		if ((sc->sc_flags & FSS_BS_THREAD) == 0) {
   1100 			mutex_exit(&sc->sc_slock);
   1101 			kthread_exit(0);
   1102 		}
   1103 
   1104 		/*
   1105 		 * Process I/O requests (persistent)
   1106 		 */
   1107 
   1108 		if (sc->sc_flags & FSS_PERSISTENT) {
   1109 			if ((bp = bufq_get(sc->sc_bufq)) == NULL)
   1110 				continue;
   1111 			is_valid = FSS_ISVALID(sc);
   1112 			is_read = (bp->b_flags & B_READ);
   1113 			thread_idle = false;
   1114 			mutex_exit(&sc->sc_slock);
   1115 
   1116 			if (is_valid) {
   1117 				disk_busy(sc->sc_dkdev);
   1118 				error = fss_bs_io(sc, FSS_READ, 0,
   1119 				    dbtob(bp->b_blkno), bp->b_bcount,
   1120 				    bp->b_data, &resid);
   1121 				if (error)
   1122 					resid = bp->b_bcount;
   1123 				disk_unbusy(sc->sc_dkdev,
   1124 				    (error ? 0 : bp->b_bcount), is_read);
   1125 			} else {
   1126 				error = ENXIO;
   1127 				resid = bp->b_bcount;
   1128 			}
   1129 
   1130 			bp->b_error = error;
   1131 			bp->b_resid = resid;
   1132 			biodone(bp);
   1133 
   1134 			mutex_enter(&sc->sc_slock);
   1135 			continue;
   1136 		}
   1137 
   1138 		/*
   1139 		 * Clean the cache
   1140 		 */
   1141 		for (i = 0; i < sc->sc_cache_size; i++) {
   1142 			crotor = (crotor + 1) % sc->sc_cache_size;
   1143 			scp = sc->sc_cache + crotor;
   1144 			if (scp->fc_type != FSS_CACHE_VALID)
   1145 				continue;
   1146 			mutex_exit(&sc->sc_slock);
   1147 
   1148 			thread_idle = false;
   1149 			indirp = fss_bs_indir(sc, scp->fc_cluster);
   1150 			if (indirp != NULL) {
   1151 				error = fss_bs_io(sc, FSS_WRITE, sc->sc_clnext,
   1152 				    0, FSS_CLSIZE(sc), scp->fc_data, NULL);
   1153 			} else
   1154 				error = EIO;
   1155 
   1156 			mutex_enter(&sc->sc_slock);
   1157 			if (error == 0) {
   1158 				*indirp = sc->sc_clnext++;
   1159 				sc->sc_indir_dirty = 1;
   1160 			} else
   1161 				fss_error(sc, "write error on backing store");
   1162 
   1163 			scp->fc_type = FSS_CACHE_FREE;
   1164 			cv_broadcast(&sc->sc_cache_cv);
   1165 			break;
   1166 		}
   1167 
   1168 		/*
   1169 		 * Process I/O requests
   1170 		 */
   1171 		if ((bp = bufq_get(sc->sc_bufq)) == NULL)
   1172 			continue;
   1173 		is_valid = FSS_ISVALID(sc);
   1174 		is_read = (bp->b_flags & B_READ);
   1175 		thread_idle = false;
   1176 
   1177 		if (!is_valid) {
   1178 			mutex_exit(&sc->sc_slock);
   1179 
   1180 			bp->b_error = ENXIO;
   1181 			bp->b_resid = bp->b_bcount;
   1182 			biodone(bp);
   1183 
   1184 			mutex_enter(&sc->sc_slock);
   1185 			continue;
   1186 		}
   1187 
   1188 		disk_busy(sc->sc_dkdev);
   1189 
   1190 		/*
   1191 		 * First read from the snapshotted block device unless
   1192 		 * this request is completely covered by backing store.
   1193 		 */
   1194 
   1195 		cl = FSS_BTOCL(sc, dbtob(bp->b_blkno));
   1196 		off = FSS_CLOFF(sc, dbtob(bp->b_blkno));
   1197 		ch = FSS_BTOCL(sc, dbtob(bp->b_blkno)+bp->b_bcount-1);
   1198 		error = 0;
   1199 		bp->b_resid = 0;
   1200 		bp->b_error = 0;
   1201 		for (c = cl; c <= ch; c++) {
   1202 			if (isset(sc->sc_copied, c))
   1203 				continue;
   1204 			mutex_exit(&sc->sc_slock);
   1205 
   1206 			/* Not on backing store, read from device. */
   1207 			nbp = getiobuf(NULL, true);
   1208 			nbp->b_flags = B_READ;
   1209 			nbp->b_resid = nbp->b_bcount = bp->b_bcount;
   1210 			nbp->b_bufsize = bp->b_bcount;
   1211 			nbp->b_data = bp->b_data;
   1212 			nbp->b_blkno = bp->b_blkno;
   1213 			nbp->b_lblkno = 0;
   1214 			nbp->b_dev = sc->sc_bdev;
   1215 			SET(nbp->b_cflags, BC_BUSY);	/* mark buffer busy */
   1216 
   1217 			bdev_strategy(nbp);
   1218 
   1219 			error = biowait(nbp);
   1220 			if (error == 0 && nbp->b_resid != 0)
   1221 				error = EIO;
   1222 			if (error != 0) {
   1223 				bp->b_resid = bp->b_bcount;
   1224 				bp->b_error = nbp->b_error;
   1225 				disk_unbusy(sc->sc_dkdev, 0, is_read);
   1226 				biodone(bp);
   1227 			}
   1228 			putiobuf(nbp);
   1229 
   1230 			mutex_enter(&sc->sc_slock);
   1231 			break;
   1232 		}
   1233 		if (error)
   1234 			continue;
   1235 
   1236 		/*
   1237 		 * Replace those parts that have been saved to backing store.
   1238 		 */
   1239 
   1240 		addr = bp->b_data;
   1241 		todo = bp->b_bcount;
   1242 		for (c = cl; c <= ch; c++, off = 0, todo -= len, addr += len) {
   1243 			len = FSS_CLSIZE(sc)-off;
   1244 			if (len > todo)
   1245 				len = todo;
   1246 			if (isclr(sc->sc_copied, c))
   1247 				continue;
   1248 			mutex_exit(&sc->sc_slock);
   1249 
   1250 			indirp = fss_bs_indir(sc, c);
   1251 			if (indirp == NULL || *indirp == 0) {
   1252 				/*
   1253 				 * Not on backing store. Either in cache
   1254 				 * or hole in the snapshotted block device.
   1255 				 */
   1256 
   1257 				mutex_enter(&sc->sc_slock);
   1258 				for (scp = sc->sc_cache; scp < scl; scp++)
   1259 					if (scp->fc_type == FSS_CACHE_VALID &&
   1260 					    scp->fc_cluster == c)
   1261 						break;
   1262 				if (scp < scl)
   1263 					memcpy(addr, (char *)scp->fc_data+off,
   1264 					    len);
   1265 				else
   1266 					memset(addr, 0, len);
   1267 				continue;
   1268 			}
   1269 
   1270 			/*
   1271 			 * Read from backing store.
   1272 			 */
   1273 			error = fss_bs_io(sc, FSS_READ,
   1274 			    *indirp, off, len, addr, NULL);
   1275 
   1276 			mutex_enter(&sc->sc_slock);
   1277 			if (error) {
   1278 				bp->b_resid = bp->b_bcount;
   1279 				bp->b_error = error;
   1280 				break;
   1281 			}
   1282 		}
   1283 		mutex_exit(&sc->sc_slock);
   1284 
   1285 		disk_unbusy(sc->sc_dkdev, (error ? 0 : bp->b_bcount), is_read);
   1286 		biodone(bp);
   1287 
   1288 		mutex_enter(&sc->sc_slock);
   1289 	}
   1290 }
   1291 
   1292 #ifdef _MODULE
   1293 
   1294 #include <sys/module.h>
   1295 
   1296 MODULE(MODULE_CLASS_DRIVER, fss, "bufq_fcfs");
   1297 CFDRIVER_DECL(fss, DV_DISK, NULL);
   1298 
   1299 devmajor_t fss_bmajor = -1, fss_cmajor = -1;
   1300 
   1301 static int
   1302 fss_modcmd(modcmd_t cmd, void *arg)
   1303 {
   1304 	int error = 0;
   1305 
   1306 	switch (cmd) {
   1307 	case MODULE_CMD_INIT:
   1308 		mutex_init(&fss_device_lock, MUTEX_DEFAULT, IPL_NONE);
   1309 		error = config_cfdriver_attach(&fss_cd);
   1310 		if (error) {
   1311 			mutex_destroy(&fss_device_lock);
   1312 			break;
   1313 		}
   1314 		error = config_cfattach_attach(fss_cd.cd_name, &fss_ca);
   1315 		if (error) {
   1316 			config_cfdriver_detach(&fss_cd);
   1317 			mutex_destroy(&fss_device_lock);
   1318 			break;
   1319 		}
   1320 		error = devsw_attach(fss_cd.cd_name,
   1321 		    &fss_bdevsw, &fss_bmajor, &fss_cdevsw, &fss_cmajor);
   1322 
   1323 		if (error) {
   1324 			config_cfattach_detach(fss_cd.cd_name, &fss_ca);
   1325 			config_cfdriver_detach(&fss_cd);
   1326 			mutex_destroy(&fss_device_lock);
   1327 			break;
   1328 		}
   1329 		break;
   1330 
   1331 	case MODULE_CMD_FINI:
   1332 		devsw_detach(&fss_bdevsw, &fss_cdevsw);
   1333 		error = config_cfattach_detach(fss_cd.cd_name, &fss_ca);
   1334 		if (error) {
   1335 			devsw_attach(fss_cd.cd_name, &fss_bdevsw, &fss_bmajor,
   1336 			    &fss_cdevsw, &fss_cmajor);
   1337 			break;
   1338 		}
   1339 		error = config_cfdriver_detach(&fss_cd);
   1340 		if (error) {
   1341 			devsw_attach(fss_cd.cd_name,
   1342 			    &fss_bdevsw, &fss_bmajor, &fss_cdevsw, &fss_cmajor);
   1343 			devsw_attach(fss_cd.cd_name, &fss_bdevsw, &fss_bmajor,
   1344 			    &fss_cdevsw, &fss_cmajor);
   1345 			break;
   1346 		}
   1347 		mutex_destroy(&fss_device_lock);
   1348 		break;
   1349 
   1350 	default:
   1351 		error = ENOTTY;
   1352 		break;
   1353 	}
   1354 
   1355 	return error;
   1356 }
   1357 
   1358 #endif /* _MODULE */
   1359