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 modification 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 mnt_idmap *real_idmap;
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_idmap = mnt_idmap(realpath->mnt);
57 err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
59 realfile = ERR_PTR(err);
61 if (!inode_owner_or_capable(real_idmap, 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 file->f_iocb_flags = iocb_flags(file);
100 spin_unlock(&file->f_lock);
105 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
108 struct dentry *dentry = file_dentry(file);
109 struct path realpath;
112 real->file = file->private_data;
115 ovl_path_real(dentry, &realpath);
117 ovl_path_realdata(dentry, &realpath);
119 /* Has it been copied up since we'd opened it? */
120 if (unlikely(file_inode(real->file) != d_inode(realpath.dentry))) {
121 real->flags = FDPUT_FPUT;
122 real->file = ovl_open_realfile(file, &realpath);
124 return PTR_ERR_OR_ZERO(real->file);
127 /* Did the flags change since open? */
128 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
129 return ovl_change_flags(real->file, file->f_flags);
134 static int ovl_real_fdget(const struct file *file, struct fd *real)
136 if (d_is_dir(file_dentry(file))) {
138 real->file = ovl_dir_real_file(file, false);
140 return PTR_ERR_OR_ZERO(real->file);
143 return ovl_real_fdget_meta(file, real, false);
146 static int ovl_open(struct inode *inode, struct file *file)
148 struct dentry *dentry = file_dentry(file);
149 struct file *realfile;
150 struct path realpath;
153 err = ovl_maybe_copy_up(dentry, file->f_flags);
157 /* No longer need these flags, so don't pass them on to underlying fs */
158 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
160 ovl_path_realdata(dentry, &realpath);
161 realfile = ovl_open_realfile(file, &realpath);
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)) {
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);
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)
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);
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_mode & FMODE_CAN_ODIRECT))
316 old_cred = ovl_override_creds(file_inode(file)->i_sb);
317 if (is_sync_kiocb(iocb)) {
318 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
319 ovl_iocb_to_rwf(iocb->ki_flags));
321 struct ovl_aio_req *aio_req;
324 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
330 aio_req->orig_iocb = iocb;
331 kiocb_clone(&aio_req->iocb, iocb, 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))
363 ret = file_remove_privs(file);
367 ret = ovl_real_fdget(file, &real);
372 if (iocb->ki_flags & IOCB_DIRECT &&
373 !(real.file->f_mode & FMODE_CAN_ODIRECT))
376 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
377 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
379 old_cred = ovl_override_creds(file_inode(file)->i_sb);
380 if (is_sync_kiocb(iocb)) {
381 file_start_write(real.file);
382 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
383 ovl_iocb_to_rwf(ifl));
384 file_end_write(real.file);
388 struct ovl_aio_req *aio_req;
391 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
395 file_start_write(real.file);
396 /* Pacify lockdep, same trick as done in aio_write() */
397 __sb_writers_release(file_inode(real.file)->i_sb,
401 aio_req->orig_iocb = iocb;
402 kiocb_clone(&aio_req->iocb, iocb, 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);
441 ret = file_remove_privs(out);
445 ret = ovl_real_fdget(out, &real);
449 old_cred = ovl_override_creds(inode->i_sb);
450 file_start_write(real.file);
452 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
454 file_end_write(real.file);
457 revert_creds(old_cred);
466 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
469 const struct cred *old_cred;
472 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
476 ret = ovl_real_fdget_meta(file, &real, !datasync);
480 /* Don't sync lower file for fear of receiving EROFS error */
481 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
482 old_cred = ovl_override_creds(file_inode(file)->i_sb);
483 ret = vfs_fsync_range(real.file, start, end, datasync);
484 revert_creds(old_cred);
492 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
494 struct file *realfile = file->private_data;
495 const struct cred *old_cred;
498 if (!realfile->f_op->mmap)
501 if (WARN_ON(file != vma->vm_file))
504 vma_set_file(vma, realfile);
506 old_cred = ovl_override_creds(file_inode(file)->i_sb);
507 ret = call_mmap(vma->vm_file, vma);
508 revert_creds(old_cred);
509 ovl_file_accessed(file);
514 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
516 struct inode *inode = file_inode(file);
518 const struct cred *old_cred;
524 ret = file_remove_privs(file);
528 ret = ovl_real_fdget(file, &real);
532 old_cred = ovl_override_creds(file_inode(file)->i_sb);
533 ret = vfs_fallocate(real.file, mode, offset, len);
534 revert_creds(old_cred);
547 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
550 const struct cred *old_cred;
553 ret = ovl_real_fdget(file, &real);
557 old_cred = ovl_override_creds(file_inode(file)->i_sb);
558 ret = vfs_fadvise(real.file, offset, len, advice);
559 revert_creds(old_cred);
572 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
573 struct file *file_out, loff_t pos_out,
574 loff_t len, unsigned int flags, enum ovl_copyop op)
576 struct inode *inode_out = file_inode(file_out);
577 struct fd real_in, real_out;
578 const struct cred *old_cred;
581 inode_lock(inode_out);
582 if (op != OVL_DEDUPE) {
584 ovl_copyattr(inode_out);
585 ret = file_remove_privs(file_out);
590 ret = ovl_real_fdget(file_out, &real_out);
594 ret = ovl_real_fdget(file_in, &real_in);
600 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
603 ret = vfs_copy_file_range(real_in.file, pos_in,
604 real_out.file, pos_out, len, flags);
608 ret = vfs_clone_file_range(real_in.file, pos_in,
609 real_out.file, pos_out, len, flags);
613 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
614 real_out.file, pos_out, len,
618 revert_creds(old_cred);
621 ovl_copyattr(inode_out);
627 inode_unlock(inode_out);
632 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
633 struct file *file_out, loff_t pos_out,
634 size_t len, unsigned int flags)
636 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
640 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
641 struct file *file_out, loff_t pos_out,
642 loff_t len, unsigned int remap_flags)
646 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
649 if (remap_flags & REMAP_FILE_DEDUP)
655 * Don't copy up because of a dedupe request, this wouldn't make sense
656 * most of the time (data would be duplicated instead of deduplicated).
658 if (op == OVL_DEDUPE &&
659 (!ovl_inode_upper(file_inode(file_in)) ||
660 !ovl_inode_upper(file_inode(file_out))))
663 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
667 static int ovl_flush(struct file *file, fl_owner_t id)
670 const struct cred *old_cred;
673 err = ovl_real_fdget(file, &real);
677 if (real.file->f_op->flush) {
678 old_cred = ovl_override_creds(file_inode(file)->i_sb);
679 err = real.file->f_op->flush(real.file, id);
680 revert_creds(old_cred);
687 const struct file_operations ovl_file_operations = {
689 .release = ovl_release,
690 .llseek = ovl_llseek,
691 .read_iter = ovl_read_iter,
692 .write_iter = ovl_write_iter,
695 .fallocate = ovl_fallocate,
696 .fadvise = ovl_fadvise,
698 .splice_read = generic_file_splice_read,
699 .splice_write = ovl_splice_write,
701 .copy_file_range = ovl_copy_file_range,
702 .remap_file_range = ovl_remap_file_range,
705 int __init ovl_aio_request_cache_init(void)
707 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
708 sizeof(struct ovl_aio_req),
709 0, SLAB_HWCACHE_ALIGN, NULL);
710 if (!ovl_aio_request_cachep)
716 void ovl_aio_request_cache_destroy(void)
718 kmem_cache_destroy(ovl_aio_request_cachep);