]>
Commit | Line | Data |
---|---|---|
2de121f0 | 1 | /* |
7387863d DDAG |
2 | * FUSE: Filesystem in Userspace |
3 | * Copyright (C) 2001-2007 Miklos Szeredi <[email protected]> | |
4 | * | |
5 | * Implementation of (most of) the low-level FUSE API. The session loop | |
6 | * functions are implemented in separate files. | |
7 | * | |
8 | * This program can be distributed under the terms of the GNU LGPLv2. | |
9 | * See the file COPYING.LIB | |
10 | */ | |
2de121f0 | 11 | |
09863ebc | 12 | #include "qemu/osdep.h" |
2de121f0 | 13 | #include "fuse_i.h" |
09863ebc | 14 | #include "standard-headers/linux/fuse.h" |
2de121f0 | 15 | #include "fuse_misc.h" |
7387863d | 16 | #include "fuse_opt.h" |
d14bf584 | 17 | #include "fuse_virtio.h" |
2de121f0 | 18 | |
7387863d DDAG |
19 | #include <assert.h> |
20 | #include <errno.h> | |
21 | #include <limits.h> | |
70995754 | 22 | #include <stdbool.h> |
7387863d | 23 | #include <stddef.h> |
2de121f0 DDAG |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
2de121f0 | 26 | #include <string.h> |
2de121f0 | 27 | #include <sys/file.h> |
7387863d | 28 | #include <unistd.h> |
2de121f0 DDAG |
29 | |
30 | ||
2de121f0 DDAG |
31 | #define OFFSET_MAX 0x7fffffffffffffffLL |
32 | ||
2de121f0 | 33 | struct fuse_pollhandle { |
7387863d DDAG |
34 | uint64_t kh; |
35 | struct fuse_session *se; | |
2de121f0 DDAG |
36 | }; |
37 | ||
38 | static size_t pagesize; | |
39 | ||
40 | static __attribute__((constructor)) void fuse_ll_init_pagesize(void) | |
41 | { | |
7387863d | 42 | pagesize = getpagesize(); |
2de121f0 DDAG |
43 | } |
44 | ||
45 | static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr) | |
46 | { | |
3db2876a SH |
47 | *attr = (struct fuse_attr){ |
48 | .ino = stbuf->st_ino, | |
49 | .mode = stbuf->st_mode, | |
50 | .nlink = stbuf->st_nlink, | |
51 | .uid = stbuf->st_uid, | |
52 | .gid = stbuf->st_gid, | |
53 | .rdev = stbuf->st_rdev, | |
54 | .size = stbuf->st_size, | |
55 | .blksize = stbuf->st_blksize, | |
56 | .blocks = stbuf->st_blocks, | |
57 | .atime = stbuf->st_atime, | |
58 | .mtime = stbuf->st_mtime, | |
59 | .ctime = stbuf->st_ctime, | |
60 | .atimensec = ST_ATIM_NSEC(stbuf), | |
61 | .mtimensec = ST_MTIM_NSEC(stbuf), | |
62 | .ctimensec = ST_CTIM_NSEC(stbuf), | |
63 | }; | |
2de121f0 DDAG |
64 | } |
65 | ||
66 | static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf) | |
67 | { | |
7387863d DDAG |
68 | stbuf->st_mode = attr->mode; |
69 | stbuf->st_uid = attr->uid; | |
70 | stbuf->st_gid = attr->gid; | |
71 | stbuf->st_size = attr->size; | |
72 | stbuf->st_atime = attr->atime; | |
73 | stbuf->st_mtime = attr->mtime; | |
74 | stbuf->st_ctime = attr->ctime; | |
75 | ST_ATIM_NSEC_SET(stbuf, attr->atimensec); | |
76 | ST_MTIM_NSEC_SET(stbuf, attr->mtimensec); | |
77 | ST_CTIM_NSEC_SET(stbuf, attr->ctimensec); | |
2de121f0 DDAG |
78 | } |
79 | ||
7387863d | 80 | static size_t iov_length(const struct iovec *iov, size_t count) |
2de121f0 | 81 | { |
7387863d DDAG |
82 | size_t seg; |
83 | size_t ret = 0; | |
2de121f0 | 84 | |
7387863d DDAG |
85 | for (seg = 0; seg < count; seg++) { |
86 | ret += iov[seg].iov_len; | |
87 | } | |
88 | return ret; | |
2de121f0 DDAG |
89 | } |
90 | ||
91 | static void list_init_req(struct fuse_req *req) | |
92 | { | |
7387863d DDAG |
93 | req->next = req; |
94 | req->prev = req; | |
2de121f0 DDAG |
95 | } |
96 | ||
97 | static void list_del_req(struct fuse_req *req) | |
98 | { | |
7387863d DDAG |
99 | struct fuse_req *prev = req->prev; |
100 | struct fuse_req *next = req->next; | |
101 | prev->next = next; | |
102 | next->prev = prev; | |
2de121f0 DDAG |
103 | } |
104 | ||
105 | static void list_add_req(struct fuse_req *req, struct fuse_req *next) | |
106 | { | |
7387863d DDAG |
107 | struct fuse_req *prev = next->prev; |
108 | req->next = next; | |
109 | req->prev = prev; | |
110 | prev->next = req; | |
111 | next->prev = req; | |
2de121f0 DDAG |
112 | } |
113 | ||
114 | static void destroy_req(fuse_req_t req) | |
115 | { | |
7387863d DDAG |
116 | pthread_mutex_destroy(&req->lock); |
117 | free(req); | |
2de121f0 DDAG |
118 | } |
119 | ||
120 | void fuse_free_req(fuse_req_t req) | |
121 | { | |
7387863d DDAG |
122 | int ctr; |
123 | struct fuse_session *se = req->se; | |
2de121f0 | 124 | |
7387863d DDAG |
125 | pthread_mutex_lock(&se->lock); |
126 | req->u.ni.func = NULL; | |
127 | req->u.ni.data = NULL; | |
128 | list_del_req(req); | |
129 | ctr = --req->ctr; | |
130 | req->ch = NULL; | |
131 | pthread_mutex_unlock(&se->lock); | |
132 | if (!ctr) { | |
133 | destroy_req(req); | |
134 | } | |
2de121f0 DDAG |
135 | } |
136 | ||
137 | static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se) | |
138 | { | |
7387863d | 139 | struct fuse_req *req; |
2de121f0 | 140 | |
7387863d DDAG |
141 | req = (struct fuse_req *)calloc(1, sizeof(struct fuse_req)); |
142 | if (req == NULL) { | |
143 | fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n"); | |
144 | } else { | |
145 | req->se = se; | |
146 | req->ctr = 1; | |
147 | list_init_req(req); | |
148 | fuse_mutex_init(&req->lock); | |
149 | } | |
2de121f0 | 150 | |
7387863d | 151 | return req; |
2de121f0 DDAG |
152 | } |
153 | ||
154 | /* Send data. If *ch* is NULL, send via session master fd */ | |
155 | static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch, | |
7387863d | 156 | struct iovec *iov, int count) |
2de121f0 | 157 | { |
7387863d | 158 | struct fuse_out_header *out = iov[0].iov_base; |
2de121f0 | 159 | |
7387863d | 160 | out->len = iov_length(iov, count); |
d240314a EG |
161 | if (out->unique == 0) { |
162 | fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n", out->error, | |
163 | out->len); | |
164 | } else if (out->error) { | |
165 | fuse_log(FUSE_LOG_DEBUG, | |
166 | " unique: %llu, error: %i (%s), outsize: %i\n", | |
167 | (unsigned long long)out->unique, out->error, | |
168 | strerror(-out->error), out->len); | |
169 | } else { | |
170 | fuse_log(FUSE_LOG_DEBUG, " unique: %llu, success, outsize: %i\n", | |
171 | (unsigned long long)out->unique, out->len); | |
7387863d | 172 | } |
2de121f0 | 173 | |
df57ba91 DDAG |
174 | if (fuse_lowlevel_is_virtio(se)) { |
175 | return virtio_send_msg(se, ch, iov, count); | |
176 | } | |
177 | ||
7387863d DDAG |
178 | abort(); /* virtio should have taken it before here */ |
179 | return 0; | |
2de121f0 DDAG |
180 | } |
181 | ||
182 | ||
183 | int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov, | |
7387863d | 184 | int count) |
2de121f0 | 185 | { |
3db2876a SH |
186 | struct fuse_out_header out = { |
187 | .unique = req->unique, | |
188 | .error = error, | |
189 | }; | |
2de121f0 | 190 | |
7387863d DDAG |
191 | if (error <= -1000 || error > 0) { |
192 | fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error); | |
193 | error = -ERANGE; | |
194 | } | |
2de121f0 | 195 | |
7387863d DDAG |
196 | iov[0].iov_base = &out; |
197 | iov[0].iov_len = sizeof(struct fuse_out_header); | |
2de121f0 | 198 | |
7387863d | 199 | return fuse_send_msg(req->se, req->ch, iov, count); |
2de121f0 DDAG |
200 | } |
201 | ||
202 | static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov, | |
7387863d | 203 | int count) |
2de121f0 | 204 | { |
7387863d | 205 | int res; |
2de121f0 | 206 | |
7387863d DDAG |
207 | res = fuse_send_reply_iov_nofree(req, error, iov, count); |
208 | fuse_free_req(req); | |
209 | return res; | |
2de121f0 DDAG |
210 | } |
211 | ||
212 | static int send_reply(fuse_req_t req, int error, const void *arg, | |
7387863d | 213 | size_t argsize) |
2de121f0 | 214 | { |
7387863d DDAG |
215 | struct iovec iov[2]; |
216 | int count = 1; | |
217 | if (argsize) { | |
218 | iov[1].iov_base = (void *)arg; | |
219 | iov[1].iov_len = argsize; | |
220 | count++; | |
221 | } | |
222 | return send_reply_iov(req, error, iov, count); | |
2de121f0 DDAG |
223 | } |
224 | ||
225 | int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count) | |
226 | { | |
7387863d DDAG |
227 | int res; |
228 | struct iovec *padded_iov; | |
2de121f0 | 229 | |
7387863d DDAG |
230 | padded_iov = malloc((count + 1) * sizeof(struct iovec)); |
231 | if (padded_iov == NULL) { | |
232 | return fuse_reply_err(req, ENOMEM); | |
233 | } | |
2de121f0 | 234 | |
7387863d DDAG |
235 | memcpy(padded_iov + 1, iov, count * sizeof(struct iovec)); |
236 | count++; | |
2de121f0 | 237 | |
7387863d DDAG |
238 | res = send_reply_iov(req, 0, padded_iov, count); |
239 | free(padded_iov); | |
2de121f0 | 240 | |
7387863d | 241 | return res; |
2de121f0 DDAG |
242 | } |
243 | ||
244 | ||
7387863d DDAG |
245 | /* |
246 | * 'buf` is allowed to be empty so that the proper size may be | |
247 | * allocated by the caller | |
248 | */ | |
2de121f0 | 249 | size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, |
7387863d | 250 | const char *name, const struct stat *stbuf, off_t off) |
2de121f0 | 251 | { |
7387863d DDAG |
252 | (void)req; |
253 | size_t namelen; | |
254 | size_t entlen; | |
255 | size_t entlen_padded; | |
256 | struct fuse_dirent *dirent; | |
2de121f0 | 257 | |
7387863d DDAG |
258 | namelen = strlen(name); |
259 | entlen = FUSE_NAME_OFFSET + namelen; | |
260 | entlen_padded = FUSE_DIRENT_ALIGN(entlen); | |
2de121f0 | 261 | |
7387863d DDAG |
262 | if ((buf == NULL) || (entlen_padded > bufsize)) { |
263 | return entlen_padded; | |
264 | } | |
2de121f0 | 265 | |
7387863d DDAG |
266 | dirent = (struct fuse_dirent *)buf; |
267 | dirent->ino = stbuf->st_ino; | |
268 | dirent->off = off; | |
269 | dirent->namelen = namelen; | |
270 | dirent->type = (stbuf->st_mode & S_IFMT) >> 12; | |
271 | memcpy(dirent->name, name, namelen); | |
272 | memset(dirent->name + namelen, 0, entlen_padded - entlen); | |
2de121f0 | 273 | |
7387863d | 274 | return entlen_padded; |
2de121f0 DDAG |
275 | } |
276 | ||
277 | static void convert_statfs(const struct statvfs *stbuf, | |
7387863d | 278 | struct fuse_kstatfs *kstatfs) |
2de121f0 | 279 | { |
3db2876a SH |
280 | *kstatfs = (struct fuse_kstatfs){ |
281 | .bsize = stbuf->f_bsize, | |
282 | .frsize = stbuf->f_frsize, | |
283 | .blocks = stbuf->f_blocks, | |
284 | .bfree = stbuf->f_bfree, | |
285 | .bavail = stbuf->f_bavail, | |
286 | .files = stbuf->f_files, | |
287 | .ffree = stbuf->f_ffree, | |
288 | .namelen = stbuf->f_namemax, | |
289 | }; | |
2de121f0 DDAG |
290 | } |
291 | ||
292 | static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize) | |
293 | { | |
7387863d | 294 | return send_reply(req, 0, arg, argsize); |
2de121f0 DDAG |
295 | } |
296 | ||
297 | int fuse_reply_err(fuse_req_t req, int err) | |
298 | { | |
7387863d | 299 | return send_reply(req, -err, NULL, 0); |
2de121f0 DDAG |
300 | } |
301 | ||
302 | void fuse_reply_none(fuse_req_t req) | |
303 | { | |
7387863d | 304 | fuse_free_req(req); |
2de121f0 DDAG |
305 | } |
306 | ||
307 | static unsigned long calc_timeout_sec(double t) | |
308 | { | |
7387863d DDAG |
309 | if (t > (double)ULONG_MAX) { |
310 | return ULONG_MAX; | |
311 | } else if (t < 0.0) { | |
312 | return 0; | |
313 | } else { | |
314 | return (unsigned long)t; | |
315 | } | |
2de121f0 DDAG |
316 | } |
317 | ||
318 | static unsigned int calc_timeout_nsec(double t) | |
319 | { | |
7387863d DDAG |
320 | double f = t - (double)calc_timeout_sec(t); |
321 | if (f < 0.0) { | |
322 | return 0; | |
323 | } else if (f >= 0.999999999) { | |
324 | return 999999999; | |
325 | } else { | |
326 | return (unsigned int)(f * 1.0e9); | |
327 | } | |
2de121f0 DDAG |
328 | } |
329 | ||
330 | static void fill_entry(struct fuse_entry_out *arg, | |
7387863d | 331 | const struct fuse_entry_param *e) |
2de121f0 | 332 | { |
3db2876a SH |
333 | *arg = (struct fuse_entry_out){ |
334 | .nodeid = e->ino, | |
335 | .generation = e->generation, | |
336 | .entry_valid = calc_timeout_sec(e->entry_timeout), | |
337 | .entry_valid_nsec = calc_timeout_nsec(e->entry_timeout), | |
338 | .attr_valid = calc_timeout_sec(e->attr_timeout), | |
339 | .attr_valid_nsec = calc_timeout_nsec(e->attr_timeout), | |
340 | }; | |
7387863d | 341 | convert_stat(&e->attr, &arg->attr); |
2de121f0 DDAG |
342 | } |
343 | ||
7387863d DDAG |
344 | /* |
345 | * `buf` is allowed to be empty so that the proper size may be | |
346 | * allocated by the caller | |
347 | */ | |
2de121f0 | 348 | size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize, |
7387863d DDAG |
349 | const char *name, |
350 | const struct fuse_entry_param *e, off_t off) | |
351 | { | |
352 | (void)req; | |
353 | size_t namelen; | |
354 | size_t entlen; | |
355 | size_t entlen_padded; | |
356 | ||
357 | namelen = strlen(name); | |
358 | entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen; | |
359 | entlen_padded = FUSE_DIRENT_ALIGN(entlen); | |
360 | if ((buf == NULL) || (entlen_padded > bufsize)) { | |
361 | return entlen_padded; | |
362 | } | |
363 | ||
364 | struct fuse_direntplus *dp = (struct fuse_direntplus *)buf; | |
365 | memset(&dp->entry_out, 0, sizeof(dp->entry_out)); | |
366 | fill_entry(&dp->entry_out, e); | |
367 | ||
368 | struct fuse_dirent *dirent = &dp->dirent; | |
3db2876a SH |
369 | *dirent = (struct fuse_dirent){ |
370 | .ino = e->attr.st_ino, | |
371 | .off = off, | |
372 | .namelen = namelen, | |
373 | .type = (e->attr.st_mode & S_IFMT) >> 12, | |
374 | }; | |
7387863d DDAG |
375 | memcpy(dirent->name, name, namelen); |
376 | memset(dirent->name + namelen, 0, entlen_padded - entlen); | |
377 | ||
378 | return entlen_padded; | |
379 | } | |
380 | ||
381 | static void fill_open(struct fuse_open_out *arg, const struct fuse_file_info *f) | |
382 | { | |
383 | arg->fh = f->fh; | |
384 | if (f->direct_io) { | |
385 | arg->open_flags |= FOPEN_DIRECT_IO; | |
386 | } | |
387 | if (f->keep_cache) { | |
388 | arg->open_flags |= FOPEN_KEEP_CACHE; | |
389 | } | |
390 | if (f->cache_readdir) { | |
391 | arg->open_flags |= FOPEN_CACHE_DIR; | |
392 | } | |
393 | if (f->nonseekable) { | |
394 | arg->open_flags |= FOPEN_NONSEEKABLE; | |
395 | } | |
2de121f0 DDAG |
396 | } |
397 | ||
398 | int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e) | |
399 | { | |
7387863d | 400 | struct fuse_entry_out arg; |
72c42e2d | 401 | size_t size = sizeof(arg); |
2de121f0 | 402 | |
7387863d DDAG |
403 | memset(&arg, 0, sizeof(arg)); |
404 | fill_entry(&arg, e); | |
405 | return send_reply_ok(req, &arg, size); | |
2de121f0 DDAG |
406 | } |
407 | ||
408 | int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, | |
7387863d | 409 | const struct fuse_file_info *f) |
2de121f0 | 410 | { |
7387863d | 411 | char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)]; |
72c42e2d | 412 | size_t entrysize = sizeof(struct fuse_entry_out); |
7387863d DDAG |
413 | struct fuse_entry_out *earg = (struct fuse_entry_out *)buf; |
414 | struct fuse_open_out *oarg = (struct fuse_open_out *)(buf + entrysize); | |
2de121f0 | 415 | |
7387863d DDAG |
416 | memset(buf, 0, sizeof(buf)); |
417 | fill_entry(earg, e); | |
418 | fill_open(oarg, f); | |
419 | return send_reply_ok(req, buf, entrysize + sizeof(struct fuse_open_out)); | |
2de121f0 DDAG |
420 | } |
421 | ||
422 | int fuse_reply_attr(fuse_req_t req, const struct stat *attr, | |
7387863d | 423 | double attr_timeout) |
2de121f0 | 424 | { |
7387863d | 425 | struct fuse_attr_out arg; |
72c42e2d | 426 | size_t size = sizeof(arg); |
2de121f0 | 427 | |
7387863d DDAG |
428 | memset(&arg, 0, sizeof(arg)); |
429 | arg.attr_valid = calc_timeout_sec(attr_timeout); | |
430 | arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout); | |
431 | convert_stat(attr, &arg.attr); | |
2de121f0 | 432 | |
7387863d | 433 | return send_reply_ok(req, &arg, size); |
2de121f0 DDAG |
434 | } |
435 | ||
436 | int fuse_reply_readlink(fuse_req_t req, const char *linkname) | |
437 | { | |
7387863d | 438 | return send_reply_ok(req, linkname, strlen(linkname)); |
2de121f0 DDAG |
439 | } |
440 | ||
441 | int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f) | |
442 | { | |
7387863d | 443 | struct fuse_open_out arg; |
2de121f0 | 444 | |
7387863d DDAG |
445 | memset(&arg, 0, sizeof(arg)); |
446 | fill_open(&arg, f); | |
447 | return send_reply_ok(req, &arg, sizeof(arg)); | |
2de121f0 DDAG |
448 | } |
449 | ||
450 | int fuse_reply_write(fuse_req_t req, size_t count) | |
451 | { | |
7387863d | 452 | struct fuse_write_out arg; |
2de121f0 | 453 | |
7387863d DDAG |
454 | memset(&arg, 0, sizeof(arg)); |
455 | arg.size = count; | |
2de121f0 | 456 | |
7387863d | 457 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
458 | } |
459 | ||
460 | int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size) | |
461 | { | |
7387863d | 462 | return send_reply_ok(req, buf, size); |
2de121f0 DDAG |
463 | } |
464 | ||
465 | static int fuse_send_data_iov_fallback(struct fuse_session *se, | |
7387863d DDAG |
466 | struct fuse_chan *ch, struct iovec *iov, |
467 | int iov_count, struct fuse_bufvec *buf, | |
468 | size_t len) | |
2de121f0 | 469 | { |
7387863d DDAG |
470 | /* Optimize common case */ |
471 | if (buf->count == 1 && buf->idx == 0 && buf->off == 0 && | |
472 | !(buf->buf[0].flags & FUSE_BUF_IS_FD)) { | |
473 | /* | |
474 | * FIXME: also avoid memory copy if there are multiple buffers | |
475 | * but none of them contain an fd | |
476 | */ | |
2de121f0 | 477 | |
7387863d DDAG |
478 | iov[iov_count].iov_base = buf->buf[0].mem; |
479 | iov[iov_count].iov_len = len; | |
480 | iov_count++; | |
481 | return fuse_send_msg(se, ch, iov, iov_count); | |
482 | } | |
2de121f0 | 483 | |
eb49d187 DDAG |
484 | if (fuse_lowlevel_is_virtio(se) && buf->count == 1 && |
485 | buf->buf[0].flags == (FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK)) { | |
486 | return virtio_send_data_iov(se, ch, iov, iov_count, buf, len); | |
487 | } | |
488 | ||
7387863d DDAG |
489 | abort(); /* Will have taken vhost path */ |
490 | return 0; | |
2de121f0 DDAG |
491 | } |
492 | ||
2de121f0 | 493 | static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch, |
7387863d | 494 | struct iovec *iov, int iov_count, |
8c3fe75e | 495 | struct fuse_bufvec *buf) |
2de121f0 | 496 | { |
7387863d | 497 | size_t len = fuse_buf_size(buf); |
2de121f0 | 498 | |
7387863d | 499 | return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len); |
2de121f0 | 500 | } |
2de121f0 | 501 | |
8c3fe75e | 502 | int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv) |
2de121f0 | 503 | { |
7387863d | 504 | struct iovec iov[2]; |
3db2876a SH |
505 | struct fuse_out_header out = { |
506 | .unique = req->unique, | |
507 | }; | |
7387863d | 508 | int res; |
2de121f0 | 509 | |
7387863d DDAG |
510 | iov[0].iov_base = &out; |
511 | iov[0].iov_len = sizeof(struct fuse_out_header); | |
2de121f0 | 512 | |
8c3fe75e | 513 | res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv); |
7387863d DDAG |
514 | if (res <= 0) { |
515 | fuse_free_req(req); | |
516 | return res; | |
517 | } else { | |
518 | return fuse_reply_err(req, res); | |
519 | } | |
2de121f0 DDAG |
520 | } |
521 | ||
522 | int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf) | |
523 | { | |
7387863d | 524 | struct fuse_statfs_out arg; |
72c42e2d | 525 | size_t size = sizeof(arg); |
2de121f0 | 526 | |
7387863d DDAG |
527 | memset(&arg, 0, sizeof(arg)); |
528 | convert_statfs(stbuf, &arg.st); | |
2de121f0 | 529 | |
7387863d | 530 | return send_reply_ok(req, &arg, size); |
2de121f0 DDAG |
531 | } |
532 | ||
533 | int fuse_reply_xattr(fuse_req_t req, size_t count) | |
534 | { | |
7387863d | 535 | struct fuse_getxattr_out arg; |
2de121f0 | 536 | |
7387863d DDAG |
537 | memset(&arg, 0, sizeof(arg)); |
538 | arg.size = count; | |
2de121f0 | 539 | |
7387863d | 540 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
541 | } |
542 | ||
543 | int fuse_reply_lock(fuse_req_t req, const struct flock *lock) | |
544 | { | |
7387863d | 545 | struct fuse_lk_out arg; |
2de121f0 | 546 | |
7387863d DDAG |
547 | memset(&arg, 0, sizeof(arg)); |
548 | arg.lk.type = lock->l_type; | |
549 | if (lock->l_type != F_UNLCK) { | |
550 | arg.lk.start = lock->l_start; | |
551 | if (lock->l_len == 0) { | |
552 | arg.lk.end = OFFSET_MAX; | |
553 | } else { | |
554 | arg.lk.end = lock->l_start + lock->l_len - 1; | |
555 | } | |
556 | } | |
557 | arg.lk.pid = lock->l_pid; | |
558 | return send_reply_ok(req, &arg, sizeof(arg)); | |
2de121f0 DDAG |
559 | } |
560 | ||
561 | int fuse_reply_bmap(fuse_req_t req, uint64_t idx) | |
562 | { | |
7387863d | 563 | struct fuse_bmap_out arg; |
2de121f0 | 564 | |
7387863d DDAG |
565 | memset(&arg, 0, sizeof(arg)); |
566 | arg.block = idx; | |
2de121f0 | 567 | |
7387863d | 568 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
569 | } |
570 | ||
571 | static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov, | |
7387863d DDAG |
572 | size_t count) |
573 | { | |
574 | struct fuse_ioctl_iovec *fiov; | |
575 | size_t i; | |
576 | ||
577 | fiov = malloc(sizeof(fiov[0]) * count); | |
578 | if (!fiov) { | |
579 | return NULL; | |
580 | } | |
581 | ||
582 | for (i = 0; i < count; i++) { | |
583 | fiov[i].base = (uintptr_t)iov[i].iov_base; | |
584 | fiov[i].len = iov[i].iov_len; | |
585 | } | |
586 | ||
587 | return fiov; | |
588 | } | |
589 | ||
590 | int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov, | |
591 | size_t in_count, const struct iovec *out_iov, | |
592 | size_t out_count) | |
593 | { | |
594 | struct fuse_ioctl_out arg; | |
595 | struct fuse_ioctl_iovec *in_fiov = NULL; | |
596 | struct fuse_ioctl_iovec *out_fiov = NULL; | |
597 | struct iovec iov[4]; | |
598 | size_t count = 1; | |
599 | int res; | |
600 | ||
601 | memset(&arg, 0, sizeof(arg)); | |
602 | arg.flags |= FUSE_IOCTL_RETRY; | |
603 | arg.in_iovs = in_count; | |
604 | arg.out_iovs = out_count; | |
605 | iov[count].iov_base = &arg; | |
606 | iov[count].iov_len = sizeof(arg); | |
607 | count++; | |
608 | ||
72c42e2d DDAG |
609 | /* Can't handle non-compat 64bit ioctls on 32bit */ |
610 | if (sizeof(void *) == 4 && req->ioctl_64bit) { | |
611 | res = fuse_reply_err(req, EINVAL); | |
612 | goto out; | |
613 | } | |
7387863d | 614 | |
72c42e2d DDAG |
615 | if (in_count) { |
616 | in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count); | |
617 | if (!in_fiov) { | |
618 | goto enomem; | |
7387863d | 619 | } |
7387863d | 620 | |
72c42e2d DDAG |
621 | iov[count].iov_base = (void *)in_fiov; |
622 | iov[count].iov_len = sizeof(in_fiov[0]) * in_count; | |
623 | count++; | |
624 | } | |
625 | if (out_count) { | |
626 | out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count); | |
627 | if (!out_fiov) { | |
628 | goto enomem; | |
7387863d | 629 | } |
7387863d | 630 | |
72c42e2d DDAG |
631 | iov[count].iov_base = (void *)out_fiov; |
632 | iov[count].iov_len = sizeof(out_fiov[0]) * out_count; | |
633 | count++; | |
7387863d DDAG |
634 | } |
635 | ||
636 | res = send_reply_iov(req, 0, iov, count); | |
2de121f0 | 637 | out: |
7387863d DDAG |
638 | free(in_fiov); |
639 | free(out_fiov); | |
2de121f0 | 640 | |
7387863d | 641 | return res; |
2de121f0 DDAG |
642 | |
643 | enomem: | |
7387863d DDAG |
644 | res = fuse_reply_err(req, ENOMEM); |
645 | goto out; | |
2de121f0 DDAG |
646 | } |
647 | ||
648 | int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size) | |
649 | { | |
7387863d DDAG |
650 | struct fuse_ioctl_out arg; |
651 | struct iovec iov[3]; | |
652 | size_t count = 1; | |
2de121f0 | 653 | |
7387863d DDAG |
654 | memset(&arg, 0, sizeof(arg)); |
655 | arg.result = result; | |
656 | iov[count].iov_base = &arg; | |
657 | iov[count].iov_len = sizeof(arg); | |
658 | count++; | |
2de121f0 | 659 | |
7387863d DDAG |
660 | if (size) { |
661 | iov[count].iov_base = (char *)buf; | |
662 | iov[count].iov_len = size; | |
663 | count++; | |
664 | } | |
2de121f0 | 665 | |
7387863d | 666 | return send_reply_iov(req, 0, iov, count); |
2de121f0 DDAG |
667 | } |
668 | ||
669 | int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, | |
7387863d | 670 | int count) |
2de121f0 | 671 | { |
7387863d DDAG |
672 | struct iovec *padded_iov; |
673 | struct fuse_ioctl_out arg; | |
674 | int res; | |
2de121f0 | 675 | |
7387863d DDAG |
676 | padded_iov = malloc((count + 2) * sizeof(struct iovec)); |
677 | if (padded_iov == NULL) { | |
678 | return fuse_reply_err(req, ENOMEM); | |
679 | } | |
2de121f0 | 680 | |
7387863d DDAG |
681 | memset(&arg, 0, sizeof(arg)); |
682 | arg.result = result; | |
683 | padded_iov[1].iov_base = &arg; | |
684 | padded_iov[1].iov_len = sizeof(arg); | |
2de121f0 | 685 | |
7387863d | 686 | memcpy(&padded_iov[2], iov, count * sizeof(struct iovec)); |
2de121f0 | 687 | |
7387863d DDAG |
688 | res = send_reply_iov(req, 0, padded_iov, count + 2); |
689 | free(padded_iov); | |
2de121f0 | 690 | |
7387863d | 691 | return res; |
2de121f0 DDAG |
692 | } |
693 | ||
694 | int fuse_reply_poll(fuse_req_t req, unsigned revents) | |
695 | { | |
7387863d | 696 | struct fuse_poll_out arg; |
2de121f0 | 697 | |
7387863d DDAG |
698 | memset(&arg, 0, sizeof(arg)); |
699 | arg.revents = revents; | |
2de121f0 | 700 | |
7387863d | 701 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
702 | } |
703 | ||
704 | int fuse_reply_lseek(fuse_req_t req, off_t off) | |
705 | { | |
7387863d | 706 | struct fuse_lseek_out arg; |
2de121f0 | 707 | |
7387863d DDAG |
708 | memset(&arg, 0, sizeof(arg)); |
709 | arg.offset = off; | |
2de121f0 | 710 | |
7387863d | 711 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
712 | } |
713 | ||
70995754 SH |
714 | static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, |
715 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 716 | { |
70995754 SH |
717 | const char *name = fuse_mbuf_iter_advance_str(iter); |
718 | if (!name) { | |
719 | fuse_reply_err(req, EINVAL); | |
720 | return; | |
721 | } | |
2de121f0 | 722 | |
7387863d DDAG |
723 | if (req->se->op.lookup) { |
724 | req->se->op.lookup(req, nodeid, name); | |
725 | } else { | |
726 | fuse_reply_err(req, ENOSYS); | |
727 | } | |
2de121f0 DDAG |
728 | } |
729 | ||
70995754 SH |
730 | static void do_forget(fuse_req_t req, fuse_ino_t nodeid, |
731 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 732 | { |
70995754 SH |
733 | struct fuse_forget_in *arg; |
734 | ||
735 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
736 | if (!arg) { | |
737 | fuse_reply_err(req, EINVAL); | |
738 | return; | |
739 | } | |
2de121f0 | 740 | |
7387863d DDAG |
741 | if (req->se->op.forget) { |
742 | req->se->op.forget(req, nodeid, arg->nlookup); | |
743 | } else { | |
744 | fuse_reply_none(req); | |
745 | } | |
2de121f0 DDAG |
746 | } |
747 | ||
748 | static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid, | |
70995754 | 749 | struct fuse_mbuf_iter *iter) |
2de121f0 | 750 | { |
70995754 SH |
751 | struct fuse_batch_forget_in *arg; |
752 | struct fuse_forget_data *forgets; | |
753 | size_t scount; | |
2de121f0 | 754 | |
7387863d | 755 | (void)nodeid; |
2de121f0 | 756 | |
70995754 SH |
757 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
758 | if (!arg) { | |
759 | fuse_reply_none(req); | |
760 | return; | |
761 | } | |
762 | ||
763 | /* | |
764 | * Prevent integer overflow. The compiler emits the following warning | |
765 | * unless we use the scount local variable: | |
766 | * | |
767 | * error: comparison is always false due to limited range of data type | |
768 | * [-Werror=type-limits] | |
769 | * | |
770 | * This may be true on 64-bit hosts but we need this check for 32-bit | |
771 | * hosts. | |
772 | */ | |
773 | scount = arg->count; | |
774 | if (scount > SIZE_MAX / sizeof(forgets[0])) { | |
775 | fuse_reply_none(req); | |
776 | return; | |
777 | } | |
778 | ||
779 | forgets = fuse_mbuf_iter_advance(iter, arg->count * sizeof(forgets[0])); | |
780 | if (!forgets) { | |
781 | fuse_reply_none(req); | |
782 | return; | |
783 | } | |
784 | ||
7387863d | 785 | if (req->se->op.forget_multi) { |
70995754 | 786 | req->se->op.forget_multi(req, arg->count, forgets); |
7387863d | 787 | } else if (req->se->op.forget) { |
70995754 SH |
788 | unsigned int i; |
789 | ||
7387863d | 790 | for (i = 0; i < arg->count; i++) { |
7387863d | 791 | struct fuse_req *dummy_req; |
2de121f0 | 792 | |
7387863d DDAG |
793 | dummy_req = fuse_ll_alloc_req(req->se); |
794 | if (dummy_req == NULL) { | |
795 | break; | |
796 | } | |
2de121f0 | 797 | |
7387863d DDAG |
798 | dummy_req->unique = req->unique; |
799 | dummy_req->ctx = req->ctx; | |
800 | dummy_req->ch = NULL; | |
2de121f0 | 801 | |
70995754 | 802 | req->se->op.forget(dummy_req, forgets[i].ino, forgets[i].nlookup); |
7387863d DDAG |
803 | } |
804 | fuse_reply_none(req); | |
805 | } else { | |
806 | fuse_reply_none(req); | |
807 | } | |
2de121f0 DDAG |
808 | } |
809 | ||
70995754 SH |
810 | static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, |
811 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 812 | { |
7387863d DDAG |
813 | struct fuse_file_info *fip = NULL; |
814 | struct fuse_file_info fi; | |
2de121f0 | 815 | |
70995754 SH |
816 | struct fuse_getattr_in *arg; |
817 | ||
818 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
819 | if (!arg) { | |
820 | fuse_reply_err(req, EINVAL); | |
821 | return; | |
822 | } | |
2de121f0 | 823 | |
72c42e2d DDAG |
824 | if (arg->getattr_flags & FUSE_GETATTR_FH) { |
825 | memset(&fi, 0, sizeof(fi)); | |
826 | fi.fh = arg->fh; | |
827 | fip = &fi; | |
7387863d | 828 | } |
2de121f0 | 829 | |
7387863d DDAG |
830 | if (req->se->op.getattr) { |
831 | req->se->op.getattr(req, nodeid, fip); | |
832 | } else { | |
833 | fuse_reply_err(req, ENOSYS); | |
834 | } | |
2de121f0 DDAG |
835 | } |
836 | ||
70995754 SH |
837 | static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, |
838 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 839 | { |
7387863d | 840 | if (req->se->op.setattr) { |
70995754 | 841 | struct fuse_setattr_in *arg; |
7387863d DDAG |
842 | struct fuse_file_info *fi = NULL; |
843 | struct fuse_file_info fi_store; | |
844 | struct stat stbuf; | |
70995754 SH |
845 | |
846 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
847 | if (!arg) { | |
848 | fuse_reply_err(req, EINVAL); | |
849 | return; | |
850 | } | |
851 | ||
7387863d DDAG |
852 | memset(&stbuf, 0, sizeof(stbuf)); |
853 | convert_attr(arg, &stbuf); | |
854 | if (arg->valid & FATTR_FH) { | |
855 | arg->valid &= ~FATTR_FH; | |
856 | memset(&fi_store, 0, sizeof(fi_store)); | |
857 | fi = &fi_store; | |
858 | fi->fh = arg->fh; | |
859 | } | |
860 | arg->valid &= FUSE_SET_ATTR_MODE | FUSE_SET_ATTR_UID | | |
861 | FUSE_SET_ATTR_GID | FUSE_SET_ATTR_SIZE | | |
862 | FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME | | |
863 | FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW | | |
864 | FUSE_SET_ATTR_CTIME; | |
865 | ||
866 | req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi); | |
867 | } else { | |
868 | fuse_reply_err(req, ENOSYS); | |
869 | } | |
2de121f0 DDAG |
870 | } |
871 | ||
70995754 SH |
872 | static void do_access(fuse_req_t req, fuse_ino_t nodeid, |
873 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 874 | { |
70995754 SH |
875 | struct fuse_access_in *arg; |
876 | ||
877 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
878 | if (!arg) { | |
879 | fuse_reply_err(req, EINVAL); | |
880 | return; | |
881 | } | |
2de121f0 | 882 | |
7387863d DDAG |
883 | if (req->se->op.access) { |
884 | req->se->op.access(req, nodeid, arg->mask); | |
885 | } else { | |
886 | fuse_reply_err(req, ENOSYS); | |
887 | } | |
2de121f0 DDAG |
888 | } |
889 | ||
70995754 SH |
890 | static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, |
891 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 892 | { |
70995754 | 893 | (void)iter; |
2de121f0 | 894 | |
7387863d DDAG |
895 | if (req->se->op.readlink) { |
896 | req->se->op.readlink(req, nodeid); | |
897 | } else { | |
898 | fuse_reply_err(req, ENOSYS); | |
899 | } | |
2de121f0 DDAG |
900 | } |
901 | ||
70995754 SH |
902 | static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, |
903 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 904 | { |
70995754 SH |
905 | struct fuse_mknod_in *arg; |
906 | const char *name; | |
907 | ||
908 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
909 | name = fuse_mbuf_iter_advance_str(iter); | |
910 | if (!arg || !name) { | |
911 | fuse_reply_err(req, EINVAL); | |
912 | return; | |
913 | } | |
2de121f0 | 914 | |
72c42e2d | 915 | req->ctx.umask = arg->umask; |
2de121f0 | 916 | |
7387863d DDAG |
917 | if (req->se->op.mknod) { |
918 | req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev); | |
919 | } else { | |
920 | fuse_reply_err(req, ENOSYS); | |
921 | } | |
2de121f0 DDAG |
922 | } |
923 | ||
70995754 SH |
924 | static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, |
925 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 926 | { |
70995754 SH |
927 | struct fuse_mkdir_in *arg; |
928 | const char *name; | |
929 | ||
930 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
931 | name = fuse_mbuf_iter_advance_str(iter); | |
932 | if (!arg || !name) { | |
933 | fuse_reply_err(req, EINVAL); | |
934 | return; | |
935 | } | |
2de121f0 | 936 | |
72c42e2d | 937 | req->ctx.umask = arg->umask; |
2de121f0 | 938 | |
7387863d | 939 | if (req->se->op.mkdir) { |
70995754 | 940 | req->se->op.mkdir(req, nodeid, name, arg->mode); |
7387863d DDAG |
941 | } else { |
942 | fuse_reply_err(req, ENOSYS); | |
943 | } | |
2de121f0 DDAG |
944 | } |
945 | ||
70995754 SH |
946 | static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, |
947 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 948 | { |
70995754 SH |
949 | const char *name = fuse_mbuf_iter_advance_str(iter); |
950 | ||
951 | if (!name) { | |
952 | fuse_reply_err(req, EINVAL); | |
953 | return; | |
954 | } | |
2de121f0 | 955 | |
7387863d DDAG |
956 | if (req->se->op.unlink) { |
957 | req->se->op.unlink(req, nodeid, name); | |
958 | } else { | |
959 | fuse_reply_err(req, ENOSYS); | |
960 | } | |
2de121f0 DDAG |
961 | } |
962 | ||
70995754 SH |
963 | static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, |
964 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 965 | { |
70995754 SH |
966 | const char *name = fuse_mbuf_iter_advance_str(iter); |
967 | ||
968 | if (!name) { | |
969 | fuse_reply_err(req, EINVAL); | |
970 | return; | |
971 | } | |
2de121f0 | 972 | |
7387863d DDAG |
973 | if (req->se->op.rmdir) { |
974 | req->se->op.rmdir(req, nodeid, name); | |
975 | } else { | |
976 | fuse_reply_err(req, ENOSYS); | |
977 | } | |
2de121f0 DDAG |
978 | } |
979 | ||
70995754 SH |
980 | static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, |
981 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 982 | { |
70995754 SH |
983 | const char *name = fuse_mbuf_iter_advance_str(iter); |
984 | const char *linkname = fuse_mbuf_iter_advance_str(iter); | |
985 | ||
986 | if (!name || !linkname) { | |
987 | fuse_reply_err(req, EINVAL); | |
988 | return; | |
989 | } | |
2de121f0 | 990 | |
7387863d DDAG |
991 | if (req->se->op.symlink) { |
992 | req->se->op.symlink(req, linkname, nodeid, name); | |
993 | } else { | |
994 | fuse_reply_err(req, ENOSYS); | |
995 | } | |
2de121f0 DDAG |
996 | } |
997 | ||
70995754 SH |
998 | static void do_rename(fuse_req_t req, fuse_ino_t nodeid, |
999 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1000 | { |
70995754 SH |
1001 | struct fuse_rename_in *arg; |
1002 | const char *oldname; | |
1003 | const char *newname; | |
1004 | ||
1005 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1006 | oldname = fuse_mbuf_iter_advance_str(iter); | |
1007 | newname = fuse_mbuf_iter_advance_str(iter); | |
1008 | if (!arg || !oldname || !newname) { | |
1009 | fuse_reply_err(req, EINVAL); | |
1010 | return; | |
1011 | } | |
2de121f0 | 1012 | |
7387863d DDAG |
1013 | if (req->se->op.rename) { |
1014 | req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, 0); | |
1015 | } else { | |
1016 | fuse_reply_err(req, ENOSYS); | |
1017 | } | |
2de121f0 DDAG |
1018 | } |
1019 | ||
70995754 SH |
1020 | static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, |
1021 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1022 | { |
70995754 SH |
1023 | struct fuse_rename2_in *arg; |
1024 | const char *oldname; | |
1025 | const char *newname; | |
1026 | ||
1027 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1028 | oldname = fuse_mbuf_iter_advance_str(iter); | |
1029 | newname = fuse_mbuf_iter_advance_str(iter); | |
1030 | if (!arg || !oldname || !newname) { | |
1031 | fuse_reply_err(req, EINVAL); | |
1032 | return; | |
1033 | } | |
2de121f0 | 1034 | |
7387863d DDAG |
1035 | if (req->se->op.rename) { |
1036 | req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, | |
1037 | arg->flags); | |
1038 | } else { | |
1039 | fuse_reply_err(req, ENOSYS); | |
1040 | } | |
2de121f0 DDAG |
1041 | } |
1042 | ||
70995754 SH |
1043 | static void do_link(fuse_req_t req, fuse_ino_t nodeid, |
1044 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1045 | { |
70995754 SH |
1046 | struct fuse_link_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1047 | const char *name = fuse_mbuf_iter_advance_str(iter); | |
1048 | ||
1049 | if (!arg || !name) { | |
1050 | fuse_reply_err(req, EINVAL); | |
1051 | return; | |
1052 | } | |
2de121f0 | 1053 | |
7387863d | 1054 | if (req->se->op.link) { |
70995754 | 1055 | req->se->op.link(req, arg->oldnodeid, nodeid, name); |
7387863d DDAG |
1056 | } else { |
1057 | fuse_reply_err(req, ENOSYS); | |
1058 | } | |
2de121f0 DDAG |
1059 | } |
1060 | ||
70995754 SH |
1061 | static void do_create(fuse_req_t req, fuse_ino_t nodeid, |
1062 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1063 | { |
7387863d | 1064 | if (req->se->op.create) { |
70995754 | 1065 | struct fuse_create_in *arg; |
7387863d | 1066 | struct fuse_file_info fi; |
70995754 SH |
1067 | const char *name; |
1068 | ||
1069 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1070 | name = fuse_mbuf_iter_advance_str(iter); | |
1071 | if (!arg || !name) { | |
1072 | fuse_reply_err(req, EINVAL); | |
1073 | return; | |
1074 | } | |
2de121f0 | 1075 | |
7387863d DDAG |
1076 | memset(&fi, 0, sizeof(fi)); |
1077 | fi.flags = arg->flags; | |
2de121f0 | 1078 | |
72c42e2d | 1079 | req->ctx.umask = arg->umask; |
2de121f0 | 1080 | |
7387863d DDAG |
1081 | req->se->op.create(req, nodeid, name, arg->mode, &fi); |
1082 | } else { | |
1083 | fuse_reply_err(req, ENOSYS); | |
1084 | } | |
2de121f0 DDAG |
1085 | } |
1086 | ||
70995754 SH |
1087 | static void do_open(fuse_req_t req, fuse_ino_t nodeid, |
1088 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1089 | { |
70995754 | 1090 | struct fuse_open_in *arg; |
7387863d | 1091 | struct fuse_file_info fi; |
2de121f0 | 1092 | |
70995754 SH |
1093 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1094 | if (!arg) { | |
1095 | fuse_reply_err(req, EINVAL); | |
1096 | return; | |
1097 | } | |
1098 | ||
7387863d DDAG |
1099 | memset(&fi, 0, sizeof(fi)); |
1100 | fi.flags = arg->flags; | |
2de121f0 | 1101 | |
7387863d DDAG |
1102 | if (req->se->op.open) { |
1103 | req->se->op.open(req, nodeid, &fi); | |
1104 | } else { | |
1105 | fuse_reply_open(req, &fi); | |
1106 | } | |
2de121f0 DDAG |
1107 | } |
1108 | ||
70995754 SH |
1109 | static void do_read(fuse_req_t req, fuse_ino_t nodeid, |
1110 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1111 | { |
7387863d | 1112 | if (req->se->op.read) { |
70995754 | 1113 | struct fuse_read_in *arg; |
7387863d | 1114 | struct fuse_file_info fi; |
2de121f0 | 1115 | |
70995754 SH |
1116 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1117 | ||
7387863d DDAG |
1118 | memset(&fi, 0, sizeof(fi)); |
1119 | fi.fh = arg->fh; | |
72c42e2d DDAG |
1120 | fi.lock_owner = arg->lock_owner; |
1121 | fi.flags = arg->flags; | |
7387863d DDAG |
1122 | req->se->op.read(req, nodeid, arg->size, arg->offset, &fi); |
1123 | } else { | |
1124 | fuse_reply_err(req, ENOSYS); | |
1125 | } | |
2de121f0 DDAG |
1126 | } |
1127 | ||
70995754 SH |
1128 | static void do_write(fuse_req_t req, fuse_ino_t nodeid, |
1129 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1130 | { |
70995754 | 1131 | struct fuse_write_in *arg; |
7387863d | 1132 | struct fuse_file_info fi; |
70995754 SH |
1133 | const char *param; |
1134 | ||
1135 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1136 | if (!arg) { | |
1137 | fuse_reply_err(req, EINVAL); | |
1138 | return; | |
1139 | } | |
1140 | ||
1141 | param = fuse_mbuf_iter_advance(iter, arg->size); | |
1142 | if (!param) { | |
1143 | fuse_reply_err(req, EINVAL); | |
1144 | return; | |
1145 | } | |
2de121f0 | 1146 | |
7387863d DDAG |
1147 | memset(&fi, 0, sizeof(fi)); |
1148 | fi.fh = arg->fh; | |
1149 | fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0; | |
f779bc52 | 1150 | fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV); |
2de121f0 | 1151 | |
72c42e2d DDAG |
1152 | fi.lock_owner = arg->lock_owner; |
1153 | fi.flags = arg->flags; | |
2de121f0 | 1154 | |
7387863d DDAG |
1155 | if (req->se->op.write) { |
1156 | req->se->op.write(req, nodeid, param, arg->size, arg->offset, &fi); | |
1157 | } else { | |
1158 | fuse_reply_err(req, ENOSYS); | |
1159 | } | |
2de121f0 DDAG |
1160 | } |
1161 | ||
0ba8c3c6 SH |
1162 | static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, |
1163 | struct fuse_mbuf_iter *iter, struct fuse_bufvec *ibufv) | |
7387863d DDAG |
1164 | { |
1165 | struct fuse_session *se = req->se; | |
469f9d2f DDAG |
1166 | struct fuse_bufvec *pbufv = ibufv; |
1167 | struct fuse_bufvec tmpbufv = { | |
1168 | .buf[0] = ibufv->buf[0], | |
7387863d DDAG |
1169 | .count = 1, |
1170 | }; | |
0ba8c3c6 SH |
1171 | struct fuse_write_in *arg; |
1172 | size_t arg_size = sizeof(*arg); | |
7387863d DDAG |
1173 | struct fuse_file_info fi; |
1174 | ||
1175 | memset(&fi, 0, sizeof(fi)); | |
0ba8c3c6 SH |
1176 | |
1177 | arg = fuse_mbuf_iter_advance(iter, arg_size); | |
1178 | if (!arg) { | |
1179 | fuse_reply_err(req, EINVAL); | |
1180 | return; | |
1181 | } | |
1182 | ||
1183 | fi.lock_owner = arg->lock_owner; | |
1184 | fi.flags = arg->flags; | |
7387863d | 1185 | fi.fh = arg->fh; |
f779bc52 VG |
1186 | fi.writepage = !!(arg->write_flags & FUSE_WRITE_CACHE); |
1187 | fi.kill_priv = !!(arg->write_flags & FUSE_WRITE_KILL_PRIV); | |
7387863d | 1188 | |
469f9d2f | 1189 | if (ibufv->count == 1) { |
0ba8c3c6 SH |
1190 | assert(!(tmpbufv.buf[0].flags & FUSE_BUF_IS_FD)); |
1191 | tmpbufv.buf[0].mem = ((char *)arg) + arg_size; | |
1192 | tmpbufv.buf[0].size -= sizeof(struct fuse_in_header) + arg_size; | |
469f9d2f DDAG |
1193 | pbufv = &tmpbufv; |
1194 | } else { | |
1195 | /* | |
1196 | * Input bufv contains the headers in the first element | |
1197 | * and the data in the rest, we need to skip that first element | |
1198 | */ | |
1199 | ibufv->buf[0].size = 0; | |
7387863d | 1200 | } |
7387863d | 1201 | |
0ba8c3c6 SH |
1202 | if (fuse_buf_size(pbufv) != arg->size) { |
1203 | fuse_log(FUSE_LOG_ERR, | |
1204 | "fuse: do_write_buf: buffer size doesn't match arg->size\n"); | |
1205 | fuse_reply_err(req, EIO); | |
1206 | return; | |
1207 | } | |
1208 | ||
469f9d2f | 1209 | se->op.write_buf(req, nodeid, pbufv, arg->offset, &fi); |
2de121f0 DDAG |
1210 | } |
1211 | ||
70995754 SH |
1212 | static void do_flush(fuse_req_t req, fuse_ino_t nodeid, |
1213 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1214 | { |
70995754 | 1215 | struct fuse_flush_in *arg; |
7387863d | 1216 | struct fuse_file_info fi; |
2de121f0 | 1217 | |
70995754 SH |
1218 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1219 | if (!arg) { | |
1220 | fuse_reply_err(req, EINVAL); | |
1221 | return; | |
1222 | } | |
1223 | ||
7387863d DDAG |
1224 | memset(&fi, 0, sizeof(fi)); |
1225 | fi.fh = arg->fh; | |
1226 | fi.flush = 1; | |
72c42e2d | 1227 | fi.lock_owner = arg->lock_owner; |
2de121f0 | 1228 | |
7387863d DDAG |
1229 | if (req->se->op.flush) { |
1230 | req->se->op.flush(req, nodeid, &fi); | |
1231 | } else { | |
1232 | fuse_reply_err(req, ENOSYS); | |
1233 | } | |
2de121f0 DDAG |
1234 | } |
1235 | ||
70995754 SH |
1236 | static void do_release(fuse_req_t req, fuse_ino_t nodeid, |
1237 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1238 | { |
70995754 | 1239 | struct fuse_release_in *arg; |
7387863d | 1240 | struct fuse_file_info fi; |
2de121f0 | 1241 | |
70995754 SH |
1242 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1243 | if (!arg) { | |
1244 | fuse_reply_err(req, EINVAL); | |
1245 | return; | |
1246 | } | |
1247 | ||
7387863d DDAG |
1248 | memset(&fi, 0, sizeof(fi)); |
1249 | fi.flags = arg->flags; | |
1250 | fi.fh = arg->fh; | |
72c42e2d DDAG |
1251 | fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0; |
1252 | fi.lock_owner = arg->lock_owner; | |
70995754 | 1253 | |
7387863d DDAG |
1254 | if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) { |
1255 | fi.flock_release = 1; | |
7387863d | 1256 | } |
2de121f0 | 1257 | |
7387863d DDAG |
1258 | if (req->se->op.release) { |
1259 | req->se->op.release(req, nodeid, &fi); | |
1260 | } else { | |
1261 | fuse_reply_err(req, 0); | |
1262 | } | |
2de121f0 DDAG |
1263 | } |
1264 | ||
70995754 SH |
1265 | static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, |
1266 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1267 | { |
70995754 | 1268 | struct fuse_fsync_in *arg; |
7387863d | 1269 | struct fuse_file_info fi; |
70995754 SH |
1270 | int datasync; |
1271 | ||
1272 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1273 | if (!arg) { | |
1274 | fuse_reply_err(req, EINVAL); | |
1275 | return; | |
1276 | } | |
1277 | datasync = arg->fsync_flags & 1; | |
2de121f0 | 1278 | |
7387863d DDAG |
1279 | memset(&fi, 0, sizeof(fi)); |
1280 | fi.fh = arg->fh; | |
2de121f0 | 1281 | |
7387863d | 1282 | if (req->se->op.fsync) { |
1b209805 VG |
1283 | if (fi.fh == (uint64_t)-1) { |
1284 | req->se->op.fsync(req, nodeid, datasync, NULL); | |
1285 | } else { | |
1286 | req->se->op.fsync(req, nodeid, datasync, &fi); | |
1287 | } | |
7387863d DDAG |
1288 | } else { |
1289 | fuse_reply_err(req, ENOSYS); | |
1290 | } | |
2de121f0 DDAG |
1291 | } |
1292 | ||
70995754 SH |
1293 | static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, |
1294 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1295 | { |
70995754 | 1296 | struct fuse_open_in *arg; |
7387863d | 1297 | struct fuse_file_info fi; |
2de121f0 | 1298 | |
70995754 SH |
1299 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1300 | if (!arg) { | |
1301 | fuse_reply_err(req, EINVAL); | |
1302 | return; | |
1303 | } | |
1304 | ||
7387863d DDAG |
1305 | memset(&fi, 0, sizeof(fi)); |
1306 | fi.flags = arg->flags; | |
2de121f0 | 1307 | |
7387863d DDAG |
1308 | if (req->se->op.opendir) { |
1309 | req->se->op.opendir(req, nodeid, &fi); | |
1310 | } else { | |
1311 | fuse_reply_open(req, &fi); | |
1312 | } | |
2de121f0 DDAG |
1313 | } |
1314 | ||
70995754 SH |
1315 | static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, |
1316 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1317 | { |
70995754 | 1318 | struct fuse_read_in *arg; |
7387863d | 1319 | struct fuse_file_info fi; |
2de121f0 | 1320 | |
70995754 SH |
1321 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1322 | if (!arg) { | |
1323 | fuse_reply_err(req, EINVAL); | |
1324 | return; | |
1325 | } | |
1326 | ||
7387863d DDAG |
1327 | memset(&fi, 0, sizeof(fi)); |
1328 | fi.fh = arg->fh; | |
2de121f0 | 1329 | |
7387863d DDAG |
1330 | if (req->se->op.readdir) { |
1331 | req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi); | |
1332 | } else { | |
1333 | fuse_reply_err(req, ENOSYS); | |
1334 | } | |
2de121f0 DDAG |
1335 | } |
1336 | ||
70995754 SH |
1337 | static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, |
1338 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1339 | { |
70995754 | 1340 | struct fuse_read_in *arg; |
7387863d | 1341 | struct fuse_file_info fi; |
2de121f0 | 1342 | |
70995754 SH |
1343 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1344 | if (!arg) { | |
1345 | fuse_reply_err(req, EINVAL); | |
1346 | return; | |
1347 | } | |
1348 | ||
7387863d DDAG |
1349 | memset(&fi, 0, sizeof(fi)); |
1350 | fi.fh = arg->fh; | |
2de121f0 | 1351 | |
7387863d DDAG |
1352 | if (req->se->op.readdirplus) { |
1353 | req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi); | |
1354 | } else { | |
1355 | fuse_reply_err(req, ENOSYS); | |
1356 | } | |
2de121f0 DDAG |
1357 | } |
1358 | ||
70995754 SH |
1359 | static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, |
1360 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1361 | { |
70995754 | 1362 | struct fuse_release_in *arg; |
7387863d | 1363 | struct fuse_file_info fi; |
2de121f0 | 1364 | |
70995754 SH |
1365 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1366 | if (!arg) { | |
1367 | fuse_reply_err(req, EINVAL); | |
1368 | return; | |
1369 | } | |
1370 | ||
7387863d DDAG |
1371 | memset(&fi, 0, sizeof(fi)); |
1372 | fi.flags = arg->flags; | |
1373 | fi.fh = arg->fh; | |
2de121f0 | 1374 | |
7387863d DDAG |
1375 | if (req->se->op.releasedir) { |
1376 | req->se->op.releasedir(req, nodeid, &fi); | |
1377 | } else { | |
1378 | fuse_reply_err(req, 0); | |
1379 | } | |
2de121f0 DDAG |
1380 | } |
1381 | ||
70995754 SH |
1382 | static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, |
1383 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1384 | { |
70995754 | 1385 | struct fuse_fsync_in *arg; |
7387863d | 1386 | struct fuse_file_info fi; |
70995754 SH |
1387 | int datasync; |
1388 | ||
1389 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1390 | if (!arg) { | |
1391 | fuse_reply_err(req, EINVAL); | |
1392 | return; | |
1393 | } | |
1394 | datasync = arg->fsync_flags & 1; | |
2de121f0 | 1395 | |
7387863d DDAG |
1396 | memset(&fi, 0, sizeof(fi)); |
1397 | fi.fh = arg->fh; | |
2de121f0 | 1398 | |
7387863d DDAG |
1399 | if (req->se->op.fsyncdir) { |
1400 | req->se->op.fsyncdir(req, nodeid, datasync, &fi); | |
1401 | } else { | |
1402 | fuse_reply_err(req, ENOSYS); | |
1403 | } | |
2de121f0 DDAG |
1404 | } |
1405 | ||
70995754 SH |
1406 | static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, |
1407 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1408 | { |
7387863d | 1409 | (void)nodeid; |
70995754 | 1410 | (void)iter; |
2de121f0 | 1411 | |
7387863d DDAG |
1412 | if (req->se->op.statfs) { |
1413 | req->se->op.statfs(req, nodeid); | |
1414 | } else { | |
1415 | struct statvfs buf = { | |
1416 | .f_namemax = 255, | |
1417 | .f_bsize = 512, | |
1418 | }; | |
1419 | fuse_reply_statfs(req, &buf); | |
1420 | } | |
2de121f0 DDAG |
1421 | } |
1422 | ||
70995754 SH |
1423 | static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, |
1424 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1425 | { |
70995754 SH |
1426 | struct fuse_setxattr_in *arg; |
1427 | const char *name; | |
1428 | const char *value; | |
1429 | ||
1430 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1431 | name = fuse_mbuf_iter_advance_str(iter); | |
1432 | if (!arg || !name) { | |
1433 | fuse_reply_err(req, EINVAL); | |
1434 | return; | |
1435 | } | |
1436 | ||
1437 | value = fuse_mbuf_iter_advance(iter, arg->size); | |
1438 | if (!value) { | |
1439 | fuse_reply_err(req, EINVAL); | |
1440 | return; | |
1441 | } | |
2de121f0 | 1442 | |
7387863d DDAG |
1443 | if (req->se->op.setxattr) { |
1444 | req->se->op.setxattr(req, nodeid, name, value, arg->size, arg->flags); | |
1445 | } else { | |
1446 | fuse_reply_err(req, ENOSYS); | |
1447 | } | |
2de121f0 DDAG |
1448 | } |
1449 | ||
70995754 SH |
1450 | static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, |
1451 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1452 | { |
70995754 SH |
1453 | struct fuse_getxattr_in *arg; |
1454 | const char *name; | |
1455 | ||
1456 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1457 | name = fuse_mbuf_iter_advance_str(iter); | |
1458 | if (!arg || !name) { | |
1459 | fuse_reply_err(req, EINVAL); | |
1460 | return; | |
1461 | } | |
2de121f0 | 1462 | |
7387863d | 1463 | if (req->se->op.getxattr) { |
70995754 | 1464 | req->se->op.getxattr(req, nodeid, name, arg->size); |
7387863d DDAG |
1465 | } else { |
1466 | fuse_reply_err(req, ENOSYS); | |
1467 | } | |
2de121f0 DDAG |
1468 | } |
1469 | ||
70995754 SH |
1470 | static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, |
1471 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1472 | { |
70995754 SH |
1473 | struct fuse_getxattr_in *arg; |
1474 | ||
1475 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1476 | if (!arg) { | |
1477 | fuse_reply_err(req, EINVAL); | |
1478 | return; | |
1479 | } | |
2de121f0 | 1480 | |
7387863d DDAG |
1481 | if (req->se->op.listxattr) { |
1482 | req->se->op.listxattr(req, nodeid, arg->size); | |
1483 | } else { | |
1484 | fuse_reply_err(req, ENOSYS); | |
1485 | } | |
2de121f0 DDAG |
1486 | } |
1487 | ||
70995754 SH |
1488 | static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, |
1489 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1490 | { |
70995754 SH |
1491 | const char *name = fuse_mbuf_iter_advance_str(iter); |
1492 | ||
1493 | if (!name) { | |
1494 | fuse_reply_err(req, EINVAL); | |
1495 | return; | |
1496 | } | |
2de121f0 | 1497 | |
7387863d DDAG |
1498 | if (req->se->op.removexattr) { |
1499 | req->se->op.removexattr(req, nodeid, name); | |
1500 | } else { | |
1501 | fuse_reply_err(req, ENOSYS); | |
1502 | } | |
2de121f0 DDAG |
1503 | } |
1504 | ||
1505 | static void convert_fuse_file_lock(struct fuse_file_lock *fl, | |
7387863d | 1506 | struct flock *flock) |
2de121f0 | 1507 | { |
7387863d DDAG |
1508 | memset(flock, 0, sizeof(struct flock)); |
1509 | flock->l_type = fl->type; | |
1510 | flock->l_whence = SEEK_SET; | |
1511 | flock->l_start = fl->start; | |
1512 | if (fl->end == OFFSET_MAX) { | |
1513 | flock->l_len = 0; | |
1514 | } else { | |
1515 | flock->l_len = fl->end - fl->start + 1; | |
1516 | } | |
1517 | flock->l_pid = fl->pid; | |
2de121f0 DDAG |
1518 | } |
1519 | ||
70995754 SH |
1520 | static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, |
1521 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1522 | { |
70995754 | 1523 | struct fuse_lk_in *arg; |
7387863d DDAG |
1524 | struct fuse_file_info fi; |
1525 | struct flock flock; | |
2de121f0 | 1526 | |
70995754 SH |
1527 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1528 | if (!arg) { | |
1529 | fuse_reply_err(req, EINVAL); | |
1530 | return; | |
1531 | } | |
1532 | ||
7387863d DDAG |
1533 | memset(&fi, 0, sizeof(fi)); |
1534 | fi.fh = arg->fh; | |
1535 | fi.lock_owner = arg->owner; | |
2de121f0 | 1536 | |
7387863d DDAG |
1537 | convert_fuse_file_lock(&arg->lk, &flock); |
1538 | if (req->se->op.getlk) { | |
1539 | req->se->op.getlk(req, nodeid, &fi, &flock); | |
1540 | } else { | |
1541 | fuse_reply_err(req, ENOSYS); | |
1542 | } | |
2de121f0 DDAG |
1543 | } |
1544 | ||
1545 | static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid, | |
70995754 | 1546 | struct fuse_mbuf_iter *iter, int sleep) |
7387863d | 1547 | { |
70995754 | 1548 | struct fuse_lk_in *arg; |
7387863d DDAG |
1549 | struct fuse_file_info fi; |
1550 | struct flock flock; | |
1551 | ||
70995754 SH |
1552 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1553 | if (!arg) { | |
1554 | fuse_reply_err(req, EINVAL); | |
1555 | return; | |
1556 | } | |
1557 | ||
7387863d DDAG |
1558 | memset(&fi, 0, sizeof(fi)); |
1559 | fi.fh = arg->fh; | |
1560 | fi.lock_owner = arg->owner; | |
1561 | ||
1562 | if (arg->lk_flags & FUSE_LK_FLOCK) { | |
1563 | int op = 0; | |
1564 | ||
1565 | switch (arg->lk.type) { | |
1566 | case F_RDLCK: | |
1567 | op = LOCK_SH; | |
1568 | break; | |
1569 | case F_WRLCK: | |
1570 | op = LOCK_EX; | |
1571 | break; | |
1572 | case F_UNLCK: | |
1573 | op = LOCK_UN; | |
1574 | break; | |
1575 | } | |
1576 | if (!sleep) { | |
1577 | op |= LOCK_NB; | |
1578 | } | |
1579 | ||
1580 | if (req->se->op.flock) { | |
1581 | req->se->op.flock(req, nodeid, &fi, op); | |
1582 | } else { | |
1583 | fuse_reply_err(req, ENOSYS); | |
1584 | } | |
1585 | } else { | |
1586 | convert_fuse_file_lock(&arg->lk, &flock); | |
1587 | if (req->se->op.setlk) { | |
1588 | req->se->op.setlk(req, nodeid, &fi, &flock, sleep); | |
1589 | } else { | |
1590 | fuse_reply_err(req, ENOSYS); | |
1591 | } | |
1592 | } | |
2de121f0 DDAG |
1593 | } |
1594 | ||
70995754 SH |
1595 | static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, |
1596 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1597 | { |
70995754 | 1598 | do_setlk_common(req, nodeid, iter, 0); |
2de121f0 DDAG |
1599 | } |
1600 | ||
70995754 SH |
1601 | static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, |
1602 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1603 | { |
70995754 | 1604 | do_setlk_common(req, nodeid, iter, 1); |
2de121f0 DDAG |
1605 | } |
1606 | ||
1607 | static int find_interrupted(struct fuse_session *se, struct fuse_req *req) | |
1608 | { | |
7387863d DDAG |
1609 | struct fuse_req *curr; |
1610 | ||
1611 | for (curr = se->list.next; curr != &se->list; curr = curr->next) { | |
1612 | if (curr->unique == req->u.i.unique) { | |
1613 | fuse_interrupt_func_t func; | |
1614 | void *data; | |
1615 | ||
1616 | curr->ctr++; | |
1617 | pthread_mutex_unlock(&se->lock); | |
1618 | ||
1619 | /* Ugh, ugly locking */ | |
1620 | pthread_mutex_lock(&curr->lock); | |
1621 | pthread_mutex_lock(&se->lock); | |
1622 | curr->interrupted = 1; | |
1623 | func = curr->u.ni.func; | |
1624 | data = curr->u.ni.data; | |
1625 | pthread_mutex_unlock(&se->lock); | |
1626 | if (func) { | |
1627 | func(curr, data); | |
1628 | } | |
1629 | pthread_mutex_unlock(&curr->lock); | |
1630 | ||
1631 | pthread_mutex_lock(&se->lock); | |
1632 | curr->ctr--; | |
1633 | if (!curr->ctr) { | |
1634 | destroy_req(curr); | |
1635 | } | |
1636 | ||
1637 | return 1; | |
1638 | } | |
1639 | } | |
1640 | for (curr = se->interrupts.next; curr != &se->interrupts; | |
1641 | curr = curr->next) { | |
1642 | if (curr->u.i.unique == req->u.i.unique) { | |
1643 | return 1; | |
1644 | } | |
1645 | } | |
1646 | return 0; | |
2de121f0 DDAG |
1647 | } |
1648 | ||
70995754 SH |
1649 | static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, |
1650 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1651 | { |
70995754 | 1652 | struct fuse_interrupt_in *arg; |
7387863d | 1653 | struct fuse_session *se = req->se; |
2de121f0 | 1654 | |
7387863d | 1655 | (void)nodeid; |
70995754 SH |
1656 | |
1657 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); | |
1658 | if (!arg) { | |
1659 | fuse_reply_err(req, EINVAL); | |
1660 | return; | |
1661 | } | |
1662 | ||
d240314a EG |
1663 | fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n", |
1664 | (unsigned long long)arg->unique); | |
2de121f0 | 1665 | |
7387863d | 1666 | req->u.i.unique = arg->unique; |
2de121f0 | 1667 | |
7387863d DDAG |
1668 | pthread_mutex_lock(&se->lock); |
1669 | if (find_interrupted(se, req)) { | |
1670 | destroy_req(req); | |
1671 | } else { | |
1672 | list_add_req(req, &se->interrupts); | |
1673 | } | |
1674 | pthread_mutex_unlock(&se->lock); | |
2de121f0 DDAG |
1675 | } |
1676 | ||
1677 | static struct fuse_req *check_interrupt(struct fuse_session *se, | |
7387863d DDAG |
1678 | struct fuse_req *req) |
1679 | { | |
1680 | struct fuse_req *curr; | |
1681 | ||
1682 | for (curr = se->interrupts.next; curr != &se->interrupts; | |
1683 | curr = curr->next) { | |
1684 | if (curr->u.i.unique == req->unique) { | |
1685 | req->interrupted = 1; | |
1686 | list_del_req(curr); | |
1687 | free(curr); | |
1688 | return NULL; | |
1689 | } | |
1690 | } | |
1691 | curr = se->interrupts.next; | |
1692 | if (curr != &se->interrupts) { | |
1693 | list_del_req(curr); | |
1694 | list_init_req(curr); | |
1695 | return curr; | |
1696 | } else { | |
1697 | return NULL; | |
1698 | } | |
2de121f0 DDAG |
1699 | } |
1700 | ||
70995754 SH |
1701 | static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, |
1702 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1703 | { |
70995754 SH |
1704 | struct fuse_bmap_in *arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1705 | ||
1706 | if (!arg) { | |
1707 | fuse_reply_err(req, EINVAL); | |
1708 | return; | |
1709 | } | |
2de121f0 | 1710 | |
7387863d DDAG |
1711 | if (req->se->op.bmap) { |
1712 | req->se->op.bmap(req, nodeid, arg->blocksize, arg->block); | |
1713 | } else { | |
1714 | fuse_reply_err(req, ENOSYS); | |
1715 | } | |
2de121f0 DDAG |
1716 | } |
1717 | ||
70995754 SH |
1718 | static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, |
1719 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1720 | { |
70995754 SH |
1721 | struct fuse_ioctl_in *arg; |
1722 | unsigned int flags; | |
1723 | void *in_buf = NULL; | |
7387863d | 1724 | struct fuse_file_info fi; |
2de121f0 | 1725 | |
70995754 SH |
1726 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1727 | if (!arg) { | |
1728 | fuse_reply_err(req, EINVAL); | |
1729 | return; | |
1730 | } | |
1731 | ||
1732 | flags = arg->flags; | |
7387863d DDAG |
1733 | if (flags & FUSE_IOCTL_DIR && !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) { |
1734 | fuse_reply_err(req, ENOTTY); | |
1735 | return; | |
1736 | } | |
2de121f0 | 1737 | |
70995754 SH |
1738 | if (arg->in_size) { |
1739 | in_buf = fuse_mbuf_iter_advance(iter, arg->in_size); | |
1740 | if (!in_buf) { | |
1741 | fuse_reply_err(req, EINVAL); | |
1742 | return; | |
1743 | } | |
1744 | } | |
1745 | ||
7387863d DDAG |
1746 | memset(&fi, 0, sizeof(fi)); |
1747 | fi.fh = arg->fh; | |
2de121f0 | 1748 | |
72c42e2d | 1749 | if (sizeof(void *) == 4 && !(flags & FUSE_IOCTL_32BIT)) { |
7387863d DDAG |
1750 | req->ioctl_64bit = 1; |
1751 | } | |
2de121f0 | 1752 | |
7387863d DDAG |
1753 | if (req->se->op.ioctl) { |
1754 | req->se->op.ioctl(req, nodeid, arg->cmd, (void *)(uintptr_t)arg->arg, | |
1755 | &fi, flags, in_buf, arg->in_size, arg->out_size); | |
1756 | } else { | |
1757 | fuse_reply_err(req, ENOSYS); | |
1758 | } | |
2de121f0 DDAG |
1759 | } |
1760 | ||
1761 | void fuse_pollhandle_destroy(struct fuse_pollhandle *ph) | |
1762 | { | |
7387863d | 1763 | free(ph); |
2de121f0 DDAG |
1764 | } |
1765 | ||
70995754 SH |
1766 | static void do_poll(fuse_req_t req, fuse_ino_t nodeid, |
1767 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1768 | { |
70995754 | 1769 | struct fuse_poll_in *arg; |
7387863d | 1770 | struct fuse_file_info fi; |
2de121f0 | 1771 | |
70995754 SH |
1772 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1773 | if (!arg) { | |
1774 | fuse_reply_err(req, EINVAL); | |
1775 | return; | |
1776 | } | |
1777 | ||
7387863d DDAG |
1778 | memset(&fi, 0, sizeof(fi)); |
1779 | fi.fh = arg->fh; | |
1780 | fi.poll_events = arg->events; | |
2de121f0 | 1781 | |
7387863d DDAG |
1782 | if (req->se->op.poll) { |
1783 | struct fuse_pollhandle *ph = NULL; | |
2de121f0 | 1784 | |
7387863d DDAG |
1785 | if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) { |
1786 | ph = malloc(sizeof(struct fuse_pollhandle)); | |
1787 | if (ph == NULL) { | |
1788 | fuse_reply_err(req, ENOMEM); | |
1789 | return; | |
1790 | } | |
1791 | ph->kh = arg->kh; | |
1792 | ph->se = req->se; | |
1793 | } | |
2de121f0 | 1794 | |
7387863d DDAG |
1795 | req->se->op.poll(req, nodeid, &fi, ph); |
1796 | } else { | |
1797 | fuse_reply_err(req, ENOSYS); | |
1798 | } | |
2de121f0 DDAG |
1799 | } |
1800 | ||
70995754 SH |
1801 | static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, |
1802 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1803 | { |
70995754 | 1804 | struct fuse_fallocate_in *arg; |
7387863d | 1805 | struct fuse_file_info fi; |
2de121f0 | 1806 | |
70995754 SH |
1807 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1808 | if (!arg) { | |
1809 | fuse_reply_err(req, EINVAL); | |
1810 | return; | |
1811 | } | |
1812 | ||
7387863d DDAG |
1813 | memset(&fi, 0, sizeof(fi)); |
1814 | fi.fh = arg->fh; | |
2de121f0 | 1815 | |
7387863d DDAG |
1816 | if (req->se->op.fallocate) { |
1817 | req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, | |
1818 | &fi); | |
1819 | } else { | |
1820 | fuse_reply_err(req, ENOSYS); | |
1821 | } | |
2de121f0 DDAG |
1822 | } |
1823 | ||
7387863d | 1824 | static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in, |
70995754 | 1825 | struct fuse_mbuf_iter *iter) |
2de121f0 | 1826 | { |
70995754 | 1827 | struct fuse_copy_file_range_in *arg; |
7387863d | 1828 | struct fuse_file_info fi_in, fi_out; |
2de121f0 | 1829 | |
70995754 SH |
1830 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1831 | if (!arg) { | |
1832 | fuse_reply_err(req, EINVAL); | |
1833 | return; | |
1834 | } | |
1835 | ||
7387863d DDAG |
1836 | memset(&fi_in, 0, sizeof(fi_in)); |
1837 | fi_in.fh = arg->fh_in; | |
2de121f0 | 1838 | |
7387863d DDAG |
1839 | memset(&fi_out, 0, sizeof(fi_out)); |
1840 | fi_out.fh = arg->fh_out; | |
2de121f0 DDAG |
1841 | |
1842 | ||
7387863d DDAG |
1843 | if (req->se->op.copy_file_range) { |
1844 | req->se->op.copy_file_range(req, nodeid_in, arg->off_in, &fi_in, | |
1845 | arg->nodeid_out, arg->off_out, &fi_out, | |
1846 | arg->len, arg->flags); | |
1847 | } else { | |
1848 | fuse_reply_err(req, ENOSYS); | |
1849 | } | |
2de121f0 DDAG |
1850 | } |
1851 | ||
70995754 SH |
1852 | static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, |
1853 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1854 | { |
70995754 | 1855 | struct fuse_lseek_in *arg; |
7387863d | 1856 | struct fuse_file_info fi; |
2de121f0 | 1857 | |
70995754 SH |
1858 | arg = fuse_mbuf_iter_advance(iter, sizeof(*arg)); |
1859 | if (!arg) { | |
1860 | fuse_reply_err(req, EINVAL); | |
1861 | return; | |
1862 | } | |
7387863d DDAG |
1863 | memset(&fi, 0, sizeof(fi)); |
1864 | fi.fh = arg->fh; | |
2de121f0 | 1865 | |
7387863d DDAG |
1866 | if (req->se->op.lseek) { |
1867 | req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi); | |
1868 | } else { | |
1869 | fuse_reply_err(req, ENOSYS); | |
1870 | } | |
2de121f0 DDAG |
1871 | } |
1872 | ||
70995754 SH |
1873 | static void do_init(fuse_req_t req, fuse_ino_t nodeid, |
1874 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 1875 | { |
70995754 SH |
1876 | size_t compat_size = offsetof(struct fuse_init_in, max_readahead); |
1877 | struct fuse_init_in *arg; | |
7387863d DDAG |
1878 | struct fuse_init_out outarg; |
1879 | struct fuse_session *se = req->se; | |
1880 | size_t bufsize = se->bufsize; | |
1881 | size_t outargsize = sizeof(outarg); | |
1882 | ||
1883 | (void)nodeid; | |
70995754 SH |
1884 | |
1885 | /* First consume the old fields... */ | |
1886 | arg = fuse_mbuf_iter_advance(iter, compat_size); | |
1887 | if (!arg) { | |
1888 | fuse_reply_err(req, EINVAL); | |
1889 | return; | |
1890 | } | |
1891 | ||
1892 | /* ...and now consume the new fields. */ | |
1893 | if (arg->major == 7 && arg->minor >= 6) { | |
1894 | if (!fuse_mbuf_iter_advance(iter, sizeof(*arg) - compat_size)) { | |
1895 | fuse_reply_err(req, EINVAL); | |
1896 | return; | |
1897 | } | |
1898 | } | |
1899 | ||
d240314a EG |
1900 | fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor); |
1901 | if (arg->major == 7 && arg->minor >= 6) { | |
1902 | fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags); | |
1903 | fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n", arg->max_readahead); | |
7387863d DDAG |
1904 | } |
1905 | se->conn.proto_major = arg->major; | |
1906 | se->conn.proto_minor = arg->minor; | |
1907 | se->conn.capable = 0; | |
1908 | se->conn.want = 0; | |
1909 | ||
1910 | memset(&outarg, 0, sizeof(outarg)); | |
1911 | outarg.major = FUSE_KERNEL_VERSION; | |
1912 | outarg.minor = FUSE_KERNEL_MINOR_VERSION; | |
1913 | ||
72c42e2d | 1914 | if (arg->major < 7 || (arg->major == 7 && arg->minor < 31)) { |
7387863d DDAG |
1915 | fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n", |
1916 | arg->major, arg->minor); | |
1917 | fuse_reply_err(req, EPROTO); | |
1918 | return; | |
1919 | } | |
1920 | ||
1921 | if (arg->major > 7) { | |
1922 | /* Wait for a second INIT request with a 7.X version */ | |
1923 | send_reply_ok(req, &outarg, sizeof(outarg)); | |
1924 | return; | |
1925 | } | |
1926 | ||
72c42e2d DDAG |
1927 | if (arg->max_readahead < se->conn.max_readahead) { |
1928 | se->conn.max_readahead = arg->max_readahead; | |
1929 | } | |
1930 | if (arg->flags & FUSE_ASYNC_READ) { | |
1931 | se->conn.capable |= FUSE_CAP_ASYNC_READ; | |
1932 | } | |
1933 | if (arg->flags & FUSE_POSIX_LOCKS) { | |
1934 | se->conn.capable |= FUSE_CAP_POSIX_LOCKS; | |
1935 | } | |
1936 | if (arg->flags & FUSE_ATOMIC_O_TRUNC) { | |
1937 | se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC; | |
1938 | } | |
1939 | if (arg->flags & FUSE_EXPORT_SUPPORT) { | |
1940 | se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT; | |
1941 | } | |
1942 | if (arg->flags & FUSE_DONT_MASK) { | |
1943 | se->conn.capable |= FUSE_CAP_DONT_MASK; | |
1944 | } | |
1945 | if (arg->flags & FUSE_FLOCK_LOCKS) { | |
1946 | se->conn.capable |= FUSE_CAP_FLOCK_LOCKS; | |
1947 | } | |
1948 | if (arg->flags & FUSE_AUTO_INVAL_DATA) { | |
1949 | se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA; | |
1950 | } | |
1951 | if (arg->flags & FUSE_DO_READDIRPLUS) { | |
1952 | se->conn.capable |= FUSE_CAP_READDIRPLUS; | |
1953 | } | |
1954 | if (arg->flags & FUSE_READDIRPLUS_AUTO) { | |
1955 | se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO; | |
1956 | } | |
1957 | if (arg->flags & FUSE_ASYNC_DIO) { | |
1958 | se->conn.capable |= FUSE_CAP_ASYNC_DIO; | |
1959 | } | |
1960 | if (arg->flags & FUSE_WRITEBACK_CACHE) { | |
1961 | se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE; | |
1962 | } | |
1963 | if (arg->flags & FUSE_NO_OPEN_SUPPORT) { | |
1964 | se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT; | |
1965 | } | |
1966 | if (arg->flags & FUSE_PARALLEL_DIROPS) { | |
1967 | se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS; | |
1968 | } | |
1969 | if (arg->flags & FUSE_POSIX_ACL) { | |
1970 | se->conn.capable |= FUSE_CAP_POSIX_ACL; | |
1971 | } | |
1972 | if (arg->flags & FUSE_HANDLE_KILLPRIV) { | |
1973 | se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV; | |
1974 | } | |
1975 | if (arg->flags & FUSE_NO_OPENDIR_SUPPORT) { | |
1976 | se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT; | |
1977 | } | |
1978 | if (!(arg->flags & FUSE_MAX_PAGES)) { | |
1979 | size_t max_bufsize = FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() + | |
1980 | FUSE_BUFFER_HEADER_SIZE; | |
1981 | if (bufsize > max_bufsize) { | |
1982 | bufsize = max_bufsize; | |
7387863d | 1983 | } |
7387863d | 1984 | } |
2de121f0 DDAG |
1985 | #ifdef HAVE_SPLICE |
1986 | #ifdef HAVE_VMSPLICE | |
72c42e2d | 1987 | se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE; |
2de121f0 | 1988 | #endif |
72c42e2d | 1989 | se->conn.capable |= FUSE_CAP_SPLICE_READ; |
2de121f0 | 1990 | #endif |
72c42e2d | 1991 | se->conn.capable |= FUSE_CAP_IOCTL_DIR; |
7387863d DDAG |
1992 | |
1993 | /* | |
1994 | * Default settings for modern filesystems. | |
1995 | * | |
1996 | * Most of these capabilities were disabled by default in | |
1997 | * libfuse2 for backwards compatibility reasons. In libfuse3, | |
1998 | * we can finally enable them by default (as long as they're | |
1999 | * supported by the kernel). | |
2000 | */ | |
2001 | #define LL_SET_DEFAULT(cond, cap) \ | |
2002 | if ((cond) && (se->conn.capable & (cap))) \ | |
2003 | se->conn.want |= (cap) | |
2004 | LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ); | |
2005 | LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS); | |
2006 | LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA); | |
2007 | LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV); | |
2008 | LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO); | |
2009 | LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR); | |
2010 | LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC); | |
2011 | LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ); | |
2012 | LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS); | |
2013 | LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS); | |
2014 | LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS); | |
2015 | LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir, | |
2016 | FUSE_CAP_READDIRPLUS_AUTO); | |
2017 | se->conn.time_gran = 1; | |
2018 | ||
2019 | if (bufsize < FUSE_MIN_READ_BUFFER) { | |
2020 | fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n", | |
2021 | bufsize); | |
2022 | bufsize = FUSE_MIN_READ_BUFFER; | |
2023 | } | |
2024 | se->bufsize = bufsize; | |
2025 | ||
2026 | if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) { | |
2027 | se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE; | |
2028 | } | |
2029 | ||
2030 | se->got_init = 1; | |
c806d643 | 2031 | se->got_destroy = 0; |
7387863d DDAG |
2032 | if (se->op.init) { |
2033 | se->op.init(se->userdata, &se->conn); | |
2034 | } | |
2035 | ||
2036 | if (se->conn.want & (~se->conn.capable)) { | |
2037 | fuse_log(FUSE_LOG_ERR, | |
2038 | "fuse: error: filesystem requested capabilities " | |
2039 | "0x%x that are not supported by kernel, aborting.\n", | |
2040 | se->conn.want & (~se->conn.capable)); | |
2041 | fuse_reply_err(req, EPROTO); | |
2042 | se->error = -EPROTO; | |
2043 | fuse_session_exit(se); | |
2044 | return; | |
2045 | } | |
2046 | ||
2047 | if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) { | |
2048 | se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE; | |
2049 | } | |
2050 | if (arg->flags & FUSE_MAX_PAGES) { | |
2051 | outarg.flags |= FUSE_MAX_PAGES; | |
2052 | outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1; | |
2053 | } | |
2054 | ||
2055 | /* | |
2056 | * Always enable big writes, this is superseded | |
2057 | * by the max_write option | |
2058 | */ | |
2059 | outarg.flags |= FUSE_BIG_WRITES; | |
2060 | ||
2061 | if (se->conn.want & FUSE_CAP_ASYNC_READ) { | |
2062 | outarg.flags |= FUSE_ASYNC_READ; | |
2063 | } | |
2064 | if (se->conn.want & FUSE_CAP_POSIX_LOCKS) { | |
2065 | outarg.flags |= FUSE_POSIX_LOCKS; | |
2066 | } | |
2067 | if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) { | |
2068 | outarg.flags |= FUSE_ATOMIC_O_TRUNC; | |
2069 | } | |
2070 | if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) { | |
2071 | outarg.flags |= FUSE_EXPORT_SUPPORT; | |
2072 | } | |
2073 | if (se->conn.want & FUSE_CAP_DONT_MASK) { | |
2074 | outarg.flags |= FUSE_DONT_MASK; | |
2075 | } | |
2076 | if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) { | |
2077 | outarg.flags |= FUSE_FLOCK_LOCKS; | |
2078 | } | |
2079 | if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) { | |
2080 | outarg.flags |= FUSE_AUTO_INVAL_DATA; | |
2081 | } | |
2082 | if (se->conn.want & FUSE_CAP_READDIRPLUS) { | |
2083 | outarg.flags |= FUSE_DO_READDIRPLUS; | |
2084 | } | |
2085 | if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) { | |
2086 | outarg.flags |= FUSE_READDIRPLUS_AUTO; | |
2087 | } | |
2088 | if (se->conn.want & FUSE_CAP_ASYNC_DIO) { | |
2089 | outarg.flags |= FUSE_ASYNC_DIO; | |
2090 | } | |
2091 | if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) { | |
2092 | outarg.flags |= FUSE_WRITEBACK_CACHE; | |
2093 | } | |
2094 | if (se->conn.want & FUSE_CAP_POSIX_ACL) { | |
2095 | outarg.flags |= FUSE_POSIX_ACL; | |
2096 | } | |
2097 | outarg.max_readahead = se->conn.max_readahead; | |
2098 | outarg.max_write = se->conn.max_write; | |
72c42e2d DDAG |
2099 | if (se->conn.max_background >= (1 << 16)) { |
2100 | se->conn.max_background = (1 << 16) - 1; | |
2101 | } | |
2102 | if (se->conn.congestion_threshold > se->conn.max_background) { | |
2103 | se->conn.congestion_threshold = se->conn.max_background; | |
7387863d | 2104 | } |
72c42e2d DDAG |
2105 | if (!se->conn.congestion_threshold) { |
2106 | se->conn.congestion_threshold = se->conn.max_background * 3 / 4; | |
7387863d DDAG |
2107 | } |
2108 | ||
72c42e2d DDAG |
2109 | outarg.max_background = se->conn.max_background; |
2110 | outarg.congestion_threshold = se->conn.congestion_threshold; | |
2111 | outarg.time_gran = se->conn.time_gran; | |
2112 | ||
d240314a EG |
2113 | fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, outarg.minor); |
2114 | fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags); | |
2115 | fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n", outarg.max_readahead); | |
2116 | fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write); | |
2117 | fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n", outarg.max_background); | |
2118 | fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n", | |
2119 | outarg.congestion_threshold); | |
2120 | fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran); | |
7387863d DDAG |
2121 | |
2122 | send_reply_ok(req, &outarg, outargsize); | |
2de121f0 DDAG |
2123 | } |
2124 | ||
70995754 SH |
2125 | static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, |
2126 | struct fuse_mbuf_iter *iter) | |
2de121f0 | 2127 | { |
7387863d | 2128 | struct fuse_session *se = req->se; |
2de121f0 | 2129 | |
7387863d | 2130 | (void)nodeid; |
70995754 | 2131 | (void)iter; |
2de121f0 | 2132 | |
7387863d | 2133 | se->got_destroy = 1; |
c806d643 | 2134 | se->got_init = 0; |
7387863d DDAG |
2135 | if (se->op.destroy) { |
2136 | se->op.destroy(se->userdata); | |
2137 | } | |
2de121f0 | 2138 | |
7387863d | 2139 | send_reply_ok(req, NULL, 0); |
2de121f0 DDAG |
2140 | } |
2141 | ||
2de121f0 | 2142 | static int send_notify_iov(struct fuse_session *se, int notify_code, |
7387863d | 2143 | struct iovec *iov, int count) |
2de121f0 | 2144 | { |
3db2876a SH |
2145 | struct fuse_out_header out = { |
2146 | .error = notify_code, | |
2147 | }; | |
2de121f0 | 2148 | |
7387863d DDAG |
2149 | if (!se->got_init) { |
2150 | return -ENOTCONN; | |
2151 | } | |
2de121f0 | 2152 | |
7387863d DDAG |
2153 | iov[0].iov_base = &out; |
2154 | iov[0].iov_len = sizeof(struct fuse_out_header); | |
2de121f0 | 2155 | |
7387863d | 2156 | return fuse_send_msg(se, NULL, iov, count); |
2de121f0 DDAG |
2157 | } |
2158 | ||
2159 | int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph) | |
2160 | { | |
7387863d | 2161 | if (ph != NULL) { |
3db2876a SH |
2162 | struct fuse_notify_poll_wakeup_out outarg = { |
2163 | .kh = ph->kh, | |
2164 | }; | |
7387863d | 2165 | struct iovec iov[2]; |
2de121f0 | 2166 | |
7387863d DDAG |
2167 | iov[1].iov_base = &outarg; |
2168 | iov[1].iov_len = sizeof(outarg); | |
2de121f0 | 2169 | |
7387863d DDAG |
2170 | return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2); |
2171 | } else { | |
2172 | return 0; | |
2173 | } | |
2de121f0 DDAG |
2174 | } |
2175 | ||
2176 | int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, | |
7387863d | 2177 | off_t off, off_t len) |
2de121f0 | 2178 | { |
3db2876a SH |
2179 | struct fuse_notify_inval_inode_out outarg = { |
2180 | .ino = ino, | |
2181 | .off = off, | |
2182 | .len = len, | |
2183 | }; | |
7387863d DDAG |
2184 | struct iovec iov[2]; |
2185 | ||
2186 | if (!se) { | |
2187 | return -EINVAL; | |
2188 | } | |
2de121f0 | 2189 | |
7387863d DDAG |
2190 | iov[1].iov_base = &outarg; |
2191 | iov[1].iov_len = sizeof(outarg); | |
2de121f0 | 2192 | |
7387863d | 2193 | return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2); |
2de121f0 DDAG |
2194 | } |
2195 | ||
2196 | int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, | |
7387863d | 2197 | const char *name, size_t namelen) |
2de121f0 | 2198 | { |
3db2876a SH |
2199 | struct fuse_notify_inval_entry_out outarg = { |
2200 | .parent = parent, | |
2201 | .namelen = namelen, | |
2202 | }; | |
7387863d DDAG |
2203 | struct iovec iov[3]; |
2204 | ||
2205 | if (!se) { | |
2206 | return -EINVAL; | |
2207 | } | |
2de121f0 | 2208 | |
7387863d DDAG |
2209 | iov[1].iov_base = &outarg; |
2210 | iov[1].iov_len = sizeof(outarg); | |
2211 | iov[2].iov_base = (void *)name; | |
2212 | iov[2].iov_len = namelen + 1; | |
2de121f0 | 2213 | |
7387863d | 2214 | return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3); |
2de121f0 DDAG |
2215 | } |
2216 | ||
7387863d DDAG |
2217 | int fuse_lowlevel_notify_delete(struct fuse_session *se, fuse_ino_t parent, |
2218 | fuse_ino_t child, const char *name, | |
2219 | size_t namelen) | |
2de121f0 | 2220 | { |
3db2876a SH |
2221 | struct fuse_notify_delete_out outarg = { |
2222 | .parent = parent, | |
2223 | .child = child, | |
2224 | .namelen = namelen, | |
2225 | }; | |
7387863d | 2226 | struct iovec iov[3]; |
2de121f0 | 2227 | |
7387863d DDAG |
2228 | if (!se) { |
2229 | return -EINVAL; | |
2230 | } | |
2de121f0 | 2231 | |
7387863d DDAG |
2232 | iov[1].iov_base = &outarg; |
2233 | iov[1].iov_len = sizeof(outarg); | |
2234 | iov[2].iov_base = (void *)name; | |
2235 | iov[2].iov_len = namelen + 1; | |
2de121f0 | 2236 | |
7387863d | 2237 | return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3); |
2de121f0 DDAG |
2238 | } |
2239 | ||
2240 | int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino, | |
8c3fe75e | 2241 | off_t offset, struct fuse_bufvec *bufv) |
2de121f0 | 2242 | { |
3db2876a SH |
2243 | struct fuse_out_header out = { |
2244 | .error = FUSE_NOTIFY_STORE, | |
2245 | }; | |
2246 | struct fuse_notify_store_out outarg = { | |
2247 | .nodeid = ino, | |
2248 | .offset = offset, | |
2249 | .size = fuse_buf_size(bufv), | |
2250 | }; | |
7387863d | 2251 | struct iovec iov[3]; |
7387863d | 2252 | int res; |
2de121f0 | 2253 | |
7387863d DDAG |
2254 | if (!se) { |
2255 | return -EINVAL; | |
2256 | } | |
2de121f0 | 2257 | |
7387863d DDAG |
2258 | iov[0].iov_base = &out; |
2259 | iov[0].iov_len = sizeof(out); | |
2260 | iov[1].iov_base = &outarg; | |
2261 | iov[1].iov_len = sizeof(outarg); | |
2de121f0 | 2262 | |
8c3fe75e | 2263 | res = fuse_send_data_iov(se, NULL, iov, 2, bufv); |
7387863d DDAG |
2264 | if (res > 0) { |
2265 | res = -res; | |
2266 | } | |
2de121f0 | 2267 | |
7387863d | 2268 | return res; |
2de121f0 DDAG |
2269 | } |
2270 | ||
2de121f0 DDAG |
2271 | void *fuse_req_userdata(fuse_req_t req) |
2272 | { | |
7387863d | 2273 | return req->se->userdata; |
2de121f0 DDAG |
2274 | } |
2275 | ||
2276 | const struct fuse_ctx *fuse_req_ctx(fuse_req_t req) | |
2277 | { | |
7387863d | 2278 | return &req->ctx; |
2de121f0 DDAG |
2279 | } |
2280 | ||
2281 | void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, | |
7387863d | 2282 | void *data) |
2de121f0 | 2283 | { |
7387863d DDAG |
2284 | pthread_mutex_lock(&req->lock); |
2285 | pthread_mutex_lock(&req->se->lock); | |
2286 | req->u.ni.func = func; | |
2287 | req->u.ni.data = data; | |
2288 | pthread_mutex_unlock(&req->se->lock); | |
2289 | if (req->interrupted && func) { | |
2290 | func(req, data); | |
2291 | } | |
2292 | pthread_mutex_unlock(&req->lock); | |
2de121f0 DDAG |
2293 | } |
2294 | ||
2295 | int fuse_req_interrupted(fuse_req_t req) | |
2296 | { | |
7387863d | 2297 | int interrupted; |
2de121f0 | 2298 | |
7387863d DDAG |
2299 | pthread_mutex_lock(&req->se->lock); |
2300 | interrupted = req->interrupted; | |
2301 | pthread_mutex_unlock(&req->se->lock); | |
2de121f0 | 2302 | |
7387863d | 2303 | return interrupted; |
2de121f0 DDAG |
2304 | } |
2305 | ||
2306 | static struct { | |
70995754 | 2307 | void (*func)(fuse_req_t, fuse_ino_t, struct fuse_mbuf_iter *); |
7387863d | 2308 | const char *name; |
2de121f0 | 2309 | } fuse_ll_ops[] = { |
7387863d DDAG |
2310 | [FUSE_LOOKUP] = { do_lookup, "LOOKUP" }, |
2311 | [FUSE_FORGET] = { do_forget, "FORGET" }, | |
2312 | [FUSE_GETATTR] = { do_getattr, "GETATTR" }, | |
2313 | [FUSE_SETATTR] = { do_setattr, "SETATTR" }, | |
2314 | [FUSE_READLINK] = { do_readlink, "READLINK" }, | |
2315 | [FUSE_SYMLINK] = { do_symlink, "SYMLINK" }, | |
2316 | [FUSE_MKNOD] = { do_mknod, "MKNOD" }, | |
2317 | [FUSE_MKDIR] = { do_mkdir, "MKDIR" }, | |
2318 | [FUSE_UNLINK] = { do_unlink, "UNLINK" }, | |
2319 | [FUSE_RMDIR] = { do_rmdir, "RMDIR" }, | |
2320 | [FUSE_RENAME] = { do_rename, "RENAME" }, | |
2321 | [FUSE_LINK] = { do_link, "LINK" }, | |
2322 | [FUSE_OPEN] = { do_open, "OPEN" }, | |
2323 | [FUSE_READ] = { do_read, "READ" }, | |
2324 | [FUSE_WRITE] = { do_write, "WRITE" }, | |
2325 | [FUSE_STATFS] = { do_statfs, "STATFS" }, | |
2326 | [FUSE_RELEASE] = { do_release, "RELEASE" }, | |
2327 | [FUSE_FSYNC] = { do_fsync, "FSYNC" }, | |
2328 | [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" }, | |
2329 | [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" }, | |
2330 | [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" }, | |
2331 | [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" }, | |
2332 | [FUSE_FLUSH] = { do_flush, "FLUSH" }, | |
2333 | [FUSE_INIT] = { do_init, "INIT" }, | |
2334 | [FUSE_OPENDIR] = { do_opendir, "OPENDIR" }, | |
2335 | [FUSE_READDIR] = { do_readdir, "READDIR" }, | |
2336 | [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" }, | |
2337 | [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" }, | |
2338 | [FUSE_GETLK] = { do_getlk, "GETLK" }, | |
2339 | [FUSE_SETLK] = { do_setlk, "SETLK" }, | |
2340 | [FUSE_SETLKW] = { do_setlkw, "SETLKW" }, | |
2341 | [FUSE_ACCESS] = { do_access, "ACCESS" }, | |
2342 | [FUSE_CREATE] = { do_create, "CREATE" }, | |
2343 | [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" }, | |
2344 | [FUSE_BMAP] = { do_bmap, "BMAP" }, | |
2345 | [FUSE_IOCTL] = { do_ioctl, "IOCTL" }, | |
2346 | [FUSE_POLL] = { do_poll, "POLL" }, | |
2347 | [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" }, | |
2348 | [FUSE_DESTROY] = { do_destroy, "DESTROY" }, | |
64c6f408 | 2349 | [FUSE_NOTIFY_REPLY] = { NULL, "NOTIFY_REPLY" }, |
7387863d DDAG |
2350 | [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" }, |
2351 | [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" }, | |
2352 | [FUSE_RENAME2] = { do_rename2, "RENAME2" }, | |
2353 | [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" }, | |
2354 | [FUSE_LSEEK] = { do_lseek, "LSEEK" }, | |
2de121f0 DDAG |
2355 | }; |
2356 | ||
2357 | #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) | |
2358 | ||
2359 | static const char *opname(enum fuse_opcode opcode) | |
2360 | { | |
7387863d DDAG |
2361 | if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) { |
2362 | return "???"; | |
2363 | } else { | |
2364 | return fuse_ll_ops[opcode].name; | |
2365 | } | |
2de121f0 DDAG |
2366 | } |
2367 | ||
2de121f0 | 2368 | void fuse_session_process_buf(struct fuse_session *se, |
7387863d | 2369 | const struct fuse_buf *buf) |
2de121f0 | 2370 | { |
469f9d2f DDAG |
2371 | struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 }; |
2372 | fuse_session_process_buf_int(se, &bufv, NULL); | |
2de121f0 DDAG |
2373 | } |
2374 | ||
469f9d2f DDAG |
2375 | /* |
2376 | * Restriction: | |
2377 | * bufv is normally a single entry buffer, except for a write | |
2378 | * where (if it's in memory) then the bufv may be multiple entries, | |
2379 | * where the first entry contains all headers and subsequent entries | |
2380 | * contain data | |
2381 | * bufv shall not use any offsets etc to make the data anything | |
2382 | * other than contiguous starting from 0. | |
2383 | */ | |
2de121f0 | 2384 | void fuse_session_process_buf_int(struct fuse_session *se, |
469f9d2f | 2385 | struct fuse_bufvec *bufv, |
7387863d DDAG |
2386 | struct fuse_chan *ch) |
2387 | { | |
469f9d2f | 2388 | const struct fuse_buf *buf = bufv->buf; |
0ba8c3c6 | 2389 | struct fuse_mbuf_iter iter = FUSE_MBUF_ITER_INIT(buf); |
7387863d | 2390 | struct fuse_in_header *in; |
7387863d DDAG |
2391 | struct fuse_req *req; |
2392 | int err; | |
2393 | ||
0ba8c3c6 SH |
2394 | /* The first buffer must be a memory buffer */ |
2395 | assert(!(buf->flags & FUSE_BUF_IS_FD)); | |
2396 | ||
2397 | in = fuse_mbuf_iter_advance(&iter, sizeof(*in)); | |
2398 | assert(in); /* caller guarantees the input buffer is large enough */ | |
7387863d | 2399 | |
d240314a EG |
2400 | fuse_log( |
2401 | FUSE_LOG_DEBUG, | |
2402 | "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n", | |
2403 | (unsigned long long)in->unique, opname((enum fuse_opcode)in->opcode), | |
2404 | in->opcode, (unsigned long long)in->nodeid, buf->size, in->pid); | |
7387863d DDAG |
2405 | |
2406 | req = fuse_ll_alloc_req(se); | |
2407 | if (req == NULL) { | |
2408 | struct fuse_out_header out = { | |
2409 | .unique = in->unique, | |
2410 | .error = -ENOMEM, | |
2411 | }; | |
2412 | struct iovec iov = { | |
2413 | .iov_base = &out, | |
2414 | .iov_len = sizeof(struct fuse_out_header), | |
2415 | }; | |
2416 | ||
2417 | fuse_send_msg(se, ch, &iov, 1); | |
2418 | return; | |
2419 | } | |
2420 | ||
2421 | req->unique = in->unique; | |
2422 | req->ctx.uid = in->uid; | |
2423 | req->ctx.gid = in->gid; | |
2424 | req->ctx.pid = in->pid; | |
2425 | req->ch = ch; | |
2426 | ||
2427 | err = EIO; | |
2428 | if (!se->got_init) { | |
2429 | enum fuse_opcode expected; | |
2430 | ||
2431 | expected = se->cuse_data ? CUSE_INIT : FUSE_INIT; | |
2432 | if (in->opcode != expected) { | |
2433 | goto reply_err; | |
2434 | } | |
2435 | } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) { | |
2436 | goto reply_err; | |
2437 | } | |
2438 | ||
2439 | err = EACCES; | |
2440 | /* Implement -o allow_root */ | |
2441 | if (se->deny_others && in->uid != se->owner && in->uid != 0 && | |
2442 | in->opcode != FUSE_INIT && in->opcode != FUSE_READ && | |
2443 | in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC && | |
2444 | in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR && | |
2445 | in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR && | |
2446 | in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) { | |
2447 | goto reply_err; | |
2448 | } | |
2449 | ||
2450 | err = ENOSYS; | |
2451 | if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) { | |
2452 | goto reply_err; | |
2453 | } | |
2454 | if (in->opcode != FUSE_INTERRUPT) { | |
2455 | struct fuse_req *intr; | |
2456 | pthread_mutex_lock(&se->lock); | |
2457 | intr = check_interrupt(se, req); | |
2458 | list_add_req(req, &se->list); | |
2459 | pthread_mutex_unlock(&se->lock); | |
2460 | if (intr) { | |
2461 | fuse_reply_err(intr, EAGAIN); | |
2462 | } | |
2463 | } | |
2464 | ||
7387863d | 2465 | if (in->opcode == FUSE_WRITE && se->op.write_buf) { |
0ba8c3c6 | 2466 | do_write_buf(req, in->nodeid, &iter, bufv); |
7387863d | 2467 | } else { |
70995754 | 2468 | fuse_ll_ops[in->opcode].func(req, in->nodeid, &iter); |
7387863d | 2469 | } |
7387863d | 2470 | return; |
2de121f0 DDAG |
2471 | |
2472 | reply_err: | |
7387863d | 2473 | fuse_reply_err(req, err); |
2de121f0 DDAG |
2474 | } |
2475 | ||
7387863d DDAG |
2476 | #define LL_OPTION(n, o, v) \ |
2477 | { \ | |
2478 | n, offsetof(struct fuse_session, o), v \ | |
2479 | } | |
2de121f0 DDAG |
2480 | |
2481 | static const struct fuse_opt fuse_ll_opts[] = { | |
205de006 DDAG |
2482 | LL_OPTION("debug", debug, 1), |
2483 | LL_OPTION("-d", debug, 1), | |
2484 | LL_OPTION("--debug", debug, 1), | |
2485 | LL_OPTION("allow_root", deny_others, 1), | |
2486 | LL_OPTION("--socket-path=%s", vu_socket_path, 0), | |
cee8e35d | 2487 | LL_OPTION("--fd=%d", vu_listen_fd, 0), |
7387863d | 2488 | FUSE_OPT_END |
2de121f0 DDAG |
2489 | }; |
2490 | ||
2491 | void fuse_lowlevel_version(void) | |
2492 | { | |
7387863d DDAG |
2493 | printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION, |
2494 | FUSE_KERNEL_MINOR_VERSION); | |
2de121f0 DDAG |
2495 | } |
2496 | ||
2497 | void fuse_lowlevel_help(void) | |
2498 | { | |
7387863d DDAG |
2499 | /* |
2500 | * These are not all options, but the ones that are | |
2501 | * potentially of interest to an end-user | |
2502 | */ | |
205de006 DDAG |
2503 | printf( |
2504 | " -o allow_root allow access by root\n" | |
cee8e35d SH |
2505 | " --socket-path=PATH path for the vhost-user socket\n" |
2506 | " --fd=FDNUM fd number of vhost-user socket\n"); | |
2de121f0 DDAG |
2507 | } |
2508 | ||
2509 | void fuse_session_destroy(struct fuse_session *se) | |
2510 | { | |
7387863d DDAG |
2511 | if (se->got_init && !se->got_destroy) { |
2512 | if (se->op.destroy) { | |
2513 | se->op.destroy(se->userdata); | |
2514 | } | |
2515 | } | |
2516 | pthread_mutex_destroy(&se->lock); | |
2517 | free(se->cuse_data); | |
2518 | if (se->fd != -1) { | |
2519 | close(se->fd); | |
2520 | } | |
2521 | free(se); | |
2de121f0 DDAG |
2522 | } |
2523 | ||
2524 | ||
2de121f0 | 2525 | struct fuse_session *fuse_session_new(struct fuse_args *args, |
7387863d DDAG |
2526 | const struct fuse_lowlevel_ops *op, |
2527 | size_t op_size, void *userdata) | |
2528 | { | |
2529 | struct fuse_session *se; | |
2530 | ||
2531 | if (sizeof(struct fuse_lowlevel_ops) < op_size) { | |
2532 | fuse_log( | |
2533 | FUSE_LOG_ERR, | |
2534 | "fuse: warning: library too old, some operations may not work\n"); | |
2535 | op_size = sizeof(struct fuse_lowlevel_ops); | |
2536 | } | |
2537 | ||
2538 | if (args->argc == 0) { | |
2539 | fuse_log(FUSE_LOG_ERR, | |
2540 | "fuse: empty argv passed to fuse_session_new().\n"); | |
2541 | return NULL; | |
2542 | } | |
2543 | ||
2544 | se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session)); | |
2545 | if (se == NULL) { | |
2546 | fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n"); | |
2547 | goto out1; | |
2548 | } | |
2549 | se->fd = -1; | |
cee8e35d | 2550 | se->vu_listen_fd = -1; |
7387863d DDAG |
2551 | se->conn.max_write = UINT_MAX; |
2552 | se->conn.max_readahead = UINT_MAX; | |
2553 | ||
2554 | /* Parse options */ | |
2555 | if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) { | |
2556 | goto out2; | |
2557 | } | |
2558 | if (args->argc == 1 && args->argv[0][0] == '-') { | |
2559 | fuse_log(FUSE_LOG_ERR, | |
2560 | "fuse: warning: argv[0] looks like an option, but " | |
2561 | "will be ignored\n"); | |
2562 | } else if (args->argc != 1) { | |
2563 | int i; | |
2564 | fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `"); | |
2565 | for (i = 1; i < args->argc - 1; i++) { | |
2566 | fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]); | |
2567 | } | |
2568 | fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]); | |
2569 | goto out4; | |
2570 | } | |
2571 | ||
cee8e35d SH |
2572 | if (!se->vu_socket_path && se->vu_listen_fd < 0) { |
2573 | fuse_log(FUSE_LOG_ERR, "fuse: missing --socket-path or --fd option\n"); | |
2574 | goto out4; | |
2575 | } | |
2576 | if (se->vu_socket_path && se->vu_listen_fd >= 0) { | |
2577 | fuse_log(FUSE_LOG_ERR, | |
2578 | "fuse: --socket-path and --fd cannot be given together\n"); | |
d14bf584 DDAG |
2579 | goto out4; |
2580 | } | |
2581 | ||
7387863d DDAG |
2582 | se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE; |
2583 | ||
2584 | list_init_req(&se->list); | |
2585 | list_init_req(&se->interrupts); | |
7387863d DDAG |
2586 | fuse_mutex_init(&se->lock); |
2587 | ||
2588 | memcpy(&se->op, op, op_size); | |
2589 | se->owner = getuid(); | |
2590 | se->userdata = userdata; | |
2591 | ||
2592 | return se; | |
2de121f0 | 2593 | |
2de121f0 | 2594 | out4: |
7387863d | 2595 | fuse_opt_free_args(args); |
2de121f0 | 2596 | out2: |
7387863d | 2597 | free(se); |
2de121f0 | 2598 | out1: |
7387863d | 2599 | return NULL; |
2de121f0 DDAG |
2600 | } |
2601 | ||
67aab022 | 2602 | int fuse_session_mount(struct fuse_session *se) |
2de121f0 | 2603 | { |
d14bf584 | 2604 | return virtio_session_mount(se); |
2de121f0 DDAG |
2605 | } |
2606 | ||
2607 | int fuse_session_fd(struct fuse_session *se) | |
2608 | { | |
7387863d | 2609 | return se->fd; |
2de121f0 DDAG |
2610 | } |
2611 | ||
2612 | void fuse_session_unmount(struct fuse_session *se) | |
2613 | { | |
2de121f0 DDAG |
2614 | } |
2615 | ||
f6f3573c DDAG |
2616 | int fuse_lowlevel_is_virtio(struct fuse_session *se) |
2617 | { | |
cee8e35d | 2618 | return !!se->virtio_dev; |
f6f3573c DDAG |
2619 | } |
2620 | ||
2de121f0 DDAG |
2621 | #ifdef linux |
2622 | int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]) | |
2623 | { | |
7387863d DDAG |
2624 | char *buf; |
2625 | size_t bufsize = 1024; | |
2626 | char path[128]; | |
2627 | int ret; | |
2628 | int fd; | |
2629 | unsigned long pid = req->ctx.pid; | |
2630 | char *s; | |
2de121f0 | 2631 | |
7387863d | 2632 | sprintf(path, "/proc/%lu/task/%lu/status", pid, pid); |
2de121f0 DDAG |
2633 | |
2634 | retry: | |
7387863d DDAG |
2635 | buf = malloc(bufsize); |
2636 | if (buf == NULL) { | |
2637 | return -ENOMEM; | |
2638 | } | |
2639 | ||
2640 | ret = -EIO; | |
2641 | fd = open(path, O_RDONLY); | |
2642 | if (fd == -1) { | |
2643 | goto out_free; | |
2644 | } | |
2645 | ||
2646 | ret = read(fd, buf, bufsize); | |
2647 | close(fd); | |
2648 | if (ret < 0) { | |
2649 | ret = -EIO; | |
2650 | goto out_free; | |
2651 | } | |
2652 | ||
2653 | if ((size_t)ret == bufsize) { | |
2654 | free(buf); | |
2655 | bufsize *= 4; | |
2656 | goto retry; | |
2657 | } | |
2658 | ||
2659 | ret = -EIO; | |
2660 | s = strstr(buf, "\nGroups:"); | |
2661 | if (s == NULL) { | |
2662 | goto out_free; | |
2663 | } | |
2664 | ||
2665 | s += 8; | |
2666 | ret = 0; | |
2667 | while (1) { | |
2668 | char *end; | |
2669 | unsigned long val = strtoul(s, &end, 0); | |
2670 | if (end == s) { | |
2671 | break; | |
2672 | } | |
2673 | ||
2674 | s = end; | |
2675 | if (ret < size) { | |
2676 | list[ret] = val; | |
2677 | } | |
2678 | ret++; | |
2679 | } | |
2de121f0 DDAG |
2680 | |
2681 | out_free: | |
7387863d DDAG |
2682 | free(buf); |
2683 | return ret; | |
2de121f0 DDAG |
2684 | } |
2685 | #else /* linux */ | |
2686 | /* | |
2687 | * This is currently not implemented on other than Linux... | |
2688 | */ | |
2689 | int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]) | |
2690 | { | |
7387863d DDAG |
2691 | (void)req; |
2692 | (void)size; | |
2693 | (void)list; | |
2694 | return -ENOSYS; | |
2de121f0 DDAG |
2695 | } |
2696 | #endif | |
2697 | ||
2698 | void fuse_session_exit(struct fuse_session *se) | |
2699 | { | |
7387863d | 2700 | se->exited = 1; |
2de121f0 DDAG |
2701 | } |
2702 | ||
2703 | void fuse_session_reset(struct fuse_session *se) | |
2704 | { | |
7387863d DDAG |
2705 | se->exited = 0; |
2706 | se->error = 0; | |
2de121f0 DDAG |
2707 | } |
2708 | ||
2709 | int fuse_session_exited(struct fuse_session *se) | |
2710 | { | |
7387863d | 2711 | return se->exited; |
2de121f0 | 2712 | } |