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"
18 #if !defined(AFS_NONFSTRANS) || defined(AFS_AIX_IAUTH_ENV)
19 #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 <linux/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) && LINUX_VERSION_CODE > KERNEL_VERSION(2,6,9)
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;
98 struct sockaddr_in *addr;
101 code = afs_orig_authtab[rqstp->rq_authop->flavour]->accept(rqstp, authp);
106 ns = find_nfs_thread(1);
109 /* XXX maybe we should fail this with rpc_system_err? */
113 addr = svc_addr_in(rqstp);
115 addr = &rqstp->rq_addr;
119 ns->flavor = rqstp->rq_authop->flavour;
121 ns->client_addr = *addr;
122 ns->client_addrlen = rqstp->rq_addrlen;
123 ns->client_uid = rqstp->rq_cred.cr_uid;
124 ns->client_gid = rqstp->rq_cred.cr_gid;
125 if (rqstp->rq_cred.cr_group_info->ngroups > 0)
126 ns->client_g0 = GROUP_AT(rqstp->rq_cred.cr_group_info, 0);
129 if (rqstp->rq_cred.cr_group_info->ngroups > 1)
130 ns->client_g1 = GROUP_AT(rqstp->rq_cred.cr_group_info, 1);
134 if (addr->sin_family != AF_INET) {
135 printk("afs: NFS request from non-IPv4 client (family %d len %d)\n",
136 addr->sin_family, rqstp->rq_addrlen);
141 credp->cr_uid = rqstp->rq_cred.cr_uid;
142 credp->cr_gid = rqstp->rq_cred.cr_gid;
143 get_group_info(rqstp->rq_cred.cr_group_info);
144 credp->cr_group_info = rqstp->rq_cred.cr_group_info;
146 /* avoid creating wildcard entries by mapping anonymous
147 * clients to afs_nobody */
148 if (credp->cr_uid == -1)
150 code = afs_nfsclient_reqhandler(0, &credp, addr->sin_addr.s_addr,
152 if (!code && outexp) EXP_RELE(outexp);
153 if (!code) ns->code = 0;
155 printk("afs: svcauth_afs_accept: afs_nfsclient_reqhandler: %d\n", code);
165 /* This doesn't work, because they helpfully NULL out rqstp->authop
166 * before calling us, so we have no way to tell what the original
170 svcauth_afs_release(struct svc_rqst *rqstp)
172 struct nfs_server_thread *ns;
175 ns = find_nfs_thread(0);
176 if (ns) ns->active = 0;
179 return afs_orig_authtab[rqstp->rq_authop->flavour]->release(rqstp);
184 int osi_linux_nfs_initreq(struct vrequest *av, AFS_UCRED *cr, int *code)
186 struct nfs_server_thread *ns;
188 ns = find_nfs_thread(0);
189 if (!ns || !ns->active)
194 cr->cr_ruid = NFSXLATOR_CRED;
200 void osi_linux_nfssrv_init(void)
205 AFS_RWLOCK_INIT(&afs_xnfssrv, "afs_xnfssrv");
207 if (authtab && !IS_ERR(authtab))
208 afs_authtab = authtab;
209 else if (authtab_addr) afs_authtab = (struct auth_ops **)authtab_addr;
211 printk("Warning: Unable to find the address of authtab\n");
212 printk("NFS Translator hooks will not be installed\n");
213 printk("To correct, specify authtab_addr=<authtab>\n");
218 for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
219 afs_orig_authtab[i] = afs_authtab[i];
220 if (!afs_orig_authtab[i] || afs_orig_authtab[i]->flavour != i ||
221 !try_module_get(afs_orig_authtab[i]->owner)) {
222 afs_orig_authtab[i] = 0;
226 afs_new_authtab[i] = afs_osi_Alloc(sizeof(struct auth_ops));
227 *(afs_new_authtab[i]) = *(afs_orig_authtab[i]);
228 afs_new_authtab[i]->owner = THIS_MODULE;
229 afs_new_authtab[i]->accept = svcauth_afs_accept;
230 /* afs_new_authtab[i]->release = svcauth_afs_release; */
231 svc_auth_unregister(i);
232 svc_auth_register(i, afs_new_authtab[i]);
236 void osi_linux_nfssrv_shutdown(void)
238 struct nfs_server_thread *next;
242 for (i = 0; i < RPC_AUTH_MAXFLAVOR; i++) {
243 if (!afs_orig_authtab[i])
245 svc_auth_unregister(i);
246 svc_auth_register(i, afs_orig_authtab[i]);
247 module_put(afs_orig_authtab[i]->owner);
248 afs_osi_Free(afs_new_authtab[i], sizeof(struct auth_ops));
253 ObtainWriteLock(&afs_xnfssrv, 805);
254 while (nfssrv_list) {
255 next = nfssrv_list->next;
256 afs_osi_Free(nfssrv_list, sizeof(struct nfs_server_thread));
259 ReleaseWriteLock(&afs_xnfssrv);
262 #endif /* AFS_NONFSTRANS */