blob: c510533c684993863a799009f2b74e6d974f785d [file] [log] [blame]
Miklos Szeredi334f4852005-09-09 13:10:27 -07001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "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
22static kmem_cache_t *fuse_req_cachep;
23
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);
37 init_waitqueue_head(&req->waitq);
38 atomic_set(&req->count, 1);
39}
40
41struct fuse_req *fuse_request_alloc(void)
42{
43 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL);
44 if (req)
45 fuse_request_init(req);
46 return req;
47}
48
49void fuse_request_free(struct fuse_req *req)
50{
51 kmem_cache_free(fuse_req_cachep, req);
52}
53
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080054static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070055{
56 sigset_t mask;
57
58 siginitsetinv(&mask, sigmask(SIGKILL));
59 sigprocmask(SIG_BLOCK, &mask, oldset);
60}
61
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080062static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070063{
64 sigprocmask(SIG_SETMASK, oldset, NULL);
65}
66
Miklos Szeredi77e7f252006-02-17 13:52:52 -080067/*
68 * Reset request, so that it can be reused
69 *
70 * The caller must be _very_ careful to make sure, that it is holding
71 * the only reference to req
72 */
Miklos Szeredi334f4852005-09-09 13:10:27 -070073void fuse_reset_request(struct fuse_req *req)
74{
75 int preallocated = req->preallocated;
76 BUG_ON(atomic_read(&req->count) != 1);
77 fuse_request_init(req);
78 req->preallocated = preallocated;
79}
80
81static void __fuse_get_request(struct fuse_req *req)
82{
83 atomic_inc(&req->count);
84}
85
86/* Must be called with > 1 refcount */
87static void __fuse_put_request(struct fuse_req *req)
88{
89 BUG_ON(atomic_read(&req->count) < 2);
90 atomic_dec(&req->count);
91}
92
93static struct fuse_req *do_get_request(struct fuse_conn *fc)
94{
95 struct fuse_req *req;
96
97 spin_lock(&fuse_lock);
98 BUG_ON(list_empty(&fc->unused_list));
99 req = list_entry(fc->unused_list.next, struct fuse_req, list);
100 list_del_init(&req->list);
101 spin_unlock(&fuse_lock);
102 fuse_request_init(req);
103 req->preallocated = 1;
104 req->in.h.uid = current->fsuid;
105 req->in.h.gid = current->fsgid;
106 req->in.h.pid = current->pid;
107 return req;
108}
109
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700110/* This can return NULL, but only in case it's interrupted by a SIGKILL */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700111struct fuse_req *fuse_get_request(struct fuse_conn *fc)
112{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700113 int intr;
114 sigset_t oldset;
115
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800116 atomic_inc(&fc->num_waiting);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700117 block_sigs(&oldset);
118 intr = down_interruptible(&fc->outstanding_sem);
119 restore_sigs(&oldset);
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800120 if (intr) {
121 atomic_dec(&fc->num_waiting);
122 return NULL;
123 }
124 return do_get_request(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700125}
126
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800127/* Must be called with fuse_lock held */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700128static void fuse_putback_request(struct fuse_conn *fc, struct fuse_req *req)
129{
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800130 if (req->preallocated) {
131 atomic_dec(&fc->num_waiting);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700132 list_add(&req->list, &fc->unused_list);
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800133 } else
Miklos Szeredi334f4852005-09-09 13:10:27 -0700134 fuse_request_free(req);
135
136 /* If we are in debt decrease that first */
137 if (fc->outstanding_debt)
138 fc->outstanding_debt--;
139 else
140 up(&fc->outstanding_sem);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700141}
142
143void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
144{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800145 if (atomic_dec_and_test(&req->count)) {
146 spin_lock(&fuse_lock);
147 fuse_putback_request(fc, req);
148 spin_unlock(&fuse_lock);
149 }
150}
151
152static void fuse_put_request_locked(struct fuse_conn *fc, struct fuse_req *req)
153{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700154 if (atomic_dec_and_test(&req->count))
155 fuse_putback_request(fc, req);
156}
157
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700158void fuse_release_background(struct fuse_req *req)
159{
160 iput(req->inode);
161 iput(req->inode2);
162 if (req->file)
163 fput(req->file);
164 spin_lock(&fuse_lock);
165 list_del(&req->bg_entry);
166 spin_unlock(&fuse_lock);
167}
168
Miklos Szeredi334f4852005-09-09 13:10:27 -0700169/*
170 * This function is called when a request is finished. Either a reply
171 * has arrived or it was interrupted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800172 * occurred during communication with userspace, or the device file
173 * was closed. In case of a background request the reference to the
174 * stored objects are released. The requester thread is woken up (if
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800175 * still waiting), the 'end' callback is called if given, else the
176 * reference to the request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700177 *
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800178 * Releasing extra reference for foreground requests must be done
179 * within the same locked region as setting state to finished. This
180 * is because fuse_reset_request() may be called after request is
181 * finished and it must be the sole possessor. If request is
182 * interrupted and put in the background, it will return with an error
183 * and hence never be reset and reused.
184 *
Miklos Szeredi334f4852005-09-09 13:10:27 -0700185 * Called with fuse_lock, unlocks it
186 */
187static void request_end(struct fuse_conn *fc, struct fuse_req *req)
188{
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800189 list_del(&req->list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800190 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800191 if (!req->background) {
192 wake_up(&req->waitq);
193 fuse_put_request_locked(fc, req);
194 spin_unlock(&fuse_lock);
195 } else {
196 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
197 req->end = NULL;
198 spin_unlock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700199 down_read(&fc->sbput_sem);
200 if (fc->mounted)
201 fuse_release_background(req);
202 up_read(&fc->sbput_sem);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800203 if (end)
204 end(fc, req);
205 else
206 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700207 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700208}
209
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700210/*
211 * Unfortunately request interruption not just solves the deadlock
212 * problem, it causes problems too. These stem from the fact, that an
213 * interrupted request is continued to be processed in userspace,
214 * while all the locks and object references (inode and file) held
215 * during the operation are released.
216 *
217 * To release the locks is exactly why there's a need to interrupt the
218 * request, so there's not a lot that can be done about this, except
219 * introduce additional locking in userspace.
220 *
221 * More important is to keep inode and file references until userspace
222 * has replied, otherwise FORGET and RELEASE could be sent while the
223 * inode/file is still used by the filesystem.
224 *
225 * For this reason the concept of "background" request is introduced.
226 * An interrupted request is backgrounded if it has been already sent
227 * to userspace. Backgrounding involves getting an extra reference to
228 * inode(s) or file used in the request, and adding the request to
229 * fc->background list. When a reply is received for a background
230 * request, the object references are released, and the request is
231 * removed from the list. If the filesystem is unmounted while there
232 * are still background requests, the list is walked and references
233 * are released as if a reply was received.
234 *
235 * There's one more use for a background request. The RELEASE message is
236 * always sent as background, since it doesn't return an error or
237 * data.
238 */
239static void background_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700240{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700241 req->background = 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700242 list_add(&req->bg_entry, &fc->background);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700243 if (req->inode)
244 req->inode = igrab(req->inode);
245 if (req->inode2)
246 req->inode2 = igrab(req->inode2);
247 if (req->file)
248 get_file(req->file);
249}
250
Miklos Szeredi334f4852005-09-09 13:10:27 -0700251/* Called with fuse_lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700252static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700253{
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700254 sigset_t oldset;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700255
256 spin_unlock(&fuse_lock);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700257 block_sigs(&oldset);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800258 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700259 restore_sigs(&oldset);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700260 spin_lock(&fuse_lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800261 if (req->state == FUSE_REQ_FINISHED && !req->interrupted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700262 return;
263
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800264 if (!req->interrupted) {
265 req->out.h.error = -EINTR;
266 req->interrupted = 1;
267 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700268 if (req->locked) {
269 /* This is uninterruptible sleep, because data is
270 being copied to/from the buffers of req. During
271 locked state, there mustn't be any filesystem
272 operation (e.g. page fault), since that could lead
273 to deadlock */
274 spin_unlock(&fuse_lock);
275 wait_event(req->waitq, !req->locked);
276 spin_lock(&fuse_lock);
277 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800278 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700279 list_del(&req->list);
280 __fuse_put_request(req);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800281 } else if (req->state == FUSE_REQ_SENT)
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700282 background_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700283}
284
285static unsigned len_args(unsigned numargs, struct fuse_arg *args)
286{
287 unsigned nbytes = 0;
288 unsigned i;
289
290 for (i = 0; i < numargs; i++)
291 nbytes += args[i].size;
292
293 return nbytes;
294}
295
296static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
297{
298 fc->reqctr++;
299 /* zero is special */
300 if (fc->reqctr == 0)
301 fc->reqctr = 1;
302 req->in.h.unique = fc->reqctr;
303 req->in.h.len = sizeof(struct fuse_in_header) +
304 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
305 if (!req->preallocated) {
306 /* If request is not preallocated (either FORGET or
307 RELEASE), then still decrease outstanding_sem, so
308 user can't open infinite number of files while not
309 processing the RELEASE requests. However for
310 efficiency do it without blocking, so if down()
311 would block, just increase the debt instead */
312 if (down_trylock(&fc->outstanding_sem))
313 fc->outstanding_debt++;
314 }
315 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800316 req->state = FUSE_REQ_PENDING;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700317 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700318 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700319}
320
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700321/*
322 * This can only be interrupted by a SIGKILL
323 */
324void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700325{
326 req->isreply = 1;
327 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700328 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700329 req->out.h.error = -ENOTCONN;
330 else if (fc->conn_error)
331 req->out.h.error = -ECONNREFUSED;
332 else {
333 queue_request(fc, req);
334 /* acquire extra reference, since request is still needed
335 after request_end() */
336 __fuse_get_request(req);
337
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700338 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700339 }
340 spin_unlock(&fuse_lock);
341}
342
Miklos Szeredi334f4852005-09-09 13:10:27 -0700343static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
344{
345 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700346 if (fc->connected) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700347 queue_request(fc, req);
348 spin_unlock(&fuse_lock);
349 } else {
350 req->out.h.error = -ENOTCONN;
351 request_end(fc, req);
352 }
353}
354
355void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
356{
357 req->isreply = 0;
358 request_send_nowait(fc, req);
359}
360
361void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
362{
363 req->isreply = 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700364 spin_lock(&fuse_lock);
365 background_request(fc, req);
366 spin_unlock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700367 request_send_nowait(fc, req);
368}
369
Miklos Szeredi334f4852005-09-09 13:10:27 -0700370/*
371 * Lock the request. Up to the next unlock_request() there mustn't be
372 * anything that could cause a page-fault. If the request was already
373 * interrupted bail out.
374 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800375static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700376{
377 int err = 0;
378 if (req) {
379 spin_lock(&fuse_lock);
380 if (req->interrupted)
381 err = -ENOENT;
382 else
383 req->locked = 1;
384 spin_unlock(&fuse_lock);
385 }
386 return err;
387}
388
389/*
390 * Unlock request. If it was interrupted during being locked, the
391 * requester thread is currently waiting for it to be unlocked, so
392 * wake it up.
393 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800394static void unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700395{
396 if (req) {
397 spin_lock(&fuse_lock);
398 req->locked = 0;
399 if (req->interrupted)
400 wake_up(&req->waitq);
401 spin_unlock(&fuse_lock);
402 }
403}
404
405struct fuse_copy_state {
406 int write;
407 struct fuse_req *req;
408 const struct iovec *iov;
409 unsigned long nr_segs;
410 unsigned long seglen;
411 unsigned long addr;
412 struct page *pg;
413 void *mapaddr;
414 void *buf;
415 unsigned len;
416};
417
418static void fuse_copy_init(struct fuse_copy_state *cs, int write,
419 struct fuse_req *req, const struct iovec *iov,
420 unsigned long nr_segs)
421{
422 memset(cs, 0, sizeof(*cs));
423 cs->write = write;
424 cs->req = req;
425 cs->iov = iov;
426 cs->nr_segs = nr_segs;
427}
428
429/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800430static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700431{
432 if (cs->mapaddr) {
433 kunmap_atomic(cs->mapaddr, KM_USER0);
434 if (cs->write) {
435 flush_dcache_page(cs->pg);
436 set_page_dirty_lock(cs->pg);
437 }
438 put_page(cs->pg);
439 cs->mapaddr = NULL;
440 }
441}
442
443/*
444 * Get another pagefull of userspace buffer, and map it to kernel
445 * address space, and lock request
446 */
447static int fuse_copy_fill(struct fuse_copy_state *cs)
448{
449 unsigned long offset;
450 int err;
451
452 unlock_request(cs->req);
453 fuse_copy_finish(cs);
454 if (!cs->seglen) {
455 BUG_ON(!cs->nr_segs);
456 cs->seglen = cs->iov[0].iov_len;
457 cs->addr = (unsigned long) cs->iov[0].iov_base;
458 cs->iov ++;
459 cs->nr_segs --;
460 }
461 down_read(&current->mm->mmap_sem);
462 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
463 &cs->pg, NULL);
464 up_read(&current->mm->mmap_sem);
465 if (err < 0)
466 return err;
467 BUG_ON(err != 1);
468 offset = cs->addr % PAGE_SIZE;
469 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
470 cs->buf = cs->mapaddr + offset;
471 cs->len = min(PAGE_SIZE - offset, cs->seglen);
472 cs->seglen -= cs->len;
473 cs->addr += cs->len;
474
475 return lock_request(cs->req);
476}
477
478/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800479static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700480{
481 unsigned ncpy = min(*size, cs->len);
482 if (val) {
483 if (cs->write)
484 memcpy(cs->buf, *val, ncpy);
485 else
486 memcpy(*val, cs->buf, ncpy);
487 *val += ncpy;
488 }
489 *size -= ncpy;
490 cs->len -= ncpy;
491 cs->buf += ncpy;
492 return ncpy;
493}
494
495/*
496 * Copy a page in the request to/from the userspace buffer. Must be
497 * done atomically
498 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800499static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
500 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700501{
502 if (page && zeroing && count < PAGE_SIZE) {
503 void *mapaddr = kmap_atomic(page, KM_USER1);
504 memset(mapaddr, 0, PAGE_SIZE);
505 kunmap_atomic(mapaddr, KM_USER1);
506 }
507 while (count) {
508 int err;
509 if (!cs->len && (err = fuse_copy_fill(cs)))
510 return err;
511 if (page) {
512 void *mapaddr = kmap_atomic(page, KM_USER1);
513 void *buf = mapaddr + offset;
514 offset += fuse_copy_do(cs, &buf, &count);
515 kunmap_atomic(mapaddr, KM_USER1);
516 } else
517 offset += fuse_copy_do(cs, NULL, &count);
518 }
519 if (page && !cs->write)
520 flush_dcache_page(page);
521 return 0;
522}
523
524/* Copy pages in the request to/from userspace buffer */
525static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
526 int zeroing)
527{
528 unsigned i;
529 struct fuse_req *req = cs->req;
530 unsigned offset = req->page_offset;
531 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
532
533 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
534 struct page *page = req->pages[i];
535 int err = fuse_copy_page(cs, page, offset, count, zeroing);
536 if (err)
537 return err;
538
539 nbytes -= count;
540 count = min(nbytes, (unsigned) PAGE_SIZE);
541 offset = 0;
542 }
543 return 0;
544}
545
546/* Copy a single argument in the request to/from userspace buffer */
547static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
548{
549 while (size) {
550 int err;
551 if (!cs->len && (err = fuse_copy_fill(cs)))
552 return err;
553 fuse_copy_do(cs, &val, &size);
554 }
555 return 0;
556}
557
558/* Copy request arguments to/from userspace buffer */
559static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
560 unsigned argpages, struct fuse_arg *args,
561 int zeroing)
562{
563 int err = 0;
564 unsigned i;
565
566 for (i = 0; !err && i < numargs; i++) {
567 struct fuse_arg *arg = &args[i];
568 if (i == numargs - 1 && argpages)
569 err = fuse_copy_pages(cs, arg->size, zeroing);
570 else
571 err = fuse_copy_one(cs, arg->value, arg->size);
572 }
573 return err;
574}
575
576/* Wait until a request is available on the pending list */
577static void request_wait(struct fuse_conn *fc)
578{
579 DECLARE_WAITQUEUE(wait, current);
580
581 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800582 while (fc->connected && list_empty(&fc->pending)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700583 set_current_state(TASK_INTERRUPTIBLE);
584 if (signal_pending(current))
585 break;
586
587 spin_unlock(&fuse_lock);
588 schedule();
589 spin_lock(&fuse_lock);
590 }
591 set_current_state(TASK_RUNNING);
592 remove_wait_queue(&fc->waitq, &wait);
593}
594
595/*
596 * Read a single request into the userspace filesystem's buffer. This
597 * function waits until a request is available, then removes it from
598 * the pending list and copies request data to userspace buffer. If
599 * no reply is needed (FORGET) or request has been interrupted or
600 * there was an error during the copying then it's finished by calling
601 * request_end(). Otherwise add it to the processing list, and set
602 * the 'sent' flag.
603 */
604static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
605 unsigned long nr_segs, loff_t *off)
606{
607 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700608 struct fuse_req *req;
609 struct fuse_in *in;
610 struct fuse_copy_state cs;
611 unsigned reqsize;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700612 struct fuse_conn *fc = fuse_get_conn(file);
613 if (!fc)
614 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700615
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800616 restart:
Miklos Szeredi334f4852005-09-09 13:10:27 -0700617 spin_lock(&fuse_lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700618 err = -EAGAIN;
619 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
620 list_empty(&fc->pending))
621 goto err_unlock;
622
Miklos Szeredi334f4852005-09-09 13:10:27 -0700623 request_wait(fc);
624 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800625 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700626 goto err_unlock;
627 err = -ERESTARTSYS;
628 if (list_empty(&fc->pending))
629 goto err_unlock;
630
631 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800632 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800633 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700634
635 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800636 reqsize = in->h.len;
637 /* If request is too large, reply with an error and restart the read */
638 if (iov_length(iov, nr_segs) < reqsize) {
639 req->out.h.error = -EIO;
640 /* SETXATTR is special, since it may contain too large data */
641 if (in->h.opcode == FUSE_SETXATTR)
642 req->out.h.error = -E2BIG;
643 request_end(fc, req);
644 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700645 }
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800646 spin_unlock(&fuse_lock);
647 fuse_copy_init(&cs, 1, req, iov, nr_segs);
648 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
649 if (!err)
650 err = fuse_copy_args(&cs, in->numargs, in->argpages,
651 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700652 fuse_copy_finish(&cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700653 spin_lock(&fuse_lock);
654 req->locked = 0;
655 if (!err && req->interrupted)
656 err = -ENOENT;
657 if (err) {
658 if (!req->interrupted)
659 req->out.h.error = -EIO;
660 request_end(fc, req);
661 return err;
662 }
663 if (!req->isreply)
664 request_end(fc, req);
665 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800666 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800667 list_move_tail(&req->list, &fc->processing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700668 spin_unlock(&fuse_lock);
669 }
670 return reqsize;
671
672 err_unlock:
673 spin_unlock(&fuse_lock);
674 return err;
675}
676
677static ssize_t fuse_dev_read(struct file *file, char __user *buf,
678 size_t nbytes, loff_t *off)
679{
680 struct iovec iov;
681 iov.iov_len = nbytes;
682 iov.iov_base = buf;
683 return fuse_dev_readv(file, &iov, 1, off);
684}
685
686/* Look up request on processing list by unique ID */
687static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
688{
689 struct list_head *entry;
690
691 list_for_each(entry, &fc->processing) {
692 struct fuse_req *req;
693 req = list_entry(entry, struct fuse_req, list);
694 if (req->in.h.unique == unique)
695 return req;
696 }
697 return NULL;
698}
699
700static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
701 unsigned nbytes)
702{
703 unsigned reqsize = sizeof(struct fuse_out_header);
704
705 if (out->h.error)
706 return nbytes != reqsize ? -EINVAL : 0;
707
708 reqsize += len_args(out->numargs, out->args);
709
710 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
711 return -EINVAL;
712 else if (reqsize > nbytes) {
713 struct fuse_arg *lastarg = &out->args[out->numargs-1];
714 unsigned diffsize = reqsize - nbytes;
715 if (diffsize > lastarg->size)
716 return -EINVAL;
717 lastarg->size -= diffsize;
718 }
719 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
720 out->page_zeroing);
721}
722
723/*
724 * Write a single reply to a request. First the header is copied from
725 * the write buffer. The request is then searched on the processing
726 * list by the unique ID found in the header. If found, then remove
727 * it from the list and copy the rest of the buffer to the request.
728 * The request is finished by calling request_end()
729 */
730static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
731 unsigned long nr_segs, loff_t *off)
732{
733 int err;
734 unsigned nbytes = iov_length(iov, nr_segs);
735 struct fuse_req *req;
736 struct fuse_out_header oh;
737 struct fuse_copy_state cs;
738 struct fuse_conn *fc = fuse_get_conn(file);
739 if (!fc)
740 return -ENODEV;
741
742 fuse_copy_init(&cs, 0, NULL, iov, nr_segs);
743 if (nbytes < sizeof(struct fuse_out_header))
744 return -EINVAL;
745
746 err = fuse_copy_one(&cs, &oh, sizeof(oh));
747 if (err)
748 goto err_finish;
749 err = -EINVAL;
750 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
751 oh.len != nbytes)
752 goto err_finish;
753
754 spin_lock(&fuse_lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800755 err = -ENOENT;
756 if (!fc->connected)
757 goto err_unlock;
758
Miklos Szeredi334f4852005-09-09 13:10:27 -0700759 req = request_find(fc, oh.unique);
760 err = -EINVAL;
761 if (!req)
762 goto err_unlock;
763
Miklos Szeredi334f4852005-09-09 13:10:27 -0700764 if (req->interrupted) {
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800765 spin_unlock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700766 fuse_copy_finish(&cs);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800767 spin_lock(&fuse_lock);
768 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700769 return -ENOENT;
770 }
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800771 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700772 req->out.h = oh;
773 req->locked = 1;
774 cs.req = req;
775 spin_unlock(&fuse_lock);
776
777 err = copy_out_args(&cs, &req->out, nbytes);
778 fuse_copy_finish(&cs);
779
780 spin_lock(&fuse_lock);
781 req->locked = 0;
782 if (!err) {
783 if (req->interrupted)
784 err = -ENOENT;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700785 } else if (!req->interrupted)
786 req->out.h.error = -EIO;
787 request_end(fc, req);
788
789 return err ? err : nbytes;
790
791 err_unlock:
792 spin_unlock(&fuse_lock);
793 err_finish:
794 fuse_copy_finish(&cs);
795 return err;
796}
797
798static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
799 size_t nbytes, loff_t *off)
800{
801 struct iovec iov;
802 iov.iov_len = nbytes;
803 iov.iov_base = (char __user *) buf;
804 return fuse_dev_writev(file, &iov, 1, off);
805}
806
807static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
808{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700809 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700810 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700811 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700812 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700813
814 poll_wait(file, &fc->waitq, wait);
815
816 spin_lock(&fuse_lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700817 if (!fc->connected)
818 mask = POLLERR;
819 else if (!list_empty(&fc->pending))
820 mask |= POLLIN | POLLRDNORM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700821 spin_unlock(&fuse_lock);
822
823 return mask;
824}
825
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800826/*
827 * Abort all requests on the given list (pending or processing)
828 *
829 * This function releases and reacquires fuse_lock
830 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700831static void end_requests(struct fuse_conn *fc, struct list_head *head)
832{
833 while (!list_empty(head)) {
834 struct fuse_req *req;
835 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700836 req->out.h.error = -ECONNABORTED;
837 request_end(fc, req);
838 spin_lock(&fuse_lock);
839 }
840}
841
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800842/*
843 * Abort requests under I/O
844 *
845 * The requests are set to interrupted and finished, and the request
846 * waiter is woken up. This will make request_wait_answer() wait
847 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800848 *
849 * If the request is asynchronous, then the end function needs to be
850 * called after waiting for the request to be unlocked (if it was
851 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800852 */
853static void end_io_requests(struct fuse_conn *fc)
854{
855 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800856 struct fuse_req *req =
857 list_entry(fc->io.next, struct fuse_req, list);
858 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
859
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800860 req->interrupted = 1;
861 req->out.h.error = -ECONNABORTED;
862 req->state = FUSE_REQ_FINISHED;
863 list_del_init(&req->list);
864 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800865 if (end) {
866 req->end = NULL;
867 /* The end function will consume this reference */
868 __fuse_get_request(req);
869 spin_unlock(&fuse_lock);
870 wait_event(req->waitq, !req->locked);
871 end(fc, req);
872 spin_lock(&fuse_lock);
873 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800874 }
875}
876
877/*
878 * Abort all requests.
879 *
880 * Emergency exit in case of a malicious or accidental deadlock, or
881 * just a hung filesystem.
882 *
883 * The same effect is usually achievable through killing the
884 * filesystem daemon and all users of the filesystem. The exception
885 * is the combination of an asynchronous request and the tricky
886 * deadlock (see Documentation/filesystems/fuse.txt).
887 *
888 * During the aborting, progression of requests from the pending and
889 * processing lists onto the io list, and progression of new requests
890 * onto the pending list is prevented by req->connected being false.
891 *
892 * Progression of requests under I/O to the processing list is
893 * prevented by the req->interrupted flag being true for these
894 * requests. For this reason requests on the io list must be aborted
895 * first.
896 */
897void fuse_abort_conn(struct fuse_conn *fc)
898{
899 spin_lock(&fuse_lock);
900 if (fc->connected) {
901 fc->connected = 0;
902 end_io_requests(fc);
903 end_requests(fc, &fc->pending);
904 end_requests(fc, &fc->processing);
905 wake_up_all(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700906 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800907 }
908 spin_unlock(&fuse_lock);
909}
910
Miklos Szeredi334f4852005-09-09 13:10:27 -0700911static int fuse_dev_release(struct inode *inode, struct file *file)
912{
Miklos Szeredi0720b312006-04-10 22:54:55 -0700913 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700914 if (fc) {
Miklos Szeredi0720b312006-04-10 22:54:55 -0700915 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700916 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700917 end_requests(fc, &fc->pending);
918 end_requests(fc, &fc->processing);
Miklos Szeredi0720b312006-04-10 22:54:55 -0700919 spin_unlock(&fuse_lock);
Jeff Dike385a17b2006-04-10 22:54:52 -0700920 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredif543f252006-01-16 22:14:35 -0800921 kobject_put(&fc->kobj);
Jeff Dike385a17b2006-04-10 22:54:52 -0700922 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800923
Miklos Szeredi334f4852005-09-09 13:10:27 -0700924 return 0;
925}
926
Jeff Dike385a17b2006-04-10 22:54:52 -0700927static int fuse_dev_fasync(int fd, struct file *file, int on)
928{
929 struct fuse_conn *fc = fuse_get_conn(file);
930 if (!fc)
931 return -ENODEV;
932
933 /* No locking - fasync_helper does its own locking */
934 return fasync_helper(fd, file, on, &fc->fasync);
935}
936
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800937const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700938 .owner = THIS_MODULE,
939 .llseek = no_llseek,
940 .read = fuse_dev_read,
941 .readv = fuse_dev_readv,
942 .write = fuse_dev_write,
943 .writev = fuse_dev_writev,
944 .poll = fuse_dev_poll,
945 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -0700946 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -0700947};
948
949static struct miscdevice fuse_miscdevice = {
950 .minor = FUSE_MINOR,
951 .name = "fuse",
952 .fops = &fuse_dev_operations,
953};
954
955int __init fuse_dev_init(void)
956{
957 int err = -ENOMEM;
958 fuse_req_cachep = kmem_cache_create("fuse_request",
959 sizeof(struct fuse_req),
960 0, 0, NULL, NULL);
961 if (!fuse_req_cachep)
962 goto out;
963
964 err = misc_register(&fuse_miscdevice);
965 if (err)
966 goto out_cache_clean;
967
968 return 0;
969
970 out_cache_clean:
971 kmem_cache_destroy(fuse_req_cachep);
972 out:
973 return err;
974}
975
976void fuse_dev_cleanup(void)
977{
978 misc_deregister(&fuse_miscdevice);
979 kmem_cache_destroy(fuse_req_cachep);
980}