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>
13 #include <linux/security.h>
16 #include "overlayfs.h"
21 struct kiocb *orig_iocb;
24 static struct kmem_cache *ovl_aio_request_cachep;
26 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
28 if (realinode != ovl_inode_upper(inode))
30 if (ovl_has_upperdata(inode))
36 /* No atime modificaton nor notify on underlying */
37 #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
39 static struct file *ovl_open_realfile(const struct file *file,
40 struct inode *realinode)
42 struct inode *inode = file_inode(file);
43 struct file *realfile;
44 const struct cred *old_cred;
45 int flags = file->f_flags | OVL_OPEN_FLAGS;
46 int acc_mode = ACC_MODE(flags);
50 acc_mode |= MAY_APPEND;
52 old_cred = ovl_override_creds(inode->i_sb);
53 err = inode_permission(realinode, MAY_OPEN | acc_mode);
55 realfile = ERR_PTR(err);
56 } else if (!inode_owner_or_capable(realinode)) {
57 realfile = ERR_PTR(-EPERM);
59 realfile = open_with_fake_path(&file->f_path, flags, realinode,
62 revert_creds(old_cred);
64 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
65 file, file, ovl_whatisit(inode, realinode), file->f_flags,
66 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
71 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
73 static int ovl_change_flags(struct file *file, unsigned int flags)
75 struct inode *inode = file_inode(file);
78 flags |= OVL_OPEN_FLAGS;
80 /* If some flag changed that cannot be changed then something's amiss */
81 if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
84 flags &= OVL_SETFL_MASK;
86 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
89 if (flags & O_DIRECT) {
90 if (!file->f_mapping->a_ops ||
91 !file->f_mapping->a_ops->direct_IO)
95 if (file->f_op->check_flags) {
96 err = file->f_op->check_flags(flags);
101 spin_lock(&file->f_lock);
102 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
103 spin_unlock(&file->f_lock);
108 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
111 struct inode *inode = file_inode(file);
112 struct inode *realinode;
115 real->file = file->private_data;
118 realinode = ovl_inode_real(inode);
120 realinode = ovl_inode_realdata(inode);
122 /* Has it been copied up since we'd opened it? */
123 if (unlikely(file_inode(real->file) != realinode)) {
124 real->flags = FDPUT_FPUT;
125 real->file = ovl_open_realfile(file, realinode);
127 return PTR_ERR_OR_ZERO(real->file);
130 /* Did the flags change since open? */
131 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
132 return ovl_change_flags(real->file, file->f_flags);
137 static int ovl_real_fdget(const struct file *file, struct fd *real)
139 if (d_is_dir(file_dentry(file))) {
141 real->file = ovl_dir_real_file(file, false);
143 return PTR_ERR_OR_ZERO(real->file);
146 return ovl_real_fdget_meta(file, real, false);
149 static int ovl_open(struct inode *inode, struct file *file)
151 struct file *realfile;
154 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
158 /* No longer need these flags, so don't pass them on to underlying fs */
159 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
161 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
162 if (IS_ERR(realfile))
163 return PTR_ERR(realfile);
165 file->private_data = realfile;
170 static int ovl_release(struct inode *inode, struct file *file)
172 fput(file->private_data);
177 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
179 struct inode *inode = file_inode(file);
181 const struct cred *old_cred;
185 * The two special cases below do not need to involve real fs,
186 * so we can optimizing concurrent callers.
189 if (whence == SEEK_CUR)
192 if (whence == SEEK_SET)
193 return vfs_setpos(file, 0, 0);
196 ret = ovl_real_fdget(file, &real);
201 * Overlay file f_pos is the master copy that is preserved
202 * through copy up and modified on read/write, but only real
203 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
204 * limitations that are more strict than ->s_maxbytes for specific
205 * files, so we use the real file to perform seeks.
207 ovl_inode_lock(inode);
208 real.file->f_pos = file->f_pos;
210 old_cred = ovl_override_creds(inode->i_sb);
211 ret = vfs_llseek(real.file, offset, whence);
212 revert_creds(old_cred);
214 file->f_pos = real.file->f_pos;
215 ovl_inode_unlock(inode);
222 static void ovl_file_accessed(struct file *file)
224 struct inode *inode, *upperinode;
226 if (file->f_flags & O_NOATIME)
229 inode = file_inode(file);
230 upperinode = ovl_inode_upper(inode);
235 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
236 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
237 inode->i_mtime = upperinode->i_mtime;
238 inode->i_ctime = upperinode->i_ctime;
241 touch_atime(&file->f_path);
244 static rwf_t ovl_iocb_to_rwf(int ifl)
248 if (ifl & IOCB_NOWAIT)
250 if (ifl & IOCB_HIPRI)
252 if (ifl & IOCB_DSYNC)
260 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
262 if (refcount_dec_and_test(&aio_req->ref)) {
263 fput(aio_req->iocb.ki_filp);
264 kmem_cache_free(ovl_aio_request_cachep, aio_req);
268 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
270 struct kiocb *iocb = &aio_req->iocb;
271 struct kiocb *orig_iocb = aio_req->orig_iocb;
273 if (iocb->ki_flags & IOCB_WRITE) {
274 struct inode *inode = file_inode(orig_iocb->ki_filp);
276 /* Actually acquired in ovl_write_iter() */
277 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
279 file_end_write(iocb->ki_filp);
280 ovl_copyattr(ovl_inode_real(inode), inode);
283 orig_iocb->ki_pos = iocb->ki_pos;
284 ovl_aio_put(aio_req);
287 static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2)
289 struct ovl_aio_req *aio_req = container_of(iocb,
290 struct ovl_aio_req, iocb);
291 struct kiocb *orig_iocb = aio_req->orig_iocb;
293 ovl_aio_cleanup_handler(aio_req);
294 orig_iocb->ki_complete(orig_iocb, res, res2);
297 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
299 struct file *file = iocb->ki_filp;
301 const struct cred *old_cred;
304 if (!iov_iter_count(iter))
307 ret = ovl_real_fdget(file, &real);
312 if (iocb->ki_flags & IOCB_DIRECT &&
313 (!real.file->f_mapping->a_ops ||
314 !real.file->f_mapping->a_ops->direct_IO))
317 old_cred = ovl_override_creds(file_inode(file)->i_sb);
318 if (is_sync_kiocb(iocb)) {
319 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
320 ovl_iocb_to_rwf(iocb->ki_flags));
322 struct ovl_aio_req *aio_req;
325 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
330 aio_req->orig_iocb = iocb;
331 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
332 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
333 refcount_set(&aio_req->ref, 2);
334 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
335 ovl_aio_put(aio_req);
336 if (ret != -EIOCBQUEUED)
337 ovl_aio_cleanup_handler(aio_req);
340 revert_creds(old_cred);
341 ovl_file_accessed(file);
348 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
350 struct file *file = iocb->ki_filp;
351 struct inode *inode = file_inode(file);
353 const struct cred *old_cred;
355 int ifl = iocb->ki_flags;
357 if (!iov_iter_count(iter))
362 ovl_copyattr(ovl_inode_real(inode), inode);
363 ret = file_remove_privs(file);
367 ret = ovl_real_fdget(file, &real);
372 if (iocb->ki_flags & IOCB_DIRECT &&
373 (!real.file->f_mapping->a_ops ||
374 !real.file->f_mapping->a_ops->direct_IO))
377 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
378 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
380 old_cred = ovl_override_creds(file_inode(file)->i_sb);
381 if (is_sync_kiocb(iocb)) {
382 file_start_write(real.file);
383 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
384 ovl_iocb_to_rwf(ifl));
385 file_end_write(real.file);
387 ovl_copyattr(ovl_inode_real(inode), inode);
389 struct ovl_aio_req *aio_req;
392 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
396 file_start_write(real.file);
397 /* Pacify lockdep, same trick as done in aio_write() */
398 __sb_writers_release(file_inode(real.file)->i_sb,
401 aio_req->orig_iocb = iocb;
402 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
403 aio_req->iocb.ki_flags = ifl;
404 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
405 refcount_set(&aio_req->ref, 2);
406 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
407 ovl_aio_put(aio_req);
408 if (ret != -EIOCBQUEUED)
409 ovl_aio_cleanup_handler(aio_req);
412 revert_creds(old_cred);
423 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
424 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
425 * and file_start_write(real.file) in ovl_write_iter().
427 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
430 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
431 loff_t *ppos, size_t len, unsigned int flags)
434 const struct cred *old_cred;
435 struct inode *inode = file_inode(out);
436 struct inode *realinode = ovl_inode_real(inode);
441 ovl_copyattr(realinode, inode);
442 ret = file_remove_privs(out);
446 ret = ovl_real_fdget(out, &real);
450 old_cred = ovl_override_creds(inode->i_sb);
451 file_start_write(real.file);
453 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
455 file_end_write(real.file);
457 ovl_copyattr(realinode, inode);
458 revert_creds(old_cred);
467 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
470 const struct cred *old_cred;
473 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
477 ret = ovl_real_fdget_meta(file, &real, !datasync);
481 /* Don't sync lower file for fear of receiving EROFS error */
482 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
483 old_cred = ovl_override_creds(file_inode(file)->i_sb);
484 ret = vfs_fsync_range(real.file, start, end, datasync);
485 revert_creds(old_cred);
493 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
495 struct file *realfile = file->private_data;
496 const struct cred *old_cred;
499 if (!realfile->f_op->mmap)
502 if (WARN_ON(file != vma->vm_file))
505 vma->vm_file = get_file(realfile);
507 old_cred = ovl_override_creds(file_inode(file)->i_sb);
508 ret = call_mmap(vma->vm_file, vma);
509 revert_creds(old_cred);
512 /* Drop reference count from new vm_file value */
515 /* Drop reference count from previous vm_file value */
519 ovl_file_accessed(file);
524 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
526 struct inode *inode = file_inode(file);
528 const struct cred *old_cred;
533 ovl_copyattr(ovl_inode_real(inode), inode);
534 ret = file_remove_privs(file);
538 ret = ovl_real_fdget(file, &real);
542 old_cred = ovl_override_creds(file_inode(file)->i_sb);
543 ret = vfs_fallocate(real.file, mode, offset, len);
544 revert_creds(old_cred);
547 ovl_copyattr(ovl_inode_real(inode), inode);
557 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
560 const struct cred *old_cred;
563 ret = ovl_real_fdget(file, &real);
567 old_cred = ovl_override_creds(file_inode(file)->i_sb);
568 ret = vfs_fadvise(real.file, offset, len, advice);
569 revert_creds(old_cred);
576 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
582 ret = ovl_real_fdget(file, &real);
586 ret = security_file_ioctl(real.file, cmd, arg);
589 * Don't override creds, since we currently can't safely check
590 * permissions before doing so.
592 ret = vfs_ioctl(real.file, cmd, arg);
600 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
604 struct inode *inode = file_inode(file);
606 if (!inode_owner_or_capable(inode))
609 ret = mnt_want_write_file(file);
616 * Prevent copy up if immutable and has no CAP_LINUX_IMMUTABLE
620 if (!ovl_has_upperdata(inode) && IS_IMMUTABLE(inode) &&
621 !capable(CAP_LINUX_IMMUTABLE))
624 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
628 ret = ovl_real_ioctl(file, cmd, arg);
630 ovl_copyflags(ovl_inode_real(inode), inode);
634 mnt_drop_write_file(file);
640 long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
645 case FS_IOC_GETFLAGS:
646 case FS_IOC_FSGETXATTR:
647 ret = ovl_real_ioctl(file, cmd, arg);
650 case FS_IOC_FSSETXATTR:
651 case FS_IOC_SETFLAGS:
652 ret = ovl_ioctl_set_flags(file, cmd, arg);
663 long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
666 case FS_IOC32_GETFLAGS:
667 cmd = FS_IOC_GETFLAGS;
670 case FS_IOC32_SETFLAGS:
671 cmd = FS_IOC_SETFLAGS;
678 return ovl_ioctl(file, cmd, arg);
688 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
689 struct file *file_out, loff_t pos_out,
690 loff_t len, unsigned int flags, enum ovl_copyop op)
692 struct inode *inode_out = file_inode(file_out);
693 struct fd real_in, real_out;
694 const struct cred *old_cred;
697 inode_lock(inode_out);
698 if (op != OVL_DEDUPE) {
700 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
701 ret = file_remove_privs(file_out);
706 ret = ovl_real_fdget(file_out, &real_out);
710 ret = ovl_real_fdget(file_in, &real_in);
716 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
719 ret = vfs_copy_file_range(real_in.file, pos_in,
720 real_out.file, pos_out, len, flags);
724 ret = vfs_clone_file_range(real_in.file, pos_in,
725 real_out.file, pos_out, len, flags);
729 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
730 real_out.file, pos_out, len,
734 revert_creds(old_cred);
737 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
743 inode_unlock(inode_out);
748 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
749 struct file *file_out, loff_t pos_out,
750 size_t len, unsigned int flags)
752 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
756 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
757 struct file *file_out, loff_t pos_out,
758 loff_t len, unsigned int remap_flags)
762 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
765 if (remap_flags & REMAP_FILE_DEDUP)
771 * Don't copy up because of a dedupe request, this wouldn't make sense
772 * most of the time (data would be duplicated instead of deduplicated).
774 if (op == OVL_DEDUPE &&
775 (!ovl_inode_upper(file_inode(file_in)) ||
776 !ovl_inode_upper(file_inode(file_out))))
779 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
783 const struct file_operations ovl_file_operations = {
785 .release = ovl_release,
786 .llseek = ovl_llseek,
787 .read_iter = ovl_read_iter,
788 .write_iter = ovl_write_iter,
791 .fallocate = ovl_fallocate,
792 .fadvise = ovl_fadvise,
793 .unlocked_ioctl = ovl_ioctl,
795 .compat_ioctl = ovl_compat_ioctl,
797 .splice_read = generic_file_splice_read,
798 .splice_write = ovl_splice_write,
800 .copy_file_range = ovl_copy_file_range,
801 .remap_file_range = ovl_remap_file_range,
804 int __init ovl_aio_request_cache_init(void)
806 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
807 sizeof(struct ovl_aio_req),
808 0, SLAB_HWCACHE_ALIGN, NULL);
809 if (!ovl_aio_request_cachep)
815 void ovl_aio_request_cache_destroy(void)
817 kmem_cache_destroy(ovl_aio_request_cachep);