2 * Copyright (C) 2017 Red Hat, Inc.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
9 #include <linux/cred.h>
10 #include <linux/file.h>
11 #include <linux/mount.h>
12 #include <linux/xattr.h>
13 #include <linux/uio.h>
14 #include "overlayfs.h"
16 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
18 if (realinode != ovl_inode_upper(inode))
20 if (ovl_has_upperdata(inode))
26 static struct file *ovl_open_realfile(const struct file *file,
27 struct inode *realinode)
29 struct inode *inode = file_inode(file);
30 struct file *realfile;
31 const struct cred *old_cred;
33 old_cred = ovl_override_creds(inode->i_sb);
34 realfile = open_with_fake_path(&file->f_path, file->f_flags | O_NOATIME,
35 realinode, current_cred());
36 revert_creds(old_cred);
38 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
39 file, file, ovl_whatisit(inode, realinode), file->f_flags,
40 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
45 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
47 static int ovl_change_flags(struct file *file, unsigned int flags)
49 struct inode *inode = file_inode(file);
52 /* No atime modificaton on underlying */
55 /* If some flag changed that cannot be changed then something's amiss */
56 if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK))
59 flags &= OVL_SETFL_MASK;
61 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
64 if (flags & O_DIRECT) {
65 if (!file->f_mapping->a_ops ||
66 !file->f_mapping->a_ops->direct_IO)
70 if (file->f_op->check_flags) {
71 err = file->f_op->check_flags(flags);
76 spin_lock(&file->f_lock);
77 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
78 spin_unlock(&file->f_lock);
83 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
86 struct inode *inode = file_inode(file);
87 struct inode *realinode;
90 real->file = file->private_data;
93 realinode = ovl_inode_real(inode);
95 realinode = ovl_inode_realdata(inode);
97 /* Has it been copied up since we'd opened it? */
98 if (unlikely(file_inode(real->file) != realinode)) {
99 real->flags = FDPUT_FPUT;
100 real->file = ovl_open_realfile(file, realinode);
102 return PTR_ERR_OR_ZERO(real->file);
105 /* Did the flags change since open? */
106 if (unlikely((file->f_flags ^ real->file->f_flags) & ~O_NOATIME))
107 return ovl_change_flags(real->file, file->f_flags);
112 static int ovl_real_fdget(const struct file *file, struct fd *real)
114 return ovl_real_fdget_meta(file, real, false);
117 static int ovl_open(struct inode *inode, struct file *file)
119 struct dentry *dentry = file_dentry(file);
120 struct file *realfile;
123 err = ovl_open_maybe_copy_up(dentry, file->f_flags);
127 /* No longer need these flags, so don't pass them on to underlying fs */
128 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
130 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
131 if (IS_ERR(realfile))
132 return PTR_ERR(realfile);
134 file->private_data = realfile;
139 static int ovl_release(struct inode *inode, struct file *file)
141 fput(file->private_data);
146 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
148 struct inode *realinode = ovl_inode_real(file_inode(file));
150 return generic_file_llseek_size(file, offset, whence,
151 realinode->i_sb->s_maxbytes,
152 i_size_read(realinode));
155 static void ovl_file_accessed(struct file *file)
157 struct inode *inode, *upperinode;
159 if (file->f_flags & O_NOATIME)
162 inode = file_inode(file);
163 upperinode = ovl_inode_upper(inode);
168 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
169 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
170 inode->i_mtime = upperinode->i_mtime;
171 inode->i_ctime = upperinode->i_ctime;
174 touch_atime(&file->f_path);
177 static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb)
179 int ifl = iocb->ki_flags;
182 if (ifl & IOCB_NOWAIT)
184 if (ifl & IOCB_HIPRI)
186 if (ifl & IOCB_DSYNC)
194 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
196 struct file *file = iocb->ki_filp;
198 const struct cred *old_cred;
201 if (!iov_iter_count(iter))
204 ret = ovl_real_fdget(file, &real);
208 old_cred = ovl_override_creds(file_inode(file)->i_sb);
209 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
210 ovl_iocb_to_rwf(iocb));
211 revert_creds(old_cred);
213 ovl_file_accessed(file);
220 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
222 struct file *file = iocb->ki_filp;
223 struct inode *inode = file_inode(file);
225 const struct cred *old_cred;
228 if (!iov_iter_count(iter))
233 ovl_copyattr(ovl_inode_real(inode), inode);
234 ret = file_remove_privs(file);
238 ret = ovl_real_fdget(file, &real);
242 old_cred = ovl_override_creds(file_inode(file)->i_sb);
243 file_start_write(real.file);
244 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
245 ovl_iocb_to_rwf(iocb));
246 file_end_write(real.file);
247 revert_creds(old_cred);
250 ovl_copyattr(ovl_inode_real(inode), inode);
260 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
263 const struct cred *old_cred;
266 ret = ovl_real_fdget_meta(file, &real, !datasync);
270 /* Don't sync lower file for fear of receiving EROFS error */
271 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
272 old_cred = ovl_override_creds(file_inode(file)->i_sb);
273 ret = vfs_fsync_range(real.file, start, end, datasync);
274 revert_creds(old_cred);
282 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
284 struct file *realfile = file->private_data;
285 const struct cred *old_cred;
288 if (!realfile->f_op->mmap)
291 if (WARN_ON(file != vma->vm_file))
294 vma->vm_file = get_file(realfile);
296 old_cred = ovl_override_creds(file_inode(file)->i_sb);
297 ret = call_mmap(vma->vm_file, vma);
298 revert_creds(old_cred);
301 /* Drop reference count from new vm_file value */
304 /* Drop reference count from previous vm_file value */
308 ovl_file_accessed(file);
313 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
315 struct inode *inode = file_inode(file);
317 const struct cred *old_cred;
320 ret = ovl_real_fdget(file, &real);
324 old_cred = ovl_override_creds(file_inode(file)->i_sb);
325 ret = vfs_fallocate(real.file, mode, offset, len);
326 revert_creds(old_cred);
329 ovl_copyattr(ovl_inode_real(inode), inode);
336 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
339 const struct cred *old_cred;
342 ret = ovl_real_fdget(file, &real);
346 old_cred = ovl_override_creds(file_inode(file)->i_sb);
347 ret = vfs_fadvise(real.file, offset, len, advice);
348 revert_creds(old_cred);
355 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
359 const struct cred *old_cred;
362 ret = ovl_real_fdget(file, &real);
366 old_cred = ovl_override_creds(file_inode(file)->i_sb);
367 ret = vfs_ioctl(real.file, cmd, arg);
368 revert_creds(old_cred);
375 static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
378 struct inode *inode = file_inode(file);
381 case FS_IOC_GETFLAGS:
382 ret = ovl_real_ioctl(file, cmd, arg);
385 case FS_IOC_SETFLAGS:
386 if (!inode_owner_or_capable(inode))
389 ret = mnt_want_write_file(file);
393 ret = ovl_copy_up_with_data(file_dentry(file));
395 ret = ovl_real_ioctl(file, cmd, arg);
398 ovl_copyflags(ovl_inode_real(inode), inode);
402 mnt_drop_write_file(file);
412 static long ovl_compat_ioctl(struct file *file, unsigned int cmd,
416 case FS_IOC32_GETFLAGS:
417 cmd = FS_IOC_GETFLAGS;
420 case FS_IOC32_SETFLAGS:
421 cmd = FS_IOC_SETFLAGS;
428 return ovl_ioctl(file, cmd, arg);
437 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
438 struct file *file_out, loff_t pos_out,
439 loff_t len, unsigned int flags, enum ovl_copyop op)
441 struct inode *inode_out = file_inode(file_out);
442 struct fd real_in, real_out;
443 const struct cred *old_cred;
446 ret = ovl_real_fdget(file_out, &real_out);
450 ret = ovl_real_fdget(file_in, &real_in);
456 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
459 ret = vfs_copy_file_range(real_in.file, pos_in,
460 real_out.file, pos_out, len, flags);
464 ret = vfs_clone_file_range(real_in.file, pos_in,
465 real_out.file, pos_out, len, flags);
469 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
470 real_out.file, pos_out, len,
474 revert_creds(old_cred);
477 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
485 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
486 struct file *file_out, loff_t pos_out,
487 size_t len, unsigned int flags)
489 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
493 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
494 struct file *file_out, loff_t pos_out,
495 loff_t len, unsigned int remap_flags)
499 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
502 if (remap_flags & REMAP_FILE_DEDUP)
508 * Don't copy up because of a dedupe request, this wouldn't make sense
509 * most of the time (data would be duplicated instead of deduplicated).
511 if (op == OVL_DEDUPE &&
512 (!ovl_inode_upper(file_inode(file_in)) ||
513 !ovl_inode_upper(file_inode(file_out))))
516 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
520 const struct file_operations ovl_file_operations = {
522 .release = ovl_release,
523 .llseek = ovl_llseek,
524 .read_iter = ovl_read_iter,
525 .write_iter = ovl_write_iter,
528 .fallocate = ovl_fallocate,
529 .fadvise = ovl_fadvise,
530 .unlocked_ioctl = ovl_ioctl,
531 .compat_ioctl = ovl_compat_ioctl,
533 .copy_file_range = ovl_copy_file_range,
534 .remap_file_range = ovl_remap_file_range,