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