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 "overlayfs.h"
14 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
16 if (realinode != ovl_inode_upper(inode))
18 if (ovl_has_upperdata(inode))
24 static struct file *ovl_open_realfile(const struct file *file,
25 struct inode *realinode)
27 struct inode *inode = file_inode(file);
28 struct file *realfile;
29 const struct cred *old_cred;
30 int flags = file->f_flags | O_NOATIME | FMODE_NONOTIFY;
32 old_cred = ovl_override_creds(inode->i_sb);
33 realfile = open_with_fake_path(&file->f_path, flags, realinode,
35 revert_creds(old_cred);
37 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
38 file, file, ovl_whatisit(inode, realinode), file->f_flags,
39 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
44 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
46 static int ovl_change_flags(struct file *file, unsigned int flags)
48 struct inode *inode = file_inode(file);
51 /* No atime modificaton on underlying */
52 flags |= O_NOATIME | FMODE_NONOTIFY;
54 /* If some flag changed that cannot be changed then something's amiss */
55 if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
58 flags &= OVL_SETFL_MASK;
60 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
63 if (flags & O_DIRECT) {
64 if (!file->f_mapping->a_ops ||
65 !file->f_mapping->a_ops->direct_IO)
69 if (file->f_op->check_flags) {
70 err = file->f_op->check_flags(flags);
75 spin_lock(&file->f_lock);
76 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
77 spin_unlock(&file->f_lock);
82 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
85 struct inode *inode = file_inode(file);
86 struct inode *realinode;
89 real->file = file->private_data;
92 realinode = ovl_inode_real(inode);
94 realinode = ovl_inode_realdata(inode);
96 /* Has it been copied up since we'd opened it? */
97 if (unlikely(file_inode(real->file) != realinode)) {
98 real->flags = FDPUT_FPUT;
99 real->file = ovl_open_realfile(file, realinode);
101 return PTR_ERR_OR_ZERO(real->file);
104 /* Did the flags change since open? */
105 if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME))
106 return ovl_change_flags(real->file, file->f_flags);
111 static int ovl_real_fdget(const struct file *file, struct fd *real)
113 return ovl_real_fdget_meta(file, real, false);
116 static int ovl_open(struct inode *inode, struct file *file)
118 struct file *realfile;
121 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
125 /* No longer need these flags, so don't pass them on to underlying fs */
126 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
128 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
129 if (IS_ERR(realfile))
130 return PTR_ERR(realfile);
132 file->private_data = realfile;
137 static int ovl_release(struct inode *inode, struct file *file)
139 fput(file->private_data);
144 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
146 struct inode *inode = file_inode(file);
148 const struct cred *old_cred;
152 * The two special cases below do not need to involve real fs,
153 * so we can optimizing concurrent callers.
156 if (whence == SEEK_CUR)
159 if (whence == SEEK_SET)
160 return vfs_setpos(file, 0, 0);
163 ret = ovl_real_fdget(file, &real);
168 * Overlay file f_pos is the master copy that is preserved
169 * through copy up and modified on read/write, but only real
170 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
171 * limitations that are more strict than ->s_maxbytes for specific
172 * files, so we use the real file to perform seeks.
175 real.file->f_pos = file->f_pos;
177 old_cred = ovl_override_creds(inode->i_sb);
178 ret = vfs_llseek(real.file, offset, whence);
179 revert_creds(old_cred);
181 file->f_pos = real.file->f_pos;
189 static void ovl_file_accessed(struct file *file)
191 struct inode *inode, *upperinode;
193 if (file->f_flags & O_NOATIME)
196 inode = file_inode(file);
197 upperinode = ovl_inode_upper(inode);
202 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
203 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
204 inode->i_mtime = upperinode->i_mtime;
205 inode->i_ctime = upperinode->i_ctime;
208 touch_atime(&file->f_path);
211 static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb)
213 int ifl = iocb->ki_flags;
216 if (ifl & IOCB_NOWAIT)
218 if (ifl & IOCB_HIPRI)
220 if (ifl & IOCB_DSYNC)
228 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
230 struct file *file = iocb->ki_filp;
232 const struct cred *old_cred;
235 if (!iov_iter_count(iter))
238 ret = ovl_real_fdget(file, &real);
242 old_cred = ovl_override_creds(file_inode(file)->i_sb);
243 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
244 ovl_iocb_to_rwf(iocb));
245 revert_creds(old_cred);
247 ovl_file_accessed(file);
254 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
256 struct file *file = iocb->ki_filp;
257 struct inode *inode = file_inode(file);
259 const struct cred *old_cred;
262 if (!iov_iter_count(iter))
267 ovl_copyattr(ovl_inode_real(inode), inode);
268 ret = file_remove_privs(file);
272 ret = ovl_real_fdget(file, &real);
276 old_cred = ovl_override_creds(file_inode(file)->i_sb);
277 file_start_write(real.file);
278 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
279 ovl_iocb_to_rwf(iocb));
280 file_end_write(real.file);
281 revert_creds(old_cred);
284 ovl_copyattr(ovl_inode_real(inode), inode);
294 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
297 const struct cred *old_cred;
300 ret = ovl_real_fdget_meta(file, &real, !datasync);
304 /* Don't sync lower file for fear of receiving EROFS error */
305 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
306 old_cred = ovl_override_creds(file_inode(file)->i_sb);
307 ret = vfs_fsync_range(real.file, start, end, datasync);
308 revert_creds(old_cred);
316 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
318 struct file *realfile = file->private_data;
319 const struct cred *old_cred;
322 if (!realfile->f_op->mmap)
325 if (WARN_ON(file != vma->vm_file))
328 vma->vm_file = get_file(realfile);
330 old_cred = ovl_override_creds(file_inode(file)->i_sb);
331 ret = call_mmap(vma->vm_file, vma);
332 revert_creds(old_cred);
335 /* Drop reference count from new vm_file value */
338 /* Drop reference count from previous vm_file value */
342 ovl_file_accessed(file);
347 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
349 struct inode *inode = file_inode(file);
351 const struct cred *old_cred;
354 ret = ovl_real_fdget(file, &real);
358 old_cred = ovl_override_creds(file_inode(file)->i_sb);
359 ret = vfs_fallocate(real.file, mode, offset, len);
360 revert_creds(old_cred);
363 ovl_copyattr(ovl_inode_real(inode), inode);
370 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
373 const struct cred *old_cred;
376 ret = ovl_real_fdget(file, &real);
380 old_cred = ovl_override_creds(file_inode(file)->i_sb);
381 ret = vfs_fadvise(real.file, offset, len, advice);
382 revert_creds(old_cred);
389 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
393 const struct cred *old_cred;
396 ret = ovl_real_fdget(file, &real);
400 old_cred = ovl_override_creds(file_inode(file)->i_sb);
401 ret = vfs_ioctl(real.file, cmd, arg);
402 revert_creds(old_cred);
409 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
410 unsigned long arg, unsigned int iflags)
413 struct inode *inode = file_inode(file);
414 unsigned int old_iflags;
416 if (!inode_owner_or_capable(inode))
419 ret = mnt_want_write_file(file);
425 /* Check the capability before cred override */
427 old_iflags = READ_ONCE(inode->i_flags);
428 if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
429 !capable(CAP_LINUX_IMMUTABLE))
432 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
436 ret = ovl_real_ioctl(file, cmd, arg);
438 ovl_copyflags(ovl_inode_real(inode), inode);
442 mnt_drop_write_file(file);
448 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
450 unsigned int iflags = 0;
452 if (flags & FS_SYNC_FL)
454 if (flags & FS_APPEND_FL)
456 if (flags & FS_IMMUTABLE_FL)
457 iflags |= S_IMMUTABLE;
458 if (flags & FS_NOATIME_FL)
464 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
469 if (get_user(flags, (int __user *) arg))
472 return ovl_ioctl_set_flags(file, cmd, arg,
473 ovl_fsflags_to_iflags(flags));
476 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
478 unsigned int iflags = 0;
480 if (xflags & FS_XFLAG_SYNC)
482 if (xflags & FS_XFLAG_APPEND)
484 if (xflags & FS_XFLAG_IMMUTABLE)
485 iflags |= S_IMMUTABLE;
486 if (xflags & FS_XFLAG_NOATIME)
492 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
497 memset(&fa, 0, sizeof(fa));
498 if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
501 return ovl_ioctl_set_flags(file, cmd, arg,
502 ovl_fsxflags_to_iflags(fa.fsx_xflags));
505 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
510 case FS_IOC_GETFLAGS:
511 case FS_IOC_FSGETXATTR:
512 ret = ovl_real_ioctl(file, cmd, arg);
515 case FS_IOC_SETFLAGS:
516 ret = ovl_ioctl_set_fsflags(file, cmd, arg);
519 case FS_IOC_FSSETXATTR:
520 ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
530 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
534 case FS_IOC32_GETFLAGS:
535 cmd = FS_IOC_GETFLAGS;
538 case FS_IOC32_SETFLAGS:
539 cmd = FS_IOC_SETFLAGS;
546 return ovl_ioctl(file, cmd, arg);
555 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
556 struct file *file_out, loff_t pos_out,
557 loff_t len, unsigned int flags, enum ovl_copyop op)
559 struct inode *inode_out = file_inode(file_out);
560 struct fd real_in, real_out;
561 const struct cred *old_cred;
564 ret = ovl_real_fdget(file_out, &real_out);
568 ret = ovl_real_fdget(file_in, &real_in);
574 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
577 ret = vfs_copy_file_range(real_in.file, pos_in,
578 real_out.file, pos_out, len, flags);
582 ret = vfs_clone_file_range(real_in.file, pos_in,
583 real_out.file, pos_out, len, flags);
587 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
588 real_out.file, pos_out, len,
592 revert_creds(old_cred);
595 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
603 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
604 struct file *file_out, loff_t pos_out,
605 size_t len, unsigned int flags)
607 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
611 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
612 struct file *file_out, loff_t pos_out,
613 loff_t len, unsigned int remap_flags)
617 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
620 if (remap_flags & REMAP_FILE_DEDUP)
626 * Don't copy up because of a dedupe request, this wouldn't make sense
627 * most of the time (data would be duplicated instead of deduplicated).
629 if (op == OVL_DEDUPE &&
630 (!ovl_inode_upper(file_inode(file_in)) ||
631 !ovl_inode_upper(file_inode(file_out))))
634 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
638 const struct file_operations ovl_file_operations = {
640 .release = ovl_release,
641 .llseek = ovl_llseek,
642 .read_iter = ovl_read_iter,
643 .write_iter = ovl_write_iter,
646 .fallocate = ovl_fallocate,
647 .fadvise = ovl_fadvise,
648 .unlocked_ioctl = ovl_ioctl,
649 .compat_ioctl = ovl_compat_ioctl,
651 .copy_file_range = ovl_copy_file_range,
652 .remap_file_range = ovl_remap_file_range,