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