Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Red Hat, Inc. |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/cred.h> |
| 7 | #include <linux/file.h> |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 8 | #include <linux/mount.h> |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 9 | #include <linux/xattr.h> |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 10 | #include <linux/uio.h> |
Jiufei Xue | 98487de | 2019-05-06 15:41:02 +0800 | [diff] [blame] | 11 | #include <linux/uaccess.h> |
Murphy Zhou | 1a980b8 | 2020-01-17 20:49:29 +0800 | [diff] [blame] | 12 | #include <linux/splice.h> |
Miklos Szeredi | 292f902 | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 13 | #include <linux/security.h> |
Murphy Zhou | 1a980b8 | 2020-01-17 20:49:29 +0800 | [diff] [blame] | 14 | #include <linux/mm.h> |
| 15 | #include <linux/fs.h> |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 16 | #include "overlayfs.h" |
| 17 | |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 18 | struct ovl_aio_req { |
| 19 | struct kiocb iocb; |
yangerkun | 9a25440 | 2021-09-30 11:22:28 +0800 | [diff] [blame] | 20 | refcount_t ref; |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 21 | struct kiocb *orig_iocb; |
| 22 | struct fd fd; |
| 23 | }; |
| 24 | |
| 25 | static struct kmem_cache *ovl_aio_request_cachep; |
| 26 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 27 | static char ovl_whatisit(struct inode *inode, struct inode *realinode) |
| 28 | { |
| 29 | if (realinode != ovl_inode_upper(inode)) |
| 30 | return 'l'; |
| 31 | if (ovl_has_upperdata(inode)) |
| 32 | return 'u'; |
| 33 | else |
| 34 | return 'm'; |
| 35 | } |
| 36 | |
Amir Goldstein | 81a33c1 | 2020-06-18 18:43:53 +0300 | [diff] [blame] | 37 | /* No atime modificaton nor notify on underlying */ |
| 38 | #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY) |
| 39 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 40 | static struct file *ovl_open_realfile(const struct file *file, |
| 41 | struct inode *realinode) |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 42 | { |
| 43 | struct inode *inode = file_inode(file); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 44 | struct file *realfile; |
| 45 | const struct cred *old_cred; |
Amir Goldstein | 81a33c1 | 2020-06-18 18:43:53 +0300 | [diff] [blame] | 46 | int flags = file->f_flags | OVL_OPEN_FLAGS; |
Miklos Szeredi | 05acefb | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 47 | int acc_mode = ACC_MODE(flags); |
| 48 | int err; |
| 49 | |
| 50 | if (flags & O_APPEND) |
| 51 | acc_mode |= MAY_APPEND; |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 52 | |
| 53 | old_cred = ovl_override_creds(inode->i_sb); |
Christian Brauner | 47291ba | 2021-01-21 14:19:24 +0100 | [diff] [blame] | 54 | err = inode_permission(&init_user_ns, realinode, MAY_OPEN | acc_mode); |
Miklos Szeredi | 05acefb | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 55 | if (err) { |
| 56 | realfile = ERR_PTR(err); |
Miklos Szeredi | 05acefb | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 57 | } else { |
Christian Brauner | 21cb47b | 2021-01-21 14:19:25 +0100 | [diff] [blame] | 58 | if (!inode_owner_or_capable(&init_user_ns, realinode)) |
Miklos Szeredi | b6650da | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 59 | flags &= ~O_NOATIME; |
| 60 | |
Miklos Szeredi | 05acefb | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 61 | realfile = open_with_fake_path(&file->f_path, flags, realinode, |
| 62 | current_cred()); |
| 63 | } |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 64 | revert_creds(old_cred); |
| 65 | |
| 66 | pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 67 | file, file, ovl_whatisit(inode, realinode), file->f_flags, |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 68 | realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); |
| 69 | |
| 70 | return realfile; |
| 71 | } |
| 72 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 73 | #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT) |
| 74 | |
| 75 | static int ovl_change_flags(struct file *file, unsigned int flags) |
| 76 | { |
| 77 | struct inode *inode = file_inode(file); |
| 78 | int err; |
| 79 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 80 | flags &= OVL_SETFL_MASK; |
| 81 | |
| 82 | if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) |
| 83 | return -EPERM; |
| 84 | |
| 85 | if (flags & O_DIRECT) { |
| 86 | if (!file->f_mapping->a_ops || |
| 87 | !file->f_mapping->a_ops->direct_IO) |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | if (file->f_op->check_flags) { |
| 92 | err = file->f_op->check_flags(flags); |
| 93 | if (err) |
| 94 | return err; |
| 95 | } |
| 96 | |
| 97 | spin_lock(&file->f_lock); |
| 98 | file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags; |
| 99 | spin_unlock(&file->f_lock); |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 104 | static int ovl_real_fdget_meta(const struct file *file, struct fd *real, |
| 105 | bool allow_meta) |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 106 | { |
| 107 | struct inode *inode = file_inode(file); |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 108 | struct inode *realinode; |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 109 | |
| 110 | real->flags = 0; |
| 111 | real->file = file->private_data; |
| 112 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 113 | if (allow_meta) |
| 114 | realinode = ovl_inode_real(inode); |
| 115 | else |
| 116 | realinode = ovl_inode_realdata(inode); |
| 117 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 118 | /* Has it been copied up since we'd opened it? */ |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 119 | if (unlikely(file_inode(real->file) != realinode)) { |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 120 | real->flags = FDPUT_FPUT; |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 121 | real->file = ovl_open_realfile(file, realinode); |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 122 | |
| 123 | return PTR_ERR_OR_ZERO(real->file); |
| 124 | } |
| 125 | |
| 126 | /* Did the flags change since open? */ |
Amir Goldstein | 81a33c1 | 2020-06-18 18:43:53 +0300 | [diff] [blame] | 127 | if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS)) |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 128 | return ovl_change_flags(real->file, file->f_flags); |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 133 | static int ovl_real_fdget(const struct file *file, struct fd *real) |
| 134 | { |
Amir Goldstein | 61536be | 2020-09-29 15:28:47 +0800 | [diff] [blame] | 135 | if (d_is_dir(file_dentry(file))) { |
| 136 | real->flags = 0; |
| 137 | real->file = ovl_dir_real_file(file, false); |
| 138 | |
| 139 | return PTR_ERR_OR_ZERO(real->file); |
| 140 | } |
| 141 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 142 | return ovl_real_fdget_meta(file, real, false); |
| 143 | } |
| 144 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 145 | static int ovl_open(struct inode *inode, struct file *file) |
| 146 | { |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 147 | struct file *realfile; |
| 148 | int err; |
| 149 | |
Amir Goldstein | 3428030 | 2019-01-22 07:01:39 +0200 | [diff] [blame] | 150 | err = ovl_maybe_copy_up(file_dentry(file), file->f_flags); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 151 | if (err) |
| 152 | return err; |
| 153 | |
| 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); |
| 156 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 157 | realfile = ovl_open_realfile(file, ovl_inode_realdata(inode)); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 158 | if (IS_ERR(realfile)) |
| 159 | return PTR_ERR(realfile); |
| 160 | |
| 161 | file->private_data = realfile; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static int ovl_release(struct inode *inode, struct file *file) |
| 167 | { |
| 168 | fput(file->private_data); |
| 169 | |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) |
| 174 | { |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 175 | struct inode *inode = file_inode(file); |
| 176 | struct fd real; |
| 177 | const struct cred *old_cred; |
Miklos Szeredi | a4ac9d4 | 2020-02-03 11:41:53 +0100 | [diff] [blame] | 178 | loff_t ret; |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 179 | |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 180 | /* |
| 181 | * The two special cases below do not need to involve real fs, |
| 182 | * so we can optimizing concurrent callers. |
| 183 | */ |
| 184 | if (offset == 0) { |
| 185 | if (whence == SEEK_CUR) |
| 186 | return file->f_pos; |
| 187 | |
| 188 | if (whence == SEEK_SET) |
| 189 | return vfs_setpos(file, 0, 0); |
| 190 | } |
| 191 | |
| 192 | ret = ovl_real_fdget(file, &real); |
| 193 | if (ret) |
| 194 | return ret; |
| 195 | |
| 196 | /* |
| 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. |
| 202 | */ |
Amir Goldstein | b1f9d38 | 2019-12-21 11:42:29 +0200 | [diff] [blame] | 203 | ovl_inode_lock(inode); |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 204 | real.file->f_pos = file->f_pos; |
| 205 | |
| 206 | old_cred = ovl_override_creds(inode->i_sb); |
| 207 | ret = vfs_llseek(real.file, offset, whence); |
| 208 | revert_creds(old_cred); |
| 209 | |
| 210 | file->f_pos = real.file->f_pos; |
Amir Goldstein | b1f9d38 | 2019-12-21 11:42:29 +0200 | [diff] [blame] | 211 | ovl_inode_unlock(inode); |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 212 | |
| 213 | fdput(real); |
| 214 | |
| 215 | return ret; |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 218 | static void ovl_file_accessed(struct file *file) |
| 219 | { |
| 220 | struct inode *inode, *upperinode; |
| 221 | |
| 222 | if (file->f_flags & O_NOATIME) |
| 223 | return; |
| 224 | |
| 225 | inode = file_inode(file); |
| 226 | upperinode = ovl_inode_upper(inode); |
| 227 | |
| 228 | if (!upperinode) |
| 229 | return; |
| 230 | |
| 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; |
| 235 | } |
| 236 | |
| 237 | touch_atime(&file->f_path); |
| 238 | } |
| 239 | |
Miklos Szeredi | b778e1e | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 240 | static rwf_t ovl_iocb_to_rwf(int ifl) |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 241 | { |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 242 | rwf_t flags = 0; |
| 243 | |
| 244 | if (ifl & IOCB_NOWAIT) |
| 245 | flags |= RWF_NOWAIT; |
| 246 | if (ifl & IOCB_HIPRI) |
| 247 | flags |= RWF_HIPRI; |
| 248 | if (ifl & IOCB_DSYNC) |
| 249 | flags |= RWF_DSYNC; |
| 250 | if (ifl & IOCB_SYNC) |
| 251 | flags |= RWF_SYNC; |
| 252 | |
| 253 | return flags; |
| 254 | } |
| 255 | |
yangerkun | 9a25440 | 2021-09-30 11:22:28 +0800 | [diff] [blame] | 256 | static inline void ovl_aio_put(struct ovl_aio_req *aio_req) |
| 257 | { |
| 258 | if (refcount_dec_and_test(&aio_req->ref)) { |
| 259 | fdput(aio_req->fd); |
| 260 | kmem_cache_free(ovl_aio_request_cachep, aio_req); |
| 261 | } |
| 262 | } |
| 263 | |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 264 | static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req) |
| 265 | { |
| 266 | struct kiocb *iocb = &aio_req->iocb; |
| 267 | struct kiocb *orig_iocb = aio_req->orig_iocb; |
| 268 | |
| 269 | if (iocb->ki_flags & IOCB_WRITE) { |
| 270 | struct inode *inode = file_inode(orig_iocb->ki_filp); |
| 271 | |
Miklos Szeredi | c853680 | 2020-03-13 15:42:20 +0100 | [diff] [blame] | 272 | /* Actually acquired in ovl_write_iter() */ |
| 273 | __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb, |
| 274 | SB_FREEZE_WRITE); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 275 | file_end_write(iocb->ki_filp); |
| 276 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 277 | } |
| 278 | |
| 279 | orig_iocb->ki_pos = iocb->ki_pos; |
yangerkun | 9a25440 | 2021-09-30 11:22:28 +0800 | [diff] [blame] | 280 | ovl_aio_put(aio_req); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 281 | } |
| 282 | |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 283 | static void ovl_aio_rw_complete(struct kiocb *iocb, long res) |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 284 | { |
| 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; |
| 288 | |
| 289 | ovl_aio_cleanup_handler(aio_req); |
Jens Axboe | 6b19b76 | 2021-10-21 09:22:35 -0600 | [diff] [blame] | 290 | orig_iocb->ki_complete(orig_iocb, res); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 291 | } |
| 292 | |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 293 | static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 294 | { |
| 295 | struct file *file = iocb->ki_filp; |
| 296 | struct fd real; |
| 297 | const struct cred *old_cred; |
| 298 | ssize_t ret; |
| 299 | |
| 300 | if (!iov_iter_count(iter)) |
| 301 | return 0; |
| 302 | |
| 303 | ret = ovl_real_fdget(file, &real); |
| 304 | if (ret) |
| 305 | return ret; |
| 306 | |
Miklos Szeredi | 1dc1eed | 2021-09-27 11:23:57 +0200 | [diff] [blame] | 307 | ret = -EINVAL; |
| 308 | if (iocb->ki_flags & IOCB_DIRECT && |
| 309 | (!real.file->f_mapping->a_ops || |
| 310 | !real.file->f_mapping->a_ops->direct_IO)) |
| 311 | goto out_fdput; |
| 312 | |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 313 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 314 | if (is_sync_kiocb(iocb)) { |
| 315 | ret = vfs_iter_read(real.file, iter, &iocb->ki_pos, |
Miklos Szeredi | b778e1e | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 316 | ovl_iocb_to_rwf(iocb->ki_flags)); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 317 | } else { |
| 318 | struct ovl_aio_req *aio_req; |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 319 | |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 320 | ret = -ENOMEM; |
| 321 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); |
| 322 | if (!aio_req) |
| 323 | goto out; |
| 324 | |
| 325 | aio_req->fd = real; |
| 326 | real.flags = 0; |
| 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; |
yangerkun | 9a25440 | 2021-09-30 11:22:28 +0800 | [diff] [blame] | 330 | refcount_set(&aio_req->ref, 2); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 331 | ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter); |
yangerkun | 9a25440 | 2021-09-30 11:22:28 +0800 | [diff] [blame] | 332 | ovl_aio_put(aio_req); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 333 | if (ret != -EIOCBQUEUED) |
| 334 | ovl_aio_cleanup_handler(aio_req); |
| 335 | } |
| 336 | out: |
| 337 | revert_creds(old_cred); |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 338 | ovl_file_accessed(file); |
Miklos Szeredi | 1dc1eed | 2021-09-27 11:23:57 +0200 | [diff] [blame] | 339 | out_fdput: |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 340 | fdput(real); |
| 341 | |
| 342 | return ret; |
| 343 | } |
| 344 | |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 345 | static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 346 | { |
| 347 | struct file *file = iocb->ki_filp; |
| 348 | struct inode *inode = file_inode(file); |
| 349 | struct fd real; |
| 350 | const struct cred *old_cred; |
| 351 | ssize_t ret; |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 352 | int ifl = iocb->ki_flags; |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 353 | |
| 354 | if (!iov_iter_count(iter)) |
| 355 | return 0; |
| 356 | |
| 357 | inode_lock(inode); |
| 358 | /* Update mode */ |
| 359 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 360 | ret = file_remove_privs(file); |
| 361 | if (ret) |
| 362 | goto out_unlock; |
| 363 | |
| 364 | ret = ovl_real_fdget(file, &real); |
| 365 | if (ret) |
| 366 | goto out_unlock; |
| 367 | |
Miklos Szeredi | 1dc1eed | 2021-09-27 11:23:57 +0200 | [diff] [blame] | 368 | ret = -EINVAL; |
| 369 | if (iocb->ki_flags & IOCB_DIRECT && |
| 370 | (!real.file->f_mapping->a_ops || |
| 371 | !real.file->f_mapping->a_ops->direct_IO)) |
| 372 | goto out_fdput; |
| 373 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 374 | if (!ovl_should_sync(OVL_FS(inode->i_sb))) |
| 375 | ifl &= ~(IOCB_DSYNC | IOCB_SYNC); |
| 376 | |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 377 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 378 | if (is_sync_kiocb(iocb)) { |
| 379 | file_start_write(real.file); |
| 380 | ret = vfs_iter_write(real.file, iter, &iocb->ki_pos, |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 381 | ovl_iocb_to_rwf(ifl)); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 382 | file_end_write(real.file); |
| 383 | /* Update size */ |
| 384 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 385 | } else { |
| 386 | struct ovl_aio_req *aio_req; |
| 387 | |
| 388 | ret = -ENOMEM; |
| 389 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); |
| 390 | if (!aio_req) |
| 391 | goto out; |
| 392 | |
| 393 | file_start_write(real.file); |
Miklos Szeredi | c853680 | 2020-03-13 15:42:20 +0100 | [diff] [blame] | 394 | /* Pacify lockdep, same trick as done in aio_write() */ |
| 395 | __sb_writers_release(file_inode(real.file)->i_sb, |
| 396 | SB_FREEZE_WRITE); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 397 | aio_req->fd = real; |
| 398 | real.flags = 0; |
| 399 | aio_req->orig_iocb = iocb; |
| 400 | kiocb_clone(&aio_req->iocb, iocb, real.file); |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 401 | aio_req->iocb.ki_flags = ifl; |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 402 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; |
yangerkun | 9a25440 | 2021-09-30 11:22:28 +0800 | [diff] [blame] | 403 | refcount_set(&aio_req->ref, 2); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 404 | ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter); |
yangerkun | 9a25440 | 2021-09-30 11:22:28 +0800 | [diff] [blame] | 405 | ovl_aio_put(aio_req); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 406 | if (ret != -EIOCBQUEUED) |
| 407 | ovl_aio_cleanup_handler(aio_req); |
| 408 | } |
| 409 | out: |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 410 | revert_creds(old_cred); |
Miklos Szeredi | 1dc1eed | 2021-09-27 11:23:57 +0200 | [diff] [blame] | 411 | out_fdput: |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 412 | fdput(real); |
| 413 | |
| 414 | out_unlock: |
| 415 | inode_unlock(inode); |
| 416 | |
| 417 | return ret; |
| 418 | } |
| 419 | |
Miklos Szeredi | 9b91b6b | 2021-07-28 10:38:43 +0200 | [diff] [blame] | 420 | /* |
| 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(). |
| 424 | * |
| 425 | * So do everything ovl_write_iter() does and call iter_file_splice_write() on |
| 426 | * the real file. |
| 427 | */ |
| 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) |
| 430 | { |
| 431 | struct fd real; |
| 432 | const struct cred *old_cred; |
| 433 | struct inode *inode = file_inode(out); |
| 434 | struct inode *realinode = ovl_inode_real(inode); |
| 435 | ssize_t ret; |
| 436 | |
| 437 | inode_lock(inode); |
| 438 | /* Update mode */ |
| 439 | ovl_copyattr(realinode, inode); |
| 440 | ret = file_remove_privs(out); |
| 441 | if (ret) |
| 442 | goto out_unlock; |
| 443 | |
| 444 | ret = ovl_real_fdget(out, &real); |
| 445 | if (ret) |
| 446 | goto out_unlock; |
| 447 | |
| 448 | old_cred = ovl_override_creds(inode->i_sb); |
| 449 | file_start_write(real.file); |
| 450 | |
| 451 | ret = iter_file_splice_write(pipe, real.file, ppos, len, flags); |
| 452 | |
| 453 | file_end_write(real.file); |
| 454 | /* Update size */ |
| 455 | ovl_copyattr(realinode, inode); |
| 456 | revert_creds(old_cred); |
| 457 | fdput(real); |
| 458 | |
| 459 | out_unlock: |
| 460 | inode_unlock(inode); |
| 461 | |
| 462 | return ret; |
| 463 | } |
| 464 | |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 465 | static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 466 | { |
| 467 | struct fd real; |
| 468 | const struct cred *old_cred; |
| 469 | int ret; |
| 470 | |
Sargun Dhillon | 335d3fc | 2021-01-07 16:10:43 -0800 | [diff] [blame] | 471 | ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb)); |
| 472 | if (ret <= 0) |
| 473 | return ret; |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 474 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 475 | ret = ovl_real_fdget_meta(file, &real, !datasync); |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 476 | if (ret) |
| 477 | return ret; |
| 478 | |
| 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); |
| 484 | } |
| 485 | |
| 486 | fdput(real); |
| 487 | |
| 488 | return ret; |
| 489 | } |
| 490 | |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 491 | static int ovl_mmap(struct file *file, struct vm_area_struct *vma) |
| 492 | { |
| 493 | struct file *realfile = file->private_data; |
| 494 | const struct cred *old_cred; |
| 495 | int ret; |
| 496 | |
| 497 | if (!realfile->f_op->mmap) |
| 498 | return -ENODEV; |
| 499 | |
| 500 | if (WARN_ON(file != vma->vm_file)) |
| 501 | return -EIO; |
| 502 | |
Christian König | 2896900 | 2021-04-23 14:28:54 -0700 | [diff] [blame] | 503 | vma_set_file(vma, realfile); |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 504 | |
| 505 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 506 | ret = call_mmap(vma->vm_file, vma); |
| 507 | revert_creds(old_cred); |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 508 | ovl_file_accessed(file); |
| 509 | |
| 510 | return ret; |
| 511 | } |
| 512 | |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 513 | static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 514 | { |
| 515 | struct inode *inode = file_inode(file); |
| 516 | struct fd real; |
| 517 | const struct cred *old_cred; |
| 518 | int ret; |
| 519 | |
| 520 | ret = ovl_real_fdget(file, &real); |
| 521 | if (ret) |
| 522 | return ret; |
| 523 | |
| 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); |
| 527 | |
| 528 | /* Update size */ |
| 529 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 530 | |
| 531 | fdput(real); |
| 532 | |
| 533 | return ret; |
| 534 | } |
| 535 | |
Amir Goldstein | b833a36 | 2018-08-28 10:58:41 +0300 | [diff] [blame] | 536 | static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) |
| 537 | { |
| 538 | struct fd real; |
| 539 | const struct cred *old_cred; |
| 540 | int ret; |
| 541 | |
| 542 | ret = ovl_real_fdget(file, &real); |
| 543 | if (ret) |
| 544 | return ret; |
| 545 | |
| 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); |
| 549 | |
| 550 | fdput(real); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 555 | enum ovl_copyop { |
| 556 | OVL_COPY, |
| 557 | OVL_CLONE, |
| 558 | OVL_DEDUPE, |
| 559 | }; |
| 560 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 561 | static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 562 | struct file *file_out, loff_t pos_out, |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 563 | loff_t len, unsigned int flags, enum ovl_copyop op) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 564 | { |
| 565 | struct inode *inode_out = file_inode(file_out); |
| 566 | struct fd real_in, real_out; |
| 567 | const struct cred *old_cred; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 568 | loff_t ret; |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 569 | |
| 570 | ret = ovl_real_fdget(file_out, &real_out); |
| 571 | if (ret) |
| 572 | return ret; |
| 573 | |
| 574 | ret = ovl_real_fdget(file_in, &real_in); |
| 575 | if (ret) { |
| 576 | fdput(real_out); |
| 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | old_cred = ovl_override_creds(file_inode(file_out)->i_sb); |
| 581 | switch (op) { |
| 582 | case OVL_COPY: |
| 583 | ret = vfs_copy_file_range(real_in.file, pos_in, |
| 584 | real_out.file, pos_out, len, flags); |
| 585 | break; |
| 586 | |
| 587 | case OVL_CLONE: |
Amir Goldstein | a725356 | 2018-09-18 16:34:34 +0300 | [diff] [blame] | 588 | ret = vfs_clone_file_range(real_in.file, pos_in, |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 589 | real_out.file, pos_out, len, flags); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 590 | break; |
| 591 | |
| 592 | case OVL_DEDUPE: |
| 593 | ret = vfs_dedupe_file_range_one(real_in.file, pos_in, |
Darrick J. Wong | df36583 | 2018-10-30 10:42:03 +1100 | [diff] [blame] | 594 | real_out.file, pos_out, len, |
| 595 | flags); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 596 | break; |
| 597 | } |
| 598 | revert_creds(old_cred); |
| 599 | |
| 600 | /* Update size */ |
| 601 | ovl_copyattr(ovl_inode_real(inode_out), inode_out); |
| 602 | |
| 603 | fdput(real_in); |
| 604 | fdput(real_out); |
| 605 | |
| 606 | return ret; |
| 607 | } |
| 608 | |
| 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) |
| 612 | { |
| 613 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags, |
| 614 | OVL_COPY); |
| 615 | } |
| 616 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 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) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 620 | { |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 621 | enum ovl_copyop op; |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 622 | |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 623 | if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) |
| 624 | return -EINVAL; |
| 625 | |
| 626 | if (remap_flags & REMAP_FILE_DEDUP) |
| 627 | op = OVL_DEDUPE; |
| 628 | else |
| 629 | op = OVL_CLONE; |
| 630 | |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 631 | /* |
| 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). |
| 634 | */ |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 635 | if (op == OVL_DEDUPE && |
| 636 | (!ovl_inode_upper(file_inode(file_in)) || |
| 637 | !ovl_inode_upper(file_inode(file_out)))) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 638 | return -EPERM; |
| 639 | |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 640 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, |
| 641 | remap_flags, op); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 642 | } |
| 643 | |
Sargun Dhillon | 1f0cb8b | 2020-11-29 19:00:39 -0800 | [diff] [blame] | 644 | static int ovl_flush(struct file *file, fl_owner_t id) |
| 645 | { |
| 646 | struct fd real; |
| 647 | const struct cred *old_cred; |
| 648 | int err; |
| 649 | |
| 650 | err = ovl_real_fdget(file, &real); |
| 651 | if (err) |
| 652 | return err; |
| 653 | |
| 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); |
| 658 | } |
| 659 | fdput(real); |
| 660 | |
| 661 | return err; |
| 662 | } |
| 663 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 664 | const struct file_operations ovl_file_operations = { |
| 665 | .open = ovl_open, |
| 666 | .release = ovl_release, |
| 667 | .llseek = ovl_llseek, |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 668 | .read_iter = ovl_read_iter, |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 669 | .write_iter = ovl_write_iter, |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 670 | .fsync = ovl_fsync, |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 671 | .mmap = ovl_mmap, |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 672 | .fallocate = ovl_fallocate, |
Amir Goldstein | b833a36 | 2018-08-28 10:58:41 +0300 | [diff] [blame] | 673 | .fadvise = ovl_fadvise, |
Sargun Dhillon | 1f0cb8b | 2020-11-29 19:00:39 -0800 | [diff] [blame] | 674 | .flush = ovl_flush, |
Miklos Szeredi | 82a763e | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 675 | .splice_read = generic_file_splice_read, |
Miklos Szeredi | 9b91b6b | 2021-07-28 10:38:43 +0200 | [diff] [blame] | 676 | .splice_write = ovl_splice_write, |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 677 | |
| 678 | .copy_file_range = ovl_copy_file_range, |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 679 | .remap_file_range = ovl_remap_file_range, |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 680 | }; |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 681 | |
| 682 | int __init ovl_aio_request_cache_init(void) |
| 683 | { |
| 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) |
| 688 | return -ENOMEM; |
| 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
| 693 | void ovl_aio_request_cache_destroy(void) |
| 694 | { |
| 695 | kmem_cache_destroy(ovl_aio_request_cachep); |
| 696 | } |