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