Bryan Schumaker | 428360d | 2012-07-16 16:39:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. |
| 3 | * Written by David Howells (dhowells@redhat.com) |
| 4 | */ |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 5 | #include <linux/module.h> |
Bryan Schumaker | 428360d | 2012-07-16 16:39:17 -0400 | [diff] [blame] | 6 | #include <linux/nfs_fs.h> |
| 7 | #include <linux/nfs_idmap.h> |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 8 | #include <linux/nfs_mount.h> |
Bryan Schumaker | 428360d | 2012-07-16 16:39:17 -0400 | [diff] [blame] | 9 | #include <linux/sunrpc/auth.h> |
| 10 | #include <linux/sunrpc/xprt.h> |
| 11 | #include <linux/sunrpc/bc_xprt.h> |
| 12 | #include "internal.h" |
| 13 | #include "callback.h" |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 14 | #include "delegation.h" |
Trond Myklebust | 73e39aa | 2012-11-26 12:49:34 -0500 | [diff] [blame] | 15 | #include "nfs4session.h" |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 16 | #include "pnfs.h" |
| 17 | #include "netns.h" |
Bryan Schumaker | 428360d | 2012-07-16 16:39:17 -0400 | [diff] [blame] | 18 | |
| 19 | #define NFSDBG_FACILITY NFSDBG_CLIENT |
| 20 | |
| 21 | /* |
Bryan Schumaker | ec40989 | 2012-07-16 16:39:21 -0400 | [diff] [blame] | 22 | * Get a unique NFSv4.0 callback identifier which will be used |
| 23 | * by the V4.0 callback service to lookup the nfs_client struct |
| 24 | */ |
| 25 | static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion) |
| 26 | { |
| 27 | int ret = 0; |
| 28 | struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); |
| 29 | |
| 30 | if (clp->rpc_ops->version != 4 || minorversion != 0) |
| 31 | return ret; |
Tejun Heo | d687031 | 2013-02-27 17:05:01 -0800 | [diff] [blame^] | 32 | idr_preload(GFP_KERNEL); |
Bryan Schumaker | ec40989 | 2012-07-16 16:39:21 -0400 | [diff] [blame] | 33 | spin_lock(&nn->nfs_client_lock); |
Tejun Heo | d687031 | 2013-02-27 17:05:01 -0800 | [diff] [blame^] | 34 | ret = idr_alloc(&nn->cb_ident_idr, clp, 0, 0, GFP_NOWAIT); |
| 35 | if (ret >= 0) |
| 36 | clp->cl_cb_ident = ret; |
Bryan Schumaker | ec40989 | 2012-07-16 16:39:21 -0400 | [diff] [blame] | 37 | spin_unlock(&nn->nfs_client_lock); |
Tejun Heo | d687031 | 2013-02-27 17:05:01 -0800 | [diff] [blame^] | 38 | idr_preload_end(); |
| 39 | return ret < 0 ? ret : 0; |
Bryan Schumaker | ec40989 | 2012-07-16 16:39:21 -0400 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | #ifdef CONFIG_NFS_V4_1 |
| 43 | static void nfs4_shutdown_session(struct nfs_client *clp) |
| 44 | { |
| 45 | if (nfs4_has_session(clp)) { |
| 46 | nfs4_destroy_session(clp->cl_session); |
| 47 | nfs4_destroy_clientid(clp); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 | #else /* CONFIG_NFS_V4_1 */ |
| 52 | static void nfs4_shutdown_session(struct nfs_client *clp) |
| 53 | { |
| 54 | } |
| 55 | #endif /* CONFIG_NFS_V4_1 */ |
| 56 | |
| 57 | struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init) |
| 58 | { |
| 59 | int err; |
| 60 | struct nfs_client *clp = nfs_alloc_client(cl_init); |
| 61 | if (IS_ERR(clp)) |
| 62 | return clp; |
| 63 | |
| 64 | err = nfs_get_cb_ident_idr(clp, cl_init->minorversion); |
| 65 | if (err) |
| 66 | goto error; |
| 67 | |
| 68 | spin_lock_init(&clp->cl_lock); |
| 69 | INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state); |
| 70 | rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client"); |
| 71 | clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED; |
| 72 | clp->cl_minorversion = cl_init->minorversion; |
| 73 | clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion]; |
| 74 | return clp; |
| 75 | |
| 76 | error: |
Trond Myklebust | 7653f6f | 2012-08-20 12:12:29 -0400 | [diff] [blame] | 77 | nfs_free_client(clp); |
Bryan Schumaker | ec40989 | 2012-07-16 16:39:21 -0400 | [diff] [blame] | 78 | return ERR_PTR(err); |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Destroy the NFS4 callback service |
| 83 | */ |
| 84 | static void nfs4_destroy_callback(struct nfs_client *clp) |
| 85 | { |
| 86 | if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) |
Stanislav Kinsbursky | c946556 | 2012-08-20 18:00:16 +0400 | [diff] [blame] | 87 | nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net); |
Bryan Schumaker | ec40989 | 2012-07-16 16:39:21 -0400 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | static void nfs4_shutdown_client(struct nfs_client *clp) |
| 91 | { |
| 92 | if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state)) |
| 93 | nfs4_kill_renewd(clp); |
| 94 | nfs4_shutdown_session(clp); |
| 95 | nfs4_destroy_callback(clp); |
| 96 | if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) |
| 97 | nfs_idmap_delete(clp); |
| 98 | |
| 99 | rpc_destroy_wait_queue(&clp->cl_rpcwaitq); |
| 100 | kfree(clp->cl_serverowner); |
| 101 | kfree(clp->cl_serverscope); |
| 102 | kfree(clp->cl_implid); |
| 103 | } |
| 104 | |
| 105 | void nfs4_free_client(struct nfs_client *clp) |
| 106 | { |
| 107 | nfs4_shutdown_client(clp); |
| 108 | nfs_free_client(clp); |
| 109 | } |
| 110 | |
| 111 | /* |
Bryan Schumaker | 428360d | 2012-07-16 16:39:17 -0400 | [diff] [blame] | 112 | * Initialize the NFS4 callback service |
| 113 | */ |
| 114 | static int nfs4_init_callback(struct nfs_client *clp) |
| 115 | { |
| 116 | int error; |
| 117 | |
| 118 | if (clp->rpc_ops->version == 4) { |
| 119 | struct rpc_xprt *xprt; |
| 120 | |
| 121 | xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt); |
| 122 | |
| 123 | if (nfs4_has_session(clp)) { |
| 124 | error = xprt_setup_backchannel(xprt, |
| 125 | NFS41_BC_MIN_CALLBACKS); |
| 126 | if (error < 0) |
| 127 | return error; |
| 128 | } |
| 129 | |
| 130 | error = nfs_callback_up(clp->cl_mvops->minor_version, xprt); |
| 131 | if (error < 0) { |
| 132 | dprintk("%s: failed to start callback. Error = %d\n", |
| 133 | __func__, error); |
| 134 | return error; |
| 135 | } |
| 136 | __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state); |
| 137 | } |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * Initialize the minor version specific parts of an NFS4 client record |
| 143 | */ |
| 144 | static int nfs4_init_client_minor_version(struct nfs_client *clp) |
| 145 | { |
| 146 | #if defined(CONFIG_NFS_V4_1) |
| 147 | if (clp->cl_mvops->minor_version) { |
| 148 | struct nfs4_session *session = NULL; |
| 149 | /* |
| 150 | * Create the session and mark it expired. |
| 151 | * When a SEQUENCE operation encounters the expired session |
| 152 | * it will do session recovery to initialize it. |
| 153 | */ |
| 154 | session = nfs4_alloc_session(clp); |
| 155 | if (!session) |
| 156 | return -ENOMEM; |
| 157 | |
| 158 | clp->cl_session = session; |
| 159 | /* |
| 160 | * The create session reply races with the server back |
| 161 | * channel probe. Mark the client NFS_CS_SESSION_INITING |
| 162 | * so that the client back channel can find the |
| 163 | * nfs_client struct |
| 164 | */ |
| 165 | nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING); |
| 166 | } |
| 167 | #endif /* CONFIG_NFS_V4_1 */ |
| 168 | |
| 169 | return nfs4_init_callback(clp); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * nfs4_init_client - Initialise an NFS4 client record |
| 174 | * |
| 175 | * @clp: nfs_client to initialise |
| 176 | * @timeparms: timeout parameters for underlying RPC transport |
| 177 | * @ip_addr: callback IP address in presentation format |
| 178 | * @authflavor: authentication flavor for underlying RPC transport |
| 179 | * |
| 180 | * Returns pointer to an NFS client, or an ERR_PTR value. |
| 181 | */ |
| 182 | struct nfs_client *nfs4_init_client(struct nfs_client *clp, |
| 183 | const struct rpc_timeout *timeparms, |
| 184 | const char *ip_addr, |
| 185 | rpc_authflavor_t authflavour) |
| 186 | { |
| 187 | char buf[INET6_ADDRSTRLEN + 1]; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 188 | struct nfs_client *old; |
Bryan Schumaker | 428360d | 2012-07-16 16:39:17 -0400 | [diff] [blame] | 189 | int error; |
| 190 | |
| 191 | if (clp->cl_cons_state == NFS_CS_READY) { |
| 192 | /* the client is initialised already */ |
| 193 | dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp); |
| 194 | return clp; |
| 195 | } |
| 196 | |
| 197 | /* Check NFS protocol revision and initialize RPC op vector */ |
| 198 | clp->rpc_ops = &nfs_v4_clientops; |
| 199 | |
| 200 | __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags); |
| 201 | error = nfs_create_rpc_client(clp, timeparms, authflavour); |
| 202 | if (error < 0) |
| 203 | goto error; |
| 204 | |
| 205 | /* If no clientaddr= option was specified, find a usable cb address */ |
| 206 | if (ip_addr == NULL) { |
| 207 | struct sockaddr_storage cb_addr; |
| 208 | struct sockaddr *sap = (struct sockaddr *)&cb_addr; |
| 209 | |
| 210 | error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr)); |
| 211 | if (error < 0) |
| 212 | goto error; |
| 213 | error = rpc_ntop(sap, buf, sizeof(buf)); |
| 214 | if (error < 0) |
| 215 | goto error; |
| 216 | ip_addr = (const char *)buf; |
| 217 | } |
| 218 | strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); |
| 219 | |
| 220 | error = nfs_idmap_new(clp); |
| 221 | if (error < 0) { |
| 222 | dprintk("%s: failed to create idmapper. Error = %d\n", |
| 223 | __func__, error); |
| 224 | goto error; |
| 225 | } |
| 226 | __set_bit(NFS_CS_IDMAP, &clp->cl_res_state); |
| 227 | |
| 228 | error = nfs4_init_client_minor_version(clp); |
| 229 | if (error < 0) |
| 230 | goto error; |
| 231 | |
| 232 | if (!nfs4_has_session(clp)) |
| 233 | nfs_mark_client_ready(clp, NFS_CS_READY); |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 234 | |
| 235 | error = nfs4_discover_server_trunking(clp, &old); |
| 236 | if (error < 0) |
| 237 | goto error; |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 238 | nfs_put_client(clp); |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 239 | if (clp != old) { |
| 240 | clp->cl_preserve_clid = true; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 241 | clp = old; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 242 | } |
| 243 | |
Bryan Schumaker | 428360d | 2012-07-16 16:39:17 -0400 | [diff] [blame] | 244 | return clp; |
| 245 | |
| 246 | error: |
| 247 | nfs_mark_client_ready(clp, error); |
| 248 | nfs_put_client(clp); |
| 249 | dprintk("<-- nfs4_init_client() = xerror %d\n", error); |
| 250 | return ERR_PTR(error); |
| 251 | } |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 252 | |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 253 | /* |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 254 | * SETCLIENTID just did a callback update with the callback ident in |
| 255 | * "drop," but server trunking discovery claims "drop" and "keep" are |
| 256 | * actually the same server. Swap the callback IDs so that "keep" |
| 257 | * will continue to use the callback ident the server now knows about, |
| 258 | * and so that "keep"'s original callback ident is destroyed when |
| 259 | * "drop" is freed. |
| 260 | */ |
| 261 | static void nfs4_swap_callback_idents(struct nfs_client *keep, |
| 262 | struct nfs_client *drop) |
| 263 | { |
| 264 | struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id); |
| 265 | unsigned int save = keep->cl_cb_ident; |
| 266 | |
| 267 | if (keep->cl_cb_ident == drop->cl_cb_ident) |
| 268 | return; |
| 269 | |
| 270 | dprintk("%s: keeping callback ident %u and dropping ident %u\n", |
| 271 | __func__, keep->cl_cb_ident, drop->cl_cb_ident); |
| 272 | |
| 273 | spin_lock(&nn->nfs_client_lock); |
| 274 | |
| 275 | idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident); |
| 276 | keep->cl_cb_ident = drop->cl_cb_ident; |
| 277 | |
| 278 | idr_replace(&nn->cb_ident_idr, drop, save); |
| 279 | drop->cl_cb_ident = save; |
| 280 | |
| 281 | spin_unlock(&nn->nfs_client_lock); |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * nfs40_walk_client_list - Find server that recognizes a client ID |
| 286 | * |
| 287 | * @new: nfs_client with client ID to test |
| 288 | * @result: OUT: found nfs_client, or new |
| 289 | * @cred: credential to use for trunking test |
| 290 | * |
| 291 | * Returns zero, a negative errno, or a negative NFS4ERR status. |
| 292 | * If zero is returned, an nfs_client pointer is planted in "result." |
| 293 | * |
| 294 | * NB: nfs40_walk_client_list() relies on the new nfs_client being |
| 295 | * the last nfs_client on the list. |
| 296 | */ |
| 297 | int nfs40_walk_client_list(struct nfs_client *new, |
| 298 | struct nfs_client **result, |
| 299 | struct rpc_cred *cred) |
| 300 | { |
| 301 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); |
| 302 | struct nfs_client *pos, *n, *prev = NULL; |
| 303 | struct nfs4_setclientid_res clid = { |
| 304 | .clientid = new->cl_clientid, |
| 305 | .confirm = new->cl_confirm, |
| 306 | }; |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 307 | int status = -NFS4ERR_STALE_CLIENTID; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 308 | |
| 309 | spin_lock(&nn->nfs_client_lock); |
| 310 | list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) { |
| 311 | /* If "pos" isn't marked ready, we can't trust the |
| 312 | * remaining fields in "pos" */ |
| 313 | if (pos->cl_cons_state < NFS_CS_READY) |
| 314 | continue; |
| 315 | |
| 316 | if (pos->rpc_ops != new->rpc_ops) |
| 317 | continue; |
| 318 | |
| 319 | if (pos->cl_proto != new->cl_proto) |
| 320 | continue; |
| 321 | |
| 322 | if (pos->cl_minorversion != new->cl_minorversion) |
| 323 | continue; |
| 324 | |
| 325 | if (pos->cl_clientid != new->cl_clientid) |
| 326 | continue; |
| 327 | |
| 328 | atomic_inc(&pos->cl_count); |
| 329 | spin_unlock(&nn->nfs_client_lock); |
| 330 | |
| 331 | if (prev) |
| 332 | nfs_put_client(prev); |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 333 | prev = pos; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 334 | |
| 335 | status = nfs4_proc_setclientid_confirm(pos, &clid, cred); |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 336 | switch (status) { |
| 337 | case -NFS4ERR_STALE_CLIENTID: |
| 338 | break; |
| 339 | case 0: |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 340 | nfs4_swap_callback_idents(pos, new); |
| 341 | |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 342 | prev = NULL; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 343 | *result = pos; |
| 344 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", |
| 345 | __func__, pos, atomic_read(&pos->cl_count)); |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 346 | default: |
| 347 | goto out; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | spin_lock(&nn->nfs_client_lock); |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 351 | } |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 352 | spin_unlock(&nn->nfs_client_lock); |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 353 | |
Trond Myklebust | 202c312 | 2013-01-18 22:56:23 -0500 | [diff] [blame] | 354 | /* No match found. The server lost our clientid */ |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 355 | out: |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 356 | if (prev) |
| 357 | nfs_put_client(prev); |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 358 | dprintk("NFS: <-- %s status = %d\n", __func__, status); |
| 359 | return status; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | #ifdef CONFIG_NFS_V4_1 |
| 363 | /* |
Trond Myklebust | f9d640f | 2012-10-01 16:37:51 -0700 | [diff] [blame] | 364 | * Returns true if the client IDs match |
| 365 | */ |
| 366 | static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b) |
| 367 | { |
| 368 | if (a->cl_clientid != b->cl_clientid) { |
| 369 | dprintk("NFS: --> %s client ID %llx does not match %llx\n", |
| 370 | __func__, a->cl_clientid, b->cl_clientid); |
| 371 | return false; |
| 372 | } |
| 373 | dprintk("NFS: --> %s client ID %llx matches %llx\n", |
| 374 | __func__, a->cl_clientid, b->cl_clientid); |
| 375 | return true; |
| 376 | } |
| 377 | |
| 378 | /* |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 379 | * Returns true if the server owners match |
| 380 | */ |
| 381 | static bool |
| 382 | nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b) |
| 383 | { |
| 384 | struct nfs41_server_owner *o1 = a->cl_serverowner; |
| 385 | struct nfs41_server_owner *o2 = b->cl_serverowner; |
| 386 | |
| 387 | if (o1->minor_id != o2->minor_id) { |
| 388 | dprintk("NFS: --> %s server owner minor IDs do not match\n", |
| 389 | __func__); |
| 390 | return false; |
| 391 | } |
| 392 | |
| 393 | if (o1->major_id_sz != o2->major_id_sz) |
| 394 | goto out_major_mismatch; |
| 395 | if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0) |
| 396 | goto out_major_mismatch; |
| 397 | |
| 398 | dprintk("NFS: --> %s server owners match\n", __func__); |
| 399 | return true; |
| 400 | |
| 401 | out_major_mismatch: |
| 402 | dprintk("NFS: --> %s server owner major IDs do not match\n", |
| 403 | __func__); |
| 404 | return false; |
| 405 | } |
| 406 | |
| 407 | /** |
| 408 | * nfs41_walk_client_list - Find nfs_client that matches a client/server owner |
| 409 | * |
| 410 | * @new: nfs_client with client ID to test |
| 411 | * @result: OUT: found nfs_client, or new |
| 412 | * @cred: credential to use for trunking test |
| 413 | * |
| 414 | * Returns zero, a negative errno, or a negative NFS4ERR status. |
| 415 | * If zero is returned, an nfs_client pointer is planted in "result." |
| 416 | * |
| 417 | * NB: nfs41_walk_client_list() relies on the new nfs_client being |
| 418 | * the last nfs_client on the list. |
| 419 | */ |
| 420 | int nfs41_walk_client_list(struct nfs_client *new, |
| 421 | struct nfs_client **result, |
| 422 | struct rpc_cred *cred) |
| 423 | { |
| 424 | struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); |
| 425 | struct nfs_client *pos, *n, *prev = NULL; |
Trond Myklebust | 202c312 | 2013-01-18 22:56:23 -0500 | [diff] [blame] | 426 | int status = -NFS4ERR_STALE_CLIENTID; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 427 | |
| 428 | spin_lock(&nn->nfs_client_lock); |
| 429 | list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) { |
| 430 | /* If "pos" isn't marked ready, we can't trust the |
| 431 | * remaining fields in "pos", especially the client |
| 432 | * ID and serverowner fields. Wait for CREATE_SESSION |
| 433 | * to finish. */ |
| 434 | if (pos->cl_cons_state < NFS_CS_READY) { |
| 435 | atomic_inc(&pos->cl_count); |
| 436 | spin_unlock(&nn->nfs_client_lock); |
| 437 | |
| 438 | if (prev) |
| 439 | nfs_put_client(prev); |
| 440 | prev = pos; |
| 441 | |
Trond Myklebust | 65436ec | 2013-01-18 23:01:43 -0500 | [diff] [blame] | 442 | nfs4_schedule_lease_recovery(pos); |
Trond Myklebust | 202c312 | 2013-01-18 22:56:23 -0500 | [diff] [blame] | 443 | status = nfs_wait_client_init_complete(pos); |
| 444 | if (status < 0) { |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 445 | nfs_put_client(pos); |
Chuck Lever | c2ccc08 | 2012-10-02 09:18:12 -0700 | [diff] [blame] | 446 | spin_lock(&nn->nfs_client_lock); |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 447 | continue; |
| 448 | } |
Trond Myklebust | 65436ec | 2013-01-18 23:01:43 -0500 | [diff] [blame] | 449 | status = pos->cl_cons_state; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 450 | spin_lock(&nn->nfs_client_lock); |
Trond Myklebust | 65436ec | 2013-01-18 23:01:43 -0500 | [diff] [blame] | 451 | if (status < 0) |
| 452 | continue; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | if (pos->rpc_ops != new->rpc_ops) |
| 456 | continue; |
| 457 | |
| 458 | if (pos->cl_proto != new->cl_proto) |
| 459 | continue; |
| 460 | |
| 461 | if (pos->cl_minorversion != new->cl_minorversion) |
| 462 | continue; |
| 463 | |
| 464 | if (!nfs4_match_clientids(pos, new)) |
| 465 | continue; |
| 466 | |
| 467 | if (!nfs4_match_serverowners(pos, new)) |
| 468 | continue; |
| 469 | |
Trond Myklebust | 4ae19c2 | 2013-01-18 22:41:53 -0500 | [diff] [blame] | 470 | atomic_inc(&pos->cl_count); |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 471 | spin_unlock(&nn->nfs_client_lock); |
| 472 | dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n", |
| 473 | __func__, pos, atomic_read(&pos->cl_count)); |
| 474 | |
| 475 | *result = pos; |
| 476 | return 0; |
| 477 | } |
| 478 | |
Trond Myklebust | 202c312 | 2013-01-18 22:56:23 -0500 | [diff] [blame] | 479 | /* No matching nfs_client found. */ |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 480 | spin_unlock(&nn->nfs_client_lock); |
Trond Myklebust | 202c312 | 2013-01-18 22:56:23 -0500 | [diff] [blame] | 481 | dprintk("NFS: <-- %s status = %d\n", __func__, status); |
| 482 | return status; |
Chuck Lever | 05f4c35 | 2012-09-14 17:24:32 -0400 | [diff] [blame] | 483 | } |
| 484 | #endif /* CONFIG_NFS_V4_1 */ |
| 485 | |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 486 | static void nfs4_destroy_server(struct nfs_server *server) |
| 487 | { |
| 488 | nfs_server_return_all_delegations(server); |
| 489 | unset_pnfs_layoutdriver(server); |
| 490 | nfs4_purge_state_owners(server); |
| 491 | } |
| 492 | |
| 493 | /* |
| 494 | * NFSv4.0 callback thread helper |
| 495 | * |
| 496 | * Find a client by callback identifier |
| 497 | */ |
| 498 | struct nfs_client * |
| 499 | nfs4_find_client_ident(struct net *net, int cb_ident) |
| 500 | { |
| 501 | struct nfs_client *clp; |
| 502 | struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 503 | |
| 504 | spin_lock(&nn->nfs_client_lock); |
| 505 | clp = idr_find(&nn->cb_ident_idr, cb_ident); |
| 506 | if (clp) |
| 507 | atomic_inc(&clp->cl_count); |
| 508 | spin_unlock(&nn->nfs_client_lock); |
| 509 | return clp; |
| 510 | } |
| 511 | |
| 512 | #if defined(CONFIG_NFS_V4_1) |
| 513 | /* Common match routine for v4.0 and v4.1 callback services */ |
| 514 | static bool nfs4_cb_match_client(const struct sockaddr *addr, |
| 515 | struct nfs_client *clp, u32 minorversion) |
| 516 | { |
| 517 | struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; |
| 518 | |
| 519 | /* Don't match clients that failed to initialise */ |
| 520 | if (!(clp->cl_cons_state == NFS_CS_READY || |
| 521 | clp->cl_cons_state == NFS_CS_SESSION_INITING)) |
| 522 | return false; |
| 523 | |
| 524 | smp_rmb(); |
| 525 | |
| 526 | /* Match the version and minorversion */ |
| 527 | if (clp->rpc_ops->version != 4 || |
| 528 | clp->cl_minorversion != minorversion) |
| 529 | return false; |
| 530 | |
| 531 | /* Match only the IP address, not the port number */ |
| 532 | if (!nfs_sockaddr_match_ipaddr(addr, clap)) |
| 533 | return false; |
| 534 | |
| 535 | return true; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * NFSv4.1 callback thread helper |
| 540 | * For CB_COMPOUND calls, find a client by IP address, protocol version, |
| 541 | * minorversion, and sessionID |
| 542 | * |
| 543 | * Returns NULL if no such client |
| 544 | */ |
| 545 | struct nfs_client * |
| 546 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, |
| 547 | struct nfs4_sessionid *sid) |
| 548 | { |
| 549 | struct nfs_client *clp; |
| 550 | struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 551 | |
| 552 | spin_lock(&nn->nfs_client_lock); |
| 553 | list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { |
| 554 | if (nfs4_cb_match_client(addr, clp, 1) == false) |
| 555 | continue; |
| 556 | |
| 557 | if (!nfs4_has_session(clp)) |
| 558 | continue; |
| 559 | |
| 560 | /* Match sessionid*/ |
| 561 | if (memcmp(clp->cl_session->sess_id.data, |
| 562 | sid->data, NFS4_MAX_SESSIONID_LEN) != 0) |
| 563 | continue; |
| 564 | |
| 565 | atomic_inc(&clp->cl_count); |
| 566 | spin_unlock(&nn->nfs_client_lock); |
| 567 | return clp; |
| 568 | } |
| 569 | spin_unlock(&nn->nfs_client_lock); |
| 570 | return NULL; |
| 571 | } |
| 572 | |
| 573 | #else /* CONFIG_NFS_V4_1 */ |
| 574 | |
| 575 | struct nfs_client * |
| 576 | nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, |
| 577 | struct nfs4_sessionid *sid) |
| 578 | { |
| 579 | return NULL; |
| 580 | } |
| 581 | #endif /* CONFIG_NFS_V4_1 */ |
| 582 | |
| 583 | /* |
| 584 | * Set up an NFS4 client |
| 585 | */ |
| 586 | static int nfs4_set_client(struct nfs_server *server, |
| 587 | const char *hostname, |
| 588 | const struct sockaddr *addr, |
| 589 | const size_t addrlen, |
| 590 | const char *ip_addr, |
| 591 | rpc_authflavor_t authflavour, |
| 592 | int proto, const struct rpc_timeout *timeparms, |
| 593 | u32 minorversion, struct net *net) |
| 594 | { |
| 595 | struct nfs_client_initdata cl_init = { |
| 596 | .hostname = hostname, |
| 597 | .addr = addr, |
| 598 | .addrlen = addrlen, |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 599 | .nfs_mod = &nfs_v4, |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 600 | .proto = proto, |
| 601 | .minorversion = minorversion, |
| 602 | .net = net, |
| 603 | }; |
| 604 | struct nfs_client *clp; |
| 605 | int error; |
| 606 | |
| 607 | dprintk("--> nfs4_set_client()\n"); |
| 608 | |
| 609 | if (server->flags & NFS_MOUNT_NORESVPORT) |
| 610 | set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); |
| 611 | |
| 612 | /* Allocate or find a client reference we can use */ |
| 613 | clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour); |
| 614 | if (IS_ERR(clp)) { |
| 615 | error = PTR_ERR(clp); |
| 616 | goto error; |
| 617 | } |
| 618 | |
| 619 | /* |
| 620 | * Query for the lease time on clientid setup or renewal |
| 621 | * |
| 622 | * Note that this will be set on nfs_clients that were created |
| 623 | * only for the DS role and did not set this bit, but now will |
| 624 | * serve a dual role. |
| 625 | */ |
| 626 | set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state); |
| 627 | |
| 628 | server->nfs_client = clp; |
| 629 | dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp); |
| 630 | return 0; |
| 631 | error: |
| 632 | dprintk("<-- nfs4_set_client() = xerror %d\n", error); |
| 633 | return error; |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Set up a pNFS Data Server client. |
| 638 | * |
| 639 | * Return any existing nfs_client that matches server address,port,version |
| 640 | * and minorversion. |
| 641 | * |
| 642 | * For a new nfs_client, use a soft mount (default), a low retrans and a |
| 643 | * low timeout interval so that if a connection is lost, we retry through |
| 644 | * the MDS. |
| 645 | */ |
| 646 | struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp, |
| 647 | const struct sockaddr *ds_addr, int ds_addrlen, |
| 648 | int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans) |
| 649 | { |
| 650 | struct nfs_client_initdata cl_init = { |
| 651 | .addr = ds_addr, |
| 652 | .addrlen = ds_addrlen, |
Bryan Schumaker | ab7017a | 2012-07-30 16:05:16 -0400 | [diff] [blame] | 653 | .nfs_mod = &nfs_v4, |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 654 | .proto = ds_proto, |
| 655 | .minorversion = mds_clp->cl_minorversion, |
| 656 | .net = mds_clp->cl_net, |
| 657 | }; |
| 658 | struct rpc_timeout ds_timeout; |
| 659 | struct nfs_client *clp; |
| 660 | |
| 661 | /* |
| 662 | * Set an authflavor equual to the MDS value. Use the MDS nfs_client |
| 663 | * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS |
| 664 | * (section 13.1 RFC 5661). |
| 665 | */ |
| 666 | nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans); |
| 667 | clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr, |
| 668 | mds_clp->cl_rpcclient->cl_auth->au_flavor); |
| 669 | |
| 670 | dprintk("<-- %s %p\n", __func__, clp); |
| 671 | return clp; |
| 672 | } |
| 673 | EXPORT_SYMBOL_GPL(nfs4_set_ds_client); |
| 674 | |
| 675 | /* |
| 676 | * Session has been established, and the client marked ready. |
| 677 | * Set the mount rsize and wsize with negotiated fore channel |
| 678 | * attributes which will be bound checked in nfs_server_set_fsinfo. |
| 679 | */ |
| 680 | static void nfs4_session_set_rwsize(struct nfs_server *server) |
| 681 | { |
| 682 | #ifdef CONFIG_NFS_V4_1 |
| 683 | struct nfs4_session *sess; |
| 684 | u32 server_resp_sz; |
| 685 | u32 server_rqst_sz; |
| 686 | |
| 687 | if (!nfs4_has_session(server->nfs_client)) |
| 688 | return; |
| 689 | sess = server->nfs_client->cl_session; |
| 690 | server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead; |
| 691 | server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead; |
| 692 | |
| 693 | if (server->rsize > server_resp_sz) |
| 694 | server->rsize = server_resp_sz; |
| 695 | if (server->wsize > server_rqst_sz) |
| 696 | server->wsize = server_rqst_sz; |
| 697 | #endif /* CONFIG_NFS_V4_1 */ |
| 698 | } |
| 699 | |
| 700 | static int nfs4_server_common_setup(struct nfs_server *server, |
| 701 | struct nfs_fh *mntfh) |
| 702 | { |
| 703 | struct nfs_fattr *fattr; |
| 704 | int error; |
| 705 | |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 706 | /* data servers support only a subset of NFSv4.1 */ |
| 707 | if (is_ds_only_client(server->nfs_client)) |
| 708 | return -EPROTONOSUPPORT; |
| 709 | |
| 710 | fattr = nfs_alloc_fattr(); |
| 711 | if (fattr == NULL) |
| 712 | return -ENOMEM; |
| 713 | |
| 714 | /* We must ensure the session is initialised first */ |
| 715 | error = nfs4_init_session(server); |
| 716 | if (error < 0) |
| 717 | goto out; |
| 718 | |
| 719 | /* Probe the root fh to retrieve its FSID and filehandle */ |
| 720 | error = nfs4_get_rootfh(server, mntfh); |
| 721 | if (error < 0) |
| 722 | goto out; |
| 723 | |
| 724 | dprintk("Server FSID: %llx:%llx\n", |
| 725 | (unsigned long long) server->fsid.major, |
| 726 | (unsigned long long) server->fsid.minor); |
| 727 | dprintk("Mount FH: %d\n", mntfh->size); |
| 728 | |
| 729 | nfs4_session_set_rwsize(server); |
| 730 | |
| 731 | error = nfs_probe_fsinfo(server, mntfh, fattr); |
| 732 | if (error < 0) |
| 733 | goto out; |
| 734 | |
| 735 | if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
| 736 | server->namelen = NFS4_MAXNAMLEN; |
| 737 | |
| 738 | nfs_server_insert_lists(server); |
| 739 | server->mount_time = jiffies; |
| 740 | server->destroy = nfs4_destroy_server; |
| 741 | out: |
| 742 | nfs_free_fattr(fattr); |
| 743 | return error; |
| 744 | } |
| 745 | |
| 746 | /* |
| 747 | * Create a version 4 volume record |
| 748 | */ |
| 749 | static int nfs4_init_server(struct nfs_server *server, |
| 750 | const struct nfs_parsed_mount_data *data) |
| 751 | { |
| 752 | struct rpc_timeout timeparms; |
| 753 | int error; |
| 754 | |
| 755 | dprintk("--> nfs4_init_server()\n"); |
| 756 | |
| 757 | nfs_init_timeout_values(&timeparms, data->nfs_server.protocol, |
| 758 | data->timeo, data->retrans); |
| 759 | |
| 760 | /* Initialise the client representation from the mount data */ |
| 761 | server->flags = data->flags; |
| 762 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK; |
| 763 | if (!(data->flags & NFS_MOUNT_NORDIRPLUS)) |
| 764 | server->caps |= NFS_CAP_READDIRPLUS; |
| 765 | server->options = data->options; |
| 766 | |
| 767 | /* Get a client record */ |
| 768 | error = nfs4_set_client(server, |
| 769 | data->nfs_server.hostname, |
| 770 | (const struct sockaddr *)&data->nfs_server.address, |
| 771 | data->nfs_server.addrlen, |
| 772 | data->client_address, |
| 773 | data->auth_flavors[0], |
| 774 | data->nfs_server.protocol, |
| 775 | &timeparms, |
| 776 | data->minorversion, |
| 777 | data->net); |
| 778 | if (error < 0) |
| 779 | goto error; |
| 780 | |
| 781 | /* |
| 782 | * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower |
| 783 | * authentication. |
| 784 | */ |
| 785 | if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX) |
| 786 | server->caps |= NFS_CAP_UIDGID_NOMAP; |
| 787 | |
| 788 | if (data->rsize) |
| 789 | server->rsize = nfs_block_size(data->rsize, NULL); |
| 790 | if (data->wsize) |
| 791 | server->wsize = nfs_block_size(data->wsize, NULL); |
| 792 | |
| 793 | server->acregmin = data->acregmin * HZ; |
| 794 | server->acregmax = data->acregmax * HZ; |
| 795 | server->acdirmin = data->acdirmin * HZ; |
| 796 | server->acdirmax = data->acdirmax * HZ; |
| 797 | |
| 798 | server->port = data->nfs_server.port; |
| 799 | |
| 800 | error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]); |
| 801 | |
| 802 | error: |
| 803 | /* Done */ |
| 804 | dprintk("<-- nfs4_init_server() = %d\n", error); |
| 805 | return error; |
| 806 | } |
| 807 | |
| 808 | /* |
| 809 | * Create a version 4 volume record |
| 810 | * - keyed on server and FSID |
| 811 | */ |
Bryan Schumaker | 1179acc | 2012-07-30 16:05:19 -0400 | [diff] [blame] | 812 | /*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data, |
| 813 | struct nfs_fh *mntfh)*/ |
| 814 | struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info, |
| 815 | struct nfs_subversion *nfs_mod) |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 816 | { |
| 817 | struct nfs_server *server; |
| 818 | int error; |
| 819 | |
| 820 | dprintk("--> nfs4_create_server()\n"); |
| 821 | |
| 822 | server = nfs_alloc_server(); |
| 823 | if (!server) |
| 824 | return ERR_PTR(-ENOMEM); |
| 825 | |
| 826 | /* set up the general RPC client */ |
Bryan Schumaker | 1179acc | 2012-07-30 16:05:19 -0400 | [diff] [blame] | 827 | error = nfs4_init_server(server, mount_info->parsed); |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 828 | if (error < 0) |
| 829 | goto error; |
| 830 | |
Bryan Schumaker | 1179acc | 2012-07-30 16:05:19 -0400 | [diff] [blame] | 831 | error = nfs4_server_common_setup(server, mount_info->mntfh); |
Bryan Schumaker | fcf1039 | 2012-07-16 16:39:18 -0400 | [diff] [blame] | 832 | if (error < 0) |
| 833 | goto error; |
| 834 | |
| 835 | dprintk("<-- nfs4_create_server() = %p\n", server); |
| 836 | return server; |
| 837 | |
| 838 | error: |
| 839 | nfs_free_server(server); |
| 840 | dprintk("<-- nfs4_create_server() = error %d\n", error); |
| 841 | return ERR_PTR(error); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Create an NFS4 referral server record |
| 846 | */ |
| 847 | struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data, |
| 848 | struct nfs_fh *mntfh) |
| 849 | { |
| 850 | struct nfs_client *parent_client; |
| 851 | struct nfs_server *server, *parent_server; |
| 852 | int error; |
| 853 | |
| 854 | dprintk("--> nfs4_create_referral_server()\n"); |
| 855 | |
| 856 | server = nfs_alloc_server(); |
| 857 | if (!server) |
| 858 | return ERR_PTR(-ENOMEM); |
| 859 | |
| 860 | parent_server = NFS_SB(data->sb); |
| 861 | parent_client = parent_server->nfs_client; |
| 862 | |
| 863 | /* Initialise the client representation from the parent server */ |
| 864 | nfs_server_copy_userdata(server, parent_server); |
| 865 | server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR; |
| 866 | |
| 867 | /* Get a client representation. |
| 868 | * Note: NFSv4 always uses TCP, */ |
| 869 | error = nfs4_set_client(server, data->hostname, |
| 870 | data->addr, |
| 871 | data->addrlen, |
| 872 | parent_client->cl_ipaddr, |
| 873 | data->authflavor, |
| 874 | rpc_protocol(parent_server->client), |
| 875 | parent_server->client->cl_timeout, |
| 876 | parent_client->cl_mvops->minor_version, |
| 877 | parent_client->cl_net); |
| 878 | if (error < 0) |
| 879 | goto error; |
| 880 | |
| 881 | error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor); |
| 882 | if (error < 0) |
| 883 | goto error; |
| 884 | |
| 885 | error = nfs4_server_common_setup(server, mntfh); |
| 886 | if (error < 0) |
| 887 | goto error; |
| 888 | |
| 889 | dprintk("<-- nfs_create_referral_server() = %p\n", server); |
| 890 | return server; |
| 891 | |
| 892 | error: |
| 893 | nfs_free_server(server); |
| 894 | dprintk("<-- nfs4_create_referral_server() = error %d\n", error); |
| 895 | return ERR_PTR(error); |
| 896 | } |