2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 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/wait.h>
12 #include <linux/list.h>
13 #include <linux/spinlock.h>
15 #include <linux/backing-dev.h>
16 #include <asm/semaphore.h>
18 /** Max number of pages that can be used in a single read request */
19 #define FUSE_MAX_PAGES_PER_REQ 32
21 /** If more requests are outstanding, then the operation will block */
22 #define FUSE_MAX_OUTSTANDING 10
29 /** Unique ID, which identifies the inode between userspace
33 /** Number of lookups on this inode */
36 /** The request used for sending the FORGET message */
37 struct fuse_req
*forget_req
;
39 /** Time in jiffies until the file attributes are valid */
43 /** FUSE specific file data */
45 /** Request reserved for flush and release */
46 struct fuse_req
*release_req
;
48 /** File handle used by userspace */
52 /** One input argument of a request */
58 /** The request input */
60 /** The request header */
61 struct fuse_in_header h
;
63 /** True if the data for the last argument is in req->pages */
66 /** Number of arguments */
69 /** Array of arguments */
70 struct fuse_in_arg args
[3];
73 /** One output argument of a request */
79 /** The request output */
81 /** Header returned from userspace */
82 struct fuse_out_header h
;
84 /** Last argument is variable length (can be shorter than
88 /** Last argument is a list of pages to copy data to */
91 /** Zero partially or not copied pages */
92 unsigned page_zeroing
:1;
94 /** Number or arguments */
97 /** Array of arguments */
98 struct fuse_arg args
[3];
105 * A request to the client
108 /** This can be on either unused_list, pending or processing
109 lists in fuse_conn */
110 struct list_head list
;
115 /** True if the request has reply */
118 /** The request is preallocated */
119 unsigned preallocated
:1;
121 /** The request was interrupted */
122 unsigned interrupted
:1;
124 /** Request is sent in the background */
125 unsigned background
:1;
127 /** Data is being copied to/from the request */
130 /** Request has been sent to userspace */
133 /** The request is finished */
136 /** The request input */
139 /** The request output */
142 /** Used to wake up the task waiting for completion of request*/
143 wait_queue_head_t waitq
;
145 /** Data for asynchronous requests */
147 struct fuse_forget_in forget_in
;
148 struct fuse_release_in release_in
;
149 struct fuse_init_in_out init_in_out
;
153 struct page
*pages
[FUSE_MAX_PAGES_PER_REQ
];
155 /** number of pages in vector */
158 /** offset of data on first page */
159 unsigned page_offset
;
161 /** Inode used in the request */
164 /** Second inode used in the request (or NULL) */
165 struct inode
*inode2
;
167 /** File used in the request (or NULL) */
174 * This structure is created, when the filesystem is mounted, and is
175 * destroyed, when the client device is closed and the filesystem is
179 /** The superblock of the mounted filesystem */
180 struct super_block
*sb
;
182 /** The opened client device */
185 /** The user id for this mount */
188 /** Readers of the connection are waiting on this */
189 wait_queue_head_t waitq
;
191 /** The list of pending requests */
192 struct list_head pending
;
194 /** The list of requests being processed */
195 struct list_head processing
;
197 /** Controls the maximum number of outstanding requests */
198 struct semaphore outstanding_sem
;
200 /** This counts the number of outstanding requests if
201 outstanding_sem would go negative */
202 unsigned outstanding_debt
;
204 /** The list of unused requests */
205 struct list_head unused_list
;
207 /** The next unique request id */
210 /** Connection failed (version mismatch) */
211 unsigned conn_error
: 1;
213 /** Is fsync not implemented by fs? */
214 unsigned no_fsync
: 1;
216 /** Is flush not implemented by fs? */
217 unsigned no_flush
: 1;
219 /** Backing dev info */
220 struct backing_dev_info bdi
;
223 struct fuse_getdir_out_i
{
225 void *file
; /* Used by kernel only */
228 static inline struct fuse_conn
**get_fuse_conn_super_p(struct super_block
*sb
)
230 return (struct fuse_conn
**) &sb
->s_fs_info
;
233 static inline struct fuse_conn
*get_fuse_conn_super(struct super_block
*sb
)
235 return *get_fuse_conn_super_p(sb
);
238 static inline struct fuse_conn
*get_fuse_conn(struct inode
*inode
)
240 return get_fuse_conn_super(inode
->i_sb
);
243 static inline struct fuse_inode
*get_fuse_inode(struct inode
*inode
)
245 return container_of(inode
, struct fuse_inode
, inode
);
248 static inline u64
get_node_id(struct inode
*inode
)
250 return get_fuse_inode(inode
)->nodeid
;
253 /** Device operations */
254 extern struct file_operations fuse_dev_operations
;
257 * This is the single global spinlock which protects FUSE's structures
259 * The following data is protected by this lock:
261 * - the private_data field of the device file
262 * - the s_fs_info field of the super block
263 * - unused_list, pending, processing lists in fuse_conn
264 * - the unique request ID counter reqctr in fuse_conn
265 * - the sb (super_block) field in fuse_conn
266 * - the file (device file) field in fuse_conn
268 extern spinlock_t fuse_lock
;
271 * Get a filled in inode
273 struct inode
*fuse_iget(struct super_block
*sb
, unsigned long nodeid
,
274 int generation
, struct fuse_attr
*attr
);
277 * Send FORGET command
279 void fuse_send_forget(struct fuse_conn
*fc
, struct fuse_req
*req
,
280 unsigned long nodeid
, u64 nlookup
);
283 * Initialise file operations on a regular file
285 void fuse_init_file_inode(struct inode
*inode
);
288 * Initialise inode operations on regular files and special files
290 void fuse_init_common(struct inode
*inode
);
293 * Initialise inode and file operations on a directory
295 void fuse_init_dir(struct inode
*inode
);
298 * Initialise inode operations on a symlink
300 void fuse_init_symlink(struct inode
*inode
);
303 * Change attributes of an inode
305 void fuse_change_attributes(struct inode
*inode
, struct fuse_attr
*attr
);
308 * Check if the connection can be released, and if yes, then free the
309 * connection structure
311 void fuse_release_conn(struct fuse_conn
*fc
);
314 * Initialize the client device
316 int fuse_dev_init(void);
319 * Cleanup the client device
321 void fuse_dev_cleanup(void);
326 struct fuse_req
*fuse_request_alloc(void);
331 void fuse_request_free(struct fuse_req
*req
);
334 * Reinitialize a request, the preallocated flag is left unmodified
336 void fuse_reset_request(struct fuse_req
*req
);
339 * Reserve a preallocated request
341 struct fuse_req
*fuse_get_request(struct fuse_conn
*fc
);
344 * Reserve a preallocated request, only interruptible by SIGKILL
346 struct fuse_req
*fuse_get_request_nonint(struct fuse_conn
*fc
);
349 * Decrement reference count of a request. If count goes to zero put
350 * on unused list (preallocated) or free reqest (not preallocated).
352 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
);
355 * Send a request (synchronous, interruptible)
357 void request_send(struct fuse_conn
*fc
, struct fuse_req
*req
);
360 * Send a request (synchronous, non-interruptible except by SIGKILL)
362 void request_send_nonint(struct fuse_conn
*fc
, struct fuse_req
*req
);
365 * Send a request with no reply
367 void request_send_noreply(struct fuse_conn
*fc
, struct fuse_req
*req
);
370 * Send a request in the background
372 void request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
);
375 * Get the attributes of a file
377 int fuse_do_getattr(struct inode
*inode
);
380 * Invalidate inode attributes
382 void fuse_invalidate_attr(struct inode
*inode
);
385 * Send the INIT message
387 void fuse_send_init(struct fuse_conn
*fc
);