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;
25 static struct kmem_cache *ovl_aio_request_cachep;
27 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
29 if (realinode != ovl_inode_upper(inode))
31 if (ovl_has_upperdata(inode))
37 /* No atime modificaton nor notify on underlying */
38 #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
40 static struct file *ovl_open_realfile(const struct file *file,
41 const struct path *realpath)
43 struct inode *realinode = d_inode(realpath->dentry);
44 struct inode *inode = file_inode(file);
45 struct user_namespace *real_mnt_userns;
46 struct file *realfile;
47 const struct cred *old_cred;
48 int flags = file->f_flags | OVL_OPEN_FLAGS;
49 int acc_mode = ACC_MODE(flags);
53 acc_mode |= MAY_APPEND;
55 old_cred = ovl_override_creds(inode->i_sb);
56 real_mnt_userns = mnt_user_ns(realpath->mnt);
57 err = inode_permission(real_mnt_userns, realinode, MAY_OPEN | acc_mode);
59 realfile = ERR_PTR(err);
61 if (!inode_owner_or_capable(real_mnt_userns, realinode))
64 realfile = open_with_fake_path(&file->f_path, flags, realinode,
67 revert_creds(old_cred);
69 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
70 file, file, ovl_whatisit(inode, realinode), file->f_flags,
71 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
76 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
78 static int ovl_change_flags(struct file *file, unsigned int flags)
80 struct inode *inode = file_inode(file);
83 flags &= OVL_SETFL_MASK;
85 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
88 if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
91 if (file->f_op->check_flags) {
92 err = file->f_op->check_flags(flags);
97 spin_lock(&file->f_lock);
98 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
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;
111 real->file = file->private_data;
114 ovl_path_real(dentry, &realpath);
116 ovl_path_realdata(dentry, &realpath);
118 /* Has it been copied up since we'd opened it? */
119 if (unlikely(file_inode(real->file) != d_inode(realpath.dentry))) {
120 real->flags = FDPUT_FPUT;
121 real->file = ovl_open_realfile(file, &realpath);
123 return PTR_ERR_OR_ZERO(real->file);
126 /* Did the flags change since open? */
127 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
128 return ovl_change_flags(real->file, file->f_flags);
133 static int ovl_real_fdget(const struct file *file, struct fd *real)
135 if (d_is_dir(file_dentry(file))) {
137 real->file = ovl_dir_real_file(file, false);
139 return PTR_ERR_OR_ZERO(real->file);
142 return ovl_real_fdget_meta(file, real, false);
145 static int ovl_open(struct inode *inode, struct file *file)
147 struct dentry *dentry = file_dentry(file);
148 struct file *realfile;
149 struct path realpath;
152 err = ovl_maybe_copy_up(dentry, file->f_flags);
156 /* No longer need these flags, so don't pass them on to underlying fs */
157 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
159 ovl_path_realdata(dentry, &realpath);
160 realfile = ovl_open_realfile(file, &realpath);
161 if (IS_ERR(realfile))
162 return PTR_ERR(realfile);
164 file->private_data = realfile;
169 static int ovl_release(struct inode *inode, struct file *file)
171 fput(file->private_data);
176 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
178 struct inode *inode = file_inode(file);
180 const struct cred *old_cred;
184 * The two special cases below do not need to involve real fs,
185 * so we can optimizing concurrent callers.
188 if (whence == SEEK_CUR)
191 if (whence == SEEK_SET)
192 return vfs_setpos(file, 0, 0);
195 ret = ovl_real_fdget(file, &real);
200 * Overlay file f_pos is the master copy that is preserved
201 * through copy up and modified on read/write, but only real
202 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
203 * limitations that are more strict than ->s_maxbytes for specific
204 * files, so we use the real file to perform seeks.
206 ovl_inode_lock(inode);
207 real.file->f_pos = file->f_pos;
209 old_cred = ovl_override_creds(inode->i_sb);
210 ret = vfs_llseek(real.file, offset, whence);
211 revert_creds(old_cred);
213 file->f_pos = real.file->f_pos;
214 ovl_inode_unlock(inode);
221 static void ovl_file_accessed(struct file *file)
223 struct inode *inode, *upperinode;
225 if (file->f_flags & O_NOATIME)
228 inode = file_inode(file);
229 upperinode = ovl_inode_upper(inode);
234 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
235 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
236 inode->i_mtime = upperinode->i_mtime;
237 inode->i_ctime = upperinode->i_ctime;
240 touch_atime(&file->f_path);
243 static rwf_t ovl_iocb_to_rwf(int ifl)
247 if (ifl & IOCB_NOWAIT)
249 if (ifl & IOCB_HIPRI)
251 if (ifl & IOCB_DSYNC)
259 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
261 if (refcount_dec_and_test(&aio_req->ref)) {
263 kmem_cache_free(ovl_aio_request_cachep, aio_req);
267 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
269 struct kiocb *iocb = &aio_req->iocb;
270 struct kiocb *orig_iocb = aio_req->orig_iocb;
272 if (iocb->ki_flags & IOCB_WRITE) {
273 struct inode *inode = file_inode(orig_iocb->ki_filp);
275 /* Actually acquired in ovl_write_iter() */
276 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
278 file_end_write(iocb->ki_filp);
282 orig_iocb->ki_pos = iocb->ki_pos;
283 ovl_aio_put(aio_req);
286 static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
288 struct ovl_aio_req *aio_req = container_of(iocb,
289 struct ovl_aio_req, iocb);
290 struct kiocb *orig_iocb = aio_req->orig_iocb;
292 ovl_aio_cleanup_handler(aio_req);
293 orig_iocb->ki_complete(orig_iocb, res);
296 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
298 struct file *file = iocb->ki_filp;
300 const struct cred *old_cred;
303 if (!iov_iter_count(iter))
306 ret = ovl_real_fdget(file, &real);
311 if (iocb->ki_flags & IOCB_DIRECT &&
312 !(real.file->f_mode & FMODE_CAN_ODIRECT))
315 old_cred = ovl_override_creds(file_inode(file)->i_sb);
316 if (is_sync_kiocb(iocb)) {
317 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
318 ovl_iocb_to_rwf(iocb->ki_flags));
320 struct ovl_aio_req *aio_req;
323 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
329 aio_req->orig_iocb = iocb;
330 kiocb_clone(&aio_req->iocb, iocb, real.file);
331 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
332 refcount_set(&aio_req->ref, 2);
333 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
334 ovl_aio_put(aio_req);
335 if (ret != -EIOCBQUEUED)
336 ovl_aio_cleanup_handler(aio_req);
339 revert_creds(old_cred);
340 ovl_file_accessed(file);
347 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
349 struct file *file = iocb->ki_filp;
350 struct inode *inode = file_inode(file);
352 const struct cred *old_cred;
354 int ifl = iocb->ki_flags;
356 if (!iov_iter_count(iter))
362 ret = file_remove_privs(file);
366 ret = ovl_real_fdget(file, &real);
371 if (iocb->ki_flags & IOCB_DIRECT &&
372 !(real.file->f_mode & FMODE_CAN_ODIRECT))
375 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
376 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
378 old_cred = ovl_override_creds(file_inode(file)->i_sb);
379 if (is_sync_kiocb(iocb)) {
380 file_start_write(real.file);
381 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
382 ovl_iocb_to_rwf(ifl));
383 file_end_write(real.file);
387 struct ovl_aio_req *aio_req;
390 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
394 file_start_write(real.file);
395 /* Pacify lockdep, same trick as done in aio_write() */
396 __sb_writers_release(file_inode(real.file)->i_sb,
400 aio_req->orig_iocb = iocb;
401 kiocb_clone(&aio_req->iocb, iocb, real.file);
402 aio_req->iocb.ki_flags = ifl;
403 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
404 refcount_set(&aio_req->ref, 2);
405 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
406 ovl_aio_put(aio_req);
407 if (ret != -EIOCBQUEUED)
408 ovl_aio_cleanup_handler(aio_req);
411 revert_creds(old_cred);
422 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
423 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
424 * and file_start_write(real.file) in ovl_write_iter().
426 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
429 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
430 loff_t *ppos, size_t len, unsigned int flags)
433 const struct cred *old_cred;
434 struct inode *inode = file_inode(out);
440 ret = file_remove_privs(out);
444 ret = ovl_real_fdget(out, &real);
448 old_cred = ovl_override_creds(inode->i_sb);
449 file_start_write(real.file);
451 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
453 file_end_write(real.file);
456 revert_creds(old_cred);
465 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
468 const struct cred *old_cred;
471 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
475 ret = ovl_real_fdget_meta(file, &real, !datasync);
479 /* Don't sync lower file for fear of receiving EROFS error */
480 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
481 old_cred = ovl_override_creds(file_inode(file)->i_sb);
482 ret = vfs_fsync_range(real.file, start, end, datasync);
483 revert_creds(old_cred);
491 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
493 struct file *realfile = file->private_data;
494 const struct cred *old_cred;
497 if (!realfile->f_op->mmap)
500 if (WARN_ON(file != vma->vm_file))
503 vma_set_file(vma, realfile);
505 old_cred = ovl_override_creds(file_inode(file)->i_sb);
506 ret = call_mmap(vma->vm_file, vma);
507 revert_creds(old_cred);
508 ovl_file_accessed(file);
513 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
515 struct inode *inode = file_inode(file);
517 const struct cred *old_cred;
520 ret = ovl_real_fdget(file, &real);
524 old_cred = ovl_override_creds(file_inode(file)->i_sb);
525 ret = vfs_fallocate(real.file, mode, offset, len);
526 revert_creds(old_cred);
536 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
539 const struct cred *old_cred;
542 ret = ovl_real_fdget(file, &real);
546 old_cred = ovl_override_creds(file_inode(file)->i_sb);
547 ret = vfs_fadvise(real.file, offset, len, advice);
548 revert_creds(old_cred);
561 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
562 struct file *file_out, loff_t pos_out,
563 loff_t len, unsigned int flags, enum ovl_copyop op)
565 struct inode *inode_out = file_inode(file_out);
566 struct fd real_in, real_out;
567 const struct cred *old_cred;
570 ret = ovl_real_fdget(file_out, &real_out);
574 ret = ovl_real_fdget(file_in, &real_in);
580 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
583 ret = vfs_copy_file_range(real_in.file, pos_in,
584 real_out.file, pos_out, len, flags);
588 ret = vfs_clone_file_range(real_in.file, pos_in,
589 real_out.file, pos_out, len, flags);
593 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
594 real_out.file, pos_out, len,
598 revert_creds(old_cred);
601 ovl_copyattr(inode_out);
609 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
610 struct file *file_out, loff_t pos_out,
611 size_t len, unsigned int flags)
613 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
617 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
618 struct file *file_out, loff_t pos_out,
619 loff_t len, unsigned int remap_flags)
623 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
626 if (remap_flags & REMAP_FILE_DEDUP)
632 * Don't copy up because of a dedupe request, this wouldn't make sense
633 * most of the time (data would be duplicated instead of deduplicated).
635 if (op == OVL_DEDUPE &&
636 (!ovl_inode_upper(file_inode(file_in)) ||
637 !ovl_inode_upper(file_inode(file_out))))
640 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
644 static int ovl_flush(struct file *file, fl_owner_t id)
647 const struct cred *old_cred;
650 err = ovl_real_fdget(file, &real);
654 if (real.file->f_op->flush) {
655 old_cred = ovl_override_creds(file_inode(file)->i_sb);
656 err = real.file->f_op->flush(real.file, id);
657 revert_creds(old_cred);
664 const struct file_operations ovl_file_operations = {
666 .release = ovl_release,
667 .llseek = ovl_llseek,
668 .read_iter = ovl_read_iter,
669 .write_iter = ovl_write_iter,
672 .fallocate = ovl_fallocate,
673 .fadvise = ovl_fadvise,
675 .splice_read = generic_file_splice_read,
676 .splice_write = ovl_splice_write,
678 .copy_file_range = ovl_copy_file_range,
679 .remap_file_range = ovl_remap_file_range,
682 int __init ovl_aio_request_cache_init(void)
684 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
685 sizeof(struct ovl_aio_req),
686 0, SLAB_HWCACHE_ALIGN, NULL);
687 if (!ovl_aio_request_cachep)
693 void ovl_aio_request_cache_destroy(void)
695 kmem_cache_destroy(ovl_aio_request_cachep);