nfs4filelayoutdev.c 20 KB

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