xref: /openbmc/linux/fs/overlayfs/file.c (revision 3612485b)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Red Hat, Inc.
4  */
5 
6 #include <linux/cred.h>
7 #include <linux/file.h>
8 #include <linux/mount.h>
9 #include <linux/xattr.h>
10 #include <linux/uio.h>
11 #include <linux/uaccess.h>
12 #include <linux/splice.h>
13 #include <linux/security.h>
14 #include <linux/mm.h>
15 #include <linux/fs.h>
16 #include "overlayfs.h"
17 
18 struct ovl_aio_req {
19 	struct kiocb iocb;
20 	struct kiocb *orig_iocb;
21 	struct fd fd;
22 };
23 
24 static struct kmem_cache *ovl_aio_request_cachep;
25 
26 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
27 {
28 	if (realinode != ovl_inode_upper(inode))
29 		return 'l';
30 	if (ovl_has_upperdata(inode))
31 		return 'u';
32 	else
33 		return 'm';
34 }
35 
36 static struct file *ovl_open_realfile(const struct file *file,
37 				      struct inode *realinode)
38 {
39 	struct inode *inode = file_inode(file);
40 	struct file *realfile;
41 	const struct cred *old_cred;
42 	int flags = file->f_flags | O_NOATIME | FMODE_NONOTIFY;
43 	int acc_mode = ACC_MODE(flags);
44 	int err;
45 
46 	if (flags & O_APPEND)
47 		acc_mode |= MAY_APPEND;
48 
49 	old_cred = ovl_override_creds(inode->i_sb);
50 	err = inode_permission(realinode, MAY_OPEN | acc_mode);
51 	if (err) {
52 		realfile = ERR_PTR(err);
53 	} else if (!inode_owner_or_capable(realinode)) {
54 		realfile = ERR_PTR(-EPERM);
55 	} else {
56 		realfile = open_with_fake_path(&file->f_path, flags, realinode,
57 					       current_cred());
58 	}
59 	revert_creds(old_cred);
60 
61 	pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
62 		 file, file, ovl_whatisit(inode, realinode), file->f_flags,
63 		 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
64 
65 	return realfile;
66 }
67 
68 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
69 
70 static int ovl_change_flags(struct file *file, unsigned int flags)
71 {
72 	struct inode *inode = file_inode(file);
73 	int err;
74 
75 	/* No atime modificaton on underlying */
76 	flags |= O_NOATIME | FMODE_NONOTIFY;
77 
78 	/* If some flag changed that cannot be changed then something's amiss */
79 	if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
80 		return -EIO;
81 
82 	flags &= OVL_SETFL_MASK;
83 
84 	if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
85 		return -EPERM;
86 
87 	if (flags & O_DIRECT) {
88 		if (!file->f_mapping->a_ops ||
89 		    !file->f_mapping->a_ops->direct_IO)
90 			return -EINVAL;
91 	}
92 
93 	if (file->f_op->check_flags) {
94 		err = file->f_op->check_flags(flags);
95 		if (err)
96 			return err;
97 	}
98 
99 	spin_lock(&file->f_lock);
100 	file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
101 	spin_unlock(&file->f_lock);
102 
103 	return 0;
104 }
105 
106 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
107 			       bool allow_meta)
108 {
109 	struct inode *inode = file_inode(file);
110 	struct inode *realinode;
111 
112 	real->flags = 0;
113 	real->file = file->private_data;
114 
115 	if (allow_meta)
116 		realinode = ovl_inode_real(inode);
117 	else
118 		realinode = ovl_inode_realdata(inode);
119 
120 	/* Has it been copied up since we'd opened it? */
121 	if (unlikely(file_inode(real->file) != realinode)) {
122 		real->flags = FDPUT_FPUT;
123 		real->file = ovl_open_realfile(file, realinode);
124 
125 		return PTR_ERR_OR_ZERO(real->file);
126 	}
127 
128 	/* Did the flags change since open? */
129 	if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME))
130 		return ovl_change_flags(real->file, file->f_flags);
131 
132 	return 0;
133 }
134 
135 static int ovl_real_fdget(const struct file *file, struct fd *real)
136 {
137 	return ovl_real_fdget_meta(file, real, false);
138 }
139 
140 static int ovl_open(struct inode *inode, struct file *file)
141 {
142 	struct file *realfile;
143 	int err;
144 
145 	err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
146 	if (err)
147 		return err;
148 
149 	/* No longer need these flags, so don't pass them on to underlying fs */
150 	file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
151 
152 	realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
153 	if (IS_ERR(realfile))
154 		return PTR_ERR(realfile);
155 
156 	file->private_data = realfile;
157 
158 	return 0;
159 }
160 
161 static int ovl_release(struct inode *inode, struct file *file)
162 {
163 	fput(file->private_data);
164 
165 	return 0;
166 }
167 
168 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
169 {
170 	struct inode *inode = file_inode(file);
171 	struct fd real;
172 	const struct cred *old_cred;
173 	loff_t ret;
174 
175 	/*
176 	 * The two special cases below do not need to involve real fs,
177 	 * so we can optimizing concurrent callers.
178 	 */
179 	if (offset == 0) {
180 		if (whence == SEEK_CUR)
181 			return file->f_pos;
182 
183 		if (whence == SEEK_SET)
184 			return vfs_setpos(file, 0, 0);
185 	}
186 
187 	ret = ovl_real_fdget(file, &real);
188 	if (ret)
189 		return ret;
190 
191 	/*
192 	 * Overlay file f_pos is the master copy that is preserved
193 	 * through copy up and modified on read/write, but only real
194 	 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
195 	 * limitations that are more strict than ->s_maxbytes for specific
196 	 * files, so we use the real file to perform seeks.
197 	 */
198 	ovl_inode_lock(inode);
199 	real.file->f_pos = file->f_pos;
200 
201 	old_cred = ovl_override_creds(inode->i_sb);
202 	ret = vfs_llseek(real.file, offset, whence);
203 	revert_creds(old_cred);
204 
205 	file->f_pos = real.file->f_pos;
206 	ovl_inode_unlock(inode);
207 
208 	fdput(real);
209 
210 	return ret;
211 }
212 
213 static void ovl_file_accessed(struct file *file)
214 {
215 	struct inode *inode, *upperinode;
216 
217 	if (file->f_flags & O_NOATIME)
218 		return;
219 
220 	inode = file_inode(file);
221 	upperinode = ovl_inode_upper(inode);
222 
223 	if (!upperinode)
224 		return;
225 
226 	if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
227 	     !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
228 		inode->i_mtime = upperinode->i_mtime;
229 		inode->i_ctime = upperinode->i_ctime;
230 	}
231 
232 	touch_atime(&file->f_path);
233 }
234 
235 static rwf_t ovl_iocb_to_rwf(int ifl)
236 {
237 	rwf_t flags = 0;
238 
239 	if (ifl & IOCB_NOWAIT)
240 		flags |= RWF_NOWAIT;
241 	if (ifl & IOCB_HIPRI)
242 		flags |= RWF_HIPRI;
243 	if (ifl & IOCB_DSYNC)
244 		flags |= RWF_DSYNC;
245 	if (ifl & IOCB_SYNC)
246 		flags |= RWF_SYNC;
247 
248 	return flags;
249 }
250 
251 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
252 {
253 	struct kiocb *iocb = &aio_req->iocb;
254 	struct kiocb *orig_iocb = aio_req->orig_iocb;
255 
256 	if (iocb->ki_flags & IOCB_WRITE) {
257 		struct inode *inode = file_inode(orig_iocb->ki_filp);
258 
259 		/* Actually acquired in ovl_write_iter() */
260 		__sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
261 				      SB_FREEZE_WRITE);
262 		file_end_write(iocb->ki_filp);
263 		ovl_copyattr(ovl_inode_real(inode), inode);
264 	}
265 
266 	orig_iocb->ki_pos = iocb->ki_pos;
267 	fdput(aio_req->fd);
268 	kmem_cache_free(ovl_aio_request_cachep, aio_req);
269 }
270 
271 static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2)
272 {
273 	struct ovl_aio_req *aio_req = container_of(iocb,
274 						   struct ovl_aio_req, iocb);
275 	struct kiocb *orig_iocb = aio_req->orig_iocb;
276 
277 	ovl_aio_cleanup_handler(aio_req);
278 	orig_iocb->ki_complete(orig_iocb, res, res2);
279 }
280 
281 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
282 {
283 	struct file *file = iocb->ki_filp;
284 	struct fd real;
285 	const struct cred *old_cred;
286 	ssize_t ret;
287 
288 	if (!iov_iter_count(iter))
289 		return 0;
290 
291 	ret = ovl_real_fdget(file, &real);
292 	if (ret)
293 		return ret;
294 
295 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
296 	if (is_sync_kiocb(iocb)) {
297 		ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
298 				    ovl_iocb_to_rwf(iocb->ki_flags));
299 	} else {
300 		struct ovl_aio_req *aio_req;
301 
302 		ret = -ENOMEM;
303 		aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
304 		if (!aio_req)
305 			goto out;
306 
307 		aio_req->fd = real;
308 		real.flags = 0;
309 		aio_req->orig_iocb = iocb;
310 		kiocb_clone(&aio_req->iocb, iocb, real.file);
311 		aio_req->iocb.ki_complete = ovl_aio_rw_complete;
312 		ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
313 		if (ret != -EIOCBQUEUED)
314 			ovl_aio_cleanup_handler(aio_req);
315 	}
316 out:
317 	revert_creds(old_cred);
318 	ovl_file_accessed(file);
319 
320 	fdput(real);
321 
322 	return ret;
323 }
324 
325 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
326 {
327 	struct file *file = iocb->ki_filp;
328 	struct inode *inode = file_inode(file);
329 	struct fd real;
330 	const struct cred *old_cred;
331 	ssize_t ret;
332 
333 	if (!iov_iter_count(iter))
334 		return 0;
335 
336 	inode_lock(inode);
337 	/* Update mode */
338 	ovl_copyattr(ovl_inode_real(inode), inode);
339 	ret = file_remove_privs(file);
340 	if (ret)
341 		goto out_unlock;
342 
343 	ret = ovl_real_fdget(file, &real);
344 	if (ret)
345 		goto out_unlock;
346 
347 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
348 	if (is_sync_kiocb(iocb)) {
349 		file_start_write(real.file);
350 		ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
351 				     ovl_iocb_to_rwf(iocb->ki_flags));
352 		file_end_write(real.file);
353 		/* Update size */
354 		ovl_copyattr(ovl_inode_real(inode), inode);
355 	} else {
356 		struct ovl_aio_req *aio_req;
357 
358 		ret = -ENOMEM;
359 		aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
360 		if (!aio_req)
361 			goto out;
362 
363 		file_start_write(real.file);
364 		/* Pacify lockdep, same trick as done in aio_write() */
365 		__sb_writers_release(file_inode(real.file)->i_sb,
366 				     SB_FREEZE_WRITE);
367 		aio_req->fd = real;
368 		real.flags = 0;
369 		aio_req->orig_iocb = iocb;
370 		kiocb_clone(&aio_req->iocb, iocb, real.file);
371 		aio_req->iocb.ki_complete = ovl_aio_rw_complete;
372 		ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
373 		if (ret != -EIOCBQUEUED)
374 			ovl_aio_cleanup_handler(aio_req);
375 	}
376 out:
377 	revert_creds(old_cred);
378 	fdput(real);
379 
380 out_unlock:
381 	inode_unlock(inode);
382 
383 	return ret;
384 }
385 
386 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
387 			 struct pipe_inode_info *pipe, size_t len,
388 			 unsigned int flags)
389 {
390 	ssize_t ret;
391 	struct fd real;
392 	const struct cred *old_cred;
393 
394 	ret = ovl_real_fdget(in, &real);
395 	if (ret)
396 		return ret;
397 
398 	old_cred = ovl_override_creds(file_inode(in)->i_sb);
399 	ret = generic_file_splice_read(real.file, ppos, pipe, len, flags);
400 	revert_creds(old_cred);
401 
402 	ovl_file_accessed(in);
403 	fdput(real);
404 	return ret;
405 }
406 
407 static ssize_t
408 ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
409 			  loff_t *ppos, size_t len, unsigned int flags)
410 {
411 	struct fd real;
412 	const struct cred *old_cred;
413 	ssize_t ret;
414 
415 	ret = ovl_real_fdget(out, &real);
416 	if (ret)
417 		return ret;
418 
419 	old_cred = ovl_override_creds(file_inode(out)->i_sb);
420 	ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
421 	revert_creds(old_cred);
422 
423 	ovl_file_accessed(out);
424 	fdput(real);
425 	return ret;
426 }
427 
428 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
429 {
430 	struct fd real;
431 	const struct cred *old_cred;
432 	int ret;
433 
434 	ret = ovl_real_fdget_meta(file, &real, !datasync);
435 	if (ret)
436 		return ret;
437 
438 	/* Don't sync lower file for fear of receiving EROFS error */
439 	if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
440 		old_cred = ovl_override_creds(file_inode(file)->i_sb);
441 		ret = vfs_fsync_range(real.file, start, end, datasync);
442 		revert_creds(old_cred);
443 	}
444 
445 	fdput(real);
446 
447 	return ret;
448 }
449 
450 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
451 {
452 	struct file *realfile = file->private_data;
453 	const struct cred *old_cred;
454 	int ret;
455 
456 	if (!realfile->f_op->mmap)
457 		return -ENODEV;
458 
459 	if (WARN_ON(file != vma->vm_file))
460 		return -EIO;
461 
462 	vma->vm_file = get_file(realfile);
463 
464 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
465 	ret = call_mmap(vma->vm_file, vma);
466 	revert_creds(old_cred);
467 
468 	if (ret) {
469 		/* Drop reference count from new vm_file value */
470 		fput(realfile);
471 	} else {
472 		/* Drop reference count from previous vm_file value */
473 		fput(file);
474 	}
475 
476 	ovl_file_accessed(file);
477 
478 	return ret;
479 }
480 
481 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
482 {
483 	struct inode *inode = file_inode(file);
484 	struct fd real;
485 	const struct cred *old_cred;
486 	int ret;
487 
488 	ret = ovl_real_fdget(file, &real);
489 	if (ret)
490 		return ret;
491 
492 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
493 	ret = vfs_fallocate(real.file, mode, offset, len);
494 	revert_creds(old_cred);
495 
496 	/* Update size */
497 	ovl_copyattr(ovl_inode_real(inode), inode);
498 
499 	fdput(real);
500 
501 	return ret;
502 }
503 
504 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
505 {
506 	struct fd real;
507 	const struct cred *old_cred;
508 	int ret;
509 
510 	ret = ovl_real_fdget(file, &real);
511 	if (ret)
512 		return ret;
513 
514 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
515 	ret = vfs_fadvise(real.file, offset, len, advice);
516 	revert_creds(old_cred);
517 
518 	fdput(real);
519 
520 	return ret;
521 }
522 
523 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
524 			   unsigned long arg)
525 {
526 	struct fd real;
527 	const struct cred *old_cred;
528 	long ret;
529 
530 	ret = ovl_real_fdget(file, &real);
531 	if (ret)
532 		return ret;
533 
534 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
535 	ret = security_file_ioctl(real.file, cmd, arg);
536 	if (!ret)
537 		ret = vfs_ioctl(real.file, cmd, arg);
538 	revert_creds(old_cred);
539 
540 	fdput(real);
541 
542 	return ret;
543 }
544 
545 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
546 				unsigned long arg, unsigned int iflags)
547 {
548 	long ret;
549 	struct inode *inode = file_inode(file);
550 	unsigned int old_iflags;
551 
552 	if (!inode_owner_or_capable(inode))
553 		return -EACCES;
554 
555 	ret = mnt_want_write_file(file);
556 	if (ret)
557 		return ret;
558 
559 	inode_lock(inode);
560 
561 	/* Check the capability before cred override */
562 	ret = -EPERM;
563 	old_iflags = READ_ONCE(inode->i_flags);
564 	if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
565 	    !capable(CAP_LINUX_IMMUTABLE))
566 		goto unlock;
567 
568 	ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
569 	if (ret)
570 		goto unlock;
571 
572 	ret = ovl_real_ioctl(file, cmd, arg);
573 
574 	ovl_copyflags(ovl_inode_real(inode), inode);
575 unlock:
576 	inode_unlock(inode);
577 
578 	mnt_drop_write_file(file);
579 
580 	return ret;
581 
582 }
583 
584 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
585 {
586 	unsigned int iflags = 0;
587 
588 	if (flags & FS_SYNC_FL)
589 		iflags |= S_SYNC;
590 	if (flags & FS_APPEND_FL)
591 		iflags |= S_APPEND;
592 	if (flags & FS_IMMUTABLE_FL)
593 		iflags |= S_IMMUTABLE;
594 	if (flags & FS_NOATIME_FL)
595 		iflags |= S_NOATIME;
596 
597 	return iflags;
598 }
599 
600 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
601 				  unsigned long arg)
602 {
603 	unsigned int flags;
604 
605 	if (get_user(flags, (int __user *) arg))
606 		return -EFAULT;
607 
608 	return ovl_ioctl_set_flags(file, cmd, arg,
609 				   ovl_fsflags_to_iflags(flags));
610 }
611 
612 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
613 {
614 	unsigned int iflags = 0;
615 
616 	if (xflags & FS_XFLAG_SYNC)
617 		iflags |= S_SYNC;
618 	if (xflags & FS_XFLAG_APPEND)
619 		iflags |= S_APPEND;
620 	if (xflags & FS_XFLAG_IMMUTABLE)
621 		iflags |= S_IMMUTABLE;
622 	if (xflags & FS_XFLAG_NOATIME)
623 		iflags |= S_NOATIME;
624 
625 	return iflags;
626 }
627 
628 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
629 				   unsigned long arg)
630 {
631 	struct fsxattr fa;
632 
633 	memset(&fa, 0, sizeof(fa));
634 	if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
635 		return -EFAULT;
636 
637 	return ovl_ioctl_set_flags(file, cmd, arg,
638 				   ovl_fsxflags_to_iflags(fa.fsx_xflags));
639 }
640 
641 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
642 {
643 	long ret;
644 
645 	switch (cmd) {
646 	case FS_IOC_GETFLAGS:
647 	case FS_IOC_FSGETXATTR:
648 		ret = ovl_real_ioctl(file, cmd, arg);
649 		break;
650 
651 	case FS_IOC_SETFLAGS:
652 		ret = ovl_ioctl_set_fsflags(file, cmd, arg);
653 		break;
654 
655 	case FS_IOC_FSSETXATTR:
656 		ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
657 		break;
658 
659 	default:
660 		ret = -ENOTTY;
661 	}
662 
663 	return ret;
664 }
665 
666 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
667 			     unsigned long arg)
668 {
669 	switch (cmd) {
670 	case FS_IOC32_GETFLAGS:
671 		cmd = FS_IOC_GETFLAGS;
672 		break;
673 
674 	case FS_IOC32_SETFLAGS:
675 		cmd = FS_IOC_SETFLAGS;
676 		break;
677 
678 	default:
679 		return -ENOIOCTLCMD;
680 	}
681 
682 	return ovl_ioctl(file, cmd, arg);
683 }
684 
685 enum ovl_copyop {
686 	OVL_COPY,
687 	OVL_CLONE,
688 	OVL_DEDUPE,
689 };
690 
691 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
692 			    struct file *file_out, loff_t pos_out,
693 			    loff_t len, unsigned int flags, enum ovl_copyop op)
694 {
695 	struct inode *inode_out = file_inode(file_out);
696 	struct fd real_in, real_out;
697 	const struct cred *old_cred;
698 	loff_t ret;
699 
700 	ret = ovl_real_fdget(file_out, &real_out);
701 	if (ret)
702 		return ret;
703 
704 	ret = ovl_real_fdget(file_in, &real_in);
705 	if (ret) {
706 		fdput(real_out);
707 		return ret;
708 	}
709 
710 	old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
711 	switch (op) {
712 	case OVL_COPY:
713 		ret = vfs_copy_file_range(real_in.file, pos_in,
714 					  real_out.file, pos_out, len, flags);
715 		break;
716 
717 	case OVL_CLONE:
718 		ret = vfs_clone_file_range(real_in.file, pos_in,
719 					   real_out.file, pos_out, len, flags);
720 		break;
721 
722 	case OVL_DEDUPE:
723 		ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
724 						real_out.file, pos_out, len,
725 						flags);
726 		break;
727 	}
728 	revert_creds(old_cred);
729 
730 	/* Update size */
731 	ovl_copyattr(ovl_inode_real(inode_out), inode_out);
732 
733 	fdput(real_in);
734 	fdput(real_out);
735 
736 	return ret;
737 }
738 
739 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
740 				   struct file *file_out, loff_t pos_out,
741 				   size_t len, unsigned int flags)
742 {
743 	return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
744 			    OVL_COPY);
745 }
746 
747 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
748 				   struct file *file_out, loff_t pos_out,
749 				   loff_t len, unsigned int remap_flags)
750 {
751 	enum ovl_copyop op;
752 
753 	if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
754 		return -EINVAL;
755 
756 	if (remap_flags & REMAP_FILE_DEDUP)
757 		op = OVL_DEDUPE;
758 	else
759 		op = OVL_CLONE;
760 
761 	/*
762 	 * Don't copy up because of a dedupe request, this wouldn't make sense
763 	 * most of the time (data would be duplicated instead of deduplicated).
764 	 */
765 	if (op == OVL_DEDUPE &&
766 	    (!ovl_inode_upper(file_inode(file_in)) ||
767 	     !ovl_inode_upper(file_inode(file_out))))
768 		return -EPERM;
769 
770 	return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
771 			    remap_flags, op);
772 }
773 
774 const struct file_operations ovl_file_operations = {
775 	.open		= ovl_open,
776 	.release	= ovl_release,
777 	.llseek		= ovl_llseek,
778 	.read_iter	= ovl_read_iter,
779 	.write_iter	= ovl_write_iter,
780 	.fsync		= ovl_fsync,
781 	.mmap		= ovl_mmap,
782 	.fallocate	= ovl_fallocate,
783 	.fadvise	= ovl_fadvise,
784 	.unlocked_ioctl	= ovl_ioctl,
785 	.compat_ioctl	= ovl_compat_ioctl,
786 	.splice_read    = ovl_splice_read,
787 	.splice_write   = ovl_splice_write,
788 
789 	.copy_file_range	= ovl_copy_file_range,
790 	.remap_file_range	= ovl_remap_file_range,
791 };
792 
793 int __init ovl_aio_request_cache_init(void)
794 {
795 	ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
796 						   sizeof(struct ovl_aio_req),
797 						   0, SLAB_HWCACHE_ALIGN, NULL);
798 	if (!ovl_aio_request_cachep)
799 		return -ENOMEM;
800 
801 	return 0;
802 }
803 
804 void ovl_aio_request_cache_destroy(void)
805 {
806 	kmem_cache_destroy(ovl_aio_request_cachep);
807 }
808