blob: b4aa8f7bc2c106d521875e4988b91ffa13201de6 [file] [log] [blame]
Miklos Szeredid8a5ba42005-09-09 13:10:26 -07001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
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
21/** If more requests are outstanding, then the operation will block */
22#define FUSE_MAX_OUTSTANDING 10
23
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070024/** FUSE inode */
25struct fuse_inode {
26 /** Inode data */
27 struct inode inode;
28
29 /** Unique ID, which identifies the inode between userspace
30 * and kernel */
31 u64 nodeid;
32
Miklos Szeredi9e6268d2005-09-09 13:10:29 -070033 /** Number of lookups on this inode */
34 u64 nlookup;
35
Miklos Szeredie5e55582005-09-09 13:10:28 -070036 /** The request used for sending the FORGET message */
37 struct fuse_req *forget_req;
38
Miklos Szeredid8a5ba42005-09-09 13:10:26 -070039 /** Time in jiffies until the file attributes are valid */
40 unsigned long i_time;
41};
42
Miklos Szeredib6aeade2005-09-09 13:10:30 -070043/** FUSE specific file data */
44struct fuse_file {
45 /** Request reserved for flush and release */
46 struct fuse_req *release_req;
47
48 /** File handle used by userspace */
49 u64 fh;
50};
51
Miklos Szeredi334f4852005-09-09 13:10:27 -070052/** One input argument of a request */
53struct fuse_in_arg {
54 unsigned size;
55 const void *value;
56};
57
58/** The request input */
59struct fuse_in {
60 /** The request header */
61 struct fuse_in_header h;
62
63 /** True if the data for the last argument is in req->pages */
64 unsigned argpages:1;
65
66 /** Number of arguments */
67 unsigned numargs;
68
69 /** Array of arguments */
70 struct fuse_in_arg args[3];
71};
72
73/** One output argument of a request */
74struct fuse_arg {
75 unsigned size;
76 void *value;
77};
78
79/** The request output */
80struct fuse_out {
81 /** Header returned from userspace */
82 struct fuse_out_header h;
83
84 /** Last argument is variable length (can be shorter than
85 arg->size) */
86 unsigned argvar:1;
87
88 /** Last argument is a list of pages to copy data to */
89 unsigned argpages:1;
90
91 /** Zero partially or not copied pages */
92 unsigned page_zeroing:1;
93
94 /** Number or arguments */
95 unsigned numargs;
96
97 /** Array of arguments */
98 struct fuse_arg args[3];
99};
100
101struct fuse_req;
102struct fuse_conn;
103
104/**
105 * A request to the client
106 */
107struct fuse_req {
108 /** This can be on either unused_list, pending or processing
109 lists in fuse_conn */
110 struct list_head list;
111
112 /** refcount */
113 atomic_t count;
114
115 /** True if the request has reply */
116 unsigned isreply:1;
117
118 /** The request is preallocated */
119 unsigned preallocated:1;
120
121 /** The request was interrupted */
122 unsigned interrupted:1;
123
124 /** Request is sent in the background */
125 unsigned background:1;
126
127 /** Data is being copied to/from the request */
128 unsigned locked:1;
129
130 /** Request has been sent to userspace */
131 unsigned sent:1;
132
133 /** The request is finished */
134 unsigned finished:1;
135
136 /** The request input */
137 struct fuse_in in;
138
139 /** The request output */
140 struct fuse_out out;
141
142 /** Used to wake up the task waiting for completion of request*/
143 wait_queue_head_t waitq;
144
145 /** Data for asynchronous requests */
146 union {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700147 struct fuse_forget_in forget_in;
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700148 struct fuse_release_in release_in;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700149 struct fuse_init_in_out init_in_out;
150 } misc;
151
152 /** page vector */
153 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
154
155 /** number of pages in vector */
156 unsigned num_pages;
157
158 /** offset of data on first page */
159 unsigned page_offset;
160
161 /** Inode used in the request */
162 struct inode *inode;
163
164 /** Second inode used in the request (or NULL) */
165 struct inode *inode2;
166
167 /** File used in the request (or NULL) */
168 struct file *file;
169};
170
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700171/**
172 * A Fuse connection.
173 *
174 * This structure is created, when the filesystem is mounted, and is
175 * destroyed, when the client device is closed and the filesystem is
176 * unmounted.
177 */
178struct fuse_conn {
179 /** The superblock of the mounted filesystem */
180 struct super_block *sb;
181
Miklos Szeredi334f4852005-09-09 13:10:27 -0700182 /** The opened client device */
183 struct file *file;
184
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700185 /** The user id for this mount */
186 uid_t user_id;
187
Miklos Szeredi334f4852005-09-09 13:10:27 -0700188 /** Readers of the connection are waiting on this */
189 wait_queue_head_t waitq;
190
191 /** The list of pending requests */
192 struct list_head pending;
193
194 /** The list of requests being processed */
195 struct list_head processing;
196
197 /** Controls the maximum number of outstanding requests */
198 struct semaphore outstanding_sem;
199
200 /** This counts the number of outstanding requests if
201 outstanding_sem would go negative */
202 unsigned outstanding_debt;
203
204 /** The list of unused requests */
205 struct list_head unused_list;
206
207 /** The next unique request id */
208 u64 reqctr;
209
210 /** Connection failed (version mismatch) */
211 unsigned conn_error : 1;
212
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700213 /** Is fsync not implemented by fs? */
214 unsigned no_fsync : 1;
215
216 /** Is flush not implemented by fs? */
217 unsigned no_flush : 1;
218
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700219 /** Backing dev info */
220 struct backing_dev_info bdi;
221};
222
Miklos Szeredie5e55582005-09-09 13:10:28 -0700223struct fuse_getdir_out_i {
224 int fd;
225 void *file; /* Used by kernel only */
226};
227
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700228static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb)
229{
230 return (struct fuse_conn **) &sb->s_fs_info;
231}
232
233static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
234{
235 return *get_fuse_conn_super_p(sb);
236}
237
238static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
239{
240 return get_fuse_conn_super(inode->i_sb);
241}
242
243static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
244{
245 return container_of(inode, struct fuse_inode, inode);
246}
247
248static inline u64 get_node_id(struct inode *inode)
249{
250 return get_fuse_inode(inode)->nodeid;
251}
252
Miklos Szeredi334f4852005-09-09 13:10:27 -0700253/** Device operations */
254extern struct file_operations fuse_dev_operations;
255
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700256/**
257 * This is the single global spinlock which protects FUSE's structures
258 *
259 * The following data is protected by this lock:
260 *
Miklos Szeredi334f4852005-09-09 13:10:27 -0700261 * - the private_data field of the device file
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700262 * - the s_fs_info field of the super block
Miklos Szeredi334f4852005-09-09 13:10:27 -0700263 * - unused_list, pending, processing lists in fuse_conn
264 * - the unique request ID counter reqctr in fuse_conn
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700265 * - the sb (super_block) field in fuse_conn
Miklos Szeredi334f4852005-09-09 13:10:27 -0700266 * - the file (device file) field in fuse_conn
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700267 */
268extern spinlock_t fuse_lock;
269
270/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700271 * Get a filled in inode
272 */
273struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700274 int generation, struct fuse_attr *attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700275
276/**
277 * Send FORGET command
278 */
279void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700280 unsigned long nodeid, u64 nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700281
282/**
Miklos Szeredib6aeade2005-09-09 13:10:30 -0700283 * Initialise file operations on a regular file
284 */
285void fuse_init_file_inode(struct inode *inode);
286
287/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700288 * Initialise inode operations on regular files and special files
289 */
290void fuse_init_common(struct inode *inode);
291
292/**
293 * Initialise inode and file operations on a directory
294 */
295void fuse_init_dir(struct inode *inode);
296
297/**
298 * Initialise inode operations on a symlink
299 */
300void fuse_init_symlink(struct inode *inode);
301
302/**
303 * Change attributes of an inode
304 */
305void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
306
307/**
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700308 * Check if the connection can be released, and if yes, then free the
309 * connection structure
310 */
311void fuse_release_conn(struct fuse_conn *fc);
312
Miklos Szeredi334f4852005-09-09 13:10:27 -0700313/**
314 * Initialize the client device
315 */
316int fuse_dev_init(void);
317
318/**
319 * Cleanup the client device
320 */
321void fuse_dev_cleanup(void);
322
323/**
324 * Allocate a request
325 */
326struct fuse_req *fuse_request_alloc(void);
327
328/**
329 * Free a request
330 */
331void fuse_request_free(struct fuse_req *req);
332
333/**
334 * Reinitialize a request, the preallocated flag is left unmodified
335 */
336void fuse_reset_request(struct fuse_req *req);
337
338/**
339 * Reserve a preallocated request
340 */
341struct fuse_req *fuse_get_request(struct fuse_conn *fc);
342
343/**
344 * Reserve a preallocated request, only interruptible by SIGKILL
345 */
346struct fuse_req *fuse_get_request_nonint(struct fuse_conn *fc);
347
348/**
349 * Decrement reference count of a request. If count goes to zero put
350 * on unused list (preallocated) or free reqest (not preallocated).
351 */
352void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
353
354/**
355 * Send a request (synchronous, interruptible)
356 */
357void request_send(struct fuse_conn *fc, struct fuse_req *req);
358
359/**
360 * Send a request (synchronous, non-interruptible except by SIGKILL)
361 */
362void request_send_nonint(struct fuse_conn *fc, struct fuse_req *req);
363
364/**
365 * Send a request with no reply
366 */
367void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
368
369/**
370 * Send a request in the background
371 */
372void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
373
374/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700375 * Get the attributes of a file
376 */
377int fuse_do_getattr(struct inode *inode);
378
379/**
380 * Invalidate inode attributes
381 */
382void fuse_invalidate_attr(struct inode *inode);
383
384/**
Miklos Szeredi334f4852005-09-09 13:10:27 -0700385 * Send the INIT message
386 */
387void fuse_send_init(struct fuse_conn *fc);