nfs4client.c 31 KB

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