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 modification on underlying */
37 #define OVL_OPEN_FLAGS (O_NOATIME)
39 static struct file *ovl_open_realfile(const struct file *file,
40 const struct path *realpath)
42 struct inode *realinode = d_inode(realpath->dentry);
43 struct inode *inode = file_inode(file);
44 struct mnt_idmap *real_idmap;
45 struct file *realfile;
46 const struct cred *old_cred;
47 int flags = file->f_flags | OVL_OPEN_FLAGS;
48 int acc_mode = ACC_MODE(flags);
52 acc_mode |= MAY_APPEND;
54 old_cred = ovl_override_creds(inode->i_sb);
55 real_idmap = mnt_idmap(realpath->mnt);
56 err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
58 realfile = ERR_PTR(err);
60 if (!inode_owner_or_capable(real_idmap, realinode))
63 realfile = backing_file_open(&file->f_path, flags, realpath,
66 revert_creds(old_cred);
68 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
69 file, file, ovl_whatisit(inode, realinode), file->f_flags,
70 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
75 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
77 static int ovl_change_flags(struct file *file, unsigned int flags)
79 struct inode *inode = file_inode(file);
82 flags &= OVL_SETFL_MASK;
84 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
87 if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
90 if (file->f_op->check_flags) {
91 err = file->f_op->check_flags(flags);
96 spin_lock(&file->f_lock);
97 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
98 file->f_iocb_flags = iocb_flags(file);
99 spin_unlock(&file->f_lock);
104 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
107 struct dentry *dentry = file_dentry(file);
108 struct path realpath;
112 real->file = file->private_data;
115 ovl_path_real(dentry, &realpath);
117 /* lazy lookup and verify of lowerdata */
118 err = ovl_verify_lowerdata(dentry);
122 ovl_path_realdata(dentry, &realpath);
124 if (!realpath.dentry)
127 /* Has it been copied up since we'd opened it? */
128 if (unlikely(file_inode(real->file) != d_inode(realpath.dentry))) {
129 real->flags = FDPUT_FPUT;
130 real->file = ovl_open_realfile(file, &realpath);
132 return PTR_ERR_OR_ZERO(real->file);
135 /* Did the flags change since open? */
136 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
137 return ovl_change_flags(real->file, file->f_flags);
142 static int ovl_real_fdget(const struct file *file, struct fd *real)
144 if (d_is_dir(file_dentry(file))) {
146 real->file = ovl_dir_real_file(file, false);
148 return PTR_ERR_OR_ZERO(real->file);
151 return ovl_real_fdget_meta(file, real, false);
154 static int ovl_open(struct inode *inode, struct file *file)
156 struct dentry *dentry = file_dentry(file);
157 struct file *realfile;
158 struct path realpath;
161 /* lazy lookup and verify lowerdata */
162 err = ovl_verify_lowerdata(dentry);
166 err = ovl_maybe_copy_up(dentry, file->f_flags);
170 /* No longer need these flags, so don't pass them on to underlying fs */
171 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
173 ovl_path_realdata(dentry, &realpath);
174 if (!realpath.dentry)
177 realfile = ovl_open_realfile(file, &realpath);
178 if (IS_ERR(realfile))
179 return PTR_ERR(realfile);
181 file->private_data = realfile;
186 static int ovl_release(struct inode *inode, struct file *file)
188 fput(file->private_data);
193 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
195 struct inode *inode = file_inode(file);
197 const struct cred *old_cred;
201 * The two special cases below do not need to involve real fs,
202 * so we can optimizing concurrent callers.
205 if (whence == SEEK_CUR)
208 if (whence == SEEK_SET)
209 return vfs_setpos(file, 0, 0);
212 ret = ovl_real_fdget(file, &real);
217 * Overlay file f_pos is the master copy that is preserved
218 * through copy up and modified on read/write, but only real
219 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
220 * limitations that are more strict than ->s_maxbytes for specific
221 * files, so we use the real file to perform seeks.
223 ovl_inode_lock(inode);
224 real.file->f_pos = file->f_pos;
226 old_cred = ovl_override_creds(inode->i_sb);
227 ret = vfs_llseek(real.file, offset, whence);
228 revert_creds(old_cred);
230 file->f_pos = real.file->f_pos;
231 ovl_inode_unlock(inode);
238 static void ovl_file_accessed(struct file *file)
240 struct inode *inode, *upperinode;
241 struct timespec64 ctime, uctime;
243 if (file->f_flags & O_NOATIME)
246 inode = file_inode(file);
247 upperinode = ovl_inode_upper(inode);
252 ctime = inode_get_ctime(inode);
253 uctime = inode_get_ctime(upperinode);
254 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
255 !timespec64_equal(&ctime, &uctime))) {
256 inode->i_mtime = upperinode->i_mtime;
257 inode_set_ctime_to_ts(inode, uctime);
260 touch_atime(&file->f_path);
263 static rwf_t ovl_iocb_to_rwf(int ifl)
267 if (ifl & IOCB_NOWAIT)
269 if (ifl & IOCB_HIPRI)
271 if (ifl & IOCB_DSYNC)
279 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
281 if (refcount_dec_and_test(&aio_req->ref)) {
282 fput(aio_req->iocb.ki_filp);
283 kmem_cache_free(ovl_aio_request_cachep, aio_req);
287 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
289 struct kiocb *iocb = &aio_req->iocb;
290 struct kiocb *orig_iocb = aio_req->orig_iocb;
292 if (iocb->ki_flags & IOCB_WRITE) {
293 struct inode *inode = file_inode(orig_iocb->ki_filp);
295 kiocb_end_write(iocb);
299 orig_iocb->ki_pos = iocb->ki_pos;
300 ovl_aio_put(aio_req);
303 static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
305 struct ovl_aio_req *aio_req = container_of(iocb,
306 struct ovl_aio_req, iocb);
307 struct kiocb *orig_iocb = aio_req->orig_iocb;
309 ovl_aio_cleanup_handler(aio_req);
310 orig_iocb->ki_complete(orig_iocb, res);
313 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
315 struct file *file = iocb->ki_filp;
317 const struct cred *old_cred;
320 if (!iov_iter_count(iter))
323 ret = ovl_real_fdget(file, &real);
328 if (iocb->ki_flags & IOCB_DIRECT &&
329 !(real.file->f_mode & FMODE_CAN_ODIRECT))
332 old_cred = ovl_override_creds(file_inode(file)->i_sb);
333 if (is_sync_kiocb(iocb)) {
334 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
335 ovl_iocb_to_rwf(iocb->ki_flags));
337 struct ovl_aio_req *aio_req;
340 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
344 aio_req->orig_iocb = iocb;
345 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
346 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
347 refcount_set(&aio_req->ref, 2);
348 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
349 ovl_aio_put(aio_req);
350 if (ret != -EIOCBQUEUED)
351 ovl_aio_cleanup_handler(aio_req);
354 revert_creds(old_cred);
355 ovl_file_accessed(file);
362 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
364 struct file *file = iocb->ki_filp;
365 struct inode *inode = file_inode(file);
367 const struct cred *old_cred;
369 int ifl = iocb->ki_flags;
371 if (!iov_iter_count(iter))
377 ret = file_remove_privs(file);
381 ret = ovl_real_fdget(file, &real);
386 if (iocb->ki_flags & IOCB_DIRECT &&
387 !(real.file->f_mode & FMODE_CAN_ODIRECT))
390 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
391 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
394 * Overlayfs doesn't support deferred completions, don't copy
395 * this property in case it is set by the issuer.
397 ifl &= ~IOCB_DIO_CALLER_COMP;
399 old_cred = ovl_override_creds(file_inode(file)->i_sb);
400 if (is_sync_kiocb(iocb)) {
401 file_start_write(real.file);
402 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
403 ovl_iocb_to_rwf(ifl));
404 file_end_write(real.file);
408 struct ovl_aio_req *aio_req;
411 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
415 aio_req->orig_iocb = iocb;
416 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
417 aio_req->iocb.ki_flags = ifl;
418 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
419 refcount_set(&aio_req->ref, 2);
420 kiocb_start_write(&aio_req->iocb);
421 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
422 ovl_aio_put(aio_req);
423 if (ret != -EIOCBQUEUED)
424 ovl_aio_cleanup_handler(aio_req);
427 revert_creds(old_cred);
437 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
438 struct pipe_inode_info *pipe, size_t len,
441 const struct cred *old_cred;
445 ret = ovl_real_fdget(in, &real);
449 old_cred = ovl_override_creds(file_inode(in)->i_sb);
450 ret = vfs_splice_read(real.file, ppos, pipe, len, flags);
451 revert_creds(old_cred);
452 ovl_file_accessed(in);
459 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
460 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
461 * and file_start_write(real.file) in ovl_write_iter().
463 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
466 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
467 loff_t *ppos, size_t len, unsigned int flags)
470 const struct cred *old_cred;
471 struct inode *inode = file_inode(out);
477 ret = file_remove_privs(out);
481 ret = ovl_real_fdget(out, &real);
485 old_cred = ovl_override_creds(inode->i_sb);
486 file_start_write(real.file);
488 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
490 file_end_write(real.file);
493 revert_creds(old_cred);
502 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
505 const struct cred *old_cred;
508 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
512 ret = ovl_real_fdget_meta(file, &real, !datasync);
516 /* Don't sync lower file for fear of receiving EROFS error */
517 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
518 old_cred = ovl_override_creds(file_inode(file)->i_sb);
519 ret = vfs_fsync_range(real.file, start, end, datasync);
520 revert_creds(old_cred);
528 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
530 struct file *realfile = file->private_data;
531 const struct cred *old_cred;
534 if (!realfile->f_op->mmap)
537 if (WARN_ON(file != vma->vm_file))
540 vma_set_file(vma, realfile);
542 old_cred = ovl_override_creds(file_inode(file)->i_sb);
543 ret = call_mmap(vma->vm_file, vma);
544 revert_creds(old_cred);
545 ovl_file_accessed(file);
550 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
552 struct inode *inode = file_inode(file);
554 const struct cred *old_cred;
560 ret = file_remove_privs(file);
564 ret = ovl_real_fdget(file, &real);
568 old_cred = ovl_override_creds(file_inode(file)->i_sb);
569 ret = vfs_fallocate(real.file, mode, offset, len);
570 revert_creds(old_cred);
583 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
586 const struct cred *old_cred;
589 ret = ovl_real_fdget(file, &real);
593 old_cred = ovl_override_creds(file_inode(file)->i_sb);
594 ret = vfs_fadvise(real.file, offset, len, advice);
595 revert_creds(old_cred);
608 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
609 struct file *file_out, loff_t pos_out,
610 loff_t len, unsigned int flags, enum ovl_copyop op)
612 struct inode *inode_out = file_inode(file_out);
613 struct fd real_in, real_out;
614 const struct cred *old_cred;
617 inode_lock(inode_out);
618 if (op != OVL_DEDUPE) {
620 ovl_copyattr(inode_out);
621 ret = file_remove_privs(file_out);
626 ret = ovl_real_fdget(file_out, &real_out);
630 ret = ovl_real_fdget(file_in, &real_in);
636 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
639 ret = vfs_copy_file_range(real_in.file, pos_in,
640 real_out.file, pos_out, len, flags);
644 ret = vfs_clone_file_range(real_in.file, pos_in,
645 real_out.file, pos_out, len, flags);
649 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
650 real_out.file, pos_out, len,
654 revert_creds(old_cred);
657 ovl_copyattr(inode_out);
663 inode_unlock(inode_out);
668 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
669 struct file *file_out, loff_t pos_out,
670 size_t len, unsigned int flags)
672 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
676 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
677 struct file *file_out, loff_t pos_out,
678 loff_t len, unsigned int remap_flags)
682 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
685 if (remap_flags & REMAP_FILE_DEDUP)
691 * Don't copy up because of a dedupe request, this wouldn't make sense
692 * most of the time (data would be duplicated instead of deduplicated).
694 if (op == OVL_DEDUPE &&
695 (!ovl_inode_upper(file_inode(file_in)) ||
696 !ovl_inode_upper(file_inode(file_out))))
699 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
703 static int ovl_flush(struct file *file, fl_owner_t id)
706 const struct cred *old_cred;
709 err = ovl_real_fdget(file, &real);
713 if (real.file->f_op->flush) {
714 old_cred = ovl_override_creds(file_inode(file)->i_sb);
715 err = real.file->f_op->flush(real.file, id);
716 revert_creds(old_cred);
723 const struct file_operations ovl_file_operations = {
725 .release = ovl_release,
726 .llseek = ovl_llseek,
727 .read_iter = ovl_read_iter,
728 .write_iter = ovl_write_iter,
731 .fallocate = ovl_fallocate,
732 .fadvise = ovl_fadvise,
734 .splice_read = ovl_splice_read,
735 .splice_write = ovl_splice_write,
737 .copy_file_range = ovl_copy_file_range,
738 .remap_file_range = ovl_remap_file_range,
741 int __init ovl_aio_request_cache_init(void)
743 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
744 sizeof(struct ovl_aio_req),
745 0, SLAB_HWCACHE_ALIGN, NULL);
746 if (!ovl_aio_request_cachep)
752 void ovl_aio_request_cache_destroy(void)
754 kmem_cache_destroy(ovl_aio_request_cachep);