nfs4filelayoutdev.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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. ds->ds_clp = clp;
  162. dprintk("%s [new] addr: %s\n", __func__, ds->ds_remotestr);
  163. out:
  164. return status;
  165. out_put:
  166. nfs_put_client(clp);
  167. goto out;
  168. }
  169. static void
  170. destroy_ds(struct nfs4_pnfs_ds *ds)
  171. {
  172. struct nfs4_pnfs_ds_addr *da;
  173. dprintk("--> %s\n", __func__);
  174. ifdebug(FACILITY)
  175. print_ds(ds);
  176. if (ds->ds_clp)
  177. nfs_put_client(ds->ds_clp);
  178. while (!list_empty(&ds->ds_addrs)) {
  179. da = list_first_entry(&ds->ds_addrs,
  180. struct nfs4_pnfs_ds_addr,
  181. da_node);
  182. list_del_init(&da->da_node);
  183. kfree(da->da_remotestr);
  184. kfree(da);
  185. }
  186. kfree(ds->ds_remotestr);
  187. kfree(ds);
  188. }
  189. void
  190. nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  191. {
  192. struct nfs4_pnfs_ds *ds;
  193. int i;
  194. nfs4_print_deviceid(&dsaddr->id_node.deviceid);
  195. for (i = 0; i < dsaddr->ds_num; i++) {
  196. ds = dsaddr->ds_list[i];
  197. if (ds != NULL) {
  198. if (atomic_dec_and_lock(&ds->ds_count,
  199. &nfs4_ds_cache_lock)) {
  200. list_del_init(&ds->ds_node);
  201. spin_unlock(&nfs4_ds_cache_lock);
  202. destroy_ds(ds);
  203. }
  204. }
  205. }
  206. kfree(dsaddr->stripe_indices);
  207. kfree(dsaddr);
  208. }
  209. /*
  210. * Create a string with a human readable address and port to avoid
  211. * complicated setup around many dprinks.
  212. */
  213. static char *
  214. nfs4_pnfs_remotestr(struct list_head *dsaddrs, gfp_t gfp_flags)
  215. {
  216. struct nfs4_pnfs_ds_addr *da;
  217. char *remotestr;
  218. size_t len;
  219. char *p;
  220. len = 3; /* '{', '}' and eol */
  221. list_for_each_entry(da, dsaddrs, da_node) {
  222. len += strlen(da->da_remotestr) + 1; /* string plus comma */
  223. }
  224. remotestr = kzalloc(len, gfp_flags);
  225. if (!remotestr)
  226. return NULL;
  227. p = remotestr;
  228. *(p++) = '{';
  229. len--;
  230. list_for_each_entry(da, dsaddrs, da_node) {
  231. size_t ll = strlen(da->da_remotestr);
  232. if (ll > len)
  233. goto out_err;
  234. memcpy(p, da->da_remotestr, ll);
  235. p += ll;
  236. len -= ll;
  237. if (len < 1)
  238. goto out_err;
  239. (*p++) = ',';
  240. len--;
  241. }
  242. if (len < 2)
  243. goto out_err;
  244. *(p++) = '}';
  245. *p = '\0';
  246. return remotestr;
  247. out_err:
  248. kfree(remotestr);
  249. return NULL;
  250. }
  251. static struct nfs4_pnfs_ds *
  252. nfs4_pnfs_ds_add(struct list_head *dsaddrs, gfp_t gfp_flags)
  253. {
  254. struct nfs4_pnfs_ds *tmp_ds, *ds = NULL;
  255. char *remotestr;
  256. if (list_empty(dsaddrs)) {
  257. dprintk("%s: no addresses defined\n", __func__);
  258. goto out;
  259. }
  260. ds = kzalloc(sizeof(*ds), gfp_flags);
  261. if (!ds)
  262. goto out;
  263. /* this is only used for debugging, so it's ok if its NULL */
  264. remotestr = nfs4_pnfs_remotestr(dsaddrs, gfp_flags);
  265. spin_lock(&nfs4_ds_cache_lock);
  266. tmp_ds = _data_server_lookup_locked(dsaddrs);
  267. if (tmp_ds == NULL) {
  268. INIT_LIST_HEAD(&ds->ds_addrs);
  269. list_splice_init(dsaddrs, &ds->ds_addrs);
  270. ds->ds_remotestr = remotestr;
  271. atomic_set(&ds->ds_count, 1);
  272. INIT_LIST_HEAD(&ds->ds_node);
  273. ds->ds_clp = NULL;
  274. list_add(&ds->ds_node, &nfs4_data_server_cache);
  275. dprintk("%s add new data server %s\n", __func__,
  276. ds->ds_remotestr);
  277. } else {
  278. kfree(remotestr);
  279. kfree(ds);
  280. atomic_inc(&tmp_ds->ds_count);
  281. dprintk("%s data server %s found, inc'ed ds_count to %d\n",
  282. __func__, tmp_ds->ds_remotestr,
  283. atomic_read(&tmp_ds->ds_count));
  284. ds = tmp_ds;
  285. }
  286. spin_unlock(&nfs4_ds_cache_lock);
  287. out:
  288. return ds;
  289. }
  290. /*
  291. * Currently only supports ipv4, ipv6 and one multi-path address.
  292. */
  293. static struct nfs4_pnfs_ds_addr *
  294. decode_ds_addr(struct net *net, struct xdr_stream *streamp, gfp_t gfp_flags)
  295. {
  296. struct nfs4_pnfs_ds_addr *da = NULL;
  297. char *buf, *portstr;
  298. __be16 port;
  299. int nlen, rlen;
  300. int tmp[2];
  301. __be32 *p;
  302. char *netid, *match_netid;
  303. size_t len, match_netid_len;
  304. char *startsep = "";
  305. char *endsep = "";
  306. /* r_netid */
  307. p = xdr_inline_decode(streamp, 4);
  308. if (unlikely(!p))
  309. goto out_err;
  310. nlen = be32_to_cpup(p++);
  311. p = xdr_inline_decode(streamp, nlen);
  312. if (unlikely(!p))
  313. goto out_err;
  314. netid = kmalloc(nlen+1, gfp_flags);
  315. if (unlikely(!netid))
  316. goto out_err;
  317. netid[nlen] = '\0';
  318. memcpy(netid, p, nlen);
  319. /* r_addr: ip/ip6addr with port in dec octets - see RFC 5665 */
  320. p = xdr_inline_decode(streamp, 4);
  321. if (unlikely(!p))
  322. goto out_free_netid;
  323. rlen = be32_to_cpup(p);
  324. p = xdr_inline_decode(streamp, rlen);
  325. if (unlikely(!p))
  326. goto out_free_netid;
  327. /* port is ".ABC.DEF", 8 chars max */
  328. if (rlen > INET6_ADDRSTRLEN + IPV6_SCOPE_ID_LEN + 8) {
  329. dprintk("%s: Invalid address, length %d\n", __func__,
  330. rlen);
  331. goto out_free_netid;
  332. }
  333. buf = kmalloc(rlen + 1, gfp_flags);
  334. if (!buf) {
  335. dprintk("%s: Not enough memory\n", __func__);
  336. goto out_free_netid;
  337. }
  338. buf[rlen] = '\0';
  339. memcpy(buf, p, rlen);
  340. /* replace port '.' with '-' */
  341. portstr = strrchr(buf, '.');
  342. if (!portstr) {
  343. dprintk("%s: Failed finding expected dot in port\n",
  344. __func__);
  345. goto out_free_buf;
  346. }
  347. *portstr = '-';
  348. /* find '.' between address and port */
  349. portstr = strrchr(buf, '.');
  350. if (!portstr) {
  351. dprintk("%s: Failed finding expected dot between address and "
  352. "port\n", __func__);
  353. goto out_free_buf;
  354. }
  355. *portstr = '\0';
  356. da = kzalloc(sizeof(*da), gfp_flags);
  357. if (unlikely(!da))
  358. goto out_free_buf;
  359. INIT_LIST_HEAD(&da->da_node);
  360. if (!rpc_pton(net, buf, portstr-buf, (struct sockaddr *)&da->da_addr,
  361. sizeof(da->da_addr))) {
  362. dprintk("%s: error parsing address %s\n", __func__, buf);
  363. goto out_free_da;
  364. }
  365. portstr++;
  366. sscanf(portstr, "%d-%d", &tmp[0], &tmp[1]);
  367. port = htons((tmp[0] << 8) | (tmp[1]));
  368. switch (da->da_addr.ss_family) {
  369. case AF_INET:
  370. ((struct sockaddr_in *)&da->da_addr)->sin_port = port;
  371. da->da_addrlen = sizeof(struct sockaddr_in);
  372. match_netid = "tcp";
  373. match_netid_len = 3;
  374. break;
  375. case AF_INET6:
  376. ((struct sockaddr_in6 *)&da->da_addr)->sin6_port = port;
  377. da->da_addrlen = sizeof(struct sockaddr_in6);
  378. match_netid = "tcp6";
  379. match_netid_len = 4;
  380. startsep = "[";
  381. endsep = "]";
  382. break;
  383. default:
  384. dprintk("%s: unsupported address family: %u\n",
  385. __func__, da->da_addr.ss_family);
  386. goto out_free_da;
  387. }
  388. if (nlen != match_netid_len || strncmp(netid, match_netid, nlen)) {
  389. dprintk("%s: ERROR: r_netid \"%s\" != \"%s\"\n",
  390. __func__, netid, match_netid);
  391. goto out_free_da;
  392. }
  393. /* save human readable address */
  394. len = strlen(startsep) + strlen(buf) + strlen(endsep) + 7;
  395. da->da_remotestr = kzalloc(len, gfp_flags);
  396. /* NULL is ok, only used for dprintk */
  397. if (da->da_remotestr)
  398. snprintf(da->da_remotestr, len, "%s%s%s:%u", startsep,
  399. buf, endsep, ntohs(port));
  400. dprintk("%s: Parsed DS addr %s\n", __func__, da->da_remotestr);
  401. kfree(buf);
  402. kfree(netid);
  403. return da;
  404. out_free_da:
  405. kfree(da);
  406. out_free_buf:
  407. dprintk("%s: Error parsing DS addr: %s\n", __func__, buf);
  408. kfree(buf);
  409. out_free_netid:
  410. kfree(netid);
  411. out_err:
  412. return NULL;
  413. }
  414. /* Decode opaque device data and return the result */
  415. static struct nfs4_file_layout_dsaddr*
  416. decode_device(struct inode *ino, struct pnfs_device *pdev, gfp_t gfp_flags)
  417. {
  418. int i;
  419. u32 cnt, num;
  420. u8 *indexp;
  421. __be32 *p;
  422. u8 *stripe_indices;
  423. u8 max_stripe_index;
  424. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  425. struct xdr_stream stream;
  426. struct xdr_buf buf;
  427. struct page *scratch;
  428. struct list_head dsaddrs;
  429. struct nfs4_pnfs_ds_addr *da;
  430. /* set up xdr stream */
  431. scratch = alloc_page(gfp_flags);
  432. if (!scratch)
  433. goto out_err;
  434. xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
  435. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  436. /* Get the stripe count (number of stripe index) */
  437. p = xdr_inline_decode(&stream, 4);
  438. if (unlikely(!p))
  439. goto out_err_free_scratch;
  440. cnt = be32_to_cpup(p);
  441. dprintk("%s stripe count %d\n", __func__, cnt);
  442. if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
  443. printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
  444. "supported maximum %d\n", __func__,
  445. cnt, NFS4_PNFS_MAX_STRIPE_CNT);
  446. goto out_err_free_scratch;
  447. }
  448. /* read stripe indices */
  449. stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
  450. if (!stripe_indices)
  451. goto out_err_free_scratch;
  452. p = xdr_inline_decode(&stream, cnt << 2);
  453. if (unlikely(!p))
  454. goto out_err_free_stripe_indices;
  455. indexp = &stripe_indices[0];
  456. max_stripe_index = 0;
  457. for (i = 0; i < cnt; i++) {
  458. *indexp = be32_to_cpup(p++);
  459. max_stripe_index = max(max_stripe_index, *indexp);
  460. indexp++;
  461. }
  462. /* Check the multipath list count */
  463. p = xdr_inline_decode(&stream, 4);
  464. if (unlikely(!p))
  465. goto out_err_free_stripe_indices;
  466. num = be32_to_cpup(p);
  467. dprintk("%s ds_num %u\n", __func__, num);
  468. if (num > NFS4_PNFS_MAX_MULTI_CNT) {
  469. printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
  470. "supported maximum %d\n", __func__,
  471. num, NFS4_PNFS_MAX_MULTI_CNT);
  472. goto out_err_free_stripe_indices;
  473. }
  474. /* validate stripe indices are all < num */
  475. if (max_stripe_index >= num) {
  476. printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
  477. __func__, max_stripe_index, num);
  478. goto out_err_free_stripe_indices;
  479. }
  480. dsaddr = kzalloc(sizeof(*dsaddr) +
  481. (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
  482. gfp_flags);
  483. if (!dsaddr)
  484. goto out_err_free_stripe_indices;
  485. dsaddr->stripe_count = cnt;
  486. dsaddr->stripe_indices = stripe_indices;
  487. stripe_indices = NULL;
  488. dsaddr->ds_num = num;
  489. nfs4_init_deviceid_node(&dsaddr->id_node,
  490. NFS_SERVER(ino)->pnfs_curr_ld,
  491. NFS_SERVER(ino)->nfs_client,
  492. &pdev->dev_id);
  493. INIT_LIST_HEAD(&dsaddrs);
  494. for (i = 0; i < dsaddr->ds_num; i++) {
  495. int j;
  496. u32 mp_count;
  497. p = xdr_inline_decode(&stream, 4);
  498. if (unlikely(!p))
  499. goto out_err_free_deviceid;
  500. mp_count = be32_to_cpup(p); /* multipath count */
  501. for (j = 0; j < mp_count; j++) {
  502. da = decode_ds_addr(NFS_SERVER(ino)->nfs_client->cl_net,
  503. &stream, gfp_flags);
  504. if (da)
  505. list_add_tail(&da->da_node, &dsaddrs);
  506. }
  507. if (list_empty(&dsaddrs)) {
  508. dprintk("%s: no suitable DS addresses found\n",
  509. __func__);
  510. goto out_err_free_deviceid;
  511. }
  512. dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
  513. if (!dsaddr->ds_list[i])
  514. goto out_err_drain_dsaddrs;
  515. /* If DS was already in cache, free ds addrs */
  516. while (!list_empty(&dsaddrs)) {
  517. da = list_first_entry(&dsaddrs,
  518. struct nfs4_pnfs_ds_addr,
  519. da_node);
  520. list_del_init(&da->da_node);
  521. kfree(da->da_remotestr);
  522. kfree(da);
  523. }
  524. }
  525. __free_page(scratch);
  526. return dsaddr;
  527. out_err_drain_dsaddrs:
  528. while (!list_empty(&dsaddrs)) {
  529. da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
  530. da_node);
  531. list_del_init(&da->da_node);
  532. kfree(da->da_remotestr);
  533. kfree(da);
  534. }
  535. out_err_free_deviceid:
  536. nfs4_fl_free_deviceid(dsaddr);
  537. /* stripe_indicies was part of dsaddr */
  538. goto out_err_free_scratch;
  539. out_err_free_stripe_indices:
  540. kfree(stripe_indices);
  541. out_err_free_scratch:
  542. __free_page(scratch);
  543. out_err:
  544. dprintk("%s ERROR: returning NULL\n", __func__);
  545. return NULL;
  546. }
  547. /*
  548. * Decode the opaque device specified in 'dev' and add it to the cache of
  549. * available devices.
  550. */
  551. static struct nfs4_file_layout_dsaddr *
  552. decode_and_add_device(struct inode *inode, struct pnfs_device *dev, gfp_t gfp_flags)
  553. {
  554. struct nfs4_deviceid_node *d;
  555. struct nfs4_file_layout_dsaddr *n, *new;
  556. new = decode_device(inode, dev, gfp_flags);
  557. if (!new) {
  558. printk(KERN_WARNING "NFS: %s: Could not decode or add device\n",
  559. __func__);
  560. return NULL;
  561. }
  562. d = nfs4_insert_deviceid_node(&new->id_node);
  563. n = container_of(d, struct nfs4_file_layout_dsaddr, id_node);
  564. if (n != new) {
  565. nfs4_fl_free_deviceid(new);
  566. return n;
  567. }
  568. return new;
  569. }
  570. /*
  571. * Retrieve the information for dev_id, add it to the list
  572. * of available devices, and return it.
  573. */
  574. struct nfs4_file_layout_dsaddr *
  575. filelayout_get_device_info(struct inode *inode,
  576. struct nfs4_deviceid *dev_id,
  577. struct rpc_cred *cred,
  578. gfp_t gfp_flags)
  579. {
  580. struct pnfs_device *pdev = NULL;
  581. u32 max_resp_sz;
  582. int max_pages;
  583. struct page **pages = NULL;
  584. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  585. int rc, i;
  586. struct nfs_server *server = NFS_SERVER(inode);
  587. /*
  588. * Use the session max response size as the basis for setting
  589. * GETDEVICEINFO's maxcount
  590. */
  591. max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
  592. max_pages = nfs_page_array_len(0, max_resp_sz);
  593. dprintk("%s inode %p max_resp_sz %u max_pages %d\n",
  594. __func__, inode, max_resp_sz, max_pages);
  595. pdev = kzalloc(sizeof(struct pnfs_device), gfp_flags);
  596. if (pdev == NULL)
  597. return NULL;
  598. pages = kzalloc(max_pages * sizeof(struct page *), gfp_flags);
  599. if (pages == NULL) {
  600. kfree(pdev);
  601. return NULL;
  602. }
  603. for (i = 0; i < max_pages; i++) {
  604. pages[i] = alloc_page(gfp_flags);
  605. if (!pages[i])
  606. goto out_free;
  607. }
  608. memcpy(&pdev->dev_id, dev_id, sizeof(*dev_id));
  609. pdev->layout_type = LAYOUT_NFSV4_1_FILES;
  610. pdev->pages = pages;
  611. pdev->pgbase = 0;
  612. pdev->pglen = max_resp_sz;
  613. pdev->mincount = 0;
  614. pdev->maxcount = max_resp_sz - nfs41_maxgetdevinfo_overhead;
  615. rc = nfs4_proc_getdeviceinfo(server, pdev, cred);
  616. dprintk("%s getdevice info returns %d\n", __func__, rc);
  617. if (rc)
  618. goto out_free;
  619. /*
  620. * Found new device, need to decode it and then add it to the
  621. * list of known devices for this mountpoint.
  622. */
  623. dsaddr = decode_and_add_device(inode, pdev, gfp_flags);
  624. out_free:
  625. for (i = 0; i < max_pages; i++)
  626. __free_page(pages[i]);
  627. kfree(pages);
  628. kfree(pdev);
  629. dprintk("<-- %s dsaddr %p\n", __func__, dsaddr);
  630. return dsaddr;
  631. }
  632. void
  633. nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  634. {
  635. nfs4_put_deviceid_node(&dsaddr->id_node);
  636. }
  637. /*
  638. * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
  639. * Then: ((res + fsi) % dsaddr->stripe_count)
  640. */
  641. u32
  642. nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
  643. {
  644. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  645. u64 tmp;
  646. tmp = offset - flseg->pattern_offset;
  647. do_div(tmp, flseg->stripe_unit);
  648. tmp += flseg->first_stripe_index;
  649. return do_div(tmp, flseg->dsaddr->stripe_count);
  650. }
  651. u32
  652. nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
  653. {
  654. return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
  655. }
  656. struct nfs_fh *
  657. nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
  658. {
  659. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  660. u32 i;
  661. if (flseg->stripe_type == STRIPE_SPARSE) {
  662. if (flseg->num_fh == 1)
  663. i = 0;
  664. else if (flseg->num_fh == 0)
  665. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  666. return NULL;
  667. else
  668. i = nfs4_fl_calc_ds_index(lseg, j);
  669. } else
  670. i = j;
  671. return flseg->fh_array[i];
  672. }
  673. static void nfs4_wait_ds_connect(struct nfs4_pnfs_ds *ds)
  674. {
  675. might_sleep();
  676. wait_on_bit(&ds->ds_state, NFS4DS_CONNECTING,
  677. nfs_wait_bit_killable, TASK_KILLABLE);
  678. }
  679. static void nfs4_clear_ds_conn_bit(struct nfs4_pnfs_ds *ds)
  680. {
  681. smp_mb__before_clear_bit();
  682. clear_bit(NFS4DS_CONNECTING, &ds->ds_state);
  683. smp_mb__after_clear_bit();
  684. wake_up_bit(&ds->ds_state, NFS4DS_CONNECTING);
  685. }
  686. struct nfs4_pnfs_ds *
  687. nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
  688. {
  689. struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
  690. struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
  691. struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
  692. if (filelayout_test_devid_unavailable(devid))
  693. return NULL;
  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. return NULL;
  699. }
  700. if (ds->ds_clp)
  701. return ds;
  702. if (test_and_set_bit(NFS4DS_CONNECTING, &ds->ds_state) == 0) {
  703. struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
  704. int err;
  705. err = nfs4_ds_connect(s, ds);
  706. if (err) {
  707. nfs4_mark_deviceid_unavailable(devid);
  708. ds = NULL;
  709. }
  710. nfs4_clear_ds_conn_bit(ds);
  711. } else {
  712. /* Either ds is connected, or ds is NULL */
  713. nfs4_wait_ds_connect(ds);
  714. }
  715. return ds;
  716. }
  717. module_param(dataserver_retrans, uint, 0644);
  718. MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
  719. "retries a request before it attempts further "
  720. " recovery action.");
  721. module_param(dataserver_timeo, uint, 0644);
  722. MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
  723. "NFSv4.1 client waits for a response from a "
  724. " data server before it retries an NFS request.");