Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / fs / nfsd / blocklayoutxdr.c
blob6c3b316f932e94d993f34a054f88e0e7355ff8fd
1 /*
2 * Copyright (c) 2014-2016 Christoph Hellwig.
3 */
4 #include <linux/sunrpc/svc.h>
5 #include <linux/exportfs.h>
6 #include <linux/nfs4.h>
8 #include "nfsd.h"
9 #include "blocklayoutxdr.h"
11 #define NFSDDBG_FACILITY NFSDDBG_PNFS
14 __be32
15 nfsd4_block_encode_layoutget(struct xdr_stream *xdr,
16 struct nfsd4_layoutget *lgp)
18 struct pnfs_block_extent *b = lgp->lg_content;
19 int len = sizeof(__be32) + 5 * sizeof(__be64) + sizeof(__be32);
20 __be32 *p;
22 p = xdr_reserve_space(xdr, sizeof(__be32) + len);
23 if (!p)
24 return nfserr_toosmall;
26 *p++ = cpu_to_be32(len);
27 *p++ = cpu_to_be32(1); /* we always return a single extent */
29 p = xdr_encode_opaque_fixed(p, &b->vol_id,
30 sizeof(struct nfsd4_deviceid));
31 p = xdr_encode_hyper(p, b->foff);
32 p = xdr_encode_hyper(p, b->len);
33 p = xdr_encode_hyper(p, b->soff);
34 *p++ = cpu_to_be32(b->es);
35 return 0;
38 static int
39 nfsd4_block_encode_volume(struct xdr_stream *xdr, struct pnfs_block_volume *b)
41 __be32 *p;
42 int len;
44 switch (b->type) {
45 case PNFS_BLOCK_VOLUME_SIMPLE:
46 len = 4 + 4 + 8 + 4 + b->simple.sig_len;
47 p = xdr_reserve_space(xdr, len);
48 if (!p)
49 return -ETOOSMALL;
51 *p++ = cpu_to_be32(b->type);
52 *p++ = cpu_to_be32(1); /* single signature */
53 p = xdr_encode_hyper(p, b->simple.offset);
54 p = xdr_encode_opaque(p, b->simple.sig, b->simple.sig_len);
55 break;
56 case PNFS_BLOCK_VOLUME_SCSI:
57 len = 4 + 4 + 4 + 4 + b->scsi.designator_len + 8;
58 p = xdr_reserve_space(xdr, len);
59 if (!p)
60 return -ETOOSMALL;
62 *p++ = cpu_to_be32(b->type);
63 *p++ = cpu_to_be32(b->scsi.code_set);
64 *p++ = cpu_to_be32(b->scsi.designator_type);
65 p = xdr_encode_opaque(p, b->scsi.designator, b->scsi.designator_len);
66 p = xdr_encode_hyper(p, b->scsi.pr_key);
67 break;
68 default:
69 return -ENOTSUPP;
72 return len;
75 __be32
76 nfsd4_block_encode_getdeviceinfo(struct xdr_stream *xdr,
77 struct nfsd4_getdeviceinfo *gdp)
79 struct pnfs_block_deviceaddr *dev = gdp->gd_device;
80 int len = sizeof(__be32), ret, i;
81 __be32 *p;
83 p = xdr_reserve_space(xdr, len + sizeof(__be32));
84 if (!p)
85 return nfserr_resource;
87 for (i = 0; i < dev->nr_volumes; i++) {
88 ret = nfsd4_block_encode_volume(xdr, &dev->volumes[i]);
89 if (ret < 0)
90 return nfserrno(ret);
91 len += ret;
95 * Fill in the overall length and number of volumes at the beginning
96 * of the layout.
98 *p++ = cpu_to_be32(len);
99 *p++ = cpu_to_be32(dev->nr_volumes);
100 return 0;
104 nfsd4_block_decode_layoutupdate(__be32 *p, u32 len, struct iomap **iomapp,
105 u32 block_size)
107 struct iomap *iomaps;
108 u32 nr_iomaps, i;
110 if (len < sizeof(u32)) {
111 dprintk("%s: extent array too small: %u\n", __func__, len);
112 return -EINVAL;
114 len -= sizeof(u32);
115 if (len % PNFS_BLOCK_EXTENT_SIZE) {
116 dprintk("%s: extent array invalid: %u\n", __func__, len);
117 return -EINVAL;
120 nr_iomaps = be32_to_cpup(p++);
121 if (nr_iomaps != len / PNFS_BLOCK_EXTENT_SIZE) {
122 dprintk("%s: extent array size mismatch: %u/%u\n",
123 __func__, len, nr_iomaps);
124 return -EINVAL;
127 iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
128 if (!iomaps) {
129 dprintk("%s: failed to allocate extent array\n", __func__);
130 return -ENOMEM;
133 for (i = 0; i < nr_iomaps; i++) {
134 struct pnfs_block_extent bex;
136 memcpy(&bex.vol_id, p, sizeof(struct nfsd4_deviceid));
137 p += XDR_QUADLEN(sizeof(struct nfsd4_deviceid));
139 p = xdr_decode_hyper(p, &bex.foff);
140 if (bex.foff & (block_size - 1)) {
141 dprintk("%s: unaligned offset 0x%llx\n",
142 __func__, bex.foff);
143 goto fail;
145 p = xdr_decode_hyper(p, &bex.len);
146 if (bex.len & (block_size - 1)) {
147 dprintk("%s: unaligned length 0x%llx\n",
148 __func__, bex.foff);
149 goto fail;
151 p = xdr_decode_hyper(p, &bex.soff);
152 if (bex.soff & (block_size - 1)) {
153 dprintk("%s: unaligned disk offset 0x%llx\n",
154 __func__, bex.soff);
155 goto fail;
157 bex.es = be32_to_cpup(p++);
158 if (bex.es != PNFS_BLOCK_READWRITE_DATA) {
159 dprintk("%s: incorrect extent state %d\n",
160 __func__, bex.es);
161 goto fail;
164 iomaps[i].offset = bex.foff;
165 iomaps[i].length = bex.len;
168 *iomapp = iomaps;
169 return nr_iomaps;
170 fail:
171 kfree(iomaps);
172 return -EINVAL;
176 nfsd4_scsi_decode_layoutupdate(__be32 *p, u32 len, struct iomap **iomapp,
177 u32 block_size)
179 struct iomap *iomaps;
180 u32 nr_iomaps, expected, i;
182 if (len < sizeof(u32)) {
183 dprintk("%s: extent array too small: %u\n", __func__, len);
184 return -EINVAL;
187 nr_iomaps = be32_to_cpup(p++);
188 expected = sizeof(__be32) + nr_iomaps * PNFS_SCSI_RANGE_SIZE;
189 if (len != expected) {
190 dprintk("%s: extent array size mismatch: %u/%u\n",
191 __func__, len, expected);
192 return -EINVAL;
195 iomaps = kcalloc(nr_iomaps, sizeof(*iomaps), GFP_KERNEL);
196 if (!iomaps) {
197 dprintk("%s: failed to allocate extent array\n", __func__);
198 return -ENOMEM;
201 for (i = 0; i < nr_iomaps; i++) {
202 u64 val;
204 p = xdr_decode_hyper(p, &val);
205 if (val & (block_size - 1)) {
206 dprintk("%s: unaligned offset 0x%llx\n", __func__, val);
207 goto fail;
209 iomaps[i].offset = val;
211 p = xdr_decode_hyper(p, &val);
212 if (val & (block_size - 1)) {
213 dprintk("%s: unaligned length 0x%llx\n", __func__, val);
214 goto fail;
216 iomaps[i].length = val;
219 *iomapp = iomaps;
220 return nr_iomaps;
221 fail:
222 kfree(iomaps);
223 return -EINVAL;