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 #include <linux/module.h> /* early to avoid printf->printk mapping */
21 #include "afs/sysincludes.h"
22 #include "afsincludes.h"
23 #include "nfsclient.h"
24 #include "h/smp_lock.h"
25 #include <linux/sunrpc/svc.h>
26 #include <linux/sunrpc/svcauth.h>
28 static unsigned long authtab_addr = 0;
29 #if defined(module_param)
30 module_param(authtab_addr, long, 0);
32 MODULE_PARM(authtab_addr, "l");
34 MODULE_PARM_DESC(authtab_addr, "Address of the authtab array.");
36 extern struct auth_ops *authtab[] __attribute__((weak));
37 static struct auth_ops **afs_authtab;
38 static struct auth_ops *afs_new_authtab[RPC_AUTH_MAXFLAVOR];
39 static struct auth_ops *afs_orig_authtab[RPC_AUTH_MAXFLAVOR];
41 static int whine_memory = 0;
43 afs_lock_t afs_xnfssrv;
45 struct nfs_server_thread {
46 struct nfs_server_thread *next; /* next in chain */
47 pid_t pid; /* pid of this thread */
48 int active; /* this thread is servicing an RPC */
49 struct sockaddr_in client_addr; /* latest client of this thread */
51 afs_int32 uid; /* AFS UID/PAG for this thread */
52 afs_int32 code; /* What should InitReq return? */
53 int flavor; /* auth flavor */
54 uid_t client_uid; /* UID claimed by client */
55 gid_t client_gid; /* GID claimed by client */
56 gid_t client_g0, client_g1; /* groups claimed by client */
59 static struct nfs_server_thread *nfssrv_list = 0;
61 static struct nfs_server_thread *find_nfs_thread(int create)
63 struct nfs_server_thread *this;
65 /* Check that this is an nfsd kernel thread */
66 if (current->files != init_task.files || strcmp(current->comm, "nfsd"))
69 ObtainWriteLock(&afs_xnfssrv, 804);
70 for (this = nfssrv_list; this; this = this->next)
71 if (this->pid == current->pid)
73 if (!this && create) {
74 this = afs_osi_Alloc(sizeof(struct nfs_server_thread));
76 this->next = nfssrv_list;
77 this->pid = current->pid;
78 this->client_addrlen = 0;
80 printk("afs: added nfsd task %d/%d\n",
81 current->tgid, current->pid);
82 } else if (!whine_memory) {
84 printk("afs: failed to allocate memory for nfsd task %d/%d\n",
85 current->tgid, current->pid);
88 ReleaseWriteLock(&afs_xnfssrv);
93 svcauth_afs_accept(struct svc_rqst *rqstp, u32 *authp)
95 struct nfs_server_thread *ns;
96 struct afs_exporter *outexp;
97 struct AFS_UCRED *credp;
100 code = afs_orig_authtab[rqstp->rq_authop->flavour]->accept(rqstp, authp);
105 ns = find_nfs_thread(1);
108 /* XXX maybe we should fail this with rpc_system_err? */
113 ns->flavor = rqstp->rq_authop->flavour;
115 ns->client_addr = rqstp->rq_addr;
116 ns->client_addrlen = rqstp->rq_addrlen;
117 ns->client_uid = rqstp->rq_cred.cr_uid;
118 ns->client_gid = rqstp->rq_cred.cr_gid;
119 if (rqstp->rq_cred.cr_group_info->ngroups > 0)
120 ns->client_g0 = GROUP_AT(rqstp->rq_cred.cr_group_info, 0);
123 if (rqstp->rq_cred.cr_group_info->ngroups > 1)
124 ns->client_g1 = GROUP_AT(rqstp->rq_cred.cr_group_info, 1);
128 /* NB: Don't check the length; it's not always filled in! */
129 if (rqstp->rq_addr.sin_family != AF_INET) {
130 printk("afs: NFS request from non-IPv4 client (family %d len %d)\n",
131 rqstp->rq_addr.sin_family, rqstp->rq_addrlen);
136 credp->cr_uid = rqstp->rq_cred.cr_uid;
137 credp->cr_gid = rqstp->rq_cred.cr_gid;
138 get_group_info(rqstp->rq_cred.cr_group_info);
139 credp->cr_group_info = rqstp->rq_cred.cr_group_info;
141 /* avoid creating wildcard entries by mapping anonymous
142 * clients to afs_nobody */
143 if (credp->cr_uid == -1)
145 code = afs_nfsclient_reqhandler(0, &credp, rqstp->rq_addr.sin_addr.s_addr,
147 if (!code && outexp) EXP_RELE(outexp);
148 if (!code) ns->code = 0;
150 printk("afs: svcauth_afs_accept: afs_nfsclient_reqhandler: %d\n", code);
160 /* This doesn't work, because they helpfully NULL out rqstp->authop
161 * before calling us, so we have no way to tell what the original
165 svcauth_afs_release(struct svc_rqst *rqstp)
167 struct nfs_server_thread *ns;
170 ns = find_nfs_thread(0);
171 if (ns) ns->active = 0;
174 return afs_orig_authtab[rqstp->rq_authop->flavour]->release(rqstp);
179 int osi_linux_nfs_initreq(struct vrequest *av, struct AFS_UCRED *cr, int *code)
181 struct nfs_server_thread *ns;
183 ns = find_nfs_thread(0);
184 if (!ns || !ns->active)
189 cr->cr_ruid = NFSXLATOR_CRED;
195 void osi_linux_nfssrv_init(void)
200 RWLOCK_INIT(&afs_xnfssrv, "afs_xnfssrv");
202 if (authtab) afs_authtab = authtab;
203 else if (authtab_addr) afs_authtab = (struct auth_ops **)authtab_addr;
205 printk("Warning: Unable to find the address of authtab\n");
206 printk("NFS Translator hooks will not be installed\n");
207 printk("To correct, specify authtab_addr=<authtab>\n");
212 for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
213 afs_orig_authtab[i] = afs_authtab[i];
214 if (!afs_orig_authtab[i] || afs_orig_authtab[i]->flavour != i ||
215 !try_module_get(afs_orig_authtab[i]->owner)) {
216 afs_orig_authtab[i] = 0;
220 afs_new_authtab[i] = afs_osi_Alloc(sizeof(struct auth_ops));
221 *(afs_new_authtab[i]) = *(afs_orig_authtab[i]);
222 afs_new_authtab[i]->owner = THIS_MODULE;
223 afs_new_authtab[i]->accept = svcauth_afs_accept;
224 /* afs_new_authtab[i]->release = svcauth_afs_release; */
225 svc_auth_unregister(i);
226 svc_auth_register(i, afs_new_authtab[i]);
230 void osi_linux_nfssrv_shutdown(void)
232 struct nfs_server_thread *next;
236 for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
237 if (!afs_orig_authtab[i])
239 svc_auth_unregister(i);
240 svc_auth_register(i, afs_orig_authtab[i]);
241 module_put(afs_orig_authtab[i]->owner);
242 afs_osi_Free(afs_new_authtab[i], sizeof(struct auth_ops));
247 ObtainWriteLock(&afs_xnfssrv, 805);
248 while (nfssrv_list) {
249 next = nfssrv_list->next;
250 afs_osi_Free(nfssrv_list, sizeof(struct nfs_server_thread));
253 ReleaseWriteLock(&afs_xnfssrv);