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