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) && !(file->f_mode & FMODE_CAN_ODIRECT))
88 if (file->f_op->check_flags) {
89 err = file->f_op->check_flags(flags);
94 spin_lock(&file->f_lock);
95 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
96 spin_unlock(&file->f_lock);
101 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
104 struct inode *inode = file_inode(file);
105 struct inode *realinode;
108 real->file = file->private_data;
111 realinode = ovl_inode_real(inode);
113 realinode = ovl_inode_realdata(inode);
115 /* Has it been copied up since we'd opened it? */
116 if (unlikely(file_inode(real->file) != realinode)) {
117 real->flags = FDPUT_FPUT;
118 real->file = ovl_open_realfile(file, realinode);
120 return PTR_ERR_OR_ZERO(real->file);
123 /* Did the flags change since open? */
124 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
125 return ovl_change_flags(real->file, file->f_flags);
130 static int ovl_real_fdget(const struct file *file, struct fd *real)
132 if (d_is_dir(file_dentry(file))) {
134 real->file = ovl_dir_real_file(file, false);
136 return PTR_ERR_OR_ZERO(real->file);
139 return ovl_real_fdget_meta(file, real, false);
142 static int ovl_open(struct inode *inode, struct file *file)
144 struct file *realfile;
147 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
151 /* No longer need these flags, so don't pass them on to underlying fs */
152 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
154 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
155 if (IS_ERR(realfile))
156 return PTR_ERR(realfile);
158 file->private_data = realfile;
163 static int ovl_release(struct inode *inode, struct file *file)
165 fput(file->private_data);
170 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
172 struct inode *inode = file_inode(file);
174 const struct cred *old_cred;
178 * The two special cases below do not need to involve real fs,
179 * so we can optimizing concurrent callers.
182 if (whence == SEEK_CUR)
185 if (whence == SEEK_SET)
186 return vfs_setpos(file, 0, 0);
189 ret = ovl_real_fdget(file, &real);
194 * Overlay file f_pos is the master copy that is preserved
195 * through copy up and modified on read/write, but only real
196 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
197 * limitations that are more strict than ->s_maxbytes for specific
198 * files, so we use the real file to perform seeks.
200 ovl_inode_lock(inode);
201 real.file->f_pos = file->f_pos;
203 old_cred = ovl_override_creds(inode->i_sb);
204 ret = vfs_llseek(real.file, offset, whence);
205 revert_creds(old_cred);
207 file->f_pos = real.file->f_pos;
208 ovl_inode_unlock(inode);
215 static void ovl_file_accessed(struct file *file)
217 struct inode *inode, *upperinode;
219 if (file->f_flags & O_NOATIME)
222 inode = file_inode(file);
223 upperinode = ovl_inode_upper(inode);
228 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
229 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
230 inode->i_mtime = upperinode->i_mtime;
231 inode->i_ctime = upperinode->i_ctime;
234 touch_atime(&file->f_path);
237 static rwf_t ovl_iocb_to_rwf(int ifl)
241 if (ifl & IOCB_NOWAIT)
243 if (ifl & IOCB_HIPRI)
245 if (ifl & IOCB_DSYNC)
253 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
255 if (refcount_dec_and_test(&aio_req->ref)) {
257 kmem_cache_free(ovl_aio_request_cachep, aio_req);
261 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
263 struct kiocb *iocb = &aio_req->iocb;
264 struct kiocb *orig_iocb = aio_req->orig_iocb;
266 if (iocb->ki_flags & IOCB_WRITE) {
267 struct inode *inode = file_inode(orig_iocb->ki_filp);
269 /* Actually acquired in ovl_write_iter() */
270 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
272 file_end_write(iocb->ki_filp);
273 ovl_copyattr(ovl_inode_real(inode), inode);
276 orig_iocb->ki_pos = iocb->ki_pos;
277 ovl_aio_put(aio_req);
280 static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
282 struct ovl_aio_req *aio_req = container_of(iocb,
283 struct ovl_aio_req, iocb);
284 struct kiocb *orig_iocb = aio_req->orig_iocb;
286 ovl_aio_cleanup_handler(aio_req);
287 orig_iocb->ki_complete(orig_iocb, res);
290 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
292 struct file *file = iocb->ki_filp;
294 const struct cred *old_cred;
297 if (!iov_iter_count(iter))
300 ret = ovl_real_fdget(file, &real);
305 if (iocb->ki_flags & IOCB_DIRECT &&
306 !(real.file->f_mode & FMODE_CAN_ODIRECT))
309 old_cred = ovl_override_creds(file_inode(file)->i_sb);
310 if (is_sync_kiocb(iocb)) {
311 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
312 ovl_iocb_to_rwf(iocb->ki_flags));
314 struct ovl_aio_req *aio_req;
317 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
323 aio_req->orig_iocb = iocb;
324 kiocb_clone(&aio_req->iocb, iocb, real.file);
325 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
326 refcount_set(&aio_req->ref, 2);
327 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
328 ovl_aio_put(aio_req);
329 if (ret != -EIOCBQUEUED)
330 ovl_aio_cleanup_handler(aio_req);
333 revert_creds(old_cred);
334 ovl_file_accessed(file);
341 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
343 struct file *file = iocb->ki_filp;
344 struct inode *inode = file_inode(file);
346 const struct cred *old_cred;
348 int ifl = iocb->ki_flags;
350 if (!iov_iter_count(iter))
355 ovl_copyattr(ovl_inode_real(inode), inode);
356 ret = file_remove_privs(file);
360 ret = ovl_real_fdget(file, &real);
365 if (iocb->ki_flags & IOCB_DIRECT &&
366 !(real.file->f_mode & FMODE_CAN_ODIRECT))
369 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
370 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
372 old_cred = ovl_override_creds(file_inode(file)->i_sb);
373 if (is_sync_kiocb(iocb)) {
374 file_start_write(real.file);
375 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
376 ovl_iocb_to_rwf(ifl));
377 file_end_write(real.file);
379 ovl_copyattr(ovl_inode_real(inode), inode);
381 struct ovl_aio_req *aio_req;
384 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
388 file_start_write(real.file);
389 /* Pacify lockdep, same trick as done in aio_write() */
390 __sb_writers_release(file_inode(real.file)->i_sb,
394 aio_req->orig_iocb = iocb;
395 kiocb_clone(&aio_req->iocb, iocb, real.file);
396 aio_req->iocb.ki_flags = ifl;
397 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
398 refcount_set(&aio_req->ref, 2);
399 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
400 ovl_aio_put(aio_req);
401 if (ret != -EIOCBQUEUED)
402 ovl_aio_cleanup_handler(aio_req);
405 revert_creds(old_cred);
416 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
417 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
418 * and file_start_write(real.file) in ovl_write_iter().
420 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
423 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
424 loff_t *ppos, size_t len, unsigned int flags)
427 const struct cred *old_cred;
428 struct inode *inode = file_inode(out);
429 struct inode *realinode = ovl_inode_real(inode);
434 ovl_copyattr(realinode, inode);
435 ret = file_remove_privs(out);
439 ret = ovl_real_fdget(out, &real);
443 old_cred = ovl_override_creds(inode->i_sb);
444 file_start_write(real.file);
446 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
448 file_end_write(real.file);
450 ovl_copyattr(realinode, inode);
451 revert_creds(old_cred);
460 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
463 const struct cred *old_cred;
466 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
470 ret = ovl_real_fdget_meta(file, &real, !datasync);
474 /* Don't sync lower file for fear of receiving EROFS error */
475 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
476 old_cred = ovl_override_creds(file_inode(file)->i_sb);
477 ret = vfs_fsync_range(real.file, start, end, datasync);
478 revert_creds(old_cred);
486 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
488 struct file *realfile = file->private_data;
489 const struct cred *old_cred;
492 if (!realfile->f_op->mmap)
495 if (WARN_ON(file != vma->vm_file))
498 vma_set_file(vma, realfile);
500 old_cred = ovl_override_creds(file_inode(file)->i_sb);
501 ret = call_mmap(vma->vm_file, vma);
502 revert_creds(old_cred);
503 ovl_file_accessed(file);
508 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
510 struct inode *inode = file_inode(file);
512 const struct cred *old_cred;
515 ret = ovl_real_fdget(file, &real);
519 old_cred = ovl_override_creds(file_inode(file)->i_sb);
520 ret = vfs_fallocate(real.file, mode, offset, len);
521 revert_creds(old_cred);
524 ovl_copyattr(ovl_inode_real(inode), inode);
531 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
534 const struct cred *old_cred;
537 ret = ovl_real_fdget(file, &real);
541 old_cred = ovl_override_creds(file_inode(file)->i_sb);
542 ret = vfs_fadvise(real.file, offset, len, advice);
543 revert_creds(old_cred);
556 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
557 struct file *file_out, loff_t pos_out,
558 loff_t len, unsigned int flags, enum ovl_copyop op)
560 struct inode *inode_out = file_inode(file_out);
561 struct fd real_in, real_out;
562 const struct cred *old_cred;
565 ret = ovl_real_fdget(file_out, &real_out);
569 ret = ovl_real_fdget(file_in, &real_in);
575 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
578 ret = vfs_copy_file_range(real_in.file, pos_in,
579 real_out.file, pos_out, len, flags);
583 ret = vfs_clone_file_range(real_in.file, pos_in,
584 real_out.file, pos_out, len, flags);
588 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
589 real_out.file, pos_out, len,
593 revert_creds(old_cred);
596 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
604 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
605 struct file *file_out, loff_t pos_out,
606 size_t len, unsigned int flags)
608 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
612 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
613 struct file *file_out, loff_t pos_out,
614 loff_t len, unsigned int remap_flags)
618 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
621 if (remap_flags & REMAP_FILE_DEDUP)
627 * Don't copy up because of a dedupe request, this wouldn't make sense
628 * most of the time (data would be duplicated instead of deduplicated).
630 if (op == OVL_DEDUPE &&
631 (!ovl_inode_upper(file_inode(file_in)) ||
632 !ovl_inode_upper(file_inode(file_out))))
635 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
639 static int ovl_flush(struct file *file, fl_owner_t id)
642 const struct cred *old_cred;
645 err = ovl_real_fdget(file, &real);
649 if (real.file->f_op->flush) {
650 old_cred = ovl_override_creds(file_inode(file)->i_sb);
651 err = real.file->f_op->flush(real.file, id);
652 revert_creds(old_cred);
659 const struct file_operations ovl_file_operations = {
661 .release = ovl_release,
662 .llseek = ovl_llseek,
663 .read_iter = ovl_read_iter,
664 .write_iter = ovl_write_iter,
667 .fallocate = ovl_fallocate,
668 .fadvise = ovl_fadvise,
670 .splice_read = generic_file_splice_read,
671 .splice_write = ovl_splice_write,
673 .copy_file_range = ovl_copy_file_range,
674 .remap_file_range = ovl_remap_file_range,
677 int __init ovl_aio_request_cache_init(void)
679 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
680 sizeof(struct ovl_aio_req),
681 0, SLAB_HWCACHE_ALIGN, NULL);
682 if (!ovl_aio_request_cachep)
688 void ovl_aio_request_cache_destroy(void)
690 kmem_cache_destroy(ovl_aio_request_cachep);