linux-user: Add support for a group of btrfs ioctls used for snapshots
[qemu.git] / tools / virtiofsd / buffer.c
1 /*
2 * FUSE: Filesystem in Userspace
3 * Copyright (C) 2010 Miklos Szeredi <miklos@szeredi.hu>
4 *
5 * Functions for dealing with `struct fuse_buf` and `struct
6 * fuse_bufvec`.
7 *
8 * This program can be distributed under the terms of the GNU LGPLv2.
9 * See the file COPYING.LIB
10 */
11
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>
20
21 size_t fuse_buf_size(const struct fuse_bufvec *bufv)
22 {
23 size_t i;
24 size_t size = 0;
25
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;
31 }
32 }
33
34 return size;
35 }
36
37 static ssize_t fuse_buf_writev(struct fuse_buf *out_buf,
38 struct fuse_bufvec *in_buf)
39 {
40 ssize_t res, i, j;
41 size_t iovcnt = in_buf->count;
42 struct iovec *iov;
43 int fd = out_buf->fd;
44
45 iov = calloc(iovcnt, sizeof(struct iovec));
46 if (!iov) {
47 return -ENOMEM;
48 }
49
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++;
56 }
57 }
58
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);
63 }
64
65 if (res == -1) {
66 res = -errno;
67 }
68
69 free(iov);
70 return res;
71 }
72
73 static size_t min_size(size_t s1, size_t s2)
74 {
75 return s1 < s2 ? s1 : s2;
76 }
77
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)
81 {
82 ssize_t res = 0;
83 size_t copied = 0;
84
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);
91 }
92 if (res == -1) {
93 if (!copied) {
94 return -errno;
95 }
96 break;
97 }
98 if (res == 0) {
99 break;
100 }
101
102 copied += res;
103 if (!(dst->flags & FUSE_BUF_FD_RETRY)) {
104 break;
105 }
106
107 src_off += res;
108 dst_off += res;
109 len -= res;
110 }
111
112 return copied;
113 }
114
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)
118 {
119 ssize_t res = 0;
120 size_t copied = 0;
121
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);
128 }
129 if (res == -1) {
130 if (!copied) {
131 return -errno;
132 }
133 break;
134 }
135 if (res == 0) {
136 break;
137 }
138
139 copied += res;
140 if (!(src->flags & FUSE_BUF_FD_RETRY)) {
141 break;
142 }
143
144 dst_off += res;
145 src_off += res;
146 len -= res;
147 }
148
149 return copied;
150 }
151
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)
155 {
156 char buf[4096];
157 struct fuse_buf tmp = {
158 .size = sizeof(buf),
159 .flags = 0,
160 };
161 ssize_t res;
162 size_t copied = 0;
163
164 tmp.mem = buf;
165
166 while (len) {
167 size_t this_len = min_size(tmp.size, len);
168 size_t read_len;
169
170 res = fuse_buf_read(&tmp, 0, src, src_off, this_len);
171 if (res < 0) {
172 if (!copied) {
173 return res;
174 }
175 break;
176 }
177 if (res == 0) {
178 break;
179 }
180
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;
186 }
187 break;
188 }
189 if (res == 0) {
190 break;
191 }
192
193 copied += res;
194
195 if (res < this_len) {
196 break;
197 }
198
199 dst_off += res;
200 src_off += res;
201 len -= res;
202 }
203
204 return copied;
205 }
206
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)
210 {
211 int src_is_fd = src->flags & FUSE_BUF_IS_FD;
212 int dst_is_fd = dst->flags & FUSE_BUF_IS_FD;
213
214 if (!src_is_fd && !dst_is_fd) {
215 char *dstmem = (char *)dst->mem + dst_off;
216 char *srcmem = (char *)src->mem + src_off;
217
218 if (dstmem != srcmem) {
219 if (dstmem + len <= srcmem || srcmem + len <= dstmem) {
220 memcpy(dstmem, srcmem, len);
221 } else {
222 memmove(dstmem, srcmem, len);
223 }
224 }
225
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);
233 }
234 }
235
236 static const struct fuse_buf *fuse_bufvec_current(struct fuse_bufvec *bufv)
237 {
238 if (bufv->idx < bufv->count) {
239 return &bufv->buf[bufv->idx];
240 } else {
241 return NULL;
242 }
243 }
244
245 static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
246 {
247 const struct fuse_buf *buf = fuse_bufvec_current(bufv);
248
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;
256 }
257 bufv->off = 0;
258 }
259 return 1;
260 }
261
262 ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv)
263 {
264 size_t copied = 0, i;
265
266 if (dstv == srcv) {
267 return fuse_buf_size(dstv);
268 }
269
270 /*
271 * use writev to improve bandwidth when all the
272 * src buffers already mapped by the daemon
273 * process
274 */
275 for (i = 0; i < srcv->count; i++) {
276 if (srcv->buf[i].flags & FUSE_BUF_IS_FD) {
277 break;
278 }
279 }
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);
285 }
286
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;
294
295 if (src == NULL || dst == NULL) {
296 break;
297 }
298
299 src_len = src->size - srcv->off;
300 dst_len = dst->size - dstv->off;
301 len = min_size(src_len, dst_len);
302
303 res = fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len);
304 if (res < 0) {
305 if (!copied) {
306 return res;
307 }
308 break;
309 }
310 copied += res;
311
312 if (!fuse_bufvec_advance(srcv, res) ||
313 !fuse_bufvec_advance(dstv, res)) {
314 break;
315 }
316
317 if (res < len) {
318 break;
319 }
320 }
321
322 return copied;
323 }
324
325 void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len)
326 {
327 void *ptr;
328
329 if (len > iter->size - iter->pos) {
330 return NULL;
331 }
332
333 ptr = iter->mem + iter->pos;
334 iter->pos += len;
335 return ptr;
336 }
337
338 const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter)
339 {
340 const char *str = iter->mem + iter->pos;
341 size_t remaining = iter->size - iter->pos;
342 size_t i;
343
344 for (i = 0; i < remaining; i++) {
345 if (str[i] == '\0') {
346 iter->pos += i + 1;
347 return str;
348 }
349 }
350 return NULL;
351 }