1 /* $OpenBSD: fusebuf.c,v 1.18 2021/03/11 13:31:35 jsg Exp $ */
2 /*
3 * Copyright (c) 2012-2013 Sylvestre Gallon <ccna.syl@gmail.com>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 #include <sys/param.h>
19 #include <sys/filedesc.h>
20 #include <sys/kernel.h>
21 #include <sys/malloc.h>
22 #include <sys/pool.h>
23 #include <sys/proc.h>
24 #include <sys/stat.h>
25 #include <sys/statvfs.h>
26 #include <sys/systm.h>
27 #include <sys/vnode.h>
28 #include <sys/fusebuf.h>
29
30 #include "fusefs_node.h"
31 #include "fusefs.h"
32
33 struct fusebuf *
fb_setup(size_t len,ino_t ino,int op,struct proc * p)34 fb_setup(size_t len, ino_t ino, int op, struct proc *p)
35 {
36 struct fusebuf *fbuf;
37
38 fbuf = pool_get(&fusefs_fbuf_pool, PR_WAITOK | PR_ZERO);
39 fbuf->fb_len = len;
40 fbuf->fb_err = 0;
41 arc4random_buf(&fbuf->fb_uuid, sizeof fbuf->fb_uuid);
42 fbuf->fb_type = op;
43 fbuf->fb_ino = ino;
44 /*
45 * When exposed to userspace, thread IDs have THREAD_PID_OFFSET added
46 * to keep them from overlapping the PID range.
47 */
48 fbuf->fb_tid = p->p_tid + THREAD_PID_OFFSET;
49 fbuf->fb_uid = p->p_ucred->cr_uid;
50 fbuf->fb_gid = p->p_ucred->cr_gid;
51 fbuf->fb_umask = p->p_p->ps_fd->fd_cmask;
52 if (len == 0)
53 fbuf->fb_dat = NULL;
54 else
55 fbuf->fb_dat = (uint8_t *)malloc(len, M_FUSEFS,
56 M_WAITOK | M_ZERO);
57
58 return (fbuf);
59 }
60
61 /*
62 * Puts the fbuf on the queue and waits for the file system to process
63 * it. The current process will block indefinitely and cannot be
64 * interrupted or killed. This is consistent with how VFS system calls
65 * should behave. nfs supports the -ointr or -i mount option and FUSE
66 * can too but this is non-trivial. The file system daemon must be
67 * multi-threaded and also support being interrupted. Note that
68 * libfuse currently only supports single-threaded daemons.
69 *
70 * Why not timeout similar to mount_nfs -osoft?
71 * It introduces another point of failure and a possible mount option
72 * (to specify the timeout) that users need to understand and tune to
73 * avoid premature timeouts for slow file systems. More complexity,
74 * less reliability.
75 *
76 * In the case where the daemon has become unresponsive the daemon
77 * will have to be killed in order for the current process to
78 * wakeup. The FUSE device is automatically closed when the daemon
79 * terminates and any waiting fbuf is woken up.
80 */
81 int
fb_queue(dev_t dev,struct fusebuf * fbuf)82 fb_queue(dev_t dev, struct fusebuf *fbuf)
83 {
84 fuse_device_queue_fbuf(dev, fbuf);
85 tsleep_nsec(fbuf, PWAIT, "fuse", INFSLP);
86
87 return (fbuf->fb_err);
88 }
89
90 void
fb_delete(struct fusebuf * fbuf)91 fb_delete(struct fusebuf *fbuf)
92 {
93 if (fbuf != NULL) {
94 free(fbuf->fb_dat, M_FUSEFS, fbuf->fb_len);
95 pool_put(&fusefs_fbuf_pool, fbuf);
96 }
97 }
98