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 10
25 /** It could be as large as PATH_MAX, but would that have any uses? */
26 #define FUSE_NAME_MAX 1024
28 /** Number of dentries for each connection in the control filesystem */
29 #define FUSE_CTL_NUM_DENTRIES 3
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)
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)
40 /** List of active connections */
41 extern struct list_head fuse_conn_list
;
43 /** Global mutex protecting fuse_conn_list and the control filesystem */
44 extern struct mutex fuse_mutex
;
51 /** Unique ID, which identifies the inode between userspace
55 /** Number of lookups on this inode */
58 /** The request used for sending the FORGET message */
59 struct fuse_req
*forget_req
;
61 /** Time in jiffies until the file attributes are valid */
65 /** FUSE specific file data */
67 /** Request reserved for flush and release */
68 struct fuse_req
*reserved_req
;
70 /** File handle used by userspace */
74 /** One input argument of a request */
80 /** The request input */
82 /** The request header */
83 struct fuse_in_header h
;
85 /** True if the data for the last argument is in req->pages */
88 /** Number of arguments */
91 /** Array of arguments */
92 struct fuse_in_arg args
[3];
95 /** One output argument of a request */
101 /** The request output */
103 /** Header returned from userspace */
104 struct fuse_out_header h
;
107 * The following bitfields are not changed during the request
111 /** Last argument is variable length (can be shorter than
115 /** Last argument is a list of pages to copy data to */
118 /** Zero partially or not copied pages */
119 unsigned page_zeroing
:1;
121 /** Number or arguments */
124 /** Array of arguments */
125 struct fuse_arg args
[3];
128 /** The request state */
129 enum fuse_req_state
{
141 * A request to the client
144 /** This can be on either pending processing or io lists in
146 struct list_head list
;
148 /** Entry on the interrupts list */
149 struct list_head intr_entry
;
154 /** Unique ID for the interrupt request */
158 * The following bitfields are either set once before the
159 * request is queued or setting/clearing them is protected by
163 /** True if the request has reply */
166 /** Force sending of the request even if interrupted */
169 /** The request was aborted */
172 /** Request is sent in the background */
173 unsigned background
:1;
175 /** The request has been interrupted */
176 unsigned interrupted
:1;
178 /** Data is being copied to/from the request */
181 /** Request is counted as "waiting" */
184 /** State of the request */
185 enum fuse_req_state state
;
187 /** The request input */
190 /** The request output */
193 /** Used to wake up the task waiting for completion of request*/
194 wait_queue_head_t waitq
;
196 /** Data for asynchronous requests */
198 struct fuse_forget_in forget_in
;
199 struct fuse_release_in release_in
;
200 struct fuse_init_in init_in
;
201 struct fuse_init_out init_out
;
202 struct fuse_read_in read_in
;
203 struct fuse_lk_in lk_in
;
207 struct page
*pages
[FUSE_MAX_PAGES_PER_REQ
];
209 /** number of pages in vector */
212 /** offset of data on first page */
213 unsigned page_offset
;
215 /** File used in the request (or NULL) */
218 /** vfsmount used in release */
219 struct vfsmount
*vfsmount
;
221 /** dentry used in release */
222 struct dentry
*dentry
;
224 /** Request completion callback */
225 void (*end
)(struct fuse_conn
*, struct fuse_req
*);
227 /** Request is stolen from fuse_file->reserved_req */
228 struct file
*stolen_file
;
234 * This structure is created, when the filesystem is mounted, and is
235 * destroyed, when the client device is closed and the filesystem is
239 /** Lock protecting accessess to members of this structure */
245 /** The user id for this mount */
248 /** The group id for this mount */
251 /** The fuse mount flags for this mount */
254 /** Maximum read size */
257 /** Maximum write size */
260 /** Readers of the connection are waiting on this */
261 wait_queue_head_t waitq
;
263 /** The list of pending requests */
264 struct list_head pending
;
266 /** The list of requests being processed */
267 struct list_head processing
;
269 /** The list of requests under I/O */
272 /** Number of requests currently in the background */
273 unsigned num_background
;
275 /** Pending interrupts */
276 struct list_head interrupts
;
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 */
283 /** waitq for blocked connection */
284 wait_queue_head_t blocked_waitq
;
286 /** The next unique request id */
289 /** Connection established, cleared on umount, connection
290 abort and device release */
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 */
296 unsigned conn_error
: 1;
298 /** Do readpages asynchronously? Only set in INIT */
299 unsigned async_read
: 1;
302 * The following bitfields are only for optimization purposes
303 * and hence races in setting them will not cause malfunction
306 /** Is fsync not implemented by fs? */
307 unsigned no_fsync
: 1;
309 /** Is fsyncdir not implemented by fs? */
310 unsigned no_fsyncdir
: 1;
312 /** Is flush not implemented by fs? */
313 unsigned no_flush
: 1;
315 /** Is setxattr not implemented by fs? */
316 unsigned no_setxattr
: 1;
318 /** Is getxattr not implemented by fs? */
319 unsigned no_getxattr
: 1;
321 /** Is listxattr not implemented by fs? */
322 unsigned no_listxattr
: 1;
324 /** Is removexattr not implemented by fs? */
325 unsigned no_removexattr
: 1;
327 /** Are file locking primitives not implemented by fs? */
328 unsigned no_lock
: 1;
330 /** Is access not implemented by fs? */
331 unsigned no_access
: 1;
333 /** Is create not implemented by fs? */
334 unsigned no_create
: 1;
336 /** Is interrupt not implemented by fs? */
337 unsigned no_interrupt
: 1;
339 /** The number of requests waiting for completion */
340 atomic_t num_waiting
;
342 /** Negotiated minor version */
345 /** Backing dev info */
346 struct backing_dev_info bdi
;
348 /** Entry on the fuse_conn_list */
349 struct list_head entry
;
354 /** Dentries in the control filesystem */
355 struct dentry
*ctl_dentry
[FUSE_CTL_NUM_DENTRIES
];
357 /** number of dentries used in the above array */
360 /** O_ASYNC requests */
361 struct fasync_struct
*fasync
;
363 /** Key for lock owner ID scrambling */
367 static inline struct fuse_conn
*get_fuse_conn_super(struct super_block
*sb
)
369 return sb
->s_fs_info
;
372 static inline struct fuse_conn
*get_fuse_conn(struct inode
*inode
)
374 return get_fuse_conn_super(inode
->i_sb
);
377 static inline struct fuse_inode
*get_fuse_inode(struct inode
*inode
)
379 return container_of(inode
, struct fuse_inode
, inode
);
382 static inline u64
get_node_id(struct inode
*inode
)
384 return get_fuse_inode(inode
)->nodeid
;
387 /** Device operations */
388 extern const struct file_operations fuse_dev_operations
;
391 * Get a filled in inode
393 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
394 int generation
, struct fuse_attr
*attr
);
397 * Send FORGET command
399 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
400 unsigned long nodeid
, u64 nlookup
);
403 * Initialize READ or READDIR request
405 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
,
406 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
);
409 * Send OPEN or OPENDIR request
411 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
);
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
);
419 struct fuse_req
*fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
,
422 * Send RELEASE or RELEASEDIR request
424 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
);
427 * Send FSYNC or FSYNCDIR request
429 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
433 * Initialize file operations on a regular file
435 void fuse_init_file_inode(struct inode
*inode
);
438 * Initialize inode operations on regular files and special files
440 void fuse_init_common(struct inode
*inode
);
443 * Initialize inode and file operations on a directory
445 void fuse_init_dir(struct inode
*inode
);
448 * Initialize inode operations on a symlink
450 void fuse_init_symlink(struct inode
*inode
);
453 * Change attributes of an inode
455 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
);
458 * Initialize the client device
460 int fuse_dev_init(void);
463 * Cleanup the client device
465 void fuse_dev_cleanup(void);
467 int fuse_ctl_init(void);
468 void fuse_ctl_cleanup(void);
473 struct fuse_req
*fuse_request_alloc(void);
478 void fuse_request_free(struct fuse_req
*req
);
481 * Get a request, may fail with -ENOMEM
483 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
);
486 * Gets a requests for a file operation, always succeeds
488 struct fuse_req
*fuse_get_req_nofail(struct fuse_conn
*fc
, struct file
*file
);
491 * Decrement reference count of a request. If count goes to zero free
494 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
);
497 * Send a request (synchronous)
499 void request_send(struct fuse_conn
*fc
, struct fuse_req
*req
);
502 * Send a request with no reply
504 void request_send_noreply(struct fuse_conn
*fc
, struct fuse_req
*req
);
507 * Send a request in the background
509 void request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
);
511 /* Abort all requests */
512 void fuse_abort_conn(struct fuse_conn
*fc
);
515 * Get the attributes of a file
517 int fuse_do_getattr(struct inode
*inode
);
520 * Invalidate inode attributes
522 void fuse_invalidate_attr(struct inode
*inode
);
525 * Acquire reference to fuse_conn
527 struct fuse_conn
*fuse_conn_get(struct fuse_conn
*fc
);
530 * Release reference to fuse_conn
532 void fuse_conn_put(struct fuse_conn
*fc
);
535 * Add connection to control filesystem
537 int fuse_ctl_add_conn(struct fuse_conn
*fc
);
540 * Remove connection from control filesystem
542 void fuse_ctl_remove_conn(struct fuse_conn
*fc
);