blob: 242e69cb12517c017975fea7489b9736277172b4 [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>
11#include <linux/wait.h>
12#include <linux/list.h>
13#include <linux/spinlock.h>
14#include <linux/mm.h>
15#include <linux/backing-dev.h>
16#include <asm/semaphore.h>
17
Miklos Szeredi334f4852005-09-09 13:10:27 -070018/** Max number of pages that can be used in a single read request */
19#define FUSE_MAX_PAGES_PER_REQ 32
20
Miklos Szeredi1d3d7522006-01-06 00:19:40 -080021/** It could be as large as PATH_MAX, but would that have any uses? */
22#define FUSE_NAME_MAX 1024
23
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -070024/** If the FUSE_DEFAULT_PERMISSIONS flag is given, the filesystem
25 module will check permissions based on the file mode. Otherwise no
26 permission checking is done in the kernel */
27#define FUSE_DEFAULT_PERMISSIONS (1 << 0)
28
29/** If the FUSE_ALLOW_OTHER flag is given, then not only the user
30 doing the mount will be allowed to access the filesystem */
31#define FUSE_ALLOW_OTHER (1 << 1)
32
Miklos Szeredi413ef8c2005-09-09 13:10:35 -070033
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070034/** FUSE inode */
35struct fuse_inode {
36 /** Inode data */
37 struct inode inode;
38
39 /** Unique ID, which identifies the inode between userspace
40 * and kernel */
41 u64 nodeid;
42
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070043 /** Number of lookups on this inode */
44 u64 nlookup;
45
Miklos Szeredie5e55582005-09-09 13:10:28 -070046 /** The request used for sending the FORGET message */
47 struct fuse_req *forget_req;
48
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070049 /** Time in jiffies until the file attributes are valid */
50 unsigned long i_time;
51};
52
Miklos Szeredib6aeade2005-09-09 13:10:30 -070053/** FUSE specific file data */
54struct fuse_file {
55 /** Request reserved for flush and release */
56 struct fuse_req *release_req;
57
58 /** File handle used by userspace */
59 u64 fh;
60};
61
Miklos Szeredi334f4852005-09-09 13:10:27 -070062/** One input argument of a request */
63struct fuse_in_arg {
64 unsigned size;
65 const void *value;
66};
67
68/** The request input */
69struct fuse_in {
70 /** The request header */
71 struct fuse_in_header h;
72
73 /** True if the data for the last argument is in req->pages */
74 unsigned argpages:1;
75
76 /** Number of arguments */
77 unsigned numargs;
78
79 /** Array of arguments */
80 struct fuse_in_arg args[3];
81};
82
83/** One output argument of a request */
84struct fuse_arg {
85 unsigned size;
86 void *value;
87};
88
89/** The request output */
90struct fuse_out {
91 /** Header returned from userspace */
92 struct fuse_out_header h;
93
Miklos Szeredi095da6c2006-01-16 22:14:52 -080094 /*
95 * The following bitfields are not changed during the request
96 * processing
97 */
98
Miklos Szeredi334f4852005-09-09 13:10:27 -070099 /** Last argument is variable length (can be shorter than
100 arg->size) */
101 unsigned argvar:1;
102
103 /** Last argument is a list of pages to copy data to */
104 unsigned argpages:1;
105
106 /** Zero partially or not copied pages */
107 unsigned page_zeroing:1;
108
109 /** Number or arguments */
110 unsigned numargs;
111
112 /** Array of arguments */
113 struct fuse_arg args[3];
114};
115
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800116/** The request state */
117enum fuse_req_state {
118 FUSE_REQ_INIT = 0,
119 FUSE_REQ_PENDING,
120 FUSE_REQ_READING,
121 FUSE_REQ_SENT,
122 FUSE_REQ_FINISHED
123};
124
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800125struct fuse_conn;
126
Miklos Szeredi334f4852005-09-09 13:10:27 -0700127/**
128 * A request to the client
129 */
130struct fuse_req {
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700131 /** This can be on either pending processing or io lists in
132 fuse_conn */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700133 struct list_head list;
134
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700135 /** Entry on the background list */
136 struct list_head bg_entry;
137
Miklos Szeredi334f4852005-09-09 13:10:27 -0700138 /** refcount */
139 atomic_t count;
140
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800141 /*
142 * The following bitfields are either set once before the
143 * request is queued or setting/clearing them is protected by
Miklos Szeredid7133112006-04-10 22:54:55 -0700144 * fuse_conn->lock
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800145 */
146
Miklos Szeredi334f4852005-09-09 13:10:27 -0700147 /** True if the request has reply */
148 unsigned isreply:1;
149
Miklos Szeredi334f4852005-09-09 13:10:27 -0700150 /** The request was interrupted */
151 unsigned interrupted:1;
152
153 /** Request is sent in the background */
154 unsigned background:1;
155
156 /** Data is being copied to/from the request */
157 unsigned locked:1;
158
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800159 /** State of the request */
160 enum fuse_req_state state;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700161
162 /** The request input */
163 struct fuse_in in;
164
165 /** The request output */
166 struct fuse_out out;
167
168 /** Used to wake up the task waiting for completion of request*/
169 wait_queue_head_t waitq;
170
171 /** Data for asynchronous requests */
172 union {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700173 struct fuse_forget_in forget_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700174 struct fuse_release_in release_in;
Miklos Szeredi3ec870d2006-01-06 00:19:41 -0800175 struct fuse_init_in init_in;
176 struct fuse_init_out init_out;
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800177 struct fuse_read_in read_in;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700178 } misc;
179
180 /** page vector */
181 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
182
183 /** number of pages in vector */
184 unsigned num_pages;
185
186 /** offset of data on first page */
187 unsigned page_offset;
188
189 /** Inode used in the request */
190 struct inode *inode;
191
192 /** Second inode used in the request (or NULL) */
193 struct inode *inode2;
194
195 /** File used in the request (or NULL) */
196 struct file *file;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800197
198 /** Request completion callback */
199 void (*end)(struct fuse_conn *, struct fuse_req *);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700200};
201
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700202/**
203 * A Fuse connection.
204 *
205 * This structure is created, when the filesystem is mounted, and is
206 * destroyed, when the client device is closed and the filesystem is
207 * unmounted.
208 */
209struct fuse_conn {
Miklos Szeredid7133112006-04-10 22:54:55 -0700210 /** Lock protecting accessess to members of this structure */
211 spinlock_t lock;
212
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700213 /** The user id for this mount */
214 uid_t user_id;
215
Miklos Szeredi87729a52005-09-09 13:10:34 -0700216 /** The group id for this mount */
217 gid_t group_id;
218
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700219 /** The fuse mount flags for this mount */
220 unsigned flags;
221
Miklos Szeredidb50b962005-09-09 13:10:33 -0700222 /** Maximum read size */
223 unsigned max_read;
224
Miklos Szeredi413ef8c2005-09-09 13:10:35 -0700225 /** Maximum write size */
226 unsigned max_write;
227
Miklos Szeredi334f4852005-09-09 13:10:27 -0700228 /** Readers of the connection are waiting on this */
229 wait_queue_head_t waitq;
230
231 /** The list of pending requests */
232 struct list_head pending;
233
234 /** The list of requests being processed */
235 struct list_head processing;
236
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800237 /** The list of requests under I/O */
238 struct list_head io;
239
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700240 /** Requests put in the background (RELEASE or any other
241 interrupted request) */
242 struct list_head background;
243
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700244 /** RW semaphore for exclusion with fuse_put_super() */
245 struct rw_semaphore sbput_sem;
246
Miklos Szeredi334f4852005-09-09 13:10:27 -0700247 /** The next unique request id */
248 u64 reqctr;
249
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700250 /** Mount is active */
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800251 unsigned mounted;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700252
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800253 /** Connection established, cleared on umount, connection
254 abort and device release */
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800255 unsigned connected;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700256
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800257 /** Connection failed (version mismatch). Cannot race with
258 setting other bitfields since it is only set once in INIT
259 reply, before any other request, and never cleared */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700260 unsigned conn_error : 1;
261
Miklos Szeredi9cd68452006-02-01 03:04:40 -0800262 /** Do readpages asynchronously? Only set in INIT */
263 unsigned async_read : 1;
264
Miklos Szeredi095da6c2006-01-16 22:14:52 -0800265 /*
266 * The following bitfields are only for optimization purposes
267 * and hence races in setting them will not cause malfunction
268 */
269
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700270 /** Is fsync not implemented by fs? */
271 unsigned no_fsync : 1;
272
Miklos Szeredi82547982005-09-09 13:10:38 -0700273 /** Is fsyncdir not implemented by fs? */
274 unsigned no_fsyncdir : 1;
275
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700276 /** Is flush not implemented by fs? */
277 unsigned no_flush : 1;
278
Miklos Szeredi92a87802005-09-09 13:10:31 -0700279 /** Is setxattr not implemented by fs? */
280 unsigned no_setxattr : 1;
281
282 /** Is getxattr not implemented by fs? */
283 unsigned no_getxattr : 1;
284
285 /** Is listxattr not implemented by fs? */
286 unsigned no_listxattr : 1;
287
288 /** Is removexattr not implemented by fs? */
289 unsigned no_removexattr : 1;
290
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800291 /** Is access not implemented by fs? */
292 unsigned no_access : 1;
293
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800294 /** Is create not implemented by fs? */
295 unsigned no_create : 1;
296
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800297 /** The number of requests waiting for completion */
298 atomic_t num_waiting;
299
Miklos Szeredi45714d62006-01-06 00:19:36 -0800300 /** Negotiated minor version */
301 unsigned minor;
302
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700303 /** Backing dev info */
304 struct backing_dev_info bdi;
Miklos Szeredif543f252006-01-16 22:14:35 -0800305
306 /** kobject */
307 struct kobject kobj;
Jeff Dike385a17b2006-04-10 22:54:52 -0700308
309 /** O_ASYNC requests */
310 struct fasync_struct *fasync;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700311};
312
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700313static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
314{
Miklos Szeredi6383bda2006-01-16 22:14:29 -0800315 return sb->s_fs_info;
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700316}
317
318static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
319{
320 return get_fuse_conn_super(inode->i_sb);
321}
322
Miklos Szeredif543f252006-01-16 22:14:35 -0800323static inline struct fuse_conn *get_fuse_conn_kobj(struct kobject *obj)
324{
325 return container_of(obj, struct fuse_conn, kobj);
326}
327
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700328static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
329{
330 return container_of(inode, struct fuse_inode, inode);
331}
332
333static inline u64 get_node_id(struct inode *inode)
334{
335 return get_fuse_inode(inode)->nodeid;
336}
337
Miklos Szeredi334f4852005-09-09 13:10:27 -0700338/** Device operations */
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800339extern const struct file_operations fuse_dev_operations;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700340
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700341/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700342 * Get a filled in inode
343 */
344struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700345 int generation, struct fuse_attr *attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700346
347/**
348 * Send FORGET command
349 */
350void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700351 unsigned long nodeid, u64 nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700352
353/**
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800354 * Initialize READ or READDIR request
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700355 */
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800356void fuse_read_fill(struct fuse_req *req, struct file *file,
357 struct inode *inode, loff_t pos, size_t count, int opcode);
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700358
359/**
360 * Send OPEN or OPENDIR request
361 */
362int fuse_open_common(struct inode *inode, struct file *file, int isdir);
363
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800364struct fuse_file *fuse_file_alloc(void);
365void fuse_file_free(struct fuse_file *ff);
366void fuse_finish_open(struct inode *inode, struct file *file,
367 struct fuse_file *ff, struct fuse_open_out *outarg);
368
369/**
370 * Send a RELEASE request
371 */
372void fuse_send_release(struct fuse_conn *fc, struct fuse_file *ff,
373 u64 nodeid, struct inode *inode, int flags, int isdir);
374
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700375/**
376 * Send RELEASE or RELEASEDIR request
377 */
378int fuse_release_common(struct inode *inode, struct file *file, int isdir);
379
380/**
Miklos Szeredi82547982005-09-09 13:10:38 -0700381 * Send FSYNC or FSYNCDIR request
382 */
383int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
384 int isdir);
385
386/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800387 * Initialize file operations on a regular file
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700388 */
389void fuse_init_file_inode(struct inode *inode);
390
391/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800392 * Initialize inode operations on regular files and special files
Miklos Szeredie5e55582005-09-09 13:10:28 -0700393 */
394void fuse_init_common(struct inode *inode);
395
396/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800397 * Initialize inode and file operations on a directory
Miklos Szeredie5e55582005-09-09 13:10:28 -0700398 */
399void fuse_init_dir(struct inode *inode);
400
401/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800402 * Initialize inode operations on a symlink
Miklos Szeredie5e55582005-09-09 13:10:28 -0700403 */
404void fuse_init_symlink(struct inode *inode);
405
406/**
407 * Change attributes of an inode
408 */
409void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
410
411/**
Miklos Szeredi334f4852005-09-09 13:10:27 -0700412 * Initialize the client device
413 */
414int fuse_dev_init(void);
415
416/**
417 * Cleanup the client device
418 */
419void fuse_dev_cleanup(void);
420
421/**
422 * Allocate a request
423 */
424struct fuse_req *fuse_request_alloc(void);
425
426/**
427 * Free a request
428 */
429void fuse_request_free(struct fuse_req *req);
430
431/**
432 * Reinitialize a request, the preallocated flag is left unmodified
433 */
434void fuse_reset_request(struct fuse_req *req);
435
436/**
437 * Reserve a preallocated request
438 */
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700439struct fuse_req *fuse_get_req(struct fuse_conn *fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700440
441/**
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700442 * Decrement reference count of a request. If count goes to zero free
443 * the request.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700444 */
445void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
446
447/**
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700448 * Send a request (synchronous)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700449 */
450void request_send(struct fuse_conn *fc, struct fuse_req *req);
451
452/**
Miklos Szeredi334f4852005-09-09 13:10:27 -0700453 * Send a request with no reply
454 */
455void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
456
457/**
458 * Send a request in the background
459 */
460void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
461
462/**
Miklos Szeredi17793812005-10-30 15:02:51 -0800463 * Release inodes and file associated with background request
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700464 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700465void fuse_release_background(struct fuse_conn *fc, struct fuse_req *req);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700466
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800467/* Abort all requests */
468void fuse_abort_conn(struct fuse_conn *fc);
469
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700470/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700471 * Get the attributes of a file
472 */
473int fuse_do_getattr(struct inode *inode);
474
475/**
476 * Invalidate inode attributes
477 */
478void fuse_invalidate_attr(struct inode *inode);