xref: /linux/fs/fuse/fuse_dev_i.h (revision 1b78070aaef63512688aebfbc82365ef9d6660f1)
1 /* SPDX-License-Identifier: GPL-2.0
2  *
3  * FUSE: Filesystem in Userspace
4  * Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
5  */
6 #ifndef _FS_FUSE_DEV_I_H
7 #define _FS_FUSE_DEV_I_H
8 
9 #include <linux/fuse.h>
10 #include <linux/types.h>
11 #include <linux/refcount.h>
12 #include <linux/wait.h>
13 #include <linux/workqueue.h>
14 #include <linux/fs.h>
15 
16 /* Ordinary requests have even IDs, while interrupts IDs are odd */
17 #define FUSE_INT_REQ_BIT (1ULL << 0)
18 #define FUSE_REQ_ID_STEP (1ULL << 1)
19 
20 struct fuse_arg;
21 struct fuse_args;
22 struct fuse_pqueue;
23 struct fuse_iqueue;
24 
25 /**
26  * enum fuse_req_flag - Request flags
27  *
28  * @FR_ISREPLY:		set if the request has reply
29  * @FR_FORCE:		force sending of the request even if interrupted
30  * @FR_BACKGROUND:	request is sent in the background
31  * @FR_WAITING:		request is counted as "waiting"
32  * @FR_ABORTED:		the request was aborted
33  * @FR_INTERRUPTED:	the request has been interrupted
34  * @FR_LOCKED:		data is being copied to/from the request
35  * @FR_PENDING:		request is not yet in userspace
36  * @FR_SENT:		request is in userspace, waiting for an answer
37  * @FR_FINISHED:	request is finished
38  * @FR_PRIVATE:		request is on private list
39  * @FR_ASYNC:		request is asynchronous
40  * @FR_URING:		request is handled through fuse-io-uring
41  * @FR_SYNC_WAKEUP:	use synchronous wakeup when queueing this request to
42  *			give the scheduler a hint about the waker task
43  */
44 enum fuse_req_flag {
45 	FR_ISREPLY,
46 	FR_FORCE,
47 	FR_BACKGROUND,
48 	FR_WAITING,
49 	FR_ABORTED,
50 	FR_INTERRUPTED,
51 	FR_LOCKED,
52 	FR_PENDING,
53 	FR_SENT,
54 	FR_FINISHED,
55 	FR_PRIVATE,
56 	FR_ASYNC,
57 	FR_URING,
58 	FR_SYNC_WAKEUP,
59 };
60 
61 /**
62  * struct fuse_req - A request to the client
63  *
64  * .waitq.lock protects the following fields:
65  *   - FR_ABORTED
66  *   - FR_LOCKED (may also be modified under fpq->lock, tested under both)
67  */
68 struct fuse_req {
69 	/**
70 	 * @list: This can be on either pending processing or io lists in
71 	 * fuse_conn
72 	 */
73 	struct list_head list;
74 
75 	/** @intr_entry: Entry on the interrupts list  */
76 	struct list_head intr_entry;
77 
78 	/** @args: Input/output arguments */
79 	struct fuse_args *args;
80 
81 	/** @count: refcount */
82 	refcount_t count;
83 
84 	/** @flags: Request flags, updated with test/set/clear_bit() */
85 	unsigned long flags;
86 
87 	/** @in: The request input header */
88 	struct {
89 		/** @in.h: The request input header */
90 		struct fuse_in_header h;
91 	} in;
92 
93 	/** @out: The request output header */
94 	struct {
95 		/** @out.h: The request output header */
96 		struct fuse_out_header h;
97 	} out;
98 
99 	/** @waitq: Used to wake up the task waiting for completion of request */
100 	wait_queue_head_t waitq;
101 
102 #if IS_ENABLED(CONFIG_VIRTIO_FS)
103 	/**
104 	 * @argbuf: virtio-fs's physically contiguous buffer for in and out
105 	 * args
106 	 */
107 	void *argbuf;
108 #endif
109 
110 	/** @chan: fuse_chan this request belongs to */
111 	struct fuse_chan *chan;
112 
113 #ifdef CONFIG_FUSE_IO_URING
114 	void *ring_entry;
115 	void *ring_queue;
116 #endif
117 	/** @create_time: When (in jiffies) the request was created */
118 	unsigned long create_time;
119 };
120 
121 /* One forget request */
122 struct fuse_forget_link {
123 	struct fuse_forget_one forget_one;
124 	struct fuse_forget_link *next;
125 };
126 
127 /**
128  * struct fuse_iqueue_ops - Input queue callbacks
129  *
130  * Input queue signalling is device-specific.  For example, the /dev/fuse file
131  * uses fiq->waitq and fasync to wake processes that are waiting on queue
132  * readiness.  These callbacks allow other device types to respond to input
133  * queue activity.
134  */
135 struct fuse_iqueue_ops {
136 	/**
137 	 * @send_forget: Send one forget
138 	 */
139 	void (*send_forget)(struct fuse_iqueue *fiq, struct fuse_forget_link *link);
140 
141 	/**
142 	 * @send_interrupt: Send interrupt for request
143 	 */
144 	void (*send_interrupt)(struct fuse_iqueue *fiq, struct fuse_req *req);
145 
146 	/**
147 	 * @send_req: Send one request
148 	 */
149 	void (*send_req)(struct fuse_iqueue *fiq, struct fuse_req *req);
150 
151 	/**
152 	 * @release: Clean up when fuse_iqueue is destroyed
153 	 */
154 	void (*release)(struct fuse_iqueue *fiq);
155 };
156 
157 struct fuse_iqueue {
158 	/** Connection established */
159 	unsigned connected;
160 
161 	/** Lock protecting accesses to members of this structure */
162 	spinlock_t lock;
163 
164 	/** Readers of the connection are waiting on this */
165 	wait_queue_head_t waitq;
166 
167 	/** The next unique request id */
168 	u64 reqctr;
169 
170 	/** The list of pending requests */
171 	struct list_head pending;
172 
173 	/** Pending interrupts */
174 	struct list_head interrupts;
175 
176 	/** Queue of pending forgets */
177 	struct fuse_forget_link forget_list_head;
178 	struct fuse_forget_link *forget_list_tail;
179 
180 	/** Batching of FORGET requests (positive indicates FORGET batch) */
181 	int forget_batch;
182 
183 	/** O_ASYNC requests */
184 	struct fasync_struct *fasync;
185 
186 	/** Device-specific callbacks */
187 	const struct fuse_iqueue_ops *ops;
188 
189 	/** Device-specific state */
190 	void *priv;
191 };
192 
193 struct fuse_chan {
194 	/** Lock protecting:
195 	    - devices
196 	    - connected
197 	    - ring
198 	    - ring->queues[qid]
199 	 */
200 	spinlock_t lock;
201 
202 	/* back pointer: fc->chan->conn == fc */
203 	struct fuse_conn *conn;
204 
205 	/** Input queue */
206 	struct fuse_iqueue iq;
207 
208 	/** List of device instances belonging to this connection */
209 	struct list_head devices;
210 
211 	/** Maximum number of outstanding background requests */
212 	unsigned max_background;
213 
214 	/** Number of requests currently in the background */
215 	unsigned num_background;
216 
217 	/** Number of background requests currently queued for userspace */
218 	unsigned active_background;
219 
220 	/** The list of background requests set aside for later queuing */
221 	struct list_head bg_queue;
222 
223 	/** Protects: max_background, num_background, active_background, bg_queue, blocked */
224 	spinlock_t bg_lock;
225 
226 	/** Flag indicating that INIT reply has been received. Allocating
227 	 * any fuse request will be suspended until the flag is set */
228 	int initialized;
229 
230 	/** Flag indicating if connection is blocked.  This will be
231 	    the case before the INIT reply is received, and if there
232 	    are too many outstading backgrounds requests */
233 	int blocked;
234 
235 	/** waitq for blocked connection */
236 	wait_queue_head_t blocked_waitq;
237 
238 	/** Connection established, cleared on umount, connection
239 	    abort and device release */
240 	unsigned connected;
241 
242 	/** The number of requests waiting for completion */
243 	atomic_t num_waiting;
244 
245 	/** Is interrupt not implemented by fs? */
246 	bool no_interrupt;
247 
248 	/* Use io_uring for communication */
249 	unsigned int io_uring;
250 
251 	/* Negotiated minor version */
252 	unsigned int minor;
253 
254 	/* Maximum write size */
255 	unsigned int max_write;
256 
257 	/* Maximum number of pages that can be used in a single request */
258 	unsigned int max_pages;
259 
260 	/* Before being installed into fud, contains the preallocated pq array*/
261 	struct list_head *pq_prealloc;
262 
263 	/** Connection aborted via sysfs, respond with ECONNABORTED on device I/O */
264 	bool abort_with_err;
265 
266 #ifdef CONFIG_FUSE_IO_URING
267 	/**  uring connection information*/
268 	struct fuse_ring *ring;
269 #endif
270 
271 	/** Only used if the connection opts into request timeouts */
272 	struct {
273 		/* Worker for checking if any requests have timed out */
274 		struct delayed_work work;
275 
276 		/* Request timeout (in jiffies). 0 = no timeout */
277 		unsigned int req_timeout;
278 	} timeout;
279 };
280 
281 #define FUSE_PQ_HASH_BITS 8
282 #define FUSE_PQ_HASH_SIZE (1 << FUSE_PQ_HASH_BITS)
283 
284 struct fuse_pqueue {
285 	/** Connection established */
286 	unsigned connected;
287 
288 	/** Lock protecting accessess to  members of this structure */
289 	spinlock_t lock;
290 
291 	/** Hash table of requests being processed */
292 	struct list_head *processing;
293 
294 	/** The list of requests under I/O */
295 	struct list_head io;
296 };
297 
298 /**
299  * struct fuse_dev - Fuse device instance
300  */
301 struct fuse_dev {
302 	/** @ref: Reference count of this object */
303 	refcount_t ref;
304 
305 	/** @sync_init: Issue FUSE_INIT synchronously */
306 	bool sync_init;
307 
308 	/** @chan: Fuse channel for this device */
309 	struct fuse_chan *chan;
310 
311 	/** @pq: Processing queue */
312 	struct fuse_pqueue pq;
313 
314 	/** @entry: list entry on fch->devices */
315 	struct list_head entry;
316 };
317 
318 struct fuse_copy_state {
319 	struct fuse_req *req;
320 	struct iov_iter *iter;
321 	struct pipe_buffer *pipebufs;
322 	struct pipe_buffer *currbuf;
323 	struct pipe_inode_info *pipe;
324 	unsigned long nr_segs;
325 	struct page *pg;
326 	unsigned int len;
327 	unsigned int offset;
328 	bool write:1;
329 	bool move_folios:1;
330 	bool is_uring:1;
331 	/* set when the payload is zero-copied. folios are filled in place */
332 	bool skip_folio_copy:1;
333 	struct {
334 		unsigned int copied_sz; /* copied size into the user buffer */
335 	} ring;
336 };
337 
338 /* fud->chan gets assigned to this value when /dev/fuse is closed */
339 #define FUSE_DEV_CHAN_DISCONNECTED ((struct fuse_chan *) 1)
340 
341 /*
342  * Lockless access is OK, because fud->chan is set once during mount and is valid
343  * until the file is released.
344  *
345  * fud->chan is set to FUSE_DEV_CHAN_DISCONNECTED only after the containing file is
346  * released, so result is safe to dereference in most cases.  Exceptions are:
347  * fuse_dev_put() and fuse_fill_super_common().
348  */
349 static inline struct fuse_chan *fuse_dev_chan_get(struct fuse_dev *fud)
350 {
351 	/* Pairs with xchg() in fuse_dev_install() */
352 	return smp_load_acquire(&fud->chan);
353 }
354 
355 static inline struct fuse_dev *fuse_file_to_fud(struct file *file)
356 {
357 	return file->private_data;
358 }
359 
360 static inline struct fuse_dev *__fuse_get_dev(struct file *file)
361 {
362 	struct fuse_dev *fud = fuse_file_to_fud(file);
363 
364 	if (!fuse_dev_chan_get(fud))
365 		return NULL;
366 
367 	return fud;
368 }
369 
370 void fuse_iqueue_init(struct fuse_iqueue *fiq, const struct fuse_iqueue_ops *ops, void *priv);
371 
372 struct fuse_dev *fuse_get_dev(struct file *file);
373 
374 unsigned int fuse_req_hash(u64 unique);
375 struct fuse_req *fuse_request_find(struct fuse_pqueue *fpq, u64 unique);
376 
377 void fuse_dev_end_requests(struct list_head *head);
378 void fuse_request_bg_finish(struct fuse_chan *fch, struct fuse_req *req);
379 
380 void fuse_copy_init(struct fuse_copy_state *cs, bool write,
381 			   struct iov_iter *iter);
382 /*
383  * Return the number of bytes in an arguments list
384  */
385 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args);
386 
387 int fuse_copy_args(struct fuse_copy_state *cs, unsigned int numargs,
388 		   unsigned int argpages, struct fuse_arg *args,
389 		   int zeroing);
390 int fuse_copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
391 		       unsigned int nbytes);
392 void fuse_dev_queue_forget(struct fuse_iqueue *fiq,
393 			   struct fuse_forget_link *forget);
394 void fuse_dev_queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req);
395 bool fuse_remove_pending_req(struct fuse_req *req, spinlock_t *lock);
396 
397 bool fuse_request_expired(struct fuse_chan *fch, struct list_head *list);
398 
399 /*
400  * Assign a unique id to a fuse request
401  */
402 void fuse_request_assign_unique(struct fuse_iqueue *fiq, struct fuse_req *req);
403 
404 /*
405  * Get the next unique ID for a request
406  */
407 u64 fuse_get_unique(struct fuse_iqueue *fiq);
408 
409 struct fuse_dev *fuse_dev_alloc_install(struct fuse_chan *fch);
410 struct fuse_dev *fuse_dev_alloc(void);
411 
412 int fuse_dev_release(struct inode *inode, struct file *file);
413 
414 struct list_head *fuse_pqueue_alloc(void);
415 
416 /*
417  * Initialize the fuse processing queue
418  */
419 void fuse_pqueue_init(struct fuse_pqueue *fpq);
420 
421 /*
422  * End a finished request
423  */
424 void fuse_request_end(struct fuse_req *req);
425 
426 #endif
427 
428