buf.c revision 1.1 1 1.1 pho /* $NetBSD: buf.c,v 1.1 2022/01/22 07:54:57 pho Exp $ */
2 1.1 pho
3 1.1 pho /*
4 1.1 pho * Copyright (c) 2021 The NetBSD Foundation, Inc.
5 1.1 pho * All rights reserved.
6 1.1 pho *
7 1.1 pho * Redistribution and use in source and binary forms, with or without
8 1.1 pho * modification, are permitted provided that the following conditions
9 1.1 pho * are met:
10 1.1 pho * 1. Redistributions of source code must retain the above copyright
11 1.1 pho * notice, this list of conditions and the following disclaimer.
12 1.1 pho * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 pho * notice, this list of conditions and the following disclaimer in the
14 1.1 pho * documentation and/or other materials provided with the distribution.
15 1.1 pho * 3. The name of the author may not be used to endorse or promote
16 1.1 pho * products derived from this software without specific prior written
17 1.1 pho * permission.
18 1.1 pho *
19 1.1 pho * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
20 1.1 pho * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 1.1 pho * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 pho * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 1.1 pho * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 pho * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 1.1 pho * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 pho * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 1.1 pho * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 1.1 pho * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 1.1 pho * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 1.1 pho */
31 1.1 pho
32 1.1 pho #include <sys/cdefs.h>
33 1.1 pho #if !defined(lint)
34 1.1 pho __RCSID("$NetBSD: buf.c,v 1.1 2022/01/22 07:54:57 pho Exp $");
35 1.1 pho #endif /* !lint */
36 1.1 pho
37 1.1 pho #include <assert.h>
38 1.1 pho #include <errno.h>
39 1.1 pho #include <fuse_internal.h>
40 1.1 pho #include <machine/vmparam.h> /* for PAGE_SIZE */
41 1.1 pho #include <stdbool.h>
42 1.1 pho #include <stdint.h>
43 1.1 pho #include <stdlib.h>
44 1.1 pho #include <string.h>
45 1.1 pho #include <sys/param.h> /* for MIN(a, b) */
46 1.1 pho #include <unistd.h>
47 1.1 pho
48 1.1 pho size_t
49 1.1 pho fuse_buf_size(const struct fuse_bufvec *bufv) {
50 1.1 pho size_t i;
51 1.1 pho size_t total = 0;
52 1.1 pho
53 1.1 pho for (i = 0; i < bufv->count; i++) {
54 1.1 pho total += bufv->buf[i].size;
55 1.1 pho }
56 1.1 pho
57 1.1 pho return total;
58 1.1 pho }
59 1.1 pho
60 1.1 pho /* Return the pointer to the current buffer in a buffer vector, or
61 1.1 pho * NULL if we have reached the end of the vector. */
62 1.1 pho static const struct fuse_buf*
63 1.1 pho fuse_buf_current(const struct fuse_bufvec *bufv) {
64 1.1 pho if (bufv->idx < bufv->count)
65 1.1 pho return &bufv->buf[bufv->idx];
66 1.1 pho else
67 1.1 pho return NULL;
68 1.1 pho }
69 1.1 pho
70 1.1 pho /* Copy data from one fd to a memory buffer, and return the number of
71 1.1 pho * octets that have been copied, or -1 on failure. */
72 1.1 pho static ssize_t
73 1.1 pho fuse_buf_read_fd_to_mem(const struct fuse_buf *dst, size_t dst_off,
74 1.1 pho const struct fuse_buf *src, size_t src_off,
75 1.1 pho size_t len) {
76 1.1 pho ssize_t total = 0;
77 1.1 pho
78 1.1 pho while (len > 0) {
79 1.1 pho ssize_t n_read;
80 1.1 pho
81 1.1 pho if (src->flags & FUSE_BUF_FD_SEEK)
82 1.1 pho n_read = pread(src->fd, (uint8_t*)dst->mem + dst_off, len,
83 1.1 pho src->pos + (off_t)src_off);
84 1.1 pho else
85 1.1 pho n_read = read(src->fd, (uint8_t*)dst->mem + dst_off, len);
86 1.1 pho
87 1.1 pho if (n_read == -1) {
88 1.1 pho if (errno == EINTR)
89 1.1 pho continue;
90 1.1 pho else if (total == 0)
91 1.1 pho return -1;
92 1.1 pho else
93 1.1 pho /* The last pread(2) or read(2) failed but we have
94 1.1 pho * already copied some data. */
95 1.1 pho break;
96 1.1 pho }
97 1.1 pho else if (n_read == 0) {
98 1.1 pho /* Reached EOF */
99 1.1 pho break;
100 1.1 pho }
101 1.1 pho else {
102 1.1 pho total += n_read;
103 1.1 pho dst_off += (size_t)n_read;
104 1.1 pho src_off += (size_t)n_read;
105 1.1 pho len -= (size_t)n_read;
106 1.1 pho
107 1.1 pho if (src->flags & FUSE_BUF_FD_RETRY)
108 1.1 pho continue;
109 1.1 pho }
110 1.1 pho }
111 1.1 pho
112 1.1 pho return total;
113 1.1 pho }
114 1.1 pho
115 1.1 pho /* Copy data from one memory buffer to an fd, and return the number of
116 1.1 pho * octets that have been copied, or -1 on failure. */
117 1.1 pho static ssize_t
118 1.1 pho fuse_buf_write_mem_to_fd(const struct fuse_buf *dst, size_t dst_off,
119 1.1 pho const struct fuse_buf *src, size_t src_off,
120 1.1 pho size_t len) {
121 1.1 pho ssize_t total = 0;
122 1.1 pho
123 1.1 pho while (len > 0) {
124 1.1 pho ssize_t n_wrote;
125 1.1 pho
126 1.1 pho if (dst->flags & FUSE_BUF_FD_SEEK)
127 1.1 pho n_wrote = pwrite(dst->fd, (uint8_t*)src->mem + src_off, len,
128 1.1 pho dst->pos + (off_t)dst_off);
129 1.1 pho else
130 1.1 pho n_wrote = write(dst->fd, (uint8_t*)src->mem + src_off, len);
131 1.1 pho
132 1.1 pho if (n_wrote == -1) {
133 1.1 pho if (errno == EINTR)
134 1.1 pho continue;
135 1.1 pho else if (total == 0)
136 1.1 pho return -1;
137 1.1 pho else
138 1.1 pho /* The last pwrite(2) or write(2) failed but we have
139 1.1 pho * already copied some data. */
140 1.1 pho break;
141 1.1 pho }
142 1.1 pho else if (n_wrote == 0) {
143 1.1 pho break;
144 1.1 pho }
145 1.1 pho else {
146 1.1 pho total += n_wrote;
147 1.1 pho dst_off += (size_t)n_wrote;
148 1.1 pho src_off += (size_t)n_wrote;
149 1.1 pho len -= (size_t)n_wrote;
150 1.1 pho
151 1.1 pho if (dst->flags & FUSE_BUF_FD_RETRY)
152 1.1 pho continue;
153 1.1 pho }
154 1.1 pho }
155 1.1 pho
156 1.1 pho return total;
157 1.1 pho }
158 1.1 pho
159 1.1 pho /* Copy data from one fd to another, and return the number of octets
160 1.1 pho * that have been copied, or -1 on failure. */
161 1.1 pho static ssize_t
162 1.1 pho fuse_buf_copy_fd_to_fd(const struct fuse_buf *dst, size_t dst_off,
163 1.1 pho const struct fuse_buf *src, size_t src_off,
164 1.1 pho size_t len) {
165 1.1 pho ssize_t total = 0;
166 1.1 pho struct fuse_buf tmp;
167 1.1 pho
168 1.1 pho tmp.size = PAGE_SIZE;
169 1.1 pho tmp.flags = (enum fuse_buf_flags)0;
170 1.1 pho tmp.mem = malloc(tmp.size);
171 1.1 pho
172 1.1 pho if (tmp.mem == NULL) {
173 1.1 pho return -1;
174 1.1 pho }
175 1.1 pho
176 1.1 pho while (len) {
177 1.1 pho size_t n_to_read = MIN(tmp.size, len);
178 1.1 pho ssize_t n_read;
179 1.1 pho ssize_t n_wrote;
180 1.1 pho
181 1.1 pho n_read = fuse_buf_read_fd_to_mem(&tmp, 0, src, src_off, n_to_read);
182 1.1 pho if (n_read == -1) {
183 1.1 pho if (total == 0) {
184 1.1 pho free(tmp.mem);
185 1.1 pho return -1;
186 1.1 pho }
187 1.1 pho else {
188 1.1 pho /* We have already copied some data. */
189 1.1 pho break;
190 1.1 pho }
191 1.1 pho }
192 1.1 pho else if (n_read == 0) {
193 1.1 pho /* Reached EOF */
194 1.1 pho break;
195 1.1 pho }
196 1.1 pho
197 1.1 pho n_wrote = fuse_buf_write_mem_to_fd(dst, dst_off, &tmp, 0, (size_t)n_read);
198 1.1 pho if (n_wrote == -1) {
199 1.1 pho if (total == 0) {
200 1.1 pho free(tmp.mem);
201 1.1 pho return -1;
202 1.1 pho }
203 1.1 pho else {
204 1.1 pho /* We have already copied some data. */
205 1.1 pho break;
206 1.1 pho }
207 1.1 pho }
208 1.1 pho else if (n_wrote == 0) {
209 1.1 pho break;
210 1.1 pho }
211 1.1 pho
212 1.1 pho total += n_wrote;
213 1.1 pho dst_off += (size_t)n_wrote;
214 1.1 pho src_off += (size_t)n_wrote;
215 1.1 pho len -= (size_t)n_wrote;
216 1.1 pho
217 1.1 pho if (n_wrote < n_read)
218 1.1 pho /* Now we have some data that were read but couldn't be
219 1.1 pho * written, and can't do anything about it. */
220 1.1 pho break;
221 1.1 pho }
222 1.1 pho
223 1.1 pho free(tmp.mem);
224 1.1 pho return total;
225 1.1 pho }
226 1.1 pho
227 1.1 pho /* Copy data from one buffer to another, and return the number of
228 1.1 pho * octets that have been copied. */
229 1.1 pho static ssize_t
230 1.1 pho fuse_buf_copy_one(const struct fuse_buf *dst, size_t dst_off,
231 1.1 pho const struct fuse_buf *src, size_t src_off,
232 1.1 pho size_t len,
233 1.1 pho enum fuse_buf_copy_flags flags __attribute__((__unused__))) {
234 1.1 pho
235 1.1 pho const bool dst_is_fd = !!(dst->flags & FUSE_BUF_IS_FD);
236 1.1 pho const bool src_is_fd = !!(src->flags & FUSE_BUF_IS_FD);
237 1.1 pho
238 1.1 pho if (!dst_is_fd && !src_is_fd) {
239 1.1 pho void* dst_mem = (uint8_t*)dst->mem + dst_off;
240 1.1 pho void* src_mem = (uint8_t*)src->mem + src_off;
241 1.1 pho
242 1.1 pho memmove(dst_mem, src_mem, len);
243 1.1 pho
244 1.1 pho return (ssize_t)len;
245 1.1 pho }
246 1.1 pho else if (!dst_is_fd) {
247 1.1 pho return fuse_buf_read_fd_to_mem(dst, dst_off, src, src_off, len);
248 1.1 pho }
249 1.1 pho else if (!src_is_fd) {
250 1.1 pho return fuse_buf_write_mem_to_fd(dst, dst_off, src, src_off, len);
251 1.1 pho }
252 1.1 pho else {
253 1.1 pho return fuse_buf_copy_fd_to_fd(dst, dst_off, src, src_off, len);
254 1.1 pho }
255 1.1 pho }
256 1.1 pho
257 1.1 pho /* Advance the buffer by a given number of octets. Return 0 on
258 1.1 pho * success, or -1 otherwise. Reaching the end of the buffer vector
259 1.1 pho * counts as a failure. */
260 1.1 pho static int
261 1.1 pho fuse_buf_advance(struct fuse_bufvec *bufv, size_t len) {
262 1.1 pho const struct fuse_buf *buf = fuse_buf_current(bufv);
263 1.1 pho
264 1.1 pho assert(bufv->off + len <= buf->size);
265 1.1 pho bufv->off += len;
266 1.1 pho if (bufv->off == buf->size) {
267 1.1 pho /* Done with the current buffer. Advance to the next one. */
268 1.1 pho assert(bufv->idx < bufv->count);
269 1.1 pho bufv->idx++;
270 1.1 pho if (bufv->idx == bufv->count)
271 1.1 pho return -1; /* No more buffers in the vector. */
272 1.1 pho bufv->off = 0;
273 1.1 pho }
274 1.1 pho return 0;
275 1.1 pho }
276 1.1 pho
277 1.1 pho ssize_t
278 1.1 pho fuse_buf_copy(struct fuse_bufvec *dstv, struct fuse_bufvec *srcv,
279 1.1 pho enum fuse_buf_copy_flags flags) {
280 1.1 pho ssize_t total = 0;
281 1.1 pho
282 1.1 pho while (true) {
283 1.1 pho const struct fuse_buf* dst;
284 1.1 pho const struct fuse_buf* src;
285 1.1 pho size_t src_len;
286 1.1 pho size_t dst_len;
287 1.1 pho size_t len;
288 1.1 pho ssize_t n_copied;
289 1.1 pho
290 1.1 pho dst = fuse_buf_current(dstv);
291 1.1 pho src = fuse_buf_current(srcv);
292 1.1 pho if (src == NULL || dst == NULL)
293 1.1 pho break;
294 1.1 pho
295 1.1 pho src_len = src->size - srcv->off;
296 1.1 pho dst_len = dst->size - dstv->off;
297 1.1 pho len = MIN(src_len, dst_len);
298 1.1 pho
299 1.1 pho n_copied = fuse_buf_copy_one(dst, dstv->off, src, srcv->off, len, flags);
300 1.1 pho if (n_copied == -1) {
301 1.1 pho if (total == 0)
302 1.1 pho return -1;
303 1.1 pho else
304 1.1 pho /* Failed to copy the current buffer but we have
305 1.1 pho * already copied some part of the vector. It is
306 1.1 pho * therefore inappropriate to return an error. */
307 1.1 pho break;
308 1.1 pho }
309 1.1 pho total += n_copied;
310 1.1 pho
311 1.1 pho if (fuse_buf_advance(srcv, (size_t)n_copied) != 0 ||
312 1.1 pho fuse_buf_advance(dstv, (size_t)n_copied) != 0)
313 1.1 pho break;
314 1.1 pho
315 1.1 pho if ((size_t)n_copied < len)
316 1.1 pho break;
317 1.1 pho }
318 1.1 pho
319 1.1 pho return total;
320 1.1 pho }
321