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 */
80 /** One input argument of a request */
86 /** The request input */
88 /** The request header */
89 struct fuse_in_header h
;
91 /** True if the data for the last argument is in req->pages */
94 /** Number of arguments */
97 /** Array of arguments */
98 struct fuse_in_arg args
[3];
101 /** One output argument of a request */
107 /** The request output */
109 /** Header returned from userspace */
110 struct fuse_out_header h
;
113 * The following bitfields are not changed during the request
117 /** Last argument is variable length (can be shorter than
121 /** Last argument is a list of pages to copy data to */
124 /** Zero partially or not copied pages */
125 unsigned page_zeroing
:1;
127 /** Number or arguments */
130 /** Array of arguments */
131 struct fuse_arg args
[3];
134 /** The request state */
135 enum fuse_req_state
{
147 * A request to the client
150 /** This can be on either pending processing or io lists in
152 struct list_head list
;
154 /** Entry on the interrupts list */
155 struct list_head intr_entry
;
160 /** Unique ID for the interrupt request */
164 * The following bitfields are either set once before the
165 * request is queued or setting/clearing them is protected by
169 /** True if the request has reply */
172 /** Force sending of the request even if interrupted */
175 /** The request was aborted */
178 /** Request is sent in the background */
179 unsigned background
:1;
181 /** The request has been interrupted */
182 unsigned interrupted
:1;
184 /** Data is being copied to/from the request */
187 /** Request is counted as "waiting" */
190 /** State of the request */
191 enum fuse_req_state state
;
193 /** The request input */
196 /** The request output */
199 /** Used to wake up the task waiting for completion of request*/
200 wait_queue_head_t waitq
;
202 /** Data for asynchronous requests */
204 struct fuse_forget_in forget_in
;
205 struct fuse_release_in release_in
;
206 struct fuse_init_in init_in
;
207 struct fuse_init_out init_out
;
208 struct fuse_read_in read_in
;
209 struct fuse_lk_in lk_in
;
213 struct page
*pages
[FUSE_MAX_PAGES_PER_REQ
];
215 /** number of pages in vector */
218 /** offset of data on first page */
219 unsigned page_offset
;
221 /** File used in the request (or NULL) */
222 struct fuse_file
*ff
;
224 /** vfsmount used in release */
225 struct vfsmount
*vfsmount
;
227 /** dentry used in release */
228 struct dentry
*dentry
;
230 /** Request completion callback */
231 void (*end
)(struct fuse_conn
*, struct fuse_req
*);
233 /** Request is stolen from fuse_file->reserved_req */
234 struct file
*stolen_file
;
240 * This structure is created, when the filesystem is mounted, and is
241 * destroyed, when the client device is closed and the filesystem is
245 /** Lock protecting accessess to members of this structure */
248 /** Mutex protecting against directory alias creation */
249 struct mutex inst_mutex
;
254 /** The user id for this mount */
257 /** The group id for this mount */
260 /** The fuse mount flags for this mount */
263 /** Maximum read size */
266 /** Maximum write size */
269 /** Readers of the connection are waiting on this */
270 wait_queue_head_t waitq
;
272 /** The list of pending requests */
273 struct list_head pending
;
275 /** The list of requests being processed */
276 struct list_head processing
;
278 /** The list of requests under I/O */
281 /** Number of requests currently in the background */
282 unsigned num_background
;
284 /** Pending interrupts */
285 struct list_head interrupts
;
287 /** Flag indicating if connection is blocked. This will be
288 the case before the INIT reply is received, and if there
289 are too many outstading backgrounds requests */
292 /** waitq for blocked connection */
293 wait_queue_head_t blocked_waitq
;
295 /** waitq for reserved requests */
296 wait_queue_head_t reserved_req_waitq
;
298 /** The next unique request id */
301 /** Connection established, cleared on umount, connection
302 abort and device release */
305 /** Connection failed (version mismatch). Cannot race with
306 setting other bitfields since it is only set once in INIT
307 reply, before any other request, and never cleared */
308 unsigned conn_error
: 1;
310 /** Connection successful. Only set in INIT */
311 unsigned conn_init
: 1;
313 /** Do readpages asynchronously? Only set in INIT */
314 unsigned async_read
: 1;
317 * The following bitfields are only for optimization purposes
318 * and hence races in setting them will not cause malfunction
321 /** Is fsync not implemented by fs? */
322 unsigned no_fsync
: 1;
324 /** Is fsyncdir not implemented by fs? */
325 unsigned no_fsyncdir
: 1;
327 /** Is flush not implemented by fs? */
328 unsigned no_flush
: 1;
330 /** Is setxattr not implemented by fs? */
331 unsigned no_setxattr
: 1;
333 /** Is getxattr not implemented by fs? */
334 unsigned no_getxattr
: 1;
336 /** Is listxattr not implemented by fs? */
337 unsigned no_listxattr
: 1;
339 /** Is removexattr not implemented by fs? */
340 unsigned no_removexattr
: 1;
342 /** Are file locking primitives not implemented by fs? */
343 unsigned no_lock
: 1;
345 /** Is access not implemented by fs? */
346 unsigned no_access
: 1;
348 /** Is create not implemented by fs? */
349 unsigned no_create
: 1;
351 /** Is interrupt not implemented by fs? */
352 unsigned no_interrupt
: 1;
354 /** Is bmap not implemented by fs? */
355 unsigned no_bmap
: 1;
357 /** The number of requests waiting for completion */
358 atomic_t num_waiting
;
360 /** Negotiated minor version */
363 /** Backing dev info */
364 struct backing_dev_info bdi
;
366 /** Entry on the fuse_conn_list */
367 struct list_head entry
;
372 /** Dentries in the control filesystem */
373 struct dentry
*ctl_dentry
[FUSE_CTL_NUM_DENTRIES
];
375 /** number of dentries used in the above array */
378 /** O_ASYNC requests */
379 struct fasync_struct
*fasync
;
381 /** Key for lock owner ID scrambling */
384 /** Reserved request for the DESTROY message */
385 struct fuse_req
*destroy_req
;
388 static inline struct fuse_conn
*get_fuse_conn_super(struct super_block
*sb
)
390 return sb
->s_fs_info
;
393 static inline struct fuse_conn
*get_fuse_conn(struct inode
*inode
)
395 return get_fuse_conn_super(inode
->i_sb
);
398 static inline struct fuse_inode
*get_fuse_inode(struct inode
*inode
)
400 return container_of(inode
, struct fuse_inode
, inode
);
403 static inline u64
get_node_id(struct inode
*inode
)
405 return get_fuse_inode(inode
)->nodeid
;
408 /** Device operations */
409 extern const struct file_operations fuse_dev_operations
;
412 * Get a filled in inode
414 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
415 int generation
, struct fuse_attr
*attr
);
418 * Send FORGET command
420 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
421 unsigned long nodeid
, u64 nlookup
);
424 * Initialize READ or READDIR request
426 void fuse_read_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
427 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
);
430 * Send OPEN or OPENDIR request
432 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
);
434 struct fuse_file
*fuse_file_alloc(void);
435 void fuse_file_free(struct fuse_file
*ff
);
436 void fuse_finish_open(struct inode
*inode
, struct file
*file
,
437 struct fuse_file
*ff
, struct fuse_open_out
*outarg
);
439 /** Fill in ff->reserved_req with a RELEASE request */
440 void fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
, int opcode
);
443 * Send RELEASE or RELEASEDIR request
445 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
);
448 * Send FSYNC or FSYNCDIR request
450 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
454 * Initialize file operations on a regular file
456 void fuse_init_file_inode(struct inode
*inode
);
459 * Initialize inode operations on regular files and special files
461 void fuse_init_common(struct inode
*inode
);
464 * Initialize inode and file operations on a directory
466 void fuse_init_dir(struct inode
*inode
);
469 * Initialize inode operations on a symlink
471 void fuse_init_symlink(struct inode
*inode
);
474 * Change attributes of an inode
476 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
);
479 * Initialize the client device
481 int fuse_dev_init(void);
484 * Cleanup the client device
486 void fuse_dev_cleanup(void);
488 int fuse_ctl_init(void);
489 void fuse_ctl_cleanup(void);
494 struct fuse_req
*fuse_request_alloc(void);
499 void fuse_request_free(struct fuse_req
*req
);
502 * Get a request, may fail with -ENOMEM
504 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
);
507 * Gets a requests for a file operation, always succeeds
509 struct fuse_req
*fuse_get_req_nofail(struct fuse_conn
*fc
, struct file
*file
);
512 * Decrement reference count of a request. If count goes to zero free
515 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
);
518 * Send a request (synchronous)
520 void request_send(struct fuse_conn
*fc
, struct fuse_req
*req
);
523 * Send a request with no reply
525 void request_send_noreply(struct fuse_conn
*fc
, struct fuse_req
*req
);
528 * Send a request in the background
530 void request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
);
532 /* Abort all requests */
533 void fuse_abort_conn(struct fuse_conn
*fc
);
536 * Invalidate inode attributes
538 void fuse_invalidate_attr(struct inode
*inode
);
541 * Acquire reference to fuse_conn
543 struct fuse_conn
*fuse_conn_get(struct fuse_conn
*fc
);
546 * Release reference to fuse_conn
548 void fuse_conn_put(struct fuse_conn
*fc
);
551 * Add connection to control filesystem
553 int fuse_ctl_add_conn(struct fuse_conn
*fc
);
556 * Remove connection from control filesystem
558 void fuse_ctl_remove_conn(struct fuse_conn
*fc
);
561 * Is file type valid?
563 int fuse_valid_type(int m
);