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