]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/fuse/file.c
fuse: update mtime on open(O_TRUNC) in atomic_o_trunc mode
[mirror_ubuntu-bionic-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 #include <linux/falloc.h>
20
21 static const struct file_operations fuse_direct_io_file_operations;
22
23 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
25 {
26 struct fuse_open_in inarg;
27 struct fuse_req *req;
28 int err;
29
30 req = fuse_get_req_nopages(fc);
31 if (IS_ERR(req))
32 return PTR_ERR(req);
33
34 memset(&inarg, 0, sizeof(inarg));
35 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
36 if (!fc->atomic_o_trunc)
37 inarg.flags &= ~O_TRUNC;
38 req->in.h.opcode = opcode;
39 req->in.h.nodeid = nodeid;
40 req->in.numargs = 1;
41 req->in.args[0].size = sizeof(inarg);
42 req->in.args[0].value = &inarg;
43 req->out.numargs = 1;
44 req->out.args[0].size = sizeof(*outargp);
45 req->out.args[0].value = outargp;
46 fuse_request_send(fc, req);
47 err = req->out.h.error;
48 fuse_put_request(fc, req);
49
50 return err;
51 }
52
53 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
54 {
55 struct fuse_file *ff;
56
57 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
58 if (unlikely(!ff))
59 return NULL;
60
61 ff->fc = fc;
62 ff->reserved_req = fuse_request_alloc(0);
63 if (unlikely(!ff->reserved_req)) {
64 kfree(ff);
65 return NULL;
66 }
67
68 INIT_LIST_HEAD(&ff->write_entry);
69 atomic_set(&ff->count, 0);
70 RB_CLEAR_NODE(&ff->polled_node);
71 init_waitqueue_head(&ff->poll_wait);
72
73 spin_lock(&fc->lock);
74 ff->kh = ++fc->khctr;
75 spin_unlock(&fc->lock);
76
77 return ff;
78 }
79
80 void fuse_file_free(struct fuse_file *ff)
81 {
82 fuse_request_free(ff->reserved_req);
83 kfree(ff);
84 }
85
86 struct fuse_file *fuse_file_get(struct fuse_file *ff)
87 {
88 atomic_inc(&ff->count);
89 return ff;
90 }
91
92 static void fuse_release_async(struct work_struct *work)
93 {
94 struct fuse_req *req;
95 struct fuse_conn *fc;
96 struct path path;
97
98 req = container_of(work, struct fuse_req, misc.release.work);
99 path = req->misc.release.path;
100 fc = get_fuse_conn(path.dentry->d_inode);
101
102 fuse_put_request(fc, req);
103 path_put(&path);
104 }
105
106 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
107 {
108 if (fc->destroy_req) {
109 /*
110 * If this is a fuseblk mount, then it's possible that
111 * releasing the path will result in releasing the
112 * super block and sending the DESTROY request. If
113 * the server is single threaded, this would hang.
114 * For this reason do the path_put() in a separate
115 * thread.
116 */
117 atomic_inc(&req->count);
118 INIT_WORK(&req->misc.release.work, fuse_release_async);
119 schedule_work(&req->misc.release.work);
120 } else {
121 path_put(&req->misc.release.path);
122 }
123 }
124
125 static void fuse_file_put(struct fuse_file *ff, bool sync)
126 {
127 if (atomic_dec_and_test(&ff->count)) {
128 struct fuse_req *req = ff->reserved_req;
129
130 if (ff->fc->no_open) {
131 /*
132 * Drop the release request when client does not
133 * implement 'open'
134 */
135 req->background = 0;
136 path_put(&req->misc.release.path);
137 fuse_put_request(ff->fc, req);
138 } else if (sync) {
139 req->background = 0;
140 fuse_request_send(ff->fc, req);
141 path_put(&req->misc.release.path);
142 fuse_put_request(ff->fc, req);
143 } else {
144 req->end = fuse_release_end;
145 req->background = 1;
146 fuse_request_send_background(ff->fc, req);
147 }
148 kfree(ff);
149 }
150 }
151
152 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
153 bool isdir)
154 {
155 struct fuse_file *ff;
156 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
157
158 ff = fuse_file_alloc(fc);
159 if (!ff)
160 return -ENOMEM;
161
162 ff->fh = 0;
163 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
164 if (!fc->no_open || isdir) {
165 struct fuse_open_out outarg;
166 int err;
167
168 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
169 if (!err) {
170 ff->fh = outarg.fh;
171 ff->open_flags = outarg.open_flags;
172
173 } else if (err != -ENOSYS || isdir) {
174 fuse_file_free(ff);
175 return err;
176 } else {
177 fc->no_open = 1;
178 }
179 }
180
181 if (isdir)
182 ff->open_flags &= ~FOPEN_DIRECT_IO;
183
184 ff->nodeid = nodeid;
185 file->private_data = fuse_file_get(ff);
186
187 return 0;
188 }
189 EXPORT_SYMBOL_GPL(fuse_do_open);
190
191 static void fuse_link_write_file(struct file *file)
192 {
193 struct inode *inode = file_inode(file);
194 struct fuse_conn *fc = get_fuse_conn(inode);
195 struct fuse_inode *fi = get_fuse_inode(inode);
196 struct fuse_file *ff = file->private_data;
197 /*
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
200 */
201 spin_lock(&fc->lock);
202 if (list_empty(&ff->write_entry))
203 list_add(&ff->write_entry, &fi->write_files);
204 spin_unlock(&fc->lock);
205 }
206
207 void fuse_finish_open(struct inode *inode, struct file *file)
208 {
209 struct fuse_file *ff = file->private_data;
210 struct fuse_conn *fc = get_fuse_conn(inode);
211
212 if (ff->open_flags & FOPEN_DIRECT_IO)
213 file->f_op = &fuse_direct_io_file_operations;
214 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
215 invalidate_inode_pages2(inode->i_mapping);
216 if (ff->open_flags & FOPEN_NONSEEKABLE)
217 nonseekable_open(inode, file);
218 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
219 struct fuse_inode *fi = get_fuse_inode(inode);
220
221 spin_lock(&fc->lock);
222 fi->attr_version = ++fc->attr_version;
223 i_size_write(inode, 0);
224 spin_unlock(&fc->lock);
225 fuse_invalidate_attr(inode);
226 if (fc->writeback_cache)
227 file_update_time(file);
228 }
229 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
230 fuse_link_write_file(file);
231 }
232
233 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
234 {
235 struct fuse_conn *fc = get_fuse_conn(inode);
236 int err;
237 bool lock_inode = (file->f_flags & O_TRUNC) &&
238 fc->atomic_o_trunc &&
239 fc->writeback_cache;
240
241 err = generic_file_open(inode, file);
242 if (err)
243 return err;
244
245 if (lock_inode)
246 mutex_lock(&inode->i_mutex);
247
248 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
249
250 if (!err)
251 fuse_finish_open(inode, file);
252
253 if (lock_inode)
254 mutex_unlock(&inode->i_mutex);
255
256 return err;
257 }
258
259 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
260 {
261 struct fuse_conn *fc = ff->fc;
262 struct fuse_req *req = ff->reserved_req;
263 struct fuse_release_in *inarg = &req->misc.release.in;
264
265 spin_lock(&fc->lock);
266 list_del(&ff->write_entry);
267 if (!RB_EMPTY_NODE(&ff->polled_node))
268 rb_erase(&ff->polled_node, &fc->polled_files);
269 spin_unlock(&fc->lock);
270
271 wake_up_interruptible_all(&ff->poll_wait);
272
273 inarg->fh = ff->fh;
274 inarg->flags = flags;
275 req->in.h.opcode = opcode;
276 req->in.h.nodeid = ff->nodeid;
277 req->in.numargs = 1;
278 req->in.args[0].size = sizeof(struct fuse_release_in);
279 req->in.args[0].value = inarg;
280 }
281
282 void fuse_release_common(struct file *file, int opcode)
283 {
284 struct fuse_file *ff;
285 struct fuse_req *req;
286
287 ff = file->private_data;
288 if (unlikely(!ff))
289 return;
290
291 req = ff->reserved_req;
292 fuse_prepare_release(ff, file->f_flags, opcode);
293
294 if (ff->flock) {
295 struct fuse_release_in *inarg = &req->misc.release.in;
296 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
297 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
298 (fl_owner_t) file);
299 }
300 /* Hold vfsmount and dentry until release is finished */
301 path_get(&file->f_path);
302 req->misc.release.path = file->f_path;
303
304 /*
305 * Normally this will send the RELEASE request, however if
306 * some asynchronous READ or WRITE requests are outstanding,
307 * the sending will be delayed.
308 *
309 * Make the release synchronous if this is a fuseblk mount,
310 * synchronous RELEASE is allowed (and desirable) in this case
311 * because the server can be trusted not to screw up.
312 */
313 fuse_file_put(ff, ff->fc->destroy_req != NULL);
314 }
315
316 static int fuse_open(struct inode *inode, struct file *file)
317 {
318 return fuse_open_common(inode, file, false);
319 }
320
321 static int fuse_release(struct inode *inode, struct file *file)
322 {
323 struct fuse_conn *fc = get_fuse_conn(inode);
324
325 /* see fuse_vma_close() for !writeback_cache case */
326 if (fc->writeback_cache)
327 filemap_write_and_wait(file->f_mapping);
328
329 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state))
330 fuse_flush_mtime(file, true);
331
332 fuse_release_common(file, FUSE_RELEASE);
333
334 /* return value is ignored by VFS */
335 return 0;
336 }
337
338 void fuse_sync_release(struct fuse_file *ff, int flags)
339 {
340 WARN_ON(atomic_read(&ff->count) > 1);
341 fuse_prepare_release(ff, flags, FUSE_RELEASE);
342 ff->reserved_req->force = 1;
343 ff->reserved_req->background = 0;
344 fuse_request_send(ff->fc, ff->reserved_req);
345 fuse_put_request(ff->fc, ff->reserved_req);
346 kfree(ff);
347 }
348 EXPORT_SYMBOL_GPL(fuse_sync_release);
349
350 /*
351 * Scramble the ID space with XTEA, so that the value of the files_struct
352 * pointer is not exposed to userspace.
353 */
354 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
355 {
356 u32 *k = fc->scramble_key;
357 u64 v = (unsigned long) id;
358 u32 v0 = v;
359 u32 v1 = v >> 32;
360 u32 sum = 0;
361 int i;
362
363 for (i = 0; i < 32; i++) {
364 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
365 sum += 0x9E3779B9;
366 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
367 }
368
369 return (u64) v0 + ((u64) v1 << 32);
370 }
371
372 /*
373 * Check if any page in a range is under writeback
374 *
375 * This is currently done by walking the list of writepage requests
376 * for the inode, which can be pretty inefficient.
377 */
378 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
379 pgoff_t idx_to)
380 {
381 struct fuse_conn *fc = get_fuse_conn(inode);
382 struct fuse_inode *fi = get_fuse_inode(inode);
383 struct fuse_req *req;
384 bool found = false;
385
386 spin_lock(&fc->lock);
387 list_for_each_entry(req, &fi->writepages, writepages_entry) {
388 pgoff_t curr_index;
389
390 BUG_ON(req->inode != inode);
391 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
392 if (idx_from < curr_index + req->num_pages &&
393 curr_index <= idx_to) {
394 found = true;
395 break;
396 }
397 }
398 spin_unlock(&fc->lock);
399
400 return found;
401 }
402
403 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
404 {
405 return fuse_range_is_writeback(inode, index, index);
406 }
407
408 /*
409 * Wait for page writeback to be completed.
410 *
411 * Since fuse doesn't rely on the VM writeback tracking, this has to
412 * use some other means.
413 */
414 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
415 {
416 struct fuse_inode *fi = get_fuse_inode(inode);
417
418 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
419 return 0;
420 }
421
422 /*
423 * Wait for all pending writepages on the inode to finish.
424 *
425 * This is currently done by blocking further writes with FUSE_NOWRITE
426 * and waiting for all sent writes to complete.
427 *
428 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
429 * could conflict with truncation.
430 */
431 static void fuse_sync_writes(struct inode *inode)
432 {
433 fuse_set_nowrite(inode);
434 fuse_release_nowrite(inode);
435 }
436
437 static int fuse_flush(struct file *file, fl_owner_t id)
438 {
439 struct inode *inode = file_inode(file);
440 struct fuse_conn *fc = get_fuse_conn(inode);
441 struct fuse_file *ff = file->private_data;
442 struct fuse_req *req;
443 struct fuse_flush_in inarg;
444 int err;
445
446 if (is_bad_inode(inode))
447 return -EIO;
448
449 if (fc->no_flush)
450 return 0;
451
452 err = filemap_write_and_wait(file->f_mapping);
453 if (err)
454 return err;
455
456 mutex_lock(&inode->i_mutex);
457 fuse_sync_writes(inode);
458 mutex_unlock(&inode->i_mutex);
459
460 req = fuse_get_req_nofail_nopages(fc, file);
461 memset(&inarg, 0, sizeof(inarg));
462 inarg.fh = ff->fh;
463 inarg.lock_owner = fuse_lock_owner_id(fc, id);
464 req->in.h.opcode = FUSE_FLUSH;
465 req->in.h.nodeid = get_node_id(inode);
466 req->in.numargs = 1;
467 req->in.args[0].size = sizeof(inarg);
468 req->in.args[0].value = &inarg;
469 req->force = 1;
470 fuse_request_send(fc, req);
471 err = req->out.h.error;
472 fuse_put_request(fc, req);
473 if (err == -ENOSYS) {
474 fc->no_flush = 1;
475 err = 0;
476 }
477 return err;
478 }
479
480 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
481 int datasync, int isdir)
482 {
483 struct inode *inode = file->f_mapping->host;
484 struct fuse_conn *fc = get_fuse_conn(inode);
485 struct fuse_file *ff = file->private_data;
486 struct fuse_req *req;
487 struct fuse_fsync_in inarg;
488 int err;
489
490 if (is_bad_inode(inode))
491 return -EIO;
492
493 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
494 if (err)
495 return err;
496
497 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
498 return 0;
499
500 mutex_lock(&inode->i_mutex);
501
502 /*
503 * Start writeback against all dirty pages of the inode, then
504 * wait for all outstanding writes, before sending the FSYNC
505 * request.
506 */
507 err = write_inode_now(inode, 0);
508 if (err)
509 goto out;
510
511 fuse_sync_writes(inode);
512
513 if (test_bit(FUSE_I_MTIME_DIRTY, &get_fuse_inode(inode)->state)) {
514 err = fuse_flush_mtime(file, false);
515 if (err)
516 goto out;
517 }
518
519 req = fuse_get_req_nopages(fc);
520 if (IS_ERR(req)) {
521 err = PTR_ERR(req);
522 goto out;
523 }
524
525 memset(&inarg, 0, sizeof(inarg));
526 inarg.fh = ff->fh;
527 inarg.fsync_flags = datasync ? 1 : 0;
528 req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
529 req->in.h.nodeid = get_node_id(inode);
530 req->in.numargs = 1;
531 req->in.args[0].size = sizeof(inarg);
532 req->in.args[0].value = &inarg;
533 fuse_request_send(fc, req);
534 err = req->out.h.error;
535 fuse_put_request(fc, req);
536 if (err == -ENOSYS) {
537 if (isdir)
538 fc->no_fsyncdir = 1;
539 else
540 fc->no_fsync = 1;
541 err = 0;
542 }
543 out:
544 mutex_unlock(&inode->i_mutex);
545 return err;
546 }
547
548 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
549 int datasync)
550 {
551 return fuse_fsync_common(file, start, end, datasync, 0);
552 }
553
554 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
555 size_t count, int opcode)
556 {
557 struct fuse_read_in *inarg = &req->misc.read.in;
558 struct fuse_file *ff = file->private_data;
559
560 inarg->fh = ff->fh;
561 inarg->offset = pos;
562 inarg->size = count;
563 inarg->flags = file->f_flags;
564 req->in.h.opcode = opcode;
565 req->in.h.nodeid = ff->nodeid;
566 req->in.numargs = 1;
567 req->in.args[0].size = sizeof(struct fuse_read_in);
568 req->in.args[0].value = inarg;
569 req->out.argvar = 1;
570 req->out.numargs = 1;
571 req->out.args[0].size = count;
572 }
573
574 static void fuse_release_user_pages(struct fuse_req *req, int write)
575 {
576 unsigned i;
577
578 for (i = 0; i < req->num_pages; i++) {
579 struct page *page = req->pages[i];
580 if (write)
581 set_page_dirty_lock(page);
582 put_page(page);
583 }
584 }
585
586 /**
587 * In case of short read, the caller sets 'pos' to the position of
588 * actual end of fuse request in IO request. Otherwise, if bytes_requested
589 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
590 *
591 * An example:
592 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
593 * both submitted asynchronously. The first of them was ACKed by userspace as
594 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
595 * second request was ACKed as short, e.g. only 1K was read, resulting in
596 * pos == 33K.
597 *
598 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
599 * will be equal to the length of the longest contiguous fragment of
600 * transferred data starting from the beginning of IO request.
601 */
602 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
603 {
604 int left;
605
606 spin_lock(&io->lock);
607 if (err)
608 io->err = io->err ? : err;
609 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
610 io->bytes = pos;
611
612 left = --io->reqs;
613 spin_unlock(&io->lock);
614
615 if (!left) {
616 long res;
617
618 if (io->err)
619 res = io->err;
620 else if (io->bytes >= 0 && io->write)
621 res = -EIO;
622 else {
623 res = io->bytes < 0 ? io->size : io->bytes;
624
625 if (!is_sync_kiocb(io->iocb)) {
626 struct inode *inode = file_inode(io->iocb->ki_filp);
627 struct fuse_conn *fc = get_fuse_conn(inode);
628 struct fuse_inode *fi = get_fuse_inode(inode);
629
630 spin_lock(&fc->lock);
631 fi->attr_version = ++fc->attr_version;
632 spin_unlock(&fc->lock);
633 }
634 }
635
636 aio_complete(io->iocb, res, 0);
637 kfree(io);
638 }
639 }
640
641 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
642 {
643 struct fuse_io_priv *io = req->io;
644 ssize_t pos = -1;
645
646 fuse_release_user_pages(req, !io->write);
647
648 if (io->write) {
649 if (req->misc.write.in.size != req->misc.write.out.size)
650 pos = req->misc.write.in.offset - io->offset +
651 req->misc.write.out.size;
652 } else {
653 if (req->misc.read.in.size != req->out.args[0].size)
654 pos = req->misc.read.in.offset - io->offset +
655 req->out.args[0].size;
656 }
657
658 fuse_aio_complete(io, req->out.h.error, pos);
659 }
660
661 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
662 size_t num_bytes, struct fuse_io_priv *io)
663 {
664 spin_lock(&io->lock);
665 io->size += num_bytes;
666 io->reqs++;
667 spin_unlock(&io->lock);
668
669 req->io = io;
670 req->end = fuse_aio_complete_req;
671
672 __fuse_get_request(req);
673 fuse_request_send_background(fc, req);
674
675 return num_bytes;
676 }
677
678 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
679 loff_t pos, size_t count, fl_owner_t owner)
680 {
681 struct file *file = io->file;
682 struct fuse_file *ff = file->private_data;
683 struct fuse_conn *fc = ff->fc;
684
685 fuse_read_fill(req, file, pos, count, FUSE_READ);
686 if (owner != NULL) {
687 struct fuse_read_in *inarg = &req->misc.read.in;
688
689 inarg->read_flags |= FUSE_READ_LOCKOWNER;
690 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
691 }
692
693 if (io->async)
694 return fuse_async_req_send(fc, req, count, io);
695
696 fuse_request_send(fc, req);
697 return req->out.args[0].size;
698 }
699
700 static void fuse_read_update_size(struct inode *inode, loff_t size,
701 u64 attr_ver)
702 {
703 struct fuse_conn *fc = get_fuse_conn(inode);
704 struct fuse_inode *fi = get_fuse_inode(inode);
705
706 spin_lock(&fc->lock);
707 if (attr_ver == fi->attr_version && size < inode->i_size &&
708 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
709 fi->attr_version = ++fc->attr_version;
710 i_size_write(inode, size);
711 }
712 spin_unlock(&fc->lock);
713 }
714
715 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
716 u64 attr_ver)
717 {
718 size_t num_read = req->out.args[0].size;
719 struct fuse_conn *fc = get_fuse_conn(inode);
720
721 if (fc->writeback_cache) {
722 /*
723 * A hole in a file. Some data after the hole are in page cache,
724 * but have not reached the client fs yet. So, the hole is not
725 * present there.
726 */
727 int i;
728 int start_idx = num_read >> PAGE_CACHE_SHIFT;
729 size_t off = num_read & (PAGE_CACHE_SIZE - 1);
730
731 for (i = start_idx; i < req->num_pages; i++) {
732 zero_user_segment(req->pages[i], off, PAGE_CACHE_SIZE);
733 off = 0;
734 }
735 } else {
736 loff_t pos = page_offset(req->pages[0]) + num_read;
737 fuse_read_update_size(inode, pos, attr_ver);
738 }
739 }
740
741 static int fuse_do_readpage(struct file *file, struct page *page)
742 {
743 struct fuse_io_priv io = { .async = 0, .file = file };
744 struct inode *inode = page->mapping->host;
745 struct fuse_conn *fc = get_fuse_conn(inode);
746 struct fuse_req *req;
747 size_t num_read;
748 loff_t pos = page_offset(page);
749 size_t count = PAGE_CACHE_SIZE;
750 u64 attr_ver;
751 int err;
752
753 /*
754 * Page writeback can extend beyond the lifetime of the
755 * page-cache page, so make sure we read a properly synced
756 * page.
757 */
758 fuse_wait_on_page_writeback(inode, page->index);
759
760 req = fuse_get_req(fc, 1);
761 if (IS_ERR(req))
762 return PTR_ERR(req);
763
764 attr_ver = fuse_get_attr_version(fc);
765
766 req->out.page_zeroing = 1;
767 req->out.argpages = 1;
768 req->num_pages = 1;
769 req->pages[0] = page;
770 req->page_descs[0].length = count;
771 num_read = fuse_send_read(req, &io, pos, count, NULL);
772 err = req->out.h.error;
773
774 if (!err) {
775 /*
776 * Short read means EOF. If file size is larger, truncate it
777 */
778 if (num_read < count)
779 fuse_short_read(req, inode, attr_ver);
780
781 SetPageUptodate(page);
782 }
783
784 fuse_put_request(fc, req);
785
786 return err;
787 }
788
789 static int fuse_readpage(struct file *file, struct page *page)
790 {
791 struct inode *inode = page->mapping->host;
792 int err;
793
794 err = -EIO;
795 if (is_bad_inode(inode))
796 goto out;
797
798 err = fuse_do_readpage(file, page);
799 fuse_invalidate_atime(inode);
800 out:
801 unlock_page(page);
802 return err;
803 }
804
805 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
806 {
807 int i;
808 size_t count = req->misc.read.in.size;
809 size_t num_read = req->out.args[0].size;
810 struct address_space *mapping = NULL;
811
812 for (i = 0; mapping == NULL && i < req->num_pages; i++)
813 mapping = req->pages[i]->mapping;
814
815 if (mapping) {
816 struct inode *inode = mapping->host;
817
818 /*
819 * Short read means EOF. If file size is larger, truncate it
820 */
821 if (!req->out.h.error && num_read < count)
822 fuse_short_read(req, inode, req->misc.read.attr_ver);
823
824 fuse_invalidate_atime(inode);
825 }
826
827 for (i = 0; i < req->num_pages; i++) {
828 struct page *page = req->pages[i];
829 if (!req->out.h.error)
830 SetPageUptodate(page);
831 else
832 SetPageError(page);
833 unlock_page(page);
834 page_cache_release(page);
835 }
836 if (req->ff)
837 fuse_file_put(req->ff, false);
838 }
839
840 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
841 {
842 struct fuse_file *ff = file->private_data;
843 struct fuse_conn *fc = ff->fc;
844 loff_t pos = page_offset(req->pages[0]);
845 size_t count = req->num_pages << PAGE_CACHE_SHIFT;
846
847 req->out.argpages = 1;
848 req->out.page_zeroing = 1;
849 req->out.page_replace = 1;
850 fuse_read_fill(req, file, pos, count, FUSE_READ);
851 req->misc.read.attr_ver = fuse_get_attr_version(fc);
852 if (fc->async_read) {
853 req->ff = fuse_file_get(ff);
854 req->end = fuse_readpages_end;
855 fuse_request_send_background(fc, req);
856 } else {
857 fuse_request_send(fc, req);
858 fuse_readpages_end(fc, req);
859 fuse_put_request(fc, req);
860 }
861 }
862
863 struct fuse_fill_data {
864 struct fuse_req *req;
865 struct file *file;
866 struct inode *inode;
867 unsigned nr_pages;
868 };
869
870 static int fuse_readpages_fill(void *_data, struct page *page)
871 {
872 struct fuse_fill_data *data = _data;
873 struct fuse_req *req = data->req;
874 struct inode *inode = data->inode;
875 struct fuse_conn *fc = get_fuse_conn(inode);
876
877 fuse_wait_on_page_writeback(inode, page->index);
878
879 if (req->num_pages &&
880 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
881 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
882 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
883 int nr_alloc = min_t(unsigned, data->nr_pages,
884 FUSE_MAX_PAGES_PER_REQ);
885 fuse_send_readpages(req, data->file);
886 if (fc->async_read)
887 req = fuse_get_req_for_background(fc, nr_alloc);
888 else
889 req = fuse_get_req(fc, nr_alloc);
890
891 data->req = req;
892 if (IS_ERR(req)) {
893 unlock_page(page);
894 return PTR_ERR(req);
895 }
896 }
897
898 if (WARN_ON(req->num_pages >= req->max_pages)) {
899 fuse_put_request(fc, req);
900 return -EIO;
901 }
902
903 page_cache_get(page);
904 req->pages[req->num_pages] = page;
905 req->page_descs[req->num_pages].length = PAGE_SIZE;
906 req->num_pages++;
907 data->nr_pages--;
908 return 0;
909 }
910
911 static int fuse_readpages(struct file *file, struct address_space *mapping,
912 struct list_head *pages, unsigned nr_pages)
913 {
914 struct inode *inode = mapping->host;
915 struct fuse_conn *fc = get_fuse_conn(inode);
916 struct fuse_fill_data data;
917 int err;
918 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
919
920 err = -EIO;
921 if (is_bad_inode(inode))
922 goto out;
923
924 data.file = file;
925 data.inode = inode;
926 if (fc->async_read)
927 data.req = fuse_get_req_for_background(fc, nr_alloc);
928 else
929 data.req = fuse_get_req(fc, nr_alloc);
930 data.nr_pages = nr_pages;
931 err = PTR_ERR(data.req);
932 if (IS_ERR(data.req))
933 goto out;
934
935 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
936 if (!err) {
937 if (data.req->num_pages)
938 fuse_send_readpages(data.req, file);
939 else
940 fuse_put_request(fc, data.req);
941 }
942 out:
943 return err;
944 }
945
946 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
947 unsigned long nr_segs, loff_t pos)
948 {
949 struct inode *inode = iocb->ki_filp->f_mapping->host;
950 struct fuse_conn *fc = get_fuse_conn(inode);
951
952 /*
953 * In auto invalidate mode, always update attributes on read.
954 * Otherwise, only update if we attempt to read past EOF (to ensure
955 * i_size is up to date).
956 */
957 if (fc->auto_inval_data ||
958 (pos + iov_length(iov, nr_segs) > i_size_read(inode))) {
959 int err;
960 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
961 if (err)
962 return err;
963 }
964
965 return generic_file_aio_read(iocb, iov, nr_segs, pos);
966 }
967
968 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
969 loff_t pos, size_t count)
970 {
971 struct fuse_write_in *inarg = &req->misc.write.in;
972 struct fuse_write_out *outarg = &req->misc.write.out;
973
974 inarg->fh = ff->fh;
975 inarg->offset = pos;
976 inarg->size = count;
977 req->in.h.opcode = FUSE_WRITE;
978 req->in.h.nodeid = ff->nodeid;
979 req->in.numargs = 2;
980 if (ff->fc->minor < 9)
981 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
982 else
983 req->in.args[0].size = sizeof(struct fuse_write_in);
984 req->in.args[0].value = inarg;
985 req->in.args[1].size = count;
986 req->out.numargs = 1;
987 req->out.args[0].size = sizeof(struct fuse_write_out);
988 req->out.args[0].value = outarg;
989 }
990
991 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
992 loff_t pos, size_t count, fl_owner_t owner)
993 {
994 struct file *file = io->file;
995 struct fuse_file *ff = file->private_data;
996 struct fuse_conn *fc = ff->fc;
997 struct fuse_write_in *inarg = &req->misc.write.in;
998
999 fuse_write_fill(req, ff, pos, count);
1000 inarg->flags = file->f_flags;
1001 if (owner != NULL) {
1002 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
1003 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
1004 }
1005
1006 if (io->async)
1007 return fuse_async_req_send(fc, req, count, io);
1008
1009 fuse_request_send(fc, req);
1010 return req->misc.write.out.size;
1011 }
1012
1013 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1014 {
1015 struct fuse_conn *fc = get_fuse_conn(inode);
1016 struct fuse_inode *fi = get_fuse_inode(inode);
1017 bool ret = false;
1018
1019 spin_lock(&fc->lock);
1020 fi->attr_version = ++fc->attr_version;
1021 if (pos > inode->i_size) {
1022 i_size_write(inode, pos);
1023 ret = true;
1024 }
1025 spin_unlock(&fc->lock);
1026
1027 return ret;
1028 }
1029
1030 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1031 struct inode *inode, loff_t pos,
1032 size_t count)
1033 {
1034 size_t res;
1035 unsigned offset;
1036 unsigned i;
1037 struct fuse_io_priv io = { .async = 0, .file = file };
1038
1039 for (i = 0; i < req->num_pages; i++)
1040 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1041
1042 res = fuse_send_write(req, &io, pos, count, NULL);
1043
1044 offset = req->page_descs[0].offset;
1045 count = res;
1046 for (i = 0; i < req->num_pages; i++) {
1047 struct page *page = req->pages[i];
1048
1049 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
1050 SetPageUptodate(page);
1051
1052 if (count > PAGE_CACHE_SIZE - offset)
1053 count -= PAGE_CACHE_SIZE - offset;
1054 else
1055 count = 0;
1056 offset = 0;
1057
1058 unlock_page(page);
1059 page_cache_release(page);
1060 }
1061
1062 return res;
1063 }
1064
1065 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1066 struct address_space *mapping,
1067 struct iov_iter *ii, loff_t pos)
1068 {
1069 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1070 unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
1071 size_t count = 0;
1072 int err;
1073
1074 req->in.argpages = 1;
1075 req->page_descs[0].offset = offset;
1076
1077 do {
1078 size_t tmp;
1079 struct page *page;
1080 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1081 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
1082 iov_iter_count(ii));
1083
1084 bytes = min_t(size_t, bytes, fc->max_write - count);
1085
1086 again:
1087 err = -EFAULT;
1088 if (iov_iter_fault_in_readable(ii, bytes))
1089 break;
1090
1091 err = -ENOMEM;
1092 page = grab_cache_page_write_begin(mapping, index, 0);
1093 if (!page)
1094 break;
1095
1096 if (mapping_writably_mapped(mapping))
1097 flush_dcache_page(page);
1098
1099 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1100 flush_dcache_page(page);
1101
1102 mark_page_accessed(page);
1103
1104 if (!tmp) {
1105 unlock_page(page);
1106 page_cache_release(page);
1107 bytes = min(bytes, iov_iter_single_seg_count(ii));
1108 goto again;
1109 }
1110
1111 err = 0;
1112 req->pages[req->num_pages] = page;
1113 req->page_descs[req->num_pages].length = tmp;
1114 req->num_pages++;
1115
1116 iov_iter_advance(ii, tmp);
1117 count += tmp;
1118 pos += tmp;
1119 offset += tmp;
1120 if (offset == PAGE_CACHE_SIZE)
1121 offset = 0;
1122
1123 if (!fc->big_writes)
1124 break;
1125 } while (iov_iter_count(ii) && count < fc->max_write &&
1126 req->num_pages < req->max_pages && offset == 0);
1127
1128 return count > 0 ? count : err;
1129 }
1130
1131 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1132 {
1133 return min_t(unsigned,
1134 ((pos + len - 1) >> PAGE_CACHE_SHIFT) -
1135 (pos >> PAGE_CACHE_SHIFT) + 1,
1136 FUSE_MAX_PAGES_PER_REQ);
1137 }
1138
1139 static ssize_t fuse_perform_write(struct file *file,
1140 struct address_space *mapping,
1141 struct iov_iter *ii, loff_t pos)
1142 {
1143 struct inode *inode = mapping->host;
1144 struct fuse_conn *fc = get_fuse_conn(inode);
1145 struct fuse_inode *fi = get_fuse_inode(inode);
1146 int err = 0;
1147 ssize_t res = 0;
1148
1149 if (is_bad_inode(inode))
1150 return -EIO;
1151
1152 if (inode->i_size < pos + iov_iter_count(ii))
1153 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1154
1155 do {
1156 struct fuse_req *req;
1157 ssize_t count;
1158 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1159
1160 req = fuse_get_req(fc, nr_pages);
1161 if (IS_ERR(req)) {
1162 err = PTR_ERR(req);
1163 break;
1164 }
1165
1166 count = fuse_fill_write_pages(req, mapping, ii, pos);
1167 if (count <= 0) {
1168 err = count;
1169 } else {
1170 size_t num_written;
1171
1172 num_written = fuse_send_write_pages(req, file, inode,
1173 pos, count);
1174 err = req->out.h.error;
1175 if (!err) {
1176 res += num_written;
1177 pos += num_written;
1178
1179 /* break out of the loop on short write */
1180 if (num_written != count)
1181 err = -EIO;
1182 }
1183 }
1184 fuse_put_request(fc, req);
1185 } while (!err && iov_iter_count(ii));
1186
1187 if (res > 0)
1188 fuse_write_update_size(inode, pos);
1189
1190 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1191 fuse_invalidate_attr(inode);
1192
1193 return res > 0 ? res : err;
1194 }
1195
1196 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
1197 unsigned long nr_segs, loff_t pos)
1198 {
1199 struct file *file = iocb->ki_filp;
1200 struct address_space *mapping = file->f_mapping;
1201 size_t count = 0;
1202 size_t ocount = 0;
1203 ssize_t written = 0;
1204 ssize_t written_buffered = 0;
1205 struct inode *inode = mapping->host;
1206 ssize_t err;
1207 struct iov_iter i;
1208 loff_t endbyte = 0;
1209
1210 if (get_fuse_conn(inode)->writeback_cache) {
1211 /* Update size (EOF optimization) and mode (SUID clearing) */
1212 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1213 if (err)
1214 return err;
1215
1216 return generic_file_aio_write(iocb, iov, nr_segs, pos);
1217 }
1218
1219 WARN_ON(iocb->ki_pos != pos);
1220
1221 ocount = 0;
1222 err = generic_segment_checks(iov, &nr_segs, &ocount, VERIFY_READ);
1223 if (err)
1224 return err;
1225
1226 count = ocount;
1227 mutex_lock(&inode->i_mutex);
1228
1229 /* We can write back this queue in page reclaim */
1230 current->backing_dev_info = mapping->backing_dev_info;
1231
1232 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
1233 if (err)
1234 goto out;
1235
1236 if (count == 0)
1237 goto out;
1238
1239 err = file_remove_suid(file);
1240 if (err)
1241 goto out;
1242
1243 err = file_update_time(file);
1244 if (err)
1245 goto out;
1246
1247 if (file->f_flags & O_DIRECT) {
1248 written = generic_file_direct_write(iocb, iov, &nr_segs, pos,
1249 count, ocount);
1250 if (written < 0 || written == count)
1251 goto out;
1252
1253 pos += written;
1254 count -= written;
1255
1256 iov_iter_init(&i, iov, nr_segs, count, written);
1257 written_buffered = fuse_perform_write(file, mapping, &i, pos);
1258 if (written_buffered < 0) {
1259 err = written_buffered;
1260 goto out;
1261 }
1262 endbyte = pos + written_buffered - 1;
1263
1264 err = filemap_write_and_wait_range(file->f_mapping, pos,
1265 endbyte);
1266 if (err)
1267 goto out;
1268
1269 invalidate_mapping_pages(file->f_mapping,
1270 pos >> PAGE_CACHE_SHIFT,
1271 endbyte >> PAGE_CACHE_SHIFT);
1272
1273 written += written_buffered;
1274 iocb->ki_pos = pos + written_buffered;
1275 } else {
1276 iov_iter_init(&i, iov, nr_segs, count, 0);
1277 written = fuse_perform_write(file, mapping, &i, pos);
1278 if (written >= 0)
1279 iocb->ki_pos = pos + written;
1280 }
1281 out:
1282 current->backing_dev_info = NULL;
1283 mutex_unlock(&inode->i_mutex);
1284
1285 return written ? written : err;
1286 }
1287
1288 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1289 unsigned index, unsigned nr_pages)
1290 {
1291 int i;
1292
1293 for (i = index; i < index + nr_pages; i++)
1294 req->page_descs[i].length = PAGE_SIZE -
1295 req->page_descs[i].offset;
1296 }
1297
1298 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1299 {
1300 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1301 }
1302
1303 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1304 size_t max_size)
1305 {
1306 return min(iov_iter_single_seg_count(ii), max_size);
1307 }
1308
1309 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1310 size_t *nbytesp, int write)
1311 {
1312 size_t nbytes = 0; /* # bytes already packed in req */
1313
1314 /* Special case for kernel I/O: can copy directly into the buffer */
1315 if (segment_eq(get_fs(), KERNEL_DS)) {
1316 unsigned long user_addr = fuse_get_user_addr(ii);
1317 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1318
1319 if (write)
1320 req->in.args[1].value = (void *) user_addr;
1321 else
1322 req->out.args[0].value = (void *) user_addr;
1323
1324 iov_iter_advance(ii, frag_size);
1325 *nbytesp = frag_size;
1326 return 0;
1327 }
1328
1329 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1330 unsigned npages;
1331 unsigned long user_addr = fuse_get_user_addr(ii);
1332 unsigned offset = user_addr & ~PAGE_MASK;
1333 size_t frag_size = fuse_get_frag_size(ii, *nbytesp - nbytes);
1334 int ret;
1335
1336 unsigned n = req->max_pages - req->num_pages;
1337 frag_size = min_t(size_t, frag_size, n << PAGE_SHIFT);
1338
1339 npages = (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1340 npages = clamp(npages, 1U, n);
1341
1342 ret = get_user_pages_fast(user_addr, npages, !write,
1343 &req->pages[req->num_pages]);
1344 if (ret < 0)
1345 return ret;
1346
1347 npages = ret;
1348 frag_size = min_t(size_t, frag_size,
1349 (npages << PAGE_SHIFT) - offset);
1350 iov_iter_advance(ii, frag_size);
1351
1352 req->page_descs[req->num_pages].offset = offset;
1353 fuse_page_descs_length_init(req, req->num_pages, npages);
1354
1355 req->num_pages += npages;
1356 req->page_descs[req->num_pages - 1].length -=
1357 (npages << PAGE_SHIFT) - offset - frag_size;
1358
1359 nbytes += frag_size;
1360 }
1361
1362 if (write)
1363 req->in.argpages = 1;
1364 else
1365 req->out.argpages = 1;
1366
1367 *nbytesp = nbytes;
1368
1369 return 0;
1370 }
1371
1372 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1373 {
1374 struct iov_iter ii = *ii_p;
1375 int npages = 0;
1376
1377 while (iov_iter_count(&ii) && npages < FUSE_MAX_PAGES_PER_REQ) {
1378 unsigned long user_addr = fuse_get_user_addr(&ii);
1379 unsigned offset = user_addr & ~PAGE_MASK;
1380 size_t frag_size = iov_iter_single_seg_count(&ii);
1381
1382 npages += (frag_size + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1383 iov_iter_advance(&ii, frag_size);
1384 }
1385
1386 return min(npages, FUSE_MAX_PAGES_PER_REQ);
1387 }
1388
1389 ssize_t fuse_direct_io(struct fuse_io_priv *io, const struct iovec *iov,
1390 unsigned long nr_segs, size_t count, loff_t *ppos,
1391 int flags)
1392 {
1393 int write = flags & FUSE_DIO_WRITE;
1394 int cuse = flags & FUSE_DIO_CUSE;
1395 struct file *file = io->file;
1396 struct inode *inode = file->f_mapping->host;
1397 struct fuse_file *ff = file->private_data;
1398 struct fuse_conn *fc = ff->fc;
1399 size_t nmax = write ? fc->max_write : fc->max_read;
1400 loff_t pos = *ppos;
1401 pgoff_t idx_from = pos >> PAGE_CACHE_SHIFT;
1402 pgoff_t idx_to = (pos + count - 1) >> PAGE_CACHE_SHIFT;
1403 ssize_t res = 0;
1404 struct fuse_req *req;
1405 struct iov_iter ii;
1406
1407 iov_iter_init(&ii, iov, nr_segs, count, 0);
1408
1409 if (io->async)
1410 req = fuse_get_req_for_background(fc, fuse_iter_npages(&ii));
1411 else
1412 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1413 if (IS_ERR(req))
1414 return PTR_ERR(req);
1415
1416 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1417 if (!write)
1418 mutex_lock(&inode->i_mutex);
1419 fuse_sync_writes(inode);
1420 if (!write)
1421 mutex_unlock(&inode->i_mutex);
1422 }
1423
1424 while (count) {
1425 size_t nres;
1426 fl_owner_t owner = current->files;
1427 size_t nbytes = min(count, nmax);
1428 int err = fuse_get_user_pages(req, &ii, &nbytes, write);
1429 if (err) {
1430 res = err;
1431 break;
1432 }
1433
1434 if (write)
1435 nres = fuse_send_write(req, io, pos, nbytes, owner);
1436 else
1437 nres = fuse_send_read(req, io, pos, nbytes, owner);
1438
1439 if (!io->async)
1440 fuse_release_user_pages(req, !write);
1441 if (req->out.h.error) {
1442 if (!res)
1443 res = req->out.h.error;
1444 break;
1445 } else if (nres > nbytes) {
1446 res = -EIO;
1447 break;
1448 }
1449 count -= nres;
1450 res += nres;
1451 pos += nres;
1452 if (nres != nbytes)
1453 break;
1454 if (count) {
1455 fuse_put_request(fc, req);
1456 if (io->async)
1457 req = fuse_get_req_for_background(fc,
1458 fuse_iter_npages(&ii));
1459 else
1460 req = fuse_get_req(fc, fuse_iter_npages(&ii));
1461 if (IS_ERR(req))
1462 break;
1463 }
1464 }
1465 if (!IS_ERR(req))
1466 fuse_put_request(fc, req);
1467 if (res > 0)
1468 *ppos = pos;
1469
1470 return res;
1471 }
1472 EXPORT_SYMBOL_GPL(fuse_direct_io);
1473
1474 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1475 const struct iovec *iov,
1476 unsigned long nr_segs, loff_t *ppos,
1477 size_t count)
1478 {
1479 ssize_t res;
1480 struct file *file = io->file;
1481 struct inode *inode = file_inode(file);
1482
1483 if (is_bad_inode(inode))
1484 return -EIO;
1485
1486 res = fuse_direct_io(io, iov, nr_segs, count, ppos, 0);
1487
1488 fuse_invalidate_attr(inode);
1489
1490 return res;
1491 }
1492
1493 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1494 size_t count, loff_t *ppos)
1495 {
1496 struct fuse_io_priv io = { .async = 0, .file = file };
1497 struct iovec iov = { .iov_base = buf, .iov_len = count };
1498 return __fuse_direct_read(&io, &iov, 1, ppos, count);
1499 }
1500
1501 static ssize_t __fuse_direct_write(struct fuse_io_priv *io,
1502 const struct iovec *iov,
1503 unsigned long nr_segs, loff_t *ppos)
1504 {
1505 struct file *file = io->file;
1506 struct inode *inode = file_inode(file);
1507 size_t count = iov_length(iov, nr_segs);
1508 ssize_t res;
1509
1510 res = generic_write_checks(file, ppos, &count, 0);
1511 if (!res)
1512 res = fuse_direct_io(io, iov, nr_segs, count, ppos,
1513 FUSE_DIO_WRITE);
1514
1515 fuse_invalidate_attr(inode);
1516
1517 return res;
1518 }
1519
1520 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1521 size_t count, loff_t *ppos)
1522 {
1523 struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = count };
1524 struct inode *inode = file_inode(file);
1525 ssize_t res;
1526 struct fuse_io_priv io = { .async = 0, .file = file };
1527
1528 if (is_bad_inode(inode))
1529 return -EIO;
1530
1531 /* Don't allow parallel writes to the same file */
1532 mutex_lock(&inode->i_mutex);
1533 res = __fuse_direct_write(&io, &iov, 1, ppos);
1534 if (res > 0)
1535 fuse_write_update_size(inode, *ppos);
1536 mutex_unlock(&inode->i_mutex);
1537
1538 return res;
1539 }
1540
1541 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1542 {
1543 int i;
1544
1545 for (i = 0; i < req->num_pages; i++)
1546 __free_page(req->pages[i]);
1547
1548 if (req->ff)
1549 fuse_file_put(req->ff, false);
1550 }
1551
1552 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1553 {
1554 struct inode *inode = req->inode;
1555 struct fuse_inode *fi = get_fuse_inode(inode);
1556 struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1557 int i;
1558
1559 list_del(&req->writepages_entry);
1560 for (i = 0; i < req->num_pages; i++) {
1561 dec_bdi_stat(bdi, BDI_WRITEBACK);
1562 dec_zone_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1563 bdi_writeout_inc(bdi);
1564 }
1565 wake_up(&fi->page_waitq);
1566 }
1567
1568 /* Called under fc->lock, may release and reacquire it */
1569 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1570 loff_t size)
1571 __releases(fc->lock)
1572 __acquires(fc->lock)
1573 {
1574 struct fuse_inode *fi = get_fuse_inode(req->inode);
1575 struct fuse_write_in *inarg = &req->misc.write.in;
1576 __u64 data_size = req->num_pages * PAGE_CACHE_SIZE;
1577
1578 if (!fc->connected)
1579 goto out_free;
1580
1581 if (inarg->offset + data_size <= size) {
1582 inarg->size = data_size;
1583 } else if (inarg->offset < size) {
1584 inarg->size = size - inarg->offset;
1585 } else {
1586 /* Got truncated off completely */
1587 goto out_free;
1588 }
1589
1590 req->in.args[1].size = inarg->size;
1591 fi->writectr++;
1592 fuse_request_send_background_locked(fc, req);
1593 return;
1594
1595 out_free:
1596 fuse_writepage_finish(fc, req);
1597 spin_unlock(&fc->lock);
1598 fuse_writepage_free(fc, req);
1599 fuse_put_request(fc, req);
1600 spin_lock(&fc->lock);
1601 }
1602
1603 /*
1604 * If fi->writectr is positive (no truncate or fsync going on) send
1605 * all queued writepage requests.
1606 *
1607 * Called with fc->lock
1608 */
1609 void fuse_flush_writepages(struct inode *inode)
1610 __releases(fc->lock)
1611 __acquires(fc->lock)
1612 {
1613 struct fuse_conn *fc = get_fuse_conn(inode);
1614 struct fuse_inode *fi = get_fuse_inode(inode);
1615 size_t crop = i_size_read(inode);
1616 struct fuse_req *req;
1617
1618 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1619 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1620 list_del_init(&req->list);
1621 fuse_send_writepage(fc, req, crop);
1622 }
1623 }
1624
1625 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1626 {
1627 struct inode *inode = req->inode;
1628 struct fuse_inode *fi = get_fuse_inode(inode);
1629
1630 mapping_set_error(inode->i_mapping, req->out.h.error);
1631 spin_lock(&fc->lock);
1632 while (req->misc.write.next) {
1633 struct fuse_conn *fc = get_fuse_conn(inode);
1634 struct fuse_write_in *inarg = &req->misc.write.in;
1635 struct fuse_req *next = req->misc.write.next;
1636 req->misc.write.next = next->misc.write.next;
1637 next->misc.write.next = NULL;
1638 next->ff = fuse_file_get(req->ff);
1639 list_add(&next->writepages_entry, &fi->writepages);
1640
1641 /*
1642 * Skip fuse_flush_writepages() to make it easy to crop requests
1643 * based on primary request size.
1644 *
1645 * 1st case (trivial): there are no concurrent activities using
1646 * fuse_set/release_nowrite. Then we're on safe side because
1647 * fuse_flush_writepages() would call fuse_send_writepage()
1648 * anyway.
1649 *
1650 * 2nd case: someone called fuse_set_nowrite and it is waiting
1651 * now for completion of all in-flight requests. This happens
1652 * rarely and no more than once per page, so this should be
1653 * okay.
1654 *
1655 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1656 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1657 * that fuse_set_nowrite returned implies that all in-flight
1658 * requests were completed along with all of their secondary
1659 * requests. Further primary requests are blocked by negative
1660 * writectr. Hence there cannot be any in-flight requests and
1661 * no invocations of fuse_writepage_end() while we're in
1662 * fuse_set_nowrite..fuse_release_nowrite section.
1663 */
1664 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1665 }
1666 fi->writectr--;
1667 fuse_writepage_finish(fc, req);
1668 spin_unlock(&fc->lock);
1669 fuse_writepage_free(fc, req);
1670 }
1671
1672 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1673 struct fuse_inode *fi)
1674 {
1675 struct fuse_file *ff = NULL;
1676
1677 spin_lock(&fc->lock);
1678 if (!WARN_ON(list_empty(&fi->write_files))) {
1679 ff = list_entry(fi->write_files.next, struct fuse_file,
1680 write_entry);
1681 fuse_file_get(ff);
1682 }
1683 spin_unlock(&fc->lock);
1684
1685 return ff;
1686 }
1687
1688 static int fuse_writepage_locked(struct page *page)
1689 {
1690 struct address_space *mapping = page->mapping;
1691 struct inode *inode = mapping->host;
1692 struct fuse_conn *fc = get_fuse_conn(inode);
1693 struct fuse_inode *fi = get_fuse_inode(inode);
1694 struct fuse_req *req;
1695 struct page *tmp_page;
1696 int error = -ENOMEM;
1697
1698 set_page_writeback(page);
1699
1700 req = fuse_request_alloc_nofs(1);
1701 if (!req)
1702 goto err;
1703
1704 req->background = 1; /* writeback always goes to bg_queue */
1705 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1706 if (!tmp_page)
1707 goto err_free;
1708
1709 error = -EIO;
1710 req->ff = fuse_write_file_get(fc, fi);
1711 if (!req->ff)
1712 goto err_free;
1713
1714 fuse_write_fill(req, req->ff, page_offset(page), 0);
1715
1716 copy_highpage(tmp_page, page);
1717 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1718 req->misc.write.next = NULL;
1719 req->in.argpages = 1;
1720 req->num_pages = 1;
1721 req->pages[0] = tmp_page;
1722 req->page_descs[0].offset = 0;
1723 req->page_descs[0].length = PAGE_SIZE;
1724 req->end = fuse_writepage_end;
1725 req->inode = inode;
1726
1727 inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1728 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1729
1730 spin_lock(&fc->lock);
1731 list_add(&req->writepages_entry, &fi->writepages);
1732 list_add_tail(&req->list, &fi->queued_writes);
1733 fuse_flush_writepages(inode);
1734 spin_unlock(&fc->lock);
1735
1736 end_page_writeback(page);
1737
1738 return 0;
1739
1740 err_free:
1741 fuse_request_free(req);
1742 err:
1743 end_page_writeback(page);
1744 return error;
1745 }
1746
1747 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1748 {
1749 int err;
1750
1751 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1752 /*
1753 * ->writepages() should be called for sync() and friends. We
1754 * should only get here on direct reclaim and then we are
1755 * allowed to skip a page which is already in flight
1756 */
1757 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1758
1759 redirty_page_for_writepage(wbc, page);
1760 return 0;
1761 }
1762
1763 err = fuse_writepage_locked(page);
1764 unlock_page(page);
1765
1766 return err;
1767 }
1768
1769 struct fuse_fill_wb_data {
1770 struct fuse_req *req;
1771 struct fuse_file *ff;
1772 struct inode *inode;
1773 struct page **orig_pages;
1774 };
1775
1776 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1777 {
1778 struct fuse_req *req = data->req;
1779 struct inode *inode = data->inode;
1780 struct fuse_conn *fc = get_fuse_conn(inode);
1781 struct fuse_inode *fi = get_fuse_inode(inode);
1782 int num_pages = req->num_pages;
1783 int i;
1784
1785 req->ff = fuse_file_get(data->ff);
1786 spin_lock(&fc->lock);
1787 list_add_tail(&req->list, &fi->queued_writes);
1788 fuse_flush_writepages(inode);
1789 spin_unlock(&fc->lock);
1790
1791 for (i = 0; i < num_pages; i++)
1792 end_page_writeback(data->orig_pages[i]);
1793 }
1794
1795 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1796 struct page *page)
1797 {
1798 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1799 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1800 struct fuse_req *tmp;
1801 struct fuse_req *old_req;
1802 bool found = false;
1803 pgoff_t curr_index;
1804
1805 BUG_ON(new_req->num_pages != 0);
1806
1807 spin_lock(&fc->lock);
1808 list_del(&new_req->writepages_entry);
1809 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1810 BUG_ON(old_req->inode != new_req->inode);
1811 curr_index = old_req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1812 if (curr_index <= page->index &&
1813 page->index < curr_index + old_req->num_pages) {
1814 found = true;
1815 break;
1816 }
1817 }
1818 if (!found) {
1819 list_add(&new_req->writepages_entry, &fi->writepages);
1820 goto out_unlock;
1821 }
1822
1823 new_req->num_pages = 1;
1824 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1825 BUG_ON(tmp->inode != new_req->inode);
1826 curr_index = tmp->misc.write.in.offset >> PAGE_CACHE_SHIFT;
1827 if (tmp->num_pages == 1 &&
1828 curr_index == page->index) {
1829 old_req = tmp;
1830 }
1831 }
1832
1833 if (old_req->num_pages == 1 && (old_req->state == FUSE_REQ_INIT ||
1834 old_req->state == FUSE_REQ_PENDING)) {
1835 struct backing_dev_info *bdi = page->mapping->backing_dev_info;
1836
1837 copy_highpage(old_req->pages[0], page);
1838 spin_unlock(&fc->lock);
1839
1840 dec_bdi_stat(bdi, BDI_WRITEBACK);
1841 dec_zone_page_state(page, NR_WRITEBACK_TEMP);
1842 bdi_writeout_inc(bdi);
1843 fuse_writepage_free(fc, new_req);
1844 fuse_request_free(new_req);
1845 goto out;
1846 } else {
1847 new_req->misc.write.next = old_req->misc.write.next;
1848 old_req->misc.write.next = new_req;
1849 }
1850 out_unlock:
1851 spin_unlock(&fc->lock);
1852 out:
1853 return found;
1854 }
1855
1856 static int fuse_writepages_fill(struct page *page,
1857 struct writeback_control *wbc, void *_data)
1858 {
1859 struct fuse_fill_wb_data *data = _data;
1860 struct fuse_req *req = data->req;
1861 struct inode *inode = data->inode;
1862 struct fuse_conn *fc = get_fuse_conn(inode);
1863 struct page *tmp_page;
1864 bool is_writeback;
1865 int err;
1866
1867 if (!data->ff) {
1868 err = -EIO;
1869 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1870 if (!data->ff)
1871 goto out_unlock;
1872 }
1873
1874 /*
1875 * Being under writeback is unlikely but possible. For example direct
1876 * read to an mmaped fuse file will set the page dirty twice; once when
1877 * the pages are faulted with get_user_pages(), and then after the read
1878 * completed.
1879 */
1880 is_writeback = fuse_page_is_writeback(inode, page->index);
1881
1882 if (req && req->num_pages &&
1883 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1884 (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_write ||
1885 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1886 fuse_writepages_send(data);
1887 data->req = NULL;
1888 }
1889 err = -ENOMEM;
1890 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1891 if (!tmp_page)
1892 goto out_unlock;
1893
1894 /*
1895 * The page must not be redirtied until the writeout is completed
1896 * (i.e. userspace has sent a reply to the write request). Otherwise
1897 * there could be more than one temporary page instance for each real
1898 * page.
1899 *
1900 * This is ensured by holding the page lock in page_mkwrite() while
1901 * checking fuse_page_is_writeback(). We already hold the page lock
1902 * since clear_page_dirty_for_io() and keep it held until we add the
1903 * request to the fi->writepages list and increment req->num_pages.
1904 * After this fuse_page_is_writeback() will indicate that the page is
1905 * under writeback, so we can release the page lock.
1906 */
1907 if (data->req == NULL) {
1908 struct fuse_inode *fi = get_fuse_inode(inode);
1909
1910 err = -ENOMEM;
1911 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1912 if (!req) {
1913 __free_page(tmp_page);
1914 goto out_unlock;
1915 }
1916
1917 fuse_write_fill(req, data->ff, page_offset(page), 0);
1918 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1919 req->misc.write.next = NULL;
1920 req->in.argpages = 1;
1921 req->background = 1;
1922 req->num_pages = 0;
1923 req->end = fuse_writepage_end;
1924 req->inode = inode;
1925
1926 spin_lock(&fc->lock);
1927 list_add(&req->writepages_entry, &fi->writepages);
1928 spin_unlock(&fc->lock);
1929
1930 data->req = req;
1931 }
1932 set_page_writeback(page);
1933
1934 copy_highpage(tmp_page, page);
1935 req->pages[req->num_pages] = tmp_page;
1936 req->page_descs[req->num_pages].offset = 0;
1937 req->page_descs[req->num_pages].length = PAGE_SIZE;
1938
1939 inc_bdi_stat(page->mapping->backing_dev_info, BDI_WRITEBACK);
1940 inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1941
1942 err = 0;
1943 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1944 end_page_writeback(page);
1945 data->req = NULL;
1946 goto out_unlock;
1947 }
1948 data->orig_pages[req->num_pages] = page;
1949
1950 /*
1951 * Protected by fc->lock against concurrent access by
1952 * fuse_page_is_writeback().
1953 */
1954 spin_lock(&fc->lock);
1955 req->num_pages++;
1956 spin_unlock(&fc->lock);
1957
1958 out_unlock:
1959 unlock_page(page);
1960
1961 return err;
1962 }
1963
1964 static int fuse_writepages(struct address_space *mapping,
1965 struct writeback_control *wbc)
1966 {
1967 struct inode *inode = mapping->host;
1968 struct fuse_fill_wb_data data;
1969 int err;
1970
1971 err = -EIO;
1972 if (is_bad_inode(inode))
1973 goto out;
1974
1975 data.inode = inode;
1976 data.req = NULL;
1977 data.ff = NULL;
1978
1979 err = -ENOMEM;
1980 data.orig_pages = kzalloc(sizeof(struct page *) *
1981 FUSE_MAX_PAGES_PER_REQ,
1982 GFP_NOFS);
1983 if (!data.orig_pages)
1984 goto out;
1985
1986 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1987 if (data.req) {
1988 /* Ignore errors if we can write at least one page */
1989 BUG_ON(!data.req->num_pages);
1990 fuse_writepages_send(&data);
1991 err = 0;
1992 }
1993 if (data.ff)
1994 fuse_file_put(data.ff, false);
1995
1996 kfree(data.orig_pages);
1997 out:
1998 return err;
1999 }
2000
2001 /*
2002 * It's worthy to make sure that space is reserved on disk for the write,
2003 * but how to implement it without killing performance need more thinking.
2004 */
2005 static int fuse_write_begin(struct file *file, struct address_space *mapping,
2006 loff_t pos, unsigned len, unsigned flags,
2007 struct page **pagep, void **fsdata)
2008 {
2009 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
2010 struct fuse_conn *fc = get_fuse_conn(file->f_dentry->d_inode);
2011 struct page *page;
2012 loff_t fsize;
2013 int err = -ENOMEM;
2014
2015 WARN_ON(!fc->writeback_cache);
2016
2017 page = grab_cache_page_write_begin(mapping, index, flags);
2018 if (!page)
2019 goto error;
2020
2021 fuse_wait_on_page_writeback(mapping->host, page->index);
2022
2023 if (PageUptodate(page) || len == PAGE_CACHE_SIZE)
2024 goto success;
2025 /*
2026 * Check if the start this page comes after the end of file, in which
2027 * case the readpage can be optimized away.
2028 */
2029 fsize = i_size_read(mapping->host);
2030 if (fsize <= (pos & PAGE_CACHE_MASK)) {
2031 size_t off = pos & ~PAGE_CACHE_MASK;
2032 if (off)
2033 zero_user_segment(page, 0, off);
2034 goto success;
2035 }
2036 err = fuse_do_readpage(file, page);
2037 if (err)
2038 goto cleanup;
2039 success:
2040 *pagep = page;
2041 return 0;
2042
2043 cleanup:
2044 unlock_page(page);
2045 page_cache_release(page);
2046 error:
2047 return err;
2048 }
2049
2050 static int fuse_write_end(struct file *file, struct address_space *mapping,
2051 loff_t pos, unsigned len, unsigned copied,
2052 struct page *page, void *fsdata)
2053 {
2054 struct inode *inode = page->mapping->host;
2055
2056 if (!PageUptodate(page)) {
2057 /* Zero any unwritten bytes at the end of the page */
2058 size_t endoff = (pos + copied) & ~PAGE_CACHE_MASK;
2059 if (endoff)
2060 zero_user_segment(page, endoff, PAGE_CACHE_SIZE);
2061 SetPageUptodate(page);
2062 }
2063
2064 fuse_write_update_size(inode, pos + copied);
2065 set_page_dirty(page);
2066 unlock_page(page);
2067 page_cache_release(page);
2068
2069 return copied;
2070 }
2071
2072 static int fuse_launder_page(struct page *page)
2073 {
2074 int err = 0;
2075 if (clear_page_dirty_for_io(page)) {
2076 struct inode *inode = page->mapping->host;
2077 err = fuse_writepage_locked(page);
2078 if (!err)
2079 fuse_wait_on_page_writeback(inode, page->index);
2080 }
2081 return err;
2082 }
2083
2084 /*
2085 * Write back dirty pages now, because there may not be any suitable
2086 * open files later
2087 */
2088 static void fuse_vma_close(struct vm_area_struct *vma)
2089 {
2090 filemap_write_and_wait(vma->vm_file->f_mapping);
2091 }
2092
2093 /*
2094 * Wait for writeback against this page to complete before allowing it
2095 * to be marked dirty again, and hence written back again, possibly
2096 * before the previous writepage completed.
2097 *
2098 * Block here, instead of in ->writepage(), so that the userspace fs
2099 * can only block processes actually operating on the filesystem.
2100 *
2101 * Otherwise unprivileged userspace fs would be able to block
2102 * unrelated:
2103 *
2104 * - page migration
2105 * - sync(2)
2106 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2107 */
2108 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2109 {
2110 struct page *page = vmf->page;
2111 struct inode *inode = file_inode(vma->vm_file);
2112
2113 file_update_time(vma->vm_file);
2114 lock_page(page);
2115 if (page->mapping != inode->i_mapping) {
2116 unlock_page(page);
2117 return VM_FAULT_NOPAGE;
2118 }
2119
2120 fuse_wait_on_page_writeback(inode, page->index);
2121 return VM_FAULT_LOCKED;
2122 }
2123
2124 static const struct vm_operations_struct fuse_file_vm_ops = {
2125 .close = fuse_vma_close,
2126 .fault = filemap_fault,
2127 .map_pages = filemap_map_pages,
2128 .page_mkwrite = fuse_page_mkwrite,
2129 .remap_pages = generic_file_remap_pages,
2130 };
2131
2132 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2133 {
2134 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2135 fuse_link_write_file(file);
2136
2137 file_accessed(file);
2138 vma->vm_ops = &fuse_file_vm_ops;
2139 return 0;
2140 }
2141
2142 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2143 {
2144 /* Can't provide the coherency needed for MAP_SHARED */
2145 if (vma->vm_flags & VM_MAYSHARE)
2146 return -ENODEV;
2147
2148 invalidate_inode_pages2(file->f_mapping);
2149
2150 return generic_file_mmap(file, vma);
2151 }
2152
2153 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2154 struct file_lock *fl)
2155 {
2156 switch (ffl->type) {
2157 case F_UNLCK:
2158 break;
2159
2160 case F_RDLCK:
2161 case F_WRLCK:
2162 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2163 ffl->end < ffl->start)
2164 return -EIO;
2165
2166 fl->fl_start = ffl->start;
2167 fl->fl_end = ffl->end;
2168 fl->fl_pid = ffl->pid;
2169 break;
2170
2171 default:
2172 return -EIO;
2173 }
2174 fl->fl_type = ffl->type;
2175 return 0;
2176 }
2177
2178 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
2179 const struct file_lock *fl, int opcode, pid_t pid,
2180 int flock)
2181 {
2182 struct inode *inode = file_inode(file);
2183 struct fuse_conn *fc = get_fuse_conn(inode);
2184 struct fuse_file *ff = file->private_data;
2185 struct fuse_lk_in *arg = &req->misc.lk_in;
2186
2187 arg->fh = ff->fh;
2188 arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2189 arg->lk.start = fl->fl_start;
2190 arg->lk.end = fl->fl_end;
2191 arg->lk.type = fl->fl_type;
2192 arg->lk.pid = pid;
2193 if (flock)
2194 arg->lk_flags |= FUSE_LK_FLOCK;
2195 req->in.h.opcode = opcode;
2196 req->in.h.nodeid = get_node_id(inode);
2197 req->in.numargs = 1;
2198 req->in.args[0].size = sizeof(*arg);
2199 req->in.args[0].value = arg;
2200 }
2201
2202 static int fuse_getlk(struct file *file, struct file_lock *fl)
2203 {
2204 struct inode *inode = file_inode(file);
2205 struct fuse_conn *fc = get_fuse_conn(inode);
2206 struct fuse_req *req;
2207 struct fuse_lk_out outarg;
2208 int err;
2209
2210 req = fuse_get_req_nopages(fc);
2211 if (IS_ERR(req))
2212 return PTR_ERR(req);
2213
2214 fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
2215 req->out.numargs = 1;
2216 req->out.args[0].size = sizeof(outarg);
2217 req->out.args[0].value = &outarg;
2218 fuse_request_send(fc, req);
2219 err = req->out.h.error;
2220 fuse_put_request(fc, req);
2221 if (!err)
2222 err = convert_fuse_file_lock(&outarg.lk, fl);
2223
2224 return err;
2225 }
2226
2227 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2228 {
2229 struct inode *inode = file_inode(file);
2230 struct fuse_conn *fc = get_fuse_conn(inode);
2231 struct fuse_req *req;
2232 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2233 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2234 int err;
2235
2236 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2237 /* NLM needs asynchronous locks, which we don't support yet */
2238 return -ENOLCK;
2239 }
2240
2241 /* Unlock on close is handled by the flush method */
2242 if (fl->fl_flags & FL_CLOSE)
2243 return 0;
2244
2245 req = fuse_get_req_nopages(fc);
2246 if (IS_ERR(req))
2247 return PTR_ERR(req);
2248
2249 fuse_lk_fill(req, file, fl, opcode, pid, flock);
2250 fuse_request_send(fc, req);
2251 err = req->out.h.error;
2252 /* locking is restartable */
2253 if (err == -EINTR)
2254 err = -ERESTARTSYS;
2255 fuse_put_request(fc, req);
2256 return err;
2257 }
2258
2259 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2260 {
2261 struct inode *inode = file_inode(file);
2262 struct fuse_conn *fc = get_fuse_conn(inode);
2263 int err;
2264
2265 if (cmd == F_CANCELLK) {
2266 err = 0;
2267 } else if (cmd == F_GETLK) {
2268 if (fc->no_lock) {
2269 posix_test_lock(file, fl);
2270 err = 0;
2271 } else
2272 err = fuse_getlk(file, fl);
2273 } else {
2274 if (fc->no_lock)
2275 err = posix_lock_file(file, fl, NULL);
2276 else
2277 err = fuse_setlk(file, fl, 0);
2278 }
2279 return err;
2280 }
2281
2282 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2283 {
2284 struct inode *inode = file_inode(file);
2285 struct fuse_conn *fc = get_fuse_conn(inode);
2286 int err;
2287
2288 if (fc->no_flock) {
2289 err = flock_lock_file_wait(file, fl);
2290 } else {
2291 struct fuse_file *ff = file->private_data;
2292
2293 /* emulate flock with POSIX locks */
2294 fl->fl_owner = (fl_owner_t) file;
2295 ff->flock = true;
2296 err = fuse_setlk(file, fl, 1);
2297 }
2298
2299 return err;
2300 }
2301
2302 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2303 {
2304 struct inode *inode = mapping->host;
2305 struct fuse_conn *fc = get_fuse_conn(inode);
2306 struct fuse_req *req;
2307 struct fuse_bmap_in inarg;
2308 struct fuse_bmap_out outarg;
2309 int err;
2310
2311 if (!inode->i_sb->s_bdev || fc->no_bmap)
2312 return 0;
2313
2314 req = fuse_get_req_nopages(fc);
2315 if (IS_ERR(req))
2316 return 0;
2317
2318 memset(&inarg, 0, sizeof(inarg));
2319 inarg.block = block;
2320 inarg.blocksize = inode->i_sb->s_blocksize;
2321 req->in.h.opcode = FUSE_BMAP;
2322 req->in.h.nodeid = get_node_id(inode);
2323 req->in.numargs = 1;
2324 req->in.args[0].size = sizeof(inarg);
2325 req->in.args[0].value = &inarg;
2326 req->out.numargs = 1;
2327 req->out.args[0].size = sizeof(outarg);
2328 req->out.args[0].value = &outarg;
2329 fuse_request_send(fc, req);
2330 err = req->out.h.error;
2331 fuse_put_request(fc, req);
2332 if (err == -ENOSYS)
2333 fc->no_bmap = 1;
2334
2335 return err ? 0 : outarg.block;
2336 }
2337
2338 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2339 {
2340 loff_t retval;
2341 struct inode *inode = file_inode(file);
2342
2343 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2344 if (whence == SEEK_CUR || whence == SEEK_SET)
2345 return generic_file_llseek(file, offset, whence);
2346
2347 mutex_lock(&inode->i_mutex);
2348 retval = fuse_update_attributes(inode, NULL, file, NULL);
2349 if (!retval)
2350 retval = generic_file_llseek(file, offset, whence);
2351 mutex_unlock(&inode->i_mutex);
2352
2353 return retval;
2354 }
2355
2356 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2357 unsigned int nr_segs, size_t bytes, bool to_user)
2358 {
2359 struct iov_iter ii;
2360 int page_idx = 0;
2361
2362 if (!bytes)
2363 return 0;
2364
2365 iov_iter_init(&ii, iov, nr_segs, bytes, 0);
2366
2367 while (iov_iter_count(&ii)) {
2368 struct page *page = pages[page_idx++];
2369 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
2370 void *kaddr;
2371
2372 kaddr = kmap(page);
2373
2374 while (todo) {
2375 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2376 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2377 size_t copy = min(todo, iov_len);
2378 size_t left;
2379
2380 if (!to_user)
2381 left = copy_from_user(kaddr, uaddr, copy);
2382 else
2383 left = copy_to_user(uaddr, kaddr, copy);
2384
2385 if (unlikely(left))
2386 return -EFAULT;
2387
2388 iov_iter_advance(&ii, copy);
2389 todo -= copy;
2390 kaddr += copy;
2391 }
2392
2393 kunmap(page);
2394 }
2395
2396 return 0;
2397 }
2398
2399 /*
2400 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2401 * ABI was defined to be 'struct iovec' which is different on 32bit
2402 * and 64bit. Fortunately we can determine which structure the server
2403 * used from the size of the reply.
2404 */
2405 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2406 size_t transferred, unsigned count,
2407 bool is_compat)
2408 {
2409 #ifdef CONFIG_COMPAT
2410 if (count * sizeof(struct compat_iovec) == transferred) {
2411 struct compat_iovec *ciov = src;
2412 unsigned i;
2413
2414 /*
2415 * With this interface a 32bit server cannot support
2416 * non-compat (i.e. ones coming from 64bit apps) ioctl
2417 * requests
2418 */
2419 if (!is_compat)
2420 return -EINVAL;
2421
2422 for (i = 0; i < count; i++) {
2423 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2424 dst[i].iov_len = ciov[i].iov_len;
2425 }
2426 return 0;
2427 }
2428 #endif
2429
2430 if (count * sizeof(struct iovec) != transferred)
2431 return -EIO;
2432
2433 memcpy(dst, src, transferred);
2434 return 0;
2435 }
2436
2437 /* Make sure iov_length() won't overflow */
2438 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2439 {
2440 size_t n;
2441 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2442
2443 for (n = 0; n < count; n++, iov++) {
2444 if (iov->iov_len > (size_t) max)
2445 return -ENOMEM;
2446 max -= iov->iov_len;
2447 }
2448 return 0;
2449 }
2450
2451 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2452 void *src, size_t transferred, unsigned count,
2453 bool is_compat)
2454 {
2455 unsigned i;
2456 struct fuse_ioctl_iovec *fiov = src;
2457
2458 if (fc->minor < 16) {
2459 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2460 count, is_compat);
2461 }
2462
2463 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2464 return -EIO;
2465
2466 for (i = 0; i < count; i++) {
2467 /* Did the server supply an inappropriate value? */
2468 if (fiov[i].base != (unsigned long) fiov[i].base ||
2469 fiov[i].len != (unsigned long) fiov[i].len)
2470 return -EIO;
2471
2472 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2473 dst[i].iov_len = (size_t) fiov[i].len;
2474
2475 #ifdef CONFIG_COMPAT
2476 if (is_compat &&
2477 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2478 (compat_size_t) dst[i].iov_len != fiov[i].len))
2479 return -EIO;
2480 #endif
2481 }
2482
2483 return 0;
2484 }
2485
2486
2487 /*
2488 * For ioctls, there is no generic way to determine how much memory
2489 * needs to be read and/or written. Furthermore, ioctls are allowed
2490 * to dereference the passed pointer, so the parameter requires deep
2491 * copying but FUSE has no idea whatsoever about what to copy in or
2492 * out.
2493 *
2494 * This is solved by allowing FUSE server to retry ioctl with
2495 * necessary in/out iovecs. Let's assume the ioctl implementation
2496 * needs to read in the following structure.
2497 *
2498 * struct a {
2499 * char *buf;
2500 * size_t buflen;
2501 * }
2502 *
2503 * On the first callout to FUSE server, inarg->in_size and
2504 * inarg->out_size will be NULL; then, the server completes the ioctl
2505 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2506 * the actual iov array to
2507 *
2508 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2509 *
2510 * which tells FUSE to copy in the requested area and retry the ioctl.
2511 * On the second round, the server has access to the structure and
2512 * from that it can tell what to look for next, so on the invocation,
2513 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2514 *
2515 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2516 * { .iov_base = a.buf, .iov_len = a.buflen } }
2517 *
2518 * FUSE will copy both struct a and the pointed buffer from the
2519 * process doing the ioctl and retry ioctl with both struct a and the
2520 * buffer.
2521 *
2522 * This time, FUSE server has everything it needs and completes ioctl
2523 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2524 *
2525 * Copying data out works the same way.
2526 *
2527 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2528 * automatically initializes in and out iovs by decoding @cmd with
2529 * _IOC_* macros and the server is not allowed to request RETRY. This
2530 * limits ioctl data transfers to well-formed ioctls and is the forced
2531 * behavior for all FUSE servers.
2532 */
2533 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2534 unsigned int flags)
2535 {
2536 struct fuse_file *ff = file->private_data;
2537 struct fuse_conn *fc = ff->fc;
2538 struct fuse_ioctl_in inarg = {
2539 .fh = ff->fh,
2540 .cmd = cmd,
2541 .arg = arg,
2542 .flags = flags
2543 };
2544 struct fuse_ioctl_out outarg;
2545 struct fuse_req *req = NULL;
2546 struct page **pages = NULL;
2547 struct iovec *iov_page = NULL;
2548 struct iovec *in_iov = NULL, *out_iov = NULL;
2549 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2550 size_t in_size, out_size, transferred;
2551 int err;
2552
2553 #if BITS_PER_LONG == 32
2554 inarg.flags |= FUSE_IOCTL_32BIT;
2555 #else
2556 if (flags & FUSE_IOCTL_COMPAT)
2557 inarg.flags |= FUSE_IOCTL_32BIT;
2558 #endif
2559
2560 /* assume all the iovs returned by client always fits in a page */
2561 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2562
2563 err = -ENOMEM;
2564 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2565 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2566 if (!pages || !iov_page)
2567 goto out;
2568
2569 /*
2570 * If restricted, initialize IO parameters as encoded in @cmd.
2571 * RETRY from server is not allowed.
2572 */
2573 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2574 struct iovec *iov = iov_page;
2575
2576 iov->iov_base = (void __user *)arg;
2577 iov->iov_len = _IOC_SIZE(cmd);
2578
2579 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2580 in_iov = iov;
2581 in_iovs = 1;
2582 }
2583
2584 if (_IOC_DIR(cmd) & _IOC_READ) {
2585 out_iov = iov;
2586 out_iovs = 1;
2587 }
2588 }
2589
2590 retry:
2591 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2592 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2593
2594 /*
2595 * Out data can be used either for actual out data or iovs,
2596 * make sure there always is at least one page.
2597 */
2598 out_size = max_t(size_t, out_size, PAGE_SIZE);
2599 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2600
2601 /* make sure there are enough buffer pages and init request with them */
2602 err = -ENOMEM;
2603 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2604 goto out;
2605 while (num_pages < max_pages) {
2606 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2607 if (!pages[num_pages])
2608 goto out;
2609 num_pages++;
2610 }
2611
2612 req = fuse_get_req(fc, num_pages);
2613 if (IS_ERR(req)) {
2614 err = PTR_ERR(req);
2615 req = NULL;
2616 goto out;
2617 }
2618 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2619 req->num_pages = num_pages;
2620 fuse_page_descs_length_init(req, 0, req->num_pages);
2621
2622 /* okay, let's send it to the client */
2623 req->in.h.opcode = FUSE_IOCTL;
2624 req->in.h.nodeid = ff->nodeid;
2625 req->in.numargs = 1;
2626 req->in.args[0].size = sizeof(inarg);
2627 req->in.args[0].value = &inarg;
2628 if (in_size) {
2629 req->in.numargs++;
2630 req->in.args[1].size = in_size;
2631 req->in.argpages = 1;
2632
2633 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2634 false);
2635 if (err)
2636 goto out;
2637 }
2638
2639 req->out.numargs = 2;
2640 req->out.args[0].size = sizeof(outarg);
2641 req->out.args[0].value = &outarg;
2642 req->out.args[1].size = out_size;
2643 req->out.argpages = 1;
2644 req->out.argvar = 1;
2645
2646 fuse_request_send(fc, req);
2647 err = req->out.h.error;
2648 transferred = req->out.args[1].size;
2649 fuse_put_request(fc, req);
2650 req = NULL;
2651 if (err)
2652 goto out;
2653
2654 /* did it ask for retry? */
2655 if (outarg.flags & FUSE_IOCTL_RETRY) {
2656 void *vaddr;
2657
2658 /* no retry if in restricted mode */
2659 err = -EIO;
2660 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2661 goto out;
2662
2663 in_iovs = outarg.in_iovs;
2664 out_iovs = outarg.out_iovs;
2665
2666 /*
2667 * Make sure things are in boundary, separate checks
2668 * are to protect against overflow.
2669 */
2670 err = -ENOMEM;
2671 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2672 out_iovs > FUSE_IOCTL_MAX_IOV ||
2673 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2674 goto out;
2675
2676 vaddr = kmap_atomic(pages[0]);
2677 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2678 transferred, in_iovs + out_iovs,
2679 (flags & FUSE_IOCTL_COMPAT) != 0);
2680 kunmap_atomic(vaddr);
2681 if (err)
2682 goto out;
2683
2684 in_iov = iov_page;
2685 out_iov = in_iov + in_iovs;
2686
2687 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2688 if (err)
2689 goto out;
2690
2691 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2692 if (err)
2693 goto out;
2694
2695 goto retry;
2696 }
2697
2698 err = -EIO;
2699 if (transferred > inarg.out_size)
2700 goto out;
2701
2702 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2703 out:
2704 if (req)
2705 fuse_put_request(fc, req);
2706 free_page((unsigned long) iov_page);
2707 while (num_pages)
2708 __free_page(pages[--num_pages]);
2709 kfree(pages);
2710
2711 return err ? err : outarg.result;
2712 }
2713 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2714
2715 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2716 unsigned long arg, unsigned int flags)
2717 {
2718 struct inode *inode = file_inode(file);
2719 struct fuse_conn *fc = get_fuse_conn(inode);
2720
2721 if (!fuse_allow_current_process(fc))
2722 return -EACCES;
2723
2724 if (is_bad_inode(inode))
2725 return -EIO;
2726
2727 return fuse_do_ioctl(file, cmd, arg, flags);
2728 }
2729
2730 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2731 unsigned long arg)
2732 {
2733 return fuse_ioctl_common(file, cmd, arg, 0);
2734 }
2735
2736 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2737 unsigned long arg)
2738 {
2739 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2740 }
2741
2742 /*
2743 * All files which have been polled are linked to RB tree
2744 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2745 * find the matching one.
2746 */
2747 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2748 struct rb_node **parent_out)
2749 {
2750 struct rb_node **link = &fc->polled_files.rb_node;
2751 struct rb_node *last = NULL;
2752
2753 while (*link) {
2754 struct fuse_file *ff;
2755
2756 last = *link;
2757 ff = rb_entry(last, struct fuse_file, polled_node);
2758
2759 if (kh < ff->kh)
2760 link = &last->rb_left;
2761 else if (kh > ff->kh)
2762 link = &last->rb_right;
2763 else
2764 return link;
2765 }
2766
2767 if (parent_out)
2768 *parent_out = last;
2769 return link;
2770 }
2771
2772 /*
2773 * The file is about to be polled. Make sure it's on the polled_files
2774 * RB tree. Note that files once added to the polled_files tree are
2775 * not removed before the file is released. This is because a file
2776 * polled once is likely to be polled again.
2777 */
2778 static void fuse_register_polled_file(struct fuse_conn *fc,
2779 struct fuse_file *ff)
2780 {
2781 spin_lock(&fc->lock);
2782 if (RB_EMPTY_NODE(&ff->polled_node)) {
2783 struct rb_node **link, *uninitialized_var(parent);
2784
2785 link = fuse_find_polled_node(fc, ff->kh, &parent);
2786 BUG_ON(*link);
2787 rb_link_node(&ff->polled_node, parent, link);
2788 rb_insert_color(&ff->polled_node, &fc->polled_files);
2789 }
2790 spin_unlock(&fc->lock);
2791 }
2792
2793 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2794 {
2795 struct fuse_file *ff = file->private_data;
2796 struct fuse_conn *fc = ff->fc;
2797 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2798 struct fuse_poll_out outarg;
2799 struct fuse_req *req;
2800 int err;
2801
2802 if (fc->no_poll)
2803 return DEFAULT_POLLMASK;
2804
2805 poll_wait(file, &ff->poll_wait, wait);
2806 inarg.events = (__u32)poll_requested_events(wait);
2807
2808 /*
2809 * Ask for notification iff there's someone waiting for it.
2810 * The client may ignore the flag and always notify.
2811 */
2812 if (waitqueue_active(&ff->poll_wait)) {
2813 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2814 fuse_register_polled_file(fc, ff);
2815 }
2816
2817 req = fuse_get_req_nopages(fc);
2818 if (IS_ERR(req))
2819 return POLLERR;
2820
2821 req->in.h.opcode = FUSE_POLL;
2822 req->in.h.nodeid = ff->nodeid;
2823 req->in.numargs = 1;
2824 req->in.args[0].size = sizeof(inarg);
2825 req->in.args[0].value = &inarg;
2826 req->out.numargs = 1;
2827 req->out.args[0].size = sizeof(outarg);
2828 req->out.args[0].value = &outarg;
2829 fuse_request_send(fc, req);
2830 err = req->out.h.error;
2831 fuse_put_request(fc, req);
2832
2833 if (!err)
2834 return outarg.revents;
2835 if (err == -ENOSYS) {
2836 fc->no_poll = 1;
2837 return DEFAULT_POLLMASK;
2838 }
2839 return POLLERR;
2840 }
2841 EXPORT_SYMBOL_GPL(fuse_file_poll);
2842
2843 /*
2844 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2845 * wakes up the poll waiters.
2846 */
2847 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2848 struct fuse_notify_poll_wakeup_out *outarg)
2849 {
2850 u64 kh = outarg->kh;
2851 struct rb_node **link;
2852
2853 spin_lock(&fc->lock);
2854
2855 link = fuse_find_polled_node(fc, kh, NULL);
2856 if (*link) {
2857 struct fuse_file *ff;
2858
2859 ff = rb_entry(*link, struct fuse_file, polled_node);
2860 wake_up_interruptible_sync(&ff->poll_wait);
2861 }
2862
2863 spin_unlock(&fc->lock);
2864 return 0;
2865 }
2866
2867 static void fuse_do_truncate(struct file *file)
2868 {
2869 struct inode *inode = file->f_mapping->host;
2870 struct iattr attr;
2871
2872 attr.ia_valid = ATTR_SIZE;
2873 attr.ia_size = i_size_read(inode);
2874
2875 attr.ia_file = file;
2876 attr.ia_valid |= ATTR_FILE;
2877
2878 fuse_do_setattr(inode, &attr, file);
2879 }
2880
2881 static inline loff_t fuse_round_up(loff_t off)
2882 {
2883 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2884 }
2885
2886 static ssize_t
2887 fuse_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
2888 loff_t offset, unsigned long nr_segs)
2889 {
2890 ssize_t ret = 0;
2891 struct file *file = iocb->ki_filp;
2892 struct fuse_file *ff = file->private_data;
2893 bool async_dio = ff->fc->async_dio;
2894 loff_t pos = 0;
2895 struct inode *inode;
2896 loff_t i_size;
2897 size_t count = iov_length(iov, nr_segs);
2898 struct fuse_io_priv *io;
2899
2900 pos = offset;
2901 inode = file->f_mapping->host;
2902 i_size = i_size_read(inode);
2903
2904 if ((rw == READ) && (offset > i_size))
2905 return 0;
2906
2907 /* optimization for short read */
2908 if (async_dio && rw != WRITE && offset + count > i_size) {
2909 if (offset >= i_size)
2910 return 0;
2911 count = min_t(loff_t, count, fuse_round_up(i_size - offset));
2912 }
2913
2914 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2915 if (!io)
2916 return -ENOMEM;
2917 spin_lock_init(&io->lock);
2918 io->reqs = 1;
2919 io->bytes = -1;
2920 io->size = 0;
2921 io->offset = offset;
2922 io->write = (rw == WRITE);
2923 io->err = 0;
2924 io->file = file;
2925 /*
2926 * By default, we want to optimize all I/Os with async request
2927 * submission to the client filesystem if supported.
2928 */
2929 io->async = async_dio;
2930 io->iocb = iocb;
2931
2932 /*
2933 * We cannot asynchronously extend the size of a file. We have no method
2934 * to wait on real async I/O requests, so we must submit this request
2935 * synchronously.
2936 */
2937 if (!is_sync_kiocb(iocb) && (offset + count > i_size) && rw == WRITE)
2938 io->async = false;
2939
2940 if (rw == WRITE)
2941 ret = __fuse_direct_write(io, iov, nr_segs, &pos);
2942 else
2943 ret = __fuse_direct_read(io, iov, nr_segs, &pos, count);
2944
2945 if (io->async) {
2946 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2947
2948 /* we have a non-extending, async request, so return */
2949 if (!is_sync_kiocb(iocb))
2950 return -EIOCBQUEUED;
2951
2952 ret = wait_on_sync_kiocb(iocb);
2953 } else {
2954 kfree(io);
2955 }
2956
2957 if (rw == WRITE) {
2958 if (ret > 0)
2959 fuse_write_update_size(inode, pos);
2960 else if (ret < 0 && offset + count > i_size)
2961 fuse_do_truncate(file);
2962 }
2963
2964 return ret;
2965 }
2966
2967 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2968 loff_t length)
2969 {
2970 struct fuse_file *ff = file->private_data;
2971 struct inode *inode = file->f_inode;
2972 struct fuse_inode *fi = get_fuse_inode(inode);
2973 struct fuse_conn *fc = ff->fc;
2974 struct fuse_req *req;
2975 struct fuse_fallocate_in inarg = {
2976 .fh = ff->fh,
2977 .offset = offset,
2978 .length = length,
2979 .mode = mode
2980 };
2981 int err;
2982 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2983 (mode & FALLOC_FL_PUNCH_HOLE);
2984
2985 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2986 return -EOPNOTSUPP;
2987
2988 if (fc->no_fallocate)
2989 return -EOPNOTSUPP;
2990
2991 if (lock_inode) {
2992 mutex_lock(&inode->i_mutex);
2993 if (mode & FALLOC_FL_PUNCH_HOLE) {
2994 loff_t endbyte = offset + length - 1;
2995 err = filemap_write_and_wait_range(inode->i_mapping,
2996 offset, endbyte);
2997 if (err)
2998 goto out;
2999
3000 fuse_sync_writes(inode);
3001 }
3002 }
3003
3004 if (!(mode & FALLOC_FL_KEEP_SIZE))
3005 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3006
3007 req = fuse_get_req_nopages(fc);
3008 if (IS_ERR(req)) {
3009 err = PTR_ERR(req);
3010 goto out;
3011 }
3012
3013 req->in.h.opcode = FUSE_FALLOCATE;
3014 req->in.h.nodeid = ff->nodeid;
3015 req->in.numargs = 1;
3016 req->in.args[0].size = sizeof(inarg);
3017 req->in.args[0].value = &inarg;
3018 fuse_request_send(fc, req);
3019 err = req->out.h.error;
3020 if (err == -ENOSYS) {
3021 fc->no_fallocate = 1;
3022 err = -EOPNOTSUPP;
3023 }
3024 fuse_put_request(fc, req);
3025
3026 if (err)
3027 goto out;
3028
3029 /* we could have extended the file */
3030 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3031 bool changed = fuse_write_update_size(inode, offset + length);
3032
3033 if (changed && fc->writeback_cache) {
3034 struct fuse_inode *fi = get_fuse_inode(inode);
3035
3036 inode->i_mtime = current_fs_time(inode->i_sb);
3037 set_bit(FUSE_I_MTIME_DIRTY, &fi->state);
3038 }
3039 }
3040
3041 if (mode & FALLOC_FL_PUNCH_HOLE)
3042 truncate_pagecache_range(inode, offset, offset + length - 1);
3043
3044 fuse_invalidate_attr(inode);
3045
3046 out:
3047 if (!(mode & FALLOC_FL_KEEP_SIZE))
3048 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3049
3050 if (lock_inode)
3051 mutex_unlock(&inode->i_mutex);
3052
3053 return err;
3054 }
3055
3056 static const struct file_operations fuse_file_operations = {
3057 .llseek = fuse_file_llseek,
3058 .read = do_sync_read,
3059 .aio_read = fuse_file_aio_read,
3060 .write = do_sync_write,
3061 .aio_write = fuse_file_aio_write,
3062 .mmap = fuse_file_mmap,
3063 .open = fuse_open,
3064 .flush = fuse_flush,
3065 .release = fuse_release,
3066 .fsync = fuse_fsync,
3067 .lock = fuse_file_lock,
3068 .flock = fuse_file_flock,
3069 .splice_read = generic_file_splice_read,
3070 .unlocked_ioctl = fuse_file_ioctl,
3071 .compat_ioctl = fuse_file_compat_ioctl,
3072 .poll = fuse_file_poll,
3073 .fallocate = fuse_file_fallocate,
3074 };
3075
3076 static const struct file_operations fuse_direct_io_file_operations = {
3077 .llseek = fuse_file_llseek,
3078 .read = fuse_direct_read,
3079 .write = fuse_direct_write,
3080 .mmap = fuse_direct_mmap,
3081 .open = fuse_open,
3082 .flush = fuse_flush,
3083 .release = fuse_release,
3084 .fsync = fuse_fsync,
3085 .lock = fuse_file_lock,
3086 .flock = fuse_file_flock,
3087 .unlocked_ioctl = fuse_file_ioctl,
3088 .compat_ioctl = fuse_file_compat_ioctl,
3089 .poll = fuse_file_poll,
3090 .fallocate = fuse_file_fallocate,
3091 /* no splice_read */
3092 };
3093
3094 static const struct address_space_operations fuse_file_aops = {
3095 .readpage = fuse_readpage,
3096 .writepage = fuse_writepage,
3097 .writepages = fuse_writepages,
3098 .launder_page = fuse_launder_page,
3099 .readpages = fuse_readpages,
3100 .set_page_dirty = __set_page_dirty_nobuffers,
3101 .bmap = fuse_bmap,
3102 .direct_IO = fuse_direct_IO,
3103 .write_begin = fuse_write_begin,
3104 .write_end = fuse_write_end,
3105 };
3106
3107 void fuse_init_file_inode(struct inode *inode)
3108 {
3109 inode->i_fop = &fuse_file_operations;
3110 inode->i_data.a_ops = &fuse_file_aops;
3111 }