nfs4client.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338
  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. /**
  400. * nfs40_walk_client_list - Find server that recognizes a client ID
  401. *
  402. * @new: nfs_client with client ID to test
  403. * @result: OUT: found nfs_client, or new
  404. * @cred: credential to use for trunking test
  405. *
  406. * Returns zero, a negative errno, or a negative NFS4ERR status.
  407. * If zero is returned, an nfs_client pointer is planted in "result."
  408. *
  409. * NB: nfs40_walk_client_list() relies on the new nfs_client being
  410. * the last nfs_client on the list.
  411. */
  412. int nfs40_walk_client_list(struct nfs_client *new,
  413. struct nfs_client **result,
  414. struct rpc_cred *cred)
  415. {
  416. struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
  417. struct nfs_client *pos, *prev = NULL;
  418. struct nfs4_setclientid_res clid = {
  419. .clientid = new->cl_clientid,
  420. .confirm = new->cl_confirm,
  421. };
  422. int status = -NFS4ERR_STALE_CLIENTID;
  423. spin_lock(&nn->nfs_client_lock);
  424. list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
  425. if (pos->rpc_ops != new->rpc_ops)
  426. continue;
  427. if (pos->cl_minorversion != new->cl_minorversion)
  428. continue;
  429. /* If "pos" isn't marked ready, we can't trust the
  430. * remaining fields in "pos" */
  431. if (pos->cl_cons_state > NFS_CS_READY) {
  432. atomic_inc(&pos->cl_count);
  433. spin_unlock(&nn->nfs_client_lock);
  434. nfs_put_client(prev);
  435. prev = pos;
  436. status = nfs_wait_client_init_complete(pos);
  437. if (status < 0)
  438. goto out;
  439. status = -NFS4ERR_STALE_CLIENTID;
  440. spin_lock(&nn->nfs_client_lock);
  441. }
  442. if (pos->cl_cons_state != NFS_CS_READY)
  443. continue;
  444. if (pos->cl_clientid != new->cl_clientid)
  445. continue;
  446. if (!nfs4_match_client_owner_id(pos, new))
  447. continue;
  448. atomic_inc(&pos->cl_count);
  449. spin_unlock(&nn->nfs_client_lock);
  450. nfs_put_client(prev);
  451. prev = pos;
  452. status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
  453. switch (status) {
  454. case -NFS4ERR_STALE_CLIENTID:
  455. break;
  456. case 0:
  457. nfs4_swap_callback_idents(pos, new);
  458. prev = NULL;
  459. *result = pos;
  460. dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
  461. __func__, pos, atomic_read(&pos->cl_count));
  462. goto out;
  463. case -ERESTARTSYS:
  464. case -ETIMEDOUT:
  465. /* The callback path may have been inadvertently
  466. * changed. Schedule recovery!
  467. */
  468. nfs4_schedule_path_down_recovery(pos);
  469. default:
  470. goto out;
  471. }
  472. spin_lock(&nn->nfs_client_lock);
  473. }
  474. spin_unlock(&nn->nfs_client_lock);
  475. /* No match found. The server lost our clientid */
  476. out:
  477. nfs_put_client(prev);
  478. dprintk("NFS: <-- %s status = %d\n", __func__, status);
  479. return status;
  480. }
  481. #ifdef CONFIG_NFS_V4_1
  482. /*
  483. * Returns true if the client IDs match
  484. */
  485. static bool nfs4_match_clientids(u64 a, u64 b)
  486. {
  487. if (a != b) {
  488. dprintk("NFS: --> %s client ID %llx does not match %llx\n",
  489. __func__, a, b);
  490. return false;
  491. }
  492. dprintk("NFS: --> %s client ID %llx matches %llx\n",
  493. __func__, a, b);
  494. return true;
  495. }
  496. /*
  497. * Returns true if the server major ids match
  498. */
  499. static bool
  500. nfs4_check_serverowner_major_id(struct nfs41_server_owner *o1,
  501. struct nfs41_server_owner *o2)
  502. {
  503. if (o1->major_id_sz != o2->major_id_sz)
  504. goto out_major_mismatch;
  505. if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
  506. goto out_major_mismatch;
  507. dprintk("NFS: --> %s server owner major IDs match\n", __func__);
  508. return true;
  509. out_major_mismatch:
  510. dprintk("NFS: --> %s server owner major IDs do not match\n",
  511. __func__);
  512. return false;
  513. }
  514. /*
  515. * Returns true if server minor ids match
  516. */
  517. static bool
  518. nfs4_check_serverowner_minor_id(struct nfs41_server_owner *o1,
  519. struct nfs41_server_owner *o2)
  520. {
  521. /* Check eir_server_owner so_minor_id */
  522. if (o1->minor_id != o2->minor_id)
  523. goto out_minor_mismatch;
  524. dprintk("NFS: --> %s server owner minor IDs match\n", __func__);
  525. return true;
  526. out_minor_mismatch:
  527. dprintk("NFS: --> %s server owner minor IDs do not match\n", __func__);
  528. return false;
  529. }
  530. /*
  531. * Returns true if the server scopes match
  532. */
  533. static bool
  534. nfs4_check_server_scope(struct nfs41_server_scope *s1,
  535. struct nfs41_server_scope *s2)
  536. {
  537. if (s1->server_scope_sz != s2->server_scope_sz)
  538. goto out_scope_mismatch;
  539. if (memcmp(s1->server_scope, s2->server_scope,
  540. s1->server_scope_sz) != 0)
  541. goto out_scope_mismatch;
  542. dprintk("NFS: --> %s server scopes match\n", __func__);
  543. return true;
  544. out_scope_mismatch:
  545. dprintk("NFS: --> %s server scopes do not match\n",
  546. __func__);
  547. return false;
  548. }
  549. /**
  550. * nfs4_detect_session_trunking - Checks for session trunking.
  551. *
  552. * Called after a successful EXCHANGE_ID on a multi-addr connection.
  553. * Upon success, add the transport.
  554. *
  555. * @clp: original mount nfs_client
  556. * @res: result structure from an exchange_id using the original mount
  557. * nfs_client with a new multi_addr transport
  558. *
  559. * Returns zero on success, otherwise -EINVAL
  560. *
  561. * Note: since the exchange_id for the new multi_addr transport uses the
  562. * same nfs_client from the original mount, the cl_owner_id is reused,
  563. * so eir_clientowner is the same.
  564. */
  565. int nfs4_detect_session_trunking(struct nfs_client *clp,
  566. struct nfs41_exchange_id_res *res,
  567. struct rpc_xprt *xprt)
  568. {
  569. /* Check eir_clientid */
  570. if (!nfs4_match_clientids(clp->cl_clientid, res->clientid))
  571. goto out_err;
  572. /* Check eir_server_owner so_major_id */
  573. if (!nfs4_check_serverowner_major_id(clp->cl_serverowner,
  574. res->server_owner))
  575. goto out_err;
  576. /* Check eir_server_owner so_minor_id */
  577. if (!nfs4_check_serverowner_minor_id(clp->cl_serverowner,
  578. res->server_owner))
  579. goto out_err;
  580. /* Check eir_server_scope */
  581. if (!nfs4_check_server_scope(clp->cl_serverscope, res->server_scope))
  582. goto out_err;
  583. /* Session trunking passed, add the xprt */
  584. rpc_clnt_xprt_switch_add_xprt(clp->cl_rpcclient, xprt);
  585. pr_info("NFS: %s: Session trunking succeeded for %s\n",
  586. clp->cl_hostname,
  587. xprt->address_strings[RPC_DISPLAY_ADDR]);
  588. return 0;
  589. out_err:
  590. pr_info("NFS: %s: Session trunking failed for %s\n", clp->cl_hostname,
  591. xprt->address_strings[RPC_DISPLAY_ADDR]);
  592. return -EINVAL;
  593. }
  594. /**
  595. * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
  596. *
  597. * @new: nfs_client with client ID to test
  598. * @result: OUT: found nfs_client, or new
  599. * @cred: credential to use for trunking test
  600. *
  601. * Returns zero, a negative errno, or a negative NFS4ERR status.
  602. * If zero is returned, an nfs_client pointer is planted in "result."
  603. *
  604. * NB: nfs41_walk_client_list() relies on the new nfs_client being
  605. * the last nfs_client on the list.
  606. */
  607. int nfs41_walk_client_list(struct nfs_client *new,
  608. struct nfs_client **result,
  609. struct rpc_cred *cred)
  610. {
  611. struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
  612. struct nfs_client *pos, *prev = NULL;
  613. int status = -NFS4ERR_STALE_CLIENTID;
  614. spin_lock(&nn->nfs_client_lock);
  615. list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
  616. if (pos == new)
  617. goto found;
  618. if (pos->rpc_ops != new->rpc_ops)
  619. continue;
  620. if (pos->cl_minorversion != new->cl_minorversion)
  621. continue;
  622. /* If "pos" isn't marked ready, we can't trust the
  623. * remaining fields in "pos", especially the client
  624. * ID and serverowner fields. Wait for CREATE_SESSION
  625. * to finish. */
  626. if (pos->cl_cons_state > NFS_CS_READY) {
  627. atomic_inc(&pos->cl_count);
  628. spin_unlock(&nn->nfs_client_lock);
  629. nfs_put_client(prev);
  630. prev = pos;
  631. status = nfs_wait_client_init_complete(pos);
  632. spin_lock(&nn->nfs_client_lock);
  633. if (status < 0)
  634. break;
  635. status = -NFS4ERR_STALE_CLIENTID;
  636. }
  637. if (pos->cl_cons_state != NFS_CS_READY)
  638. continue;
  639. if (!nfs4_match_clientids(pos->cl_clientid, new->cl_clientid))
  640. continue;
  641. /*
  642. * Note that session trunking is just a special subcase of
  643. * client id trunking. In either case, we want to fall back
  644. * to using the existing nfs_client.
  645. */
  646. if (!nfs4_check_serverowner_major_id(pos->cl_serverowner,
  647. new->cl_serverowner))
  648. continue;
  649. /* Unlike NFSv4.0, we know that NFSv4.1 always uses the
  650. * uniform string, however someone might switch the
  651. * uniquifier string on us.
  652. */
  653. if (!nfs4_match_client_owner_id(pos, new))
  654. continue;
  655. found:
  656. atomic_inc(&pos->cl_count);
  657. *result = pos;
  658. status = 0;
  659. dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
  660. __func__, pos, atomic_read(&pos->cl_count));
  661. break;
  662. }
  663. spin_unlock(&nn->nfs_client_lock);
  664. dprintk("NFS: <-- %s status = %d\n", __func__, status);
  665. nfs_put_client(prev);
  666. return status;
  667. }
  668. #endif /* CONFIG_NFS_V4_1 */
  669. static void nfs4_destroy_server(struct nfs_server *server)
  670. {
  671. nfs_server_return_all_delegations(server);
  672. unset_pnfs_layoutdriver(server);
  673. nfs4_purge_state_owners(server);
  674. }
  675. /*
  676. * NFSv4.0 callback thread helper
  677. *
  678. * Find a client by callback identifier
  679. */
  680. struct nfs_client *
  681. nfs4_find_client_ident(struct net *net, int cb_ident)
  682. {
  683. struct nfs_client *clp;
  684. struct nfs_net *nn = net_generic(net, nfs_net_id);
  685. spin_lock(&nn->nfs_client_lock);
  686. clp = idr_find(&nn->cb_ident_idr, cb_ident);
  687. if (clp)
  688. atomic_inc(&clp->cl_count);
  689. spin_unlock(&nn->nfs_client_lock);
  690. return clp;
  691. }
  692. #if defined(CONFIG_NFS_V4_1)
  693. /* Common match routine for v4.0 and v4.1 callback services */
  694. static bool nfs4_cb_match_client(const struct sockaddr *addr,
  695. struct nfs_client *clp, u32 minorversion)
  696. {
  697. struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  698. /* Don't match clients that failed to initialise */
  699. if (!(clp->cl_cons_state == NFS_CS_READY ||
  700. clp->cl_cons_state == NFS_CS_SESSION_INITING))
  701. return false;
  702. smp_rmb();
  703. /* Match the version and minorversion */
  704. if (clp->rpc_ops->version != 4 ||
  705. clp->cl_minorversion != minorversion)
  706. return false;
  707. /* Match only the IP address, not the port number */
  708. return rpc_cmp_addr(addr, clap);
  709. }
  710. /*
  711. * NFSv4.1 callback thread helper
  712. * For CB_COMPOUND calls, find a client by IP address, protocol version,
  713. * minorversion, and sessionID
  714. *
  715. * Returns NULL if no such client
  716. */
  717. struct nfs_client *
  718. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  719. struct nfs4_sessionid *sid, u32 minorversion)
  720. {
  721. struct nfs_client *clp;
  722. struct nfs_net *nn = net_generic(net, nfs_net_id);
  723. spin_lock(&nn->nfs_client_lock);
  724. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  725. if (nfs4_cb_match_client(addr, clp, minorversion) == false)
  726. continue;
  727. if (!nfs4_has_session(clp))
  728. continue;
  729. /* Match sessionid*/
  730. if (memcmp(clp->cl_session->sess_id.data,
  731. sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
  732. continue;
  733. atomic_inc(&clp->cl_count);
  734. spin_unlock(&nn->nfs_client_lock);
  735. return clp;
  736. }
  737. spin_unlock(&nn->nfs_client_lock);
  738. return NULL;
  739. }
  740. #else /* CONFIG_NFS_V4_1 */
  741. struct nfs_client *
  742. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  743. struct nfs4_sessionid *sid, u32 minorversion)
  744. {
  745. return NULL;
  746. }
  747. #endif /* CONFIG_NFS_V4_1 */
  748. /*
  749. * Set up an NFS4 client
  750. */
  751. static int nfs4_set_client(struct nfs_server *server,
  752. const char *hostname,
  753. const struct sockaddr *addr,
  754. const size_t addrlen,
  755. const char *ip_addr,
  756. rpc_authflavor_t authflavour,
  757. int proto, const struct rpc_timeout *timeparms,
  758. u32 minorversion, struct net *net)
  759. {
  760. struct nfs_client_initdata cl_init = {
  761. .hostname = hostname,
  762. .addr = addr,
  763. .addrlen = addrlen,
  764. .ip_addr = ip_addr,
  765. .nfs_mod = &nfs_v4,
  766. .proto = proto,
  767. .minorversion = minorversion,
  768. .net = net,
  769. .timeparms = timeparms,
  770. };
  771. struct nfs_client *clp;
  772. int error;
  773. dprintk("--> nfs4_set_client()\n");
  774. if (server->flags & NFS_MOUNT_NORESVPORT)
  775. set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
  776. if (server->options & NFS_OPTION_MIGRATION)
  777. set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
  778. /* Allocate or find a client reference we can use */
  779. clp = nfs_get_client(&cl_init, authflavour);
  780. if (IS_ERR(clp)) {
  781. error = PTR_ERR(clp);
  782. goto error;
  783. }
  784. if (server->nfs_client == clp) {
  785. error = -ELOOP;
  786. goto error;
  787. }
  788. /*
  789. * Query for the lease time on clientid setup or renewal
  790. *
  791. * Note that this will be set on nfs_clients that were created
  792. * only for the DS role and did not set this bit, but now will
  793. * serve a dual role.
  794. */
  795. set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
  796. server->nfs_client = clp;
  797. dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
  798. return 0;
  799. error:
  800. dprintk("<-- nfs4_set_client() = xerror %d\n", error);
  801. return error;
  802. }
  803. /*
  804. * Set up a pNFS Data Server client.
  805. *
  806. * Return any existing nfs_client that matches server address,port,version
  807. * and minorversion.
  808. *
  809. * For a new nfs_client, use a soft mount (default), a low retrans and a
  810. * low timeout interval so that if a connection is lost, we retry through
  811. * the MDS.
  812. */
  813. struct nfs_client *nfs4_set_ds_client(struct nfs_server *mds_srv,
  814. const struct sockaddr *ds_addr, int ds_addrlen,
  815. int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans,
  816. u32 minor_version, rpc_authflavor_t au_flavor)
  817. {
  818. struct rpc_timeout ds_timeout;
  819. struct nfs_client *mds_clp = mds_srv->nfs_client;
  820. struct nfs_client_initdata cl_init = {
  821. .addr = ds_addr,
  822. .addrlen = ds_addrlen,
  823. .nodename = mds_clp->cl_rpcclient->cl_nodename,
  824. .ip_addr = mds_clp->cl_ipaddr,
  825. .nfs_mod = &nfs_v4,
  826. .proto = ds_proto,
  827. .minorversion = minor_version,
  828. .net = mds_clp->cl_net,
  829. .timeparms = &ds_timeout,
  830. };
  831. struct nfs_client *clp;
  832. char buf[INET6_ADDRSTRLEN + 1];
  833. if (rpc_ntop(ds_addr, buf, sizeof(buf)) <= 0)
  834. return ERR_PTR(-EINVAL);
  835. cl_init.hostname = buf;
  836. if (mds_srv->flags & NFS_MOUNT_NORESVPORT)
  837. __set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
  838. /*
  839. * Set an authflavor equual to the MDS value. Use the MDS nfs_client
  840. * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
  841. * (section 13.1 RFC 5661).
  842. */
  843. nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
  844. clp = nfs_get_client(&cl_init, au_flavor);
  845. dprintk("<-- %s %p\n", __func__, clp);
  846. return clp;
  847. }
  848. EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
  849. /*
  850. * Session has been established, and the client marked ready.
  851. * Set the mount rsize and wsize with negotiated fore channel
  852. * attributes which will be bound checked in nfs_server_set_fsinfo.
  853. */
  854. static void nfs4_session_set_rwsize(struct nfs_server *server)
  855. {
  856. #ifdef CONFIG_NFS_V4_1
  857. struct nfs4_session *sess;
  858. u32 server_resp_sz;
  859. u32 server_rqst_sz;
  860. if (!nfs4_has_session(server->nfs_client))
  861. return;
  862. sess = server->nfs_client->cl_session;
  863. server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
  864. server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
  865. if (!server->rsize || server->rsize > server_resp_sz)
  866. server->rsize = server_resp_sz;
  867. if (!server->wsize || server->wsize > server_rqst_sz)
  868. server->wsize = server_rqst_sz;
  869. #endif /* CONFIG_NFS_V4_1 */
  870. }
  871. static int nfs4_server_common_setup(struct nfs_server *server,
  872. struct nfs_fh *mntfh, bool auth_probe)
  873. {
  874. struct nfs_fattr *fattr;
  875. int error;
  876. /* data servers support only a subset of NFSv4.1 */
  877. if (is_ds_only_client(server->nfs_client))
  878. return -EPROTONOSUPPORT;
  879. fattr = nfs_alloc_fattr();
  880. if (fattr == NULL)
  881. return -ENOMEM;
  882. /* We must ensure the session is initialised first */
  883. error = nfs4_init_session(server->nfs_client);
  884. if (error < 0)
  885. goto out;
  886. /* Set the basic capabilities */
  887. server->caps |= server->nfs_client->cl_mvops->init_caps;
  888. if (server->flags & NFS_MOUNT_NORDIRPLUS)
  889. server->caps &= ~NFS_CAP_READDIRPLUS;
  890. /*
  891. * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
  892. * authentication.
  893. */
  894. if (nfs4_disable_idmapping &&
  895. server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
  896. server->caps |= NFS_CAP_UIDGID_NOMAP;
  897. /* Probe the root fh to retrieve its FSID and filehandle */
  898. error = nfs4_get_rootfh(server, mntfh, auth_probe);
  899. if (error < 0)
  900. goto out;
  901. dprintk("Server FSID: %llx:%llx\n",
  902. (unsigned long long) server->fsid.major,
  903. (unsigned long long) server->fsid.minor);
  904. nfs_display_fhandle(mntfh, "Pseudo-fs root FH");
  905. nfs4_session_set_rwsize(server);
  906. error = nfs_probe_fsinfo(server, mntfh, fattr);
  907. if (error < 0)
  908. goto out;
  909. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  910. server->namelen = NFS4_MAXNAMLEN;
  911. nfs_server_insert_lists(server);
  912. server->mount_time = jiffies;
  913. server->destroy = nfs4_destroy_server;
  914. out:
  915. nfs_free_fattr(fattr);
  916. return error;
  917. }
  918. /*
  919. * Create a version 4 volume record
  920. */
  921. static int nfs4_init_server(struct nfs_server *server,
  922. struct nfs_parsed_mount_data *data)
  923. {
  924. struct rpc_timeout timeparms;
  925. int error;
  926. dprintk("--> nfs4_init_server()\n");
  927. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  928. data->timeo, data->retrans);
  929. /* Initialise the client representation from the mount data */
  930. server->flags = data->flags;
  931. server->options = data->options;
  932. server->auth_info = data->auth_info;
  933. /* Use the first specified auth flavor. If this flavor isn't
  934. * allowed by the server, use the SECINFO path to try the
  935. * other specified flavors */
  936. if (data->auth_info.flavor_len >= 1)
  937. data->selected_flavor = data->auth_info.flavors[0];
  938. else
  939. data->selected_flavor = RPC_AUTH_UNIX;
  940. /* Get a client record */
  941. error = nfs4_set_client(server,
  942. data->nfs_server.hostname,
  943. (const struct sockaddr *)&data->nfs_server.address,
  944. data->nfs_server.addrlen,
  945. data->client_address,
  946. data->selected_flavor,
  947. data->nfs_server.protocol,
  948. &timeparms,
  949. data->minorversion,
  950. data->net);
  951. if (error < 0)
  952. goto error;
  953. if (data->rsize)
  954. server->rsize = nfs_block_size(data->rsize, NULL);
  955. if (data->wsize)
  956. server->wsize = nfs_block_size(data->wsize, NULL);
  957. server->acregmin = data->acregmin * HZ;
  958. server->acregmax = data->acregmax * HZ;
  959. server->acdirmin = data->acdirmin * HZ;
  960. server->acdirmax = data->acdirmax * HZ;
  961. server->port = data->nfs_server.port;
  962. error = nfs_init_server_rpcclient(server, &timeparms,
  963. data->selected_flavor);
  964. error:
  965. /* Done */
  966. dprintk("<-- nfs4_init_server() = %d\n", error);
  967. return error;
  968. }
  969. /*
  970. * Create a version 4 volume record
  971. * - keyed on server and FSID
  972. */
  973. /*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
  974. struct nfs_fh *mntfh)*/
  975. struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
  976. struct nfs_subversion *nfs_mod)
  977. {
  978. struct nfs_server *server;
  979. bool auth_probe;
  980. int error;
  981. dprintk("--> nfs4_create_server()\n");
  982. server = nfs_alloc_server();
  983. if (!server)
  984. return ERR_PTR(-ENOMEM);
  985. auth_probe = mount_info->parsed->auth_info.flavor_len < 1;
  986. /* set up the general RPC client */
  987. error = nfs4_init_server(server, mount_info->parsed);
  988. if (error < 0)
  989. goto error;
  990. error = nfs4_server_common_setup(server, mount_info->mntfh, auth_probe);
  991. if (error < 0)
  992. goto error;
  993. dprintk("<-- nfs4_create_server() = %p\n", server);
  994. return server;
  995. error:
  996. nfs_free_server(server);
  997. dprintk("<-- nfs4_create_server() = error %d\n", error);
  998. return ERR_PTR(error);
  999. }
  1000. /*
  1001. * Create an NFS4 referral server record
  1002. */
  1003. struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
  1004. struct nfs_fh *mntfh)
  1005. {
  1006. struct nfs_client *parent_client;
  1007. struct nfs_server *server, *parent_server;
  1008. bool auth_probe;
  1009. int error;
  1010. dprintk("--> nfs4_create_referral_server()\n");
  1011. server = nfs_alloc_server();
  1012. if (!server)
  1013. return ERR_PTR(-ENOMEM);
  1014. parent_server = NFS_SB(data->sb);
  1015. parent_client = parent_server->nfs_client;
  1016. /* Initialise the client representation from the parent server */
  1017. nfs_server_copy_userdata(server, parent_server);
  1018. /* Get a client representation.
  1019. * Note: NFSv4 always uses TCP, */
  1020. error = nfs4_set_client(server, data->hostname,
  1021. data->addr,
  1022. data->addrlen,
  1023. parent_client->cl_ipaddr,
  1024. data->authflavor,
  1025. rpc_protocol(parent_server->client),
  1026. parent_server->client->cl_timeout,
  1027. parent_client->cl_mvops->minor_version,
  1028. parent_client->cl_net);
  1029. if (error < 0)
  1030. goto error;
  1031. error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
  1032. if (error < 0)
  1033. goto error;
  1034. auth_probe = parent_server->auth_info.flavor_len < 1;
  1035. error = nfs4_server_common_setup(server, mntfh, auth_probe);
  1036. if (error < 0)
  1037. goto error;
  1038. dprintk("<-- nfs_create_referral_server() = %p\n", server);
  1039. return server;
  1040. error:
  1041. nfs_free_server(server);
  1042. dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
  1043. return ERR_PTR(error);
  1044. }
  1045. /*
  1046. * Grab the destination's particulars, including lease expiry time.
  1047. *
  1048. * Returns zero if probe succeeded and retrieved FSID matches the FSID
  1049. * we have cached.
  1050. */
  1051. static int nfs_probe_destination(struct nfs_server *server)
  1052. {
  1053. struct inode *inode = d_inode(server->super->s_root);
  1054. struct nfs_fattr *fattr;
  1055. int error;
  1056. fattr = nfs_alloc_fattr();
  1057. if (fattr == NULL)
  1058. return -ENOMEM;
  1059. /* Sanity: the probe won't work if the destination server
  1060. * does not recognize the migrated FH. */
  1061. error = nfs_probe_fsinfo(server, NFS_FH(inode), fattr);
  1062. nfs_free_fattr(fattr);
  1063. return error;
  1064. }
  1065. /**
  1066. * nfs4_update_server - Move an nfs_server to a different nfs_client
  1067. *
  1068. * @server: represents FSID to be moved
  1069. * @hostname: new end-point's hostname
  1070. * @sap: new end-point's socket address
  1071. * @salen: size of "sap"
  1072. * @net: net namespace
  1073. *
  1074. * The nfs_server must be quiescent before this function is invoked.
  1075. * Either its session is drained (NFSv4.1+), or its transport is
  1076. * plugged and drained (NFSv4.0).
  1077. *
  1078. * Returns zero on success, or a negative errno value.
  1079. */
  1080. int nfs4_update_server(struct nfs_server *server, const char *hostname,
  1081. struct sockaddr *sap, size_t salen, struct net *net)
  1082. {
  1083. struct nfs_client *clp = server->nfs_client;
  1084. struct rpc_clnt *clnt = server->client;
  1085. struct xprt_create xargs = {
  1086. .ident = clp->cl_proto,
  1087. .net = net,
  1088. .dstaddr = sap,
  1089. .addrlen = salen,
  1090. .servername = hostname,
  1091. };
  1092. char buf[INET6_ADDRSTRLEN + 1];
  1093. struct sockaddr_storage address;
  1094. struct sockaddr *localaddr = (struct sockaddr *)&address;
  1095. int error;
  1096. dprintk("--> %s: move FSID %llx:%llx to \"%s\")\n", __func__,
  1097. (unsigned long long)server->fsid.major,
  1098. (unsigned long long)server->fsid.minor,
  1099. hostname);
  1100. error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout);
  1101. if (error != 0) {
  1102. dprintk("<-- %s(): rpc_switch_client_transport returned %d\n",
  1103. __func__, error);
  1104. goto out;
  1105. }
  1106. error = rpc_localaddr(clnt, localaddr, sizeof(address));
  1107. if (error != 0) {
  1108. dprintk("<-- %s(): rpc_localaddr returned %d\n",
  1109. __func__, error);
  1110. goto out;
  1111. }
  1112. error = -EAFNOSUPPORT;
  1113. if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0) {
  1114. dprintk("<-- %s(): rpc_ntop returned %d\n",
  1115. __func__, error);
  1116. goto out;
  1117. }
  1118. nfs_server_remove_lists(server);
  1119. error = nfs4_set_client(server, hostname, sap, salen, buf,
  1120. clp->cl_rpcclient->cl_auth->au_flavor,
  1121. clp->cl_proto, clnt->cl_timeout,
  1122. clp->cl_minorversion, net);
  1123. nfs_put_client(clp);
  1124. if (error != 0) {
  1125. nfs_server_insert_lists(server);
  1126. dprintk("<-- %s(): nfs4_set_client returned %d\n",
  1127. __func__, error);
  1128. goto out;
  1129. }
  1130. if (server->nfs_client->cl_hostname == NULL)
  1131. server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
  1132. nfs_server_insert_lists(server);
  1133. error = nfs_probe_destination(server);
  1134. if (error < 0)
  1135. goto out;
  1136. dprintk("<-- %s() succeeded\n", __func__);
  1137. out:
  1138. return error;
  1139. }