xref: /openbsd-src/sys/miscfs/fuse/fusebuf.c (revision b66b9ef836b4335d57b8481be6faf1a0b9b7e205)
1*b66b9ef8Sjsg /* $OpenBSD: fusebuf.c,v 1.18 2021/03/11 13:31:35 jsg Exp $ */
2a2badd06Stedu /*
3a2badd06Stedu  * Copyright (c) 2012-2013 Sylvestre Gallon <ccna.syl@gmail.com>
4a2badd06Stedu  *
5a2badd06Stedu  * Permission to use, copy, modify, and distribute this software for any
6a2badd06Stedu  * purpose with or without fee is hereby granted, provided that the above
7a2badd06Stedu  * copyright notice and this permission notice appear in all copies.
8a2badd06Stedu  *
9a2badd06Stedu  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10a2badd06Stedu  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11a2badd06Stedu  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12a2badd06Stedu  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13a2badd06Stedu  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14a2badd06Stedu  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15a2badd06Stedu  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16a2badd06Stedu  */
17a2badd06Stedu 
18a2badd06Stedu #include <sys/param.h>
19413ec31cShelg #include <sys/filedesc.h>
20a2badd06Stedu #include <sys/kernel.h>
2137318181Ssyl #include <sys/malloc.h>
22a2badd06Stedu #include <sys/pool.h>
23413ec31cShelg #include <sys/proc.h>
246f9b5942Snatano #include <sys/stat.h>
25a2badd06Stedu #include <sys/statvfs.h>
26a2badd06Stedu #include <sys/systm.h>
27a2badd06Stedu #include <sys/vnode.h>
28a2badd06Stedu #include <sys/fusebuf.h>
29a2badd06Stedu 
30a2badd06Stedu #include "fusefs_node.h"
31a2badd06Stedu #include "fusefs.h"
32a2badd06Stedu 
33a2badd06Stedu struct fusebuf *
fb_setup(size_t len,ino_t ino,int op,struct proc * p)34a2badd06Stedu fb_setup(size_t len, ino_t ino, int op, struct proc *p)
35a2badd06Stedu {
36a2badd06Stedu 	struct fusebuf *fbuf;
37a2badd06Stedu 
38a2badd06Stedu 	fbuf = pool_get(&fusefs_fbuf_pool, PR_WAITOK | PR_ZERO);
39a2badd06Stedu 	fbuf->fb_len = len;
40a2badd06Stedu 	fbuf->fb_err = 0;
41ebb7fa22Sderaadt 	arc4random_buf(&fbuf->fb_uuid, sizeof fbuf->fb_uuid);
42a2badd06Stedu 	fbuf->fb_type = op;
43a2badd06Stedu 	fbuf->fb_ino = ino;
44413ec31cShelg 	/*
45413ec31cShelg 	 * When exposed to userspace, thread IDs have THREAD_PID_OFFSET added
46413ec31cShelg 	 * to keep them from overlapping the PID range.
47413ec31cShelg 	 */
48413ec31cShelg 	fbuf->fb_tid = p->p_tid + THREAD_PID_OFFSET;
49413ec31cShelg 	fbuf->fb_uid = p->p_ucred->cr_uid;
50413ec31cShelg 	fbuf->fb_gid = p->p_ucred->cr_gid;
51413ec31cShelg 	fbuf->fb_umask = p->p_p->ps_fd->fd_cmask;
521c8e8de3Ssyl 	if (len == 0)
531c8e8de3Ssyl 		fbuf->fb_dat = NULL;
541c8e8de3Ssyl 	else
551c8e8de3Ssyl 		fbuf->fb_dat = (uint8_t *)malloc(len, M_FUSEFS,
561c8e8de3Ssyl 		    M_WAITOK | M_ZERO);
57a2badd06Stedu 
58a2badd06Stedu 	return (fbuf);
59a2badd06Stedu }
60a2badd06Stedu 
6113907cdeShelg /*
6213907cdeShelg  * Puts the fbuf on the queue and waits for the file system to process
6313907cdeShelg  * it. The current process will block indefinitely and cannot be
6413907cdeShelg  * interrupted or killed. This is consistent with how VFS system calls
6513907cdeShelg  * should behave. nfs supports the -ointr or -i mount option and FUSE
6613907cdeShelg  * can too but this is non-trivial. The file system daemon must be
6713907cdeShelg  * multi-threaded and also support being interrupted. Note that
68*b66b9ef8Sjsg  * libfuse currently only supports single-threaded daemons.
6913907cdeShelg  *
7013907cdeShelg  * Why not timeout similar to mount_nfs -osoft?
7113907cdeShelg  * It introduces another point of failure and a possible mount option
7213907cdeShelg  * (to specify the timeout) that users need to understand and tune to
7313907cdeShelg  * avoid premature timeouts for slow file systems. More complexity,
7413907cdeShelg  * less reliability.
7513907cdeShelg  *
7613907cdeShelg  * In the case where the daemon has become unresponsive the daemon
7713907cdeShelg  * will have to be killed in order for the current process to
7813907cdeShelg  * wakeup. The FUSE device is automatically closed when the daemon
7913907cdeShelg  * terminates and any waiting fbuf is woken up.
8013907cdeShelg  */
81a2badd06Stedu int
fb_queue(dev_t dev,struct fusebuf * fbuf)82a2badd06Stedu fb_queue(dev_t dev, struct fusebuf *fbuf)
83a2badd06Stedu {
84a2badd06Stedu 	fuse_device_queue_fbuf(dev, fbuf);
8583824c38Smpi 	tsleep_nsec(fbuf, PWAIT, "fuse", INFSLP);
86a2badd06Stedu 
87a2badd06Stedu 	return (fbuf->fb_err);
88a2badd06Stedu }
8937318181Ssyl 
9037318181Ssyl void
fb_delete(struct fusebuf * fbuf)9137318181Ssyl fb_delete(struct fusebuf *fbuf)
9237318181Ssyl {
9337318181Ssyl 	if (fbuf != NULL) {
94903f970aShelg 		free(fbuf->fb_dat, M_FUSEFS, fbuf->fb_len);
9537318181Ssyl 		pool_put(&fusefs_fbuf_pool, fbuf);
9637318181Ssyl 	}
9737318181Ssyl }
98