• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 */
11 
12 #define _GNU_SOURCE
13 
14 #include "config.h"
15 #include "fuse_i.h"
16 #include "fuse_kernel.h"
17 #include "fuse_opt.h"
18 #include "fuse_misc.h"
19 #include "mount_util.h"
20 
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stddef.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <limits.h>
27 #include <errno.h>
28 #include <assert.h>
29 #include <sys/file.h>
30 #include <sys/ioctl.h>
31 
32 #ifndef F_LINUX_SPECIFIC_BASE
33 #define F_LINUX_SPECIFIC_BASE       1024
34 #endif
35 #ifndef F_SETPIPE_SZ
36 #define F_SETPIPE_SZ	(F_LINUX_SPECIFIC_BASE + 7)
37 #endif
38 
39 
40 #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg)))
41 #define OFFSET_MAX 0x7fffffffffffffffLL
42 
43 #define container_of(ptr, type, member) ({				\
44 			const typeof( ((type *)0)->member ) *__mptr = (ptr); \
45 			(type *)( (char *)__mptr - offsetof(type,member) );})
46 
47 struct fuse_pollhandle {
48 	uint64_t kh;
49 	struct fuse_session *se;
50 };
51 
52 static size_t pagesize;
53 
fuse_ll_init_pagesize(void)54 static __attribute__((constructor)) void fuse_ll_init_pagesize(void)
55 {
56 	pagesize = getpagesize();
57 }
58 
convert_stat(const struct stat * stbuf,struct fuse_attr * attr)59 static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr)
60 {
61 	attr->ino	= stbuf->st_ino;
62 	attr->mode	= stbuf->st_mode;
63 	attr->nlink	= stbuf->st_nlink;
64 	attr->uid	= stbuf->st_uid;
65 	attr->gid	= stbuf->st_gid;
66 	attr->rdev	= stbuf->st_rdev;
67 	attr->size	= stbuf->st_size;
68 	attr->blksize	= stbuf->st_blksize;
69 	attr->blocks	= stbuf->st_blocks;
70 	attr->atime	= stbuf->st_atime;
71 	attr->mtime	= stbuf->st_mtime;
72 	attr->ctime	= stbuf->st_ctime;
73 	attr->atimensec = ST_ATIM_NSEC(stbuf);
74 	attr->mtimensec = ST_MTIM_NSEC(stbuf);
75 	attr->ctimensec = ST_CTIM_NSEC(stbuf);
76 }
77 
convert_attr(const struct fuse_setattr_in * attr,struct stat * stbuf)78 static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf)
79 {
80 	stbuf->st_mode	       = attr->mode;
81 	stbuf->st_uid	       = attr->uid;
82 	stbuf->st_gid	       = attr->gid;
83 	stbuf->st_size	       = attr->size;
84 	stbuf->st_atime	       = attr->atime;
85 	stbuf->st_mtime	       = attr->mtime;
86 	stbuf->st_ctime        = attr->ctime;
87 	ST_ATIM_NSEC_SET(stbuf, attr->atimensec);
88 	ST_MTIM_NSEC_SET(stbuf, attr->mtimensec);
89 	ST_CTIM_NSEC_SET(stbuf, attr->ctimensec);
90 }
91 
iov_length(const struct iovec * iov,size_t count)92 static	size_t iov_length(const struct iovec *iov, size_t count)
93 {
94 	size_t seg;
95 	size_t ret = 0;
96 
97 	for (seg = 0; seg < count; seg++)
98 		ret += iov[seg].iov_len;
99 	return ret;
100 }
101 
list_init_req(struct fuse_req * req)102 static void list_init_req(struct fuse_req *req)
103 {
104 	req->next = req;
105 	req->prev = req;
106 }
107 
list_del_req(struct fuse_req * req)108 static void list_del_req(struct fuse_req *req)
109 {
110 	struct fuse_req *prev = req->prev;
111 	struct fuse_req *next = req->next;
112 	prev->next = next;
113 	next->prev = prev;
114 }
115 
list_add_req(struct fuse_req * req,struct fuse_req * next)116 static void list_add_req(struct fuse_req *req, struct fuse_req *next)
117 {
118 	struct fuse_req *prev = next->prev;
119 	req->next = next;
120 	req->prev = prev;
121 	prev->next = req;
122 	next->prev = req;
123 }
124 
destroy_req(fuse_req_t req)125 static void destroy_req(fuse_req_t req)
126 {
127 	pthread_mutex_destroy(&req->lock);
128 	free(req);
129 }
130 
fuse_free_req(fuse_req_t req)131 void fuse_free_req(fuse_req_t req)
132 {
133 	int ctr;
134 	struct fuse_session *se = req->se;
135 
136 	pthread_mutex_lock(&se->lock);
137 	req->u.ni.func = NULL;
138 	req->u.ni.data = NULL;
139 	list_del_req(req);
140 	ctr = --req->ctr;
141 	fuse_chan_put(req->ch);
142 	req->ch = NULL;
143 	pthread_mutex_unlock(&se->lock);
144 	if (!ctr)
145 		destroy_req(req);
146 }
147 
fuse_ll_alloc_req(struct fuse_session * se)148 static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se)
149 {
150 	struct fuse_req *req;
151 
152 	req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req));
153 	if (req == NULL) {
154 		fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n");
155 	} else {
156 		req->se = se;
157 		req->ctr = 1;
158 		list_init_req(req);
159 		pthread_mutex_init(&req->lock, NULL);
160 	}
161 
162 	return req;
163 }
164 
165 /* Send data. If *ch* is NULL, send via session master fd */
fuse_send_msg(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int count)166 static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch,
167 			 struct iovec *iov, int count)
168 {
169 	struct fuse_out_header *out = iov[0].iov_base;
170 
171 	assert(se != NULL);
172 	out->len = iov_length(iov, count);
173 	if (se->debug) {
174 		if (out->unique == 0) {
175 			fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n",
176 				out->error, out->len);
177 		} else if (out->error) {
178 			fuse_log(FUSE_LOG_DEBUG,
179 				"   unique: %llu, error: %i (%s), outsize: %i\n",
180 				(unsigned long long) out->unique, out->error,
181 				strerror(-out->error), out->len);
182 		} else {
183 			fuse_log(FUSE_LOG_DEBUG,
184 				"   unique: %llu, success, outsize: %i\n",
185 				(unsigned long long) out->unique, out->len);
186 		}
187 	}
188 
189 	ssize_t res = writev(ch ? ch->fd : se->fd,
190 			     iov, count);
191 	int err = errno;
192 
193 	if (res == -1) {
194 		/* ENOENT means the operation was interrupted */
195 		if (!fuse_session_exited(se) && err != ENOENT)
196 			perror("fuse: writing device");
197 		return -err;
198 	}
199 
200 	return 0;
201 }
202 
203 
fuse_send_reply_iov_nofree(fuse_req_t req,int error,struct iovec * iov,int count)204 int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov,
205 			       int count)
206 {
207 	struct fuse_out_header out;
208 
209 	if (error <= -1000 || error > 0) {
210 		fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n",	error);
211 		error = -ERANGE;
212 	}
213 
214 	out.unique = req->unique;
215 	out.error = error;
216 
217 	iov[0].iov_base = &out;
218 	iov[0].iov_len = sizeof(struct fuse_out_header);
219 
220 	return fuse_send_msg(req->se, req->ch, iov, count);
221 }
222 
send_reply_iov(fuse_req_t req,int error,struct iovec * iov,int count)223 static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov,
224 			  int count)
225 {
226 	int res;
227 
228 	res = fuse_send_reply_iov_nofree(req, error, iov, count);
229 	fuse_free_req(req);
230 	return res;
231 }
232 
send_reply(fuse_req_t req,int error,const void * arg,size_t argsize)233 static int send_reply(fuse_req_t req, int error, const void *arg,
234 		      size_t argsize)
235 {
236 	struct iovec iov[2];
237 	int count = 1;
238 	if (argsize) {
239 		iov[1].iov_base = (void *) arg;
240 		iov[1].iov_len = argsize;
241 		count++;
242 	}
243 	return send_reply_iov(req, error, iov, count);
244 }
245 
fuse_reply_iov(fuse_req_t req,const struct iovec * iov,int count)246 int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count)
247 {
248 	int res;
249 	struct iovec *padded_iov;
250 
251 	padded_iov = malloc((count + 1) * sizeof(struct iovec));
252 	if (padded_iov == NULL)
253 		return fuse_reply_err(req, ENOMEM);
254 
255 	memcpy(padded_iov + 1, iov, count * sizeof(struct iovec));
256 	count++;
257 
258 	res = send_reply_iov(req, 0, padded_iov, count);
259 	free(padded_iov);
260 
261 	return res;
262 }
263 
264 
265 /* `buf` is allowed to be empty so that the proper size may be
266    allocated by the caller */
fuse_add_direntry(fuse_req_t req,char * buf,size_t bufsize,const char * name,const struct stat * stbuf,off_t off)267 size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
268 			 const char *name, const struct stat *stbuf, off_t off)
269 {
270 	(void)req;
271 	size_t namelen;
272 	size_t entlen;
273 	size_t entlen_padded;
274 	struct fuse_dirent *dirent;
275 
276 	namelen = strlen(name);
277 	entlen = FUSE_NAME_OFFSET + namelen;
278 	entlen_padded = FUSE_DIRENT_ALIGN(entlen);
279 
280 	if ((buf == NULL) || (entlen_padded > bufsize))
281 	  return entlen_padded;
282 
283 	dirent = (struct fuse_dirent*) buf;
284 	dirent->ino = stbuf->st_ino;
285 	dirent->off = off;
286 	dirent->namelen = namelen;
287 	dirent->type = (stbuf->st_mode & S_IFMT) >> 12;
288 	memcpy(dirent->name, name, namelen);
289 	memset(dirent->name + namelen, 0, entlen_padded - entlen);
290 
291 	return entlen_padded;
292 }
293 
convert_statfs(const struct statvfs * stbuf,struct fuse_kstatfs * kstatfs)294 static void convert_statfs(const struct statvfs *stbuf,
295 			   struct fuse_kstatfs *kstatfs)
296 {
297 	kstatfs->bsize	 = stbuf->f_bsize;
298 	kstatfs->frsize	 = stbuf->f_frsize;
299 	kstatfs->blocks	 = stbuf->f_blocks;
300 	kstatfs->bfree	 = stbuf->f_bfree;
301 	kstatfs->bavail	 = stbuf->f_bavail;
302 	kstatfs->files	 = stbuf->f_files;
303 	kstatfs->ffree	 = stbuf->f_ffree;
304 	kstatfs->namelen = stbuf->f_namemax;
305 }
306 
send_reply_ok(fuse_req_t req,const void * arg,size_t argsize)307 static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize)
308 {
309 	return send_reply(req, 0, arg, argsize);
310 }
311 
fuse_reply_err(fuse_req_t req,int err)312 int fuse_reply_err(fuse_req_t req, int err)
313 {
314 	return send_reply(req, -err, NULL, 0);
315 }
316 
fuse_reply_none(fuse_req_t req)317 void fuse_reply_none(fuse_req_t req)
318 {
319 	fuse_free_req(req);
320 }
321 
calc_timeout_sec(double t)322 static unsigned long calc_timeout_sec(double t)
323 {
324 	if (t > (double) ULONG_MAX)
325 		return ULONG_MAX;
326 	else if (t < 0.0)
327 		return 0;
328 	else
329 		return (unsigned long) t;
330 }
331 
calc_timeout_nsec(double t)332 static unsigned int calc_timeout_nsec(double t)
333 {
334 	double f = t - (double) calc_timeout_sec(t);
335 	if (f < 0.0)
336 		return 0;
337 	else if (f >= 0.999999999)
338 		return 999999999;
339 	else
340 		return (unsigned int) (f * 1.0e9);
341 }
342 
fill_entry(struct fuse_entry_out * arg,const struct fuse_entry_param * e)343 static void fill_entry(struct fuse_entry_out *arg,
344 		       const struct fuse_entry_param *e)
345 {
346 	arg->nodeid = e->ino;
347 	arg->generation = e->generation;
348 	arg->entry_valid = calc_timeout_sec(e->entry_timeout);
349 	arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout);
350 	arg->attr_valid = calc_timeout_sec(e->attr_timeout);
351 	arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout);
352 	convert_stat(&e->attr, &arg->attr);
353 }
354 
355 /* `buf` is allowed to be empty so that the proper size may be
356    allocated by the caller */
fuse_add_direntry_plus(fuse_req_t req,char * buf,size_t bufsize,const char * name,const struct fuse_entry_param * e,off_t off)357 size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize,
358 			      const char *name,
359 			      const struct fuse_entry_param *e, off_t off)
360 {
361 	(void)req;
362 	size_t namelen;
363 	size_t entlen;
364 	size_t entlen_padded;
365 
366 	namelen = strlen(name);
367 	entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen;
368 	entlen_padded = FUSE_DIRENT_ALIGN(entlen);
369 	if ((buf == NULL) || (entlen_padded > bufsize))
370 	  return entlen_padded;
371 
372 	struct fuse_direntplus *dp = (struct fuse_direntplus *) buf;
373 	memset(&dp->entry_out, 0, sizeof(dp->entry_out));
374 	fill_entry(&dp->entry_out, e);
375 
376 	struct fuse_dirent *dirent = &dp->dirent;
377 	dirent->ino = e->attr.st_ino;
378 	dirent->off = off;
379 	dirent->namelen = namelen;
380 	dirent->type = (e->attr.st_mode & S_IFMT) >> 12;
381 	memcpy(dirent->name, name, namelen);
382 	memset(dirent->name + namelen, 0, entlen_padded - entlen);
383 
384 	return entlen_padded;
385 }
386 
fill_open(struct fuse_open_out * arg,const struct fuse_file_info * f)387 static void fill_open(struct fuse_open_out *arg,
388 		      const struct fuse_file_info *f)
389 {
390 	arg->fh = f->fh;
391 	arg->passthrough_fh = f->passthrough_fh;
392 	if (f->direct_io)
393 		arg->open_flags |= FOPEN_DIRECT_IO;
394 	if (f->keep_cache)
395 		arg->open_flags |= FOPEN_KEEP_CACHE;
396 	if (f->cache_readdir)
397 		arg->open_flags |= FOPEN_CACHE_DIR;
398 	if (f->nonseekable)
399 		arg->open_flags |= FOPEN_NONSEEKABLE;
400 }
401 
fuse_reply_entry(fuse_req_t req,const struct fuse_entry_param * e)402 int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param* e) {
403     struct {
404         struct fuse_entry_out arg;
405         struct fuse_entry_bpf_out bpf_arg;
406     } __attribute__((packed)) arg_ext = {0};
407 
408     struct fuse_entry_out arg;
409     struct fuse_entry_bpf_out bpf_arg;
410     size_t size;
411     int extended_args = e->bpf_action || bpf_arg.bpf_fd || e->backing_action || e->backing_fd;
412 
413     if (extended_args) {
414         size = req->se->conn.proto_minor < 9 ? FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg_ext);
415     } else {
416         size = req->se->conn.proto_minor < 9 ? FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(arg);
417     }
418 
419     /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant
420        negative entry */
421     if (!e->ino && req->se->conn.proto_minor < 4) return fuse_reply_err(req, ENOENT);
422 
423     memset(&arg, 0, sizeof(arg));
424 
425     if (extended_args) {
426         memset(&bpf_arg, 0, sizeof(bpf_arg));
427 
428         bpf_arg.bpf_action = e->bpf_action;
429         bpf_arg.bpf_fd = e->bpf_fd;
430         bpf_arg.backing_action = e->backing_action;
431         bpf_arg.backing_fd = e->backing_fd;
432 
433         arg_ext.arg = arg;
434         arg_ext.bpf_arg = bpf_arg;
435 
436         return send_reply_ok(req, &arg_ext, size);
437     } else {
438         fill_entry(&arg, e);
439         return send_reply_ok(req, &arg, size);
440     }
441 }
442 
fuse_reply_create(fuse_req_t req,const struct fuse_entry_param * e,const struct fuse_file_info * f)443 int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e,
444 		      const struct fuse_file_info *f)
445 {
446 	char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)];
447 	size_t entrysize = req->se->conn.proto_minor < 9 ?
448 		FUSE_COMPAT_ENTRY_OUT_SIZE : sizeof(struct fuse_entry_out);
449 	struct fuse_entry_out *earg = (struct fuse_entry_out *) buf;
450 	struct fuse_open_out *oarg = (struct fuse_open_out *) (buf + entrysize);
451 
452 	memset(buf, 0, sizeof(buf));
453 	fill_entry(earg, e);
454 	fill_open(oarg, f);
455 	return send_reply_ok(req, buf,
456 			     entrysize + sizeof(struct fuse_open_out));
457 }
458 
fuse_reply_attr(fuse_req_t req,const struct stat * attr,double attr_timeout)459 int fuse_reply_attr(fuse_req_t req, const struct stat *attr,
460 		    double attr_timeout)
461 {
462 	struct fuse_attr_out arg;
463 	size_t size = req->se->conn.proto_minor < 9 ?
464 		FUSE_COMPAT_ATTR_OUT_SIZE : sizeof(arg);
465 
466 	memset(&arg, 0, sizeof(arg));
467 	arg.attr_valid = calc_timeout_sec(attr_timeout);
468 	arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout);
469 	convert_stat(attr, &arg.attr);
470 
471 	return send_reply_ok(req, &arg, size);
472 }
473 
fuse_reply_readlink(fuse_req_t req,const char * linkname)474 int fuse_reply_readlink(fuse_req_t req, const char *linkname)
475 {
476 	return send_reply_ok(req, linkname, strlen(linkname));
477 }
478 
fuse_reply_canonical_path(fuse_req_t req,const char * path)479 int fuse_reply_canonical_path(fuse_req_t req, const char *path)
480 {
481         // The kernel expects a buffer containing the null terminator for this op
482         // So we add the null terminator size to strlen
483 	return send_reply_ok(req, path, strlen(path) + 1);
484 }
485 
486 enum {
487 	FUSE_PASSTHROUGH_API_UNAVAILABLE,
488 	FUSE_PASSTHROUGH_API_V0,
489 	FUSE_PASSTHROUGH_API_V1,
490 	FUSE_PASSTHROUGH_API_V2,
491 	FUSE_PASSTHROUGH_API_STABLE,
492 };
493 
494 /*
495  * Requests the FUSE passthrough feature to be enabled on a specific file
496  * through the passed fd.
497  * This function returns an identifier that must be used as passthrough_fh
498  * when the open/create_open request reply is sent back to /dev/fuse.
499  * As for the current FUSE passthrough implementation, passthrough_fh values
500  * are only valid if > 0, so in case the FUSE passthrough open ioctl returns
501  * a value <= 0, this must be considered an error and is returned as-is by
502  * this function.
503  */
fuse_passthrough_enable(fuse_req_t req,unsigned int fd)504 int fuse_passthrough_enable(fuse_req_t req, unsigned int fd) {
505 	static sig_atomic_t passthrough_version = FUSE_PASSTHROUGH_API_STABLE;
506 	int ret = 0; /* values <= 0 represent errors in FUSE passthrough */
507 
508 	/*
509 	 * The interface of FUSE passthrough is still unstable in the kernel,
510 	 * so the following solution is to search for the most updated API
511 	 * version and, if not found, fall back to an older one.
512 	 * This happens when ioctl() returns -1 and errno is set to ENOTTY,
513 	 * an error code that corresponds to the lack of a specific ioctl.
514 	 */
515 	switch (passthrough_version) {
516 	case FUSE_PASSTHROUGH_API_STABLE:
517 		/* There is not a stable API yet */
518 		passthrough_version = FUSE_PASSTHROUGH_API_V2;
519 	case FUSE_PASSTHROUGH_API_V2: {
520 		ret = ioctl(req->se->fd, FUSE_DEV_IOC_PASSTHROUGH_OPEN_V2, &fd);
521 		if (ret == -1 && errno == ENOTTY)
522 			passthrough_version = FUSE_PASSTHROUGH_API_V1;
523 		else
524 			break;
525 	}
526 	case FUSE_PASSTHROUGH_API_V1: {
527 		struct fuse_passthrough_out_v0 out = {};
528 		out.fd = fd;
529 
530 		ret = ioctl(req->se->fd, FUSE_DEV_IOC_PASSTHROUGH_OPEN_V1, &out);
531 		if (ret == -1 && errno == ENOTTY)
532 			passthrough_version = FUSE_PASSTHROUGH_API_V0;
533 		else
534 			break;
535 	}
536 	case FUSE_PASSTHROUGH_API_V0: {
537 		struct fuse_passthrough_out_v0 out = {};
538 		out.fd = fd;
539 
540 		ret = ioctl(req->se->fd, FUSE_DEV_IOC_PASSTHROUGH_OPEN_V0, &out);
541 		if (ret == -1 && errno == ENOTTY)
542 			passthrough_version = FUSE_PASSTHROUGH_API_UNAVAILABLE;
543 		else
544 			break;
545 	}
546 	default:
547 		fuse_log(FUSE_LOG_ERR, "fuse: passthrough_enable no valid API\n");
548 		return -ENOTTY;
549 	}
550 
551 	if (ret <= 0)
552 		fuse_log(FUSE_LOG_ERR, "fuse: passthrough_enable: %s\n", strerror(errno));
553 
554 	return ret;
555 }
556 
fuse_reply_open(fuse_req_t req,const struct fuse_file_info * f)557 int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f)
558 {
559 	struct fuse_open_out arg;
560 
561 	memset(&arg, 0, sizeof(arg));
562 	fill_open(&arg, f);
563 	return send_reply_ok(req, &arg, sizeof(arg));
564 }
565 
fuse_reply_write(fuse_req_t req,size_t count)566 int fuse_reply_write(fuse_req_t req, size_t count)
567 {
568 	struct fuse_write_out arg;
569 
570 	memset(&arg, 0, sizeof(arg));
571 	arg.size = count;
572 
573 	return send_reply_ok(req, &arg, sizeof(arg));
574 }
575 
fuse_reply_buf(fuse_req_t req,const char * buf,size_t size)576 int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size)
577 {
578 	return send_reply_ok(req, buf, size);
579 }
580 
fuse_send_data_iov_fallback(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int iov_count,struct fuse_bufvec * buf,size_t len)581 static int fuse_send_data_iov_fallback(struct fuse_session *se,
582 				       struct fuse_chan *ch,
583 				       struct iovec *iov, int iov_count,
584 				       struct fuse_bufvec *buf,
585 				       size_t len)
586 {
587 	struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
588 	void *mbuf;
589 	int res;
590 
591 	/* Optimize common case */
592 	if (buf->count == 1 && buf->idx == 0 && buf->off == 0 &&
593 	    !(buf->buf[0].flags & FUSE_BUF_IS_FD)) {
594 		/* FIXME: also avoid memory copy if there are multiple buffers
595 		   but none of them contain an fd */
596 
597 		iov[iov_count].iov_base = buf->buf[0].mem;
598 		iov[iov_count].iov_len = len;
599 		iov_count++;
600 		return fuse_send_msg(se, ch, iov, iov_count);
601 	}
602 
603 	res = posix_memalign(&mbuf, pagesize, len);
604 	if (res != 0)
605 		return res;
606 
607 	mem_buf.buf[0].mem = mbuf;
608 	res = fuse_buf_copy(&mem_buf, buf, 0);
609 	if (res < 0) {
610 		free(mbuf);
611 		return -res;
612 	}
613 	len = res;
614 
615 	iov[iov_count].iov_base = mbuf;
616 	iov[iov_count].iov_len = len;
617 	iov_count++;
618 	res = fuse_send_msg(se, ch, iov, iov_count);
619 	free(mbuf);
620 
621 	return res;
622 }
623 
624 struct fuse_ll_pipe {
625 	size_t size;
626 	int can_grow;
627 	int pipe[2];
628 };
629 
fuse_ll_pipe_free(struct fuse_ll_pipe * llp)630 static void fuse_ll_pipe_free(struct fuse_ll_pipe *llp)
631 {
632 	close(llp->pipe[0]);
633 	close(llp->pipe[1]);
634 	free(llp);
635 }
636 
637 #ifdef HAVE_SPLICE
638 #if !defined(HAVE_PIPE2) || !defined(O_CLOEXEC)
fuse_pipe(int fds[2])639 static int fuse_pipe(int fds[2])
640 {
641 	int rv = pipe(fds);
642 
643 	if (rv == -1)
644 		return rv;
645 
646 	if (fcntl(fds[0], F_SETFL, O_NONBLOCK) == -1 ||
647 	    fcntl(fds[1], F_SETFL, O_NONBLOCK) == -1 ||
648 	    fcntl(fds[0], F_SETFD, FD_CLOEXEC) == -1 ||
649 	    fcntl(fds[1], F_SETFD, FD_CLOEXEC) == -1) {
650 		close(fds[0]);
651 		close(fds[1]);
652 		rv = -1;
653 	}
654 	return rv;
655 }
656 #else
fuse_pipe(int fds[2])657 static int fuse_pipe(int fds[2])
658 {
659 	return pipe2(fds, O_CLOEXEC | O_NONBLOCK);
660 }
661 #endif
662 
fuse_ll_get_pipe(struct fuse_session * se)663 static struct fuse_ll_pipe *fuse_ll_get_pipe(struct fuse_session *se)
664 {
665 	struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
666 	if (llp == NULL) {
667 		int res;
668 
669 		llp = malloc(sizeof(struct fuse_ll_pipe));
670 		if (llp == NULL)
671 			return NULL;
672 
673 		res = fuse_pipe(llp->pipe);
674 		if (res == -1) {
675 			free(llp);
676 			return NULL;
677 		}
678 
679 		/*
680 		 *the default size is 16 pages on linux
681 		 */
682 		llp->size = pagesize * 16;
683 		llp->can_grow = 1;
684 
685 		pthread_setspecific(se->pipe_key, llp);
686 	}
687 
688 	return llp;
689 }
690 #endif
691 
fuse_ll_clear_pipe(struct fuse_session * se)692 static void fuse_ll_clear_pipe(struct fuse_session *se)
693 {
694 	struct fuse_ll_pipe *llp = pthread_getspecific(se->pipe_key);
695 	if (llp) {
696 		pthread_setspecific(se->pipe_key, NULL);
697 		fuse_ll_pipe_free(llp);
698 	}
699 }
700 
701 #if defined(HAVE_SPLICE) && defined(HAVE_VMSPLICE)
read_back(int fd,char * buf,size_t len)702 static int read_back(int fd, char *buf, size_t len)
703 {
704 	int res;
705 
706 	res = read(fd, buf, len);
707 	if (res == -1) {
708 		fuse_log(FUSE_LOG_ERR, "fuse: internal error: failed to read back from pipe: %s\n", strerror(errno));
709 		return -EIO;
710 	}
711 	if (res != len) {
712 		fuse_log(FUSE_LOG_ERR, "fuse: internal error: short read back from pipe: %i from %zi\n", res, len);
713 		return -EIO;
714 	}
715 	return 0;
716 }
717 
grow_pipe_to_max(int pipefd)718 static int grow_pipe_to_max(int pipefd)
719 {
720 	int max;
721 	int res;
722 	int maxfd;
723 	char buf[32];
724 
725 	maxfd = open("/proc/sys/fs/pipe-max-size", O_RDONLY);
726 	if (maxfd < 0)
727 		return -errno;
728 
729 	res = read(maxfd, buf, sizeof(buf) - 1);
730 	if (res < 0) {
731 		int saved_errno;
732 
733 		saved_errno = errno;
734 		close(maxfd);
735 		return -saved_errno;
736 	}
737 	close(maxfd);
738 	buf[res] = '\0';
739 
740 	max = atoi(buf);
741 	res = fcntl(pipefd, F_SETPIPE_SZ, max);
742 	if (res < 0)
743 		return -errno;
744 	return max;
745 }
746 
fuse_send_data_iov(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int iov_count,struct fuse_bufvec * buf,unsigned int flags)747 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
748 			       struct iovec *iov, int iov_count,
749 			       struct fuse_bufvec *buf, unsigned int flags)
750 {
751 	int res;
752 	size_t len = fuse_buf_size(buf);
753 	struct fuse_out_header *out = iov[0].iov_base;
754 	struct fuse_ll_pipe *llp;
755 	int splice_flags;
756 	size_t pipesize;
757 	size_t total_buf_size;
758 	size_t idx;
759 	size_t headerlen;
760 	struct fuse_bufvec pipe_buf = FUSE_BUFVEC_INIT(len);
761 
762 	if (se->broken_splice_nonblock)
763 		goto fallback;
764 
765 	if (flags & FUSE_BUF_NO_SPLICE)
766 		goto fallback;
767 
768 	total_buf_size = 0;
769 	for (idx = buf->idx; idx < buf->count; idx++) {
770 		total_buf_size += buf->buf[idx].size;
771 		if (idx == buf->idx)
772 			total_buf_size -= buf->off;
773 	}
774 	if (total_buf_size < 2 * pagesize)
775 		goto fallback;
776 
777 	if (se->conn.proto_minor < 14 ||
778 	    !(se->conn.want & FUSE_CAP_SPLICE_WRITE))
779 		goto fallback;
780 
781 	llp = fuse_ll_get_pipe(se);
782 	if (llp == NULL)
783 		goto fallback;
784 
785 
786 	headerlen = iov_length(iov, iov_count);
787 
788 	out->len = headerlen + len;
789 
790 	/*
791 	 * Heuristic for the required pipe size, does not work if the
792 	 * source contains less than page size fragments
793 	 */
794 	pipesize = pagesize * (iov_count + buf->count + 1) + out->len;
795 
796 	if (llp->size < pipesize) {
797 		if (llp->can_grow) {
798 			res = fcntl(llp->pipe[0], F_SETPIPE_SZ, pipesize);
799 			if (res == -1) {
800 				res = grow_pipe_to_max(llp->pipe[0]);
801 				if (res > 0)
802 					llp->size = res;
803 				llp->can_grow = 0;
804 				goto fallback;
805 			}
806 			llp->size = res;
807 		}
808 		if (llp->size < pipesize)
809 			goto fallback;
810 	}
811 
812 
813 	res = vmsplice(llp->pipe[1], iov, iov_count, SPLICE_F_NONBLOCK);
814 	if (res == -1)
815 		goto fallback;
816 
817 	if (res != headerlen) {
818 		res = -EIO;
819 		fuse_log(FUSE_LOG_ERR, "fuse: short vmsplice to pipe: %u/%zu\n", res,
820 			headerlen);
821 		goto clear_pipe;
822 	}
823 
824 	pipe_buf.buf[0].flags = FUSE_BUF_IS_FD;
825 	pipe_buf.buf[0].fd = llp->pipe[1];
826 
827 	res = fuse_buf_copy(&pipe_buf, buf,
828 			    FUSE_BUF_FORCE_SPLICE | FUSE_BUF_SPLICE_NONBLOCK);
829 	if (res < 0) {
830 		if (res == -EAGAIN || res == -EINVAL) {
831 			/*
832 			 * Should only get EAGAIN on kernels with
833 			 * broken SPLICE_F_NONBLOCK support (<=
834 			 * 2.6.35) where this error or a short read is
835 			 * returned even if the pipe itself is not
836 			 * full
837 			 *
838 			 * EINVAL might mean that splice can't handle
839 			 * this combination of input and output.
840 			 */
841 			if (res == -EAGAIN)
842 				se->broken_splice_nonblock = 1;
843 
844 			pthread_setspecific(se->pipe_key, NULL);
845 			fuse_ll_pipe_free(llp);
846 			goto fallback;
847 		}
848 		res = -res;
849 		goto clear_pipe;
850 	}
851 
852 	if (res != 0 && res < len) {
853 		struct fuse_bufvec mem_buf = FUSE_BUFVEC_INIT(len);
854 		void *mbuf;
855 		size_t now_len = res;
856 		/*
857 		 * For regular files a short count is either
858 		 *  1) due to EOF, or
859 		 *  2) because of broken SPLICE_F_NONBLOCK (see above)
860 		 *
861 		 * For other inputs it's possible that we overflowed
862 		 * the pipe because of small buffer fragments.
863 		 */
864 
865 		res = posix_memalign(&mbuf, pagesize, len);
866 		if (res != 0)
867 			goto clear_pipe;
868 
869 		mem_buf.buf[0].mem = mbuf;
870 		mem_buf.off = now_len;
871 		res = fuse_buf_copy(&mem_buf, buf, 0);
872 		if (res > 0) {
873 			char *tmpbuf;
874 			size_t extra_len = res;
875 			/*
876 			 * Trickiest case: got more data.  Need to get
877 			 * back the data from the pipe and then fall
878 			 * back to regular write.
879 			 */
880 			tmpbuf = malloc(headerlen);
881 			if (tmpbuf == NULL) {
882 				free(mbuf);
883 				res = ENOMEM;
884 				goto clear_pipe;
885 			}
886 			res = read_back(llp->pipe[0], tmpbuf, headerlen);
887 			free(tmpbuf);
888 			if (res != 0) {
889 				free(mbuf);
890 				goto clear_pipe;
891 			}
892 			res = read_back(llp->pipe[0], mbuf, now_len);
893 			if (res != 0) {
894 				free(mbuf);
895 				goto clear_pipe;
896 			}
897 			len = now_len + extra_len;
898 			iov[iov_count].iov_base = mbuf;
899 			iov[iov_count].iov_len = len;
900 			iov_count++;
901 			res = fuse_send_msg(se, ch, iov, iov_count);
902 			free(mbuf);
903 			return res;
904 		}
905 		free(mbuf);
906 		res = now_len;
907 	}
908 	len = res;
909 	out->len = headerlen + len;
910 
911 	if (se->debug) {
912 		fuse_log(FUSE_LOG_DEBUG,
913 			"   unique: %llu, success, outsize: %i (splice)\n",
914 			(unsigned long long) out->unique, out->len);
915 	}
916 
917 	splice_flags = 0;
918 	if ((flags & FUSE_BUF_SPLICE_MOVE) &&
919 	    (se->conn.want & FUSE_CAP_SPLICE_MOVE))
920 		splice_flags |= SPLICE_F_MOVE;
921 
922 	res = splice(llp->pipe[0], NULL, ch ? ch->fd : se->fd,
923 		     NULL, out->len, splice_flags);
924 	if (res == -1) {
925 		res = -errno;
926 		perror("fuse: splice from pipe");
927 		goto clear_pipe;
928 	}
929 	if (res != out->len) {
930 		res = -EIO;
931 		fuse_log(FUSE_LOG_ERR, "fuse: short splice from pipe: %u/%u\n",
932 			res, out->len);
933 		goto clear_pipe;
934 	}
935 	return 0;
936 
937 clear_pipe:
938 	fuse_ll_clear_pipe(se);
939 	return res;
940 
941 fallback:
942 	return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
943 }
944 #else
fuse_send_data_iov(struct fuse_session * se,struct fuse_chan * ch,struct iovec * iov,int iov_count,struct fuse_bufvec * buf,unsigned int flags)945 static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch,
946 			       struct iovec *iov, int iov_count,
947 			       struct fuse_bufvec *buf, unsigned int flags)
948 {
949 	size_t len = fuse_buf_size(buf);
950 	(void) flags;
951 
952 	return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len);
953 }
954 #endif
955 
fuse_reply_data(fuse_req_t req,struct fuse_bufvec * bufv,enum fuse_buf_copy_flags flags)956 int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv,
957 		    enum fuse_buf_copy_flags flags)
958 {
959 	struct iovec iov[2];
960 	struct fuse_out_header out;
961 	int res;
962 
963 	iov[0].iov_base = &out;
964 	iov[0].iov_len = sizeof(struct fuse_out_header);
965 
966 	out.unique = req->unique;
967 	out.error = 0;
968 
969 	res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv, flags);
970 	if (res <= 0) {
971 		fuse_free_req(req);
972 		return res;
973 	} else {
974 		return fuse_reply_err(req, res);
975 	}
976 }
977 
fuse_reply_statfs(fuse_req_t req,const struct statvfs * stbuf)978 int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf)
979 {
980 	struct fuse_statfs_out arg;
981 	size_t size = req->se->conn.proto_minor < 4 ?
982 		FUSE_COMPAT_STATFS_SIZE : sizeof(arg);
983 
984 	memset(&arg, 0, sizeof(arg));
985 	convert_statfs(stbuf, &arg.st);
986 
987 	return send_reply_ok(req, &arg, size);
988 }
989 
fuse_reply_xattr(fuse_req_t req,size_t count)990 int fuse_reply_xattr(fuse_req_t req, size_t count)
991 {
992 	struct fuse_getxattr_out arg;
993 
994 	memset(&arg, 0, sizeof(arg));
995 	arg.size = count;
996 
997 	return send_reply_ok(req, &arg, sizeof(arg));
998 }
999 
fuse_reply_lock(fuse_req_t req,const struct flock * lock)1000 int fuse_reply_lock(fuse_req_t req, const struct flock *lock)
1001 {
1002 	struct fuse_lk_out arg;
1003 
1004 	memset(&arg, 0, sizeof(arg));
1005 	arg.lk.type = lock->l_type;
1006 	if (lock->l_type != F_UNLCK) {
1007 		arg.lk.start = lock->l_start;
1008 		if (lock->l_len == 0)
1009 			arg.lk.end = OFFSET_MAX;
1010 		else
1011 			arg.lk.end = lock->l_start + lock->l_len - 1;
1012 	}
1013 	arg.lk.pid = lock->l_pid;
1014 	return send_reply_ok(req, &arg, sizeof(arg));
1015 }
1016 
fuse_reply_bmap(fuse_req_t req,uint64_t idx)1017 int fuse_reply_bmap(fuse_req_t req, uint64_t idx)
1018 {
1019 	struct fuse_bmap_out arg;
1020 
1021 	memset(&arg, 0, sizeof(arg));
1022 	arg.block = idx;
1023 
1024 	return send_reply_ok(req, &arg, sizeof(arg));
1025 }
1026 
fuse_ioctl_iovec_copy(const struct iovec * iov,size_t count)1027 static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov,
1028 						      size_t count)
1029 {
1030 	struct fuse_ioctl_iovec *fiov;
1031 	size_t i;
1032 
1033 	fiov = malloc(sizeof(fiov[0]) * count);
1034 	if (!fiov)
1035 		return NULL;
1036 
1037 	for (i = 0; i < count; i++) {
1038 		fiov[i].base = (uintptr_t) iov[i].iov_base;
1039 		fiov[i].len = iov[i].iov_len;
1040 	}
1041 
1042 	return fiov;
1043 }
1044 
fuse_reply_ioctl_retry(fuse_req_t req,const struct iovec * in_iov,size_t in_count,const struct iovec * out_iov,size_t out_count)1045 int fuse_reply_ioctl_retry(fuse_req_t req,
1046 			   const struct iovec *in_iov, size_t in_count,
1047 			   const struct iovec *out_iov, size_t out_count)
1048 {
1049 	struct fuse_ioctl_out arg;
1050 	struct fuse_ioctl_iovec *in_fiov = NULL;
1051 	struct fuse_ioctl_iovec *out_fiov = NULL;
1052 	struct iovec iov[4];
1053 	size_t count = 1;
1054 	int res;
1055 
1056 	memset(&arg, 0, sizeof(arg));
1057 	arg.flags |= FUSE_IOCTL_RETRY;
1058 	arg.in_iovs = in_count;
1059 	arg.out_iovs = out_count;
1060 	iov[count].iov_base = &arg;
1061 	iov[count].iov_len = sizeof(arg);
1062 	count++;
1063 
1064 	if (req->se->conn.proto_minor < 16) {
1065 		if (in_count) {
1066 			iov[count].iov_base = (void *)in_iov;
1067 			iov[count].iov_len = sizeof(in_iov[0]) * in_count;
1068 			count++;
1069 		}
1070 
1071 		if (out_count) {
1072 			iov[count].iov_base = (void *)out_iov;
1073 			iov[count].iov_len = sizeof(out_iov[0]) * out_count;
1074 			count++;
1075 		}
1076 	} else {
1077 		/* Can't handle non-compat 64bit ioctls on 32bit */
1078 		if (sizeof(void *) == 4 && req->ioctl_64bit) {
1079 			res = fuse_reply_err(req, EINVAL);
1080 			goto out;
1081 		}
1082 
1083 		if (in_count) {
1084 			in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count);
1085 			if (!in_fiov)
1086 				goto enomem;
1087 
1088 			iov[count].iov_base = (void *)in_fiov;
1089 			iov[count].iov_len = sizeof(in_fiov[0]) * in_count;
1090 			count++;
1091 		}
1092 		if (out_count) {
1093 			out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count);
1094 			if (!out_fiov)
1095 				goto enomem;
1096 
1097 			iov[count].iov_base = (void *)out_fiov;
1098 			iov[count].iov_len = sizeof(out_fiov[0]) * out_count;
1099 			count++;
1100 		}
1101 	}
1102 
1103 	res = send_reply_iov(req, 0, iov, count);
1104 out:
1105 	free(in_fiov);
1106 	free(out_fiov);
1107 
1108 	return res;
1109 
1110 enomem:
1111 	res = fuse_reply_err(req, ENOMEM);
1112 	goto out;
1113 }
1114 
fuse_reply_ioctl(fuse_req_t req,int result,const void * buf,size_t size)1115 int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size)
1116 {
1117 	struct fuse_ioctl_out arg;
1118 	struct iovec iov[3];
1119 	size_t count = 1;
1120 
1121 	memset(&arg, 0, sizeof(arg));
1122 	arg.result = result;
1123 	iov[count].iov_base = &arg;
1124 	iov[count].iov_len = sizeof(arg);
1125 	count++;
1126 
1127 	if (size) {
1128 		iov[count].iov_base = (char *) buf;
1129 		iov[count].iov_len = size;
1130 		count++;
1131 	}
1132 
1133 	return send_reply_iov(req, 0, iov, count);
1134 }
1135 
fuse_reply_ioctl_iov(fuse_req_t req,int result,const struct iovec * iov,int count)1136 int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov,
1137 			 int count)
1138 {
1139 	struct iovec *padded_iov;
1140 	struct fuse_ioctl_out arg;
1141 	int res;
1142 
1143 	padded_iov = malloc((count + 2) * sizeof(struct iovec));
1144 	if (padded_iov == NULL)
1145 		return fuse_reply_err(req, ENOMEM);
1146 
1147 	memset(&arg, 0, sizeof(arg));
1148 	arg.result = result;
1149 	padded_iov[1].iov_base = &arg;
1150 	padded_iov[1].iov_len = sizeof(arg);
1151 
1152 	memcpy(&padded_iov[2], iov, count * sizeof(struct iovec));
1153 
1154 	res = send_reply_iov(req, 0, padded_iov, count + 2);
1155 	free(padded_iov);
1156 
1157 	return res;
1158 }
1159 
fuse_reply_poll(fuse_req_t req,unsigned revents)1160 int fuse_reply_poll(fuse_req_t req, unsigned revents)
1161 {
1162 	struct fuse_poll_out arg;
1163 
1164 	memset(&arg, 0, sizeof(arg));
1165 	arg.revents = revents;
1166 
1167 	return send_reply_ok(req, &arg, sizeof(arg));
1168 }
1169 
fuse_reply_lseek(fuse_req_t req,off_t off)1170 int fuse_reply_lseek(fuse_req_t req, off_t off)
1171 {
1172 	struct fuse_lseek_out arg;
1173 
1174 	memset(&arg, 0, sizeof(arg));
1175 	arg.offset = off;
1176 
1177 	return send_reply_ok(req, &arg, sizeof(arg));
1178 }
1179 
do_lookup(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1180 static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1181 {
1182 	char *name = (char *) inarg;
1183 
1184 	if (req->se->op.lookup)
1185 		req->se->op.lookup(req, nodeid, name);
1186 	else
1187 		fuse_reply_err(req, ENOSYS);
1188 }
1189 
do_forget(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1190 static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1191 {
1192 	struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg;
1193 
1194 	if (req->se->op.forget)
1195 		req->se->op.forget(req, nodeid, arg->nlookup);
1196 	else
1197 		fuse_reply_none(req);
1198 }
1199 
do_batch_forget(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1200 static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid,
1201 			    const void *inarg)
1202 {
1203 	struct fuse_batch_forget_in *arg = (void *) inarg;
1204 	struct fuse_forget_one *param = (void *) PARAM(arg);
1205 	unsigned int i;
1206 
1207 	(void) nodeid;
1208 
1209 	if (req->se->op.forget_multi) {
1210 		req->se->op.forget_multi(req, arg->count,
1211 				     (struct fuse_forget_data *) param);
1212 	} else if (req->se->op.forget) {
1213 		for (i = 0; i < arg->count; i++) {
1214 			struct fuse_forget_one *forget = &param[i];
1215 			struct fuse_req *dummy_req;
1216 
1217 			dummy_req = fuse_ll_alloc_req(req->se);
1218 			if (dummy_req == NULL)
1219 				break;
1220 
1221 			dummy_req->unique = req->unique;
1222 			dummy_req->ctx = req->ctx;
1223 			dummy_req->ch = NULL;
1224 
1225 			req->se->op.forget(dummy_req, forget->nodeid,
1226 					  forget->nlookup);
1227 		}
1228 		fuse_reply_none(req);
1229 	} else {
1230 		fuse_reply_none(req);
1231 	}
1232 }
1233 
do_getattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1234 static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1235 {
1236 	struct fuse_file_info *fip = NULL;
1237 	struct fuse_file_info fi;
1238 
1239 	if (req->se->conn.proto_minor >= 9) {
1240 		struct fuse_getattr_in *arg = (struct fuse_getattr_in *) inarg;
1241 
1242 		if (arg->getattr_flags & FUSE_GETATTR_FH) {
1243 			memset(&fi, 0, sizeof(fi));
1244 			fi.fh = arg->fh;
1245 			fip = &fi;
1246 		}
1247 	}
1248 
1249 	if (req->se->op.getattr)
1250 		req->se->op.getattr(req, nodeid, fip);
1251 	else
1252 		fuse_reply_err(req, ENOSYS);
1253 }
1254 
do_setattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1255 static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1256 {
1257 	struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg;
1258 
1259 	if (req->se->op.setattr) {
1260 		struct fuse_file_info *fi = NULL;
1261 		struct fuse_file_info fi_store;
1262 		struct stat stbuf;
1263 		memset(&stbuf, 0, sizeof(stbuf));
1264 		convert_attr(arg, &stbuf);
1265 		if (arg->valid & FATTR_FH) {
1266 			arg->valid &= ~FATTR_FH;
1267 			memset(&fi_store, 0, sizeof(fi_store));
1268 			fi = &fi_store;
1269 			fi->fh = arg->fh;
1270 		}
1271 		arg->valid &=
1272 			FUSE_SET_ATTR_MODE	|
1273 			FUSE_SET_ATTR_UID	|
1274 			FUSE_SET_ATTR_GID	|
1275 			FUSE_SET_ATTR_SIZE	|
1276 			FUSE_SET_ATTR_ATIME	|
1277 			FUSE_SET_ATTR_MTIME	|
1278 			FUSE_SET_ATTR_ATIME_NOW	|
1279 			FUSE_SET_ATTR_MTIME_NOW |
1280 			FUSE_SET_ATTR_CTIME;
1281 
1282 		req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi);
1283 	} else
1284 		fuse_reply_err(req, ENOSYS);
1285 }
1286 
do_access(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1287 static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1288 {
1289 	struct fuse_access_in *arg = (struct fuse_access_in *) inarg;
1290 
1291 	if (req->se->op.access)
1292 		req->se->op.access(req, nodeid, arg->mask);
1293 	else
1294 		fuse_reply_err(req, ENOSYS);
1295 }
1296 
do_readlink(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1297 static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1298 {
1299 	(void) inarg;
1300 
1301 	if (req->se->op.readlink)
1302 		req->se->op.readlink(req, nodeid);
1303 	else
1304 		fuse_reply_err(req, ENOSYS);
1305 }
1306 
do_canonical_path(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1307 static void do_canonical_path(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1308 {
1309 	(void) inarg;
1310 
1311 	if (req->se->op.canonical_path)
1312 		req->se->op.canonical_path(req, nodeid);
1313 	else
1314 		fuse_reply_err(req, ENOSYS);
1315 }
1316 
do_mknod(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1317 static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1318 {
1319 	struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg;
1320 	char *name = PARAM(arg);
1321 
1322 	if (req->se->conn.proto_minor >= 12)
1323 		req->ctx.umask = arg->umask;
1324 	else
1325 		name = (char *) inarg + FUSE_COMPAT_MKNOD_IN_SIZE;
1326 
1327 	if (req->se->op.mknod)
1328 		req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev);
1329 	else
1330 		fuse_reply_err(req, ENOSYS);
1331 }
1332 
do_mkdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1333 static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1334 {
1335 	struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg;
1336 
1337 	if (req->se->conn.proto_minor >= 12)
1338 		req->ctx.umask = arg->umask;
1339 
1340 	if (req->se->op.mkdir)
1341 		req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode);
1342 	else
1343 		fuse_reply_err(req, ENOSYS);
1344 }
1345 
do_unlink(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1346 static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1347 {
1348 	char *name = (char *) inarg;
1349 
1350 	if (req->se->op.unlink)
1351 		req->se->op.unlink(req, nodeid, name);
1352 	else
1353 		fuse_reply_err(req, ENOSYS);
1354 }
1355 
do_rmdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1356 static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1357 {
1358 	char *name = (char *) inarg;
1359 
1360 	if (req->se->op.rmdir)
1361 		req->se->op.rmdir(req, nodeid, name);
1362 	else
1363 		fuse_reply_err(req, ENOSYS);
1364 }
1365 
do_symlink(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1366 static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1367 {
1368 	char *name = (char *) inarg;
1369 	char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1;
1370 
1371 	if (req->se->op.symlink)
1372 		req->se->op.symlink(req, linkname, nodeid, name);
1373 	else
1374 		fuse_reply_err(req, ENOSYS);
1375 }
1376 
do_rename(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1377 static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1378 {
1379 	struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg;
1380 	char *oldname = PARAM(arg);
1381 	char *newname = oldname + strlen(oldname) + 1;
1382 
1383 	if (req->se->op.rename)
1384 		req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1385 				  0);
1386 	else
1387 		fuse_reply_err(req, ENOSYS);
1388 }
1389 
do_rename2(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1390 static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1391 {
1392 	struct fuse_rename2_in *arg = (struct fuse_rename2_in *) inarg;
1393 	char *oldname = PARAM(arg);
1394 	char *newname = oldname + strlen(oldname) + 1;
1395 
1396 	if (req->se->op.rename)
1397 		req->se->op.rename(req, nodeid, oldname, arg->newdir, newname,
1398 				  arg->flags);
1399 	else
1400 		fuse_reply_err(req, ENOSYS);
1401 }
1402 
do_link(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1403 static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1404 {
1405 	struct fuse_link_in *arg = (struct fuse_link_in *) inarg;
1406 
1407 	if (req->se->op.link)
1408 		req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg));
1409 	else
1410 		fuse_reply_err(req, ENOSYS);
1411 }
1412 
do_create(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1413 static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1414 {
1415 	struct fuse_create_in *arg = (struct fuse_create_in *) inarg;
1416 
1417 	if (req->se->op.create) {
1418 		struct fuse_file_info fi;
1419 		char *name = PARAM(arg);
1420 
1421 		memset(&fi, 0, sizeof(fi));
1422 		fi.flags = arg->flags;
1423 
1424 		if (req->se->conn.proto_minor >= 12)
1425 			req->ctx.umask = arg->umask;
1426 		else
1427 			name = (char *) inarg + sizeof(struct fuse_open_in);
1428 
1429 		req->se->op.create(req, nodeid, name, arg->mode, &fi);
1430 	} else
1431 		fuse_reply_err(req, ENOSYS);
1432 }
1433 
do_open(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1434 static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1435 {
1436 	struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1437 	struct fuse_file_info fi;
1438 
1439 	memset(&fi, 0, sizeof(fi));
1440 	fi.flags = arg->flags;
1441 
1442 	if (req->se->op.open)
1443 		req->se->op.open(req, nodeid, &fi);
1444 	else
1445 		fuse_reply_open(req, &fi);
1446 }
1447 
do_read(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1448 static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1449 {
1450 	struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1451 
1452 	if (req->se->op.read) {
1453 		struct fuse_file_info fi;
1454 
1455 		memset(&fi, 0, sizeof(fi));
1456 		fi.fh = arg->fh;
1457 		if (req->se->conn.proto_minor >= 9) {
1458 			fi.lock_owner = arg->lock_owner;
1459 			fi.flags = arg->flags;
1460 		}
1461 		req->se->op.read(req, nodeid, arg->size, arg->offset, &fi);
1462 	} else
1463 		fuse_reply_err(req, ENOSYS);
1464 }
1465 
do_write(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1466 static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1467 {
1468 	struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1469 	struct fuse_file_info fi;
1470 	char *param;
1471 
1472 	memset(&fi, 0, sizeof(fi));
1473 	fi.fh = arg->fh;
1474 	fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0;
1475 
1476 	if (req->se->conn.proto_minor < 9) {
1477 		param = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1478 	} else {
1479 		fi.lock_owner = arg->lock_owner;
1480 		fi.flags = arg->flags;
1481 		param = PARAM(arg);
1482 	}
1483 
1484 	if (req->se->op.write)
1485 		req->se->op.write(req, nodeid, param, arg->size,
1486 				 arg->offset, &fi);
1487 	else
1488 		fuse_reply_err(req, ENOSYS);
1489 }
1490 
do_write_buf(fuse_req_t req,fuse_ino_t nodeid,const void * inarg,const struct fuse_buf * ibuf)1491 static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg,
1492 			 const struct fuse_buf *ibuf)
1493 {
1494 	struct fuse_session *se = req->se;
1495 	struct fuse_bufvec bufv = {
1496 		.buf[0] = *ibuf,
1497 		.count = 1,
1498 	};
1499 	struct fuse_write_in *arg = (struct fuse_write_in *) inarg;
1500 	struct fuse_file_info fi;
1501 
1502 	memset(&fi, 0, sizeof(fi));
1503 	fi.fh = arg->fh;
1504 	fi.writepage = arg->write_flags & FUSE_WRITE_CACHE;
1505 
1506 	if (se->conn.proto_minor < 9) {
1507 		bufv.buf[0].mem = ((char *) arg) + FUSE_COMPAT_WRITE_IN_SIZE;
1508 		bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1509 			FUSE_COMPAT_WRITE_IN_SIZE;
1510 		assert(!(bufv.buf[0].flags & FUSE_BUF_IS_FD));
1511 	} else {
1512 		fi.lock_owner = arg->lock_owner;
1513 		fi.flags = arg->flags;
1514 		if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
1515 			bufv.buf[0].mem = PARAM(arg);
1516 
1517 		bufv.buf[0].size -= sizeof(struct fuse_in_header) +
1518 			sizeof(struct fuse_write_in);
1519 	}
1520 	if (bufv.buf[0].size < arg->size) {
1521 		fuse_log(FUSE_LOG_ERR, "fuse: do_write_buf: buffer size too small\n");
1522 		fuse_reply_err(req, EIO);
1523 		goto out;
1524 	}
1525 	bufv.buf[0].size = arg->size;
1526 
1527 	se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi);
1528 
1529 out:
1530 	/* Need to reset the pipe if ->write_buf() didn't consume all data */
1531 	if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
1532 		fuse_ll_clear_pipe(se);
1533 }
1534 
do_flush(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1535 static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1536 {
1537 	struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg;
1538 	struct fuse_file_info fi;
1539 
1540 	memset(&fi, 0, sizeof(fi));
1541 	fi.fh = arg->fh;
1542 	fi.flush = 1;
1543 	if (req->se->conn.proto_minor >= 7)
1544 		fi.lock_owner = arg->lock_owner;
1545 
1546 	if (req->se->op.flush)
1547 		req->se->op.flush(req, nodeid, &fi);
1548 	else
1549 		fuse_reply_err(req, ENOSYS);
1550 }
1551 
do_release(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1552 static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1553 {
1554 	struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1555 	struct fuse_file_info fi;
1556 
1557 	memset(&fi, 0, sizeof(fi));
1558 	fi.flags = arg->flags;
1559 	fi.fh = arg->fh;
1560 	if (req->se->conn.proto_minor >= 8) {
1561 		fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0;
1562 		fi.lock_owner = arg->lock_owner;
1563 	}
1564 	if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) {
1565 		fi.flock_release = 1;
1566 		fi.lock_owner = arg->lock_owner;
1567 	}
1568 
1569 	if (req->se->op.release)
1570 		req->se->op.release(req, nodeid, &fi);
1571 	else
1572 		fuse_reply_err(req, 0);
1573 }
1574 
do_fsync(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1575 static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1576 {
1577 	struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1578 	struct fuse_file_info fi;
1579 	int datasync = arg->fsync_flags & 1;
1580 
1581 	memset(&fi, 0, sizeof(fi));
1582 	fi.fh = arg->fh;
1583 
1584 	if (req->se->op.fsync)
1585 		req->se->op.fsync(req, nodeid, datasync, &fi);
1586 	else
1587 		fuse_reply_err(req, ENOSYS);
1588 }
1589 
do_opendir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1590 static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1591 {
1592 	struct fuse_open_in *arg = (struct fuse_open_in *) inarg;
1593 	struct fuse_file_info fi;
1594 
1595 	memset(&fi, 0, sizeof(fi));
1596 	fi.flags = arg->flags;
1597 
1598 	if (req->se->op.opendir)
1599 		req->se->op.opendir(req, nodeid, &fi);
1600 	else
1601 		fuse_reply_open(req, &fi);
1602 }
1603 
do_readdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1604 static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1605 {
1606 	struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1607 	struct fuse_file_info fi;
1608 
1609 	memset(&fi, 0, sizeof(fi));
1610 	fi.fh = arg->fh;
1611 
1612 	if (req->se->op.readdir)
1613 		req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi);
1614 	else
1615 		fuse_reply_err(req, ENOSYS);
1616 }
1617 
do_readdirplus(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1618 static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1619 {
1620 	struct fuse_read_in *arg = (struct fuse_read_in *) inarg;
1621 	struct fuse_file_info fi;
1622 
1623 	memset(&fi, 0, sizeof(fi));
1624 	fi.fh = arg->fh;
1625 
1626 	if (req->se->op.readdirplus)
1627 		req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi);
1628 	else
1629 		fuse_reply_err(req, ENOSYS);
1630 }
1631 
do_releasedir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1632 static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1633 {
1634 	struct fuse_release_in *arg = (struct fuse_release_in *) inarg;
1635 	struct fuse_file_info fi;
1636 
1637 	memset(&fi, 0, sizeof(fi));
1638 	fi.flags = arg->flags;
1639 	fi.fh = arg->fh;
1640 
1641 	if (req->se->op.releasedir)
1642 		req->se->op.releasedir(req, nodeid, &fi);
1643 	else
1644 		fuse_reply_err(req, 0);
1645 }
1646 
do_fsyncdir(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1647 static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1648 {
1649 	struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg;
1650 	struct fuse_file_info fi;
1651 	int datasync = arg->fsync_flags & 1;
1652 
1653 	memset(&fi, 0, sizeof(fi));
1654 	fi.fh = arg->fh;
1655 
1656 	if (req->se->op.fsyncdir)
1657 		req->se->op.fsyncdir(req, nodeid, datasync, &fi);
1658 	else
1659 		fuse_reply_err(req, ENOSYS);
1660 }
1661 
do_statfs(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1662 static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1663 {
1664 	(void) nodeid;
1665 	(void) inarg;
1666 
1667 	if (req->se->op.statfs)
1668 		req->se->op.statfs(req, nodeid);
1669 	else {
1670 		struct statvfs buf = {
1671 			.f_namemax = 255,
1672 			.f_bsize = 512,
1673 		};
1674 		fuse_reply_statfs(req, &buf);
1675 	}
1676 }
1677 
do_setxattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1678 static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1679 {
1680 	struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg;
1681 	char *name = PARAM(arg);
1682 	char *value = name + strlen(name) + 1;
1683 
1684 	if (req->se->op.setxattr)
1685 		req->se->op.setxattr(req, nodeid, name, value, arg->size,
1686 				    arg->flags);
1687 	else
1688 		fuse_reply_err(req, ENOSYS);
1689 }
1690 
do_getxattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1691 static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1692 {
1693 	struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1694 
1695 	if (req->se->op.getxattr)
1696 		req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size);
1697 	else
1698 		fuse_reply_err(req, ENOSYS);
1699 }
1700 
do_listxattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1701 static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1702 {
1703 	struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg;
1704 
1705 	if (req->se->op.listxattr)
1706 		req->se->op.listxattr(req, nodeid, arg->size);
1707 	else
1708 		fuse_reply_err(req, ENOSYS);
1709 }
1710 
do_removexattr(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1711 static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1712 {
1713 	char *name = (char *) inarg;
1714 
1715 	if (req->se->op.removexattr)
1716 		req->se->op.removexattr(req, nodeid, name);
1717 	else
1718 		fuse_reply_err(req, ENOSYS);
1719 }
1720 
convert_fuse_file_lock(struct fuse_file_lock * fl,struct flock * flock)1721 static void convert_fuse_file_lock(struct fuse_file_lock *fl,
1722 				   struct flock *flock)
1723 {
1724 	memset(flock, 0, sizeof(struct flock));
1725 	flock->l_type = fl->type;
1726 	flock->l_whence = SEEK_SET;
1727 	flock->l_start = fl->start;
1728 	if (fl->end == OFFSET_MAX)
1729 		flock->l_len = 0;
1730 	else
1731 		flock->l_len = fl->end - fl->start + 1;
1732 	flock->l_pid = fl->pid;
1733 }
1734 
do_getlk(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1735 static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1736 {
1737 	struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1738 	struct fuse_file_info fi;
1739 	struct flock flock;
1740 
1741 	memset(&fi, 0, sizeof(fi));
1742 	fi.fh = arg->fh;
1743 	fi.lock_owner = arg->owner;
1744 
1745 	convert_fuse_file_lock(&arg->lk, &flock);
1746 	if (req->se->op.getlk)
1747 		req->se->op.getlk(req, nodeid, &fi, &flock);
1748 	else
1749 		fuse_reply_err(req, ENOSYS);
1750 }
1751 
do_setlk_common(fuse_req_t req,fuse_ino_t nodeid,const void * inarg,int sleep)1752 static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid,
1753 			    const void *inarg, int sleep)
1754 {
1755 	struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg;
1756 	struct fuse_file_info fi;
1757 	struct flock flock;
1758 
1759 	memset(&fi, 0, sizeof(fi));
1760 	fi.fh = arg->fh;
1761 	fi.lock_owner = arg->owner;
1762 
1763 	if (arg->lk_flags & FUSE_LK_FLOCK) {
1764 		int op = 0;
1765 
1766 		switch (arg->lk.type) {
1767 		case F_RDLCK:
1768 			op = LOCK_SH;
1769 			break;
1770 		case F_WRLCK:
1771 			op = LOCK_EX;
1772 			break;
1773 		case F_UNLCK:
1774 			op = LOCK_UN;
1775 			break;
1776 		}
1777 		if (!sleep)
1778 			op |= LOCK_NB;
1779 
1780 		if (req->se->op.flock)
1781 			req->se->op.flock(req, nodeid, &fi, op);
1782 		else
1783 			fuse_reply_err(req, ENOSYS);
1784 	} else {
1785 		convert_fuse_file_lock(&arg->lk, &flock);
1786 		if (req->se->op.setlk)
1787 			req->se->op.setlk(req, nodeid, &fi, &flock, sleep);
1788 		else
1789 			fuse_reply_err(req, ENOSYS);
1790 	}
1791 }
1792 
do_setlk(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1793 static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1794 {
1795 	do_setlk_common(req, nodeid, inarg, 0);
1796 }
1797 
do_setlkw(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1798 static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1799 {
1800 	do_setlk_common(req, nodeid, inarg, 1);
1801 }
1802 
find_interrupted(struct fuse_session * se,struct fuse_req * req)1803 static int find_interrupted(struct fuse_session *se, struct fuse_req *req)
1804 {
1805 	struct fuse_req *curr;
1806 
1807 	for (curr = se->list.next; curr != &se->list; curr = curr->next) {
1808 		if (curr->unique == req->u.i.unique) {
1809 			fuse_interrupt_func_t func;
1810 			void *data;
1811 
1812 			curr->ctr++;
1813 			pthread_mutex_unlock(&se->lock);
1814 
1815 			/* Ugh, ugly locking */
1816 			pthread_mutex_lock(&curr->lock);
1817 			pthread_mutex_lock(&se->lock);
1818 			curr->interrupted = 1;
1819 			func = curr->u.ni.func;
1820 			data = curr->u.ni.data;
1821 			pthread_mutex_unlock(&se->lock);
1822 			if (func)
1823 				func(curr, data);
1824 			pthread_mutex_unlock(&curr->lock);
1825 
1826 			pthread_mutex_lock(&se->lock);
1827 			curr->ctr--;
1828 			if (!curr->ctr)
1829 				destroy_req(curr);
1830 
1831 			return 1;
1832 		}
1833 	}
1834 	for (curr = se->interrupts.next; curr != &se->interrupts;
1835 	     curr = curr->next) {
1836 		if (curr->u.i.unique == req->u.i.unique)
1837 			return 1;
1838 	}
1839 	return 0;
1840 }
1841 
do_interrupt(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1842 static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1843 {
1844 	struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg;
1845 	struct fuse_session *se = req->se;
1846 
1847 	(void) nodeid;
1848 	if (se->debug)
1849 		fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n",
1850 			(unsigned long long) arg->unique);
1851 
1852 	req->u.i.unique = arg->unique;
1853 
1854 	pthread_mutex_lock(&se->lock);
1855 	if (find_interrupted(se, req))
1856 		destroy_req(req);
1857 	else
1858 		list_add_req(req, &se->interrupts);
1859 	pthread_mutex_unlock(&se->lock);
1860 }
1861 
check_interrupt(struct fuse_session * se,struct fuse_req * req)1862 static struct fuse_req *check_interrupt(struct fuse_session *se,
1863 					struct fuse_req *req)
1864 {
1865 	struct fuse_req *curr;
1866 
1867 	for (curr = se->interrupts.next; curr != &se->interrupts;
1868 	     curr = curr->next) {
1869 		if (curr->u.i.unique == req->unique) {
1870 			req->interrupted = 1;
1871 			list_del_req(curr);
1872 			free(curr);
1873 			return NULL;
1874 		}
1875 	}
1876 	curr = se->interrupts.next;
1877 	if (curr != &se->interrupts) {
1878 		list_del_req(curr);
1879 		list_init_req(curr);
1880 		return curr;
1881 	} else
1882 		return NULL;
1883 }
1884 
do_bmap(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1885 static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1886 {
1887 	struct fuse_bmap_in *arg = (struct fuse_bmap_in *) inarg;
1888 
1889 	if (req->se->op.bmap)
1890 		req->se->op.bmap(req, nodeid, arg->blocksize, arg->block);
1891 	else
1892 		fuse_reply_err(req, ENOSYS);
1893 }
1894 
do_ioctl(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1895 static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1896 {
1897 	struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *) inarg;
1898 	unsigned int flags = arg->flags;
1899 	void *in_buf = arg->in_size ? PARAM(arg) : NULL;
1900 	struct fuse_file_info fi;
1901 
1902 	if (flags & FUSE_IOCTL_DIR &&
1903 	    !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) {
1904 		fuse_reply_err(req, ENOTTY);
1905 		return;
1906 	}
1907 
1908 	memset(&fi, 0, sizeof(fi));
1909 	fi.fh = arg->fh;
1910 
1911 	if (sizeof(void *) == 4 && req->se->conn.proto_minor >= 16 &&
1912 	    !(flags & FUSE_IOCTL_32BIT)) {
1913 		req->ioctl_64bit = 1;
1914 	}
1915 
1916 	if (req->se->op.ioctl)
1917 		req->se->op.ioctl(req, nodeid, arg->cmd,
1918 				 (void *)(uintptr_t)arg->arg, &fi, flags,
1919 				 in_buf, arg->in_size, arg->out_size);
1920 	else
1921 		fuse_reply_err(req, ENOSYS);
1922 }
1923 
fuse_pollhandle_destroy(struct fuse_pollhandle * ph)1924 void fuse_pollhandle_destroy(struct fuse_pollhandle *ph)
1925 {
1926 	free(ph);
1927 }
1928 
do_poll(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1929 static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1930 {
1931 	struct fuse_poll_in *arg = (struct fuse_poll_in *) inarg;
1932 	struct fuse_file_info fi;
1933 
1934 	memset(&fi, 0, sizeof(fi));
1935 	fi.fh = arg->fh;
1936 	fi.poll_events = arg->events;
1937 
1938 	if (req->se->op.poll) {
1939 		struct fuse_pollhandle *ph = NULL;
1940 
1941 		if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) {
1942 			ph = malloc(sizeof(struct fuse_pollhandle));
1943 			if (ph == NULL) {
1944 				fuse_reply_err(req, ENOMEM);
1945 				return;
1946 			}
1947 			ph->kh = arg->kh;
1948 			ph->se = req->se;
1949 		}
1950 
1951 		req->se->op.poll(req, nodeid, &fi, ph);
1952 	} else {
1953 		fuse_reply_err(req, ENOSYS);
1954 	}
1955 }
1956 
do_fallocate(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1957 static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1958 {
1959 	struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *) inarg;
1960 	struct fuse_file_info fi;
1961 
1962 	memset(&fi, 0, sizeof(fi));
1963 	fi.fh = arg->fh;
1964 
1965 	if (req->se->op.fallocate)
1966 		req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, &fi);
1967 	else
1968 		fuse_reply_err(req, ENOSYS);
1969 }
1970 
do_copy_file_range(fuse_req_t req,fuse_ino_t nodeid_in,const void * inarg)1971 static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in, const void *inarg)
1972 {
1973 	struct fuse_copy_file_range_in *arg = (struct fuse_copy_file_range_in *) inarg;
1974 	struct fuse_file_info fi_in, fi_out;
1975 
1976 	memset(&fi_in, 0, sizeof(fi_in));
1977 	fi_in.fh = arg->fh_in;
1978 
1979 	memset(&fi_out, 0, sizeof(fi_out));
1980 	fi_out.fh = arg->fh_out;
1981 
1982 
1983 	if (req->se->op.copy_file_range)
1984 		req->se->op.copy_file_range(req, nodeid_in, arg->off_in,
1985 					    &fi_in, arg->nodeid_out,
1986 					    arg->off_out, &fi_out, arg->len,
1987 					    arg->flags);
1988 	else
1989 		fuse_reply_err(req, ENOSYS);
1990 }
1991 
do_lseek(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)1992 static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
1993 {
1994 	struct fuse_lseek_in *arg = (struct fuse_lseek_in *) inarg;
1995 	struct fuse_file_info fi;
1996 
1997 	memset(&fi, 0, sizeof(fi));
1998 	fi.fh = arg->fh;
1999 
2000 	if (req->se->op.lseek)
2001 		req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi);
2002 	else
2003 		fuse_reply_err(req, ENOSYS);
2004 }
2005 
2006 /* Prevent bogus data races (bogus since "init" is called before
2007  * multi-threading becomes relevant */
2008 static __attribute__((no_sanitize("thread")))
do_init(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)2009 void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2010 {
2011 	struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
2012 	struct fuse_init_out outarg;
2013 	struct fuse_session *se = req->se;
2014 	size_t bufsize = se->bufsize;
2015 	size_t outargsize = sizeof(outarg);
2016 
2017 	(void) nodeid;
2018 	if (se->debug) {
2019 		fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor);
2020 		if (arg->major == 7 && arg->minor >= 6) {
2021 			fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
2022 			fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n",
2023 				arg->max_readahead);
2024 		}
2025 	}
2026 	se->conn.proto_major = arg->major;
2027 	se->conn.proto_minor = arg->minor;
2028 	se->conn.capable = 0;
2029 	se->conn.want = 0;
2030 
2031 	memset(&outarg, 0, sizeof(outarg));
2032 	outarg.major = FUSE_KERNEL_VERSION;
2033 	outarg.minor = FUSE_KERNEL_MINOR_VERSION;
2034 
2035 	if (arg->major < 7) {
2036 		fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n",
2037 			arg->major, arg->minor);
2038 		fuse_reply_err(req, EPROTO);
2039 		return;
2040 	}
2041 
2042 	if (arg->major > 7) {
2043 		/* Wait for a second INIT request with a 7.X version */
2044 		send_reply_ok(req, &outarg, sizeof(outarg));
2045 		return;
2046 	}
2047 
2048 	if (arg->minor >= 6) {
2049 		if (arg->max_readahead < se->conn.max_readahead)
2050 			se->conn.max_readahead = arg->max_readahead;
2051 		if (arg->flags & FUSE_ASYNC_READ)
2052 			se->conn.capable |= FUSE_CAP_ASYNC_READ;
2053 		if (arg->flags & FUSE_POSIX_LOCKS)
2054 			se->conn.capable |= FUSE_CAP_POSIX_LOCKS;
2055 		if (arg->flags & FUSE_ATOMIC_O_TRUNC)
2056 			se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC;
2057 		if (arg->flags & FUSE_EXPORT_SUPPORT)
2058 			se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT;
2059 		if (arg->flags & FUSE_DONT_MASK)
2060 			se->conn.capable |= FUSE_CAP_DONT_MASK;
2061 		if (arg->flags & FUSE_FLOCK_LOCKS)
2062 			se->conn.capable |= FUSE_CAP_FLOCK_LOCKS;
2063 		if (arg->flags & FUSE_AUTO_INVAL_DATA)
2064 			se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA;
2065 		if (arg->flags & FUSE_DO_READDIRPLUS)
2066 			se->conn.capable |= FUSE_CAP_READDIRPLUS;
2067 		if (arg->flags & FUSE_READDIRPLUS_AUTO)
2068 			se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO;
2069 		if (arg->flags & FUSE_ASYNC_DIO)
2070 			se->conn.capable |= FUSE_CAP_ASYNC_DIO;
2071 		if (arg->flags & FUSE_WRITEBACK_CACHE)
2072 			se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE;
2073 		if (arg->flags & FUSE_NO_OPEN_SUPPORT)
2074 			se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT;
2075 		if (arg->flags & FUSE_PARALLEL_DIROPS)
2076 			se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS;
2077 		if (arg->flags & FUSE_POSIX_ACL)
2078 			se->conn.capable |= FUSE_CAP_POSIX_ACL;
2079 		if (arg->flags & FUSE_HANDLE_KILLPRIV)
2080 			se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV;
2081 		if (arg->flags & FUSE_CACHE_SYMLINKS)
2082 			se->conn.capable |= FUSE_CAP_CACHE_SYMLINKS;
2083 		if (arg->flags & FUSE_NO_OPENDIR_SUPPORT)
2084 			se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT;
2085 		if (arg->flags & FUSE_EXPLICIT_INVAL_DATA)
2086 			se->conn.capable |= FUSE_CAP_EXPLICIT_INVAL_DATA;
2087 		if (!(arg->flags & FUSE_MAX_PAGES)) {
2088 			size_t max_bufsize =
2089 				FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize()
2090 				+ FUSE_BUFFER_HEADER_SIZE;
2091 			if (bufsize > max_bufsize) {
2092 				bufsize = max_bufsize;
2093 			}
2094 		}
2095 		if (arg->flags & FUSE_PASSTHROUGH)
2096 			se->conn.capable |= FUSE_PASSTHROUGH;
2097 	} else {
2098 		se->conn.max_readahead = 0;
2099 	}
2100 
2101 	if (se->conn.proto_minor >= 14) {
2102 #ifdef HAVE_SPLICE
2103 #ifdef HAVE_VMSPLICE
2104 		se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE;
2105 #endif
2106 		se->conn.capable |= FUSE_CAP_SPLICE_READ;
2107 #endif
2108 	}
2109 	if (se->conn.proto_minor >= 18)
2110 		se->conn.capable |= FUSE_CAP_IOCTL_DIR;
2111 
2112 	/* Default settings for modern filesystems.
2113 	 *
2114 	 * Most of these capabilities were disabled by default in
2115 	 * libfuse2 for backwards compatibility reasons. In libfuse3,
2116 	 * we can finally enable them by default (as long as they're
2117 	 * supported by the kernel).
2118 	 */
2119 #define LL_SET_DEFAULT(cond, cap) \
2120 	if ((cond) && (se->conn.capable & (cap))) \
2121 		se->conn.want |= (cap)
2122 	LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ);
2123 	LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS);
2124 	LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA);
2125 	LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV);
2126 	LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO);
2127 	LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR);
2128 	LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC);
2129 	LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ);
2130 	LL_SET_DEFAULT(se->op.getlk && se->op.setlk,
2131 		       FUSE_CAP_POSIX_LOCKS);
2132 	LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS);
2133 	LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS);
2134 	LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir,
2135 		       FUSE_CAP_READDIRPLUS_AUTO);
2136 	se->conn.time_gran = 1;
2137 
2138 	if (bufsize < FUSE_MIN_READ_BUFFER) {
2139 		fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n",
2140 			bufsize);
2141 		bufsize = FUSE_MIN_READ_BUFFER;
2142 	}
2143 	se->bufsize = bufsize;
2144 
2145 	if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE)
2146 		se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE;
2147 
2148 	se->got_init = 1;
2149 	if (se->op.init)
2150 		se->op.init(se->userdata, &se->conn);
2151 
2152 	if (se->conn.want & (~se->conn.capable)) {
2153 		fuse_log(FUSE_LOG_ERR, "fuse: error: filesystem requested capabilities "
2154 			"0x%x that are not supported by kernel, aborting.\n",
2155 			se->conn.want & (~se->conn.capable));
2156 		fuse_reply_err(req, EPROTO);
2157 		se->error = -EPROTO;
2158 		fuse_session_exit(se);
2159 		return;
2160 	}
2161 
2162 	unsigned max_read_mo = get_max_read(se->mo);
2163 	if (se->conn.max_read != max_read_mo) {
2164 		fuse_log(FUSE_LOG_ERR, "fuse: error: init() and fuse_session_new() "
2165 			"requested different maximum read size (%u vs %u)\n",
2166 			se->conn.max_read, max_read_mo);
2167 		fuse_reply_err(req, EPROTO);
2168 		se->error = -EPROTO;
2169 		fuse_session_exit(se);
2170 		return;
2171 	}
2172 
2173 	if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) {
2174 		se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE;
2175 	}
2176 	if (arg->flags & FUSE_MAX_PAGES) {
2177 		outarg.flags |= FUSE_MAX_PAGES;
2178 		outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1;
2179 	}
2180 
2181 	/* Always enable big writes, this is superseded
2182 	   by the max_write option */
2183 	outarg.flags |= FUSE_BIG_WRITES;
2184 
2185 	if (se->conn.want & FUSE_CAP_ASYNC_READ)
2186 		outarg.flags |= FUSE_ASYNC_READ;
2187 	if (se->conn.want & FUSE_CAP_POSIX_LOCKS)
2188 		outarg.flags |= FUSE_POSIX_LOCKS;
2189 	if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC)
2190 		outarg.flags |= FUSE_ATOMIC_O_TRUNC;
2191 	if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT)
2192 		outarg.flags |= FUSE_EXPORT_SUPPORT;
2193 	if (se->conn.want & FUSE_CAP_DONT_MASK)
2194 		outarg.flags |= FUSE_DONT_MASK;
2195 	if (se->conn.want & FUSE_CAP_FLOCK_LOCKS)
2196 		outarg.flags |= FUSE_FLOCK_LOCKS;
2197 	if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA)
2198 		outarg.flags |= FUSE_AUTO_INVAL_DATA;
2199 	if (se->conn.want & FUSE_CAP_READDIRPLUS)
2200 		outarg.flags |= FUSE_DO_READDIRPLUS;
2201 	if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO)
2202 		outarg.flags |= FUSE_READDIRPLUS_AUTO;
2203 	if (se->conn.want & FUSE_CAP_ASYNC_DIO)
2204 		outarg.flags |= FUSE_ASYNC_DIO;
2205 	if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE)
2206 		outarg.flags |= FUSE_WRITEBACK_CACHE;
2207 	if (se->conn.want & FUSE_CAP_POSIX_ACL)
2208 		outarg.flags |= FUSE_POSIX_ACL;
2209 	if (se->conn.want & FUSE_CAP_PASSTHROUGH)
2210 		outarg.flags |= FUSE_PASSTHROUGH;
2211 	if (se->conn.want & FUSE_CAP_CACHE_SYMLINKS)
2212 		outarg.flags |= FUSE_CACHE_SYMLINKS;
2213 	if (se->conn.want & FUSE_CAP_EXPLICIT_INVAL_DATA)
2214 		outarg.flags |= FUSE_EXPLICIT_INVAL_DATA;
2215 	outarg.max_readahead = se->conn.max_readahead;
2216 	outarg.max_write = se->conn.max_write;
2217 	if (se->conn.proto_minor >= 13) {
2218 		if (se->conn.max_background >= (1 << 16))
2219 			se->conn.max_background = (1 << 16) - 1;
2220 		if (se->conn.congestion_threshold > se->conn.max_background)
2221 			se->conn.congestion_threshold = se->conn.max_background;
2222 		if (!se->conn.congestion_threshold) {
2223 			se->conn.congestion_threshold =
2224 				se->conn.max_background * 3 / 4;
2225 		}
2226 
2227 		outarg.max_background = se->conn.max_background;
2228 		outarg.congestion_threshold = se->conn.congestion_threshold;
2229 	}
2230 	if (se->conn.proto_minor >= 23)
2231 		outarg.time_gran = se->conn.time_gran;
2232 
2233 	if (se->debug) {
2234 		fuse_log(FUSE_LOG_DEBUG, "   INIT: %u.%u\n", outarg.major, outarg.minor);
2235 		fuse_log(FUSE_LOG_DEBUG, "   flags=0x%08x\n", outarg.flags);
2236 		fuse_log(FUSE_LOG_DEBUG, "   max_readahead=0x%08x\n",
2237 			outarg.max_readahead);
2238 		fuse_log(FUSE_LOG_DEBUG, "   max_write=0x%08x\n", outarg.max_write);
2239 		fuse_log(FUSE_LOG_DEBUG, "   max_background=%i\n",
2240 			outarg.max_background);
2241 		fuse_log(FUSE_LOG_DEBUG, "   congestion_threshold=%i\n",
2242 			outarg.congestion_threshold);
2243 		fuse_log(FUSE_LOG_DEBUG, "   time_gran=%u\n",
2244 			outarg.time_gran);
2245 	}
2246 	if (arg->minor < 5)
2247 		outargsize = FUSE_COMPAT_INIT_OUT_SIZE;
2248 	else if (arg->minor < 23)
2249 		outargsize = FUSE_COMPAT_22_INIT_OUT_SIZE;
2250 
2251 	send_reply_ok(req, &outarg, outargsize);
2252 }
2253 
do_destroy(fuse_req_t req,fuse_ino_t nodeid,const void * inarg)2254 static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
2255 {
2256 	struct fuse_session *se = req->se;
2257 
2258 	(void) nodeid;
2259 	(void) inarg;
2260 
2261 	se->got_destroy = 1;
2262 	if (se->op.destroy)
2263 		se->op.destroy(se->userdata);
2264 
2265 	send_reply_ok(req, NULL, 0);
2266 }
2267 
list_del_nreq(struct fuse_notify_req * nreq)2268 static void list_del_nreq(struct fuse_notify_req *nreq)
2269 {
2270 	struct fuse_notify_req *prev = nreq->prev;
2271 	struct fuse_notify_req *next = nreq->next;
2272 	prev->next = next;
2273 	next->prev = prev;
2274 }
2275 
list_add_nreq(struct fuse_notify_req * nreq,struct fuse_notify_req * next)2276 static void list_add_nreq(struct fuse_notify_req *nreq,
2277 			  struct fuse_notify_req *next)
2278 {
2279 	struct fuse_notify_req *prev = next->prev;
2280 	nreq->next = next;
2281 	nreq->prev = prev;
2282 	prev->next = nreq;
2283 	next->prev = nreq;
2284 }
2285 
list_init_nreq(struct fuse_notify_req * nreq)2286 static void list_init_nreq(struct fuse_notify_req *nreq)
2287 {
2288 	nreq->next = nreq;
2289 	nreq->prev = nreq;
2290 }
2291 
do_notify_reply(fuse_req_t req,fuse_ino_t nodeid,const void * inarg,const struct fuse_buf * buf)2292 static void do_notify_reply(fuse_req_t req, fuse_ino_t nodeid,
2293 			    const void *inarg, const struct fuse_buf *buf)
2294 {
2295 	struct fuse_session *se = req->se;
2296 	struct fuse_notify_req *nreq;
2297 	struct fuse_notify_req *head;
2298 
2299 	pthread_mutex_lock(&se->lock);
2300 	head = &se->notify_list;
2301 	for (nreq = head->next; nreq != head; nreq = nreq->next) {
2302 		if (nreq->unique == req->unique) {
2303 			list_del_nreq(nreq);
2304 			break;
2305 		}
2306 	}
2307 	pthread_mutex_unlock(&se->lock);
2308 
2309 	if (nreq != head)
2310 		nreq->reply(nreq, req, nodeid, inarg, buf);
2311 }
2312 
send_notify_iov(struct fuse_session * se,int notify_code,struct iovec * iov,int count)2313 static int send_notify_iov(struct fuse_session *se, int notify_code,
2314 			   struct iovec *iov, int count)
2315 {
2316 	struct fuse_out_header out;
2317 
2318 	if (!se->got_init)
2319 		return -ENOTCONN;
2320 
2321 	out.unique = 0;
2322 	out.error = notify_code;
2323 	iov[0].iov_base = &out;
2324 	iov[0].iov_len = sizeof(struct fuse_out_header);
2325 
2326 	return fuse_send_msg(se, NULL, iov, count);
2327 }
2328 
fuse_lowlevel_notify_poll(struct fuse_pollhandle * ph)2329 int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph)
2330 {
2331 	if (ph != NULL) {
2332 		struct fuse_notify_poll_wakeup_out outarg;
2333 		struct iovec iov[2];
2334 
2335 		outarg.kh = ph->kh;
2336 
2337 		iov[1].iov_base = &outarg;
2338 		iov[1].iov_len = sizeof(outarg);
2339 
2340 		return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2);
2341 	} else {
2342 		return 0;
2343 	}
2344 }
2345 
fuse_lowlevel_notify_inval_inode(struct fuse_session * se,fuse_ino_t ino,off_t off,off_t len)2346 int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino,
2347 				     off_t off, off_t len)
2348 {
2349 	struct fuse_notify_inval_inode_out outarg;
2350 	struct iovec iov[2];
2351 
2352 	if (!se)
2353 		return -EINVAL;
2354 
2355 	if (se->conn.proto_minor < 12)
2356 		return -ENOSYS;
2357 
2358 	outarg.ino = ino;
2359 	outarg.off = off;
2360 	outarg.len = len;
2361 
2362 	iov[1].iov_base = &outarg;
2363 	iov[1].iov_len = sizeof(outarg);
2364 
2365 	return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2);
2366 }
2367 
fuse_lowlevel_notify_inval_entry(struct fuse_session * se,fuse_ino_t parent,const char * name,size_t namelen)2368 int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent,
2369 				     const char *name, size_t namelen)
2370 {
2371 	struct fuse_notify_inval_entry_out outarg;
2372 	struct iovec iov[3];
2373 
2374 	if (!se)
2375 		return -EINVAL;
2376 
2377 	if (se->conn.proto_minor < 12)
2378 		return -ENOSYS;
2379 
2380 	outarg.parent = parent;
2381 	outarg.namelen = namelen;
2382 	outarg.padding = 0;
2383 
2384 	iov[1].iov_base = &outarg;
2385 	iov[1].iov_len = sizeof(outarg);
2386 	iov[2].iov_base = (void *)name;
2387 	iov[2].iov_len = namelen + 1;
2388 
2389 	return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3);
2390 }
2391 
fuse_lowlevel_notify_delete(struct fuse_session * se,fuse_ino_t parent,fuse_ino_t child,const char * name,size_t namelen)2392 int fuse_lowlevel_notify_delete(struct fuse_session *se,
2393 				fuse_ino_t parent, fuse_ino_t child,
2394 				const char *name, size_t namelen)
2395 {
2396 	struct fuse_notify_delete_out outarg;
2397 	struct iovec iov[3];
2398 
2399 	if (!se)
2400 		return -EINVAL;
2401 
2402 	if (se->conn.proto_minor < 18)
2403 		return -ENOSYS;
2404 
2405 	outarg.parent = parent;
2406 	outarg.child = child;
2407 	outarg.namelen = namelen;
2408 	outarg.padding = 0;
2409 
2410 	iov[1].iov_base = &outarg;
2411 	iov[1].iov_len = sizeof(outarg);
2412 	iov[2].iov_base = (void *)name;
2413 	iov[2].iov_len = namelen + 1;
2414 
2415 	return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3);
2416 }
2417 
fuse_lowlevel_notify_store(struct fuse_session * se,fuse_ino_t ino,off_t offset,struct fuse_bufvec * bufv,enum fuse_buf_copy_flags flags)2418 int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino,
2419 			       off_t offset, struct fuse_bufvec *bufv,
2420 			       enum fuse_buf_copy_flags flags)
2421 {
2422 	struct fuse_out_header out;
2423 	struct fuse_notify_store_out outarg;
2424 	struct iovec iov[3];
2425 	size_t size = fuse_buf_size(bufv);
2426 	int res;
2427 
2428 	if (!se)
2429 		return -EINVAL;
2430 
2431 	if (se->conn.proto_minor < 15)
2432 		return -ENOSYS;
2433 
2434 	out.unique = 0;
2435 	out.error = FUSE_NOTIFY_STORE;
2436 
2437 	outarg.nodeid = ino;
2438 	outarg.offset = offset;
2439 	outarg.size = size;
2440 	outarg.padding = 0;
2441 
2442 	iov[0].iov_base = &out;
2443 	iov[0].iov_len = sizeof(out);
2444 	iov[1].iov_base = &outarg;
2445 	iov[1].iov_len = sizeof(outarg);
2446 
2447 	res = fuse_send_data_iov(se, NULL, iov, 2, bufv, flags);
2448 	if (res > 0)
2449 		res = -res;
2450 
2451 	return res;
2452 }
2453 
2454 struct fuse_retrieve_req {
2455 	struct fuse_notify_req nreq;
2456 	void *cookie;
2457 };
2458 
fuse_ll_retrieve_reply(struct fuse_notify_req * nreq,fuse_req_t req,fuse_ino_t ino,const void * inarg,const struct fuse_buf * ibuf)2459 static void fuse_ll_retrieve_reply(struct fuse_notify_req *nreq,
2460 				   fuse_req_t req, fuse_ino_t ino,
2461 				   const void *inarg,
2462 				   const struct fuse_buf *ibuf)
2463 {
2464 	struct fuse_session *se = req->se;
2465 	struct fuse_retrieve_req *rreq =
2466 		container_of(nreq, struct fuse_retrieve_req, nreq);
2467 	const struct fuse_notify_retrieve_in *arg = inarg;
2468 	struct fuse_bufvec bufv = {
2469 		.buf[0] = *ibuf,
2470 		.count = 1,
2471 	};
2472 
2473 	if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD))
2474 		bufv.buf[0].mem = PARAM(arg);
2475 
2476 	bufv.buf[0].size -= sizeof(struct fuse_in_header) +
2477 		sizeof(struct fuse_notify_retrieve_in);
2478 
2479 	if (bufv.buf[0].size < arg->size) {
2480 		fuse_log(FUSE_LOG_ERR, "fuse: retrieve reply: buffer size too small\n");
2481 		fuse_reply_none(req);
2482 		goto out;
2483 	}
2484 	bufv.buf[0].size = arg->size;
2485 
2486 	if (se->op.retrieve_reply) {
2487 		se->op.retrieve_reply(req, rreq->cookie, ino,
2488 					  arg->offset, &bufv);
2489 	} else {
2490 		fuse_reply_none(req);
2491 	}
2492 out:
2493 	free(rreq);
2494 	if ((ibuf->flags & FUSE_BUF_IS_FD) && bufv.idx < bufv.count)
2495 		fuse_ll_clear_pipe(se);
2496 }
2497 
fuse_lowlevel_notify_retrieve(struct fuse_session * se,fuse_ino_t ino,size_t size,off_t offset,void * cookie)2498 int fuse_lowlevel_notify_retrieve(struct fuse_session *se, fuse_ino_t ino,
2499 				  size_t size, off_t offset, void *cookie)
2500 {
2501 	struct fuse_notify_retrieve_out outarg;
2502 	struct iovec iov[2];
2503 	struct fuse_retrieve_req *rreq;
2504 	int err;
2505 
2506 	if (!se)
2507 		return -EINVAL;
2508 
2509 	if (se->conn.proto_minor < 15)
2510 		return -ENOSYS;
2511 
2512 	rreq = malloc(sizeof(*rreq));
2513 	if (rreq == NULL)
2514 		return -ENOMEM;
2515 
2516 	pthread_mutex_lock(&se->lock);
2517 	rreq->cookie = cookie;
2518 	rreq->nreq.unique = se->notify_ctr++;
2519 	rreq->nreq.reply = fuse_ll_retrieve_reply;
2520 	list_add_nreq(&rreq->nreq, &se->notify_list);
2521 	pthread_mutex_unlock(&se->lock);
2522 
2523 	outarg.notify_unique = rreq->nreq.unique;
2524 	outarg.nodeid = ino;
2525 	outarg.offset = offset;
2526 	outarg.size = size;
2527 	outarg.padding = 0;
2528 
2529 	iov[1].iov_base = &outarg;
2530 	iov[1].iov_len = sizeof(outarg);
2531 
2532 	err = send_notify_iov(se, FUSE_NOTIFY_RETRIEVE, iov, 2);
2533 	if (err) {
2534 		pthread_mutex_lock(&se->lock);
2535 		list_del_nreq(&rreq->nreq);
2536 		pthread_mutex_unlock(&se->lock);
2537 		free(rreq);
2538 	}
2539 
2540 	return err;
2541 }
2542 
fuse_req_userdata(fuse_req_t req)2543 void *fuse_req_userdata(fuse_req_t req)
2544 {
2545 	return req->se->userdata;
2546 }
2547 
fuse_req_ctx(fuse_req_t req)2548 const struct fuse_ctx *fuse_req_ctx(fuse_req_t req)
2549 {
2550 	return &req->ctx;
2551 }
2552 
fuse_req_interrupt_func(fuse_req_t req,fuse_interrupt_func_t func,void * data)2553 void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func,
2554 			     void *data)
2555 {
2556 	pthread_mutex_lock(&req->lock);
2557 	pthread_mutex_lock(&req->se->lock);
2558 	req->u.ni.func = func;
2559 	req->u.ni.data = data;
2560 	pthread_mutex_unlock(&req->se->lock);
2561 	if (req->interrupted && func)
2562 		func(req, data);
2563 	pthread_mutex_unlock(&req->lock);
2564 }
2565 
fuse_req_interrupted(fuse_req_t req)2566 int fuse_req_interrupted(fuse_req_t req)
2567 {
2568 	int interrupted;
2569 
2570 	pthread_mutex_lock(&req->se->lock);
2571 	interrupted = req->interrupted;
2572 	pthread_mutex_unlock(&req->se->lock);
2573 
2574 	return interrupted;
2575 }
2576 
2577 static struct {
2578 	void (*func)(fuse_req_t, fuse_ino_t, const void *);
2579 	const char *name;
2580 } fuse_ll_ops[] = {
2581 	[FUSE_LOOKUP]	   = { do_lookup,      "LOOKUP"	     },
2582 	[FUSE_FORGET]	   = { do_forget,      "FORGET"	     },
2583 	[FUSE_GETATTR]	   = { do_getattr,     "GETATTR"     },
2584 	[FUSE_SETATTR]	   = { do_setattr,     "SETATTR"     },
2585 	[FUSE_READLINK]	   = { do_readlink,    "READLINK"    },
2586         [FUSE_CANONICAL_PATH] = { do_canonical_path, "CANONICAL_PATH" },
2587 	[FUSE_SYMLINK]	   = { do_symlink,     "SYMLINK"     },
2588 	[FUSE_MKNOD]	   = { do_mknod,       "MKNOD"	     },
2589 	[FUSE_MKDIR]	   = { do_mkdir,       "MKDIR"	     },
2590 	[FUSE_UNLINK]	   = { do_unlink,      "UNLINK"	     },
2591 	[FUSE_RMDIR]	   = { do_rmdir,       "RMDIR"	     },
2592 	[FUSE_RENAME]	   = { do_rename,      "RENAME"	     },
2593 	[FUSE_LINK]	   = { do_link,	       "LINK"	     },
2594 	[FUSE_OPEN]	   = { do_open,	       "OPEN"	     },
2595 	[FUSE_READ]	   = { do_read,	       "READ"	     },
2596 	[FUSE_WRITE]	   = { do_write,       "WRITE"	     },
2597 	[FUSE_STATFS]	   = { do_statfs,      "STATFS"	     },
2598 	[FUSE_RELEASE]	   = { do_release,     "RELEASE"     },
2599 	[FUSE_FSYNC]	   = { do_fsync,       "FSYNC"	     },
2600 	[FUSE_SETXATTR]	   = { do_setxattr,    "SETXATTR"    },
2601 	[FUSE_GETXATTR]	   = { do_getxattr,    "GETXATTR"    },
2602 	[FUSE_LISTXATTR]   = { do_listxattr,   "LISTXATTR"   },
2603 	[FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" },
2604 	[FUSE_FLUSH]	   = { do_flush,       "FLUSH"	     },
2605 	[FUSE_INIT]	   = { do_init,	       "INIT"	     },
2606 	[FUSE_OPENDIR]	   = { do_opendir,     "OPENDIR"     },
2607 	[FUSE_READDIR]	   = { do_readdir,     "READDIR"     },
2608 	[FUSE_RELEASEDIR]  = { do_releasedir,  "RELEASEDIR"  },
2609 	[FUSE_FSYNCDIR]	   = { do_fsyncdir,    "FSYNCDIR"    },
2610 	[FUSE_GETLK]	   = { do_getlk,       "GETLK"	     },
2611 	[FUSE_SETLK]	   = { do_setlk,       "SETLK"	     },
2612 	[FUSE_SETLKW]	   = { do_setlkw,      "SETLKW"	     },
2613 	[FUSE_ACCESS]	   = { do_access,      "ACCESS"	     },
2614 	[FUSE_CREATE]	   = { do_create,      "CREATE"	     },
2615 	[FUSE_INTERRUPT]   = { do_interrupt,   "INTERRUPT"   },
2616 	[FUSE_BMAP]	   = { do_bmap,	       "BMAP"	     },
2617 	[FUSE_IOCTL]	   = { do_ioctl,       "IOCTL"	     },
2618 	[FUSE_POLL]	   = { do_poll,        "POLL"	     },
2619 	[FUSE_FALLOCATE]   = { do_fallocate,   "FALLOCATE"   },
2620 	[FUSE_DESTROY]	   = { do_destroy,     "DESTROY"     },
2621 	[FUSE_NOTIFY_REPLY] = { (void *) 1,    "NOTIFY_REPLY" },
2622 	[FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" },
2623 	[FUSE_READDIRPLUS] = { do_readdirplus,	"READDIRPLUS"},
2624 	[FUSE_RENAME2]     = { do_rename2,      "RENAME2"    },
2625 	[FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" },
2626 	[FUSE_LSEEK]	   = { do_lseek,       "LSEEK"	     },
2627 	[CUSE_INIT]	   = { cuse_lowlevel_init, "CUSE_INIT"   },
2628 };
2629 
2630 #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0]))
2631 
opname(enum fuse_opcode opcode)2632 static const char *opname(enum fuse_opcode opcode)
2633 {
2634 	if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name)
2635 		return "???";
2636 	else
2637 		return fuse_ll_ops[opcode].name;
2638 }
2639 
opfiltername(int filter)2640 static const char *opfiltername(int filter)
2641 {
2642 	switch (filter) {
2643 	case 0:
2644 		return "NONE";
2645 	case FUSE_PREFILTER:
2646 		return "FUSE_PREFILTER";
2647 	case FUSE_POSTFILTER:
2648 		return "FUSE_POSTFILTER";
2649 	case FUSE_PREFILTER | FUSE_POSTFILTER:
2650 		return "FUSE_PREFILTER | FUSE_POSTFILTER";
2651 	default:
2652 		return "???";
2653 	}
2654 }
2655 
fuse_ll_copy_from_pipe(struct fuse_bufvec * dst,struct fuse_bufvec * src)2656 static int fuse_ll_copy_from_pipe(struct fuse_bufvec *dst,
2657 				  struct fuse_bufvec *src)
2658 {
2659 	ssize_t res = fuse_buf_copy(dst, src, 0);
2660 	if (res < 0) {
2661 		fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n", strerror(-res));
2662 		return res;
2663 	}
2664 	if ((size_t)res < fuse_buf_size(dst)) {
2665 		fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
2666 		return -1;
2667 	}
2668 	return 0;
2669 }
2670 
fuse_session_process_buf(struct fuse_session * se,const struct fuse_buf * buf)2671 void fuse_session_process_buf(struct fuse_session *se,
2672 			      const struct fuse_buf *buf)
2673 {
2674 	fuse_session_process_buf_int(se, buf, NULL);
2675 }
2676 
fuse_session_process_buf_int(struct fuse_session * se,const struct fuse_buf * buf,struct fuse_chan * ch)2677 void fuse_session_process_buf_int(struct fuse_session *se,
2678 				  const struct fuse_buf *buf, struct fuse_chan *ch)
2679 {
2680 	const size_t write_header_size = sizeof(struct fuse_in_header) +
2681 		sizeof(struct fuse_write_in);
2682 	struct fuse_bufvec bufv = { .buf[0] = *buf, .count = 1 };
2683 	struct fuse_bufvec tmpbuf = FUSE_BUFVEC_INIT(write_header_size);
2684 	struct fuse_in_header *in;
2685 	const void *inarg;
2686 	struct fuse_req *req;
2687 	void *mbuf = NULL;
2688 	int err;
2689 	int res;
2690 	int opcode_filter;
2691 
2692 	if (buf->flags & FUSE_BUF_IS_FD) {
2693 		if (buf->size < tmpbuf.buf[0].size)
2694 			tmpbuf.buf[0].size = buf->size;
2695 
2696 		mbuf = malloc(tmpbuf.buf[0].size);
2697 		if (mbuf == NULL) {
2698 			fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate header\n");
2699 			goto clear_pipe;
2700 		}
2701 		tmpbuf.buf[0].mem = mbuf;
2702 
2703 		res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2704 		if (res < 0)
2705 			goto clear_pipe;
2706 
2707 		in = mbuf;
2708 	} else {
2709 		in = buf->mem;
2710 	}
2711 
2712 	/* Cleanup opcode most significant bits used by FUSE BPF */
2713 	opcode_filter = in->opcode & ~FUSE_OPCODE_FILTER;
2714 	in->opcode &= FUSE_OPCODE_FILTER;
2715 
2716 	if (se->debug) {
2717 		fuse_log(FUSE_LOG_DEBUG,
2718 			"unique: %llu, opcode: %s (%i), opcode filter: %s (%i), nodeid: %llu, insize: %zu, pid: %u\n",
2719 			(unsigned long long) in->unique,
2720 			opname((enum fuse_opcode) in->opcode), in->opcode,
2721 			opfiltername((enum fuse_opcode) opcode_filter), opcode_filter,
2722 			(unsigned long long) in->nodeid, buf->size, in->pid);
2723 	}
2724 
2725 	req = fuse_ll_alloc_req(se);
2726 	if (req == NULL) {
2727 		struct fuse_out_header out = {
2728 			.unique = in->unique,
2729 			.error = -ENOMEM,
2730 		};
2731 		struct iovec iov = {
2732 			.iov_base = &out,
2733 			.iov_len = sizeof(struct fuse_out_header),
2734 		};
2735 
2736 		fuse_send_msg(se, ch, &iov, 1);
2737 		goto clear_pipe;
2738 	}
2739 
2740 	req->unique = in->unique;
2741 	req->ctx.uid = in->uid;
2742 	req->ctx.gid = in->gid;
2743 	req->ctx.pid = in->pid;
2744 	req->ch = ch ? fuse_chan_get(ch) : NULL;
2745 
2746 	err = EIO;
2747 	if (!se->got_init) {
2748 		enum fuse_opcode expected;
2749 
2750 		expected = se->cuse_data ? CUSE_INIT : FUSE_INIT;
2751 		if (in->opcode != expected)
2752 			goto reply_err;
2753 	} else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT)
2754 		goto reply_err;
2755 
2756 	err = EACCES;
2757 	/* Implement -o allow_root */
2758 	if (se->deny_others && in->uid != se->owner && in->uid != 0 &&
2759 		 in->opcode != FUSE_INIT && in->opcode != FUSE_READ &&
2760 		 in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC &&
2761 		 in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR &&
2762 		 in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR &&
2763 		 in->opcode != FUSE_NOTIFY_REPLY &&
2764 		 in->opcode != FUSE_READDIRPLUS)
2765 		goto reply_err;
2766 
2767 	err = ENOSYS;
2768 	if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func)
2769 		goto reply_err;
2770 	if (in->opcode != FUSE_INTERRUPT) {
2771 		struct fuse_req *intr;
2772 		pthread_mutex_lock(&se->lock);
2773 		intr = check_interrupt(se, req);
2774 		list_add_req(req, &se->list);
2775 		pthread_mutex_unlock(&se->lock);
2776 		if (intr)
2777 			fuse_reply_err(intr, EAGAIN);
2778 	}
2779 
2780 	if ((buf->flags & FUSE_BUF_IS_FD) && write_header_size < buf->size &&
2781 	    (in->opcode != FUSE_WRITE || !se->op.write_buf) &&
2782 	    in->opcode != FUSE_NOTIFY_REPLY) {
2783 		void *newmbuf;
2784 
2785 		err = ENOMEM;
2786 		newmbuf = realloc(mbuf, buf->size);
2787 		if (newmbuf == NULL)
2788 			goto reply_err;
2789 		mbuf = newmbuf;
2790 
2791 		tmpbuf = FUSE_BUFVEC_INIT(buf->size - write_header_size);
2792 		tmpbuf.buf[0].mem = (char *)mbuf + write_header_size;
2793 
2794 		res = fuse_ll_copy_from_pipe(&tmpbuf, &bufv);
2795 		err = -res;
2796 		if (res < 0)
2797 			goto reply_err;
2798 
2799 		in = mbuf;
2800 	}
2801 
2802 	inarg = (void *) &in[1];
2803 	if (in->opcode == FUSE_WRITE && se->op.write_buf)
2804 		do_write_buf(req, in->nodeid, inarg, buf);
2805 	else if (in->opcode == FUSE_NOTIFY_REPLY)
2806 		do_notify_reply(req, in->nodeid, inarg, buf);
2807 	else
2808 		fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg);
2809 
2810 out_free:
2811 	free(mbuf);
2812 	return;
2813 
2814 reply_err:
2815 	fuse_reply_err(req, err);
2816 clear_pipe:
2817 	if (buf->flags & FUSE_BUF_IS_FD)
2818 		fuse_ll_clear_pipe(se);
2819 	goto out_free;
2820 }
2821 
2822 #define LL_OPTION(n,o,v) \
2823 	{ n, offsetof(struct fuse_session, o), v }
2824 
2825 static const struct fuse_opt fuse_ll_opts[] = {
2826 	LL_OPTION("debug", debug, 1),
2827 	LL_OPTION("-d", debug, 1),
2828 	LL_OPTION("--debug", debug, 1),
2829 	LL_OPTION("allow_root", deny_others, 1),
2830 	FUSE_OPT_END
2831 };
2832 
fuse_lowlevel_version(void)2833 void fuse_lowlevel_version(void)
2834 {
2835 	printf("using FUSE kernel interface version %i.%i\n",
2836 	       FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION);
2837 	fuse_mount_version();
2838 }
2839 
fuse_lowlevel_help(void)2840 void fuse_lowlevel_help(void)
2841 {
2842 	/* These are not all options, but the ones that are
2843 	   potentially of interest to an end-user */
2844 	printf(
2845 "    -o allow_other         allow access by all users\n"
2846 "    -o allow_root          allow access by root\n"
2847 "    -o auto_unmount        auto unmount on process termination\n");
2848 }
2849 
fuse_session_destroy(struct fuse_session * se)2850 void fuse_session_destroy(struct fuse_session *se)
2851 {
2852 	struct fuse_ll_pipe *llp;
2853 
2854 	if (se->got_init && !se->got_destroy) {
2855 		if (se->op.destroy)
2856 			se->op.destroy(se->userdata);
2857 	}
2858 	llp = pthread_getspecific(se->pipe_key);
2859 	if (llp != NULL)
2860 		fuse_ll_pipe_free(llp);
2861 	pthread_key_delete(se->pipe_key);
2862 	pthread_mutex_destroy(&se->lock);
2863 	free(se->cuse_data);
2864 	if (se->fd != -1)
2865 		close(se->fd);
2866 	destroy_mount_opts(se->mo);
2867 	free(se);
2868 }
2869 
2870 
fuse_ll_pipe_destructor(void * data)2871 static void fuse_ll_pipe_destructor(void *data)
2872 {
2873 	struct fuse_ll_pipe *llp = data;
2874 	fuse_ll_pipe_free(llp);
2875 }
2876 
fuse_session_receive_buf(struct fuse_session * se,struct fuse_buf * buf)2877 int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf)
2878 {
2879 	return fuse_session_receive_buf_int(se, buf, NULL);
2880 }
2881 
fuse_session_receive_buf_int(struct fuse_session * se,struct fuse_buf * buf,struct fuse_chan * ch)2882 int fuse_session_receive_buf_int(struct fuse_session *se, struct fuse_buf *buf,
2883 				 struct fuse_chan *ch)
2884 {
2885 	int err;
2886 	ssize_t res;
2887 #ifdef HAVE_SPLICE
2888 	size_t bufsize = se->bufsize;
2889 	struct fuse_ll_pipe *llp;
2890 	struct fuse_buf tmpbuf;
2891 
2892 	if (se->conn.proto_minor < 14 || !(se->conn.want & FUSE_CAP_SPLICE_READ))
2893 		goto fallback;
2894 
2895 	llp = fuse_ll_get_pipe(se);
2896 	if (llp == NULL)
2897 		goto fallback;
2898 
2899 	if (llp->size < bufsize) {
2900 		if (llp->can_grow) {
2901 			res = fcntl(llp->pipe[0], F_SETPIPE_SZ, bufsize);
2902 			if (res == -1) {
2903 				llp->can_grow = 0;
2904 				res = grow_pipe_to_max(llp->pipe[0]);
2905 				if (res > 0)
2906 					llp->size = res;
2907 				goto fallback;
2908 			}
2909 			llp->size = res;
2910 		}
2911 		if (llp->size < bufsize)
2912 			goto fallback;
2913 	}
2914 
2915 	res = splice(ch ? ch->fd : se->fd,
2916 		     NULL, llp->pipe[1], NULL, bufsize, 0);
2917 	err = errno;
2918 
2919 	if (fuse_session_exited(se))
2920 		return 0;
2921 
2922 	if (res == -1) {
2923 		if (err == ENODEV) {
2924 			/* Filesystem was unmounted, or connection was aborted
2925 			   via /sys/fs/fuse/connections */
2926 			fuse_session_exit(se);
2927 			return 0;
2928 		}
2929 		if (err != EINTR && err != EAGAIN)
2930 			perror("fuse: splice from device");
2931 		return -err;
2932 	}
2933 
2934 	if (res < sizeof(struct fuse_in_header)) {
2935 		fuse_log(FUSE_LOG_ERR, "short splice from fuse device\n");
2936 		return -EIO;
2937 	}
2938 
2939 	tmpbuf = (struct fuse_buf) {
2940 		.size = res,
2941 		.flags = FUSE_BUF_IS_FD,
2942 		.fd = llp->pipe[0],
2943 	};
2944 
2945 	/*
2946 	 * Don't bother with zero copy for small requests.
2947 	 * fuse_loop_mt() needs to check for FORGET so this more than
2948 	 * just an optimization.
2949 	 */
2950 	if (res < sizeof(struct fuse_in_header) +
2951 	    sizeof(struct fuse_write_in) + pagesize) {
2952 		struct fuse_bufvec src = { .buf[0] = tmpbuf, .count = 1 };
2953 		struct fuse_bufvec dst = { .count = 1 };
2954 
2955 		if (!buf->mem) {
2956 			buf->mem = malloc(se->bufsize);
2957 			if (!buf->mem) {
2958 				fuse_log(FUSE_LOG_ERR,
2959 					"fuse: failed to allocate read buffer\n");
2960 				return -ENOMEM;
2961 			}
2962 		}
2963 		buf->size = se->bufsize;
2964 		buf->flags = 0;
2965 		dst.buf[0] = *buf;
2966 
2967 		res = fuse_buf_copy(&dst, &src, 0);
2968 		if (res < 0) {
2969 			fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: %s\n",
2970 				strerror(-res));
2971 			fuse_ll_clear_pipe(se);
2972 			return res;
2973 		}
2974 		if (res < tmpbuf.size) {
2975 			fuse_log(FUSE_LOG_ERR, "fuse: copy from pipe: short read\n");
2976 			fuse_ll_clear_pipe(se);
2977 			return -EIO;
2978 		}
2979 		assert(res == tmpbuf.size);
2980 
2981 	} else {
2982 		/* Don't overwrite buf->mem, as that would cause a leak */
2983 		buf->fd = tmpbuf.fd;
2984 		buf->flags = tmpbuf.flags;
2985 	}
2986 	buf->size = tmpbuf.size;
2987 
2988 	return res;
2989 
2990 fallback:
2991 #endif
2992 	if (!buf->mem) {
2993 		buf->mem = malloc(se->bufsize);
2994 		if (!buf->mem) {
2995 			fuse_log(FUSE_LOG_ERR,
2996 				"fuse: failed to allocate read buffer\n");
2997 			return -ENOMEM;
2998 		}
2999 	}
3000 
3001 restart:
3002 	res = read(ch ? ch->fd : se->fd, buf->mem, se->bufsize);
3003 	err = errno;
3004 
3005 	if (fuse_session_exited(se))
3006 		return 0;
3007 	if (res == -1) {
3008 		/* ENOENT means the operation was interrupted, it's safe
3009 		   to restart */
3010 		if (err == ENOENT)
3011 			goto restart;
3012 
3013 		if (err == ENODEV) {
3014 			/* Filesystem was unmounted, or connection was aborted
3015 			   via /sys/fs/fuse/connections */
3016 			fuse_session_exit(se);
3017 			return 0;
3018 		}
3019 		/* Errors occurring during normal operation: EINTR (read
3020 		   interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
3021 		   umounted) */
3022 		if (err != EINTR && err != EAGAIN)
3023 			perror("fuse: reading device");
3024 		return -err;
3025 	}
3026 	if ((size_t) res < sizeof(struct fuse_in_header)) {
3027 		fuse_log(FUSE_LOG_ERR, "short read on fuse device\n");
3028 		return -EIO;
3029 	}
3030 
3031 	buf->size = res;
3032 
3033 	return res;
3034 }
3035 
fuse_session_new(struct fuse_args * args,const struct fuse_lowlevel_ops * op,size_t op_size,void * userdata)3036 struct fuse_session *fuse_session_new(struct fuse_args *args,
3037 				      const struct fuse_lowlevel_ops *op,
3038 				      size_t op_size, void *userdata)
3039 {
3040 	int err;
3041 	struct fuse_session *se;
3042 	struct mount_opts *mo;
3043 
3044 	if (sizeof(struct fuse_lowlevel_ops) < op_size) {
3045 		fuse_log(FUSE_LOG_ERR, "fuse: warning: library too old, some operations may not work\n");
3046 		op_size = sizeof(struct fuse_lowlevel_ops);
3047 	}
3048 
3049 	if (args->argc == 0) {
3050 		fuse_log(FUSE_LOG_ERR, "fuse: empty argv passed to fuse_session_new().\n");
3051 		return NULL;
3052 	}
3053 
3054 	se = (struct fuse_session *) calloc(1, sizeof(struct fuse_session));
3055 	if (se == NULL) {
3056 		fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n");
3057 		goto out1;
3058 	}
3059 	se->fd = -1;
3060 	se->conn.max_write = UINT_MAX;
3061 	se->conn.max_readahead = UINT_MAX;
3062 
3063 	/* Parse options */
3064 	if(fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1)
3065 		goto out2;
3066 	if(se->deny_others) {
3067 		/* Allowing access only by root is done by instructing
3068 		 * kernel to allow access by everyone, and then restricting
3069 		 * access to root and mountpoint owner in libfuse.
3070 		 */
3071 		// We may be adding the option a second time, but
3072 		// that doesn't hurt.
3073 		if(fuse_opt_add_arg(args, "-oallow_other") == -1)
3074 			goto out2;
3075 	}
3076 	mo = parse_mount_opts(args);
3077 	if (mo == NULL)
3078 		goto out3;
3079 
3080 	if(args->argc == 1 &&
3081 	   args->argv[0][0] == '-') {
3082 		fuse_log(FUSE_LOG_ERR, "fuse: warning: argv[0] looks like an option, but "
3083 			"will be ignored\n");
3084 	} else if (args->argc != 1) {
3085 		int i;
3086 		fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `");
3087 		for(i = 1; i < args->argc-1; i++)
3088 			fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]);
3089 		fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]);
3090 		goto out4;
3091 	}
3092 
3093 	if (se->debug)
3094 		fuse_log(FUSE_LOG_DEBUG, "FUSE library version: %s\n", PACKAGE_VERSION);
3095 
3096 	se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() +
3097 		FUSE_BUFFER_HEADER_SIZE;
3098 
3099 	list_init_req(&se->list);
3100 	list_init_req(&se->interrupts);
3101 	list_init_nreq(&se->notify_list);
3102 	se->notify_ctr = 1;
3103 	pthread_mutex_init(&se->lock, NULL);
3104 
3105 	err = pthread_key_create(&se->pipe_key, fuse_ll_pipe_destructor);
3106 	if (err) {
3107 		fuse_log(FUSE_LOG_ERR, "fuse: failed to create thread specific key: %s\n",
3108 			strerror(err));
3109 		goto out5;
3110 	}
3111 
3112 	memcpy(&se->op, op, op_size);
3113 	se->owner = getuid();
3114 	se->userdata = userdata;
3115 
3116 	se->mo = mo;
3117 	return se;
3118 
3119 out5:
3120 	pthread_mutex_destroy(&se->lock);
3121 out4:
3122 	fuse_opt_free_args(args);
3123 out3:
3124 	if (mo != NULL)
3125 		destroy_mount_opts(mo);
3126 out2:
3127 	free(se);
3128 out1:
3129 	return NULL;
3130 }
3131 
fuse_session_mount(struct fuse_session * se,const char * mountpoint)3132 int fuse_session_mount(struct fuse_session *se, const char *mountpoint)
3133 {
3134 	int fd;
3135 
3136 	/*
3137 	 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
3138 	 * would ensue.
3139 	 */
3140 	do {
3141 		fd = open("/dev/null", O_RDWR);
3142 		if (fd > 2)
3143 			close(fd);
3144 	} while (fd >= 0 && fd <= 2);
3145 
3146 	/*
3147 	 * To allow FUSE daemons to run without privileges, the caller may open
3148 	 * /dev/fuse before launching the file system and pass on the file
3149 	 * descriptor by specifying /dev/fd/N as the mount point. Note that the
3150 	 * parent process takes care of performing the mount in this case.
3151 	 */
3152 	fd = fuse_mnt_parse_fuse_fd(mountpoint);
3153 	if (fd != -1) {
3154 		if (fcntl(fd, F_GETFD) == -1) {
3155 			fuse_log(FUSE_LOG_ERR,
3156 				"fuse: Invalid file descriptor /dev/fd/%u\n",
3157 				fd);
3158 			return -1;
3159 		}
3160 		se->fd = fd;
3161 		return 0;
3162 	}
3163 
3164 	/* Open channel */
3165 	fd = fuse_kern_mount(mountpoint, se->mo);
3166 	if (fd == -1)
3167 		return -1;
3168 	se->fd = fd;
3169 
3170 	/* Save mountpoint */
3171 	se->mountpoint = strdup(mountpoint);
3172 	if (se->mountpoint == NULL)
3173 		goto error_out;
3174 
3175 	return 0;
3176 
3177 error_out:
3178 	fuse_kern_unmount(mountpoint, fd);
3179 	return -1;
3180 }
3181 
fuse_session_fd(struct fuse_session * se)3182 int fuse_session_fd(struct fuse_session *se)
3183 {
3184 	return se->fd;
3185 }
3186 
fuse_session_unmount(struct fuse_session * se)3187 void fuse_session_unmount(struct fuse_session *se)
3188 {
3189 	if (se->mountpoint != NULL) {
3190 		fuse_kern_unmount(se->mountpoint, se->fd);
3191 		se->fd = -1;
3192 		free(se->mountpoint);
3193 		se->mountpoint = NULL;
3194 	}
3195 }
3196 
3197 #ifdef linux
fuse_req_getgroups(fuse_req_t req,int size,gid_t list[])3198 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3199 {
3200 	char *buf;
3201 	size_t bufsize = 1024;
3202 	char path[128];
3203 	int ret;
3204 	int fd;
3205 	unsigned long pid = req->ctx.pid;
3206 	char *s;
3207 
3208 	sprintf(path, "/proc/%lu/task/%lu/status", pid, pid);
3209 
3210 retry:
3211 	buf = malloc(bufsize);
3212 	if (buf == NULL)
3213 		return -ENOMEM;
3214 
3215 	ret = -EIO;
3216 	fd = open(path, O_RDONLY);
3217 	if (fd == -1)
3218 		goto out_free;
3219 
3220 	ret = read(fd, buf, bufsize);
3221 	close(fd);
3222 	if (ret < 0) {
3223 		ret = -EIO;
3224 		goto out_free;
3225 	}
3226 
3227 	if ((size_t)ret == bufsize) {
3228 		free(buf);
3229 		bufsize *= 4;
3230 		goto retry;
3231 	}
3232 
3233 	ret = -EIO;
3234 	s = strstr(buf, "\nGroups:");
3235 	if (s == NULL)
3236 		goto out_free;
3237 
3238 	s += 8;
3239 	ret = 0;
3240 	while (1) {
3241 		char *end;
3242 		unsigned long val = strtoul(s, &end, 0);
3243 		if (end == s)
3244 			break;
3245 
3246 		s = end;
3247 		if (ret < size)
3248 			list[ret] = val;
3249 		ret++;
3250 	}
3251 
3252 out_free:
3253 	free(buf);
3254 	return ret;
3255 }
3256 #else /* linux */
3257 /*
3258  * This is currently not implemented on other than Linux...
3259  */
fuse_req_getgroups(fuse_req_t req,int size,gid_t list[])3260 int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[])
3261 {
3262 	(void) req; (void) size; (void) list;
3263 	return -ENOSYS;
3264 }
3265 #endif
3266 
3267 /* Prevent spurious data race warning - we don't care
3268  * about races for this flag */
3269 __attribute__((no_sanitize_thread))
fuse_session_exit(struct fuse_session * se)3270 void fuse_session_exit(struct fuse_session *se)
3271 {
3272 	se->exited = 1;
3273 }
3274 
3275 __attribute__((no_sanitize_thread))
fuse_session_reset(struct fuse_session * se)3276 void fuse_session_reset(struct fuse_session *se)
3277 {
3278 	se->exited = 0;
3279 	se->error = 0;
3280 }
3281 
3282 __attribute__((no_sanitize_thread))
fuse_session_exited(struct fuse_session * se)3283 int fuse_session_exited(struct fuse_session *se)
3284 {
3285 	return se->exited;
3286 }
3287