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 */
67 /** The sticky bit in inode->i_mode may have been removed, so
68 preserve the original mode */
72 /** FUSE specific file data */
74 /** Request reserved for flush and release */
75 struct fuse_req
*reserved_req
;
77 /** File handle used by userspace */
84 /** One input argument of a request */
90 /** The request input */
92 /** The request header */
93 struct fuse_in_header h
;
95 /** True if the data for the last argument is in req->pages */
98 /** Number of arguments */
101 /** Array of arguments */
102 struct fuse_in_arg args
[3];
105 /** One output argument of a request */
111 /** The request output */
113 /** Header returned from userspace */
114 struct fuse_out_header h
;
117 * The following bitfields are not changed during the request
121 /** Last argument is variable length (can be shorter than
125 /** Last argument is a list of pages to copy data to */
128 /** Zero partially or not copied pages */
129 unsigned page_zeroing
:1;
131 /** Number or arguments */
134 /** Array of arguments */
135 struct fuse_arg args
[3];
138 /** The request state */
139 enum fuse_req_state
{
151 * A request to the client
154 /** This can be on either pending processing or io lists in
156 struct list_head list
;
158 /** Entry on the interrupts list */
159 struct list_head intr_entry
;
164 /** Unique ID for the interrupt request */
168 * The following bitfields are either set once before the
169 * request is queued or setting/clearing them is protected by
173 /** True if the request has reply */
176 /** Force sending of the request even if interrupted */
179 /** The request was aborted */
182 /** Request is sent in the background */
183 unsigned background
:1;
185 /** The request has been interrupted */
186 unsigned interrupted
:1;
188 /** Data is being copied to/from the request */
191 /** Request is counted as "waiting" */
194 /** State of the request */
195 enum fuse_req_state state
;
197 /** The request input */
200 /** The request output */
203 /** Used to wake up the task waiting for completion of request*/
204 wait_queue_head_t waitq
;
206 /** Data for asynchronous requests */
208 struct fuse_forget_in forget_in
;
209 struct fuse_release_in release_in
;
210 struct fuse_init_in init_in
;
211 struct fuse_init_out init_out
;
212 struct fuse_read_in read_in
;
213 struct fuse_lk_in lk_in
;
217 struct page
*pages
[FUSE_MAX_PAGES_PER_REQ
];
219 /** number of pages in vector */
222 /** offset of data on first page */
223 unsigned page_offset
;
225 /** File used in the request (or NULL) */
226 struct fuse_file
*ff
;
228 /** vfsmount used in release */
229 struct vfsmount
*vfsmount
;
231 /** dentry used in release */
232 struct dentry
*dentry
;
234 /** Request completion callback */
235 void (*end
)(struct fuse_conn
*, struct fuse_req
*);
237 /** Request is stolen from fuse_file->reserved_req */
238 struct file
*stolen_file
;
244 * This structure is created, when the filesystem is mounted, and is
245 * destroyed, when the client device is closed and the filesystem is
249 /** Lock protecting accessess to members of this structure */
252 /** Mutex protecting against directory alias creation */
253 struct mutex inst_mutex
;
258 /** The user id for this mount */
261 /** The group id for this mount */
264 /** The fuse mount flags for this mount */
267 /** Maximum read size */
270 /** Maximum write size */
273 /** Readers of the connection are waiting on this */
274 wait_queue_head_t waitq
;
276 /** The list of pending requests */
277 struct list_head pending
;
279 /** The list of requests being processed */
280 struct list_head processing
;
282 /** The list of requests under I/O */
285 /** Number of requests currently in the background */
286 unsigned num_background
;
288 /** Pending interrupts */
289 struct list_head interrupts
;
291 /** Flag indicating if connection is blocked. This will be
292 the case before the INIT reply is received, and if there
293 are too many outstading backgrounds requests */
296 /** waitq for blocked connection */
297 wait_queue_head_t blocked_waitq
;
299 /** waitq for reserved requests */
300 wait_queue_head_t reserved_req_waitq
;
302 /** The next unique request id */
305 /** Connection established, cleared on umount, connection
306 abort and device release */
309 /** Connection failed (version mismatch). Cannot race with
310 setting other bitfields since it is only set once in INIT
311 reply, before any other request, and never cleared */
312 unsigned conn_error
: 1;
314 /** Connection successful. Only set in INIT */
315 unsigned conn_init
: 1;
317 /** Do readpages asynchronously? Only set in INIT */
318 unsigned async_read
: 1;
321 * The following bitfields are only for optimization purposes
322 * and hence races in setting them will not cause malfunction
325 /** Is fsync not implemented by fs? */
326 unsigned no_fsync
: 1;
328 /** Is fsyncdir not implemented by fs? */
329 unsigned no_fsyncdir
: 1;
331 /** Is flush not implemented by fs? */
332 unsigned no_flush
: 1;
334 /** Is setxattr not implemented by fs? */
335 unsigned no_setxattr
: 1;
337 /** Is getxattr not implemented by fs? */
338 unsigned no_getxattr
: 1;
340 /** Is listxattr not implemented by fs? */
341 unsigned no_listxattr
: 1;
343 /** Is removexattr not implemented by fs? */
344 unsigned no_removexattr
: 1;
346 /** Are file locking primitives not implemented by fs? */
347 unsigned no_lock
: 1;
349 /** Is access not implemented by fs? */
350 unsigned no_access
: 1;
352 /** Is create not implemented by fs? */
353 unsigned no_create
: 1;
355 /** Is interrupt not implemented by fs? */
356 unsigned no_interrupt
: 1;
358 /** Is bmap not implemented by fs? */
359 unsigned no_bmap
: 1;
361 /** The number of requests waiting for completion */
362 atomic_t num_waiting
;
364 /** Negotiated minor version */
367 /** Backing dev info */
368 struct backing_dev_info bdi
;
370 /** Entry on the fuse_conn_list */
371 struct list_head entry
;
376 /** Dentries in the control filesystem */
377 struct dentry
*ctl_dentry
[FUSE_CTL_NUM_DENTRIES
];
379 /** number of dentries used in the above array */
382 /** O_ASYNC requests */
383 struct fasync_struct
*fasync
;
385 /** Key for lock owner ID scrambling */
388 /** Reserved request for the DESTROY message */
389 struct fuse_req
*destroy_req
;
392 static inline struct fuse_conn
*get_fuse_conn_super(struct super_block
*sb
)
394 return sb
->s_fs_info
;
397 static inline struct fuse_conn
*get_fuse_conn(struct inode
*inode
)
399 return get_fuse_conn_super(inode
->i_sb
);
402 static inline struct fuse_inode
*get_fuse_inode(struct inode
*inode
)
404 return container_of(inode
, struct fuse_inode
, inode
);
407 static inline u64
get_node_id(struct inode
*inode
)
409 return get_fuse_inode(inode
)->nodeid
;
412 /** Device operations */
413 extern const struct file_operations fuse_dev_operations
;
416 * Get a filled in inode
418 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
419 int generation
, struct fuse_attr
*attr
);
422 * Send FORGET command
424 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
425 unsigned long nodeid
, u64 nlookup
);
428 * Initialize READ or READDIR request
430 void fuse_read_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
431 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
);
434 * Send OPEN or OPENDIR request
436 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
);
438 struct fuse_file
*fuse_file_alloc(void);
439 void fuse_file_free(struct fuse_file
*ff
);
440 void fuse_finish_open(struct inode
*inode
, struct file
*file
,
441 struct fuse_file
*ff
, struct fuse_open_out
*outarg
);
443 /** Fill in ff->reserved_req with a RELEASE request */
444 void fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
, int opcode
);
447 * Send RELEASE or RELEASEDIR request
449 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
);
452 * Send FSYNC or FSYNCDIR request
454 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
458 * Initialize file operations on a regular file
460 void fuse_init_file_inode(struct inode
*inode
);
463 * Initialize inode operations on regular files and special files
465 void fuse_init_common(struct inode
*inode
);
468 * Initialize inode and file operations on a directory
470 void fuse_init_dir(struct inode
*inode
);
473 * Initialize inode operations on a symlink
475 void fuse_init_symlink(struct inode
*inode
);
478 * Change attributes of an inode
480 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
);
483 * Initialize the client device
485 int fuse_dev_init(void);
488 * Cleanup the client device
490 void fuse_dev_cleanup(void);
492 int fuse_ctl_init(void);
493 void fuse_ctl_cleanup(void);
498 struct fuse_req
*fuse_request_alloc(void);
503 void fuse_request_free(struct fuse_req
*req
);
506 * Get a request, may fail with -ENOMEM
508 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
);
511 * Gets a requests for a file operation, always succeeds
513 struct fuse_req
*fuse_get_req_nofail(struct fuse_conn
*fc
, struct file
*file
);
516 * Decrement reference count of a request. If count goes to zero free
519 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
);
522 * Send a request (synchronous)
524 void request_send(struct fuse_conn
*fc
, struct fuse_req
*req
);
527 * Send a request with no reply
529 void request_send_noreply(struct fuse_conn
*fc
, struct fuse_req
*req
);
532 * Send a request in the background
534 void request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
);
536 /* Abort all requests */
537 void fuse_abort_conn(struct fuse_conn
*fc
);
540 * Invalidate inode attributes
542 void fuse_invalidate_attr(struct inode
*inode
);
545 * Acquire reference to fuse_conn
547 struct fuse_conn
*fuse_conn_get(struct fuse_conn
*fc
);
550 * Release reference to fuse_conn
552 void fuse_conn_put(struct fuse_conn
*fc
);
555 * Add connection to control filesystem
557 int fuse_ctl_add_conn(struct fuse_conn
*fc
);
560 * Remove connection from control filesystem
562 void fuse_ctl_remove_conn(struct fuse_conn
*fc
);
565 * Is file type valid?
567 int fuse_valid_type(int m
);