nfs4client.c 31 KB

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