Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
0004  * Written by David Howells (dhowells@redhat.com)
0005  */
0006 #include <linux/module.h>
0007 #include <linux/nfs_fs.h>
0008 #include <linux/nfs_mount.h>
0009 #include <linux/sunrpc/addr.h>
0010 #include <linux/sunrpc/auth.h>
0011 #include <linux/sunrpc/xprt.h>
0012 #include <linux/sunrpc/bc_xprt.h>
0013 #include <linux/sunrpc/rpc_pipe_fs.h>
0014 #include "internal.h"
0015 #include "callback.h"
0016 #include "delegation.h"
0017 #include "nfs4session.h"
0018 #include "nfs4idmap.h"
0019 #include "pnfs.h"
0020 #include "netns.h"
0021 
0022 #define NFSDBG_FACILITY     NFSDBG_CLIENT
0023 
0024 /*
0025  * Get a unique NFSv4.0 callback identifier which will be used
0026  * by the V4.0 callback service to lookup the nfs_client struct
0027  */
0028 static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
0029 {
0030     int ret = 0;
0031     struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
0032 
0033     if (clp->rpc_ops->version != 4 || minorversion != 0)
0034         return ret;
0035     idr_preload(GFP_KERNEL);
0036     spin_lock(&nn->nfs_client_lock);
0037     ret = idr_alloc(&nn->cb_ident_idr, clp, 1, 0, GFP_NOWAIT);
0038     if (ret >= 0)
0039         clp->cl_cb_ident = ret;
0040     spin_unlock(&nn->nfs_client_lock);
0041     idr_preload_end();
0042     return ret < 0 ? ret : 0;
0043 }
0044 
0045 #ifdef CONFIG_NFS_V4_1
0046 /*
0047  * Per auth flavor data server rpc clients
0048  */
0049 struct nfs4_ds_server {
0050     struct list_head    list;   /* ds_clp->cl_ds_clients */
0051     struct rpc_clnt     *rpc_clnt;
0052 };
0053 
0054 /**
0055  * nfs4_find_ds_client - Common lookup case for DS I/O
0056  * @ds_clp: pointer to the DS's nfs_client
0057  * @flavor: rpc auth flavour to match
0058  */
0059 static struct nfs4_ds_server *
0060 nfs4_find_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
0061 {
0062     struct nfs4_ds_server *dss;
0063 
0064     rcu_read_lock();
0065     list_for_each_entry_rcu(dss, &ds_clp->cl_ds_clients, list) {
0066         if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
0067             continue;
0068         goto out;
0069     }
0070     dss = NULL;
0071 out:
0072     rcu_read_unlock();
0073     return dss;
0074 }
0075 
0076 static struct nfs4_ds_server *
0077 nfs4_add_ds_client(struct nfs_client *ds_clp, rpc_authflavor_t flavor,
0078                struct nfs4_ds_server *new)
0079 {
0080     struct nfs4_ds_server *dss;
0081 
0082     spin_lock(&ds_clp->cl_lock);
0083     list_for_each_entry(dss, &ds_clp->cl_ds_clients, list) {
0084         if (dss->rpc_clnt->cl_auth->au_flavor != flavor)
0085             continue;
0086         goto out;
0087     }
0088     if (new)
0089         list_add_rcu(&new->list, &ds_clp->cl_ds_clients);
0090     dss = new;
0091 out:
0092     spin_unlock(&ds_clp->cl_lock); /* need some lock to protect list */
0093     return dss;
0094 }
0095 
0096 static struct nfs4_ds_server *
0097 nfs4_alloc_ds_server(struct nfs_client *ds_clp, rpc_authflavor_t flavor)
0098 {
0099     struct nfs4_ds_server *dss;
0100 
0101     dss = kmalloc(sizeof(*dss), GFP_NOFS);
0102     if (dss == NULL)
0103         return ERR_PTR(-ENOMEM);
0104 
0105     dss->rpc_clnt = rpc_clone_client_set_auth(ds_clp->cl_rpcclient, flavor);
0106     if (IS_ERR(dss->rpc_clnt)) {
0107         int err = PTR_ERR(dss->rpc_clnt);
0108         kfree (dss);
0109         return ERR_PTR(err);
0110     }
0111     INIT_LIST_HEAD(&dss->list);
0112 
0113     return dss;
0114 }
0115 
0116 static void
0117 nfs4_free_ds_server(struct nfs4_ds_server *dss)
0118 {
0119     rpc_release_client(dss->rpc_clnt);
0120     kfree(dss);
0121 }
0122 
0123 /**
0124  * nfs4_find_or_create_ds_client - Find or create a DS rpc client
0125  * @ds_clp: pointer to the DS's nfs_client
0126  * @inode: pointer to the inode
0127  *
0128  * Find or create a DS rpc client with th MDS server rpc client auth flavor
0129  * in the nfs_client cl_ds_clients list.
0130  */
0131 struct rpc_clnt *
0132 nfs4_find_or_create_ds_client(struct nfs_client *ds_clp, struct inode *inode)
0133 {
0134     struct nfs4_ds_server *dss, *new;
0135     rpc_authflavor_t flavor = NFS_SERVER(inode)->client->cl_auth->au_flavor;
0136 
0137     dss = nfs4_find_ds_client(ds_clp, flavor);
0138     if (dss != NULL)
0139         goto out;
0140     new = nfs4_alloc_ds_server(ds_clp, flavor);
0141     if (IS_ERR(new))
0142         return ERR_CAST(new);
0143     dss = nfs4_add_ds_client(ds_clp, flavor, new);
0144     if (dss != new)
0145         nfs4_free_ds_server(new);
0146 out:
0147     return dss->rpc_clnt;
0148 }
0149 EXPORT_SYMBOL_GPL(nfs4_find_or_create_ds_client);
0150 
0151 static void
0152 nfs4_shutdown_ds_clients(struct nfs_client *clp)
0153 {
0154     struct nfs4_ds_server *dss;
0155 
0156     while (!list_empty(&clp->cl_ds_clients)) {
0157         dss = list_entry(clp->cl_ds_clients.next,
0158                     struct nfs4_ds_server, list);
0159         list_del(&dss->list);
0160         rpc_shutdown_client(dss->rpc_clnt);
0161         kfree (dss);
0162     }
0163 }
0164 
0165 static void
0166 nfs4_cleanup_callback(struct nfs_client *clp)
0167 {
0168     struct nfs4_copy_state *cp_state;
0169 
0170     while (!list_empty(&clp->pending_cb_stateids)) {
0171         cp_state = list_entry(clp->pending_cb_stateids.next,
0172                     struct nfs4_copy_state, copies);
0173         list_del(&cp_state->copies);
0174         kfree(cp_state);
0175     }
0176 }
0177 
0178 void nfs41_shutdown_client(struct nfs_client *clp)
0179 {
0180     if (nfs4_has_session(clp)) {
0181         nfs4_cleanup_callback(clp);
0182         nfs4_shutdown_ds_clients(clp);
0183         nfs4_destroy_session(clp->cl_session);
0184         nfs4_destroy_clientid(clp);
0185     }
0186 
0187 }
0188 #endif  /* CONFIG_NFS_V4_1 */
0189 
0190 void nfs40_shutdown_client(struct nfs_client *clp)
0191 {
0192     if (clp->cl_slot_tbl) {
0193         nfs4_shutdown_slot_table(clp->cl_slot_tbl);
0194         kfree(clp->cl_slot_tbl);
0195     }
0196 }
0197 
0198 struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
0199 {
0200     char buf[INET6_ADDRSTRLEN + 1];
0201     const char *ip_addr = cl_init->ip_addr;
0202     struct nfs_client *clp = nfs_alloc_client(cl_init);
0203     int err;
0204 
0205     if (IS_ERR(clp))
0206         return clp;
0207 
0208     err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
0209     if (err)
0210         goto error;
0211 
0212     if (cl_init->minorversion > NFS4_MAX_MINOR_VERSION) {
0213         err = -EINVAL;
0214         goto error;
0215     }
0216 
0217     spin_lock_init(&clp->cl_lock);
0218     INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
0219     INIT_LIST_HEAD(&clp->cl_ds_clients);
0220     rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
0221     clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
0222     clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
0223     clp->cl_mig_gen = 1;
0224 #if IS_ENABLED(CONFIG_NFS_V4_1)
0225     init_waitqueue_head(&clp->cl_lock_waitq);
0226 #endif
0227     INIT_LIST_HEAD(&clp->pending_cb_stateids);
0228 
0229     if (cl_init->minorversion != 0)
0230         __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
0231     __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
0232     __set_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags);
0233 
0234     /*
0235      * Set up the connection to the server before we add add to the
0236      * global list.
0237      */
0238     err = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_GSS_KRB5I);
0239     if (err == -EINVAL)
0240         err = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
0241     if (err < 0)
0242         goto error;
0243 
0244     /* If no clientaddr= option was specified, find a usable cb address */
0245     if (ip_addr == NULL) {
0246         struct sockaddr_storage cb_addr;
0247         struct sockaddr *sap = (struct sockaddr *)&cb_addr;
0248 
0249         err = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
0250         if (err < 0)
0251             goto error;
0252         err = rpc_ntop(sap, buf, sizeof(buf));
0253         if (err < 0)
0254             goto error;
0255         ip_addr = (const char *)buf;
0256     }
0257     strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
0258 
0259     err = nfs_idmap_new(clp);
0260     if (err < 0) {
0261         dprintk("%s: failed to create idmapper. Error = %d\n",
0262             __func__, err);
0263         goto error;
0264     }
0265     __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
0266     return clp;
0267 
0268 error:
0269     nfs_free_client(clp);
0270     return ERR_PTR(err);
0271 }
0272 
0273 /*
0274  * Destroy the NFS4 callback service
0275  */
0276 static void nfs4_destroy_callback(struct nfs_client *clp)
0277 {
0278     if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
0279         nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
0280 }
0281 
0282 static void nfs4_shutdown_client(struct nfs_client *clp)
0283 {
0284     if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
0285         nfs4_kill_renewd(clp);
0286     clp->cl_mvops->shutdown_client(clp);
0287     nfs4_destroy_callback(clp);
0288     if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
0289         nfs_idmap_delete(clp);
0290 
0291     rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
0292     kfree(clp->cl_serverowner);
0293     kfree(clp->cl_serverscope);
0294     kfree(clp->cl_implid);
0295     kfree(clp->cl_owner_id);
0296 }
0297 
0298 void nfs4_free_client(struct nfs_client *clp)
0299 {
0300     nfs4_shutdown_client(clp);
0301     nfs_free_client(clp);
0302 }
0303 
0304 /*
0305  * Initialize the NFS4 callback service
0306  */
0307 static int nfs4_init_callback(struct nfs_client *clp)
0308 {
0309     struct rpc_xprt *xprt;
0310     int error;
0311 
0312     xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
0313 
0314     if (nfs4_has_session(clp)) {
0315         error = xprt_setup_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
0316         if (error < 0)
0317             return error;
0318     }
0319 
0320     error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
0321     if (error < 0) {
0322         dprintk("%s: failed to start callback. Error = %d\n",
0323             __func__, error);
0324         return error;
0325     }
0326     __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
0327 
0328     return 0;
0329 }
0330 
0331 /**
0332  * nfs40_init_client - nfs_client initialization tasks for NFSv4.0
0333  * @clp: nfs_client to initialize
0334  *
0335  * Returns zero on success, or a negative errno if some error occurred.
0336  */
0337 int nfs40_init_client(struct nfs_client *clp)
0338 {
0339     struct nfs4_slot_table *tbl;
0340     int ret;
0341 
0342     tbl = kzalloc(sizeof(*tbl), GFP_NOFS);
0343     if (tbl == NULL)
0344         return -ENOMEM;
0345 
0346     ret = nfs4_setup_slot_table(tbl, NFS4_MAX_SLOT_TABLE,
0347                     "NFSv4.0 transport Slot table");
0348     if (ret) {
0349         kfree(tbl);
0350         return ret;
0351     }
0352 
0353     clp->cl_slot_tbl = tbl;
0354     return 0;
0355 }
0356 
0357 #if defined(CONFIG_NFS_V4_1)
0358 
0359 /**
0360  * nfs41_init_client - nfs_client initialization tasks for NFSv4.1+
0361  * @clp: nfs_client to initialize
0362  *
0363  * Returns zero on success, or a negative errno if some error occurred.
0364  */
0365 int nfs41_init_client(struct nfs_client *clp)
0366 {
0367     struct nfs4_session *session = NULL;
0368 
0369     /*
0370      * Create the session and mark it expired.
0371      * When a SEQUENCE operation encounters the expired session
0372      * it will do session recovery to initialize it.
0373      */
0374     session = nfs4_alloc_session(clp);
0375     if (!session)
0376         return -ENOMEM;
0377 
0378     clp->cl_session = session;
0379 
0380     /*
0381      * The create session reply races with the server back
0382      * channel probe. Mark the client NFS_CS_SESSION_INITING
0383      * so that the client back channel can find the
0384      * nfs_client struct
0385      */
0386     nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
0387     return 0;
0388 }
0389 
0390 #endif  /* CONFIG_NFS_V4_1 */
0391 
0392 /*
0393  * Initialize the minor version specific parts of an NFS4 client record
0394  */
0395 static int nfs4_init_client_minor_version(struct nfs_client *clp)
0396 {
0397     int ret;
0398 
0399     ret = clp->cl_mvops->init_client(clp);
0400     if (ret)
0401         return ret;
0402     return nfs4_init_callback(clp);
0403 }
0404 
0405 static void nfs4_add_trunk(struct nfs_client *clp, struct nfs_client *old)
0406 {
0407     struct sockaddr_storage clp_addr, old_addr;
0408     struct sockaddr *clp_sap = (struct sockaddr *)&clp_addr;
0409     struct sockaddr *old_sap = (struct sockaddr *)&old_addr;
0410     size_t clp_salen;
0411     struct xprt_create xprt_args = {
0412         .ident = old->cl_proto,
0413         .net = old->cl_net,
0414         .servername = old->cl_hostname,
0415     };
0416 
0417     if (clp->cl_proto != old->cl_proto)
0418         return;
0419     clp_salen = rpc_peeraddr(clp->cl_rpcclient, clp_sap, sizeof(clp_addr));
0420     rpc_peeraddr(old->cl_rpcclient, old_sap, sizeof(old_addr));
0421 
0422     if (clp_addr.ss_family != old_addr.ss_family)
0423         return;
0424 
0425     xprt_args.dstaddr = clp_sap;
0426     xprt_args.addrlen = clp_salen;
0427 
0428     rpc_clnt_add_xprt(old->cl_rpcclient, &xprt_args,
0429               rpc_clnt_test_and_add_xprt, NULL);
0430 }
0431 
0432 /**
0433  * nfs4_init_client - Initialise an NFS4 client record
0434  *
0435  * @clp: nfs_client to initialise
0436  * @cl_init: pointer to nfs_client_initdata
0437  *
0438  * Returns pointer to an NFS client, or an ERR_PTR value.
0439  */
0440 struct nfs_client *nfs4_init_client(struct nfs_client *clp,
0441                     const struct nfs_client_initdata *cl_init)
0442 {
0443     struct nfs_client *old;
0444     int error;
0445 
0446     if (clp->cl_cons_state == NFS_CS_READY)
0447         /* the client is initialised already */
0448         return clp;
0449 
0450     error = nfs4_init_client_minor_version(clp);
0451     if (error < 0)
0452         goto error;
0453 
0454     error = nfs4_discover_server_trunking(clp, &old);
0455     if (error < 0)
0456         goto error;
0457 
0458     if (clp != old) {
0459         clp->cl_preserve_clid = true;
0460         /*
0461          * Mark the client as having failed initialization so other
0462          * processes walking the nfs_client_list in nfs_match_client()
0463          * won't try to use it.
0464          */
0465         nfs_mark_client_ready(clp, -EPERM);
0466         if (old->cl_mvops->session_trunk)
0467             nfs4_add_trunk(clp, old);
0468     }
0469     clear_bit(NFS_CS_TSM_POSSIBLE, &clp->cl_flags);
0470     nfs_put_client(clp);
0471     return old;
0472 
0473 error:
0474     nfs_mark_client_ready(clp, error);
0475     nfs_put_client(clp);
0476     return ERR_PTR(error);
0477 }
0478 
0479 /*
0480  * SETCLIENTID just did a callback update with the callback ident in
0481  * "drop," but server trunking discovery claims "drop" and "keep" are
0482  * actually the same server.  Swap the callback IDs so that "keep"
0483  * will continue to use the callback ident the server now knows about,
0484  * and so that "keep"'s original callback ident is destroyed when
0485  * "drop" is freed.
0486  */
0487 static void nfs4_swap_callback_idents(struct nfs_client *keep,
0488                       struct nfs_client *drop)
0489 {
0490     struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
0491     unsigned int save = keep->cl_cb_ident;
0492 
0493     if (keep->cl_cb_ident == drop->cl_cb_ident)
0494         return;
0495 
0496     dprintk("%s: keeping callback ident %u and dropping ident %u\n",
0497         __func__, keep->cl_cb_ident, drop->cl_cb_ident);
0498 
0499     spin_lock(&nn->nfs_client_lock);
0500 
0501     idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
0502     keep->cl_cb_ident = drop->cl_cb_ident;
0503 
0504     idr_replace(&nn->cb_ident_idr, drop, save);
0505     drop->cl_cb_ident = save;
0506 
0507     spin_unlock(&nn->nfs_client_lock);
0508 }
0509 
0510 static bool nfs4_match_client_owner_id(const struct nfs_client *clp1,
0511         const struct nfs_client *clp2)
0512 {
0513     if (clp1->cl_owner_id == NULL || clp2->cl_owner_id == NULL)
0514         return true;
0515     return strcmp(clp1->cl_owner_id, clp2->cl_owner_id) == 0;
0516 }
0517 
0518 static bool nfs4_same_verifier(nfs4_verifier *v1, nfs4_verifier *v2)
0519 {
0520     return memcmp(v1->data, v2->data, sizeof(v1->data)) == 0;
0521 }
0522 
0523 static int nfs4_match_client(struct nfs_client  *pos,  struct nfs_client *new,
0524                  struct nfs_client **prev, struct nfs_net *nn)
0525 {
0526     int status;
0527 
0528     if (pos->rpc_ops != new->rpc_ops)
0529         return 1;
0530 
0531     if (pos->cl_minorversion != new->cl_minorversion)
0532         return 1;
0533 
0534     /* If "pos" isn't marked ready, we can't trust the
0535      * remaining fields in "pos", especially the client
0536      * ID and serverowner fields.  Wait for CREATE_SESSION
0537      * to finish. */
0538     if (pos->cl_cons_state > NFS_CS_READY) {
0539         refcount_inc(&pos->cl_count);
0540         spin_unlock(&nn->nfs_client_lock);
0541 
0542         nfs_put_client(*prev);
0543         *prev = pos;
0544 
0545         status = nfs_wait_client_init_complete(pos);
0546         spin_lock(&nn->nfs_client_lock);
0547 
0548         if (status < 0)
0549             return status;
0550     }
0551 
0552     if (pos->cl_cons_state != NFS_CS_READY)
0553         return 1;
0554 
0555     if (pos->cl_clientid != new->cl_clientid)
0556         return 1;
0557 
0558     /* NFSv4.1 always uses the uniform string, however someone
0559      * might switch the uniquifier string on us.
0560      */
0561     if (!nfs4_match_client_owner_id(pos, new))
0562         return 1;
0563 
0564     return 0;
0565 }
0566 
0567 /**
0568  * nfs40_walk_client_list - Find server that recognizes a client ID
0569  *
0570  * @new: nfs_client with client ID to test
0571  * @result: OUT: found nfs_client, or new
0572  * @cred: credential to use for trunking test
0573  *
0574  * Returns zero, a negative errno, or a negative NFS4ERR status.
0575  * If zero is returned, an nfs_client pointer is planted in "result."
0576  *
0577  * NB: nfs40_walk_client_list() relies on the new nfs_client being
0578  *     the last nfs_client on the list.
0579  */
0580 int nfs40_walk_client_list(struct nfs_client *new,
0581                struct nfs_client **result,
0582                const struct cred *cred)
0583 {
0584     struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
0585     struct nfs_client *pos, *prev = NULL;
0586     struct nfs4_setclientid_res clid = {
0587         .clientid   = new->cl_clientid,
0588         .confirm    = new->cl_confirm,
0589     };
0590     int status = -NFS4ERR_STALE_CLIENTID;
0591 
0592     spin_lock(&nn->nfs_client_lock);
0593     list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
0594 
0595         if (pos == new)
0596             goto found;
0597 
0598         status = nfs4_match_client(pos, new, &prev, nn);
0599         if (status < 0)
0600             goto out_unlock;
0601         if (status != 0)
0602             continue;
0603         /*
0604          * We just sent a new SETCLIENTID, which should have
0605          * caused the server to return a new cl_confirm.  So if
0606          * cl_confirm is the same, then this is a different
0607          * server that just returned the same cl_confirm by
0608          * coincidence:
0609          */
0610         if ((new != pos) && nfs4_same_verifier(&pos->cl_confirm,
0611                                &new->cl_confirm))
0612             continue;
0613         /*
0614          * But if the cl_confirm's are different, then the only
0615          * way that a SETCLIENTID_CONFIRM to pos can succeed is
0616          * if new and pos point to the same server:
0617          */
0618 found:
0619         refcount_inc(&pos->cl_count);
0620         spin_unlock(&nn->nfs_client_lock);
0621 
0622         nfs_put_client(prev);
0623         prev = pos;
0624 
0625         status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
0626         switch (status) {
0627         case -NFS4ERR_STALE_CLIENTID:
0628             break;
0629         case 0:
0630             nfs4_swap_callback_idents(pos, new);
0631             pos->cl_confirm = new->cl_confirm;
0632             nfs_mark_client_ready(pos, NFS_CS_READY);
0633 
0634             prev = NULL;
0635             *result = pos;
0636             goto out;
0637         case -ERESTARTSYS:
0638         case -ETIMEDOUT:
0639             /* The callback path may have been inadvertently
0640              * changed. Schedule recovery!
0641              */
0642             nfs4_schedule_path_down_recovery(pos);
0643             goto out;
0644         default:
0645             goto out;
0646         }
0647 
0648         spin_lock(&nn->nfs_client_lock);
0649     }
0650 out_unlock:
0651     spin_unlock(&nn->nfs_client_lock);
0652 
0653     /* No match found. The server lost our clientid */
0654 out:
0655     nfs_put_client(prev);
0656     return status;
0657 }
0658 
0659 #ifdef CONFIG_NFS_V4_1
0660 /*
0661  * Returns true if the server major ids match
0662  */
0663 bool
0664 nfs4_check_serverowner_major_id(struct nfs41_server_owner *o1,
0665                 struct nfs41_server_owner *o2)
0666 {
0667     if (o1->major_id_sz != o2->major_id_sz)
0668         return false;
0669     return memcmp(o1->major_id, o2->major_id, o1->major_id_sz) == 0;
0670 }
0671 
0672 /*
0673  * Returns true if the server scopes match
0674  */
0675 static bool
0676 nfs4_check_server_scope(struct nfs41_server_scope *s1,
0677             struct nfs41_server_scope *s2)
0678 {
0679     if (s1->server_scope_sz != s2->server_scope_sz)
0680         return false;
0681     return memcmp(s1->server_scope, s2->server_scope,
0682                     s1->server_scope_sz) == 0;
0683 }
0684 
0685 /**
0686  * nfs4_detect_session_trunking - Checks for session trunking.
0687  * @clp:    original mount nfs_client
0688  * @res:    result structure from an exchange_id using the original mount
0689  *          nfs_client with a new multi_addr transport
0690  * @xprt:   pointer to the transport to add.
0691  *
0692  * Called after a successful EXCHANGE_ID on a multi-addr connection.
0693  * Upon success, add the transport.
0694  *
0695  * Returns zero on success, otherwise -EINVAL
0696  *
0697  * Note: since the exchange_id for the new multi_addr transport uses the
0698  * same nfs_client from the original mount, the cl_owner_id is reused,
0699  * so eir_clientowner is the same.
0700  */
0701 int nfs4_detect_session_trunking(struct nfs_client *clp,
0702                  struct nfs41_exchange_id_res *res,
0703                  struct rpc_xprt *xprt)
0704 {
0705     /* Check eir_clientid */
0706     if (clp->cl_clientid != res->clientid)
0707         goto out_err;
0708 
0709     /* Check eir_server_owner so_major_id */
0710     if (!nfs4_check_serverowner_major_id(clp->cl_serverowner,
0711                          res->server_owner))
0712         goto out_err;
0713 
0714     /* Check eir_server_owner so_minor_id */
0715     if (clp->cl_serverowner->minor_id != res->server_owner->minor_id)
0716         goto out_err;
0717 
0718     /* Check eir_server_scope */
0719     if (!nfs4_check_server_scope(clp->cl_serverscope, res->server_scope))
0720         goto out_err;
0721 
0722     pr_info("NFS:  %s: Session trunking succeeded for %s\n",
0723         clp->cl_hostname,
0724         xprt->address_strings[RPC_DISPLAY_ADDR]);
0725 
0726     return 0;
0727 out_err:
0728     pr_info("NFS:  %s: Session trunking failed for %s\n", clp->cl_hostname,
0729         xprt->address_strings[RPC_DISPLAY_ADDR]);
0730 
0731     return -EINVAL;
0732 }
0733 
0734 /**
0735  * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
0736  *
0737  * @new: nfs_client with client ID to test
0738  * @result: OUT: found nfs_client, or new
0739  * @cred: credential to use for trunking test
0740  *
0741  * Returns zero, a negative errno, or a negative NFS4ERR status.
0742  * If zero is returned, an nfs_client pointer is planted in "result."
0743  *
0744  * NB: nfs41_walk_client_list() relies on the new nfs_client being
0745  *     the last nfs_client on the list.
0746  */
0747 int nfs41_walk_client_list(struct nfs_client *new,
0748                struct nfs_client **result,
0749                const struct cred *cred)
0750 {
0751     struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
0752     struct nfs_client *pos, *prev = NULL;
0753     int status = -NFS4ERR_STALE_CLIENTID;
0754 
0755     spin_lock(&nn->nfs_client_lock);
0756     list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
0757 
0758         if (pos == new)
0759             goto found;
0760 
0761         status = nfs4_match_client(pos, new, &prev, nn);
0762         if (status < 0)
0763             goto out;
0764         if (status != 0)
0765             continue;
0766 
0767         /*
0768          * Note that session trunking is just a special subcase of
0769          * client id trunking. In either case, we want to fall back
0770          * to using the existing nfs_client.
0771          */
0772         if (!nfs4_check_serverowner_major_id(pos->cl_serverowner,
0773                              new->cl_serverowner))
0774             continue;
0775 
0776 found:
0777         refcount_inc(&pos->cl_count);
0778         *result = pos;
0779         status = 0;
0780         break;
0781     }
0782 
0783 out:
0784     spin_unlock(&nn->nfs_client_lock);
0785     nfs_put_client(prev);
0786     return status;
0787 }
0788 #endif  /* CONFIG_NFS_V4_1 */
0789 
0790 static void nfs4_destroy_server(struct nfs_server *server)
0791 {
0792     LIST_HEAD(freeme);
0793 
0794     nfs_server_return_all_delegations(server);
0795     unset_pnfs_layoutdriver(server);
0796     nfs4_purge_state_owners(server, &freeme);
0797     nfs4_free_state_owners(&freeme);
0798 }
0799 
0800 /*
0801  * NFSv4.0 callback thread helper
0802  *
0803  * Find a client by callback identifier
0804  */
0805 struct nfs_client *
0806 nfs4_find_client_ident(struct net *net, int cb_ident)
0807 {
0808     struct nfs_client *clp;
0809     struct nfs_net *nn = net_generic(net, nfs_net_id);
0810 
0811     spin_lock(&nn->nfs_client_lock);
0812     clp = idr_find(&nn->cb_ident_idr, cb_ident);
0813     if (clp)
0814         refcount_inc(&clp->cl_count);
0815     spin_unlock(&nn->nfs_client_lock);
0816     return clp;
0817 }
0818 
0819 #if defined(CONFIG_NFS_V4_1)
0820 /* Common match routine for v4.0 and v4.1 callback services */
0821 static bool nfs4_cb_match_client(const struct sockaddr *addr,
0822         struct nfs_client *clp, u32 minorversion)
0823 {
0824     struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
0825 
0826     /* Don't match clients that failed to initialise */
0827     if (!(clp->cl_cons_state == NFS_CS_READY ||
0828         clp->cl_cons_state == NFS_CS_SESSION_INITING))
0829         return false;
0830 
0831     smp_rmb();
0832 
0833     /* Match the version and minorversion */
0834     if (clp->rpc_ops->version != 4 ||
0835         clp->cl_minorversion != minorversion)
0836         return false;
0837 
0838     /* Match only the IP address, not the port number */
0839     return rpc_cmp_addr(addr, clap);
0840 }
0841 
0842 /*
0843  * NFSv4.1 callback thread helper
0844  * For CB_COMPOUND calls, find a client by IP address, protocol version,
0845  * minorversion, and sessionID
0846  *
0847  * Returns NULL if no such client
0848  */
0849 struct nfs_client *
0850 nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
0851                struct nfs4_sessionid *sid, u32 minorversion)
0852 {
0853     struct nfs_client *clp;
0854     struct nfs_net *nn = net_generic(net, nfs_net_id);
0855 
0856     spin_lock(&nn->nfs_client_lock);
0857     list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
0858         if (!nfs4_cb_match_client(addr, clp, minorversion))
0859             continue;
0860 
0861         if (!nfs4_has_session(clp))
0862             continue;
0863 
0864         /* Match sessionid*/
0865         if (memcmp(clp->cl_session->sess_id.data,
0866             sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
0867             continue;
0868 
0869         refcount_inc(&clp->cl_count);
0870         spin_unlock(&nn->nfs_client_lock);
0871         return clp;
0872     }
0873     spin_unlock(&nn->nfs_client_lock);
0874     return NULL;
0875 }
0876 
0877 #else /* CONFIG_NFS_V4_1 */
0878 
0879 struct nfs_client *
0880 nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
0881                struct nfs4_sessionid *sid, u32 minorversion)
0882 {
0883     return NULL;
0884 }
0885 #endif /* CONFIG_NFS_V4_1 */
0886 
0887 /*
0888  * Set up an NFS4 client
0889  */
0890 static int nfs4_set_client(struct nfs_server *server,
0891         const char *hostname,
0892         const struct sockaddr *addr,
0893         const size_t addrlen,
0894         const char *ip_addr,
0895         int proto, const struct rpc_timeout *timeparms,
0896         u32 minorversion, unsigned int nconnect,
0897         unsigned int max_connect,
0898         struct net *net)
0899 {
0900     struct nfs_client_initdata cl_init = {
0901         .hostname = hostname,
0902         .addr = addr,
0903         .addrlen = addrlen,
0904         .ip_addr = ip_addr,
0905         .nfs_mod = &nfs_v4,
0906         .proto = proto,
0907         .minorversion = minorversion,
0908         .net = net,
0909         .timeparms = timeparms,
0910         .cred = server->cred,
0911     };
0912     struct nfs_client *clp;
0913 
0914     if (minorversion == 0)
0915         __set_bit(NFS_CS_REUSEPORT, &cl_init.init_flags);
0916     else
0917         cl_init.max_connect = max_connect;
0918     if (proto == XPRT_TRANSPORT_TCP)
0919         cl_init.nconnect = nconnect;
0920 
0921     if (server->flags & NFS_MOUNT_NORESVPORT)
0922         __set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
0923     if (server->options & NFS_OPTION_MIGRATION)
0924         __set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
0925     if (test_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status))
0926         __set_bit(NFS_CS_TSM_POSSIBLE, &cl_init.init_flags);
0927     server->port = rpc_get_port(addr);
0928 
0929     /* Allocate or find a client reference we can use */
0930     clp = nfs_get_client(&cl_init);
0931     if (IS_ERR(clp))
0932         return PTR_ERR(clp);
0933 
0934     if (server->nfs_client == clp) {
0935         nfs_put_client(clp);
0936         return -ELOOP;
0937     }
0938 
0939     /*
0940      * Query for the lease time on clientid setup or renewal
0941      *
0942      * Note that this will be set on nfs_clients that were created
0943      * only for the DS role and did not set this bit, but now will
0944      * serve a dual role.
0945      */
0946     set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
0947 
0948     server->nfs_client = clp;
0949     return 0;
0950 }
0951 
0952 /*
0953  * Set up a pNFS Data Server client.
0954  *
0955  * Return any existing nfs_client that matches server address,port,version
0956  * and minorversion.
0957  *
0958  * For a new nfs_client, use a soft mount (default), a low retrans and a
0959  * low timeout interval so that if a connection is lost, we retry through
0960  * the MDS.
0961  */
0962 struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv,
0963         const struct sockaddr *ds_addr, int ds_addrlen,
0964         int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans,
0965         u32 minor_version)
0966 {
0967     struct rpc_timeout ds_timeout;
0968     struct nfs_client *mds_clp = mds_srv->nfs_client;
0969     struct nfs_client_initdata cl_init = {
0970         .addr = ds_addr,
0971         .addrlen = ds_addrlen,
0972         .nodename = mds_clp->cl_rpcclient->cl_nodename,
0973         .ip_addr = mds_clp->cl_ipaddr,
0974         .nfs_mod = &nfs_v4,
0975         .proto = ds_proto,
0976         .minorversion = minor_version,
0977         .net = mds_clp->cl_net,
0978         .timeparms = &ds_timeout,
0979         .cred = mds_srv->cred,
0980     };
0981     char buf[INET6_ADDRSTRLEN + 1];
0982 
0983     if (rpc_ntop(ds_addr, buf, sizeof(buf)) <= 0)
0984         return ERR_PTR(-EINVAL);
0985     cl_init.hostname = buf;
0986 
0987     if (mds_clp->cl_nconnect > 1 && ds_proto == XPRT_TRANSPORT_TCP) {
0988         cl_init.nconnect = mds_clp->cl_nconnect;
0989         cl_init.max_connect = NFS_MAX_TRANSPORTS;
0990     }
0991 
0992     if (mds_srv->flags & NFS_MOUNT_NORESVPORT)
0993         __set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
0994 
0995     /*
0996      * Set an authflavor equual to the MDS value. Use the MDS nfs_client
0997      * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
0998      * (section 13.1 RFC 5661).
0999      */
1000     nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
1001     return nfs_get_client(&cl_init);
1002 }
1003 EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
1004 
1005 /*
1006  * Session has been established, and the client marked ready.
1007  * Limit the mount rsize, wsize and dtsize using negotiated fore
1008  * channel attributes.
1009  */
1010 static void nfs4_session_limit_rwsize(struct nfs_server *server)
1011 {
1012 #ifdef CONFIG_NFS_V4_1
1013     struct nfs4_session *sess;
1014     u32 server_resp_sz;
1015     u32 server_rqst_sz;
1016 
1017     if (!nfs4_has_session(server->nfs_client))
1018         return;
1019     sess = server->nfs_client->cl_session;
1020     server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
1021     server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
1022 
1023     if (server->dtsize > server_resp_sz)
1024         server->dtsize = server_resp_sz;
1025     if (server->rsize > server_resp_sz)
1026         server->rsize = server_resp_sz;
1027     if (server->wsize > server_rqst_sz)
1028         server->wsize = server_rqst_sz;
1029 #endif /* CONFIG_NFS_V4_1 */
1030 }
1031 
1032 /*
1033  * Limit xattr sizes using the channel attributes.
1034  */
1035 static void nfs4_session_limit_xasize(struct nfs_server *server)
1036 {
1037 #ifdef CONFIG_NFS_V4_2
1038     struct nfs4_session *sess;
1039     u32 server_gxa_sz;
1040     u32 server_sxa_sz;
1041     u32 server_lxa_sz;
1042 
1043     if (!nfs4_has_session(server->nfs_client))
1044         return;
1045 
1046     sess = server->nfs_client->cl_session;
1047 
1048     server_gxa_sz = sess->fc_attrs.max_resp_sz - nfs42_maxgetxattr_overhead;
1049     server_sxa_sz = sess->fc_attrs.max_rqst_sz - nfs42_maxsetxattr_overhead;
1050     server_lxa_sz = sess->fc_attrs.max_resp_sz -
1051         nfs42_maxlistxattrs_overhead;
1052 
1053     if (server->gxasize > server_gxa_sz)
1054         server->gxasize = server_gxa_sz;
1055     if (server->sxasize > server_sxa_sz)
1056         server->sxasize = server_sxa_sz;
1057     if (server->lxasize > server_lxa_sz)
1058         server->lxasize = server_lxa_sz;
1059 #endif
1060 }
1061 
1062 void nfs4_server_set_init_caps(struct nfs_server *server)
1063 {
1064     /* Set the basic capabilities */
1065     server->caps |= server->nfs_client->cl_mvops->init_caps;
1066     if (server->flags & NFS_MOUNT_NORDIRPLUS)
1067             server->caps &= ~NFS_CAP_READDIRPLUS;
1068     if (server->nfs_client->cl_proto == XPRT_TRANSPORT_RDMA)
1069         server->caps &= ~NFS_CAP_READ_PLUS;
1070 
1071     /*
1072      * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
1073      * authentication.
1074      */
1075     if (nfs4_disable_idmapping &&
1076             server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
1077         server->caps |= NFS_CAP_UIDGID_NOMAP;
1078 }
1079 
1080 static int nfs4_server_common_setup(struct nfs_server *server,
1081         struct nfs_fh *mntfh, bool auth_probe)
1082 {
1083     int error;
1084 
1085     /* data servers support only a subset of NFSv4.1 */
1086     if (is_ds_only_client(server->nfs_client))
1087         return -EPROTONOSUPPORT;
1088 
1089     /* We must ensure the session is initialised first */
1090     error = nfs4_init_session(server->nfs_client);
1091     if (error < 0)
1092         goto out;
1093 
1094     nfs4_server_set_init_caps(server);
1095 
1096     /* Probe the root fh to retrieve its FSID and filehandle */
1097     error = nfs4_get_rootfh(server, mntfh, auth_probe);
1098     if (error < 0)
1099         goto out;
1100 
1101     dprintk("Server FSID: %llx:%llx\n",
1102             (unsigned long long) server->fsid.major,
1103             (unsigned long long) server->fsid.minor);
1104     nfs_display_fhandle(mntfh, "Pseudo-fs root FH");
1105 
1106     error = nfs_probe_server(server, mntfh);
1107     if (error < 0)
1108         goto out;
1109 
1110     nfs4_session_limit_rwsize(server);
1111     nfs4_session_limit_xasize(server);
1112 
1113     if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1114         server->namelen = NFS4_MAXNAMLEN;
1115 
1116     nfs_server_insert_lists(server);
1117     server->mount_time = jiffies;
1118     server->destroy = nfs4_destroy_server;
1119 out:
1120     return error;
1121 }
1122 
1123 /*
1124  * Create a version 4 volume record
1125  */
1126 static int nfs4_init_server(struct nfs_server *server, struct fs_context *fc)
1127 {
1128     struct nfs_fs_context *ctx = nfs_fc2context(fc);
1129     struct rpc_timeout timeparms;
1130     int error;
1131 
1132     nfs_init_timeout_values(&timeparms, ctx->nfs_server.protocol,
1133                 ctx->timeo, ctx->retrans);
1134 
1135     /* Initialise the client representation from the mount data */
1136     server->flags = ctx->flags;
1137     server->options = ctx->options;
1138     server->auth_info = ctx->auth_info;
1139 
1140     /* Use the first specified auth flavor. If this flavor isn't
1141      * allowed by the server, use the SECINFO path to try the
1142      * other specified flavors */
1143     if (ctx->auth_info.flavor_len >= 1)
1144         ctx->selected_flavor = ctx->auth_info.flavors[0];
1145     else
1146         ctx->selected_flavor = RPC_AUTH_UNIX;
1147 
1148     /* Get a client record */
1149     error = nfs4_set_client(server,
1150                 ctx->nfs_server.hostname,
1151                 &ctx->nfs_server.address,
1152                 ctx->nfs_server.addrlen,
1153                 ctx->client_address,
1154                 ctx->nfs_server.protocol,
1155                 &timeparms,
1156                 ctx->minorversion,
1157                 ctx->nfs_server.nconnect,
1158                 ctx->nfs_server.max_connect,
1159                 fc->net_ns);
1160     if (error < 0)
1161         return error;
1162 
1163     if (ctx->rsize)
1164         server->rsize = nfs_io_size(ctx->rsize, server->nfs_client->cl_proto);
1165     if (ctx->wsize)
1166         server->wsize = nfs_io_size(ctx->wsize, server->nfs_client->cl_proto);
1167 
1168     server->acregmin = ctx->acregmin * HZ;
1169     server->acregmax = ctx->acregmax * HZ;
1170     server->acdirmin = ctx->acdirmin * HZ;
1171     server->acdirmax = ctx->acdirmax * HZ;
1172     server->port     = ctx->nfs_server.port;
1173 
1174     return nfs_init_server_rpcclient(server, &timeparms,
1175                      ctx->selected_flavor);
1176 }
1177 
1178 /*
1179  * Create a version 4 volume record
1180  * - keyed on server and FSID
1181  */
1182 struct nfs_server *nfs4_create_server(struct fs_context *fc)
1183 {
1184     struct nfs_fs_context *ctx = nfs_fc2context(fc);
1185     struct nfs_server *server;
1186     bool auth_probe;
1187     int error;
1188 
1189     server = nfs_alloc_server();
1190     if (!server)
1191         return ERR_PTR(-ENOMEM);
1192 
1193     server->cred = get_cred(fc->cred);
1194 
1195     auth_probe = ctx->auth_info.flavor_len < 1;
1196 
1197     /* set up the general RPC client */
1198     error = nfs4_init_server(server, fc);
1199     if (error < 0)
1200         goto error;
1201 
1202     error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe);
1203     if (error < 0)
1204         goto error;
1205 
1206     return server;
1207 
1208 error:
1209     nfs_free_server(server);
1210     return ERR_PTR(error);
1211 }
1212 
1213 /*
1214  * Create an NFS4 referral server record
1215  */
1216 struct nfs_server *nfs4_create_referral_server(struct fs_context *fc)
1217 {
1218     struct nfs_fs_context *ctx = nfs_fc2context(fc);
1219     struct nfs_client *parent_client;
1220     struct nfs_server *server, *parent_server;
1221     bool auth_probe;
1222     int error;
1223 
1224     server = nfs_alloc_server();
1225     if (!server)
1226         return ERR_PTR(-ENOMEM);
1227 
1228     parent_server = NFS_SB(ctx->clone_data.sb);
1229     parent_client = parent_server->nfs_client;
1230 
1231     server->cred = get_cred(parent_server->cred);
1232 
1233     /* Initialise the client representation from the parent server */
1234     nfs_server_copy_userdata(server, parent_server);
1235 
1236     /* Get a client representation */
1237 #if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA)
1238     rpc_set_port(&ctx->nfs_server.address, NFS_RDMA_PORT);
1239     error = nfs4_set_client(server,
1240                 ctx->nfs_server.hostname,
1241                 &ctx->nfs_server.address,
1242                 ctx->nfs_server.addrlen,
1243                 parent_client->cl_ipaddr,
1244                 XPRT_TRANSPORT_RDMA,
1245                 parent_server->client->cl_timeout,
1246                 parent_client->cl_mvops->minor_version,
1247                 parent_client->cl_nconnect,
1248                 parent_client->cl_max_connect,
1249                 parent_client->cl_net);
1250     if (!error)
1251         goto init_server;
1252 #endif  /* IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA) */
1253 
1254     rpc_set_port(&ctx->nfs_server.address, NFS_PORT);
1255     error = nfs4_set_client(server,
1256                 ctx->nfs_server.hostname,
1257                 &ctx->nfs_server.address,
1258                 ctx->nfs_server.addrlen,
1259                 parent_client->cl_ipaddr,
1260                 XPRT_TRANSPORT_TCP,
1261                 parent_server->client->cl_timeout,
1262                 parent_client->cl_mvops->minor_version,
1263                 parent_client->cl_nconnect,
1264                 parent_client->cl_max_connect,
1265                 parent_client->cl_net);
1266     if (error < 0)
1267         goto error;
1268 
1269 #if IS_ENABLED(CONFIG_SUNRPC_XPRT_RDMA)
1270 init_server:
1271 #endif
1272     error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout,
1273                       ctx->selected_flavor);
1274     if (error < 0)
1275         goto error;
1276 
1277     auth_probe = parent_server->auth_info.flavor_len < 1;
1278 
1279     error = nfs4_server_common_setup(server, ctx->mntfh, auth_probe);
1280     if (error < 0)
1281         goto error;
1282 
1283     return server;
1284 
1285 error:
1286     nfs_free_server(server);
1287     return ERR_PTR(error);
1288 }
1289 
1290 /**
1291  * nfs4_update_server - Move an nfs_server to a different nfs_client
1292  *
1293  * @server: represents FSID to be moved
1294  * @hostname: new end-point's hostname
1295  * @sap: new end-point's socket address
1296  * @salen: size of "sap"
1297  * @net: net namespace
1298  *
1299  * The nfs_server must be quiescent before this function is invoked.
1300  * Either its session is drained (NFSv4.1+), or its transport is
1301  * plugged and drained (NFSv4.0).
1302  *
1303  * Returns zero on success, or a negative errno value.
1304  */
1305 int nfs4_update_server(struct nfs_server *server, const char *hostname,
1306                struct sockaddr *sap, size_t salen, struct net *net)
1307 {
1308     struct nfs_client *clp = server->nfs_client;
1309     struct rpc_clnt *clnt = server->client;
1310     struct xprt_create xargs = {
1311         .ident      = clp->cl_proto,
1312         .net        = net,
1313         .dstaddr    = sap,
1314         .addrlen    = salen,
1315         .servername = hostname,
1316     };
1317     char buf[INET6_ADDRSTRLEN + 1];
1318     struct sockaddr_storage address;
1319     struct sockaddr *localaddr = (struct sockaddr *)&address;
1320     int error;
1321 
1322     error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout);
1323     if (error != 0)
1324         return error;
1325 
1326     error = rpc_localaddr(clnt, localaddr, sizeof(address));
1327     if (error != 0)
1328         return error;
1329 
1330     if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0)
1331         return -EAFNOSUPPORT;
1332 
1333     nfs_server_remove_lists(server);
1334     set_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status);
1335     error = nfs4_set_client(server, hostname, sap, salen, buf,
1336                 clp->cl_proto, clnt->cl_timeout,
1337                 clp->cl_minorversion,
1338                 clp->cl_nconnect, clp->cl_max_connect, net);
1339     clear_bit(NFS_MIG_TSM_POSSIBLE, &server->mig_status);
1340     if (error != 0) {
1341         nfs_server_insert_lists(server);
1342         return error;
1343     }
1344     nfs_put_client(clp);
1345 
1346     if (server->nfs_client->cl_hostname == NULL) {
1347         server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
1348         if (server->nfs_client->cl_hostname == NULL)
1349             return -ENOMEM;
1350     }
1351     nfs_server_insert_lists(server);
1352 
1353     return nfs_probe_server(server, NFS_FH(d_inode(server->super->s_root)));
1354 }