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