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