blob: cc6c2908fb1221395d145af9fc596d2036c32146 [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
Miklos Szeredia131de02007-10-16 23:31:04 -0700276 if (!req->force) {
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700277 sigset_t oldset;
278
279 /* Only fatal signals may interrupt this */
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700280 block_sigs(&oldset);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700281 wait_answer_interruptible(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700282 restore_sigs(&oldset);
Miklos Szeredia131de02007-10-16 23:31:04 -0700283
284 if (req->aborted)
285 goto aborted;
286 if (req->state == FUSE_REQ_FINISHED)
287 return;
288
289 /* Request is not yet in userspace, bail out */
290 if (req->state == FUSE_REQ_PENDING) {
291 list_del(&req->list);
292 __fuse_put_request(req);
293 req->out.h.error = -EINTR;
294 return;
295 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700296 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700297
Miklos Szeredia131de02007-10-16 23:31:04 -0700298 /*
299 * Either request is already in userspace, or it was forced.
300 * Wait it out.
301 */
302 spin_unlock(&fc->lock);
303 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
304 spin_lock(&fc->lock);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700305
Miklos Szeredia131de02007-10-16 23:31:04 -0700306 if (!req->aborted)
307 return;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700308
309 aborted:
Miklos Szeredia131de02007-10-16 23:31:04 -0700310 BUG_ON(req->state != FUSE_REQ_FINISHED);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700311 if (req->locked) {
312 /* This is uninterruptible sleep, because data is
313 being copied to/from the buffers of req. During
314 locked state, there mustn't be any filesystem
315 operation (e.g. page fault), since that could lead
316 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700317 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700318 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700319 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700320 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700321}
322
323static unsigned len_args(unsigned numargs, struct fuse_arg *args)
324{
325 unsigned nbytes = 0;
326 unsigned i;
327
328 for (i = 0; i < numargs; i++)
329 nbytes += args[i].size;
330
331 return nbytes;
332}
333
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700334static u64 fuse_get_unique(struct fuse_conn *fc)
335 {
336 fc->reqctr++;
337 /* zero is special */
338 if (fc->reqctr == 0)
339 fc->reqctr = 1;
340
341 return fc->reqctr;
342}
343
Miklos Szeredi334f4852005-09-09 13:10:27 -0700344static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
345{
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700346 req->in.h.unique = fuse_get_unique(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700347 req->in.h.len = sizeof(struct fuse_in_header) +
348 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700349 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800350 req->state = FUSE_REQ_PENDING;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200351 if (!req->waiting) {
352 req->waiting = 1;
353 atomic_inc(&fc->num_waiting);
354 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700355 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700356 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700357}
358
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700359void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700360{
361 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700362 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700363 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700364 req->out.h.error = -ENOTCONN;
365 else if (fc->conn_error)
366 req->out.h.error = -ECONNREFUSED;
367 else {
368 queue_request(fc, req);
369 /* acquire extra reference, since request is still needed
370 after request_end() */
371 __fuse_get_request(req);
372
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700373 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700374 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700375 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700376}
377
Miklos Szeredi334f4852005-09-09 13:10:27 -0700378static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
379{
Miklos Szeredid7133112006-04-10 22:54:55 -0700380 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700381 if (fc->connected) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700382 req->background = 1;
383 fc->num_background++;
384 if (fc->num_background == FUSE_MAX_BACKGROUND)
385 fc->blocked = 1;
Miklos Szeredif92b99b2007-10-16 23:30:59 -0700386 if (fc->num_background == FUSE_CONGESTION_THRESHOLD) {
387 set_bdi_congested(&fc->bdi, READ);
388 set_bdi_congested(&fc->bdi, WRITE);
389 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700390
Miklos Szeredi334f4852005-09-09 13:10:27 -0700391 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700392 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700393 } else {
394 req->out.h.error = -ENOTCONN;
395 request_end(fc, req);
396 }
397}
398
399void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
400{
401 req->isreply = 0;
402 request_send_nowait(fc, req);
403}
404
405void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
406{
407 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700408 request_send_nowait(fc, req);
409}
410
Miklos Szeredi334f4852005-09-09 13:10:27 -0700411/*
412 * Lock the request. Up to the next unlock_request() there mustn't be
413 * anything that could cause a page-fault. If the request was already
Miklos Szeredif9a28422006-06-25 05:48:53 -0700414 * aborted bail out.
Miklos Szeredi334f4852005-09-09 13:10:27 -0700415 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700416static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417{
418 int err = 0;
419 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700420 spin_lock(&fc->lock);
Miklos Szeredif9a28422006-06-25 05:48:53 -0700421 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700422 err = -ENOENT;
423 else
424 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700425 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700426 }
427 return err;
428}
429
430/*
Miklos Szeredif9a28422006-06-25 05:48:53 -0700431 * Unlock request. If it was aborted during being locked, the
Miklos Szeredi334f4852005-09-09 13:10:27 -0700432 * requester thread is currently waiting for it to be unlocked, so
433 * wake it up.
434 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700435static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700436{
437 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700438 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700439 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700440 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700441 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700442 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700443 }
444}
445
446struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700447 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700448 int write;
449 struct fuse_req *req;
450 const struct iovec *iov;
451 unsigned long nr_segs;
452 unsigned long seglen;
453 unsigned long addr;
454 struct page *pg;
455 void *mapaddr;
456 void *buf;
457 unsigned len;
458};
459
Miklos Szeredid7133112006-04-10 22:54:55 -0700460static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
461 int write, struct fuse_req *req,
462 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700463{
464 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700465 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700466 cs->write = write;
467 cs->req = req;
468 cs->iov = iov;
469 cs->nr_segs = nr_segs;
470}
471
472/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800473static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700474{
475 if (cs->mapaddr) {
476 kunmap_atomic(cs->mapaddr, KM_USER0);
477 if (cs->write) {
478 flush_dcache_page(cs->pg);
479 set_page_dirty_lock(cs->pg);
480 }
481 put_page(cs->pg);
482 cs->mapaddr = NULL;
483 }
484}
485
486/*
487 * Get another pagefull of userspace buffer, and map it to kernel
488 * address space, and lock request
489 */
490static int fuse_copy_fill(struct fuse_copy_state *cs)
491{
492 unsigned long offset;
493 int err;
494
Miklos Szeredid7133112006-04-10 22:54:55 -0700495 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700496 fuse_copy_finish(cs);
497 if (!cs->seglen) {
498 BUG_ON(!cs->nr_segs);
499 cs->seglen = cs->iov[0].iov_len;
500 cs->addr = (unsigned long) cs->iov[0].iov_base;
501 cs->iov ++;
502 cs->nr_segs --;
503 }
504 down_read(&current->mm->mmap_sem);
505 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
506 &cs->pg, NULL);
507 up_read(&current->mm->mmap_sem);
508 if (err < 0)
509 return err;
510 BUG_ON(err != 1);
511 offset = cs->addr % PAGE_SIZE;
512 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
513 cs->buf = cs->mapaddr + offset;
514 cs->len = min(PAGE_SIZE - offset, cs->seglen);
515 cs->seglen -= cs->len;
516 cs->addr += cs->len;
517
Miklos Szeredid7133112006-04-10 22:54:55 -0700518 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700519}
520
521/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800522static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700523{
524 unsigned ncpy = min(*size, cs->len);
525 if (val) {
526 if (cs->write)
527 memcpy(cs->buf, *val, ncpy);
528 else
529 memcpy(*val, cs->buf, ncpy);
530 *val += ncpy;
531 }
532 *size -= ncpy;
533 cs->len -= ncpy;
534 cs->buf += ncpy;
535 return ncpy;
536}
537
538/*
539 * Copy a page in the request to/from the userspace buffer. Must be
540 * done atomically
541 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800542static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
543 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700544{
545 if (page && zeroing && count < PAGE_SIZE) {
546 void *mapaddr = kmap_atomic(page, KM_USER1);
547 memset(mapaddr, 0, PAGE_SIZE);
548 kunmap_atomic(mapaddr, KM_USER1);
549 }
550 while (count) {
551 int err;
552 if (!cs->len && (err = fuse_copy_fill(cs)))
553 return err;
554 if (page) {
555 void *mapaddr = kmap_atomic(page, KM_USER1);
556 void *buf = mapaddr + offset;
557 offset += fuse_copy_do(cs, &buf, &count);
558 kunmap_atomic(mapaddr, KM_USER1);
559 } else
560 offset += fuse_copy_do(cs, NULL, &count);
561 }
562 if (page && !cs->write)
563 flush_dcache_page(page);
564 return 0;
565}
566
567/* Copy pages in the request to/from userspace buffer */
568static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
569 int zeroing)
570{
571 unsigned i;
572 struct fuse_req *req = cs->req;
573 unsigned offset = req->page_offset;
574 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
575
576 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
577 struct page *page = req->pages[i];
578 int err = fuse_copy_page(cs, page, offset, count, zeroing);
579 if (err)
580 return err;
581
582 nbytes -= count;
583 count = min(nbytes, (unsigned) PAGE_SIZE);
584 offset = 0;
585 }
586 return 0;
587}
588
589/* Copy a single argument in the request to/from userspace buffer */
590static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
591{
592 while (size) {
593 int err;
594 if (!cs->len && (err = fuse_copy_fill(cs)))
595 return err;
596 fuse_copy_do(cs, &val, &size);
597 }
598 return 0;
599}
600
601/* Copy request arguments to/from userspace buffer */
602static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
603 unsigned argpages, struct fuse_arg *args,
604 int zeroing)
605{
606 int err = 0;
607 unsigned i;
608
609 for (i = 0; !err && i < numargs; i++) {
610 struct fuse_arg *arg = &args[i];
611 if (i == numargs - 1 && argpages)
612 err = fuse_copy_pages(cs, arg->size, zeroing);
613 else
614 err = fuse_copy_one(cs, arg->value, arg->size);
615 }
616 return err;
617}
618
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700619static int request_pending(struct fuse_conn *fc)
620{
621 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts);
622}
623
Miklos Szeredi334f4852005-09-09 13:10:27 -0700624/* Wait until a request is available on the pending list */
625static void request_wait(struct fuse_conn *fc)
626{
627 DECLARE_WAITQUEUE(wait, current);
628
629 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700630 while (fc->connected && !request_pending(fc)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700631 set_current_state(TASK_INTERRUPTIBLE);
632 if (signal_pending(current))
633 break;
634
Miklos Szeredid7133112006-04-10 22:54:55 -0700635 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700636 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700637 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700638 }
639 set_current_state(TASK_RUNNING);
640 remove_wait_queue(&fc->waitq, &wait);
641}
642
643/*
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700644 * Transfer an interrupt request to userspace
645 *
646 * Unlike other requests this is assembled on demand, without a need
647 * to allocate a separate fuse_req structure.
648 *
649 * Called with fc->lock held, releases it
650 */
651static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req,
652 const struct iovec *iov, unsigned long nr_segs)
Josh Triplett105f4d72006-09-29 01:59:25 -0700653 __releases(fc->lock)
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700654{
655 struct fuse_copy_state cs;
656 struct fuse_in_header ih;
657 struct fuse_interrupt_in arg;
658 unsigned reqsize = sizeof(ih) + sizeof(arg);
659 int err;
660
661 list_del_init(&req->intr_entry);
662 req->intr_unique = fuse_get_unique(fc);
663 memset(&ih, 0, sizeof(ih));
664 memset(&arg, 0, sizeof(arg));
665 ih.len = reqsize;
666 ih.opcode = FUSE_INTERRUPT;
667 ih.unique = req->intr_unique;
668 arg.unique = req->in.h.unique;
669
670 spin_unlock(&fc->lock);
671 if (iov_length(iov, nr_segs) < reqsize)
672 return -EINVAL;
673
674 fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs);
675 err = fuse_copy_one(&cs, &ih, sizeof(ih));
676 if (!err)
677 err = fuse_copy_one(&cs, &arg, sizeof(arg));
678 fuse_copy_finish(&cs);
679
680 return err ? err : reqsize;
681}
682
683/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700684 * Read a single request into the userspace filesystem's buffer. This
685 * function waits until a request is available, then removes it from
686 * the pending list and copies request data to userspace buffer. If
Miklos Szeredif9a28422006-06-25 05:48:53 -0700687 * no reply is needed (FORGET) or request has been aborted or there
688 * was an error during the copying then it's finished by calling
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689 * request_end(). Otherwise add it to the processing list, and set
690 * the 'sent' flag.
691 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700692static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
693 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694{
695 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700696 struct fuse_req *req;
697 struct fuse_in *in;
698 struct fuse_copy_state cs;
699 unsigned reqsize;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700700 struct file *file = iocb->ki_filp;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700701 struct fuse_conn *fc = fuse_get_conn(file);
702 if (!fc)
703 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700704
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800705 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700706 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700707 err = -EAGAIN;
708 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700709 !request_pending(fc))
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700710 goto err_unlock;
711
Miklos Szeredi334f4852005-09-09 13:10:27 -0700712 request_wait(fc);
713 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800714 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700715 goto err_unlock;
716 err = -ERESTARTSYS;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700717 if (!request_pending(fc))
Miklos Szeredi334f4852005-09-09 13:10:27 -0700718 goto err_unlock;
719
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700720 if (!list_empty(&fc->interrupts)) {
721 req = list_entry(fc->interrupts.next, struct fuse_req,
722 intr_entry);
723 return fuse_read_interrupt(fc, req, iov, nr_segs);
724 }
725
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800727 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800728 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700729
730 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800731 reqsize = in->h.len;
732 /* If request is too large, reply with an error and restart the read */
733 if (iov_length(iov, nr_segs) < reqsize) {
734 req->out.h.error = -EIO;
735 /* SETXATTR is special, since it may contain too large data */
736 if (in->h.opcode == FUSE_SETXATTR)
737 req->out.h.error = -E2BIG;
738 request_end(fc, req);
739 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700740 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700741 spin_unlock(&fc->lock);
742 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800743 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
744 if (!err)
745 err = fuse_copy_args(&cs, in->numargs, in->argpages,
746 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700747 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700748 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 req->locked = 0;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700750 if (!err && req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700751 err = -ENOENT;
752 if (err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700753 if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700754 req->out.h.error = -EIO;
755 request_end(fc, req);
756 return err;
757 }
758 if (!req->isreply)
759 request_end(fc, req);
760 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800761 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800762 list_move_tail(&req->list, &fc->processing);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700763 if (req->interrupted)
764 queue_interrupt(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700765 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700766 }
767 return reqsize;
768
769 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700770 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700771 return err;
772}
773
Miklos Szeredi334f4852005-09-09 13:10:27 -0700774/* Look up request on processing list by unique ID */
775static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
776{
777 struct list_head *entry;
778
779 list_for_each(entry, &fc->processing) {
780 struct fuse_req *req;
781 req = list_entry(entry, struct fuse_req, list);
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700782 if (req->in.h.unique == unique || req->intr_unique == unique)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700783 return req;
784 }
785 return NULL;
786}
787
788static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
789 unsigned nbytes)
790{
791 unsigned reqsize = sizeof(struct fuse_out_header);
792
793 if (out->h.error)
794 return nbytes != reqsize ? -EINVAL : 0;
795
796 reqsize += len_args(out->numargs, out->args);
797
798 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
799 return -EINVAL;
800 else if (reqsize > nbytes) {
801 struct fuse_arg *lastarg = &out->args[out->numargs-1];
802 unsigned diffsize = reqsize - nbytes;
803 if (diffsize > lastarg->size)
804 return -EINVAL;
805 lastarg->size -= diffsize;
806 }
807 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
808 out->page_zeroing);
809}
810
811/*
812 * Write a single reply to a request. First the header is copied from
813 * the write buffer. The request is then searched on the processing
814 * list by the unique ID found in the header. If found, then remove
815 * it from the list and copy the rest of the buffer to the request.
816 * The request is finished by calling request_end()
817 */
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700818static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
819 unsigned long nr_segs, loff_t pos)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700820{
821 int err;
822 unsigned nbytes = iov_length(iov, nr_segs);
823 struct fuse_req *req;
824 struct fuse_out_header oh;
825 struct fuse_copy_state cs;
Badari Pulavartyee0b3e62006-09-30 23:28:47 -0700826 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700827 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700828 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700829
Miklos Szeredid7133112006-04-10 22:54:55 -0700830 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700831 if (nbytes < sizeof(struct fuse_out_header))
832 return -EINVAL;
833
834 err = fuse_copy_one(&cs, &oh, sizeof(oh));
835 if (err)
836 goto err_finish;
837 err = -EINVAL;
838 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
839 oh.len != nbytes)
840 goto err_finish;
841
Miklos Szeredid7133112006-04-10 22:54:55 -0700842 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800843 err = -ENOENT;
844 if (!fc->connected)
845 goto err_unlock;
846
Miklos Szeredi334f4852005-09-09 13:10:27 -0700847 req = request_find(fc, oh.unique);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700848 if (!req)
849 goto err_unlock;
850
Miklos Szeredif9a28422006-06-25 05:48:53 -0700851 if (req->aborted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700852 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700853 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700854 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800855 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700856 return -ENOENT;
857 }
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700858 /* Is it an interrupt reply? */
859 if (req->intr_unique == oh.unique) {
860 err = -EINVAL;
861 if (nbytes != sizeof(struct fuse_out_header))
862 goto err_unlock;
863
864 if (oh.error == -ENOSYS)
865 fc->no_interrupt = 1;
866 else if (oh.error == -EAGAIN)
867 queue_interrupt(fc, req);
868
869 spin_unlock(&fc->lock);
870 fuse_copy_finish(&cs);
871 return nbytes;
872 }
873
874 req->state = FUSE_REQ_WRITING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800875 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700876 req->out.h = oh;
877 req->locked = 1;
878 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700879 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700880
881 err = copy_out_args(&cs, &req->out, nbytes);
882 fuse_copy_finish(&cs);
883
Miklos Szeredid7133112006-04-10 22:54:55 -0700884 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700885 req->locked = 0;
886 if (!err) {
Miklos Szeredif9a28422006-06-25 05:48:53 -0700887 if (req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700888 err = -ENOENT;
Miklos Szeredif9a28422006-06-25 05:48:53 -0700889 } else if (!req->aborted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700890 req->out.h.error = -EIO;
891 request_end(fc, req);
892
893 return err ? err : nbytes;
894
895 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700896 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700897 err_finish:
898 fuse_copy_finish(&cs);
899 return err;
900}
901
Miklos Szeredi334f4852005-09-09 13:10:27 -0700902static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
903{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700904 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700905 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700906 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700907 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700908
909 poll_wait(file, &fc->waitq, wait);
910
Miklos Szeredid7133112006-04-10 22:54:55 -0700911 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700912 if (!fc->connected)
913 mask = POLLERR;
Miklos Szeredia4d27e72006-06-25 05:48:54 -0700914 else if (request_pending(fc))
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700915 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700916 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700917
918 return mask;
919}
920
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800921/*
922 * Abort all requests on the given list (pending or processing)
923 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700924 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800925 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700926static void end_requests(struct fuse_conn *fc, struct list_head *head)
927{
928 while (!list_empty(head)) {
929 struct fuse_req *req;
930 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700931 req->out.h.error = -ECONNABORTED;
932 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700933 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700934 }
935}
936
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800937/*
938 * Abort requests under I/O
939 *
Miklos Szeredif9a28422006-06-25 05:48:53 -0700940 * The requests are set to aborted and finished, and the request
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800941 * waiter is woken up. This will make request_wait_answer() wait
942 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800943 *
944 * If the request is asynchronous, then the end function needs to be
945 * called after waiting for the request to be unlocked (if it was
946 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800947 */
948static void end_io_requests(struct fuse_conn *fc)
949{
950 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800951 struct fuse_req *req =
952 list_entry(fc->io.next, struct fuse_req, list);
953 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
954
Miklos Szeredif9a28422006-06-25 05:48:53 -0700955 req->aborted = 1;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800956 req->out.h.error = -ECONNABORTED;
957 req->state = FUSE_REQ_FINISHED;
958 list_del_init(&req->list);
959 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800960 if (end) {
961 req->end = NULL;
962 /* The end function will consume this reference */
963 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700964 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800965 wait_event(req->waitq, !req->locked);
966 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700967 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800968 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800969 }
970}
971
972/*
973 * Abort all requests.
974 *
975 * Emergency exit in case of a malicious or accidental deadlock, or
976 * just a hung filesystem.
977 *
978 * The same effect is usually achievable through killing the
979 * filesystem daemon and all users of the filesystem. The exception
980 * is the combination of an asynchronous request and the tricky
981 * deadlock (see Documentation/filesystems/fuse.txt).
982 *
983 * During the aborting, progression of requests from the pending and
984 * processing lists onto the io list, and progression of new requests
985 * onto the pending list is prevented by req->connected being false.
986 *
987 * Progression of requests under I/O to the processing list is
Miklos Szeredif9a28422006-06-25 05:48:53 -0700988 * prevented by the req->aborted flag being true for these requests.
989 * For this reason requests on the io list must be aborted first.
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800990 */
991void fuse_abort_conn(struct fuse_conn *fc)
992{
Miklos Szeredid7133112006-04-10 22:54:55 -0700993 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800994 if (fc->connected) {
995 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700996 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800997 end_io_requests(fc);
998 end_requests(fc, &fc->pending);
999 end_requests(fc, &fc->processing);
1000 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07001001 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -07001002 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001003 }
Miklos Szeredid7133112006-04-10 22:54:55 -07001004 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -08001005}
1006
Miklos Szeredi334f4852005-09-09 13:10:27 -07001007static int fuse_dev_release(struct inode *inode, struct file *file)
1008{
Miklos Szeredi0720b312006-04-10 22:54:55 -07001009 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001010 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -07001011 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001012 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -07001013 end_requests(fc, &fc->pending);
1014 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -07001015 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -07001016 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredibafa9652006-06-25 05:48:51 -07001017 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -07001018 }
Miklos Szeredif543f252006-01-16 22:14:35 -08001019
Miklos Szeredi334f4852005-09-09 13:10:27 -07001020 return 0;
1021}
1022
Jeff Dike385a17b2006-04-10 22:54:52 -07001023static int fuse_dev_fasync(int fd, struct file *file, int on)
1024{
1025 struct fuse_conn *fc = fuse_get_conn(file);
1026 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -07001027 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -07001028
1029 /* No locking - fasync_helper does its own locking */
1030 return fasync_helper(fd, file, on, &fc->fasync);
1031}
1032
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001033const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -07001034 .owner = THIS_MODULE,
1035 .llseek = no_llseek,
Badari Pulavartyee0b3e62006-09-30 23:28:47 -07001036 .read = do_sync_read,
1037 .aio_read = fuse_dev_read,
1038 .write = do_sync_write,
1039 .aio_write = fuse_dev_write,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001040 .poll = fuse_dev_poll,
1041 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -07001042 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -07001043};
1044
1045static struct miscdevice fuse_miscdevice = {
1046 .minor = FUSE_MINOR,
1047 .name = "fuse",
1048 .fops = &fuse_dev_operations,
1049};
1050
1051int __init fuse_dev_init(void)
1052{
1053 int err = -ENOMEM;
1054 fuse_req_cachep = kmem_cache_create("fuse_request",
1055 sizeof(struct fuse_req),
Paul Mundt20c2df82007-07-20 10:11:58 +09001056 0, 0, NULL);
Miklos Szeredi334f4852005-09-09 13:10:27 -07001057 if (!fuse_req_cachep)
1058 goto out;
1059
1060 err = misc_register(&fuse_miscdevice);
1061 if (err)
1062 goto out_cache_clean;
1063
1064 return 0;
1065
1066 out_cache_clean:
1067 kmem_cache_destroy(fuse_req_cachep);
1068 out:
1069 return err;
1070}
1071
1072void fuse_dev_cleanup(void)
1073{
1074 misc_deregister(&fuse_miscdevice);
1075 kmem_cache_destroy(fuse_req_cachep);
1076}