blob: ebc36f525eeedbf26b83e75d1d172556c8965125 [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 {
132 wait_event(fc->blocked_waitq, ff->reserved_req);
133 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;
158 wake_up(&fc->blocked_waitq);
159 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);
234 dput(req->dentry);
235 mntput(req->vfsmount);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700236 if (req->file)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700237 fput(req->file);
238 wake_up(&req->waitq);
239 if (end)
240 end(fc, req);
241 else
242 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700243}
244
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700245static void wait_answer_interruptible(struct fuse_conn *fc,
246 struct fuse_req *req)
247{
248 if (signal_pending(current))
249 return;
250
251 spin_unlock(&fc->lock);
252 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
253 spin_lock(&fc->lock);
254}
255
256static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
257{
258 list_add_tail(&req->intr_entry, &fc->interrupts);
259 wake_up(&fc->waitq);
260 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
261}
262
Miklos Szeredid7133112006-04-10 22:54:55 -0700263/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700264static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700265{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700266 if (!fc->no_interrupt) {
267 /* Any signal may interrupt this */
268 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700269
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700270 if (req->aborted)
271 goto aborted;
272 if (req->state == FUSE_REQ_FINISHED)
273 return;
274
275 req->interrupted = 1;
276 if (req->state == FUSE_REQ_SENT)
277 queue_interrupt(fc, req);
278 }
279
280 if (req->force) {
281 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700282 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700283 spin_lock(&fc->lock);
284 } else {
285 sigset_t oldset;
286
287 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700288 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700289 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700290 restore_sigs(&oldset);
291 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700292
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700293 if (req->aborted)
294 goto aborted;
295 if (req->state == FUSE_REQ_FINISHED)
296 return;
297
298 req->out.h.error = -EINTR;
299 req->aborted = 1;
300
301 aborted:
Miklos Szeredi334f4852005-09-09 13:10:27 -0700302 if (req->locked) {
303 /* This is uninterruptible sleep, because data is
304 being copied to/from the buffers of req. During
305 locked state, there mustn't be any filesystem
306 operation (e.g. page fault), since that could lead
307 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700308 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700309 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700310 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700311 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800312 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700313 list_del(&req->list);
314 __fuse_put_request(req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700315 } else if (req->state == FUSE_REQ_SENT) {
316 spin_unlock(&fc->lock);
317 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
318 spin_lock(&fc->lock);
319 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700320}
321
322static unsigned len_args(unsigned numargs, struct fuse_arg *args)
323{
324 unsigned nbytes = 0;
325 unsigned i;
326
327 for (i = 0; i < numargs; i++)
328 nbytes += args[i].size;
329
330 return nbytes;
331}
332
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700333static u64 fuse_get_unique(struct fuse_conn *fc)
334 {
335 fc->reqctr++;
336 /* zero is special */
337 if (fc->reqctr == 0)
338 fc->reqctr = 1;
339
340 return fc->reqctr;
341}
342
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
344{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700345 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700346 req->in.h.len = sizeof(struct fuse_in_header) +
347 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700348 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800349 req->state = FUSE_REQ_PENDING;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200350 if (!req->waiting) {
351 req->waiting = 1;
352 atomic_inc(&fc->num_waiting);
353 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700354 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700355 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700356}
357
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700358void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700359{
360 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700361 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700362 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700363 req->out.h.error = -ENOTCONN;
364 else if (fc->conn_error)
365 req->out.h.error = -ECONNREFUSED;
366 else {
367 queue_request(fc, req);
368 /* acquire extra reference, since request is still needed
369 after request_end() */
370 __fuse_get_request(req);
371
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700372 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700373 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700374 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375}
376
Miklos Szeredi334f4852005-09-09 13:10:27 -0700377static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
378{
Miklos Szeredid7133112006-04-10 22:54:55 -0700379 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700380 if (fc->connected) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700381 req->background = 1;
382 fc->num_background++;
383 if (fc->num_background == FUSE_MAX_BACKGROUND)
384 fc->blocked = 1;
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700385 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
386 set_bdi_congested(&fc->bdi, READ);
387 set_bdi_congested(&fc->bdi, WRITE);
388 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700389
Miklos Szeredi334f4852005-09-09 13:10:27 -0700390 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700391 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700392 } else {
393 req->out.h.error = -ENOTCONN;
394 request_end(fc, req);
395 }
396}
397
398void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
399{
400 req->isreply = 0;
401 request_send_nowait(fc, req);
402}
403
404void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
405{
406 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700407 request_send_nowait(fc, req);
408}
409
Miklos Szeredi334f4852005-09-09 13:10:27 -0700410/*
411 * Lock the request. Up to the next unlock_request() there mustn't be
412 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700413 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700414 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700415static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700416{
417 int err = 0;
418 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700419 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700420 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700421 err = -ENOENT;
422 else
423 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700424 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700425 }
426 return err;
427}
428
429/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700430 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700431 * requester thread is currently waiting for it to be unlocked, so
432 * wake it up.
433 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700434static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700435{
436 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700437 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700438 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700439 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700440 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700441 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700442 }
443}
444
445struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700446 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700447 int write;
448 struct fuse_req *req;
449 const struct iovec *iov;
450 unsigned long nr_segs;
451 unsigned long seglen;
452 unsigned long addr;
453 struct page *pg;
454 void *mapaddr;
455 void *buf;
456 unsigned len;
457};
458
Miklos Szeredid7133112006-04-10 22:54:55 -0700459static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
460 int write, struct fuse_req *req,
461 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700462{
463 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700464 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700465 cs->write = write;
466 cs->req = req;
467 cs->iov = iov;
468 cs->nr_segs = nr_segs;
469}
470
471/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800472static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700473{
474 if (cs->mapaddr) {
475 kunmap_atomic(cs->mapaddr, KM_USER0);
476 if (cs->write) {
477 flush_dcache_page(cs->pg);
478 set_page_dirty_lock(cs->pg);
479 }
480 put_page(cs->pg);
481 cs->mapaddr = NULL;
482 }
483}
484
485/*
486 * Get another pagefull of userspace buffer, and map it to kernel
487 * address space, and lock request
488 */
489static int fuse_copy_fill(struct fuse_copy_state *cs)
490{
491 unsigned long offset;
492 int err;
493
Miklos Szeredid7133112006-04-10 22:54:55 -0700494 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700495 fuse_copy_finish(cs);
496 if (!cs->seglen) {
497 BUG_ON(!cs->nr_segs);
498 cs->seglen = cs->iov[0].iov_len;
499 cs->addr = (unsigned long) cs->iov[0].iov_base;
500 cs->iov ++;
501 cs->nr_segs --;
502 }
503 down_read(&current->mm->mmap_sem);
504 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
505 &cs->pg, NULL);
506 up_read(&current->mm->mmap_sem);
507 if (err < 0)
508 return err;
509 BUG_ON(err != 1);
510 offset = cs->addr % PAGE_SIZE;
511 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
512 cs->buf = cs->mapaddr + offset;
513 cs->len = min(PAGE_SIZE - offset, cs->seglen);
514 cs->seglen -= cs->len;
515 cs->addr += cs->len;
516
Miklos Szeredid7133112006-04-10 22:54:55 -0700517 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700518}
519
520/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800521static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700522{
523 unsigned ncpy = min(*size, cs->len);
524 if (val) {
525 if (cs->write)
526 memcpy(cs->buf, *val, ncpy);
527 else
528 memcpy(*val, cs->buf, ncpy);
529 *val += ncpy;
530 }
531 *size -= ncpy;
532 cs->len -= ncpy;
533 cs->buf += ncpy;
534 return ncpy;
535}
536
537/*
538 * Copy a page in the request to/from the userspace buffer. Must be
539 * done atomically
540 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800541static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
542 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700543{
544 if (page && zeroing && count < PAGE_SIZE) {
545 void *mapaddr = kmap_atomic(page, KM_USER1);
546 memset(mapaddr, 0, PAGE_SIZE);
547 kunmap_atomic(mapaddr, KM_USER1);
548 }
549 while (count) {
550 int err;
551 if (!cs->len && (err = fuse_copy_fill(cs)))
552 return err;
553 if (page) {
554 void *mapaddr = kmap_atomic(page, KM_USER1);
555 void *buf = mapaddr + offset;
556 offset += fuse_copy_do(cs, &buf, &count);
557 kunmap_atomic(mapaddr, KM_USER1);
558 } else
559 offset += fuse_copy_do(cs, NULL, &count);
560 }
561 if (page && !cs->write)
562 flush_dcache_page(page);
563 return 0;
564}
565
566/* Copy pages in the request to/from userspace buffer */
567static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
568 int zeroing)
569{
570 unsigned i;
571 struct fuse_req *req = cs->req;
572 unsigned offset = req->page_offset;
573 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
574
575 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
576 struct page *page = req->pages[i];
577 int err = fuse_copy_page(cs, page, offset, count, zeroing);
578 if (err)
579 return err;
580
581 nbytes -= count;
582 count = min(nbytes, (unsigned) PAGE_SIZE);
583 offset = 0;
584 }
585 return 0;
586}
587
588/* Copy a single argument in the request to/from userspace buffer */
589static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
590{
591 while (size) {
592 int err;
593 if (!cs->len && (err = fuse_copy_fill(cs)))
594 return err;
595 fuse_copy_do(cs, &val, &size);
596 }
597 return 0;
598}
599
600/* Copy request arguments to/from userspace buffer */
601static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
602 unsigned argpages, struct fuse_arg *args,
603 int zeroing)
604{
605 int err = 0;
606 unsigned i;
607
608 for (i = 0; !err && i < numargs; i++) {
609 struct fuse_arg *arg = &args[i];
610 if (i == numargs - 1 && argpages)
611 err = fuse_copy_pages(cs, arg->size, zeroing);
612 else
613 err = fuse_copy_one(cs, arg->value, arg->size);
614 }
615 return err;
616}
617
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700618static int request_pending(struct fuse_conn *fc)
619{
620 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
621}
622
Miklos Szeredi334f4852005-09-09 13:10:27 -0700623/* Wait until a request is available on the pending list */
624static void request_wait(struct fuse_conn *fc)
625{
626 DECLARE_WAITQUEUE(wait, current);
627
628 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700629 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700630 set_current_state(TASK_INTERRUPTIBLE);
631 if (signal_pending(current))
632 break;
633
Miklos Szeredid7133112006-04-10 22:54:55 -0700634 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700635 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700636 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700637 }
638 set_current_state(TASK_RUNNING);
639 remove_wait_queue(&fc->waitq, &wait);
640}
641
642/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700643 * Transfer an interrupt request to userspace
644 *
645 * Unlike other requests this is assembled on demand, without a need
646 * to allocate a separate fuse_req structure.
647 *
648 * Called with fc->lock held, releases it
649 */
650static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
651 const struct iovec *iov, unsigned long nr_segs)
Josh Triplett105f4d72006-09-29 01:59:25 -0700652 __releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700653{
654 struct fuse_copy_state cs;
655 struct fuse_in_header ih;
656 struct fuse_interrupt_in arg;
657 unsigned reqsize = sizeof(ih) + sizeof(arg);
658 int err;
659
660 list_del_init(&req->intr_entry);
661 req->intr_unique = fuse_get_unique(fc);
662 memset(&ih, 0, sizeof(ih));
663 memset(&arg, 0, sizeof(arg));
664 ih.len = reqsize;
665 ih.opcode = FUSE_INTERRUPT;
666 ih.unique = req->intr_unique;
667 arg.unique = req->in.h.unique;
668
669 spin_unlock(&fc->lock);
670 if (iov_length(iov, nr_segs) < reqsize)
671 return -EINVAL;
672
673 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
674 err = fuse_copy_one(&cs, &ih, sizeof(ih));
675 if (!err)
676 err = fuse_copy_one(&cs, &arg, sizeof(arg));
677 fuse_copy_finish(&cs);
678
679 return err ? err : reqsize;
680}
681
682/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700683 * Read a single request into the userspace filesystem's buffer. This
684 * function waits until a request is available, then removes it from
685 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700686 * no reply is needed (FORGET) or request has been aborted or there
687 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700688 * request_end(). Otherwise add it to the processing list, and set
689 * the 'sent' flag.
690 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700691static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
692 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700693{
694 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700695 struct fuse_req *req;
696 struct fuse_in *in;
697 struct fuse_copy_state cs;
698 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700699 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700700 struct fuse_conn *fc = fuse_get_conn(file);
701 if (!fc)
702 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700703
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800704 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700705 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700706 err = -EAGAIN;
707 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700708 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700709 goto err_unlock;
710
Miklos Szeredi334f4852005-09-09 13:10:27 -0700711 request_wait(fc);
712 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800713 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700714 goto err_unlock;
715 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700716 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717 goto err_unlock;
718
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700719 if (!list_empty(&fc->interrupts)) {
720 req = list_entry(fc->interrupts.next, struct fuse_req,
721 intr_entry);
722 return fuse_read_interrupt(fc, req, iov, nr_segs);
723 }
724
Miklos Szeredi334f4852005-09-09 13:10:27 -0700725 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800726 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800727 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700728
729 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800730 reqsize = in->h.len;
731 /* If request is too large, reply with an error and restart the read */
732 if (iov_length(iov, nr_segs) < reqsize) {
733 req->out.h.error = -EIO;
734 /* SETXATTR is special, since it may contain too large data */
735 if (in->h.opcode == FUSE_SETXATTR)
736 req->out.h.error = -E2BIG;
737 request_end(fc, req);
738 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700740 spin_unlock(&fc->lock);
741 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800742 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
743 if (!err)
744 err = fuse_copy_args(&cs, in->numargs, in->argpages,
745 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700746 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700747 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700748 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700749 if (!err && req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700750 err = -ENOENT;
751 if (err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700752 if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700753 req->out.h.error = -EIO;
754 request_end(fc, req);
755 return err;
756 }
757 if (!req->isreply)
758 request_end(fc, req);
759 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800760 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800761 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700762 if (req->interrupted)
763 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700764 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700765 }
766 return reqsize;
767
768 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700769 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700770 return err;
771}
772
Miklos Szeredi334f4852005-09-09 13:10:27 -0700773/* Look up request on processing list by unique ID */
774static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
775{
776 struct list_head *entry;
777
778 list_for_each(entry, &fc->processing) {
779 struct fuse_req *req;
780 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700781 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700782 return req;
783 }
784 return NULL;
785}
786
787static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
788 unsigned nbytes)
789{
790 unsigned reqsize = sizeof(struct fuse_out_header);
791
792 if (out->h.error)
793 return nbytes != reqsize ? -EINVAL : 0;
794
795 reqsize += len_args(out->numargs, out->args);
796
797 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
798 return -EINVAL;
799 else if (reqsize > nbytes) {
800 struct fuse_arg *lastarg = &out->args[out->numargs-1];
801 unsigned diffsize = reqsize - nbytes;
802 if (diffsize > lastarg->size)
803 return -EINVAL;
804 lastarg->size -= diffsize;
805 }
806 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
807 out->page_zeroing);
808}
809
810/*
811 * Write a single reply to a request. First the header is copied from
812 * the write buffer. The request is then searched on the processing
813 * list by the unique ID found in the header. If found, then remove
814 * it from the list and copy the rest of the buffer to the request.
815 * The request is finished by calling request_end()
816 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700817static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
818 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700819{
820 int err;
821 unsigned nbytes = iov_length(iov, nr_segs);
822 struct fuse_req *req;
823 struct fuse_out_header oh;
824 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700825 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700826 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700827 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700828
Miklos Szeredid7133112006-04-10 22:54:55 -0700829 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700830 if (nbytes < sizeof(struct fuse_out_header))
831 return -EINVAL;
832
833 err = fuse_copy_one(&cs, &oh, sizeof(oh));
834 if (err)
835 goto err_finish;
836 err = -EINVAL;
837 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
838 oh.len != nbytes)
839 goto err_finish;
840
Miklos Szeredid7133112006-04-10 22:54:55 -0700841 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800842 err = -ENOENT;
843 if (!fc->connected)
844 goto err_unlock;
845
Miklos Szeredi334f4852005-09-09 13:10:27 -0700846 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700847 if (!req)
848 goto err_unlock;
849
Miklos Szeredif9a28422006-06-25 05:48:53 -0700850 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700851 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700852 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700853 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800854 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700855 return -ENOENT;
856 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700857 /* Is it an interrupt reply? */
858 if (req->intr_unique == oh.unique) {
859 err = -EINVAL;
860 if (nbytes != sizeof(struct fuse_out_header))
861 goto err_unlock;
862
863 if (oh.error == -ENOSYS)
864 fc->no_interrupt = 1;
865 else if (oh.error == -EAGAIN)
866 queue_interrupt(fc, req);
867
868 spin_unlock(&fc->lock);
869 fuse_copy_finish(&cs);
870 return nbytes;
871 }
872
873 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800874 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700875 req->out.h = oh;
876 req->locked = 1;
877 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700878 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700879
880 err = copy_out_args(&cs, &req->out, nbytes);
881 fuse_copy_finish(&cs);
882
Miklos Szeredid7133112006-04-10 22:54:55 -0700883 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700884 req->locked = 0;
885 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700886 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700887 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700888 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700889 req->out.h.error = -EIO;
890 request_end(fc, req);
891
892 return err ? err : nbytes;
893
894 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700895 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700896 err_finish:
897 fuse_copy_finish(&cs);
898 return err;
899}
900
Miklos Szeredi334f4852005-09-09 13:10:27 -0700901static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
902{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700903 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700904 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700905 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700906 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700907
908 poll_wait(file, &fc->waitq, wait);
909
Miklos Szeredid7133112006-04-10 22:54:55 -0700910 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700911 if (!fc->connected)
912 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700913 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700914 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700915 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700916
917 return mask;
918}
919
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800920/*
921 * Abort all requests on the given list (pending or processing)
922 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700923 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800924 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700925static void end_requests(struct fuse_conn *fc, struct list_head *head)
926{
927 while (!list_empty(head)) {
928 struct fuse_req *req;
929 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700930 req->out.h.error = -ECONNABORTED;
931 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700932 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700933 }
934}
935
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800936/*
937 * Abort requests under I/O
938 *
Miklos Szeredif9a28422006-06-25 05:48:53 -0700939 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800940 * waiter is woken up. This will make request_wait_answer() wait
941 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800942 *
943 * If the request is asynchronous, then the end function needs to be
944 * called after waiting for the request to be unlocked (if it was
945 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800946 */
947static void end_io_requests(struct fuse_conn *fc)
948{
949 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800950 struct fuse_req *req =
951 list_entry(fc->io.next, struct fuse_req, list);
952 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
953
Miklos Szeredif9a28422006-06-25 05:48:53 -0700954 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800955 req->out.h.error = -ECONNABORTED;
956 req->state = FUSE_REQ_FINISHED;
957 list_del_init(&req->list);
958 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800959 if (end) {
960 req->end = NULL;
961 /* The end function will consume this reference */
962 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700963 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800964 wait_event(req->waitq, !req->locked);
965 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700966 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800967 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800968 }
969}
970
971/*
972 * Abort all requests.
973 *
974 * Emergency exit in case of a malicious or accidental deadlock, or
975 * just a hung filesystem.
976 *
977 * The same effect is usually achievable through killing the
978 * filesystem daemon and all users of the filesystem. The exception
979 * is the combination of an asynchronous request and the tricky
980 * deadlock (see Documentation/filesystems/fuse.txt).
981 *
982 * During the aborting, progression of requests from the pending and
983 * processing lists onto the io list, and progression of new requests
984 * onto the pending list is prevented by req->connected being false.
985 *
986 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -0700987 * prevented by the req->aborted flag being true for these requests.
988 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800989 */
990void fuse_abort_conn(struct fuse_conn *fc)
991{
Miklos Szeredid7133112006-04-10 22:54:55 -0700992 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800993 if (fc->connected) {
994 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700995 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800996 end_io_requests(fc);
997 end_requests(fc, &fc->pending);
998 end_requests(fc, &fc->processing);
999 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001000 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001001 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001002 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001003 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001004}
1005
Miklos Szeredi334f4852005-09-09 13:10:27 -07001006static int fuse_dev_release(struct inode *inode, struct file *file)
1007{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001008 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001009 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001010 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001011 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001012 end_requests(fc, &fc->pending);
1013 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001014 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -07001015 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001016 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001017 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001018
Miklos Szeredi334f4852005-09-09 13:10:27 -07001019 return 0;
1020}
1021
Jeff Dike385a17b2006-04-10 22:54:52 -07001022static int fuse_dev_fasync(int fd, struct file *file, int on)
1023{
1024 struct fuse_conn *fc = fuse_get_conn(file);
1025 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001026 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001027
1028 /* No locking - fasync_helper does its own locking */
1029 return fasync_helper(fd, file, on, &fc->fasync);
1030}
1031
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001032const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001033 .owner = THIS_MODULE,
1034 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001035 .read = do_sync_read,
1036 .aio_read = fuse_dev_read,
1037 .write = do_sync_write,
1038 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001039 .poll = fuse_dev_poll,
1040 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001041 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001042};
1043
1044static struct miscdevice fuse_miscdevice = {
1045 .minor = FUSE_MINOR,
1046 .name = "fuse",
1047 .fops = &fuse_dev_operations,
1048};
1049
1050int __init fuse_dev_init(void)
1051{
1052 int err = -ENOMEM;
1053 fuse_req_cachep = kmem_cache_create("fuse_request",
1054 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001055 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001056 if (!fuse_req_cachep)
1057 goto out;
1058
1059 err = misc_register(&fuse_miscdevice);
1060 if (err)
1061 goto out_cache_clean;
1062
1063 return 0;
1064
1065 out_cache_clean:
1066 kmem_cache_destroy(fuse_req_cachep);
1067 out:
1068 return err;
1069}
1070
1071void fuse_dev_cleanup(void)
1072{
1073 misc_deregister(&fuse_miscdevice);
1074 kmem_cache_destroy(fuse_req_cachep);
1075}