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