nfs4client.c 31 KB

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