blocklayoutxdr.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2014-2016 Christoph Hellwig.
  4. */
  5. #include <linux/sunrpc/svc.h>
  6. #include <linux/exportfs.h>
  7. #include <linux/iomap.h>
  8. #include <linux/nfs4.h>
  9. #include "nfsd.h"
  10. #include "blocklayoutxdr.h"
  11. #include "vfs.h"
  12. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  13. __be32
  14. nfsd4_block_encode_layoutget(struct xdr_stream *xdr,
  15. const struct nfsd4_layoutget *lgp)
  16. {
  17. const struct pnfs_block_extent *b = lgp->lg_content;
  18. int len = sizeof(__be32) + 5 * sizeof(__be64) + sizeof(__be32);
  19. __be32 *p;
  20. p = xdr_reserve_space(xdr, sizeof(__be32) + len);
  21. if (!p)
  22. return nfserr_toosmall;
  23. *p++ = cpu_to_be32(len);
  24. *p++ = cpu_to_be32(1); /* we always return a single extent */
  25. p = svcxdr_encode_deviceid4(p, &b->vol_id);
  26. p = xdr_encode_hyper(p, b->foff);
  27. p = xdr_encode_hyper(p, b->len);
  28. p = xdr_encode_hyper(p, b->soff);
  29. *p++ = cpu_to_be32(b->es);
  30. return 0;
  31. }
  32. static int
  33. nfsd4_block_encode_volume(struct xdr_stream *xdr, struct pnfs_block_volume *b)
  34. {
  35. __be32 *p;
  36. int len;
  37. switch (b->type) {
  38. case PNFS_BLOCK_VOLUME_SIMPLE:
  39. len = 4 + 4 + 8 + 4 + (XDR_QUADLEN(b->simple.sig_len) << 2);
  40. p = xdr_reserve_space(xdr, len);
  41. if (!p)
  42. return -ETOOSMALL;
  43. *p++ = cpu_to_be32(b->type);
  44. *p++ = cpu_to_be32(1); /* single signature */
  45. p = xdr_encode_hyper(p, b->simple.offset);
  46. p = xdr_encode_opaque(p, b->simple.sig, b->simple.sig_len);
  47. break;
  48. case PNFS_BLOCK_VOLUME_SCSI:
  49. len = 4 + 4 + 4 + 4 + (XDR_QUADLEN(b->scsi.designator_len) << 2) + 8;
  50. p = xdr_reserve_space(xdr, len);
  51. if (!p)
  52. return -ETOOSMALL;
  53. *p++ = cpu_to_be32(b->type);
  54. *p++ = cpu_to_be32(b->scsi.code_set);
  55. *p++ = cpu_to_be32(b->scsi.designator_type);
  56. p = xdr_encode_opaque(p, b->scsi.designator, b->scsi.designator_len);
  57. p = xdr_encode_hyper(p, b->scsi.pr_key);
  58. break;
  59. default:
  60. return -ENOTSUPP;
  61. }
  62. return len;
  63. }
  64. __be32
  65. nfsd4_block_encode_getdeviceinfo(struct xdr_stream *xdr,
  66. const struct nfsd4_getdeviceinfo *gdp)
  67. {
  68. struct pnfs_block_deviceaddr *dev = gdp->gd_device;
  69. int len = sizeof(__be32), ret, i;
  70. __be32 *p;
  71. /*
  72. * See paragraph 5 of RFC 8881 S18.40.3.
  73. */
  74. if (!gdp->gd_maxcount) {
  75. if (xdr_stream_encode_u32(xdr, 0) != XDR_UNIT)
  76. return nfserr_resource;
  77. return nfs_ok;
  78. }
  79. p = xdr_reserve_space(xdr, len + sizeof(__be32));
  80. if (!p)
  81. return nfserr_resource;
  82. for (i = 0; i < dev->nr_volumes; i++) {
  83. ret = nfsd4_block_encode_volume(xdr, &dev->volumes[i]);
  84. if (ret < 0)
  85. return nfserrno(ret);
  86. len += ret;
  87. }
  88. /*
  89. * Fill in the overall length and number of volumes at the beginning
  90. * of the layout.
  91. */
  92. *p++ = cpu_to_be32(len);
  93. *p++ = cpu_to_be32(dev->nr_volumes);
  94. return 0;
  95. }
  96. /**
  97. * nfsd4_block_decode_layoutupdate - decode the block layout extent array
  98. * @xdr: subbuf set to the encoded array
  99. * @iomapp: pointer to store the decoded extent array
  100. * @nr_iomapsp: pointer to store the number of extents
  101. * @block_size: alignment of extent offset and length
  102. *
  103. * This function decodes the opaque field of the layoutupdate4 structure
  104. * in a layoutcommit request for the block layout driver. The field is
  105. * actually an array of extents sent by the client. It also checks that
  106. * the file offset, storage offset and length of each extent are aligned
  107. * by @block_size.
  108. *
  109. * Return values:
  110. * %nfs_ok: Successful decoding, @iomapp and @nr_iomapsp are valid
  111. * %nfserr_bad_xdr: The encoded array in @xdr is invalid
  112. * %nfserr_inval: An unaligned extent found
  113. * %nfserr_delay: Failed to allocate memory for @iomapp
  114. */
  115. __be32
  116. nfsd4_block_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
  117. int *nr_iomapsp, u32 block_size)
  118. {
  119. struct iomap *iomaps;
  120. u32 nr_iomaps, expected, len, i;
  121. __be32 nfserr;
  122. if (xdr_stream_decode_u32(xdr, &nr_iomaps))
  123. return nfserr_bad_xdr;
  124. len = sizeof(__be32) + xdr_stream_remaining(xdr);
  125. expected = sizeof(__be32) + nr_iomaps * PNFS_BLOCK_EXTENT_SIZE;
  126. if (len != expected)
  127. return nfserr_bad_xdr;
  128. iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
  129. if (!iomaps)
  130. return nfserr_delay;
  131. for (i = 0; i < nr_iomaps; i++) {
  132. struct pnfs_block_extent bex;
  133. if (nfsd4_decode_deviceid4(xdr, &bex.vol_id)) {
  134. nfserr = nfserr_bad_xdr;
  135. goto fail;
  136. }
  137. if (xdr_stream_decode_u64(xdr, &bex.foff)) {
  138. nfserr = nfserr_bad_xdr;
  139. goto fail;
  140. }
  141. if (bex.foff & (block_size - 1)) {
  142. nfserr = nfserr_inval;
  143. goto fail;
  144. }
  145. if (xdr_stream_decode_u64(xdr, &bex.len)) {
  146. nfserr = nfserr_bad_xdr;
  147. goto fail;
  148. }
  149. if (bex.len & (block_size - 1)) {
  150. nfserr = nfserr_inval;
  151. goto fail;
  152. }
  153. if (xdr_stream_decode_u64(xdr, &bex.soff)) {
  154. nfserr = nfserr_bad_xdr;
  155. goto fail;
  156. }
  157. if (bex.soff & (block_size - 1)) {
  158. nfserr = nfserr_inval;
  159. goto fail;
  160. }
  161. if (xdr_stream_decode_u32(xdr, &bex.es)) {
  162. nfserr = nfserr_bad_xdr;
  163. goto fail;
  164. }
  165. if (bex.es != PNFS_BLOCK_READWRITE_DATA) {
  166. nfserr = nfserr_inval;
  167. goto fail;
  168. }
  169. iomaps[i].offset = bex.foff;
  170. iomaps[i].length = bex.len;
  171. }
  172. *iomapp = iomaps;
  173. *nr_iomapsp = nr_iomaps;
  174. return nfs_ok;
  175. fail:
  176. kfree(iomaps);
  177. return nfserr;
  178. }
  179. /**
  180. * nfsd4_scsi_decode_layoutupdate - decode the scsi layout extent array
  181. * @xdr: subbuf set to the encoded array
  182. * @iomapp: pointer to store the decoded extent array
  183. * @nr_iomapsp: pointer to store the number of extents
  184. * @block_size: alignment of extent offset and length
  185. *
  186. * This function decodes the opaque field of the layoutupdate4 structure
  187. * in a layoutcommit request for the scsi layout driver. The field is
  188. * actually an array of extents sent by the client. It also checks that
  189. * the offset and length of each extent are aligned by @block_size.
  190. *
  191. * Return values:
  192. * %nfs_ok: Successful decoding, @iomapp and @nr_iomapsp are valid
  193. * %nfserr_bad_xdr: The encoded array in @xdr is invalid
  194. * %nfserr_inval: An unaligned extent found
  195. * %nfserr_delay: Failed to allocate memory for @iomapp
  196. */
  197. __be32
  198. nfsd4_scsi_decode_layoutupdate(struct xdr_stream *xdr, struct iomap **iomapp,
  199. int *nr_iomapsp, u32 block_size)
  200. {
  201. struct iomap *iomaps;
  202. u32 nr_iomaps, expected, len, i;
  203. __be32 nfserr;
  204. if (xdr_stream_decode_u32(xdr, &nr_iomaps))
  205. return nfserr_bad_xdr;
  206. len = sizeof(__be32) + xdr_stream_remaining(xdr);
  207. expected = sizeof(__be32) + nr_iomaps * PNFS_SCSI_RANGE_SIZE;
  208. if (len != expected)
  209. return nfserr_bad_xdr;
  210. iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
  211. if (!iomaps)
  212. return nfserr_delay;
  213. for (i = 0; i < nr_iomaps; i++) {
  214. u64 val;
  215. if (xdr_stream_decode_u64(xdr, &val)) {
  216. nfserr = nfserr_bad_xdr;
  217. goto fail;
  218. }
  219. if (val & (block_size - 1)) {
  220. nfserr = nfserr_inval;
  221. goto fail;
  222. }
  223. iomaps[i].offset = val;
  224. if (xdr_stream_decode_u64(xdr, &val)) {
  225. nfserr = nfserr_bad_xdr;
  226. goto fail;
  227. }
  228. if (val & (block_size - 1)) {
  229. nfserr = nfserr_inval;
  230. goto fail;
  231. }
  232. iomaps[i].length = val;
  233. }
  234. *iomapp = iomaps;
  235. *nr_iomapsp = nr_iomaps;
  236. return nfs_ok;
  237. fail:
  238. kfree(iomaps);
  239. return nfserr;
  240. }