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