aboutsummaryrefslogtreecommitdiff
path: root/tools/virtiofsd/buffer.c
blob: 27c1377f22324c5290e9e27c27078b3867151a74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * FUSE: Filesystem in Userspace
 * Copyright (C) 2010  Miklos Szeredi <miklos@szeredi.hu>
 *
 * Functions for dealing with `struct fuse_buf` and `struct
 * fuse_bufvec`.
 *
 * This program can be distributed under the terms of the GNU LGPLv2.
 * See the file COPYING.LIB
 */

#include "qemu/osdep.h"
#include "fuse_i.h"
#include "fuse_lowlevel.h"
#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

size_t fuse_buf_size(const struct fuse_bufvec *bufv)
{
    size_t i;
    size_t size = 0;

    for (i = 0; i < bufv->count; i++) {
        if (bufv->buf[i].size == SIZE_MAX) {
            size = SIZE_MAX;
        } else {
            size += bufv->buf[i].size;
        }
    }

    return size;
}

static ssize_t fuse_buf_writev(struct fuse_buf *out_buf,
                               struct fuse_bufvec *in_buf)
{
    ssize_t res, i, j;
    size_t iovcnt = in_buf->count;
    struct iovec *iov;
    int fd = out_buf->fd;

    iov = calloc(iovcnt, sizeof(struct iovec));
    if (!iov) {
        return -ENOMEM;
    }

    for (i = 0, j = 0; i < iovcnt; i++) {
        /* Skip the buf with 0 size */
        if (in_buf->buf[i].size) {
            iov[j].iov_base = in_buf->buf[i].mem;
            iov[j].iov_len = in_buf->buf[i].size;
            j++;
        }
    }

    if (out_buf->flags & FUSE_BUF_FD_SEEK) {
        res = pwritev(fd, iov, iovcnt, out_buf->pos);
    } else {
        res = writev(fd, iov, iovcnt);
    }

    if (res == -1) {
        res = -errno;
    }

    free(iov);
    return res;
}

static size_t min_size(size_t s1, size_t s2)
{
    return s1 < s2 ? s1 : s2;
}

static ssize_t fuse_buf_write(const struct fuse_buf *dst, size_t dst_off,
                              const struct fuse_buf *src, size_t src_off,
                              size_t len)
{
    ssize_t res = 0;
    size_t copied = 0;

    while (len) {
        if (dst->flags & FUSE_BUF_FD_SEEK) {
            res = pwrite(dst->fd, (char *)src->mem + src_off, len,
                         dst->pos + dst_off);
        } else {
            res = write(dst->fd, (char *)src->mem + src_off, len);
        }
        if (res == -1) {
            if (!copied) {
                return -errno;
            }
            break;
        }
        if (res == 0) {
            break;
        }

        copied += res;
        if (!(dst->flags & FUSE_BUF_FD_RETRY)) {
            break;
        }

        src_off += res;
        dst_off += res;
        len -= res;
    }

    return copied;
}

static ssize_t fuse_buf_read(const struct fuse_buf *dst, size_t dst_off,
                             const struct fuse_buf *src, size_t src_off,
                             size_t len)
{
    ssize_t res = 0;
    size_t copied = 0;

    while (len) {
        if (src->flags & FUSE_BUF_FD_SEEK) {
            res = pread(src->fd, (char *)dst->mem + dst_off, len,
                        src->pos + src_off);
        } else {
            res = read(src->fd, (char *)dst->mem + dst_off, len);
        }
        if (res == -1) {
            if (!copied) {
                return -errno;
            }
            break;
        }
        if (res == 0) {
            break;
        }

        copied += res;
        if (!(src->flags & FUSE_BUF_FD_RETRY)) {
            break;
        }

        dst_off += res;
        src_off += res;
        len -= res;
    }

    return copied;
}

static ssize_t fuse_buf_fd_to_fd(const struct fuse_buf *dst, size_t dst_off,
                                 const struct fuse_buf *src, size_t src_off,
                                 size_t len)
{
    char buf[4096];
    struct fuse_buf tmp = {
        .size = sizeof(buf),
        .flags = 0,
    };
    ssize_t res;
    size_t copied = 0;

    tmp.mem = buf;

    while (len) {
        size_t this_len = min_size(tmp.size, len);
        size_t read_len;

        res = fuse_buf_read(&tmp, 0, src, src_off, this_len);
        if (res < 0) {
            if (!copied) {
                return res;
            }
            break;
        }
        if (res == 0) {
            break;
        }

        read_len = res;
        res = fuse_buf_write(dst, dst_off, &tmp, 0, read_len);
        if (res < 0) {
            if (!copied) {
                return res;
            }
            break;
        }
        if (res == 0) {
            break;
        }

        copied += res;

        if (res < this_len) {
            break;
        }

        dst_off += res;
        src_off += res;
        len -= res;
    }

    return copied;
}

static ssize_t fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_off,
                                 const struct fuse_buf *src, size_t src_off,
                                 size_t len)
{
    int src_is_fd = src->flags & FUSE_BUF_IS_FD;
    int dst_is_fd = dst->flags & FUSE_BUF_IS_FD;

    if (!src_is_fd && !dst_is_fd) {
        char *dstmem = (char *)dst->mem + dst_off;
        char *srcmem = (char *)src->mem + src_off;

        if (dstmem != srcmem) {
            if (dstmem + len <= srcmem || srcmem + len <= dstmem) {
                memcpy(dstmem, srcmem, len);
            } else {
                memmove(dstmem, srcmem, len);
            }
        }

        return len;
    } else if (!src_is_fd) {
        return fuse_buf_write(dst, dst_off, src, src_off, len);
    } else if (!dst_is_fd) {
        return fuse_buf_read(dst, dst_off, src, src_off, len);
    } else {
        return fuse_buf_fd_to_fd(dst, dst_off, src, src_off, len);
    }
}

static const struct fuse_buf *fuse_bufvec_current(struct fuse_bufvec *bufv)
{
    if (bufv->idx < bufv->count) {
        return &bufv->buf[bufv->idx];
    } else {
        return NULL;
    }
}

static int fuse_bufvec_advance(struct fuse_bufvec *bufv, size_t len)
{
    const struct fuse_buf *buf = fuse_bufvec_current(bufv);

    bufv->off += len;
    assert(bufv->off <= buf->size);
    if (bufv->off == buf->size) {
        assert(bufv->idx < bufv->count);
        bufv->idx++;
        if (bufv->idx == bufv->count) {
            return 0;
        }
        bufv->off = 0;
    }
    return 1;
}

ssize_t fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv)
{
    size_t copied = 0, i;

    if (dstv == srcv) {
        return fuse_buf_size(dstv);
    }

    /*
     * use writev to improve bandwidth when all the
     * src buffers already mapped by the daemon
     * process
     */
    for (i = 0; i < srcv->count; i++) {
        if (srcv->buf[i].flags & FUSE_BUF_IS_FD) {
            break;
        }
    }
    if ((i == srcv->count) && (dstv->count == 1) &&
        (dstv->idx == 0) &&
        (dstv->buf[0].flags & FUSE_BUF_IS_FD)) {
        dstv->buf[0].pos += dstv->off;
        return fuse_buf_writev(&dstv->buf[0], srcv);
    }

    for (;;) {
        const struct fuse_buf *src = fuse_bufvec_current(srcv);
        const struct fuse_buf *dst = fuse_bufvec_current(dstv);
        size_t src_len;
        size_t dst_len;
        size_t len;
        ssize_t res;

        if (src == NULL || dst == NULL) {
            break;
        }

        src_len = src->size - srcv->off;
        dst_len = dst->size - dstv->off;
        len = min_size(src_len, dst_len);

        res = fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len);
        if (res < 0) {
            if (!copied) {
                return res;
            }
            break;
        }
        copied += res;

        if (!fuse_bufvec_advance(srcv, res) ||
            !fuse_bufvec_advance(dstv, res)) {
            break;
        }

        if (res < len) {
            break;
        }
    }

    return copied;
}

void *fuse_mbuf_iter_advance(struct fuse_mbuf_iter *iter, size_t len)
{
    void *ptr;

    if (len > iter->size - iter->pos) {
        return NULL;
    }

    ptr = iter->mem + iter->pos;
    iter->pos += len;
    return ptr;
}

const char *fuse_mbuf_iter_advance_str(struct fuse_mbuf_iter *iter)
{
    const char *str = iter->mem + iter->pos;
    size_t remaining = iter->size - iter->pos;
    size_t i;

    for (i = 0; i < remaining; i++) {
        if (str[i] == '\0') {
            iter->pos += i + 1;
            return str;
        }
    }
    return NULL;
}