Commit | Line | Data |
---|---|---|
d8a5ba45 MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
d7133114 | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
d8a5ba45 MS |
4 | |
5 | This program can be distributed under the terms of the GNU GPL. | |
6 | See the file COPYING. | |
7 | */ | |
8 | ||
9 | #include <linux/fuse.h> | |
10 | #include <linux/fs.h> | |
51eb01e7 | 11 | #include <linux/mount.h> |
d8a5ba45 MS |
12 | #include <linux/wait.h> |
13 | #include <linux/list.h> | |
14 | #include <linux/spinlock.h> | |
15 | #include <linux/mm.h> | |
16 | #include <linux/backing-dev.h> | |
bafa9654 | 17 | #include <linux/mutex.h> |
d8a5ba45 | 18 | |
334f485d MS |
19 | /** Max number of pages that can be used in a single read request */ |
20 | #define FUSE_MAX_PAGES_PER_REQ 32 | |
21 | ||
08a53cdc | 22 | /** Maximum number of outstanding background requests */ |
f92b99b9 MS |
23 | #define FUSE_MAX_BACKGROUND 12 |
24 | ||
25 | /** Congestion starts at 75% of maximum */ | |
26 | #define FUSE_CONGESTION_THRESHOLD (FUSE_MAX_BACKGROUND * 75 / 100) | |
08a53cdc | 27 | |
1d3d752b MS |
28 | /** It could be as large as PATH_MAX, but would that have any uses? */ |
29 | #define FUSE_NAME_MAX 1024 | |
30 | ||
bafa9654 MS |
31 | /** Number of dentries for each connection in the control filesystem */ |
32 | #define FUSE_CTL_NUM_DENTRIES 3 | |
33 | ||
1e9a4ed9 MS |
34 | /** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem |
35 | module will check permissions based on the file mode. Otherwise no | |
36 | permission checking is done in the kernel */ | |
37 | #define FUSE_DEFAULT_PERMISSIONS (1 << 0) | |
38 | ||
39 | /** If the FUSE_ALLOW_OTHER flag is given, then not only the user | |
40 | doing the mount will be allowed to access the filesystem */ | |
41 | #define FUSE_ALLOW_OTHER (1 << 1) | |
42 | ||
bafa9654 MS |
43 | /** List of active connections */ |
44 | extern struct list_head fuse_conn_list; | |
45 | ||
46 | /** Global mutex protecting fuse_conn_list and the control filesystem */ | |
47 | extern struct mutex fuse_mutex; | |
413ef8cb | 48 | |
d8a5ba45 MS |
49 | /** FUSE inode */ |
50 | struct fuse_inode { | |
51 | /** Inode data */ | |
52 | struct inode inode; | |
53 | ||
54 | /** Unique ID, which identifies the inode between userspace | |
55 | * and kernel */ | |
56 | u64 nodeid; | |
57 | ||
9e6268db MS |
58 | /** Number of lookups on this inode */ |
59 | u64 nlookup; | |
60 | ||
e5e5558e MS |
61 | /** The request used for sending the FORGET message */ |
62 | struct fuse_req *forget_req; | |
63 | ||
d8a5ba45 | 64 | /** Time in jiffies until the file attributes are valid */ |
0a0898cf | 65 | u64 i_time; |
d8a5ba45 MS |
66 | }; |
67 | ||
b6aeaded MS |
68 | /** FUSE specific file data */ |
69 | struct fuse_file { | |
70 | /** Request reserved for flush and release */ | |
33649c91 | 71 | struct fuse_req *reserved_req; |
b6aeaded MS |
72 | |
73 | /** File handle used by userspace */ | |
74 | u64 fh; | |
75 | }; | |
76 | ||
334f485d MS |
77 | /** One input argument of a request */ |
78 | struct fuse_in_arg { | |
79 | unsigned size; | |
80 | const void *value; | |
81 | }; | |
82 | ||
83 | /** The request input */ | |
84 | struct fuse_in { | |
85 | /** The request header */ | |
86 | struct fuse_in_header h; | |
87 | ||
88 | /** True if the data for the last argument is in req->pages */ | |
89 | unsigned argpages:1; | |
90 | ||
91 | /** Number of arguments */ | |
92 | unsigned numargs; | |
93 | ||
94 | /** Array of arguments */ | |
95 | struct fuse_in_arg args[3]; | |
96 | }; | |
97 | ||
98 | /** One output argument of a request */ | |
99 | struct fuse_arg { | |
100 | unsigned size; | |
101 | void *value; | |
102 | }; | |
103 | ||
104 | /** The request output */ | |
105 | struct fuse_out { | |
106 | /** Header returned from userspace */ | |
107 | struct fuse_out_header h; | |
108 | ||
095da6cb MS |
109 | /* |
110 | * The following bitfields are not changed during the request | |
111 | * processing | |
112 | */ | |
113 | ||
334f485d MS |
114 | /** Last argument is variable length (can be shorter than |
115 | arg->size) */ | |
116 | unsigned argvar:1; | |
117 | ||
118 | /** Last argument is a list of pages to copy data to */ | |
119 | unsigned argpages:1; | |
120 | ||
121 | /** Zero partially or not copied pages */ | |
122 | unsigned page_zeroing:1; | |
123 | ||
124 | /** Number or arguments */ | |
125 | unsigned numargs; | |
126 | ||
127 | /** Array of arguments */ | |
128 | struct fuse_arg args[3]; | |
129 | }; | |
130 | ||
83cfd493 MS |
131 | /** The request state */ |
132 | enum fuse_req_state { | |
133 | FUSE_REQ_INIT = 0, | |
134 | FUSE_REQ_PENDING, | |
135 | FUSE_REQ_READING, | |
136 | FUSE_REQ_SENT, | |
a4d27e75 | 137 | FUSE_REQ_WRITING, |
83cfd493 MS |
138 | FUSE_REQ_FINISHED |
139 | }; | |
140 | ||
64c6d8ed MS |
141 | struct fuse_conn; |
142 | ||
334f485d MS |
143 | /** |
144 | * A request to the client | |
145 | */ | |
146 | struct fuse_req { | |
ce1d5a49 MS |
147 | /** This can be on either pending processing or io lists in |
148 | fuse_conn */ | |
334f485d MS |
149 | struct list_head list; |
150 | ||
a4d27e75 MS |
151 | /** Entry on the interrupts list */ |
152 | struct list_head intr_entry; | |
153 | ||
334f485d MS |
154 | /** refcount */ |
155 | atomic_t count; | |
156 | ||
a4d27e75 MS |
157 | /** Unique ID for the interrupt request */ |
158 | u64 intr_unique; | |
159 | ||
095da6cb MS |
160 | /* |
161 | * The following bitfields are either set once before the | |
162 | * request is queued or setting/clearing them is protected by | |
d7133114 | 163 | * fuse_conn->lock |
095da6cb MS |
164 | */ |
165 | ||
334f485d MS |
166 | /** True if the request has reply */ |
167 | unsigned isreply:1; | |
168 | ||
51eb01e7 MS |
169 | /** Force sending of the request even if interrupted */ |
170 | unsigned force:1; | |
171 | ||
f9a2842e MS |
172 | /** The request was aborted */ |
173 | unsigned aborted:1; | |
334f485d MS |
174 | |
175 | /** Request is sent in the background */ | |
176 | unsigned background:1; | |
177 | ||
a4d27e75 MS |
178 | /** The request has been interrupted */ |
179 | unsigned interrupted:1; | |
180 | ||
334f485d MS |
181 | /** Data is being copied to/from the request */ |
182 | unsigned locked:1; | |
183 | ||
9bc5ddda MS |
184 | /** Request is counted as "waiting" */ |
185 | unsigned waiting:1; | |
186 | ||
83cfd493 MS |
187 | /** State of the request */ |
188 | enum fuse_req_state state; | |
334f485d MS |
189 | |
190 | /** The request input */ | |
191 | struct fuse_in in; | |
192 | ||
193 | /** The request output */ | |
194 | struct fuse_out out; | |
195 | ||
196 | /** Used to wake up the task waiting for completion of request*/ | |
197 | wait_queue_head_t waitq; | |
198 | ||
199 | /** Data for asynchronous requests */ | |
200 | union { | |
e5e5558e | 201 | struct fuse_forget_in forget_in; |
b6aeaded | 202 | struct fuse_release_in release_in; |
3ec870d5 MS |
203 | struct fuse_init_in init_in; |
204 | struct fuse_init_out init_out; | |
361b1eb5 | 205 | struct fuse_read_in read_in; |
71421259 | 206 | struct fuse_lk_in lk_in; |
334f485d MS |
207 | } misc; |
208 | ||
209 | /** page vector */ | |
210 | struct page *pages[FUSE_MAX_PAGES_PER_REQ]; | |
211 | ||
212 | /** number of pages in vector */ | |
213 | unsigned num_pages; | |
214 | ||
215 | /** offset of data on first page */ | |
216 | unsigned page_offset; | |
217 | ||
334f485d MS |
218 | /** File used in the request (or NULL) */ |
219 | struct file *file; | |
64c6d8ed | 220 | |
51eb01e7 MS |
221 | /** vfsmount used in release */ |
222 | struct vfsmount *vfsmount; | |
223 | ||
224 | /** dentry used in release */ | |
225 | struct dentry *dentry; | |
226 | ||
64c6d8ed MS |
227 | /** Request completion callback */ |
228 | void (*end)(struct fuse_conn *, struct fuse_req *); | |
33649c91 MS |
229 | |
230 | /** Request is stolen from fuse_file->reserved_req */ | |
231 | struct file *stolen_file; | |
334f485d MS |
232 | }; |
233 | ||
d8a5ba45 MS |
234 | /** |
235 | * A Fuse connection. | |
236 | * | |
237 | * This structure is created, when the filesystem is mounted, and is | |
238 | * destroyed, when the client device is closed and the filesystem is | |
239 | * unmounted. | |
240 | */ | |
241 | struct fuse_conn { | |
d7133114 MS |
242 | /** Lock protecting accessess to members of this structure */ |
243 | spinlock_t lock; | |
244 | ||
d2a85164 MS |
245 | /** Mutex protecting against directory alias creation */ |
246 | struct mutex inst_mutex; | |
247 | ||
bafa9654 MS |
248 | /** Refcount */ |
249 | atomic_t count; | |
250 | ||
d8a5ba45 MS |
251 | /** The user id for this mount */ |
252 | uid_t user_id; | |
253 | ||
87729a55 MS |
254 | /** The group id for this mount */ |
255 | gid_t group_id; | |
256 | ||
1e9a4ed9 MS |
257 | /** The fuse mount flags for this mount */ |
258 | unsigned flags; | |
259 | ||
db50b96c MS |
260 | /** Maximum read size */ |
261 | unsigned max_read; | |
262 | ||
413ef8cb MS |
263 | /** Maximum write size */ |
264 | unsigned max_write; | |
265 | ||
334f485d MS |
266 | /** Readers of the connection are waiting on this */ |
267 | wait_queue_head_t waitq; | |
268 | ||
269 | /** The list of pending requests */ | |
270 | struct list_head pending; | |
271 | ||
272 | /** The list of requests being processed */ | |
273 | struct list_head processing; | |
274 | ||
d77a1d5b MS |
275 | /** The list of requests under I/O */ |
276 | struct list_head io; | |
277 | ||
08a53cdc MS |
278 | /** Number of requests currently in the background */ |
279 | unsigned num_background; | |
280 | ||
a4d27e75 MS |
281 | /** Pending interrupts */ |
282 | struct list_head interrupts; | |
283 | ||
08a53cdc MS |
284 | /** Flag indicating if connection is blocked. This will be |
285 | the case before the INIT reply is received, and if there | |
286 | are too many outstading backgrounds requests */ | |
287 | int blocked; | |
288 | ||
289 | /** waitq for blocked connection */ | |
290 | wait_queue_head_t blocked_waitq; | |
291 | ||
334f485d MS |
292 | /** The next unique request id */ |
293 | u64 reqctr; | |
294 | ||
69a53bf2 MS |
295 | /** Connection established, cleared on umount, connection |
296 | abort and device release */ | |
095da6cb | 297 | unsigned connected; |
1e9a4ed9 | 298 | |
095da6cb MS |
299 | /** Connection failed (version mismatch). Cannot race with |
300 | setting other bitfields since it is only set once in INIT | |
301 | reply, before any other request, and never cleared */ | |
334f485d MS |
302 | unsigned conn_error : 1; |
303 | ||
0ec7ca41 MS |
304 | /** Connection successful. Only set in INIT */ |
305 | unsigned conn_init : 1; | |
306 | ||
9cd68455 MS |
307 | /** Do readpages asynchronously? Only set in INIT */ |
308 | unsigned async_read : 1; | |
309 | ||
095da6cb MS |
310 | /* |
311 | * The following bitfields are only for optimization purposes | |
312 | * and hence races in setting them will not cause malfunction | |
313 | */ | |
314 | ||
b6aeaded MS |
315 | /** Is fsync not implemented by fs? */ |
316 | unsigned no_fsync : 1; | |
317 | ||
82547981 MS |
318 | /** Is fsyncdir not implemented by fs? */ |
319 | unsigned no_fsyncdir : 1; | |
320 | ||
b6aeaded MS |
321 | /** Is flush not implemented by fs? */ |
322 | unsigned no_flush : 1; | |
323 | ||
92a8780e MS |
324 | /** Is setxattr not implemented by fs? */ |
325 | unsigned no_setxattr : 1; | |
326 | ||
327 | /** Is getxattr not implemented by fs? */ | |
328 | unsigned no_getxattr : 1; | |
329 | ||
330 | /** Is listxattr not implemented by fs? */ | |
331 | unsigned no_listxattr : 1; | |
332 | ||
333 | /** Is removexattr not implemented by fs? */ | |
334 | unsigned no_removexattr : 1; | |
335 | ||
71421259 MS |
336 | /** Are file locking primitives not implemented by fs? */ |
337 | unsigned no_lock : 1; | |
338 | ||
31d40d74 MS |
339 | /** Is access not implemented by fs? */ |
340 | unsigned no_access : 1; | |
341 | ||
fd72faac MS |
342 | /** Is create not implemented by fs? */ |
343 | unsigned no_create : 1; | |
344 | ||
a4d27e75 MS |
345 | /** Is interrupt not implemented by fs? */ |
346 | unsigned no_interrupt : 1; | |
347 | ||
b2d2272f MS |
348 | /** Is bmap not implemented by fs? */ |
349 | unsigned no_bmap : 1; | |
350 | ||
0cd5b885 MS |
351 | /** The number of requests waiting for completion */ |
352 | atomic_t num_waiting; | |
353 | ||
45714d65 MS |
354 | /** Negotiated minor version */ |
355 | unsigned minor; | |
356 | ||
d8a5ba45 MS |
357 | /** Backing dev info */ |
358 | struct backing_dev_info bdi; | |
f543f253 | 359 | |
bafa9654 MS |
360 | /** Entry on the fuse_conn_list */ |
361 | struct list_head entry; | |
362 | ||
363 | /** Unique ID */ | |
364 | u64 id; | |
365 | ||
366 | /** Dentries in the control filesystem */ | |
367 | struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES]; | |
368 | ||
369 | /** number of dentries used in the above array */ | |
370 | int ctl_ndents; | |
385a17bf JD |
371 | |
372 | /** O_ASYNC requests */ | |
373 | struct fasync_struct *fasync; | |
9c8ef561 MS |
374 | |
375 | /** Key for lock owner ID scrambling */ | |
376 | u32 scramble_key[4]; | |
0ec7ca41 MS |
377 | |
378 | /** Reserved request for the DESTROY message */ | |
379 | struct fuse_req *destroy_req; | |
d8a5ba45 MS |
380 | }; |
381 | ||
d8a5ba45 MS |
382 | static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb) |
383 | { | |
6383bdaa | 384 | return sb->s_fs_info; |
d8a5ba45 MS |
385 | } |
386 | ||
387 | static inline struct fuse_conn *get_fuse_conn(struct inode *inode) | |
388 | { | |
389 | return get_fuse_conn_super(inode->i_sb); | |
390 | } | |
391 | ||
392 | static inline struct fuse_inode *get_fuse_inode(struct inode *inode) | |
393 | { | |
394 | return container_of(inode, struct fuse_inode, inode); | |
395 | } | |
396 | ||
397 | static inline u64 get_node_id(struct inode *inode) | |
398 | { | |
399 | return get_fuse_inode(inode)->nodeid; | |
400 | } | |
401 | ||
334f485d | 402 | /** Device operations */ |
4b6f5d20 | 403 | extern const struct file_operations fuse_dev_operations; |
334f485d | 404 | |
e5e5558e MS |
405 | /** |
406 | * Get a filled in inode | |
407 | */ | |
408 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, | |
9e6268db | 409 | int generation, struct fuse_attr *attr); |
e5e5558e MS |
410 | |
411 | /** | |
412 | * Send FORGET command | |
413 | */ | |
414 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, | |
9e6268db | 415 | unsigned long nodeid, u64 nlookup); |
e5e5558e | 416 | |
04730fef | 417 | /** |
361b1eb5 | 418 | * Initialize READ or READDIR request |
04730fef | 419 | */ |
361b1eb5 MS |
420 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
421 | struct inode *inode, loff_t pos, size_t count, int opcode); | |
04730fef MS |
422 | |
423 | /** | |
424 | * Send OPEN or OPENDIR request | |
425 | */ | |
426 | int fuse_open_common(struct inode *inode, struct file *file, int isdir); | |
427 | ||
fd72faac MS |
428 | struct fuse_file *fuse_file_alloc(void); |
429 | void fuse_file_free(struct fuse_file *ff); | |
430 | void fuse_finish_open(struct inode *inode, struct file *file, | |
431 | struct fuse_file *ff, struct fuse_open_out *outarg); | |
432 | ||
51eb01e7 MS |
433 | /** */ |
434 | struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, | |
435 | int opcode); | |
04730fef MS |
436 | /** |
437 | * Send RELEASE or RELEASEDIR request | |
438 | */ | |
439 | int fuse_release_common(struct inode *inode, struct file *file, int isdir); | |
440 | ||
82547981 MS |
441 | /** |
442 | * Send FSYNC or FSYNCDIR request | |
443 | */ | |
444 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, | |
445 | int isdir); | |
446 | ||
b6aeaded | 447 | /** |
1779381d | 448 | * Initialize file operations on a regular file |
b6aeaded MS |
449 | */ |
450 | void fuse_init_file_inode(struct inode *inode); | |
451 | ||
e5e5558e | 452 | /** |
1779381d | 453 | * Initialize inode operations on regular files and special files |
e5e5558e MS |
454 | */ |
455 | void fuse_init_common(struct inode *inode); | |
456 | ||
457 | /** | |
1779381d | 458 | * Initialize inode and file operations on a directory |
e5e5558e MS |
459 | */ |
460 | void fuse_init_dir(struct inode *inode); | |
461 | ||
462 | /** | |
1779381d | 463 | * Initialize inode operations on a symlink |
e5e5558e MS |
464 | */ |
465 | void fuse_init_symlink(struct inode *inode); | |
466 | ||
467 | /** | |
468 | * Change attributes of an inode | |
469 | */ | |
470 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr); | |
471 | ||
334f485d MS |
472 | /** |
473 | * Initialize the client device | |
474 | */ | |
475 | int fuse_dev_init(void); | |
476 | ||
477 | /** | |
478 | * Cleanup the client device | |
479 | */ | |
480 | void fuse_dev_cleanup(void); | |
481 | ||
bafa9654 MS |
482 | int fuse_ctl_init(void); |
483 | void fuse_ctl_cleanup(void); | |
484 | ||
334f485d MS |
485 | /** |
486 | * Allocate a request | |
487 | */ | |
488 | struct fuse_req *fuse_request_alloc(void); | |
489 | ||
490 | /** | |
491 | * Free a request | |
492 | */ | |
493 | void fuse_request_free(struct fuse_req *req); | |
494 | ||
334f485d | 495 | /** |
33649c91 | 496 | * Get a request, may fail with -ENOMEM |
334f485d | 497 | */ |
ce1d5a49 | 498 | struct fuse_req *fuse_get_req(struct fuse_conn *fc); |
334f485d | 499 | |
33649c91 MS |
500 | /** |
501 | * Gets a requests for a file operation, always succeeds | |
502 | */ | |
503 | struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file); | |
504 | ||
334f485d | 505 | /** |
ce1d5a49 MS |
506 | * Decrement reference count of a request. If count goes to zero free |
507 | * the request. | |
334f485d MS |
508 | */ |
509 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); | |
510 | ||
511 | /** | |
7c352bdf | 512 | * Send a request (synchronous) |
334f485d MS |
513 | */ |
514 | void request_send(struct fuse_conn *fc, struct fuse_req *req); | |
515 | ||
334f485d MS |
516 | /** |
517 | * Send a request with no reply | |
518 | */ | |
519 | void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req); | |
520 | ||
521 | /** | |
522 | * Send a request in the background | |
523 | */ | |
524 | void request_send_background(struct fuse_conn *fc, struct fuse_req *req); | |
525 | ||
5a5fb1ea | 526 | /* Abort all requests */ |
69a53bf2 MS |
527 | void fuse_abort_conn(struct fuse_conn *fc); |
528 | ||
e5e5558e MS |
529 | /** |
530 | * Get the attributes of a file | |
531 | */ | |
532 | int fuse_do_getattr(struct inode *inode); | |
533 | ||
534 | /** | |
535 | * Invalidate inode attributes | |
536 | */ | |
537 | void fuse_invalidate_attr(struct inode *inode); | |
bafa9654 MS |
538 | |
539 | /** | |
540 | * Acquire reference to fuse_conn | |
541 | */ | |
542 | struct fuse_conn *fuse_conn_get(struct fuse_conn *fc); | |
543 | ||
544 | /** | |
545 | * Release reference to fuse_conn | |
546 | */ | |
547 | void fuse_conn_put(struct fuse_conn *fc); | |
548 | ||
549 | /** | |
550 | * Add connection to control filesystem | |
551 | */ | |
552 | int fuse_ctl_add_conn(struct fuse_conn *fc); | |
553 | ||
554 | /** | |
555 | * Remove connection from control filesystem | |
556 | */ | |
557 | void fuse_ctl_remove_conn(struct fuse_conn *fc); | |
a5bfffac TS |
558 | |
559 | /** | |
560 | * Is file type valid? | |
561 | */ | |
562 | int fuse_valid_type(int m); |