nfs4client.c 31 KB

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