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