]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - fs/fuse/file.c
aio: don't include aio.h in sched.h
[mirror_ubuntu-artful-kernel.git] / fs / fuse / file.c
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
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/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/aio.h>
19
20 static const struct file_operations fuse_direct_io_file_operations;
21
22 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
23 int opcode, struct fuse_open_out *outargp)
24 {
25 struct fuse_open_in inarg;
26 struct fuse_req *req;
27 int err;
28
29 req = fuse_get_req_nopages(fc);
30 if (IS_ERR(req))
31 return PTR_ERR(req);
32
33 memset(&inarg, 0, sizeof(inarg));
34 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
35 if (!fc->atomic_o_trunc)
36 inarg.flags &= ~O_TRUNC;
37 req->in.h.opcode = opcode;
38 req->in.h.nodeid = nodeid;
39 req->in.numargs = 1;
40 req->in.args[0].size = sizeof(inarg);
41 req->in.args[0].value = &inarg;
42 req->out.numargs = 1;
43 req->out.args[0].size = sizeof(*outargp);
44 req->out.args[0].value = outargp;
45 fuse_request_send(fc, req);
46 err = req->out.h.error;
47 fuse_put_request(fc, req);
48
49 return err;
50 }
51
52 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
53 {
54 struct fuse_file *ff;
55
56 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
57 if (unlikely(!ff))
58 return NULL;
59
60 ff->fc = fc;
61 ff->reserved_req = fuse_request_alloc(0);
62 if (unlikely(!ff->reserved_req)) {
63 kfree(ff);
64 return NULL;
65 }
66
67 INIT_LIST_HEAD(&ff->write_entry);
68 atomic_set(&ff->count, 0);
69 RB_CLEAR_NODE(&ff->polled_node);
70 init_waitqueue_head(&ff->poll_wait);
71
72 spin_lock(&fc->lock);
73 ff->kh = ++fc->khctr;
74 spin_unlock(&fc->lock);
75
76 return ff;
77 }
78
79 void fuse_file_free(struct fuse_file *ff)
80 {
81 fuse_request_free(ff->reserved_req);
82 kfree(ff);
83 }
84
85 struct fuse_file *fuse_file_get(struct fuse_file *ff)
86 {
87 atomic_inc(&ff->count);
88 return ff;
89 }
90
91 static void fuse_release_async(struct work_struct *work)
92 {
93 struct fuse_req *req;
94 struct fuse_conn *fc;
95 struct path path;
96
97 req = container_of(work, struct fuse_req, misc.release.work);
98 path = req->misc.release.path;
99 fc = get_fuse_conn(path.dentry->d_inode);
100
101 fuse_put_request(fc, req);
102 path_put(&path);
103 }
104
105 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
106 {
107 if (fc->destroy_req) {
108 /*
109 * If this is a fuseblk mount, then it's possible that
110 * releasing the path will result in releasing the
111 * super block and sending the DESTROY request. If
112 * the server is single threaded, this would hang.
113 * For this reason do the path_put() in a separate
114 * thread.
115 */
116 atomic_inc(&req->count);
117 INIT_WORK(&req->misc.release.work, fuse_release_async);
118 schedule_work(&req->misc.release.work);
119 } else {
120 path_put(&req->misc.release.path);
121 }
122 }
123
124 static void fuse_file_put(struct fuse_file *ff, bool sync)
125 {
126 if (atomic_dec_and_test(&ff->count)) {
127 struct fuse_req *req = ff->reserved_req;
128
129 if (sync) {
130 fuse_request_send(ff->fc, req);
131 path_put(&req->misc.release.path);
132 fuse_put_request(ff->fc, req);
133 } else {
134 req->end = fuse_release_end;
135 fuse_request_send_background(ff->fc, req);
136 }
137 kfree(ff);
138 }
139 }
140
141 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
142 bool isdir)
143 {
144 struct fuse_open_out outarg;
145 struct fuse_file *ff;
146 int err;
147 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
148
149 ff = fuse_file_alloc(fc);
150 if (!ff)
151 return -ENOMEM;
152
153 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
154 if (err) {
155 fuse_file_free(ff);
156 return err;
157 }
158
159 if (isdir)
160 outarg.open_flags &= ~FOPEN_DIRECT_IO;
161
162 ff->fh = outarg.fh;
163 ff->nodeid = nodeid;
164 ff->open_flags = outarg.open_flags;
165 file->private_data = fuse_file_get(ff);
166
167 return 0;
168 }
169 EXPORT_SYMBOL_GPL(fuse_do_open);
170
171 void fuse_finish_open(struct inode *inode, struct file *file)
172 {
173 struct fuse_file *ff = file->private_data;
174 struct fuse_conn *fc = get_fuse_conn(inode);
175
176 if (ff->open_flags & FOPEN_DIRECT_IO)
177 file->f_op = &fuse_direct_io_file_operations;
178 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
179 invalidate_inode_pages2(inode->i_mapping);
180 if (ff->open_flags & FOPEN_NONSEEKABLE)
181 nonseekable_open(inode, file);
182 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
183 struct fuse_inode *fi = get_fuse_inode(inode);
184
185 spin_lock(&fc->lock);
186 fi->attr_version = ++fc->attr_version;
187 i_size_write(inode, 0);
188 spin_unlock(&fc->lock);
189 fuse_invalidate_attr(inode);
190 }
191 }
192
193 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
194 {
195 struct fuse_conn *fc = get_fuse_conn(inode);
196 int err;
197
198 err = generic_file_open(inode, file);
199 if (err)
200 return err;
201
202 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
203 if (err)
204 return err;
205
206 fuse_finish_open(inode, file);
207
208 return 0;
209 }
210
211 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
212 {
213 struct fuse_conn *fc = ff->fc;
214 struct fuse_req *req = ff->reserved_req;
215 struct fuse_release_in *inarg = &req->misc.release.in;
216
217 spin_lock(&fc->lock);
218 list_del(&ff->write_entry);
219 if (!RB_EMPTY_NODE(&ff->polled_node))
220 rb_erase(&ff->polled_node, &fc->polled_files);
221 spin_unlock(&fc->lock);
222
223 wake_up_interruptible_all(&ff->poll_wait);
224
225 inarg->fh = ff->fh;
226 inarg->flags = flags;
227 req->in.h.opcode = opcode;
228 req->in.h.nodeid = ff->nodeid;
229 req->in.numargs = 1;
230 req->in.args[0].size = sizeof(struct fuse_release_in);
231 req->in.args[0].value = inarg;
232 }
233
234 void fuse_release_common(struct file *file, int opcode)
235 {
236 struct fuse_file *ff;
237 struct fuse_req *req;
238
239 ff = file->private_data;
240 if (unlikely(!ff))
241 return;
242
243 req = ff->reserved_req;
244 fuse_prepare_release(ff, file->f_flags, opcode);
245
246 if (ff->flock) {
247 struct fuse_release_in *inarg = &req->misc.release.in;
248 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
249 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
250 (fl_owner_t) file);
251 }
252 /* Hold vfsmount and dentry until release is finished */
253 path_get(&file->f_path);
254 req->misc.release.path = file->f_path;
255
256 /*
257 * Normally this will send the RELEASE request, however if
258 * some asynchronous READ or WRITE requests are outstanding,
259 * the sending will be delayed.
260 *
261 * Make the release synchronous if this is a fuseblk mount,
262 * synchronous RELEASE is allowed (and desirable) in this case
263 * because the server can be trusted not to screw up.
264 */
265 fuse_file_put(ff, ff->fc->destroy_req != NULL);
266 }
267
268 static int fuse_open(struct inode *inode, struct file *file)
269 {
270 return fuse_open_common(inode, file, false);
271 }
272
273 static int fuse_release(struct inode *inode, struct file *file)
274 {
275 fuse_release_common(file, FUSE_RELEASE);
276
277 /* return value is ignored by VFS */
278 return 0;
279 }
280
281 void fuse_sync_release(struct fuse_file *ff, int flags)
282 {
283 WARN_ON(atomic_read(&ff->count) > 1);
284 fuse_prepare_release(ff, flags, FUSE_RELEASE);
285 ff->reserved_req->force = 1;
286 fuse_request_send(ff->fc, ff->reserved_req);
287 fuse_put_request(ff->fc, ff->reserved_req);
288 kfree(ff);
289 }
290 EXPORT_SYMBOL_GPL(fuse_sync_release);
291
292 /*
293 * Scramble the ID space with XTEA, so that the value of the files_struct
294 * pointer is not exposed to userspace.
295 */
296 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
297 {
298 u32 *k = fc->scramble_key;
299 u64 v = (unsigned long) id;
300 u32 v0 = v;
301 u32 v1 = v >> 32;
302 u32 sum = 0;
303 int i;
304
305 for (i = 0; i < 32; i++) {
306 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
307 sum += 0x9E3779B9;
308 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
309 }
310
311 return (u64) v0 + ((u64) v1 << 32);
312 }
313
314 /*
315 * Check if page is under writeback
316 *
317 * This is currently done by walking the list of writepage requests
318 * for the inode, which can be pretty inefficient.
319 */
320 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
321 {
322 struct fuse_conn *fc = get_fuse_conn(inode);
323 struct fuse_inode *fi = get_fuse_inode(inode);
324 struct fuse_req *req;
325 bool found = false;
326
327 spin_lock(&fc->lock);
328 list_for_each_entry(req, &fi->writepages, writepages_entry) {
329 pgoff_t curr_index;
330
331 BUG_ON(req->inode != inode);
332 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
333 if (curr_index == index) {
334 found = true;
335 break;
336 }
337 }
338 spin_unlock(&fc->lock);
339
340 return found;
341 }
342
343 /*
344 * Wait for page writeback to be completed.
345 *
346 * Since fuse doesn't rely on the VM writeback tracking, this has to
347 * use some other means.
348 */
349 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
350 {
351 struct fuse_inode *fi = get_fuse_inode(inode);
352
353 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
354 return 0;
355 }
356
357 static int fuse_flush(struct file *file, fl_owner_t id)
358 {
359 struct inode *inode = file_inode(file);
360 struct fuse_conn *fc = get_fuse_conn(inode);
361 struct fuse_file *ff = file->private_data;
362 struct fuse_req *req;
363 struct fuse_flush_in inarg;
364 int err;
365
366 if (is_bad_inode(inode))
367 return -EIO;
368
369 if (fc->no_flush)
370 return 0;
371
372 req = fuse_get_req_nofail_nopages(fc, file);
373 memset(&inarg, 0, sizeof(inarg));
374 inarg.fh = ff->fh;
375 inarg.lock_owner = fuse_lock_owner_id(fc, id);
376 req->in.h.opcode = FUSE_FLUSH;
377 req->in.h.nodeid = get_node_id(inode);
378 req->in.numargs = 1;
379 req->in.args[0].size = sizeof(inarg);
380 req->in.args[0].value = &inarg;
381 req->force = 1;
382 fuse_request_send(fc, req);
383 err = req->out.h.error;
384 fuse_put_request(fc, req);
385 if (err == -ENOSYS) {
386 fc->no_flush = 1;
387 err = 0;
388 }
389 return err;
390 }
391
392 /*
393 * Wait for all pending writepages on the inode to finish.
394 *
395 * This is currently done by blocking further writes with FUSE_NOWRITE
396 * and waiting for all sent writes to complete.
397 *
398 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
399 * could conflict with truncation.
400 */
401 static void fuse_sync_writes(struct inode *inode)
402 {
403 fuse_set_nowrite(inode);
404 fuse_release_nowrite(inode);
405 }
406
407 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
408 int datasync, int isdir)
409 {
410 struct inode *inode = file->f_mapping->host;
411 struct fuse_conn *fc = get_fuse_conn(inode);
412 struct fuse_file *ff = file->private_data;
413 struct fuse_req *req;
414 struct fuse_fsync_in inarg;
415 int err;
416
417 if (is_bad_inode(inode))
418 return -EIO;
419
420 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
421 if (err)
422 return err;
423
424 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
425 return 0;
426
427 mutex_lock(&inode->i_mutex);
428
429 /*
430 * Start writeback against all dirty pages of the inode, then
431 * wait for all outstanding writes, before sending the FSYNC
432 * request.
433 */
434 err = write_inode_now(inode, 0);
435 if (err)
436 goto out;
437
438 fuse_sync_writes(inode);
439
440 req = fuse_get_req_nopages(fc);
441 if (IS_ERR(req)) {
442 err = PTR_ERR(req);
443 goto out;
444 }
445
446 memset(&inarg, 0, sizeof(inarg));
447 inarg.fh = ff->fh;
448 inarg.fsync_flags = datasync ? 1 : 0;
449 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
450 req->in.h.nodeid = get_node_id(inode);
451 req->in.numargs = 1;
452 req->in.args[0].size = sizeof(inarg);
453 req->in.args[0].value = &inarg;
454 fuse_request_send(fc, req);
455 err = req->out.h.error;
456 fuse_put_request(fc, req);
457 if (err == -ENOSYS) {
458 if (isdir)
459 fc->no_fsyncdir = 1;
460 else
461 fc->no_fsync = 1;
462 err = 0;
463 }
464 out:
465 mutex_unlock(&inode->i_mutex);
466 return err;
467 }
468
469 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
470 int datasync)
471 {
472 return fuse_fsync_common(file, start, end, datasync, 0);
473 }
474
475 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
476 size_t count, int opcode)
477 {
478 struct fuse_read_in *inarg = &req->misc.read.in;
479 struct fuse_file *ff = file->private_data;
480
481 inarg->fh = ff->fh;
482 inarg->offset = pos;
483 inarg->size = count;
484 inarg->flags = file->f_flags;
485 req->in.h.opcode = opcode;
486 req->in.h.nodeid = ff->nodeid;
487 req->in.numargs = 1;
488 req->in.args[0].size = sizeof(struct fuse_read_in);
489 req->in.args[0].value = inarg;
490 req->out.argvar = 1;
491 req->out.numargs = 1;
492 req->out.args[0].size = count;
493 }
494
495 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
496 loff_t pos, size_t count, fl_owner_t owner)
497 {
498 struct fuse_file *ff = file->private_data;
499 struct fuse_conn *fc = ff->fc;
500
501 fuse_read_fill(req, file, pos, count, FUSE_READ);
502 if (owner != NULL) {
503 struct fuse_read_in *inarg = &req->misc.read.in;
504
505 inarg->read_flags |= FUSE_READ_LOCKOWNER;
506 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
507 }
508 fuse_request_send(fc, req);
509 return req->out.args[0].size;
510 }
511
512 static void fuse_read_update_size(struct inode *inode, loff_t size,
513 u64 attr_ver)
514 {
515 struct fuse_conn *fc = get_fuse_conn(inode);
516 struct fuse_inode *fi = get_fuse_inode(inode);
517
518 spin_lock(&fc->lock);
519 if (attr_ver == fi->attr_version && size < inode->i_size) {
520 fi->attr_version = ++fc->attr_version;
521 i_size_write(inode, size);
522 }
523 spin_unlock(&fc->lock);
524 }
525
526 static int fuse_readpage(struct file *file, struct page *page)
527 {
528 struct inode *inode = page->mapping->host;
529 struct fuse_conn *fc = get_fuse_conn(inode);
530 struct fuse_req *req;
531 size_t num_read;
532 loff_t pos = page_offset(page);
533 size_t count = PAGE_CACHE_SIZE;
534 u64 attr_ver;
535 int err;
536
537 err = -EIO;
538 if (is_bad_inode(inode))
539 goto out;
540
541 /*
542 * Page writeback can extend beyond the lifetime of the
543 * page-cache page, so make sure we read a properly synced
544 * page.
545 */
546 fuse_wait_on_page_writeback(inode, page->index);
547
548 req = fuse_get_req(fc, 1);
549 err = PTR_ERR(req);
550 if (IS_ERR(req))
551 goto out;
552
553 attr_ver = fuse_get_attr_version(fc);
554
555 req->out.page_zeroing = 1;
556 req->out.argpages = 1;
557 req->num_pages = 1;
558 req->pages[0] = page;
559 req->page_descs[0].length = count;
560 num_read = fuse_send_read(req, file, pos, count, NULL);
561 err = req->out.h.error;
562 fuse_put_request(fc, req);
563
564 if (!err) {
565 /*
566 * Short read means EOF. If file size is larger, truncate it
567 */
568 if (num_read < count)
569 fuse_read_update_size(inode, pos + num_read, attr_ver);
570
571 SetPageUptodate(page);
572 }
573
574 fuse_invalidate_attr(inode); /* atime changed */
575 out:
576 unlock_page(page);
577 return err;
578 }
579
580 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
581 {
582 int i;
583 size_t count = req->misc.read.in.size;
584 size_t num_read = req->out.args[0].size;
585 struct address_space *mapping = NULL;
586
587 for (i = 0; mapping == NULL && i < req->num_pages; i++)
588 mapping = req->pages[i]->mapping;
589
590 if (mapping) {
591 struct inode *inode = mapping->host;
592
593 /*
594 * Short read means EOF. If file size is larger, truncate it
595 */
596 if (!req->out.h.error && num_read < count) {
597 loff_t pos;
598
599 pos = page_offset(req->pages[0]) + num_read;
600 fuse_read_update_size(inode, pos,
601 req->misc.read.attr_ver);
602 }
603 fuse_invalidate_attr(inode); /* atime changed */
604 }
605
606 for (i = 0; i < req->num_pages; i++) {
607 struct page *page = req->pages[i];
608 if (!req->out.h.error)
609 SetPageUptodate(page);
610 else
611 SetPageError(page);
612 unlock_page(page);
613 page_cache_release(page);
614 }
615 if (req->ff)
616 fuse_file_put(req->ff, false);
617 }
618
619 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
620 {
621 struct fuse_file *ff = file->private_data;
622 struct fuse_conn *fc = ff->fc;
623 loff_t pos = page_offset(req->pages[0]);
624 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
625
626 req->out.argpages = 1;
627 req->out.page_zeroing = 1;
628 req->out.page_replace = 1;
629 fuse_read_fill(req, file, pos, count, FUSE_READ);
630 req->misc.read.attr_ver = fuse_get_attr_version(fc);
631 if (fc->async_read) {
632 req->ff = fuse_file_get(ff);
633 req->end = fuse_readpages_end;
634 fuse_request_send_background(fc, req);
635 } else {
636 fuse_request_send(fc, req);
637 fuse_readpages_end(fc, req);
638 fuse_put_request(fc, req);
639 }
640 }
641
642 struct fuse_fill_data {
643 struct fuse_req *req;
644 struct file *file;
645 struct inode *inode;
646 unsigned nr_pages;
647 };
648
649 static int fuse_readpages_fill(void *_data, struct page *page)
650 {
651 struct fuse_fill_data *data = _data;
652 struct fuse_req *req = data->req;
653 struct inode *inode = data->inode;
654 struct fuse_conn *fc = get_fuse_conn(inode);
655
656 fuse_wait_on_page_writeback(inode, page->index);
657
658 if (req->num_pages &&
659 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
660 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
661 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
662 int nr_alloc = min_t(unsigned, data->nr_pages,
663 FUSE_MAX_PAGES_PER_REQ);
664 fuse_send_readpages(req, data->file);
665 data->req = req = fuse_get_req(fc, nr_alloc);
666 if (IS_ERR(req)) {
667 unlock_page(page);
668 return PTR_ERR(req);
669 }
670 }
671
672 if (WARN_ON(req->num_pages >= req->max_pages)) {
673 fuse_put_request(fc, req);
674 return -EIO;
675 }
676
677 page_cache_get(page);
678 req->pages[req->num_pages] = page;
679 req->page_descs[req->num_pages].length = PAGE_SIZE;
680 req->num_pages++;
681 data->nr_pages--;
682 return 0;
683 }
684
685 static int fuse_readpages(struct file *file, struct address_space *mapping,
686 struct list_head *pages, unsigned nr_pages)
687 {
688 struct inode *inode = mapping->host;
689 struct fuse_conn *fc = get_fuse_conn(inode);
690 struct fuse_fill_data data;
691 int err;
692 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
693
694 err = -EIO;
695 if (is_bad_inode(inode))
696 goto out;
697
698 data.file = file;
699 data.inode = inode;
700 data.req = fuse_get_req(fc, nr_alloc);
701 data.nr_pages = nr_pages;
702 err = PTR_ERR(data.req);
703 if (IS_ERR(data.req))
704 goto out;
705
706 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
707 if (!err) {
708 if (data.req->num_pages)
709 fuse_send_readpages(data.req, file);
710 else
711 fuse_put_request(fc, data.req);
712 }
713 out:
714 return err;
715 }
716
717 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
718 unsigned long nr_segs, loff_t pos)
719 {
720 struct inode *inode = iocb->ki_filp->f_mapping->host;
721 struct fuse_conn *fc = get_fuse_conn(inode);
722
723 /*
724 * In auto invalidate mode, always update attributes on read.
725 * Otherwise, only update if we attempt to read past EOF (to ensure
726 * i_size is up to date).
727 */
728 if (fc->auto_inval_data ||
729 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
730 int err;
731 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
732 if (err)
733 return err;
734 }
735
736 return generic_file_aio_read(iocb, iov, nr_segs, pos);
737 }
738
739 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
740 loff_t pos, size_t count)
741 {
742 struct fuse_write_in *inarg = &req->misc.write.in;
743 struct fuse_write_out *outarg = &req->misc.write.out;
744
745 inarg->fh = ff->fh;
746 inarg->offset = pos;
747 inarg->size = count;
748 req->in.h.opcode = FUSE_WRITE;
749 req->in.h.nodeid = ff->nodeid;
750 req->in.numargs = 2;
751 if (ff->fc->minor < 9)
752 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
753 else
754 req->in.args[0].size = sizeof(struct fuse_write_in);
755 req->in.args[0].value = inarg;
756 req->in.args[1].size = count;
757 req->out.numargs = 1;
758 req->out.args[0].size = sizeof(struct fuse_write_out);
759 req->out.args[0].value = outarg;
760 }
761
762 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
763 loff_t pos, size_t count, fl_owner_t owner)
764 {
765 struct fuse_file *ff = file->private_data;
766 struct fuse_conn *fc = ff->fc;
767 struct fuse_write_in *inarg = &req->misc.write.in;
768
769 fuse_write_fill(req, ff, pos, count);
770 inarg->flags = file->f_flags;
771 if (owner != NULL) {
772 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
773 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
774 }
775 fuse_request_send(fc, req);
776 return req->misc.write.out.size;
777 }
778
779 void fuse_write_update_size(struct inode *inode, loff_t pos)
780 {
781 struct fuse_conn *fc = get_fuse_conn(inode);
782 struct fuse_inode *fi = get_fuse_inode(inode);
783
784 spin_lock(&fc->lock);
785 fi->attr_version = ++fc->attr_version;
786 if (pos > inode->i_size)
787 i_size_write(inode, pos);
788 spin_unlock(&fc->lock);
789 }
790
791 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
792 struct inode *inode, loff_t pos,
793 size_t count)
794 {
795 size_t res;
796 unsigned offset;
797 unsigned i;
798
799 for (i = 0; i < req->num_pages; i++)
800 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
801
802 res = fuse_send_write(req, file, pos, count, NULL);
803
804 offset = req->page_descs[0].offset;
805 count = res;
806 for (i = 0; i < req->num_pages; i++) {
807 struct page *page = req->pages[i];
808
809 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
810 SetPageUptodate(page);
811
812 if (count > PAGE_CACHE_SIZE - offset)
813 count -= PAGE_CACHE_SIZE - offset;
814 else
815 count = 0;
816 offset = 0;
817
818 unlock_page(page);
819 page_cache_release(page);
820 }
821
822 return res;
823 }
824
825 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
826 struct address_space *mapping,
827 struct iov_iter *ii, loff_t pos)
828 {
829 struct fuse_conn *fc = get_fuse_conn(mapping->host);
830 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
831 size_t count = 0;
832 int err;
833
834 req->in.argpages = 1;
835 req->page_descs[0].offset = offset;
836
837 do {
838 size_t tmp;
839 struct page *page;
840 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
841 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
842 iov_iter_count(ii));
843
844 bytes = min_t(size_t, bytes, fc->max_write - count);
845
846 again:
847 err = -EFAULT;
848 if (iov_iter_fault_in_readable(ii, bytes))
849 break;
850
851 err = -ENOMEM;
852 page = grab_cache_page_write_begin(mapping, index, 0);
853 if (!page)
854 break;
855
856 if (mapping_writably_mapped(mapping))
857 flush_dcache_page(page);
858
859 pagefault_disable();
860 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
861 pagefault_enable();
862 flush_dcache_page(page);
863
864 mark_page_accessed(page);
865
866 if (!tmp) {
867 unlock_page(page);
868 page_cache_release(page);
869 bytes = min(bytes, iov_iter_single_seg_count(ii));
870 goto again;
871 }
872
873 err = 0;
874 req->pages[req->num_pages] = page;
875 req->page_descs[req->num_pages].length = tmp;
876 req->num_pages++;
877
878 iov_iter_advance(ii, tmp);
879 count += tmp;
880 pos += tmp;
881 offset += tmp;
882 if (offset == PAGE_CACHE_SIZE)
883 offset = 0;
884
885 if (!fc->big_writes)
886 break;
887 } while (iov_iter_count(ii) && count < fc->max_write &&
888 req->num_pages < req->max_pages && offset == 0);
889
890 return count > 0 ? count : err;
891 }
892
893 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
894 {
895 return min_t(unsigned,
896 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
897 (pos >> PAGE_CACHE_SHIFT) + 1,
898 FUSE_MAX_PAGES_PER_REQ);
899 }
900
901 static ssize_t fuse_perform_write(struct file *file,
902 struct address_space *mapping,
903 struct iov_iter *ii, loff_t pos)
904 {
905 struct inode *inode = mapping->host;
906 struct fuse_conn *fc = get_fuse_conn(inode);
907 int err = 0;
908 ssize_t res = 0;
909
910 if (is_bad_inode(inode))
911 return -EIO;
912
913 do {
914 struct fuse_req *req;
915 ssize_t count;
916 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
917
918 req = fuse_get_req(fc, nr_pages);
919 if (IS_ERR(req)) {
920 err = PTR_ERR(req);
921 break;
922 }
923
924 count = fuse_fill_write_pages(req, mapping, ii, pos);
925 if (count <= 0) {
926 err = count;
927 } else {
928 size_t num_written;
929
930 num_written = fuse_send_write_pages(req, file, inode,
931 pos, count);
932 err = req->out.h.error;
933 if (!err) {
934 res += num_written;
935 pos += num_written;
936
937 /* break out of the loop on short write */
938 if (num_written != count)
939 err = -EIO;
940 }
941 }
942 fuse_put_request(fc, req);
943 } while (!err && iov_iter_count(ii));
944
945 if (res > 0)
946 fuse_write_update_size(inode, pos);
947
948 fuse_invalidate_attr(inode);
949
950 return res > 0 ? res : err;
951 }
952
953 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
954 unsigned long nr_segs, loff_t pos)
955 {
956 struct file *file = iocb->ki_filp;
957 struct address_space *mapping = file->f_mapping;
958 size_t count = 0;
959 size_t ocount = 0;
960 ssize_t written = 0;
961 ssize_t written_buffered = 0;
962 struct inode *inode = mapping->host;
963 ssize_t err;
964 struct iov_iter i;
965 loff_t endbyte = 0;
966
967 WARN_ON(iocb->ki_pos != pos);
968
969 ocount = 0;
970 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
971 if (err)
972 return err;
973
974 count = ocount;
975 mutex_lock(&inode->i_mutex);
976
977 /* We can write back this queue in page reclaim */
978 current->backing_dev_info = mapping->backing_dev_info;
979
980 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
981 if (err)
982 goto out;
983
984 if (count == 0)
985 goto out;
986
987 err = file_remove_suid(file);
988 if (err)
989 goto out;
990
991 err = file_update_time(file);
992 if (err)
993 goto out;
994
995 if (file->f_flags & O_DIRECT) {
996 written = generic_file_direct_write(iocb, iov, &nr_segs,
997 pos, &iocb->ki_pos,
998 count, ocount);
999 if (written < 0 || written == count)
1000 goto out;
1001
1002 pos += written;
1003 count -= written;
1004
1005 iov_iter_init(&i, iov, nr_segs, count, written);
1006 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1007 if (written_buffered < 0) {
1008 err = written_buffered;
1009 goto out;
1010 }
1011 endbyte = pos + written_buffered - 1;
1012
1013 err = filemap_write_and_wait_range(file->f_mapping, pos,
1014 endbyte);
1015 if (err)
1016 goto out;
1017
1018 invalidate_mapping_pages(file->f_mapping,
1019 pos >> PAGE_CACHE_SHIFT,
1020 endbyte >> PAGE_CACHE_SHIFT);
1021
1022 written += written_buffered;
1023 iocb->ki_pos = pos + written_buffered;
1024 } else {
1025 iov_iter_init(&i, iov, nr_segs, count, 0);
1026 written = fuse_perform_write(file, mapping, &i, pos);
1027 if (written >= 0)
1028 iocb->ki_pos = pos + written;
1029 }
1030 out:
1031 current->backing_dev_info = NULL;
1032 mutex_unlock(&inode->i_mutex);
1033
1034 return written ? written : err;
1035 }
1036
1037 static void fuse_release_user_pages(struct fuse_req *req, int write)
1038 {
1039 unsigned i;
1040
1041 for (i = 0; i < req->num_pages; i++) {
1042 struct page *page = req->pages[i];
1043 if (write)
1044 set_page_dirty_lock(page);
1045 put_page(page);
1046 }
1047 }
1048
1049 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1050 unsigned index, unsigned nr_pages)
1051 {
1052 int i;
1053
1054 for (i = index; i < index + nr_pages; i++)
1055 req->page_descs[i].length = PAGE_SIZE -
1056 req->page_descs[i].offset;
1057 }
1058
1059 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1060 {
1061 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1062 }
1063
1064 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1065 size_t max_size)
1066 {
1067 return min(iov_iter_single_seg_count(ii), max_size);
1068 }
1069
1070 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1071 size_t *nbytesp, int write)
1072 {
1073 size_t nbytes = 0; /* # bytes already packed in req */
1074
1075 /* Special case for kernel I/O: can copy directly into the buffer */
1076 if (segment_eq(get_fs(), KERNEL_DS)) {
1077 unsigned long user_addr = fuse_get_user_addr(ii);
1078 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1079
1080 if (write)
1081 req->in.args[1].value = (void *) user_addr;
1082 else
1083 req->out.args[0].value = (void *) user_addr;
1084
1085 iov_iter_advance(ii, frag_size);
1086 *nbytesp = frag_size;
1087 return 0;
1088 }
1089
1090 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1091 unsigned npages;
1092 unsigned long user_addr = fuse_get_user_addr(ii);
1093 unsigned offset = user_addr & ~PAGE_MASK;
1094 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1095 int ret;
1096
1097 unsigned n = req->max_pages - req->num_pages;
1098 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1099
1100 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1101 npages = clamp(npages, 1U, n);
1102
1103 ret = get_user_pages_fast(user_addr, npages, !write,
1104 &req->pages[req->num_pages]);
1105 if (ret < 0)
1106 return ret;
1107
1108 npages = ret;
1109 frag_size = min_t(size_t, frag_size,
1110 (npages << PAGE_SHIFT) - offset);
1111 iov_iter_advance(ii, frag_size);
1112
1113 req->page_descs[req->num_pages].offset = offset;
1114 fuse_page_descs_length_init(req, req->num_pages, npages);
1115
1116 req->num_pages += npages;
1117 req->page_descs[req->num_pages - 1].length -=
1118 (npages << PAGE_SHIFT) - offset - frag_size;
1119
1120 nbytes += frag_size;
1121 }
1122
1123 if (write)
1124 req->in.argpages = 1;
1125 else
1126 req->out.argpages = 1;
1127
1128 *nbytesp = nbytes;
1129
1130 return 0;
1131 }
1132
1133 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1134 {
1135 struct iov_iter ii = *ii_p;
1136 int npages = 0;
1137
1138 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1139 unsigned long user_addr = fuse_get_user_addr(&ii);
1140 unsigned offset = user_addr & ~PAGE_MASK;
1141 size_t frag_size = iov_iter_single_seg_count(&ii);
1142
1143 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1144 iov_iter_advance(&ii, frag_size);
1145 }
1146
1147 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1148 }
1149
1150 ssize_t fuse_direct_io(struct file *file, const struct iovec *iov,
1151 unsigned long nr_segs, size_t count, loff_t *ppos,
1152 int write)
1153 {
1154 struct fuse_file *ff = file->private_data;
1155 struct fuse_conn *fc = ff->fc;
1156 size_t nmax = write ? fc->max_write : fc->max_read;
1157 loff_t pos = *ppos;
1158 ssize_t res = 0;
1159 struct fuse_req *req;
1160 struct iov_iter ii;
1161
1162 iov_iter_init(&ii, iov, nr_segs, count, 0);
1163
1164 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1165 if (IS_ERR(req))
1166 return PTR_ERR(req);
1167
1168 while (count) {
1169 size_t nres;
1170 fl_owner_t owner = current->files;
1171 size_t nbytes = min(count, nmax);
1172 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
1173 if (err) {
1174 res = err;
1175 break;
1176 }
1177
1178 if (write)
1179 nres = fuse_send_write(req, file, pos, nbytes, owner);
1180 else
1181 nres = fuse_send_read(req, file, pos, nbytes, owner);
1182
1183 fuse_release_user_pages(req, !write);
1184 if (req->out.h.error) {
1185 if (!res)
1186 res = req->out.h.error;
1187 break;
1188 } else if (nres > nbytes) {
1189 res = -EIO;
1190 break;
1191 }
1192 count -= nres;
1193 res += nres;
1194 pos += nres;
1195 if (nres != nbytes)
1196 break;
1197 if (count) {
1198 fuse_put_request(fc, req);
1199 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1200 if (IS_ERR(req))
1201 break;
1202 }
1203 }
1204 if (!IS_ERR(req))
1205 fuse_put_request(fc, req);
1206 if (res > 0)
1207 *ppos = pos;
1208
1209 return res;
1210 }
1211 EXPORT_SYMBOL_GPL(fuse_direct_io);
1212
1213 static ssize_t __fuse_direct_read(struct file *file, const struct iovec *iov,
1214 unsigned long nr_segs, loff_t *ppos)
1215 {
1216 ssize_t res;
1217 struct inode *inode = file_inode(file);
1218
1219 if (is_bad_inode(inode))
1220 return -EIO;
1221
1222 res = fuse_direct_io(file, iov, nr_segs, iov_length(iov, nr_segs),
1223 ppos, 0);
1224
1225 fuse_invalidate_attr(inode);
1226
1227 return res;
1228 }
1229
1230 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1231 size_t count, loff_t *ppos)
1232 {
1233 struct iovec iov = { .iov_base = buf, .iov_len = count };
1234 return __fuse_direct_read(file, &iov, 1, ppos);
1235 }
1236
1237 static ssize_t __fuse_direct_write(struct file *file, const struct iovec *iov,
1238 unsigned long nr_segs, loff_t *ppos)
1239 {
1240 struct inode *inode = file_inode(file);
1241 size_t count = iov_length(iov, nr_segs);
1242 ssize_t res;
1243
1244 res = generic_write_checks(file, ppos, &count, 0);
1245 if (!res) {
1246 res = fuse_direct_io(file, iov, nr_segs, count, ppos, 1);
1247 if (res > 0)
1248 fuse_write_update_size(inode, *ppos);
1249 }
1250
1251 fuse_invalidate_attr(inode);
1252
1253 return res;
1254 }
1255
1256 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1257 size_t count, loff_t *ppos)
1258 {
1259 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
1260 struct inode *inode = file_inode(file);
1261 ssize_t res;
1262
1263 if (is_bad_inode(inode))
1264 return -EIO;
1265
1266 /* Don't allow parallel writes to the same file */
1267 mutex_lock(&inode->i_mutex);
1268 res = __fuse_direct_write(file, &iov, 1, ppos);
1269 mutex_unlock(&inode->i_mutex);
1270
1271 return res;
1272 }
1273
1274 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1275 {
1276 __free_page(req->pages[0]);
1277 fuse_file_put(req->ff, false);
1278 }
1279
1280 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1281 {
1282 struct inode *inode = req->inode;
1283 struct fuse_inode *fi = get_fuse_inode(inode);
1284 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1285
1286 list_del(&req->writepages_entry);
1287 dec_bdi_stat(bdi, BDI_WRITEBACK);
1288 dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1289 bdi_writeout_inc(bdi);
1290 wake_up(&fi->page_waitq);
1291 }
1292
1293 /* Called under fc->lock, may release and reacquire it */
1294 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1295 __releases(fc->lock)
1296 __acquires(fc->lock)
1297 {
1298 struct fuse_inode *fi = get_fuse_inode(req->inode);
1299 loff_t size = i_size_read(req->inode);
1300 struct fuse_write_in *inarg = &req->misc.write.in;
1301
1302 if (!fc->connected)
1303 goto out_free;
1304
1305 if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1306 inarg->size = PAGE_CACHE_SIZE;
1307 } else if (inarg->offset < size) {
1308 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1309 } else {
1310 /* Got truncated off completely */
1311 goto out_free;
1312 }
1313
1314 req->in.args[1].size = inarg->size;
1315 fi->writectr++;
1316 fuse_request_send_background_locked(fc, req);
1317 return;
1318
1319 out_free:
1320 fuse_writepage_finish(fc, req);
1321 spin_unlock(&fc->lock);
1322 fuse_writepage_free(fc, req);
1323 fuse_put_request(fc, req);
1324 spin_lock(&fc->lock);
1325 }
1326
1327 /*
1328 * If fi->writectr is positive (no truncate or fsync going on) send
1329 * all queued writepage requests.
1330 *
1331 * Called with fc->lock
1332 */
1333 void fuse_flush_writepages(struct inode *inode)
1334 __releases(fc->lock)
1335 __acquires(fc->lock)
1336 {
1337 struct fuse_conn *fc = get_fuse_conn(inode);
1338 struct fuse_inode *fi = get_fuse_inode(inode);
1339 struct fuse_req *req;
1340
1341 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1342 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1343 list_del_init(&req->list);
1344 fuse_send_writepage(fc, req);
1345 }
1346 }
1347
1348 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1349 {
1350 struct inode *inode = req->inode;
1351 struct fuse_inode *fi = get_fuse_inode(inode);
1352
1353 mapping_set_error(inode->i_mapping, req->out.h.error);
1354 spin_lock(&fc->lock);
1355 fi->writectr--;
1356 fuse_writepage_finish(fc, req);
1357 spin_unlock(&fc->lock);
1358 fuse_writepage_free(fc, req);
1359 }
1360
1361 static int fuse_writepage_locked(struct page *page)
1362 {
1363 struct address_space *mapping = page->mapping;
1364 struct inode *inode = mapping->host;
1365 struct fuse_conn *fc = get_fuse_conn(inode);
1366 struct fuse_inode *fi = get_fuse_inode(inode);
1367 struct fuse_req *req;
1368 struct fuse_file *ff;
1369 struct page *tmp_page;
1370
1371 set_page_writeback(page);
1372
1373 req = fuse_request_alloc_nofs(1);
1374 if (!req)
1375 goto err;
1376
1377 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1378 if (!tmp_page)
1379 goto err_free;
1380
1381 spin_lock(&fc->lock);
1382 BUG_ON(list_empty(&fi->write_files));
1383 ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1384 req->ff = fuse_file_get(ff);
1385 spin_unlock(&fc->lock);
1386
1387 fuse_write_fill(req, ff, page_offset(page), 0);
1388
1389 copy_highpage(tmp_page, page);
1390 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1391 req->in.argpages = 1;
1392 req->num_pages = 1;
1393 req->pages[0] = tmp_page;
1394 req->page_descs[0].offset = 0;
1395 req->page_descs[0].length = PAGE_SIZE;
1396 req->end = fuse_writepage_end;
1397 req->inode = inode;
1398
1399 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1400 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1401 end_page_writeback(page);
1402
1403 spin_lock(&fc->lock);
1404 list_add(&req->writepages_entry, &fi->writepages);
1405 list_add_tail(&req->list, &fi->queued_writes);
1406 fuse_flush_writepages(inode);
1407 spin_unlock(&fc->lock);
1408
1409 return 0;
1410
1411 err_free:
1412 fuse_request_free(req);
1413 err:
1414 end_page_writeback(page);
1415 return -ENOMEM;
1416 }
1417
1418 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1419 {
1420 int err;
1421
1422 err = fuse_writepage_locked(page);
1423 unlock_page(page);
1424
1425 return err;
1426 }
1427
1428 static int fuse_launder_page(struct page *page)
1429 {
1430 int err = 0;
1431 if (clear_page_dirty_for_io(page)) {
1432 struct inode *inode = page->mapping->host;
1433 err = fuse_writepage_locked(page);
1434 if (!err)
1435 fuse_wait_on_page_writeback(inode, page->index);
1436 }
1437 return err;
1438 }
1439
1440 /*
1441 * Write back dirty pages now, because there may not be any suitable
1442 * open files later
1443 */
1444 static void fuse_vma_close(struct vm_area_struct *vma)
1445 {
1446 filemap_write_and_wait(vma->vm_file->f_mapping);
1447 }
1448
1449 /*
1450 * Wait for writeback against this page to complete before allowing it
1451 * to be marked dirty again, and hence written back again, possibly
1452 * before the previous writepage completed.
1453 *
1454 * Block here, instead of in ->writepage(), so that the userspace fs
1455 * can only block processes actually operating on the filesystem.
1456 *
1457 * Otherwise unprivileged userspace fs would be able to block
1458 * unrelated:
1459 *
1460 * - page migration
1461 * - sync(2)
1462 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1463 */
1464 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1465 {
1466 struct page *page = vmf->page;
1467 /*
1468 * Don't use page->mapping as it may become NULL from a
1469 * concurrent truncate.
1470 */
1471 struct inode *inode = vma->vm_file->f_mapping->host;
1472
1473 fuse_wait_on_page_writeback(inode, page->index);
1474 return 0;
1475 }
1476
1477 static const struct vm_operations_struct fuse_file_vm_ops = {
1478 .close = fuse_vma_close,
1479 .fault = filemap_fault,
1480 .page_mkwrite = fuse_page_mkwrite,
1481 .remap_pages = generic_file_remap_pages,
1482 };
1483
1484 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1485 {
1486 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1487 struct inode *inode = file_inode(file);
1488 struct fuse_conn *fc = get_fuse_conn(inode);
1489 struct fuse_inode *fi = get_fuse_inode(inode);
1490 struct fuse_file *ff = file->private_data;
1491 /*
1492 * file may be written through mmap, so chain it onto the
1493 * inodes's write_file list
1494 */
1495 spin_lock(&fc->lock);
1496 if (list_empty(&ff->write_entry))
1497 list_add(&ff->write_entry, &fi->write_files);
1498 spin_unlock(&fc->lock);
1499 }
1500 file_accessed(file);
1501 vma->vm_ops = &fuse_file_vm_ops;
1502 return 0;
1503 }
1504
1505 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
1506 {
1507 /* Can't provide the coherency needed for MAP_SHARED */
1508 if (vma->vm_flags & VM_MAYSHARE)
1509 return -ENODEV;
1510
1511 invalidate_inode_pages2(file->f_mapping);
1512
1513 return generic_file_mmap(file, vma);
1514 }
1515
1516 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1517 struct file_lock *fl)
1518 {
1519 switch (ffl->type) {
1520 case F_UNLCK:
1521 break;
1522
1523 case F_RDLCK:
1524 case F_WRLCK:
1525 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1526 ffl->end < ffl->start)
1527 return -EIO;
1528
1529 fl->fl_start = ffl->start;
1530 fl->fl_end = ffl->end;
1531 fl->fl_pid = ffl->pid;
1532 break;
1533
1534 default:
1535 return -EIO;
1536 }
1537 fl->fl_type = ffl->type;
1538 return 0;
1539 }
1540
1541 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1542 const struct file_lock *fl, int opcode, pid_t pid,
1543 int flock)
1544 {
1545 struct inode *inode = file_inode(file);
1546 struct fuse_conn *fc = get_fuse_conn(inode);
1547 struct fuse_file *ff = file->private_data;
1548 struct fuse_lk_in *arg = &req->misc.lk_in;
1549
1550 arg->fh = ff->fh;
1551 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1552 arg->lk.start = fl->fl_start;
1553 arg->lk.end = fl->fl_end;
1554 arg->lk.type = fl->fl_type;
1555 arg->lk.pid = pid;
1556 if (flock)
1557 arg->lk_flags |= FUSE_LK_FLOCK;
1558 req->in.h.opcode = opcode;
1559 req->in.h.nodeid = get_node_id(inode);
1560 req->in.numargs = 1;
1561 req->in.args[0].size = sizeof(*arg);
1562 req->in.args[0].value = arg;
1563 }
1564
1565 static int fuse_getlk(struct file *file, struct file_lock *fl)
1566 {
1567 struct inode *inode = file_inode(file);
1568 struct fuse_conn *fc = get_fuse_conn(inode);
1569 struct fuse_req *req;
1570 struct fuse_lk_out outarg;
1571 int err;
1572
1573 req = fuse_get_req_nopages(fc);
1574 if (IS_ERR(req))
1575 return PTR_ERR(req);
1576
1577 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1578 req->out.numargs = 1;
1579 req->out.args[0].size = sizeof(outarg);
1580 req->out.args[0].value = &outarg;
1581 fuse_request_send(fc, req);
1582 err = req->out.h.error;
1583 fuse_put_request(fc, req);
1584 if (!err)
1585 err = convert_fuse_file_lock(&outarg.lk, fl);
1586
1587 return err;
1588 }
1589
1590 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1591 {
1592 struct inode *inode = file_inode(file);
1593 struct fuse_conn *fc = get_fuse_conn(inode);
1594 struct fuse_req *req;
1595 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1596 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1597 int err;
1598
1599 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
1600 /* NLM needs asynchronous locks, which we don't support yet */
1601 return -ENOLCK;
1602 }
1603
1604 /* Unlock on close is handled by the flush method */
1605 if (fl->fl_flags & FL_CLOSE)
1606 return 0;
1607
1608 req = fuse_get_req_nopages(fc);
1609 if (IS_ERR(req))
1610 return PTR_ERR(req);
1611
1612 fuse_lk_fill(req, file, fl, opcode, pid, flock);
1613 fuse_request_send(fc, req);
1614 err = req->out.h.error;
1615 /* locking is restartable */
1616 if (err == -EINTR)
1617 err = -ERESTARTSYS;
1618 fuse_put_request(fc, req);
1619 return err;
1620 }
1621
1622 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1623 {
1624 struct inode *inode = file_inode(file);
1625 struct fuse_conn *fc = get_fuse_conn(inode);
1626 int err;
1627
1628 if (cmd == F_CANCELLK) {
1629 err = 0;
1630 } else if (cmd == F_GETLK) {
1631 if (fc->no_lock) {
1632 posix_test_lock(file, fl);
1633 err = 0;
1634 } else
1635 err = fuse_getlk(file, fl);
1636 } else {
1637 if (fc->no_lock)
1638 err = posix_lock_file(file, fl, NULL);
1639 else
1640 err = fuse_setlk(file, fl, 0);
1641 }
1642 return err;
1643 }
1644
1645 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1646 {
1647 struct inode *inode = file_inode(file);
1648 struct fuse_conn *fc = get_fuse_conn(inode);
1649 int err;
1650
1651 if (fc->no_flock) {
1652 err = flock_lock_file_wait(file, fl);
1653 } else {
1654 struct fuse_file *ff = file->private_data;
1655
1656 /* emulate flock with POSIX locks */
1657 fl->fl_owner = (fl_owner_t) file;
1658 ff->flock = true;
1659 err = fuse_setlk(file, fl, 1);
1660 }
1661
1662 return err;
1663 }
1664
1665 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1666 {
1667 struct inode *inode = mapping->host;
1668 struct fuse_conn *fc = get_fuse_conn(inode);
1669 struct fuse_req *req;
1670 struct fuse_bmap_in inarg;
1671 struct fuse_bmap_out outarg;
1672 int err;
1673
1674 if (!inode->i_sb->s_bdev || fc->no_bmap)
1675 return 0;
1676
1677 req = fuse_get_req_nopages(fc);
1678 if (IS_ERR(req))
1679 return 0;
1680
1681 memset(&inarg, 0, sizeof(inarg));
1682 inarg.block = block;
1683 inarg.blocksize = inode->i_sb->s_blocksize;
1684 req->in.h.opcode = FUSE_BMAP;
1685 req->in.h.nodeid = get_node_id(inode);
1686 req->in.numargs = 1;
1687 req->in.args[0].size = sizeof(inarg);
1688 req->in.args[0].value = &inarg;
1689 req->out.numargs = 1;
1690 req->out.args[0].size = sizeof(outarg);
1691 req->out.args[0].value = &outarg;
1692 fuse_request_send(fc, req);
1693 err = req->out.h.error;
1694 fuse_put_request(fc, req);
1695 if (err == -ENOSYS)
1696 fc->no_bmap = 1;
1697
1698 return err ? 0 : outarg.block;
1699 }
1700
1701 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
1702 {
1703 loff_t retval;
1704 struct inode *inode = file_inode(file);
1705
1706 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
1707 if (whence == SEEK_CUR || whence == SEEK_SET)
1708 return generic_file_llseek(file, offset, whence);
1709
1710 mutex_lock(&inode->i_mutex);
1711 retval = fuse_update_attributes(inode, NULL, file, NULL);
1712 if (!retval)
1713 retval = generic_file_llseek(file, offset, whence);
1714 mutex_unlock(&inode->i_mutex);
1715
1716 return retval;
1717 }
1718
1719 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
1720 unsigned int nr_segs, size_t bytes, bool to_user)
1721 {
1722 struct iov_iter ii;
1723 int page_idx = 0;
1724
1725 if (!bytes)
1726 return 0;
1727
1728 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
1729
1730 while (iov_iter_count(&ii)) {
1731 struct page *page = pages[page_idx++];
1732 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
1733 void *kaddr;
1734
1735 kaddr = kmap(page);
1736
1737 while (todo) {
1738 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
1739 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
1740 size_t copy = min(todo, iov_len);
1741 size_t left;
1742
1743 if (!to_user)
1744 left = copy_from_user(kaddr, uaddr, copy);
1745 else
1746 left = copy_to_user(uaddr, kaddr, copy);
1747
1748 if (unlikely(left))
1749 return -EFAULT;
1750
1751 iov_iter_advance(&ii, copy);
1752 todo -= copy;
1753 kaddr += copy;
1754 }
1755
1756 kunmap(page);
1757 }
1758
1759 return 0;
1760 }
1761
1762 /*
1763 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1764 * ABI was defined to be 'struct iovec' which is different on 32bit
1765 * and 64bit. Fortunately we can determine which structure the server
1766 * used from the size of the reply.
1767 */
1768 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
1769 size_t transferred, unsigned count,
1770 bool is_compat)
1771 {
1772 #ifdef CONFIG_COMPAT
1773 if (count * sizeof(struct compat_iovec) == transferred) {
1774 struct compat_iovec *ciov = src;
1775 unsigned i;
1776
1777 /*
1778 * With this interface a 32bit server cannot support
1779 * non-compat (i.e. ones coming from 64bit apps) ioctl
1780 * requests
1781 */
1782 if (!is_compat)
1783 return -EINVAL;
1784
1785 for (i = 0; i < count; i++) {
1786 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
1787 dst[i].iov_len = ciov[i].iov_len;
1788 }
1789 return 0;
1790 }
1791 #endif
1792
1793 if (count * sizeof(struct iovec) != transferred)
1794 return -EIO;
1795
1796 memcpy(dst, src, transferred);
1797 return 0;
1798 }
1799
1800 /* Make sure iov_length() won't overflow */
1801 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
1802 {
1803 size_t n;
1804 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
1805
1806 for (n = 0; n < count; n++, iov++) {
1807 if (iov->iov_len > (size_t) max)
1808 return -ENOMEM;
1809 max -= iov->iov_len;
1810 }
1811 return 0;
1812 }
1813
1814 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
1815 void *src, size_t transferred, unsigned count,
1816 bool is_compat)
1817 {
1818 unsigned i;
1819 struct fuse_ioctl_iovec *fiov = src;
1820
1821 if (fc->minor < 16) {
1822 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
1823 count, is_compat);
1824 }
1825
1826 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
1827 return -EIO;
1828
1829 for (i = 0; i < count; i++) {
1830 /* Did the server supply an inappropriate value? */
1831 if (fiov[i].base != (unsigned long) fiov[i].base ||
1832 fiov[i].len != (unsigned long) fiov[i].len)
1833 return -EIO;
1834
1835 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
1836 dst[i].iov_len = (size_t) fiov[i].len;
1837
1838 #ifdef CONFIG_COMPAT
1839 if (is_compat &&
1840 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
1841 (compat_size_t) dst[i].iov_len != fiov[i].len))
1842 return -EIO;
1843 #endif
1844 }
1845
1846 return 0;
1847 }
1848
1849
1850 /*
1851 * For ioctls, there is no generic way to determine how much memory
1852 * needs to be read and/or written. Furthermore, ioctls are allowed
1853 * to dereference the passed pointer, so the parameter requires deep
1854 * copying but FUSE has no idea whatsoever about what to copy in or
1855 * out.
1856 *
1857 * This is solved by allowing FUSE server to retry ioctl with
1858 * necessary in/out iovecs. Let's assume the ioctl implementation
1859 * needs to read in the following structure.
1860 *
1861 * struct a {
1862 * char *buf;
1863 * size_t buflen;
1864 * }
1865 *
1866 * On the first callout to FUSE server, inarg->in_size and
1867 * inarg->out_size will be NULL; then, the server completes the ioctl
1868 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1869 * the actual iov array to
1870 *
1871 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1872 *
1873 * which tells FUSE to copy in the requested area and retry the ioctl.
1874 * On the second round, the server has access to the structure and
1875 * from that it can tell what to look for next, so on the invocation,
1876 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1877 *
1878 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1879 * { .iov_base = a.buf, .iov_len = a.buflen } }
1880 *
1881 * FUSE will copy both struct a and the pointed buffer from the
1882 * process doing the ioctl and retry ioctl with both struct a and the
1883 * buffer.
1884 *
1885 * This time, FUSE server has everything it needs and completes ioctl
1886 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1887 *
1888 * Copying data out works the same way.
1889 *
1890 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1891 * automatically initializes in and out iovs by decoding @cmd with
1892 * _IOC_* macros and the server is not allowed to request RETRY. This
1893 * limits ioctl data transfers to well-formed ioctls and is the forced
1894 * behavior for all FUSE servers.
1895 */
1896 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
1897 unsigned int flags)
1898 {
1899 struct fuse_file *ff = file->private_data;
1900 struct fuse_conn *fc = ff->fc;
1901 struct fuse_ioctl_in inarg = {
1902 .fh = ff->fh,
1903 .cmd = cmd,
1904 .arg = arg,
1905 .flags = flags
1906 };
1907 struct fuse_ioctl_out outarg;
1908 struct fuse_req *req = NULL;
1909 struct page **pages = NULL;
1910 struct iovec *iov_page = NULL;
1911 struct iovec *in_iov = NULL, *out_iov = NULL;
1912 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
1913 size_t in_size, out_size, transferred;
1914 int err;
1915
1916 #if BITS_PER_LONG == 32
1917 inarg.flags |= FUSE_IOCTL_32BIT;
1918 #else
1919 if (flags & FUSE_IOCTL_COMPAT)
1920 inarg.flags |= FUSE_IOCTL_32BIT;
1921 #endif
1922
1923 /* assume all the iovs returned by client always fits in a page */
1924 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
1925
1926 err = -ENOMEM;
1927 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
1928 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
1929 if (!pages || !iov_page)
1930 goto out;
1931
1932 /*
1933 * If restricted, initialize IO parameters as encoded in @cmd.
1934 * RETRY from server is not allowed.
1935 */
1936 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
1937 struct iovec *iov = iov_page;
1938
1939 iov->iov_base = (void __user *)arg;
1940 iov->iov_len = _IOC_SIZE(cmd);
1941
1942 if (_IOC_DIR(cmd) & _IOC_WRITE) {
1943 in_iov = iov;
1944 in_iovs = 1;
1945 }
1946
1947 if (_IOC_DIR(cmd) & _IOC_READ) {
1948 out_iov = iov;
1949 out_iovs = 1;
1950 }
1951 }
1952
1953 retry:
1954 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
1955 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
1956
1957 /*
1958 * Out data can be used either for actual out data or iovs,
1959 * make sure there always is at least one page.
1960 */
1961 out_size = max_t(size_t, out_size, PAGE_SIZE);
1962 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
1963
1964 /* make sure there are enough buffer pages and init request with them */
1965 err = -ENOMEM;
1966 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
1967 goto out;
1968 while (num_pages < max_pages) {
1969 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
1970 if (!pages[num_pages])
1971 goto out;
1972 num_pages++;
1973 }
1974
1975 req = fuse_get_req(fc, num_pages);
1976 if (IS_ERR(req)) {
1977 err = PTR_ERR(req);
1978 req = NULL;
1979 goto out;
1980 }
1981 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
1982 req->num_pages = num_pages;
1983 fuse_page_descs_length_init(req, 0, req->num_pages);
1984
1985 /* okay, let's send it to the client */
1986 req->in.h.opcode = FUSE_IOCTL;
1987 req->in.h.nodeid = ff->nodeid;
1988 req->in.numargs = 1;
1989 req->in.args[0].size = sizeof(inarg);
1990 req->in.args[0].value = &inarg;
1991 if (in_size) {
1992 req->in.numargs++;
1993 req->in.args[1].size = in_size;
1994 req->in.argpages = 1;
1995
1996 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
1997 false);
1998 if (err)
1999 goto out;
2000 }
2001
2002 req->out.numargs = 2;
2003 req->out.args[0].size = sizeof(outarg);
2004 req->out.args[0].value = &outarg;
2005 req->out.args[1].size = out_size;
2006 req->out.argpages = 1;
2007 req->out.argvar = 1;
2008
2009 fuse_request_send(fc, req);
2010 err = req->out.h.error;
2011 transferred = req->out.args[1].size;
2012 fuse_put_request(fc, req);
2013 req = NULL;
2014 if (err)
2015 goto out;
2016
2017 /* did it ask for retry? */
2018 if (outarg.flags & FUSE_IOCTL_RETRY) {
2019 void *vaddr;
2020
2021 /* no retry if in restricted mode */
2022 err = -EIO;
2023 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2024 goto out;
2025
2026 in_iovs = outarg.in_iovs;
2027 out_iovs = outarg.out_iovs;
2028
2029 /*
2030 * Make sure things are in boundary, separate checks
2031 * are to protect against overflow.
2032 */
2033 err = -ENOMEM;
2034 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2035 out_iovs > FUSE_IOCTL_MAX_IOV ||
2036 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2037 goto out;
2038
2039 vaddr = kmap_atomic(pages[0]);
2040 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2041 transferred, in_iovs + out_iovs,
2042 (flags & FUSE_IOCTL_COMPAT) != 0);
2043 kunmap_atomic(vaddr);
2044 if (err)
2045 goto out;
2046
2047 in_iov = iov_page;
2048 out_iov = in_iov + in_iovs;
2049
2050 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2051 if (err)
2052 goto out;
2053
2054 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2055 if (err)
2056 goto out;
2057
2058 goto retry;
2059 }
2060
2061 err = -EIO;
2062 if (transferred > inarg.out_size)
2063 goto out;
2064
2065 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2066 out:
2067 if (req)
2068 fuse_put_request(fc, req);
2069 free_page((unsigned long) iov_page);
2070 while (num_pages)
2071 __free_page(pages[--num_pages]);
2072 kfree(pages);
2073
2074 return err ? err : outarg.result;
2075 }
2076 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2077
2078 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2079 unsigned long arg, unsigned int flags)
2080 {
2081 struct inode *inode = file_inode(file);
2082 struct fuse_conn *fc = get_fuse_conn(inode);
2083
2084 if (!fuse_allow_current_process(fc))
2085 return -EACCES;
2086
2087 if (is_bad_inode(inode))
2088 return -EIO;
2089
2090 return fuse_do_ioctl(file, cmd, arg, flags);
2091 }
2092
2093 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2094 unsigned long arg)
2095 {
2096 return fuse_ioctl_common(file, cmd, arg, 0);
2097 }
2098
2099 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2100 unsigned long arg)
2101 {
2102 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2103 }
2104
2105 /*
2106 * All files which have been polled are linked to RB tree
2107 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2108 * find the matching one.
2109 */
2110 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2111 struct rb_node **parent_out)
2112 {
2113 struct rb_node **link = &fc->polled_files.rb_node;
2114 struct rb_node *last = NULL;
2115
2116 while (*link) {
2117 struct fuse_file *ff;
2118
2119 last = *link;
2120 ff = rb_entry(last, struct fuse_file, polled_node);
2121
2122 if (kh < ff->kh)
2123 link = &last->rb_left;
2124 else if (kh > ff->kh)
2125 link = &last->rb_right;
2126 else
2127 return link;
2128 }
2129
2130 if (parent_out)
2131 *parent_out = last;
2132 return link;
2133 }
2134
2135 /*
2136 * The file is about to be polled. Make sure it's on the polled_files
2137 * RB tree. Note that files once added to the polled_files tree are
2138 * not removed before the file is released. This is because a file
2139 * polled once is likely to be polled again.
2140 */
2141 static void fuse_register_polled_file(struct fuse_conn *fc,
2142 struct fuse_file *ff)
2143 {
2144 spin_lock(&fc->lock);
2145 if (RB_EMPTY_NODE(&ff->polled_node)) {
2146 struct rb_node **link, *parent;
2147
2148 link = fuse_find_polled_node(fc, ff->kh, &parent);
2149 BUG_ON(*link);
2150 rb_link_node(&ff->polled_node, parent, link);
2151 rb_insert_color(&ff->polled_node, &fc->polled_files);
2152 }
2153 spin_unlock(&fc->lock);
2154 }
2155
2156 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2157 {
2158 struct fuse_file *ff = file->private_data;
2159 struct fuse_conn *fc = ff->fc;
2160 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2161 struct fuse_poll_out outarg;
2162 struct fuse_req *req;
2163 int err;
2164
2165 if (fc->no_poll)
2166 return DEFAULT_POLLMASK;
2167
2168 poll_wait(file, &ff->poll_wait, wait);
2169 inarg.events = (__u32)poll_requested_events(wait);
2170
2171 /*
2172 * Ask for notification iff there's someone waiting for it.
2173 * The client may ignore the flag and always notify.
2174 */
2175 if (waitqueue_active(&ff->poll_wait)) {
2176 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2177 fuse_register_polled_file(fc, ff);
2178 }
2179
2180 req = fuse_get_req_nopages(fc);
2181 if (IS_ERR(req))
2182 return POLLERR;
2183
2184 req->in.h.opcode = FUSE_POLL;
2185 req->in.h.nodeid = ff->nodeid;
2186 req->in.numargs = 1;
2187 req->in.args[0].size = sizeof(inarg);
2188 req->in.args[0].value = &inarg;
2189 req->out.numargs = 1;
2190 req->out.args[0].size = sizeof(outarg);
2191 req->out.args[0].value = &outarg;
2192 fuse_request_send(fc, req);
2193 err = req->out.h.error;
2194 fuse_put_request(fc, req);
2195
2196 if (!err)
2197 return outarg.revents;
2198 if (err == -ENOSYS) {
2199 fc->no_poll = 1;
2200 return DEFAULT_POLLMASK;
2201 }
2202 return POLLERR;
2203 }
2204 EXPORT_SYMBOL_GPL(fuse_file_poll);
2205
2206 /*
2207 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2208 * wakes up the poll waiters.
2209 */
2210 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2211 struct fuse_notify_poll_wakeup_out *outarg)
2212 {
2213 u64 kh = outarg->kh;
2214 struct rb_node **link;
2215
2216 spin_lock(&fc->lock);
2217
2218 link = fuse_find_polled_node(fc, kh, NULL);
2219 if (*link) {
2220 struct fuse_file *ff;
2221
2222 ff = rb_entry(*link, struct fuse_file, polled_node);
2223 wake_up_interruptible_sync(&ff->poll_wait);
2224 }
2225
2226 spin_unlock(&fc->lock);
2227 return 0;
2228 }
2229
2230 static ssize_t
2231 fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2232 loff_t offset, unsigned long nr_segs)
2233 {
2234 ssize_t ret = 0;
2235 struct file *file = NULL;
2236 loff_t pos = 0;
2237
2238 file = iocb->ki_filp;
2239 pos = offset;
2240
2241 if (rw == WRITE)
2242 ret = __fuse_direct_write(file, iov, nr_segs, &pos);
2243 else
2244 ret = __fuse_direct_read(file, iov, nr_segs, &pos);
2245
2246 return ret;
2247 }
2248
2249 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2250 loff_t length)
2251 {
2252 struct fuse_file *ff = file->private_data;
2253 struct fuse_conn *fc = ff->fc;
2254 struct fuse_req *req;
2255 struct fuse_fallocate_in inarg = {
2256 .fh = ff->fh,
2257 .offset = offset,
2258 .length = length,
2259 .mode = mode
2260 };
2261 int err;
2262
2263 if (fc->no_fallocate)
2264 return -EOPNOTSUPP;
2265
2266 req = fuse_get_req_nopages(fc);
2267 if (IS_ERR(req))
2268 return PTR_ERR(req);
2269
2270 req->in.h.opcode = FUSE_FALLOCATE;
2271 req->in.h.nodeid = ff->nodeid;
2272 req->in.numargs = 1;
2273 req->in.args[0].size = sizeof(inarg);
2274 req->in.args[0].value = &inarg;
2275 fuse_request_send(fc, req);
2276 err = req->out.h.error;
2277 if (err == -ENOSYS) {
2278 fc->no_fallocate = 1;
2279 err = -EOPNOTSUPP;
2280 }
2281 fuse_put_request(fc, req);
2282
2283 return err;
2284 }
2285
2286 static const struct file_operations fuse_file_operations = {
2287 .llseek = fuse_file_llseek,
2288 .read = do_sync_read,
2289 .aio_read = fuse_file_aio_read,
2290 .write = do_sync_write,
2291 .aio_write = fuse_file_aio_write,
2292 .mmap = fuse_file_mmap,
2293 .open = fuse_open,
2294 .flush = fuse_flush,
2295 .release = fuse_release,
2296 .fsync = fuse_fsync,
2297 .lock = fuse_file_lock,
2298 .flock = fuse_file_flock,
2299 .splice_read = generic_file_splice_read,
2300 .unlocked_ioctl = fuse_file_ioctl,
2301 .compat_ioctl = fuse_file_compat_ioctl,
2302 .poll = fuse_file_poll,
2303 .fallocate = fuse_file_fallocate,
2304 };
2305
2306 static const struct file_operations fuse_direct_io_file_operations = {
2307 .llseek = fuse_file_llseek,
2308 .read = fuse_direct_read,
2309 .write = fuse_direct_write,
2310 .mmap = fuse_direct_mmap,
2311 .open = fuse_open,
2312 .flush = fuse_flush,
2313 .release = fuse_release,
2314 .fsync = fuse_fsync,
2315 .lock = fuse_file_lock,
2316 .flock = fuse_file_flock,
2317 .unlocked_ioctl = fuse_file_ioctl,
2318 .compat_ioctl = fuse_file_compat_ioctl,
2319 .poll = fuse_file_poll,
2320 .fallocate = fuse_file_fallocate,
2321 /* no splice_read */
2322 };
2323
2324 static const struct address_space_operations fuse_file_aops = {
2325 .readpage = fuse_readpage,
2326 .writepage = fuse_writepage,
2327 .launder_page = fuse_launder_page,
2328 .readpages = fuse_readpages,
2329 .set_page_dirty = __set_page_dirty_nobuffers,
2330 .bmap = fuse_bmap,
2331 .direct_IO = fuse_direct_IO,
2332 };
2333
2334 void fuse_init_file_inode(struct inode *inode)
2335 {
2336 inode->i_fop = &fuse_file_operations;
2337 inode->i_data.a_ops = &fuse_file_aops;
2338 }