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