blob: 87d25b8f2dc18ffe062ce350a5083f591d750817 [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 Szeredi334f4852005-09-09 13:10:27 -070043/** One input argument of a request */
44struct fuse_in_arg {
45 unsigned size;
46 const void *value;
47};
48
49/** The request input */
50struct fuse_in {
51 /** The request header */
52 struct fuse_in_header h;
53
54 /** True if the data for the last argument is in req->pages */
55 unsigned argpages:1;
56
57 /** Number of arguments */
58 unsigned numargs;
59
60 /** Array of arguments */
61 struct fuse_in_arg args[3];
62};
63
64/** One output argument of a request */
65struct fuse_arg {
66 unsigned size;
67 void *value;
68};
69
70/** The request output */
71struct fuse_out {
72 /** Header returned from userspace */
73 struct fuse_out_header h;
74
75 /** Last argument is variable length (can be shorter than
76 arg->size) */
77 unsigned argvar:1;
78
79 /** Last argument is a list of pages to copy data to */
80 unsigned argpages:1;
81
82 /** Zero partially or not copied pages */
83 unsigned page_zeroing:1;
84
85 /** Number or arguments */
86 unsigned numargs;
87
88 /** Array of arguments */
89 struct fuse_arg args[3];
90};
91
92struct fuse_req;
93struct fuse_conn;
94
95/**
96 * A request to the client
97 */
98struct fuse_req {
99 /** This can be on either unused_list, pending or processing
100 lists in fuse_conn */
101 struct list_head list;
102
103 /** refcount */
104 atomic_t count;
105
106 /** True if the request has reply */
107 unsigned isreply:1;
108
109 /** The request is preallocated */
110 unsigned preallocated:1;
111
112 /** The request was interrupted */
113 unsigned interrupted:1;
114
115 /** Request is sent in the background */
116 unsigned background:1;
117
118 /** Data is being copied to/from the request */
119 unsigned locked:1;
120
121 /** Request has been sent to userspace */
122 unsigned sent:1;
123
124 /** The request is finished */
125 unsigned finished:1;
126
127 /** The request input */
128 struct fuse_in in;
129
130 /** The request output */
131 struct fuse_out out;
132
133 /** Used to wake up the task waiting for completion of request*/
134 wait_queue_head_t waitq;
135
136 /** Data for asynchronous requests */
137 union {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700138 struct fuse_forget_in forget_in;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700139 struct fuse_init_in_out init_in_out;
140 } misc;
141
142 /** page vector */
143 struct page *pages[FUSE_MAX_PAGES_PER_REQ];
144
145 /** number of pages in vector */
146 unsigned num_pages;
147
148 /** offset of data on first page */
149 unsigned page_offset;
150
151 /** Inode used in the request */
152 struct inode *inode;
153
154 /** Second inode used in the request (or NULL) */
155 struct inode *inode2;
156
157 /** File used in the request (or NULL) */
158 struct file *file;
159};
160
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700161/**
162 * A Fuse connection.
163 *
164 * This structure is created, when the filesystem is mounted, and is
165 * destroyed, when the client device is closed and the filesystem is
166 * unmounted.
167 */
168struct fuse_conn {
169 /** The superblock of the mounted filesystem */
170 struct super_block *sb;
171
Miklos Szeredi334f4852005-09-09 13:10:27 -0700172 /** The opened client device */
173 struct file *file;
174
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700175 /** The user id for this mount */
176 uid_t user_id;
177
Miklos Szeredi334f4852005-09-09 13:10:27 -0700178 /** Readers of the connection are waiting on this */
179 wait_queue_head_t waitq;
180
181 /** The list of pending requests */
182 struct list_head pending;
183
184 /** The list of requests being processed */
185 struct list_head processing;
186
187 /** Controls the maximum number of outstanding requests */
188 struct semaphore outstanding_sem;
189
190 /** This counts the number of outstanding requests if
191 outstanding_sem would go negative */
192 unsigned outstanding_debt;
193
194 /** The list of unused requests */
195 struct list_head unused_list;
196
197 /** The next unique request id */
198 u64 reqctr;
199
200 /** Connection failed (version mismatch) */
201 unsigned conn_error : 1;
202
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700203 /** Backing dev info */
204 struct backing_dev_info bdi;
205};
206
Miklos Szeredie5e55582005-09-09 13:10:28 -0700207struct fuse_getdir_out_i {
208 int fd;
209 void *file; /* Used by kernel only */
210};
211
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700212static inline struct fuse_conn **get_fuse_conn_super_p(struct super_block *sb)
213{
214 return (struct fuse_conn **) &sb->s_fs_info;
215}
216
217static inline struct fuse_conn *get_fuse_conn_super(struct super_block *sb)
218{
219 return *get_fuse_conn_super_p(sb);
220}
221
222static inline struct fuse_conn *get_fuse_conn(struct inode *inode)
223{
224 return get_fuse_conn_super(inode->i_sb);
225}
226
227static inline struct fuse_inode *get_fuse_inode(struct inode *inode)
228{
229 return container_of(inode, struct fuse_inode, inode);
230}
231
232static inline u64 get_node_id(struct inode *inode)
233{
234 return get_fuse_inode(inode)->nodeid;
235}
236
Miklos Szeredi334f4852005-09-09 13:10:27 -0700237/** Device operations */
238extern struct file_operations fuse_dev_operations;
239
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700240/**
241 * This is the single global spinlock which protects FUSE's structures
242 *
243 * The following data is protected by this lock:
244 *
Miklos Szeredi334f4852005-09-09 13:10:27 -0700245 * - the private_data field of the device file
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700246 * - the s_fs_info field of the super block
Miklos Szeredi334f4852005-09-09 13:10:27 -0700247 * - unused_list, pending, processing lists in fuse_conn
248 * - the unique request ID counter reqctr in fuse_conn
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700249 * - the sb (super_block) field in fuse_conn
Miklos Szeredi334f4852005-09-09 13:10:27 -0700250 * - the file (device file) field in fuse_conn
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700251 */
252extern spinlock_t fuse_lock;
253
254/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700255 * Get a filled in inode
256 */
257struct inode *fuse_iget(struct super_block *sb, unsigned long nodeid,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700258 int generation, struct fuse_attr *attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700259
260/**
261 * Send FORGET command
262 */
263void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700264 unsigned long nodeid, u64 nlookup);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700265
266/**
267 * Initialise inode operations on regular files and special files
268 */
269void fuse_init_common(struct inode *inode);
270
271/**
272 * Initialise inode and file operations on a directory
273 */
274void fuse_init_dir(struct inode *inode);
275
276/**
277 * Initialise inode operations on a symlink
278 */
279void fuse_init_symlink(struct inode *inode);
280
281/**
282 * Change attributes of an inode
283 */
284void fuse_change_attributes(struct inode *inode, struct fuse_attr *attr);
285
286/**
Miklos Szeredid8a5ba42005-09-09 13:10:26 -0700287 * Check if the connection can be released, and if yes, then free the
288 * connection structure
289 */
290void fuse_release_conn(struct fuse_conn *fc);
291
Miklos Szeredi334f4852005-09-09 13:10:27 -0700292/**
293 * Initialize the client device
294 */
295int fuse_dev_init(void);
296
297/**
298 * Cleanup the client device
299 */
300void fuse_dev_cleanup(void);
301
302/**
303 * Allocate a request
304 */
305struct fuse_req *fuse_request_alloc(void);
306
307/**
308 * Free a request
309 */
310void fuse_request_free(struct fuse_req *req);
311
312/**
313 * Reinitialize a request, the preallocated flag is left unmodified
314 */
315void fuse_reset_request(struct fuse_req *req);
316
317/**
318 * Reserve a preallocated request
319 */
320struct fuse_req *fuse_get_request(struct fuse_conn *fc);
321
322/**
323 * Reserve a preallocated request, only interruptible by SIGKILL
324 */
325struct fuse_req *fuse_get_request_nonint(struct fuse_conn *fc);
326
327/**
328 * Decrement reference count of a request. If count goes to zero put
329 * on unused list (preallocated) or free reqest (not preallocated).
330 */
331void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
332
333/**
334 * Send a request (synchronous, interruptible)
335 */
336void request_send(struct fuse_conn *fc, struct fuse_req *req);
337
338/**
339 * Send a request (synchronous, non-interruptible except by SIGKILL)
340 */
341void request_send_nonint(struct fuse_conn *fc, struct fuse_req *req);
342
343/**
344 * Send a request with no reply
345 */
346void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req);
347
348/**
349 * Send a request in the background
350 */
351void request_send_background(struct fuse_conn *fc, struct fuse_req *req);
352
353/**
Miklos Szeredie5e55582005-09-09 13:10:28 -0700354 * Get the attributes of a file
355 */
356int fuse_do_getattr(struct inode *inode);
357
358/**
359 * Invalidate inode attributes
360 */
361void fuse_invalidate_attr(struct inode *inode);
362
363/**
Miklos Szeredi334f4852005-09-09 13:10:27 -0700364 * Send the INIT message
365 */
366void fuse_send_init(struct fuse_conn *fc);