nfs4filelayoutdev.c 20 KB

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