blob: fe3adf589174dfda727348d9fe92add73d4a0088 [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
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 Szeredi334f4852005-09-09 13:10:27 -070067static void __fuse_get_request(struct fuse_req *req)
68{
69 atomic_inc(&req->count);
70}
71
72/* Must be called with > 1 refcount */
73static void __fuse_put_request(struct fuse_req *req)
74{
75 BUG_ON(atomic_read(&req->count) < 2);
76 atomic_dec(&req->count);
77}
78
Miklos Szeredice1d5a42006-04-10 22:54:58 -070079struct fuse_req *fuse_get_req(struct fuse_conn *fc)
Miklos Szeredi334f4852005-09-09 13:10:27 -070080{
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070081 struct fuse_req *req;
82 sigset_t oldset;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020083 int intr;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070084 int err;
85
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020086 atomic_inc(&fc->num_waiting);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070087 block_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020088 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070089 restore_sigs(&oldset);
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020090 err = -EINTR;
91 if (intr)
92 goto out;
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070093
Miklos Szeredi51eb01e2006-06-25 05:48:50 -070094 err = -ENOTCONN;
95 if (!fc->connected)
96 goto out;
97
Miklos Szeredi08a53cd2006-04-10 22:54:59 -070098 req = fuse_request_alloc();
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +020099 err = -ENOMEM;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700100 if (!req)
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200101 goto out;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700102
Miklos Szeredi334f4852005-09-09 13:10:27 -0700103 req->in.h.uid = current->fsuid;
104 req->in.h.gid = current->fsgid;
105 req->in.h.pid = current->pid;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200106 req->waiting = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700107 return req;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200108
109 out:
110 atomic_dec(&fc->num_waiting);
111 return ERR_PTR(err);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700112}
113
Miklos Szeredi334f4852005-09-09 13:10:27 -0700114void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
115{
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800116 if (atomic_dec_and_test(&req->count)) {
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200117 if (req->waiting)
118 atomic_dec(&fc->num_waiting);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700119 fuse_request_free(req);
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800120 }
121}
122
Miklos Szeredi6dbbcb12006-04-26 10:49:06 +0200123/*
Miklos Szeredi334f4852005-09-09 13:10:27 -0700124 * This function is called when a request is finished. Either a reply
125 * has arrived or it was interrupted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800126 * occurred during communication with userspace, or the device file
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700127 * was closed. The requester thread is woken up (if still waiting),
128 * the 'end' callback is called if given, else the reference to the
129 * request is released
Miklos Szeredi7128ec22006-02-04 23:27:40 -0800130 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700131 * Called with fc->lock, unlocks it
Miklos Szeredi334f4852005-09-09 13:10:27 -0700132 */
133static void request_end(struct fuse_conn *fc, struct fuse_req *req)
134{
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700135 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
136 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800137 list_del(&req->list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800138 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700139 if (req->background) {
140 if (fc->num_background == FUSE_MAX_BACKGROUND) {
141 fc->blocked = 0;
142 wake_up_all(&fc->blocked_waitq);
143 }
144 fc->num_background--;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700145 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700146 spin_unlock(&fc->lock);
147 dput(req->dentry);
148 mntput(req->vfsmount);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700149 if (req->file)
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700150 fput(req->file);
151 wake_up(&req->waitq);
152 if (end)
153 end(fc, req);
154 else
155 fuse_put_request(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700156}
157
Miklos Szeredid7133112006-04-10 22:54:55 -0700158/* Called with fc->lock held. Releases, and then reacquires it. */
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700159static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700160{
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700161 sigset_t oldset;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700162
Miklos Szeredid7133112006-04-10 22:54:55 -0700163 spin_unlock(&fc->lock);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700164 if (req->force)
165 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
166 else {
167 block_sigs(&oldset);
168 wait_event_interruptible(req->waitq,
169 req->state == FUSE_REQ_FINISHED);
170 restore_sigs(&oldset);
171 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700172 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800173 if (req->state == FUSE_REQ_FINISHED && !req->interrupted)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700174 return;
175
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800176 if (!req->interrupted) {
177 req->out.h.error = -EINTR;
178 req->interrupted = 1;
179 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700180 if (req->locked) {
181 /* This is uninterruptible sleep, because data is
182 being copied to/from the buffers of req. During
183 locked state, there mustn't be any filesystem
184 operation (e.g. page fault), since that could lead
185 to deadlock */
Miklos Szeredid7133112006-04-10 22:54:55 -0700186 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700187 wait_event(req->waitq, !req->locked);
Miklos Szeredid7133112006-04-10 22:54:55 -0700188 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700189 }
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800190 if (req->state == FUSE_REQ_PENDING) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700191 list_del(&req->list);
192 __fuse_put_request(req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700193 } else if (req->state == FUSE_REQ_SENT) {
194 spin_unlock(&fc->lock);
195 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
196 spin_lock(&fc->lock);
197 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700198}
199
200static unsigned len_args(unsigned numargs, struct fuse_arg *args)
201{
202 unsigned nbytes = 0;
203 unsigned i;
204
205 for (i = 0; i < numargs; i++)
206 nbytes += args[i].size;
207
208 return nbytes;
209}
210
211static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
212{
213 fc->reqctr++;
214 /* zero is special */
215 if (fc->reqctr == 0)
216 fc->reqctr = 1;
217 req->in.h.unique = fc->reqctr;
218 req->in.h.len = sizeof(struct fuse_in_header) +
219 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700220 list_add_tail(&req->list, &fc->pending);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800221 req->state = FUSE_REQ_PENDING;
Miklos Szeredi9bc5ddd2006-04-11 21:16:09 +0200222 if (!req->waiting) {
223 req->waiting = 1;
224 atomic_inc(&fc->num_waiting);
225 }
Miklos Szeredi334f4852005-09-09 13:10:27 -0700226 wake_up(&fc->waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700227 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700228}
229
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700230/*
231 * This can only be interrupted by a SIGKILL
232 */
233void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700234{
235 req->isreply = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700236 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700237 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700238 req->out.h.error = -ENOTCONN;
239 else if (fc->conn_error)
240 req->out.h.error = -ECONNREFUSED;
241 else {
242 queue_request(fc, req);
243 /* acquire extra reference, since request is still needed
244 after request_end() */
245 __fuse_get_request(req);
246
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700247 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700248 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700249 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700250}
251
Miklos Szeredi334f4852005-09-09 13:10:27 -0700252static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
253{
Miklos Szeredid7133112006-04-10 22:54:55 -0700254 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700255 if (fc->connected) {
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700256 req->background = 1;
257 fc->num_background++;
258 if (fc->num_background == FUSE_MAX_BACKGROUND)
259 fc->blocked = 1;
260
Miklos Szeredi334f4852005-09-09 13:10:27 -0700261 queue_request(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700262 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700263 } else {
264 req->out.h.error = -ENOTCONN;
265 request_end(fc, req);
266 }
267}
268
269void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
270{
271 req->isreply = 0;
272 request_send_nowait(fc, req);
273}
274
275void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
276{
277 req->isreply = 1;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700278 request_send_nowait(fc, req);
279}
280
Miklos Szeredi334f4852005-09-09 13:10:27 -0700281/*
282 * Lock the request. Up to the next unlock_request() there mustn't be
283 * anything that could cause a page-fault. If the request was already
284 * interrupted bail out.
285 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700286static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700287{
288 int err = 0;
289 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700290 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700291 if (req->interrupted)
292 err = -ENOENT;
293 else
294 req->locked = 1;
Miklos Szeredid7133112006-04-10 22:54:55 -0700295 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700296 }
297 return err;
298}
299
300/*
301 * Unlock request. If it was interrupted during being locked, the
302 * requester thread is currently waiting for it to be unlocked, so
303 * wake it up.
304 */
Miklos Szeredid7133112006-04-10 22:54:55 -0700305static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700306{
307 if (req) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700308 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700309 req->locked = 0;
310 if (req->interrupted)
311 wake_up(&req->waitq);
Miklos Szeredid7133112006-04-10 22:54:55 -0700312 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700313 }
314}
315
316struct fuse_copy_state {
Miklos Szeredid7133112006-04-10 22:54:55 -0700317 struct fuse_conn *fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700318 int write;
319 struct fuse_req *req;
320 const struct iovec *iov;
321 unsigned long nr_segs;
322 unsigned long seglen;
323 unsigned long addr;
324 struct page *pg;
325 void *mapaddr;
326 void *buf;
327 unsigned len;
328};
329
Miklos Szeredid7133112006-04-10 22:54:55 -0700330static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
331 int write, struct fuse_req *req,
332 const struct iovec *iov, unsigned long nr_segs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700333{
334 memset(cs, 0, sizeof(*cs));
Miklos Szeredid7133112006-04-10 22:54:55 -0700335 cs->fc = fc;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700336 cs->write = write;
337 cs->req = req;
338 cs->iov = iov;
339 cs->nr_segs = nr_segs;
340}
341
342/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800343static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700344{
345 if (cs->mapaddr) {
346 kunmap_atomic(cs->mapaddr, KM_USER0);
347 if (cs->write) {
348 flush_dcache_page(cs->pg);
349 set_page_dirty_lock(cs->pg);
350 }
351 put_page(cs->pg);
352 cs->mapaddr = NULL;
353 }
354}
355
356/*
357 * Get another pagefull of userspace buffer, and map it to kernel
358 * address space, and lock request
359 */
360static int fuse_copy_fill(struct fuse_copy_state *cs)
361{
362 unsigned long offset;
363 int err;
364
Miklos Szeredid7133112006-04-10 22:54:55 -0700365 unlock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700366 fuse_copy_finish(cs);
367 if (!cs->seglen) {
368 BUG_ON(!cs->nr_segs);
369 cs->seglen = cs->iov[0].iov_len;
370 cs->addr = (unsigned long) cs->iov[0].iov_base;
371 cs->iov ++;
372 cs->nr_segs --;
373 }
374 down_read(&current->mm->mmap_sem);
375 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
376 &cs->pg, NULL);
377 up_read(&current->mm->mmap_sem);
378 if (err < 0)
379 return err;
380 BUG_ON(err != 1);
381 offset = cs->addr % PAGE_SIZE;
382 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
383 cs->buf = cs->mapaddr + offset;
384 cs->len = min(PAGE_SIZE - offset, cs->seglen);
385 cs->seglen -= cs->len;
386 cs->addr += cs->len;
387
Miklos Szeredid7133112006-04-10 22:54:55 -0700388 return lock_request(cs->fc, cs->req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700389}
390
391/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800392static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700393{
394 unsigned ncpy = min(*size, cs->len);
395 if (val) {
396 if (cs->write)
397 memcpy(cs->buf, *val, ncpy);
398 else
399 memcpy(*val, cs->buf, ncpy);
400 *val += ncpy;
401 }
402 *size -= ncpy;
403 cs->len -= ncpy;
404 cs->buf += ncpy;
405 return ncpy;
406}
407
408/*
409 * Copy a page in the request to/from the userspace buffer. Must be
410 * done atomically
411 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800412static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
413 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700414{
415 if (page && zeroing && count < PAGE_SIZE) {
416 void *mapaddr = kmap_atomic(page, KM_USER1);
417 memset(mapaddr, 0, PAGE_SIZE);
418 kunmap_atomic(mapaddr, KM_USER1);
419 }
420 while (count) {
421 int err;
422 if (!cs->len && (err = fuse_copy_fill(cs)))
423 return err;
424 if (page) {
425 void *mapaddr = kmap_atomic(page, KM_USER1);
426 void *buf = mapaddr + offset;
427 offset += fuse_copy_do(cs, &buf, &count);
428 kunmap_atomic(mapaddr, KM_USER1);
429 } else
430 offset += fuse_copy_do(cs, NULL, &count);
431 }
432 if (page && !cs->write)
433 flush_dcache_page(page);
434 return 0;
435}
436
437/* Copy pages in the request to/from userspace buffer */
438static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
439 int zeroing)
440{
441 unsigned i;
442 struct fuse_req *req = cs->req;
443 unsigned offset = req->page_offset;
444 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
445
446 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
447 struct page *page = req->pages[i];
448 int err = fuse_copy_page(cs, page, offset, count, zeroing);
449 if (err)
450 return err;
451
452 nbytes -= count;
453 count = min(nbytes, (unsigned) PAGE_SIZE);
454 offset = 0;
455 }
456 return 0;
457}
458
459/* Copy a single argument in the request to/from userspace buffer */
460static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
461{
462 while (size) {
463 int err;
464 if (!cs->len && (err = fuse_copy_fill(cs)))
465 return err;
466 fuse_copy_do(cs, &val, &size);
467 }
468 return 0;
469}
470
471/* Copy request arguments to/from userspace buffer */
472static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
473 unsigned argpages, struct fuse_arg *args,
474 int zeroing)
475{
476 int err = 0;
477 unsigned i;
478
479 for (i = 0; !err && i < numargs; i++) {
480 struct fuse_arg *arg = &args[i];
481 if (i == numargs - 1 && argpages)
482 err = fuse_copy_pages(cs, arg->size, zeroing);
483 else
484 err = fuse_copy_one(cs, arg->value, arg->size);
485 }
486 return err;
487}
488
489/* Wait until a request is available on the pending list */
490static void request_wait(struct fuse_conn *fc)
491{
492 DECLARE_WAITQUEUE(wait, current);
493
494 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800495 while (fc->connected && list_empty(&fc->pending)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700496 set_current_state(TASK_INTERRUPTIBLE);
497 if (signal_pending(current))
498 break;
499
Miklos Szeredid7133112006-04-10 22:54:55 -0700500 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700501 schedule();
Miklos Szeredid7133112006-04-10 22:54:55 -0700502 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700503 }
504 set_current_state(TASK_RUNNING);
505 remove_wait_queue(&fc->waitq, &wait);
506}
507
508/*
509 * Read a single request into the userspace filesystem's buffer. This
510 * function waits until a request is available, then removes it from
511 * the pending list and copies request data to userspace buffer. If
512 * no reply is needed (FORGET) or request has been interrupted or
513 * there was an error during the copying then it's finished by calling
514 * request_end(). Otherwise add it to the processing list, and set
515 * the 'sent' flag.
516 */
517static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
518 unsigned long nr_segs, loff_t *off)
519{
520 int err;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700521 struct fuse_req *req;
522 struct fuse_in *in;
523 struct fuse_copy_state cs;
524 unsigned reqsize;
Miklos Szeredi0720b312006-04-10 22:54:55 -0700525 struct fuse_conn *fc = fuse_get_conn(file);
526 if (!fc)
527 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700528
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800529 restart:
Miklos Szeredid7133112006-04-10 22:54:55 -0700530 spin_lock(&fc->lock);
Jeff Dikee5ac1d12006-04-10 22:54:53 -0700531 err = -EAGAIN;
532 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
533 list_empty(&fc->pending))
534 goto err_unlock;
535
Miklos Szeredi334f4852005-09-09 13:10:27 -0700536 request_wait(fc);
537 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800538 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700539 goto err_unlock;
540 err = -ERESTARTSYS;
541 if (list_empty(&fc->pending))
542 goto err_unlock;
543
544 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800545 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800546 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700547
548 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800549 reqsize = in->h.len;
550 /* If request is too large, reply with an error and restart the read */
551 if (iov_length(iov, nr_segs) < reqsize) {
552 req->out.h.error = -EIO;
553 /* SETXATTR is special, since it may contain too large data */
554 if (in->h.opcode == FUSE_SETXATTR)
555 req->out.h.error = -E2BIG;
556 request_end(fc, req);
557 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700558 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700559 spin_unlock(&fc->lock);
560 fuse_copy_init(&cs, fc, 1, req, iov, nr_segs);
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800561 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
562 if (!err)
563 err = fuse_copy_args(&cs, in->numargs, in->argpages,
564 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700565 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700566 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700567 req->locked = 0;
568 if (!err && req->interrupted)
569 err = -ENOENT;
570 if (err) {
571 if (!req->interrupted)
572 req->out.h.error = -EIO;
573 request_end(fc, req);
574 return err;
575 }
576 if (!req->isreply)
577 request_end(fc, req);
578 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800579 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800580 list_move_tail(&req->list, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700581 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700582 }
583 return reqsize;
584
585 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700586 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700587 return err;
588}
589
590static ssize_t fuse_dev_read(struct file *file, char __user *buf,
591 size_t nbytes, loff_t *off)
592{
593 struct iovec iov;
594 iov.iov_len = nbytes;
595 iov.iov_base = buf;
596 return fuse_dev_readv(file, &iov, 1, off);
597}
598
599/* Look up request on processing list by unique ID */
600static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
601{
602 struct list_head *entry;
603
604 list_for_each(entry, &fc->processing) {
605 struct fuse_req *req;
606 req = list_entry(entry, struct fuse_req, list);
607 if (req->in.h.unique == unique)
608 return req;
609 }
610 return NULL;
611}
612
613static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
614 unsigned nbytes)
615{
616 unsigned reqsize = sizeof(struct fuse_out_header);
617
618 if (out->h.error)
619 return nbytes != reqsize ? -EINVAL : 0;
620
621 reqsize += len_args(out->numargs, out->args);
622
623 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
624 return -EINVAL;
625 else if (reqsize > nbytes) {
626 struct fuse_arg *lastarg = &out->args[out->numargs-1];
627 unsigned diffsize = reqsize - nbytes;
628 if (diffsize > lastarg->size)
629 return -EINVAL;
630 lastarg->size -= diffsize;
631 }
632 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
633 out->page_zeroing);
634}
635
636/*
637 * Write a single reply to a request. First the header is copied from
638 * the write buffer. The request is then searched on the processing
639 * list by the unique ID found in the header. If found, then remove
640 * it from the list and copy the rest of the buffer to the request.
641 * The request is finished by calling request_end()
642 */
643static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
644 unsigned long nr_segs, loff_t *off)
645{
646 int err;
647 unsigned nbytes = iov_length(iov, nr_segs);
648 struct fuse_req *req;
649 struct fuse_out_header oh;
650 struct fuse_copy_state cs;
651 struct fuse_conn *fc = fuse_get_conn(file);
652 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700653 return -EPERM;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700654
Miklos Szeredid7133112006-04-10 22:54:55 -0700655 fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700656 if (nbytes < sizeof(struct fuse_out_header))
657 return -EINVAL;
658
659 err = fuse_copy_one(&cs, &oh, sizeof(oh));
660 if (err)
661 goto err_finish;
662 err = -EINVAL;
663 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
664 oh.len != nbytes)
665 goto err_finish;
666
Miklos Szeredid7133112006-04-10 22:54:55 -0700667 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800668 err = -ENOENT;
669 if (!fc->connected)
670 goto err_unlock;
671
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 req = request_find(fc, oh.unique);
673 err = -EINVAL;
674 if (!req)
675 goto err_unlock;
676
Miklos Szeredi334f4852005-09-09 13:10:27 -0700677 if (req->interrupted) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700678 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700679 fuse_copy_finish(&cs);
Miklos Szeredid7133112006-04-10 22:54:55 -0700680 spin_lock(&fc->lock);
Miklos Szeredi222f1d62006-01-16 22:14:25 -0800681 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700682 return -ENOENT;
683 }
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800684 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700685 req->out.h = oh;
686 req->locked = 1;
687 cs.req = req;
Miklos Szeredid7133112006-04-10 22:54:55 -0700688 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700689
690 err = copy_out_args(&cs, &req->out, nbytes);
691 fuse_copy_finish(&cs);
692
Miklos Szeredid7133112006-04-10 22:54:55 -0700693 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700694 req->locked = 0;
695 if (!err) {
696 if (req->interrupted)
697 err = -ENOENT;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700698 } else if (!req->interrupted)
699 req->out.h.error = -EIO;
700 request_end(fc, req);
701
702 return err ? err : nbytes;
703
704 err_unlock:
Miklos Szeredid7133112006-04-10 22:54:55 -0700705 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700706 err_finish:
707 fuse_copy_finish(&cs);
708 return err;
709}
710
711static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
712 size_t nbytes, loff_t *off)
713{
714 struct iovec iov;
715 iov.iov_len = nbytes;
716 iov.iov_base = (char __user *) buf;
717 return fuse_dev_writev(file, &iov, 1, off);
718}
719
720static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
721{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700722 unsigned mask = POLLOUT | POLLWRNORM;
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700723 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700724 if (!fc)
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700725 return POLLERR;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700726
727 poll_wait(file, &fc->waitq, wait);
728
Miklos Szeredid7133112006-04-10 22:54:55 -0700729 spin_lock(&fc->lock);
Miklos Szeredi7025d9a2006-04-10 22:54:50 -0700730 if (!fc->connected)
731 mask = POLLERR;
732 else if (!list_empty(&fc->pending))
733 mask |= POLLIN | POLLRDNORM;
Miklos Szeredid7133112006-04-10 22:54:55 -0700734 spin_unlock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700735
736 return mask;
737}
738
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800739/*
740 * Abort all requests on the given list (pending or processing)
741 *
Miklos Szeredid7133112006-04-10 22:54:55 -0700742 * This function releases and reacquires fc->lock
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800743 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700744static void end_requests(struct fuse_conn *fc, struct list_head *head)
745{
746 while (!list_empty(head)) {
747 struct fuse_req *req;
748 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700749 req->out.h.error = -ECONNABORTED;
750 request_end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700751 spin_lock(&fc->lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700752 }
753}
754
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800755/*
756 * Abort requests under I/O
757 *
758 * The requests are set to interrupted and finished, and the request
759 * waiter is woken up. This will make request_wait_answer() wait
760 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800761 *
762 * If the request is asynchronous, then the end function needs to be
763 * called after waiting for the request to be unlocked (if it was
764 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800765 */
766static void end_io_requests(struct fuse_conn *fc)
767{
768 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800769 struct fuse_req *req =
770 list_entry(fc->io.next, struct fuse_req, list);
771 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
772
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800773 req->interrupted = 1;
774 req->out.h.error = -ECONNABORTED;
775 req->state = FUSE_REQ_FINISHED;
776 list_del_init(&req->list);
777 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800778 if (end) {
779 req->end = NULL;
780 /* The end function will consume this reference */
781 __fuse_get_request(req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700782 spin_unlock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800783 wait_event(req->waitq, !req->locked);
784 end(fc, req);
Miklos Szeredid7133112006-04-10 22:54:55 -0700785 spin_lock(&fc->lock);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800786 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800787 }
788}
789
790/*
791 * Abort all requests.
792 *
793 * Emergency exit in case of a malicious or accidental deadlock, or
794 * just a hung filesystem.
795 *
796 * The same effect is usually achievable through killing the
797 * filesystem daemon and all users of the filesystem. The exception
798 * is the combination of an asynchronous request and the tricky
799 * deadlock (see Documentation/filesystems/fuse.txt).
800 *
801 * During the aborting, progression of requests from the pending and
802 * processing lists onto the io list, and progression of new requests
803 * onto the pending list is prevented by req->connected being false.
804 *
805 * Progression of requests under I/O to the processing list is
806 * prevented by the req->interrupted flag being true for these
807 * requests. For this reason requests on the io list must be aborted
808 * first.
809 */
810void fuse_abort_conn(struct fuse_conn *fc)
811{
Miklos Szeredid7133112006-04-10 22:54:55 -0700812 spin_lock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800813 if (fc->connected) {
814 fc->connected = 0;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700815 fc->blocked = 0;
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800816 end_io_requests(fc);
817 end_requests(fc, &fc->pending);
818 end_requests(fc, &fc->processing);
819 wake_up_all(&fc->waitq);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700820 wake_up_all(&fc->blocked_waitq);
Jeff Dike385a17b2006-04-10 22:54:52 -0700821 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800822 }
Miklos Szeredid7133112006-04-10 22:54:55 -0700823 spin_unlock(&fc->lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800824}
825
Miklos Szeredi334f4852005-09-09 13:10:27 -0700826static int fuse_dev_release(struct inode *inode, struct file *file)
827{
Miklos Szeredi0720b312006-04-10 22:54:55 -0700828 struct fuse_conn *fc = fuse_get_conn(file);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700829 if (fc) {
Miklos Szeredid7133112006-04-10 22:54:55 -0700830 spin_lock(&fc->lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700831 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700832 end_requests(fc, &fc->pending);
833 end_requests(fc, &fc->processing);
Miklos Szeredid7133112006-04-10 22:54:55 -0700834 spin_unlock(&fc->lock);
Jeff Dike385a17b2006-04-10 22:54:52 -0700835 fasync_helper(-1, file, 0, &fc->fasync);
Miklos Szeredibafa9652006-06-25 05:48:51 -0700836 fuse_conn_put(fc);
Jeff Dike385a17b2006-04-10 22:54:52 -0700837 }
Miklos Szeredif543f252006-01-16 22:14:35 -0800838
Miklos Szeredi334f4852005-09-09 13:10:27 -0700839 return 0;
840}
841
Jeff Dike385a17b2006-04-10 22:54:52 -0700842static int fuse_dev_fasync(int fd, struct file *file, int on)
843{
844 struct fuse_conn *fc = fuse_get_conn(file);
845 if (!fc)
Miklos Szeredia87046d2006-04-10 22:54:56 -0700846 return -EPERM;
Jeff Dike385a17b2006-04-10 22:54:52 -0700847
848 /* No locking - fasync_helper does its own locking */
849 return fasync_helper(fd, file, on, &fc->fasync);
850}
851
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800852const struct file_operations fuse_dev_operations = {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700853 .owner = THIS_MODULE,
854 .llseek = no_llseek,
855 .read = fuse_dev_read,
856 .readv = fuse_dev_readv,
857 .write = fuse_dev_write,
858 .writev = fuse_dev_writev,
859 .poll = fuse_dev_poll,
860 .release = fuse_dev_release,
Jeff Dike385a17b2006-04-10 22:54:52 -0700861 .fasync = fuse_dev_fasync,
Miklos Szeredi334f4852005-09-09 13:10:27 -0700862};
863
864static struct miscdevice fuse_miscdevice = {
865 .minor = FUSE_MINOR,
866 .name = "fuse",
867 .fops = &fuse_dev_operations,
868};
869
870int __init fuse_dev_init(void)
871{
872 int err = -ENOMEM;
873 fuse_req_cachep = kmem_cache_create("fuse_request",
874 sizeof(struct fuse_req),
875 0, 0, NULL, NULL);
876 if (!fuse_req_cachep)
877 goto out;
878
879 err = misc_register(&fuse_miscdevice);
880 if (err)
881 goto out_cache_clean;
882
883 return 0;
884
885 out_cache_clean:
886 kmem_cache_destroy(fuse_req_cachep);
887 out:
888 return err;
889}
890
891void fuse_dev_cleanup(void)
892{
893 misc_deregister(&fuse_miscdevice);
894 kmem_cache_destroy(fuse_req_cachep);
895}