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 | { |
| 61 | struct svc_serv *serv = rqstp->rq_server; |
| 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); |
| 71 | |
| 72 | complete(&nfs_callback_info.started); |
| 73 | |
Trond Myklebust | 1dd761e | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 74 | for(;;) { |
| 75 | if (signalled()) { |
| 76 | if (nfs_callback_info.users == 0) |
| 77 | break; |
| 78 | flush_signals(current); |
| 79 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Listen for a request on the socket |
| 82 | */ |
| 83 | err = svc_recv(serv, rqstp, MAX_SCHEDULE_TIMEOUT); |
| 84 | if (err == -EAGAIN || err == -EINTR) |
| 85 | continue; |
| 86 | if (err < 0) { |
| 87 | printk(KERN_WARNING |
| 88 | "%s: terminating on error %d\n", |
| 89 | __FUNCTION__, -err); |
| 90 | break; |
| 91 | } |
| 92 | dprintk("%s: request from %u.%u.%u.%u\n", __FUNCTION__, |
| 93 | NIPQUAD(rqstp->rq_addr.sin_addr.s_addr)); |
| 94 | svc_process(serv, rqstp); |
| 95 | } |
| 96 | |
Trond Myklebust | f25bc34 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 97 | svc_exit_thread(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | nfs_callback_info.pid = 0; |
| 99 | complete(&nfs_callback_info.stopped); |
| 100 | unlock_kernel(); |
| 101 | module_put_and_exit(0); |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Bring up the server process if it is not already up. |
| 106 | */ |
| 107 | int nfs_callback_up(void) |
| 108 | { |
| 109 | struct svc_serv *serv; |
| 110 | struct svc_sock *svsk; |
| 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); |
| 119 | serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE); |
| 120 | ret = -ENOMEM; |
| 121 | if (!serv) |
| 122 | goto out_err; |
| 123 | /* FIXME: We don't want to register this socket with the portmapper */ |
Trond Myklebust | a72b442 | 2006-01-03 09:55:41 +0100 | [diff] [blame] | 124 | ret = svc_makesock(serv, IPPROTO_TCP, nfs_callback_set_tcpport); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (ret < 0) |
| 126 | goto out_destroy; |
| 127 | if (!list_empty(&serv->sv_permsocks)) { |
| 128 | svsk = list_entry(serv->sv_permsocks.next, |
| 129 | struct svc_sock, sk_list); |
| 130 | nfs_callback_tcpport = ntohs(inet_sk(svsk->sk_sk)->sport); |
| 131 | dprintk ("Callback port = 0x%x\n", nfs_callback_tcpport); |
| 132 | } else |
| 133 | BUG(); |
| 134 | ret = svc_create_thread(nfs_callback_svc, serv); |
| 135 | if (ret < 0) |
| 136 | goto out_destroy; |
| 137 | nfs_callback_info.serv = serv; |
| 138 | wait_for_completion(&nfs_callback_info.started); |
| 139 | out: |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 140 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | unlock_kernel(); |
| 142 | return ret; |
| 143 | out_destroy: |
| 144 | svc_destroy(serv); |
| 145 | out_err: |
| 146 | nfs_callback_info.users--; |
| 147 | goto out; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Kill the server process if it is not already up. |
| 152 | */ |
David Howells | 5ae1fbc | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 153 | void nfs_callback_down(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | lock_kernel(); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 156 | mutex_lock(&nfs_callback_mutex); |
Trond Myklebust | 1dd761e | 2006-03-20 13:44:49 -0500 | [diff] [blame] | 157 | nfs_callback_info.users--; |
| 158 | do { |
| 159 | if (nfs_callback_info.users != 0 || nfs_callback_info.pid == 0) |
| 160 | break; |
| 161 | if (kill_proc(nfs_callback_info.pid, SIGKILL, 1) < 0) |
| 162 | break; |
| 163 | } while (wait_for_completion_timeout(&nfs_callback_info.stopped, 5*HZ) == 0); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 164 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | unlock_kernel(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static int nfs_callback_authenticate(struct svc_rqst *rqstp) |
| 169 | { |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame^] | 170 | struct sockaddr_in *addr = &rqstp->rq_addr; |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 171 | struct nfs_client *clp; |
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; |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame^] | 177 | dprintk("%s: %u.%u.%u.%u NFSv4 callback!\n", __FUNCTION__, NIPQUAD(addr->sin_addr)); |
| 178 | nfs_put_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | switch (rqstp->rq_authop->flavour) { |
| 180 | case RPC_AUTH_NULL: |
| 181 | if (rqstp->rq_proc != CB_NULL) |
| 182 | return SVC_DENIED; |
| 183 | break; |
| 184 | case RPC_AUTH_UNIX: |
| 185 | break; |
| 186 | case RPC_AUTH_GSS: |
| 187 | /* FIXME: RPCSEC_GSS handling? */ |
| 188 | default: |
| 189 | return SVC_DENIED; |
| 190 | } |
| 191 | return SVC_OK; |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Define NFS4 callback program |
| 196 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | static struct svc_version *nfs4_callback_version[] = { |
| 198 | [1] = &nfs4_callback_version1, |
| 199 | }; |
| 200 | |
| 201 | static struct svc_stat nfs4_callback_stats; |
| 202 | |
| 203 | static struct svc_program nfs4_callback_program = { |
| 204 | .pg_prog = NFS4_CALLBACK, /* RPC service number */ |
| 205 | .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */ |
| 206 | .pg_vers = nfs4_callback_version, /* version table */ |
| 207 | .pg_name = "NFSv4 callback", /* service name */ |
| 208 | .pg_class = "nfs", /* authentication class */ |
| 209 | .pg_stats = &nfs4_callback_stats, |
| 210 | .pg_authenticate = nfs_callback_authenticate, |
| 211 | }; |