| 1 |
// SPDX-License-Identifier: GPL-2.0-only |
| 2 |
/* |
| 3 |
* Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. |
| 4 |
* Written by David Howells ([email protected]) |
| 5 |
*/ |
| 6 |
#include <linux/module.h> |
| 7 |
#include <linux/nfs_fs.h> |
| 8 |
#include <linux/nfs_mount.h> |
| 9 |
#include <linux/sunrpc/addr.h> |
| 10 |
#include <linux/sunrpc/auth.h> |
| 11 |
#include <linux/sunrpc/xprt.h> |
| 12 |
#include <linux/sunrpc/bc_xprt.h> |
| 13 |
#include <linux/sunrpc/rpc_pipe_fs.h> |
| 14 |
#include <net/handshake.h> |
| 15 |
#include "internal.h" |
| 16 |
#include "callback.h" |
| 17 |
#include "delegation.h" |
| 18 |
#include "nfs4session.h" |
| 19 |
#include "nfs4idmap.h" |
| 20 |
#include "pnfs.h" |
| 21 |
#include "netns.h" |
| 22 |
#include "sysfs.h" |
| 23 |
|
| 24 |
#define NFSDBG_FACILITY NFSDBG_CLIENT |
| 25 |
|
| 26 |
/* |
| 27 |
* Get a unique NFSv4.0 callback identifier which will be used |
| 28 |
* by the V4.0 callback service to lookup the nfs_client struct |
| 29 |
*/ |
| 30 |
static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion) |
| 31 |
{ |
| 32 |
int ret = 0; |
| 33 |
struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); |
| 34 |
|
| 35 |
if (clp->rpc_ops->version != 4 || minorversion != 0) |
| 36 |
return ret; |
| 37 |
idr_preload(GFP_KERNEL); |
| 38 |
spin_lock(&nn->nfs_client_lock); |
| 39 |
ret = idr_alloc(&nn->cb_ident_idr, clp, 1, 0, GFP_NOWAIT); |
| 40 |
if (ret >= 0) |
| 41 |
clp->cl_cb_ident = ret; |
| 42 |
spin_unlock(&nn->nfs_client_lock); |
| 43 |
idr_preload_end(); |
| 44 |
return ret < 0 ? ret : 0; |
| 45 |
} |
| 46 |
|
| 47 |
/* |
| 48 |
* Per auth flavor data server rpc clients |
| 49 |
*/ |
| 50 |
struct nfs4_ds_server { |
| 51 |
struct list_head list; /* ds_clp->cl_ds_clients */ |
| 52 |
struct rpc_clnt *rpc_clnt; |
| 53 |
}; |
| 54 |
|
| 55 |
/** |
| 56 |
* nfs4_find_ds_client - Common lookup case for DS I/O |
| 57 |
* @ds_clp: pointer to the DS's nfs_client |
| 58 |
* @flavor: rpc auth flavour to match |
| 59 |
*/ |
| 60 |
static struct nfs4_ds_server * |
| 61 |
nfs4_find_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor) |
| 62 |
{ |
| 63 |
struct nfs4_ds_server *dss; |
| 64 |
|
| 65 |
rcu_read_lock(); |
| 66 |
list_for_each_entry_rcu(dss, &ds_clp->cl_ds_clients, list) { |
| 67 |
if (dss->rpc_clnt->cl_auth->au_flavor != flavor) |
| 68 |
continue; |
| 69 |
goto out; |
| 70 |
} |
| 71 |
dss = NULL; |
| 72 |
out: |
| 73 |
rcu_read_unlock(); |
| 74 |
return dss; |
| 75 |
} |
| 76 |
|
| 77 |
static struct nfs4_ds_server * |
| 78 |
nfs4_add_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor, |
| 79 |
struct nfs4_ds_server *new) |
| 80 |
{ |
| 81 |
struct nfs4_ds_server *dss; |
| 82 |
|
| 83 |
spin_lock(&ds_clp->cl_lock); |
| 84 |
list_for_each_entry(dss, &ds_clp->cl_ds_clients, list) { |
| 85 |
if (dss->rpc_clnt->cl_auth->au_flavor != flavor) |
| 86 |
continue; |
| 87 |
goto out; |
| 88 |
} |
| 89 |
if (new) |
| 90 |
list_add_rcu(&new->list, &ds_clp->cl_ds_clients); |
| 91 |
dss = new; |
| 92 |
out: |
| 93 |
spin_unlock(&ds_clp->cl_lock); /* need some lock to protect list */ |
| 94 |
return dss; |
| 95 |
} |
| 96 |
|
| 97 |
static struct nfs4_ds_server * |
| 98 |
nfs4_alloc_ds_server(struct nfs_client *ds_clp, rpc_authflavor_t flavor) |
| 99 |
{ |
| 100 |
struct nfs4_ds_server *dss; |
| 101 |
|
| 102 |
dss = kmalloc_obj(*dss, GFP_NOFS); |
| 103 |
if (dss == NULL) |
| 104 |
return ERR_PTR(-ENOMEM); |
| 105 |
|
| 106 |
dss->rpc_clnt = rpc_clone_client_set_auth(ds_clp->cl_rpcclient, flavor); |
| 107 |
if (IS_ERR(dss->rpc_clnt)) { |
| 108 |
int err = PTR_ERR(dss->rpc_clnt); |
| 109 |
kfree (dss); |
| 110 |
return ERR_PTR(err); |
| 111 |
} |
| 112 |
INIT_LIST_HEAD(&dss->list); |
| 113 |
|
| 114 |
return dss; |
| 115 |
} |
| 116 |
|
| 117 |
static void |
| 118 |
nfs4_free_ds_server(struct nfs4_ds_server *dss) |
| 119 |
{ |
| 120 |
rpc_release_client(dss->rpc_clnt); |
| 121 |
kfree(dss); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* nfs4_find_or_create_ds_client - Find or create a DS rpc client |
| 126 |
* @ds_clp: pointer to the DS's nfs_client |
| 127 |
* @inode: pointer to the inode |
| 128 |
* |
| 129 |
* Find or create a DS rpc client with th MDS server rpc client auth flavor |
| 130 |
* in the nfs_client cl_ds_clients list. |
| 131 |
*/ |
| 132 |
struct rpc_clnt * |
| 133 |
nfs4_find_or_create_ds_client(struct nfs_client *ds_clp, struct inode *inode) |
| 134 |
{ |
| 135 |
struct nfs4_ds_server *dss, *new; |
| 136 |
rpc_authflavor_t flavor = NFS_SERVER(inode)->client->cl_auth->au_flavor; |
| 137 |
|
| 138 |
dss = nfs4_find_ds_client(ds_clp, flavor); |
| 139 |
if (dss != NULL) |
| 140 |
goto out; |
| 141 |
new = nfs4_alloc_ds_server(ds_clp, flavor); |
| 142 |
if (IS_ERR(new)) |
| 143 |
return ERR_CAST(new); |
| 144 |
dss = nfs4_add_ds_client(ds_clp, flavor, new); |
| 145 |
if (dss != new) |
| 146 |
nfs4_free_ds_server(new); |
| 147 |
out: |
| 148 |
return dss->rpc_clnt; |
| 149 |
} |
| 150 |
EXPORT_SYMBOL_GPL(nfs4_find_or_create_ds_client); |
| 151 |
|
| 152 |
static void |
| 153 |
nfs4_shutdown_ds_clients(struct nfs_client *clp) |
| 154 |
{ |
| 155 |
struct nfs4_ds_server *dss; |
| 156 |
|
| 157 |
while (!list_empty(&clp->cl_ds_clients)) { |
| 158 |
dss = list_entry(clp->cl_ds_clients.next, |
| 159 |
struct nfs4_ds_server, list); |
| 160 |
list_del(&dss->list); |
| 161 |
rpc_shutdown_client(dss->rpc_clnt); |
| 162 |
kfree (dss); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
static void |
| 167 |
nfs4_cleanup_callback(struct nfs_client *clp) |
| 168 |
{ |
| 169 |
struct nfs4_copy_state *cp_state; |
| 170 |
|
| 171 |
while (!list_empty(&clp->pending_cb_stateids)) { |
| 172 |
cp_state = list_entry(clp->pending_cb_stateids.next, |
| 173 |
struct nfs4_copy_state, copies); |
| 174 |
list_del(&cp_state->copies); |
| 175 |
kfree(cp_state); |
| 176 |
} |
| 177 |
} |
| 178 |
|
| 179 |
void nfs41_shutdown_client(struct nfs_client *clp) |
| 180 |
{ |
| 181 |
if (nfs4_has_session(clp)) { |
| 182 |
nfs4_cleanup_callback(clp); |
| 183 |
nfs4_shutdown_ds_clients(clp); |
| 184 |
nfs4_destroy_session(clp->cl_session); |
| 185 |
nfs4_destroy_clientid(clp); |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init) |
| 191 |
{ |
| 192 |
char buf[INET6_ADDRSTRLEN + 1]; |
| 193 |
const char *ip_addr = cl_init->ip_addr; |
| 194 |
struct nfs_client *clp = nfs_alloc_client(cl_init); |
| 195 |
int err; |
| 196 |
|
| 197 |
if (IS_ERR(clp)) |
| 198 |
return clp; |
| 199 |
|
| 200 |
err = nfs_get_cb_ident_idr(clp, cl_init->minorversion); |
| 201 |
if (err) |
| 202 |
goto error; |
| 203 |
|
| 204 |
if (cl_init->minorversion < NFS4_MIN_MINOR_VERSION || |
| 205 |
cl_init->minorversion > NFS4_MAX_MINOR_VERSION) { |
| 206 |
err = -EINVAL; |
| 207 |
goto error; |
| 208 |
} |
| 209 |
|
| 210 |
spin_lock_init(&clp->cl_lock); |
| 211 |
INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state); |
| 212 |
INIT_LIST_HEAD(&clp->cl_ds_clients); |
| 213 |
rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client"); |
| 214 |
clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED; |
| 215 |
clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion]; |
| 216 |
clp->cl_mig_gen = 1; |
| 217 |
clp->cl_last_renewal = jiffies; |
| 218 |
init_waitqueue_head(&clp->cl_lock_waitq); |
| 219 |
INIT_LIST_HEAD(&clp->pending_cb_stateids); |
| 220 |
|
| 221 |
if (cl_init->minorversion != 0) |
| 222 |
__set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags); |
| 223 |
__set_bit(NFS_CS_DISCRTRY, &clp->cl_flags); |
| 224 |
__set_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags); |
| 225 |
if (test_bit(NFS_CS_PNFS, &cl_init->init_flags)) |
| 226 |
__set_bit(NFS_CS_PNFS, &clp->cl_flags); |
| 227 |
if (test_bit(NFS_CS_NETUNREACH_FATAL, &cl_init->init_flags)) |
| 228 |
__set_bit(NFS_CS_NETUNREACH_FATAL, &clp->cl_flags); |
| 229 |
/* |
| 230 |
* Set up the connection to the server before we add add to the |
| 231 |
* global list. |
| 232 |
*/ |
| 233 |
err = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_GSS_KRB5I); |
| 234 |
if (err == -EINVAL) |
| 235 |
err = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX); |
| 236 |
if (err < 0) |
| 237 |
goto error; |
| 238 |
|
| 239 |
/* If no clientaddr= option was specified, find a usable cb address */ |
| 240 |
if (ip_addr == NULL) { |
| 241 |
struct sockaddr_storage cb_addr; |
| 242 |
struct sockaddr *sap = (struct sockaddr *)&cb_addr; |
| 243 |
|
| 244 |
err = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr)); |
| 245 |
if (err < 0) |
| 246 |
goto error; |
| 247 |
err = rpc_ntop(sap, buf, sizeof(buf)); |
| 248 |
if (err < 0) |
| 249 |
goto error; |
| 250 |
ip_addr = (const char *)buf; |
| 251 |
} |
| 252 |
strscpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr)); |
| 253 |
|
| 254 |
err = nfs_idmap_new(clp); |
| 255 |
if (err < 0) { |
| 256 |
dprintk("%s: failed to create idmapper. Error = %d\n", |
| 257 |
__func__, err); |
| 258 |
goto error; |
| 259 |
} |
| 260 |
__set_bit(NFS_CS_IDMAP, &clp->cl_res_state); |
| 261 |
return clp; |
| 262 |
|
| 263 |
error: |
| 264 |
nfs_free_client(clp); |
| 265 |
return ERR_PTR(err); |
| 266 |
} |
| 267 |
|
| 268 |
/* |
| 269 |
* Destroy the NFS4 callback service |
| 270 |
*/ |
| 271 |
static void nfs4_destroy_callback(struct nfs_client *clp) |
| 272 |
{ |
| 273 |
if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state)) { |
| 274 |
struct rpc_xprt *xprt; |
| 275 |
|
| 276 |
xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt); |
| 277 |
nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net, |
| 278 |
xprt); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
static void nfs4_shutdown_client(struct nfs_client *clp) |
| 283 |
{ |
| 284 |
if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state)) |
| 285 |
nfs4_kill_renewd(clp); |
| 286 |
clp->cl_mvops->shutdown_client(clp); |
| 287 |
nfs4_destroy_callback(clp); |
| 288 |
if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state)) |
| 289 |
nfs_idmap_delete(clp); |
| 290 |
|
| 291 |
rpc_destroy_wait_queue(&clp->cl_rpcwaitq); |
| 292 |
kfree(clp->cl_serverowner); |
| 293 |
kfree(clp->cl_serverscope); |
| 294 |
kfree(clp->cl_implid); |
| 295 |
kfree(clp->cl_owner_id); |
| 296 |
} |
| 297 |
|
| 298 |
void nfs4_free_client(struct nfs_client *clp) |
| 299 |
{ |
| 300 |
nfs4_shutdown_client(clp); |
| 301 |
nfs_free_client(clp); |
| 302 |
} |
| 303 |
|
| 304 |
/* |
| 305 |
* Initialize the NFS4 callback service |
| 306 |
*/ |
| 307 |
static int nfs4_init_callback(struct nfs_client *clp) |
| 308 |
{ |
| 309 |
struct rpc_xprt *xprt; |
| 310 |
int error; |
| 311 |
|
| 312 |
xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt); |
| 313 |
|
| 314 |
if (nfs4_has_session(clp)) { |
| 315 |
error = xprt_setup_backchannel(xprt, NFS41_BC_MIN_CALLBACKS); |
| 316 |
if (error < 0) |
| 317 |
return error; |
| 318 |
} |
| 319 |
|
| 320 |
error = nfs_callback_up(clp->cl_mvops->minor_version, xprt); |
| 321 |
if (error < 0) { |
| 322 |
dprintk("%s: failed to start callback. Error = %d\n", |
| 323 |
__func__, error); |
| 324 |
return error; |
| 325 |
} |
| 326 |
__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state); |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
/** |
| 332 |
* nfs41_init_client - nfs_client initialization tasks for NFSv4.1+ |
| 333 |
* @clp: nfs_client to initialize |
| 334 |
* |
| 335 |
* Returns zero on success, or a negative errno if some error occurred. |
| 336 |
*/ |
| 337 |
int nfs41_init_client(struct nfs_client *clp) |
| 338 |
{ |
| 339 |
struct nfs4_session *session = NULL; |
| 340 |
|
| 341 |
/* |
| 342 |
* Create the session and mark it expired. |
| 343 |
* When a SEQUENCE operation encounters the expired session |
| 344 |
* it will do session recovery to initialize it. |
| 345 |
*/ |
| 346 |
session = nfs4_alloc_session(clp); |
| 347 |
if (!session) |
| 348 |
return -ENOMEM; |
| 349 |
|
| 350 |
clp->cl_session = session; |
| 351 |
|
| 352 |
/* |
| 353 |
* The create session reply races with the server back |
| 354 |
* channel probe. Mark the client NFS_CS_SESSION_INITING |
| 355 |
* so that the client back channel can find the |
| 356 |
* nfs_client struct |
| 357 |
*/ |
| 358 |
nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING); |
| 359 |
return 0; |
| 360 |
} |
| 361 |
|
| 362 |
/* |
| 363 |
* Initialize the minor version specific parts of an NFS4 client record |
| 364 |
*/ |
| 365 |
static int nfs4_init_client_minor_version(struct nfs_client *clp) |
| 366 |
{ |
| 367 |
int ret; |
| 368 |
|
| 369 |
ret = clp->cl_mvops->init_client(clp); |
| 370 |
if (ret) |
| 371 |
return ret; |
| 372 |
return nfs4_init_callback(clp); |
| 373 |
} |
| 374 |
|
| 375 |
static void nfs4_add_trunk(struct nfs_client *clp, struct nfs_client *old) |
| 376 |
{ |
| 377 |
struct sockaddr_storage clp_addr, old_addr; |
| 378 |
struct sockaddr *clp_sap = (struct sockaddr *)&clp_addr; |
| 379 |
struct sockaddr *old_sap = (struct sockaddr *)&old_addr; |
| 380 |
size_t clp_salen; |
| 381 |
struct xprt_create xprt_args = { |
| 382 |
.ident = old->cl_proto, |
| 383 |
.net = old->cl_net, |
| 384 |
.servername = old->cl_hostname, |
| 385 |
}; |
| 386 |
int max_connect = test_bit(NFS_CS_PNFS, &clp->cl_flags) ? |
| 387 |
clp->cl_max_connect : old->cl_max_connect; |
| 388 |
|
| 389 |
if (clp->cl_proto != old->cl_proto) |
| 390 |
return; |
| 391 |
clp_salen = rpc_peeraddr(clp->cl_rpcclient, clp_sap, sizeof(clp_addr)); |
| 392 |
rpc_peeraddr(old->cl_rpcclient, old_sap, sizeof(old_addr)); |
| 393 |
|
| 394 |
if (clp_addr.ss_family != old_addr.ss_family) |
| 395 |
return; |
| 396 |
|
| 397 |
xprt_args.dstaddr = clp_sap; |
| 398 |
xprt_args.addrlen = clp_salen; |
| 399 |
|
| 400 |
rpc_clnt_add_xprt(old->cl_rpcclient, &xprt_args, |
| 401 |
rpc_clnt_test_and_add_xprt, &max_connect); |
| 402 |
} |
| 403 |
|
| 404 |
/** |
| 405 |
* nfs4_init_client - Initialise an NFS4 client record |
| 406 |
* |
| 407 |
* @clp: nfs_client to initialise |
| 408 |
* @cl_init: pointer to nfs_client_initdata |
| 409 |
* |
| 410 |
* Returns pointer to an NFS client, or an ERR_PTR value. |
| 411 |
*/ |
| 412 |
struct nfs_client *nfs4_init_client(struct nfs_client *clp, |
| 413 |
const struct nfs_client_initdata *cl_init) |
| 414 |
{ |
| 415 |
struct nfs_client *old; |
| 416 |
int error; |
| 417 |
|
| 418 |
if (clp->cl_cons_state == NFS_CS_READY) |
| 419 |
/* the client is initialised already */ |
| 420 |
return clp; |
| 421 |
|
| 422 |
error = nfs4_init_client_minor_version(clp); |
| 423 |
if (error < 0) |
| 424 |
goto error; |
| 425 |
|
| 426 |
error = nfs4_discover_server_trunking(clp, &old); |
| 427 |
if (error < 0) |
| 428 |
goto error; |
| 429 |
|
| 430 |
if (clp != old) { |
| 431 |
clp->cl_preserve_clid = true; |
| 432 |
/* |
| 433 |
* Mark the client as having failed initialization so other |
| 434 |
* processes walking the nfs_client_list in nfs_match_client() |
| 435 |
* won't try to use it. |
| 436 |
*/ |
| 437 |
nfs_mark_client_ready(clp, -EPERM); |
| 438 |
if (old->cl_mvops->session_trunk) |
| 439 |
nfs4_add_trunk(clp, old); |
| 440 |
} |
| 441 |
clear_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags); |
| 442 |
nfs_put_client(clp); |
| 443 |
return old; |
| 444 |
|
| 445 |
error: |
| 446 |
nfs_mark_client_ready(clp, error); |
| 447 |
nfs_put_client(clp); |
| 448 |
return ERR_PTR(error); |
| 449 |
} |
| 450 |
|
| 451 |
static bool nfs4_match_client_owner_id(const struct nfs_client *clp1, |
| 452 |
const struct nfs_client *clp2) |
| 453 |
{ |
| 454 |
if (clp1->cl_owner_id == NULL || clp2->cl_owner_id == NULL) |
| 455 |
return true; |
| 456 |
return strcmp(clp1->cl_owner_id, clp2->cl_owner_id) == 0; |
| 457 |
} |
| 458 |
|
| 459 |
int nfs4_match_client(struct nfs_client *pos, struct nfs_client *new, |
| 460 |
struct nfs_client **prev, struct nfs_net *nn) |
| 461 |
{ |
| 462 |
int status; |
| 463 |
|
| 464 |
if (pos->rpc_ops != new->rpc_ops) |
| 465 |
return 1; |
| 466 |
|
| 467 |
if (pos->cl_minorversion != new->cl_minorversion) |
| 468 |
return 1; |
| 469 |
|
| 470 |
/* If "pos" isn't marked ready, we can't trust the |
| 471 |
* remaining fields in "pos", especially the client |
| 472 |
* ID and serverowner fields. Wait for CREATE_SESSION |
| 473 |
* to finish. */ |
| 474 |
if (pos->cl_cons_state > NFS_CS_READY) { |
| 475 |
refcount_inc(&pos->cl_count); |
| 476 |
spin_unlock(&nn->nfs_client_lock); |
| 477 |
|
| 478 |
nfs_put_client(*prev); |
| 479 |
*prev = pos; |
| 480 |
|
| 481 |
status = nfs_wait_client_init_complete(pos); |
| 482 |
spin_lock(&nn->nfs_client_lock); |
| 483 |
|
| 484 |
if (status < 0) |
| 485 |
return status; |
| 486 |
} |
| 487 |
|
| 488 |
if (pos->cl_cons_state != NFS_CS_READY) |
| 489 |
return 1; |
| 490 |
|
| 491 |
if (pos->cl_clientid != new->cl_clientid) |
| 492 |
return 1; |
| 493 |
|
| 494 |
/* NFSv4.1 always uses the uniform string, however someone |
| 495 |
* might switch the uniquifier string on us. |
| 496 |
*/ |
| 497 |
if (!nfs4_match_client_owner_id(pos, new)) |
| 498 |
return 1; |
| 499 |
|
| 500 |
return 0; |
| 501 |
} |
| 502 |
|
| 503 |
/* |
| 504 |
* Returns true if the server major ids match |
| 505 |
*/ |
| 506 |
bool |
| 507 |
nfs4_check_serverowner_major_id(struct nfs41_server_owner *o1, |
| 508 |
struct nfs41_server_owner *o2) |
| 509 |
{ |
| 510 |
if (o1->major_id_sz != o2->major_id_sz) |
| 511 |
return false; |
| 512 |
return memcmp(o1->major_id, o2->major_id, o1->major_id_sz) == 0; |
| 513 |
} |
| 514 |
|
| 515 |
/* |
| 516 |
* Returns true if the server scopes match |
| 517 |
*/ |
| 518 |
static bool |
| 519 |
nfs4_check_server_scope(struct nfs41_server_scope *s1, |
| 520 |
struct nfs41_server_scope *s2) |
| 521 |
{ |
| 522 |
if (s1->server_scope_sz != s2->server_scope_sz) |
| 523 |
return false; |
| 524 |
return memcmp(s1->server_scope, s2->server_scope, |
| 525 |
s1->server_scope_sz) == 0; |
| 526 |
} |
| 527 |
|
| 528 |
/** |
| 529 |
* nfs4_detect_session_trunking - Checks for session trunking. |
| 530 |
* @clp: original mount nfs_client |
| 531 |
* @res: result structure from an exchange_id using the original mount |
| 532 |
* nfs_client with a new multi_addr transport |
| 533 |
* @xprt: pointer to the transport to add. |
| 534 |
* |
| 535 |
* Called after a successful EXCHANGE_ID on a multi-addr connection. |
| 536 |
* Upon success, add the transport. |
| 537 |
* |
| 538 |
* Returns zero on success, otherwise -EINVAL |
| 539 |
* |
| 540 |
* Note: since the exchange_id for the new multi_addr transport uses the |
| 541 |
* same nfs_client from the original mount, the cl_owner_id is reused, |
| 542 |
* so eir_clientowner is the same. |
| 543 |
*/ |
| 544 |
int nfs4_detect_session_trunking(struct nfs_client *clp, |
| 545 |
struct nfs41_exchange_id_res *res, |
| 546 |
struct rpc_xprt *xprt) |
| 547 |
{ |
| 548 |
/* Check eir_clientid */ |
| 549 |
if (clp->cl_clientid != res->clientid) |
| 550 |
goto out_err; |
| 551 |
|
| 552 |
/* Check eir_server_owner so_major_id */ |
| 553 |
if (!nfs4_check_serverowner_major_id(clp->cl_serverowner, |
| 554 |
res->server_owner)) |
| 555 |
goto out_err; |
| 556 |
|
| 557 |
/* Check eir_server_owner so_minor_id */ |
| 558 |
if (clp->cl_serverowner->minor_id != res->server_owner->minor_id) |
| 559 |
goto out_err; |
| 560 |
|
| 561 |
/* Check eir_server_scope */ |
| 562 |
if (!nfs4_check_server_scope(clp->cl_serverscope, res->server_scope)) |
| 563 |
goto out_err; |
| 564 |
|
| 565 |
pr_info("NFS: %s: Session trunking succeeded for %s\n", |
| 566 |
clp->cl_hostname, |
| 567 |
xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 568 |
|
| 569 |
return 0; |
| 570 |
out_err: |
| 571 |
pr_info("NFS: %s: Session trunking failed for %s\n", clp->cl_hostname, |
| 572 |
xprt->address_strings[RPC_DISPLAY_ADDR]); |
| 573 |
|
| 574 |
return -EINVAL; |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* nfs41_walk_client_list - Find nfs_client that matches a client/server owner |
| 579 |
* |
| 580 |
* @new: nfs_client with client ID to test |
| 581 |
* @result: OUT: found nfs_client, or new |
| 582 |
* @cred: credential to use for trunking test |
| 583 |
* |
| 584 |
* Returns zero, a negative errno, or a negative NFS4ERR status. |
| 585 |
* If zero is returned, an nfs_client pointer is planted in "result." |
| 586 |
* |
| 587 |
* NB: nfs41_walk_client_list() relies on the new nfs_client being |
| 588 |
* the last nfs_client on the list. |
| 589 |
*/ |
| 590 |
int nfs41_walk_client_list(struct nfs_client *new, |
| 591 |
struct nfs_client **result, |
| 592 |
const struct cred *cred) |
| 593 |
{ |
| 594 |
struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id); |
| 595 |
struct nfs_client *pos, *prev = NULL; |
| 596 |
int status = -NFS4ERR_STALE_CLIENTID; |
| 597 |
|
| 598 |
spin_lock(&nn->nfs_client_lock); |
| 599 |
list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) { |
| 600 |
|
| 601 |
if (pos == new) |
| 602 |
goto found; |
| 603 |
|
| 604 |
status = nfs4_match_client(pos, new, &prev, nn); |
| 605 |
if (status < 0) |
| 606 |
goto out; |
| 607 |
if (status != 0) |
| 608 |
continue; |
| 609 |
|
| 610 |
/* |
| 611 |
* Note that session trunking is just a special subcase of |
| 612 |
* client id trunking. In either case, we want to fall back |
| 613 |
* to using the existing nfs_client. |
| 614 |
*/ |
| 615 |
if (!nfs4_check_serverowner_major_id(pos->cl_serverowner, |
| 616 |
new->cl_serverowner)) |
| 617 |
continue; |
| 618 |
|
| 619 |
found: |
| 620 |
refcount_inc(&pos->cl_count); |
| 621 |
*result = pos; |
| 622 |
status = 0; |
| 623 |
break; |
| 624 |
} |
| 625 |
|
| 626 |
out: |
| 627 |
spin_unlock(&nn->nfs_client_lock); |
| 628 |
nfs_put_client(prev); |
| 629 |
return status; |
| 630 |
} |
| 631 |
|
| 632 |
static void nfs4_destroy_server(struct nfs_server *server) |
| 633 |
{ |
| 634 |
LIST_HEAD(freeme); |
| 635 |
|
| 636 |
nfs_server_return_all_delegations(server); |
| 637 |
unset_pnfs_layoutdriver(server); |
| 638 |
nfs4_purge_state_owners(server, &freeme); |
| 639 |
nfs4_free_state_owners(&freeme); |
| 640 |
kfree(server->delegation_hash_table); |
| 641 |
} |
| 642 |
|
| 643 |
/* |
| 644 |
* NFSv4.0 callback thread helper |
| 645 |
* |
| 646 |
* Find a client by callback identifier |
| 647 |
*/ |
| 648 |
struct nfs_client * |
| 649 |
nfs4_find_client_ident(struct net *net, int cb_ident) |
| 650 |
{ |
| 651 |
struct nfs_client *clp; |
| 652 |
struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 653 |
|
| 654 |
spin_lock(&nn->nfs_client_lock); |
| 655 |
clp = idr_find(&nn->cb_ident_idr, cb_ident); |
| 656 |
if (clp) |
| 657 |
refcount_inc(&clp->cl_count); |
| 658 |
spin_unlock(&nn->nfs_client_lock); |
| 659 |
return clp; |
| 660 |
} |
| 661 |
|
| 662 |
/* Common match routine for v4.0 and v4.1 callback services */ |
| 663 |
static bool nfs4_cb_match_client(const struct sockaddr *addr, |
| 664 |
struct nfs_client *clp, u32 minorversion) |
| 665 |
{ |
| 666 |
struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; |
| 667 |
|
| 668 |
/* Don't match clients that failed to initialise */ |
| 669 |
if (!(clp->cl_cons_state == NFS_CS_READY || |
| 670 |
clp->cl_cons_state == NFS_CS_SESSION_INITING)) |
| 671 |
return false; |
| 672 |
|
| 673 |
smp_rmb(); |
| 674 |
|
| 675 |
/* Match the version and minorversion */ |
| 676 |
if (clp->rpc_ops->version != 4 || |
| 677 |
clp->cl_minorversion != minorversion) |
| 678 |
return false; |
| 679 |
|
| 680 |
/* Match only the IP address, not the port number */ |
| 681 |
return rpc_cmp_addr(addr, clap); |
| 682 |
} |
| 683 |
|
| 684 |
/* |
| 685 |
* NFSv4.1 callback thread helper |
| 686 |
* For CB_COMPOUND calls, find a client by IP address, protocol version, |
| 687 |
* minorversion, and sessionID |
| 688 |
* |
| 689 |
* Returns NULL if no such client |
| 690 |
*/ |
| 691 |
struct nfs_client * |
| 692 |
nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr, |
| 693 |
struct nfs4_sessionid *sid, u32 minorversion) |
| 694 |
{ |
| 695 |
struct nfs_client *clp; |
| 696 |
struct nfs_net *nn = net_generic(net, nfs_net_id); |
| 697 |
|
| 698 |
spin_lock(&nn->nfs_client_lock); |
| 699 |
list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { |
| 700 |
if (!nfs4_cb_match_client(addr, clp, minorversion)) |
| 701 |
continue; |
| 702 |
|
| 703 |
if (!nfs4_has_session(clp)) |
| 704 |
continue; |
| 705 |
|
| 706 |
/* Match sessionid*/ |
| 707 |
if (memcmp(clp->cl_session->sess_id.data, |
| 708 |
sid->data, NFS4_MAX_SESSIONID_LEN) != 0) |
| 709 |
continue; |
| 710 |
|
| 711 |
refcount_inc(&clp->cl_count); |
| 712 |
spin_unlock(&nn->nfs_client_lock); |
| 713 |
return clp; |
| 714 |
} |
| 715 |
spin_unlock(&nn->nfs_client_lock); |
| 716 |
return NULL; |
| 717 |
} |
| 718 |
|
| 719 |
/* |
| 720 |
* Set up an NFS4 client |
| 721 |
*/ |
| 722 |
static int nfs4_set_client(struct nfs_server *server, |
| 723 |
struct nfs_client_initdata *cl_init) |
| 724 |
{ |
| 725 |
struct nfs_client *clp; |
| 726 |
|
| 727 |
cl_init->nfs_mod = &nfs_v4; |
| 728 |
cl_init->cred = server->cred; |
| 729 |
|
| 730 |
if (cl_init->minorversion == 0) { |
| 731 |
__set_bit(NFS_CS_REUSEPORT, &cl_init->init_flags); |
| 732 |
cl_init->max_connect = 0; |
| 733 |
} |
| 734 |
|
| 735 |
switch (cl_init->proto) { |
| 736 |
case XPRT_TRANSPORT_RDMA: |
| 737 |
case XPRT_TRANSPORT_TCP: |
| 738 |
case XPRT_TRANSPORT_TCP_TLS: |
| 739 |
break; |
| 740 |
default: |
| 741 |
cl_init->nconnect = 0; |
| 742 |
} |
| 743 |
|
| 744 |
if (server->flags & NFS_MOUNT_NORESVPORT) |
| 745 |
__set_bit(NFS_CS_NORESVPORT, &cl_init->init_flags); |
| 746 |
if (server->options & NFS_OPTION_MIGRATION) |
| 747 |
__set_bit(NFS_CS_MIGRATION, &cl_init->init_flags); |
| 748 |
if (test_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status)) |
| 749 |
__set_bit(NFS_CS_TSM_POSSIBLE, &cl_init->init_flags); |
| 750 |
server->port = rpc_get_port((struct sockaddr *)cl_init->addr); |
| 751 |
|
| 752 |
if (server->flags & NFS_MOUNT_NETUNREACH_FATAL) |
| 753 |
__set_bit(NFS_CS_NETUNREACH_FATAL, &cl_init->init_flags); |
| 754 |
|
| 755 |
/* Allocate or find a client reference we can use */ |
| 756 |
clp = nfs_get_client(cl_init); |
| 757 |
if (IS_ERR(clp)) |
| 758 |
return PTR_ERR(clp); |
| 759 |
|
| 760 |
if (server->nfs_client == clp) { |
| 761 |
nfs_put_client(clp); |
| 762 |
return -ELOOP; |
| 763 |
} |
| 764 |
|
| 765 |
/* |
| 766 |
* Query for the lease time on clientid setup or renewal |
| 767 |
* |
| 768 |
* Note that this will be set on nfs_clients that were created |
| 769 |
* only for the DS role and did not set this bit, but now will |
| 770 |
* serve a dual role. |
| 771 |
*/ |
| 772 |
set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state); |
| 773 |
|
| 774 |
server->nfs_client = clp; |
| 775 |
nfs_sysfs_add_server(server); |
| 776 |
nfs_sysfs_link_rpc_client(server, clp->cl_rpcclient, "_state"); |
| 777 |
|
| 778 |
return 0; |
| 779 |
} |
| 780 |
|
| 781 |
/* |
| 782 |
* Set up a pNFS Data Server client. |
| 783 |
* |
| 784 |
* Return any existing nfs_client that matches server address,port,version |
| 785 |
* and minorversion. |
| 786 |
* |
| 787 |
* For a new nfs_client, use a soft mount (default), a low retrans and a |
| 788 |
* low timeout interval so that if a connection is lost, we retry through |
| 789 |
* the MDS. |
| 790 |
*/ |
| 791 |
struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv, |
| 792 |
const struct sockaddr_storage *ds_addr, int ds_addrlen, |
| 793 |
int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans, |
| 794 |
u32 minor_version) |
| 795 |
{ |
| 796 |
struct rpc_timeout ds_timeout; |
| 797 |
struct nfs_client *mds_clp = mds_srv->nfs_client; |
| 798 |
struct nfs_client_initdata cl_init = { |
| 799 |
.addr = ds_addr, |
| 800 |
.addrlen = ds_addrlen, |
| 801 |
.nodename = mds_clp->cl_rpcclient->cl_nodename, |
| 802 |
.ip_addr = mds_clp->cl_ipaddr, |
| 803 |
.nfs_mod = &nfs_v4, |
| 804 |
.proto = ds_proto, |
| 805 |
.minorversion = minor_version, |
| 806 |
.net = mds_clp->cl_net, |
| 807 |
.timeparms = &ds_timeout, |
| 808 |
.cred = mds_srv->cred, |
| 809 |
.xprtsec = { |
| 810 |
.policy = RPC_XPRTSEC_NONE, |
| 811 |
.cert_serial = TLS_NO_CERT, |
| 812 |
.privkey_serial = TLS_NO_PRIVKEY, |
| 813 |
}, |
| 814 |
}; |
| 815 |
char buf[INET6_ADDRSTRLEN + 1]; |
| 816 |
|
| 817 |
if (rpc_ntop((struct sockaddr *)ds_addr, buf, sizeof(buf)) <= 0) |
| 818 |
return ERR_PTR(-EINVAL); |
| 819 |
cl_init.hostname = buf; |
| 820 |
|
| 821 |
switch (ds_proto) { |
| 822 |
case XPRT_TRANSPORT_TCP_TLS: |
| 823 |
if (mds_srv->nfs_client->cl_xprtsec.policy != RPC_XPRTSEC_NONE) |
| 824 |
cl_init.xprtsec = mds_srv->nfs_client->cl_xprtsec; |
| 825 |
else |
| 826 |
ds_proto = XPRT_TRANSPORT_TCP; |
| 827 |
fallthrough; |
| 828 |
case XPRT_TRANSPORT_RDMA: |
| 829 |
case XPRT_TRANSPORT_TCP: |
| 830 |
if (mds_clp->cl_nconnect > 1) { |
| 831 |
cl_init.nconnect = mds_clp->cl_nconnect; |
| 832 |
cl_init.max_connect = NFS_MAX_TRANSPORTS; |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
if (mds_srv->flags & NFS_MOUNT_NORESVPORT) |
| 837 |
__set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); |
| 838 |
if (test_bit(NFS_CS_NETUNREACH_FATAL, &mds_clp->cl_flags)) |
| 839 |
__set_bit(NFS_CS_NETUNREACH_FATAL, &cl_init.init_flags); |
| 840 |
|
| 841 |
__set_bit(NFS_CS_PNFS, &cl_init.init_flags); |
| 842 |
cl_init.max_connect = NFS_MAX_TRANSPORTS; |
| 843 |
/* |
| 844 |
* Set an authflavor equual to the MDS value. Use the MDS nfs_client |
| 845 |
* cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS |
| 846 |
* (section 13.1 RFC 5661). |
| 847 |
*/ |
| 848 |
nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans); |
| 849 |
return nfs_get_client(&cl_init); |
| 850 |
} |
| 851 |
EXPORT_SYMBOL_GPL(nfs4_set_ds_client); |
| 852 |
|
| 853 |
/* |
| 854 |
* Session has been established, and the client marked ready. |
| 855 |
* Limit the mount rsize, wsize and dtsize using negotiated fore |
| 856 |
* channel attributes. |
| 857 |
*/ |
| 858 |
void nfs4_session_limit_rwsize(struct nfs_server *server) |
| 859 |
{ |
| 860 |
struct nfs4_session *sess; |
| 861 |
u32 server_resp_sz; |
| 862 |
u32 server_rqst_sz; |
| 863 |
|
| 864 |
if (!nfs4_has_session(server->nfs_client)) |
| 865 |
return; |
| 866 |
sess = server->nfs_client->cl_session; |
| 867 |
server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead; |
| 868 |
server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead; |
| 869 |
|
| 870 |
if (server->dtsize > server_resp_sz) |
| 871 |
server->dtsize = server_resp_sz; |
| 872 |
if (server->rsize > server_resp_sz) |
| 873 |
server->rsize = server_resp_sz; |
| 874 |
if (server->wsize > server_rqst_sz) |
| 875 |
server->wsize = server_rqst_sz; |
| 876 |
} |
| 877 |
|
| 878 |
/* |
| 879 |
* Limit xattr sizes using the channel attributes. |
| 880 |
*/ |
| 881 |
void nfs4_session_limit_xasize(struct nfs_server *server) |
| 882 |
{ |
| 883 |
#ifdef CONFIG_NFS_V4_2 |
| 884 |
struct nfs4_session *sess; |
| 885 |
u32 server_gxa_sz; |
| 886 |
u32 server_sxa_sz; |
| 887 |
u32 server_lxa_sz; |
| 888 |
|
| 889 |
if (!nfs4_has_session(server->nfs_client)) |
| 890 |
return; |
| 891 |
|
| 892 |
sess = server->nfs_client->cl_session; |
| 893 |
|
| 894 |
server_gxa_sz = sess->fc_attrs.max_resp_sz - nfs42_maxgetxattr_overhead; |
| 895 |
server_sxa_sz = sess->fc_attrs.max_rqst_sz - nfs42_maxsetxattr_overhead; |
| 896 |
server_lxa_sz = sess->fc_attrs.max_resp_sz - |
| 897 |
nfs42_maxlistxattrs_overhead; |
| 898 |
|
| 899 |
if (server->gxasize > server_gxa_sz) |
| 900 |
server->gxasize = server_gxa_sz; |
| 901 |
if (server->sxasize > server_sxa_sz) |
| 902 |
server->sxasize = server_sxa_sz; |
| 903 |
if (server->lxasize > server_lxa_sz) |
| 904 |
server->lxasize = server_lxa_sz; |
| 905 |
#endif |
| 906 |
} |
| 907 |
|
| 908 |
static int nfs4_server_common_setup(struct nfs_server *server, |
| 909 |
struct nfs_fh *mntfh, bool auth_probe) |
| 910 |
{ |
| 911 |
int error; |
| 912 |
|
| 913 |
error = nfs4_delegation_hash_alloc(server); |
| 914 |
if (error) |
| 915 |
return error; |
| 916 |
|
| 917 |
/* data servers support only a subset of NFSv4.1 */ |
| 918 |
if (is_ds_only_client(server->nfs_client)) |
| 919 |
return -EPROTONOSUPPORT; |
| 920 |
|
| 921 |
/* We must ensure the session is initialised first */ |
| 922 |
error = nfs4_init_session(server->nfs_client); |
| 923 |
if (error < 0) |
| 924 |
return error; |
| 925 |
|
| 926 |
nfs_server_set_init_caps(server); |
| 927 |
|
| 928 |
/* Probe the root fh to retrieve its FSID and filehandle */ |
| 929 |
error = nfs4_get_rootfh(server, mntfh, auth_probe); |
| 930 |
if (error < 0) |
| 931 |
return error; |
| 932 |
|
| 933 |
dprintk("Server FSID: %llx:%llx\n", |
| 934 |
(unsigned long long) server->fsid.major, |
| 935 |
(unsigned long long) server->fsid.minor); |
| 936 |
nfs_display_fhandle(mntfh, "Pseudo-fs root FH"); |
| 937 |
|
| 938 |
error = nfs_probe_server(server, mntfh); |
| 939 |
if (error < 0) |
| 940 |
return error; |
| 941 |
|
| 942 |
nfs4_session_limit_rwsize(server); |
| 943 |
nfs4_session_limit_xasize(server); |
| 944 |
|
| 945 |
if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) |
| 946 |
server->namelen = NFS4_MAXNAMLEN; |
| 947 |
|
| 948 |
nfs_server_insert_lists(server); |
| 949 |
server->mount_time = jiffies; |
| 950 |
server->destroy = nfs4_destroy_server; |
| 951 |
return 0; |
| 952 |
} |
| 953 |
|
| 954 |
/* |
| 955 |
* Create a version 4 volume record |
| 956 |
*/ |
| 957 |
static int nfs4_init_server(struct nfs_server *server, struct fs_context *fc) |
| 958 |
{ |
| 959 |
struct nfs_fs_context *ctx = nfs_fc2context(fc); |
| 960 |
struct rpc_timeout timeparms; |
| 961 |
struct nfs_client_initdata cl_init = { |
| 962 |
.hostname = ctx->nfs_server.hostname, |
| 963 |
.addr = &ctx->nfs_server._address, |
| 964 |
.addrlen = ctx->nfs_server.addrlen, |
| 965 |
.ip_addr = ctx->client_address, |
| 966 |
.proto = ctx->nfs_server.protocol, |
| 967 |
.minorversion = ctx->minorversion, |
| 968 |
.net = fc->net_ns, |
| 969 |
.timeparms = &timeparms, |
| 970 |
.xprtsec = ctx->xprtsec, |
| 971 |
.nconnect = ctx->nfs_server.nconnect, |
| 972 |
.max_connect = ctx->nfs_server.max_connect, |
| 973 |
}; |
| 974 |
int error; |
| 975 |
|
| 976 |
nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol, |
| 977 |
ctx->timeo, ctx->retrans); |
| 978 |
|
| 979 |
/* Initialise the client representation from the mount data */ |
| 980 |
server->flags = ctx->flags; |
| 981 |
server->options = ctx->options; |
| 982 |
server->auth_info = ctx->auth_info; |
| 983 |
|
| 984 |
/* Use the first specified auth flavor. If this flavor isn't |
| 985 |
* allowed by the server, use the SECINFO path to try the |
| 986 |
* other specified flavors */ |
| 987 |
if (ctx->auth_info.flavor_len >= 1) |
| 988 |
ctx->selected_flavor = ctx->auth_info.flavors[0]; |
| 989 |
else |
| 990 |
ctx->selected_flavor = RPC_AUTH_UNIX; |
| 991 |
|
| 992 |
/* Get a client record */ |
| 993 |
error = nfs4_set_client(server, &cl_init); |
| 994 |
if (error < 0) |
| 995 |
return error; |
| 996 |
|
| 997 |
if (ctx->bsize) { |
| 998 |
server->bsize = ctx->bsize; |
| 999 |
server->automount_inherit |= NFS_AUTOMOUNT_INHERIT_BSIZE; |
| 1000 |
} |
| 1001 |
if (ctx->rsize) { |
| 1002 |
server->rsize = |
| 1003 |
nfs_io_size(ctx->rsize, server->nfs_client->cl_proto); |
| 1004 |
server->automount_inherit |= NFS_AUTOMOUNT_INHERIT_RSIZE; |
| 1005 |
} |
| 1006 |
if (ctx->wsize) { |
| 1007 |
server->wsize = |
| 1008 |
nfs_io_size(ctx->wsize, server->nfs_client->cl_proto); |
| 1009 |
server->automount_inherit |= NFS_AUTOMOUNT_INHERIT_WSIZE; |
| 1010 |
} |
| 1011 |
|
| 1012 |
server->acregmin = ctx->acregmin * HZ; |
| 1013 |
server->acregmax = ctx->acregmax * HZ; |
| 1014 |
server->acdirmin = ctx->acdirmin * HZ; |
| 1015 |
server->acdirmax = ctx->acdirmax * HZ; |
| 1016 |
server->port = ctx->nfs_server.port; |
| 1017 |
|
| 1018 |
return nfs_init_server_rpcclient(server, &timeparms, |
| 1019 |
ctx->selected_flavor); |
| 1020 |
} |
| 1021 |
|
| 1022 |
/* |
| 1023 |
* Create a version 4 volume record |
| 1024 |
* - keyed on server and FSID |
| 1025 |
*/ |
| 1026 |
struct nfs_server *nfs4_create_server(struct fs_context *fc) |
| 1027 |
{ |
| 1028 |
struct nfs_fs_context *ctx = nfs_fc2context(fc); |
| 1029 |
struct nfs_server *server; |
| 1030 |
bool auth_probe; |
| 1031 |
int error; |
| 1032 |
|
| 1033 |
server = nfs_alloc_server(); |
| 1034 |
if (!server) |
| 1035 |
return ERR_PTR(-ENOMEM); |
| 1036 |
|
| 1037 |
server->cred = get_cred(fc->cred); |
| 1038 |
|
| 1039 |
auth_probe = ctx->auth_info.flavor_len < 1; |
| 1040 |
|
| 1041 |
/* set up the general RPC client */ |
| 1042 |
error = nfs4_init_server(server, fc); |
| 1043 |
if (error < 0) |
| 1044 |
goto error; |
| 1045 |
|
| 1046 |
error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe); |
| 1047 |
if (error < 0) |
| 1048 |
goto error; |
| 1049 |
|
| 1050 |
return server; |
| 1051 |
|
| 1052 |
error: |
| 1053 |
nfs_free_server(server); |
| 1054 |
return ERR_PTR(error); |
| 1055 |
} |
| 1056 |
|
| 1057 |
/* |
| 1058 |
* Create an NFS4 referral server record |
| 1059 |
*/ |
| 1060 |
struct nfs_server *nfs4_create_referral_server(struct fs_context *fc) |
| 1061 |
{ |
| 1062 |
struct nfs_fs_context *ctx = nfs_fc2context(fc); |
| 1063 |
struct nfs_server *parent_server = NFS_SB(ctx->clone_data.sb); |
| 1064 |
struct nfs_client *parent_client = parent_server->nfs_client; |
| 1065 |
struct nfs_client_initdata cl_init = { |
| 1066 |
.hostname = ctx->nfs_server.hostname, |
| 1067 |
.addr = &ctx->nfs_server._address, |
| 1068 |
.addrlen = ctx->nfs_server.addrlen, |
| 1069 |
.ip_addr = parent_client->cl_ipaddr, |
| 1070 |
.minorversion = parent_client->cl_mvops->minor_version, |
| 1071 |
.net = parent_client->cl_net, |
| 1072 |
.timeparms = parent_server->client->cl_timeout, |
| 1073 |
.xprtsec = parent_client->cl_xprtsec, |
| 1074 |
.nconnect = parent_client->cl_nconnect, |
| 1075 |
.max_connect = parent_client->cl_max_connect, |
| 1076 |
}; |
| 1077 |
struct nfs_server *server; |
| 1078 |
bool auth_probe; |
| 1079 |
int error; |
| 1080 |
|
| 1081 |
server = nfs_alloc_server(); |
| 1082 |
if (!server) |
| 1083 |
return ERR_PTR(-ENOMEM); |
| 1084 |
|
| 1085 |
server->cred = get_cred(parent_server->cred); |
| 1086 |
|
| 1087 |
/* Initialise the client representation from the parent server */ |
| 1088 |
nfs_server_copy_userdata(server, parent_server); |
| 1089 |
|
| 1090 |
/* Get a client representation */ |
| 1091 |
#if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA) |
| 1092 |
rpc_set_port(&ctx->nfs_server.address, NFS_RDMA_PORT); |
| 1093 |
cl_init.proto = XPRT_TRANSPORT_RDMA; |
| 1094 |
error = nfs4_set_client(server, &cl_init); |
| 1095 |
if (!error) |
| 1096 |
goto init_server; |
| 1097 |
#endif /* IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA) */ |
| 1098 |
|
| 1099 |
cl_init.proto = XPRT_TRANSPORT_TCP; |
| 1100 |
if (parent_client->cl_xprtsec.policy != RPC_XPRTSEC_NONE) |
| 1101 |
cl_init.proto = XPRT_TRANSPORT_TCP_TLS; |
| 1102 |
rpc_set_port(&ctx->nfs_server.address, NFS_PORT); |
| 1103 |
error = nfs4_set_client(server, &cl_init); |
| 1104 |
if (error < 0) |
| 1105 |
goto error; |
| 1106 |
|
| 1107 |
#if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA) |
| 1108 |
init_server: |
| 1109 |
#endif |
| 1110 |
error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, |
| 1111 |
ctx->selected_flavor); |
| 1112 |
if (error < 0) |
| 1113 |
goto error; |
| 1114 |
|
| 1115 |
auth_probe = parent_server->auth_info.flavor_len < 1; |
| 1116 |
|
| 1117 |
error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe); |
| 1118 |
if (error < 0) |
| 1119 |
goto error; |
| 1120 |
|
| 1121 |
return server; |
| 1122 |
|
| 1123 |
error: |
| 1124 |
nfs_free_server(server); |
| 1125 |
return ERR_PTR(error); |
| 1126 |
} |
| 1127 |
|
| 1128 |
/** |
| 1129 |
* nfs4_update_server - Move an nfs_server to a different nfs_client |
| 1130 |
* |
| 1131 |
* @server: represents FSID to be moved |
| 1132 |
* @hostname: new end-point's hostname |
| 1133 |
* @sap: new end-point's socket address |
| 1134 |
* @salen: size of "sap" |
| 1135 |
* @net: net namespace |
| 1136 |
* |
| 1137 |
* The nfs_server must be quiescent before this function is invoked. |
| 1138 |
* Either its session is drained (NFSv4.1+), or its transport is |
| 1139 |
* plugged and drained (NFSv4.0). |
| 1140 |
* |
| 1141 |
* Returns zero on success, or a negative errno value. |
| 1142 |
*/ |
| 1143 |
int nfs4_update_server(struct nfs_server *server, const char *hostname, |
| 1144 |
struct sockaddr_storage *sap, size_t salen, struct net *net) |
| 1145 |
{ |
| 1146 |
struct nfs_client *clp = server->nfs_client; |
| 1147 |
struct rpc_clnt *clnt = server->client; |
| 1148 |
struct xprt_create xargs = { |
| 1149 |
.ident = clp->cl_proto, |
| 1150 |
.net = net, |
| 1151 |
.dstaddr = (struct sockaddr *)sap, |
| 1152 |
.addrlen = salen, |
| 1153 |
.servername = hostname, |
| 1154 |
/* cel: bleh. We might need to pass TLS parameters here */ |
| 1155 |
}; |
| 1156 |
char buf[INET6_ADDRSTRLEN + 1]; |
| 1157 |
struct sockaddr_storage address; |
| 1158 |
struct sockaddr *localaddr = (struct sockaddr *)&address; |
| 1159 |
struct nfs_client_initdata cl_init = { |
| 1160 |
.hostname = hostname, |
| 1161 |
.addr = sap, |
| 1162 |
.addrlen = salen, |
| 1163 |
.ip_addr = buf, |
| 1164 |
.proto = clp->cl_proto, |
| 1165 |
.minorversion = clp->cl_minorversion, |
| 1166 |
.net = net, |
| 1167 |
.timeparms = clnt->cl_timeout, |
| 1168 |
.xprtsec = clp->cl_xprtsec, |
| 1169 |
.nconnect = clp->cl_nconnect, |
| 1170 |
.max_connect = clp->cl_max_connect, |
| 1171 |
}; |
| 1172 |
int error; |
| 1173 |
|
| 1174 |
error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout); |
| 1175 |
if (error != 0) |
| 1176 |
return error; |
| 1177 |
|
| 1178 |
error = rpc_localaddr(clnt, localaddr, sizeof(address)); |
| 1179 |
if (error != 0) |
| 1180 |
return error; |
| 1181 |
|
| 1182 |
if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0) |
| 1183 |
return -EAFNOSUPPORT; |
| 1184 |
|
| 1185 |
nfs_server_remove_lists(server); |
| 1186 |
set_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status); |
| 1187 |
error = nfs4_set_client(server, &cl_init); |
| 1188 |
clear_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status); |
| 1189 |
if (error != 0) { |
| 1190 |
nfs_server_insert_lists(server); |
| 1191 |
return error; |
| 1192 |
} |
| 1193 |
nfs_put_client(clp); |
| 1194 |
|
| 1195 |
if (server->nfs_client->cl_hostname == NULL) { |
| 1196 |
server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL); |
| 1197 |
if (server->nfs_client->cl_hostname == NULL) |
| 1198 |
return -ENOMEM; |
| 1199 |
} |
| 1200 |
nfs_server_insert_lists(server); |
| 1201 |
|
| 1202 |
return nfs_probe_server(server, NFS_FH(d_inode(server->super->s_root))); |
| 1203 |
} |