fuse: general infrastructure for pages[] of variable size
[linux-2.6-block.git] / fs / fuse / dev.c
CommitLineData
334f485d
MS
1/*
2 FUSE: Filesystem in Userspace
1729a16c 3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
334f485d
MS
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>
dd3bb14f 19#include <linux/pipe_fs_i.h>
ce534fb0
MS
20#include <linux/swap.h>
21#include <linux/splice.h>
334f485d
MS
22
23MODULE_ALIAS_MISCDEV(FUSE_MINOR);
578454ff 24MODULE_ALIAS("devname:fuse");
334f485d 25
e18b890b 26static struct kmem_cache *fuse_req_cachep;
334f485d 27
8bfc016d 28static struct fuse_conn *fuse_get_conn(struct file *file)
334f485d 29{
0720b315
MS
30 /*
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
33 */
34 return file->private_data;
334f485d
MS
35}
36
4250c066
MP
37static void fuse_request_init(struct fuse_req *req, struct page **pages,
38 unsigned npages)
334f485d
MS
39{
40 memset(req, 0, sizeof(*req));
4250c066 41 memset(pages, 0, sizeof(*pages) * npages);
334f485d 42 INIT_LIST_HEAD(&req->list);
a4d27e75 43 INIT_LIST_HEAD(&req->intr_entry);
334f485d
MS
44 init_waitqueue_head(&req->waitq);
45 atomic_set(&req->count, 1);
4250c066
MP
46 req->pages = pages;
47 req->max_pages = npages;
334f485d
MS
48}
49
4250c066 50static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
334f485d 51{
4250c066
MP
52 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
53 if (req) {
54 struct page **pages;
55
56 if (npages <= FUSE_REQ_INLINE_PAGES)
57 pages = req->inline_pages;
58 else
59 pages = kmalloc(sizeof(struct page *) * npages, flags);
60
61 if (!pages) {
62 kmem_cache_free(fuse_req_cachep, req);
63 return NULL;
64 }
65
66 fuse_request_init(req, pages, npages);
67 }
334f485d
MS
68 return req;
69}
4250c066
MP
70
71struct fuse_req *fuse_request_alloc(unsigned npages)
72{
73 return __fuse_request_alloc(npages, GFP_KERNEL);
74}
08cbf542 75EXPORT_SYMBOL_GPL(fuse_request_alloc);
334f485d 76
4250c066 77struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
3be5a52b 78{
4250c066 79 return __fuse_request_alloc(npages, GFP_NOFS);
3be5a52b
MS
80}
81
334f485d
MS
82void fuse_request_free(struct fuse_req *req)
83{
4250c066
MP
84 if (req->pages != req->inline_pages)
85 kfree(req->pages);
334f485d
MS
86 kmem_cache_free(fuse_req_cachep, req);
87}
88
8bfc016d 89static void block_sigs(sigset_t *oldset)
334f485d
MS
90{
91 sigset_t mask;
92
93 siginitsetinv(&mask, sigmask(SIGKILL));
94 sigprocmask(SIG_BLOCK, &mask, oldset);
95}
96
8bfc016d 97static void restore_sigs(sigset_t *oldset)
334f485d
MS
98{
99 sigprocmask(SIG_SETMASK, oldset, NULL);
100}
101
334f485d
MS
102static void __fuse_get_request(struct fuse_req *req)
103{
104 atomic_inc(&req->count);
105}
106
107/* Must be called with > 1 refcount */
108static void __fuse_put_request(struct fuse_req *req)
109{
110 BUG_ON(atomic_read(&req->count) < 2);
111 atomic_dec(&req->count);
112}
113
33649c91
MS
114static void fuse_req_init_context(struct fuse_req *req)
115{
499dcf20
EB
116 req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
117 req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
33649c91
MS
118 req->in.h.pid = current->pid;
119}
120
ce1d5a49 121struct fuse_req *fuse_get_req(struct fuse_conn *fc)
334f485d 122{
08a53cdc
MS
123 struct fuse_req *req;
124 sigset_t oldset;
9bc5ddda 125 int intr;
08a53cdc
MS
126 int err;
127
9bc5ddda 128 atomic_inc(&fc->num_waiting);
08a53cdc 129 block_sigs(&oldset);
9bc5ddda 130 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
08a53cdc 131 restore_sigs(&oldset);
9bc5ddda
MS
132 err = -EINTR;
133 if (intr)
134 goto out;
08a53cdc 135
51eb01e7
MS
136 err = -ENOTCONN;
137 if (!fc->connected)
138 goto out;
139
4250c066 140 req = fuse_request_alloc(FUSE_MAX_PAGES_PER_REQ);
9bc5ddda 141 err = -ENOMEM;
ce1d5a49 142 if (!req)
9bc5ddda 143 goto out;
334f485d 144
33649c91 145 fuse_req_init_context(req);
9bc5ddda 146 req->waiting = 1;
334f485d 147 return req;
9bc5ddda
MS
148
149 out:
150 atomic_dec(&fc->num_waiting);
151 return ERR_PTR(err);
334f485d 152}
08cbf542 153EXPORT_SYMBOL_GPL(fuse_get_req);
334f485d 154
33649c91
MS
155/*
156 * Return request in fuse_file->reserved_req. However that may
157 * currently be in use. If that is the case, wait for it to become
158 * available.
159 */
160static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
161 struct file *file)
162{
163 struct fuse_req *req = NULL;
164 struct fuse_file *ff = file->private_data;
165
166 do {
de5e3dec 167 wait_event(fc->reserved_req_waitq, ff->reserved_req);
33649c91
MS
168 spin_lock(&fc->lock);
169 if (ff->reserved_req) {
170 req = ff->reserved_req;
171 ff->reserved_req = NULL;
cb0942b8 172 req->stolen_file = get_file(file);
33649c91
MS
173 }
174 spin_unlock(&fc->lock);
175 } while (!req);
176
177 return req;
178}
179
180/*
181 * Put stolen request back into fuse_file->reserved_req
182 */
183static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
184{
185 struct file *file = req->stolen_file;
186 struct fuse_file *ff = file->private_data;
187
188 spin_lock(&fc->lock);
4250c066 189 fuse_request_init(req, req->pages, req->max_pages);
33649c91
MS
190 BUG_ON(ff->reserved_req);
191 ff->reserved_req = req;
de5e3dec 192 wake_up_all(&fc->reserved_req_waitq);
33649c91
MS
193 spin_unlock(&fc->lock);
194 fput(file);
195}
196
197/*
198 * Gets a requests for a file operation, always succeeds
199 *
200 * This is used for sending the FLUSH request, which must get to
201 * userspace, due to POSIX locks which may need to be unlocked.
202 *
203 * If allocation fails due to OOM, use the reserved request in
204 * fuse_file.
205 *
206 * This is very unlikely to deadlock accidentally, since the
207 * filesystem should not have it's own file open. If deadlock is
208 * intentional, it can still be broken by "aborting" the filesystem.
209 */
210struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
211{
212 struct fuse_req *req;
213
214 atomic_inc(&fc->num_waiting);
215 wait_event(fc->blocked_waitq, !fc->blocked);
4250c066 216 req = fuse_request_alloc(FUSE_MAX_PAGES_PER_REQ);
33649c91
MS
217 if (!req)
218 req = get_reserved_req(fc, file);
219
220 fuse_req_init_context(req);
221 req->waiting = 1;
222 return req;
223}
224
334f485d 225void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
7128ec2a
MS
226{
227 if (atomic_dec_and_test(&req->count)) {
9bc5ddda
MS
228 if (req->waiting)
229 atomic_dec(&fc->num_waiting);
33649c91
MS
230
231 if (req->stolen_file)
232 put_reserved_req(fc, req);
233 else
234 fuse_request_free(req);
7128ec2a
MS
235 }
236}
08cbf542 237EXPORT_SYMBOL_GPL(fuse_put_request);
7128ec2a 238
d12def1b
MS
239static unsigned len_args(unsigned numargs, struct fuse_arg *args)
240{
241 unsigned nbytes = 0;
242 unsigned i;
243
244 for (i = 0; i < numargs; i++)
245 nbytes += args[i].size;
246
247 return nbytes;
248}
249
250static u64 fuse_get_unique(struct fuse_conn *fc)
251{
252 fc->reqctr++;
253 /* zero is special */
254 if (fc->reqctr == 0)
255 fc->reqctr = 1;
256
257 return fc->reqctr;
258}
259
260static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
261{
d12def1b
MS
262 req->in.h.len = sizeof(struct fuse_in_header) +
263 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
264 list_add_tail(&req->list, &fc->pending);
265 req->state = FUSE_REQ_PENDING;
266 if (!req->waiting) {
267 req->waiting = 1;
268 atomic_inc(&fc->num_waiting);
269 }
270 wake_up(&fc->waitq);
271 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
272}
273
07e77dca
MS
274void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
275 u64 nodeid, u64 nlookup)
276{
02c048b9
MS
277 forget->forget_one.nodeid = nodeid;
278 forget->forget_one.nlookup = nlookup;
07e77dca
MS
279
280 spin_lock(&fc->lock);
5dfcc87f
MS
281 if (fc->connected) {
282 fc->forget_list_tail->next = forget;
283 fc->forget_list_tail = forget;
284 wake_up(&fc->waitq);
285 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
286 } else {
287 kfree(forget);
288 }
07e77dca
MS
289 spin_unlock(&fc->lock);
290}
291
d12def1b
MS
292static void flush_bg_queue(struct fuse_conn *fc)
293{
7a6d3c8b 294 while (fc->active_background < fc->max_background &&
d12def1b
MS
295 !list_empty(&fc->bg_queue)) {
296 struct fuse_req *req;
297
298 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
299 list_del(&req->list);
300 fc->active_background++;
2d45ba38 301 req->in.h.unique = fuse_get_unique(fc);
d12def1b
MS
302 queue_request(fc, req);
303 }
304}
305
334f485d
MS
306/*
307 * This function is called when a request is finished. Either a reply
f9a2842e 308 * has arrived or it was aborted (and not yet sent) or some error
f43b155a 309 * occurred during communication with userspace, or the device file
51eb01e7
MS
310 * was closed. The requester thread is woken up (if still waiting),
311 * the 'end' callback is called if given, else the reference to the
312 * request is released
7128ec2a 313 *
d7133114 314 * Called with fc->lock, unlocks it
334f485d
MS
315 */
316static void request_end(struct fuse_conn *fc, struct fuse_req *req)
b9ca67b2 317__releases(fc->lock)
334f485d 318{
51eb01e7
MS
319 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
320 req->end = NULL;
d77a1d5b 321 list_del(&req->list);
a4d27e75 322 list_del(&req->intr_entry);
83cfd493 323 req->state = FUSE_REQ_FINISHED;
51eb01e7 324 if (req->background) {
7a6d3c8b 325 if (fc->num_background == fc->max_background) {
51eb01e7
MS
326 fc->blocked = 0;
327 wake_up_all(&fc->blocked_waitq);
328 }
7a6d3c8b 329 if (fc->num_background == fc->congestion_threshold &&
a325f9b9 330 fc->connected && fc->bdi_initialized) {
8aa7e847
JA
331 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
332 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
f92b99b9 333 }
51eb01e7 334 fc->num_background--;
d12def1b
MS
335 fc->active_background--;
336 flush_bg_queue(fc);
334f485d 337 }
51eb01e7 338 spin_unlock(&fc->lock);
51eb01e7
MS
339 wake_up(&req->waitq);
340 if (end)
341 end(fc, req);
e9bb09dd 342 fuse_put_request(fc, req);
334f485d
MS
343}
344
a4d27e75
MS
345static void wait_answer_interruptible(struct fuse_conn *fc,
346 struct fuse_req *req)
b9ca67b2
MS
347__releases(fc->lock)
348__acquires(fc->lock)
a4d27e75
MS
349{
350 if (signal_pending(current))
351 return;
352
353 spin_unlock(&fc->lock);
354 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
355 spin_lock(&fc->lock);
356}
357
358static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
359{
360 list_add_tail(&req->intr_entry, &fc->interrupts);
361 wake_up(&fc->waitq);
362 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
363}
364
7c352bdf 365static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
b9ca67b2
MS
366__releases(fc->lock)
367__acquires(fc->lock)
334f485d 368{
a4d27e75
MS
369 if (!fc->no_interrupt) {
370 /* Any signal may interrupt this */
371 wait_answer_interruptible(fc, req);
334f485d 372
a4d27e75
MS
373 if (req->aborted)
374 goto aborted;
375 if (req->state == FUSE_REQ_FINISHED)
376 return;
377
378 req->interrupted = 1;
379 if (req->state == FUSE_REQ_SENT)
380 queue_interrupt(fc, req);
381 }
382
a131de0a 383 if (!req->force) {
a4d27e75
MS
384 sigset_t oldset;
385
386 /* Only fatal signals may interrupt this */
51eb01e7 387 block_sigs(&oldset);
a4d27e75 388 wait_answer_interruptible(fc, req);
51eb01e7 389 restore_sigs(&oldset);
a131de0a
MS
390
391 if (req->aborted)
392 goto aborted;
393 if (req->state == FUSE_REQ_FINISHED)
394 return;
395
396 /* Request is not yet in userspace, bail out */
397 if (req->state == FUSE_REQ_PENDING) {
398 list_del(&req->list);
399 __fuse_put_request(req);
400 req->out.h.error = -EINTR;
401 return;
402 }
51eb01e7 403 }
334f485d 404
a131de0a
MS
405 /*
406 * Either request is already in userspace, or it was forced.
407 * Wait it out.
408 */
409 spin_unlock(&fc->lock);
410 wait_event(req->waitq, req->state == FUSE_REQ_FINISHED);
411 spin_lock(&fc->lock);
a4d27e75 412
a131de0a
MS
413 if (!req->aborted)
414 return;
a4d27e75
MS
415
416 aborted:
a131de0a 417 BUG_ON(req->state != FUSE_REQ_FINISHED);
334f485d
MS
418 if (req->locked) {
419 /* This is uninterruptible sleep, because data is
420 being copied to/from the buffers of req. During
421 locked state, there mustn't be any filesystem
422 operation (e.g. page fault), since that could lead
423 to deadlock */
d7133114 424 spin_unlock(&fc->lock);
334f485d 425 wait_event(req->waitq, !req->locked);
d7133114 426 spin_lock(&fc->lock);
334f485d 427 }
334f485d
MS
428}
429
b93f858a 430void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
334f485d
MS
431{
432 req->isreply = 1;
d7133114 433 spin_lock(&fc->lock);
1e9a4ed9 434 if (!fc->connected)
334f485d
MS
435 req->out.h.error = -ENOTCONN;
436 else if (fc->conn_error)
437 req->out.h.error = -ECONNREFUSED;
438 else {
2d45ba38 439 req->in.h.unique = fuse_get_unique(fc);
334f485d
MS
440 queue_request(fc, req);
441 /* acquire extra reference, since request is still needed
442 after request_end() */
443 __fuse_get_request(req);
444
7c352bdf 445 request_wait_answer(fc, req);
334f485d 446 }
d7133114 447 spin_unlock(&fc->lock);
334f485d 448}
08cbf542 449EXPORT_SYMBOL_GPL(fuse_request_send);
334f485d 450
b93f858a
TH
451static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
452 struct fuse_req *req)
d12def1b
MS
453{
454 req->background = 1;
455 fc->num_background++;
7a6d3c8b 456 if (fc->num_background == fc->max_background)
d12def1b 457 fc->blocked = 1;
7a6d3c8b 458 if (fc->num_background == fc->congestion_threshold &&
a325f9b9 459 fc->bdi_initialized) {
8aa7e847
JA
460 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
461 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
d12def1b
MS
462 }
463 list_add_tail(&req->list, &fc->bg_queue);
464 flush_bg_queue(fc);
465}
466
b93f858a 467static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
334f485d 468{
d7133114 469 spin_lock(&fc->lock);
1e9a4ed9 470 if (fc->connected) {
b93f858a 471 fuse_request_send_nowait_locked(fc, req);
d7133114 472 spin_unlock(&fc->lock);
334f485d
MS
473 } else {
474 req->out.h.error = -ENOTCONN;
475 request_end(fc, req);
476 }
477}
478
b93f858a 479void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
334f485d
MS
480{
481 req->isreply = 1;
b93f858a 482 fuse_request_send_nowait(fc, req);
334f485d 483}
08cbf542 484EXPORT_SYMBOL_GPL(fuse_request_send_background);
334f485d 485
2d45ba38
MS
486static int fuse_request_send_notify_reply(struct fuse_conn *fc,
487 struct fuse_req *req, u64 unique)
488{
489 int err = -ENODEV;
490
491 req->isreply = 0;
492 req->in.h.unique = unique;
493 spin_lock(&fc->lock);
494 if (fc->connected) {
495 queue_request(fc, req);
496 err = 0;
497 }
498 spin_unlock(&fc->lock);
499
500 return err;
501}
502
3be5a52b
MS
503/*
504 * Called under fc->lock
505 *
506 * fc->connected must have been checked previously
507 */
b93f858a
TH
508void fuse_request_send_background_locked(struct fuse_conn *fc,
509 struct fuse_req *req)
3be5a52b
MS
510{
511 req->isreply = 1;
b93f858a 512 fuse_request_send_nowait_locked(fc, req);
3be5a52b
MS
513}
514
0b05b183
AA
515void fuse_force_forget(struct file *file, u64 nodeid)
516{
517 struct inode *inode = file->f_path.dentry->d_inode;
518 struct fuse_conn *fc = get_fuse_conn(inode);
519 struct fuse_req *req;
520 struct fuse_forget_in inarg;
521
522 memset(&inarg, 0, sizeof(inarg));
523 inarg.nlookup = 1;
524 req = fuse_get_req_nofail(fc, file);
525 req->in.h.opcode = FUSE_FORGET;
526 req->in.h.nodeid = nodeid;
527 req->in.numargs = 1;
528 req->in.args[0].size = sizeof(inarg);
529 req->in.args[0].value = &inarg;
530 req->isreply = 0;
531 fuse_request_send_nowait(fc, req);
532}
533
334f485d
MS
534/*
535 * Lock the request. Up to the next unlock_request() there mustn't be
536 * anything that could cause a page-fault. If the request was already
f9a2842e 537 * aborted bail out.
334f485d 538 */
d7133114 539static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
334f485d
MS
540{
541 int err = 0;
542 if (req) {
d7133114 543 spin_lock(&fc->lock);
f9a2842e 544 if (req->aborted)
334f485d
MS
545 err = -ENOENT;
546 else
547 req->locked = 1;
d7133114 548 spin_unlock(&fc->lock);
334f485d
MS
549 }
550 return err;
551}
552
553/*
f9a2842e 554 * Unlock request. If it was aborted during being locked, the
334f485d
MS
555 * requester thread is currently waiting for it to be unlocked, so
556 * wake it up.
557 */
d7133114 558static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
334f485d
MS
559{
560 if (req) {
d7133114 561 spin_lock(&fc->lock);
334f485d 562 req->locked = 0;
f9a2842e 563 if (req->aborted)
334f485d 564 wake_up(&req->waitq);
d7133114 565 spin_unlock(&fc->lock);
334f485d
MS
566 }
567}
568
569struct fuse_copy_state {
d7133114 570 struct fuse_conn *fc;
334f485d
MS
571 int write;
572 struct fuse_req *req;
573 const struct iovec *iov;
dd3bb14f
MS
574 struct pipe_buffer *pipebufs;
575 struct pipe_buffer *currbuf;
576 struct pipe_inode_info *pipe;
334f485d
MS
577 unsigned long nr_segs;
578 unsigned long seglen;
579 unsigned long addr;
580 struct page *pg;
581 void *mapaddr;
582 void *buf;
583 unsigned len;
ce534fb0 584 unsigned move_pages:1;
334f485d
MS
585};
586
d7133114 587static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
c3021629 588 int write,
d7133114 589 const struct iovec *iov, unsigned long nr_segs)
334f485d
MS
590{
591 memset(cs, 0, sizeof(*cs));
d7133114 592 cs->fc = fc;
334f485d 593 cs->write = write;
334f485d
MS
594 cs->iov = iov;
595 cs->nr_segs = nr_segs;
596}
597
598/* Unmap and put previous page of userspace buffer */
8bfc016d 599static void fuse_copy_finish(struct fuse_copy_state *cs)
334f485d 600{
dd3bb14f
MS
601 if (cs->currbuf) {
602 struct pipe_buffer *buf = cs->currbuf;
603
c3021629
MS
604 if (!cs->write) {
605 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
606 } else {
7909b1c6 607 kunmap(buf->page);
c3021629
MS
608 buf->len = PAGE_SIZE - cs->len;
609 }
dd3bb14f
MS
610 cs->currbuf = NULL;
611 cs->mapaddr = NULL;
612 } else if (cs->mapaddr) {
7909b1c6 613 kunmap(cs->pg);
334f485d
MS
614 if (cs->write) {
615 flush_dcache_page(cs->pg);
616 set_page_dirty_lock(cs->pg);
617 }
618 put_page(cs->pg);
619 cs->mapaddr = NULL;
620 }
621}
622
623/*
624 * Get another pagefull of userspace buffer, and map it to kernel
625 * address space, and lock request
626 */
627static int fuse_copy_fill(struct fuse_copy_state *cs)
628{
629 unsigned long offset;
630 int err;
631
d7133114 632 unlock_request(cs->fc, cs->req);
334f485d 633 fuse_copy_finish(cs);
dd3bb14f
MS
634 if (cs->pipebufs) {
635 struct pipe_buffer *buf = cs->pipebufs;
636
c3021629
MS
637 if (!cs->write) {
638 err = buf->ops->confirm(cs->pipe, buf);
639 if (err)
640 return err;
641
642 BUG_ON(!cs->nr_segs);
643 cs->currbuf = buf;
7909b1c6 644 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
c3021629
MS
645 cs->len = buf->len;
646 cs->buf = cs->mapaddr + buf->offset;
647 cs->pipebufs++;
648 cs->nr_segs--;
649 } else {
650 struct page *page;
dd3bb14f 651
c3021629
MS
652 if (cs->nr_segs == cs->pipe->buffers)
653 return -EIO;
654
655 page = alloc_page(GFP_HIGHUSER);
656 if (!page)
657 return -ENOMEM;
658
659 buf->page = page;
660 buf->offset = 0;
661 buf->len = 0;
662
663 cs->currbuf = buf;
7909b1c6 664 cs->mapaddr = kmap(page);
c3021629
MS
665 cs->buf = cs->mapaddr;
666 cs->len = PAGE_SIZE;
667 cs->pipebufs++;
668 cs->nr_segs++;
669 }
dd3bb14f
MS
670 } else {
671 if (!cs->seglen) {
672 BUG_ON(!cs->nr_segs);
673 cs->seglen = cs->iov[0].iov_len;
674 cs->addr = (unsigned long) cs->iov[0].iov_base;
675 cs->iov++;
676 cs->nr_segs--;
677 }
678 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
679 if (err < 0)
680 return err;
681 BUG_ON(err != 1);
682 offset = cs->addr % PAGE_SIZE;
7909b1c6 683 cs->mapaddr = kmap(cs->pg);
dd3bb14f
MS
684 cs->buf = cs->mapaddr + offset;
685 cs->len = min(PAGE_SIZE - offset, cs->seglen);
686 cs->seglen -= cs->len;
687 cs->addr += cs->len;
334f485d 688 }
334f485d 689
d7133114 690 return lock_request(cs->fc, cs->req);
334f485d
MS
691}
692
693/* Do as much copy to/from userspace buffer as we can */
8bfc016d 694static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
334f485d
MS
695{
696 unsigned ncpy = min(*size, cs->len);
697 if (val) {
698 if (cs->write)
699 memcpy(cs->buf, *val, ncpy);
700 else
701 memcpy(*val, cs->buf, ncpy);
702 *val += ncpy;
703 }
704 *size -= ncpy;
705 cs->len -= ncpy;
706 cs->buf += ncpy;
707 return ncpy;
708}
709
ce534fb0
MS
710static int fuse_check_page(struct page *page)
711{
712 if (page_mapcount(page) ||
713 page->mapping != NULL ||
714 page_count(page) != 1 ||
715 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
716 ~(1 << PG_locked |
717 1 << PG_referenced |
718 1 << PG_uptodate |
719 1 << PG_lru |
720 1 << PG_active |
721 1 << PG_reclaim))) {
722 printk(KERN_WARNING "fuse: trying to steal weird page\n");
723 printk(KERN_WARNING " page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page, page->index, page->flags, page_count(page), page_mapcount(page), page->mapping);
724 return 1;
725 }
726 return 0;
727}
728
729static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
730{
731 int err;
732 struct page *oldpage = *pagep;
733 struct page *newpage;
734 struct pipe_buffer *buf = cs->pipebufs;
ce534fb0
MS
735
736 unlock_request(cs->fc, cs->req);
737 fuse_copy_finish(cs);
738
739 err = buf->ops->confirm(cs->pipe, buf);
740 if (err)
741 return err;
742
743 BUG_ON(!cs->nr_segs);
744 cs->currbuf = buf;
745 cs->len = buf->len;
746 cs->pipebufs++;
747 cs->nr_segs--;
748
749 if (cs->len != PAGE_SIZE)
750 goto out_fallback;
751
752 if (buf->ops->steal(cs->pipe, buf) != 0)
753 goto out_fallback;
754
755 newpage = buf->page;
756
757 if (WARN_ON(!PageUptodate(newpage)))
758 return -EIO;
759
760 ClearPageMappedToDisk(newpage);
761
762 if (fuse_check_page(newpage) != 0)
763 goto out_fallback_unlock;
764
ce534fb0
MS
765 /*
766 * This is a new and locked page, it shouldn't be mapped or
767 * have any special flags on it
768 */
769 if (WARN_ON(page_mapped(oldpage)))
770 goto out_fallback_unlock;
771 if (WARN_ON(page_has_private(oldpage)))
772 goto out_fallback_unlock;
773 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
774 goto out_fallback_unlock;
775 if (WARN_ON(PageMlocked(oldpage)))
776 goto out_fallback_unlock;
777
ef6a3c63 778 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
ce534fb0 779 if (err) {
ef6a3c63
MS
780 unlock_page(newpage);
781 return err;
ce534fb0 782 }
ef6a3c63 783
ce534fb0
MS
784 page_cache_get(newpage);
785
786 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
787 lru_cache_add_file(newpage);
788
789 err = 0;
790 spin_lock(&cs->fc->lock);
791 if (cs->req->aborted)
792 err = -ENOENT;
793 else
794 *pagep = newpage;
795 spin_unlock(&cs->fc->lock);
796
797 if (err) {
798 unlock_page(newpage);
799 page_cache_release(newpage);
800 return err;
801 }
802
803 unlock_page(oldpage);
804 page_cache_release(oldpage);
805 cs->len = 0;
806
807 return 0;
808
809out_fallback_unlock:
810 unlock_page(newpage);
811out_fallback:
812 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
813 cs->buf = cs->mapaddr + buf->offset;
814
815 err = lock_request(cs->fc, cs->req);
816 if (err)
817 return err;
818
819 return 1;
820}
821
c3021629
MS
822static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
823 unsigned offset, unsigned count)
824{
825 struct pipe_buffer *buf;
826
827 if (cs->nr_segs == cs->pipe->buffers)
828 return -EIO;
829
830 unlock_request(cs->fc, cs->req);
831 fuse_copy_finish(cs);
832
833 buf = cs->pipebufs;
834 page_cache_get(page);
835 buf->page = page;
836 buf->offset = offset;
837 buf->len = count;
838
839 cs->pipebufs++;
840 cs->nr_segs++;
841 cs->len = 0;
842
843 return 0;
844}
845
334f485d
MS
846/*
847 * Copy a page in the request to/from the userspace buffer. Must be
848 * done atomically
849 */
ce534fb0 850static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
8bfc016d 851 unsigned offset, unsigned count, int zeroing)
334f485d 852{
ce534fb0
MS
853 int err;
854 struct page *page = *pagep;
855
b6777c40
MS
856 if (page && zeroing && count < PAGE_SIZE)
857 clear_highpage(page);
858
334f485d 859 while (count) {
c3021629
MS
860 if (cs->write && cs->pipebufs && page) {
861 return fuse_ref_page(cs, page, offset, count);
862 } else if (!cs->len) {
ce534fb0
MS
863 if (cs->move_pages && page &&
864 offset == 0 && count == PAGE_SIZE) {
865 err = fuse_try_move_page(cs, pagep);
866 if (err <= 0)
867 return err;
868 } else {
869 err = fuse_copy_fill(cs);
870 if (err)
871 return err;
872 }
1729a16c 873 }
334f485d 874 if (page) {
2408f6ef 875 void *mapaddr = kmap_atomic(page);
334f485d
MS
876 void *buf = mapaddr + offset;
877 offset += fuse_copy_do(cs, &buf, &count);
2408f6ef 878 kunmap_atomic(mapaddr);
334f485d
MS
879 } else
880 offset += fuse_copy_do(cs, NULL, &count);
881 }
882 if (page && !cs->write)
883 flush_dcache_page(page);
884 return 0;
885}
886
887/* Copy pages in the request to/from userspace buffer */
888static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
889 int zeroing)
890{
891 unsigned i;
892 struct fuse_req *req = cs->req;
893 unsigned offset = req->page_offset;
894 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
895
896 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
ce534fb0
MS
897 int err;
898
899 err = fuse_copy_page(cs, &req->pages[i], offset, count,
900 zeroing);
334f485d
MS
901 if (err)
902 return err;
903
904 nbytes -= count;
905 count = min(nbytes, (unsigned) PAGE_SIZE);
906 offset = 0;
907 }
908 return 0;
909}
910
911/* Copy a single argument in the request to/from userspace buffer */
912static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
913{
914 while (size) {
1729a16c
MS
915 if (!cs->len) {
916 int err = fuse_copy_fill(cs);
917 if (err)
918 return err;
919 }
334f485d
MS
920 fuse_copy_do(cs, &val, &size);
921 }
922 return 0;
923}
924
925/* Copy request arguments to/from userspace buffer */
926static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
927 unsigned argpages, struct fuse_arg *args,
928 int zeroing)
929{
930 int err = 0;
931 unsigned i;
932
933 for (i = 0; !err && i < numargs; i++) {
934 struct fuse_arg *arg = &args[i];
935 if (i == numargs - 1 && argpages)
936 err = fuse_copy_pages(cs, arg->size, zeroing);
937 else
938 err = fuse_copy_one(cs, arg->value, arg->size);
939 }
940 return err;
941}
942
07e77dca
MS
943static int forget_pending(struct fuse_conn *fc)
944{
945 return fc->forget_list_head.next != NULL;
946}
947
a4d27e75
MS
948static int request_pending(struct fuse_conn *fc)
949{
07e77dca
MS
950 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
951 forget_pending(fc);
a4d27e75
MS
952}
953
334f485d
MS
954/* Wait until a request is available on the pending list */
955static void request_wait(struct fuse_conn *fc)
b9ca67b2
MS
956__releases(fc->lock)
957__acquires(fc->lock)
334f485d
MS
958{
959 DECLARE_WAITQUEUE(wait, current);
960
961 add_wait_queue_exclusive(&fc->waitq, &wait);
a4d27e75 962 while (fc->connected && !request_pending(fc)) {
334f485d
MS
963 set_current_state(TASK_INTERRUPTIBLE);
964 if (signal_pending(current))
965 break;
966
d7133114 967 spin_unlock(&fc->lock);
334f485d 968 schedule();
d7133114 969 spin_lock(&fc->lock);
334f485d
MS
970 }
971 set_current_state(TASK_RUNNING);
972 remove_wait_queue(&fc->waitq, &wait);
973}
974
a4d27e75
MS
975/*
976 * Transfer an interrupt request to userspace
977 *
978 * Unlike other requests this is assembled on demand, without a need
979 * to allocate a separate fuse_req structure.
980 *
981 * Called with fc->lock held, releases it
982 */
c3021629
MS
983static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
984 size_t nbytes, struct fuse_req *req)
b9ca67b2 985__releases(fc->lock)
a4d27e75 986{
a4d27e75
MS
987 struct fuse_in_header ih;
988 struct fuse_interrupt_in arg;
989 unsigned reqsize = sizeof(ih) + sizeof(arg);
990 int err;
991
992 list_del_init(&req->intr_entry);
993 req->intr_unique = fuse_get_unique(fc);
994 memset(&ih, 0, sizeof(ih));
995 memset(&arg, 0, sizeof(arg));
996 ih.len = reqsize;
997 ih.opcode = FUSE_INTERRUPT;
998 ih.unique = req->intr_unique;
999 arg.unique = req->in.h.unique;
1000
1001 spin_unlock(&fc->lock);
c3021629 1002 if (nbytes < reqsize)
a4d27e75
MS
1003 return -EINVAL;
1004
c3021629 1005 err = fuse_copy_one(cs, &ih, sizeof(ih));
a4d27e75 1006 if (!err)
c3021629
MS
1007 err = fuse_copy_one(cs, &arg, sizeof(arg));
1008 fuse_copy_finish(cs);
a4d27e75
MS
1009
1010 return err ? err : reqsize;
1011}
1012
02c048b9
MS
1013static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
1014 unsigned max,
1015 unsigned *countp)
07e77dca 1016{
02c048b9
MS
1017 struct fuse_forget_link *head = fc->forget_list_head.next;
1018 struct fuse_forget_link **newhead = &head;
1019 unsigned count;
07e77dca 1020
02c048b9
MS
1021 for (count = 0; *newhead != NULL && count < max; count++)
1022 newhead = &(*newhead)->next;
1023
1024 fc->forget_list_head.next = *newhead;
1025 *newhead = NULL;
07e77dca
MS
1026 if (fc->forget_list_head.next == NULL)
1027 fc->forget_list_tail = &fc->forget_list_head;
1028
02c048b9
MS
1029 if (countp != NULL)
1030 *countp = count;
1031
1032 return head;
07e77dca
MS
1033}
1034
1035static int fuse_read_single_forget(struct fuse_conn *fc,
1036 struct fuse_copy_state *cs,
1037 size_t nbytes)
1038__releases(fc->lock)
1039{
1040 int err;
02c048b9 1041 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
07e77dca 1042 struct fuse_forget_in arg = {
02c048b9 1043 .nlookup = forget->forget_one.nlookup,
07e77dca
MS
1044 };
1045 struct fuse_in_header ih = {
1046 .opcode = FUSE_FORGET,
02c048b9 1047 .nodeid = forget->forget_one.nodeid,
07e77dca
MS
1048 .unique = fuse_get_unique(fc),
1049 .len = sizeof(ih) + sizeof(arg),
1050 };
1051
1052 spin_unlock(&fc->lock);
1053 kfree(forget);
1054 if (nbytes < ih.len)
1055 return -EINVAL;
1056
1057 err = fuse_copy_one(cs, &ih, sizeof(ih));
1058 if (!err)
1059 err = fuse_copy_one(cs, &arg, sizeof(arg));
1060 fuse_copy_finish(cs);
1061
1062 if (err)
1063 return err;
1064
1065 return ih.len;
1066}
1067
02c048b9
MS
1068static int fuse_read_batch_forget(struct fuse_conn *fc,
1069 struct fuse_copy_state *cs, size_t nbytes)
1070__releases(fc->lock)
1071{
1072 int err;
1073 unsigned max_forgets;
1074 unsigned count;
1075 struct fuse_forget_link *head;
1076 struct fuse_batch_forget_in arg = { .count = 0 };
1077 struct fuse_in_header ih = {
1078 .opcode = FUSE_BATCH_FORGET,
1079 .unique = fuse_get_unique(fc),
1080 .len = sizeof(ih) + sizeof(arg),
1081 };
1082
1083 if (nbytes < ih.len) {
1084 spin_unlock(&fc->lock);
1085 return -EINVAL;
1086 }
1087
1088 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1089 head = dequeue_forget(fc, max_forgets, &count);
1090 spin_unlock(&fc->lock);
1091
1092 arg.count = count;
1093 ih.len += count * sizeof(struct fuse_forget_one);
1094 err = fuse_copy_one(cs, &ih, sizeof(ih));
1095 if (!err)
1096 err = fuse_copy_one(cs, &arg, sizeof(arg));
1097
1098 while (head) {
1099 struct fuse_forget_link *forget = head;
1100
1101 if (!err) {
1102 err = fuse_copy_one(cs, &forget->forget_one,
1103 sizeof(forget->forget_one));
1104 }
1105 head = forget->next;
1106 kfree(forget);
1107 }
1108
1109 fuse_copy_finish(cs);
1110
1111 if (err)
1112 return err;
1113
1114 return ih.len;
1115}
1116
1117static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1118 size_t nbytes)
1119__releases(fc->lock)
1120{
1121 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1122 return fuse_read_single_forget(fc, cs, nbytes);
1123 else
1124 return fuse_read_batch_forget(fc, cs, nbytes);
1125}
1126
334f485d
MS
1127/*
1128 * Read a single request into the userspace filesystem's buffer. This
1129 * function waits until a request is available, then removes it from
1130 * the pending list and copies request data to userspace buffer. If
f9a2842e
MS
1131 * no reply is needed (FORGET) or request has been aborted or there
1132 * was an error during the copying then it's finished by calling
334f485d
MS
1133 * request_end(). Otherwise add it to the processing list, and set
1134 * the 'sent' flag.
1135 */
c3021629
MS
1136static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1137 struct fuse_copy_state *cs, size_t nbytes)
334f485d
MS
1138{
1139 int err;
334f485d
MS
1140 struct fuse_req *req;
1141 struct fuse_in *in;
334f485d
MS
1142 unsigned reqsize;
1143
1d3d752b 1144 restart:
d7133114 1145 spin_lock(&fc->lock);
e5ac1d1e
JD
1146 err = -EAGAIN;
1147 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
a4d27e75 1148 !request_pending(fc))
e5ac1d1e
JD
1149 goto err_unlock;
1150
334f485d
MS
1151 request_wait(fc);
1152 err = -ENODEV;
9ba7cbba 1153 if (!fc->connected)
334f485d
MS
1154 goto err_unlock;
1155 err = -ERESTARTSYS;
a4d27e75 1156 if (!request_pending(fc))
334f485d
MS
1157 goto err_unlock;
1158
a4d27e75
MS
1159 if (!list_empty(&fc->interrupts)) {
1160 req = list_entry(fc->interrupts.next, struct fuse_req,
1161 intr_entry);
c3021629 1162 return fuse_read_interrupt(fc, cs, nbytes, req);
a4d27e75
MS
1163 }
1164
07e77dca
MS
1165 if (forget_pending(fc)) {
1166 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
02c048b9 1167 return fuse_read_forget(fc, cs, nbytes);
07e77dca
MS
1168
1169 if (fc->forget_batch <= -8)
1170 fc->forget_batch = 16;
1171 }
1172
334f485d 1173 req = list_entry(fc->pending.next, struct fuse_req, list);
83cfd493 1174 req->state = FUSE_REQ_READING;
d77a1d5b 1175 list_move(&req->list, &fc->io);
334f485d
MS
1176
1177 in = &req->in;
1d3d752b
MS
1178 reqsize = in->h.len;
1179 /* If request is too large, reply with an error and restart the read */
c3021629 1180 if (nbytes < reqsize) {
1d3d752b
MS
1181 req->out.h.error = -EIO;
1182 /* SETXATTR is special, since it may contain too large data */
1183 if (in->h.opcode == FUSE_SETXATTR)
1184 req->out.h.error = -E2BIG;
1185 request_end(fc, req);
1186 goto restart;
334f485d 1187 }
d7133114 1188 spin_unlock(&fc->lock);
c3021629
MS
1189 cs->req = req;
1190 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1d3d752b 1191 if (!err)
c3021629 1192 err = fuse_copy_args(cs, in->numargs, in->argpages,
1d3d752b 1193 (struct fuse_arg *) in->args, 0);
c3021629 1194 fuse_copy_finish(cs);
d7133114 1195 spin_lock(&fc->lock);
334f485d 1196 req->locked = 0;
c9c9d7df
MS
1197 if (req->aborted) {
1198 request_end(fc, req);
1199 return -ENODEV;
1200 }
334f485d 1201 if (err) {
c9c9d7df 1202 req->out.h.error = -EIO;
334f485d
MS
1203 request_end(fc, req);
1204 return err;
1205 }
1206 if (!req->isreply)
1207 request_end(fc, req);
1208 else {
83cfd493 1209 req->state = FUSE_REQ_SENT;
d77a1d5b 1210 list_move_tail(&req->list, &fc->processing);
a4d27e75
MS
1211 if (req->interrupted)
1212 queue_interrupt(fc, req);
d7133114 1213 spin_unlock(&fc->lock);
334f485d
MS
1214 }
1215 return reqsize;
1216
1217 err_unlock:
d7133114 1218 spin_unlock(&fc->lock);
334f485d
MS
1219 return err;
1220}
1221
c3021629
MS
1222static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1223 unsigned long nr_segs, loff_t pos)
1224{
1225 struct fuse_copy_state cs;
1226 struct file *file = iocb->ki_filp;
1227 struct fuse_conn *fc = fuse_get_conn(file);
1228 if (!fc)
1229 return -EPERM;
1230
1231 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1232
1233 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1234}
1235
1236static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1237 struct pipe_buffer *buf)
1238{
1239 return 1;
1240}
1241
1242static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1243 .can_merge = 0,
1244 .map = generic_pipe_buf_map,
1245 .unmap = generic_pipe_buf_unmap,
1246 .confirm = generic_pipe_buf_confirm,
1247 .release = generic_pipe_buf_release,
1248 .steal = fuse_dev_pipe_buf_steal,
1249 .get = generic_pipe_buf_get,
1250};
1251
1252static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1253 struct pipe_inode_info *pipe,
1254 size_t len, unsigned int flags)
1255{
1256 int ret;
1257 int page_nr = 0;
1258 int do_wakeup = 0;
1259 struct pipe_buffer *bufs;
1260 struct fuse_copy_state cs;
1261 struct fuse_conn *fc = fuse_get_conn(in);
1262 if (!fc)
1263 return -EPERM;
1264
07e77dca 1265 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
c3021629
MS
1266 if (!bufs)
1267 return -ENOMEM;
1268
1269 fuse_copy_init(&cs, fc, 1, NULL, 0);
1270 cs.pipebufs = bufs;
1271 cs.pipe = pipe;
1272 ret = fuse_dev_do_read(fc, in, &cs, len);
1273 if (ret < 0)
1274 goto out;
1275
1276 ret = 0;
1277 pipe_lock(pipe);
1278
1279 if (!pipe->readers) {
1280 send_sig(SIGPIPE, current, 0);
1281 if (!ret)
1282 ret = -EPIPE;
1283 goto out_unlock;
1284 }
1285
1286 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1287 ret = -EIO;
1288 goto out_unlock;
1289 }
1290
1291 while (page_nr < cs.nr_segs) {
1292 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1293 struct pipe_buffer *buf = pipe->bufs + newbuf;
1294
1295 buf->page = bufs[page_nr].page;
1296 buf->offset = bufs[page_nr].offset;
1297 buf->len = bufs[page_nr].len;
1298 buf->ops = &fuse_dev_pipe_buf_ops;
1299
1300 pipe->nrbufs++;
1301 page_nr++;
1302 ret += buf->len;
1303
1304 if (pipe->inode)
1305 do_wakeup = 1;
1306 }
1307
1308out_unlock:
1309 pipe_unlock(pipe);
1310
1311 if (do_wakeup) {
1312 smp_mb();
1313 if (waitqueue_active(&pipe->wait))
1314 wake_up_interruptible(&pipe->wait);
1315 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1316 }
1317
1318out:
1319 for (; page_nr < cs.nr_segs; page_nr++)
1320 page_cache_release(bufs[page_nr].page);
1321
1322 kfree(bufs);
1323 return ret;
1324}
1325
95668a69
TH
1326static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1327 struct fuse_copy_state *cs)
1328{
1329 struct fuse_notify_poll_wakeup_out outarg;
f6d47a17 1330 int err = -EINVAL;
95668a69
TH
1331
1332 if (size != sizeof(outarg))
f6d47a17 1333 goto err;
95668a69
TH
1334
1335 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1336 if (err)
f6d47a17 1337 goto err;
95668a69 1338
f6d47a17 1339 fuse_copy_finish(cs);
95668a69 1340 return fuse_notify_poll_wakeup(fc, &outarg);
f6d47a17
MS
1341
1342err:
1343 fuse_copy_finish(cs);
1344 return err;
95668a69
TH
1345}
1346
3b463ae0
JM
1347static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1348 struct fuse_copy_state *cs)
1349{
1350 struct fuse_notify_inval_inode_out outarg;
1351 int err = -EINVAL;
1352
1353 if (size != sizeof(outarg))
1354 goto err;
1355
1356 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1357 if (err)
1358 goto err;
1359 fuse_copy_finish(cs);
1360
1361 down_read(&fc->killsb);
1362 err = -ENOENT;
b21dda43
MS
1363 if (fc->sb) {
1364 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1365 outarg.off, outarg.len);
1366 }
3b463ae0
JM
1367 up_read(&fc->killsb);
1368 return err;
1369
1370err:
1371 fuse_copy_finish(cs);
1372 return err;
1373}
1374
1375static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1376 struct fuse_copy_state *cs)
1377{
1378 struct fuse_notify_inval_entry_out outarg;
b2d82ee3
FW
1379 int err = -ENOMEM;
1380 char *buf;
3b463ae0
JM
1381 struct qstr name;
1382
b2d82ee3
FW
1383 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1384 if (!buf)
1385 goto err;
1386
1387 err = -EINVAL;
3b463ae0
JM
1388 if (size < sizeof(outarg))
1389 goto err;
1390
1391 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1392 if (err)
1393 goto err;
1394
1395 err = -ENAMETOOLONG;
1396 if (outarg.namelen > FUSE_NAME_MAX)
1397 goto err;
1398
c2183d1e
MS
1399 err = -EINVAL;
1400 if (size != sizeof(outarg) + outarg.namelen + 1)
1401 goto err;
1402
3b463ae0
JM
1403 name.name = buf;
1404 name.len = outarg.namelen;
1405 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1406 if (err)
1407 goto err;
1408 fuse_copy_finish(cs);
1409 buf[outarg.namelen] = 0;
1410 name.hash = full_name_hash(name.name, name.len);
1411
1412 down_read(&fc->killsb);
1413 err = -ENOENT;
b21dda43 1414 if (fc->sb)
451d0f59
JM
1415 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1416 up_read(&fc->killsb);
1417 kfree(buf);
1418 return err;
1419
1420err:
1421 kfree(buf);
1422 fuse_copy_finish(cs);
1423 return err;
1424}
1425
1426static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1427 struct fuse_copy_state *cs)
1428{
1429 struct fuse_notify_delete_out outarg;
1430 int err = -ENOMEM;
1431 char *buf;
1432 struct qstr name;
1433
1434 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1435 if (!buf)
1436 goto err;
1437
1438 err = -EINVAL;
1439 if (size < sizeof(outarg))
1440 goto err;
1441
1442 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1443 if (err)
1444 goto err;
1445
1446 err = -ENAMETOOLONG;
1447 if (outarg.namelen > FUSE_NAME_MAX)
1448 goto err;
1449
1450 err = -EINVAL;
1451 if (size != sizeof(outarg) + outarg.namelen + 1)
1452 goto err;
1453
1454 name.name = buf;
1455 name.len = outarg.namelen;
1456 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1457 if (err)
1458 goto err;
1459 fuse_copy_finish(cs);
1460 buf[outarg.namelen] = 0;
1461 name.hash = full_name_hash(name.name, name.len);
1462
1463 down_read(&fc->killsb);
1464 err = -ENOENT;
1465 if (fc->sb)
1466 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1467 outarg.child, &name);
3b463ae0 1468 up_read(&fc->killsb);
b2d82ee3 1469 kfree(buf);
3b463ae0
JM
1470 return err;
1471
1472err:
b2d82ee3 1473 kfree(buf);
3b463ae0
JM
1474 fuse_copy_finish(cs);
1475 return err;
1476}
1477
a1d75f25
MS
1478static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1479 struct fuse_copy_state *cs)
1480{
1481 struct fuse_notify_store_out outarg;
1482 struct inode *inode;
1483 struct address_space *mapping;
1484 u64 nodeid;
1485 int err;
1486 pgoff_t index;
1487 unsigned int offset;
1488 unsigned int num;
1489 loff_t file_size;
1490 loff_t end;
1491
1492 err = -EINVAL;
1493 if (size < sizeof(outarg))
1494 goto out_finish;
1495
1496 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1497 if (err)
1498 goto out_finish;
1499
1500 err = -EINVAL;
1501 if (size - sizeof(outarg) != outarg.size)
1502 goto out_finish;
1503
1504 nodeid = outarg.nodeid;
1505
1506 down_read(&fc->killsb);
1507
1508 err = -ENOENT;
1509 if (!fc->sb)
1510 goto out_up_killsb;
1511
1512 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1513 if (!inode)
1514 goto out_up_killsb;
1515
1516 mapping = inode->i_mapping;
1517 index = outarg.offset >> PAGE_CACHE_SHIFT;
1518 offset = outarg.offset & ~PAGE_CACHE_MASK;
1519 file_size = i_size_read(inode);
1520 end = outarg.offset + outarg.size;
1521 if (end > file_size) {
1522 file_size = end;
1523 fuse_write_update_size(inode, file_size);
1524 }
1525
1526 num = outarg.size;
1527 while (num) {
1528 struct page *page;
1529 unsigned int this_num;
1530
1531 err = -ENOMEM;
1532 page = find_or_create_page(mapping, index,
1533 mapping_gfp_mask(mapping));
1534 if (!page)
1535 goto out_iput;
1536
1537 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1538 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1539 if (!err && offset == 0 && (num != 0 || file_size == end))
1540 SetPageUptodate(page);
1541 unlock_page(page);
1542 page_cache_release(page);
1543
1544 if (err)
1545 goto out_iput;
1546
1547 num -= this_num;
1548 offset = 0;
1549 index++;
1550 }
1551
1552 err = 0;
1553
1554out_iput:
1555 iput(inode);
1556out_up_killsb:
1557 up_read(&fc->killsb);
1558out_finish:
1559 fuse_copy_finish(cs);
1560 return err;
1561}
1562
2d45ba38
MS
1563static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1564{
0be8557b 1565 release_pages(req->pages, req->num_pages, 0);
2d45ba38
MS
1566}
1567
1568static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1569 struct fuse_notify_retrieve_out *outarg)
1570{
1571 int err;
1572 struct address_space *mapping = inode->i_mapping;
1573 struct fuse_req *req;
1574 pgoff_t index;
1575 loff_t file_size;
1576 unsigned int num;
1577 unsigned int offset;
0157443c 1578 size_t total_len = 0;
2d45ba38
MS
1579
1580 req = fuse_get_req(fc);
1581 if (IS_ERR(req))
1582 return PTR_ERR(req);
1583
1584 offset = outarg->offset & ~PAGE_CACHE_MASK;
1585
1586 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1587 req->in.h.nodeid = outarg->nodeid;
1588 req->in.numargs = 2;
1589 req->in.argpages = 1;
1590 req->page_offset = offset;
1591 req->end = fuse_retrieve_end;
1592
1593 index = outarg->offset >> PAGE_CACHE_SHIFT;
1594 file_size = i_size_read(inode);
1595 num = outarg->size;
1596 if (outarg->offset > file_size)
1597 num = 0;
1598 else if (outarg->offset + num > file_size)
1599 num = file_size - outarg->offset;
1600
48706d0a 1601 while (num && req->num_pages < FUSE_MAX_PAGES_PER_REQ) {
2d45ba38
MS
1602 struct page *page;
1603 unsigned int this_num;
1604
1605 page = find_get_page(mapping, index);
1606 if (!page)
1607 break;
1608
1609 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1610 req->pages[req->num_pages] = page;
1611 req->num_pages++;
1612
c9e67d48 1613 offset = 0;
2d45ba38
MS
1614 num -= this_num;
1615 total_len += this_num;
48706d0a 1616 index++;
2d45ba38
MS
1617 }
1618 req->misc.retrieve_in.offset = outarg->offset;
1619 req->misc.retrieve_in.size = total_len;
1620 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1621 req->in.args[0].value = &req->misc.retrieve_in;
1622 req->in.args[1].size = total_len;
1623
1624 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1625 if (err)
1626 fuse_retrieve_end(fc, req);
1627
1628 return err;
1629}
1630
1631static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1632 struct fuse_copy_state *cs)
1633{
1634 struct fuse_notify_retrieve_out outarg;
1635 struct inode *inode;
1636 int err;
1637
1638 err = -EINVAL;
1639 if (size != sizeof(outarg))
1640 goto copy_finish;
1641
1642 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1643 if (err)
1644 goto copy_finish;
1645
1646 fuse_copy_finish(cs);
1647
1648 down_read(&fc->killsb);
1649 err = -ENOENT;
1650 if (fc->sb) {
1651 u64 nodeid = outarg.nodeid;
1652
1653 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1654 if (inode) {
1655 err = fuse_retrieve(fc, inode, &outarg);
1656 iput(inode);
1657 }
1658 }
1659 up_read(&fc->killsb);
1660
1661 return err;
1662
1663copy_finish:
1664 fuse_copy_finish(cs);
1665 return err;
1666}
1667
8599396b
TH
1668static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1669 unsigned int size, struct fuse_copy_state *cs)
1670{
1671 switch (code) {
95668a69
TH
1672 case FUSE_NOTIFY_POLL:
1673 return fuse_notify_poll(fc, size, cs);
1674
3b463ae0
JM
1675 case FUSE_NOTIFY_INVAL_INODE:
1676 return fuse_notify_inval_inode(fc, size, cs);
1677
1678 case FUSE_NOTIFY_INVAL_ENTRY:
1679 return fuse_notify_inval_entry(fc, size, cs);
1680
a1d75f25
MS
1681 case FUSE_NOTIFY_STORE:
1682 return fuse_notify_store(fc, size, cs);
1683
2d45ba38
MS
1684 case FUSE_NOTIFY_RETRIEVE:
1685 return fuse_notify_retrieve(fc, size, cs);
1686
451d0f59
JM
1687 case FUSE_NOTIFY_DELETE:
1688 return fuse_notify_delete(fc, size, cs);
1689
8599396b 1690 default:
f6d47a17 1691 fuse_copy_finish(cs);
8599396b
TH
1692 return -EINVAL;
1693 }
1694}
1695
334f485d
MS
1696/* Look up request on processing list by unique ID */
1697static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1698{
1699 struct list_head *entry;
1700
1701 list_for_each(entry, &fc->processing) {
1702 struct fuse_req *req;
1703 req = list_entry(entry, struct fuse_req, list);
a4d27e75 1704 if (req->in.h.unique == unique || req->intr_unique == unique)
334f485d
MS
1705 return req;
1706 }
1707 return NULL;
1708}
1709
1710static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1711 unsigned nbytes)
1712{
1713 unsigned reqsize = sizeof(struct fuse_out_header);
1714
1715 if (out->h.error)
1716 return nbytes != reqsize ? -EINVAL : 0;
1717
1718 reqsize += len_args(out->numargs, out->args);
1719
1720 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1721 return -EINVAL;
1722 else if (reqsize > nbytes) {
1723 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1724 unsigned diffsize = reqsize - nbytes;
1725 if (diffsize > lastarg->size)
1726 return -EINVAL;
1727 lastarg->size -= diffsize;
1728 }
1729 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1730 out->page_zeroing);
1731}
1732
1733/*
1734 * Write a single reply to a request. First the header is copied from
1735 * the write buffer. The request is then searched on the processing
1736 * list by the unique ID found in the header. If found, then remove
1737 * it from the list and copy the rest of the buffer to the request.
1738 * The request is finished by calling request_end()
1739 */
dd3bb14f
MS
1740static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1741 struct fuse_copy_state *cs, size_t nbytes)
334f485d
MS
1742{
1743 int err;
334f485d
MS
1744 struct fuse_req *req;
1745 struct fuse_out_header oh;
334f485d 1746
334f485d
MS
1747 if (nbytes < sizeof(struct fuse_out_header))
1748 return -EINVAL;
1749
dd3bb14f 1750 err = fuse_copy_one(cs, &oh, sizeof(oh));
334f485d
MS
1751 if (err)
1752 goto err_finish;
8599396b
TH
1753
1754 err = -EINVAL;
1755 if (oh.len != nbytes)
1756 goto err_finish;
1757
1758 /*
1759 * Zero oh.unique indicates unsolicited notification message
1760 * and error contains notification code.
1761 */
1762 if (!oh.unique) {
dd3bb14f 1763 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
8599396b
TH
1764 return err ? err : nbytes;
1765 }
1766
334f485d 1767 err = -EINVAL;
8599396b 1768 if (oh.error <= -1000 || oh.error > 0)
334f485d
MS
1769 goto err_finish;
1770
d7133114 1771 spin_lock(&fc->lock);
69a53bf2
MS
1772 err = -ENOENT;
1773 if (!fc->connected)
1774 goto err_unlock;
1775
334f485d 1776 req = request_find(fc, oh.unique);
334f485d
MS
1777 if (!req)
1778 goto err_unlock;
1779
f9a2842e 1780 if (req->aborted) {
d7133114 1781 spin_unlock(&fc->lock);
dd3bb14f 1782 fuse_copy_finish(cs);
d7133114 1783 spin_lock(&fc->lock);
222f1d69 1784 request_end(fc, req);
334f485d
MS
1785 return -ENOENT;
1786 }
a4d27e75
MS
1787 /* Is it an interrupt reply? */
1788 if (req->intr_unique == oh.unique) {
1789 err = -EINVAL;
1790 if (nbytes != sizeof(struct fuse_out_header))
1791 goto err_unlock;
1792
1793 if (oh.error == -ENOSYS)
1794 fc->no_interrupt = 1;
1795 else if (oh.error == -EAGAIN)
1796 queue_interrupt(fc, req);
1797
1798 spin_unlock(&fc->lock);
dd3bb14f 1799 fuse_copy_finish(cs);
a4d27e75
MS
1800 return nbytes;
1801 }
1802
1803 req->state = FUSE_REQ_WRITING;
d77a1d5b 1804 list_move(&req->list, &fc->io);
334f485d
MS
1805 req->out.h = oh;
1806 req->locked = 1;
dd3bb14f 1807 cs->req = req;
ce534fb0
MS
1808 if (!req->out.page_replace)
1809 cs->move_pages = 0;
d7133114 1810 spin_unlock(&fc->lock);
334f485d 1811
dd3bb14f
MS
1812 err = copy_out_args(cs, &req->out, nbytes);
1813 fuse_copy_finish(cs);
334f485d 1814
d7133114 1815 spin_lock(&fc->lock);
334f485d
MS
1816 req->locked = 0;
1817 if (!err) {
f9a2842e 1818 if (req->aborted)
334f485d 1819 err = -ENOENT;
f9a2842e 1820 } else if (!req->aborted)
334f485d
MS
1821 req->out.h.error = -EIO;
1822 request_end(fc, req);
1823
1824 return err ? err : nbytes;
1825
1826 err_unlock:
d7133114 1827 spin_unlock(&fc->lock);
334f485d 1828 err_finish:
dd3bb14f 1829 fuse_copy_finish(cs);
334f485d
MS
1830 return err;
1831}
1832
dd3bb14f
MS
1833static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1834 unsigned long nr_segs, loff_t pos)
1835{
1836 struct fuse_copy_state cs;
1837 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1838 if (!fc)
1839 return -EPERM;
1840
c3021629 1841 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
dd3bb14f
MS
1842
1843 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1844}
1845
1846static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1847 struct file *out, loff_t *ppos,
1848 size_t len, unsigned int flags)
1849{
1850 unsigned nbuf;
1851 unsigned idx;
1852 struct pipe_buffer *bufs;
1853 struct fuse_copy_state cs;
1854 struct fuse_conn *fc;
1855 size_t rem;
1856 ssize_t ret;
1857
1858 fc = fuse_get_conn(out);
1859 if (!fc)
1860 return -EPERM;
1861
07e77dca 1862 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
dd3bb14f
MS
1863 if (!bufs)
1864 return -ENOMEM;
1865
1866 pipe_lock(pipe);
1867 nbuf = 0;
1868 rem = 0;
1869 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1870 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1871
1872 ret = -EINVAL;
1873 if (rem < len) {
1874 pipe_unlock(pipe);
1875 goto out;
1876 }
1877
1878 rem = len;
1879 while (rem) {
1880 struct pipe_buffer *ibuf;
1881 struct pipe_buffer *obuf;
1882
1883 BUG_ON(nbuf >= pipe->buffers);
1884 BUG_ON(!pipe->nrbufs);
1885 ibuf = &pipe->bufs[pipe->curbuf];
1886 obuf = &bufs[nbuf];
1887
1888 if (rem >= ibuf->len) {
1889 *obuf = *ibuf;
1890 ibuf->ops = NULL;
1891 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1892 pipe->nrbufs--;
1893 } else {
1894 ibuf->ops->get(pipe, ibuf);
1895 *obuf = *ibuf;
1896 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1897 obuf->len = rem;
1898 ibuf->offset += obuf->len;
1899 ibuf->len -= obuf->len;
1900 }
1901 nbuf++;
1902 rem -= obuf->len;
1903 }
1904 pipe_unlock(pipe);
1905
c3021629 1906 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
dd3bb14f 1907 cs.pipebufs = bufs;
dd3bb14f
MS
1908 cs.pipe = pipe;
1909
ce534fb0
MS
1910 if (flags & SPLICE_F_MOVE)
1911 cs.move_pages = 1;
1912
dd3bb14f
MS
1913 ret = fuse_dev_do_write(fc, &cs, len);
1914
1915 for (idx = 0; idx < nbuf; idx++) {
1916 struct pipe_buffer *buf = &bufs[idx];
1917 buf->ops->release(pipe, buf);
1918 }
1919out:
1920 kfree(bufs);
1921 return ret;
1922}
1923
334f485d
MS
1924static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1925{
334f485d 1926 unsigned mask = POLLOUT | POLLWRNORM;
7025d9ad 1927 struct fuse_conn *fc = fuse_get_conn(file);
334f485d 1928 if (!fc)
7025d9ad 1929 return POLLERR;
334f485d
MS
1930
1931 poll_wait(file, &fc->waitq, wait);
1932
d7133114 1933 spin_lock(&fc->lock);
7025d9ad
MS
1934 if (!fc->connected)
1935 mask = POLLERR;
a4d27e75 1936 else if (request_pending(fc))
7025d9ad 1937 mask |= POLLIN | POLLRDNORM;
d7133114 1938 spin_unlock(&fc->lock);
334f485d
MS
1939
1940 return mask;
1941}
1942
69a53bf2
MS
1943/*
1944 * Abort all requests on the given list (pending or processing)
1945 *
d7133114 1946 * This function releases and reacquires fc->lock
69a53bf2 1947 */
334f485d 1948static void end_requests(struct fuse_conn *fc, struct list_head *head)
b9ca67b2
MS
1949__releases(fc->lock)
1950__acquires(fc->lock)
334f485d
MS
1951{
1952 while (!list_empty(head)) {
1953 struct fuse_req *req;
1954 req = list_entry(head->next, struct fuse_req, list);
334f485d
MS
1955 req->out.h.error = -ECONNABORTED;
1956 request_end(fc, req);
d7133114 1957 spin_lock(&fc->lock);
334f485d
MS
1958 }
1959}
1960
69a53bf2
MS
1961/*
1962 * Abort requests under I/O
1963 *
f9a2842e 1964 * The requests are set to aborted and finished, and the request
69a53bf2
MS
1965 * waiter is woken up. This will make request_wait_answer() wait
1966 * until the request is unlocked and then return.
64c6d8ed
MS
1967 *
1968 * If the request is asynchronous, then the end function needs to be
1969 * called after waiting for the request to be unlocked (if it was
1970 * locked).
69a53bf2
MS
1971 */
1972static void end_io_requests(struct fuse_conn *fc)
b9ca67b2
MS
1973__releases(fc->lock)
1974__acquires(fc->lock)
69a53bf2
MS
1975{
1976 while (!list_empty(&fc->io)) {
64c6d8ed
MS
1977 struct fuse_req *req =
1978 list_entry(fc->io.next, struct fuse_req, list);
1979 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1980
f9a2842e 1981 req->aborted = 1;
69a53bf2
MS
1982 req->out.h.error = -ECONNABORTED;
1983 req->state = FUSE_REQ_FINISHED;
1984 list_del_init(&req->list);
1985 wake_up(&req->waitq);
64c6d8ed
MS
1986 if (end) {
1987 req->end = NULL;
64c6d8ed 1988 __fuse_get_request(req);
d7133114 1989 spin_unlock(&fc->lock);
64c6d8ed
MS
1990 wait_event(req->waitq, !req->locked);
1991 end(fc, req);
e9bb09dd 1992 fuse_put_request(fc, req);
d7133114 1993 spin_lock(&fc->lock);
64c6d8ed 1994 }
69a53bf2
MS
1995 }
1996}
1997
595afaf9 1998static void end_queued_requests(struct fuse_conn *fc)
b9ca67b2
MS
1999__releases(fc->lock)
2000__acquires(fc->lock)
595afaf9
MS
2001{
2002 fc->max_background = UINT_MAX;
2003 flush_bg_queue(fc);
2004 end_requests(fc, &fc->pending);
2005 end_requests(fc, &fc->processing);
07e77dca 2006 while (forget_pending(fc))
02c048b9 2007 kfree(dequeue_forget(fc, 1, NULL));
595afaf9
MS
2008}
2009
357ccf2b
BG
2010static void end_polls(struct fuse_conn *fc)
2011{
2012 struct rb_node *p;
2013
2014 p = rb_first(&fc->polled_files);
2015
2016 while (p) {
2017 struct fuse_file *ff;
2018 ff = rb_entry(p, struct fuse_file, polled_node);
2019 wake_up_interruptible_all(&ff->poll_wait);
2020
2021 p = rb_next(p);
2022 }
2023}
2024
69a53bf2
MS
2025/*
2026 * Abort all requests.
2027 *
2028 * Emergency exit in case of a malicious or accidental deadlock, or
2029 * just a hung filesystem.
2030 *
2031 * The same effect is usually achievable through killing the
2032 * filesystem daemon and all users of the filesystem. The exception
2033 * is the combination of an asynchronous request and the tricky
2034 * deadlock (see Documentation/filesystems/fuse.txt).
2035 *
2036 * During the aborting, progression of requests from the pending and
2037 * processing lists onto the io list, and progression of new requests
2038 * onto the pending list is prevented by req->connected being false.
2039 *
2040 * Progression of requests under I/O to the processing list is
f9a2842e
MS
2041 * prevented by the req->aborted flag being true for these requests.
2042 * For this reason requests on the io list must be aborted first.
69a53bf2
MS
2043 */
2044void fuse_abort_conn(struct fuse_conn *fc)
2045{
d7133114 2046 spin_lock(&fc->lock);
69a53bf2
MS
2047 if (fc->connected) {
2048 fc->connected = 0;
51eb01e7 2049 fc->blocked = 0;
69a53bf2 2050 end_io_requests(fc);
595afaf9 2051 end_queued_requests(fc);
357ccf2b 2052 end_polls(fc);
69a53bf2 2053 wake_up_all(&fc->waitq);
51eb01e7 2054 wake_up_all(&fc->blocked_waitq);
385a17bf 2055 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
69a53bf2 2056 }
d7133114 2057 spin_unlock(&fc->lock);
69a53bf2 2058}
08cbf542 2059EXPORT_SYMBOL_GPL(fuse_abort_conn);
69a53bf2 2060
08cbf542 2061int fuse_dev_release(struct inode *inode, struct file *file)
334f485d 2062{
0720b315 2063 struct fuse_conn *fc = fuse_get_conn(file);
334f485d 2064 if (fc) {
d7133114 2065 spin_lock(&fc->lock);
1e9a4ed9 2066 fc->connected = 0;
595afaf9
MS
2067 fc->blocked = 0;
2068 end_queued_requests(fc);
357ccf2b 2069 end_polls(fc);
595afaf9 2070 wake_up_all(&fc->blocked_waitq);
d7133114 2071 spin_unlock(&fc->lock);
bafa9654 2072 fuse_conn_put(fc);
385a17bf 2073 }
f543f253 2074
334f485d
MS
2075 return 0;
2076}
08cbf542 2077EXPORT_SYMBOL_GPL(fuse_dev_release);
334f485d 2078
385a17bf
JD
2079static int fuse_dev_fasync(int fd, struct file *file, int on)
2080{
2081 struct fuse_conn *fc = fuse_get_conn(file);
2082 if (!fc)
a87046d8 2083 return -EPERM;
385a17bf
JD
2084
2085 /* No locking - fasync_helper does its own locking */
2086 return fasync_helper(fd, file, on, &fc->fasync);
2087}
2088
4b6f5d20 2089const struct file_operations fuse_dev_operations = {
334f485d
MS
2090 .owner = THIS_MODULE,
2091 .llseek = no_llseek,
ee0b3e67
BP
2092 .read = do_sync_read,
2093 .aio_read = fuse_dev_read,
c3021629 2094 .splice_read = fuse_dev_splice_read,
ee0b3e67
BP
2095 .write = do_sync_write,
2096 .aio_write = fuse_dev_write,
dd3bb14f 2097 .splice_write = fuse_dev_splice_write,
334f485d
MS
2098 .poll = fuse_dev_poll,
2099 .release = fuse_dev_release,
385a17bf 2100 .fasync = fuse_dev_fasync,
334f485d 2101};
08cbf542 2102EXPORT_SYMBOL_GPL(fuse_dev_operations);
334f485d
MS
2103
2104static struct miscdevice fuse_miscdevice = {
2105 .minor = FUSE_MINOR,
2106 .name = "fuse",
2107 .fops = &fuse_dev_operations,
2108};
2109
2110int __init fuse_dev_init(void)
2111{
2112 int err = -ENOMEM;
2113 fuse_req_cachep = kmem_cache_create("fuse_request",
2114 sizeof(struct fuse_req),
20c2df83 2115 0, 0, NULL);
334f485d
MS
2116 if (!fuse_req_cachep)
2117 goto out;
2118
2119 err = misc_register(&fuse_miscdevice);
2120 if (err)
2121 goto out_cache_clean;
2122
2123 return 0;
2124
2125 out_cache_clean:
2126 kmem_cache_destroy(fuse_req_cachep);
2127 out:
2128 return err;
2129}
2130
2131void fuse_dev_cleanup(void)
2132{
2133 misc_deregister(&fuse_miscdevice);
2134 kmem_cache_destroy(fuse_req_cachep);
2135}