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