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