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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/sunrpc/svc.h> |
| 13 | #include <linux/sunrpc/svcsock.h> |
| 14 | #include <linux/nfs_fs.h> |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 15 | #include <linux/mutex.h> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 16 | #include <linux/freezer.h> |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 17 | #include <linux/kthread.h> |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 18 | #include <linux/sunrpc/svcauth_gss.h> |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 19 | #if defined(CONFIG_NFS_V4_1) |
| 20 | #include <linux/sunrpc/bc_xprt.h> |
| 21 | #endif |
Arnaldo Carvalho de Melo | 14c8502 | 2005-12-27 02:43:12 -0200 | [diff] [blame] | 22 | |
| 23 | #include <net/inet_sock.h> |
| 24 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 25 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "callback.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 27 | #include "internal.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
| 29 | #define NFSDBG_FACILITY NFSDBG_CALLBACK |
| 30 | |
| 31 | struct nfs_callback_data { |
| 32 | unsigned int users; |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 33 | struct svc_serv *serv; |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 34 | struct svc_rqst *rqst; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 35 | struct task_struct *task; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | }; |
| 37 | |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 38 | static struct nfs_callback_data nfs_callback_info[NFS4_MAX_MINOR_VERSION + 1]; |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 39 | static DEFINE_MUTEX(nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | static struct svc_program nfs4_callback_program; |
| 41 | |
Trond Myklebust | a72b442 | 2006-01-03 09:55:41 +0100 | [diff] [blame] | 42 | unsigned int nfs_callback_set_tcpport; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | unsigned short nfs_callback_tcpport; |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 44 | unsigned short nfs_callback_tcpport6; |
Trond Myklebust | c140aa9 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 45 | #define NFS_CALLBACK_MAXPORTNR (65535U) |
David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 46 | |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 47 | static int param_set_portnr(const char *val, const struct kernel_param *kp) |
David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 48 | { |
Trond Myklebust | c140aa9 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 49 | unsigned long num; |
| 50 | int ret; |
| 51 | |
| 52 | if (!val) |
David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 53 | return -EINVAL; |
Trond Myklebust | c140aa9 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 54 | ret = strict_strtoul(val, 0, &num); |
| 55 | if (ret == -EINVAL || num > NFS_CALLBACK_MAXPORTNR) |
| 56 | return -EINVAL; |
| 57 | *((unsigned int *)kp->arg) = num; |
David Howells | 7d4e274 | 2006-08-22 20:06:07 -0400 | [diff] [blame] | 58 | return 0; |
| 59 | } |
Rusty Russell | 9bbb9e5 | 2010-08-11 23:04:12 -0600 | [diff] [blame] | 60 | static struct kernel_param_ops param_ops_portnr = { |
| 61 | .set = param_set_portnr, |
| 62 | .get = param_get_uint, |
| 63 | }; |
Trond Myklebust | c140aa9 | 2009-08-09 15:06:19 -0400 | [diff] [blame] | 64 | #define param_check_portnr(name, p) __param_check(name, p, unsigned int); |
| 65 | |
| 66 | module_param_named(callback_tcpport, nfs_callback_set_tcpport, portnr, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
| 68 | /* |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 69 | * This is the NFSv4 callback kernel thread. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | */ |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 71 | static int |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 72 | nfs4_callback_svc(void *vrqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
Jeff Layton | 06e02d6 | 2008-04-08 15:40:07 -0400 | [diff] [blame] | 74 | int err, preverr = 0; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 75 | struct svc_rqst *rqstp = vrqstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 77 | set_freezable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 79 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /* |
| 81 | * Listen for a request on the socket |
| 82 | */ |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 83 | err = svc_recv(rqstp, MAX_SCHEDULE_TIMEOUT); |
Jeff Layton | 06e02d6 | 2008-04-08 15:40:07 -0400 | [diff] [blame] | 84 | if (err == -EAGAIN || err == -EINTR) { |
| 85 | preverr = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | } |
Jeff Layton | 06e02d6 | 2008-04-08 15:40:07 -0400 | [diff] [blame] | 88 | if (err < 0) { |
| 89 | if (err != preverr) { |
| 90 | printk(KERN_WARNING "%s: unexpected error " |
| 91 | "from svc_recv (%d)\n", __func__, err); |
| 92 | preverr = err; |
| 93 | } |
| 94 | schedule_timeout_uninterruptible(HZ); |
| 95 | continue; |
| 96 | } |
| 97 | preverr = err; |
NeilBrown | 6fb2b47 | 2006-10-02 02:17:50 -0700 | [diff] [blame] | 98 | svc_process(rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | } |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 100 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | /* |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 104 | * Prepare to bring up the NFSv4 callback service |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | */ |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 106 | struct svc_rqst * |
| 107 | nfs4_callback_up(struct svc_serv *serv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 109 | int ret; |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 110 | |
Pavel Emelyanov | fc5d00b | 2010-09-29 16:03:50 +0400 | [diff] [blame] | 111 | ret = svc_create_xprt(serv, "tcp", &init_net, PF_INET, |
Chuck Lever | 9652ada | 2009-03-18 20:46:21 -0400 | [diff] [blame] | 112 | nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS); |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 113 | if (ret <= 0) |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 114 | goto out_err; |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 115 | nfs_callback_tcpport = ret; |
Chuck Lever | 18de973 | 2008-10-16 17:41:11 -0400 | [diff] [blame] | 116 | dprintk("NFS: Callback listener port = %u (af %u)\n", |
Chuck Lever | 26298ca | 2009-03-18 20:46:36 -0400 | [diff] [blame] | 117 | nfs_callback_tcpport, PF_INET); |
Chuck Lever | 482fb94 | 2007-02-12 00:53:29 -0800 | [diff] [blame] | 118 | |
Pavel Emelyanov | fc5d00b | 2010-09-29 16:03:50 +0400 | [diff] [blame] | 119 | ret = svc_create_xprt(serv, "tcp", &init_net, PF_INET6, |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 120 | nfs_callback_set_tcpport, SVC_SOCK_ANONYMOUS); |
| 121 | if (ret > 0) { |
| 122 | nfs_callback_tcpport6 = ret; |
| 123 | dprintk("NFS: Callback listener port = %u (af %u)\n", |
| 124 | nfs_callback_tcpport6, PF_INET6); |
Chuck Lever | 18fc316 | 2009-06-17 18:02:10 -0700 | [diff] [blame] | 125 | } else if (ret == -EAFNOSUPPORT) |
| 126 | ret = 0; |
| 127 | else |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 128 | goto out_err; |
Chuck Lever | f738f51 | 2009-03-18 20:48:06 -0400 | [diff] [blame] | 129 | |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 130 | return svc_prepare_thread(serv, &serv->sv_pools[0]); |
| 131 | |
| 132 | out_err: |
| 133 | if (ret == 0) |
| 134 | ret = -ENOMEM; |
| 135 | return ERR_PTR(ret); |
| 136 | } |
| 137 | |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 138 | #if defined(CONFIG_NFS_V4_1) |
| 139 | /* |
| 140 | * The callback service for NFSv4.1 callbacks |
| 141 | */ |
| 142 | static int |
| 143 | nfs41_callback_svc(void *vrqstp) |
| 144 | { |
| 145 | struct svc_rqst *rqstp = vrqstp; |
| 146 | struct svc_serv *serv = rqstp->rq_server; |
| 147 | struct rpc_rqst *req; |
| 148 | int error; |
| 149 | DEFINE_WAIT(wq); |
| 150 | |
| 151 | set_freezable(); |
| 152 | |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 153 | while (!kthread_should_stop()) { |
| 154 | prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE); |
| 155 | spin_lock_bh(&serv->sv_cb_lock); |
| 156 | if (!list_empty(&serv->sv_cb_list)) { |
| 157 | req = list_first_entry(&serv->sv_cb_list, |
| 158 | struct rpc_rqst, rq_bc_list); |
| 159 | list_del(&req->rq_bc_list); |
| 160 | spin_unlock_bh(&serv->sv_cb_lock); |
| 161 | dprintk("Invoking bc_svc_process()\n"); |
| 162 | error = bc_svc_process(serv, req, rqstp); |
| 163 | dprintk("bc_svc_process() returned w/ error code= %d\n", |
| 164 | error); |
| 165 | } else { |
| 166 | spin_unlock_bh(&serv->sv_cb_lock); |
| 167 | schedule(); |
| 168 | } |
| 169 | finish_wait(&serv->sv_cb_waitq, &wq); |
| 170 | } |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | /* |
| 175 | * Bring up the NFSv4.1 callback service |
| 176 | */ |
| 177 | struct svc_rqst * |
| 178 | nfs41_callback_up(struct svc_serv *serv, struct rpc_xprt *xprt) |
| 179 | { |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame^] | 180 | struct svc_rqst *rqstp; |
| 181 | int ret; |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 182 | |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame^] | 183 | /* |
| 184 | * Create an svc_sock for the back channel service that shares the |
| 185 | * fore channel connection. |
| 186 | * Returns the input port (0) and sets the svc_serv bc_xprt on success |
| 187 | */ |
| 188 | ret = svc_create_xprt(serv, "tcp-bc", &init_net, PF_INET, 0, |
| 189 | SVC_SOCK_ANONYMOUS); |
| 190 | if (ret < 0) { |
| 191 | rqstp = ERR_PTR(ret); |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 192 | goto out; |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame^] | 193 | } |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 194 | |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 195 | /* |
| 196 | * Save the svc_serv in the transport so that it can |
| 197 | * be referenced when the session backchannel is initialized |
| 198 | */ |
| 199 | xprt->bc_serv = serv; |
| 200 | |
| 201 | INIT_LIST_HEAD(&serv->sv_cb_list); |
| 202 | spin_lock_init(&serv->sv_cb_lock); |
| 203 | init_waitqueue_head(&serv->sv_cb_waitq); |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 204 | rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]); |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame^] | 205 | if (IS_ERR(rqstp)) { |
| 206 | svc_xprt_put(serv->bc_xprt); |
| 207 | serv->bc_xprt = NULL; |
| 208 | } |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 209 | out: |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame^] | 210 | dprintk("--> %s return %ld\n", __func__, |
| 211 | IS_ERR(rqstp) ? PTR_ERR(rqstp) : 0); |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 212 | return rqstp; |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static inline int nfs_minorversion_callback_svc_setup(u32 minorversion, |
| 216 | struct svc_serv *serv, struct rpc_xprt *xprt, |
| 217 | struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp)) |
| 218 | { |
| 219 | if (minorversion) { |
| 220 | *rqstpp = nfs41_callback_up(serv, xprt); |
| 221 | *callback_svc = nfs41_callback_svc; |
| 222 | } |
| 223 | return minorversion; |
| 224 | } |
| 225 | |
| 226 | static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt, |
| 227 | struct nfs_callback_data *cb_info) |
| 228 | { |
| 229 | if (minorversion) |
| 230 | xprt->bc_serv = cb_info->serv; |
| 231 | } |
| 232 | #else |
| 233 | static inline int nfs_minorversion_callback_svc_setup(u32 minorversion, |
| 234 | struct svc_serv *serv, struct rpc_xprt *xprt, |
| 235 | struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp)) |
| 236 | { |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt, |
| 241 | struct nfs_callback_data *cb_info) |
| 242 | { |
| 243 | } |
| 244 | #endif /* CONFIG_NFS_V4_1 */ |
| 245 | |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 246 | /* |
| 247 | * Bring up the callback thread if it is not already up. |
| 248 | */ |
| 249 | int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt) |
| 250 | { |
| 251 | struct svc_serv *serv = NULL; |
| 252 | struct svc_rqst *rqstp; |
| 253 | int (*callback_svc)(void *vrqstp); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 254 | struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion]; |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 255 | char svc_name[12]; |
| 256 | int ret = 0; |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 257 | int minorversion_setup; |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 258 | |
| 259 | mutex_lock(&nfs_callback_mutex); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 260 | if (cb_info->users++ || cb_info->task != NULL) { |
| 261 | nfs_callback_bc_serv(minorversion, xprt, cb_info); |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 262 | goto out; |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 263 | } |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 264 | serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL); |
| 265 | if (!serv) { |
| 266 | ret = -ENOMEM; |
| 267 | goto out_err; |
| 268 | } |
| 269 | |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 270 | minorversion_setup = nfs_minorversion_callback_svc_setup(minorversion, |
| 271 | serv, xprt, &rqstp, &callback_svc); |
| 272 | if (!minorversion_setup) { |
| 273 | /* v4.0 callback setup */ |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 274 | rqstp = nfs4_callback_up(serv); |
| 275 | callback_svc = nfs4_callback_svc; |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | if (IS_ERR(rqstp)) { |
| 279 | ret = PTR_ERR(rqstp); |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 280 | goto out_err; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | svc_sock_update_bufs(serv); |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 284 | |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 285 | sprintf(svc_name, "nfsv4.%u-svc", minorversion); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 286 | cb_info->serv = serv; |
| 287 | cb_info->rqst = rqstp; |
| 288 | cb_info->task = kthread_run(callback_svc, cb_info->rqst, svc_name); |
| 289 | if (IS_ERR(cb_info->task)) { |
| 290 | ret = PTR_ERR(cb_info->task); |
| 291 | svc_exit_thread(cb_info->rqst); |
| 292 | cb_info->rqst = NULL; |
| 293 | cb_info->task = NULL; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 294 | goto out_err; |
| 295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | out: |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 297 | /* |
| 298 | * svc_create creates the svc_serv with sv_nrthreads == 1, and then |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 299 | * svc_prepare_thread increments that. So we need to call svc_destroy |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 300 | * on both success and failure so that the refcount is 1 when the |
| 301 | * thread exits. |
| 302 | */ |
| 303 | if (serv) |
| 304 | svc_destroy(serv); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 305 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | return ret; |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 307 | out_err: |
Chuck Lever | 18de973 | 2008-10-16 17:41:11 -0400 | [diff] [blame] | 308 | dprintk("NFS: Couldn't create callback socket or server thread; " |
| 309 | "err = %d\n", ret); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 310 | cb_info->users--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | goto out; |
| 312 | } |
| 313 | |
| 314 | /* |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 315 | * Kill the callback thread if it's no longer being used. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | */ |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 317 | void nfs_callback_down(int minorversion) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | { |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 319 | struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion]; |
| 320 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 321 | mutex_lock(&nfs_callback_mutex); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 322 | cb_info->users--; |
| 323 | if (cb_info->users == 0 && cb_info->task != NULL) { |
| 324 | kthread_stop(cb_info->task); |
| 325 | svc_exit_thread(cb_info->rqst); |
| 326 | cb_info->serv = NULL; |
| 327 | cb_info->rqst = NULL; |
| 328 | cb_info->task = NULL; |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 329 | } |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 330 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | } |
| 332 | |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 333 | static int check_gss_callback_principal(struct nfs_client *clp, |
| 334 | struct svc_rqst *rqstp) |
| 335 | { |
| 336 | struct rpc_clnt *r = clp->cl_rpcclient; |
| 337 | char *p = svc_gss_principal(rqstp); |
| 338 | |
| 339 | /* |
| 340 | * It might just be a normal user principal, in which case |
| 341 | * userspace won't bother to tell us the name at all. |
| 342 | */ |
| 343 | if (p == NULL) |
| 344 | return SVC_DENIED; |
| 345 | |
| 346 | /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */ |
| 347 | |
| 348 | if (memcmp(p, "nfs@", 4) != 0) |
| 349 | return SVC_DENIED; |
| 350 | p += 4; |
| 351 | if (strcmp(p, r->cl_server) != 0) |
| 352 | return SVC_DENIED; |
| 353 | return SVC_OK; |
| 354 | } |
| 355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | static int nfs_callback_authenticate(struct svc_rqst *rqstp) |
| 357 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 358 | struct nfs_client *clp; |
Pavel Emelyanov | 5216a8e | 2008-02-21 10:57:45 +0300 | [diff] [blame] | 359 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 360 | int ret = SVC_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | /* Don't talk to strangers */ |
Chuck Lever | ff05264 | 2007-12-10 14:58:44 -0500 | [diff] [blame] | 363 | clp = nfs_find_client(svc_addr(rqstp), 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | if (clp == NULL) |
| 365 | return SVC_DROP; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 366 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 367 | dprintk("%s: %s NFSv4 callback!\n", __func__, |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 368 | svc_print_addr(rqstp, buf, sizeof(buf))); |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | switch (rqstp->rq_authop->flavour) { |
| 371 | case RPC_AUTH_NULL: |
| 372 | if (rqstp->rq_proc != CB_NULL) |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 373 | ret = SVC_DENIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | break; |
| 375 | case RPC_AUTH_UNIX: |
| 376 | break; |
| 377 | case RPC_AUTH_GSS: |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 378 | ret = check_gss_callback_principal(clp, rqstp); |
| 379 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | default: |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 381 | ret = SVC_DENIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 383 | nfs_put_client(clp); |
| 384 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | /* |
| 388 | * Define NFS4 callback program |
| 389 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | static struct svc_version *nfs4_callback_version[] = { |
| 391 | [1] = &nfs4_callback_version1, |
Alexandros Batsakis | 07bccc2 | 2009-12-05 13:19:01 -0500 | [diff] [blame] | 392 | [4] = &nfs4_callback_version4, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | }; |
| 394 | |
| 395 | static struct svc_stat nfs4_callback_stats; |
| 396 | |
| 397 | static struct svc_program nfs4_callback_program = { |
| 398 | .pg_prog = NFS4_CALLBACK, /* RPC service number */ |
| 399 | .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */ |
| 400 | .pg_vers = nfs4_callback_version, /* version table */ |
| 401 | .pg_name = "NFSv4 callback", /* service name */ |
| 402 | .pg_class = "nfs", /* authentication class */ |
| 403 | .pg_stats = &nfs4_callback_stats, |
| 404 | .pg_authenticate = nfs_callback_authenticate, |
| 405 | }; |