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