nfs4client.c 34 KB

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