objlayout.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * pNFS Objects layout driver high level definitions
  3. *
  4. * Copyright (C) 2007 Panasas Inc. [year of first publication]
  5. * All rights reserved.
  6. *
  7. * Benny Halevy <bhalevy@panasas.com>
  8. * Boaz Harrosh <bharrosh@panasas.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * See the file COPYING included with this distribution for more details.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions
  16. * are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. * 3. Neither the name of the Panasas company nor the names of its
  24. * contributors may be used to endorse or promote products derived
  25. * from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  33. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
  34. * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  35. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  36. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  37. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. */
  39. #include <scsi/osd_initiator.h>
  40. #include "objlayout.h"
  41. #define NFSDBG_FACILITY NFSDBG_PNFS_LD
  42. /*
  43. * Create a objlayout layout structure for the given inode and return it.
  44. */
  45. struct pnfs_layout_hdr *
  46. objlayout_alloc_layout_hdr(struct inode *inode, gfp_t gfp_flags)
  47. {
  48. struct objlayout *objlay;
  49. objlay = kzalloc(sizeof(struct objlayout), gfp_flags);
  50. dprintk("%s: Return %p\n", __func__, objlay);
  51. return &objlay->pnfs_layout;
  52. }
  53. /*
  54. * Free an objlayout layout structure
  55. */
  56. void
  57. objlayout_free_layout_hdr(struct pnfs_layout_hdr *lo)
  58. {
  59. struct objlayout *objlay = OBJLAYOUT(lo);
  60. dprintk("%s: objlay %p\n", __func__, objlay);
  61. kfree(objlay);
  62. }
  63. /*
  64. * Unmarshall layout and store it in pnfslay.
  65. */
  66. struct pnfs_layout_segment *
  67. objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay,
  68. struct nfs4_layoutget_res *lgr,
  69. gfp_t gfp_flags)
  70. {
  71. int status = -ENOMEM;
  72. struct xdr_stream stream;
  73. struct xdr_buf buf = {
  74. .pages = lgr->layoutp->pages,
  75. .page_len = lgr->layoutp->len,
  76. .buflen = lgr->layoutp->len,
  77. .len = lgr->layoutp->len,
  78. };
  79. struct page *scratch;
  80. struct pnfs_layout_segment *lseg;
  81. dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay);
  82. scratch = alloc_page(gfp_flags);
  83. if (!scratch)
  84. goto err_nofree;
  85. xdr_init_decode(&stream, &buf, NULL);
  86. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  87. status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags);
  88. if (unlikely(status)) {
  89. dprintk("%s: objio_alloc_lseg Return err %d\n", __func__,
  90. status);
  91. goto err;
  92. }
  93. __free_page(scratch);
  94. dprintk("%s: Return %p\n", __func__, lseg);
  95. return lseg;
  96. err:
  97. __free_page(scratch);
  98. err_nofree:
  99. dprintk("%s: Err Return=>%d\n", __func__, status);
  100. return ERR_PTR(status);
  101. }
  102. /*
  103. * Free a layout segement
  104. */
  105. void
  106. objlayout_free_lseg(struct pnfs_layout_segment *lseg)
  107. {
  108. dprintk("%s: freeing layout segment %p\n", __func__, lseg);
  109. if (unlikely(!lseg))
  110. return;
  111. objio_free_lseg(lseg);
  112. }
  113. /*
  114. * Get Device Info API for io engines
  115. */
  116. struct objlayout_deviceinfo {
  117. struct page *page;
  118. struct pnfs_osd_deviceaddr da; /* This must be last */
  119. };
  120. /* Initialize and call nfs_getdeviceinfo, then decode and return a
  121. * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
  122. * should be called.
  123. */
  124. int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
  125. struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
  126. gfp_t gfp_flags)
  127. {
  128. struct objlayout_deviceinfo *odi;
  129. struct pnfs_device pd;
  130. struct super_block *sb;
  131. struct page *page, **pages;
  132. u32 *p;
  133. int err;
  134. page = alloc_page(gfp_flags);
  135. if (!page)
  136. return -ENOMEM;
  137. pages = &page;
  138. pd.pages = pages;
  139. memcpy(&pd.dev_id, d_id, sizeof(*d_id));
  140. pd.layout_type = LAYOUT_OSD2_OBJECTS;
  141. pd.pages = &page;
  142. pd.pgbase = 0;
  143. pd.pglen = PAGE_SIZE;
  144. pd.mincount = 0;
  145. sb = pnfslay->plh_inode->i_sb;
  146. err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
  147. dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
  148. if (err)
  149. goto err_out;
  150. p = page_address(page);
  151. odi = kzalloc(sizeof(*odi), gfp_flags);
  152. if (!odi) {
  153. err = -ENOMEM;
  154. goto err_out;
  155. }
  156. pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
  157. odi->page = page;
  158. *deviceaddr = &odi->da;
  159. return 0;
  160. err_out:
  161. __free_page(page);
  162. return err;
  163. }
  164. void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
  165. {
  166. struct objlayout_deviceinfo *odi = container_of(deviceaddr,
  167. struct objlayout_deviceinfo,
  168. da);
  169. __free_page(odi->page);
  170. kfree(odi);
  171. }