1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2017 Red Hat, Inc.
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>
15 #include "overlayfs.h"
19 struct kiocb
*orig_iocb
;
23 static struct kmem_cache
*ovl_aio_request_cachep
;
25 static char ovl_whatisit(struct inode
*inode
, struct inode
*realinode
)
27 if (realinode
!= ovl_inode_upper(inode
))
29 if (ovl_has_upperdata(inode
))
35 static struct file
*ovl_open_realfile(const struct file
*file
,
36 struct inode
*realinode
)
38 struct inode
*inode
= file_inode(file
);
39 struct file
*realfile
;
40 const struct cred
*old_cred
;
41 int flags
= file
->f_flags
| O_NOATIME
| FMODE_NONOTIFY
;
43 old_cred
= ovl_override_creds(inode
->i_sb
);
44 realfile
= open_with_fake_path(&file
->f_path
, flags
, realinode
,
46 revert_creds(old_cred
);
48 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
49 file
, file
, ovl_whatisit(inode
, realinode
), file
->f_flags
,
50 realfile
, IS_ERR(realfile
) ? 0 : realfile
->f_flags
);
55 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
57 static int ovl_change_flags(struct file
*file
, unsigned int flags
)
59 struct inode
*inode
= file_inode(file
);
62 /* No atime modificaton on underlying */
63 flags
|= O_NOATIME
| FMODE_NONOTIFY
;
65 /* If some flag changed that cannot be changed then something's amiss */
66 if (WARN_ON((file
->f_flags
^ flags
) & ~OVL_SETFL_MASK
))
69 flags
&= OVL_SETFL_MASK
;
71 if (((flags
^ file
->f_flags
) & O_APPEND
) && IS_APPEND(inode
))
74 if (flags
& O_DIRECT
) {
75 if (!file
->f_mapping
->a_ops
||
76 !file
->f_mapping
->a_ops
->direct_IO
)
80 if (file
->f_op
->check_flags
) {
81 err
= file
->f_op
->check_flags(flags
);
86 spin_lock(&file
->f_lock
);
87 file
->f_flags
= (file
->f_flags
& ~OVL_SETFL_MASK
) | flags
;
88 spin_unlock(&file
->f_lock
);
93 static int ovl_real_fdget_meta(const struct file
*file
, struct fd
*real
,
96 struct inode
*inode
= file_inode(file
);
97 struct inode
*realinode
;
100 real
->file
= file
->private_data
;
103 realinode
= ovl_inode_real(inode
);
105 realinode
= ovl_inode_realdata(inode
);
107 /* Has it been copied up since we'd opened it? */
108 if (unlikely(file_inode(real
->file
) != realinode
)) {
109 real
->flags
= FDPUT_FPUT
;
110 real
->file
= ovl_open_realfile(file
, realinode
);
112 return PTR_ERR_OR_ZERO(real
->file
);
115 /* Did the flags change since open? */
116 if (unlikely((file
->f_flags
^ real
->file
->f_flags
) & ~O_NOATIME
))
117 return ovl_change_flags(real
->file
, file
->f_flags
);
122 static int ovl_real_fdget(const struct file
*file
, struct fd
*real
)
124 return ovl_real_fdget_meta(file
, real
, false);
127 static int ovl_open(struct inode
*inode
, struct file
*file
)
129 struct file
*realfile
;
132 err
= ovl_maybe_copy_up(file_dentry(file
), file
->f_flags
);
136 /* No longer need these flags, so don't pass them on to underlying fs */
137 file
->f_flags
&= ~(O_CREAT
| O_EXCL
| O_NOCTTY
| O_TRUNC
);
139 realfile
= ovl_open_realfile(file
, ovl_inode_realdata(inode
));
140 if (IS_ERR(realfile
))
141 return PTR_ERR(realfile
);
143 file
->private_data
= realfile
;
148 static int ovl_release(struct inode
*inode
, struct file
*file
)
150 fput(file
->private_data
);
155 static loff_t
ovl_llseek(struct file
*file
, loff_t offset
, int whence
)
157 struct inode
*inode
= file_inode(file
);
159 const struct cred
*old_cred
;
163 * The two special cases below do not need to involve real fs,
164 * so we can optimizing concurrent callers.
167 if (whence
== SEEK_CUR
)
170 if (whence
== SEEK_SET
)
171 return vfs_setpos(file
, 0, 0);
174 ret
= ovl_real_fdget(file
, &real
);
179 * Overlay file f_pos is the master copy that is preserved
180 * through copy up and modified on read/write, but only real
181 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
182 * limitations that are more strict than ->s_maxbytes for specific
183 * files, so we use the real file to perform seeks.
185 ovl_inode_lock(inode
);
186 real
.file
->f_pos
= file
->f_pos
;
188 old_cred
= ovl_override_creds(inode
->i_sb
);
189 ret
= vfs_llseek(real
.file
, offset
, whence
);
190 revert_creds(old_cred
);
192 file
->f_pos
= real
.file
->f_pos
;
193 ovl_inode_unlock(inode
);
200 static void ovl_file_accessed(struct file
*file
)
202 struct inode
*inode
, *upperinode
;
204 if (file
->f_flags
& O_NOATIME
)
207 inode
= file_inode(file
);
208 upperinode
= ovl_inode_upper(inode
);
213 if ((!timespec64_equal(&inode
->i_mtime
, &upperinode
->i_mtime
) ||
214 !timespec64_equal(&inode
->i_ctime
, &upperinode
->i_ctime
))) {
215 inode
->i_mtime
= upperinode
->i_mtime
;
216 inode
->i_ctime
= upperinode
->i_ctime
;
219 touch_atime(&file
->f_path
);
222 static rwf_t
ovl_iocb_to_rwf(struct kiocb
*iocb
)
224 int ifl
= iocb
->ki_flags
;
227 if (ifl
& IOCB_NOWAIT
)
229 if (ifl
& IOCB_HIPRI
)
231 if (ifl
& IOCB_DSYNC
)
239 static void ovl_aio_cleanup_handler(struct ovl_aio_req
*aio_req
)
241 struct kiocb
*iocb
= &aio_req
->iocb
;
242 struct kiocb
*orig_iocb
= aio_req
->orig_iocb
;
244 if (iocb
->ki_flags
& IOCB_WRITE
) {
245 struct inode
*inode
= file_inode(orig_iocb
->ki_filp
);
247 file_end_write(iocb
->ki_filp
);
248 ovl_copyattr(ovl_inode_real(inode
), inode
);
251 orig_iocb
->ki_pos
= iocb
->ki_pos
;
253 kmem_cache_free(ovl_aio_request_cachep
, aio_req
);
256 static void ovl_aio_rw_complete(struct kiocb
*iocb
, long res
, long res2
)
258 struct ovl_aio_req
*aio_req
= container_of(iocb
,
259 struct ovl_aio_req
, iocb
);
260 struct kiocb
*orig_iocb
= aio_req
->orig_iocb
;
262 ovl_aio_cleanup_handler(aio_req
);
263 orig_iocb
->ki_complete(orig_iocb
, res
, res2
);
266 static ssize_t
ovl_read_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
268 struct file
*file
= iocb
->ki_filp
;
270 const struct cred
*old_cred
;
273 if (!iov_iter_count(iter
))
276 ret
= ovl_real_fdget(file
, &real
);
280 old_cred
= ovl_override_creds(file_inode(file
)->i_sb
);
281 if (is_sync_kiocb(iocb
)) {
282 ret
= vfs_iter_read(real
.file
, iter
, &iocb
->ki_pos
,
283 ovl_iocb_to_rwf(iocb
));
285 struct ovl_aio_req
*aio_req
;
288 aio_req
= kmem_cache_zalloc(ovl_aio_request_cachep
, GFP_KERNEL
);
294 aio_req
->orig_iocb
= iocb
;
295 kiocb_clone(&aio_req
->iocb
, iocb
, real
.file
);
296 aio_req
->iocb
.ki_complete
= ovl_aio_rw_complete
;
297 ret
= vfs_iocb_iter_read(real
.file
, &aio_req
->iocb
, iter
);
298 if (ret
!= -EIOCBQUEUED
)
299 ovl_aio_cleanup_handler(aio_req
);
302 revert_creds(old_cred
);
303 ovl_file_accessed(file
);
310 static ssize_t
ovl_write_iter(struct kiocb
*iocb
, struct iov_iter
*iter
)
312 struct file
*file
= iocb
->ki_filp
;
313 struct inode
*inode
= file_inode(file
);
315 const struct cred
*old_cred
;
318 if (!iov_iter_count(iter
))
323 ovl_copyattr(ovl_inode_real(inode
), inode
);
324 ret
= file_remove_privs(file
);
328 ret
= ovl_real_fdget(file
, &real
);
332 old_cred
= ovl_override_creds(file_inode(file
)->i_sb
);
333 if (is_sync_kiocb(iocb
)) {
334 file_start_write(real
.file
);
335 ret
= vfs_iter_write(real
.file
, iter
, &iocb
->ki_pos
,
336 ovl_iocb_to_rwf(iocb
));
337 file_end_write(real
.file
);
339 ovl_copyattr(ovl_inode_real(inode
), inode
);
341 struct ovl_aio_req
*aio_req
;
344 aio_req
= kmem_cache_zalloc(ovl_aio_request_cachep
, GFP_KERNEL
);
348 file_start_write(real
.file
);
351 aio_req
->orig_iocb
= iocb
;
352 kiocb_clone(&aio_req
->iocb
, iocb
, real
.file
);
353 aio_req
->iocb
.ki_complete
= ovl_aio_rw_complete
;
354 ret
= vfs_iocb_iter_write(real
.file
, &aio_req
->iocb
, iter
);
355 if (ret
!= -EIOCBQUEUED
)
356 ovl_aio_cleanup_handler(aio_req
);
359 revert_creds(old_cred
);
368 static ssize_t
ovl_splice_read(struct file
*in
, loff_t
*ppos
,
369 struct pipe_inode_info
*pipe
, size_t len
,
374 const struct cred
*old_cred
;
376 ret
= ovl_real_fdget(in
, &real
);
380 old_cred
= ovl_override_creds(file_inode(in
)->i_sb
);
381 ret
= generic_file_splice_read(real
.file
, ppos
, pipe
, len
, flags
);
382 revert_creds(old_cred
);
384 ovl_file_accessed(in
);
390 ovl_splice_write(struct pipe_inode_info
*pipe
, struct file
*out
,
391 loff_t
*ppos
, size_t len
, unsigned int flags
)
394 const struct cred
*old_cred
;
397 ret
= ovl_real_fdget(out
, &real
);
401 old_cred
= ovl_override_creds(file_inode(out
)->i_sb
);
402 ret
= iter_file_splice_write(pipe
, real
.file
, ppos
, len
, flags
);
403 revert_creds(old_cred
);
405 ovl_file_accessed(out
);
410 static int ovl_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
413 const struct cred
*old_cred
;
416 ret
= ovl_real_fdget_meta(file
, &real
, !datasync
);
420 /* Don't sync lower file for fear of receiving EROFS error */
421 if (file_inode(real
.file
) == ovl_inode_upper(file_inode(file
))) {
422 old_cred
= ovl_override_creds(file_inode(file
)->i_sb
);
423 ret
= vfs_fsync_range(real
.file
, start
, end
, datasync
);
424 revert_creds(old_cred
);
432 static int ovl_mmap(struct file
*file
, struct vm_area_struct
*vma
)
434 struct file
*realfile
= file
->private_data
;
435 const struct cred
*old_cred
;
438 if (!realfile
->f_op
->mmap
)
441 if (WARN_ON(file
!= vma
->vm_file
))
444 vma
->vm_file
= get_file(realfile
);
446 old_cred
= ovl_override_creds(file_inode(file
)->i_sb
);
447 ret
= call_mmap(vma
->vm_file
, vma
);
448 revert_creds(old_cred
);
451 /* Drop reference count from new vm_file value */
454 /* Drop reference count from previous vm_file value */
458 ovl_file_accessed(file
);
463 static long ovl_fallocate(struct file
*file
, int mode
, loff_t offset
, loff_t len
)
465 struct inode
*inode
= file_inode(file
);
467 const struct cred
*old_cred
;
470 ret
= ovl_real_fdget(file
, &real
);
474 old_cred
= ovl_override_creds(file_inode(file
)->i_sb
);
475 ret
= vfs_fallocate(real
.file
, mode
, offset
, len
);
476 revert_creds(old_cred
);
479 ovl_copyattr(ovl_inode_real(inode
), inode
);
486 static int ovl_fadvise(struct file
*file
, loff_t offset
, loff_t len
, int advice
)
489 const struct cred
*old_cred
;
492 ret
= ovl_real_fdget(file
, &real
);
496 old_cred
= ovl_override_creds(file_inode(file
)->i_sb
);
497 ret
= vfs_fadvise(real
.file
, offset
, len
, advice
);
498 revert_creds(old_cred
);
505 static long ovl_real_ioctl(struct file
*file
, unsigned int cmd
,
509 const struct cred
*old_cred
;
512 ret
= ovl_real_fdget(file
, &real
);
516 old_cred
= ovl_override_creds(file_inode(file
)->i_sb
);
517 ret
= vfs_ioctl(real
.file
, cmd
, arg
);
518 revert_creds(old_cred
);
525 static long ovl_ioctl_set_flags(struct file
*file
, unsigned int cmd
,
526 unsigned long arg
, unsigned int iflags
)
529 struct inode
*inode
= file_inode(file
);
530 unsigned int old_iflags
;
532 if (!inode_owner_or_capable(inode
))
535 ret
= mnt_want_write_file(file
);
541 /* Check the capability before cred override */
543 old_iflags
= READ_ONCE(inode
->i_flags
);
544 if (((iflags
^ old_iflags
) & (S_APPEND
| S_IMMUTABLE
)) &&
545 !capable(CAP_LINUX_IMMUTABLE
))
548 ret
= ovl_maybe_copy_up(file_dentry(file
), O_WRONLY
);
552 ret
= ovl_real_ioctl(file
, cmd
, arg
);
554 ovl_copyflags(ovl_inode_real(inode
), inode
);
558 mnt_drop_write_file(file
);
564 static unsigned int ovl_fsflags_to_iflags(unsigned int flags
)
566 unsigned int iflags
= 0;
568 if (flags
& FS_SYNC_FL
)
570 if (flags
& FS_APPEND_FL
)
572 if (flags
& FS_IMMUTABLE_FL
)
573 iflags
|= S_IMMUTABLE
;
574 if (flags
& FS_NOATIME_FL
)
580 static long ovl_ioctl_set_fsflags(struct file
*file
, unsigned int cmd
,
585 if (get_user(flags
, (int __user
*) arg
))
588 return ovl_ioctl_set_flags(file
, cmd
, arg
,
589 ovl_fsflags_to_iflags(flags
));
592 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags
)
594 unsigned int iflags
= 0;
596 if (xflags
& FS_XFLAG_SYNC
)
598 if (xflags
& FS_XFLAG_APPEND
)
600 if (xflags
& FS_XFLAG_IMMUTABLE
)
601 iflags
|= S_IMMUTABLE
;
602 if (xflags
& FS_XFLAG_NOATIME
)
608 static long ovl_ioctl_set_fsxflags(struct file
*file
, unsigned int cmd
,
613 memset(&fa
, 0, sizeof(fa
));
614 if (copy_from_user(&fa
, (void __user
*) arg
, sizeof(fa
)))
617 return ovl_ioctl_set_flags(file
, cmd
, arg
,
618 ovl_fsxflags_to_iflags(fa
.fsx_xflags
));
621 static long ovl_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
626 case FS_IOC_GETFLAGS
:
627 case FS_IOC_FSGETXATTR
:
628 ret
= ovl_real_ioctl(file
, cmd
, arg
);
631 case FS_IOC_SETFLAGS
:
632 ret
= ovl_ioctl_set_fsflags(file
, cmd
, arg
);
635 case FS_IOC_FSSETXATTR
:
636 ret
= ovl_ioctl_set_fsxflags(file
, cmd
, arg
);
646 static long ovl_compat_ioctl(struct file
*file
, unsigned int cmd
,
650 case FS_IOC32_GETFLAGS
:
651 cmd
= FS_IOC_GETFLAGS
;
654 case FS_IOC32_SETFLAGS
:
655 cmd
= FS_IOC_SETFLAGS
;
662 return ovl_ioctl(file
, cmd
, arg
);
671 static loff_t
ovl_copyfile(struct file
*file_in
, loff_t pos_in
,
672 struct file
*file_out
, loff_t pos_out
,
673 loff_t len
, unsigned int flags
, enum ovl_copyop op
)
675 struct inode
*inode_out
= file_inode(file_out
);
676 struct fd real_in
, real_out
;
677 const struct cred
*old_cred
;
680 ret
= ovl_real_fdget(file_out
, &real_out
);
684 ret
= ovl_real_fdget(file_in
, &real_in
);
690 old_cred
= ovl_override_creds(file_inode(file_out
)->i_sb
);
693 ret
= vfs_copy_file_range(real_in
.file
, pos_in
,
694 real_out
.file
, pos_out
, len
, flags
);
698 ret
= vfs_clone_file_range(real_in
.file
, pos_in
,
699 real_out
.file
, pos_out
, len
, flags
);
703 ret
= vfs_dedupe_file_range_one(real_in
.file
, pos_in
,
704 real_out
.file
, pos_out
, len
,
708 revert_creds(old_cred
);
711 ovl_copyattr(ovl_inode_real(inode_out
), inode_out
);
719 static ssize_t
ovl_copy_file_range(struct file
*file_in
, loff_t pos_in
,
720 struct file
*file_out
, loff_t pos_out
,
721 size_t len
, unsigned int flags
)
723 return ovl_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
, flags
,
727 static loff_t
ovl_remap_file_range(struct file
*file_in
, loff_t pos_in
,
728 struct file
*file_out
, loff_t pos_out
,
729 loff_t len
, unsigned int remap_flags
)
733 if (remap_flags
& ~(REMAP_FILE_DEDUP
| REMAP_FILE_ADVISORY
))
736 if (remap_flags
& REMAP_FILE_DEDUP
)
742 * Don't copy up because of a dedupe request, this wouldn't make sense
743 * most of the time (data would be duplicated instead of deduplicated).
745 if (op
== OVL_DEDUPE
&&
746 (!ovl_inode_upper(file_inode(file_in
)) ||
747 !ovl_inode_upper(file_inode(file_out
))))
750 return ovl_copyfile(file_in
, pos_in
, file_out
, pos_out
, len
,
754 const struct file_operations ovl_file_operations
= {
756 .release
= ovl_release
,
757 .llseek
= ovl_llseek
,
758 .read_iter
= ovl_read_iter
,
759 .write_iter
= ovl_write_iter
,
762 .fallocate
= ovl_fallocate
,
763 .fadvise
= ovl_fadvise
,
764 .unlocked_ioctl
= ovl_ioctl
,
765 .compat_ioctl
= ovl_compat_ioctl
,
766 .splice_read
= ovl_splice_read
,
767 .splice_write
= ovl_splice_write
,
769 .copy_file_range
= ovl_copy_file_range
,
770 .remap_file_range
= ovl_remap_file_range
,
773 int __init
ovl_aio_request_cache_init(void)
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
)
784 void ovl_aio_request_cache_destroy(void)
786 kmem_cache_destroy(ovl_aio_request_cachep
);