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