]>
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 MS |
61 | /** Time in jiffies until the file attributes are valid */ |
62 | unsigned long i_time; | |
63 | }; | |
64 | ||
b6aeaded MS |
65 | /** FUSE specific file data */ |
66 | struct fuse_file { | |
67 | /** Request reserved for flush and release */ | |
68 | struct fuse_req *release_req; | |
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, | |
134 | FUSE_REQ_FINISHED | |
135 | }; | |
136 | ||
64c6d8ed MS |
137 | struct fuse_conn; |
138 | ||
334f485d MS |
139 | /** |
140 | * A request to the client | |
141 | */ | |
142 | struct fuse_req { | |
ce1d5a49 MS |
143 | /** This can be on either pending processing or io lists in |
144 | fuse_conn */ | |
334f485d MS |
145 | struct list_head list; |
146 | ||
147 | /** refcount */ | |
148 | atomic_t count; | |
149 | ||
095da6cb MS |
150 | /* |
151 | * The following bitfields are either set once before the | |
152 | * request is queued or setting/clearing them is protected by | |
d7133114 | 153 | * fuse_conn->lock |
095da6cb MS |
154 | */ |
155 | ||
334f485d MS |
156 | /** True if the request has reply */ |
157 | unsigned isreply:1; | |
158 | ||
51eb01e7 MS |
159 | /** Force sending of the request even if interrupted */ |
160 | unsigned force:1; | |
161 | ||
334f485d MS |
162 | /** The request was interrupted */ |
163 | unsigned interrupted:1; | |
164 | ||
165 | /** Request is sent in the background */ | |
166 | unsigned background:1; | |
167 | ||
168 | /** Data is being copied to/from the request */ | |
169 | unsigned locked:1; | |
170 | ||
9bc5ddda MS |
171 | /** Request is counted as "waiting" */ |
172 | unsigned waiting:1; | |
173 | ||
83cfd493 MS |
174 | /** State of the request */ |
175 | enum fuse_req_state state; | |
334f485d MS |
176 | |
177 | /** The request input */ | |
178 | struct fuse_in in; | |
179 | ||
180 | /** The request output */ | |
181 | struct fuse_out out; | |
182 | ||
183 | /** Used to wake up the task waiting for completion of request*/ | |
184 | wait_queue_head_t waitq; | |
185 | ||
186 | /** Data for asynchronous requests */ | |
187 | union { | |
e5e5558e | 188 | struct fuse_forget_in forget_in; |
b6aeaded | 189 | struct fuse_release_in release_in; |
3ec870d5 MS |
190 | struct fuse_init_in init_in; |
191 | struct fuse_init_out init_out; | |
361b1eb5 | 192 | struct fuse_read_in read_in; |
334f485d MS |
193 | } misc; |
194 | ||
195 | /** page vector */ | |
196 | struct page *pages[FUSE_MAX_PAGES_PER_REQ]; | |
197 | ||
198 | /** number of pages in vector */ | |
199 | unsigned num_pages; | |
200 | ||
201 | /** offset of data on first page */ | |
202 | unsigned page_offset; | |
203 | ||
334f485d MS |
204 | /** File used in the request (or NULL) */ |
205 | struct file *file; | |
64c6d8ed | 206 | |
51eb01e7 MS |
207 | /** vfsmount used in release */ |
208 | struct vfsmount *vfsmount; | |
209 | ||
210 | /** dentry used in release */ | |
211 | struct dentry *dentry; | |
212 | ||
64c6d8ed MS |
213 | /** Request completion callback */ |
214 | void (*end)(struct fuse_conn *, struct fuse_req *); | |
334f485d MS |
215 | }; |
216 | ||
d8a5ba45 MS |
217 | /** |
218 | * A Fuse connection. | |
219 | * | |
220 | * This structure is created, when the filesystem is mounted, and is | |
221 | * destroyed, when the client device is closed and the filesystem is | |
222 | * unmounted. | |
223 | */ | |
224 | struct fuse_conn { | |
d7133114 MS |
225 | /** Lock protecting accessess to members of this structure */ |
226 | spinlock_t lock; | |
227 | ||
bafa9654 MS |
228 | /** Refcount */ |
229 | atomic_t count; | |
230 | ||
d8a5ba45 MS |
231 | /** The user id for this mount */ |
232 | uid_t user_id; | |
233 | ||
87729a55 MS |
234 | /** The group id for this mount */ |
235 | gid_t group_id; | |
236 | ||
1e9a4ed9 MS |
237 | /** The fuse mount flags for this mount */ |
238 | unsigned flags; | |
239 | ||
db50b96c MS |
240 | /** Maximum read size */ |
241 | unsigned max_read; | |
242 | ||
413ef8cb MS |
243 | /** Maximum write size */ |
244 | unsigned max_write; | |
245 | ||
334f485d MS |
246 | /** Readers of the connection are waiting on this */ |
247 | wait_queue_head_t waitq; | |
248 | ||
249 | /** The list of pending requests */ | |
250 | struct list_head pending; | |
251 | ||
252 | /** The list of requests being processed */ | |
253 | struct list_head processing; | |
254 | ||
d77a1d5b MS |
255 | /** The list of requests under I/O */ |
256 | struct list_head io; | |
257 | ||
08a53cdc MS |
258 | /** Number of requests currently in the background */ |
259 | unsigned num_background; | |
260 | ||
261 | /** Flag indicating if connection is blocked. This will be | |
262 | the case before the INIT reply is received, and if there | |
263 | are too many outstading backgrounds requests */ | |
264 | int blocked; | |
265 | ||
266 | /** waitq for blocked connection */ | |
267 | wait_queue_head_t blocked_waitq; | |
268 | ||
334f485d MS |
269 | /** The next unique request id */ |
270 | u64 reqctr; | |
271 | ||
69a53bf2 MS |
272 | /** Connection established, cleared on umount, connection |
273 | abort and device release */ | |
095da6cb | 274 | unsigned connected; |
1e9a4ed9 | 275 | |
095da6cb MS |
276 | /** Connection failed (version mismatch). Cannot race with |
277 | setting other bitfields since it is only set once in INIT | |
278 | reply, before any other request, and never cleared */ | |
334f485d MS |
279 | unsigned conn_error : 1; |
280 | ||
9cd68455 MS |
281 | /** Do readpages asynchronously? Only set in INIT */ |
282 | unsigned async_read : 1; | |
283 | ||
095da6cb MS |
284 | /* |
285 | * The following bitfields are only for optimization purposes | |
286 | * and hence races in setting them will not cause malfunction | |
287 | */ | |
288 | ||
b6aeaded MS |
289 | /** Is fsync not implemented by fs? */ |
290 | unsigned no_fsync : 1; | |
291 | ||
82547981 MS |
292 | /** Is fsyncdir not implemented by fs? */ |
293 | unsigned no_fsyncdir : 1; | |
294 | ||
b6aeaded MS |
295 | /** Is flush not implemented by fs? */ |
296 | unsigned no_flush : 1; | |
297 | ||
92a8780e MS |
298 | /** Is setxattr not implemented by fs? */ |
299 | unsigned no_setxattr : 1; | |
300 | ||
301 | /** Is getxattr not implemented by fs? */ | |
302 | unsigned no_getxattr : 1; | |
303 | ||
304 | /** Is listxattr not implemented by fs? */ | |
305 | unsigned no_listxattr : 1; | |
306 | ||
307 | /** Is removexattr not implemented by fs? */ | |
308 | unsigned no_removexattr : 1; | |
309 | ||
31d40d74 MS |
310 | /** Is access not implemented by fs? */ |
311 | unsigned no_access : 1; | |
312 | ||
fd72faac MS |
313 | /** Is create not implemented by fs? */ |
314 | unsigned no_create : 1; | |
315 | ||
0cd5b885 MS |
316 | /** The number of requests waiting for completion */ |
317 | atomic_t num_waiting; | |
318 | ||
45714d65 MS |
319 | /** Negotiated minor version */ |
320 | unsigned minor; | |
321 | ||
d8a5ba45 MS |
322 | /** Backing dev info */ |
323 | struct backing_dev_info bdi; | |
f543f253 | 324 | |
bafa9654 MS |
325 | /** Entry on the fuse_conn_list */ |
326 | struct list_head entry; | |
327 | ||
328 | /** Unique ID */ | |
329 | u64 id; | |
330 | ||
331 | /** Dentries in the control filesystem */ | |
332 | struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES]; | |
333 | ||
334 | /** number of dentries used in the above array */ | |
335 | int ctl_ndents; | |
385a17bf JD |
336 | |
337 | /** O_ASYNC requests */ | |
338 | struct fasync_struct *fasync; | |
d8a5ba45 MS |
339 | }; |
340 | ||
d8a5ba45 MS |
341 | static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb) |
342 | { | |
6383bdaa | 343 | return sb->s_fs_info; |
d8a5ba45 MS |
344 | } |
345 | ||
346 | static inline struct fuse_conn *get_fuse_conn(struct inode *inode) | |
347 | { | |
348 | return get_fuse_conn_super(inode->i_sb); | |
349 | } | |
350 | ||
351 | static inline struct fuse_inode *get_fuse_inode(struct inode *inode) | |
352 | { | |
353 | return container_of(inode, struct fuse_inode, inode); | |
354 | } | |
355 | ||
356 | static inline u64 get_node_id(struct inode *inode) | |
357 | { | |
358 | return get_fuse_inode(inode)->nodeid; | |
359 | } | |
360 | ||
334f485d | 361 | /** Device operations */ |
4b6f5d20 | 362 | extern const struct file_operations fuse_dev_operations; |
334f485d | 363 | |
e5e5558e MS |
364 | /** |
365 | * Get a filled in inode | |
366 | */ | |
367 | struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid, | |
9e6268db | 368 | int generation, struct fuse_attr *attr); |
e5e5558e MS |
369 | |
370 | /** | |
371 | * Send FORGET command | |
372 | */ | |
373 | void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req, | |
9e6268db | 374 | unsigned long nodeid, u64 nlookup); |
e5e5558e | 375 | |
04730fef | 376 | /** |
361b1eb5 | 377 | * Initialize READ or READDIR request |
04730fef | 378 | */ |
361b1eb5 MS |
379 | void fuse_read_fill(struct fuse_req *req, struct file *file, |
380 | struct inode *inode, loff_t pos, size_t count, int opcode); | |
04730fef MS |
381 | |
382 | /** | |
383 | * Send OPEN or OPENDIR request | |
384 | */ | |
385 | int fuse_open_common(struct inode *inode, struct file *file, int isdir); | |
386 | ||
fd72faac MS |
387 | struct fuse_file *fuse_file_alloc(void); |
388 | void fuse_file_free(struct fuse_file *ff); | |
389 | void fuse_finish_open(struct inode *inode, struct file *file, | |
390 | struct fuse_file *ff, struct fuse_open_out *outarg); | |
391 | ||
51eb01e7 MS |
392 | /** */ |
393 | struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, | |
394 | int opcode); | |
04730fef MS |
395 | /** |
396 | * Send RELEASE or RELEASEDIR request | |
397 | */ | |
398 | int fuse_release_common(struct inode *inode, struct file *file, int isdir); | |
399 | ||
82547981 MS |
400 | /** |
401 | * Send FSYNC or FSYNCDIR request | |
402 | */ | |
403 | int fuse_fsync_common(struct file *file, struct dentry *de, int datasync, | |
404 | int isdir); | |
405 | ||
b6aeaded | 406 | /** |
1779381d | 407 | * Initialize file operations on a regular file |
b6aeaded MS |
408 | */ |
409 | void fuse_init_file_inode(struct inode *inode); | |
410 | ||
e5e5558e | 411 | /** |
1779381d | 412 | * Initialize inode operations on regular files and special files |
e5e5558e MS |
413 | */ |
414 | void fuse_init_common(struct inode *inode); | |
415 | ||
416 | /** | |
1779381d | 417 | * Initialize inode and file operations on a directory |
e5e5558e MS |
418 | */ |
419 | void fuse_init_dir(struct inode *inode); | |
420 | ||
421 | /** | |
1779381d | 422 | * Initialize inode operations on a symlink |
e5e5558e MS |
423 | */ |
424 | void fuse_init_symlink(struct inode *inode); | |
425 | ||
426 | /** | |
427 | * Change attributes of an inode | |
428 | */ | |
429 | void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr); | |
430 | ||
334f485d MS |
431 | /** |
432 | * Initialize the client device | |
433 | */ | |
434 | int fuse_dev_init(void); | |
435 | ||
436 | /** | |
437 | * Cleanup the client device | |
438 | */ | |
439 | void fuse_dev_cleanup(void); | |
440 | ||
bafa9654 MS |
441 | int fuse_ctl_init(void); |
442 | void fuse_ctl_cleanup(void); | |
443 | ||
334f485d MS |
444 | /** |
445 | * Allocate a request | |
446 | */ | |
447 | struct fuse_req *fuse_request_alloc(void); | |
448 | ||
449 | /** | |
450 | * Free a request | |
451 | */ | |
452 | void fuse_request_free(struct fuse_req *req); | |
453 | ||
334f485d MS |
454 | /** |
455 | * Reserve a preallocated request | |
456 | */ | |
ce1d5a49 | 457 | struct fuse_req *fuse_get_req(struct fuse_conn *fc); |
334f485d | 458 | |
334f485d | 459 | /** |
ce1d5a49 MS |
460 | * Decrement reference count of a request. If count goes to zero free |
461 | * the request. | |
334f485d MS |
462 | */ |
463 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req); | |
464 | ||
465 | /** | |
7c352bdf | 466 | * Send a request (synchronous) |
334f485d MS |
467 | */ |
468 | void request_send(struct fuse_conn *fc, struct fuse_req *req); | |
469 | ||
334f485d MS |
470 | /** |
471 | * Send a request with no reply | |
472 | */ | |
473 | void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req); | |
474 | ||
475 | /** | |
476 | * Send a request in the background | |
477 | */ | |
478 | void request_send_background(struct fuse_conn *fc, struct fuse_req *req); | |
479 | ||
5a5fb1ea | 480 | /* Abort all requests */ |
69a53bf2 MS |
481 | void fuse_abort_conn(struct fuse_conn *fc); |
482 | ||
e5e5558e MS |
483 | /** |
484 | * Get the attributes of a file | |
485 | */ | |
486 | int fuse_do_getattr(struct inode *inode); | |
487 | ||
488 | /** | |
489 | * Invalidate inode attributes | |
490 | */ | |
491 | void fuse_invalidate_attr(struct inode *inode); | |
bafa9654 MS |
492 | |
493 | /** | |
494 | * Acquire reference to fuse_conn | |
495 | */ | |
496 | struct fuse_conn *fuse_conn_get(struct fuse_conn *fc); | |
497 | ||
498 | /** | |
499 | * Release reference to fuse_conn | |
500 | */ | |
501 | void fuse_conn_put(struct fuse_conn *fc); | |
502 | ||
503 | /** | |
504 | * Add connection to control filesystem | |
505 | */ | |
506 | int fuse_ctl_add_conn(struct fuse_conn *fc); | |
507 | ||
508 | /** | |
509 | * Remove connection from control filesystem | |
510 | */ | |
511 | void fuse_ctl_remove_conn(struct fuse_conn *fc); |