nfs4client.c 23 KB

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