nfs4filelayout.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Module for the pnfs nfs4 file layout driver.
  3. * Defines all I/O and Policy interface operations, plus code
  4. * to register itself with the pNFS client.
  5. *
  6. * Copyright (c) 2002
  7. * The Regents of the University of Michigan
  8. * All Rights Reserved
  9. *
  10. * Dean Hildebrand <dhildebz@umich.edu>
  11. *
  12. * Permission is granted to use, copy, create derivative works, and
  13. * redistribute this software and such derivative works for any purpose,
  14. * so long as the name of the University of Michigan is not used in
  15. * any advertising or publicity pertaining to the use or distribution
  16. * of this software without specific, written prior authorization. If
  17. * the above copyright notice or any other identification of the
  18. * University of Michigan is included in any copy of any portion of
  19. * this software, then the disclaimer below must also be included.
  20. *
  21. * This software is provided as is, without representation or warranty
  22. * of any kind either express or implied, including without limitation
  23. * the implied warranties of merchantability, fitness for a particular
  24. * purpose, or noninfringement. The Regents of the University of
  25. * Michigan shall not be liable for any damages, including special,
  26. * indirect, incidental, or consequential damages, with respect to any
  27. * claim arising out of or in connection with the use of the software,
  28. * even if it has been or is hereafter advised of the possibility of
  29. * such damages.
  30. */
  31. #include <linux/nfs_fs.h>
  32. #include "internal.h"
  33. #include "nfs4filelayout.h"
  34. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  35. MODULE_LICENSE("GPL");
  36. MODULE_AUTHOR("Dean Hildebrand <dhildebz@umich.edu>");
  37. MODULE_DESCRIPTION("The NFSv4 file layout driver");
  38. static int
  39. filelayout_set_layoutdriver(struct nfs_server *nfss)
  40. {
  41. int status = pnfs_alloc_init_deviceid_cache(nfss->nfs_client,
  42. nfs4_fl_free_deviceid_callback);
  43. if (status) {
  44. printk(KERN_WARNING "%s: deviceid cache could not be "
  45. "initialized\n", __func__);
  46. return status;
  47. }
  48. dprintk("%s: deviceid cache has been initialized successfully\n",
  49. __func__);
  50. return 0;
  51. }
  52. /* Clear out the layout by destroying its device list */
  53. static int
  54. filelayout_clear_layoutdriver(struct nfs_server *nfss)
  55. {
  56. dprintk("--> %s\n", __func__);
  57. if (nfss->nfs_client->cl_devid_cache)
  58. pnfs_put_deviceid_cache(nfss->nfs_client);
  59. return 0;
  60. }
  61. /*
  62. * filelayout_check_layout()
  63. *
  64. * Make sure layout segment parameters are sane WRT the device.
  65. * At this point no generic layer initialization of the lseg has occurred,
  66. * and nothing has been added to the layout_hdr cache.
  67. *
  68. */
  69. static int
  70. filelayout_check_layout(struct pnfs_layout_hdr *lo,
  71. struct nfs4_filelayout_segment *fl,
  72. struct nfs4_layoutget_res *lgr,
  73. struct nfs4_deviceid *id)
  74. {
  75. struct nfs4_file_layout_dsaddr *dsaddr;
  76. int status = -EINVAL;
  77. struct nfs_server *nfss = NFS_SERVER(lo->plh_inode);
  78. dprintk("--> %s\n", __func__);
  79. if (fl->pattern_offset > lgr->range.offset) {
  80. dprintk("%s pattern_offset %lld to large\n",
  81. __func__, fl->pattern_offset);
  82. goto out;
  83. }
  84. if (fl->stripe_unit % PAGE_SIZE) {
  85. dprintk("%s Stripe unit (%u) not page aligned\n",
  86. __func__, fl->stripe_unit);
  87. goto out;
  88. }
  89. /* find and reference the deviceid */
  90. dsaddr = nfs4_fl_find_get_deviceid(nfss->nfs_client, id);
  91. if (dsaddr == NULL) {
  92. dsaddr = get_device_info(lo->plh_inode, id);
  93. if (dsaddr == NULL)
  94. goto out;
  95. }
  96. fl->dsaddr = dsaddr;
  97. if (fl->first_stripe_index < 0 ||
  98. fl->first_stripe_index >= dsaddr->stripe_count) {
  99. dprintk("%s Bad first_stripe_index %d\n",
  100. __func__, fl->first_stripe_index);
  101. goto out_put;
  102. }
  103. if ((fl->stripe_type == STRIPE_SPARSE &&
  104. fl->num_fh > 1 && fl->num_fh != dsaddr->ds_num) ||
  105. (fl->stripe_type == STRIPE_DENSE &&
  106. fl->num_fh != dsaddr->stripe_count)) {
  107. dprintk("%s num_fh %u not valid for given packing\n",
  108. __func__, fl->num_fh);
  109. goto out_put;
  110. }
  111. if (fl->stripe_unit % nfss->rsize || fl->stripe_unit % nfss->wsize) {
  112. dprintk("%s Stripe unit (%u) not aligned with rsize %u "
  113. "wsize %u\n", __func__, fl->stripe_unit, nfss->rsize,
  114. nfss->wsize);
  115. }
  116. status = 0;
  117. out:
  118. dprintk("--> %s returns %d\n", __func__, status);
  119. return status;
  120. out_put:
  121. pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache, &dsaddr->deviceid);
  122. goto out;
  123. }
  124. static void filelayout_free_fh_array(struct nfs4_filelayout_segment *fl)
  125. {
  126. int i;
  127. for (i = 0; i < fl->num_fh; i++) {
  128. if (!fl->fh_array[i])
  129. break;
  130. kfree(fl->fh_array[i]);
  131. }
  132. kfree(fl->fh_array);
  133. fl->fh_array = NULL;
  134. }
  135. static void
  136. _filelayout_free_lseg(struct nfs4_filelayout_segment *fl)
  137. {
  138. filelayout_free_fh_array(fl);
  139. kfree(fl);
  140. }
  141. static int
  142. filelayout_decode_layout(struct pnfs_layout_hdr *flo,
  143. struct nfs4_filelayout_segment *fl,
  144. struct nfs4_layoutget_res *lgr,
  145. struct nfs4_deviceid *id)
  146. {
  147. uint32_t *p = (uint32_t *)lgr->layout.buf;
  148. uint32_t nfl_util;
  149. int i;
  150. dprintk("%s: set_layout_map Begin\n", __func__);
  151. memcpy(id, p, sizeof(*id));
  152. p += XDR_QUADLEN(NFS4_DEVICEID4_SIZE);
  153. print_deviceid(id);
  154. nfl_util = be32_to_cpup(p++);
  155. if (nfl_util & NFL4_UFLG_COMMIT_THRU_MDS)
  156. fl->commit_through_mds = 1;
  157. if (nfl_util & NFL4_UFLG_DENSE)
  158. fl->stripe_type = STRIPE_DENSE;
  159. else
  160. fl->stripe_type = STRIPE_SPARSE;
  161. fl->stripe_unit = nfl_util & ~NFL4_UFLG_MASK;
  162. fl->first_stripe_index = be32_to_cpup(p++);
  163. p = xdr_decode_hyper(p, &fl->pattern_offset);
  164. fl->num_fh = be32_to_cpup(p++);
  165. dprintk("%s: nfl_util 0x%X num_fh %u fsi %u po %llu\n",
  166. __func__, nfl_util, fl->num_fh, fl->first_stripe_index,
  167. fl->pattern_offset);
  168. fl->fh_array = kzalloc(fl->num_fh * sizeof(struct nfs_fh *),
  169. GFP_KERNEL);
  170. if (!fl->fh_array)
  171. return -ENOMEM;
  172. for (i = 0; i < fl->num_fh; i++) {
  173. /* Do we want to use a mempool here? */
  174. fl->fh_array[i] = kmalloc(sizeof(struct nfs_fh), GFP_KERNEL);
  175. if (!fl->fh_array[i]) {
  176. filelayout_free_fh_array(fl);
  177. return -ENOMEM;
  178. }
  179. fl->fh_array[i]->size = be32_to_cpup(p++);
  180. if (sizeof(struct nfs_fh) < fl->fh_array[i]->size) {
  181. printk(KERN_ERR "Too big fh %d received %d\n",
  182. i, fl->fh_array[i]->size);
  183. filelayout_free_fh_array(fl);
  184. return -EIO;
  185. }
  186. memcpy(fl->fh_array[i]->data, p, fl->fh_array[i]->size);
  187. p += XDR_QUADLEN(fl->fh_array[i]->size);
  188. dprintk("DEBUG: %s: fh len %d\n", __func__,
  189. fl->fh_array[i]->size);
  190. }
  191. return 0;
  192. }
  193. static struct pnfs_layout_segment *
  194. filelayout_alloc_lseg(struct pnfs_layout_hdr *layoutid,
  195. struct nfs4_layoutget_res *lgr)
  196. {
  197. struct nfs4_filelayout_segment *fl;
  198. int rc;
  199. struct nfs4_deviceid id;
  200. dprintk("--> %s\n", __func__);
  201. fl = kzalloc(sizeof(*fl), GFP_KERNEL);
  202. if (!fl)
  203. return NULL;
  204. rc = filelayout_decode_layout(layoutid, fl, lgr, &id);
  205. if (rc != 0 || filelayout_check_layout(layoutid, fl, lgr, &id)) {
  206. _filelayout_free_lseg(fl);
  207. return NULL;
  208. }
  209. return &fl->generic_hdr;
  210. }
  211. static void
  212. filelayout_free_lseg(struct pnfs_layout_segment *lseg)
  213. {
  214. struct nfs_server *nfss = NFS_SERVER(lseg->pls_layout->plh_inode);
  215. struct nfs4_filelayout_segment *fl = FILELAYOUT_LSEG(lseg);
  216. dprintk("--> %s\n", __func__);
  217. pnfs_put_deviceid(nfss->nfs_client->cl_devid_cache,
  218. &fl->dsaddr->deviceid);
  219. _filelayout_free_lseg(fl);
  220. }
  221. static struct pnfs_layoutdriver_type filelayout_type = {
  222. .id = LAYOUT_NFSV4_1_FILES,
  223. .name = "LAYOUT_NFSV4_1_FILES",
  224. .owner = THIS_MODULE,
  225. .set_layoutdriver = filelayout_set_layoutdriver,
  226. .clear_layoutdriver = filelayout_clear_layoutdriver,
  227. .alloc_lseg = filelayout_alloc_lseg,
  228. .free_lseg = filelayout_free_lseg,
  229. };
  230. static int __init nfs4filelayout_init(void)
  231. {
  232. printk(KERN_INFO "%s: NFSv4 File Layout Driver Registering...\n",
  233. __func__);
  234. return pnfs_register_layoutdriver(&filelayout_type);
  235. }
  236. static void __exit nfs4filelayout_exit(void)
  237. {
  238. printk(KERN_INFO "%s: NFSv4 File Layout Driver Unregistering...\n",
  239. __func__);
  240. pnfs_unregister_layoutdriver(&filelayout_type);
  241. }
  242. module_init(nfs4filelayout_init);
  243. module_exit(nfs4filelayout_exit);