Port cache manager to NetBSD-5 and NetBSD-current
[openafs.git] / src / afs / NBSD / osi_kmod.c
1 /*
2  * Copyright (c) 2011 Jonathan A. Kollasch
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
15  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <afsconfig.h>
28 #include "afs/param.h"
29
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/kernel.h>
34 #if !defined(AFS_NBSD60_ENV)
35 #include <sys/lkm.h>
36 #endif
37 #include <sys/module.h>
38 #include <sys/systm.h>
39 #include <sys/syscall.h>
40 #include <sys/syscallargs.h>
41 #include <sys/syscallvar.h>
42
43 #include "afs/sysincludes.h"    /* Standard vendor system headers */
44 #include "afs/afsincludes.h"    /* Afs-based standard headers */
45
46 extern int afs3_syscall(struct lwp *, const void *, register_t *);
47 extern int afs_xioctl(struct lwp *, const void *, register_t *);
48 extern int Afs_xsetgroups(struct lwp *, const void *, register_t *);
49
50 #if !defined(AFS_NBSD60_ENV)
51 extern int sys_lkmnosys(struct lwp *, const void *, register_t *);
52 #endif
53
54 extern struct vfsops afs_vfsops;
55
56 static const struct sysent openafs_sysent = {
57         sizeof(struct afs_sysargs)/sizeof(register_t),
58         sizeof(struct afs_sysargs),
59         0,
60         afs3_syscall,
61 };
62
63 static struct sysent old_sysent;
64 static sy_call_t *old_setgroups;
65 static sy_call_t *old_ioctl;
66
67 MODULE(MODULE_CLASS_VFS, openafs, NULL);
68
69 static int
70 openafs_modcmd(modcmd_t cmd, void *arg)
71 {
72         struct sysent *se;
73         int error;
74
75 #ifndef RUMP
76         se = sysent;
77 #else
78         se = emul_netbsd.e_sysent;
79 #endif
80
81         switch (cmd) {
82         case MODULE_CMD_INIT:
83                 error = vfs_attach(&afs_vfsops);
84                 if (error != 0)
85                         break;
86                 old_sysent = se[AFS_SYSCALL];
87                 old_setgroups = se[SYS_setgroups].sy_call;
88                 old_ioctl = se[SYS_ioctl].sy_call;
89 #if defined(AFS_NBSD60_ENV)
90 # ifndef RUMP
91                 if (old_sysent.sy_call == sys_nosys) {
92 # else
93                 if (true) {
94 # endif
95 #else
96                 if (old_sysent.sy_call == sys_lkmnosys) {
97 #endif
98 #if defined(AFS_NBSD60_ENV)
99                         kernconfig_lock();
100 #endif
101                         se[AFS_SYSCALL] = openafs_sysent;
102                         se[SYS_setgroups].sy_call = Afs_xsetgroups;
103                         se[SYS_ioctl].sy_call = afs_xioctl;
104 #if defined(AFS_NBSD60_ENV)
105                         kernconfig_unlock();
106 #endif
107                 } else {
108                         error = EBUSY;
109                 }
110                 if (error != 0)
111                         break;
112                 break;
113         case MODULE_CMD_FINI:
114 #if defined(AFS_NBSD60_ENV)
115                 kernconfig_lock();
116 #endif
117                 se[SYS_ioctl].sy_call = old_ioctl;
118                 se[SYS_setgroups].sy_call = old_setgroups;
119                 se[AFS_SYSCALL] = old_sysent;
120 #if defined(AFS_NBSD60_ENV)
121                 kernconfig_unlock();
122 #endif
123                 error = vfs_detach(&afs_vfsops);
124                 if (error != 0)
125                         break;
126                 break;
127         default:
128                 error = ENOTTY;
129                 break;
130         }
131
132         return error;
133 }