linux-user: Validate mmap/mprotect prot value
[qemu/ar7.git] / tools / virtiofsd / buffer.c
blob27c1377f22324c5290e9e27c27078b3867151a74
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2010 Miklos Szeredi <miklos@szeredi.hu>
5 * Functions for dealing with `struct fuse_buf` and `struct
6 * fuse_bufvec`.
8 * This program can be distributed under the terms of the GNU LGPLv2.
9 * See the file COPYING.LIB
12 #include "qemu/osdep.h"
13 #include "fuse_i.h"
14 #include "fuse_lowlevel.h"
15 #include <assert.h>
16 #include <errno.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 size_t fuse_buf_size(const struct fuse_bufvec *bufv)
23 size_t i;
24 size_t size = 0;
26 for (i = 0; i < bufv->count; i++) {
27 if (bufv->buf[i].size == SIZE_MAX) {
28 size = SIZE_MAX;
29 } else {
30 size += bufv->buf[i].size;
34 return size;
37 static ssize_t fuse_buf_writev(struct fuse_buf *out_buf,
38 struct fuse_bufvec *in_buf)
40 ssize_t res, i, j;
41 size_t iovcnt = in_buf->count;
42 struct iovec *iov;
43 int fd = out_buf->fd;
45 iov = calloc(iovcnt, sizeof(struct iovec));
46 if (!iov) {
47 return -ENOMEM;
50 for (i = 0, j = 0; i < iovcnt; i++) {
51 /* Skip the buf with 0 size */
52 if (in_buf->buf[i].size) {
53 iov[j].iov_base = in_buf->buf[i].mem;
54 iov[j].iov_len = in_buf->buf[i].size;
55 j++;
59 if (out_buf->flags & FUSE_BUF_FD_SEEK) {
60 res = pwritev(fd, iov, iovcnt, out_buf->pos);
61 } else {
62 res = writev(fd, iov, iovcnt);
65 if (res == -1) {
66 res = -errno;
69 free(iov);
70 return res;
73 static size_t min_size(size_t s1, size_t s2)
75 return s1 < s2 ? s1 : s2;
78 static ssize_t fuse_buf_write(const struct fuse_buf *dst, size_t dst_off,
79 const struct fuse_buf *src, size_t src_off,
80 size_t len)
82 ssize_t res = 0;
83 size_t copied = 0;
85 while (len) {
86 if (dst->flags & FUSE_BUF_FD_SEEK) {
87 res = pwrite(dst->fd, (char *)src->mem + src_off, len,
88 dst->pos + dst_off);
89 } else {
90 res = write(dst->fd, (char *)src->mem + src_off, len);
92 if (res == -1) {
93 if (!copied) {
94 return -errno;
96 break;
98 if (res == 0) {
99 break;
102 copied += res;
103 if (!(dst->flags & FUSE_BUF_FD_RETRY)) {
104 break;
107 src_off += res;
108 dst_off += res;
109 len -= res;
112 return copied;
115 static ssize_t fuse_buf_read(const struct fuse_buf *dst, size_t dst_off,
116 const struct fuse_buf *src, size_t src_off,
117 size_t len)
119 ssize_t res = 0;
120 size_t copied = 0;
122 while (len) {
123 if (src->flags & FUSE_BUF_FD_SEEK) {
124 res = pread(src->fd, (char *)dst->mem + dst_off, len,
125 src->pos + src_off);
126 } else {
127 res = read(src->fd, (char *)dst->mem + dst_off, len);
129 if (res == -1) {
130 if (!copied) {
131 return -errno;
133 break;
135 if (res == 0) {
136 break;
139 copied += res;
140 if (!(src->flags & FUSE_BUF_FD_RETRY)) {
141 break;
144 dst_off += res;
145 src_off += res;
146 len -= res;
149 return copied;
152 static ssize_t fuse_buf_fd_to_fd(const struct fuse_buf *dst, size_t dst_off,
153 const struct fuse_buf *src, size_t src_off,
154 size_t len)
156 char buf[4096];
157 struct fuse_buf tmp = {
158 .size = sizeof(buf),
159 .flags = 0,
161 ssize_t res;
162 size_t copied = 0;
164 tmp.mem = buf;
166 while (len) {
167 size_t this_len = min_size(tmp.size, len);
168 size_t read_len;
170 res = fuse_buf_read(&tmp, 0, src, src_off, this_len);
171 if (res < 0) {
172 if (!copied) {
173 return res;
175 break;
177 if (res == 0) {
178 break;
181 read_len = res;
182 res = fuse_buf_write(dst, dst_off, &tmp, 0, read_len);
183 if (res < 0) {
184 if (!copied) {
185 return res;
187 break;
189 if (res == 0) {
190 break;
193 copied += res;
195 if (res < this_len) {
196 break;
199 dst_off += res;
200 src_off += res;
201 len -= res;
204 return copied;
207 static ssize_t fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_off,
208 const struct fuse_buf *src, size_t src_off,
209 size_t len)
211 int src_is_fd = src->flags & FUSE_BUF_IS_FD;
212 int dst_is_fd = dst->flags & FUSE_BUF_IS_FD;
214 if (!src_is_fd && !dst_is_fd) {
215 char *dstmem = (char *)dst->mem + dst_off;
216 char *srcmem = (char *)src->mem + src_off;
218 if (dstmem != srcmem) {
219 if (dstmem + len <= srcmem || srcmem + len <= dstmem) {
220 memcpy(dstmem, srcmem, len);
221 } else {
222 memmove(dstmem, srcmem, len);
226 return len;
227 } else if (!src_is_fd) {
228 return fuse_buf_write(dst, dst_off, src, src_off, len);
229 } else if (!dst_is_fd) {
230 return fuse_buf_read(dst, dst_off, src, src_off, len);
231 } else {
232 return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len);
236 static const struct fuse_buf *fuse_bufvec_current(struct fuse_bufvec *bufv)
238 if (bufv->idx < bufv->count) {
239 return &bufv->buf[bufv->idx];
240 } else {
241 return NULL;
245 static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
247 const struct fuse_buf *buf = fuse_bufvec_current(bufv);
249 bufv->off += len;
250 assert(bufv->off <= buf->size);
251 if (bufv->off == buf->size) {
252 assert(bufv->idx < bufv->count);
253 bufv->idx++;
254 if (bufv->idx == bufv->count) {
255 return 0;
257 bufv->off = 0;
259 return 1;
262 ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv)
264 size_t copied = 0, i;
266 if (dstv == srcv) {
267 return fuse_buf_size(dstv);
271 * use writev to improve bandwidth when all the
272 * src buffers already mapped by the daemon
273 * process
275 for (i = 0; i < srcv->count; i++) {
276 if (srcv->buf[i].flags & FUSE_BUF_IS_FD) {
277 break;
280 if ((i == srcv->count) && (dstv->count == 1) &&
281 (dstv->idx == 0) &&
282 (dstv->buf[0].flags & FUSE_BUF_IS_FD)) {
283 dstv->buf[0].pos += dstv->off;
284 return fuse_buf_writev(&dstv->buf[0], srcv);
287 for (;;) {
288 const struct fuse_buf *src = fuse_bufvec_current(srcv);
289 const struct fuse_buf *dst = fuse_bufvec_current(dstv);
290 size_t src_len;
291 size_t dst_len;
292 size_t len;
293 ssize_t res;
295 if (src == NULL || dst == NULL) {
296 break;
299 src_len = src->size - srcv->off;
300 dst_len = dst->size - dstv->off;
301 len = min_size(src_len, dst_len);
303 res = fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len);
304 if (res < 0) {
305 if (!copied) {
306 return res;
308 break;
310 copied += res;
312 if (!fuse_bufvec_advance(srcv, res) ||
313 !fuse_bufvec_advance(dstv, res)) {
314 break;
317 if (res < len) {
318 break;
322 return copied;
325 void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len)
327 void *ptr;
329 if (len > iter->size - iter->pos) {
330 return NULL;
333 ptr = iter->mem + iter->pos;
334 iter->pos += len;
335 return ptr;
338 const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter)
340 const char *str = iter->mem + iter->pos;
341 size_t remaining = iter->size - iter->pos;
342 size_t i;
344 for (i = 0; i < remaining; i++) {
345 if (str[i] == '\0') {
346 iter->pos += i + 1;
347 return str;
350 return NULL;