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