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