nfs4client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908
  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 "internal.h"
  14. #include "callback.h"
  15. #include "delegation.h"
  16. #include "nfs4session.h"
  17. #include "pnfs.h"
  18. #include "netns.h"
  19. #define NFSDBG_FACILITY NFSDBG_CLIENT
  20. /*
  21. * Get a unique NFSv4.0 callback identifier which will be used
  22. * by the V4.0 callback service to lookup the nfs_client struct
  23. */
  24. static int nfs_get_cb_ident_idr(struct nfs_client *clp, int minorversion)
  25. {
  26. int ret = 0;
  27. struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
  28. if (clp->rpc_ops->version != 4 || minorversion != 0)
  29. return ret;
  30. retry:
  31. if (!idr_pre_get(&nn->cb_ident_idr, GFP_KERNEL))
  32. return -ENOMEM;
  33. spin_lock(&nn->nfs_client_lock);
  34. ret = idr_get_new(&nn->cb_ident_idr, clp, &clp->cl_cb_ident);
  35. spin_unlock(&nn->nfs_client_lock);
  36. if (ret == -EAGAIN)
  37. goto retry;
  38. return ret;
  39. }
  40. #ifdef CONFIG_NFS_V4_1
  41. static void nfs4_shutdown_session(struct nfs_client *clp)
  42. {
  43. if (nfs4_has_session(clp)) {
  44. nfs4_destroy_session(clp->cl_session);
  45. nfs4_destroy_clientid(clp);
  46. }
  47. }
  48. #else /* CONFIG_NFS_V4_1 */
  49. static void nfs4_shutdown_session(struct nfs_client *clp)
  50. {
  51. }
  52. #endif /* CONFIG_NFS_V4_1 */
  53. struct nfs_client *nfs4_alloc_client(const struct nfs_client_initdata *cl_init)
  54. {
  55. int err;
  56. struct nfs_client *clp = nfs_alloc_client(cl_init);
  57. if (IS_ERR(clp))
  58. return clp;
  59. err = nfs_get_cb_ident_idr(clp, cl_init->minorversion);
  60. if (err)
  61. goto error;
  62. spin_lock_init(&clp->cl_lock);
  63. INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
  64. rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
  65. clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
  66. clp->cl_minorversion = cl_init->minorversion;
  67. clp->cl_mvops = nfs_v4_minor_ops[cl_init->minorversion];
  68. return clp;
  69. error:
  70. nfs_free_client(clp);
  71. return ERR_PTR(err);
  72. }
  73. /*
  74. * Destroy the NFS4 callback service
  75. */
  76. static void nfs4_destroy_callback(struct nfs_client *clp)
  77. {
  78. if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
  79. nfs_callback_down(clp->cl_mvops->minor_version, clp->cl_net);
  80. }
  81. static void nfs4_shutdown_client(struct nfs_client *clp)
  82. {
  83. if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
  84. nfs4_kill_renewd(clp);
  85. nfs4_shutdown_session(clp);
  86. nfs4_destroy_callback(clp);
  87. if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
  88. nfs_idmap_delete(clp);
  89. rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
  90. kfree(clp->cl_serverowner);
  91. kfree(clp->cl_serverscope);
  92. kfree(clp->cl_implid);
  93. }
  94. void nfs4_free_client(struct nfs_client *clp)
  95. {
  96. nfs4_shutdown_client(clp);
  97. nfs_free_client(clp);
  98. }
  99. /*
  100. * Initialize the NFS4 callback service
  101. */
  102. static int nfs4_init_callback(struct nfs_client *clp)
  103. {
  104. int error;
  105. if (clp->rpc_ops->version == 4) {
  106. struct rpc_xprt *xprt;
  107. xprt = rcu_dereference_raw(clp->cl_rpcclient->cl_xprt);
  108. if (nfs4_has_session(clp)) {
  109. error = xprt_setup_backchannel(xprt,
  110. NFS41_BC_MIN_CALLBACKS);
  111. if (error < 0)
  112. return error;
  113. }
  114. error = nfs_callback_up(clp->cl_mvops->minor_version, xprt);
  115. if (error < 0) {
  116. dprintk("%s: failed to start callback. Error = %d\n",
  117. __func__, error);
  118. return error;
  119. }
  120. __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
  121. }
  122. return 0;
  123. }
  124. /*
  125. * Initialize the minor version specific parts of an NFS4 client record
  126. */
  127. static int nfs4_init_client_minor_version(struct nfs_client *clp)
  128. {
  129. #if defined(CONFIG_NFS_V4_1)
  130. if (clp->cl_mvops->minor_version) {
  131. struct nfs4_session *session = NULL;
  132. /*
  133. * Create the session and mark it expired.
  134. * When a SEQUENCE operation encounters the expired session
  135. * it will do session recovery to initialize it.
  136. */
  137. session = nfs4_alloc_session(clp);
  138. if (!session)
  139. return -ENOMEM;
  140. clp->cl_session = session;
  141. /*
  142. * The create session reply races with the server back
  143. * channel probe. Mark the client NFS_CS_SESSION_INITING
  144. * so that the client back channel can find the
  145. * nfs_client struct
  146. */
  147. nfs_mark_client_ready(clp, NFS_CS_SESSION_INITING);
  148. }
  149. #endif /* CONFIG_NFS_V4_1 */
  150. return nfs4_init_callback(clp);
  151. }
  152. /**
  153. * nfs4_init_client - Initialise an NFS4 client record
  154. *
  155. * @clp: nfs_client to initialise
  156. * @timeparms: timeout parameters for underlying RPC transport
  157. * @ip_addr: callback IP address in presentation format
  158. * @authflavor: authentication flavor for underlying RPC transport
  159. *
  160. * Returns pointer to an NFS client, or an ERR_PTR value.
  161. */
  162. struct nfs_client *nfs4_init_client(struct nfs_client *clp,
  163. const struct rpc_timeout *timeparms,
  164. const char *ip_addr,
  165. rpc_authflavor_t authflavour)
  166. {
  167. char buf[INET6_ADDRSTRLEN + 1];
  168. struct nfs_client *old;
  169. int error;
  170. if (clp->cl_cons_state == NFS_CS_READY) {
  171. /* the client is initialised already */
  172. dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
  173. return clp;
  174. }
  175. /* Check NFS protocol revision and initialize RPC op vector */
  176. clp->rpc_ops = &nfs_v4_clientops;
  177. __set_bit(NFS_CS_DISCRTRY, &clp->cl_flags);
  178. error = nfs_create_rpc_client(clp, timeparms, authflavour);
  179. if (error < 0)
  180. goto error;
  181. /* If no clientaddr= option was specified, find a usable cb address */
  182. if (ip_addr == NULL) {
  183. struct sockaddr_storage cb_addr;
  184. struct sockaddr *sap = (struct sockaddr *)&cb_addr;
  185. error = rpc_localaddr(clp->cl_rpcclient, sap, sizeof(cb_addr));
  186. if (error < 0)
  187. goto error;
  188. error = rpc_ntop(sap, buf, sizeof(buf));
  189. if (error < 0)
  190. goto error;
  191. ip_addr = (const char *)buf;
  192. }
  193. strlcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
  194. error = nfs_idmap_new(clp);
  195. if (error < 0) {
  196. dprintk("%s: failed to create idmapper. Error = %d\n",
  197. __func__, error);
  198. goto error;
  199. }
  200. __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
  201. error = nfs4_init_client_minor_version(clp);
  202. if (error < 0)
  203. goto error;
  204. if (!nfs4_has_session(clp))
  205. nfs_mark_client_ready(clp, NFS_CS_READY);
  206. error = nfs4_discover_server_trunking(clp, &old);
  207. if (error < 0)
  208. goto error;
  209. if (clp != old) {
  210. clp->cl_preserve_clid = true;
  211. nfs_put_client(clp);
  212. clp = old;
  213. atomic_inc(&clp->cl_count);
  214. }
  215. return clp;
  216. error:
  217. nfs_mark_client_ready(clp, error);
  218. nfs_put_client(clp);
  219. dprintk("<-- nfs4_init_client() = xerror %d\n", error);
  220. return ERR_PTR(error);
  221. }
  222. /*
  223. * SETCLIENTID just did a callback update with the callback ident in
  224. * "drop," but server trunking discovery claims "drop" and "keep" are
  225. * actually the same server. Swap the callback IDs so that "keep"
  226. * will continue to use the callback ident the server now knows about,
  227. * and so that "keep"'s original callback ident is destroyed when
  228. * "drop" is freed.
  229. */
  230. static void nfs4_swap_callback_idents(struct nfs_client *keep,
  231. struct nfs_client *drop)
  232. {
  233. struct nfs_net *nn = net_generic(keep->cl_net, nfs_net_id);
  234. unsigned int save = keep->cl_cb_ident;
  235. if (keep->cl_cb_ident == drop->cl_cb_ident)
  236. return;
  237. dprintk("%s: keeping callback ident %u and dropping ident %u\n",
  238. __func__, keep->cl_cb_ident, drop->cl_cb_ident);
  239. spin_lock(&nn->nfs_client_lock);
  240. idr_replace(&nn->cb_ident_idr, keep, drop->cl_cb_ident);
  241. keep->cl_cb_ident = drop->cl_cb_ident;
  242. idr_replace(&nn->cb_ident_idr, drop, save);
  243. drop->cl_cb_ident = save;
  244. spin_unlock(&nn->nfs_client_lock);
  245. }
  246. /**
  247. * nfs40_walk_client_list - Find server that recognizes a client ID
  248. *
  249. * @new: nfs_client with client ID to test
  250. * @result: OUT: found nfs_client, or new
  251. * @cred: credential to use for trunking test
  252. *
  253. * Returns zero, a negative errno, or a negative NFS4ERR status.
  254. * If zero is returned, an nfs_client pointer is planted in "result."
  255. *
  256. * NB: nfs40_walk_client_list() relies on the new nfs_client being
  257. * the last nfs_client on the list.
  258. */
  259. int nfs40_walk_client_list(struct nfs_client *new,
  260. struct nfs_client **result,
  261. struct rpc_cred *cred)
  262. {
  263. struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
  264. struct nfs_client *pos, *n, *prev = NULL;
  265. struct nfs4_setclientid_res clid = {
  266. .clientid = new->cl_clientid,
  267. .confirm = new->cl_confirm,
  268. };
  269. int status;
  270. spin_lock(&nn->nfs_client_lock);
  271. list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) {
  272. /* If "pos" isn't marked ready, we can't trust the
  273. * remaining fields in "pos" */
  274. if (pos->cl_cons_state < NFS_CS_READY)
  275. continue;
  276. if (pos->rpc_ops != new->rpc_ops)
  277. continue;
  278. if (pos->cl_proto != new->cl_proto)
  279. continue;
  280. if (pos->cl_minorversion != new->cl_minorversion)
  281. continue;
  282. if (pos->cl_clientid != new->cl_clientid)
  283. continue;
  284. atomic_inc(&pos->cl_count);
  285. spin_unlock(&nn->nfs_client_lock);
  286. if (prev)
  287. nfs_put_client(prev);
  288. status = nfs4_proc_setclientid_confirm(pos, &clid, cred);
  289. if (status == 0) {
  290. nfs4_swap_callback_idents(pos, new);
  291. nfs_put_client(pos);
  292. *result = pos;
  293. dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
  294. __func__, pos, atomic_read(&pos->cl_count));
  295. return 0;
  296. }
  297. if (status != -NFS4ERR_STALE_CLIENTID) {
  298. nfs_put_client(pos);
  299. dprintk("NFS: <-- %s status = %d, no result\n",
  300. __func__, status);
  301. return status;
  302. }
  303. spin_lock(&nn->nfs_client_lock);
  304. prev = pos;
  305. }
  306. /*
  307. * No matching nfs_client found. This should be impossible,
  308. * because the new nfs_client has already been added to
  309. * nfs_client_list by nfs_get_client().
  310. *
  311. * Don't BUG(), since the caller is holding a mutex.
  312. */
  313. if (prev)
  314. nfs_put_client(prev);
  315. spin_unlock(&nn->nfs_client_lock);
  316. pr_err("NFS: %s Error: no matching nfs_client found\n", __func__);
  317. return -NFS4ERR_STALE_CLIENTID;
  318. }
  319. #ifdef CONFIG_NFS_V4_1
  320. /*
  321. * Returns true if the client IDs match
  322. */
  323. static bool nfs4_match_clientids(struct nfs_client *a, struct nfs_client *b)
  324. {
  325. if (a->cl_clientid != b->cl_clientid) {
  326. dprintk("NFS: --> %s client ID %llx does not match %llx\n",
  327. __func__, a->cl_clientid, b->cl_clientid);
  328. return false;
  329. }
  330. dprintk("NFS: --> %s client ID %llx matches %llx\n",
  331. __func__, a->cl_clientid, b->cl_clientid);
  332. return true;
  333. }
  334. /*
  335. * Returns true if the server owners match
  336. */
  337. static bool
  338. nfs4_match_serverowners(struct nfs_client *a, struct nfs_client *b)
  339. {
  340. struct nfs41_server_owner *o1 = a->cl_serverowner;
  341. struct nfs41_server_owner *o2 = b->cl_serverowner;
  342. if (o1->minor_id != o2->minor_id) {
  343. dprintk("NFS: --> %s server owner minor IDs do not match\n",
  344. __func__);
  345. return false;
  346. }
  347. if (o1->major_id_sz != o2->major_id_sz)
  348. goto out_major_mismatch;
  349. if (memcmp(o1->major_id, o2->major_id, o1->major_id_sz) != 0)
  350. goto out_major_mismatch;
  351. dprintk("NFS: --> %s server owners match\n", __func__);
  352. return true;
  353. out_major_mismatch:
  354. dprintk("NFS: --> %s server owner major IDs do not match\n",
  355. __func__);
  356. return false;
  357. }
  358. /**
  359. * nfs41_walk_client_list - Find nfs_client that matches a client/server owner
  360. *
  361. * @new: nfs_client with client ID to test
  362. * @result: OUT: found nfs_client, or new
  363. * @cred: credential to use for trunking test
  364. *
  365. * Returns zero, a negative errno, or a negative NFS4ERR status.
  366. * If zero is returned, an nfs_client pointer is planted in "result."
  367. *
  368. * NB: nfs41_walk_client_list() relies on the new nfs_client being
  369. * the last nfs_client on the list.
  370. */
  371. int nfs41_walk_client_list(struct nfs_client *new,
  372. struct nfs_client **result,
  373. struct rpc_cred *cred)
  374. {
  375. struct nfs_net *nn = net_generic(new->cl_net, nfs_net_id);
  376. struct nfs_client *pos, *n, *prev = NULL;
  377. int error;
  378. spin_lock(&nn->nfs_client_lock);
  379. list_for_each_entry_safe(pos, n, &nn->nfs_client_list, cl_share_link) {
  380. /* If "pos" isn't marked ready, we can't trust the
  381. * remaining fields in "pos", especially the client
  382. * ID and serverowner fields. Wait for CREATE_SESSION
  383. * to finish. */
  384. if (pos->cl_cons_state < NFS_CS_READY) {
  385. atomic_inc(&pos->cl_count);
  386. spin_unlock(&nn->nfs_client_lock);
  387. if (prev)
  388. nfs_put_client(prev);
  389. prev = pos;
  390. error = nfs_wait_client_init_complete(pos);
  391. if (error < 0) {
  392. nfs_put_client(pos);
  393. spin_lock(&nn->nfs_client_lock);
  394. continue;
  395. }
  396. spin_lock(&nn->nfs_client_lock);
  397. }
  398. if (pos->rpc_ops != new->rpc_ops)
  399. continue;
  400. if (pos->cl_proto != new->cl_proto)
  401. continue;
  402. if (pos->cl_minorversion != new->cl_minorversion)
  403. continue;
  404. if (!nfs4_match_clientids(pos, new))
  405. continue;
  406. if (!nfs4_match_serverowners(pos, new))
  407. continue;
  408. spin_unlock(&nn->nfs_client_lock);
  409. dprintk("NFS: <-- %s using nfs_client = %p ({%d})\n",
  410. __func__, pos, atomic_read(&pos->cl_count));
  411. *result = pos;
  412. return 0;
  413. }
  414. /*
  415. * No matching nfs_client found. This should be impossible,
  416. * because the new nfs_client has already been added to
  417. * nfs_client_list by nfs_get_client().
  418. *
  419. * Don't BUG(), since the caller is holding a mutex.
  420. */
  421. spin_unlock(&nn->nfs_client_lock);
  422. pr_err("NFS: %s Error: no matching nfs_client found\n", __func__);
  423. return -NFS4ERR_STALE_CLIENTID;
  424. }
  425. #endif /* CONFIG_NFS_V4_1 */
  426. static void nfs4_destroy_server(struct nfs_server *server)
  427. {
  428. nfs_server_return_all_delegations(server);
  429. unset_pnfs_layoutdriver(server);
  430. nfs4_purge_state_owners(server);
  431. }
  432. /*
  433. * NFSv4.0 callback thread helper
  434. *
  435. * Find a client by callback identifier
  436. */
  437. struct nfs_client *
  438. nfs4_find_client_ident(struct net *net, int cb_ident)
  439. {
  440. struct nfs_client *clp;
  441. struct nfs_net *nn = net_generic(net, nfs_net_id);
  442. spin_lock(&nn->nfs_client_lock);
  443. clp = idr_find(&nn->cb_ident_idr, cb_ident);
  444. if (clp)
  445. atomic_inc(&clp->cl_count);
  446. spin_unlock(&nn->nfs_client_lock);
  447. return clp;
  448. }
  449. #if defined(CONFIG_NFS_V4_1)
  450. /* Common match routine for v4.0 and v4.1 callback services */
  451. static bool nfs4_cb_match_client(const struct sockaddr *addr,
  452. struct nfs_client *clp, u32 minorversion)
  453. {
  454. struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
  455. /* Don't match clients that failed to initialise */
  456. if (!(clp->cl_cons_state == NFS_CS_READY ||
  457. clp->cl_cons_state == NFS_CS_SESSION_INITING))
  458. return false;
  459. smp_rmb();
  460. /* Match the version and minorversion */
  461. if (clp->rpc_ops->version != 4 ||
  462. clp->cl_minorversion != minorversion)
  463. return false;
  464. /* Match only the IP address, not the port number */
  465. if (!nfs_sockaddr_match_ipaddr(addr, clap))
  466. return false;
  467. return true;
  468. }
  469. /*
  470. * NFSv4.1 callback thread helper
  471. * For CB_COMPOUND calls, find a client by IP address, protocol version,
  472. * minorversion, and sessionID
  473. *
  474. * Returns NULL if no such client
  475. */
  476. struct nfs_client *
  477. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  478. struct nfs4_sessionid *sid)
  479. {
  480. struct nfs_client *clp;
  481. struct nfs_net *nn = net_generic(net, nfs_net_id);
  482. spin_lock(&nn->nfs_client_lock);
  483. list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
  484. if (nfs4_cb_match_client(addr, clp, 1) == false)
  485. continue;
  486. if (!nfs4_has_session(clp))
  487. continue;
  488. /* Match sessionid*/
  489. if (memcmp(clp->cl_session->sess_id.data,
  490. sid->data, NFS4_MAX_SESSIONID_LEN) != 0)
  491. continue;
  492. atomic_inc(&clp->cl_count);
  493. spin_unlock(&nn->nfs_client_lock);
  494. return clp;
  495. }
  496. spin_unlock(&nn->nfs_client_lock);
  497. return NULL;
  498. }
  499. #else /* CONFIG_NFS_V4_1 */
  500. struct nfs_client *
  501. nfs4_find_client_sessionid(struct net *net, const struct sockaddr *addr,
  502. struct nfs4_sessionid *sid)
  503. {
  504. return NULL;
  505. }
  506. #endif /* CONFIG_NFS_V4_1 */
  507. /*
  508. * Set up an NFS4 client
  509. */
  510. static int nfs4_set_client(struct nfs_server *server,
  511. const char *hostname,
  512. const struct sockaddr *addr,
  513. const size_t addrlen,
  514. const char *ip_addr,
  515. rpc_authflavor_t authflavour,
  516. int proto, const struct rpc_timeout *timeparms,
  517. u32 minorversion, struct net *net)
  518. {
  519. struct nfs_client_initdata cl_init = {
  520. .hostname = hostname,
  521. .addr = addr,
  522. .addrlen = addrlen,
  523. .nfs_mod = &nfs_v4,
  524. .proto = proto,
  525. .minorversion = minorversion,
  526. .net = net,
  527. };
  528. struct nfs_client *clp;
  529. int error;
  530. dprintk("--> nfs4_set_client()\n");
  531. if (server->flags & NFS_MOUNT_NORESVPORT)
  532. set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
  533. /* Allocate or find a client reference we can use */
  534. clp = nfs_get_client(&cl_init, timeparms, ip_addr, authflavour);
  535. if (IS_ERR(clp)) {
  536. error = PTR_ERR(clp);
  537. goto error;
  538. }
  539. /*
  540. * Query for the lease time on clientid setup or renewal
  541. *
  542. * Note that this will be set on nfs_clients that were created
  543. * only for the DS role and did not set this bit, but now will
  544. * serve a dual role.
  545. */
  546. set_bit(NFS_CS_CHECK_LEASE_TIME, &clp->cl_res_state);
  547. server->nfs_client = clp;
  548. dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
  549. return 0;
  550. error:
  551. dprintk("<-- nfs4_set_client() = xerror %d\n", error);
  552. return error;
  553. }
  554. /*
  555. * Set up a pNFS Data Server client.
  556. *
  557. * Return any existing nfs_client that matches server address,port,version
  558. * and minorversion.
  559. *
  560. * For a new nfs_client, use a soft mount (default), a low retrans and a
  561. * low timeout interval so that if a connection is lost, we retry through
  562. * the MDS.
  563. */
  564. struct nfs_client *nfs4_set_ds_client(struct nfs_client* mds_clp,
  565. const struct sockaddr *ds_addr, int ds_addrlen,
  566. int ds_proto, unsigned int ds_timeo, unsigned int ds_retrans)
  567. {
  568. struct nfs_client_initdata cl_init = {
  569. .addr = ds_addr,
  570. .addrlen = ds_addrlen,
  571. .nfs_mod = &nfs_v4,
  572. .proto = ds_proto,
  573. .minorversion = mds_clp->cl_minorversion,
  574. .net = mds_clp->cl_net,
  575. };
  576. struct rpc_timeout ds_timeout;
  577. struct nfs_client *clp;
  578. /*
  579. * Set an authflavor equual to the MDS value. Use the MDS nfs_client
  580. * cl_ipaddr so as to use the same EXCHANGE_ID co_ownerid as the MDS
  581. * (section 13.1 RFC 5661).
  582. */
  583. nfs_init_timeout_values(&ds_timeout, ds_proto, ds_timeo, ds_retrans);
  584. clp = nfs_get_client(&cl_init, &ds_timeout, mds_clp->cl_ipaddr,
  585. mds_clp->cl_rpcclient->cl_auth->au_flavor);
  586. dprintk("<-- %s %p\n", __func__, clp);
  587. return clp;
  588. }
  589. EXPORT_SYMBOL_GPL(nfs4_set_ds_client);
  590. /*
  591. * Session has been established, and the client marked ready.
  592. * Set the mount rsize and wsize with negotiated fore channel
  593. * attributes which will be bound checked in nfs_server_set_fsinfo.
  594. */
  595. static void nfs4_session_set_rwsize(struct nfs_server *server)
  596. {
  597. #ifdef CONFIG_NFS_V4_1
  598. struct nfs4_session *sess;
  599. u32 server_resp_sz;
  600. u32 server_rqst_sz;
  601. if (!nfs4_has_session(server->nfs_client))
  602. return;
  603. sess = server->nfs_client->cl_session;
  604. server_resp_sz = sess->fc_attrs.max_resp_sz - nfs41_maxread_overhead;
  605. server_rqst_sz = sess->fc_attrs.max_rqst_sz - nfs41_maxwrite_overhead;
  606. if (server->rsize > server_resp_sz)
  607. server->rsize = server_resp_sz;
  608. if (server->wsize > server_rqst_sz)
  609. server->wsize = server_rqst_sz;
  610. #endif /* CONFIG_NFS_V4_1 */
  611. }
  612. static int nfs4_server_common_setup(struct nfs_server *server,
  613. struct nfs_fh *mntfh)
  614. {
  615. struct nfs_fattr *fattr;
  616. int error;
  617. /* data servers support only a subset of NFSv4.1 */
  618. if (is_ds_only_client(server->nfs_client))
  619. return -EPROTONOSUPPORT;
  620. fattr = nfs_alloc_fattr();
  621. if (fattr == NULL)
  622. return -ENOMEM;
  623. /* We must ensure the session is initialised first */
  624. error = nfs4_init_session(server);
  625. if (error < 0)
  626. goto out;
  627. /* Probe the root fh to retrieve its FSID and filehandle */
  628. error = nfs4_get_rootfh(server, mntfh);
  629. if (error < 0)
  630. goto out;
  631. dprintk("Server FSID: %llx:%llx\n",
  632. (unsigned long long) server->fsid.major,
  633. (unsigned long long) server->fsid.minor);
  634. dprintk("Mount FH: %d\n", mntfh->size);
  635. nfs4_session_set_rwsize(server);
  636. error = nfs_probe_fsinfo(server, mntfh, fattr);
  637. if (error < 0)
  638. goto out;
  639. if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
  640. server->namelen = NFS4_MAXNAMLEN;
  641. nfs_server_insert_lists(server);
  642. server->mount_time = jiffies;
  643. server->destroy = nfs4_destroy_server;
  644. out:
  645. nfs_free_fattr(fattr);
  646. return error;
  647. }
  648. /*
  649. * Create a version 4 volume record
  650. */
  651. static int nfs4_init_server(struct nfs_server *server,
  652. const struct nfs_parsed_mount_data *data)
  653. {
  654. struct rpc_timeout timeparms;
  655. int error;
  656. dprintk("--> nfs4_init_server()\n");
  657. nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
  658. data->timeo, data->retrans);
  659. /* Initialise the client representation from the mount data */
  660. server->flags = data->flags;
  661. server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR|NFS_CAP_POSIX_LOCK;
  662. if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
  663. server->caps |= NFS_CAP_READDIRPLUS;
  664. server->options = data->options;
  665. /* Get a client record */
  666. error = nfs4_set_client(server,
  667. data->nfs_server.hostname,
  668. (const struct sockaddr *)&data->nfs_server.address,
  669. data->nfs_server.addrlen,
  670. data->client_address,
  671. data->auth_flavors[0],
  672. data->nfs_server.protocol,
  673. &timeparms,
  674. data->minorversion,
  675. data->net);
  676. if (error < 0)
  677. goto error;
  678. /*
  679. * Don't use NFS uid/gid mapping if we're using AUTH_SYS or lower
  680. * authentication.
  681. */
  682. if (nfs4_disable_idmapping && data->auth_flavors[0] == RPC_AUTH_UNIX)
  683. server->caps |= NFS_CAP_UIDGID_NOMAP;
  684. if (data->rsize)
  685. server->rsize = nfs_block_size(data->rsize, NULL);
  686. if (data->wsize)
  687. server->wsize = nfs_block_size(data->wsize, NULL);
  688. server->acregmin = data->acregmin * HZ;
  689. server->acregmax = data->acregmax * HZ;
  690. server->acdirmin = data->acdirmin * HZ;
  691. server->acdirmax = data->acdirmax * HZ;
  692. server->port = data->nfs_server.port;
  693. error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
  694. error:
  695. /* Done */
  696. dprintk("<-- nfs4_init_server() = %d\n", error);
  697. return error;
  698. }
  699. /*
  700. * Create a version 4 volume record
  701. * - keyed on server and FSID
  702. */
  703. /*struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
  704. struct nfs_fh *mntfh)*/
  705. struct nfs_server *nfs4_create_server(struct nfs_mount_info *mount_info,
  706. struct nfs_subversion *nfs_mod)
  707. {
  708. struct nfs_server *server;
  709. int error;
  710. dprintk("--> nfs4_create_server()\n");
  711. server = nfs_alloc_server();
  712. if (!server)
  713. return ERR_PTR(-ENOMEM);
  714. /* set up the general RPC client */
  715. error = nfs4_init_server(server, mount_info->parsed);
  716. if (error < 0)
  717. goto error;
  718. error = nfs4_server_common_setup(server, mount_info->mntfh);
  719. if (error < 0)
  720. goto error;
  721. dprintk("<-- nfs4_create_server() = %p\n", server);
  722. return server;
  723. error:
  724. nfs_free_server(server);
  725. dprintk("<-- nfs4_create_server() = error %d\n", error);
  726. return ERR_PTR(error);
  727. }
  728. /*
  729. * Create an NFS4 referral server record
  730. */
  731. struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
  732. struct nfs_fh *mntfh)
  733. {
  734. struct nfs_client *parent_client;
  735. struct nfs_server *server, *parent_server;
  736. int error;
  737. dprintk("--> nfs4_create_referral_server()\n");
  738. server = nfs_alloc_server();
  739. if (!server)
  740. return ERR_PTR(-ENOMEM);
  741. parent_server = NFS_SB(data->sb);
  742. parent_client = parent_server->nfs_client;
  743. /* Initialise the client representation from the parent server */
  744. nfs_server_copy_userdata(server, parent_server);
  745. server->caps |= NFS_CAP_ATOMIC_OPEN|NFS_CAP_CHANGE_ATTR;
  746. /* Get a client representation.
  747. * Note: NFSv4 always uses TCP, */
  748. error = nfs4_set_client(server, data->hostname,
  749. data->addr,
  750. data->addrlen,
  751. parent_client->cl_ipaddr,
  752. data->authflavor,
  753. rpc_protocol(parent_server->client),
  754. parent_server->client->cl_timeout,
  755. parent_client->cl_mvops->minor_version,
  756. parent_client->cl_net);
  757. if (error < 0)
  758. goto error;
  759. error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
  760. if (error < 0)
  761. goto error;
  762. error = nfs4_server_common_setup(server, mntfh);
  763. if (error < 0)
  764. goto error;
  765. dprintk("<-- nfs_create_referral_server() = %p\n", server);
  766. return server;
  767. error:
  768. nfs_free_server(server);
  769. dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
  770. return ERR_PTR(error);
  771. }