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