| 1 | |
| 2 | *
|
| 3 | * FUSE: Filesystem in Userspace
|
| 4 | * Copyright (C) 2001-2008 Miklos Szeredi <[email protected]>
|
| 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 |
|
| 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 |
|
| 76 | struct list_head intr_entry;
|
| 77 |
|
| 78 |
|
| 79 | struct fuse_args *args;
|
| 80 |
|
| 81 |
|
| 82 | refcount_t count;
|
| 83 |
|
| 84 |
|
| 85 | unsigned long flags;
|
| 86 |
|
| 87 |
|
| 88 | struct {
|
| 89 |
|
| 90 | struct fuse_in_header h;
|
| 91 | } in;
|
| 92 |
|
| 93 |
|
| 94 | struct {
|
| 95 |
|
| 96 | struct fuse_out_header h;
|
| 97 | } out;
|
| 98 |
|
| 99 |
|
| 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 |
|
| 111 | struct fuse_chan *chan;
|
| 112 |
|
| 113 | #ifdef CONFIG_FUSE_IO_URING
|
| 114 | void *ring_entry;
|
| 115 | void *ring_queue;
|
| 116 | #endif
|
| 117 |
|
| 118 | unsigned long create_time;
|
| 119 | };
|
| 120 |
|
| 121 |
|
| 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 |
|
| 159 | unsigned connected;
|
| 160 |
|
| 161 |
|
| 162 | spinlock_t lock;
|
| 163 |
|
| 164 |
|
| 165 | wait_queue_head_t waitq;
|
| 166 |
|
| 167 |
|
| 168 | u64 reqctr;
|
| 169 |
|
| 170 |
|
| 171 | struct list_head pending;
|
| 172 |
|
| 173 |
|
| 174 | struct list_head interrupts;
|
| 175 |
|
| 176 |
|
| 177 | struct fuse_forget_link forget_list_head;
|
| 178 | struct fuse_forget_link *forget_list_tail;
|
| 179 |
|
| 180 |
|
| 181 | int forget_batch;
|
| 182 |
|
| 183 |
|
| 184 | struct fasync_struct *fasync;
|
| 185 |
|
| 186 |
|
| 187 | const struct fuse_iqueue_ops *ops;
|
| 188 |
|
| 189 |
|
| 190 | void *priv;
|
| 191 | };
|
| 192 |
|
| 193 | struct fuse_chan {
|
| 194 | |
| 195 | - devices
|
| 196 | - connected
|
| 197 | - ring
|
| 198 | - ring->queues[qid]
|
| 199 |
|
| 200 | spinlock_t lock;
|
| 201 |
|
| 202 |
|
| 203 | struct fuse_conn *conn;
|
| 204 |
|
| 205 |
|
| 206 | struct fuse_iqueue iq;
|
| 207 |
|
| 208 |
|
| 209 | struct list_head devices;
|
| 210 |
|
| 211 |
|
| 212 | unsigned max_background;
|
| 213 |
|
| 214 |
|
| 215 | unsigned num_background;
|
| 216 |
|
| 217 |
|
| 218 | unsigned active_background;
|
| 219 |
|
| 220 |
|
| 221 | struct list_head bg_queue;
|
| 222 |
|
| 223 |
|
| 224 | spinlock_t bg_lock;
|
| 225 |
|
| 226 | |
| 227 | * any fuse request will be suspended until the flag is set
|
| 228 | int initialized;
|
| 229 |
|
| 230 | |
| 231 | the case before the INIT reply is received, and if there
|
| 232 | are too many outstading backgrounds requests
|
| 233 | int blocked;
|
| 234 |
|
| 235 |
|
| 236 | wait_queue_head_t blocked_waitq;
|
| 237 |
|
| 238 | |
| 239 | abort and device release
|
| 240 | unsigned connected;
|
| 241 |
|
| 242 |
|
| 243 | atomic_t num_waiting;
|
| 244 |
|
| 245 |
|
| 246 | bool no_interrupt;
|
| 247 |
|
| 248 |
|
| 249 | unsigned int io_uring;
|
| 250 |
|
| 251 |
|
| 252 | unsigned int minor;
|
| 253 |
|
| 254 |
|
| 255 | unsigned int max_write;
|
| 256 |
|
| 257 |
|
| 258 | unsigned int max_pages;
|
| 259 |
|
| 260 |
|
| 261 | struct list_head *pq_prealloc;
|
| 262 |
|
| 263 |
|
| 264 | bool abort_with_err;
|
| 265 |
|
| 266 | #ifdef CONFIG_FUSE_IO_URING
|
| 267 |
|
| 268 | struct fuse_ring *ring;
|
| 269 | #endif
|
| 270 |
|
| 271 |
|
| 272 | struct {
|
| 273 |
|
| 274 | struct delayed_work work;
|
| 275 |
|
| 276 |
|
| 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 |
|
| 286 | unsigned connected;
|
| 287 |
|
| 288 |
|
| 289 | spinlock_t lock;
|
| 290 |
|
| 291 |
|
| 292 | struct list_head *processing;
|
| 293 |
|
| 294 |
|
| 295 | struct list_head io;
|
| 296 | };
|
| 297 |
|
| 298 | |
| 299 | * struct fuse_dev - Fuse device instance
|
| 300 |
|
| 301 | struct fuse_dev {
|
| 302 |
|
| 303 | refcount_t ref;
|
| 304 |
|
| 305 |
|
| 306 | bool sync_init;
|
| 307 |
|
| 308 |
|
| 309 | struct fuse_chan *chan;
|
| 310 |
|
| 311 |
|
| 312 | struct fuse_pqueue pq;
|
| 313 |
|
| 314 |
|
| 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 |
|
| 332 | bool skip_folio_copy:1;
|
| 333 | struct {
|
| 334 | unsigned int copied_sz;
|
| 335 | } ring;
|
| 336 | };
|
| 337 |
|
| 338 |
|
| 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 |
|
| 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 |
|