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 | /* |
Andy Adamson | 2c2618c | 2011-01-06 02:04:31 +0000 | [diff] [blame^] | 140 | * * CB_SEQUENCE operations will fail until the callback sessionid is set. |
| 141 | * */ |
| 142 | int nfs4_set_callback_sessionid(struct nfs_client *clp) |
| 143 | { |
| 144 | struct svc_serv *serv = clp->cl_rpcclient->cl_xprt->bc_serv; |
| 145 | struct nfs4_sessionid *bc_sid; |
| 146 | |
| 147 | if (!serv->bc_xprt) |
| 148 | return -EINVAL; |
| 149 | |
| 150 | /* on success freed in xprt_free */ |
| 151 | bc_sid = kmalloc(sizeof(struct nfs4_sessionid), GFP_KERNEL); |
| 152 | if (!bc_sid) |
| 153 | return -ENOMEM; |
| 154 | memcpy(bc_sid->data, &clp->cl_session->sess_id.data, |
| 155 | NFS4_MAX_SESSIONID_LEN); |
| 156 | spin_lock_bh(&serv->sv_cb_lock); |
| 157 | serv->bc_xprt->xpt_bc_sid = bc_sid; |
| 158 | spin_unlock_bh(&serv->sv_cb_lock); |
| 159 | dprintk("%s set xpt_bc_sid=%u:%u:%u:%u for bc_xprt %p\n", __func__, |
| 160 | ((u32 *)bc_sid->data)[0], ((u32 *)bc_sid->data)[1], |
| 161 | ((u32 *)bc_sid->data)[2], ((u32 *)bc_sid->data)[3], |
| 162 | serv->bc_xprt); |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | /* |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 167 | * The callback service for NFSv4.1 callbacks |
| 168 | */ |
| 169 | static int |
| 170 | nfs41_callback_svc(void *vrqstp) |
| 171 | { |
| 172 | struct svc_rqst *rqstp = vrqstp; |
| 173 | struct svc_serv *serv = rqstp->rq_server; |
| 174 | struct rpc_rqst *req; |
| 175 | int error; |
| 176 | DEFINE_WAIT(wq); |
| 177 | |
| 178 | set_freezable(); |
| 179 | |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 180 | while (!kthread_should_stop()) { |
| 181 | prepare_to_wait(&serv->sv_cb_waitq, &wq, TASK_INTERRUPTIBLE); |
| 182 | spin_lock_bh(&serv->sv_cb_lock); |
| 183 | if (!list_empty(&serv->sv_cb_list)) { |
| 184 | req = list_first_entry(&serv->sv_cb_list, |
| 185 | struct rpc_rqst, rq_bc_list); |
| 186 | list_del(&req->rq_bc_list); |
| 187 | spin_unlock_bh(&serv->sv_cb_lock); |
| 188 | dprintk("Invoking bc_svc_process()\n"); |
| 189 | error = bc_svc_process(serv, req, rqstp); |
| 190 | dprintk("bc_svc_process() returned w/ error code= %d\n", |
| 191 | error); |
| 192 | } else { |
| 193 | spin_unlock_bh(&serv->sv_cb_lock); |
| 194 | schedule(); |
| 195 | } |
| 196 | finish_wait(&serv->sv_cb_waitq, &wq); |
| 197 | } |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Bring up the NFSv4.1 callback service |
| 203 | */ |
| 204 | struct svc_rqst * |
| 205 | nfs41_callback_up(struct svc_serv *serv, struct rpc_xprt *xprt) |
| 206 | { |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame] | 207 | struct svc_rqst *rqstp; |
| 208 | int ret; |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 209 | |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame] | 210 | /* |
| 211 | * Create an svc_sock for the back channel service that shares the |
| 212 | * fore channel connection. |
| 213 | * Returns the input port (0) and sets the svc_serv bc_xprt on success |
| 214 | */ |
| 215 | ret = svc_create_xprt(serv, "tcp-bc", &init_net, PF_INET, 0, |
| 216 | SVC_SOCK_ANONYMOUS); |
| 217 | if (ret < 0) { |
| 218 | rqstp = ERR_PTR(ret); |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 219 | goto out; |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame] | 220 | } |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 221 | |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 222 | /* |
| 223 | * Save the svc_serv in the transport so that it can |
| 224 | * be referenced when the session backchannel is initialized |
| 225 | */ |
| 226 | xprt->bc_serv = serv; |
| 227 | |
| 228 | INIT_LIST_HEAD(&serv->sv_cb_list); |
| 229 | spin_lock_init(&serv->sv_cb_lock); |
| 230 | init_waitqueue_head(&serv->sv_cb_waitq); |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 231 | rqstp = svc_prepare_thread(serv, &serv->sv_pools[0]); |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame] | 232 | if (IS_ERR(rqstp)) { |
| 233 | svc_xprt_put(serv->bc_xprt); |
| 234 | serv->bc_xprt = NULL; |
| 235 | } |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 236 | out: |
Andy Adamson | 01c9a0b | 2011-01-06 02:04:28 +0000 | [diff] [blame] | 237 | dprintk("--> %s return %ld\n", __func__, |
| 238 | IS_ERR(rqstp) ? PTR_ERR(rqstp) : 0); |
Benny Halevy | 8f97524 | 2009-04-01 09:23:11 -0400 | [diff] [blame] | 239 | return rqstp; |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | static inline int nfs_minorversion_callback_svc_setup(u32 minorversion, |
| 243 | struct svc_serv *serv, struct rpc_xprt *xprt, |
| 244 | struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp)) |
| 245 | { |
| 246 | if (minorversion) { |
| 247 | *rqstpp = nfs41_callback_up(serv, xprt); |
| 248 | *callback_svc = nfs41_callback_svc; |
| 249 | } |
| 250 | return minorversion; |
| 251 | } |
| 252 | |
| 253 | static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt, |
| 254 | struct nfs_callback_data *cb_info) |
| 255 | { |
| 256 | if (minorversion) |
| 257 | xprt->bc_serv = cb_info->serv; |
| 258 | } |
| 259 | #else |
| 260 | static inline int nfs_minorversion_callback_svc_setup(u32 minorversion, |
| 261 | struct svc_serv *serv, struct rpc_xprt *xprt, |
| 262 | struct svc_rqst **rqstpp, int (**callback_svc)(void *vrqstp)) |
| 263 | { |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static inline void nfs_callback_bc_serv(u32 minorversion, struct rpc_xprt *xprt, |
| 268 | struct nfs_callback_data *cb_info) |
| 269 | { |
| 270 | } |
Andy Adamson | 2c2618c | 2011-01-06 02:04:31 +0000 | [diff] [blame^] | 271 | int nfs4_set_callback_sessionid(struct nfs_client *clp) |
| 272 | { |
| 273 | return 0; |
| 274 | } |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 275 | #endif /* CONFIG_NFS_V4_1 */ |
| 276 | |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 277 | /* |
| 278 | * Bring up the callback thread if it is not already up. |
| 279 | */ |
| 280 | int nfs_callback_up(u32 minorversion, struct rpc_xprt *xprt) |
| 281 | { |
| 282 | struct svc_serv *serv = NULL; |
| 283 | struct svc_rqst *rqstp; |
| 284 | int (*callback_svc)(void *vrqstp); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 285 | struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion]; |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 286 | char svc_name[12]; |
| 287 | int ret = 0; |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 288 | int minorversion_setup; |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 289 | |
| 290 | mutex_lock(&nfs_callback_mutex); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 291 | if (cb_info->users++ || cb_info->task != NULL) { |
| 292 | nfs_callback_bc_serv(minorversion, xprt, cb_info); |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 293 | goto out; |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 294 | } |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 295 | serv = svc_create(&nfs4_callback_program, NFS4_CALLBACK_BUFSIZE, NULL); |
| 296 | if (!serv) { |
| 297 | ret = -ENOMEM; |
| 298 | goto out_err; |
| 299 | } |
| 300 | |
Ricardo Labiaga | a43cde9 | 2009-04-01 09:23:08 -0400 | [diff] [blame] | 301 | minorversion_setup = nfs_minorversion_callback_svc_setup(minorversion, |
| 302 | serv, xprt, &rqstp, &callback_svc); |
| 303 | if (!minorversion_setup) { |
| 304 | /* v4.0 callback setup */ |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 305 | rqstp = nfs4_callback_up(serv); |
| 306 | callback_svc = nfs4_callback_svc; |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | if (IS_ERR(rqstp)) { |
| 310 | ret = PTR_ERR(rqstp); |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 311 | goto out_err; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | svc_sock_update_bufs(serv); |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 315 | |
Benny Halevy | 7146851 | 2009-04-01 09:22:56 -0400 | [diff] [blame] | 316 | sprintf(svc_name, "nfsv4.%u-svc", minorversion); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 317 | cb_info->serv = serv; |
| 318 | cb_info->rqst = rqstp; |
| 319 | cb_info->task = kthread_run(callback_svc, cb_info->rqst, svc_name); |
| 320 | if (IS_ERR(cb_info->task)) { |
| 321 | ret = PTR_ERR(cb_info->task); |
| 322 | svc_exit_thread(cb_info->rqst); |
| 323 | cb_info->rqst = NULL; |
| 324 | cb_info->task = NULL; |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 325 | goto out_err; |
| 326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | out: |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 328 | /* |
| 329 | * svc_create creates the svc_serv with sv_nrthreads == 1, and then |
Jeff Layton | a277e33 | 2008-02-20 08:55:30 -0500 | [diff] [blame] | 330 | * svc_prepare_thread increments that. So we need to call svc_destroy |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 331 | * on both success and failure so that the refcount is 1 when the |
| 332 | * thread exits. |
| 333 | */ |
| 334 | if (serv) |
| 335 | svc_destroy(serv); |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 336 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | return ret; |
Jeff Layton | 8e60029 | 2008-02-11 10:00:20 -0500 | [diff] [blame] | 338 | out_err: |
Chuck Lever | 18de973 | 2008-10-16 17:41:11 -0400 | [diff] [blame] | 339 | dprintk("NFS: Couldn't create callback socket or server thread; " |
| 340 | "err = %d\n", ret); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 341 | cb_info->users--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | goto out; |
| 343 | } |
| 344 | |
| 345 | /* |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 346 | * Kill the callback thread if it's no longer being used. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | */ |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 348 | void nfs_callback_down(int minorversion) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | { |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 350 | struct nfs_callback_data *cb_info = &nfs_callback_info[minorversion]; |
| 351 | |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 352 | mutex_lock(&nfs_callback_mutex); |
Andy Adamson | e82dc22 | 2009-04-01 09:23:14 -0400 | [diff] [blame] | 353 | cb_info->users--; |
| 354 | if (cb_info->users == 0 && cb_info->task != NULL) { |
| 355 | kthread_stop(cb_info->task); |
| 356 | svc_exit_thread(cb_info->rqst); |
| 357 | cb_info->serv = NULL; |
| 358 | cb_info->rqst = NULL; |
| 359 | cb_info->task = NULL; |
Jeff Layton | 5afc597 | 2008-06-11 10:03:11 -0400 | [diff] [blame] | 360 | } |
Ingo Molnar | 353ab6e | 2006-03-26 01:37:12 -0800 | [diff] [blame] | 361 | mutex_unlock(&nfs_callback_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 364 | static int check_gss_callback_principal(struct nfs_client *clp, |
| 365 | struct svc_rqst *rqstp) |
| 366 | { |
| 367 | struct rpc_clnt *r = clp->cl_rpcclient; |
| 368 | char *p = svc_gss_principal(rqstp); |
| 369 | |
| 370 | /* |
| 371 | * It might just be a normal user principal, in which case |
| 372 | * userspace won't bother to tell us the name at all. |
| 373 | */ |
| 374 | if (p == NULL) |
| 375 | return SVC_DENIED; |
| 376 | |
| 377 | /* Expect a GSS_C_NT_HOSTBASED_NAME like "nfs@serverhostname" */ |
| 378 | |
| 379 | if (memcmp(p, "nfs@", 4) != 0) |
| 380 | return SVC_DENIED; |
| 381 | p += 4; |
| 382 | if (strcmp(p, r->cl_server) != 0) |
| 383 | return SVC_DENIED; |
| 384 | return SVC_OK; |
| 385 | } |
| 386 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | static int nfs_callback_authenticate(struct svc_rqst *rqstp) |
| 388 | { |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 389 | struct nfs_client *clp; |
Pavel Emelyanov | 5216a8e | 2008-02-21 10:57:45 +0300 | [diff] [blame] | 390 | RPC_IFDEBUG(char buf[RPC_MAX_ADDRBUFLEN]); |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 391 | int ret = SVC_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
| 393 | /* Don't talk to strangers */ |
Chuck Lever | ff05264 | 2007-12-10 14:58:44 -0500 | [diff] [blame] | 394 | clp = nfs_find_client(svc_addr(rqstp), 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | if (clp == NULL) |
| 396 | return SVC_DROP; |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 397 | |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 398 | dprintk("%s: %s NFSv4 callback!\n", __func__, |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 399 | svc_print_addr(rqstp, buf, sizeof(buf))); |
Chuck Lever | ad06e4b | 2007-02-12 00:53:32 -0800 | [diff] [blame] | 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | switch (rqstp->rq_authop->flavour) { |
| 402 | case RPC_AUTH_NULL: |
| 403 | if (rqstp->rq_proc != CB_NULL) |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 404 | ret = SVC_DENIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | break; |
| 406 | case RPC_AUTH_UNIX: |
| 407 | break; |
| 408 | case RPC_AUTH_GSS: |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 409 | ret = check_gss_callback_principal(clp, rqstp); |
| 410 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | default: |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 412 | ret = SVC_DENIED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | } |
Olga Kornievskaia | 945b34a | 2008-12-23 16:18:34 -0500 | [diff] [blame] | 414 | nfs_put_client(clp); |
| 415 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Define NFS4 callback program |
| 420 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | static struct svc_version *nfs4_callback_version[] = { |
| 422 | [1] = &nfs4_callback_version1, |
Alexandros Batsakis | 07bccc2 | 2009-12-05 13:19:01 -0500 | [diff] [blame] | 423 | [4] = &nfs4_callback_version4, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
| 426 | static struct svc_stat nfs4_callback_stats; |
| 427 | |
| 428 | static struct svc_program nfs4_callback_program = { |
| 429 | .pg_prog = NFS4_CALLBACK, /* RPC service number */ |
| 430 | .pg_nvers = ARRAY_SIZE(nfs4_callback_version), /* Number of entries */ |
| 431 | .pg_vers = nfs4_callback_version, /* version table */ |
| 432 | .pg_name = "NFSv4 callback", /* service name */ |
| 433 | .pg_class = "nfs", /* authentication class */ |
| 434 | .pg_stats = &nfs4_callback_stats, |
| 435 | .pg_authenticate = nfs_callback_authenticate, |
| 436 | }; |