1 /*        $NetBSD: bio_30.c,v 1.5 2020/12/19 22:10:56 thorpej Exp $ */
2 /*        $OpenBSD: bio.c,v 1.9 2007/03/20 02:35:55 marco Exp $       */
3 
4 /*
5  * Copyright (c) 2002 Niklas Hallqvist.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: bio_30.c,v 1.5 2020/12/19 22:10:56 thorpej Exp $");
30 
31 #if defined(_KERNEL_OPT)
32 #include "opt_compat_netbsd.h"
33 #endif
34 
35 #include <sys/param.h>
36 #include <sys/conf.h>
37 #include <sys/device.h>
38 #include <sys/event.h>
39 #include <sys/ioctl.h>
40 #include <sys/kmem.h>
41 #include <sys/queue.h>
42 #include <sys/systm.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/kauth.h>
46 #include <sys/compat_stub.h>
47 
48 #include <dev/biovar.h>
49 #include <dev/sysmon/sysmonvar.h>
50 
51 #include <compat/common/compat_mod.h>
52 
53 static int
compat_30_bio(void * cookie,u_long cmd,void * addr,int (* delegate)(void *,u_long,void *))54 compat_30_bio(void *cookie, u_long cmd, void *addr,
55     int (*delegate)(void *, u_long, void *))
56 {
57           int error;
58 
59           switch (cmd) {
60           case OBIOCDISK: {
61                     struct bioc_disk *bd = kmem_zalloc(sizeof(*bd), KM_SLEEP);
62 
63                     (void)memcpy(bd, addr, sizeof(struct obioc_disk));
64                     error = (*delegate)(cookie, BIOCDISK, bd);
65                     if (error) {
66                               kmem_free(bd, sizeof(*bd));
67                               return error;
68                     }
69 
70                     (void)memcpy(addr, bd, sizeof(struct obioc_disk));
71                     kmem_free(bd, sizeof(*bd));
72                     return 0;
73           }
74           case OBIOCVOL: {
75                     struct bioc_vol *bv = kmem_zalloc(sizeof(*bv), KM_SLEEP);
76 
77                     (void)memcpy(bv, addr, sizeof(struct obioc_vol));
78                     error = (*delegate)(cookie, BIOCVOL, bv);
79                     if (error) {
80                               kmem_free(bv, sizeof(*bv));
81                               return error;
82                     }
83 
84                     (void)memcpy(addr, bv, sizeof(struct obioc_vol));
85                     kmem_free(bv, sizeof(*bv));
86                     return 0;
87           }
88           default:
89                     return ENOSYS;
90           }
91 }
92 
93 void
bio_30_init(void)94 bio_30_init(void)
95 {
96 
97           MODULE_HOOK_SET(compat_bio_30_hook, compat_30_bio);
98 }
99 
100 void
bio_30_fini(void)101 bio_30_fini(void)
102 {
103 
104           MODULE_HOOK_UNSET(compat_bio_30_hook);
105 }
106