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