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