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