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