nfs4client.c 34 KB

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