nfs4client.c 32 KB

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