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 struct inode *realinode)
43 struct inode *inode = file_inode(file);
44 struct file *realfile;
45 const struct cred *old_cred;
46 int flags = file->f_flags | OVL_OPEN_FLAGS;
47 int acc_mode = ACC_MODE(flags);
51 acc_mode |= MAY_APPEND;
53 old_cred = ovl_override_creds(inode->i_sb);
54 err = inode_permission(&init_user_ns, realinode, MAY_OPEN | acc_mode);
56 realfile = ERR_PTR(err);
58 if (!inode_owner_or_capable(&init_user_ns, realinode))
61 realfile = open_with_fake_path(&file->f_path, flags, realinode,
64 revert_creds(old_cred);
66 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
67 file, file, ovl_whatisit(inode, realinode), file->f_flags,
68 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
73 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
75 static int ovl_change_flags(struct file *file, unsigned int flags)
77 struct inode *inode = file_inode(file);
80 flags &= OVL_SETFL_MASK;
82 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
85 if (flags & O_DIRECT) {
86 if (!file->f_mapping->a_ops ||
87 !file->f_mapping->a_ops->direct_IO)
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 inode *inode = file_inode(file);
108 struct inode *realinode;
111 real->file = file->private_data;
114 realinode = ovl_inode_real(inode);
116 realinode = ovl_inode_realdata(inode);
118 /* Has it been copied up since we'd opened it? */
119 if (unlikely(file_inode(real->file) != realinode)) {
120 real->flags = FDPUT_FPUT;
121 real->file = ovl_open_realfile(file, realinode);
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 file *realfile;
150 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
154 /* No longer need these flags, so don't pass them on to underlying fs */
155 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
157 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
158 if (IS_ERR(realfile))
159 return PTR_ERR(realfile);
161 file->private_data = realfile;
166 static int ovl_release(struct inode *inode, struct file *file)
168 fput(file->private_data);
173 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
175 struct inode *inode = file_inode(file);
177 const struct cred *old_cred;
181 * The two special cases below do not need to involve real fs,
182 * so we can optimizing concurrent callers.
185 if (whence == SEEK_CUR)
188 if (whence == SEEK_SET)
189 return vfs_setpos(file, 0, 0);
192 ret = ovl_real_fdget(file, &real);
197 * Overlay file f_pos is the master copy that is preserved
198 * through copy up and modified on read/write, but only real
199 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
200 * limitations that are more strict than ->s_maxbytes for specific
201 * files, so we use the real file to perform seeks.
203 ovl_inode_lock(inode);
204 real.file->f_pos = file->f_pos;
206 old_cred = ovl_override_creds(inode->i_sb);
207 ret = vfs_llseek(real.file, offset, whence);
208 revert_creds(old_cred);
210 file->f_pos = real.file->f_pos;
211 ovl_inode_unlock(inode);
218 static void ovl_file_accessed(struct file *file)
220 struct inode *inode, *upperinode;
222 if (file->f_flags & O_NOATIME)
225 inode = file_inode(file);
226 upperinode = ovl_inode_upper(inode);
231 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
232 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
233 inode->i_mtime = upperinode->i_mtime;
234 inode->i_ctime = upperinode->i_ctime;
237 touch_atime(&file->f_path);
240 static rwf_t ovl_iocb_to_rwf(int ifl)
244 if (ifl & IOCB_NOWAIT)
246 if (ifl & IOCB_HIPRI)
248 if (ifl & IOCB_DSYNC)
256 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
258 if (refcount_dec_and_test(&aio_req->ref)) {
260 kmem_cache_free(ovl_aio_request_cachep, aio_req);
264 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
266 struct kiocb *iocb = &aio_req->iocb;
267 struct kiocb *orig_iocb = aio_req->orig_iocb;
269 if (iocb->ki_flags & IOCB_WRITE) {
270 struct inode *inode = file_inode(orig_iocb->ki_filp);
272 /* Actually acquired in ovl_write_iter() */
273 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
275 file_end_write(iocb->ki_filp);
276 ovl_copyattr(ovl_inode_real(inode), inode);
279 orig_iocb->ki_pos = iocb->ki_pos;
280 ovl_aio_put(aio_req);
283 static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
285 struct ovl_aio_req *aio_req = container_of(iocb,
286 struct ovl_aio_req, iocb);
287 struct kiocb *orig_iocb = aio_req->orig_iocb;
289 ovl_aio_cleanup_handler(aio_req);
290 orig_iocb->ki_complete(orig_iocb, res);
293 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
295 struct file *file = iocb->ki_filp;
297 const struct cred *old_cred;
300 if (!iov_iter_count(iter))
303 ret = ovl_real_fdget(file, &real);
308 if (iocb->ki_flags & IOCB_DIRECT &&
309 (!real.file->f_mapping->a_ops ||
310 !real.file->f_mapping->a_ops->direct_IO))
313 old_cred = ovl_override_creds(file_inode(file)->i_sb);
314 if (is_sync_kiocb(iocb)) {
315 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
316 ovl_iocb_to_rwf(iocb->ki_flags));
318 struct ovl_aio_req *aio_req;
321 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
327 aio_req->orig_iocb = iocb;
328 kiocb_clone(&aio_req->iocb, iocb, real.file);
329 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
330 refcount_set(&aio_req->ref, 2);
331 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
332 ovl_aio_put(aio_req);
333 if (ret != -EIOCBQUEUED)
334 ovl_aio_cleanup_handler(aio_req);
337 revert_creds(old_cred);
338 ovl_file_accessed(file);
345 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
347 struct file *file = iocb->ki_filp;
348 struct inode *inode = file_inode(file);
350 const struct cred *old_cred;
352 int ifl = iocb->ki_flags;
354 if (!iov_iter_count(iter))
359 ovl_copyattr(ovl_inode_real(inode), inode);
360 ret = file_remove_privs(file);
364 ret = ovl_real_fdget(file, &real);
369 if (iocb->ki_flags & IOCB_DIRECT &&
370 (!real.file->f_mapping->a_ops ||
371 !real.file->f_mapping->a_ops->direct_IO))
374 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
375 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
377 old_cred = ovl_override_creds(file_inode(file)->i_sb);
378 if (is_sync_kiocb(iocb)) {
379 file_start_write(real.file);
380 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
381 ovl_iocb_to_rwf(ifl));
382 file_end_write(real.file);
384 ovl_copyattr(ovl_inode_real(inode), inode);
386 struct ovl_aio_req *aio_req;
389 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
393 file_start_write(real.file);
394 /* Pacify lockdep, same trick as done in aio_write() */
395 __sb_writers_release(file_inode(real.file)->i_sb,
399 aio_req->orig_iocb = iocb;
400 kiocb_clone(&aio_req->iocb, iocb, real.file);
401 aio_req->iocb.ki_flags = ifl;
402 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
403 refcount_set(&aio_req->ref, 2);
404 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
405 ovl_aio_put(aio_req);
406 if (ret != -EIOCBQUEUED)
407 ovl_aio_cleanup_handler(aio_req);
410 revert_creds(old_cred);
421 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
422 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
423 * and file_start_write(real.file) in ovl_write_iter().
425 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
428 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
429 loff_t *ppos, size_t len, unsigned int flags)
432 const struct cred *old_cred;
433 struct inode *inode = file_inode(out);
434 struct inode *realinode = ovl_inode_real(inode);
439 ovl_copyattr(realinode, inode);
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);
455 ovl_copyattr(realinode, inode);
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);
529 ovl_copyattr(ovl_inode_real(inode), inode);
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(ovl_inode_real(inode_out), 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);