nfs4client.c 32 KB

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