1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* client.c: NFS client sharing and management code 3 * 4 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved. 5 * Written by David Howells (dhowells@redhat.com) 6 */ 7 8 9 #include <linux/module.h> 10 #include <linux/init.h> 11 #include <linux/sched.h> 12 #include <linux/time.h> 13 #include <linux/kernel.h> 14 #include <linux/mm.h> 15 #include <linux/string.h> 16 #include <linux/stat.h> 17 #include <linux/errno.h> 18 #include <linux/unistd.h> 19 #include <linux/sunrpc/addr.h> 20 #include <linux/sunrpc/clnt.h> 21 #include <linux/sunrpc/stats.h> 22 #include <linux/sunrpc/metrics.h> 23 #include <linux/sunrpc/xprtsock.h> 24 #include <linux/sunrpc/xprtrdma.h> 25 #include <linux/nfs_fs.h> 26 #include <linux/nfs_mount.h> 27 #include <linux/nfs4_mount.h> 28 #include <linux/lockd/bind.h> 29 #include <linux/seq_file.h> 30 #include <linux/mount.h> 31 #include <linux/vfs.h> 32 #include <linux/inet.h> 33 #include <linux/in6.h> 34 #include <linux/slab.h> 35 #include <linux/idr.h> 36 #include <net/ipv6.h> 37 #include <linux/nfs_xdr.h> 38 #include <linux/sunrpc/bc_xprt.h> 39 #include <linux/nsproxy.h> 40 #include <linux/pid_namespace.h> 41 42 43 #include "nfs4_fs.h" 44 #include "callback.h" 45 #include "delegation.h" 46 #include "iostat.h" 47 #include "internal.h" 48 #include "fscache.h" 49 #include "pnfs.h" 50 #include "nfs.h" 51 #include "netns.h" 52 #include "sysfs.h" 53 #include "nfs42.h" 54 55 #define NFSDBG_FACILITY NFSDBG_CLIENT 56 57 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq); 58 static DEFINE_SPINLOCK(nfs_version_lock); 59 static DEFINE_MUTEX(nfs_version_mutex); 60 static LIST_HEAD(nfs_versions); 61 62 /* 63 * RPC cruft for NFS 64 */ 65 static const struct rpc_version *nfs_version[5] = { 66 [2] = NULL, 67 [3] = NULL, 68 [4] = NULL, 69 }; 70 71 const struct rpc_program nfs_program = { 72 .name = "nfs", 73 .number = NFS_PROGRAM, 74 .nrvers = ARRAY_SIZE(nfs_version), 75 .version = nfs_version, 76 .pipe_dir_name = NFS_PIPE_DIRNAME, 77 }; 78 79 static struct nfs_subversion *find_nfs_version(unsigned int version) 80 { 81 struct nfs_subversion *nfs; 82 spin_lock(&nfs_version_lock); 83 84 list_for_each_entry(nfs, &nfs_versions, list) { 85 if (nfs->rpc_ops->version == version) { 86 spin_unlock(&nfs_version_lock); 87 return nfs; 88 } 89 } 90 91 spin_unlock(&nfs_version_lock); 92 return ERR_PTR(-EPROTONOSUPPORT); 93 } 94 95 struct nfs_subversion *get_nfs_version(unsigned int version) 96 { 97 struct nfs_subversion *nfs = find_nfs_version(version); 98 99 if (IS_ERR(nfs)) { 100 mutex_lock(&nfs_version_mutex); 101 request_module("nfsv%d", version); 102 nfs = find_nfs_version(version); 103 mutex_unlock(&nfs_version_mutex); 104 } 105 106 if (!IS_ERR(nfs) && !try_module_get(nfs->owner)) 107 return ERR_PTR(-EAGAIN); 108 return nfs; 109 } 110 111 void put_nfs_version(struct nfs_subversion *nfs) 112 { 113 module_put(nfs->owner); 114 } 115 116 void register_nfs_version(struct nfs_subversion *nfs) 117 { 118 spin_lock(&nfs_version_lock); 119 120 list_add(&nfs->list, &nfs_versions); 121 nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers; 122 123 spin_unlock(&nfs_version_lock); 124 } 125 EXPORT_SYMBOL_GPL(register_nfs_version); 126 127 void unregister_nfs_version(struct nfs_subversion *nfs) 128 { 129 spin_lock(&nfs_version_lock); 130 131 nfs_version[nfs->rpc_ops->version] = NULL; 132 list_del(&nfs->list); 133 134 spin_unlock(&nfs_version_lock); 135 } 136 EXPORT_SYMBOL_GPL(unregister_nfs_version); 137 138 /* 139 * Allocate a shared client record 140 * 141 * Since these are allocated/deallocated very rarely, we don't 142 * bother putting them in a slab cache... 143 */ 144 struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init) 145 { 146 struct nfs_client *clp; 147 int err = -ENOMEM; 148 149 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL) 150 goto error_0; 151 152 clp->cl_minorversion = cl_init->minorversion; 153 clp->cl_nfs_mod = cl_init->nfs_mod; 154 if (!try_module_get(clp->cl_nfs_mod->owner)) 155 goto error_dealloc; 156 157 clp->rpc_ops = clp->cl_nfs_mod->rpc_ops; 158 159 refcount_set(&clp->cl_count, 1); 160 clp->cl_cons_state = NFS_CS_INITING; 161 162 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen); 163 clp->cl_addrlen = cl_init->addrlen; 164 165 if (cl_init->hostname) { 166 err = -ENOMEM; 167 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL); 168 if (!clp->cl_hostname) 169 goto error_cleanup; 170 } 171 172 INIT_LIST_HEAD(&clp->cl_superblocks); 173 clp->cl_rpcclient = ERR_PTR(-EINVAL); 174 175 clp->cl_flags = cl_init->init_flags; 176 clp->cl_proto = cl_init->proto; 177 clp->cl_nconnect = cl_init->nconnect; 178 clp->cl_max_connect = cl_init->max_connect ? cl_init->max_connect : 1; 179 clp->cl_net = get_net(cl_init->net); 180 181 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 182 seqlock_init(&clp->cl_boot_lock); 183 ktime_get_real_ts64(&clp->cl_nfssvc_boot); 184 clp->cl_uuid.net = NULL; 185 clp->cl_uuid.dom = NULL; 186 spin_lock_init(&clp->cl_localio_lock); 187 #endif /* CONFIG_NFS_LOCALIO */ 188 189 clp->cl_principal = "*"; 190 clp->cl_xprtsec = cl_init->xprtsec; 191 return clp; 192 193 error_cleanup: 194 put_nfs_version(clp->cl_nfs_mod); 195 error_dealloc: 196 kfree(clp); 197 error_0: 198 return ERR_PTR(err); 199 } 200 EXPORT_SYMBOL_GPL(nfs_alloc_client); 201 202 #if IS_ENABLED(CONFIG_NFS_V4) 203 static void nfs_cleanup_cb_ident_idr(struct net *net) 204 { 205 struct nfs_net *nn = net_generic(net, nfs_net_id); 206 207 idr_destroy(&nn->cb_ident_idr); 208 } 209 210 /* nfs_client_lock held */ 211 static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 212 { 213 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 214 215 if (clp->cl_cb_ident) 216 idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident); 217 } 218 219 static void pnfs_init_server(struct nfs_server *server) 220 { 221 rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC"); 222 } 223 224 #else 225 static void nfs_cleanup_cb_ident_idr(struct net *net) 226 { 227 } 228 229 static void nfs_cb_idr_remove_locked(struct nfs_client *clp) 230 { 231 } 232 233 static void pnfs_init_server(struct nfs_server *server) 234 { 235 } 236 237 #endif /* CONFIG_NFS_V4 */ 238 239 /* 240 * Destroy a shared client record 241 */ 242 void nfs_free_client(struct nfs_client *clp) 243 { 244 nfs_local_disable(clp); 245 246 /* -EIO all pending I/O */ 247 if (!IS_ERR(clp->cl_rpcclient)) 248 rpc_shutdown_client(clp->cl_rpcclient); 249 250 put_net(clp->cl_net); 251 put_nfs_version(clp->cl_nfs_mod); 252 kfree(clp->cl_hostname); 253 kfree(clp->cl_acceptor); 254 kfree_rcu(clp, rcu); 255 } 256 EXPORT_SYMBOL_GPL(nfs_free_client); 257 258 /* 259 * Release a reference to a shared client record 260 */ 261 void nfs_put_client(struct nfs_client *clp) 262 { 263 struct nfs_net *nn; 264 265 if (!clp) 266 return; 267 268 nn = net_generic(clp->cl_net, nfs_net_id); 269 270 if (refcount_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) { 271 list_del(&clp->cl_share_link); 272 nfs_cb_idr_remove_locked(clp); 273 spin_unlock(&nn->nfs_client_lock); 274 275 WARN_ON_ONCE(!list_empty(&clp->cl_superblocks)); 276 277 clp->rpc_ops->free_client(clp); 278 } 279 } 280 EXPORT_SYMBOL_GPL(nfs_put_client); 281 282 /* 283 * Find an nfs_client on the list that matches the initialisation data 284 * that is supplied. 285 */ 286 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data) 287 { 288 struct nfs_client *clp; 289 const struct sockaddr *sap = (struct sockaddr *)data->addr; 290 struct nfs_net *nn = net_generic(data->net, nfs_net_id); 291 int error; 292 293 again: 294 list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) { 295 const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr; 296 /* Don't match clients that failed to initialise properly */ 297 if (clp->cl_cons_state < 0) 298 continue; 299 300 /* If a client is still initializing then we need to wait */ 301 if (clp->cl_cons_state > NFS_CS_READY) { 302 refcount_inc(&clp->cl_count); 303 spin_unlock(&nn->nfs_client_lock); 304 error = nfs_wait_client_init_complete(clp); 305 nfs_put_client(clp); 306 spin_lock(&nn->nfs_client_lock); 307 if (error < 0) 308 return ERR_PTR(error); 309 goto again; 310 } 311 312 /* Different NFS versions cannot share the same nfs_client */ 313 if (clp->rpc_ops != data->nfs_mod->rpc_ops) 314 continue; 315 316 if (clp->cl_proto != data->proto) 317 continue; 318 /* Match nfsv4 minorversion */ 319 if (clp->cl_minorversion != data->minorversion) 320 continue; 321 322 /* Match request for a dedicated DS */ 323 if (test_bit(NFS_CS_DS, &data->init_flags) != 324 test_bit(NFS_CS_DS, &clp->cl_flags)) 325 continue; 326 327 /* Match the full socket address */ 328 if (!rpc_cmp_addr_port(sap, clap)) 329 /* Match all xprt_switch full socket addresses */ 330 if (IS_ERR(clp->cl_rpcclient) || 331 !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient, 332 sap)) 333 continue; 334 335 /* Match the xprt security policy */ 336 if (clp->cl_xprtsec.policy != data->xprtsec.policy) 337 continue; 338 339 refcount_inc(&clp->cl_count); 340 return clp; 341 } 342 return NULL; 343 } 344 345 /* 346 * Return true if @clp is done initializing, false if still working on it. 347 * 348 * Use nfs_client_init_status to check if it was successful. 349 */ 350 bool nfs_client_init_is_complete(const struct nfs_client *clp) 351 { 352 return clp->cl_cons_state <= NFS_CS_READY; 353 } 354 EXPORT_SYMBOL_GPL(nfs_client_init_is_complete); 355 356 /* 357 * Return 0 if @clp was successfully initialized, -errno otherwise. 358 * 359 * This must be called *after* nfs_client_init_is_complete() returns true, 360 * otherwise it will pop WARN_ON_ONCE and return -EINVAL 361 */ 362 int nfs_client_init_status(const struct nfs_client *clp) 363 { 364 /* called without checking nfs_client_init_is_complete */ 365 if (clp->cl_cons_state > NFS_CS_READY) { 366 WARN_ON_ONCE(1); 367 return -EINVAL; 368 } 369 return clp->cl_cons_state; 370 } 371 EXPORT_SYMBOL_GPL(nfs_client_init_status); 372 373 int nfs_wait_client_init_complete(const struct nfs_client *clp) 374 { 375 return wait_event_killable(nfs_client_active_wq, 376 nfs_client_init_is_complete(clp)); 377 } 378 EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete); 379 380 /* 381 * Found an existing client. Make sure it's ready before returning. 382 */ 383 static struct nfs_client * 384 nfs_found_client(const struct nfs_client_initdata *cl_init, 385 struct nfs_client *clp) 386 { 387 int error; 388 389 error = nfs_wait_client_init_complete(clp); 390 if (error < 0) { 391 nfs_put_client(clp); 392 return ERR_PTR(-ERESTARTSYS); 393 } 394 395 if (clp->cl_cons_state < NFS_CS_READY) { 396 error = clp->cl_cons_state; 397 nfs_put_client(clp); 398 return ERR_PTR(error); 399 } 400 401 smp_rmb(); 402 return clp; 403 } 404 405 /* 406 * Look up a client by IP address and protocol version 407 * - creates a new record if one doesn't yet exist 408 */ 409 struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init) 410 { 411 struct nfs_client *clp, *new = NULL; 412 struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id); 413 const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops; 414 415 if (cl_init->hostname == NULL) { 416 WARN_ON(1); 417 return ERR_PTR(-EINVAL); 418 } 419 420 /* see if the client already exists */ 421 do { 422 spin_lock(&nn->nfs_client_lock); 423 424 clp = nfs_match_client(cl_init); 425 if (clp) { 426 spin_unlock(&nn->nfs_client_lock); 427 if (new) 428 new->rpc_ops->free_client(new); 429 if (IS_ERR(clp)) 430 return clp; 431 return nfs_found_client(cl_init, clp); 432 } 433 if (new) { 434 list_add_tail(&new->cl_share_link, 435 &nn->nfs_client_list); 436 spin_unlock(&nn->nfs_client_lock); 437 new = rpc_ops->init_client(new, cl_init); 438 if (!IS_ERR(new)) 439 nfs_local_probe(new); 440 return new; 441 } 442 443 spin_unlock(&nn->nfs_client_lock); 444 445 new = rpc_ops->alloc_client(cl_init); 446 } while (!IS_ERR(new)); 447 448 return new; 449 } 450 EXPORT_SYMBOL_GPL(nfs_get_client); 451 452 /* 453 * Mark a server as ready or failed 454 */ 455 void nfs_mark_client_ready(struct nfs_client *clp, int state) 456 { 457 smp_wmb(); 458 clp->cl_cons_state = state; 459 wake_up_all(&nfs_client_active_wq); 460 } 461 EXPORT_SYMBOL_GPL(nfs_mark_client_ready); 462 463 /* 464 * Initialise the timeout values for a connection 465 */ 466 void nfs_init_timeout_values(struct rpc_timeout *to, int proto, 467 int timeo, int retrans) 468 { 469 to->to_initval = timeo * HZ / 10; 470 to->to_retries = retrans; 471 472 switch (proto) { 473 case XPRT_TRANSPORT_TCP: 474 case XPRT_TRANSPORT_TCP_TLS: 475 case XPRT_TRANSPORT_RDMA: 476 if (retrans == NFS_UNSPEC_RETRANS) 477 to->to_retries = NFS_DEF_TCP_RETRANS; 478 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0) 479 to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10; 480 if (to->to_initval > NFS_MAX_TCP_TIMEOUT) 481 to->to_initval = NFS_MAX_TCP_TIMEOUT; 482 to->to_increment = to->to_initval; 483 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries); 484 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT) 485 to->to_maxval = NFS_MAX_TCP_TIMEOUT; 486 if (to->to_maxval < to->to_initval) 487 to->to_maxval = to->to_initval; 488 to->to_exponential = 0; 489 break; 490 case XPRT_TRANSPORT_UDP: 491 if (retrans == NFS_UNSPEC_RETRANS) 492 to->to_retries = NFS_DEF_UDP_RETRANS; 493 if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0) 494 to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10; 495 if (to->to_initval > NFS_MAX_UDP_TIMEOUT) 496 to->to_initval = NFS_MAX_UDP_TIMEOUT; 497 to->to_maxval = NFS_MAX_UDP_TIMEOUT; 498 to->to_exponential = 1; 499 break; 500 default: 501 BUG(); 502 } 503 } 504 EXPORT_SYMBOL_GPL(nfs_init_timeout_values); 505 506 /* 507 * Create an RPC client handle 508 */ 509 int nfs_create_rpc_client(struct nfs_client *clp, 510 const struct nfs_client_initdata *cl_init, 511 rpc_authflavor_t flavor) 512 { 513 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 514 struct rpc_clnt *clnt = NULL; 515 struct rpc_create_args args = { 516 .net = clp->cl_net, 517 .protocol = clp->cl_proto, 518 .nconnect = clp->cl_nconnect, 519 .address = (struct sockaddr *)&clp->cl_addr, 520 .addrsize = clp->cl_addrlen, 521 .timeout = cl_init->timeparms, 522 .servername = clp->cl_hostname, 523 .nodename = cl_init->nodename, 524 .program = &nfs_program, 525 .stats = &nn->rpcstats, 526 .version = clp->rpc_ops->version, 527 .authflavor = flavor, 528 .cred = cl_init->cred, 529 .xprtsec = cl_init->xprtsec, 530 .connect_timeout = cl_init->connect_timeout, 531 .reconnect_timeout = cl_init->reconnect_timeout, 532 }; 533 534 if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags)) 535 args.flags |= RPC_CLNT_CREATE_DISCRTRY; 536 if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags)) 537 args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT; 538 if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags)) 539 args.flags |= RPC_CLNT_CREATE_NONPRIVPORT; 540 if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags)) 541 args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS; 542 if (test_bit(NFS_CS_NOPING, &clp->cl_flags)) 543 args.flags |= RPC_CLNT_CREATE_NOPING; 544 if (test_bit(NFS_CS_REUSEPORT, &clp->cl_flags)) 545 args.flags |= RPC_CLNT_CREATE_REUSEPORT; 546 547 if (!IS_ERR(clp->cl_rpcclient)) 548 return 0; 549 550 clnt = rpc_create(&args); 551 if (IS_ERR(clnt)) { 552 dprintk("%s: cannot create RPC client. Error = %ld\n", 553 __func__, PTR_ERR(clnt)); 554 return PTR_ERR(clnt); 555 } 556 557 clnt->cl_principal = clp->cl_principal; 558 clp->cl_rpcclient = clnt; 559 clnt->cl_max_connect = clp->cl_max_connect; 560 return 0; 561 } 562 EXPORT_SYMBOL_GPL(nfs_create_rpc_client); 563 564 /* 565 * Version 2 or 3 client destruction 566 */ 567 static void nfs_destroy_server(struct nfs_server *server) 568 { 569 if (server->nlm_host) 570 nlmclnt_done(server->nlm_host); 571 } 572 573 /* 574 * Version 2 or 3 lockd setup 575 */ 576 static int nfs_start_lockd(struct nfs_server *server) 577 { 578 struct nlm_host *host; 579 struct nfs_client *clp = server->nfs_client; 580 struct nlmclnt_initdata nlm_init = { 581 .hostname = clp->cl_hostname, 582 .address = (struct sockaddr *)&clp->cl_addr, 583 .addrlen = clp->cl_addrlen, 584 .nfs_version = clp->rpc_ops->version, 585 .noresvport = server->flags & NFS_MOUNT_NORESVPORT ? 586 1 : 0, 587 .net = clp->cl_net, 588 .nlmclnt_ops = clp->cl_nfs_mod->rpc_ops->nlmclnt_ops, 589 .cred = server->cred, 590 }; 591 592 if (nlm_init.nfs_version > 3) 593 return 0; 594 if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) && 595 (server->flags & NFS_MOUNT_LOCAL_FCNTL)) 596 return 0; 597 598 switch (clp->cl_proto) { 599 default: 600 nlm_init.protocol = IPPROTO_TCP; 601 break; 602 #ifndef CONFIG_NFS_DISABLE_UDP_SUPPORT 603 case XPRT_TRANSPORT_UDP: 604 nlm_init.protocol = IPPROTO_UDP; 605 #endif 606 } 607 608 host = nlmclnt_init(&nlm_init); 609 if (IS_ERR(host)) 610 return PTR_ERR(host); 611 612 server->nlm_host = host; 613 server->destroy = nfs_destroy_server; 614 nfs_sysfs_link_rpc_client(server, nlmclnt_rpc_clnt(host), NULL); 615 return 0; 616 } 617 618 /* 619 * Create a general RPC client 620 */ 621 int nfs_init_server_rpcclient(struct nfs_server *server, 622 const struct rpc_timeout *timeo, 623 rpc_authflavor_t pseudoflavour) 624 { 625 struct nfs_client *clp = server->nfs_client; 626 627 server->client = rpc_clone_client_set_auth(clp->cl_rpcclient, 628 pseudoflavour); 629 if (IS_ERR(server->client)) { 630 dprintk("%s: couldn't create rpc_client!\n", __func__); 631 return PTR_ERR(server->client); 632 } 633 634 memcpy(&server->client->cl_timeout_default, 635 timeo, 636 sizeof(server->client->cl_timeout_default)); 637 server->client->cl_timeout = &server->client->cl_timeout_default; 638 server->client->cl_softrtry = 0; 639 if (server->flags & NFS_MOUNT_SOFTERR) 640 server->client->cl_softerr = 1; 641 if (server->flags & NFS_MOUNT_SOFT) 642 server->client->cl_softrtry = 1; 643 644 nfs_sysfs_link_rpc_client(server, server->client, NULL); 645 return 0; 646 } 647 EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient); 648 649 /** 650 * nfs_init_client - Initialise an NFS2 or NFS3 client 651 * 652 * @clp: nfs_client to initialise 653 * @cl_init: Initialisation parameters 654 * 655 * Returns pointer to an NFS client, or an ERR_PTR value. 656 */ 657 struct nfs_client *nfs_init_client(struct nfs_client *clp, 658 const struct nfs_client_initdata *cl_init) 659 { 660 int error; 661 662 /* the client is already initialised */ 663 if (clp->cl_cons_state == NFS_CS_READY) 664 return clp; 665 666 /* 667 * Create a client RPC handle for doing FSSTAT with UNIX auth only 668 * - RFC 2623, sec 2.3.2 669 */ 670 error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX); 671 nfs_mark_client_ready(clp, error == 0 ? NFS_CS_READY : error); 672 if (error < 0) { 673 nfs_put_client(clp); 674 clp = ERR_PTR(error); 675 } 676 return clp; 677 } 678 EXPORT_SYMBOL_GPL(nfs_init_client); 679 680 /* 681 * Create a version 2 or 3 client 682 */ 683 static int nfs_init_server(struct nfs_server *server, 684 const struct fs_context *fc) 685 { 686 const struct nfs_fs_context *ctx = nfs_fc2context(fc); 687 struct rpc_timeout timeparms; 688 struct nfs_client_initdata cl_init = { 689 .hostname = ctx->nfs_server.hostname, 690 .addr = &ctx->nfs_server._address, 691 .addrlen = ctx->nfs_server.addrlen, 692 .nfs_mod = ctx->nfs_mod, 693 .proto = ctx->nfs_server.protocol, 694 .net = fc->net_ns, 695 .timeparms = &timeparms, 696 .cred = server->cred, 697 .nconnect = ctx->nfs_server.nconnect, 698 .init_flags = (1UL << NFS_CS_REUSEPORT), 699 .xprtsec = ctx->xprtsec, 700 }; 701 struct nfs_client *clp; 702 int error; 703 704 nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol, 705 ctx->timeo, ctx->retrans); 706 if (ctx->flags & NFS_MOUNT_NORESVPORT) 707 set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags); 708 709 /* Allocate or find a client reference we can use */ 710 clp = nfs_get_client(&cl_init); 711 if (IS_ERR(clp)) 712 return PTR_ERR(clp); 713 714 server->nfs_client = clp; 715 nfs_sysfs_add_server(server); 716 nfs_sysfs_link_rpc_client(server, clp->cl_rpcclient, "_state"); 717 718 /* Initialise the client representation from the mount data */ 719 server->flags = ctx->flags; 720 server->options = ctx->options; 721 server->caps |= NFS_CAP_HARDLINKS | NFS_CAP_SYMLINKS; 722 723 switch (clp->rpc_ops->version) { 724 case 2: 725 server->fattr_valid = NFS_ATTR_FATTR_V2; 726 break; 727 case 3: 728 server->fattr_valid = NFS_ATTR_FATTR_V3; 729 break; 730 default: 731 server->fattr_valid = NFS_ATTR_FATTR_V4; 732 } 733 734 if (ctx->rsize) 735 server->rsize = nfs_io_size(ctx->rsize, clp->cl_proto); 736 if (ctx->wsize) 737 server->wsize = nfs_io_size(ctx->wsize, clp->cl_proto); 738 739 server->acregmin = ctx->acregmin * HZ; 740 server->acregmax = ctx->acregmax * HZ; 741 server->acdirmin = ctx->acdirmin * HZ; 742 server->acdirmax = ctx->acdirmax * HZ; 743 744 /* Start lockd here, before we might error out */ 745 error = nfs_start_lockd(server); 746 if (error < 0) 747 goto error; 748 749 server->port = ctx->nfs_server.port; 750 server->auth_info = ctx->auth_info; 751 752 error = nfs_init_server_rpcclient(server, &timeparms, 753 ctx->selected_flavor); 754 if (error < 0) 755 goto error; 756 757 /* Preserve the values of mount_server-related mount options */ 758 if (ctx->mount_server.addrlen) { 759 memcpy(&server->mountd_address, &ctx->mount_server.address, 760 ctx->mount_server.addrlen); 761 server->mountd_addrlen = ctx->mount_server.addrlen; 762 } 763 server->mountd_version = ctx->mount_server.version; 764 server->mountd_port = ctx->mount_server.port; 765 server->mountd_protocol = ctx->mount_server.protocol; 766 767 server->namelen = ctx->namlen; 768 return 0; 769 770 error: 771 server->nfs_client = NULL; 772 nfs_put_client(clp); 773 return error; 774 } 775 776 /* 777 * Load up the server record from information gained in an fsinfo record 778 */ 779 static void nfs_server_set_fsinfo(struct nfs_server *server, 780 struct nfs_fsinfo *fsinfo) 781 { 782 struct nfs_client *clp = server->nfs_client; 783 unsigned long max_rpc_payload, raw_max_rpc_payload; 784 785 /* Work out a lot of parameters */ 786 if (server->rsize == 0) 787 server->rsize = nfs_io_size(fsinfo->rtpref, clp->cl_proto); 788 if (server->wsize == 0) 789 server->wsize = nfs_io_size(fsinfo->wtpref, clp->cl_proto); 790 791 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax) 792 server->rsize = nfs_io_size(fsinfo->rtmax, clp->cl_proto); 793 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax) 794 server->wsize = nfs_io_size(fsinfo->wtmax, clp->cl_proto); 795 796 raw_max_rpc_payload = rpc_max_payload(server->client); 797 max_rpc_payload = nfs_block_size(raw_max_rpc_payload, NULL); 798 799 if (server->rsize > max_rpc_payload) 800 server->rsize = max_rpc_payload; 801 if (server->rsize > NFS_MAX_FILE_IO_SIZE) 802 server->rsize = NFS_MAX_FILE_IO_SIZE; 803 server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 804 805 if (server->wsize > max_rpc_payload) 806 server->wsize = max_rpc_payload; 807 if (server->wsize > NFS_MAX_FILE_IO_SIZE) 808 server->wsize = NFS_MAX_FILE_IO_SIZE; 809 server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT; 810 811 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL); 812 813 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL); 814 if (server->dtsize > NFS_MAX_FILE_IO_SIZE) 815 server->dtsize = NFS_MAX_FILE_IO_SIZE; 816 if (server->dtsize > server->rsize) 817 server->dtsize = server->rsize; 818 819 if (server->flags & NFS_MOUNT_NOAC) { 820 server->acregmin = server->acregmax = 0; 821 server->acdirmin = server->acdirmax = 0; 822 } 823 824 server->maxfilesize = fsinfo->maxfilesize; 825 826 server->time_delta = fsinfo->time_delta; 827 server->change_attr_type = fsinfo->change_attr_type; 828 829 server->clone_blksize = fsinfo->clone_blksize; 830 /* We're airborne Set socket buffersize */ 831 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100); 832 833 #ifdef CONFIG_NFS_V4_2 834 /* 835 * Defaults until limited by the session parameters. 836 */ 837 server->gxasize = min_t(unsigned int, raw_max_rpc_payload, 838 XATTR_SIZE_MAX); 839 server->sxasize = min_t(unsigned int, raw_max_rpc_payload, 840 XATTR_SIZE_MAX); 841 server->lxasize = min_t(unsigned int, raw_max_rpc_payload, 842 nfs42_listxattr_xdrsize(XATTR_LIST_MAX)); 843 844 if (fsinfo->xattr_support) 845 server->caps |= NFS_CAP_XATTR; 846 #endif 847 } 848 849 /* 850 * Probe filesystem information, including the FSID on v2/v3 851 */ 852 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr) 853 { 854 struct nfs_fsinfo fsinfo; 855 struct nfs_client *clp = server->nfs_client; 856 int error; 857 858 if (clp->rpc_ops->set_capabilities != NULL) { 859 error = clp->rpc_ops->set_capabilities(server, mntfh); 860 if (error < 0) 861 return error; 862 } 863 864 fsinfo.fattr = fattr; 865 fsinfo.nlayouttypes = 0; 866 memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype)); 867 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo); 868 if (error < 0) 869 return error; 870 871 nfs_server_set_fsinfo(server, &fsinfo); 872 873 /* Get some general file system info */ 874 if (server->namelen == 0) { 875 struct nfs_pathconf pathinfo; 876 877 pathinfo.fattr = fattr; 878 nfs_fattr_init(fattr); 879 880 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0) 881 server->namelen = pathinfo.max_namelen; 882 } 883 884 if (clp->rpc_ops->discover_trunking != NULL && 885 (server->caps & NFS_CAP_FS_LOCATIONS && 886 (server->flags & NFS_MOUNT_TRUNK_DISCOVERY))) { 887 error = clp->rpc_ops->discover_trunking(server, mntfh); 888 if (error < 0) 889 return error; 890 } 891 892 return 0; 893 } 894 895 /* 896 * Grab the destination's particulars, including lease expiry time. 897 * 898 * Returns zero if probe succeeded and retrieved FSID matches the FSID 899 * we have cached. 900 */ 901 int nfs_probe_server(struct nfs_server *server, struct nfs_fh *mntfh) 902 { 903 struct nfs_fattr *fattr; 904 int error; 905 906 fattr = nfs_alloc_fattr(); 907 if (fattr == NULL) 908 return -ENOMEM; 909 910 /* Sanity: the probe won't work if the destination server 911 * does not recognize the migrated FH. */ 912 error = nfs_probe_fsinfo(server, mntfh, fattr); 913 914 nfs_free_fattr(fattr); 915 return error; 916 } 917 EXPORT_SYMBOL_GPL(nfs_probe_server); 918 919 /* 920 * Copy useful information when duplicating a server record 921 */ 922 void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source) 923 { 924 target->flags = source->flags; 925 target->rsize = source->rsize; 926 target->wsize = source->wsize; 927 target->acregmin = source->acregmin; 928 target->acregmax = source->acregmax; 929 target->acdirmin = source->acdirmin; 930 target->acdirmax = source->acdirmax; 931 target->caps = source->caps; 932 target->options = source->options; 933 target->auth_info = source->auth_info; 934 target->port = source->port; 935 } 936 EXPORT_SYMBOL_GPL(nfs_server_copy_userdata); 937 938 void nfs_server_insert_lists(struct nfs_server *server) 939 { 940 struct nfs_client *clp = server->nfs_client; 941 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id); 942 943 spin_lock(&nn->nfs_client_lock); 944 list_add_tail_rcu(&server->client_link, &clp->cl_superblocks); 945 list_add_tail(&server->master_link, &nn->nfs_volume_list); 946 clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 947 spin_unlock(&nn->nfs_client_lock); 948 949 } 950 EXPORT_SYMBOL_GPL(nfs_server_insert_lists); 951 952 void nfs_server_remove_lists(struct nfs_server *server) 953 { 954 struct nfs_client *clp = server->nfs_client; 955 struct nfs_net *nn; 956 957 if (clp == NULL) 958 return; 959 nn = net_generic(clp->cl_net, nfs_net_id); 960 spin_lock(&nn->nfs_client_lock); 961 list_del_rcu(&server->client_link); 962 if (list_empty(&clp->cl_superblocks)) 963 set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state); 964 list_del(&server->master_link); 965 spin_unlock(&nn->nfs_client_lock); 966 967 synchronize_rcu(); 968 } 969 EXPORT_SYMBOL_GPL(nfs_server_remove_lists); 970 971 static DEFINE_IDA(s_sysfs_ids); 972 973 /* 974 * Allocate and initialise a server record 975 */ 976 struct nfs_server *nfs_alloc_server(void) 977 { 978 struct nfs_server *server; 979 980 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL); 981 if (!server) 982 return NULL; 983 984 server->s_sysfs_id = ida_alloc(&s_sysfs_ids, GFP_KERNEL); 985 if (server->s_sysfs_id < 0) { 986 kfree(server); 987 return NULL; 988 } 989 990 server->client = server->client_acl = ERR_PTR(-EINVAL); 991 992 /* Zero out the NFS state stuff */ 993 INIT_LIST_HEAD(&server->client_link); 994 INIT_LIST_HEAD(&server->master_link); 995 INIT_LIST_HEAD(&server->delegations); 996 INIT_LIST_HEAD(&server->layouts); 997 INIT_LIST_HEAD(&server->state_owners_lru); 998 INIT_LIST_HEAD(&server->ss_copies); 999 1000 atomic_set(&server->active, 0); 1001 1002 server->io_stats = nfs_alloc_iostats(); 1003 if (!server->io_stats) { 1004 kfree(server); 1005 return NULL; 1006 } 1007 1008 server->change_attr_type = NFS4_CHANGE_TYPE_IS_UNDEFINED; 1009 1010 init_waitqueue_head(&server->write_congestion_wait); 1011 atomic_long_set(&server->writeback, 0); 1012 1013 atomic64_set(&server->owner_ctr, 0); 1014 1015 pnfs_init_server(server); 1016 rpc_init_wait_queue(&server->uoc_rpcwaitq, "NFS UOC"); 1017 1018 return server; 1019 } 1020 EXPORT_SYMBOL_GPL(nfs_alloc_server); 1021 1022 static void delayed_free(struct rcu_head *p) 1023 { 1024 struct nfs_server *server = container_of(p, struct nfs_server, rcu); 1025 1026 nfs_free_iostats(server->io_stats); 1027 kfree(server); 1028 } 1029 1030 /* 1031 * Free up a server record 1032 */ 1033 void nfs_free_server(struct nfs_server *server) 1034 { 1035 nfs_server_remove_lists(server); 1036 1037 if (server->destroy != NULL) 1038 server->destroy(server); 1039 1040 if (!IS_ERR(server->client_acl)) 1041 rpc_shutdown_client(server->client_acl); 1042 if (!IS_ERR(server->client)) 1043 rpc_shutdown_client(server->client); 1044 1045 nfs_put_client(server->nfs_client); 1046 1047 if (server->kobj.state_initialized) { 1048 nfs_sysfs_remove_server(server); 1049 kobject_put(&server->kobj); 1050 } 1051 ida_free(&s_sysfs_ids, server->s_sysfs_id); 1052 1053 put_cred(server->cred); 1054 nfs_release_automount_timer(); 1055 call_rcu(&server->rcu, delayed_free); 1056 } 1057 EXPORT_SYMBOL_GPL(nfs_free_server); 1058 1059 /* 1060 * Create a version 2 or 3 volume record 1061 * - keyed on server and FSID 1062 */ 1063 struct nfs_server *nfs_create_server(struct fs_context *fc) 1064 { 1065 struct nfs_fs_context *ctx = nfs_fc2context(fc); 1066 struct nfs_server *server; 1067 struct nfs_fattr *fattr; 1068 int error; 1069 1070 server = nfs_alloc_server(); 1071 if (!server) 1072 return ERR_PTR(-ENOMEM); 1073 1074 server->cred = get_cred(fc->cred); 1075 1076 error = -ENOMEM; 1077 fattr = nfs_alloc_fattr(); 1078 if (fattr == NULL) 1079 goto error; 1080 1081 /* Get a client representation */ 1082 error = nfs_init_server(server, fc); 1083 if (error < 0) 1084 goto error; 1085 1086 /* Probe the root fh to retrieve its FSID */ 1087 error = nfs_probe_fsinfo(server, ctx->mntfh, fattr); 1088 if (error < 0) 1089 goto error; 1090 if (server->nfs_client->rpc_ops->version == 3) { 1091 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN) 1092 server->namelen = NFS3_MAXNAMLEN; 1093 if (!(ctx->flags & NFS_MOUNT_NORDIRPLUS)) 1094 server->caps |= NFS_CAP_READDIRPLUS; 1095 } else { 1096 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN) 1097 server->namelen = NFS2_MAXNAMLEN; 1098 } 1099 1100 if (!(fattr->valid & NFS_ATTR_FATTR)) { 1101 error = ctx->nfs_mod->rpc_ops->getattr(server, ctx->mntfh, 1102 fattr, NULL); 1103 if (error < 0) { 1104 dprintk("nfs_create_server: getattr error = %d\n", -error); 1105 goto error; 1106 } 1107 } 1108 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid)); 1109 1110 dprintk("Server FSID: %llx:%llx\n", 1111 (unsigned long long) server->fsid.major, 1112 (unsigned long long) server->fsid.minor); 1113 1114 nfs_server_insert_lists(server); 1115 server->mount_time = jiffies; 1116 nfs_free_fattr(fattr); 1117 return server; 1118 1119 error: 1120 nfs_free_fattr(fattr); 1121 nfs_free_server(server); 1122 return ERR_PTR(error); 1123 } 1124 EXPORT_SYMBOL_GPL(nfs_create_server); 1125 1126 /* 1127 * Clone an NFS2, NFS3 or NFS4 server record 1128 */ 1129 struct nfs_server *nfs_clone_server(struct nfs_server *source, 1130 struct nfs_fh *fh, 1131 struct nfs_fattr *fattr, 1132 rpc_authflavor_t flavor) 1133 { 1134 struct nfs_server *server; 1135 int error; 1136 1137 server = nfs_alloc_server(); 1138 if (!server) 1139 return ERR_PTR(-ENOMEM); 1140 1141 server->cred = get_cred(source->cred); 1142 1143 /* Copy data from the source */ 1144 server->nfs_client = source->nfs_client; 1145 server->destroy = source->destroy; 1146 refcount_inc(&server->nfs_client->cl_count); 1147 nfs_server_copy_userdata(server, source); 1148 1149 server->fsid = fattr->fsid; 1150 1151 nfs_sysfs_add_server(server); 1152 1153 nfs_sysfs_link_rpc_client(server, 1154 server->nfs_client->cl_rpcclient, "_state"); 1155 1156 error = nfs_init_server_rpcclient(server, 1157 source->client->cl_timeout, 1158 flavor); 1159 if (error < 0) 1160 goto out_free_server; 1161 1162 /* probe the filesystem info for this server filesystem */ 1163 error = nfs_probe_server(server, fh); 1164 if (error < 0) 1165 goto out_free_server; 1166 1167 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN) 1168 server->namelen = NFS4_MAXNAMLEN; 1169 1170 error = nfs_start_lockd(server); 1171 if (error < 0) 1172 goto out_free_server; 1173 1174 nfs_server_insert_lists(server); 1175 server->mount_time = jiffies; 1176 1177 return server; 1178 1179 out_free_server: 1180 nfs_free_server(server); 1181 return ERR_PTR(error); 1182 } 1183 EXPORT_SYMBOL_GPL(nfs_clone_server); 1184 1185 void nfs_clients_init(struct net *net) 1186 { 1187 struct nfs_net *nn = net_generic(net, nfs_net_id); 1188 1189 INIT_LIST_HEAD(&nn->nfs_client_list); 1190 INIT_LIST_HEAD(&nn->nfs_volume_list); 1191 #if IS_ENABLED(CONFIG_NFS_V4) 1192 idr_init(&nn->cb_ident_idr); 1193 #endif 1194 spin_lock_init(&nn->nfs_client_lock); 1195 nn->boot_time = ktime_get_real(); 1196 memset(&nn->rpcstats, 0, sizeof(nn->rpcstats)); 1197 nn->rpcstats.program = &nfs_program; 1198 1199 nfs_netns_sysfs_setup(nn, net); 1200 } 1201 1202 void nfs_clients_exit(struct net *net) 1203 { 1204 struct nfs_net *nn = net_generic(net, nfs_net_id); 1205 1206 nfs_netns_sysfs_destroy(nn); 1207 nfs_cleanup_cb_ident_idr(net); 1208 WARN_ON_ONCE(!list_empty(&nn->nfs_client_list)); 1209 WARN_ON_ONCE(!list_empty(&nn->nfs_volume_list)); 1210 } 1211 1212 #ifdef CONFIG_PROC_FS 1213 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos); 1214 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos); 1215 static void nfs_server_list_stop(struct seq_file *p, void *v); 1216 static int nfs_server_list_show(struct seq_file *m, void *v); 1217 1218 static const struct seq_operations nfs_server_list_ops = { 1219 .start = nfs_server_list_start, 1220 .next = nfs_server_list_next, 1221 .stop = nfs_server_list_stop, 1222 .show = nfs_server_list_show, 1223 }; 1224 1225 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos); 1226 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos); 1227 static void nfs_volume_list_stop(struct seq_file *p, void *v); 1228 static int nfs_volume_list_show(struct seq_file *m, void *v); 1229 1230 static const struct seq_operations nfs_volume_list_ops = { 1231 .start = nfs_volume_list_start, 1232 .next = nfs_volume_list_next, 1233 .stop = nfs_volume_list_stop, 1234 .show = nfs_volume_list_show, 1235 }; 1236 1237 /* 1238 * set up the iterator to start reading from the server list and return the first item 1239 */ 1240 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos) 1241 __acquires(&nn->nfs_client_lock) 1242 { 1243 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 1244 1245 /* lock the list against modification */ 1246 spin_lock(&nn->nfs_client_lock); 1247 return seq_list_start_head(&nn->nfs_client_list, *_pos); 1248 } 1249 1250 /* 1251 * move to next server 1252 */ 1253 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos) 1254 { 1255 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1256 1257 return seq_list_next(v, &nn->nfs_client_list, pos); 1258 } 1259 1260 /* 1261 * clean up after reading from the transports list 1262 */ 1263 static void nfs_server_list_stop(struct seq_file *p, void *v) 1264 __releases(&nn->nfs_client_lock) 1265 { 1266 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1267 1268 spin_unlock(&nn->nfs_client_lock); 1269 } 1270 1271 /* 1272 * display a header line followed by a load of call lines 1273 */ 1274 static int nfs_server_list_show(struct seq_file *m, void *v) 1275 { 1276 struct nfs_client *clp; 1277 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 1278 1279 /* display header on line 1 */ 1280 if (v == &nn->nfs_client_list) { 1281 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n"); 1282 return 0; 1283 } 1284 1285 /* display one transport per line on subsequent lines */ 1286 clp = list_entry(v, struct nfs_client, cl_share_link); 1287 1288 /* Check if the client is initialized */ 1289 if (clp->cl_cons_state != NFS_CS_READY) 1290 return 0; 1291 1292 rcu_read_lock(); 1293 seq_printf(m, "v%u %s %s %3d %s\n", 1294 clp->rpc_ops->version, 1295 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 1296 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 1297 refcount_read(&clp->cl_count), 1298 clp->cl_hostname); 1299 rcu_read_unlock(); 1300 1301 return 0; 1302 } 1303 1304 /* 1305 * set up the iterator to start reading from the volume list and return the first item 1306 */ 1307 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos) 1308 __acquires(&nn->nfs_client_lock) 1309 { 1310 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 1311 1312 /* lock the list against modification */ 1313 spin_lock(&nn->nfs_client_lock); 1314 return seq_list_start_head(&nn->nfs_volume_list, *_pos); 1315 } 1316 1317 /* 1318 * move to next volume 1319 */ 1320 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos) 1321 { 1322 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1323 1324 return seq_list_next(v, &nn->nfs_volume_list, pos); 1325 } 1326 1327 /* 1328 * clean up after reading from the transports list 1329 */ 1330 static void nfs_volume_list_stop(struct seq_file *p, void *v) 1331 __releases(&nn->nfs_client_lock) 1332 { 1333 struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id); 1334 1335 spin_unlock(&nn->nfs_client_lock); 1336 } 1337 1338 /* 1339 * display a header line followed by a load of call lines 1340 */ 1341 static int nfs_volume_list_show(struct seq_file *m, void *v) 1342 { 1343 struct nfs_server *server; 1344 struct nfs_client *clp; 1345 char dev[13]; // 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0' 1346 char fsid[34]; // 2 * 16 for %llx, 1 for ':', 1 for '\0' 1347 struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id); 1348 1349 /* display header on line 1 */ 1350 if (v == &nn->nfs_volume_list) { 1351 seq_puts(m, "NV SERVER PORT DEV FSID" 1352 " FSC\n"); 1353 return 0; 1354 } 1355 /* display one transport per line on subsequent lines */ 1356 server = list_entry(v, struct nfs_server, master_link); 1357 clp = server->nfs_client; 1358 1359 snprintf(dev, sizeof(dev), "%u:%u", 1360 MAJOR(server->s_dev), MINOR(server->s_dev)); 1361 1362 snprintf(fsid, sizeof(fsid), "%llx:%llx", 1363 (unsigned long long) server->fsid.major, 1364 (unsigned long long) server->fsid.minor); 1365 1366 rcu_read_lock(); 1367 seq_printf(m, "v%u %s %s %-12s %-33s %s\n", 1368 clp->rpc_ops->version, 1369 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR), 1370 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT), 1371 dev, 1372 fsid, 1373 nfs_server_fscache_state(server)); 1374 rcu_read_unlock(); 1375 1376 return 0; 1377 } 1378 1379 int nfs_fs_proc_net_init(struct net *net) 1380 { 1381 struct nfs_net *nn = net_generic(net, nfs_net_id); 1382 struct proc_dir_entry *p; 1383 1384 nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net); 1385 if (!nn->proc_nfsfs) 1386 goto error_0; 1387 1388 /* a file of servers with which we're dealing */ 1389 p = proc_create_net("servers", S_IFREG|S_IRUGO, nn->proc_nfsfs, 1390 &nfs_server_list_ops, sizeof(struct seq_net_private)); 1391 if (!p) 1392 goto error_1; 1393 1394 /* a file of volumes that we have mounted */ 1395 p = proc_create_net("volumes", S_IFREG|S_IRUGO, nn->proc_nfsfs, 1396 &nfs_volume_list_ops, sizeof(struct seq_net_private)); 1397 if (!p) 1398 goto error_1; 1399 return 0; 1400 1401 error_1: 1402 remove_proc_subtree("nfsfs", net->proc_net); 1403 error_0: 1404 return -ENOMEM; 1405 } 1406 1407 void nfs_fs_proc_net_exit(struct net *net) 1408 { 1409 remove_proc_subtree("nfsfs", net->proc_net); 1410 } 1411 1412 /* 1413 * initialise the /proc/fs/nfsfs/ directory 1414 */ 1415 int __init nfs_fs_proc_init(void) 1416 { 1417 if (!proc_mkdir("fs/nfsfs", NULL)) 1418 goto error_0; 1419 1420 /* a file of servers with which we're dealing */ 1421 if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers")) 1422 goto error_1; 1423 1424 /* a file of volumes that we have mounted */ 1425 if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes")) 1426 goto error_1; 1427 1428 return 0; 1429 error_1: 1430 remove_proc_subtree("fs/nfsfs", NULL); 1431 error_0: 1432 return -ENOMEM; 1433 } 1434 1435 /* 1436 * clean up the /proc/fs/nfsfs/ directory 1437 */ 1438 void nfs_fs_proc_exit(void) 1439 { 1440 remove_proc_subtree("fs/nfsfs", NULL); 1441 ida_destroy(&s_sysfs_ids); 1442 } 1443 1444 #endif /* CONFIG_PROC_FS */ 1445