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