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