2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
9 #include <linux/fuse.h>
11 #include <linux/mount.h>
12 #include <linux/wait.h>
13 #include <linux/list.h>
14 #include <linux/spinlock.h>
16 #include <linux/backing-dev.h>
17 #include <linux/mutex.h>
19 /** Max number of pages that can be used in a single read request */
20 #define FUSE_MAX_PAGES_PER_REQ 32
22 /** Maximum number of outstanding background requests */
23 #define FUSE_MAX_BACKGROUND 12
25 /** Congestion starts at 75% of maximum */
26 #define FUSE_CONGESTION_THRESHOLD (FUSE_MAX_BACKGROUND * 75 / 100)
28 /** It could be as large as PATH_MAX, but would that have any uses? */
29 #define FUSE_NAME_MAX 1024
31 /** Number of dentries for each connection in the control filesystem */
32 #define FUSE_CTL_NUM_DENTRIES 3
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)
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)
43 /** List of active connections */
44 extern struct list_head fuse_conn_list
;
46 /** Global mutex protecting fuse_conn_list and the control filesystem */
47 extern struct mutex fuse_mutex
;
54 /** Unique ID, which identifies the inode between userspace
58 /** Number of lookups on this inode */
61 /** The request used for sending the FORGET message */
62 struct fuse_req
*forget_req
;
64 /** Time in jiffies until the file attributes are valid */
68 /** FUSE specific file data */
70 /** Request reserved for flush and release */
71 struct fuse_req
*reserved_req
;
73 /** File handle used by userspace */
77 /** One input argument of a request */
83 /** The request input */
85 /** The request header */
86 struct fuse_in_header h
;
88 /** True if the data for the last argument is in req->pages */
91 /** Number of arguments */
94 /** Array of arguments */
95 struct fuse_in_arg args
[3];
98 /** One output argument of a request */
104 /** The request output */
106 /** Header returned from userspace */
107 struct fuse_out_header h
;
110 * The following bitfields are not changed during the request
114 /** Last argument is variable length (can be shorter than
118 /** Last argument is a list of pages to copy data to */
121 /** Zero partially or not copied pages */
122 unsigned page_zeroing
:1;
124 /** Number or arguments */
127 /** Array of arguments */
128 struct fuse_arg args
[3];
131 /** The request state */
132 enum fuse_req_state
{
144 * A request to the client
147 /** This can be on either pending processing or io lists in
149 struct list_head list
;
151 /** Entry on the interrupts list */
152 struct list_head intr_entry
;
157 /** Unique ID for the interrupt request */
161 * The following bitfields are either set once before the
162 * request is queued or setting/clearing them is protected by
166 /** True if the request has reply */
169 /** Force sending of the request even if interrupted */
172 /** The request was aborted */
175 /** Request is sent in the background */
176 unsigned background
:1;
178 /** The request has been interrupted */
179 unsigned interrupted
:1;
181 /** Data is being copied to/from the request */
184 /** Request is counted as "waiting" */
187 /** State of the request */
188 enum fuse_req_state state
;
190 /** The request input */
193 /** The request output */
196 /** Used to wake up the task waiting for completion of request*/
197 wait_queue_head_t waitq
;
199 /** Data for asynchronous requests */
201 struct fuse_forget_in forget_in
;
202 struct fuse_release_in release_in
;
203 struct fuse_init_in init_in
;
204 struct fuse_init_out init_out
;
205 struct fuse_read_in read_in
;
206 struct fuse_lk_in lk_in
;
210 struct page
*pages
[FUSE_MAX_PAGES_PER_REQ
];
212 /** number of pages in vector */
215 /** offset of data on first page */
216 unsigned page_offset
;
218 /** File used in the request (or NULL) */
221 /** vfsmount used in release */
222 struct vfsmount
*vfsmount
;
224 /** dentry used in release */
225 struct dentry
*dentry
;
227 /** Request completion callback */
228 void (*end
)(struct fuse_conn
*, struct fuse_req
*);
230 /** Request is stolen from fuse_file->reserved_req */
231 struct file
*stolen_file
;
237 * This structure is created, when the filesystem is mounted, and is
238 * destroyed, when the client device is closed and the filesystem is
242 /** Lock protecting accessess to members of this structure */
245 /** Mutex protecting against directory alias creation */
246 struct mutex inst_mutex
;
251 /** The user id for this mount */
254 /** The group id for this mount */
257 /** The fuse mount flags for this mount */
260 /** Maximum read size */
263 /** Maximum write size */
266 /** Readers of the connection are waiting on this */
267 wait_queue_head_t waitq
;
269 /** The list of pending requests */
270 struct list_head pending
;
272 /** The list of requests being processed */
273 struct list_head processing
;
275 /** The list of requests under I/O */
278 /** Number of requests currently in the background */
279 unsigned num_background
;
281 /** Pending interrupts */
282 struct list_head interrupts
;
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 */
289 /** waitq for blocked connection */
290 wait_queue_head_t blocked_waitq
;
292 /** The next unique request id */
295 /** Connection established, cleared on umount, connection
296 abort and device release */
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 */
302 unsigned conn_error
: 1;
304 /** Connection successful. Only set in INIT */
305 unsigned conn_init
: 1;
307 /** Do readpages asynchronously? Only set in INIT */
308 unsigned async_read
: 1;
311 * The following bitfields are only for optimization purposes
312 * and hence races in setting them will not cause malfunction
315 /** Is fsync not implemented by fs? */
316 unsigned no_fsync
: 1;
318 /** Is fsyncdir not implemented by fs? */
319 unsigned no_fsyncdir
: 1;
321 /** Is flush not implemented by fs? */
322 unsigned no_flush
: 1;
324 /** Is setxattr not implemented by fs? */
325 unsigned no_setxattr
: 1;
327 /** Is getxattr not implemented by fs? */
328 unsigned no_getxattr
: 1;
330 /** Is listxattr not implemented by fs? */
331 unsigned no_listxattr
: 1;
333 /** Is removexattr not implemented by fs? */
334 unsigned no_removexattr
: 1;
336 /** Are file locking primitives not implemented by fs? */
337 unsigned no_lock
: 1;
339 /** Is access not implemented by fs? */
340 unsigned no_access
: 1;
342 /** Is create not implemented by fs? */
343 unsigned no_create
: 1;
345 /** Is interrupt not implemented by fs? */
346 unsigned no_interrupt
: 1;
348 /** Is bmap not implemented by fs? */
349 unsigned no_bmap
: 1;
351 /** The number of requests waiting for completion */
352 atomic_t num_waiting
;
354 /** Negotiated minor version */
357 /** Backing dev info */
358 struct backing_dev_info bdi
;
360 /** Entry on the fuse_conn_list */
361 struct list_head entry
;
366 /** Dentries in the control filesystem */
367 struct dentry
*ctl_dentry
[FUSE_CTL_NUM_DENTRIES
];
369 /** number of dentries used in the above array */
372 /** O_ASYNC requests */
373 struct fasync_struct
*fasync
;
375 /** Key for lock owner ID scrambling */
378 /** Reserved request for the DESTROY message */
379 struct fuse_req
*destroy_req
;
382 static inline struct fuse_conn
*get_fuse_conn_super(struct super_block
*sb
)
384 return sb
->s_fs_info
;
387 static inline struct fuse_conn
*get_fuse_conn(struct inode
*inode
)
389 return get_fuse_conn_super(inode
->i_sb
);
392 static inline struct fuse_inode
*get_fuse_inode(struct inode
*inode
)
394 return container_of(inode
, struct fuse_inode
, inode
);
397 static inline u64
get_node_id(struct inode
*inode
)
399 return get_fuse_inode(inode
)->nodeid
;
402 /** Device operations */
403 extern const struct file_operations fuse_dev_operations
;
406 * Get a filled in inode
408 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
409 int generation
, struct fuse_attr
*attr
);
412 * Send FORGET command
414 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
415 unsigned long nodeid
, u64 nlookup
);
418 * Initialize READ or READDIR request
420 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
,
421 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
);
424 * Send OPEN or OPENDIR request
426 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
);
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
);
434 struct fuse_req
*fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
,
437 * Send RELEASE or RELEASEDIR request
439 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
);
442 * Send FSYNC or FSYNCDIR request
444 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
448 * Initialize file operations on a regular file
450 void fuse_init_file_inode(struct inode
*inode
);
453 * Initialize inode operations on regular files and special files
455 void fuse_init_common(struct inode
*inode
);
458 * Initialize inode and file operations on a directory
460 void fuse_init_dir(struct inode
*inode
);
463 * Initialize inode operations on a symlink
465 void fuse_init_symlink(struct inode
*inode
);
468 * Change attributes of an inode
470 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
);
473 * Initialize the client device
475 int fuse_dev_init(void);
478 * Cleanup the client device
480 void fuse_dev_cleanup(void);
482 int fuse_ctl_init(void);
483 void fuse_ctl_cleanup(void);
488 struct fuse_req
*fuse_request_alloc(void);
493 void fuse_request_free(struct fuse_req
*req
);
496 * Get a request, may fail with -ENOMEM
498 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
);
501 * Gets a requests for a file operation, always succeeds
503 struct fuse_req
*fuse_get_req_nofail(struct fuse_conn
*fc
, struct file
*file
);
506 * Decrement reference count of a request. If count goes to zero free
509 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
);
512 * Send a request (synchronous)
514 void request_send(struct fuse_conn
*fc
, struct fuse_req
*req
);
517 * Send a request with no reply
519 void request_send_noreply(struct fuse_conn
*fc
, struct fuse_req
*req
);
522 * Send a request in the background
524 void request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
);
526 /* Abort all requests */
527 void fuse_abort_conn(struct fuse_conn
*fc
);
530 * Get the attributes of a file
532 int fuse_do_getattr(struct inode
*inode
);
535 * Invalidate inode attributes
537 void fuse_invalidate_attr(struct inode
*inode
);
540 * Acquire reference to fuse_conn
542 struct fuse_conn
*fuse_conn_get(struct fuse_conn
*fc
);
545 * Release reference to fuse_conn
547 void fuse_conn_put(struct fuse_conn
*fc
);
550 * Add connection to control filesystem
552 int fuse_ctl_add_conn(struct fuse_conn
*fc
);
555 * Remove connection from control filesystem
557 void fuse_ctl_remove_conn(struct fuse_conn
*fc
);
560 * Is file type valid?
562 int fuse_valid_type(int m
);