objlayout.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * Unmarshall layout and store it in pnfslay.
  44. */
  45. struct pnfs_layout_segment *
  46. objlayout_alloc_lseg(struct pnfs_layout_hdr *pnfslay,
  47. struct nfs4_layoutget_res *lgr,
  48. gfp_t gfp_flags)
  49. {
  50. int status = -ENOMEM;
  51. struct xdr_stream stream;
  52. struct xdr_buf buf = {
  53. .pages = lgr->layoutp->pages,
  54. .page_len = lgr->layoutp->len,
  55. .buflen = lgr->layoutp->len,
  56. .len = lgr->layoutp->len,
  57. };
  58. struct page *scratch;
  59. struct pnfs_layout_segment *lseg;
  60. dprintk("%s: Begin pnfslay %p\n", __func__, pnfslay);
  61. scratch = alloc_page(gfp_flags);
  62. if (!scratch)
  63. goto err_nofree;
  64. xdr_init_decode(&stream, &buf, NULL);
  65. xdr_set_scratch_buffer(&stream, page_address(scratch), PAGE_SIZE);
  66. status = objio_alloc_lseg(&lseg, pnfslay, &lgr->range, &stream, gfp_flags);
  67. if (unlikely(status)) {
  68. dprintk("%s: objio_alloc_lseg Return err %d\n", __func__,
  69. status);
  70. goto err;
  71. }
  72. __free_page(scratch);
  73. dprintk("%s: Return %p\n", __func__, lseg);
  74. return lseg;
  75. err:
  76. __free_page(scratch);
  77. err_nofree:
  78. dprintk("%s: Err Return=>%d\n", __func__, status);
  79. return ERR_PTR(status);
  80. }
  81. /*
  82. * Free a layout segement
  83. */
  84. void
  85. objlayout_free_lseg(struct pnfs_layout_segment *lseg)
  86. {
  87. dprintk("%s: freeing layout segment %p\n", __func__, lseg);
  88. if (unlikely(!lseg))
  89. return;
  90. objio_free_lseg(lseg);
  91. }
  92. /*
  93. * Get Device Info API for io engines
  94. */
  95. struct objlayout_deviceinfo {
  96. struct page *page;
  97. struct pnfs_osd_deviceaddr da; /* This must be last */
  98. };
  99. /* Initialize and call nfs_getdeviceinfo, then decode and return a
  100. * "struct pnfs_osd_deviceaddr *" Eventually objlayout_put_deviceinfo()
  101. * should be called.
  102. */
  103. int objlayout_get_deviceinfo(struct pnfs_layout_hdr *pnfslay,
  104. struct nfs4_deviceid *d_id, struct pnfs_osd_deviceaddr **deviceaddr,
  105. gfp_t gfp_flags)
  106. {
  107. struct objlayout_deviceinfo *odi;
  108. struct pnfs_device pd;
  109. struct super_block *sb;
  110. struct page *page, **pages;
  111. u32 *p;
  112. int err;
  113. page = alloc_page(gfp_flags);
  114. if (!page)
  115. return -ENOMEM;
  116. pages = &page;
  117. pd.pages = pages;
  118. memcpy(&pd.dev_id, d_id, sizeof(*d_id));
  119. pd.layout_type = LAYOUT_OSD2_OBJECTS;
  120. pd.pages = &page;
  121. pd.pgbase = 0;
  122. pd.pglen = PAGE_SIZE;
  123. pd.mincount = 0;
  124. sb = pnfslay->plh_inode->i_sb;
  125. err = nfs4_proc_getdeviceinfo(NFS_SERVER(pnfslay->plh_inode), &pd);
  126. dprintk("%s nfs_getdeviceinfo returned %d\n", __func__, err);
  127. if (err)
  128. goto err_out;
  129. p = page_address(page);
  130. odi = kzalloc(sizeof(*odi), gfp_flags);
  131. if (!odi) {
  132. err = -ENOMEM;
  133. goto err_out;
  134. }
  135. pnfs_osd_xdr_decode_deviceaddr(&odi->da, p);
  136. odi->page = page;
  137. *deviceaddr = &odi->da;
  138. return 0;
  139. err_out:
  140. __free_page(page);
  141. return err;
  142. }
  143. void objlayout_put_deviceinfo(struct pnfs_osd_deviceaddr *deviceaddr)
  144. {
  145. struct objlayout_deviceinfo *odi = container_of(deviceaddr,
  146. struct objlayout_deviceinfo,
  147. da);
  148. __free_page(odi->page);
  149. kfree(odi);
  150. }