Merge remote-tracking branch 'iommu/next'
[deliverable/linux.git] / fs / fuse / file.c
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
8
9 #include "fuse_i.h"
10
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/falloc.h>
19 #include <linux/uio.h>
20
21 static const struct file_operations fuse_direct_io_file_operations;
22
23 static int fuse_send_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
24 int opcode, struct fuse_open_out *outargp)
25 {
26 struct fuse_open_in inarg;
27 FUSE_ARGS(args);
28
29 memset(&inarg, 0, sizeof(inarg));
30 inarg.flags = file->f_flags & ~(O_CREAT | O_EXCL | O_NOCTTY);
31 if (!fc->atomic_o_trunc)
32 inarg.flags &= ~O_TRUNC;
33 args.in.h.opcode = opcode;
34 args.in.h.nodeid = nodeid;
35 args.in.numargs = 1;
36 args.in.args[0].size = sizeof(inarg);
37 args.in.args[0].value = &inarg;
38 args.out.numargs = 1;
39 args.out.args[0].size = sizeof(*outargp);
40 args.out.args[0].value = outargp;
41
42 return fuse_simple_request(fc, &args);
43 }
44
45 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
46 {
47 struct fuse_file *ff;
48
49 ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
50 if (unlikely(!ff))
51 return NULL;
52
53 ff->fc = fc;
54 ff->reserved_req = fuse_request_alloc(0);
55 if (unlikely(!ff->reserved_req)) {
56 kfree(ff);
57 return NULL;
58 }
59
60 INIT_LIST_HEAD(&ff->write_entry);
61 atomic_set(&ff->count, 0);
62 RB_CLEAR_NODE(&ff->polled_node);
63 init_waitqueue_head(&ff->poll_wait);
64
65 spin_lock(&fc->lock);
66 ff->kh = ++fc->khctr;
67 spin_unlock(&fc->lock);
68
69 return ff;
70 }
71
72 void fuse_file_free(struct fuse_file *ff)
73 {
74 fuse_request_free(ff->reserved_req);
75 kfree(ff);
76 }
77
78 struct fuse_file *fuse_file_get(struct fuse_file *ff)
79 {
80 atomic_inc(&ff->count);
81 return ff;
82 }
83
84 static void fuse_release_end(struct fuse_conn *fc, struct fuse_req *req)
85 {
86 iput(req->misc.release.inode);
87 }
88
89 static void fuse_file_put(struct fuse_file *ff, bool sync)
90 {
91 if (atomic_dec_and_test(&ff->count)) {
92 struct fuse_req *req = ff->reserved_req;
93
94 if (ff->fc->no_open) {
95 /*
96 * Drop the release request when client does not
97 * implement 'open'
98 */
99 __clear_bit(FR_BACKGROUND, &req->flags);
100 iput(req->misc.release.inode);
101 fuse_put_request(ff->fc, req);
102 } else if (sync) {
103 __clear_bit(FR_BACKGROUND, &req->flags);
104 fuse_request_send(ff->fc, req);
105 iput(req->misc.release.inode);
106 fuse_put_request(ff->fc, req);
107 } else {
108 req->end = fuse_release_end;
109 __set_bit(FR_BACKGROUND, &req->flags);
110 fuse_request_send_background(ff->fc, req);
111 }
112 kfree(ff);
113 }
114 }
115
116 int fuse_do_open(struct fuse_conn *fc, u64 nodeid, struct file *file,
117 bool isdir)
118 {
119 struct fuse_file *ff;
120 int opcode = isdir ? FUSE_OPENDIR : FUSE_OPEN;
121
122 ff = fuse_file_alloc(fc);
123 if (!ff)
124 return -ENOMEM;
125
126 ff->fh = 0;
127 ff->open_flags = FOPEN_KEEP_CACHE; /* Default for no-open */
128 if (!fc->no_open || isdir) {
129 struct fuse_open_out outarg;
130 int err;
131
132 err = fuse_send_open(fc, nodeid, file, opcode, &outarg);
133 if (!err) {
134 ff->fh = outarg.fh;
135 ff->open_flags = outarg.open_flags;
136
137 } else if (err != -ENOSYS || isdir) {
138 fuse_file_free(ff);
139 return err;
140 } else {
141 fc->no_open = 1;
142 }
143 }
144
145 if (isdir)
146 ff->open_flags &= ~FOPEN_DIRECT_IO;
147
148 ff->nodeid = nodeid;
149 file->private_data = fuse_file_get(ff);
150
151 return 0;
152 }
153 EXPORT_SYMBOL_GPL(fuse_do_open);
154
155 static void fuse_link_write_file(struct file *file)
156 {
157 struct inode *inode = file_inode(file);
158 struct fuse_conn *fc = get_fuse_conn(inode);
159 struct fuse_inode *fi = get_fuse_inode(inode);
160 struct fuse_file *ff = file->private_data;
161 /*
162 * file may be written through mmap, so chain it onto the
163 * inodes's write_file list
164 */
165 spin_lock(&fc->lock);
166 if (list_empty(&ff->write_entry))
167 list_add(&ff->write_entry, &fi->write_files);
168 spin_unlock(&fc->lock);
169 }
170
171 void fuse_finish_open(struct inode *inode, struct file *file)
172 {
173 struct fuse_file *ff = file->private_data;
174 struct fuse_conn *fc = get_fuse_conn(inode);
175
176 if (ff->open_flags & FOPEN_DIRECT_IO)
177 file->f_op = &fuse_direct_io_file_operations;
178 if (!(ff->open_flags & FOPEN_KEEP_CACHE))
179 invalidate_inode_pages2(inode->i_mapping);
180 if (ff->open_flags & FOPEN_NONSEEKABLE)
181 nonseekable_open(inode, file);
182 if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
183 struct fuse_inode *fi = get_fuse_inode(inode);
184
185 spin_lock(&fc->lock);
186 fi->attr_version = ++fc->attr_version;
187 i_size_write(inode, 0);
188 spin_unlock(&fc->lock);
189 fuse_invalidate_attr(inode);
190 if (fc->writeback_cache)
191 file_update_time(file);
192 }
193 if ((file->f_mode & FMODE_WRITE) && fc->writeback_cache)
194 fuse_link_write_file(file);
195 }
196
197 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
198 {
199 struct fuse_conn *fc = get_fuse_conn(inode);
200 int err;
201 bool lock_inode = (file->f_flags & O_TRUNC) &&
202 fc->atomic_o_trunc &&
203 fc->writeback_cache;
204
205 err = generic_file_open(inode, file);
206 if (err)
207 return err;
208
209 if (lock_inode)
210 inode_lock(inode);
211
212 err = fuse_do_open(fc, get_node_id(inode), file, isdir);
213
214 if (!err)
215 fuse_finish_open(inode, file);
216
217 if (lock_inode)
218 inode_unlock(inode);
219
220 return err;
221 }
222
223 static void fuse_prepare_release(struct fuse_file *ff, int flags, int opcode)
224 {
225 struct fuse_conn *fc = ff->fc;
226 struct fuse_req *req = ff->reserved_req;
227 struct fuse_release_in *inarg = &req->misc.release.in;
228
229 spin_lock(&fc->lock);
230 list_del(&ff->write_entry);
231 if (!RB_EMPTY_NODE(&ff->polled_node))
232 rb_erase(&ff->polled_node, &fc->polled_files);
233 spin_unlock(&fc->lock);
234
235 wake_up_interruptible_all(&ff->poll_wait);
236
237 inarg->fh = ff->fh;
238 inarg->flags = flags;
239 req->in.h.opcode = opcode;
240 req->in.h.nodeid = ff->nodeid;
241 req->in.numargs = 1;
242 req->in.args[0].size = sizeof(struct fuse_release_in);
243 req->in.args[0].value = inarg;
244 }
245
246 void fuse_release_common(struct file *file, int opcode)
247 {
248 struct fuse_file *ff;
249 struct fuse_req *req;
250
251 ff = file->private_data;
252 if (unlikely(!ff))
253 return;
254
255 req = ff->reserved_req;
256 fuse_prepare_release(ff, file->f_flags, opcode);
257
258 if (ff->flock) {
259 struct fuse_release_in *inarg = &req->misc.release.in;
260 inarg->release_flags |= FUSE_RELEASE_FLOCK_UNLOCK;
261 inarg->lock_owner = fuse_lock_owner_id(ff->fc,
262 (fl_owner_t) file);
263 }
264 /* Hold inode until release is finished */
265 req->misc.release.inode = igrab(file_inode(file));
266
267 /*
268 * Normally this will send the RELEASE request, however if
269 * some asynchronous READ or WRITE requests are outstanding,
270 * the sending will be delayed.
271 *
272 * Make the release synchronous if this is a fuseblk mount,
273 * synchronous RELEASE is allowed (and desirable) in this case
274 * because the server can be trusted not to screw up.
275 */
276 fuse_file_put(ff, ff->fc->destroy_req != NULL);
277 }
278
279 static int fuse_open(struct inode *inode, struct file *file)
280 {
281 return fuse_open_common(inode, file, false);
282 }
283
284 static int fuse_release(struct inode *inode, struct file *file)
285 {
286 struct fuse_conn *fc = get_fuse_conn(inode);
287
288 /* see fuse_vma_close() for !writeback_cache case */
289 if (fc->writeback_cache)
290 write_inode_now(inode, 1);
291
292 fuse_release_common(file, FUSE_RELEASE);
293
294 /* return value is ignored by VFS */
295 return 0;
296 }
297
298 void fuse_sync_release(struct fuse_file *ff, int flags)
299 {
300 WARN_ON(atomic_read(&ff->count) > 1);
301 fuse_prepare_release(ff, flags, FUSE_RELEASE);
302 __set_bit(FR_FORCE, &ff->reserved_req->flags);
303 __clear_bit(FR_BACKGROUND, &ff->reserved_req->flags);
304 fuse_request_send(ff->fc, ff->reserved_req);
305 fuse_put_request(ff->fc, ff->reserved_req);
306 kfree(ff);
307 }
308 EXPORT_SYMBOL_GPL(fuse_sync_release);
309
310 /*
311 * Scramble the ID space with XTEA, so that the value of the files_struct
312 * pointer is not exposed to userspace.
313 */
314 u64 fuse_lock_owner_id(struct fuse_conn *fc, fl_owner_t id)
315 {
316 u32 *k = fc->scramble_key;
317 u64 v = (unsigned long) id;
318 u32 v0 = v;
319 u32 v1 = v >> 32;
320 u32 sum = 0;
321 int i;
322
323 for (i = 0; i < 32; i++) {
324 v0 += ((v1 << 4 ^ v1 >> 5) + v1) ^ (sum + k[sum & 3]);
325 sum += 0x9E3779B9;
326 v1 += ((v0 << 4 ^ v0 >> 5) + v0) ^ (sum + k[sum>>11 & 3]);
327 }
328
329 return (u64) v0 + ((u64) v1 << 32);
330 }
331
332 /*
333 * Check if any page in a range is under writeback
334 *
335 * This is currently done by walking the list of writepage requests
336 * for the inode, which can be pretty inefficient.
337 */
338 static bool fuse_range_is_writeback(struct inode *inode, pgoff_t idx_from,
339 pgoff_t idx_to)
340 {
341 struct fuse_conn *fc = get_fuse_conn(inode);
342 struct fuse_inode *fi = get_fuse_inode(inode);
343 struct fuse_req *req;
344 bool found = false;
345
346 spin_lock(&fc->lock);
347 list_for_each_entry(req, &fi->writepages, writepages_entry) {
348 pgoff_t curr_index;
349
350 BUG_ON(req->inode != inode);
351 curr_index = req->misc.write.in.offset >> PAGE_SHIFT;
352 if (idx_from < curr_index + req->num_pages &&
353 curr_index <= idx_to) {
354 found = true;
355 break;
356 }
357 }
358 spin_unlock(&fc->lock);
359
360 return found;
361 }
362
363 static inline bool fuse_page_is_writeback(struct inode *inode, pgoff_t index)
364 {
365 return fuse_range_is_writeback(inode, index, index);
366 }
367
368 /*
369 * Wait for page writeback to be completed.
370 *
371 * Since fuse doesn't rely on the VM writeback tracking, this has to
372 * use some other means.
373 */
374 static int fuse_wait_on_page_writeback(struct inode *inode, pgoff_t index)
375 {
376 struct fuse_inode *fi = get_fuse_inode(inode);
377
378 wait_event(fi->page_waitq, !fuse_page_is_writeback(inode, index));
379 return 0;
380 }
381
382 /*
383 * Wait for all pending writepages on the inode to finish.
384 *
385 * This is currently done by blocking further writes with FUSE_NOWRITE
386 * and waiting for all sent writes to complete.
387 *
388 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
389 * could conflict with truncation.
390 */
391 static void fuse_sync_writes(struct inode *inode)
392 {
393 fuse_set_nowrite(inode);
394 fuse_release_nowrite(inode);
395 }
396
397 static int fuse_flush(struct file *file, fl_owner_t id)
398 {
399 struct inode *inode = file_inode(file);
400 struct fuse_conn *fc = get_fuse_conn(inode);
401 struct fuse_file *ff = file->private_data;
402 struct fuse_req *req;
403 struct fuse_flush_in inarg;
404 int err;
405
406 if (is_bad_inode(inode))
407 return -EIO;
408
409 if (fc->no_flush)
410 return 0;
411
412 err = write_inode_now(inode, 1);
413 if (err)
414 return err;
415
416 inode_lock(inode);
417 fuse_sync_writes(inode);
418 inode_unlock(inode);
419
420 err = filemap_check_errors(file->f_mapping);
421 if (err)
422 return err;
423
424 req = fuse_get_req_nofail_nopages(fc, file);
425 memset(&inarg, 0, sizeof(inarg));
426 inarg.fh = ff->fh;
427 inarg.lock_owner = fuse_lock_owner_id(fc, id);
428 req->in.h.opcode = FUSE_FLUSH;
429 req->in.h.nodeid = get_node_id(inode);
430 req->in.numargs = 1;
431 req->in.args[0].size = sizeof(inarg);
432 req->in.args[0].value = &inarg;
433 __set_bit(FR_FORCE, &req->flags);
434 fuse_request_send(fc, req);
435 err = req->out.h.error;
436 fuse_put_request(fc, req);
437 if (err == -ENOSYS) {
438 fc->no_flush = 1;
439 err = 0;
440 }
441 return err;
442 }
443
444 int fuse_fsync_common(struct file *file, loff_t start, loff_t end,
445 int datasync, int isdir)
446 {
447 struct inode *inode = file->f_mapping->host;
448 struct fuse_conn *fc = get_fuse_conn(inode);
449 struct fuse_file *ff = file->private_data;
450 FUSE_ARGS(args);
451 struct fuse_fsync_in inarg;
452 int err;
453
454 if (is_bad_inode(inode))
455 return -EIO;
456
457 inode_lock(inode);
458
459 /*
460 * Start writeback against all dirty pages of the inode, then
461 * wait for all outstanding writes, before sending the FSYNC
462 * request.
463 */
464 err = filemap_write_and_wait_range(inode->i_mapping, start, end);
465 if (err)
466 goto out;
467
468 fuse_sync_writes(inode);
469
470 /*
471 * Due to implementation of fuse writeback
472 * filemap_write_and_wait_range() does not catch errors.
473 * We have to do this directly after fuse_sync_writes()
474 */
475 err = filemap_check_errors(file->f_mapping);
476 if (err)
477 goto out;
478
479 err = sync_inode_metadata(inode, 1);
480 if (err)
481 goto out;
482
483 if ((!isdir && fc->no_fsync) || (isdir && fc->no_fsyncdir))
484 goto out;
485
486 memset(&inarg, 0, sizeof(inarg));
487 inarg.fh = ff->fh;
488 inarg.fsync_flags = datasync ? 1 : 0;
489 args.in.h.opcode = isdir ? FUSE_FSYNCDIR : FUSE_FSYNC;
490 args.in.h.nodeid = get_node_id(inode);
491 args.in.numargs = 1;
492 args.in.args[0].size = sizeof(inarg);
493 args.in.args[0].value = &inarg;
494 err = fuse_simple_request(fc, &args);
495 if (err == -ENOSYS) {
496 if (isdir)
497 fc->no_fsyncdir = 1;
498 else
499 fc->no_fsync = 1;
500 err = 0;
501 }
502 out:
503 inode_unlock(inode);
504 return err;
505 }
506
507 static int fuse_fsync(struct file *file, loff_t start, loff_t end,
508 int datasync)
509 {
510 return fuse_fsync_common(file, start, end, datasync, 0);
511 }
512
513 void fuse_read_fill(struct fuse_req *req, struct file *file, loff_t pos,
514 size_t count, int opcode)
515 {
516 struct fuse_read_in *inarg = &req->misc.read.in;
517 struct fuse_file *ff = file->private_data;
518
519 inarg->fh = ff->fh;
520 inarg->offset = pos;
521 inarg->size = count;
522 inarg->flags = file->f_flags;
523 req->in.h.opcode = opcode;
524 req->in.h.nodeid = ff->nodeid;
525 req->in.numargs = 1;
526 req->in.args[0].size = sizeof(struct fuse_read_in);
527 req->in.args[0].value = inarg;
528 req->out.argvar = 1;
529 req->out.numargs = 1;
530 req->out.args[0].size = count;
531 }
532
533 static void fuse_release_user_pages(struct fuse_req *req, bool should_dirty)
534 {
535 unsigned i;
536
537 for (i = 0; i < req->num_pages; i++) {
538 struct page *page = req->pages[i];
539 if (should_dirty)
540 set_page_dirty_lock(page);
541 put_page(page);
542 }
543 }
544
545 static void fuse_io_release(struct kref *kref)
546 {
547 kfree(container_of(kref, struct fuse_io_priv, refcnt));
548 }
549
550 static ssize_t fuse_get_res_by_io(struct fuse_io_priv *io)
551 {
552 if (io->err)
553 return io->err;
554
555 if (io->bytes >= 0 && io->write)
556 return -EIO;
557
558 return io->bytes < 0 ? io->size : io->bytes;
559 }
560
561 /**
562 * In case of short read, the caller sets 'pos' to the position of
563 * actual end of fuse request in IO request. Otherwise, if bytes_requested
564 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
565 *
566 * An example:
567 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
568 * both submitted asynchronously. The first of them was ACKed by userspace as
569 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
570 * second request was ACKed as short, e.g. only 1K was read, resulting in
571 * pos == 33K.
572 *
573 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
574 * will be equal to the length of the longest contiguous fragment of
575 * transferred data starting from the beginning of IO request.
576 */
577 static void fuse_aio_complete(struct fuse_io_priv *io, int err, ssize_t pos)
578 {
579 int left;
580
581 spin_lock(&io->lock);
582 if (err)
583 io->err = io->err ? : err;
584 else if (pos >= 0 && (io->bytes < 0 || pos < io->bytes))
585 io->bytes = pos;
586
587 left = --io->reqs;
588 if (!left && io->blocking)
589 complete(io->done);
590 spin_unlock(&io->lock);
591
592 if (!left && !io->blocking) {
593 ssize_t res = fuse_get_res_by_io(io);
594
595 if (res >= 0) {
596 struct inode *inode = file_inode(io->iocb->ki_filp);
597 struct fuse_conn *fc = get_fuse_conn(inode);
598 struct fuse_inode *fi = get_fuse_inode(inode);
599
600 spin_lock(&fc->lock);
601 fi->attr_version = ++fc->attr_version;
602 spin_unlock(&fc->lock);
603 }
604
605 io->iocb->ki_complete(io->iocb, res, 0);
606 }
607
608 kref_put(&io->refcnt, fuse_io_release);
609 }
610
611 static void fuse_aio_complete_req(struct fuse_conn *fc, struct fuse_req *req)
612 {
613 struct fuse_io_priv *io = req->io;
614 ssize_t pos = -1;
615
616 fuse_release_user_pages(req, !io->write);
617
618 if (io->write) {
619 if (req->misc.write.in.size != req->misc.write.out.size)
620 pos = req->misc.write.in.offset - io->offset +
621 req->misc.write.out.size;
622 } else {
623 if (req->misc.read.in.size != req->out.args[0].size)
624 pos = req->misc.read.in.offset - io->offset +
625 req->out.args[0].size;
626 }
627
628 fuse_aio_complete(io, req->out.h.error, pos);
629 }
630
631 static size_t fuse_async_req_send(struct fuse_conn *fc, struct fuse_req *req,
632 size_t num_bytes, struct fuse_io_priv *io)
633 {
634 spin_lock(&io->lock);
635 kref_get(&io->refcnt);
636 io->size += num_bytes;
637 io->reqs++;
638 spin_unlock(&io->lock);
639
640 req->io = io;
641 req->end = fuse_aio_complete_req;
642
643 __fuse_get_request(req);
644 fuse_request_send_background(fc, req);
645
646 return num_bytes;
647 }
648
649 static size_t fuse_send_read(struct fuse_req *req, struct fuse_io_priv *io,
650 loff_t pos, size_t count, fl_owner_t owner)
651 {
652 struct file *file = io->file;
653 struct fuse_file *ff = file->private_data;
654 struct fuse_conn *fc = ff->fc;
655
656 fuse_read_fill(req, file, pos, count, FUSE_READ);
657 if (owner != NULL) {
658 struct fuse_read_in *inarg = &req->misc.read.in;
659
660 inarg->read_flags |= FUSE_READ_LOCKOWNER;
661 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
662 }
663
664 if (io->async)
665 return fuse_async_req_send(fc, req, count, io);
666
667 fuse_request_send(fc, req);
668 return req->out.args[0].size;
669 }
670
671 static void fuse_read_update_size(struct inode *inode, loff_t size,
672 u64 attr_ver)
673 {
674 struct fuse_conn *fc = get_fuse_conn(inode);
675 struct fuse_inode *fi = get_fuse_inode(inode);
676
677 spin_lock(&fc->lock);
678 if (attr_ver == fi->attr_version && size < inode->i_size &&
679 !test_bit(FUSE_I_SIZE_UNSTABLE, &fi->state)) {
680 fi->attr_version = ++fc->attr_version;
681 i_size_write(inode, size);
682 }
683 spin_unlock(&fc->lock);
684 }
685
686 static void fuse_short_read(struct fuse_req *req, struct inode *inode,
687 u64 attr_ver)
688 {
689 size_t num_read = req->out.args[0].size;
690 struct fuse_conn *fc = get_fuse_conn(inode);
691
692 if (fc->writeback_cache) {
693 /*
694 * A hole in a file. Some data after the hole are in page cache,
695 * but have not reached the client fs yet. So, the hole is not
696 * present there.
697 */
698 int i;
699 int start_idx = num_read >> PAGE_SHIFT;
700 size_t off = num_read & (PAGE_SIZE - 1);
701
702 for (i = start_idx; i < req->num_pages; i++) {
703 zero_user_segment(req->pages[i], off, PAGE_SIZE);
704 off = 0;
705 }
706 } else {
707 loff_t pos = page_offset(req->pages[0]) + num_read;
708 fuse_read_update_size(inode, pos, attr_ver);
709 }
710 }
711
712 static int fuse_do_readpage(struct file *file, struct page *page)
713 {
714 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
715 struct inode *inode = page->mapping->host;
716 struct fuse_conn *fc = get_fuse_conn(inode);
717 struct fuse_req *req;
718 size_t num_read;
719 loff_t pos = page_offset(page);
720 size_t count = PAGE_SIZE;
721 u64 attr_ver;
722 int err;
723
724 /*
725 * Page writeback can extend beyond the lifetime of the
726 * page-cache page, so make sure we read a properly synced
727 * page.
728 */
729 fuse_wait_on_page_writeback(inode, page->index);
730
731 req = fuse_get_req(fc, 1);
732 if (IS_ERR(req))
733 return PTR_ERR(req);
734
735 attr_ver = fuse_get_attr_version(fc);
736
737 req->out.page_zeroing = 1;
738 req->out.argpages = 1;
739 req->num_pages = 1;
740 req->pages[0] = page;
741 req->page_descs[0].length = count;
742 num_read = fuse_send_read(req, &io, pos, count, NULL);
743 err = req->out.h.error;
744
745 if (!err) {
746 /*
747 * Short read means EOF. If file size is larger, truncate it
748 */
749 if (num_read < count)
750 fuse_short_read(req, inode, attr_ver);
751
752 SetPageUptodate(page);
753 }
754
755 fuse_put_request(fc, req);
756
757 return err;
758 }
759
760 static int fuse_readpage(struct file *file, struct page *page)
761 {
762 struct inode *inode = page->mapping->host;
763 int err;
764
765 err = -EIO;
766 if (is_bad_inode(inode))
767 goto out;
768
769 err = fuse_do_readpage(file, page);
770 fuse_invalidate_atime(inode);
771 out:
772 unlock_page(page);
773 return err;
774 }
775
776 static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
777 {
778 int i;
779 size_t count = req->misc.read.in.size;
780 size_t num_read = req->out.args[0].size;
781 struct address_space *mapping = NULL;
782
783 for (i = 0; mapping == NULL && i < req->num_pages; i++)
784 mapping = req->pages[i]->mapping;
785
786 if (mapping) {
787 struct inode *inode = mapping->host;
788
789 /*
790 * Short read means EOF. If file size is larger, truncate it
791 */
792 if (!req->out.h.error && num_read < count)
793 fuse_short_read(req, inode, req->misc.read.attr_ver);
794
795 fuse_invalidate_atime(inode);
796 }
797
798 for (i = 0; i < req->num_pages; i++) {
799 struct page *page = req->pages[i];
800 if (!req->out.h.error)
801 SetPageUptodate(page);
802 else
803 SetPageError(page);
804 unlock_page(page);
805 put_page(page);
806 }
807 if (req->ff)
808 fuse_file_put(req->ff, false);
809 }
810
811 static void fuse_send_readpages(struct fuse_req *req, struct file *file)
812 {
813 struct fuse_file *ff = file->private_data;
814 struct fuse_conn *fc = ff->fc;
815 loff_t pos = page_offset(req->pages[0]);
816 size_t count = req->num_pages << PAGE_SHIFT;
817
818 req->out.argpages = 1;
819 req->out.page_zeroing = 1;
820 req->out.page_replace = 1;
821 fuse_read_fill(req, file, pos, count, FUSE_READ);
822 req->misc.read.attr_ver = fuse_get_attr_version(fc);
823 if (fc->async_read) {
824 req->ff = fuse_file_get(ff);
825 req->end = fuse_readpages_end;
826 fuse_request_send_background(fc, req);
827 } else {
828 fuse_request_send(fc, req);
829 fuse_readpages_end(fc, req);
830 fuse_put_request(fc, req);
831 }
832 }
833
834 struct fuse_fill_data {
835 struct fuse_req *req;
836 struct file *file;
837 struct inode *inode;
838 unsigned nr_pages;
839 };
840
841 static int fuse_readpages_fill(void *_data, struct page *page)
842 {
843 struct fuse_fill_data *data = _data;
844 struct fuse_req *req = data->req;
845 struct inode *inode = data->inode;
846 struct fuse_conn *fc = get_fuse_conn(inode);
847
848 fuse_wait_on_page_writeback(inode, page->index);
849
850 if (req->num_pages &&
851 (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
852 (req->num_pages + 1) * PAGE_SIZE > fc->max_read ||
853 req->pages[req->num_pages - 1]->index + 1 != page->index)) {
854 int nr_alloc = min_t(unsigned, data->nr_pages,
855 FUSE_MAX_PAGES_PER_REQ);
856 fuse_send_readpages(req, data->file);
857 if (fc->async_read)
858 req = fuse_get_req_for_background(fc, nr_alloc);
859 else
860 req = fuse_get_req(fc, nr_alloc);
861
862 data->req = req;
863 if (IS_ERR(req)) {
864 unlock_page(page);
865 return PTR_ERR(req);
866 }
867 }
868
869 if (WARN_ON(req->num_pages >= req->max_pages)) {
870 fuse_put_request(fc, req);
871 return -EIO;
872 }
873
874 get_page(page);
875 req->pages[req->num_pages] = page;
876 req->page_descs[req->num_pages].length = PAGE_SIZE;
877 req->num_pages++;
878 data->nr_pages--;
879 return 0;
880 }
881
882 static int fuse_readpages(struct file *file, struct address_space *mapping,
883 struct list_head *pages, unsigned nr_pages)
884 {
885 struct inode *inode = mapping->host;
886 struct fuse_conn *fc = get_fuse_conn(inode);
887 struct fuse_fill_data data;
888 int err;
889 int nr_alloc = min_t(unsigned, nr_pages, FUSE_MAX_PAGES_PER_REQ);
890
891 err = -EIO;
892 if (is_bad_inode(inode))
893 goto out;
894
895 data.file = file;
896 data.inode = inode;
897 if (fc->async_read)
898 data.req = fuse_get_req_for_background(fc, nr_alloc);
899 else
900 data.req = fuse_get_req(fc, nr_alloc);
901 data.nr_pages = nr_pages;
902 err = PTR_ERR(data.req);
903 if (IS_ERR(data.req))
904 goto out;
905
906 err = read_cache_pages(mapping, pages, fuse_readpages_fill, &data);
907 if (!err) {
908 if (data.req->num_pages)
909 fuse_send_readpages(data.req, file);
910 else
911 fuse_put_request(fc, data.req);
912 }
913 out:
914 return err;
915 }
916
917 static ssize_t fuse_update_attr_before_read(struct file *filp, loff_t pos,
918 size_t len)
919 {
920 struct inode *inode = filp->f_mapping->host;
921 struct fuse_conn *fc = get_fuse_conn(inode);
922 int err = 0;
923
924 /*
925 * In auto invalidate mode, always update attributes on read.
926 * Otherwise, only update if we attempt to read past EOF (to ensure
927 * i_size is up to date).
928 */
929 if (fc->auto_inval_data || (pos + len > i_size_read(inode)))
930 err = fuse_update_attributes(inode, NULL, filp, NULL);
931
932 return err;
933 }
934
935 static ssize_t fuse_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
936 {
937 ssize_t err;
938
939 err = fuse_update_attr_before_read(iocb->ki_filp, iocb->ki_pos,
940 iov_iter_count(to));
941 if (err)
942 return err;
943
944 return generic_file_read_iter(iocb, to);
945 }
946
947 static ssize_t fuse_file_splice_read(struct file *in, loff_t *ppos,
948 struct pipe_inode_info *pipe, size_t len,
949 unsigned int flags)
950 {
951 ssize_t err;
952
953 err = fuse_update_attr_before_read(in, *ppos, len);
954 if (err)
955 return err;
956
957 return generic_file_splice_read(in, ppos, pipe, len, flags);
958 }
959
960 static void fuse_write_fill(struct fuse_req *req, struct fuse_file *ff,
961 loff_t pos, size_t count)
962 {
963 struct fuse_write_in *inarg = &req->misc.write.in;
964 struct fuse_write_out *outarg = &req->misc.write.out;
965
966 inarg->fh = ff->fh;
967 inarg->offset = pos;
968 inarg->size = count;
969 req->in.h.opcode = FUSE_WRITE;
970 req->in.h.nodeid = ff->nodeid;
971 req->in.numargs = 2;
972 if (ff->fc->minor < 9)
973 req->in.args[0].size = FUSE_COMPAT_WRITE_IN_SIZE;
974 else
975 req->in.args[0].size = sizeof(struct fuse_write_in);
976 req->in.args[0].value = inarg;
977 req->in.args[1].size = count;
978 req->out.numargs = 1;
979 req->out.args[0].size = sizeof(struct fuse_write_out);
980 req->out.args[0].value = outarg;
981 }
982
983 static size_t fuse_send_write(struct fuse_req *req, struct fuse_io_priv *io,
984 loff_t pos, size_t count, fl_owner_t owner)
985 {
986 struct file *file = io->file;
987 struct fuse_file *ff = file->private_data;
988 struct fuse_conn *fc = ff->fc;
989 struct fuse_write_in *inarg = &req->misc.write.in;
990
991 fuse_write_fill(req, ff, pos, count);
992 inarg->flags = file->f_flags;
993 if (owner != NULL) {
994 inarg->write_flags |= FUSE_WRITE_LOCKOWNER;
995 inarg->lock_owner = fuse_lock_owner_id(fc, owner);
996 }
997
998 if (io->async)
999 return fuse_async_req_send(fc, req, count, io);
1000
1001 fuse_request_send(fc, req);
1002 return req->misc.write.out.size;
1003 }
1004
1005 bool fuse_write_update_size(struct inode *inode, loff_t pos)
1006 {
1007 struct fuse_conn *fc = get_fuse_conn(inode);
1008 struct fuse_inode *fi = get_fuse_inode(inode);
1009 bool ret = false;
1010
1011 spin_lock(&fc->lock);
1012 fi->attr_version = ++fc->attr_version;
1013 if (pos > inode->i_size) {
1014 i_size_write(inode, pos);
1015 ret = true;
1016 }
1017 spin_unlock(&fc->lock);
1018
1019 return ret;
1020 }
1021
1022 static size_t fuse_send_write_pages(struct fuse_req *req, struct file *file,
1023 struct inode *inode, loff_t pos,
1024 size_t count)
1025 {
1026 size_t res;
1027 unsigned offset;
1028 unsigned i;
1029 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
1030
1031 for (i = 0; i < req->num_pages; i++)
1032 fuse_wait_on_page_writeback(inode, req->pages[i]->index);
1033
1034 res = fuse_send_write(req, &io, pos, count, NULL);
1035
1036 offset = req->page_descs[0].offset;
1037 count = res;
1038 for (i = 0; i < req->num_pages; i++) {
1039 struct page *page = req->pages[i];
1040
1041 if (!req->out.h.error && !offset && count >= PAGE_SIZE)
1042 SetPageUptodate(page);
1043
1044 if (count > PAGE_SIZE - offset)
1045 count -= PAGE_SIZE - offset;
1046 else
1047 count = 0;
1048 offset = 0;
1049
1050 unlock_page(page);
1051 put_page(page);
1052 }
1053
1054 return res;
1055 }
1056
1057 static ssize_t fuse_fill_write_pages(struct fuse_req *req,
1058 struct address_space *mapping,
1059 struct iov_iter *ii, loff_t pos)
1060 {
1061 struct fuse_conn *fc = get_fuse_conn(mapping->host);
1062 unsigned offset = pos & (PAGE_SIZE - 1);
1063 size_t count = 0;
1064 int err;
1065
1066 req->in.argpages = 1;
1067 req->page_descs[0].offset = offset;
1068
1069 do {
1070 size_t tmp;
1071 struct page *page;
1072 pgoff_t index = pos >> PAGE_SHIFT;
1073 size_t bytes = min_t(size_t, PAGE_SIZE - offset,
1074 iov_iter_count(ii));
1075
1076 bytes = min_t(size_t, bytes, fc->max_write - count);
1077
1078 again:
1079 err = -EFAULT;
1080 if (iov_iter_fault_in_readable(ii, bytes))
1081 break;
1082
1083 err = -ENOMEM;
1084 page = grab_cache_page_write_begin(mapping, index, 0);
1085 if (!page)
1086 break;
1087
1088 if (mapping_writably_mapped(mapping))
1089 flush_dcache_page(page);
1090
1091 tmp = iov_iter_copy_from_user_atomic(page, ii, offset, bytes);
1092 flush_dcache_page(page);
1093
1094 iov_iter_advance(ii, tmp);
1095 if (!tmp) {
1096 unlock_page(page);
1097 put_page(page);
1098 bytes = min(bytes, iov_iter_single_seg_count(ii));
1099 goto again;
1100 }
1101
1102 err = 0;
1103 req->pages[req->num_pages] = page;
1104 req->page_descs[req->num_pages].length = tmp;
1105 req->num_pages++;
1106
1107 count += tmp;
1108 pos += tmp;
1109 offset += tmp;
1110 if (offset == PAGE_SIZE)
1111 offset = 0;
1112
1113 if (!fc->big_writes)
1114 break;
1115 } while (iov_iter_count(ii) && count < fc->max_write &&
1116 req->num_pages < req->max_pages && offset == 0);
1117
1118 return count > 0 ? count : err;
1119 }
1120
1121 static inline unsigned fuse_wr_pages(loff_t pos, size_t len)
1122 {
1123 return min_t(unsigned,
1124 ((pos + len - 1) >> PAGE_SHIFT) -
1125 (pos >> PAGE_SHIFT) + 1,
1126 FUSE_MAX_PAGES_PER_REQ);
1127 }
1128
1129 static ssize_t fuse_perform_write(struct file *file,
1130 struct address_space *mapping,
1131 struct iov_iter *ii, loff_t pos)
1132 {
1133 struct inode *inode = mapping->host;
1134 struct fuse_conn *fc = get_fuse_conn(inode);
1135 struct fuse_inode *fi = get_fuse_inode(inode);
1136 int err = 0;
1137 ssize_t res = 0;
1138
1139 if (is_bad_inode(inode))
1140 return -EIO;
1141
1142 if (inode->i_size < pos + iov_iter_count(ii))
1143 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1144
1145 do {
1146 struct fuse_req *req;
1147 ssize_t count;
1148 unsigned nr_pages = fuse_wr_pages(pos, iov_iter_count(ii));
1149
1150 req = fuse_get_req(fc, nr_pages);
1151 if (IS_ERR(req)) {
1152 err = PTR_ERR(req);
1153 break;
1154 }
1155
1156 count = fuse_fill_write_pages(req, mapping, ii, pos);
1157 if (count <= 0) {
1158 err = count;
1159 } else {
1160 size_t num_written;
1161
1162 num_written = fuse_send_write_pages(req, file, inode,
1163 pos, count);
1164 err = req->out.h.error;
1165 if (!err) {
1166 res += num_written;
1167 pos += num_written;
1168
1169 /* break out of the loop on short write */
1170 if (num_written != count)
1171 err = -EIO;
1172 }
1173 }
1174 fuse_put_request(fc, req);
1175 } while (!err && iov_iter_count(ii));
1176
1177 if (res > 0)
1178 fuse_write_update_size(inode, pos);
1179
1180 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1181 fuse_invalidate_attr(inode);
1182
1183 return res > 0 ? res : err;
1184 }
1185
1186 static ssize_t fuse_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1187 {
1188 struct file *file = iocb->ki_filp;
1189 struct address_space *mapping = file->f_mapping;
1190 ssize_t written = 0;
1191 ssize_t written_buffered = 0;
1192 struct inode *inode = mapping->host;
1193 ssize_t err;
1194 loff_t endbyte = 0;
1195
1196 if (get_fuse_conn(inode)->writeback_cache) {
1197 /* Update size (EOF optimization) and mode (SUID clearing) */
1198 err = fuse_update_attributes(mapping->host, NULL, file, NULL);
1199 if (err)
1200 return err;
1201
1202 return generic_file_write_iter(iocb, from);
1203 }
1204
1205 inode_lock(inode);
1206
1207 /* We can write back this queue in page reclaim */
1208 current->backing_dev_info = inode_to_bdi(inode);
1209
1210 err = generic_write_checks(iocb, from);
1211 if (err <= 0)
1212 goto out;
1213
1214 err = file_remove_privs(file);
1215 if (err)
1216 goto out;
1217
1218 err = file_update_time(file);
1219 if (err)
1220 goto out;
1221
1222 if (iocb->ki_flags & IOCB_DIRECT) {
1223 loff_t pos = iocb->ki_pos;
1224 written = generic_file_direct_write(iocb, from);
1225 if (written < 0 || !iov_iter_count(from))
1226 goto out;
1227
1228 pos += written;
1229
1230 written_buffered = fuse_perform_write(file, mapping, from, pos);
1231 if (written_buffered < 0) {
1232 err = written_buffered;
1233 goto out;
1234 }
1235 endbyte = pos + written_buffered - 1;
1236
1237 err = filemap_write_and_wait_range(file->f_mapping, pos,
1238 endbyte);
1239 if (err)
1240 goto out;
1241
1242 invalidate_mapping_pages(file->f_mapping,
1243 pos >> PAGE_SHIFT,
1244 endbyte >> PAGE_SHIFT);
1245
1246 written += written_buffered;
1247 iocb->ki_pos = pos + written_buffered;
1248 } else {
1249 written = fuse_perform_write(file, mapping, from, iocb->ki_pos);
1250 if (written >= 0)
1251 iocb->ki_pos += written;
1252 }
1253 out:
1254 current->backing_dev_info = NULL;
1255 inode_unlock(inode);
1256
1257 return written ? written : err;
1258 }
1259
1260 static inline void fuse_page_descs_length_init(struct fuse_req *req,
1261 unsigned index, unsigned nr_pages)
1262 {
1263 int i;
1264
1265 for (i = index; i < index + nr_pages; i++)
1266 req->page_descs[i].length = PAGE_SIZE -
1267 req->page_descs[i].offset;
1268 }
1269
1270 static inline unsigned long fuse_get_user_addr(const struct iov_iter *ii)
1271 {
1272 return (unsigned long)ii->iov->iov_base + ii->iov_offset;
1273 }
1274
1275 static inline size_t fuse_get_frag_size(const struct iov_iter *ii,
1276 size_t max_size)
1277 {
1278 return min(iov_iter_single_seg_count(ii), max_size);
1279 }
1280
1281 static int fuse_get_user_pages(struct fuse_req *req, struct iov_iter *ii,
1282 size_t *nbytesp, int write)
1283 {
1284 size_t nbytes = 0; /* # bytes already packed in req */
1285 ssize_t ret = 0;
1286
1287 /* Special case for kernel I/O: can copy directly into the buffer */
1288 if (ii->type & ITER_KVEC) {
1289 unsigned long user_addr = fuse_get_user_addr(ii);
1290 size_t frag_size = fuse_get_frag_size(ii, *nbytesp);
1291
1292 if (write)
1293 req->in.args[1].value = (void *) user_addr;
1294 else
1295 req->out.args[0].value = (void *) user_addr;
1296
1297 iov_iter_advance(ii, frag_size);
1298 *nbytesp = frag_size;
1299 return 0;
1300 }
1301
1302 while (nbytes < *nbytesp && req->num_pages < req->max_pages) {
1303 unsigned npages;
1304 size_t start;
1305 ret = iov_iter_get_pages(ii, &req->pages[req->num_pages],
1306 *nbytesp - nbytes,
1307 req->max_pages - req->num_pages,
1308 &start);
1309 if (ret < 0)
1310 break;
1311
1312 iov_iter_advance(ii, ret);
1313 nbytes += ret;
1314
1315 ret += start;
1316 npages = (ret + PAGE_SIZE - 1) / PAGE_SIZE;
1317
1318 req->page_descs[req->num_pages].offset = start;
1319 fuse_page_descs_length_init(req, req->num_pages, npages);
1320
1321 req->num_pages += npages;
1322 req->page_descs[req->num_pages - 1].length -=
1323 (PAGE_SIZE - ret) & (PAGE_SIZE - 1);
1324 }
1325
1326 if (write)
1327 req->in.argpages = 1;
1328 else
1329 req->out.argpages = 1;
1330
1331 *nbytesp = nbytes;
1332
1333 return ret < 0 ? ret : 0;
1334 }
1335
1336 static inline int fuse_iter_npages(const struct iov_iter *ii_p)
1337 {
1338 return iov_iter_npages(ii_p, FUSE_MAX_PAGES_PER_REQ);
1339 }
1340
1341 ssize_t fuse_direct_io(struct fuse_io_priv *io, struct iov_iter *iter,
1342 loff_t *ppos, int flags)
1343 {
1344 int write = flags & FUSE_DIO_WRITE;
1345 bool should_dirty = !write && iter_is_iovec(iter);
1346 int cuse = flags & FUSE_DIO_CUSE;
1347 struct file *file = io->file;
1348 struct inode *inode = file->f_mapping->host;
1349 struct fuse_file *ff = file->private_data;
1350 struct fuse_conn *fc = ff->fc;
1351 size_t nmax = write ? fc->max_write : fc->max_read;
1352 loff_t pos = *ppos;
1353 size_t count = iov_iter_count(iter);
1354 pgoff_t idx_from = pos >> PAGE_SHIFT;
1355 pgoff_t idx_to = (pos + count - 1) >> PAGE_SHIFT;
1356 ssize_t res = 0;
1357 struct fuse_req *req;
1358 int err = 0;
1359
1360 if (io->async)
1361 req = fuse_get_req_for_background(fc, fuse_iter_npages(iter));
1362 else
1363 req = fuse_get_req(fc, fuse_iter_npages(iter));
1364 if (IS_ERR(req))
1365 return PTR_ERR(req);
1366
1367 if (!cuse && fuse_range_is_writeback(inode, idx_from, idx_to)) {
1368 if (!write)
1369 inode_lock(inode);
1370 fuse_sync_writes(inode);
1371 if (!write)
1372 inode_unlock(inode);
1373 }
1374
1375 while (count) {
1376 size_t nres;
1377 fl_owner_t owner = current->files;
1378 size_t nbytes = min(count, nmax);
1379 err = fuse_get_user_pages(req, iter, &nbytes, write);
1380 if (err && !nbytes)
1381 break;
1382
1383 if (write)
1384 nres = fuse_send_write(req, io, pos, nbytes, owner);
1385 else
1386 nres = fuse_send_read(req, io, pos, nbytes, owner);
1387
1388 if (!io->async)
1389 fuse_release_user_pages(req, should_dirty);
1390 if (req->out.h.error) {
1391 err = req->out.h.error;
1392 break;
1393 } else if (nres > nbytes) {
1394 res = 0;
1395 err = -EIO;
1396 break;
1397 }
1398 count -= nres;
1399 res += nres;
1400 pos += nres;
1401 if (nres != nbytes)
1402 break;
1403 if (count) {
1404 fuse_put_request(fc, req);
1405 if (io->async)
1406 req = fuse_get_req_for_background(fc,
1407 fuse_iter_npages(iter));
1408 else
1409 req = fuse_get_req(fc, fuse_iter_npages(iter));
1410 if (IS_ERR(req))
1411 break;
1412 }
1413 }
1414 if (!IS_ERR(req))
1415 fuse_put_request(fc, req);
1416 if (res > 0)
1417 *ppos = pos;
1418
1419 return res > 0 ? res : err;
1420 }
1421 EXPORT_SYMBOL_GPL(fuse_direct_io);
1422
1423 static ssize_t __fuse_direct_read(struct fuse_io_priv *io,
1424 struct iov_iter *iter,
1425 loff_t *ppos)
1426 {
1427 ssize_t res;
1428 struct file *file = io->file;
1429 struct inode *inode = file_inode(file);
1430
1431 if (is_bad_inode(inode))
1432 return -EIO;
1433
1434 res = fuse_direct_io(io, iter, ppos, 0);
1435
1436 fuse_invalidate_attr(inode);
1437
1438 return res;
1439 }
1440
1441 static ssize_t fuse_direct_read_iter(struct kiocb *iocb, struct iov_iter *to)
1442 {
1443 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(iocb->ki_filp);
1444 return __fuse_direct_read(&io, to, &iocb->ki_pos);
1445 }
1446
1447 static ssize_t fuse_direct_write_iter(struct kiocb *iocb, struct iov_iter *from)
1448 {
1449 struct file *file = iocb->ki_filp;
1450 struct inode *inode = file_inode(file);
1451 struct fuse_io_priv io = FUSE_IO_PRIV_SYNC(file);
1452 ssize_t res;
1453
1454 if (is_bad_inode(inode))
1455 return -EIO;
1456
1457 /* Don't allow parallel writes to the same file */
1458 inode_lock(inode);
1459 res = generic_write_checks(iocb, from);
1460 if (res > 0)
1461 res = fuse_direct_io(&io, from, &iocb->ki_pos, FUSE_DIO_WRITE);
1462 fuse_invalidate_attr(inode);
1463 if (res > 0)
1464 fuse_write_update_size(inode, iocb->ki_pos);
1465 inode_unlock(inode);
1466
1467 return res;
1468 }
1469
1470 static void fuse_writepage_free(struct fuse_conn *fc, struct fuse_req *req)
1471 {
1472 int i;
1473
1474 for (i = 0; i < req->num_pages; i++)
1475 __free_page(req->pages[i]);
1476
1477 if (req->ff)
1478 fuse_file_put(req->ff, false);
1479 }
1480
1481 static void fuse_writepage_finish(struct fuse_conn *fc, struct fuse_req *req)
1482 {
1483 struct inode *inode = req->inode;
1484 struct fuse_inode *fi = get_fuse_inode(inode);
1485 struct backing_dev_info *bdi = inode_to_bdi(inode);
1486 int i;
1487
1488 list_del(&req->writepages_entry);
1489 for (i = 0; i < req->num_pages; i++) {
1490 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1491 dec_node_page_state(req->pages[i], NR_WRITEBACK_TEMP);
1492 wb_writeout_inc(&bdi->wb);
1493 }
1494 wake_up(&fi->page_waitq);
1495 }
1496
1497 /* Called under fc->lock, may release and reacquire it */
1498 static void fuse_send_writepage(struct fuse_conn *fc, struct fuse_req *req,
1499 loff_t size)
1500 __releases(fc->lock)
1501 __acquires(fc->lock)
1502 {
1503 struct fuse_inode *fi = get_fuse_inode(req->inode);
1504 struct fuse_write_in *inarg = &req->misc.write.in;
1505 __u64 data_size = req->num_pages * PAGE_SIZE;
1506
1507 if (!fc->connected)
1508 goto out_free;
1509
1510 if (inarg->offset + data_size <= size) {
1511 inarg->size = data_size;
1512 } else if (inarg->offset < size) {
1513 inarg->size = size - inarg->offset;
1514 } else {
1515 /* Got truncated off completely */
1516 goto out_free;
1517 }
1518
1519 req->in.args[1].size = inarg->size;
1520 fi->writectr++;
1521 fuse_request_send_background_locked(fc, req);
1522 return;
1523
1524 out_free:
1525 fuse_writepage_finish(fc, req);
1526 spin_unlock(&fc->lock);
1527 fuse_writepage_free(fc, req);
1528 fuse_put_request(fc, req);
1529 spin_lock(&fc->lock);
1530 }
1531
1532 /*
1533 * If fi->writectr is positive (no truncate or fsync going on) send
1534 * all queued writepage requests.
1535 *
1536 * Called with fc->lock
1537 */
1538 void fuse_flush_writepages(struct inode *inode)
1539 __releases(fc->lock)
1540 __acquires(fc->lock)
1541 {
1542 struct fuse_conn *fc = get_fuse_conn(inode);
1543 struct fuse_inode *fi = get_fuse_inode(inode);
1544 size_t crop = i_size_read(inode);
1545 struct fuse_req *req;
1546
1547 while (fi->writectr >= 0 && !list_empty(&fi->queued_writes)) {
1548 req = list_entry(fi->queued_writes.next, struct fuse_req, list);
1549 list_del_init(&req->list);
1550 fuse_send_writepage(fc, req, crop);
1551 }
1552 }
1553
1554 static void fuse_writepage_end(struct fuse_conn *fc, struct fuse_req *req)
1555 {
1556 struct inode *inode = req->inode;
1557 struct fuse_inode *fi = get_fuse_inode(inode);
1558
1559 mapping_set_error(inode->i_mapping, req->out.h.error);
1560 spin_lock(&fc->lock);
1561 while (req->misc.write.next) {
1562 struct fuse_conn *fc = get_fuse_conn(inode);
1563 struct fuse_write_in *inarg = &req->misc.write.in;
1564 struct fuse_req *next = req->misc.write.next;
1565 req->misc.write.next = next->misc.write.next;
1566 next->misc.write.next = NULL;
1567 next->ff = fuse_file_get(req->ff);
1568 list_add(&next->writepages_entry, &fi->writepages);
1569
1570 /*
1571 * Skip fuse_flush_writepages() to make it easy to crop requests
1572 * based on primary request size.
1573 *
1574 * 1st case (trivial): there are no concurrent activities using
1575 * fuse_set/release_nowrite. Then we're on safe side because
1576 * fuse_flush_writepages() would call fuse_send_writepage()
1577 * anyway.
1578 *
1579 * 2nd case: someone called fuse_set_nowrite and it is waiting
1580 * now for completion of all in-flight requests. This happens
1581 * rarely and no more than once per page, so this should be
1582 * okay.
1583 *
1584 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1585 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1586 * that fuse_set_nowrite returned implies that all in-flight
1587 * requests were completed along with all of their secondary
1588 * requests. Further primary requests are blocked by negative
1589 * writectr. Hence there cannot be any in-flight requests and
1590 * no invocations of fuse_writepage_end() while we're in
1591 * fuse_set_nowrite..fuse_release_nowrite section.
1592 */
1593 fuse_send_writepage(fc, next, inarg->offset + inarg->size);
1594 }
1595 fi->writectr--;
1596 fuse_writepage_finish(fc, req);
1597 spin_unlock(&fc->lock);
1598 fuse_writepage_free(fc, req);
1599 }
1600
1601 static struct fuse_file *__fuse_write_file_get(struct fuse_conn *fc,
1602 struct fuse_inode *fi)
1603 {
1604 struct fuse_file *ff = NULL;
1605
1606 spin_lock(&fc->lock);
1607 if (!list_empty(&fi->write_files)) {
1608 ff = list_entry(fi->write_files.next, struct fuse_file,
1609 write_entry);
1610 fuse_file_get(ff);
1611 }
1612 spin_unlock(&fc->lock);
1613
1614 return ff;
1615 }
1616
1617 static struct fuse_file *fuse_write_file_get(struct fuse_conn *fc,
1618 struct fuse_inode *fi)
1619 {
1620 struct fuse_file *ff = __fuse_write_file_get(fc, fi);
1621 WARN_ON(!ff);
1622 return ff;
1623 }
1624
1625 int fuse_write_inode(struct inode *inode, struct writeback_control *wbc)
1626 {
1627 struct fuse_conn *fc = get_fuse_conn(inode);
1628 struct fuse_inode *fi = get_fuse_inode(inode);
1629 struct fuse_file *ff;
1630 int err;
1631
1632 ff = __fuse_write_file_get(fc, fi);
1633 err = fuse_flush_times(inode, ff);
1634 if (ff)
1635 fuse_file_put(ff, 0);
1636
1637 return err;
1638 }
1639
1640 static int fuse_writepage_locked(struct page *page)
1641 {
1642 struct address_space *mapping = page->mapping;
1643 struct inode *inode = mapping->host;
1644 struct fuse_conn *fc = get_fuse_conn(inode);
1645 struct fuse_inode *fi = get_fuse_inode(inode);
1646 struct fuse_req *req;
1647 struct page *tmp_page;
1648 int error = -ENOMEM;
1649
1650 set_page_writeback(page);
1651
1652 req = fuse_request_alloc_nofs(1);
1653 if (!req)
1654 goto err;
1655
1656 /* writeback always goes to bg_queue */
1657 __set_bit(FR_BACKGROUND, &req->flags);
1658 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1659 if (!tmp_page)
1660 goto err_free;
1661
1662 error = -EIO;
1663 req->ff = fuse_write_file_get(fc, fi);
1664 if (!req->ff)
1665 goto err_nofile;
1666
1667 fuse_write_fill(req, req->ff, page_offset(page), 0);
1668
1669 copy_highpage(tmp_page, page);
1670 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1671 req->misc.write.next = NULL;
1672 req->in.argpages = 1;
1673 req->num_pages = 1;
1674 req->pages[0] = tmp_page;
1675 req->page_descs[0].offset = 0;
1676 req->page_descs[0].length = PAGE_SIZE;
1677 req->end = fuse_writepage_end;
1678 req->inode = inode;
1679
1680 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1681 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1682
1683 spin_lock(&fc->lock);
1684 list_add(&req->writepages_entry, &fi->writepages);
1685 list_add_tail(&req->list, &fi->queued_writes);
1686 fuse_flush_writepages(inode);
1687 spin_unlock(&fc->lock);
1688
1689 end_page_writeback(page);
1690
1691 return 0;
1692
1693 err_nofile:
1694 __free_page(tmp_page);
1695 err_free:
1696 fuse_request_free(req);
1697 err:
1698 end_page_writeback(page);
1699 return error;
1700 }
1701
1702 static int fuse_writepage(struct page *page, struct writeback_control *wbc)
1703 {
1704 int err;
1705
1706 if (fuse_page_is_writeback(page->mapping->host, page->index)) {
1707 /*
1708 * ->writepages() should be called for sync() and friends. We
1709 * should only get here on direct reclaim and then we are
1710 * allowed to skip a page which is already in flight
1711 */
1712 WARN_ON(wbc->sync_mode == WB_SYNC_ALL);
1713
1714 redirty_page_for_writepage(wbc, page);
1715 return 0;
1716 }
1717
1718 err = fuse_writepage_locked(page);
1719 unlock_page(page);
1720
1721 return err;
1722 }
1723
1724 struct fuse_fill_wb_data {
1725 struct fuse_req *req;
1726 struct fuse_file *ff;
1727 struct inode *inode;
1728 struct page **orig_pages;
1729 };
1730
1731 static void fuse_writepages_send(struct fuse_fill_wb_data *data)
1732 {
1733 struct fuse_req *req = data->req;
1734 struct inode *inode = data->inode;
1735 struct fuse_conn *fc = get_fuse_conn(inode);
1736 struct fuse_inode *fi = get_fuse_inode(inode);
1737 int num_pages = req->num_pages;
1738 int i;
1739
1740 req->ff = fuse_file_get(data->ff);
1741 spin_lock(&fc->lock);
1742 list_add_tail(&req->list, &fi->queued_writes);
1743 fuse_flush_writepages(inode);
1744 spin_unlock(&fc->lock);
1745
1746 for (i = 0; i < num_pages; i++)
1747 end_page_writeback(data->orig_pages[i]);
1748 }
1749
1750 static bool fuse_writepage_in_flight(struct fuse_req *new_req,
1751 struct page *page)
1752 {
1753 struct fuse_conn *fc = get_fuse_conn(new_req->inode);
1754 struct fuse_inode *fi = get_fuse_inode(new_req->inode);
1755 struct fuse_req *tmp;
1756 struct fuse_req *old_req;
1757 bool found = false;
1758 pgoff_t curr_index;
1759
1760 BUG_ON(new_req->num_pages != 0);
1761
1762 spin_lock(&fc->lock);
1763 list_del(&new_req->writepages_entry);
1764 list_for_each_entry(old_req, &fi->writepages, writepages_entry) {
1765 BUG_ON(old_req->inode != new_req->inode);
1766 curr_index = old_req->misc.write.in.offset >> PAGE_SHIFT;
1767 if (curr_index <= page->index &&
1768 page->index < curr_index + old_req->num_pages) {
1769 found = true;
1770 break;
1771 }
1772 }
1773 if (!found) {
1774 list_add(&new_req->writepages_entry, &fi->writepages);
1775 goto out_unlock;
1776 }
1777
1778 new_req->num_pages = 1;
1779 for (tmp = old_req; tmp != NULL; tmp = tmp->misc.write.next) {
1780 BUG_ON(tmp->inode != new_req->inode);
1781 curr_index = tmp->misc.write.in.offset >> PAGE_SHIFT;
1782 if (tmp->num_pages == 1 &&
1783 curr_index == page->index) {
1784 old_req = tmp;
1785 }
1786 }
1787
1788 if (old_req->num_pages == 1 && test_bit(FR_PENDING, &old_req->flags)) {
1789 struct backing_dev_info *bdi = inode_to_bdi(page->mapping->host);
1790
1791 copy_highpage(old_req->pages[0], page);
1792 spin_unlock(&fc->lock);
1793
1794 dec_wb_stat(&bdi->wb, WB_WRITEBACK);
1795 dec_node_page_state(page, NR_WRITEBACK_TEMP);
1796 wb_writeout_inc(&bdi->wb);
1797 fuse_writepage_free(fc, new_req);
1798 fuse_request_free(new_req);
1799 goto out;
1800 } else {
1801 new_req->misc.write.next = old_req->misc.write.next;
1802 old_req->misc.write.next = new_req;
1803 }
1804 out_unlock:
1805 spin_unlock(&fc->lock);
1806 out:
1807 return found;
1808 }
1809
1810 static int fuse_writepages_fill(struct page *page,
1811 struct writeback_control *wbc, void *_data)
1812 {
1813 struct fuse_fill_wb_data *data = _data;
1814 struct fuse_req *req = data->req;
1815 struct inode *inode = data->inode;
1816 struct fuse_conn *fc = get_fuse_conn(inode);
1817 struct page *tmp_page;
1818 bool is_writeback;
1819 int err;
1820
1821 if (!data->ff) {
1822 err = -EIO;
1823 data->ff = fuse_write_file_get(fc, get_fuse_inode(inode));
1824 if (!data->ff)
1825 goto out_unlock;
1826 }
1827
1828 /*
1829 * Being under writeback is unlikely but possible. For example direct
1830 * read to an mmaped fuse file will set the page dirty twice; once when
1831 * the pages are faulted with get_user_pages(), and then after the read
1832 * completed.
1833 */
1834 is_writeback = fuse_page_is_writeback(inode, page->index);
1835
1836 if (req && req->num_pages &&
1837 (is_writeback || req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
1838 (req->num_pages + 1) * PAGE_SIZE > fc->max_write ||
1839 data->orig_pages[req->num_pages - 1]->index + 1 != page->index)) {
1840 fuse_writepages_send(data);
1841 data->req = NULL;
1842 }
1843 err = -ENOMEM;
1844 tmp_page = alloc_page(GFP_NOFS | __GFP_HIGHMEM);
1845 if (!tmp_page)
1846 goto out_unlock;
1847
1848 /*
1849 * The page must not be redirtied until the writeout is completed
1850 * (i.e. userspace has sent a reply to the write request). Otherwise
1851 * there could be more than one temporary page instance for each real
1852 * page.
1853 *
1854 * This is ensured by holding the page lock in page_mkwrite() while
1855 * checking fuse_page_is_writeback(). We already hold the page lock
1856 * since clear_page_dirty_for_io() and keep it held until we add the
1857 * request to the fi->writepages list and increment req->num_pages.
1858 * After this fuse_page_is_writeback() will indicate that the page is
1859 * under writeback, so we can release the page lock.
1860 */
1861 if (data->req == NULL) {
1862 struct fuse_inode *fi = get_fuse_inode(inode);
1863
1864 err = -ENOMEM;
1865 req = fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ);
1866 if (!req) {
1867 __free_page(tmp_page);
1868 goto out_unlock;
1869 }
1870
1871 fuse_write_fill(req, data->ff, page_offset(page), 0);
1872 req->misc.write.in.write_flags |= FUSE_WRITE_CACHE;
1873 req->misc.write.next = NULL;
1874 req->in.argpages = 1;
1875 __set_bit(FR_BACKGROUND, &req->flags);
1876 req->num_pages = 0;
1877 req->end = fuse_writepage_end;
1878 req->inode = inode;
1879
1880 spin_lock(&fc->lock);
1881 list_add(&req->writepages_entry, &fi->writepages);
1882 spin_unlock(&fc->lock);
1883
1884 data->req = req;
1885 }
1886 set_page_writeback(page);
1887
1888 copy_highpage(tmp_page, page);
1889 req->pages[req->num_pages] = tmp_page;
1890 req->page_descs[req->num_pages].offset = 0;
1891 req->page_descs[req->num_pages].length = PAGE_SIZE;
1892
1893 inc_wb_stat(&inode_to_bdi(inode)->wb, WB_WRITEBACK);
1894 inc_node_page_state(tmp_page, NR_WRITEBACK_TEMP);
1895
1896 err = 0;
1897 if (is_writeback && fuse_writepage_in_flight(req, page)) {
1898 end_page_writeback(page);
1899 data->req = NULL;
1900 goto out_unlock;
1901 }
1902 data->orig_pages[req->num_pages] = page;
1903
1904 /*
1905 * Protected by fc->lock against concurrent access by
1906 * fuse_page_is_writeback().
1907 */
1908 spin_lock(&fc->lock);
1909 req->num_pages++;
1910 spin_unlock(&fc->lock);
1911
1912 out_unlock:
1913 unlock_page(page);
1914
1915 return err;
1916 }
1917
1918 static int fuse_writepages(struct address_space *mapping,
1919 struct writeback_control *wbc)
1920 {
1921 struct inode *inode = mapping->host;
1922 struct fuse_fill_wb_data data;
1923 int err;
1924
1925 err = -EIO;
1926 if (is_bad_inode(inode))
1927 goto out;
1928
1929 data.inode = inode;
1930 data.req = NULL;
1931 data.ff = NULL;
1932
1933 err = -ENOMEM;
1934 data.orig_pages = kcalloc(FUSE_MAX_PAGES_PER_REQ,
1935 sizeof(struct page *),
1936 GFP_NOFS);
1937 if (!data.orig_pages)
1938 goto out;
1939
1940 err = write_cache_pages(mapping, wbc, fuse_writepages_fill, &data);
1941 if (data.req) {
1942 /* Ignore errors if we can write at least one page */
1943 BUG_ON(!data.req->num_pages);
1944 fuse_writepages_send(&data);
1945 err = 0;
1946 }
1947 if (data.ff)
1948 fuse_file_put(data.ff, false);
1949
1950 kfree(data.orig_pages);
1951 out:
1952 return err;
1953 }
1954
1955 /*
1956 * It's worthy to make sure that space is reserved on disk for the write,
1957 * but how to implement it without killing performance need more thinking.
1958 */
1959 static int fuse_write_begin(struct file *file, struct address_space *mapping,
1960 loff_t pos, unsigned len, unsigned flags,
1961 struct page **pagep, void **fsdata)
1962 {
1963 pgoff_t index = pos >> PAGE_SHIFT;
1964 struct fuse_conn *fc = get_fuse_conn(file_inode(file));
1965 struct page *page;
1966 loff_t fsize;
1967 int err = -ENOMEM;
1968
1969 WARN_ON(!fc->writeback_cache);
1970
1971 page = grab_cache_page_write_begin(mapping, index, flags);
1972 if (!page)
1973 goto error;
1974
1975 fuse_wait_on_page_writeback(mapping->host, page->index);
1976
1977 if (PageUptodate(page) || len == PAGE_SIZE)
1978 goto success;
1979 /*
1980 * Check if the start this page comes after the end of file, in which
1981 * case the readpage can be optimized away.
1982 */
1983 fsize = i_size_read(mapping->host);
1984 if (fsize <= (pos & PAGE_MASK)) {
1985 size_t off = pos & ~PAGE_MASK;
1986 if (off)
1987 zero_user_segment(page, 0, off);
1988 goto success;
1989 }
1990 err = fuse_do_readpage(file, page);
1991 if (err)
1992 goto cleanup;
1993 success:
1994 *pagep = page;
1995 return 0;
1996
1997 cleanup:
1998 unlock_page(page);
1999 put_page(page);
2000 error:
2001 return err;
2002 }
2003
2004 static int fuse_write_end(struct file *file, struct address_space *mapping,
2005 loff_t pos, unsigned len, unsigned copied,
2006 struct page *page, void *fsdata)
2007 {
2008 struct inode *inode = page->mapping->host;
2009
2010 if (!PageUptodate(page)) {
2011 /* Zero any unwritten bytes at the end of the page */
2012 size_t endoff = (pos + copied) & ~PAGE_MASK;
2013 if (endoff)
2014 zero_user_segment(page, endoff, PAGE_SIZE);
2015 SetPageUptodate(page);
2016 }
2017
2018 fuse_write_update_size(inode, pos + copied);
2019 set_page_dirty(page);
2020 unlock_page(page);
2021 put_page(page);
2022
2023 return copied;
2024 }
2025
2026 static int fuse_launder_page(struct page *page)
2027 {
2028 int err = 0;
2029 if (clear_page_dirty_for_io(page)) {
2030 struct inode *inode = page->mapping->host;
2031 err = fuse_writepage_locked(page);
2032 if (!err)
2033 fuse_wait_on_page_writeback(inode, page->index);
2034 }
2035 return err;
2036 }
2037
2038 /*
2039 * Write back dirty pages now, because there may not be any suitable
2040 * open files later
2041 */
2042 static void fuse_vma_close(struct vm_area_struct *vma)
2043 {
2044 filemap_write_and_wait(vma->vm_file->f_mapping);
2045 }
2046
2047 /*
2048 * Wait for writeback against this page to complete before allowing it
2049 * to be marked dirty again, and hence written back again, possibly
2050 * before the previous writepage completed.
2051 *
2052 * Block here, instead of in ->writepage(), so that the userspace fs
2053 * can only block processes actually operating on the filesystem.
2054 *
2055 * Otherwise unprivileged userspace fs would be able to block
2056 * unrelated:
2057 *
2058 * - page migration
2059 * - sync(2)
2060 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2061 */
2062 static int fuse_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
2063 {
2064 struct page *page = vmf->page;
2065 struct inode *inode = file_inode(vma->vm_file);
2066
2067 file_update_time(vma->vm_file);
2068 lock_page(page);
2069 if (page->mapping != inode->i_mapping) {
2070 unlock_page(page);
2071 return VM_FAULT_NOPAGE;
2072 }
2073
2074 fuse_wait_on_page_writeback(inode, page->index);
2075 return VM_FAULT_LOCKED;
2076 }
2077
2078 static const struct vm_operations_struct fuse_file_vm_ops = {
2079 .close = fuse_vma_close,
2080 .fault = filemap_fault,
2081 .map_pages = filemap_map_pages,
2082 .page_mkwrite = fuse_page_mkwrite,
2083 };
2084
2085 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
2086 {
2087 if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE))
2088 fuse_link_write_file(file);
2089
2090 file_accessed(file);
2091 vma->vm_ops = &fuse_file_vm_ops;
2092 return 0;
2093 }
2094
2095 static int fuse_direct_mmap(struct file *file, struct vm_area_struct *vma)
2096 {
2097 /* Can't provide the coherency needed for MAP_SHARED */
2098 if (vma->vm_flags & VM_MAYSHARE)
2099 return -ENODEV;
2100
2101 invalidate_inode_pages2(file->f_mapping);
2102
2103 return generic_file_mmap(file, vma);
2104 }
2105
2106 static int convert_fuse_file_lock(const struct fuse_file_lock *ffl,
2107 struct file_lock *fl)
2108 {
2109 switch (ffl->type) {
2110 case F_UNLCK:
2111 break;
2112
2113 case F_RDLCK:
2114 case F_WRLCK:
2115 if (ffl->start > OFFSET_MAX || ffl->end > OFFSET_MAX ||
2116 ffl->end < ffl->start)
2117 return -EIO;
2118
2119 fl->fl_start = ffl->start;
2120 fl->fl_end = ffl->end;
2121 fl->fl_pid = ffl->pid;
2122 break;
2123
2124 default:
2125 return -EIO;
2126 }
2127 fl->fl_type = ffl->type;
2128 return 0;
2129 }
2130
2131 static void fuse_lk_fill(struct fuse_args *args, struct file *file,
2132 const struct file_lock *fl, int opcode, pid_t pid,
2133 int flock, struct fuse_lk_in *inarg)
2134 {
2135 struct inode *inode = file_inode(file);
2136 struct fuse_conn *fc = get_fuse_conn(inode);
2137 struct fuse_file *ff = file->private_data;
2138
2139 memset(inarg, 0, sizeof(*inarg));
2140 inarg->fh = ff->fh;
2141 inarg->owner = fuse_lock_owner_id(fc, fl->fl_owner);
2142 inarg->lk.start = fl->fl_start;
2143 inarg->lk.end = fl->fl_end;
2144 inarg->lk.type = fl->fl_type;
2145 inarg->lk.pid = pid;
2146 if (flock)
2147 inarg->lk_flags |= FUSE_LK_FLOCK;
2148 args->in.h.opcode = opcode;
2149 args->in.h.nodeid = get_node_id(inode);
2150 args->in.numargs = 1;
2151 args->in.args[0].size = sizeof(*inarg);
2152 args->in.args[0].value = inarg;
2153 }
2154
2155 static int fuse_getlk(struct file *file, struct file_lock *fl)
2156 {
2157 struct inode *inode = file_inode(file);
2158 struct fuse_conn *fc = get_fuse_conn(inode);
2159 FUSE_ARGS(args);
2160 struct fuse_lk_in inarg;
2161 struct fuse_lk_out outarg;
2162 int err;
2163
2164 fuse_lk_fill(&args, file, fl, FUSE_GETLK, 0, 0, &inarg);
2165 args.out.numargs = 1;
2166 args.out.args[0].size = sizeof(outarg);
2167 args.out.args[0].value = &outarg;
2168 err = fuse_simple_request(fc, &args);
2169 if (!err)
2170 err = convert_fuse_file_lock(&outarg.lk, fl);
2171
2172 return err;
2173 }
2174
2175 static int fuse_setlk(struct file *file, struct file_lock *fl, int flock)
2176 {
2177 struct inode *inode = file_inode(file);
2178 struct fuse_conn *fc = get_fuse_conn(inode);
2179 FUSE_ARGS(args);
2180 struct fuse_lk_in inarg;
2181 int opcode = (fl->fl_flags & FL_SLEEP) ? FUSE_SETLKW : FUSE_SETLK;
2182 pid_t pid = fl->fl_type != F_UNLCK ? current->tgid : 0;
2183 int err;
2184
2185 if (fl->fl_lmops && fl->fl_lmops->lm_grant) {
2186 /* NLM needs asynchronous locks, which we don't support yet */
2187 return -ENOLCK;
2188 }
2189
2190 /* Unlock on close is handled by the flush method */
2191 if (fl->fl_flags & FL_CLOSE)
2192 return 0;
2193
2194 fuse_lk_fill(&args, file, fl, opcode, pid, flock, &inarg);
2195 err = fuse_simple_request(fc, &args);
2196
2197 /* locking is restartable */
2198 if (err == -EINTR)
2199 err = -ERESTARTSYS;
2200
2201 return err;
2202 }
2203
2204 static int fuse_file_lock(struct file *file, int cmd, struct file_lock *fl)
2205 {
2206 struct inode *inode = file_inode(file);
2207 struct fuse_conn *fc = get_fuse_conn(inode);
2208 int err;
2209
2210 if (cmd == F_CANCELLK) {
2211 err = 0;
2212 } else if (cmd == F_GETLK) {
2213 if (fc->no_lock) {
2214 posix_test_lock(file, fl);
2215 err = 0;
2216 } else
2217 err = fuse_getlk(file, fl);
2218 } else {
2219 if (fc->no_lock)
2220 err = posix_lock_file(file, fl, NULL);
2221 else
2222 err = fuse_setlk(file, fl, 0);
2223 }
2224 return err;
2225 }
2226
2227 static int fuse_file_flock(struct file *file, int cmd, struct file_lock *fl)
2228 {
2229 struct inode *inode = file_inode(file);
2230 struct fuse_conn *fc = get_fuse_conn(inode);
2231 int err;
2232
2233 if (fc->no_flock) {
2234 err = locks_lock_file_wait(file, fl);
2235 } else {
2236 struct fuse_file *ff = file->private_data;
2237
2238 /* emulate flock with POSIX locks */
2239 ff->flock = true;
2240 err = fuse_setlk(file, fl, 1);
2241 }
2242
2243 return err;
2244 }
2245
2246 static sector_t fuse_bmap(struct address_space *mapping, sector_t block)
2247 {
2248 struct inode *inode = mapping->host;
2249 struct fuse_conn *fc = get_fuse_conn(inode);
2250 FUSE_ARGS(args);
2251 struct fuse_bmap_in inarg;
2252 struct fuse_bmap_out outarg;
2253 int err;
2254
2255 if (!inode->i_sb->s_bdev || fc->no_bmap)
2256 return 0;
2257
2258 memset(&inarg, 0, sizeof(inarg));
2259 inarg.block = block;
2260 inarg.blocksize = inode->i_sb->s_blocksize;
2261 args.in.h.opcode = FUSE_BMAP;
2262 args.in.h.nodeid = get_node_id(inode);
2263 args.in.numargs = 1;
2264 args.in.args[0].size = sizeof(inarg);
2265 args.in.args[0].value = &inarg;
2266 args.out.numargs = 1;
2267 args.out.args[0].size = sizeof(outarg);
2268 args.out.args[0].value = &outarg;
2269 err = fuse_simple_request(fc, &args);
2270 if (err == -ENOSYS)
2271 fc->no_bmap = 1;
2272
2273 return err ? 0 : outarg.block;
2274 }
2275
2276 static loff_t fuse_lseek(struct file *file, loff_t offset, int whence)
2277 {
2278 struct inode *inode = file->f_mapping->host;
2279 struct fuse_conn *fc = get_fuse_conn(inode);
2280 struct fuse_file *ff = file->private_data;
2281 FUSE_ARGS(args);
2282 struct fuse_lseek_in inarg = {
2283 .fh = ff->fh,
2284 .offset = offset,
2285 .whence = whence
2286 };
2287 struct fuse_lseek_out outarg;
2288 int err;
2289
2290 if (fc->no_lseek)
2291 goto fallback;
2292
2293 args.in.h.opcode = FUSE_LSEEK;
2294 args.in.h.nodeid = ff->nodeid;
2295 args.in.numargs = 1;
2296 args.in.args[0].size = sizeof(inarg);
2297 args.in.args[0].value = &inarg;
2298 args.out.numargs = 1;
2299 args.out.args[0].size = sizeof(outarg);
2300 args.out.args[0].value = &outarg;
2301 err = fuse_simple_request(fc, &args);
2302 if (err) {
2303 if (err == -ENOSYS) {
2304 fc->no_lseek = 1;
2305 goto fallback;
2306 }
2307 return err;
2308 }
2309
2310 return vfs_setpos(file, outarg.offset, inode->i_sb->s_maxbytes);
2311
2312 fallback:
2313 err = fuse_update_attributes(inode, NULL, file, NULL);
2314 if (!err)
2315 return generic_file_llseek(file, offset, whence);
2316 else
2317 return err;
2318 }
2319
2320 static loff_t fuse_file_llseek(struct file *file, loff_t offset, int whence)
2321 {
2322 loff_t retval;
2323 struct inode *inode = file_inode(file);
2324
2325 switch (whence) {
2326 case SEEK_SET:
2327 case SEEK_CUR:
2328 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2329 retval = generic_file_llseek(file, offset, whence);
2330 break;
2331 case SEEK_END:
2332 inode_lock(inode);
2333 retval = fuse_update_attributes(inode, NULL, file, NULL);
2334 if (!retval)
2335 retval = generic_file_llseek(file, offset, whence);
2336 inode_unlock(inode);
2337 break;
2338 case SEEK_HOLE:
2339 case SEEK_DATA:
2340 inode_lock(inode);
2341 retval = fuse_lseek(file, offset, whence);
2342 inode_unlock(inode);
2343 break;
2344 default:
2345 retval = -EINVAL;
2346 }
2347
2348 return retval;
2349 }
2350
2351 static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
2352 unsigned int nr_segs, size_t bytes, bool to_user)
2353 {
2354 struct iov_iter ii;
2355 int page_idx = 0;
2356
2357 if (!bytes)
2358 return 0;
2359
2360 iov_iter_init(&ii, to_user ? READ : WRITE, iov, nr_segs, bytes);
2361
2362 while (iov_iter_count(&ii)) {
2363 struct page *page = pages[page_idx++];
2364 size_t todo = min_t(size_t, PAGE_SIZE, iov_iter_count(&ii));
2365 void *kaddr;
2366
2367 kaddr = kmap(page);
2368
2369 while (todo) {
2370 char __user *uaddr = ii.iov->iov_base + ii.iov_offset;
2371 size_t iov_len = ii.iov->iov_len - ii.iov_offset;
2372 size_t copy = min(todo, iov_len);
2373 size_t left;
2374
2375 if (!to_user)
2376 left = copy_from_user(kaddr, uaddr, copy);
2377 else
2378 left = copy_to_user(uaddr, kaddr, copy);
2379
2380 if (unlikely(left))
2381 return -EFAULT;
2382
2383 iov_iter_advance(&ii, copy);
2384 todo -= copy;
2385 kaddr += copy;
2386 }
2387
2388 kunmap(page);
2389 }
2390
2391 return 0;
2392 }
2393
2394 /*
2395 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2396 * ABI was defined to be 'struct iovec' which is different on 32bit
2397 * and 64bit. Fortunately we can determine which structure the server
2398 * used from the size of the reply.
2399 */
2400 static int fuse_copy_ioctl_iovec_old(struct iovec *dst, void *src,
2401 size_t transferred, unsigned count,
2402 bool is_compat)
2403 {
2404 #ifdef CONFIG_COMPAT
2405 if (count * sizeof(struct compat_iovec) == transferred) {
2406 struct compat_iovec *ciov = src;
2407 unsigned i;
2408
2409 /*
2410 * With this interface a 32bit server cannot support
2411 * non-compat (i.e. ones coming from 64bit apps) ioctl
2412 * requests
2413 */
2414 if (!is_compat)
2415 return -EINVAL;
2416
2417 for (i = 0; i < count; i++) {
2418 dst[i].iov_base = compat_ptr(ciov[i].iov_base);
2419 dst[i].iov_len = ciov[i].iov_len;
2420 }
2421 return 0;
2422 }
2423 #endif
2424
2425 if (count * sizeof(struct iovec) != transferred)
2426 return -EIO;
2427
2428 memcpy(dst, src, transferred);
2429 return 0;
2430 }
2431
2432 /* Make sure iov_length() won't overflow */
2433 static int fuse_verify_ioctl_iov(struct iovec *iov, size_t count)
2434 {
2435 size_t n;
2436 u32 max = FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT;
2437
2438 for (n = 0; n < count; n++, iov++) {
2439 if (iov->iov_len > (size_t) max)
2440 return -ENOMEM;
2441 max -= iov->iov_len;
2442 }
2443 return 0;
2444 }
2445
2446 static int fuse_copy_ioctl_iovec(struct fuse_conn *fc, struct iovec *dst,
2447 void *src, size_t transferred, unsigned count,
2448 bool is_compat)
2449 {
2450 unsigned i;
2451 struct fuse_ioctl_iovec *fiov = src;
2452
2453 if (fc->minor < 16) {
2454 return fuse_copy_ioctl_iovec_old(dst, src, transferred,
2455 count, is_compat);
2456 }
2457
2458 if (count * sizeof(struct fuse_ioctl_iovec) != transferred)
2459 return -EIO;
2460
2461 for (i = 0; i < count; i++) {
2462 /* Did the server supply an inappropriate value? */
2463 if (fiov[i].base != (unsigned long) fiov[i].base ||
2464 fiov[i].len != (unsigned long) fiov[i].len)
2465 return -EIO;
2466
2467 dst[i].iov_base = (void __user *) (unsigned long) fiov[i].base;
2468 dst[i].iov_len = (size_t) fiov[i].len;
2469
2470 #ifdef CONFIG_COMPAT
2471 if (is_compat &&
2472 (ptr_to_compat(dst[i].iov_base) != fiov[i].base ||
2473 (compat_size_t) dst[i].iov_len != fiov[i].len))
2474 return -EIO;
2475 #endif
2476 }
2477
2478 return 0;
2479 }
2480
2481
2482 /*
2483 * For ioctls, there is no generic way to determine how much memory
2484 * needs to be read and/or written. Furthermore, ioctls are allowed
2485 * to dereference the passed pointer, so the parameter requires deep
2486 * copying but FUSE has no idea whatsoever about what to copy in or
2487 * out.
2488 *
2489 * This is solved by allowing FUSE server to retry ioctl with
2490 * necessary in/out iovecs. Let's assume the ioctl implementation
2491 * needs to read in the following structure.
2492 *
2493 * struct a {
2494 * char *buf;
2495 * size_t buflen;
2496 * }
2497 *
2498 * On the first callout to FUSE server, inarg->in_size and
2499 * inarg->out_size will be NULL; then, the server completes the ioctl
2500 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2501 * the actual iov array to
2502 *
2503 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2504 *
2505 * which tells FUSE to copy in the requested area and retry the ioctl.
2506 * On the second round, the server has access to the structure and
2507 * from that it can tell what to look for next, so on the invocation,
2508 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2509 *
2510 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2511 * { .iov_base = a.buf, .iov_len = a.buflen } }
2512 *
2513 * FUSE will copy both struct a and the pointed buffer from the
2514 * process doing the ioctl and retry ioctl with both struct a and the
2515 * buffer.
2516 *
2517 * This time, FUSE server has everything it needs and completes ioctl
2518 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2519 *
2520 * Copying data out works the same way.
2521 *
2522 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2523 * automatically initializes in and out iovs by decoding @cmd with
2524 * _IOC_* macros and the server is not allowed to request RETRY. This
2525 * limits ioctl data transfers to well-formed ioctls and is the forced
2526 * behavior for all FUSE servers.
2527 */
2528 long fuse_do_ioctl(struct file *file, unsigned int cmd, unsigned long arg,
2529 unsigned int flags)
2530 {
2531 struct fuse_file *ff = file->private_data;
2532 struct fuse_conn *fc = ff->fc;
2533 struct fuse_ioctl_in inarg = {
2534 .fh = ff->fh,
2535 .cmd = cmd,
2536 .arg = arg,
2537 .flags = flags
2538 };
2539 struct fuse_ioctl_out outarg;
2540 struct fuse_req *req = NULL;
2541 struct page **pages = NULL;
2542 struct iovec *iov_page = NULL;
2543 struct iovec *in_iov = NULL, *out_iov = NULL;
2544 unsigned int in_iovs = 0, out_iovs = 0, num_pages = 0, max_pages;
2545 size_t in_size, out_size, transferred;
2546 int err;
2547
2548 #if BITS_PER_LONG == 32
2549 inarg.flags |= FUSE_IOCTL_32BIT;
2550 #else
2551 if (flags & FUSE_IOCTL_COMPAT)
2552 inarg.flags |= FUSE_IOCTL_32BIT;
2553 #endif
2554
2555 /* assume all the iovs returned by client always fits in a page */
2556 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec) * FUSE_IOCTL_MAX_IOV > PAGE_SIZE);
2557
2558 err = -ENOMEM;
2559 pages = kcalloc(FUSE_MAX_PAGES_PER_REQ, sizeof(pages[0]), GFP_KERNEL);
2560 iov_page = (struct iovec *) __get_free_page(GFP_KERNEL);
2561 if (!pages || !iov_page)
2562 goto out;
2563
2564 /*
2565 * If restricted, initialize IO parameters as encoded in @cmd.
2566 * RETRY from server is not allowed.
2567 */
2568 if (!(flags & FUSE_IOCTL_UNRESTRICTED)) {
2569 struct iovec *iov = iov_page;
2570
2571 iov->iov_base = (void __user *)arg;
2572 iov->iov_len = _IOC_SIZE(cmd);
2573
2574 if (_IOC_DIR(cmd) & _IOC_WRITE) {
2575 in_iov = iov;
2576 in_iovs = 1;
2577 }
2578
2579 if (_IOC_DIR(cmd) & _IOC_READ) {
2580 out_iov = iov;
2581 out_iovs = 1;
2582 }
2583 }
2584
2585 retry:
2586 inarg.in_size = in_size = iov_length(in_iov, in_iovs);
2587 inarg.out_size = out_size = iov_length(out_iov, out_iovs);
2588
2589 /*
2590 * Out data can be used either for actual out data or iovs,
2591 * make sure there always is at least one page.
2592 */
2593 out_size = max_t(size_t, out_size, PAGE_SIZE);
2594 max_pages = DIV_ROUND_UP(max(in_size, out_size), PAGE_SIZE);
2595
2596 /* make sure there are enough buffer pages and init request with them */
2597 err = -ENOMEM;
2598 if (max_pages > FUSE_MAX_PAGES_PER_REQ)
2599 goto out;
2600 while (num_pages < max_pages) {
2601 pages[num_pages] = alloc_page(GFP_KERNEL | __GFP_HIGHMEM);
2602 if (!pages[num_pages])
2603 goto out;
2604 num_pages++;
2605 }
2606
2607 req = fuse_get_req(fc, num_pages);
2608 if (IS_ERR(req)) {
2609 err = PTR_ERR(req);
2610 req = NULL;
2611 goto out;
2612 }
2613 memcpy(req->pages, pages, sizeof(req->pages[0]) * num_pages);
2614 req->num_pages = num_pages;
2615 fuse_page_descs_length_init(req, 0, req->num_pages);
2616
2617 /* okay, let's send it to the client */
2618 req->in.h.opcode = FUSE_IOCTL;
2619 req->in.h.nodeid = ff->nodeid;
2620 req->in.numargs = 1;
2621 req->in.args[0].size = sizeof(inarg);
2622 req->in.args[0].value = &inarg;
2623 if (in_size) {
2624 req->in.numargs++;
2625 req->in.args[1].size = in_size;
2626 req->in.argpages = 1;
2627
2628 err = fuse_ioctl_copy_user(pages, in_iov, in_iovs, in_size,
2629 false);
2630 if (err)
2631 goto out;
2632 }
2633
2634 req->out.numargs = 2;
2635 req->out.args[0].size = sizeof(outarg);
2636 req->out.args[0].value = &outarg;
2637 req->out.args[1].size = out_size;
2638 req->out.argpages = 1;
2639 req->out.argvar = 1;
2640
2641 fuse_request_send(fc, req);
2642 err = req->out.h.error;
2643 transferred = req->out.args[1].size;
2644 fuse_put_request(fc, req);
2645 req = NULL;
2646 if (err)
2647 goto out;
2648
2649 /* did it ask for retry? */
2650 if (outarg.flags & FUSE_IOCTL_RETRY) {
2651 void *vaddr;
2652
2653 /* no retry if in restricted mode */
2654 err = -EIO;
2655 if (!(flags & FUSE_IOCTL_UNRESTRICTED))
2656 goto out;
2657
2658 in_iovs = outarg.in_iovs;
2659 out_iovs = outarg.out_iovs;
2660
2661 /*
2662 * Make sure things are in boundary, separate checks
2663 * are to protect against overflow.
2664 */
2665 err = -ENOMEM;
2666 if (in_iovs > FUSE_IOCTL_MAX_IOV ||
2667 out_iovs > FUSE_IOCTL_MAX_IOV ||
2668 in_iovs + out_iovs > FUSE_IOCTL_MAX_IOV)
2669 goto out;
2670
2671 vaddr = kmap_atomic(pages[0]);
2672 err = fuse_copy_ioctl_iovec(fc, iov_page, vaddr,
2673 transferred, in_iovs + out_iovs,
2674 (flags & FUSE_IOCTL_COMPAT) != 0);
2675 kunmap_atomic(vaddr);
2676 if (err)
2677 goto out;
2678
2679 in_iov = iov_page;
2680 out_iov = in_iov + in_iovs;
2681
2682 err = fuse_verify_ioctl_iov(in_iov, in_iovs);
2683 if (err)
2684 goto out;
2685
2686 err = fuse_verify_ioctl_iov(out_iov, out_iovs);
2687 if (err)
2688 goto out;
2689
2690 goto retry;
2691 }
2692
2693 err = -EIO;
2694 if (transferred > inarg.out_size)
2695 goto out;
2696
2697 err = fuse_ioctl_copy_user(pages, out_iov, out_iovs, transferred, true);
2698 out:
2699 if (req)
2700 fuse_put_request(fc, req);
2701 free_page((unsigned long) iov_page);
2702 while (num_pages)
2703 __free_page(pages[--num_pages]);
2704 kfree(pages);
2705
2706 return err ? err : outarg.result;
2707 }
2708 EXPORT_SYMBOL_GPL(fuse_do_ioctl);
2709
2710 long fuse_ioctl_common(struct file *file, unsigned int cmd,
2711 unsigned long arg, unsigned int flags)
2712 {
2713 struct inode *inode = file_inode(file);
2714 struct fuse_conn *fc = get_fuse_conn(inode);
2715
2716 if (!fuse_allow_current_process(fc))
2717 return -EACCES;
2718
2719 if (is_bad_inode(inode))
2720 return -EIO;
2721
2722 return fuse_do_ioctl(file, cmd, arg, flags);
2723 }
2724
2725 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
2726 unsigned long arg)
2727 {
2728 return fuse_ioctl_common(file, cmd, arg, 0);
2729 }
2730
2731 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
2732 unsigned long arg)
2733 {
2734 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
2735 }
2736
2737 /*
2738 * All files which have been polled are linked to RB tree
2739 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2740 * find the matching one.
2741 */
2742 static struct rb_node **fuse_find_polled_node(struct fuse_conn *fc, u64 kh,
2743 struct rb_node **parent_out)
2744 {
2745 struct rb_node **link = &fc->polled_files.rb_node;
2746 struct rb_node *last = NULL;
2747
2748 while (*link) {
2749 struct fuse_file *ff;
2750
2751 last = *link;
2752 ff = rb_entry(last, struct fuse_file, polled_node);
2753
2754 if (kh < ff->kh)
2755 link = &last->rb_left;
2756 else if (kh > ff->kh)
2757 link = &last->rb_right;
2758 else
2759 return link;
2760 }
2761
2762 if (parent_out)
2763 *parent_out = last;
2764 return link;
2765 }
2766
2767 /*
2768 * The file is about to be polled. Make sure it's on the polled_files
2769 * RB tree. Note that files once added to the polled_files tree are
2770 * not removed before the file is released. This is because a file
2771 * polled once is likely to be polled again.
2772 */
2773 static void fuse_register_polled_file(struct fuse_conn *fc,
2774 struct fuse_file *ff)
2775 {
2776 spin_lock(&fc->lock);
2777 if (RB_EMPTY_NODE(&ff->polled_node)) {
2778 struct rb_node **link, *uninitialized_var(parent);
2779
2780 link = fuse_find_polled_node(fc, ff->kh, &parent);
2781 BUG_ON(*link);
2782 rb_link_node(&ff->polled_node, parent, link);
2783 rb_insert_color(&ff->polled_node, &fc->polled_files);
2784 }
2785 spin_unlock(&fc->lock);
2786 }
2787
2788 unsigned fuse_file_poll(struct file *file, poll_table *wait)
2789 {
2790 struct fuse_file *ff = file->private_data;
2791 struct fuse_conn *fc = ff->fc;
2792 struct fuse_poll_in inarg = { .fh = ff->fh, .kh = ff->kh };
2793 struct fuse_poll_out outarg;
2794 FUSE_ARGS(args);
2795 int err;
2796
2797 if (fc->no_poll)
2798 return DEFAULT_POLLMASK;
2799
2800 poll_wait(file, &ff->poll_wait, wait);
2801 inarg.events = (__u32)poll_requested_events(wait);
2802
2803 /*
2804 * Ask for notification iff there's someone waiting for it.
2805 * The client may ignore the flag and always notify.
2806 */
2807 if (waitqueue_active(&ff->poll_wait)) {
2808 inarg.flags |= FUSE_POLL_SCHEDULE_NOTIFY;
2809 fuse_register_polled_file(fc, ff);
2810 }
2811
2812 args.in.h.opcode = FUSE_POLL;
2813 args.in.h.nodeid = ff->nodeid;
2814 args.in.numargs = 1;
2815 args.in.args[0].size = sizeof(inarg);
2816 args.in.args[0].value = &inarg;
2817 args.out.numargs = 1;
2818 args.out.args[0].size = sizeof(outarg);
2819 args.out.args[0].value = &outarg;
2820 err = fuse_simple_request(fc, &args);
2821
2822 if (!err)
2823 return outarg.revents;
2824 if (err == -ENOSYS) {
2825 fc->no_poll = 1;
2826 return DEFAULT_POLLMASK;
2827 }
2828 return POLLERR;
2829 }
2830 EXPORT_SYMBOL_GPL(fuse_file_poll);
2831
2832 /*
2833 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2834 * wakes up the poll waiters.
2835 */
2836 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
2837 struct fuse_notify_poll_wakeup_out *outarg)
2838 {
2839 u64 kh = outarg->kh;
2840 struct rb_node **link;
2841
2842 spin_lock(&fc->lock);
2843
2844 link = fuse_find_polled_node(fc, kh, NULL);
2845 if (*link) {
2846 struct fuse_file *ff;
2847
2848 ff = rb_entry(*link, struct fuse_file, polled_node);
2849 wake_up_interruptible_sync(&ff->poll_wait);
2850 }
2851
2852 spin_unlock(&fc->lock);
2853 return 0;
2854 }
2855
2856 static void fuse_do_truncate(struct file *file)
2857 {
2858 struct inode *inode = file->f_mapping->host;
2859 struct iattr attr;
2860
2861 attr.ia_valid = ATTR_SIZE;
2862 attr.ia_size = i_size_read(inode);
2863
2864 attr.ia_file = file;
2865 attr.ia_valid |= ATTR_FILE;
2866
2867 fuse_do_setattr(inode, &attr, file);
2868 }
2869
2870 static inline loff_t fuse_round_up(loff_t off)
2871 {
2872 return round_up(off, FUSE_MAX_PAGES_PER_REQ << PAGE_SHIFT);
2873 }
2874
2875 static ssize_t
2876 fuse_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2877 {
2878 DECLARE_COMPLETION_ONSTACK(wait);
2879 ssize_t ret = 0;
2880 struct file *file = iocb->ki_filp;
2881 struct fuse_file *ff = file->private_data;
2882 bool async_dio = ff->fc->async_dio;
2883 loff_t pos = 0;
2884 struct inode *inode;
2885 loff_t i_size;
2886 size_t count = iov_iter_count(iter);
2887 loff_t offset = iocb->ki_pos;
2888 struct fuse_io_priv *io;
2889
2890 pos = offset;
2891 inode = file->f_mapping->host;
2892 i_size = i_size_read(inode);
2893
2894 if ((iov_iter_rw(iter) == READ) && (offset > i_size))
2895 return 0;
2896
2897 /* optimization for short read */
2898 if (async_dio && iov_iter_rw(iter) != WRITE && offset + count > i_size) {
2899 if (offset >= i_size)
2900 return 0;
2901 iov_iter_truncate(iter, fuse_round_up(i_size - offset));
2902 count = iov_iter_count(iter);
2903 }
2904
2905 io = kmalloc(sizeof(struct fuse_io_priv), GFP_KERNEL);
2906 if (!io)
2907 return -ENOMEM;
2908 spin_lock_init(&io->lock);
2909 kref_init(&io->refcnt);
2910 io->reqs = 1;
2911 io->bytes = -1;
2912 io->size = 0;
2913 io->offset = offset;
2914 io->write = (iov_iter_rw(iter) == WRITE);
2915 io->err = 0;
2916 io->file = file;
2917 /*
2918 * By default, we want to optimize all I/Os with async request
2919 * submission to the client filesystem if supported.
2920 */
2921 io->async = async_dio;
2922 io->iocb = iocb;
2923 io->blocking = is_sync_kiocb(iocb);
2924
2925 /*
2926 * We cannot asynchronously extend the size of a file.
2927 * In such case the aio will behave exactly like sync io.
2928 */
2929 if ((offset + count > i_size) && iov_iter_rw(iter) == WRITE)
2930 io->blocking = true;
2931
2932 if (io->async && io->blocking) {
2933 /*
2934 * Additional reference to keep io around after
2935 * calling fuse_aio_complete()
2936 */
2937 kref_get(&io->refcnt);
2938 io->done = &wait;
2939 }
2940
2941 if (iov_iter_rw(iter) == WRITE) {
2942 ret = fuse_direct_io(io, iter, &pos, FUSE_DIO_WRITE);
2943 fuse_invalidate_attr(inode);
2944 } else {
2945 ret = __fuse_direct_read(io, iter, &pos);
2946 }
2947
2948 if (io->async) {
2949 fuse_aio_complete(io, ret < 0 ? ret : 0, -1);
2950
2951 /* we have a non-extending, async request, so return */
2952 if (!io->blocking)
2953 return -EIOCBQUEUED;
2954
2955 wait_for_completion(&wait);
2956 ret = fuse_get_res_by_io(io);
2957 }
2958
2959 kref_put(&io->refcnt, fuse_io_release);
2960
2961 if (iov_iter_rw(iter) == WRITE) {
2962 if (ret > 0)
2963 fuse_write_update_size(inode, pos);
2964 else if (ret < 0 && offset + count > i_size)
2965 fuse_do_truncate(file);
2966 }
2967
2968 return ret;
2969 }
2970
2971 static long fuse_file_fallocate(struct file *file, int mode, loff_t offset,
2972 loff_t length)
2973 {
2974 struct fuse_file *ff = file->private_data;
2975 struct inode *inode = file_inode(file);
2976 struct fuse_inode *fi = get_fuse_inode(inode);
2977 struct fuse_conn *fc = ff->fc;
2978 FUSE_ARGS(args);
2979 struct fuse_fallocate_in inarg = {
2980 .fh = ff->fh,
2981 .offset = offset,
2982 .length = length,
2983 .mode = mode
2984 };
2985 int err;
2986 bool lock_inode = !(mode & FALLOC_FL_KEEP_SIZE) ||
2987 (mode & FALLOC_FL_PUNCH_HOLE);
2988
2989 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
2990 return -EOPNOTSUPP;
2991
2992 if (fc->no_fallocate)
2993 return -EOPNOTSUPP;
2994
2995 if (lock_inode) {
2996 inode_lock(inode);
2997 if (mode & FALLOC_FL_PUNCH_HOLE) {
2998 loff_t endbyte = offset + length - 1;
2999 err = filemap_write_and_wait_range(inode->i_mapping,
3000 offset, endbyte);
3001 if (err)
3002 goto out;
3003
3004 fuse_sync_writes(inode);
3005 }
3006 }
3007
3008 if (!(mode & FALLOC_FL_KEEP_SIZE))
3009 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3010
3011 args.in.h.opcode = FUSE_FALLOCATE;
3012 args.in.h.nodeid = ff->nodeid;
3013 args.in.numargs = 1;
3014 args.in.args[0].size = sizeof(inarg);
3015 args.in.args[0].value = &inarg;
3016 err = fuse_simple_request(fc, &args);
3017 if (err == -ENOSYS) {
3018 fc->no_fallocate = 1;
3019 err = -EOPNOTSUPP;
3020 }
3021 if (err)
3022 goto out;
3023
3024 /* we could have extended the file */
3025 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
3026 bool changed = fuse_write_update_size(inode, offset + length);
3027
3028 if (changed && fc->writeback_cache)
3029 file_update_time(file);
3030 }
3031
3032 if (mode & FALLOC_FL_PUNCH_HOLE)
3033 truncate_pagecache_range(inode, offset, offset + length - 1);
3034
3035 fuse_invalidate_attr(inode);
3036
3037 out:
3038 if (!(mode & FALLOC_FL_KEEP_SIZE))
3039 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
3040
3041 if (lock_inode)
3042 inode_unlock(inode);
3043
3044 return err;
3045 }
3046
3047 static const struct file_operations fuse_file_operations = {
3048 .llseek = fuse_file_llseek,
3049 .read_iter = fuse_file_read_iter,
3050 .write_iter = fuse_file_write_iter,
3051 .mmap = fuse_file_mmap,
3052 .open = fuse_open,
3053 .flush = fuse_flush,
3054 .release = fuse_release,
3055 .fsync = fuse_fsync,
3056 .lock = fuse_file_lock,
3057 .flock = fuse_file_flock,
3058 .splice_read = fuse_file_splice_read,
3059 .unlocked_ioctl = fuse_file_ioctl,
3060 .compat_ioctl = fuse_file_compat_ioctl,
3061 .poll = fuse_file_poll,
3062 .fallocate = fuse_file_fallocate,
3063 };
3064
3065 static const struct file_operations fuse_direct_io_file_operations = {
3066 .llseek = fuse_file_llseek,
3067 .read_iter = fuse_direct_read_iter,
3068 .write_iter = fuse_direct_write_iter,
3069 .mmap = fuse_direct_mmap,
3070 .open = fuse_open,
3071 .flush = fuse_flush,
3072 .release = fuse_release,
3073 .fsync = fuse_fsync,
3074 .lock = fuse_file_lock,
3075 .flock = fuse_file_flock,
3076 .unlocked_ioctl = fuse_file_ioctl,
3077 .compat_ioctl = fuse_file_compat_ioctl,
3078 .poll = fuse_file_poll,
3079 .fallocate = fuse_file_fallocate,
3080 /* no splice_read */
3081 };
3082
3083 static const struct address_space_operations fuse_file_aops = {
3084 .readpage = fuse_readpage,
3085 .writepage = fuse_writepage,
3086 .writepages = fuse_writepages,
3087 .launder_page = fuse_launder_page,
3088 .readpages = fuse_readpages,
3089 .set_page_dirty = __set_page_dirty_nobuffers,
3090 .bmap = fuse_bmap,
3091 .direct_IO = fuse_direct_IO,
3092 .write_begin = fuse_write_begin,
3093 .write_end = fuse_write_end,
3094 };
3095
3096 void fuse_init_file_inode(struct inode *inode)
3097 {
3098 inode->i_fop = &fuse_file_operations;
3099 inode->i_data.a_ops = &fuse_file_aops;
3100 }
This page took 0.116257 seconds and 5 git commands to generate.