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