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