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