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