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