2 * Copyright (C) 2017 Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
9 #include <linux/cred.h>
10 #include <linux/file.h>
11 #include <linux/mount.h>
12 #include <linux/xattr.h>
13 #include <linux/uio.h>
14 #include <linux/uaccess.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);
299 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
302 const struct cred *old_cred;
305 ret = ovl_real_fdget_meta(file, &real, !datasync);
309 /* Don't sync lower file for fear of receiving EROFS error */
310 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
311 old_cred = ovl_override_creds(file_inode(file)->i_sb);
312 ret = vfs_fsync_range(real.file, start, end, datasync);
313 revert_creds(old_cred);
321 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
323 struct file *realfile = file->private_data;
324 const struct cred *old_cred;
327 if (!realfile->f_op->mmap)
330 if (WARN_ON(file != vma->vm_file))
333 vma->vm_file = get_file(realfile);
335 old_cred = ovl_override_creds(file_inode(file)->i_sb);
336 ret = call_mmap(vma->vm_file, vma);
337 revert_creds(old_cred);
340 /* Drop reference count from new vm_file value */
343 /* Drop reference count from previous vm_file value */
347 ovl_file_accessed(file);
352 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
354 struct inode *inode = file_inode(file);
356 const struct cred *old_cred;
359 ret = ovl_real_fdget(file, &real);
363 old_cred = ovl_override_creds(file_inode(file)->i_sb);
364 ret = vfs_fallocate(real.file, mode, offset, len);
365 revert_creds(old_cred);
368 ovl_copyattr(ovl_inode_real(inode), inode);
375 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
378 const struct cred *old_cred;
381 ret = ovl_real_fdget(file, &real);
385 old_cred = ovl_override_creds(file_inode(file)->i_sb);
386 ret = vfs_fadvise(real.file, offset, len, advice);
387 revert_creds(old_cred);
394 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
398 const struct cred *old_cred;
401 ret = ovl_real_fdget(file, &real);
405 old_cred = ovl_override_creds(file_inode(file)->i_sb);
406 ret = vfs_ioctl(real.file, cmd, arg);
407 revert_creds(old_cred);
414 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
415 unsigned long arg, unsigned int iflags)
418 struct inode *inode = file_inode(file);
419 unsigned int old_iflags;
421 if (!inode_owner_or_capable(inode))
424 ret = mnt_want_write_file(file);
430 /* Check the capability before cred override */
432 old_iflags = READ_ONCE(inode->i_flags);
433 if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
434 !capable(CAP_LINUX_IMMUTABLE))
437 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
441 ret = ovl_real_ioctl(file, cmd, arg);
443 ovl_copyflags(ovl_inode_real(inode), inode);
447 mnt_drop_write_file(file);
453 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
455 unsigned int iflags = 0;
457 if (flags & FS_SYNC_FL)
459 if (flags & FS_APPEND_FL)
461 if (flags & FS_IMMUTABLE_FL)
462 iflags |= S_IMMUTABLE;
463 if (flags & FS_NOATIME_FL)
469 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
474 if (get_user(flags, (int __user *) arg))
477 return ovl_ioctl_set_flags(file, cmd, arg,
478 ovl_fsflags_to_iflags(flags));
481 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
483 unsigned int iflags = 0;
485 if (xflags & FS_XFLAG_SYNC)
487 if (xflags & FS_XFLAG_APPEND)
489 if (xflags & FS_XFLAG_IMMUTABLE)
490 iflags |= S_IMMUTABLE;
491 if (xflags & FS_XFLAG_NOATIME)
497 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
502 memset(&fa, 0, sizeof(fa));
503 if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
506 return ovl_ioctl_set_flags(file, cmd, arg,
507 ovl_fsxflags_to_iflags(fa.fsx_xflags));
510 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
515 case FS_IOC_GETFLAGS:
516 case FS_IOC_FSGETXATTR:
517 ret = ovl_real_ioctl(file, cmd, arg);
520 case FS_IOC_SETFLAGS:
521 ret = ovl_ioctl_set_fsflags(file, cmd, arg);
524 case FS_IOC_FSSETXATTR:
525 ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
535 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
539 case FS_IOC32_GETFLAGS:
540 cmd = FS_IOC_GETFLAGS;
543 case FS_IOC32_SETFLAGS:
544 cmd = FS_IOC_SETFLAGS;
551 return ovl_ioctl(file, cmd, arg);
560 static ssize_t ovl_copyfile(struct file *file_in, loff_t pos_in,
561 struct file *file_out, loff_t pos_out,
562 u64 len, unsigned int flags, enum ovl_copyop op)
564 struct inode *inode_out = file_inode(file_out);
565 struct fd real_in, real_out;
566 const struct cred *old_cred;
569 ret = ovl_real_fdget(file_out, &real_out);
573 ret = ovl_real_fdget(file_in, &real_in);
579 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
582 ret = vfs_copy_file_range(real_in.file, pos_in,
583 real_out.file, pos_out, len, flags);
587 ret = vfs_clone_file_range(real_in.file, pos_in,
588 real_out.file, pos_out, len);
592 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
593 real_out.file, pos_out, len);
596 revert_creds(old_cred);
599 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
607 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
608 struct file *file_out, loff_t pos_out,
609 size_t len, unsigned int flags)
611 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
615 static int ovl_clone_file_range(struct file *file_in, loff_t pos_in,
616 struct file *file_out, loff_t pos_out, u64 len)
618 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0,
622 static int ovl_dedupe_file_range(struct file *file_in, loff_t pos_in,
623 struct file *file_out, loff_t pos_out, u64 len)
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 (!ovl_inode_upper(file_inode(file_in)) ||
630 !ovl_inode_upper(file_inode(file_out)))
633 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, 0,
637 const struct file_operations ovl_file_operations = {
639 .release = ovl_release,
640 .llseek = ovl_llseek,
641 .read_iter = ovl_read_iter,
642 .write_iter = ovl_write_iter,
645 .fallocate = ovl_fallocate,
646 .fadvise = ovl_fadvise,
647 .unlocked_ioctl = ovl_ioctl,
648 .compat_ioctl = ovl_compat_ioctl,
650 .copy_file_range = ovl_copy_file_range,
651 .clone_file_range = ovl_clone_file_range,
652 .dedupe_file_range = ovl_dedupe_file_range,