Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/callback.c |
| 3 | * |
| 4 | * Copyright (C) 2004 Trond Myklebust |
| 5 | * |
| 6 | * NFSv4 callback handling |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/completion.h> |
| 10 | #include <linux/ip.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/smp_lock.h> |
| 13 | #include <linux/sunrpc/svc.h> |
| 14 | #include <linux/sunrpc/svcsock.h> |
| 15 | #include <linux/nfs_fs.h> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 16 | #include <linux/mutex.h> |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 17 | |
| 18 | #include <net/inet_sock.h> |
| 19 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 20 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "callback.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 22 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | #define NFSDBG_FACILITY NFSDBG_CALLBACK |
| 25 | |
| 26 | struct nfs_callback_data { |
| 27 | unsigned int users; |
| 28 | struct svc_serv *serv; |
| 29 | pid_t pid; |
| 30 | struct completion started; |
| 31 | struct completion stopped; |
| 32 | }; |
| 33 | |
| 34 | static struct nfs_callback_data nfs_callback_info; |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 35 | static DEFINE_MUTEX(nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | static struct svc_program nfs4_callback_program; |
| 37 | |
Trond Myklebust | a72b442 | 2006-01-03 09:55:41 +0100 | [diff] [blame] | 38 | unsigned int nfs_callback_set_tcpport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | unsigned short nfs_callback_tcpport; |
David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 40 | static const int nfs_set_port_min = 0; |
| 41 | static const int nfs_set_port_max = 65535; |
| 42 | |
| 43 | static int param_set_port(const char *val, struct kernel_param *kp) |
| 44 | { |
| 45 | char *endp; |
| 46 | int num = simple_strtol(val, &endp, 0); |
| 47 | if (endp == val || *endp || num < nfs_set_port_min || num > nfs_set_port_max) |
| 48 | return -EINVAL; |
| 49 | *((int *)kp->arg) = num; |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | module_param_call(callback_tcpport, param_set_port, param_get_int, |
| 54 | &nfs_callback_set_tcpport, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
| 57 | * This is the callback kernel thread. |
| 58 | */ |
| 59 | static void nfs_callback_svc(struct svc_rqst *rqstp) |
| 60 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | int err; |
| 62 | |
| 63 | __module_get(THIS_MODULE); |
| 64 | lock_kernel(); |
| 65 | |
| 66 | nfs_callback_info.pid = current->pid; |
| 67 | daemonize("nfsv4-svc"); |
| 68 | /* Process request with signals blocked, but allow SIGKILL. */ |
| 69 | allow_signal(SIGKILL); |
| 70 | |
| 71 | complete(&nfs_callback_info.started); |
| 72 | |
Trond Myklebust | 1dd761e | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 73 | for(;;) { |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 74 | char buf[RPC_MAX_ADDRBUFLEN]; |
| 75 | |
Trond Myklebust | 1dd761e | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 76 | if (signalled()) { |
| 77 | if (nfs_callback_info.users == 0) |
| 78 | break; |
| 79 | flush_signals(current); |
| 80 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* |
| 82 | * Listen for a request on the socket |
| 83 | */ |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 84 | err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (err == -EAGAIN || err == -EINTR) |
| 86 | continue; |
| 87 | if (err < 0) { |
| 88 | printk(KERN_WARNING |
| 89 | "%s: terminating on error %d\n", |
| 90 | __FUNCTION__, -err); |
| 91 | break; |
| 92 | } |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 93 | dprintk("%s: request from %s\n", __FUNCTION__, |
| 94 | svc_print_addr(rqstp, buf, sizeof(buf))); |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 95 | svc_process(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Trond Myklebust | f25bc34 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 98 | svc_exit_thread(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | nfs_callback_info.pid = 0; |
| 100 | complete(&nfs_callback_info.stopped); |
| 101 | unlock_kernel(); |
| 102 | module_put_and_exit(0); |
| 103 | } |
| 104 | |
| 105 | /* |
| 106 | * Bring up the server process if it is not already up. |
| 107 | */ |
| 108 | int nfs_callback_up(void) |
| 109 | { |
| 110 | struct svc_serv *serv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | int ret = 0; |
| 112 | |
| 113 | lock_kernel(); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 114 | mutex_lock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | if (nfs_callback_info.users++ || nfs_callback_info.pid != 0) |
| 116 | goto out; |
| 117 | init_completion(&nfs_callback_info.started); |
| 118 | init_completion(&nfs_callback_info.stopped); |
NeilBrown | bc591cc | 2006-10-02 02:17:44 -0700 | [diff] [blame] | 119 | serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | ret = -ENOMEM; |
| 121 | if (!serv) |
| 122 | goto out_err; |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 123 | |
| 124 | ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport, |
| 125 | SVC_SOCK_ANONYMOUS); |
| 126 | if (ret <= 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | goto out_destroy; |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 128 | nfs_callback_tcpport = ret; |
| 129 | dprintk("Callback port = 0x%x\n", nfs_callback_tcpport); |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | ret = svc_create_thread(nfs_callback_svc, serv); |
| 132 | if (ret < 0) |
| 133 | goto out_destroy; |
| 134 | nfs_callback_info.serv = serv; |
| 135 | wait_for_completion(&nfs_callback_info.started); |
| 136 | out: |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 137 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | unlock_kernel(); |
| 139 | return ret; |
| 140 | out_destroy: |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 141 | dprintk("Couldn't create callback socket or server thread; err = %d\n", |
| 142 | ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | svc_destroy(serv); |
| 144 | out_err: |
| 145 | nfs_callback_info.users--; |
| 146 | goto out; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * Kill the server process if it is not already up. |
| 151 | */ |
David Howells | 5ae1fbc | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 152 | void nfs_callback_down(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | lock_kernel(); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 155 | mutex_lock(&nfs_callback_mutex); |
Trond Myklebust | 1dd761e | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 156 | nfs_callback_info.users--; |
| 157 | do { |
| 158 | if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0) |
| 159 | break; |
| 160 | if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0) |
| 161 | break; |
| 162 | } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 163 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int nfs_callback_authenticate(struct svc_rqst *rqstp) |
| 168 | { |
Chuck Lever | 27459f0 | 2007-02-12 00:53:34 -0800 | [diff] [blame] | 169 | struct sockaddr_in *addr = svc_addr_in(rqstp); |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 170 | struct nfs_client *clp; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 171 | char buf[RPC_MAX_ADDRBUFLEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /* Don't talk to strangers */ |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 174 | clp = nfs_find_client(addr, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (clp == NULL) |
| 176 | return SVC_DROP; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 177 | |
| 178 | dprintk("%s: %s NFSv4 callback!\n", __FUNCTION__, |
| 179 | svc_print_addr(rqstp, buf, sizeof(buf))); |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 180 | nfs_put_client(clp); |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | switch (rqstp->rq_authop->flavour) { |
| 183 | case RPC_AUTH_NULL: |
| 184 | if (rqstp->rq_proc != CB_NULL) |
| 185 | return SVC_DENIED; |
| 186 | break; |
| 187 | case RPC_AUTH_UNIX: |
| 188 | break; |
| 189 | case RPC_AUTH_GSS: |
| 190 | /* FIXME: RPCSEC_GSS handling? */ |
| 191 | default: |
| 192 | return SVC_DENIED; |
| 193 | } |
| 194 | return SVC_OK; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * Define NFS4 callback program |
| 199 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | static struct svc_version *nfs4_callback_version[] = { |
| 201 | [1] = &nfs4_callback_version1, |
| 202 | }; |
| 203 | |
| 204 | static struct svc_stat nfs4_callback_stats; |
| 205 | |
| 206 | static struct svc_program nfs4_callback_program = { |
| 207 | .pg_prog = NFS4_CALLBACK, /* RPC service number */ |
| 208 | .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */ |
| 209 | .pg_vers = nfs4_callback_version, /* version table */ |
| 210 | .pg_name = "NFSv4 callback", /* service name */ |
| 211 | .pg_class = "nfs", /* authentication class */ |
| 212 | .pg_stats = &nfs4_callback_stats, |
| 213 | .pg_authenticate = nfs_callback_authenticate, |
| 214 | }; |