fuse: don't let fuse_req->end() put the base reference
[linux-2.6] / 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
16 static const struct file_operations fuse_direct_io_file_operations;
17
18 static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
19                           struct fuse_open_out *outargp)
20 {
21         struct fuse_conn *fc = get_fuse_conn(inode);
22         struct fuse_open_in inarg;
23         struct fuse_req *req;
24         int err;
25
26         req = fuse_get_req(fc);
27         if (IS_ERR(req))
28                 return PTR_ERR(req);
29
30         memset(&inarg, 0, sizeof(inarg));
31         inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
32         if (!fc->atomic_o_trunc)
33                 inarg.flags &= ~O_TRUNC;
34         req->in.h.opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
35         req->in.h.nodeid = get_node_id(inode);
36         req->in.numargs = 1;
37         req->in.args[0].size = sizeof(inarg);
38         req->in.args[0].value = &inarg;
39         req->out.numargs = 1;
40         req->out.args[0].size = sizeof(*outargp);
41         req->out.args[0].value = outargp;
42         request_send(fc, req);
43         err = req->out.h.error;
44         fuse_put_request(fc, req);
45
46         return err;
47 }
48
49 struct fuse_file *fuse_file_alloc(void)
50 {
51         struct fuse_file *ff;
52         ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
53         if (ff) {
54                 ff->reserved_req = fuse_request_alloc();
55                 if (!ff->reserved_req) {
56                         kfree(ff);
57                         ff = NULL;
58                 } else {
59                         INIT_LIST_HEAD(&ff->write_entry);
60                         atomic_set(&ff->count, 0);
61                 }
62         }
63         return ff;
64 }
65
66 void fuse_file_free(struct fuse_file *ff)
67 {
68         fuse_request_free(ff->reserved_req);
69         kfree(ff);
70 }
71
72 static struct fuse_file *fuse_file_get(struct fuse_file *ff)
73 {
74         atomic_inc(&ff->count);
75         return ff;
76 }
77
78 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
79 {
80         dput(req->misc.release.dentry);
81         mntput(req->misc.release.vfsmount);
82 }
83
84 static void fuse_file_put(struct fuse_file *ff)
85 {
86         if (atomic_dec_and_test(&ff->count)) {
87                 struct fuse_req *req = ff->reserved_req;
88                 struct inode *inode = req->misc.release.dentry->d_inode;
89                 struct fuse_conn *fc = get_fuse_conn(inode);
90                 req->end = fuse_release_end;
91                 request_send_background(fc, req);
92                 kfree(ff);
93         }
94 }
95
96 void fuse_finish_open(struct inode *inode, struct file *file,
97                       struct fuse_file *ff, struct fuse_open_out *outarg)
98 {
99         if (outarg->open_flags & FOPEN_DIRECT_IO)
100                 file->f_op = &fuse_direct_io_file_operations;
101         if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
102                 invalidate_inode_pages2(inode->i_mapping);
103         if (outarg->open_flags & FOPEN_NONSEEKABLE)
104                 nonseekable_open(inode, file);
105         ff->fh = outarg->fh;
106         file->private_data = fuse_file_get(ff);
107 }
108
109 int fuse_open_common(struct inode *inode, struct file *file, int isdir)
110 {
111         struct fuse_open_out outarg;
112         struct fuse_file *ff;
113         int err;
114
115         /* VFS checks this, but only _after_ ->open() */
116         if (file->f_flags & O_DIRECT)
117                 return -EINVAL;
118
119         err = generic_file_open(inode, file);
120         if (err)
121                 return err;
122
123         ff = fuse_file_alloc();
124         if (!ff)
125                 return -ENOMEM;
126
127         err = fuse_send_open(inode, file, isdir, &outarg);
128         if (err)
129                 fuse_file_free(ff);
130         else {
131                 if (isdir)
132                         outarg.open_flags &= ~FOPEN_DIRECT_IO;
133                 fuse_finish_open(inode, file, ff, &outarg);
134         }
135
136         return err;
137 }
138
139 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
140 {
141         struct fuse_req *req = ff->reserved_req;
142         struct fuse_release_in *inarg = &req->misc.release.in;
143
144         inarg->fh = ff->fh;
145         inarg->flags = flags;
146         req->in.h.opcode = opcode;
147         req->in.h.nodeid = nodeid;
148         req->in.numargs = 1;
149         req->in.args[0].size = sizeof(struct fuse_release_in);
150         req->in.args[0].value = inarg;
151 }
152
153 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
154 {
155         struct fuse_file *ff = file->private_data;
156         if (ff) {
157                 struct fuse_conn *fc = get_fuse_conn(inode);
158                 struct fuse_req *req = ff->reserved_req;
159
160                 fuse_release_fill(ff, get_node_id(inode), file->f_flags,
161                                   isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
162
163                 /* Hold vfsmount and dentry until release is finished */
164                 req->misc.release.vfsmount = mntget(file->f_path.mnt);
165                 req->misc.release.dentry = dget(file->f_path.dentry);
166
167                 spin_lock(&fc->lock);
168                 list_del(&ff->write_entry);
169                 spin_unlock(&fc->lock);
170                 /*
171                  * Normally this will send the RELEASE request,
172                  * however if some asynchronous READ or WRITE requests
173                  * are outstanding, the sending will be delayed
174                  */
175                 fuse_file_put(ff);
176         }
177
178         /* Return value is ignored by VFS */
179         return 0;
180 }
181
182 static int fuse_open(struct inode *inode, struct file *file)
183 {
184         return fuse_open_common(inode, file, 0);
185 }
186
187 static int fuse_release(struct inode *inode, struct file *file)
188 {
189         return fuse_release_common(inode, file, 0);
190 }
191
192 /*
193  * Scramble the ID space with XTEA, so that the value of the files_struct
194  * pointer is not exposed to userspace.
195  */
196 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
197 {
198         u32 *k = fc->scramble_key;
199         u64 v = (unsigned long) id;
200         u32 v0 = v;
201         u32 v1 = v >> 32;
202         u32 sum = 0;
203         int i;
204
205         for (i = 0; i < 32; i++) {
206                 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
207                 sum += 0x9E3779B9;
208                 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
209         }
210
211         return (u64) v0 + ((u64) v1 << 32);
212 }
213
214 /*
215  * Check if page is under writeback
216  *
217  * This is currently done by walking the list of writepage requests
218  * for the inode, which can be pretty inefficient.
219  */
220 static bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
221 {
222         struct fuse_conn *fc = get_fuse_conn(inode);
223         struct fuse_inode *fi = get_fuse_inode(inode);
224         struct fuse_req *req;
225         bool found = false;
226
227         spin_lock(&fc->lock);
228         list_for_each_entry(req, &fi->writepages, writepages_entry) {
229                 pgoff_t curr_index;
230
231                 BUG_ON(req->inode != inode);
232                 curr_index = req->misc.write.in.offset >> PAGE_CACHE_SHIFT;
233                 if (curr_index == index) {
234                         found = true;
235                         break;
236                 }
237         }
238         spin_unlock(&fc->lock);
239
240         return found;
241 }
242
243 /*
244  * Wait for page writeback to be completed.
245  *
246  * Since fuse doesn't rely on the VM writeback tracking, this has to
247  * use some other means.
248  */
249 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
250 {
251         struct fuse_inode *fi = get_fuse_inode(inode);
252
253         wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
254         return 0;
255 }
256
257 static int fuse_flush(struct file *file, fl_owner_t id)
258 {
259         struct inode *inode = file->f_path.dentry->d_inode;
260         struct fuse_conn *fc = get_fuse_conn(inode);
261         struct fuse_file *ff = file->private_data;
262         struct fuse_req *req;
263         struct fuse_flush_in inarg;
264         int err;
265
266         if (is_bad_inode(inode))
267                 return -EIO;
268
269         if (fc->no_flush)
270                 return 0;
271
272         req = fuse_get_req_nofail(fc, file);
273         memset(&inarg, 0, sizeof(inarg));
274         inarg.fh = ff->fh;
275         inarg.lock_owner = fuse_lock_owner_id(fc, id);
276         req->in.h.opcode = FUSE_FLUSH;
277         req->in.h.nodeid = get_node_id(inode);
278         req->in.numargs = 1;
279         req->in.args[0].size = sizeof(inarg);
280         req->in.args[0].value = &inarg;
281         req->force = 1;
282         request_send(fc, req);
283         err = req->out.h.error;
284         fuse_put_request(fc, req);
285         if (err == -ENOSYS) {
286                 fc->no_flush = 1;
287                 err = 0;
288         }
289         return err;
290 }
291
292 /*
293  * Wait for all pending writepages on the inode to finish.
294  *
295  * This is currently done by blocking further writes with FUSE_NOWRITE
296  * and waiting for all sent writes to complete.
297  *
298  * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
299  * could conflict with truncation.
300  */
301 static void fuse_sync_writes(struct inode *inode)
302 {
303         fuse_set_nowrite(inode);
304         fuse_release_nowrite(inode);
305 }
306
307 int fuse_fsync_common(struct file *file, struct dentry *de, int datasync,
308                       int isdir)
309 {
310         struct inode *inode = de->d_inode;
311         struct fuse_conn *fc = get_fuse_conn(inode);
312         struct fuse_file *ff = file->private_data;
313         struct fuse_req *req;
314         struct fuse_fsync_in inarg;
315         int err;
316
317         if (is_bad_inode(inode))
318                 return -EIO;
319
320         if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
321                 return 0;
322
323         /*
324          * Start writeback against all dirty pages of the inode, then
325          * wait for all outstanding writes, before sending the FSYNC
326          * request.
327          */
328         err = write_inode_now(inode, 0);
329         if (err)
330                 return err;
331
332         fuse_sync_writes(inode);
333
334         req = fuse_get_req(fc);
335         if (IS_ERR(req))
336                 return PTR_ERR(req);
337
338         memset(&inarg, 0, sizeof(inarg));
339         inarg.fh = ff->fh;
340         inarg.fsync_flags = datasync ? 1 : 0;
341         req->in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
342         req->in.h.nodeid = get_node_id(inode);
343         req->in.numargs = 1;
344         req->in.args[0].size = sizeof(inarg);
345         req->in.args[0].value = &inarg;
346         request_send(fc, req);
347         err = req->out.h.error;
348         fuse_put_request(fc, req);
349         if (err == -ENOSYS) {
350                 if (isdir)
351                         fc->no_fsyncdir = 1;
352                 else
353                         fc->no_fsync = 1;
354                 err = 0;
355         }
356         return err;
357 }
358
359 static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
360 {
361         return fuse_fsync_common(file, de, datasync, 0);
362 }
363
364 void fuse_read_fill(struct fuse_req *req, struct file *file,
365                     struct inode *inode, loff_t pos, size_t count, int opcode)
366 {
367         struct fuse_read_in *inarg = &req->misc.read.in;
368         struct fuse_file *ff = file->private_data;
369
370         inarg->fh = ff->fh;
371         inarg->offset = pos;
372         inarg->size = count;
373         inarg->flags = file->f_flags;
374         req->in.h.opcode = opcode;
375         req->in.h.nodeid = get_node_id(inode);
376         req->in.numargs = 1;
377         req->in.args[0].size = sizeof(struct fuse_read_in);
378         req->in.args[0].value = inarg;
379         req->out.argpages = 1;
380         req->out.argvar = 1;
381         req->out.numargs = 1;
382         req->out.args[0].size = count;
383 }
384
385 static size_t fuse_send_read(struct fuse_req *req, struct file *file,
386                              struct inode *inode, loff_t pos, size_t count,
387                              fl_owner_t owner)
388 {
389         struct fuse_conn *fc = get_fuse_conn(inode);
390
391         fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
392         if (owner != NULL) {
393                 struct fuse_read_in *inarg = &req->misc.read.in;
394
395                 inarg->read_flags |= FUSE_READ_LOCKOWNER;
396                 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
397         }
398         request_send(fc, req);
399         return req->out.args[0].size;
400 }
401
402 static void fuse_read_update_size(struct inode *inode, loff_t size,
403                                   u64 attr_ver)
404 {
405         struct fuse_conn *fc = get_fuse_conn(inode);
406         struct fuse_inode *fi = get_fuse_inode(inode);
407
408         spin_lock(&fc->lock);
409         if (attr_ver == fi->attr_version && size < inode->i_size) {
410                 fi->attr_version = ++fc->attr_version;
411                 i_size_write(inode, size);
412         }
413         spin_unlock(&fc->lock);
414 }
415
416 static int fuse_readpage(struct file *file, struct page *page)
417 {
418         struct inode *inode = page->mapping->host;
419         struct fuse_conn *fc = get_fuse_conn(inode);
420         struct fuse_req *req;
421         size_t num_read;
422         loff_t pos = page_offset(page);
423         size_t count = PAGE_CACHE_SIZE;
424         u64 attr_ver;
425         int err;
426
427         err = -EIO;
428         if (is_bad_inode(inode))
429                 goto out;
430
431         /*
432          * Page writeback can extend beyond the liftime of the
433          * page-cache page, so make sure we read a properly synced
434          * page.
435          */
436         fuse_wait_on_page_writeback(inode, page->index);
437
438         req = fuse_get_req(fc);
439         err = PTR_ERR(req);
440         if (IS_ERR(req))
441                 goto out;
442
443         attr_ver = fuse_get_attr_version(fc);
444
445         req->out.page_zeroing = 1;
446         req->num_pages = 1;
447         req->pages[0] = page;
448         num_read = fuse_send_read(req, file, inode, pos, count, NULL);
449         err = req->out.h.error;
450         fuse_put_request(fc, req);
451
452         if (!err) {
453                 /*
454                  * Short read means EOF.  If file size is larger, truncate it
455                  */
456                 if (num_read < count)
457                         fuse_read_update_size(inode, pos + num_read, attr_ver);
458
459                 SetPageUptodate(page);
460         }
461
462         fuse_invalidate_attr(inode); /* atime changed */
463  out:
464         unlock_page(page);
465         return err;
466 }
467
468 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
469 {
470         int i;
471         size_t count = req->misc.read.in.size;
472         size_t num_read = req->out.args[0].size;
473         struct inode *inode = req->pages[0]->mapping->host;
474
475         /*
476          * Short read means EOF.  If file size is larger, truncate it
477          */
478         if (!req->out.h.error && num_read < count) {
479                 loff_t pos = page_offset(req->pages[0]) + num_read;
480                 fuse_read_update_size(inode, pos, req->misc.read.attr_ver);
481         }
482
483         fuse_invalidate_attr(inode); /* atime changed */
484
485         for (i = 0; i < req->num_pages; i++) {
486                 struct page *page = req->pages[i];
487                 if (!req->out.h.error)
488                         SetPageUptodate(page);
489                 else
490                         SetPageError(page);
491                 unlock_page(page);
492         }
493         if (req->ff)
494                 fuse_file_put(req->ff);
495 }
496
497 static void fuse_send_readpages(struct fuse_req *req, struct file *file,
498                                 struct inode *inode)
499 {
500         struct fuse_conn *fc = get_fuse_conn(inode);
501         loff_t pos = page_offset(req->pages[0]);
502         size_t count = req->num_pages << PAGE_CACHE_SHIFT;
503         req->out.page_zeroing = 1;
504         fuse_read_fill(req, file, inode, pos, count, FUSE_READ);
505         req->misc.read.attr_ver = fuse_get_attr_version(fc);
506         if (fc->async_read) {
507                 struct fuse_file *ff = file->private_data;
508                 req->ff = fuse_file_get(ff);
509                 req->end = fuse_readpages_end;
510                 request_send_background(fc, req);
511         } else {
512                 request_send(fc, req);
513                 fuse_readpages_end(fc, req);
514                 fuse_put_request(fc, req);
515         }
516 }
517
518 struct fuse_fill_data {
519         struct fuse_req *req;
520         struct file *file;
521         struct inode *inode;
522 };
523
524 static int fuse_readpages_fill(void *_data, struct page *page)
525 {
526         struct fuse_fill_data *data = _data;
527         struct fuse_req *req = data->req;
528         struct inode *inode = data->inode;
529         struct fuse_conn *fc = get_fuse_conn(inode);
530
531         fuse_wait_on_page_writeback(inode, page->index);
532
533         if (req->num_pages &&
534             (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
535              (req->num_pages + 1) * PAGE_CACHE_SIZE > fc->max_read ||
536              req->pages[req->num_pages - 1]->index + 1 != page->index)) {
537                 fuse_send_readpages(req, data->file, inode);
538                 data->req = req = fuse_get_req(fc);
539                 if (IS_ERR(req)) {
540                         unlock_page(page);
541                         return PTR_ERR(req);
542                 }
543         }
544         req->pages[req->num_pages] = page;
545         req->num_pages++;
546         return 0;
547 }
548
549 static int fuse_readpages(struct file *file, struct address_space *mapping,
550                           struct list_head *pages, unsigned nr_pages)
551 {
552         struct inode *inode = mapping->host;
553         struct fuse_conn *fc = get_fuse_conn(inode);
554         struct fuse_fill_data data;
555         int err;
556
557         err = -EIO;
558         if (is_bad_inode(inode))
559                 goto out;
560
561         data.file = file;
562         data.inode = inode;
563         data.req = fuse_get_req(fc);
564         err = PTR_ERR(data.req);
565         if (IS_ERR(data.req))
566                 goto out;
567
568         err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
569         if (!err) {
570                 if (data.req->num_pages)
571                         fuse_send_readpages(data.req, file, inode);
572                 else
573                         fuse_put_request(fc, data.req);
574         }
575 out:
576         return err;
577 }
578
579 static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
580                                   unsigned long nr_segs, loff_t pos)
581 {
582         struct inode *inode = iocb->ki_filp->f_mapping->host;
583
584         if (pos + iov_length(iov, nr_segs) > i_size_read(inode)) {
585                 int err;
586                 /*
587                  * If trying to read past EOF, make sure the i_size
588                  * attribute is up-to-date.
589                  */
590                 err = fuse_update_attributes(inode, NULL, iocb->ki_filp, NULL);
591                 if (err)
592                         return err;
593         }
594
595         return generic_file_aio_read(iocb, iov, nr_segs, pos);
596 }
597
598 static void fuse_write_fill(struct fuse_req *req, struct file *file,
599                             struct fuse_file *ff, struct inode *inode,
600                             loff_t pos, size_t count, int writepage)
601 {
602         struct fuse_conn *fc = get_fuse_conn(inode);
603         struct fuse_write_in *inarg = &req->misc.write.in;
604         struct fuse_write_out *outarg = &req->misc.write.out;
605
606         memset(inarg, 0, sizeof(struct fuse_write_in));
607         inarg->fh = ff->fh;
608         inarg->offset = pos;
609         inarg->size = count;
610         inarg->write_flags = writepage ? FUSE_WRITE_CACHE : 0;
611         inarg->flags = file ? file->f_flags : 0;
612         req->in.h.opcode = FUSE_WRITE;
613         req->in.h.nodeid = get_node_id(inode);
614         req->in.argpages = 1;
615         req->in.numargs = 2;
616         if (fc->minor < 9)
617                 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
618         else
619                 req->in.args[0].size = sizeof(struct fuse_write_in);
620         req->in.args[0].value = inarg;
621         req->in.args[1].size = count;
622         req->out.numargs = 1;
623         req->out.args[0].size = sizeof(struct fuse_write_out);
624         req->out.args[0].value = outarg;
625 }
626
627 static size_t fuse_send_write(struct fuse_req *req, struct file *file,
628                               struct inode *inode, loff_t pos, size_t count,
629                               fl_owner_t owner)
630 {
631         struct fuse_conn *fc = get_fuse_conn(inode);
632         fuse_write_fill(req, file, file->private_data, inode, pos, count, 0);
633         if (owner != NULL) {
634                 struct fuse_write_in *inarg = &req->misc.write.in;
635                 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
636                 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
637         }
638         request_send(fc, req);
639         return req->misc.write.out.size;
640 }
641
642 static int fuse_write_begin(struct file *file, struct address_space *mapping,
643                         loff_t pos, unsigned len, unsigned flags,
644                         struct page **pagep, void **fsdata)
645 {
646         pgoff_t index = pos >> PAGE_CACHE_SHIFT;
647
648         *pagep = __grab_cache_page(mapping, index);
649         if (!*pagep)
650                 return -ENOMEM;
651         return 0;
652 }
653
654 static void fuse_write_update_size(struct inode *inode, loff_t pos)
655 {
656         struct fuse_conn *fc = get_fuse_conn(inode);
657         struct fuse_inode *fi = get_fuse_inode(inode);
658
659         spin_lock(&fc->lock);
660         fi->attr_version = ++fc->attr_version;
661         if (pos > inode->i_size)
662                 i_size_write(inode, pos);
663         spin_unlock(&fc->lock);
664 }
665
666 static int fuse_buffered_write(struct file *file, struct inode *inode,
667                                loff_t pos, unsigned count, struct page *page)
668 {
669         int err;
670         size_t nres;
671         struct fuse_conn *fc = get_fuse_conn(inode);
672         unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
673         struct fuse_req *req;
674
675         if (is_bad_inode(inode))
676                 return -EIO;
677
678         /*
679          * Make sure writepages on the same page are not mixed up with
680          * plain writes.
681          */
682         fuse_wait_on_page_writeback(inode, page->index);
683
684         req = fuse_get_req(fc);
685         if (IS_ERR(req))
686                 return PTR_ERR(req);
687
688         req->num_pages = 1;
689         req->pages[0] = page;
690         req->page_offset = offset;
691         nres = fuse_send_write(req, file, inode, pos, count, NULL);
692         err = req->out.h.error;
693         fuse_put_request(fc, req);
694         if (!err && !nres)
695                 err = -EIO;
696         if (!err) {
697                 pos += nres;
698                 fuse_write_update_size(inode, pos);
699                 if (count == PAGE_CACHE_SIZE)
700                         SetPageUptodate(page);
701         }
702         fuse_invalidate_attr(inode);
703         return err ? err : nres;
704 }
705
706 static int fuse_write_end(struct file *file, struct address_space *mapping,
707                         loff_t pos, unsigned len, unsigned copied,
708                         struct page *page, void *fsdata)
709 {
710         struct inode *inode = mapping->host;
711         int res = 0;
712
713         if (copied)
714                 res = fuse_buffered_write(file, inode, pos, copied, page);
715
716         unlock_page(page);
717         page_cache_release(page);
718         return res;
719 }
720
721 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
722                                     struct inode *inode, loff_t pos,
723                                     size_t count)
724 {
725         size_t res;
726         unsigned offset;
727         unsigned i;
728
729         for (i = 0; i < req->num_pages; i++)
730                 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
731
732         res = fuse_send_write(req, file, inode, pos, count, NULL);
733
734         offset = req->page_offset;
735         count = res;
736         for (i = 0; i < req->num_pages; i++) {
737                 struct page *page = req->pages[i];
738
739                 if (!req->out.h.error && !offset && count >= PAGE_CACHE_SIZE)
740                         SetPageUptodate(page);
741
742                 if (count > PAGE_CACHE_SIZE - offset)
743                         count -= PAGE_CACHE_SIZE - offset;
744                 else
745                         count = 0;
746                 offset = 0;
747
748                 unlock_page(page);
749                 page_cache_release(page);
750         }
751
752         return res;
753 }
754
755 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
756                                struct address_space *mapping,
757                                struct iov_iter *ii, loff_t pos)
758 {
759         struct fuse_conn *fc = get_fuse_conn(mapping->host);
760         unsigned offset = pos & (PAGE_CACHE_SIZE - 1);
761         size_t count = 0;
762         int err;
763
764         req->page_offset = offset;
765
766         do {
767                 size_t tmp;
768                 struct page *page;
769                 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
770                 size_t bytes = min_t(size_t, PAGE_CACHE_SIZE - offset,
771                                      iov_iter_count(ii));
772
773                 bytes = min_t(size_t, bytes, fc->max_write - count);
774
775  again:
776                 err = -EFAULT;
777                 if (iov_iter_fault_in_readable(ii, bytes))
778                         break;
779
780                 err = -ENOMEM;
781                 page = __grab_cache_page(mapping, index);
782                 if (!page)
783                         break;
784
785                 pagefault_disable();
786                 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
787                 pagefault_enable();
788                 flush_dcache_page(page);
789
790                 if (!tmp) {
791                         unlock_page(page);
792                         page_cache_release(page);
793                         bytes = min(bytes, iov_iter_single_seg_count(ii));
794                         goto again;
795                 }
796
797                 err = 0;
798                 req->pages[req->num_pages] = page;
799                 req->num_pages++;
800
801                 iov_iter_advance(ii, tmp);
802                 count += tmp;
803                 pos += tmp;
804                 offset += tmp;
805                 if (offset == PAGE_CACHE_SIZE)
806                         offset = 0;
807
808                 if (!fc->big_writes)
809                         break;
810         } while (iov_iter_count(ii) && count < fc->max_write &&
811                  req->num_pages < FUSE_MAX_PAGES_PER_REQ && offset == 0);
812
813         return count > 0 ? count : err;
814 }
815
816 static ssize_t fuse_perform_write(struct file *file,
817                                   struct address_space *mapping,
818                                   struct iov_iter *ii, loff_t pos)
819 {
820         struct inode *inode = mapping->host;
821         struct fuse_conn *fc = get_fuse_conn(inode);
822         int err = 0;
823         ssize_t res = 0;
824
825         if (is_bad_inode(inode))
826                 return -EIO;
827
828         do {
829                 struct fuse_req *req;
830                 ssize_t count;
831
832                 req = fuse_get_req(fc);
833                 if (IS_ERR(req)) {
834                         err = PTR_ERR(req);
835                         break;
836                 }
837
838                 count = fuse_fill_write_pages(req, mapping, ii, pos);
839                 if (count <= 0) {
840                         err = count;
841                 } else {
842                         size_t num_written;
843
844                         num_written = fuse_send_write_pages(req, file, inode,
845                                                             pos, count);
846                         err = req->out.h.error;
847                         if (!err) {
848                                 res += num_written;
849                                 pos += num_written;
850
851                                 /* break out of the loop on short write */
852                                 if (num_written != count)
853                                         err = -EIO;
854                         }
855                 }
856                 fuse_put_request(fc, req);
857         } while (!err && iov_iter_count(ii));
858
859         if (res > 0)
860                 fuse_write_update_size(inode, pos);
861
862         fuse_invalidate_attr(inode);
863
864         return res > 0 ? res : err;
865 }
866
867 static ssize_t fuse_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
868                                    unsigned long nr_segs, loff_t pos)
869 {
870         struct file *file = iocb->ki_filp;
871         struct address_space *mapping = file->f_mapping;
872         size_t count = 0;
873         ssize_t written = 0;
874         struct inode *inode = mapping->host;
875         ssize_t err;
876         struct iov_iter i;
877
878         WARN_ON(iocb->ki_pos != pos);
879
880         err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
881         if (err)
882                 return err;
883
884         mutex_lock(&inode->i_mutex);
885         vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE);
886
887         /* We can write back this queue in page reclaim */
888         current->backing_dev_info = mapping->backing_dev_info;
889
890         err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
891         if (err)
892                 goto out;
893
894         if (count == 0)
895                 goto out;
896
897         err = file_remove_suid(file);
898         if (err)
899                 goto out;
900
901         file_update_time(file);
902
903         iov_iter_init(&i, iov, nr_segs, count, 0);
904         written = fuse_perform_write(file, mapping, &i, pos);
905         if (written >= 0)
906                 iocb->ki_pos = pos + written;
907
908 out:
909         current->backing_dev_info = NULL;
910         mutex_unlock(&inode->i_mutex);
911
912         return written ? written : err;
913 }
914
915 static void fuse_release_user_pages(struct fuse_req *req, int write)
916 {
917         unsigned i;
918
919         for (i = 0; i < req->num_pages; i++) {
920                 struct page *page = req->pages[i];
921                 if (write)
922                         set_page_dirty_lock(page);
923                 put_page(page);
924         }
925 }
926
927 static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
928                                unsigned nbytes, int write)
929 {
930         unsigned long user_addr = (unsigned long) buf;
931         unsigned offset = user_addr & ~PAGE_MASK;
932         int npages;
933
934         /* This doesn't work with nfsd */
935         if (!current->mm)
936                 return -EPERM;
937
938         nbytes = min(nbytes, (unsigned) FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
939         npages = (nbytes + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
940         npages = clamp(npages, 1, FUSE_MAX_PAGES_PER_REQ);
941         down_read(&current->mm->mmap_sem);
942         npages = get_user_pages(current, current->mm, user_addr, npages, write,
943                                 0, req->pages, NULL);
944         up_read(&current->mm->mmap_sem);
945         if (npages < 0)
946                 return npages;
947
948         req->num_pages = npages;
949         req->page_offset = offset;
950         return 0;
951 }
952
953 static ssize_t fuse_direct_io(struct file *file, const char __user *buf,
954                               size_t count, loff_t *ppos, int write)
955 {
956         struct inode *inode = file->f_path.dentry->d_inode;
957         struct fuse_conn *fc = get_fuse_conn(inode);
958         size_t nmax = write ? fc->max_write : fc->max_read;
959         loff_t pos = *ppos;
960         ssize_t res = 0;
961         struct fuse_req *req;
962
963         if (is_bad_inode(inode))
964                 return -EIO;
965
966         req = fuse_get_req(fc);
967         if (IS_ERR(req))
968                 return PTR_ERR(req);
969
970         while (count) {
971                 size_t nres;
972                 size_t nbytes_limit = min(count, nmax);
973                 size_t nbytes;
974                 int err = fuse_get_user_pages(req, buf, nbytes_limit, !write);
975                 if (err) {
976                         res = err;
977                         break;
978                 }
979                 nbytes = (req->num_pages << PAGE_SHIFT) - req->page_offset;
980                 nbytes = min(nbytes_limit, nbytes);
981                 if (write)
982                         nres = fuse_send_write(req, file, inode, pos, nbytes,
983                                                current->files);
984                 else
985                         nres = fuse_send_read(req, file, inode, pos, nbytes,
986                                               current->files);
987                 fuse_release_user_pages(req, !write);
988                 if (req->out.h.error) {
989                         if (!res)
990                                 res = req->out.h.error;
991                         break;
992                 } else if (nres > nbytes) {
993                         res = -EIO;
994                         break;
995                 }
996                 count -= nres;
997                 res += nres;
998                 pos += nres;
999                 buf += nres;
1000                 if (nres != nbytes)
1001                         break;
1002                 if (count) {
1003                         fuse_put_request(fc, req);
1004                         req = fuse_get_req(fc);
1005                         if (IS_ERR(req))
1006                                 break;
1007                 }
1008         }
1009         fuse_put_request(fc, req);
1010         if (res > 0) {
1011                 if (write)
1012                         fuse_write_update_size(inode, pos);
1013                 *ppos = pos;
1014         }
1015         fuse_invalidate_attr(inode);
1016
1017         return res;
1018 }
1019
1020 static ssize_t fuse_direct_read(struct file *file, char __user *buf,
1021                                      size_t count, loff_t *ppos)
1022 {
1023         return fuse_direct_io(file, buf, count, ppos, 0);
1024 }
1025
1026 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
1027                                  size_t count, loff_t *ppos)
1028 {
1029         struct inode *inode = file->f_path.dentry->d_inode;
1030         ssize_t res;
1031         /* Don't allow parallel writes to the same file */
1032         mutex_lock(&inode->i_mutex);
1033         res = generic_write_checks(file, ppos, &count, 0);
1034         if (!res)
1035                 res = fuse_direct_io(file, buf, count, ppos, 1);
1036         mutex_unlock(&inode->i_mutex);
1037         return res;
1038 }
1039
1040 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1041 {
1042         __free_page(req->pages[0]);
1043         fuse_file_put(req->ff);
1044 }
1045
1046 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1047 {
1048         struct inode *inode = req->inode;
1049         struct fuse_inode *fi = get_fuse_inode(inode);
1050         struct backing_dev_info *bdi = inode->i_mapping->backing_dev_info;
1051
1052         list_del(&req->writepages_entry);
1053         dec_bdi_stat(bdi, BDI_WRITEBACK);
1054         dec_zone_page_state(req->pages[0], NR_WRITEBACK_TEMP);
1055         bdi_writeout_inc(bdi);
1056         wake_up(&fi->page_waitq);
1057 }
1058
1059 /* Called under fc->lock, may release and reacquire it */
1060 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req)
1061 {
1062         struct fuse_inode *fi = get_fuse_inode(req->inode);
1063         loff_t size = i_size_read(req->inode);
1064         struct fuse_write_in *inarg = &req->misc.write.in;
1065
1066         if (!fc->connected)
1067                 goto out_free;
1068
1069         if (inarg->offset + PAGE_CACHE_SIZE <= size) {
1070                 inarg->size = PAGE_CACHE_SIZE;
1071         } else if (inarg->offset < size) {
1072                 inarg->size = size & (PAGE_CACHE_SIZE - 1);
1073         } else {
1074                 /* Got truncated off completely */
1075                 goto out_free;
1076         }
1077
1078         req->in.args[1].size = inarg->size;
1079         fi->writectr++;
1080         request_send_background_locked(fc, req);
1081         return;
1082
1083  out_free:
1084         fuse_writepage_finish(fc, req);
1085         spin_unlock(&fc->lock);
1086         fuse_writepage_free(fc, req);
1087         fuse_put_request(fc, req);
1088         spin_lock(&fc->lock);
1089 }
1090
1091 /*
1092  * If fi->writectr is positive (no truncate or fsync going on) send
1093  * all queued writepage requests.
1094  *
1095  * Called with fc->lock
1096  */
1097 void fuse_flush_writepages(struct inode *inode)
1098 {
1099         struct fuse_conn *fc = get_fuse_conn(inode);
1100         struct fuse_inode *fi = get_fuse_inode(inode);
1101         struct fuse_req *req;
1102
1103         while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1104                 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1105                 list_del_init(&req->list);
1106                 fuse_send_writepage(fc, req);
1107         }
1108 }
1109
1110 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1111 {
1112         struct inode *inode = req->inode;
1113         struct fuse_inode *fi = get_fuse_inode(inode);
1114
1115         mapping_set_error(inode->i_mapping, req->out.h.error);
1116         spin_lock(&fc->lock);
1117         fi->writectr--;
1118         fuse_writepage_finish(fc, req);
1119         spin_unlock(&fc->lock);
1120         fuse_writepage_free(fc, req);
1121 }
1122
1123 static int fuse_writepage_locked(struct page *page)
1124 {
1125         struct address_space *mapping = page->mapping;
1126         struct inode *inode = mapping->host;
1127         struct fuse_conn *fc = get_fuse_conn(inode);
1128         struct fuse_inode *fi = get_fuse_inode(inode);
1129         struct fuse_req *req;
1130         struct fuse_file *ff;
1131         struct page *tmp_page;
1132
1133         set_page_writeback(page);
1134
1135         req = fuse_request_alloc_nofs();
1136         if (!req)
1137                 goto err;
1138
1139         tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1140         if (!tmp_page)
1141                 goto err_free;
1142
1143         spin_lock(&fc->lock);
1144         BUG_ON(list_empty(&fi->write_files));
1145         ff = list_entry(fi->write_files.next, struct fuse_file, write_entry);
1146         req->ff = fuse_file_get(ff);
1147         spin_unlock(&fc->lock);
1148
1149         fuse_write_fill(req, NULL, ff, inode, page_offset(page), 0, 1);
1150
1151         copy_highpage(tmp_page, page);
1152         req->num_pages = 1;
1153         req->pages[0] = tmp_page;
1154         req->page_offset = 0;
1155         req->end = fuse_writepage_end;
1156         req->inode = inode;
1157
1158         inc_bdi_stat(mapping->backing_dev_info, BDI_WRITEBACK);
1159         inc_zone_page_state(tmp_page, NR_WRITEBACK_TEMP);
1160         end_page_writeback(page);
1161
1162         spin_lock(&fc->lock);
1163         list_add(&req->writepages_entry, &fi->writepages);
1164         list_add_tail(&req->list, &fi->queued_writes);
1165         fuse_flush_writepages(inode);
1166         spin_unlock(&fc->lock);
1167
1168         return 0;
1169
1170 err_free:
1171         fuse_request_free(req);
1172 err:
1173         end_page_writeback(page);
1174         return -ENOMEM;
1175 }
1176
1177 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1178 {
1179         int err;
1180
1181         err = fuse_writepage_locked(page);
1182         unlock_page(page);
1183
1184         return err;
1185 }
1186
1187 static int fuse_launder_page(struct page *page)
1188 {
1189         int err = 0;
1190         if (clear_page_dirty_for_io(page)) {
1191                 struct inode *inode = page->mapping->host;
1192                 err = fuse_writepage_locked(page);
1193                 if (!err)
1194                         fuse_wait_on_page_writeback(inode, page->index);
1195         }
1196         return err;
1197 }
1198
1199 /*
1200  * Write back dirty pages now, because there may not be any suitable
1201  * open files later
1202  */
1203 static void fuse_vma_close(struct vm_area_struct *vma)
1204 {
1205         filemap_write_and_wait(vma->vm_file->f_mapping);
1206 }
1207
1208 /*
1209  * Wait for writeback against this page to complete before allowing it
1210  * to be marked dirty again, and hence written back again, possibly
1211  * before the previous writepage completed.
1212  *
1213  * Block here, instead of in ->writepage(), so that the userspace fs
1214  * can only block processes actually operating on the filesystem.
1215  *
1216  * Otherwise unprivileged userspace fs would be able to block
1217  * unrelated:
1218  *
1219  * - page migration
1220  * - sync(2)
1221  * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1222  */
1223 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct page *page)
1224 {
1225         /*
1226          * Don't use page->mapping as it may become NULL from a
1227          * concurrent truncate.
1228          */
1229         struct inode *inode = vma->vm_file->f_mapping->host;
1230
1231         fuse_wait_on_page_writeback(inode, page->index);
1232         return 0;
1233 }
1234
1235 static struct vm_operations_struct fuse_file_vm_ops = {
1236         .close          = fuse_vma_close,
1237         .fault          = filemap_fault,
1238         .page_mkwrite   = fuse_page_mkwrite,
1239 };
1240
1241 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
1242 {
1243         if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
1244                 struct inode *inode = file->f_dentry->d_inode;
1245                 struct fuse_conn *fc = get_fuse_conn(inode);
1246                 struct fuse_inode *fi = get_fuse_inode(inode);
1247                 struct fuse_file *ff = file->private_data;
1248                 /*
1249                  * file may be written through mmap, so chain it onto the
1250                  * inodes's write_file list
1251                  */
1252                 spin_lock(&fc->lock);
1253                 if (list_empty(&ff->write_entry))
1254                         list_add(&ff->write_entry, &fi->write_files);
1255                 spin_unlock(&fc->lock);
1256         }
1257         file_accessed(file);
1258         vma->vm_ops = &fuse_file_vm_ops;
1259         return 0;
1260 }
1261
1262 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
1263                                   struct file_lock *fl)
1264 {
1265         switch (ffl->type) {
1266         case F_UNLCK:
1267                 break;
1268
1269         case F_RDLCK:
1270         case F_WRLCK:
1271                 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
1272                     ffl->end < ffl->start)
1273                         return -EIO;
1274
1275                 fl->fl_start = ffl->start;
1276                 fl->fl_end = ffl->end;
1277                 fl->fl_pid = ffl->pid;
1278                 break;
1279
1280         default:
1281                 return -EIO;
1282         }
1283         fl->fl_type = ffl->type;
1284         return 0;
1285 }
1286
1287 static void fuse_lk_fill(struct fuse_req *req, struct file *file,
1288                          const struct file_lock *fl, int opcode, pid_t pid,
1289                          int flock)
1290 {
1291         struct inode *inode = file->f_path.dentry->d_inode;
1292         struct fuse_conn *fc = get_fuse_conn(inode);
1293         struct fuse_file *ff = file->private_data;
1294         struct fuse_lk_in *arg = &req->misc.lk_in;
1295
1296         arg->fh = ff->fh;
1297         arg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
1298         arg->lk.start = fl->fl_start;
1299         arg->lk.end = fl->fl_end;
1300         arg->lk.type = fl->fl_type;
1301         arg->lk.pid = pid;
1302         if (flock)
1303                 arg->lk_flags |= FUSE_LK_FLOCK;
1304         req->in.h.opcode = opcode;
1305         req->in.h.nodeid = get_node_id(inode);
1306         req->in.numargs = 1;
1307         req->in.args[0].size = sizeof(*arg);
1308         req->in.args[0].value = arg;
1309 }
1310
1311 static int fuse_getlk(struct file *file, struct file_lock *fl)
1312 {
1313         struct inode *inode = file->f_path.dentry->d_inode;
1314         struct fuse_conn *fc = get_fuse_conn(inode);
1315         struct fuse_req *req;
1316         struct fuse_lk_out outarg;
1317         int err;
1318
1319         req = fuse_get_req(fc);
1320         if (IS_ERR(req))
1321                 return PTR_ERR(req);
1322
1323         fuse_lk_fill(req, file, fl, FUSE_GETLK, 0, 0);
1324         req->out.numargs = 1;
1325         req->out.args[0].size = sizeof(outarg);
1326         req->out.args[0].value = &outarg;
1327         request_send(fc, req);
1328         err = req->out.h.error;
1329         fuse_put_request(fc, req);
1330         if (!err)
1331                 err = convert_fuse_file_lock(&outarg.lk, fl);
1332
1333         return err;
1334 }
1335
1336 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
1337 {
1338         struct inode *inode = file->f_path.dentry->d_inode;
1339         struct fuse_conn *fc = get_fuse_conn(inode);
1340         struct fuse_req *req;
1341         int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
1342         pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
1343         int err;
1344
1345         if (fl->fl_lmops && fl->fl_lmops->fl_grant) {
1346                 /* NLM needs asynchronous locks, which we don't support yet */
1347                 return -ENOLCK;
1348         }
1349
1350         /* Unlock on close is handled by the flush method */
1351         if (fl->fl_flags & FL_CLOSE)
1352                 return 0;
1353
1354         req = fuse_get_req(fc);
1355         if (IS_ERR(req))
1356                 return PTR_ERR(req);
1357
1358         fuse_lk_fill(req, file, fl, opcode, pid, flock);
1359         request_send(fc, req);
1360         err = req->out.h.error;
1361         /* locking is restartable */
1362         if (err == -EINTR)
1363                 err = -ERESTARTSYS;
1364         fuse_put_request(fc, req);
1365         return err;
1366 }
1367
1368 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
1369 {
1370         struct inode *inode = file->f_path.dentry->d_inode;
1371         struct fuse_conn *fc = get_fuse_conn(inode);
1372         int err;
1373
1374         if (cmd == F_CANCELLK) {
1375                 err = 0;
1376         } else if (cmd == F_GETLK) {
1377                 if (fc->no_lock) {
1378                         posix_test_lock(file, fl);
1379                         err = 0;
1380                 } else
1381                         err = fuse_getlk(file, fl);
1382         } else {
1383                 if (fc->no_lock)
1384                         err = posix_lock_file(file, fl, NULL);
1385                 else
1386                         err = fuse_setlk(file, fl, 0);
1387         }
1388         return err;
1389 }
1390
1391 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
1392 {
1393         struct inode *inode = file->f_path.dentry->d_inode;
1394         struct fuse_conn *fc = get_fuse_conn(inode);
1395         int err;
1396
1397         if (fc->no_lock) {
1398                 err = flock_lock_file_wait(file, fl);
1399         } else {
1400                 /* emulate flock with POSIX locks */
1401                 fl->fl_owner = (fl_owner_t) file;
1402                 err = fuse_setlk(file, fl, 1);
1403         }
1404
1405         return err;
1406 }
1407
1408 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
1409 {
1410         struct inode *inode = mapping->host;
1411         struct fuse_conn *fc = get_fuse_conn(inode);
1412         struct fuse_req *req;
1413         struct fuse_bmap_in inarg;
1414         struct fuse_bmap_out outarg;
1415         int err;
1416
1417         if (!inode->i_sb->s_bdev || fc->no_bmap)
1418                 return 0;
1419
1420         req = fuse_get_req(fc);
1421         if (IS_ERR(req))
1422                 return 0;
1423
1424         memset(&inarg, 0, sizeof(inarg));
1425         inarg.block = block;
1426         inarg.blocksize = inode->i_sb->s_blocksize;
1427         req->in.h.opcode = FUSE_BMAP;
1428         req->in.h.nodeid = get_node_id(inode);
1429         req->in.numargs = 1;
1430         req->in.args[0].size = sizeof(inarg);
1431         req->in.args[0].value = &inarg;
1432         req->out.numargs = 1;
1433         req->out.args[0].size = sizeof(outarg);
1434         req->out.args[0].value = &outarg;
1435         request_send(fc, req);
1436         err = req->out.h.error;
1437         fuse_put_request(fc, req);
1438         if (err == -ENOSYS)
1439                 fc->no_bmap = 1;
1440
1441         return err ? 0 : outarg.block;
1442 }
1443
1444 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
1445 {
1446         loff_t retval;
1447         struct inode *inode = file->f_path.dentry->d_inode;
1448
1449         mutex_lock(&inode->i_mutex);
1450         switch (origin) {
1451         case SEEK_END:
1452                 retval = fuse_update_attributes(inode, NULL, file, NULL);
1453                 if (retval)
1454                         return retval;
1455                 offset += i_size_read(inode);
1456                 break;
1457         case SEEK_CUR:
1458                 offset += file->f_pos;
1459         }
1460         retval = -EINVAL;
1461         if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) {
1462                 if (offset != file->f_pos) {
1463                         file->f_pos = offset;
1464                         file->f_version = 0;
1465                 }
1466                 retval = offset;
1467         }
1468         mutex_unlock(&inode->i_mutex);
1469         return retval;
1470 }
1471
1472 static const struct file_operations fuse_file_operations = {
1473         .llseek         = fuse_file_llseek,
1474         .read           = do_sync_read,
1475         .aio_read       = fuse_file_aio_read,
1476         .write          = do_sync_write,
1477         .aio_write      = fuse_file_aio_write,
1478         .mmap           = fuse_file_mmap,
1479         .open           = fuse_open,
1480         .flush          = fuse_flush,
1481         .release        = fuse_release,
1482         .fsync          = fuse_fsync,
1483         .lock           = fuse_file_lock,
1484         .flock          = fuse_file_flock,
1485         .splice_read    = generic_file_splice_read,
1486 };
1487
1488 static const struct file_operations fuse_direct_io_file_operations = {
1489         .llseek         = fuse_file_llseek,
1490         .read           = fuse_direct_read,
1491         .write          = fuse_direct_write,
1492         .open           = fuse_open,
1493         .flush          = fuse_flush,
1494         .release        = fuse_release,
1495         .fsync          = fuse_fsync,
1496         .lock           = fuse_file_lock,
1497         .flock          = fuse_file_flock,
1498         /* no mmap and splice_read */
1499 };
1500
1501 static const struct address_space_operations fuse_file_aops  = {
1502         .readpage       = fuse_readpage,
1503         .writepage      = fuse_writepage,
1504         .launder_page   = fuse_launder_page,
1505         .write_begin    = fuse_write_begin,
1506         .write_end      = fuse_write_end,
1507         .readpages      = fuse_readpages,
1508         .set_page_dirty = __set_page_dirty_nobuffers,
1509         .bmap           = fuse_bmap,
1510 };
1511
1512 void fuse_init_file_inode(struct inode *inode)
1513 {
1514         inode->i_fop = &fuse_file_operations;
1515         inode->i_data.a_ops = &fuse_file_aops;
1516 }