blob: eb3166625ca92a6b49f0db441699a27c5c8b5e28 [file] [log] [blame]
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredid7133112006-04-10 22:54:55 -07003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include <linux/fuse.h>
10#include <linux/fs.h>
Miklos Szeredi51eb01e2006-06-25 05:48:50 -070011#include <linux/mount.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070012#include <linux/wait.h>
13#include <linux/list.h>
14#include <linux/spinlock.h>
15#include <linux/mm.h>
16#include <linux/backing-dev.h>
Miklos Szeredibafa9652006-06-25 05:48:51 -070017#include <linux/mutex.h>
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070018
Miklos Szeredi334f4852005-09-09 13:10:27 -070019/** Max number of pages that can be used in a single read request */
20#define FUSE_MAX_PAGES_PER_REQ 32
21
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070022/** Maximum number of outstanding background requests */
23#define FUSE_MAX_BACKGROUND 10
24
Miklos Szeredi1d3d7522006-01-06 00:19:40 -080025/** It could be as large as PATH_MAX, but would that have any uses? */
26#define FUSE_NAME_MAX 1024
27
Miklos Szeredibafa9652006-06-25 05:48:51 -070028/** Number of dentries for each connection in the control filesystem */
29#define FUSE_CTL_NUM_DENTRIES 3
30
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070031/** 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)
35
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)
39
Miklos Szeredibafa9652006-06-25 05:48:51 -070040/** List of active connections */
41extern struct list_head fuse_conn_list;
42
43/** Global mutex protecting fuse_conn_list and the control filesystem */
44extern struct mutex fuse_mutex;
Miklos Szeredi413ef8c2005-09-09 13:10:35 -070045
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070046/** FUSE inode */
47struct fuse_inode {
48 /** Inode data */
49 struct inode inode;
50
51 /** Unique ID, which identifies the inode between userspace
52 * and kernel */
53 u64 nodeid;
54
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070055 /** Number of lookups on this inode */
56 u64 nlookup;
57
Miklos Szeredie5e55582005-09-09 13:10:28 -070058 /** The request used for sending the FORGET message */
59 struct fuse_req *forget_req;
60
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070061 /** Time in jiffies until the file attributes are valid */
62 unsigned long i_time;
63};
64
Miklos Szeredib6aeade2005-09-09 13:10:30 -070065/** FUSE specific file data */
66struct fuse_file {
67 /** Request reserved for flush and release */
68 struct fuse_req *release_req;
69
70 /** File handle used by userspace */
71 u64 fh;
72};
73
Miklos Szeredi334f4852005-09-09 13:10:27 -070074/** One input argument of a request */
75struct fuse_in_arg {
76 unsigned size;
77 const void *value;
78};
79
80/** The request input */
81struct fuse_in {
82 /** The request header */
83 struct fuse_in_header h;
84
85 /** True if the data for the last argument is in req->pages */
86 unsigned argpages:1;
87
88 /** Number of arguments */
89 unsigned numargs;
90
91 /** Array of arguments */
92 struct fuse_in_arg args[3];
93};
94
95/** One output argument of a request */
96struct fuse_arg {
97 unsigned size;
98 void *value;
99};
100
101/** The request output */
102struct fuse_out {
103 /** Header returned from userspace */
104 struct fuse_out_header h;
105
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800106 /*
107 * The following bitfields are not changed during the request
108 * processing
109 */
110
Miklos Szeredi334f4852005-09-09 13:10:27 -0700111 /** Last argument is variable length (can be shorter than
112 arg->size) */
113 unsigned argvar:1;
114
115 /** Last argument is a list of pages to copy data to */
116 unsigned argpages:1;
117
118 /** Zero partially or not copied pages */
119 unsigned page_zeroing:1;
120
121 /** Number or arguments */
122 unsigned numargs;
123
124 /** Array of arguments */
125 struct fuse_arg args[3];
126};
127
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800128/** The request state */
129enum fuse_req_state {
130 FUSE_REQ_INIT = 0,
131 FUSE_REQ_PENDING,
132 FUSE_REQ_READING,
133 FUSE_REQ_SENT,
134 FUSE_REQ_FINISHED
135};
136
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800137struct fuse_conn;
138
Miklos Szeredi334f4852005-09-09 13:10:27 -0700139/**
140 * A request to the client
141 */
142struct fuse_req {
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700143 /** This can be on either pending processing or io lists in
144 fuse_conn */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700145 struct list_head list;
146
147 /** refcount */
148 atomic_t count;
149
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800150 /*
151 * The following bitfields are either set once before the
152 * request is queued or setting/clearing them is protected by
Miklos Szeredid7133112006-04-10 22:54:55 -0700153 * fuse_conn->lock
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800154 */
155
Miklos Szeredi334f4852005-09-09 13:10:27 -0700156 /** True if the request has reply */
157 unsigned isreply:1;
158
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700159 /** Force sending of the request even if interrupted */
160 unsigned force:1;
161
Miklos Szeredi334f4852005-09-09 13:10:27 -0700162 /** The request was interrupted */
163 unsigned interrupted:1;
164
165 /** Request is sent in the background */
166 unsigned background:1;
167
168 /** Data is being copied to/from the request */
169 unsigned locked:1;
170
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200171 /** Request is counted as "waiting" */
172 unsigned waiting:1;
173
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800174 /** State of the request */
175 enum fuse_req_state state;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700176
177 /** The request input */
178 struct fuse_in in;
179
180 /** The request output */
181 struct fuse_out out;
182
183 /** Used to wake up the task waiting for completion of request*/
184 wait_queue_head_t waitq;
185
186 /** Data for asynchronous requests */
187 union {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700188 struct fuse_forget_in forget_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700189 struct fuse_release_in release_in;
Miklos Szeredi3ec870d2006-01-06 00:19:41 -0800190 struct fuse_init_in init_in;
191 struct fuse_init_out init_out;
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800192 struct fuse_read_in read_in;
Miklos Szeredi71421252006-06-25 05:48:52 -0700193 struct fuse_lk_in lk_in;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700194 } misc;
195
196 /** page vector */
197 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
198
199 /** number of pages in vector */
200 unsigned num_pages;
201
202 /** offset of data on first page */
203 unsigned page_offset;
204
Miklos Szeredi334f4852005-09-09 13:10:27 -0700205 /** File used in the request (or NULL) */
206 struct file *file;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800207
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700208 /** vfsmount used in release */
209 struct vfsmount *vfsmount;
210
211 /** dentry used in release */
212 struct dentry *dentry;
213
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800214 /** Request completion callback */
215 void (*end)(struct fuse_conn *, struct fuse_req *);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700216};
217
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700218/**
219 * A Fuse connection.
220 *
221 * This structure is created, when the filesystem is mounted, and is
222 * destroyed, when the client device is closed and the filesystem is
223 * unmounted.
224 */
225struct fuse_conn {
Miklos Szeredid7133112006-04-10 22:54:55 -0700226 /** Lock protecting accessess to members of this structure */
227 spinlock_t lock;
228
Miklos Szeredibafa9652006-06-25 05:48:51 -0700229 /** Refcount */
230 atomic_t count;
231
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700232 /** The user id for this mount */
233 uid_t user_id;
234
Miklos Szeredi87729a52005-09-09 13:10:34 -0700235 /** The group id for this mount */
236 gid_t group_id;
237
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700238 /** The fuse mount flags for this mount */
239 unsigned flags;
240
Miklos Szeredidb50b962005-09-09 13:10:33 -0700241 /** Maximum read size */
242 unsigned max_read;
243
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700244 /** Maximum write size */
245 unsigned max_write;
246
Miklos Szeredi334f4852005-09-09 13:10:27 -0700247 /** Readers of the connection are waiting on this */
248 wait_queue_head_t waitq;
249
250 /** The list of pending requests */
251 struct list_head pending;
252
253 /** The list of requests being processed */
254 struct list_head processing;
255
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800256 /** The list of requests under I/O */
257 struct list_head io;
258
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700259 /** Number of requests currently in the background */
260 unsigned num_background;
261
262 /** Flag indicating if connection is blocked. This will be
263 the case before the INIT reply is received, and if there
264 are too many outstading backgrounds requests */
265 int blocked;
266
267 /** waitq for blocked connection */
268 wait_queue_head_t blocked_waitq;
269
Miklos Szeredi334f4852005-09-09 13:10:27 -0700270 /** The next unique request id */
271 u64 reqctr;
272
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800273 /** Connection established, cleared on umount, connection
274 abort and device release */
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800275 unsigned connected;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700276
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800277 /** Connection failed (version mismatch). Cannot race with
278 setting other bitfields since it is only set once in INIT
279 reply, before any other request, and never cleared */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700280 unsigned conn_error : 1;
281
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800282 /** Do readpages asynchronously? Only set in INIT */
283 unsigned async_read : 1;
284
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800285 /*
286 * The following bitfields are only for optimization purposes
287 * and hence races in setting them will not cause malfunction
288 */
289
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700290 /** Is fsync not implemented by fs? */
291 unsigned no_fsync : 1;
292
Miklos Szeredi82547982005-09-09 13:10:38 -0700293 /** Is fsyncdir not implemented by fs? */
294 unsigned no_fsyncdir : 1;
295
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700296 /** Is flush not implemented by fs? */
297 unsigned no_flush : 1;
298
Miklos Szeredi92a87802005-09-09 13:10:31 -0700299 /** Is setxattr not implemented by fs? */
300 unsigned no_setxattr : 1;
301
302 /** Is getxattr not implemented by fs? */
303 unsigned no_getxattr : 1;
304
305 /** Is listxattr not implemented by fs? */
306 unsigned no_listxattr : 1;
307
308 /** Is removexattr not implemented by fs? */
309 unsigned no_removexattr : 1;
310
Miklos Szeredi71421252006-06-25 05:48:52 -0700311 /** Are file locking primitives not implemented by fs? */
312 unsigned no_lock : 1;
313
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800314 /** Is access not implemented by fs? */
315 unsigned no_access : 1;
316
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800317 /** Is create not implemented by fs? */
318 unsigned no_create : 1;
319
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800320 /** The number of requests waiting for completion */
321 atomic_t num_waiting;
322
Miklos Szeredi45714d62006-01-06 00:19:36 -0800323 /** Negotiated minor version */
324 unsigned minor;
325
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700326 /** Backing dev info */
327 struct backing_dev_info bdi;
Miklos Szeredif543f252006-01-16 22:14:35 -0800328
Miklos Szeredibafa9652006-06-25 05:48:51 -0700329 /** Entry on the fuse_conn_list */
330 struct list_head entry;
331
332 /** Unique ID */
333 u64 id;
334
335 /** Dentries in the control filesystem */
336 struct dentry *ctl_dentry[FUSE_CTL_NUM_DENTRIES];
337
338 /** number of dentries used in the above array */
339 int ctl_ndents;
Jeff Dike385a17b2006-04-10 22:54:52 -0700340
341 /** O_ASYNC requests */
342 struct fasync_struct *fasync;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700343};
344
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700345static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
346{
Miklos Szeredi6383bda2006-01-16 22:14:29 -0800347 return sb->s_fs_info;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700348}
349
350static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
351{
352 return get_fuse_conn_super(inode->i_sb);
353}
354
355static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
356{
357 return container_of(inode, struct fuse_inode, inode);
358}
359
360static inline u64 get_node_id(struct inode *inode)
361{
362 return get_fuse_inode(inode)->nodeid;
363}
364
Miklos Szeredi334f4852005-09-09 13:10:27 -0700365/** Device operations */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800366extern const struct file_operations fuse_dev_operations;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700368/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700369 * Get a filled in inode
370 */
371struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700372 int generation, struct fuse_attr *attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700373
374/**
375 * Send FORGET command
376 */
377void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700378 unsigned long nodeid, u64 nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700379
380/**
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800381 * Initialize READ or READDIR request
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700382 */
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800383void fuse_read_fill(struct fuse_req *req, struct file *file,
384 struct inode *inode, loff_t pos, size_t count, int opcode);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700385
386/**
387 * Send OPEN or OPENDIR request
388 */
389int fuse_open_common(struct inode *inode, struct file *file, int isdir);
390
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800391struct fuse_file *fuse_file_alloc(void);
392void fuse_file_free(struct fuse_file *ff);
393void fuse_finish_open(struct inode *inode, struct file *file,
394 struct fuse_file *ff, struct fuse_open_out *outarg);
395
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700396/** */
397struct fuse_req *fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags,
398 int opcode);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700399/**
400 * Send RELEASE or RELEASEDIR request
401 */
402int fuse_release_common(struct inode *inode, struct file *file, int isdir);
403
404/**
Miklos Szeredi82547982005-09-09 13:10:38 -0700405 * Send FSYNC or FSYNCDIR request
406 */
407int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
408 int isdir);
409
410/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800411 * Initialize file operations on a regular file
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700412 */
413void fuse_init_file_inode(struct inode *inode);
414
415/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800416 * Initialize inode operations on regular files and special files
Miklos Szeredie5e55582005-09-09 13:10:28 -0700417 */
418void fuse_init_common(struct inode *inode);
419
420/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800421 * Initialize inode and file operations on a directory
Miklos Szeredie5e55582005-09-09 13:10:28 -0700422 */
423void fuse_init_dir(struct inode *inode);
424
425/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800426 * Initialize inode operations on a symlink
Miklos Szeredie5e55582005-09-09 13:10:28 -0700427 */
428void fuse_init_symlink(struct inode *inode);
429
430/**
431 * Change attributes of an inode
432 */
433void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
434
435/**
Miklos Szeredi334f4852005-09-09 13:10:27 -0700436 * Initialize the client device
437 */
438int fuse_dev_init(void);
439
440/**
441 * Cleanup the client device
442 */
443void fuse_dev_cleanup(void);
444
Miklos Szeredibafa9652006-06-25 05:48:51 -0700445int fuse_ctl_init(void);
446void fuse_ctl_cleanup(void);
447
Miklos Szeredi334f4852005-09-09 13:10:27 -0700448/**
449 * Allocate a request
450 */
451struct fuse_req *fuse_request_alloc(void);
452
453/**
454 * Free a request
455 */
456void fuse_request_free(struct fuse_req *req);
457
458/**
Miklos Szeredi334f4852005-09-09 13:10:27 -0700459 * Reserve a preallocated request
460 */
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700461struct fuse_req *fuse_get_req(struct fuse_conn *fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700462
463/**
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700464 * Decrement reference count of a request. If count goes to zero free
465 * the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700466 */
467void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
468
469/**
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700470 * Send a request (synchronous)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700471 */
472void request_send(struct fuse_conn *fc, struct fuse_req *req);
473
474/**
Miklos Szeredi334f4852005-09-09 13:10:27 -0700475 * Send a request with no reply
476 */
477void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
478
479/**
480 * Send a request in the background
481 */
482void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
483
Miklos Szeredi5a5fb1e2006-04-26 10:48:55 +0200484/* Abort all requests */
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800485void fuse_abort_conn(struct fuse_conn *fc);
486
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700487/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700488 * Get the attributes of a file
489 */
490int fuse_do_getattr(struct inode *inode);
491
492/**
493 * Invalidate inode attributes
494 */
495void fuse_invalidate_attr(struct inode *inode);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700496
497/**
498 * Acquire reference to fuse_conn
499 */
500struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
501
502/**
503 * Release reference to fuse_conn
504 */
505void fuse_conn_put(struct fuse_conn *fc);
506
507/**
508 * Add connection to control filesystem
509 */
510int fuse_ctl_add_conn(struct fuse_conn *fc);
511
512/**
513 * Remove connection from control filesystem
514 */
515void fuse_ctl_remove_conn(struct fuse_conn *fc);