filelayoutdev.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 "filelayout.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. void
  40. nfs4_fl_free_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  41. {
  42. struct nfs4_pnfs_ds *ds;
  43. int i;
  44. nfs4_print_deviceid(&dsaddr->id_node.deviceid);
  45. for (i = 0; i < dsaddr->ds_num; i++) {
  46. ds = dsaddr->ds_list[i];
  47. if (ds != NULL)
  48. nfs4_pnfs_ds_put(ds);
  49. }
  50. kfree(dsaddr->stripe_indices);
  51. kfree_rcu(dsaddr, id_node.rcu);
  52. }
  53. /* Decode opaque device data and return the result */
  54. struct nfs4_file_layout_dsaddr *
  55. nfs4_fl_alloc_deviceid_node(struct nfs_server *server, struct pnfs_device *pdev,
  56. gfp_t gfp_flags)
  57. {
  58. int i;
  59. u32 cnt, num;
  60. u8 *indexp;
  61. __be32 *p;
  62. u8 *stripe_indices;
  63. u8 max_stripe_index;
  64. struct nfs4_file_layout_dsaddr *dsaddr = NULL;
  65. struct xdr_stream stream;
  66. struct xdr_buf buf;
  67. struct page *scratch;
  68. struct list_head dsaddrs;
  69. struct nfs4_pnfs_ds_addr *da;
  70. /* set up xdr stream */
  71. scratch = alloc_page(gfp_flags);
  72. if (!scratch)
  73. goto out_err;
  74. xdr_init_decode_pages(&stream, &buf, pdev->pages, pdev->pglen);
  75. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  76. /* Get the stripe count (number of stripe index) */
  77. p = xdr_inline_decode(&stream, 4);
  78. if (unlikely(!p))
  79. goto out_err_free_scratch;
  80. cnt = be32_to_cpup(p);
  81. dprintk("%s stripe count %d\n", __func__, cnt);
  82. if (cnt > NFS4_PNFS_MAX_STRIPE_CNT) {
  83. printk(KERN_WARNING "NFS: %s: stripe count %d greater than "
  84. "supported maximum %d\n", __func__,
  85. cnt, NFS4_PNFS_MAX_STRIPE_CNT);
  86. goto out_err_free_scratch;
  87. }
  88. /* read stripe indices */
  89. stripe_indices = kcalloc(cnt, sizeof(u8), gfp_flags);
  90. if (!stripe_indices)
  91. goto out_err_free_scratch;
  92. p = xdr_inline_decode(&stream, cnt << 2);
  93. if (unlikely(!p))
  94. goto out_err_free_stripe_indices;
  95. indexp = &stripe_indices[0];
  96. max_stripe_index = 0;
  97. for (i = 0; i < cnt; i++) {
  98. *indexp = be32_to_cpup(p++);
  99. max_stripe_index = max(max_stripe_index, *indexp);
  100. indexp++;
  101. }
  102. /* Check the multipath list count */
  103. p = xdr_inline_decode(&stream, 4);
  104. if (unlikely(!p))
  105. goto out_err_free_stripe_indices;
  106. num = be32_to_cpup(p);
  107. dprintk("%s ds_num %u\n", __func__, num);
  108. if (num > NFS4_PNFS_MAX_MULTI_CNT) {
  109. printk(KERN_WARNING "NFS: %s: multipath count %d greater than "
  110. "supported maximum %d\n", __func__,
  111. num, NFS4_PNFS_MAX_MULTI_CNT);
  112. goto out_err_free_stripe_indices;
  113. }
  114. /* validate stripe indices are all < num */
  115. if (max_stripe_index >= num) {
  116. printk(KERN_WARNING "NFS: %s: stripe index %u >= num ds %u\n",
  117. __func__, max_stripe_index, num);
  118. goto out_err_free_stripe_indices;
  119. }
  120. dsaddr = kzalloc(sizeof(*dsaddr) +
  121. (sizeof(struct nfs4_pnfs_ds *) * (num - 1)),
  122. gfp_flags);
  123. if (!dsaddr)
  124. goto out_err_free_stripe_indices;
  125. dsaddr->stripe_count = cnt;
  126. dsaddr->stripe_indices = stripe_indices;
  127. stripe_indices = NULL;
  128. dsaddr->ds_num = num;
  129. nfs4_init_deviceid_node(&dsaddr->id_node, server, &pdev->dev_id);
  130. INIT_LIST_HEAD(&dsaddrs);
  131. for (i = 0; i < dsaddr->ds_num; i++) {
  132. int j;
  133. u32 mp_count;
  134. p = xdr_inline_decode(&stream, 4);
  135. if (unlikely(!p))
  136. goto out_err_free_deviceid;
  137. mp_count = be32_to_cpup(p); /* multipath count */
  138. for (j = 0; j < mp_count; j++) {
  139. da = nfs4_decode_mp_ds_addr(server->nfs_client->cl_net,
  140. &stream, gfp_flags);
  141. if (da)
  142. list_add_tail(&da->da_node, &dsaddrs);
  143. }
  144. if (list_empty(&dsaddrs)) {
  145. dprintk("%s: no suitable DS addresses found\n",
  146. __func__);
  147. goto out_err_free_deviceid;
  148. }
  149. dsaddr->ds_list[i] = nfs4_pnfs_ds_add(&dsaddrs, gfp_flags);
  150. if (!dsaddr->ds_list[i])
  151. goto out_err_drain_dsaddrs;
  152. /* If DS was already in cache, free ds addrs */
  153. while (!list_empty(&dsaddrs)) {
  154. da = list_first_entry(&dsaddrs,
  155. struct nfs4_pnfs_ds_addr,
  156. da_node);
  157. list_del_init(&da->da_node);
  158. kfree(da->da_remotestr);
  159. kfree(da);
  160. }
  161. }
  162. __free_page(scratch);
  163. return dsaddr;
  164. out_err_drain_dsaddrs:
  165. while (!list_empty(&dsaddrs)) {
  166. da = list_first_entry(&dsaddrs, struct nfs4_pnfs_ds_addr,
  167. da_node);
  168. list_del_init(&da->da_node);
  169. kfree(da->da_remotestr);
  170. kfree(da);
  171. }
  172. out_err_free_deviceid:
  173. nfs4_fl_free_deviceid(dsaddr);
  174. /* stripe_indicies was part of dsaddr */
  175. goto out_err_free_scratch;
  176. out_err_free_stripe_indices:
  177. kfree(stripe_indices);
  178. out_err_free_scratch:
  179. __free_page(scratch);
  180. out_err:
  181. dprintk("%s ERROR: returning NULL\n", __func__);
  182. return NULL;
  183. }
  184. void
  185. nfs4_fl_put_deviceid(struct nfs4_file_layout_dsaddr *dsaddr)
  186. {
  187. nfs4_put_deviceid_node(&dsaddr->id_node);
  188. }
  189. /*
  190. * Want res = (offset - layout->pattern_offset)/ layout->stripe_unit
  191. * Then: ((res + fsi) % dsaddr->stripe_count)
  192. */
  193. u32
  194. nfs4_fl_calc_j_index(struct pnfs_layout_segment *lseg, loff_t offset)
  195. {
  196. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  197. u64 tmp;
  198. tmp = offset - flseg->pattern_offset;
  199. do_div(tmp, flseg->stripe_unit);
  200. tmp += flseg->first_stripe_index;
  201. return do_div(tmp, flseg->dsaddr->stripe_count);
  202. }
  203. u32
  204. nfs4_fl_calc_ds_index(struct pnfs_layout_segment *lseg, u32 j)
  205. {
  206. return FILELAYOUT_LSEG(lseg)->dsaddr->stripe_indices[j];
  207. }
  208. struct nfs_fh *
  209. nfs4_fl_select_ds_fh(struct pnfs_layout_segment *lseg, u32 j)
  210. {
  211. struct nfs4_filelayout_segment *flseg = FILELAYOUT_LSEG(lseg);
  212. u32 i;
  213. if (flseg->stripe_type == STRIPE_SPARSE) {
  214. if (flseg->num_fh == 1)
  215. i = 0;
  216. else if (flseg->num_fh == 0)
  217. /* Use the MDS OPEN fh set in nfs_read_rpcsetup */
  218. return NULL;
  219. else
  220. i = nfs4_fl_calc_ds_index(lseg, j);
  221. } else
  222. i = j;
  223. return flseg->fh_array[i];
  224. }
  225. /* Upon return, either ds is connected, or ds is NULL */
  226. struct nfs4_pnfs_ds *
  227. nfs4_fl_prepare_ds(struct pnfs_layout_segment *lseg, u32 ds_idx)
  228. {
  229. struct nfs4_file_layout_dsaddr *dsaddr = FILELAYOUT_LSEG(lseg)->dsaddr;
  230. struct nfs4_pnfs_ds *ds = dsaddr->ds_list[ds_idx];
  231. struct nfs4_deviceid_node *devid = FILELAYOUT_DEVID_NODE(lseg);
  232. struct nfs4_pnfs_ds *ret = ds;
  233. struct nfs_server *s = NFS_SERVER(lseg->pls_layout->plh_inode);
  234. if (ds == NULL) {
  235. printk(KERN_ERR "NFS: %s: No data server for offset index %d\n",
  236. __func__, ds_idx);
  237. pnfs_generic_mark_devid_invalid(devid);
  238. goto out;
  239. }
  240. smp_rmb();
  241. if (ds->ds_clp)
  242. goto out_test_devid;
  243. nfs4_pnfs_ds_connect(s, ds, devid, dataserver_timeo,
  244. dataserver_retrans, 4,
  245. s->nfs_client->cl_minorversion,
  246. s->nfs_client->cl_rpcclient->cl_auth->au_flavor);
  247. out_test_devid:
  248. if (filelayout_test_devid_unavailable(devid))
  249. ret = NULL;
  250. out:
  251. return ret;
  252. }
  253. module_param(dataserver_retrans, uint, 0644);
  254. MODULE_PARM_DESC(dataserver_retrans, "The number of times the NFSv4.1 client "
  255. "retries a request before it attempts further "
  256. " recovery action.");
  257. module_param(dataserver_timeo, uint, 0644);
  258. MODULE_PARM_DESC(dataserver_timeo, "The time (in tenths of a second) the "
  259. "NFSv4.1 client waits for a response from a "
  260. " data server before it retries an NFS request.");