blocklayoutxdr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (c) 2014-2016 Christoph Hellwig.
  3. */
  4. #include <linux/sunrpc/svc.h>
  5. #include <linux/exportfs.h>
  6. #include <linux/iomap.h>
  7. #include <linux/nfs4.h>
  8. #include "nfsd.h"
  9. #include "blocklayoutxdr.h"
  10. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  11. __be32
  12. nfsd4_block_encode_layoutget(struct xdr_stream *xdr,
  13. struct nfsd4_layoutget *lgp)
  14. {
  15. struct pnfs_block_extent *b = lgp->lg_content;
  16. int len = sizeof(__be32) + 5 * sizeof(__be64) + sizeof(__be32);
  17. __be32 *p;
  18. p = xdr_reserve_space(xdr, sizeof(__be32) + len);
  19. if (!p)
  20. return nfserr_toosmall;
  21. *p++ = cpu_to_be32(len);
  22. *p++ = cpu_to_be32(1); /* we always return a single extent */
  23. p = xdr_encode_opaque_fixed(p, &b->vol_id,
  24. sizeof(struct nfsd4_deviceid));
  25. p = xdr_encode_hyper(p, b->foff);
  26. p = xdr_encode_hyper(p, b->len);
  27. p = xdr_encode_hyper(p, b->soff);
  28. *p++ = cpu_to_be32(b->es);
  29. return 0;
  30. }
  31. static int
  32. nfsd4_block_encode_volume(struct xdr_stream *xdr, struct pnfs_block_volume *b)
  33. {
  34. __be32 *p;
  35. int len;
  36. switch (b->type) {
  37. case PNFS_BLOCK_VOLUME_SIMPLE:
  38. len = 4 + 4 + 8 + 4 + (XDR_QUADLEN(b->simple.sig_len) << 2);
  39. p = xdr_reserve_space(xdr, len);
  40. if (!p)
  41. return -ETOOSMALL;
  42. *p++ = cpu_to_be32(b->type);
  43. *p++ = cpu_to_be32(1); /* single signature */
  44. p = xdr_encode_hyper(p, b->simple.offset);
  45. p = xdr_encode_opaque(p, b->simple.sig, b->simple.sig_len);
  46. break;
  47. case PNFS_BLOCK_VOLUME_SCSI:
  48. len = 4 + 4 + 4 + 4 + (XDR_QUADLEN(b->scsi.designator_len) << 2) + 8;
  49. p = xdr_reserve_space(xdr, len);
  50. if (!p)
  51. return -ETOOSMALL;
  52. *p++ = cpu_to_be32(b->type);
  53. *p++ = cpu_to_be32(b->scsi.code_set);
  54. *p++ = cpu_to_be32(b->scsi.designator_type);
  55. p = xdr_encode_opaque(p, b->scsi.designator, b->scsi.designator_len);
  56. p = xdr_encode_hyper(p, b->scsi.pr_key);
  57. break;
  58. default:
  59. return -ENOTSUPP;
  60. }
  61. return len;
  62. }
  63. __be32
  64. nfsd4_block_encode_getdeviceinfo(struct xdr_stream *xdr,
  65. struct nfsd4_getdeviceinfo *gdp)
  66. {
  67. struct pnfs_block_deviceaddr *dev = gdp->gd_device;
  68. int len = sizeof(__be32), ret, i;
  69. __be32 *p;
  70. p = xdr_reserve_space(xdr, len + sizeof(__be32));
  71. if (!p)
  72. return nfserr_resource;
  73. for (i = 0; i < dev->nr_volumes; i++) {
  74. ret = nfsd4_block_encode_volume(xdr, &dev->volumes[i]);
  75. if (ret < 0)
  76. return nfserrno(ret);
  77. len += ret;
  78. }
  79. /*
  80. * Fill in the overall length and number of volumes at the beginning
  81. * of the layout.
  82. */
  83. *p++ = cpu_to_be32(len);
  84. *p++ = cpu_to_be32(dev->nr_volumes);
  85. return 0;
  86. }
  87. int
  88. nfsd4_block_decode_layoutupdate(__be32 *p, u32 len, struct iomap **iomapp,
  89. u32 block_size)
  90. {
  91. struct iomap *iomaps;
  92. u32 nr_iomaps, i;
  93. if (len < sizeof(u32)) {
  94. dprintk("%s: extent array too small: %u\n", __func__, len);
  95. return -EINVAL;
  96. }
  97. len -= sizeof(u32);
  98. if (len % PNFS_BLOCK_EXTENT_SIZE) {
  99. dprintk("%s: extent array invalid: %u\n", __func__, len);
  100. return -EINVAL;
  101. }
  102. nr_iomaps = be32_to_cpup(p++);
  103. if (nr_iomaps != len / PNFS_BLOCK_EXTENT_SIZE) {
  104. dprintk("%s: extent array size mismatch: %u/%u\n",
  105. __func__, len, nr_iomaps);
  106. return -EINVAL;
  107. }
  108. iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
  109. if (!iomaps) {
  110. dprintk("%s: failed to allocate extent array\n", __func__);
  111. return -ENOMEM;
  112. }
  113. for (i = 0; i < nr_iomaps; i++) {
  114. struct pnfs_block_extent bex;
  115. memcpy(&bex.vol_id, p, sizeof(struct nfsd4_deviceid));
  116. p += XDR_QUADLEN(sizeof(struct nfsd4_deviceid));
  117. p = xdr_decode_hyper(p, &bex.foff);
  118. if (bex.foff & (block_size - 1)) {
  119. dprintk("%s: unaligned offset 0x%llx\n",
  120. __func__, bex.foff);
  121. goto fail;
  122. }
  123. p = xdr_decode_hyper(p, &bex.len);
  124. if (bex.len & (block_size - 1)) {
  125. dprintk("%s: unaligned length 0x%llx\n",
  126. __func__, bex.foff);
  127. goto fail;
  128. }
  129. p = xdr_decode_hyper(p, &bex.soff);
  130. if (bex.soff & (block_size - 1)) {
  131. dprintk("%s: unaligned disk offset 0x%llx\n",
  132. __func__, bex.soff);
  133. goto fail;
  134. }
  135. bex.es = be32_to_cpup(p++);
  136. if (bex.es != PNFS_BLOCK_READWRITE_DATA) {
  137. dprintk("%s: incorrect extent state %d\n",
  138. __func__, bex.es);
  139. goto fail;
  140. }
  141. iomaps[i].offset = bex.foff;
  142. iomaps[i].length = bex.len;
  143. }
  144. *iomapp = iomaps;
  145. return nr_iomaps;
  146. fail:
  147. kfree(iomaps);
  148. return -EINVAL;
  149. }
  150. int
  151. nfsd4_scsi_decode_layoutupdate(__be32 *p, u32 len, struct iomap **iomapp,
  152. u32 block_size)
  153. {
  154. struct iomap *iomaps;
  155. u32 nr_iomaps, expected, i;
  156. if (len < sizeof(u32)) {
  157. dprintk("%s: extent array too small: %u\n", __func__, len);
  158. return -EINVAL;
  159. }
  160. nr_iomaps = be32_to_cpup(p++);
  161. expected = sizeof(__be32) + nr_iomaps * PNFS_SCSI_RANGE_SIZE;
  162. if (len != expected) {
  163. dprintk("%s: extent array size mismatch: %u/%u\n",
  164. __func__, len, expected);
  165. return -EINVAL;
  166. }
  167. iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
  168. if (!iomaps) {
  169. dprintk("%s: failed to allocate extent array\n", __func__);
  170. return -ENOMEM;
  171. }
  172. for (i = 0; i < nr_iomaps; i++) {
  173. u64 val;
  174. p = xdr_decode_hyper(p, &val);
  175. if (val & (block_size - 1)) {
  176. dprintk("%s: unaligned offset 0x%llx\n", __func__, val);
  177. goto fail;
  178. }
  179. iomaps[i].offset = val;
  180. p = xdr_decode_hyper(p, &val);
  181. if (val & (block_size - 1)) {
  182. dprintk("%s: unaligned length 0x%llx\n", __func__, val);
  183. goto fail;
  184. }
  185. iomaps[i].length = val;
  186. }
  187. *iomapp = iomaps;
  188. return nr_iomaps;
  189. fail:
  190. kfree(iomaps);
  191. return -EINVAL;
  192. }