blob: af639807524e63164397b32f8589bf75131a5112 [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 Szeredid12def12008-02-06 01:38:39 -0800204static unsigned len_args(unsigned numargs, struct fuse_arg *args)
205{
206 unsigned nbytes = 0;
207 unsigned i;
208
209 for (i = 0; i < numargs; i++)
210 nbytes += args[i].size;
211
212 return nbytes;
213}
214
215static u64 fuse_get_unique(struct fuse_conn *fc)
216{
217 fc->reqctr++;
218 /* zero is special */
219 if (fc->reqctr == 0)
220 fc->reqctr = 1;
221
222 return fc->reqctr;
223}
224
225static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
226{
227 req->in.h.unique = fuse_get_unique(fc);
228 req->in.h.len = sizeof(struct fuse_in_header) +
229 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
230 list_add_tail(&req->list, &fc->pending);
231 req->state = FUSE_REQ_PENDING;
232 if (!req->waiting) {
233 req->waiting = 1;
234 atomic_inc(&fc->num_waiting);
235 }
236 wake_up(&fc->waitq);
237 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
238}
239
240static void flush_bg_queue(struct fuse_conn *fc)
241{
242 while (fc->active_background < FUSE_MAX_BACKGROUND &&
243 !list_empty(&fc->bg_queue)) {
244 struct fuse_req *req;
245
246 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
247 list_del(&req->list);
248 fc->active_background++;
249 queue_request(fc, req);
250 }
251}
252
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200253/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700254 * This function is called when a request is finished. Either a reply
Miklos Szeredif9a28422006-06-25 05:48:53 -0700255 * has arrived or it was aborted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800256 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700257 * was closed. The requester thread is woken up (if still waiting),
258 * the 'end' callback is called if given, else the reference to the
259 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800260 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700261 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700262 */
263static void request_end(struct fuse_conn *fc, struct fuse_req *req)
Josh Triplett105f4d72006-09-29 01:59:25 -0700264 __releases(fc->lock)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700265{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700266 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
267 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800268 list_del(&req->list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700269 list_del(&req->intr_entry);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800270 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700271 if (req->background) {
272 if (fc->num_background == FUSE_MAX_BACKGROUND) {
273 fc->blocked = 0;
274 wake_up_all(&fc->blocked_waitq);
275 }
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700276 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
277 clear_bdi_congested(&fc->bdi, READ);
278 clear_bdi_congested(&fc->bdi, WRITE);
279 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700280 fc->num_background--;
Miklos Szeredid12def12008-02-06 01:38:39 -0800281 fc->active_background--;
282 flush_bg_queue(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700283 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700284 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700285 wake_up(&req->waitq);
286 if (end)
287 end(fc, req);
288 else
289 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700290}
291
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700292static void wait_answer_interruptible(struct fuse_conn *fc,
293 struct fuse_req *req)
294{
295 if (signal_pending(current))
296 return;
297
298 spin_unlock(&fc->lock);
299 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
300 spin_lock(&fc->lock);
301}
302
303static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
304{
305 list_add_tail(&req->intr_entry, &fc->interrupts);
306 wake_up(&fc->waitq);
307 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
308}
309
Miklos Szeredid7133112006-04-10 22:54:55 -0700310/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700311static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700312{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700313 if (!fc->no_interrupt) {
314 /* Any signal may interrupt this */
315 wait_answer_interruptible(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700316
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700317 if (req->aborted)
318 goto aborted;
319 if (req->state == FUSE_REQ_FINISHED)
320 return;
321
322 req->interrupted = 1;
323 if (req->state == FUSE_REQ_SENT)
324 queue_interrupt(fc, req);
325 }
326
Miklos Szeredia131de02007-10-16 23:31:04 -0700327 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700328 sigset_t oldset;
329
330 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700331 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700332 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700333 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700334
335 if (req->aborted)
336 goto aborted;
337 if (req->state == FUSE_REQ_FINISHED)
338 return;
339
340 /* Request is not yet in userspace, bail out */
341 if (req->state == FUSE_REQ_PENDING) {
342 list_del(&req->list);
343 __fuse_put_request(req);
344 req->out.h.error = -EINTR;
345 return;
346 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700347 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700348
Miklos Szeredia131de02007-10-16 23:31:04 -0700349 /*
350 * Either request is already in userspace, or it was forced.
351 * Wait it out.
352 */
353 spin_unlock(&fc->lock);
354 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
355 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700356
Miklos Szeredia131de02007-10-16 23:31:04 -0700357 if (!req->aborted)
358 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700359
360 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700361 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700362 if (req->locked) {
363 /* This is uninterruptible sleep, because data is
364 being copied to/from the buffers of req. During
365 locked state, there mustn't be any filesystem
366 operation (e.g. page fault), since that could lead
367 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700368 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700369 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700370 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700371 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700372}
373
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700374void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375{
376 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700377 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700378 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700379 req->out.h.error = -ENOTCONN;
380 else if (fc->conn_error)
381 req->out.h.error = -ECONNREFUSED;
382 else {
383 queue_request(fc, req);
384 /* acquire extra reference, since request is still needed
385 after request_end() */
386 __fuse_get_request(req);
387
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700388 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700389 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700390 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700391}
392
Miklos Szeredid12def12008-02-06 01:38:39 -0800393static void request_send_nowait_locked(struct fuse_conn *fc,
394 struct fuse_req *req)
395{
396 req->background = 1;
397 fc->num_background++;
398 if (fc->num_background == FUSE_MAX_BACKGROUND)
399 fc->blocked = 1;
400 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
401 set_bdi_congested(&fc->bdi, READ);
402 set_bdi_congested(&fc->bdi, WRITE);
403 }
404 list_add_tail(&req->list, &fc->bg_queue);
405 flush_bg_queue(fc);
406}
407
Miklos Szeredi334f4852005-09-09 13:10:27 -0700408static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
409{
Miklos Szeredid7133112006-04-10 22:54:55 -0700410 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700411 if (fc->connected) {
Miklos Szeredid12def12008-02-06 01:38:39 -0800412 request_send_nowait_locked(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700413 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700414 } else {
415 req->out.h.error = -ENOTCONN;
416 request_end(fc, req);
417 }
418}
419
420void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
421{
422 req->isreply = 0;
423 request_send_nowait(fc, req);
424}
425
426void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
427{
428 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700429 request_send_nowait(fc, req);
430}
431
Miklos Szeredi334f4852005-09-09 13:10:27 -0700432/*
433 * Lock the request. Up to the next unlock_request() there mustn't be
434 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700435 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700436 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700437static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700438{
439 int err = 0;
440 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700441 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700442 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443 err = -ENOENT;
444 else
445 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700446 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700447 }
448 return err;
449}
450
451/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700452 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700453 * requester thread is currently waiting for it to be unlocked, so
454 * wake it up.
455 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700456static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700457{
458 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700459 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700460 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700461 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700462 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700463 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700464 }
465}
466
467struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700468 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700469 int write;
470 struct fuse_req *req;
471 const struct iovec *iov;
472 unsigned long nr_segs;
473 unsigned long seglen;
474 unsigned long addr;
475 struct page *pg;
476 void *mapaddr;
477 void *buf;
478 unsigned len;
479};
480
Miklos Szeredid7133112006-04-10 22:54:55 -0700481static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
482 int write, struct fuse_req *req,
483 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700484{
485 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700486 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700487 cs->write = write;
488 cs->req = req;
489 cs->iov = iov;
490 cs->nr_segs = nr_segs;
491}
492
493/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800494static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700495{
496 if (cs->mapaddr) {
497 kunmap_atomic(cs->mapaddr, KM_USER0);
498 if (cs->write) {
499 flush_dcache_page(cs->pg);
500 set_page_dirty_lock(cs->pg);
501 }
502 put_page(cs->pg);
503 cs->mapaddr = NULL;
504 }
505}
506
507/*
508 * Get another pagefull of userspace buffer, and map it to kernel
509 * address space, and lock request
510 */
511static int fuse_copy_fill(struct fuse_copy_state *cs)
512{
513 unsigned long offset;
514 int err;
515
Miklos Szeredid7133112006-04-10 22:54:55 -0700516 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700517 fuse_copy_finish(cs);
518 if (!cs->seglen) {
519 BUG_ON(!cs->nr_segs);
520 cs->seglen = cs->iov[0].iov_len;
521 cs->addr = (unsigned long) cs->iov[0].iov_base;
522 cs->iov ++;
523 cs->nr_segs --;
524 }
525 down_read(&current->mm->mmap_sem);
526 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
527 &cs->pg, NULL);
528 up_read(&current->mm->mmap_sem);
529 if (err < 0)
530 return err;
531 BUG_ON(err != 1);
532 offset = cs->addr % PAGE_SIZE;
533 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
534 cs->buf = cs->mapaddr + offset;
535 cs->len = min(PAGE_SIZE - offset, cs->seglen);
536 cs->seglen -= cs->len;
537 cs->addr += cs->len;
538
Miklos Szeredid7133112006-04-10 22:54:55 -0700539 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700540}
541
542/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800543static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700544{
545 unsigned ncpy = min(*size, cs->len);
546 if (val) {
547 if (cs->write)
548 memcpy(cs->buf, *val, ncpy);
549 else
550 memcpy(*val, cs->buf, ncpy);
551 *val += ncpy;
552 }
553 *size -= ncpy;
554 cs->len -= ncpy;
555 cs->buf += ncpy;
556 return ncpy;
557}
558
559/*
560 * Copy a page in the request to/from the userspace buffer. Must be
561 * done atomically
562 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800563static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
564 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700565{
566 if (page && zeroing && count < PAGE_SIZE) {
567 void *mapaddr = kmap_atomic(page, KM_USER1);
568 memset(mapaddr, 0, PAGE_SIZE);
569 kunmap_atomic(mapaddr, KM_USER1);
570 }
571 while (count) {
572 int err;
573 if (!cs->len && (err = fuse_copy_fill(cs)))
574 return err;
575 if (page) {
576 void *mapaddr = kmap_atomic(page, KM_USER1);
577 void *buf = mapaddr + offset;
578 offset += fuse_copy_do(cs, &buf, &count);
579 kunmap_atomic(mapaddr, KM_USER1);
580 } else
581 offset += fuse_copy_do(cs, NULL, &count);
582 }
583 if (page && !cs->write)
584 flush_dcache_page(page);
585 return 0;
586}
587
588/* Copy pages in the request to/from userspace buffer */
589static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
590 int zeroing)
591{
592 unsigned i;
593 struct fuse_req *req = cs->req;
594 unsigned offset = req->page_offset;
595 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
596
597 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
598 struct page *page = req->pages[i];
599 int err = fuse_copy_page(cs, page, offset, count, zeroing);
600 if (err)
601 return err;
602
603 nbytes -= count;
604 count = min(nbytes, (unsigned) PAGE_SIZE);
605 offset = 0;
606 }
607 return 0;
608}
609
610/* Copy a single argument in the request to/from userspace buffer */
611static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
612{
613 while (size) {
614 int err;
615 if (!cs->len && (err = fuse_copy_fill(cs)))
616 return err;
617 fuse_copy_do(cs, &val, &size);
618 }
619 return 0;
620}
621
622/* Copy request arguments to/from userspace buffer */
623static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
624 unsigned argpages, struct fuse_arg *args,
625 int zeroing)
626{
627 int err = 0;
628 unsigned i;
629
630 for (i = 0; !err && i < numargs; i++) {
631 struct fuse_arg *arg = &args[i];
632 if (i == numargs - 1 && argpages)
633 err = fuse_copy_pages(cs, arg->size, zeroing);
634 else
635 err = fuse_copy_one(cs, arg->value, arg->size);
636 }
637 return err;
638}
639
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700640static int request_pending(struct fuse_conn *fc)
641{
642 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
643}
644
Miklos Szeredi334f4852005-09-09 13:10:27 -0700645/* Wait until a request is available on the pending list */
646static void request_wait(struct fuse_conn *fc)
647{
648 DECLARE_WAITQUEUE(wait, current);
649
650 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700651 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700652 set_current_state(TASK_INTERRUPTIBLE);
653 if (signal_pending(current))
654 break;
655
Miklos Szeredid7133112006-04-10 22:54:55 -0700656 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700657 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700658 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700659 }
660 set_current_state(TASK_RUNNING);
661 remove_wait_queue(&fc->waitq, &wait);
662}
663
664/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700665 * Transfer an interrupt request to userspace
666 *
667 * Unlike other requests this is assembled on demand, without a need
668 * to allocate a separate fuse_req structure.
669 *
670 * Called with fc->lock held, releases it
671 */
672static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
673 const struct iovec *iov, unsigned long nr_segs)
Josh Triplett105f4d72006-09-29 01:59:25 -0700674 __releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700675{
676 struct fuse_copy_state cs;
677 struct fuse_in_header ih;
678 struct fuse_interrupt_in arg;
679 unsigned reqsize = sizeof(ih) + sizeof(arg);
680 int err;
681
682 list_del_init(&req->intr_entry);
683 req->intr_unique = fuse_get_unique(fc);
684 memset(&ih, 0, sizeof(ih));
685 memset(&arg, 0, sizeof(arg));
686 ih.len = reqsize;
687 ih.opcode = FUSE_INTERRUPT;
688 ih.unique = req->intr_unique;
689 arg.unique = req->in.h.unique;
690
691 spin_unlock(&fc->lock);
692 if (iov_length(iov, nr_segs) < reqsize)
693 return -EINVAL;
694
695 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
696 err = fuse_copy_one(&cs, &ih, sizeof(ih));
697 if (!err)
698 err = fuse_copy_one(&cs, &arg, sizeof(arg));
699 fuse_copy_finish(&cs);
700
701 return err ? err : reqsize;
702}
703
704/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700705 * Read a single request into the userspace filesystem's buffer. This
706 * function waits until a request is available, then removes it from
707 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700708 * no reply is needed (FORGET) or request has been aborted or there
709 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700710 * request_end(). Otherwise add it to the processing list, and set
711 * the 'sent' flag.
712 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700713static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
714 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700715{
716 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700717 struct fuse_req *req;
718 struct fuse_in *in;
719 struct fuse_copy_state cs;
720 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700721 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700722 struct fuse_conn *fc = fuse_get_conn(file);
723 if (!fc)
724 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700725
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800726 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700727 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700728 err = -EAGAIN;
729 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700730 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700731 goto err_unlock;
732
Miklos Szeredi334f4852005-09-09 13:10:27 -0700733 request_wait(fc);
734 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800735 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700736 goto err_unlock;
737 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700738 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700739 goto err_unlock;
740
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700741 if (!list_empty(&fc->interrupts)) {
742 req = list_entry(fc->interrupts.next, struct fuse_req,
743 intr_entry);
744 return fuse_read_interrupt(fc, req, iov, nr_segs);
745 }
746
Miklos Szeredi334f4852005-09-09 13:10:27 -0700747 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800748 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800749 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700750
751 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800752 reqsize = in->h.len;
753 /* If request is too large, reply with an error and restart the read */
754 if (iov_length(iov, nr_segs) < reqsize) {
755 req->out.h.error = -EIO;
756 /* SETXATTR is special, since it may contain too large data */
757 if (in->h.opcode == FUSE_SETXATTR)
758 req->out.h.error = -E2BIG;
759 request_end(fc, req);
760 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700761 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700762 spin_unlock(&fc->lock);
763 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800764 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
765 if (!err)
766 err = fuse_copy_args(&cs, in->numargs, in->argpages,
767 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700768 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700769 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700770 req->locked = 0;
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700771 if (req->aborted) {
772 request_end(fc, req);
773 return -ENODEV;
774 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700775 if (err) {
Miklos Szeredic9c9d7d2007-10-16 23:31:05 -0700776 req->out.h.error = -EIO;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700777 request_end(fc, req);
778 return err;
779 }
780 if (!req->isreply)
781 request_end(fc, req);
782 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800783 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800784 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700785 if (req->interrupted)
786 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700787 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700788 }
789 return reqsize;
790
791 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700792 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700793 return err;
794}
795
Miklos Szeredi334f4852005-09-09 13:10:27 -0700796/* Look up request on processing list by unique ID */
797static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
798{
799 struct list_head *entry;
800
801 list_for_each(entry, &fc->processing) {
802 struct fuse_req *req;
803 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700804 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700805 return req;
806 }
807 return NULL;
808}
809
810static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
811 unsigned nbytes)
812{
813 unsigned reqsize = sizeof(struct fuse_out_header);
814
815 if (out->h.error)
816 return nbytes != reqsize ? -EINVAL : 0;
817
818 reqsize += len_args(out->numargs, out->args);
819
820 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
821 return -EINVAL;
822 else if (reqsize > nbytes) {
823 struct fuse_arg *lastarg = &out->args[out->numargs-1];
824 unsigned diffsize = reqsize - nbytes;
825 if (diffsize > lastarg->size)
826 return -EINVAL;
827 lastarg->size -= diffsize;
828 }
829 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
830 out->page_zeroing);
831}
832
833/*
834 * Write a single reply to a request. First the header is copied from
835 * the write buffer. The request is then searched on the processing
836 * list by the unique ID found in the header. If found, then remove
837 * it from the list and copy the rest of the buffer to the request.
838 * The request is finished by calling request_end()
839 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700840static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
841 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700842{
843 int err;
844 unsigned nbytes = iov_length(iov, nr_segs);
845 struct fuse_req *req;
846 struct fuse_out_header oh;
847 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700848 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700849 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700850 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700851
Miklos Szeredid7133112006-04-10 22:54:55 -0700852 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700853 if (nbytes < sizeof(struct fuse_out_header))
854 return -EINVAL;
855
856 err = fuse_copy_one(&cs, &oh, sizeof(oh));
857 if (err)
858 goto err_finish;
859 err = -EINVAL;
860 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
861 oh.len != nbytes)
862 goto err_finish;
863
Miklos Szeredid7133112006-04-10 22:54:55 -0700864 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800865 err = -ENOENT;
866 if (!fc->connected)
867 goto err_unlock;
868
Miklos Szeredi334f4852005-09-09 13:10:27 -0700869 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700870 if (!req)
871 goto err_unlock;
872
Miklos Szeredif9a28422006-06-25 05:48:53 -0700873 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700874 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700875 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700876 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800877 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700878 return -ENOENT;
879 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700880 /* Is it an interrupt reply? */
881 if (req->intr_unique == oh.unique) {
882 err = -EINVAL;
883 if (nbytes != sizeof(struct fuse_out_header))
884 goto err_unlock;
885
886 if (oh.error == -ENOSYS)
887 fc->no_interrupt = 1;
888 else if (oh.error == -EAGAIN)
889 queue_interrupt(fc, req);
890
891 spin_unlock(&fc->lock);
892 fuse_copy_finish(&cs);
893 return nbytes;
894 }
895
896 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800897 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700898 req->out.h = oh;
899 req->locked = 1;
900 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700901 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700902
903 err = copy_out_args(&cs, &req->out, nbytes);
904 fuse_copy_finish(&cs);
905
Miklos Szeredid7133112006-04-10 22:54:55 -0700906 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700907 req->locked = 0;
908 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700909 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700910 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700911 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700912 req->out.h.error = -EIO;
913 request_end(fc, req);
914
915 return err ? err : nbytes;
916
917 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700918 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700919 err_finish:
920 fuse_copy_finish(&cs);
921 return err;
922}
923
Miklos Szeredi334f4852005-09-09 13:10:27 -0700924static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
925{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700926 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700927 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700928 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700929 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700930
931 poll_wait(file, &fc->waitq, wait);
932
Miklos Szeredid7133112006-04-10 22:54:55 -0700933 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700934 if (!fc->connected)
935 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700936 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700937 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700938 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700939
940 return mask;
941}
942
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800943/*
944 * Abort all requests on the given list (pending or processing)
945 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700946 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800947 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700948static void end_requests(struct fuse_conn *fc, struct list_head *head)
949{
950 while (!list_empty(head)) {
951 struct fuse_req *req;
952 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700953 req->out.h.error = -ECONNABORTED;
954 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700955 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700956 }
957}
958
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800959/*
960 * Abort requests under I/O
961 *
Miklos Szeredif9a28422006-06-25 05:48:53 -0700962 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800963 * waiter is woken up. This will make request_wait_answer() wait
964 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800965 *
966 * If the request is asynchronous, then the end function needs to be
967 * called after waiting for the request to be unlocked (if it was
968 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800969 */
970static void end_io_requests(struct fuse_conn *fc)
971{
972 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800973 struct fuse_req *req =
974 list_entry(fc->io.next, struct fuse_req, list);
975 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
976
Miklos Szeredif9a28422006-06-25 05:48:53 -0700977 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800978 req->out.h.error = -ECONNABORTED;
979 req->state = FUSE_REQ_FINISHED;
980 list_del_init(&req->list);
981 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800982 if (end) {
983 req->end = NULL;
984 /* The end function will consume this reference */
985 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700986 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800987 wait_event(req->waitq, !req->locked);
988 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700989 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800990 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800991 }
992}
993
994/*
995 * Abort all requests.
996 *
997 * Emergency exit in case of a malicious or accidental deadlock, or
998 * just a hung filesystem.
999 *
1000 * The same effect is usually achievable through killing the
1001 * filesystem daemon and all users of the filesystem. The exception
1002 * is the combination of an asynchronous request and the tricky
1003 * deadlock (see Documentation/filesystems/fuse.txt).
1004 *
1005 * During the aborting, progression of requests from the pending and
1006 * processing lists onto the io list, and progression of new requests
1007 * onto the pending list is prevented by req->connected being false.
1008 *
1009 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -07001010 * prevented by the req->aborted flag being true for these requests.
1011 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001012 */
1013void fuse_abort_conn(struct fuse_conn *fc)
1014{
Miklos Szeredid7133112006-04-10 22:54:55 -07001015 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001016 if (fc->connected) {
1017 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001018 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001019 end_io_requests(fc);
1020 end_requests(fc, &fc->pending);
1021 end_requests(fc, &fc->processing);
1022 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001023 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001024 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001025 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001026 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001027}
1028
Miklos Szeredi334f4852005-09-09 13:10:27 -07001029static int fuse_dev_release(struct inode *inode, struct file *file)
1030{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001031 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001032 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001033 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001034 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001035 end_requests(fc, &fc->pending);
1036 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001037 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -07001038 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001039 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001040 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001041
Miklos Szeredi334f4852005-09-09 13:10:27 -07001042 return 0;
1043}
1044
Jeff Dike385a17b2006-04-10 22:54:52 -07001045static int fuse_dev_fasync(int fd, struct file *file, int on)
1046{
1047 struct fuse_conn *fc = fuse_get_conn(file);
1048 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001049 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001050
1051 /* No locking - fasync_helper does its own locking */
1052 return fasync_helper(fd, file, on, &fc->fasync);
1053}
1054
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001055const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001056 .owner = THIS_MODULE,
1057 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001058 .read = do_sync_read,
1059 .aio_read = fuse_dev_read,
1060 .write = do_sync_write,
1061 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001062 .poll = fuse_dev_poll,
1063 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001064 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001065};
1066
1067static struct miscdevice fuse_miscdevice = {
1068 .minor = FUSE_MINOR,
1069 .name = "fuse",
1070 .fops = &fuse_dev_operations,
1071};
1072
1073int __init fuse_dev_init(void)
1074{
1075 int err = -ENOMEM;
1076 fuse_req_cachep = kmem_cache_create("fuse_request",
1077 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001078 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001079 if (!fuse_req_cachep)
1080 goto out;
1081
1082 err = misc_register(&fuse_miscdevice);
1083 if (err)
1084 goto out_cache_clean;
1085
1086 return 0;
1087
1088 out_cache_clean:
1089 kmem_cache_destroy(fuse_req_cachep);
1090 out:
1091 return err;
1092}
1093
1094void fuse_dev_cleanup(void)
1095{
1096 misc_deregister(&fuse_miscdevice);
1097 kmem_cache_destroy(fuse_req_cachep);
1098}