nfs4filelayoutdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * Device operations for the pnfs nfs4 file layout driver.
  3. *
  4. * Copyright (c) 2002
  5. * The Regents of the University of Michigan
  6. * All Rights Reserved
  7. *
  8. * Dean Hildebrand <dhildebz@umich.edu>
  9. * Garth Goodson <Garth.Goodson@netapp.com>
  10. *
  11. * Permission is granted to use, copy, create derivative works, and
  12. * redistribute this software and such derivative works for any purpose,
  13. * so long as the name of the University of Michigan is not used in
  14. * any advertising or publicity pertaining to the use or distribution
  15. * of this software without specific, written prior authorization. If
  16. * the above copyright notice or any other identification of the
  17. * University of Michigan is included in any copy of any portion of
  18. * this software, then the disclaimer below must also be included.
  19. *
  20. * This software is provided as is, without representation or warranty
  21. * of any kind either express or implied, including without limitation
  22. * the implied warranties of merchantability, fitness for a particular
  23. * purpose, or noninfringement. The Regents of the University of
  24. * Michigan shall not be liable for any damages, including special,
  25. * indirect, incidental, or consequential damages, with respect to any
  26. * claim arising out of or in connection with the use of the software,
  27. * even if it has been or is hereafter advised of the possibility of
  28. * such damages.
  29. */
  30. #include <linux/nfs_fs.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/module.h>
  33. #include <linux/sunrpc/addr.h>
  34. #include "internal.h"
  35. #include "nfs4session.h"
  36. #include "nfs4filelayout.h"
  37. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  38. static unsigned int dataserver_timeo = NFS4_DEF_DS_TIMEO;
  39. static unsigned int dataserver_retrans = NFS4_DEF_DS_RETRANS;
  40. /*
  41. * Data server cache
  42. *
  43. * Data servers can be mapped to different device ids.
  44. * nfs4_pnfs_ds reference counting
  45. * - set to 1 on allocation
  46. * - incremented when a device id maps a data server already in the cache.
  47. * - decremented when deviceid is removed from the cache.
  48. */
  49. static DEFINE_SPINLOCK(nfs4_ds_cache_lock);
  50. static LIST_HEAD(nfs4_data_server_cache);
  51. /* Debug routines */
  52. void
  53. print_ds(struct nfs4_pnfs_ds *ds)
  54. {
  55. if (ds == NULL) {
  56. printk("%s NULL device\n", __func__);
  57. return;
  58. }
  59. printk(" ds %s\n"
  60. " ref count %d\n"
  61. " client %p\n"
  62. " cl_exchange_flags %x\n",
  63. ds->ds_remotestr,
  64. atomic_read(&ds->ds_count), ds->ds_clp,
  65. ds->ds_clp ? ds->ds_clp->cl_exchange_flags : 0);
  66. }
  67. static bool
  68. same_sockaddr(struct sockaddr *addr1, struct sockaddr *addr2)
  69. {
  70. struct sockaddr_in *a, *b;
  71. struct sockaddr_in6 *a6, *b6;
  72. if (addr1->sa_family != addr2->sa_family)
  73. return false;
  74. switch (addr1->sa_family) {
  75. case AF_INET:
  76. a = (struct sockaddr_in *)addr1;
  77. b = (struct sockaddr_in *)addr2;
  78. if (a->sin_addr.s_addr == b->sin_addr.s_addr &&
  79. a->sin_port == b->sin_port)
  80. return true;
  81. break;
  82. case AF_INET6:
  83. a6 = (struct sockaddr_in6 *)addr1;
  84. b6 = (struct sockaddr_in6 *)addr2;
  85. /* LINKLOCAL addresses must have matching scope_id */
  86. if (ipv6_addr_scope(&a6->sin6_addr) ==
  87. IPV6_ADDR_SCOPE_LINKLOCAL &&
  88. a6->sin6_scope_id != b6->sin6_scope_id)
  89. return false;
  90. if (ipv6_addr_equal(&a6->sin6_addr, &b6->sin6_addr) &&
  91. a6->sin6_port == b6->sin6_port)
  92. return true;
  93. break;
  94. default:
  95. dprintk("%s: unhandled address family: %u\n",
  96. __func__, addr1->sa_family);
  97. return false;
  98. }
  99. return false;
  100. }
  101. static bool
  102. _same_data_server_addrs_locked(const struct list_head *dsaddrs1,
  103. const struct list_head *dsaddrs2)
  104. {
  105. struct nfs4_pnfs_ds_addr *da1, *da2;
  106. /* step through both lists, comparing as we go */
  107. for (da1 = list_first_entry(dsaddrs1, typeof(*da1), da_node),
  108. da2 = list_first_entry(dsaddrs2, typeof(*da2), da_node);
  109. da1 != NULL && da2 != NULL;
  110. da1 = list_entry(da1->da_node.next, typeof(*da1), da_node),
  111. da2 = list_entry(da2->da_node.next, typeof(*da2), da_node)) {
  112. if (!same_sockaddr((struct sockaddr *)&da1->da_addr,
  113. (struct sockaddr *)&da2->da_addr))
  114. return false;
  115. }
  116. if (da1 == NULL && da2 == NULL)
  117. return true;
  118. return false;
  119. }
  120. /*
  121. * Lookup DS by addresses. nfs4_ds_cache_lock is held
  122. */
  123. static struct nfs4_pnfs_ds *
  124. _data_server_lookup_locked(const struct list_head *dsaddrs)
  125. {
  126. struct nfs4_pnfs_ds *ds;
  127. list_for_each_entry(ds, &nfs4_data_server_cache, ds_node)
  128. if (_same_data_server_addrs_locked(&ds->ds_addrs, dsaddrs))
  129. return ds;
  130. return NULL;
  131. }
  132. /*
  133. * Create an rpc connection to the nfs4_pnfs_ds data server
  134. * Currently only supports IPv4 and IPv6 addresses
  135. */
  136. static int
  137. nfs4_ds_connect(struct nfs_server *mds_srv, struct nfs4_pnfs_ds *ds)
  138. {
  139. struct nfs_client *clp = ERR_PTR(-EIO);
  140. struct nfs4_pnfs_ds_addr *da;
  141. int status = 0;
  142. dprintk("--> %s DS %s au_flavor %d\n", __func__, ds->ds_remotestr,
  143. mds_srv->nfs_client->cl_rpcclient->cl_auth->au_flavor);
  144. list_for_each_entry(da, &ds->ds_addrs, da_node) {
  145. dprintk("%s: DS %s: trying address %s\n",
  146. __func__, ds->ds_remotestr, da->da_remotestr);
  147. clp = nfs4_set_ds_client(mds_srv->nfs_client,
  148. (struct sockaddr *)&da->da_addr,
  149. da->da_addrlen, IPPROTO_TCP,
  150. dataserver_timeo, dataserver_retrans);
  151. if (!IS_ERR(clp))
  152. break;
  153. }
  154. if (IS_ERR(clp)) {
  155. status = PTR_ERR(clp);
  156. goto out;
  157. }
  158. status = nfs4_init_ds_session(clp, mds_srv->nfs_client->cl_lease_time);
  159. if (status)
  160. goto out_put;
  161. smp_wmb();
  162. ds->ds_clp = clp;
  163. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  164. out:
  165. return status;
  166. out_put:
  167. nfs_put_client(clp);
  168. goto out;
  169. }
  170. static void
  171. destroy_ds(struct nfs4_pnfs_ds *ds)
  172. {
  173. struct nfs4_pnfs_ds_addr *da;
  174. dprintk("--> %s\n", __func__);
  175. ifdebug(FACILITY)
  176. print_ds(ds);
  177. if (ds->ds_clp)
  178. nfs_put_client(ds->ds_clp);
  179. while (!list_empty(&ds->ds_addrs)) {
  180. da = list_first_entry(&ds->ds_addrs,
  181. struct nfs4_pnfs_ds_addr,
  182. da_node);
  183. list_del_init(&da->da_node);
  184. kfree(da->da_remotestr);
  185. kfree(da);
  186. }
  187. kfree(ds->ds_remotestr);
  188. kfree(ds);
  189. }
  190. void
  191. nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  192. {
  193. struct nfs4_pnfs_ds *ds;
  194. int i;
  195. nfs4_print_deviceid(&dsaddr->id_node.deviceid);
  196. for (i = 0; i < dsaddr->ds_num; i++) {
  197. ds = dsaddr->ds_list[i];
  198. if (ds != NULL) {
  199. if (atomic_dec_and_lock(&ds->ds_count,
  200. &nfs4_ds_cache_lock)) {
  201. list_del_init(&ds->ds_node);
  202. spin_unlock(&nfs4_ds_cache_lock);
  203. destroy_ds(ds);
  204. }
  205. }
  206. }
  207. kfree(dsaddr->stripe_indices);
  208. kfree(dsaddr);
  209. }
  210. /*
  211. * Create a string with a human readable address and port to avoid
  212. * complicated setup around many dprinks.
  213. */
  214. static char *
  215. nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
  216. {
  217. struct nfs4_pnfs_ds_addr *da;
  218. char *remotestr;
  219. size_t len;
  220. char *p;
  221. len = 3; /* '{', '}' and eol */
  222. list_for_each_entry(da, dsaddrs, da_node) {
  223. len += strlen(da->da_remotestr) + 1; /* string plus comma */
  224. }
  225. remotestr = kzalloc(len, gfp_flags);
  226. if (!remotestr)
  227. return NULL;
  228. p = remotestr;
  229. *(p++) = '{';
  230. len--;
  231. list_for_each_entry(da, dsaddrs, da_node) {
  232. size_t ll = strlen(da->da_remotestr);
  233. if (ll > len)
  234. goto out_err;
  235. memcpy(p, da->da_remotestr, ll);
  236. p += ll;
  237. len -= ll;
  238. if (len < 1)
  239. goto out_err;
  240. (*p++) = ',';
  241. len--;
  242. }
  243. if (len < 2)
  244. goto out_err;
  245. *(p++) = '}';
  246. *p = '\0';
  247. return remotestr;
  248. out_err:
  249. kfree(remotestr);
  250. return NULL;
  251. }
  252. static struct nfs4_pnfs_ds *
  253. nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
  254. {
  255. struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
  256. char *remotestr;
  257. if (list_empty(dsaddrs)) {
  258. dprintk("%s: no addresses defined\n", __func__);
  259. goto out;
  260. }
  261. ds = kzalloc(sizeof(*ds), gfp_flags);
  262. if (!ds)
  263. goto out;
  264. /* this is only used for debugging, so it's ok if its NULL */
  265. remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
  266. spin_lock(&nfs4_ds_cache_lock);
  267. tmp_ds = _data_server_lookup_locked(dsaddrs);
  268. if (tmp_ds == NULL) {
  269. INIT_LIST_HEAD(&ds->ds_addrs);
  270. list_splice_init(dsaddrs, &ds->ds_addrs);
  271. ds->ds_remotestr = remotestr;
  272. atomic_set(&ds->ds_count, 1);
  273. INIT_LIST_HEAD(&ds->ds_node);
  274. ds->ds_clp = NULL;
  275. list_add(&ds->ds_node, &nfs4_data_server_cache);
  276. dprintk("%s add new data server %s\n", __func__,
  277. ds->ds_remotestr);
  278. } else {
  279. kfree(remotestr);
  280. kfree(ds);
  281. atomic_inc(&tmp_ds->ds_count);
  282. dprintk("%s data server %s found, inc'ed ds_count to %d\n",
  283. __func__, tmp_ds->ds_remotestr,
  284. atomic_read(&tmp_ds->ds_count));
  285. ds = tmp_ds;
  286. }
  287. spin_unlock(&nfs4_ds_cache_lock);
  288. out:
  289. return ds;
  290. }
  291. /*
  292. * Currently only supports ipv4, ipv6 and one multi-path address.
  293. */
  294. static struct nfs4_pnfs_ds_addr *
  295. decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags)
  296. {
  297. struct nfs4_pnfs_ds_addr *da = NULL;
  298. char *buf, *portstr;
  299. __be16 port;
  300. int nlen, rlen;
  301. int tmp[2];
  302. __be32 *p;
  303. char *netid, *match_netid;
  304. size_t len, match_netid_len;
  305. char *startsep = "";
  306. char *endsep = "";
  307. /* r_netid */
  308. p = xdr_inline_decode(streamp, 4);
  309. if (unlikely(!p))
  310. goto out_err;
  311. nlen = be32_to_cpup(p++);
  312. p = xdr_inline_decode(streamp, nlen);
  313. if (unlikely(!p))
  314. goto out_err;
  315. netid = kmalloc(nlen+1, gfp_flags);
  316. if (unlikely(!netid))
  317. goto out_err;
  318. netid[nlen] = '\0';
  319. memcpy(netid, p, nlen);
  320. /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
  321. p = xdr_inline_decode(streamp, 4);
  322. if (unlikely(!p))
  323. goto out_free_netid;
  324. rlen = be32_to_cpup(p);
  325. p = xdr_inline_decode(streamp, rlen);
  326. if (unlikely(!p))
  327. goto out_free_netid;
  328. /* port is ".ABC.DEF", 8 chars max */
  329. if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
  330. dprintk("%s: Invalid address, length %d\n", __func__,
  331. rlen);
  332. goto out_free_netid;
  333. }
  334. buf = kmalloc(rlen + 1, gfp_flags);
  335. if (!buf) {
  336. dprintk("%s: Not enough memory\n", __func__);
  337. goto out_free_netid;
  338. }
  339. buf[rlen] = '\0';
  340. memcpy(buf, p, rlen);
  341. /* replace port '.' with '-' */
  342. portstr = strrchr(buf, '.');
  343. if (!portstr) {
  344. dprintk("%s: Failed finding expected dot in port\n",
  345. __func__);
  346. goto out_free_buf;
  347. }
  348. *portstr = '-';
  349. /* find '.' between address and port */
  350. portstr = strrchr(buf, '.');
  351. if (!portstr) {
  352. dprintk("%s: Failed finding expected dot between address and "
  353. "port\n", __func__);
  354. goto out_free_buf;
  355. }
  356. *portstr = '\0';
  357. da = kzalloc(sizeof(*da), gfp_flags);
  358. if (unlikely(!da))
  359. goto out_free_buf;
  360. INIT_LIST_HEAD(&da->da_node);
  361. if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
  362. sizeof(da->da_addr))) {
  363. dprintk("%s: error parsing address %s\n", __func__, buf);
  364. goto out_free_da;
  365. }
  366. portstr++;
  367. sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
  368. port = htons((tmp[0] << 8) | (tmp[1]));
  369. switch (da->da_addr.ss_family) {
  370. case AF_INET:
  371. ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
  372. da->da_addrlen = sizeof(struct sockaddr_in);
  373. match_netid = "tcp";
  374. match_netid_len = 3;
  375. break;
  376. case AF_INET6:
  377. ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
  378. da->da_addrlen = sizeof(struct sockaddr_in6);
  379. match_netid = "tcp6";
  380. match_netid_len = 4;
  381. startsep = "[";
  382. endsep = "]";
  383. break;
  384. default:
  385. dprintk("%s: unsupported address family: %u\n",
  386. __func__, da->da_addr.ss_family);
  387. goto out_free_da;
  388. }
  389. if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
  390. dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
  391. __func__, netid, match_netid);
  392. goto out_free_da;
  393. }
  394. /* save human readable address */
  395. len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
  396. da->da_remotestr = kzalloc(len, gfp_flags);
  397. /* NULL is ok, only used for dprintk */
  398. if (da->da_remotestr)
  399. snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
  400. buf, endsep, ntohs(port));
  401. dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
  402. kfree(buf);
  403. kfree(netid);
  404. return da;
  405. out_free_da:
  406. kfree(da);
  407. out_free_buf:
  408. dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
  409. kfree(buf);
  410. out_free_netid:
  411. kfree(netid);
  412. out_err:
  413. return NULL;
  414. }
  415. /* Decode opaque device data and return the result */
  416. static struct nfs4_file_layout_dsaddr*
  417. decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
  418. {
  419. int i;
  420. u32 cnt, num;
  421. u8 *indexp;
  422. __be32 *p;
  423. u8 *stripe_indices;
  424. u8 max_stripe_index;
  425. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  426. struct xdr_stream stream;
  427. struct xdr_buf buf;
  428. struct page *scratch;
  429. struct list_head dsaddrs;
  430. struct nfs4_pnfs_ds_addr *da;
  431. /* set up xdr stream */
  432. scratch = alloc_page(gfp_flags);
  433. if (!scratch)
  434. goto out_err;
  435. xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
  436. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  437. /* Get the stripe count (number of stripe index) */
  438. p = xdr_inline_decode(&stream, 4);
  439. if (unlikely(!p))
  440. goto out_err_free_scratch;
  441. cnt = be32_to_cpup(p);
  442. dprintk("%s stripe count %d\n", __func__, cnt);
  443. if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
  444. printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
  445. "supported maximum %d\n", __func__,
  446. cnt, NFS4_PNFS_MAX_STRIPE_CNT);
  447. goto out_err_free_scratch;
  448. }
  449. /* read stripe indices */
  450. stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
  451. if (!stripe_indices)
  452. goto out_err_free_scratch;
  453. p = xdr_inline_decode(&stream, cnt << 2);
  454. if (unlikely(!p))
  455. goto out_err_free_stripe_indices;
  456. indexp = &stripe_indices[0];
  457. max_stripe_index = 0;
  458. for (i = 0; i < cnt; i++) {
  459. *indexp = be32_to_cpup(p++);
  460. max_stripe_index = max(max_stripe_index, *indexp);
  461. indexp++;
  462. }
  463. /* Check the multipath list count */
  464. p = xdr_inline_decode(&stream, 4);
  465. if (unlikely(!p))
  466. goto out_err_free_stripe_indices;
  467. num = be32_to_cpup(p);
  468. dprintk("%s ds_num %u\n", __func__, num);
  469. if (num > NFS4_PNFS_MAX_MULTI_CNT) {
  470. printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
  471. "supported maximum %d\n", __func__,
  472. num, NFS4_PNFS_MAX_MULTI_CNT);
  473. goto out_err_free_stripe_indices;
  474. }
  475. /* validate stripe indices are all < num */
  476. if (max_stripe_index >= num) {
  477. printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
  478. __func__, max_stripe_index, num);
  479. goto out_err_free_stripe_indices;
  480. }
  481. dsaddr = kzalloc(sizeof(*dsaddr) +
  482. (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
  483. gfp_flags);
  484. if (!dsaddr)
  485. goto out_err_free_stripe_indices;
  486. dsaddr->stripe_count = cnt;
  487. dsaddr->stripe_indices = stripe_indices;
  488. stripe_indices = NULL;
  489. dsaddr->ds_num = num;
  490. nfs4_init_deviceid_node(&dsaddr->id_node,
  491. NFS_SERVER(ino)->pnfs_curr_ld,
  492. NFS_SERVER(ino)->nfs_client,
  493. &pdev->dev_id);
  494. INIT_LIST_HEAD(&dsaddrs);
  495. for (i = 0; i < dsaddr->ds_num; i++) {
  496. int j;
  497. u32 mp_count;
  498. p = xdr_inline_decode(&stream, 4);
  499. if (unlikely(!p))
  500. goto out_err_free_deviceid;
  501. mp_count = be32_to_cpup(p); /* multipath count */
  502. for (j = 0; j < mp_count; j++) {
  503. da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->cl_net,
  504. &stream, gfp_flags);
  505. if (da)
  506. list_add_tail(&da->da_node, &dsaddrs);
  507. }
  508. if (list_empty(&dsaddrs)) {
  509. dprintk("%s: no suitable DS addresses found\n",
  510. __func__);
  511. goto out_err_free_deviceid;
  512. }
  513. dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
  514. if (!dsaddr->ds_list[i])
  515. goto out_err_drain_dsaddrs;
  516. /* If DS was already in cache, free ds addrs */
  517. while (!list_empty(&dsaddrs)) {
  518. da = list_first_entry(&dsaddrs,
  519. struct nfs4_pnfs_ds_addr,
  520. da_node);
  521. list_del_init(&da->da_node);
  522. kfree(da->da_remotestr);
  523. kfree(da);
  524. }
  525. }
  526. __free_page(scratch);
  527. return dsaddr;
  528. out_err_drain_dsaddrs:
  529. while (!list_empty(&dsaddrs)) {
  530. da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
  531. da_node);
  532. list_del_init(&da->da_node);
  533. kfree(da->da_remotestr);
  534. kfree(da);
  535. }
  536. out_err_free_deviceid:
  537. nfs4_fl_free_deviceid(dsaddr);
  538. /* stripe_indicies was part of dsaddr */
  539. goto out_err_free_scratch;
  540. out_err_free_stripe_indices:
  541. kfree(stripe_indices);
  542. out_err_free_scratch:
  543. __free_page(scratch);
  544. out_err:
  545. dprintk("%s ERROR: returning NULL\n", __func__);
  546. return NULL;
  547. }
  548. /*
  549. * Decode the opaque device specified in 'dev' and add it to the cache of
  550. * available devices.
  551. */
  552. static struct nfs4_file_layout_dsaddr *
  553. decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
  554. {
  555. struct nfs4_deviceid_node *d;
  556. struct nfs4_file_layout_dsaddr *n, *new;
  557. new = decode_device(inode, dev, gfp_flags);
  558. if (!new) {
  559. printk(KERN_WARNING "NFS: %s: Could not decode or add device\n",
  560. __func__);
  561. return NULL;
  562. }
  563. d = nfs4_insert_deviceid_node(&new->id_node);
  564. n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  565. if (n != new) {
  566. nfs4_fl_free_deviceid(new);
  567. return n;
  568. }
  569. return new;
  570. }
  571. /*
  572. * Retrieve the information for dev_id, add it to the list
  573. * of available devices, and return it.
  574. */
  575. struct nfs4_file_layout_dsaddr *
  576. filelayout_get_device_info(struct inode *inode,
  577. struct nfs4_deviceid *dev_id,
  578. struct rpc_cred *cred,
  579. gfp_t gfp_flags)
  580. {
  581. struct pnfs_device *pdev = NULL;
  582. u32 max_resp_sz;
  583. int max_pages;
  584. struct page **pages = NULL;
  585. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  586. int rc, i;
  587. struct nfs_server *server = NFS_SERVER(inode);
  588. /*
  589. * Use the session max response size as the basis for setting
  590. * GETDEVICEINFO's maxcount
  591. */
  592. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  593. max_pages = nfs_page_array_len(0, max_resp_sz);
  594. dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
  595. __func__, inode, max_resp_sz, max_pages);
  596. pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
  597. if (pdev == NULL)
  598. return NULL;
  599. pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
  600. if (pages == NULL) {
  601. kfree(pdev);
  602. return NULL;
  603. }
  604. for (i = 0; i < max_pages; i++) {
  605. pages[i] = alloc_page(gfp_flags);
  606. if (!pages[i])
  607. goto out_free;
  608. }
  609. memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
  610. pdev->layout_type = LAYOUT_NFSV4_1_FILES;
  611. pdev->pages = pages;
  612. pdev->pgbase = 0;
  613. pdev->pglen = max_resp_sz;
  614. pdev->mincount = 0;
  615. pdev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
  616. rc = nfs4_proc_getdeviceinfo(server, pdev, cred);
  617. dprintk("%s getdevice info returns %d\n", __func__, rc);
  618. if (rc)
  619. goto out_free;
  620. /*
  621. * Found new device, need to decode it and then add it to the
  622. * list of known devices for this mountpoint.
  623. */
  624. dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
  625. out_free:
  626. for (i = 0; i < max_pages; i++)
  627. __free_page(pages[i]);
  628. kfree(pages);
  629. kfree(pdev);
  630. dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
  631. return dsaddr;
  632. }
  633. void
  634. nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  635. {
  636. nfs4_put_deviceid_node(&dsaddr->id_node);
  637. }
  638. /*
  639. * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
  640. * Then: ((res + fsi) % dsaddr->stripe_count)
  641. */
  642. u32
  643. nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
  644. {
  645. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  646. u64 tmp;
  647. tmp = offset - flseg->pattern_offset;
  648. do_div(tmp, flseg->stripe_unit);
  649. tmp += flseg->first_stripe_index;
  650. return do_div(tmp, flseg->dsaddr->stripe_count);
  651. }
  652. u32
  653. nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
  654. {
  655. return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
  656. }
  657. struct nfs_fh *
  658. nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
  659. {
  660. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  661. u32 i;
  662. if (flseg->stripe_type == STRIPE_SPARSE) {
  663. if (flseg->num_fh == 1)
  664. i = 0;
  665. else if (flseg->num_fh == 0)
  666. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  667. return NULL;
  668. else
  669. i = nfs4_fl_calc_ds_index(lseg, j);
  670. } else
  671. i = j;
  672. return flseg->fh_array[i];
  673. }
  674. static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
  675. {
  676. might_sleep();
  677. wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
  678. nfs_wait_bit_killable, TASK_KILLABLE);
  679. }
  680. static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
  681. {
  682. smp_mb__before_clear_bit();
  683. clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
  684. smp_mb__after_clear_bit();
  685. wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
  686. }
  687. struct nfs4_pnfs_ds *
  688. nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
  689. {
  690. struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
  691. struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
  692. struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
  693. struct nfs4_pnfs_ds *ret = ds;
  694. if (ds == NULL) {
  695. printk(KERN_ERR "NFS: %s: No data server for offset index %d\n",
  696. __func__, ds_idx);
  697. filelayout_mark_devid_invalid(devid);
  698. goto out;
  699. }
  700. smp_rmb();
  701. if (ds->ds_clp)
  702. goto out_test_devid;
  703. if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
  704. struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
  705. int err;
  706. err = nfs4_ds_connect(s, ds);
  707. if (err)
  708. nfs4_mark_deviceid_unavailable(devid);
  709. nfs4_clear_ds_conn_bit(ds);
  710. } else {
  711. /* Either ds is connected, or ds is NULL */
  712. nfs4_wait_ds_connect(ds);
  713. }
  714. out_test_devid:
  715. if (filelayout_test_devid_unavailable(devid))
  716. ret = NULL;
  717. out:
  718. return ret;
  719. }
  720. module_param(dataserver_retrans, uint, 0644);
  721. MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
  722. "retries a request before it attempts further "
  723. " recovery action.");
  724. module_param(dataserver_timeo, uint, 0644);
  725. MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
  726. "NFSv4.1 client waits for a response from a "
  727. " data server before it retries an NFS request.");