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