nfs4client.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225
  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_idmap.h>
  8. #include <linux/nfs_mount.h>
  9. #include <linux/sunrpc/addr.h>
  10. #include <linux/sunrpc/auth.h>
  11. #include <linux/sunrpc/xprt.h>
  12. #include <linux/sunrpc/bc_xprt.h>
  13. #include <linux/sunrpc/rpc_pipe_fs.h>
  14. #include "internal.h"
  15. #include "callback.h"
  16. #include "delegation.h"
  17. #include "nfs4session.h"
  18. #include "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, 0, 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. return clp;
  179. error:
  180. nfs_free_client(clp);
  181. return ERR_PTR(err);
  182. }
  183. /*
  184. * Destroy the NFS4 callback service
  185. */
  186. static void nfs4_destroy_callback(struct nfs_client *clp)
  187. {
  188. if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
  189. nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
  190. }
  191. static void nfs4_shutdown_client(struct nfs_client *clp)
  192. {
  193. if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
  194. nfs4_kill_renewd(clp);
  195. clp->cl_mvops->shutdown_client(clp);
  196. nfs4_destroy_callback(clp);
  197. if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
  198. nfs_idmap_delete(clp);
  199. rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
  200. kfree(clp->cl_serverowner);
  201. kfree(clp->cl_serverscope);
  202. kfree(clp->cl_implid);
  203. }
  204. void nfs4_free_client(struct nfs_client *clp)
  205. {
  206. nfs4_shutdown_client(clp);
  207. nfs_free_client(clp);
  208. }
  209. /*
  210. * Initialize the NFS4 callback service
  211. */
  212. static int nfs4_init_callback(struct nfs_client *clp)
  213. {
  214. struct rpc_xprt *xprt;
  215. int error;
  216. xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
  217. if (nfs4_has_session(clp)) {
  218. error = xprt_setup_backchannel(xprt, NFS41_BC_MIN_CALLBACKS);
  219. if (error < 0)
  220. return error;
  221. }
  222. error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
  223. if (error < 0) {
  224. dprintk("%s: failed to start callback. Error = %d\n",
  225. __func__, error);
  226. return error;
  227. }
  228. __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
  229. return 0;
  230. }
  231. /**
  232. * nfs40_init_client - nfs_client initialization tasks for NFSv4.0
  233. * @clp - nfs_client to initialize
  234. *
  235. * Returns zero on success, or a negative errno if some error occurred.
  236. */
  237. int nfs40_init_client(struct nfs_client *clp)
  238. {
  239. struct nfs4_slot_table *tbl;
  240. int ret;
  241. tbl = kzalloc(sizeof(*tbl), GFP_NOFS);
  242. if (tbl == NULL)
  243. return -ENOMEM;
  244. ret = nfs4_setup_slot_table(tbl, NFS4_MAX_SLOT_TABLE,
  245. "NFSv4.0 transport Slot table");
  246. if (ret) {
  247. kfree(tbl);
  248. return ret;
  249. }
  250. clp->cl_slot_tbl = tbl;
  251. return 0;
  252. }
  253. #if defined(CONFIG_NFS_V4_1)
  254. /**
  255. * nfs41_init_client - nfs_client initialization tasks for NFSv4.1+
  256. * @clp - nfs_client to initialize
  257. *
  258. * Returns zero on success, or a negative errno if some error occurred.
  259. */
  260. int nfs41_init_client(struct nfs_client *clp)
  261. {
  262. struct nfs4_session *session = NULL;
  263. /*
  264. * Create the session and mark it expired.
  265. * When a SEQUENCE operation encounters the expired session
  266. * it will do session recovery to initialize it.
  267. */
  268. session = nfs4_alloc_session(clp);
  269. if (!session)
  270. return -ENOMEM;
  271. clp->cl_session = session;
  272. /*
  273. * The create session reply races with the server back
  274. * channel probe. Mark the client NFS_CS_SESSION_INITING
  275. * so that the client back channel can find the
  276. * nfs_client struct
  277. */
  278. nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
  279. return 0;
  280. }
  281. #endif /* CONFIG_NFS_V4_1 */
  282. /*
  283. * Initialize the minor version specific parts of an NFS4 client record
  284. */
  285. static int nfs4_init_client_minor_version(struct nfs_client *clp)
  286. {
  287. int ret;
  288. ret = clp->cl_mvops->init_client(clp);
  289. if (ret)
  290. return ret;
  291. return nfs4_init_callback(clp);
  292. }
  293. /**
  294. * nfs4_init_client - Initialise an NFS4 client record
  295. *
  296. * @clp: nfs_client to initialise
  297. * @timeparms: timeout parameters for underlying RPC transport
  298. * @ip_addr: callback IP address in presentation format
  299. * @authflavor: authentication flavor for underlying RPC transport
  300. *
  301. * Returns pointer to an NFS client, or an ERR_PTR value.
  302. */
  303. struct nfs_client *nfs4_init_client(struct nfs_client *clp,
  304. const struct rpc_timeout *timeparms,
  305. const char *ip_addr)
  306. {
  307. char buf[INET6_ADDRSTRLEN + 1];
  308. struct nfs_client *old;
  309. int error;
  310. if (clp->cl_cons_state == NFS_CS_READY) {
  311. /* the client is initialised already */
  312. dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
  313. return clp;
  314. }
  315. /* Check NFS protocol revision and initialize RPC op vector */
  316. clp->rpc_ops = &nfs_v4_clientops;
  317. if (clp->cl_minorversion != 0)
  318. __set_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags);
  319. __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
  320. __set_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags);
  321. error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_GSS_KRB5I);
  322. if (error == -EINVAL)
  323. error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX);
  324. if (error < 0)
  325. goto error;
  326. /* If no clientaddr= option was specified, find a usable cb address */
  327. if (ip_addr == NULL) {
  328. struct sockaddr_storage cb_addr;
  329. struct sockaddr *sap = (struct sockaddr *)&cb_addr;
  330. error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
  331. if (error < 0)
  332. goto error;
  333. error = rpc_ntop(sap, buf, sizeof(buf));
  334. if (error < 0)
  335. goto error;
  336. ip_addr = (const char *)buf;
  337. }
  338. strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
  339. error = nfs_idmap_new(clp);
  340. if (error < 0) {
  341. dprintk("%s: failed to create idmapper. Error = %d\n",
  342. __func__, error);
  343. goto error;
  344. }
  345. __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
  346. error = nfs4_init_client_minor_version(clp);
  347. if (error < 0)
  348. goto error;
  349. if (!nfs4_has_session(clp))
  350. nfs_mark_client_ready(clp, NFS_CS_READY);
  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. nfs_put_client(clp);
  357. return old;
  358. error:
  359. nfs_mark_client_ready(clp, error);
  360. nfs_put_client(clp);
  361. dprintk("<-- nfs4_init_client() = xerror %d\n", error);
  362. return ERR_PTR(error);
  363. }
  364. /*
  365. * SETCLIENTID just did a callback update with the callback ident in
  366. * "drop," but server trunking discovery claims "drop" and "keep" are
  367. * actually the same server. Swap the callback IDs so that "keep"
  368. * will continue to use the callback ident the server now knows about,
  369. * and so that "keep"'s original callback ident is destroyed when
  370. * "drop" is freed.
  371. */
  372. static void nfs4_swap_callback_idents(struct nfs_client *keep,
  373. struct nfs_client *drop)
  374. {
  375. struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
  376. unsigned int save = keep->cl_cb_ident;
  377. if (keep->cl_cb_ident == drop->cl_cb_ident)
  378. return;
  379. dprintk("%s: keeping callback ident %u and dropping ident %u\n",
  380. __func__, keep->cl_cb_ident, drop->cl_cb_ident);
  381. spin_lock(&nn->nfs_client_lock);
  382. idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
  383. keep->cl_cb_ident = drop->cl_cb_ident;
  384. idr_replace(&nn->cb_ident_idr, drop, save);
  385. drop->cl_cb_ident = save;
  386. spin_unlock(&nn->nfs_client_lock);
  387. }
  388. /**
  389. * nfs40_walk_client_list - Find server that recognizes a client ID
  390. *
  391. * @new: nfs_client with client ID to test
  392. * @result: OUT: found nfs_client, or new
  393. * @cred: credential to use for trunking test
  394. *
  395. * Returns zero, a negative errno, or a negative NFS4ERR status.
  396. * If zero is returned, an nfs_client pointer is planted in "result."
  397. *
  398. * NB: nfs40_walk_client_list() relies on the new nfs_client being
  399. * the last nfs_client on the list.
  400. */
  401. int nfs40_walk_client_list(struct nfs_client *new,
  402. struct nfs_client **result,
  403. struct rpc_cred *cred)
  404. {
  405. struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
  406. struct nfs_client *pos, *prev = NULL;
  407. struct nfs4_setclientid_res clid = {
  408. .clientid = new->cl_clientid,
  409. .confirm = new->cl_confirm,
  410. };
  411. int status = -NFS4ERR_STALE_CLIENTID;
  412. spin_lock(&nn->nfs_client_lock);
  413. list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
  414. if (pos->rpc_ops != new->rpc_ops)
  415. continue;
  416. if (pos->cl_proto != new->cl_proto)
  417. continue;
  418. if (pos->cl_minorversion != new->cl_minorversion)
  419. continue;
  420. /* If "pos" isn't marked ready, we can't trust the
  421. * remaining fields in "pos" */
  422. if (pos->cl_cons_state > NFS_CS_READY) {
  423. atomic_inc(&pos->cl_count);
  424. spin_unlock(&nn->nfs_client_lock);
  425. if (prev)
  426. nfs_put_client(prev);
  427. prev = pos;
  428. status = nfs_wait_client_init_complete(pos);
  429. if (status < 0)
  430. goto out;
  431. status = -NFS4ERR_STALE_CLIENTID;
  432. spin_lock(&nn->nfs_client_lock);
  433. }
  434. if (pos->cl_cons_state != NFS_CS_READY)
  435. continue;
  436. if (pos->cl_clientid != new->cl_clientid)
  437. continue;
  438. atomic_inc(&pos->cl_count);
  439. spin_unlock(&nn->nfs_client_lock);
  440. if (prev)
  441. nfs_put_client(prev);
  442. prev = pos;
  443. status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
  444. switch (status) {
  445. case -NFS4ERR_STALE_CLIENTID:
  446. break;
  447. case 0:
  448. nfs4_swap_callback_idents(pos, new);
  449. prev = NULL;
  450. *result = pos;
  451. dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
  452. __func__, pos, atomic_read(&pos->cl_count));
  453. goto out;
  454. case -ERESTARTSYS:
  455. case -ETIMEDOUT:
  456. /* The callback path may have been inadvertently
  457. * changed. Schedule recovery!
  458. */
  459. nfs4_schedule_path_down_recovery(pos);
  460. default:
  461. goto out;
  462. }
  463. spin_lock(&nn->nfs_client_lock);
  464. }
  465. spin_unlock(&nn->nfs_client_lock);
  466. /* No match found. The server lost our clientid */
  467. out:
  468. if (prev)
  469. nfs_put_client(prev);
  470. dprintk("NFS: <-- %s status = %d\n", __func__, status);
  471. return status;
  472. }
  473. #ifdef CONFIG_NFS_V4_1
  474. /*
  475. * Returns true if the client IDs match
  476. */
  477. static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
  478. {
  479. if (a->cl_clientid != b->cl_clientid) {
  480. dprintk("NFS: --> %s client ID %llx does not match %llx\n",
  481. __func__, a->cl_clientid, b->cl_clientid);
  482. return false;
  483. }
  484. dprintk("NFS: --> %s client ID %llx matches %llx\n",
  485. __func__, a->cl_clientid, b->cl_clientid);
  486. return true;
  487. }
  488. /*
  489. * Returns true if the server owners match
  490. */
  491. static bool
  492. nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
  493. {
  494. struct nfs41_server_owner *o1 = a->cl_serverowner;
  495. struct nfs41_server_owner *o2 = b->cl_serverowner;
  496. if (o1->minor_id != o2->minor_id) {
  497. dprintk("NFS: --> %s server owner minor IDs do not match\n",
  498. __func__);
  499. return false;
  500. }
  501. if (o1->major_id_sz != o2->major_id_sz)
  502. goto out_major_mismatch;
  503. if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
  504. goto out_major_mismatch;
  505. dprintk("NFS: --> %s server owners match\n", __func__);
  506. return true;
  507. out_major_mismatch:
  508. dprintk("NFS: --> %s server owner major IDs do not match\n",
  509. __func__);
  510. return false;
  511. }
  512. /**
  513. * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
  514. *
  515. * @new: nfs_client with client ID to test
  516. * @result: OUT: found nfs_client, or new
  517. * @cred: credential to use for trunking test
  518. *
  519. * Returns zero, a negative errno, or a negative NFS4ERR status.
  520. * If zero is returned, an nfs_client pointer is planted in "result."
  521. *
  522. * NB: nfs41_walk_client_list() relies on the new nfs_client being
  523. * the last nfs_client on the list.
  524. */
  525. int nfs41_walk_client_list(struct nfs_client *new,
  526. struct nfs_client **result,
  527. struct rpc_cred *cred)
  528. {
  529. struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
  530. struct nfs_client *pos, *prev = NULL;
  531. int status = -NFS4ERR_STALE_CLIENTID;
  532. spin_lock(&nn->nfs_client_lock);
  533. list_for_each_entry(pos, &nn->nfs_client_list, cl_share_link) {
  534. if (pos->rpc_ops != new->rpc_ops)
  535. continue;
  536. if (pos->cl_proto != new->cl_proto)
  537. continue;
  538. if (pos->cl_minorversion != new->cl_minorversion)
  539. continue;
  540. /* If "pos" isn't marked ready, we can't trust the
  541. * remaining fields in "pos", especially the client
  542. * ID and serverowner fields. Wait for CREATE_SESSION
  543. * to finish. */
  544. if (pos->cl_cons_state > NFS_CS_READY) {
  545. atomic_inc(&pos->cl_count);
  546. spin_unlock(&nn->nfs_client_lock);
  547. if (prev)
  548. nfs_put_client(prev);
  549. prev = pos;
  550. status = nfs_wait_client_init_complete(pos);
  551. if (status == 0) {
  552. nfs4_schedule_lease_recovery(pos);
  553. status = nfs4_wait_clnt_recover(pos);
  554. }
  555. spin_lock(&nn->nfs_client_lock);
  556. if (status < 0)
  557. break;
  558. status = -NFS4ERR_STALE_CLIENTID;
  559. }
  560. if (pos->cl_cons_state != NFS_CS_READY)
  561. continue;
  562. if (!nfs4_match_clientids(pos, new))
  563. continue;
  564. if (!nfs4_match_serverowners(pos, new))
  565. continue;
  566. atomic_inc(&pos->cl_count);
  567. *result = pos;
  568. status = 0;
  569. dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
  570. __func__, pos, atomic_read(&pos->cl_count));
  571. break;
  572. }
  573. /* No matching nfs_client found. */
  574. spin_unlock(&nn->nfs_client_lock);
  575. dprintk("NFS: <-- %s status = %d\n", __func__, status);
  576. if (prev)
  577. nfs_put_client(prev);
  578. return status;
  579. }
  580. #endif /* CONFIG_NFS_V4_1 */
  581. static void nfs4_destroy_server(struct nfs_server *server)
  582. {
  583. nfs_server_return_all_delegations(server);
  584. unset_pnfs_layoutdriver(server);
  585. nfs4_purge_state_owners(server);
  586. }
  587. /*
  588. * NFSv4.0 callback thread helper
  589. *
  590. * Find a client by callback identifier
  591. */
  592. struct nfs_client *
  593. nfs4_find_client_ident(struct net *net, int cb_ident)
  594. {
  595. struct nfs_client *clp;
  596. struct nfs_net *nn = net_generic(net, nfs_net_id);
  597. spin_lock(&nn->nfs_client_lock);
  598. clp = idr_find(&nn->cb_ident_idr, cb_ident);
  599. if (clp)
  600. atomic_inc(&clp->cl_count);
  601. spin_unlock(&nn->nfs_client_lock);
  602. return clp;
  603. }
  604. #if defined(CONFIG_NFS_V4_1)
  605. /* Common match routine for v4.0 and v4.1 callback services */
  606. static bool nfs4_cb_match_client(const struct sockaddr *addr,
  607. struct nfs_client *clp, u32 minorversion)
  608. {
  609. struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  610. /* Don't match clients that failed to initialise */
  611. if (!(clp->cl_cons_state == NFS_CS_READY ||
  612. clp->cl_cons_state == NFS_CS_SESSION_INITING))
  613. return false;
  614. smp_rmb();
  615. /* Match the version and minorversion */
  616. if (clp->rpc_ops->version != 4 ||
  617. clp->cl_minorversion != minorversion)
  618. return false;
  619. /* Match only the IP address, not the port number */
  620. if (!nfs_sockaddr_match_ipaddr(addr, clap))
  621. return false;
  622. return true;
  623. }
  624. /*
  625. * NFSv4.1 callback thread helper
  626. * For CB_COMPOUND calls, find a client by IP address, protocol version,
  627. * minorversion, and sessionID
  628. *
  629. * Returns NULL if no such client
  630. */
  631. struct nfs_client *
  632. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  633. struct nfs4_sessionid *sid, u32 minorversion)
  634. {
  635. struct nfs_client *clp;
  636. struct nfs_net *nn = net_generic(net, nfs_net_id);
  637. spin_lock(&nn->nfs_client_lock);
  638. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  639. if (nfs4_cb_match_client(addr, clp, minorversion) == false)
  640. continue;
  641. if (!nfs4_has_session(clp))
  642. continue;
  643. /* Match sessionid*/
  644. if (memcmp(clp->cl_session->sess_id.data,
  645. sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
  646. continue;
  647. atomic_inc(&clp->cl_count);
  648. spin_unlock(&nn->nfs_client_lock);
  649. return clp;
  650. }
  651. spin_unlock(&nn->nfs_client_lock);
  652. return NULL;
  653. }
  654. #else /* CONFIG_NFS_V4_1 */
  655. struct nfs_client *
  656. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  657. struct nfs4_sessionid *sid, u32 minorversion)
  658. {
  659. return NULL;
  660. }
  661. #endif /* CONFIG_NFS_V4_1 */
  662. /*
  663. * Set up an NFS4 client
  664. */
  665. static int nfs4_set_client(struct nfs_server *server,
  666. const char *hostname,
  667. const struct sockaddr *addr,
  668. const size_t addrlen,
  669. const char *ip_addr,
  670. rpc_authflavor_t authflavour,
  671. int proto, const struct rpc_timeout *timeparms,
  672. u32 minorversion, struct net *net)
  673. {
  674. struct nfs_client_initdata cl_init = {
  675. .hostname = hostname,
  676. .addr = addr,
  677. .addrlen = addrlen,
  678. .nfs_mod = &nfs_v4,
  679. .proto = proto,
  680. .minorversion = minorversion,
  681. .net = net,
  682. };
  683. struct nfs_client *clp;
  684. int error;
  685. dprintk("--> nfs4_set_client()\n");
  686. if (server->flags & NFS_MOUNT_NORESVPORT)
  687. set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
  688. if (server->options & NFS_OPTION_MIGRATION)
  689. set_bit(NFS_CS_MIGRATION, &cl_init.init_flags);
  690. /* Allocate or find a client reference we can use */
  691. clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
  692. if (IS_ERR(clp)) {
  693. error = PTR_ERR(clp);
  694. goto error;
  695. }
  696. /*
  697. * Query for the lease time on clientid setup or renewal
  698. *
  699. * Note that this will be set on nfs_clients that were created
  700. * only for the DS role and did not set this bit, but now will
  701. * serve a dual role.
  702. */
  703. set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
  704. server->nfs_client = clp;
  705. dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
  706. return 0;
  707. error:
  708. dprintk("<-- nfs4_set_client() = xerror %d\n", error);
  709. return error;
  710. }
  711. /*
  712. * Set up a pNFS Data Server client.
  713. *
  714. * Return any existing nfs_client that matches server address,port,version
  715. * and minorversion.
  716. *
  717. * For a new nfs_client, use a soft mount (default), a low retrans and a
  718. * low timeout interval so that if a connection is lost, we retry through
  719. * the MDS.
  720. */
  721. struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
  722. const struct sockaddr *ds_addr, int ds_addrlen,
  723. int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
  724. {
  725. struct nfs_client_initdata cl_init = {
  726. .addr = ds_addr,
  727. .addrlen = ds_addrlen,
  728. .nfs_mod = &nfs_v4,
  729. .proto = ds_proto,
  730. .minorversion = mds_clp->cl_minorversion,
  731. .net = mds_clp->cl_net,
  732. };
  733. struct rpc_timeout ds_timeout;
  734. struct nfs_client *clp;
  735. char buf[INET6_ADDRSTRLEN + 1];
  736. if (rpc_ntop(ds_addr, buf, sizeof(buf)) <= 0)
  737. return ERR_PTR(-EINVAL);
  738. cl_init.hostname = buf;
  739. /*
  740. * Set an authflavor equual to the MDS value. Use the MDS nfs_client
  741. * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
  742. * (section 13.1 RFC 5661).
  743. */
  744. nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
  745. clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
  746. mds_clp->cl_rpcclient->cl_auth->au_flavor);
  747. dprintk("<-- %s %p\n", __func__, clp);
  748. return clp;
  749. }
  750. EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
  751. /*
  752. * Session has been established, and the client marked ready.
  753. * Set the mount rsize and wsize with negotiated fore channel
  754. * attributes which will be bound checked in nfs_server_set_fsinfo.
  755. */
  756. static void nfs4_session_set_rwsize(struct nfs_server *server)
  757. {
  758. #ifdef CONFIG_NFS_V4_1
  759. struct nfs4_session *sess;
  760. u32 server_resp_sz;
  761. u32 server_rqst_sz;
  762. if (!nfs4_has_session(server->nfs_client))
  763. return;
  764. sess = server->nfs_client->cl_session;
  765. server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
  766. server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
  767. if (server->rsize > server_resp_sz)
  768. server->rsize = server_resp_sz;
  769. if (server->wsize > server_rqst_sz)
  770. server->wsize = server_rqst_sz;
  771. #endif /* CONFIG_NFS_V4_1 */
  772. }
  773. static int nfs4_server_common_setup(struct nfs_server *server,
  774. struct nfs_fh *mntfh, bool auth_probe)
  775. {
  776. struct nfs_fattr *fattr;
  777. int error;
  778. /* data servers support only a subset of NFSv4.1 */
  779. if (is_ds_only_client(server->nfs_client))
  780. return -EPROTONOSUPPORT;
  781. fattr = nfs_alloc_fattr();
  782. if (fattr == NULL)
  783. return -ENOMEM;
  784. /* We must ensure the session is initialised first */
  785. error = nfs4_init_session(server->nfs_client);
  786. if (error < 0)
  787. goto out;
  788. /* Set the basic capabilities */
  789. server->caps |= server->nfs_client->cl_mvops->init_caps;
  790. if (server->flags & NFS_MOUNT_NORDIRPLUS)
  791. server->caps &= ~NFS_CAP_READDIRPLUS;
  792. /*
  793. * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
  794. * authentication.
  795. */
  796. if (nfs4_disable_idmapping &&
  797. server->client->cl_auth->au_flavor == RPC_AUTH_UNIX)
  798. server->caps |= NFS_CAP_UIDGID_NOMAP;
  799. /* Probe the root fh to retrieve its FSID and filehandle */
  800. error = nfs4_get_rootfh(server, mntfh, auth_probe);
  801. if (error < 0)
  802. goto out;
  803. dprintk("Server FSID: %llx:%llx\n",
  804. (unsigned long long) server->fsid.major,
  805. (unsigned long long) server->fsid.minor);
  806. nfs_display_fhandle(mntfh, "Pseudo-fs root FH");
  807. nfs4_session_set_rwsize(server);
  808. error = nfs_probe_fsinfo(server, mntfh, fattr);
  809. if (error < 0)
  810. goto out;
  811. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  812. server->namelen = NFS4_MAXNAMLEN;
  813. nfs_server_insert_lists(server);
  814. server->mount_time = jiffies;
  815. server->destroy = nfs4_destroy_server;
  816. out:
  817. nfs_free_fattr(fattr);
  818. return error;
  819. }
  820. /*
  821. * Create a version 4 volume record
  822. */
  823. static int nfs4_init_server(struct nfs_server *server,
  824. struct nfs_parsed_mount_data *data)
  825. {
  826. struct rpc_timeout timeparms;
  827. int error;
  828. dprintk("--> nfs4_init_server()\n");
  829. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  830. data->timeo, data->retrans);
  831. /* Initialise the client representation from the mount data */
  832. server->flags = data->flags;
  833. server->options = data->options;
  834. server->auth_info = data->auth_info;
  835. /* Use the first specified auth flavor. If this flavor isn't
  836. * allowed by the server, use the SECINFO path to try the
  837. * other specified flavors */
  838. if (data->auth_info.flavor_len >= 1)
  839. data->selected_flavor = data->auth_info.flavors[0];
  840. else
  841. data->selected_flavor = RPC_AUTH_UNIX;
  842. /* Get a client record */
  843. error = nfs4_set_client(server,
  844. data->nfs_server.hostname,
  845. (const struct sockaddr *)&data->nfs_server.address,
  846. data->nfs_server.addrlen,
  847. data->client_address,
  848. data->selected_flavor,
  849. data->nfs_server.protocol,
  850. &timeparms,
  851. data->minorversion,
  852. data->net);
  853. if (error < 0)
  854. goto error;
  855. if (data->rsize)
  856. server->rsize = nfs_block_size(data->rsize, NULL);
  857. if (data->wsize)
  858. server->wsize = nfs_block_size(data->wsize, NULL);
  859. server->acregmin = data->acregmin * HZ;
  860. server->acregmax = data->acregmax * HZ;
  861. server->acdirmin = data->acdirmin * HZ;
  862. server->acdirmax = data->acdirmax * HZ;
  863. server->port = data->nfs_server.port;
  864. error = nfs_init_server_rpcclient(server, &timeparms,
  865. data->selected_flavor);
  866. error:
  867. /* Done */
  868. dprintk("<-- nfs4_init_server() = %d\n", error);
  869. return error;
  870. }
  871. /*
  872. * Create a version 4 volume record
  873. * - keyed on server and FSID
  874. */
  875. /*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
  876. struct nfs_fh *mntfh)*/
  877. struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
  878. struct nfs_subversion *nfs_mod)
  879. {
  880. struct nfs_server *server;
  881. bool auth_probe;
  882. int error;
  883. dprintk("--> nfs4_create_server()\n");
  884. server = nfs_alloc_server();
  885. if (!server)
  886. return ERR_PTR(-ENOMEM);
  887. auth_probe = mount_info->parsed->auth_info.flavor_len < 1;
  888. /* set up the general RPC client */
  889. error = nfs4_init_server(server, mount_info->parsed);
  890. if (error < 0)
  891. goto error;
  892. error = nfs4_server_common_setup(server, mount_info->mntfh, auth_probe);
  893. if (error < 0)
  894. goto error;
  895. dprintk("<-- nfs4_create_server() = %p\n", server);
  896. return server;
  897. error:
  898. nfs_free_server(server);
  899. dprintk("<-- nfs4_create_server() = error %d\n", error);
  900. return ERR_PTR(error);
  901. }
  902. /*
  903. * Create an NFS4 referral server record
  904. */
  905. struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
  906. struct nfs_fh *mntfh)
  907. {
  908. struct nfs_client *parent_client;
  909. struct nfs_server *server, *parent_server;
  910. bool auth_probe;
  911. int error;
  912. dprintk("--> nfs4_create_referral_server()\n");
  913. server = nfs_alloc_server();
  914. if (!server)
  915. return ERR_PTR(-ENOMEM);
  916. parent_server = NFS_SB(data->sb);
  917. parent_client = parent_server->nfs_client;
  918. /* Initialise the client representation from the parent server */
  919. nfs_server_copy_userdata(server, parent_server);
  920. /* Get a client representation.
  921. * Note: NFSv4 always uses TCP, */
  922. error = nfs4_set_client(server, data->hostname,
  923. data->addr,
  924. data->addrlen,
  925. parent_client->cl_ipaddr,
  926. data->authflavor,
  927. rpc_protocol(parent_server->client),
  928. parent_server->client->cl_timeout,
  929. parent_client->cl_mvops->minor_version,
  930. parent_client->cl_net);
  931. if (error < 0)
  932. goto error;
  933. error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
  934. if (error < 0)
  935. goto error;
  936. auth_probe = parent_server->auth_info.flavor_len < 1;
  937. error = nfs4_server_common_setup(server, mntfh, auth_probe);
  938. if (error < 0)
  939. goto error;
  940. dprintk("<-- nfs_create_referral_server() = %p\n", server);
  941. return server;
  942. error:
  943. nfs_free_server(server);
  944. dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
  945. return ERR_PTR(error);
  946. }
  947. /*
  948. * Grab the destination's particulars, including lease expiry time.
  949. *
  950. * Returns zero if probe succeeded and retrieved FSID matches the FSID
  951. * we have cached.
  952. */
  953. static int nfs_probe_destination(struct nfs_server *server)
  954. {
  955. struct inode *inode = server->super->s_root->d_inode;
  956. struct nfs_fattr *fattr;
  957. int error;
  958. fattr = nfs_alloc_fattr();
  959. if (fattr == NULL)
  960. return -ENOMEM;
  961. /* Sanity: the probe won't work if the destination server
  962. * does not recognize the migrated FH. */
  963. error = nfs_probe_fsinfo(server, NFS_FH(inode), fattr);
  964. nfs_free_fattr(fattr);
  965. return error;
  966. }
  967. /**
  968. * nfs4_update_server - Move an nfs_server to a different nfs_client
  969. *
  970. * @server: represents FSID to be moved
  971. * @hostname: new end-point's hostname
  972. * @sap: new end-point's socket address
  973. * @salen: size of "sap"
  974. * @net: net namespace
  975. *
  976. * The nfs_server must be quiescent before this function is invoked.
  977. * Either its session is drained (NFSv4.1+), or its transport is
  978. * plugged and drained (NFSv4.0).
  979. *
  980. * Returns zero on success, or a negative errno value.
  981. */
  982. int nfs4_update_server(struct nfs_server *server, const char *hostname,
  983. struct sockaddr *sap, size_t salen, struct net *net)
  984. {
  985. struct nfs_client *clp = server->nfs_client;
  986. struct rpc_clnt *clnt = server->client;
  987. struct xprt_create xargs = {
  988. .ident = clp->cl_proto,
  989. .net = net,
  990. .dstaddr = sap,
  991. .addrlen = salen,
  992. .servername = hostname,
  993. };
  994. char buf[INET6_ADDRSTRLEN + 1];
  995. struct sockaddr_storage address;
  996. struct sockaddr *localaddr = (struct sockaddr *)&address;
  997. int error;
  998. dprintk("--> %s: move FSID %llx:%llx to \"%s\")\n", __func__,
  999. (unsigned long long)server->fsid.major,
  1000. (unsigned long long)server->fsid.minor,
  1001. hostname);
  1002. error = rpc_switch_client_transport(clnt, &xargs, clnt->cl_timeout);
  1003. if (error != 0) {
  1004. dprintk("<-- %s(): rpc_switch_client_transport returned %d\n",
  1005. __func__, error);
  1006. goto out;
  1007. }
  1008. error = rpc_localaddr(clnt, localaddr, sizeof(address));
  1009. if (error != 0) {
  1010. dprintk("<-- %s(): rpc_localaddr returned %d\n",
  1011. __func__, error);
  1012. goto out;
  1013. }
  1014. error = -EAFNOSUPPORT;
  1015. if (rpc_ntop(localaddr, buf, sizeof(buf)) == 0) {
  1016. dprintk("<-- %s(): rpc_ntop returned %d\n",
  1017. __func__, error);
  1018. goto out;
  1019. }
  1020. nfs_server_remove_lists(server);
  1021. error = nfs4_set_client(server, hostname, sap, salen, buf,
  1022. clp->cl_rpcclient->cl_auth->au_flavor,
  1023. clp->cl_proto, clnt->cl_timeout,
  1024. clp->cl_minorversion, net);
  1025. nfs_put_client(clp);
  1026. if (error != 0) {
  1027. nfs_server_insert_lists(server);
  1028. dprintk("<-- %s(): nfs4_set_client returned %d\n",
  1029. __func__, error);
  1030. goto out;
  1031. }
  1032. if (server->nfs_client->cl_hostname == NULL)
  1033. server->nfs_client->cl_hostname = kstrdup(hostname, GFP_KERNEL);
  1034. nfs_server_insert_lists(server);
  1035. error = nfs_probe_destination(server);
  1036. if (error < 0)
  1037. goto out;
  1038. dprintk("<-- %s() succeeded\n", __func__);
  1039. out:
  1040. return error;
  1041. }