blob: 99325547604f8dacf3f086c268bb50d9299a136c [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{
26 struct fuse_conn *fc;
27 spin_lock(&fuse_lock);
28 fc = file->private_data;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -080029 if (fc && !fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -070030 fc = NULL;
31 spin_unlock(&fuse_lock);
32 return fc;
33}
34
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080035static void fuse_request_init(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -070036{
37 memset(req, 0, sizeof(*req));
38 INIT_LIST_HEAD(&req->list);
39 init_waitqueue_head(&req->waitq);
40 atomic_set(&req->count, 1);
41}
42
43struct fuse_req *fuse_request_alloc(void)
44{
45 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL);
46 if (req)
47 fuse_request_init(req);
48 return req;
49}
50
51void fuse_request_free(struct fuse_req *req)
52{
53 kmem_cache_free(fuse_req_cachep, req);
54}
55
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080056static void block_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070057{
58 sigset_t mask;
59
60 siginitsetinv(&mask, sigmask(SIGKILL));
61 sigprocmask(SIG_BLOCK, &mask, oldset);
62}
63
Miklos Szeredi8bfc0162006-01-16 22:14:28 -080064static void restore_sigs(sigset_t *oldset)
Miklos Szeredi334f4852005-09-09 13:10:27 -070065{
66 sigprocmask(SIG_SETMASK, oldset, NULL);
67}
68
69void fuse_reset_request(struct fuse_req *req)
70{
71 int preallocated = req->preallocated;
72 BUG_ON(atomic_read(&req->count) != 1);
73 fuse_request_init(req);
74 req->preallocated = preallocated;
75}
76
77static void __fuse_get_request(struct fuse_req *req)
78{
79 atomic_inc(&req->count);
80}
81
82/* Must be called with > 1 refcount */
83static void __fuse_put_request(struct fuse_req *req)
84{
85 BUG_ON(atomic_read(&req->count) < 2);
86 atomic_dec(&req->count);
87}
88
89static struct fuse_req *do_get_request(struct fuse_conn *fc)
90{
91 struct fuse_req *req;
92
93 spin_lock(&fuse_lock);
94 BUG_ON(list_empty(&fc->unused_list));
95 req = list_entry(fc->unused_list.next, struct fuse_req, list);
96 list_del_init(&req->list);
97 spin_unlock(&fuse_lock);
98 fuse_request_init(req);
99 req->preallocated = 1;
100 req->in.h.uid = current->fsuid;
101 req->in.h.gid = current->fsgid;
102 req->in.h.pid = current->pid;
103 return req;
104}
105
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700106/* This can return NULL, but only in case it's interrupted by a SIGKILL */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700107struct fuse_req *fuse_get_request(struct fuse_conn *fc)
108{
Miklos Szeredi334f4852005-09-09 13:10:27 -0700109 int intr;
110 sigset_t oldset;
111
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800112 atomic_inc(&fc->num_waiting);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700113 block_sigs(&oldset);
114 intr = down_interruptible(&fc->outstanding_sem);
115 restore_sigs(&oldset);
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800116 if (intr) {
117 atomic_dec(&fc->num_waiting);
118 return NULL;
119 }
120 return do_get_request(fc);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700121}
122
123static void fuse_putback_request(struct fuse_conn *fc, struct fuse_req *req)
124{
125 spin_lock(&fuse_lock);
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800126 if (req->preallocated) {
127 atomic_dec(&fc->num_waiting);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700128 list_add(&req->list, &fc->unused_list);
Miklos Szeredi0cd5b882006-01-16 22:14:38 -0800129 } else
Miklos Szeredi334f4852005-09-09 13:10:27 -0700130 fuse_request_free(req);
131
132 /* If we are in debt decrease that first */
133 if (fc->outstanding_debt)
134 fc->outstanding_debt--;
135 else
136 up(&fc->outstanding_sem);
137 spin_unlock(&fuse_lock);
138}
139
140void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
141{
142 if (atomic_dec_and_test(&req->count))
143 fuse_putback_request(fc, req);
144}
145
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700146void fuse_release_background(struct fuse_req *req)
147{
148 iput(req->inode);
149 iput(req->inode2);
150 if (req->file)
151 fput(req->file);
152 spin_lock(&fuse_lock);
153 list_del(&req->bg_entry);
154 spin_unlock(&fuse_lock);
155}
156
Miklos Szeredi3ec870d2006-01-06 00:19:41 -0800157static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
158{
159 int i;
160 struct fuse_init_out *arg = &req->misc.init_out;
161
Miklos Szeredib3bebd92006-01-16 22:14:27 -0800162 if (req->out.h.error || arg->major != FUSE_KERNEL_VERSION)
Miklos Szeredi3ec870d2006-01-06 00:19:41 -0800163 fc->conn_error = 1;
164 else {
165 fc->minor = arg->minor;
166 fc->max_write = arg->minor < 5 ? 4096 : arg->max_write;
167 }
168
169 /* After INIT reply is received other requests can go
170 out. So do (FUSE_MAX_OUTSTANDING - 1) number of
171 up()s on outstanding_sem. The last up() is done in
172 fuse_putback_request() */
173 for (i = 1; i < FUSE_MAX_OUTSTANDING; i++)
174 up(&fc->outstanding_sem);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800175
176 fuse_put_request(fc, req);
Miklos Szeredi3ec870d2006-01-06 00:19:41 -0800177}
178
Miklos Szeredi334f4852005-09-09 13:10:27 -0700179/*
180 * This function is called when a request is finished. Either a reply
181 * has arrived or it was interrupted (and not yet sent) or some error
Miklos Szeredif43b1552006-01-16 22:14:26 -0800182 * occurred during communication with userspace, or the device file
183 * was closed. In case of a background request the reference to the
184 * stored objects are released. The requester thread is woken up (if
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800185 * still waiting), the 'end' callback is called if given, else the
186 * reference to the request is released
Miklos Szeredi334f4852005-09-09 13:10:27 -0700187 *
188 * Called with fuse_lock, unlocks it
189 */
190static void request_end(struct fuse_conn *fc, struct fuse_req *req)
191{
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800192 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
193 req->end = NULL;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800194 list_del(&req->list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800195 req->state = FUSE_REQ_FINISHED;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700196 spin_unlock(&fuse_lock);
197 if (req->background) {
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700198 down_read(&fc->sbput_sem);
199 if (fc->mounted)
200 fuse_release_background(req);
201 up_read(&fc->sbput_sem);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700202 }
203 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800204 if (end)
205 end(fc, req);
206 else
207 fuse_put_request(fc, req);
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);
318}
319
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700320/*
321 * This can only be interrupted by a SIGKILL
322 */
323void request_send(struct fuse_conn *fc, struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700324{
325 req->isreply = 1;
326 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700327 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700328 req->out.h.error = -ENOTCONN;
329 else if (fc->conn_error)
330 req->out.h.error = -ECONNREFUSED;
331 else {
332 queue_request(fc, req);
333 /* acquire extra reference, since request is still needed
334 after request_end() */
335 __fuse_get_request(req);
336
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700337 request_wait_answer(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700338 }
339 spin_unlock(&fuse_lock);
340}
341
Miklos Szeredi334f4852005-09-09 13:10:27 -0700342static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
343{
344 spin_lock(&fuse_lock);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700345 if (fc->connected) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700346 queue_request(fc, req);
347 spin_unlock(&fuse_lock);
348 } else {
349 req->out.h.error = -ENOTCONN;
350 request_end(fc, req);
351 }
352}
353
354void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
355{
356 req->isreply = 0;
357 request_send_nowait(fc, req);
358}
359
360void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
361{
362 req->isreply = 1;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700363 spin_lock(&fuse_lock);
364 background_request(fc, req);
365 spin_unlock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700366 request_send_nowait(fc, req);
367}
368
369void fuse_send_init(struct fuse_conn *fc)
370{
371 /* This is called from fuse_read_super() so there's guaranteed
Miklos Szeredi6383bda2006-01-16 22:14:29 -0800372 to be exactly one request available */
373 struct fuse_req *req = fuse_get_request(fc);
Miklos Szeredi3ec870d2006-01-06 00:19:41 -0800374 struct fuse_init_in *arg = &req->misc.init_in;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700375 arg->major = FUSE_KERNEL_VERSION;
376 arg->minor = FUSE_KERNEL_MINOR_VERSION;
377 req->in.h.opcode = FUSE_INIT;
378 req->in.numargs = 1;
379 req->in.args[0].size = sizeof(*arg);
380 req->in.args[0].value = arg;
381 req->out.numargs = 1;
Miklos Szeredi3ec870d2006-01-06 00:19:41 -0800382 /* Variable length arguement used for backward compatibility
383 with interface version < 7.5. Rest of init_out is zeroed
384 by do_get_request(), so a short reply is not a problem */
385 req->out.argvar = 1;
386 req->out.args[0].size = sizeof(struct fuse_init_out);
387 req->out.args[0].value = &req->misc.init_out;
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800388 req->end = process_init_reply;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700389 request_send_background(fc, req);
390}
391
392/*
393 * Lock the request. Up to the next unlock_request() there mustn't be
394 * anything that could cause a page-fault. If the request was already
395 * interrupted bail out.
396 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800397static int lock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700398{
399 int err = 0;
400 if (req) {
401 spin_lock(&fuse_lock);
402 if (req->interrupted)
403 err = -ENOENT;
404 else
405 req->locked = 1;
406 spin_unlock(&fuse_lock);
407 }
408 return err;
409}
410
411/*
412 * Unlock request. If it was interrupted during being locked, the
413 * requester thread is currently waiting for it to be unlocked, so
414 * wake it up.
415 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800416static void unlock_request(struct fuse_req *req)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700417{
418 if (req) {
419 spin_lock(&fuse_lock);
420 req->locked = 0;
421 if (req->interrupted)
422 wake_up(&req->waitq);
423 spin_unlock(&fuse_lock);
424 }
425}
426
427struct fuse_copy_state {
428 int write;
429 struct fuse_req *req;
430 const struct iovec *iov;
431 unsigned long nr_segs;
432 unsigned long seglen;
433 unsigned long addr;
434 struct page *pg;
435 void *mapaddr;
436 void *buf;
437 unsigned len;
438};
439
440static void fuse_copy_init(struct fuse_copy_state *cs, int write,
441 struct fuse_req *req, const struct iovec *iov,
442 unsigned long nr_segs)
443{
444 memset(cs, 0, sizeof(*cs));
445 cs->write = write;
446 cs->req = req;
447 cs->iov = iov;
448 cs->nr_segs = nr_segs;
449}
450
451/* Unmap and put previous page of userspace buffer */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800452static void fuse_copy_finish(struct fuse_copy_state *cs)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700453{
454 if (cs->mapaddr) {
455 kunmap_atomic(cs->mapaddr, KM_USER0);
456 if (cs->write) {
457 flush_dcache_page(cs->pg);
458 set_page_dirty_lock(cs->pg);
459 }
460 put_page(cs->pg);
461 cs->mapaddr = NULL;
462 }
463}
464
465/*
466 * Get another pagefull of userspace buffer, and map it to kernel
467 * address space, and lock request
468 */
469static int fuse_copy_fill(struct fuse_copy_state *cs)
470{
471 unsigned long offset;
472 int err;
473
474 unlock_request(cs->req);
475 fuse_copy_finish(cs);
476 if (!cs->seglen) {
477 BUG_ON(!cs->nr_segs);
478 cs->seglen = cs->iov[0].iov_len;
479 cs->addr = (unsigned long) cs->iov[0].iov_base;
480 cs->iov ++;
481 cs->nr_segs --;
482 }
483 down_read(&current->mm->mmap_sem);
484 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
485 &cs->pg, NULL);
486 up_read(&current->mm->mmap_sem);
487 if (err < 0)
488 return err;
489 BUG_ON(err != 1);
490 offset = cs->addr % PAGE_SIZE;
491 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
492 cs->buf = cs->mapaddr + offset;
493 cs->len = min(PAGE_SIZE - offset, cs->seglen);
494 cs->seglen -= cs->len;
495 cs->addr += cs->len;
496
497 return lock_request(cs->req);
498}
499
500/* Do as much copy to/from userspace buffer as we can */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800501static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700502{
503 unsigned ncpy = min(*size, cs->len);
504 if (val) {
505 if (cs->write)
506 memcpy(cs->buf, *val, ncpy);
507 else
508 memcpy(*val, cs->buf, ncpy);
509 *val += ncpy;
510 }
511 *size -= ncpy;
512 cs->len -= ncpy;
513 cs->buf += ncpy;
514 return ncpy;
515}
516
517/*
518 * Copy a page in the request to/from the userspace buffer. Must be
519 * done atomically
520 */
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800521static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
522 unsigned offset, unsigned count, int zeroing)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700523{
524 if (page && zeroing && count < PAGE_SIZE) {
525 void *mapaddr = kmap_atomic(page, KM_USER1);
526 memset(mapaddr, 0, PAGE_SIZE);
527 kunmap_atomic(mapaddr, KM_USER1);
528 }
529 while (count) {
530 int err;
531 if (!cs->len && (err = fuse_copy_fill(cs)))
532 return err;
533 if (page) {
534 void *mapaddr = kmap_atomic(page, KM_USER1);
535 void *buf = mapaddr + offset;
536 offset += fuse_copy_do(cs, &buf, &count);
537 kunmap_atomic(mapaddr, KM_USER1);
538 } else
539 offset += fuse_copy_do(cs, NULL, &count);
540 }
541 if (page && !cs->write)
542 flush_dcache_page(page);
543 return 0;
544}
545
546/* Copy pages in the request to/from userspace buffer */
547static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
548 int zeroing)
549{
550 unsigned i;
551 struct fuse_req *req = cs->req;
552 unsigned offset = req->page_offset;
553 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
554
555 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
556 struct page *page = req->pages[i];
557 int err = fuse_copy_page(cs, page, offset, count, zeroing);
558 if (err)
559 return err;
560
561 nbytes -= count;
562 count = min(nbytes, (unsigned) PAGE_SIZE);
563 offset = 0;
564 }
565 return 0;
566}
567
568/* Copy a single argument in the request to/from userspace buffer */
569static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
570{
571 while (size) {
572 int err;
573 if (!cs->len && (err = fuse_copy_fill(cs)))
574 return err;
575 fuse_copy_do(cs, &val, &size);
576 }
577 return 0;
578}
579
580/* Copy request arguments to/from userspace buffer */
581static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
582 unsigned argpages, struct fuse_arg *args,
583 int zeroing)
584{
585 int err = 0;
586 unsigned i;
587
588 for (i = 0; !err && i < numargs; i++) {
589 struct fuse_arg *arg = &args[i];
590 if (i == numargs - 1 && argpages)
591 err = fuse_copy_pages(cs, arg->size, zeroing);
592 else
593 err = fuse_copy_one(cs, arg->value, arg->size);
594 }
595 return err;
596}
597
598/* Wait until a request is available on the pending list */
599static void request_wait(struct fuse_conn *fc)
600{
601 DECLARE_WAITQUEUE(wait, current);
602
603 add_wait_queue_exclusive(&fc->waitq, &wait);
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800604 while (fc->connected && list_empty(&fc->pending)) {
Miklos Szeredi334f4852005-09-09 13:10:27 -0700605 set_current_state(TASK_INTERRUPTIBLE);
606 if (signal_pending(current))
607 break;
608
609 spin_unlock(&fuse_lock);
610 schedule();
611 spin_lock(&fuse_lock);
612 }
613 set_current_state(TASK_RUNNING);
614 remove_wait_queue(&fc->waitq, &wait);
615}
616
617/*
618 * Read a single request into the userspace filesystem's buffer. This
619 * function waits until a request is available, then removes it from
620 * the pending list and copies request data to userspace buffer. If
621 * no reply is needed (FORGET) or request has been interrupted or
622 * there was an error during the copying then it's finished by calling
623 * request_end(). Otherwise add it to the processing list, and set
624 * the 'sent' flag.
625 */
626static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
627 unsigned long nr_segs, loff_t *off)
628{
629 int err;
630 struct fuse_conn *fc;
631 struct fuse_req *req;
632 struct fuse_in *in;
633 struct fuse_copy_state cs;
634 unsigned reqsize;
635
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800636 restart:
Miklos Szeredi334f4852005-09-09 13:10:27 -0700637 spin_lock(&fuse_lock);
638 fc = file->private_data;
639 err = -EPERM;
640 if (!fc)
641 goto err_unlock;
642 request_wait(fc);
643 err = -ENODEV;
Miklos Szeredi9ba7cbb2006-01-16 22:14:34 -0800644 if (!fc->connected)
Miklos Szeredi334f4852005-09-09 13:10:27 -0700645 goto err_unlock;
646 err = -ERESTARTSYS;
647 if (list_empty(&fc->pending))
648 goto err_unlock;
649
650 req = list_entry(fc->pending.next, struct fuse_req, list);
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800651 req->state = FUSE_REQ_READING;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800652 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700653
654 in = &req->in;
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800655 reqsize = in->h.len;
656 /* If request is too large, reply with an error and restart the read */
657 if (iov_length(iov, nr_segs) < reqsize) {
658 req->out.h.error = -EIO;
659 /* SETXATTR is special, since it may contain too large data */
660 if (in->h.opcode == FUSE_SETXATTR)
661 req->out.h.error = -E2BIG;
662 request_end(fc, req);
663 goto restart;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700664 }
Miklos Szeredi1d3d7522006-01-06 00:19:40 -0800665 spin_unlock(&fuse_lock);
666 fuse_copy_init(&cs, 1, req, iov, nr_segs);
667 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
668 if (!err)
669 err = fuse_copy_args(&cs, in->numargs, in->argpages,
670 (struct fuse_arg *) in->args, 0);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700671 fuse_copy_finish(&cs);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700672 spin_lock(&fuse_lock);
673 req->locked = 0;
674 if (!err && req->interrupted)
675 err = -ENOENT;
676 if (err) {
677 if (!req->interrupted)
678 req->out.h.error = -EIO;
679 request_end(fc, req);
680 return err;
681 }
682 if (!req->isreply)
683 request_end(fc, req);
684 else {
Miklos Szeredi83cfd492006-01-16 22:14:31 -0800685 req->state = FUSE_REQ_SENT;
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800686 list_move_tail(&req->list, &fc->processing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700687 spin_unlock(&fuse_lock);
688 }
689 return reqsize;
690
691 err_unlock:
692 spin_unlock(&fuse_lock);
693 return err;
694}
695
696static ssize_t fuse_dev_read(struct file *file, char __user *buf,
697 size_t nbytes, loff_t *off)
698{
699 struct iovec iov;
700 iov.iov_len = nbytes;
701 iov.iov_base = buf;
702 return fuse_dev_readv(file, &iov, 1, off);
703}
704
705/* Look up request on processing list by unique ID */
706static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
707{
708 struct list_head *entry;
709
710 list_for_each(entry, &fc->processing) {
711 struct fuse_req *req;
712 req = list_entry(entry, struct fuse_req, list);
713 if (req->in.h.unique == unique)
714 return req;
715 }
716 return NULL;
717}
718
719static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
720 unsigned nbytes)
721{
722 unsigned reqsize = sizeof(struct fuse_out_header);
723
724 if (out->h.error)
725 return nbytes != reqsize ? -EINVAL : 0;
726
727 reqsize += len_args(out->numargs, out->args);
728
729 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
730 return -EINVAL;
731 else if (reqsize > nbytes) {
732 struct fuse_arg *lastarg = &out->args[out->numargs-1];
733 unsigned diffsize = reqsize - nbytes;
734 if (diffsize > lastarg->size)
735 return -EINVAL;
736 lastarg->size -= diffsize;
737 }
738 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
739 out->page_zeroing);
740}
741
742/*
743 * Write a single reply to a request. First the header is copied from
744 * the write buffer. The request is then searched on the processing
745 * list by the unique ID found in the header. If found, then remove
746 * it from the list and copy the rest of the buffer to the request.
747 * The request is finished by calling request_end()
748 */
749static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
750 unsigned long nr_segs, loff_t *off)
751{
752 int err;
753 unsigned nbytes = iov_length(iov, nr_segs);
754 struct fuse_req *req;
755 struct fuse_out_header oh;
756 struct fuse_copy_state cs;
757 struct fuse_conn *fc = fuse_get_conn(file);
758 if (!fc)
759 return -ENODEV;
760
761 fuse_copy_init(&cs, 0, NULL, iov, nr_segs);
762 if (nbytes < sizeof(struct fuse_out_header))
763 return -EINVAL;
764
765 err = fuse_copy_one(&cs, &oh, sizeof(oh));
766 if (err)
767 goto err_finish;
768 err = -EINVAL;
769 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
770 oh.len != nbytes)
771 goto err_finish;
772
773 spin_lock(&fuse_lock);
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800774 err = -ENOENT;
775 if (!fc->connected)
776 goto err_unlock;
777
Miklos Szeredi334f4852005-09-09 13:10:27 -0700778 req = request_find(fc, oh.unique);
779 err = -EINVAL;
780 if (!req)
781 goto err_unlock;
782
Miklos Szeredi334f4852005-09-09 13:10:27 -0700783 if (req->interrupted) {
Miklos Szeredi222f1d692006-01-16 22:14:25 -0800784 spin_unlock(&fuse_lock);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700785 fuse_copy_finish(&cs);
Miklos Szeredi222f1d692006-01-16 22:14:25 -0800786 spin_lock(&fuse_lock);
787 request_end(fc, req);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700788 return -ENOENT;
789 }
Miklos Szeredid77a1d52006-01-16 22:14:31 -0800790 list_move(&req->list, &fc->io);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700791 req->out.h = oh;
792 req->locked = 1;
793 cs.req = req;
794 spin_unlock(&fuse_lock);
795
796 err = copy_out_args(&cs, &req->out, nbytes);
797 fuse_copy_finish(&cs);
798
799 spin_lock(&fuse_lock);
800 req->locked = 0;
801 if (!err) {
802 if (req->interrupted)
803 err = -ENOENT;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700804 } else if (!req->interrupted)
805 req->out.h.error = -EIO;
806 request_end(fc, req);
807
808 return err ? err : nbytes;
809
810 err_unlock:
811 spin_unlock(&fuse_lock);
812 err_finish:
813 fuse_copy_finish(&cs);
814 return err;
815}
816
817static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
818 size_t nbytes, loff_t *off)
819{
820 struct iovec iov;
821 iov.iov_len = nbytes;
822 iov.iov_base = (char __user *) buf;
823 return fuse_dev_writev(file, &iov, 1, off);
824}
825
826static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
827{
828 struct fuse_conn *fc = fuse_get_conn(file);
829 unsigned mask = POLLOUT | POLLWRNORM;
830
831 if (!fc)
832 return -ENODEV;
833
834 poll_wait(file, &fc->waitq, wait);
835
836 spin_lock(&fuse_lock);
837 if (!list_empty(&fc->pending))
838 mask |= POLLIN | POLLRDNORM;
839 spin_unlock(&fuse_lock);
840
841 return mask;
842}
843
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800844/*
845 * Abort all requests on the given list (pending or processing)
846 *
847 * This function releases and reacquires fuse_lock
848 */
Miklos Szeredi334f4852005-09-09 13:10:27 -0700849static void end_requests(struct fuse_conn *fc, struct list_head *head)
850{
851 while (!list_empty(head)) {
852 struct fuse_req *req;
853 req = list_entry(head->next, struct fuse_req, list);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700854 req->out.h.error = -ECONNABORTED;
855 request_end(fc, req);
856 spin_lock(&fuse_lock);
857 }
858}
859
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800860/*
861 * Abort requests under I/O
862 *
863 * The requests are set to interrupted and finished, and the request
864 * waiter is woken up. This will make request_wait_answer() wait
865 * until the request is unlocked and then return.
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800866 *
867 * If the request is asynchronous, then the end function needs to be
868 * called after waiting for the request to be unlocked (if it was
869 * locked).
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800870 */
871static void end_io_requests(struct fuse_conn *fc)
872{
873 while (!list_empty(&fc->io)) {
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800874 struct fuse_req *req =
875 list_entry(fc->io.next, struct fuse_req, list);
876 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
877
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800878 req->interrupted = 1;
879 req->out.h.error = -ECONNABORTED;
880 req->state = FUSE_REQ_FINISHED;
881 list_del_init(&req->list);
882 wake_up(&req->waitq);
Miklos Szeredi64c6d8e2006-01-16 22:14:42 -0800883 if (end) {
884 req->end = NULL;
885 /* The end function will consume this reference */
886 __fuse_get_request(req);
887 spin_unlock(&fuse_lock);
888 wait_event(req->waitq, !req->locked);
889 end(fc, req);
890 spin_lock(&fuse_lock);
891 }
Miklos Szeredi69a53bf2006-01-16 22:14:41 -0800892 }
893}
894
895/*
896 * Abort all requests.
897 *
898 * Emergency exit in case of a malicious or accidental deadlock, or
899 * just a hung filesystem.
900 *
901 * The same effect is usually achievable through killing the
902 * filesystem daemon and all users of the filesystem. The exception
903 * is the combination of an asynchronous request and the tricky
904 * deadlock (see Documentation/filesystems/fuse.txt).
905 *
906 * During the aborting, progression of requests from the pending and
907 * processing lists onto the io list, and progression of new requests
908 * onto the pending list is prevented by req->connected being false.
909 *
910 * Progression of requests under I/O to the processing list is
911 * prevented by the req->interrupted flag being true for these
912 * requests. For this reason requests on the io list must be aborted
913 * first.
914 */
915void fuse_abort_conn(struct fuse_conn *fc)
916{
917 spin_lock(&fuse_lock);
918 if (fc->connected) {
919 fc->connected = 0;
920 end_io_requests(fc);
921 end_requests(fc, &fc->pending);
922 end_requests(fc, &fc->processing);
923 wake_up_all(&fc->waitq);
924 }
925 spin_unlock(&fuse_lock);
926}
927
Miklos Szeredi334f4852005-09-09 13:10:27 -0700928static int fuse_dev_release(struct inode *inode, struct file *file)
929{
930 struct fuse_conn *fc;
931
932 spin_lock(&fuse_lock);
933 fc = file->private_data;
934 if (fc) {
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700935 fc->connected = 0;
Miklos Szeredi334f4852005-09-09 13:10:27 -0700936 end_requests(fc, &fc->pending);
937 end_requests(fc, &fc->processing);
Miklos Szeredi334f4852005-09-09 13:10:27 -0700938 }
939 spin_unlock(&fuse_lock);
Miklos Szeredif543f252006-01-16 22:14:35 -0800940 if (fc)
941 kobject_put(&fc->kobj);
942
Miklos Szeredi334f4852005-09-09 13:10:27 -0700943 return 0;
944}
945
946struct file_operations fuse_dev_operations = {
947 .owner = THIS_MODULE,
948 .llseek = no_llseek,
949 .read = fuse_dev_read,
950 .readv = fuse_dev_readv,
951 .write = fuse_dev_write,
952 .writev = fuse_dev_writev,
953 .poll = fuse_dev_poll,
954 .release = fuse_dev_release,
955};
956
957static struct miscdevice fuse_miscdevice = {
958 .minor = FUSE_MINOR,
959 .name = "fuse",
960 .fops = &fuse_dev_operations,
961};
962
963int __init fuse_dev_init(void)
964{
965 int err = -ENOMEM;
966 fuse_req_cachep = kmem_cache_create("fuse_request",
967 sizeof(struct fuse_req),
968 0, 0, NULL, NULL);
969 if (!fuse_req_cachep)
970 goto out;
971
972 err = misc_register(&fuse_miscdevice);
973 if (err)
974 goto out_cache_clean;
975
976 return 0;
977
978 out_cache_clean:
979 kmem_cache_destroy(fuse_req_cachep);
980 out:
981 return err;
982}
983
984void fuse_dev_cleanup(void)
985{
986 misc_deregister(&fuse_miscdevice);
987 kmem_cache_destroy(fuse_req_cachep);
988}