blob: c78f60e1ba5d667642cb35cf13ce84baff7ed373 [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredid7133112006-04-10 22:54:55 -07003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredi334f4852005-09-09 13:10:27 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/poll.h>
14#include <linux/uio.h>
15#include <linux/miscdevice.h>
16#include <linux/pagemap.h>
17#include <linux/file.h>
18#include <linux/slab.h>
19
20MODULE_ALIAS_MISCDEV(FUSE_MINOR);
21
Christoph Lametere18b8902006-12-06 20:33:20 -080022static struct kmem_cache *fuse_req_cachep;
Miklos Szeredi334f4852005-09-09 13:10:27 -070023
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080024static struct fuse_conn *fuse_get_conn(struct file *file)
Miklos Szeredi334f4852005-09-09 13:10:27 -070025{
Miklos Szeredi0720b312006-04-10 22:54:55 -070026 /*
27 * Lockless access is OK, because file->private data is set
28 * once during mount and is valid until the file is released.
29 */
30 return file->private_data;
Miklos Szeredi334f4852005-09-09 13:10:27 -070031}
32
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080033static void fuse_request_init(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070034{
35 memset(req, 0, sizeof(*req));
36 INIT_LIST_HEAD(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -070037 INIT_LIST_HEAD(&req->intr_entry);
Miklos Szeredi334f4852005-09-09 13:10:27 -070038 init_waitqueue_head(&req->waitq);
39 atomic_set(&req->count, 1);
40}
41
42struct fuse_req *fuse_request_alloc(void)
43{
Christoph Lametere94b1762006-12-06 20:33:17 -080044 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
Miklos Szeredi334f4852005-09-09 13:10:27 -070045 if (req)
46 fuse_request_init(req);
47 return req;
48}
49
50void fuse_request_free(struct fuse_req *req)
51{
52 kmem_cache_free(fuse_req_cachep, req);
53}
54
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080055static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070056{
57 sigset_t mask;
58
59 siginitsetinv(&mask, sigmask(SIGKILL));
60 sigprocmask(SIG_BLOCK, &mask, oldset);
61}
62
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080063static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070064{
65 sigprocmask(SIG_SETMASK, oldset, NULL);
66}
67
Miklos Szeredi334f4852005-09-09 13:10:27 -070068static void __fuse_get_request(struct fuse_req *req)
69{
70 atomic_inc(&req->count);
71}
72
73/* Must be called with > 1 refcount */
74static void __fuse_put_request(struct fuse_req *req)
75{
76 BUG_ON(atomic_read(&req->count) < 2);
77 atomic_dec(&req->count);
78}
79
Miklos Szeredi33649c92006-06-25 05:48:52 -070080static void fuse_req_init_context(struct fuse_req *req)
81{
82 req->in.h.uid = current->fsuid;
83 req->in.h.gid = current->fsgid;
84 req->in.h.pid = current->pid;
85}
86
Miklos Szeredice1d5a42006-04-10 22:54:58 -070087struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -070088{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070089 struct fuse_req *req;
90 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020091 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070092 int err;
93
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020094 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070095 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020096 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070097 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020098 err = -EINTR;
99 if (intr)
100 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700101
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700102 err = -ENOTCONN;
103 if (!fc->connected)
104 goto out;
105
Miklos Szeredi08a53cd2006-04-10 22:54:59 -0700106 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200107 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700108 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200109 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700110
Miklos Szeredi33649c92006-06-25 05:48:52 -0700111 fuse_req_init_context(req);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200112 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700113 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200114
115 out:
116 atomic_dec(&fc->num_waiting);
117 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700118}
119
Miklos Szeredi33649c92006-06-25 05:48:52 -0700120/*
121 * Return request in fuse_file->reserved_req. However that may
122 * currently be in use. If that is the case, wait for it to become
123 * available.
124 */
125static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
126 struct file *file)
127{
128 struct fuse_req *req = NULL;
129 struct fuse_file *ff = file->private_data;
130
131 do {
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700132 wait_event(fc->reserved_req_waitq, ff->reserved_req);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700133 spin_lock(&fc->lock);
134 if (ff->reserved_req) {
135 req = ff->reserved_req;
136 ff->reserved_req = NULL;
137 get_file(file);
138 req->stolen_file = file;
139 }
140 spin_unlock(&fc->lock);
141 } while (!req);
142
143 return req;
144}
145
146/*
147 * Put stolen request back into fuse_file->reserved_req
148 */
149static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
150{
151 struct file *file = req->stolen_file;
152 struct fuse_file *ff = file->private_data;
153
154 spin_lock(&fc->lock);
155 fuse_request_init(req);
156 BUG_ON(ff->reserved_req);
157 ff->reserved_req = req;
Miklos Szeredide5e3de2007-10-16 23:31:00 -0700158 wake_up_all(&fc->reserved_req_waitq);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700159 spin_unlock(&fc->lock);
160 fput(file);
161}
162
163/*
164 * Gets a requests for a file operation, always succeeds
165 *
166 * This is used for sending the FLUSH request, which must get to
167 * userspace, due to POSIX locks which may need to be unlocked.
168 *
169 * If allocation fails due to OOM, use the reserved request in
170 * fuse_file.
171 *
172 * This is very unlikely to deadlock accidentally, since the
173 * filesystem should not have it's own file open. If deadlock is
174 * intentional, it can still be broken by "aborting" the filesystem.
175 */
176struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
177{
178 struct fuse_req *req;
179
180 atomic_inc(&fc->num_waiting);
181 wait_event(fc->blocked_waitq, !fc->blocked);
182 req = fuse_request_alloc();
183 if (!req)
184 req = get_reserved_req(fc, file);
185
186 fuse_req_init_context(req);
187 req->waiting = 1;
188 return req;
189}
190
Miklos Szeredi334f4852005-09-09 13:10:27 -0700191void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
192{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800193 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200194 if (req->waiting)
195 atomic_dec(&fc->num_waiting);
Miklos Szeredi33649c92006-06-25 05:48:52 -0700196
197 if (req->stolen_file)
198 put_reserved_req(fc, req);
199 else
200 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800201 }
202}
203
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200204/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700205 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700206 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800207 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700208 * was closed. The requester thread is woken up (if still waiting),
209 * the 'end' callback is called if given, else the reference to the
210 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800211 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700212 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700213 */
214static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Josh Triplett105f4d72006-09-29 01:59:25 -0700215 __releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700216{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700217 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
218 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800219 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700220 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800221 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700222 if (req->background) {
223 if (fc->num_background == FUSE_MAX_BACKGROUND) {
224 fc->blocked = 0;
225 wake_up_all(&fc->blocked_waitq);
226 }
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700227 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
228 clear_bdi_congested(&fc->bdi, READ);
229 clear_bdi_congested(&fc->bdi, WRITE);
230 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700231 fc->num_background--;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700232 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700233 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700234 wake_up(&req->waitq);
235 if (end)
236 end(fc, req);
237 else
238 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700239}
240
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700241static void wait_answer_interruptible(struct fuse_conn *fc,
242 struct fuse_req *req)
243{
244 if (signal_pending(current))
245 return;
246
247 spin_unlock(&fc->lock);
248 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
249 spin_lock(&fc->lock);
250}
251
252static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
253{
254 list_add_tail(&req->intr_entry, &fc->interrupts);
255 wake_up(&fc->waitq);
256 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
257}
258
Miklos Szeredid7133112006-04-10 22:54:55 -0700259/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700260static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700261{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700262 if (!fc->no_interrupt) {
263 /* Any signal may interrupt this */
264 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700265
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700266 if (req->aborted)
267 goto aborted;
268 if (req->state == FUSE_REQ_FINISHED)
269 return;
270
271 req->interrupted = 1;
272 if (req->state == FUSE_REQ_SENT)
273 queue_interrupt(fc, req);
274 }
275
276 if (req->force) {
277 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700278 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700279 spin_lock(&fc->lock);
280 } else {
281 sigset_t oldset;
282
283 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700284 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700285 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700286 restore_sigs(&oldset);
287 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700288
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700289 if (req->aborted)
290 goto aborted;
291 if (req->state == FUSE_REQ_FINISHED)
292 return;
293
294 req->out.h.error = -EINTR;
295 req->aborted = 1;
296
297 aborted:
Miklos Szeredi334f4852005-09-09 13:10:27 -0700298 if (req->locked) {
299 /* This is uninterruptible sleep, because data is
300 being copied to/from the buffers of req. During
301 locked state, there mustn't be any filesystem
302 operation (e.g. page fault), since that could lead
303 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700304 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700305 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700306 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700307 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800308 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700309 list_del(&req->list);
310 __fuse_put_request(req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700311 } else if (req->state == FUSE_REQ_SENT) {
312 spin_unlock(&fc->lock);
313 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
314 spin_lock(&fc->lock);
315 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700316}
317
318static unsigned len_args(unsigned numargs, struct fuse_arg *args)
319{
320 unsigned nbytes = 0;
321 unsigned i;
322
323 for (i = 0; i < numargs; i++)
324 nbytes += args[i].size;
325
326 return nbytes;
327}
328
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700329static u64 fuse_get_unique(struct fuse_conn *fc)
330 {
331 fc->reqctr++;
332 /* zero is special */
333 if (fc->reqctr == 0)
334 fc->reqctr = 1;
335
336 return fc->reqctr;
337}
338
Miklos Szeredi334f4852005-09-09 13:10:27 -0700339static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
340{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700341 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700342 req->in.h.len = sizeof(struct fuse_in_header) +
343 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700344 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800345 req->state = FUSE_REQ_PENDING;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200346 if (!req->waiting) {
347 req->waiting = 1;
348 atomic_inc(&fc->num_waiting);
349 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700350 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700351 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700352}
353
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700354void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700355{
356 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700357 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700358 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700359 req->out.h.error = -ENOTCONN;
360 else if (fc->conn_error)
361 req->out.h.error = -ECONNREFUSED;
362 else {
363 queue_request(fc, req);
364 /* acquire extra reference, since request is still needed
365 after request_end() */
366 __fuse_get_request(req);
367
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700368 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700369 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700370 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700371}
372
Miklos Szeredi334f4852005-09-09 13:10:27 -0700373static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
374{
Miklos Szeredid7133112006-04-10 22:54:55 -0700375 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700376 if (fc->connected) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700377 req->background = 1;
378 fc->num_background++;
379 if (fc->num_background == FUSE_MAX_BACKGROUND)
380 fc->blocked = 1;
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700381 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
382 set_bdi_congested(&fc->bdi, READ);
383 set_bdi_congested(&fc->bdi, WRITE);
384 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700385
Miklos Szeredi334f4852005-09-09 13:10:27 -0700386 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700387 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700388 } else {
389 req->out.h.error = -ENOTCONN;
390 request_end(fc, req);
391 }
392}
393
394void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
395{
396 req->isreply = 0;
397 request_send_nowait(fc, req);
398}
399
400void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
401{
402 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700403 request_send_nowait(fc, req);
404}
405
Miklos Szeredi334f4852005-09-09 13:10:27 -0700406/*
407 * Lock the request. Up to the next unlock_request() there mustn't be
408 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700409 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700410 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700411static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700412{
413 int err = 0;
414 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700415 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700416 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417 err = -ENOENT;
418 else
419 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700420 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700421 }
422 return err;
423}
424
425/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700426 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700427 * requester thread is currently waiting for it to be unlocked, so
428 * wake it up.
429 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700430static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700431{
432 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700433 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700434 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700435 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700436 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700437 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700438 }
439}
440
441struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700442 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443 int write;
444 struct fuse_req *req;
445 const struct iovec *iov;
446 unsigned long nr_segs;
447 unsigned long seglen;
448 unsigned long addr;
449 struct page *pg;
450 void *mapaddr;
451 void *buf;
452 unsigned len;
453};
454
Miklos Szeredid7133112006-04-10 22:54:55 -0700455static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
456 int write, struct fuse_req *req,
457 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700458{
459 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700460 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700461 cs->write = write;
462 cs->req = req;
463 cs->iov = iov;
464 cs->nr_segs = nr_segs;
465}
466
467/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800468static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700469{
470 if (cs->mapaddr) {
471 kunmap_atomic(cs->mapaddr, KM_USER0);
472 if (cs->write) {
473 flush_dcache_page(cs->pg);
474 set_page_dirty_lock(cs->pg);
475 }
476 put_page(cs->pg);
477 cs->mapaddr = NULL;
478 }
479}
480
481/*
482 * Get another pagefull of userspace buffer, and map it to kernel
483 * address space, and lock request
484 */
485static int fuse_copy_fill(struct fuse_copy_state *cs)
486{
487 unsigned long offset;
488 int err;
489
Miklos Szeredid7133112006-04-10 22:54:55 -0700490 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700491 fuse_copy_finish(cs);
492 if (!cs->seglen) {
493 BUG_ON(!cs->nr_segs);
494 cs->seglen = cs->iov[0].iov_len;
495 cs->addr = (unsigned long) cs->iov[0].iov_base;
496 cs->iov ++;
497 cs->nr_segs --;
498 }
499 down_read(&current->mm->mmap_sem);
500 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
501 &cs->pg, NULL);
502 up_read(&current->mm->mmap_sem);
503 if (err < 0)
504 return err;
505 BUG_ON(err != 1);
506 offset = cs->addr % PAGE_SIZE;
507 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
508 cs->buf = cs->mapaddr + offset;
509 cs->len = min(PAGE_SIZE - offset, cs->seglen);
510 cs->seglen -= cs->len;
511 cs->addr += cs->len;
512
Miklos Szeredid7133112006-04-10 22:54:55 -0700513 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700514}
515
516/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800517static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700518{
519 unsigned ncpy = min(*size, cs->len);
520 if (val) {
521 if (cs->write)
522 memcpy(cs->buf, *val, ncpy);
523 else
524 memcpy(*val, cs->buf, ncpy);
525 *val += ncpy;
526 }
527 *size -= ncpy;
528 cs->len -= ncpy;
529 cs->buf += ncpy;
530 return ncpy;
531}
532
533/*
534 * Copy a page in the request to/from the userspace buffer. Must be
535 * done atomically
536 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800537static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
538 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700539{
540 if (page && zeroing && count < PAGE_SIZE) {
541 void *mapaddr = kmap_atomic(page, KM_USER1);
542 memset(mapaddr, 0, PAGE_SIZE);
543 kunmap_atomic(mapaddr, KM_USER1);
544 }
545 while (count) {
546 int err;
547 if (!cs->len && (err = fuse_copy_fill(cs)))
548 return err;
549 if (page) {
550 void *mapaddr = kmap_atomic(page, KM_USER1);
551 void *buf = mapaddr + offset;
552 offset += fuse_copy_do(cs, &buf, &count);
553 kunmap_atomic(mapaddr, KM_USER1);
554 } else
555 offset += fuse_copy_do(cs, NULL, &count);
556 }
557 if (page && !cs->write)
558 flush_dcache_page(page);
559 return 0;
560}
561
562/* Copy pages in the request to/from userspace buffer */
563static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
564 int zeroing)
565{
566 unsigned i;
567 struct fuse_req *req = cs->req;
568 unsigned offset = req->page_offset;
569 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
570
571 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
572 struct page *page = req->pages[i];
573 int err = fuse_copy_page(cs, page, offset, count, zeroing);
574 if (err)
575 return err;
576
577 nbytes -= count;
578 count = min(nbytes, (unsigned) PAGE_SIZE);
579 offset = 0;
580 }
581 return 0;
582}
583
584/* Copy a single argument in the request to/from userspace buffer */
585static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
586{
587 while (size) {
588 int err;
589 if (!cs->len && (err = fuse_copy_fill(cs)))
590 return err;
591 fuse_copy_do(cs, &val, &size);
592 }
593 return 0;
594}
595
596/* Copy request arguments to/from userspace buffer */
597static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
598 unsigned argpages, struct fuse_arg *args,
599 int zeroing)
600{
601 int err = 0;
602 unsigned i;
603
604 for (i = 0; !err && i < numargs; i++) {
605 struct fuse_arg *arg = &args[i];
606 if (i == numargs - 1 && argpages)
607 err = fuse_copy_pages(cs, arg->size, zeroing);
608 else
609 err = fuse_copy_one(cs, arg->value, arg->size);
610 }
611 return err;
612}
613
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700614static int request_pending(struct fuse_conn *fc)
615{
616 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
617}
618
Miklos Szeredi334f4852005-09-09 13:10:27 -0700619/* Wait until a request is available on the pending list */
620static void request_wait(struct fuse_conn *fc)
621{
622 DECLARE_WAITQUEUE(wait, current);
623
624 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700625 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700626 set_current_state(TASK_INTERRUPTIBLE);
627 if (signal_pending(current))
628 break;
629
Miklos Szeredid7133112006-04-10 22:54:55 -0700630 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700631 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700632 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700633 }
634 set_current_state(TASK_RUNNING);
635 remove_wait_queue(&fc->waitq, &wait);
636}
637
638/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700639 * Transfer an interrupt request to userspace
640 *
641 * Unlike other requests this is assembled on demand, without a need
642 * to allocate a separate fuse_req structure.
643 *
644 * Called with fc->lock held, releases it
645 */
646static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
647 const struct iovec *iov, unsigned long nr_segs)
Josh Triplett105f4d72006-09-29 01:59:25 -0700648 __releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700649{
650 struct fuse_copy_state cs;
651 struct fuse_in_header ih;
652 struct fuse_interrupt_in arg;
653 unsigned reqsize = sizeof(ih) + sizeof(arg);
654 int err;
655
656 list_del_init(&req->intr_entry);
657 req->intr_unique = fuse_get_unique(fc);
658 memset(&ih, 0, sizeof(ih));
659 memset(&arg, 0, sizeof(arg));
660 ih.len = reqsize;
661 ih.opcode = FUSE_INTERRUPT;
662 ih.unique = req->intr_unique;
663 arg.unique = req->in.h.unique;
664
665 spin_unlock(&fc->lock);
666 if (iov_length(iov, nr_segs) < reqsize)
667 return -EINVAL;
668
669 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
670 err = fuse_copy_one(&cs, &ih, sizeof(ih));
671 if (!err)
672 err = fuse_copy_one(&cs, &arg, sizeof(arg));
673 fuse_copy_finish(&cs);
674
675 return err ? err : reqsize;
676}
677
678/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 * Read a single request into the userspace filesystem's buffer. This
680 * function waits until a request is available, then removes it from
681 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700682 * no reply is needed (FORGET) or request has been aborted or there
683 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700684 * request_end(). Otherwise add it to the processing list, and set
685 * the 'sent' flag.
686 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700687static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
688 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689{
690 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700691 struct fuse_req *req;
692 struct fuse_in *in;
693 struct fuse_copy_state cs;
694 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700695 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700696 struct fuse_conn *fc = fuse_get_conn(file);
697 if (!fc)
698 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700699
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800700 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700701 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700702 err = -EAGAIN;
703 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700704 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700705 goto err_unlock;
706
Miklos Szeredi334f4852005-09-09 13:10:27 -0700707 request_wait(fc);
708 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800709 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710 goto err_unlock;
711 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700712 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700713 goto err_unlock;
714
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700715 if (!list_empty(&fc->interrupts)) {
716 req = list_entry(fc->interrupts.next, struct fuse_req,
717 intr_entry);
718 return fuse_read_interrupt(fc, req, iov, nr_segs);
719 }
720
Miklos Szeredi334f4852005-09-09 13:10:27 -0700721 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800722 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800723 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700724
725 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800726 reqsize = in->h.len;
727 /* If request is too large, reply with an error and restart the read */
728 if (iov_length(iov, nr_segs) < reqsize) {
729 req->out.h.error = -EIO;
730 /* SETXATTR is special, since it may contain too large data */
731 if (in->h.opcode == FUSE_SETXATTR)
732 req->out.h.error = -E2BIG;
733 request_end(fc, req);
734 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700735 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700736 spin_unlock(&fc->lock);
737 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800738 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
739 if (!err)
740 err = fuse_copy_args(&cs, in->numargs, in->argpages,
741 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700742 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700743 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700745 if (!err && req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700746 err = -ENOENT;
747 if (err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700748 if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 req->out.h.error = -EIO;
750 request_end(fc, req);
751 return err;
752 }
753 if (!req->isreply)
754 request_end(fc, req);
755 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800756 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800757 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700758 if (req->interrupted)
759 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700760 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700761 }
762 return reqsize;
763
764 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700765 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700766 return err;
767}
768
Miklos Szeredi334f4852005-09-09 13:10:27 -0700769/* Look up request on processing list by unique ID */
770static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
771{
772 struct list_head *entry;
773
774 list_for_each(entry, &fc->processing) {
775 struct fuse_req *req;
776 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700777 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700778 return req;
779 }
780 return NULL;
781}
782
783static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
784 unsigned nbytes)
785{
786 unsigned reqsize = sizeof(struct fuse_out_header);
787
788 if (out->h.error)
789 return nbytes != reqsize ? -EINVAL : 0;
790
791 reqsize += len_args(out->numargs, out->args);
792
793 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
794 return -EINVAL;
795 else if (reqsize > nbytes) {
796 struct fuse_arg *lastarg = &out->args[out->numargs-1];
797 unsigned diffsize = reqsize - nbytes;
798 if (diffsize > lastarg->size)
799 return -EINVAL;
800 lastarg->size -= diffsize;
801 }
802 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
803 out->page_zeroing);
804}
805
806/*
807 * Write a single reply to a request. First the header is copied from
808 * the write buffer. The request is then searched on the processing
809 * list by the unique ID found in the header. If found, then remove
810 * it from the list and copy the rest of the buffer to the request.
811 * The request is finished by calling request_end()
812 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700813static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
814 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700815{
816 int err;
817 unsigned nbytes = iov_length(iov, nr_segs);
818 struct fuse_req *req;
819 struct fuse_out_header oh;
820 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700821 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700822 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700823 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700824
Miklos Szeredid7133112006-04-10 22:54:55 -0700825 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700826 if (nbytes < sizeof(struct fuse_out_header))
827 return -EINVAL;
828
829 err = fuse_copy_one(&cs, &oh, sizeof(oh));
830 if (err)
831 goto err_finish;
832 err = -EINVAL;
833 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
834 oh.len != nbytes)
835 goto err_finish;
836
Miklos Szeredid7133112006-04-10 22:54:55 -0700837 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800838 err = -ENOENT;
839 if (!fc->connected)
840 goto err_unlock;
841
Miklos Szeredi334f4852005-09-09 13:10:27 -0700842 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700843 if (!req)
844 goto err_unlock;
845
Miklos Szeredif9a28422006-06-25 05:48:53 -0700846 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700847 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700848 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700849 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800850 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700851 return -ENOENT;
852 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700853 /* Is it an interrupt reply? */
854 if (req->intr_unique == oh.unique) {
855 err = -EINVAL;
856 if (nbytes != sizeof(struct fuse_out_header))
857 goto err_unlock;
858
859 if (oh.error == -ENOSYS)
860 fc->no_interrupt = 1;
861 else if (oh.error == -EAGAIN)
862 queue_interrupt(fc, req);
863
864 spin_unlock(&fc->lock);
865 fuse_copy_finish(&cs);
866 return nbytes;
867 }
868
869 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800870 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700871 req->out.h = oh;
872 req->locked = 1;
873 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700874 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700875
876 err = copy_out_args(&cs, &req->out, nbytes);
877 fuse_copy_finish(&cs);
878
Miklos Szeredid7133112006-04-10 22:54:55 -0700879 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700880 req->locked = 0;
881 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700882 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700883 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700884 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700885 req->out.h.error = -EIO;
886 request_end(fc, req);
887
888 return err ? err : nbytes;
889
890 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700891 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700892 err_finish:
893 fuse_copy_finish(&cs);
894 return err;
895}
896
Miklos Szeredi334f4852005-09-09 13:10:27 -0700897static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
898{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700899 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700900 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700901 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700902 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700903
904 poll_wait(file, &fc->waitq, wait);
905
Miklos Szeredid7133112006-04-10 22:54:55 -0700906 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700907 if (!fc->connected)
908 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700909 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700910 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700911 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700912
913 return mask;
914}
915
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800916/*
917 * Abort all requests on the given list (pending or processing)
918 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700919 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800920 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700921static void end_requests(struct fuse_conn *fc, struct list_head *head)
922{
923 while (!list_empty(head)) {
924 struct fuse_req *req;
925 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700926 req->out.h.error = -ECONNABORTED;
927 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700928 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700929 }
930}
931
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800932/*
933 * Abort requests under I/O
934 *
Miklos Szeredif9a28422006-06-25 05:48:53 -0700935 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800936 * waiter is woken up. This will make request_wait_answer() wait
937 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800938 *
939 * If the request is asynchronous, then the end function needs to be
940 * called after waiting for the request to be unlocked (if it was
941 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800942 */
943static void end_io_requests(struct fuse_conn *fc)
944{
945 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800946 struct fuse_req *req =
947 list_entry(fc->io.next, struct fuse_req, list);
948 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
949
Miklos Szeredif9a28422006-06-25 05:48:53 -0700950 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800951 req->out.h.error = -ECONNABORTED;
952 req->state = FUSE_REQ_FINISHED;
953 list_del_init(&req->list);
954 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800955 if (end) {
956 req->end = NULL;
957 /* The end function will consume this reference */
958 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700959 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800960 wait_event(req->waitq, !req->locked);
961 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700962 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800963 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800964 }
965}
966
967/*
968 * Abort all requests.
969 *
970 * Emergency exit in case of a malicious or accidental deadlock, or
971 * just a hung filesystem.
972 *
973 * The same effect is usually achievable through killing the
974 * filesystem daemon and all users of the filesystem. The exception
975 * is the combination of an asynchronous request and the tricky
976 * deadlock (see Documentation/filesystems/fuse.txt).
977 *
978 * During the aborting, progression of requests from the pending and
979 * processing lists onto the io list, and progression of new requests
980 * onto the pending list is prevented by req->connected being false.
981 *
982 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -0700983 * prevented by the req->aborted flag being true for these requests.
984 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800985 */
986void fuse_abort_conn(struct fuse_conn *fc)
987{
Miklos Szeredid7133112006-04-10 22:54:55 -0700988 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800989 if (fc->connected) {
990 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700991 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800992 end_io_requests(fc);
993 end_requests(fc, &fc->pending);
994 end_requests(fc, &fc->processing);
995 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700996 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700997 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800998 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700999 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001000}
1001
Miklos Szeredi334f4852005-09-09 13:10:27 -07001002static int fuse_dev_release(struct inode *inode, struct file *file)
1003{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001004 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001005 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001006 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001007 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001008 end_requests(fc, &fc->pending);
1009 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001010 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -07001011 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001012 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001013 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001014
Miklos Szeredi334f4852005-09-09 13:10:27 -07001015 return 0;
1016}
1017
Jeff Dike385a17b2006-04-10 22:54:52 -07001018static int fuse_dev_fasync(int fd, struct file *file, int on)
1019{
1020 struct fuse_conn *fc = fuse_get_conn(file);
1021 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001022 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001023
1024 /* No locking - fasync_helper does its own locking */
1025 return fasync_helper(fd, file, on, &fc->fasync);
1026}
1027
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001028const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001029 .owner = THIS_MODULE,
1030 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001031 .read = do_sync_read,
1032 .aio_read = fuse_dev_read,
1033 .write = do_sync_write,
1034 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001035 .poll = fuse_dev_poll,
1036 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001037 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001038};
1039
1040static struct miscdevice fuse_miscdevice = {
1041 .minor = FUSE_MINOR,
1042 .name = "fuse",
1043 .fops = &fuse_dev_operations,
1044};
1045
1046int __init fuse_dev_init(void)
1047{
1048 int err = -ENOMEM;
1049 fuse_req_cachep = kmem_cache_create("fuse_request",
1050 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001051 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001052 if (!fuse_req_cachep)
1053 goto out;
1054
1055 err = misc_register(&fuse_miscdevice);
1056 if (err)
1057 goto out_cache_clean;
1058
1059 return 0;
1060
1061 out_cache_clean:
1062 kmem_cache_destroy(fuse_req_cachep);
1063 out:
1064 return err;
1065}
1066
1067void fuse_dev_cleanup(void)
1068{
1069 misc_deregister(&fuse_miscdevice);
1070 kmem_cache_destroy(fuse_req_cachep);
1071}