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/security.h>
14 #include <linux/backing-file.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 modification on underlying */
28 #define OVL_OPEN_FLAGS (O_NOATIME)
30 static struct file *ovl_open_realfile(const struct file *file,
31 const struct path *realpath)
33 struct inode *realinode = d_inode(realpath->dentry);
34 struct inode *inode = file_inode(file);
35 struct mnt_idmap *real_idmap;
36 struct file *realfile;
37 const struct cred *old_cred;
38 int flags = file->f_flags | OVL_OPEN_FLAGS;
39 int acc_mode = ACC_MODE(flags);
43 acc_mode |= MAY_APPEND;
45 old_cred = ovl_override_creds(inode->i_sb);
46 real_idmap = mnt_idmap(realpath->mnt);
47 err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
49 realfile = ERR_PTR(err);
51 if (!inode_owner_or_capable(real_idmap, realinode))
54 realfile = backing_file_open(&file->f_path, flags, realpath,
57 revert_creds(old_cred);
59 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
60 file, file, ovl_whatisit(inode, realinode), file->f_flags,
61 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
66 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
68 static int ovl_change_flags(struct file *file, unsigned int flags)
70 struct inode *inode = file_inode(file);
73 flags &= OVL_SETFL_MASK;
75 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
78 if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
81 if (file->f_op->check_flags) {
82 err = file->f_op->check_flags(flags);
87 spin_lock(&file->f_lock);
88 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
89 file->f_iocb_flags = iocb_flags(file);
90 spin_unlock(&file->f_lock);
95 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
98 struct dentry *dentry = file_dentry(file);
103 real->file = file->private_data;
106 ovl_path_real(dentry, &realpath);
108 /* lazy lookup and verify of lowerdata */
109 err = ovl_verify_lowerdata(dentry);
113 ovl_path_realdata(dentry, &realpath);
115 if (!realpath.dentry)
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 /* lazy lookup and verify lowerdata */
153 err = ovl_verify_lowerdata(dentry);
157 err = ovl_maybe_copy_up(dentry, file->f_flags);
161 /* No longer need these flags, so don't pass them on to underlying fs */
162 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
164 ovl_path_realdata(dentry, &realpath);
165 if (!realpath.dentry)
168 realfile = ovl_open_realfile(file, &realpath);
169 if (IS_ERR(realfile))
170 return PTR_ERR(realfile);
172 file->private_data = realfile;
177 static int ovl_release(struct inode *inode, struct file *file)
179 fput(file->private_data);
184 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
186 struct inode *inode = file_inode(file);
188 const struct cred *old_cred;
192 * The two special cases below do not need to involve real fs,
193 * so we can optimizing concurrent callers.
196 if (whence == SEEK_CUR)
199 if (whence == SEEK_SET)
200 return vfs_setpos(file, 0, 0);
203 ret = ovl_real_fdget(file, &real);
208 * Overlay file f_pos is the master copy that is preserved
209 * through copy up and modified on read/write, but only real
210 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
211 * limitations that are more strict than ->s_maxbytes for specific
212 * files, so we use the real file to perform seeks.
214 ovl_inode_lock(inode);
215 real.file->f_pos = file->f_pos;
217 old_cred = ovl_override_creds(inode->i_sb);
218 ret = vfs_llseek(real.file, offset, whence);
219 revert_creds(old_cred);
221 file->f_pos = real.file->f_pos;
222 ovl_inode_unlock(inode);
229 static void ovl_file_modified(struct file *file)
231 /* Update size/mtime */
232 ovl_copyattr(file_inode(file));
235 static void ovl_file_accessed(struct file *file)
237 struct inode *inode, *upperinode;
238 struct timespec64 ctime, uctime;
239 struct timespec64 mtime, umtime;
241 if (file->f_flags & O_NOATIME)
244 inode = file_inode(file);
245 upperinode = ovl_inode_upper(inode);
250 ctime = inode_get_ctime(inode);
251 uctime = inode_get_ctime(upperinode);
252 mtime = inode_get_mtime(inode);
253 umtime = inode_get_mtime(upperinode);
254 if ((!timespec64_equal(&mtime, &umtime)) ||
255 !timespec64_equal(&ctime, &uctime)) {
256 inode_set_mtime_to_ts(inode, inode_get_mtime(upperinode));
257 inode_set_ctime_to_ts(inode, uctime);
260 touch_atime(&file->f_path);
263 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
265 struct file *file = iocb->ki_filp;
268 struct backing_file_ctx ctx = {
269 .cred = ovl_creds(file_inode(file)->i_sb),
271 .accessed = ovl_file_accessed,
274 if (!iov_iter_count(iter))
277 ret = ovl_real_fdget(file, &real);
281 ret = backing_file_read_iter(real.file, iter, iocb, iocb->ki_flags,
288 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
290 struct file *file = iocb->ki_filp;
291 struct inode *inode = file_inode(file);
294 int ifl = iocb->ki_flags;
295 struct backing_file_ctx ctx = {
296 .cred = ovl_creds(inode->i_sb),
298 .end_write = ovl_file_modified,
301 if (!iov_iter_count(iter))
308 ret = ovl_real_fdget(file, &real);
312 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
313 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
316 * Overlayfs doesn't support deferred completions, don't copy
317 * this property in case it is set by the issuer.
319 ifl &= ~IOCB_DIO_CALLER_COMP;
320 ret = backing_file_write_iter(real.file, iter, iocb, ifl, &ctx);
329 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
330 struct pipe_inode_info *pipe, size_t len,
335 struct backing_file_ctx ctx = {
336 .cred = ovl_creds(file_inode(in)->i_sb),
338 .accessed = ovl_file_accessed,
341 ret = ovl_real_fdget(in, &real);
345 ret = backing_file_splice_read(real.file, ppos, pipe, len, flags, &ctx);
352 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
353 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
354 * and file_start_write(real.file) in ovl_write_iter().
356 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
359 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
360 loff_t *ppos, size_t len, unsigned int flags)
363 struct inode *inode = file_inode(out);
365 struct backing_file_ctx ctx = {
366 .cred = ovl_creds(inode->i_sb),
368 .end_write = ovl_file_modified,
375 ret = ovl_real_fdget(out, &real);
379 ret = backing_file_splice_write(pipe, real.file, ppos, len, flags, &ctx);
388 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
391 const struct cred *old_cred;
394 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
398 ret = ovl_real_fdget_meta(file, &real, !datasync);
402 /* Don't sync lower file for fear of receiving EROFS error */
403 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
404 old_cred = ovl_override_creds(file_inode(file)->i_sb);
405 ret = vfs_fsync_range(real.file, start, end, datasync);
406 revert_creds(old_cred);
414 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
416 struct file *realfile = file->private_data;
417 struct backing_file_ctx ctx = {
418 .cred = ovl_creds(file_inode(file)->i_sb),
420 .accessed = ovl_file_accessed,
423 return backing_file_mmap(realfile, vma, &ctx);
426 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
428 struct inode *inode = file_inode(file);
430 const struct cred *old_cred;
436 ret = file_remove_privs(file);
440 ret = ovl_real_fdget(file, &real);
444 old_cred = ovl_override_creds(file_inode(file)->i_sb);
445 ret = vfs_fallocate(real.file, mode, offset, len);
446 revert_creds(old_cred);
449 ovl_file_modified(file);
459 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
462 const struct cred *old_cred;
465 ret = ovl_real_fdget(file, &real);
469 old_cred = ovl_override_creds(file_inode(file)->i_sb);
470 ret = vfs_fadvise(real.file, offset, len, advice);
471 revert_creds(old_cred);
484 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
485 struct file *file_out, loff_t pos_out,
486 loff_t len, unsigned int flags, enum ovl_copyop op)
488 struct inode *inode_out = file_inode(file_out);
489 struct fd real_in, real_out;
490 const struct cred *old_cred;
493 inode_lock(inode_out);
494 if (op != OVL_DEDUPE) {
496 ovl_copyattr(inode_out);
497 ret = file_remove_privs(file_out);
502 ret = ovl_real_fdget(file_out, &real_out);
506 ret = ovl_real_fdget(file_in, &real_in);
512 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
515 ret = vfs_copy_file_range(real_in.file, pos_in,
516 real_out.file, pos_out, len, flags);
520 ret = vfs_clone_file_range(real_in.file, pos_in,
521 real_out.file, pos_out, len, flags);
525 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
526 real_out.file, pos_out, len,
530 revert_creds(old_cred);
533 ovl_file_modified(file_out);
539 inode_unlock(inode_out);
544 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
545 struct file *file_out, loff_t pos_out,
546 size_t len, unsigned int flags)
548 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
552 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
553 struct file *file_out, loff_t pos_out,
554 loff_t len, unsigned int remap_flags)
558 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
561 if (remap_flags & REMAP_FILE_DEDUP)
567 * Don't copy up because of a dedupe request, this wouldn't make sense
568 * most of the time (data would be duplicated instead of deduplicated).
570 if (op == OVL_DEDUPE &&
571 (!ovl_inode_upper(file_inode(file_in)) ||
572 !ovl_inode_upper(file_inode(file_out))))
575 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
579 static int ovl_flush(struct file *file, fl_owner_t id)
582 const struct cred *old_cred;
585 err = ovl_real_fdget(file, &real);
589 if (real.file->f_op->flush) {
590 old_cred = ovl_override_creds(file_inode(file)->i_sb);
591 err = real.file->f_op->flush(real.file, id);
592 revert_creds(old_cred);
599 const struct file_operations ovl_file_operations = {
601 .release = ovl_release,
602 .llseek = ovl_llseek,
603 .read_iter = ovl_read_iter,
604 .write_iter = ovl_write_iter,
607 .fallocate = ovl_fallocate,
608 .fadvise = ovl_fadvise,
610 .splice_read = ovl_splice_read,
611 .splice_write = ovl_splice_write,
613 .copy_file_range = ovl_copy_file_range,
614 .remap_file_range = ovl_remap_file_range,