blocklayoutxdr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2014 Christoph Hellwig.
  3. */
  4. #include <linux/sunrpc/svc.h>
  5. #include <linux/exportfs.h>
  6. #include <linux/nfs4.h>
  7. #include "nfsd.h"
  8. #include "blocklayoutxdr.h"
  9. #define NFSDDBG_FACILITY NFSDDBG_PNFS
  10. __be32
  11. nfsd4_block_encode_layoutget(struct xdr_stream *xdr,
  12. struct nfsd4_layoutget *lgp)
  13. {
  14. struct pnfs_block_extent *b = lgp->lg_content;
  15. int len = sizeof(__be32) + 5 * sizeof(__be64) + sizeof(__be32);
  16. __be32 *p;
  17. p = xdr_reserve_space(xdr, sizeof(__be32) + len);
  18. if (!p)
  19. return nfserr_toosmall;
  20. *p++ = cpu_to_be32(len);
  21. *p++ = cpu_to_be32(1); /* we always return a single extent */
  22. p = xdr_encode_opaque_fixed(p, &b->vol_id,
  23. sizeof(struct nfsd4_deviceid));
  24. p = xdr_encode_hyper(p, b->foff);
  25. p = xdr_encode_hyper(p, b->len);
  26. p = xdr_encode_hyper(p, b->soff);
  27. *p++ = cpu_to_be32(b->es);
  28. return 0;
  29. }
  30. static int
  31. nfsd4_block_encode_volume(struct xdr_stream *xdr, struct pnfs_block_volume *b)
  32. {
  33. __be32 *p;
  34. int len;
  35. switch (b->type) {
  36. case PNFS_BLOCK_VOLUME_SIMPLE:
  37. len = 4 + 4 + 8 + 4 + b->simple.sig_len;
  38. p = xdr_reserve_space(xdr, len);
  39. if (!p)
  40. return -ETOOSMALL;
  41. *p++ = cpu_to_be32(b->type);
  42. *p++ = cpu_to_be32(1); /* single signature */
  43. p = xdr_encode_hyper(p, b->simple.offset);
  44. p = xdr_encode_opaque(p, b->simple.sig, b->simple.sig_len);
  45. break;
  46. default:
  47. return -ENOTSUPP;
  48. }
  49. return len;
  50. }
  51. __be32
  52. nfsd4_block_encode_getdeviceinfo(struct xdr_stream *xdr,
  53. struct nfsd4_getdeviceinfo *gdp)
  54. {
  55. struct pnfs_block_deviceaddr *dev = gdp->gd_device;
  56. int len = sizeof(__be32), ret, i;
  57. __be32 *p;
  58. p = xdr_reserve_space(xdr, len + sizeof(__be32));
  59. if (!p)
  60. return nfserr_resource;
  61. for (i = 0; i < dev->nr_volumes; i++) {
  62. ret = nfsd4_block_encode_volume(xdr, &dev->volumes[i]);
  63. if (ret < 0)
  64. return nfserrno(ret);
  65. len += ret;
  66. }
  67. /*
  68. * Fill in the overall length and number of volumes at the beginning
  69. * of the layout.
  70. */
  71. *p++ = cpu_to_be32(len);
  72. *p++ = cpu_to_be32(dev->nr_volumes);
  73. return 0;
  74. }
  75. int
  76. nfsd4_block_decode_layoutupdate(__be32 *p, u32 len, struct iomap **iomapp,
  77. u32 block_size)
  78. {
  79. struct iomap *iomaps;
  80. u32 nr_iomaps, expected, i;
  81. if (len < sizeof(u32)) {
  82. dprintk("%s: extent array too small: %u\n", __func__, len);
  83. return -EINVAL;
  84. }
  85. nr_iomaps = be32_to_cpup(p++);
  86. expected = sizeof(__be32) + nr_iomaps * PNFS_BLOCK_EXTENT_SIZE;
  87. if (len != expected) {
  88. dprintk("%s: extent array size mismatch: %u/%u\n",
  89. __func__, len, expected);
  90. return -EINVAL;
  91. }
  92. iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
  93. if (!iomaps) {
  94. dprintk("%s: failed to allocate extent array\n", __func__);
  95. return -ENOMEM;
  96. }
  97. for (i = 0; i < nr_iomaps; i++) {
  98. struct pnfs_block_extent bex;
  99. memcpy(&bex.vol_id, p, sizeof(struct nfsd4_deviceid));
  100. p += XDR_QUADLEN(sizeof(struct nfsd4_deviceid));
  101. p = xdr_decode_hyper(p, &bex.foff);
  102. if (bex.foff & (block_size - 1)) {
  103. dprintk("%s: unaligned offset 0x%llx\n",
  104. __func__, bex.foff);
  105. goto fail;
  106. }
  107. p = xdr_decode_hyper(p, &bex.len);
  108. if (bex.len & (block_size - 1)) {
  109. dprintk("%s: unaligned length 0x%llx\n",
  110. __func__, bex.foff);
  111. goto fail;
  112. }
  113. p = xdr_decode_hyper(p, &bex.soff);
  114. if (bex.soff & (block_size - 1)) {
  115. dprintk("%s: unaligned disk offset 0x%llx\n",
  116. __func__, bex.soff);
  117. goto fail;
  118. }
  119. bex.es = be32_to_cpup(p++);
  120. if (bex.es != PNFS_BLOCK_READWRITE_DATA) {
  121. dprintk("%s: incorrect extent state %d\n",
  122. __func__, bex.es);
  123. goto fail;
  124. }
  125. iomaps[i].offset = bex.foff;
  126. iomaps[i].length = bex.len;
  127. }
  128. *iomapp = iomaps;
  129. return nr_iomaps;
  130. fail:
  131. kfree(iomaps);
  132. return -EINVAL;
  133. }