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"
17 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
19 if (realinode != ovl_inode_upper(inode))
21 if (ovl_has_upperdata(inode))
27 /* No atime modificaton nor notify on underlying */
28 #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
30 static struct file *ovl_open_realfile(const struct file *file,
31 struct inode *realinode)
33 struct inode *inode = file_inode(file);
34 struct file *realfile;
35 const struct cred *old_cred;
36 int flags = file->f_flags | OVL_OPEN_FLAGS;
38 old_cred = ovl_override_creds(inode->i_sb);
39 realfile = open_with_fake_path(&file->f_path, flags, realinode,
41 revert_creds(old_cred);
43 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
44 file, file, ovl_whatisit(inode, realinode), file->f_flags,
45 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
50 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
52 static int ovl_change_flags(struct file *file, unsigned int flags)
54 struct inode *inode = file_inode(file);
57 flags |= OVL_OPEN_FLAGS;
59 /* If some flag changed that cannot be changed then something's amiss */
60 if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
63 flags &= OVL_SETFL_MASK;
65 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
68 if (flags & O_DIRECT) {
69 if (!file->f_mapping->a_ops ||
70 !file->f_mapping->a_ops->direct_IO)
74 if (file->f_op->check_flags) {
75 err = file->f_op->check_flags(flags);
80 spin_lock(&file->f_lock);
81 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
82 spin_unlock(&file->f_lock);
87 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
90 struct inode *inode = file_inode(file);
91 struct inode *realinode;
94 real->file = file->private_data;
97 realinode = ovl_inode_real(inode);
99 realinode = ovl_inode_realdata(inode);
101 /* Has it been copied up since we'd opened it? */
102 if (unlikely(file_inode(real->file) != realinode)) {
103 real->flags = FDPUT_FPUT;
104 real->file = ovl_open_realfile(file, realinode);
106 return PTR_ERR_OR_ZERO(real->file);
109 /* Did the flags change since open? */
110 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
111 return ovl_change_flags(real->file, file->f_flags);
116 static int ovl_real_fdget(const struct file *file, struct fd *real)
118 return ovl_real_fdget_meta(file, real, false);
121 static int ovl_open(struct inode *inode, struct file *file)
123 struct file *realfile;
126 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
130 /* No longer need these flags, so don't pass them on to underlying fs */
131 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
133 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
134 if (IS_ERR(realfile))
135 return PTR_ERR(realfile);
137 file->private_data = realfile;
142 static int ovl_release(struct inode *inode, struct file *file)
144 fput(file->private_data);
149 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
151 struct inode *inode = file_inode(file);
153 const struct cred *old_cred;
157 * The two special cases below do not need to involve real fs,
158 * so we can optimizing concurrent callers.
161 if (whence == SEEK_CUR)
164 if (whence == SEEK_SET)
165 return vfs_setpos(file, 0, 0);
168 ret = ovl_real_fdget(file, &real);
173 * Overlay file f_pos is the master copy that is preserved
174 * through copy up and modified on read/write, but only real
175 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
176 * limitations that are more strict than ->s_maxbytes for specific
177 * files, so we use the real file to perform seeks.
180 real.file->f_pos = file->f_pos;
182 old_cred = ovl_override_creds(inode->i_sb);
183 ret = vfs_llseek(real.file, offset, whence);
184 revert_creds(old_cred);
186 file->f_pos = real.file->f_pos;
194 static void ovl_file_accessed(struct file *file)
196 struct inode *inode, *upperinode;
198 if (file->f_flags & O_NOATIME)
201 inode = file_inode(file);
202 upperinode = ovl_inode_upper(inode);
207 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
208 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
209 inode->i_mtime = upperinode->i_mtime;
210 inode->i_ctime = upperinode->i_ctime;
213 touch_atime(&file->f_path);
216 static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb)
218 int ifl = iocb->ki_flags;
221 if (ifl & IOCB_NOWAIT)
223 if (ifl & IOCB_HIPRI)
225 if (ifl & IOCB_DSYNC)
233 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
235 struct file *file = iocb->ki_filp;
237 const struct cred *old_cred;
240 if (!iov_iter_count(iter))
243 ret = ovl_real_fdget(file, &real);
247 old_cred = ovl_override_creds(file_inode(file)->i_sb);
248 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
249 ovl_iocb_to_rwf(iocb));
250 revert_creds(old_cred);
252 ovl_file_accessed(file);
259 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
261 struct file *file = iocb->ki_filp;
262 struct inode *inode = file_inode(file);
264 const struct cred *old_cred;
267 if (!iov_iter_count(iter))
272 ovl_copyattr(ovl_inode_real(inode), inode);
273 ret = file_remove_privs(file);
277 ret = ovl_real_fdget(file, &real);
281 old_cred = ovl_override_creds(file_inode(file)->i_sb);
282 file_start_write(real.file);
283 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
284 ovl_iocb_to_rwf(iocb));
285 file_end_write(real.file);
286 revert_creds(old_cred);
289 ovl_copyattr(ovl_inode_real(inode), inode);
300 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
301 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
302 * and file_start_write(real.file) in ovl_write_iter().
304 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
307 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
308 loff_t *ppos, size_t len, unsigned int flags)
311 const struct cred *old_cred;
312 struct inode *inode = file_inode(out);
313 struct inode *realinode = ovl_inode_real(inode);
318 ovl_copyattr(realinode, inode);
319 ret = file_remove_privs(out);
323 ret = ovl_real_fdget(out, &real);
327 old_cred = ovl_override_creds(inode->i_sb);
328 file_start_write(real.file);
330 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
332 file_end_write(real.file);
334 ovl_copyattr(realinode, inode);
335 revert_creds(old_cred);
344 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
347 const struct cred *old_cred;
350 ret = ovl_real_fdget_meta(file, &real, !datasync);
354 /* Don't sync lower file for fear of receiving EROFS error */
355 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
356 old_cred = ovl_override_creds(file_inode(file)->i_sb);
357 ret = vfs_fsync_range(real.file, start, end, datasync);
358 revert_creds(old_cred);
366 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
368 struct file *realfile = file->private_data;
369 const struct cred *old_cred;
372 if (!realfile->f_op->mmap)
375 if (WARN_ON(file != vma->vm_file))
378 vma->vm_file = get_file(realfile);
380 old_cred = ovl_override_creds(file_inode(file)->i_sb);
381 ret = call_mmap(vma->vm_file, vma);
382 revert_creds(old_cred);
385 /* Drop reference count from new vm_file value */
388 /* Drop reference count from previous vm_file value */
392 ovl_file_accessed(file);
397 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
399 struct inode *inode = file_inode(file);
401 const struct cred *old_cred;
404 ret = ovl_real_fdget(file, &real);
408 old_cred = ovl_override_creds(file_inode(file)->i_sb);
409 ret = vfs_fallocate(real.file, mode, offset, len);
410 revert_creds(old_cred);
413 ovl_copyattr(ovl_inode_real(inode), inode);
420 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
423 const struct cred *old_cred;
426 ret = ovl_real_fdget(file, &real);
430 old_cred = ovl_override_creds(file_inode(file)->i_sb);
431 ret = vfs_fadvise(real.file, offset, len, advice);
432 revert_creds(old_cred);
439 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
443 const struct cred *old_cred;
446 ret = ovl_real_fdget(file, &real);
450 old_cred = ovl_override_creds(file_inode(file)->i_sb);
451 ret = vfs_ioctl(real.file, cmd, arg);
452 revert_creds(old_cred);
459 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
460 unsigned long arg, unsigned int iflags)
463 struct inode *inode = file_inode(file);
464 unsigned int old_iflags;
466 if (!inode_owner_or_capable(inode))
469 ret = mnt_want_write_file(file);
475 /* Check the capability before cred override */
477 old_iflags = READ_ONCE(inode->i_flags);
478 if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
479 !capable(CAP_LINUX_IMMUTABLE))
482 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
486 ret = ovl_real_ioctl(file, cmd, arg);
488 ovl_copyflags(ovl_inode_real(inode), inode);
492 mnt_drop_write_file(file);
498 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
500 unsigned int iflags = 0;
502 if (flags & FS_SYNC_FL)
504 if (flags & FS_APPEND_FL)
506 if (flags & FS_IMMUTABLE_FL)
507 iflags |= S_IMMUTABLE;
508 if (flags & FS_NOATIME_FL)
514 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
519 if (get_user(flags, (int __user *) arg))
522 return ovl_ioctl_set_flags(file, cmd, arg,
523 ovl_fsflags_to_iflags(flags));
526 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
528 unsigned int iflags = 0;
530 if (xflags & FS_XFLAG_SYNC)
532 if (xflags & FS_XFLAG_APPEND)
534 if (xflags & FS_XFLAG_IMMUTABLE)
535 iflags |= S_IMMUTABLE;
536 if (xflags & FS_XFLAG_NOATIME)
542 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
547 memset(&fa, 0, sizeof(fa));
548 if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
551 return ovl_ioctl_set_flags(file, cmd, arg,
552 ovl_fsxflags_to_iflags(fa.fsx_xflags));
555 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
560 case FS_IOC_GETFLAGS:
561 case FS_IOC_FSGETXATTR:
562 ret = ovl_real_ioctl(file, cmd, arg);
565 case FS_IOC_SETFLAGS:
566 ret = ovl_ioctl_set_fsflags(file, cmd, arg);
569 case FS_IOC_FSSETXATTR:
570 ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
580 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
584 case FS_IOC32_GETFLAGS:
585 cmd = FS_IOC_GETFLAGS;
588 case FS_IOC32_SETFLAGS:
589 cmd = FS_IOC_SETFLAGS;
596 return ovl_ioctl(file, cmd, arg);
605 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
606 struct file *file_out, loff_t pos_out,
607 loff_t len, unsigned int flags, enum ovl_copyop op)
609 struct inode *inode_out = file_inode(file_out);
610 struct fd real_in, real_out;
611 const struct cred *old_cred;
614 ret = ovl_real_fdget(file_out, &real_out);
618 ret = ovl_real_fdget(file_in, &real_in);
624 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
627 ret = vfs_copy_file_range(real_in.file, pos_in,
628 real_out.file, pos_out, len, flags);
632 ret = vfs_clone_file_range(real_in.file, pos_in,
633 real_out.file, pos_out, len, flags);
637 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
638 real_out.file, pos_out, len,
642 revert_creds(old_cred);
645 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
653 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
654 struct file *file_out, loff_t pos_out,
655 size_t len, unsigned int flags)
657 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
661 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
662 struct file *file_out, loff_t pos_out,
663 loff_t len, unsigned int remap_flags)
667 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
670 if (remap_flags & REMAP_FILE_DEDUP)
676 * Don't copy up because of a dedupe request, this wouldn't make sense
677 * most of the time (data would be duplicated instead of deduplicated).
679 if (op == OVL_DEDUPE &&
680 (!ovl_inode_upper(file_inode(file_in)) ||
681 !ovl_inode_upper(file_inode(file_out))))
684 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
688 const struct file_operations ovl_file_operations = {
690 .release = ovl_release,
691 .llseek = ovl_llseek,
692 .read_iter = ovl_read_iter,
693 .write_iter = ovl_write_iter,
696 .fallocate = ovl_fallocate,
697 .fadvise = ovl_fadvise,
698 .unlocked_ioctl = ovl_ioctl,
699 .compat_ioctl = ovl_compat_ioctl,
700 .splice_read = generic_file_splice_read,
701 .splice_write = ovl_splice_write,
703 .copy_file_range = ovl_copy_file_range,
704 .remap_file_range = ovl_remap_file_range,