2 * vi:set cin noet sw=4 tw=70:
3 * Copyright 2006, International Business Machines Corporation and others.
6 * This software has been released under the terms of the IBM Public
7 * License. For details, see the LICENSE file in the top-level source
8 * directory or online at http://www.openafs.org/dl/license10.html
12 * Filesystem export operations for Linux
14 #include <afsconfig.h>
15 #include "afs/param.h"
20 #if !defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV)
21 #include <linux/module.h> /* early to avoid printf->printk mapping */
23 #include "afs/sysincludes.h"
24 #include "afsincludes.h"
25 #include "nfsclient.h"
26 #include "h/smp_lock.h"
27 #include <linux/sunrpc/svc.h>
28 #include <linux/sunrpc/svcauth.h>
30 static unsigned long authtab_addr = 0;
31 #if defined(module_param) && LINUX_VERSION_CODE > KERNEL_VERSION(2,6,9)
32 module_param(authtab_addr, long, 0);
34 MODULE_PARM(authtab_addr, "l");
36 MODULE_PARM_DESC(authtab_addr, "Address of the authtab array.");
38 extern struct auth_ops *authtab[] __attribute__((weak));
39 static struct auth_ops **afs_authtab;
40 static struct auth_ops *afs_new_authtab[RPC_AUTH_MAXFLAVOR];
41 static struct auth_ops *afs_orig_authtab[RPC_AUTH_MAXFLAVOR];
43 static int whine_memory = 0;
45 afs_lock_t afs_xnfssrv;
47 struct nfs_server_thread {
48 struct nfs_server_thread *next; /* next in chain */
49 pid_t pid; /* pid of this thread */
50 int active; /* this thread is servicing an RPC */
51 struct sockaddr_in client_addr; /* latest client of this thread */
53 afs_int32 uid; /* AFS UID/PAG for this thread */
54 afs_int32 code; /* What should InitReq return? */
55 int flavor; /* auth flavor */
56 uid_t client_uid; /* UID claimed by client */
57 gid_t client_gid; /* GID claimed by client */
58 gid_t client_g0, client_g1; /* groups claimed by client */
61 static struct nfs_server_thread *nfssrv_list = 0;
63 static struct nfs_server_thread *find_nfs_thread(int create)
65 struct nfs_server_thread *this;
67 /* Check that this is an nfsd kernel thread */
68 if (current->files != init_task.files || strcmp(current->comm, "nfsd"))
71 ObtainWriteLock(&afs_xnfssrv, 804);
72 for (this = nfssrv_list; this; this = this->next)
73 if (this->pid == current->pid)
75 if (!this && create) {
76 this = afs_osi_Alloc(sizeof(struct nfs_server_thread));
78 this->next = nfssrv_list;
79 this->pid = current->pid;
80 this->client_addrlen = 0;
82 printk("afs: added nfsd task %d/%d\n",
83 current->tgid, current->pid);
84 } else if (!whine_memory) {
86 printk("afs: failed to allocate memory for nfsd task %d/%d\n",
87 current->tgid, current->pid);
90 ReleaseWriteLock(&afs_xnfssrv);
95 svcauth_afs_accept(struct svc_rqst *rqstp, u32 *authp)
97 struct nfs_server_thread *ns;
98 struct afs_exporter *outexp;
99 struct AFS_UCRED *credp;
100 struct sockaddr_in *addr;
103 code = afs_orig_authtab[rqstp->rq_authop->flavour]->accept(rqstp, authp);
108 ns = find_nfs_thread(1);
111 /* XXX maybe we should fail this with rpc_system_err? */
115 addr = svc_addr_in(rqstp);
117 addr = &rqstp->rq_addr;
121 ns->flavor = rqstp->rq_authop->flavour;
123 ns->client_addr = *addr;
124 ns->client_addrlen = rqstp->rq_addrlen;
125 ns->client_uid = rqstp->rq_cred.cr_uid;
126 ns->client_gid = rqstp->rq_cred.cr_gid;
127 if (rqstp->rq_cred.cr_group_info->ngroups > 0)
128 ns->client_g0 = GROUP_AT(rqstp->rq_cred.cr_group_info, 0);
131 if (rqstp->rq_cred.cr_group_info->ngroups > 1)
132 ns->client_g1 = GROUP_AT(rqstp->rq_cred.cr_group_info, 1);
136 if (addr->sin_family != AF_INET) {
137 printk("afs: NFS request from non-IPv4 client (family %d len %d)\n",
138 addr->sin_family, rqstp->rq_addrlen);
143 credp->cr_uid = rqstp->rq_cred.cr_uid;
144 credp->cr_gid = rqstp->rq_cred.cr_gid;
145 get_group_info(rqstp->rq_cred.cr_group_info);
146 credp->cr_group_info = rqstp->rq_cred.cr_group_info;
148 /* avoid creating wildcard entries by mapping anonymous
149 * clients to afs_nobody */
150 if (credp->cr_uid == -1)
152 code = afs_nfsclient_reqhandler(0, &credp, addr->sin_addr.s_addr,
154 if (!code && outexp) EXP_RELE(outexp);
155 if (!code) ns->code = 0;
157 printk("afs: svcauth_afs_accept: afs_nfsclient_reqhandler: %d\n", code);
167 /* This doesn't work, because they helpfully NULL out rqstp->authop
168 * before calling us, so we have no way to tell what the original
172 svcauth_afs_release(struct svc_rqst *rqstp)
174 struct nfs_server_thread *ns;
177 ns = find_nfs_thread(0);
178 if (ns) ns->active = 0;
181 return afs_orig_authtab[rqstp->rq_authop->flavour]->release(rqstp);
186 int osi_linux_nfs_initreq(struct vrequest *av, struct AFS_UCRED *cr, int *code)
188 struct nfs_server_thread *ns;
190 ns = find_nfs_thread(0);
191 if (!ns || !ns->active)
196 cr->cr_ruid = NFSXLATOR_CRED;
202 void osi_linux_nfssrv_init(void)
207 RWLOCK_INIT(&afs_xnfssrv, "afs_xnfssrv");
209 if (authtab) afs_authtab = authtab;
210 else if (authtab_addr) afs_authtab = (struct auth_ops **)authtab_addr;
212 printk("Warning: Unable to find the address of authtab\n");
213 printk("NFS Translator hooks will not be installed\n");
214 printk("To correct, specify authtab_addr=<authtab>\n");
219 for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
220 afs_orig_authtab[i] = afs_authtab[i];
221 if (!afs_orig_authtab[i] || afs_orig_authtab[i]->flavour != i ||
222 !try_module_get(afs_orig_authtab[i]->owner)) {
223 afs_orig_authtab[i] = 0;
227 afs_new_authtab[i] = afs_osi_Alloc(sizeof(struct auth_ops));
228 *(afs_new_authtab[i]) = *(afs_orig_authtab[i]);
229 afs_new_authtab[i]->owner = THIS_MODULE;
230 afs_new_authtab[i]->accept = svcauth_afs_accept;
231 /* afs_new_authtab[i]->release = svcauth_afs_release; */
232 svc_auth_unregister(i);
233 svc_auth_register(i, afs_new_authtab[i]);
237 void osi_linux_nfssrv_shutdown(void)
239 struct nfs_server_thread *next;
243 for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
244 if (!afs_orig_authtab[i])
246 svc_auth_unregister(i);
247 svc_auth_register(i, afs_orig_authtab[i]);
248 module_put(afs_orig_authtab[i]->owner);
249 afs_osi_Free(afs_new_authtab[i], sizeof(struct auth_ops));
254 ObtainWriteLock(&afs_xnfssrv, 805);
255 while (nfssrv_list) {
256 next = nfssrv_list->next;
257 afs_osi_Free(nfssrv_list, sizeof(struct nfs_server_thread));
260 ReleaseWriteLock(&afs_xnfssrv);
263 #endif /* AFS_NONFSTRANS */