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; |
| 20 | struct kiocb *orig_iocb; |
| 21 | struct fd fd; |
| 22 | }; |
| 23 | |
| 24 | static struct kmem_cache *ovl_aio_request_cachep; |
| 25 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 26 | static char ovl_whatisit(struct inode *inode, struct inode *realinode) |
| 27 | { |
| 28 | if (realinode != ovl_inode_upper(inode)) |
| 29 | return 'l'; |
| 30 | if (ovl_has_upperdata(inode)) |
| 31 | return 'u'; |
| 32 | else |
| 33 | return 'm'; |
| 34 | } |
| 35 | |
Amir Goldstein | 81a33c1 | 2020-06-18 18:43:53 +0300 | [diff] [blame] | 36 | /* No atime modificaton nor notify on underlying */ |
| 37 | #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY) |
| 38 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 39 | static struct file *ovl_open_realfile(const struct file *file, |
| 40 | struct inode *realinode) |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 41 | { |
| 42 | struct inode *inode = file_inode(file); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 43 | struct file *realfile; |
| 44 | const struct cred *old_cred; |
Amir Goldstein | 81a33c1 | 2020-06-18 18:43:53 +0300 | [diff] [blame] | 45 | int flags = file->f_flags | OVL_OPEN_FLAGS; |
Miklos Szeredi | 05acefb | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 46 | int acc_mode = ACC_MODE(flags); |
| 47 | int err; |
| 48 | |
| 49 | if (flags & O_APPEND) |
| 50 | acc_mode |= MAY_APPEND; |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 51 | |
| 52 | old_cred = ovl_override_creds(inode->i_sb); |
Miklos Szeredi | 05acefb | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 53 | err = inode_permission(realinode, MAY_OPEN | acc_mode); |
| 54 | if (err) { |
| 55 | realfile = ERR_PTR(err); |
| 56 | } else if (!inode_owner_or_capable(realinode)) { |
| 57 | realfile = ERR_PTR(-EPERM); |
| 58 | } else { |
| 59 | realfile = open_with_fake_path(&file->f_path, flags, realinode, |
| 60 | current_cred()); |
| 61 | } |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 62 | revert_creds(old_cred); |
| 63 | |
| 64 | pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 65 | file, file, ovl_whatisit(inode, realinode), file->f_flags, |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 66 | realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); |
| 67 | |
| 68 | return realfile; |
| 69 | } |
| 70 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 71 | #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT) |
| 72 | |
| 73 | static int ovl_change_flags(struct file *file, unsigned int flags) |
| 74 | { |
| 75 | struct inode *inode = file_inode(file); |
| 76 | int err; |
| 77 | |
Amir Goldstein | 81a33c1 | 2020-06-18 18:43:53 +0300 | [diff] [blame] | 78 | flags |= OVL_OPEN_FLAGS; |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 79 | |
| 80 | /* If some flag changed that cannot be changed then something's amiss */ |
| 81 | if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK)) |
| 82 | return -EIO; |
| 83 | |
| 84 | flags &= OVL_SETFL_MASK; |
| 85 | |
| 86 | if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) |
| 87 | return -EPERM; |
| 88 | |
| 89 | if (flags & O_DIRECT) { |
| 90 | if (!file->f_mapping->a_ops || |
| 91 | !file->f_mapping->a_ops->direct_IO) |
| 92 | return -EINVAL; |
| 93 | } |
| 94 | |
| 95 | if (file->f_op->check_flags) { |
| 96 | err = file->f_op->check_flags(flags); |
| 97 | if (err) |
| 98 | return err; |
| 99 | } |
| 100 | |
| 101 | spin_lock(&file->f_lock); |
| 102 | file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags; |
| 103 | spin_unlock(&file->f_lock); |
| 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 108 | static int ovl_real_fdget_meta(const struct file *file, struct fd *real, |
| 109 | bool allow_meta) |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 110 | { |
| 111 | struct inode *inode = file_inode(file); |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 112 | struct inode *realinode; |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 113 | |
| 114 | real->flags = 0; |
| 115 | real->file = file->private_data; |
| 116 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 117 | if (allow_meta) |
| 118 | realinode = ovl_inode_real(inode); |
| 119 | else |
| 120 | realinode = ovl_inode_realdata(inode); |
| 121 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 122 | /* Has it been copied up since we'd opened it? */ |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 123 | if (unlikely(file_inode(real->file) != realinode)) { |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 124 | real->flags = FDPUT_FPUT; |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 125 | real->file = ovl_open_realfile(file, realinode); |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 126 | |
| 127 | return PTR_ERR_OR_ZERO(real->file); |
| 128 | } |
| 129 | |
| 130 | /* Did the flags change since open? */ |
Amir Goldstein | 81a33c1 | 2020-06-18 18:43:53 +0300 | [diff] [blame] | 131 | if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS)) |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 132 | return ovl_change_flags(real->file, file->f_flags); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 137 | static int ovl_real_fdget(const struct file *file, struct fd *real) |
| 138 | { |
Amir Goldstein | 61536be | 2020-09-29 15:28:47 +0800 | [diff] [blame] | 139 | if (d_is_dir(file_dentry(file))) { |
| 140 | real->flags = 0; |
| 141 | real->file = ovl_dir_real_file(file, false); |
| 142 | |
| 143 | return PTR_ERR_OR_ZERO(real->file); |
| 144 | } |
| 145 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 146 | return ovl_real_fdget_meta(file, real, false); |
| 147 | } |
| 148 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 149 | static int ovl_open(struct inode *inode, struct file *file) |
| 150 | { |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 151 | struct file *realfile; |
| 152 | int err; |
| 153 | |
Amir Goldstein | 3428030 | 2019-01-22 07:01:39 +0200 | [diff] [blame] | 154 | err = ovl_maybe_copy_up(file_dentry(file), file->f_flags); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 155 | if (err) |
| 156 | return err; |
| 157 | |
| 158 | /* No longer need these flags, so don't pass them on to underlying fs */ |
| 159 | file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 160 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 161 | realfile = ovl_open_realfile(file, ovl_inode_realdata(inode)); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 162 | if (IS_ERR(realfile)) |
| 163 | return PTR_ERR(realfile); |
| 164 | |
| 165 | file->private_data = realfile; |
| 166 | |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | static int ovl_release(struct inode *inode, struct file *file) |
| 171 | { |
| 172 | fput(file->private_data); |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
| 177 | static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) |
| 178 | { |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 179 | struct inode *inode = file_inode(file); |
| 180 | struct fd real; |
| 181 | const struct cred *old_cred; |
Miklos Szeredi | a4ac9d4 | 2020-02-03 11:41:53 +0100 | [diff] [blame] | 182 | loff_t ret; |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 183 | |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 184 | /* |
| 185 | * The two special cases below do not need to involve real fs, |
| 186 | * so we can optimizing concurrent callers. |
| 187 | */ |
| 188 | if (offset == 0) { |
| 189 | if (whence == SEEK_CUR) |
| 190 | return file->f_pos; |
| 191 | |
| 192 | if (whence == SEEK_SET) |
| 193 | return vfs_setpos(file, 0, 0); |
| 194 | } |
| 195 | |
| 196 | ret = ovl_real_fdget(file, &real); |
| 197 | if (ret) |
| 198 | return ret; |
| 199 | |
| 200 | /* |
| 201 | * Overlay file f_pos is the master copy that is preserved |
| 202 | * through copy up and modified on read/write, but only real |
| 203 | * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose |
| 204 | * limitations that are more strict than ->s_maxbytes for specific |
| 205 | * files, so we use the real file to perform seeks. |
| 206 | */ |
Amir Goldstein | b1f9d38 | 2019-12-21 11:42:29 +0200 | [diff] [blame] | 207 | ovl_inode_lock(inode); |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 208 | real.file->f_pos = file->f_pos; |
| 209 | |
| 210 | old_cred = ovl_override_creds(inode->i_sb); |
| 211 | ret = vfs_llseek(real.file, offset, whence); |
| 212 | revert_creds(old_cred); |
| 213 | |
| 214 | file->f_pos = real.file->f_pos; |
Amir Goldstein | b1f9d38 | 2019-12-21 11:42:29 +0200 | [diff] [blame] | 215 | ovl_inode_unlock(inode); |
Amir Goldstein | 9e46b84 | 2019-02-27 13:32:11 +0200 | [diff] [blame] | 216 | |
| 217 | fdput(real); |
| 218 | |
| 219 | return ret; |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 220 | } |
| 221 | |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 222 | static void ovl_file_accessed(struct file *file) |
| 223 | { |
| 224 | struct inode *inode, *upperinode; |
| 225 | |
| 226 | if (file->f_flags & O_NOATIME) |
| 227 | return; |
| 228 | |
| 229 | inode = file_inode(file); |
| 230 | upperinode = ovl_inode_upper(inode); |
| 231 | |
| 232 | if (!upperinode) |
| 233 | return; |
| 234 | |
| 235 | if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) || |
| 236 | !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) { |
| 237 | inode->i_mtime = upperinode->i_mtime; |
| 238 | inode->i_ctime = upperinode->i_ctime; |
| 239 | } |
| 240 | |
| 241 | touch_atime(&file->f_path); |
| 242 | } |
| 243 | |
Miklos Szeredi | b778e1e | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 244 | static rwf_t ovl_iocb_to_rwf(int ifl) |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 245 | { |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 246 | rwf_t flags = 0; |
| 247 | |
| 248 | if (ifl & IOCB_NOWAIT) |
| 249 | flags |= RWF_NOWAIT; |
| 250 | if (ifl & IOCB_HIPRI) |
| 251 | flags |= RWF_HIPRI; |
| 252 | if (ifl & IOCB_DSYNC) |
| 253 | flags |= RWF_DSYNC; |
| 254 | if (ifl & IOCB_SYNC) |
| 255 | flags |= RWF_SYNC; |
| 256 | |
| 257 | return flags; |
| 258 | } |
| 259 | |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 260 | static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req) |
| 261 | { |
| 262 | struct kiocb *iocb = &aio_req->iocb; |
| 263 | struct kiocb *orig_iocb = aio_req->orig_iocb; |
| 264 | |
| 265 | if (iocb->ki_flags & IOCB_WRITE) { |
| 266 | struct inode *inode = file_inode(orig_iocb->ki_filp); |
| 267 | |
Miklos Szeredi | c853680 | 2020-03-13 15:42:20 +0100 | [diff] [blame] | 268 | /* Actually acquired in ovl_write_iter() */ |
| 269 | __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb, |
| 270 | SB_FREEZE_WRITE); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 271 | file_end_write(iocb->ki_filp); |
| 272 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 273 | } |
| 274 | |
| 275 | orig_iocb->ki_pos = iocb->ki_pos; |
| 276 | fdput(aio_req->fd); |
| 277 | kmem_cache_free(ovl_aio_request_cachep, aio_req); |
| 278 | } |
| 279 | |
| 280 | static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2) |
| 281 | { |
| 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; |
| 285 | |
| 286 | ovl_aio_cleanup_handler(aio_req); |
| 287 | orig_iocb->ki_complete(orig_iocb, res, res2); |
| 288 | } |
| 289 | |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 290 | static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 291 | { |
| 292 | struct file *file = iocb->ki_filp; |
| 293 | struct fd real; |
| 294 | const struct cred *old_cred; |
| 295 | ssize_t ret; |
| 296 | |
| 297 | if (!iov_iter_count(iter)) |
| 298 | return 0; |
| 299 | |
| 300 | ret = ovl_real_fdget(file, &real); |
| 301 | if (ret) |
| 302 | return ret; |
| 303 | |
| 304 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 305 | if (is_sync_kiocb(iocb)) { |
| 306 | ret = vfs_iter_read(real.file, iter, &iocb->ki_pos, |
Miklos Szeredi | b778e1e | 2020-06-04 10:48:19 +0200 | [diff] [blame] | 307 | ovl_iocb_to_rwf(iocb->ki_flags)); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 308 | } else { |
| 309 | struct ovl_aio_req *aio_req; |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 310 | |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 311 | ret = -ENOMEM; |
| 312 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); |
| 313 | if (!aio_req) |
| 314 | goto out; |
| 315 | |
| 316 | aio_req->fd = real; |
| 317 | real.flags = 0; |
| 318 | aio_req->orig_iocb = iocb; |
| 319 | kiocb_clone(&aio_req->iocb, iocb, real.file); |
| 320 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; |
| 321 | ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter); |
| 322 | if (ret != -EIOCBQUEUED) |
| 323 | ovl_aio_cleanup_handler(aio_req); |
| 324 | } |
| 325 | out: |
| 326 | revert_creds(old_cred); |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 327 | ovl_file_accessed(file); |
| 328 | |
| 329 | fdput(real); |
| 330 | |
| 331 | return ret; |
| 332 | } |
| 333 | |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 334 | static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 335 | { |
| 336 | struct file *file = iocb->ki_filp; |
| 337 | struct inode *inode = file_inode(file); |
| 338 | struct fd real; |
| 339 | const struct cred *old_cred; |
| 340 | ssize_t ret; |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 341 | int ifl = iocb->ki_flags; |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 342 | |
| 343 | if (!iov_iter_count(iter)) |
| 344 | return 0; |
| 345 | |
| 346 | inode_lock(inode); |
| 347 | /* Update mode */ |
| 348 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 349 | ret = file_remove_privs(file); |
| 350 | if (ret) |
| 351 | goto out_unlock; |
| 352 | |
| 353 | ret = ovl_real_fdget(file, &real); |
| 354 | if (ret) |
| 355 | goto out_unlock; |
| 356 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 357 | if (!ovl_should_sync(OVL_FS(inode->i_sb))) |
| 358 | ifl &= ~(IOCB_DSYNC | IOCB_SYNC); |
| 359 | |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 360 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 361 | if (is_sync_kiocb(iocb)) { |
| 362 | file_start_write(real.file); |
| 363 | ret = vfs_iter_write(real.file, iter, &iocb->ki_pos, |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 364 | ovl_iocb_to_rwf(ifl)); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 365 | file_end_write(real.file); |
| 366 | /* Update size */ |
| 367 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 368 | } else { |
| 369 | struct ovl_aio_req *aio_req; |
| 370 | |
| 371 | ret = -ENOMEM; |
| 372 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); |
| 373 | if (!aio_req) |
| 374 | goto out; |
| 375 | |
| 376 | file_start_write(real.file); |
Miklos Szeredi | c853680 | 2020-03-13 15:42:20 +0100 | [diff] [blame] | 377 | /* Pacify lockdep, same trick as done in aio_write() */ |
| 378 | __sb_writers_release(file_inode(real.file)->i_sb, |
| 379 | SB_FREEZE_WRITE); |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 380 | aio_req->fd = real; |
| 381 | real.flags = 0; |
| 382 | aio_req->orig_iocb = iocb; |
| 383 | kiocb_clone(&aio_req->iocb, iocb, real.file); |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 384 | aio_req->iocb.ki_flags = ifl; |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 385 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; |
| 386 | ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter); |
| 387 | if (ret != -EIOCBQUEUED) |
| 388 | ovl_aio_cleanup_handler(aio_req); |
| 389 | } |
| 390 | out: |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 391 | revert_creds(old_cred); |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 392 | fdput(real); |
| 393 | |
| 394 | out_unlock: |
| 395 | inode_unlock(inode); |
| 396 | |
| 397 | return ret; |
| 398 | } |
| 399 | |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 400 | static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 401 | { |
| 402 | struct fd real; |
| 403 | const struct cred *old_cred; |
| 404 | int ret; |
| 405 | |
Vivek Goyal | c86243b0 | 2020-08-31 14:15:29 -0400 | [diff] [blame] | 406 | if (!ovl_should_sync(OVL_FS(file_inode(file)->i_sb))) |
| 407 | return 0; |
| 408 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 409 | ret = ovl_real_fdget_meta(file, &real, !datasync); |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 410 | if (ret) |
| 411 | return ret; |
| 412 | |
| 413 | /* Don't sync lower file for fear of receiving EROFS error */ |
| 414 | if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) { |
| 415 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 416 | ret = vfs_fsync_range(real.file, start, end, datasync); |
| 417 | revert_creds(old_cred); |
| 418 | } |
| 419 | |
| 420 | fdput(real); |
| 421 | |
| 422 | return ret; |
| 423 | } |
| 424 | |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 425 | static int ovl_mmap(struct file *file, struct vm_area_struct *vma) |
| 426 | { |
| 427 | struct file *realfile = file->private_data; |
| 428 | const struct cred *old_cred; |
| 429 | int ret; |
| 430 | |
| 431 | if (!realfile->f_op->mmap) |
| 432 | return -ENODEV; |
| 433 | |
| 434 | if (WARN_ON(file != vma->vm_file)) |
| 435 | return -EIO; |
| 436 | |
| 437 | vma->vm_file = get_file(realfile); |
| 438 | |
| 439 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 440 | ret = call_mmap(vma->vm_file, vma); |
| 441 | revert_creds(old_cred); |
| 442 | |
| 443 | if (ret) { |
| 444 | /* Drop reference count from new vm_file value */ |
| 445 | fput(realfile); |
| 446 | } else { |
| 447 | /* Drop reference count from previous vm_file value */ |
| 448 | fput(file); |
| 449 | } |
| 450 | |
| 451 | ovl_file_accessed(file); |
| 452 | |
| 453 | return ret; |
| 454 | } |
| 455 | |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 456 | static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 457 | { |
| 458 | struct inode *inode = file_inode(file); |
| 459 | struct fd real; |
| 460 | const struct cred *old_cred; |
| 461 | int ret; |
| 462 | |
| 463 | ret = ovl_real_fdget(file, &real); |
| 464 | if (ret) |
| 465 | return ret; |
| 466 | |
| 467 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 468 | ret = vfs_fallocate(real.file, mode, offset, len); |
| 469 | revert_creds(old_cred); |
| 470 | |
| 471 | /* Update size */ |
| 472 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 473 | |
| 474 | fdput(real); |
| 475 | |
| 476 | return ret; |
| 477 | } |
| 478 | |
Amir Goldstein | b833a36 | 2018-08-28 10:58:41 +0300 | [diff] [blame] | 479 | static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) |
| 480 | { |
| 481 | struct fd real; |
| 482 | const struct cred *old_cred; |
| 483 | int ret; |
| 484 | |
| 485 | ret = ovl_real_fdget(file, &real); |
| 486 | if (ret) |
| 487 | return ret; |
| 488 | |
| 489 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 490 | ret = vfs_fadvise(real.file, offset, len, advice); |
| 491 | revert_creds(old_cred); |
| 492 | |
| 493 | fdput(real); |
| 494 | |
| 495 | return ret; |
| 496 | } |
| 497 | |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 498 | static long ovl_real_ioctl(struct file *file, unsigned int cmd, |
| 499 | unsigned long arg) |
| 500 | { |
| 501 | struct fd real; |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 502 | long ret; |
| 503 | |
| 504 | ret = ovl_real_fdget(file, &real); |
| 505 | if (ret) |
| 506 | return ret; |
| 507 | |
Miklos Szeredi | 292f902 | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 508 | ret = security_file_ioctl(real.file, cmd, arg); |
Miklos Szeredi | 89bdfaf | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 509 | if (!ret) { |
| 510 | /* |
| 511 | * Don't override creds, since we currently can't safely check |
| 512 | * permissions before doing so. |
| 513 | */ |
Miklos Szeredi | 292f902 | 2020-06-02 22:20:26 +0200 | [diff] [blame] | 514 | ret = vfs_ioctl(real.file, cmd, arg); |
Miklos Szeredi | 89bdfaf | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 515 | } |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 516 | |
| 517 | fdput(real); |
| 518 | |
| 519 | return ret; |
| 520 | } |
| 521 | |
Amir Goldstein | b21d9c43 | 2019-05-26 09:28:25 +0300 | [diff] [blame] | 522 | static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd, |
Miklos Szeredi | 89bdfaf | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 523 | unsigned long arg) |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 524 | { |
| 525 | long ret; |
| 526 | struct inode *inode = file_inode(file); |
Jiufei Xue | 98487de | 2019-05-06 15:41:02 +0800 | [diff] [blame] | 527 | |
| 528 | if (!inode_owner_or_capable(inode)) |
| 529 | return -EACCES; |
| 530 | |
Jiufei Xue | 98487de | 2019-05-06 15:41:02 +0800 | [diff] [blame] | 531 | ret = mnt_want_write_file(file); |
| 532 | if (ret) |
| 533 | return ret; |
| 534 | |
| 535 | inode_lock(inode); |
| 536 | |
Miklos Szeredi | 89bdfaf | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 537 | /* |
| 538 | * Prevent copy up if immutable and has no CAP_LINUX_IMMUTABLE |
| 539 | * capability. |
| 540 | */ |
| 541 | ret = -EPERM; |
| 542 | if (!ovl_has_upperdata(inode) && IS_IMMUTABLE(inode) && |
| 543 | !capable(CAP_LINUX_IMMUTABLE)) |
Jiufei Xue | 98487de | 2019-05-06 15:41:02 +0800 | [diff] [blame] | 544 | goto unlock; |
| 545 | |
| 546 | ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY); |
| 547 | if (ret) |
| 548 | goto unlock; |
| 549 | |
Amir Goldstein | b21d9c43 | 2019-05-26 09:28:25 +0300 | [diff] [blame] | 550 | ret = ovl_real_ioctl(file, cmd, arg); |
Jiufei Xue | 98487de | 2019-05-06 15:41:02 +0800 | [diff] [blame] | 551 | |
| 552 | ovl_copyflags(ovl_inode_real(inode), inode); |
| 553 | unlock: |
| 554 | inode_unlock(inode); |
| 555 | |
| 556 | mnt_drop_write_file(file); |
| 557 | |
| 558 | return ret; |
| 559 | |
| 560 | } |
| 561 | |
Amir Goldstein | 61536be | 2020-09-29 15:28:47 +0800 | [diff] [blame] | 562 | long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Jiufei Xue | 98487de | 2019-05-06 15:41:02 +0800 | [diff] [blame] | 563 | { |
| 564 | long ret; |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 565 | |
| 566 | switch (cmd) { |
| 567 | case FS_IOC_GETFLAGS: |
Amir Goldstein | b21d9c43 | 2019-05-26 09:28:25 +0300 | [diff] [blame] | 568 | case FS_IOC_FSGETXATTR: |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 569 | ret = ovl_real_ioctl(file, cmd, arg); |
| 570 | break; |
| 571 | |
Amir Goldstein | b21d9c43 | 2019-05-26 09:28:25 +0300 | [diff] [blame] | 572 | case FS_IOC_FSSETXATTR: |
Miklos Szeredi | 89bdfaf | 2020-12-14 15:26:14 +0100 | [diff] [blame] | 573 | case FS_IOC_SETFLAGS: |
| 574 | ret = ovl_ioctl_set_flags(file, cmd, arg); |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 575 | break; |
| 576 | |
| 577 | default: |
| 578 | ret = -ENOTTY; |
| 579 | } |
| 580 | |
| 581 | return ret; |
| 582 | } |
| 583 | |
Amir Goldstein | 61536be | 2020-09-29 15:28:47 +0800 | [diff] [blame] | 584 | #ifdef CONFIG_COMPAT |
| 585 | long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 586 | { |
| 587 | switch (cmd) { |
| 588 | case FS_IOC32_GETFLAGS: |
| 589 | cmd = FS_IOC_GETFLAGS; |
| 590 | break; |
| 591 | |
| 592 | case FS_IOC32_SETFLAGS: |
| 593 | cmd = FS_IOC_SETFLAGS; |
| 594 | break; |
| 595 | |
| 596 | default: |
| 597 | return -ENOIOCTLCMD; |
| 598 | } |
| 599 | |
| 600 | return ovl_ioctl(file, cmd, arg); |
| 601 | } |
Amir Goldstein | 61536be | 2020-09-29 15:28:47 +0800 | [diff] [blame] | 602 | #endif |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 603 | |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 604 | enum ovl_copyop { |
| 605 | OVL_COPY, |
| 606 | OVL_CLONE, |
| 607 | OVL_DEDUPE, |
| 608 | }; |
| 609 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 610 | 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] | 611 | struct file *file_out, loff_t pos_out, |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 612 | loff_t len, unsigned int flags, enum ovl_copyop op) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 613 | { |
| 614 | struct inode *inode_out = file_inode(file_out); |
| 615 | struct fd real_in, real_out; |
| 616 | const struct cred *old_cred; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 617 | loff_t ret; |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 618 | |
| 619 | ret = ovl_real_fdget(file_out, &real_out); |
| 620 | if (ret) |
| 621 | return ret; |
| 622 | |
| 623 | ret = ovl_real_fdget(file_in, &real_in); |
| 624 | if (ret) { |
| 625 | fdput(real_out); |
| 626 | return ret; |
| 627 | } |
| 628 | |
| 629 | old_cred = ovl_override_creds(file_inode(file_out)->i_sb); |
| 630 | switch (op) { |
| 631 | case OVL_COPY: |
| 632 | ret = vfs_copy_file_range(real_in.file, pos_in, |
| 633 | real_out.file, pos_out, len, flags); |
| 634 | break; |
| 635 | |
| 636 | case OVL_CLONE: |
Amir Goldstein | a725356 | 2018-09-18 16:34:34 +0300 | [diff] [blame] | 637 | ret = vfs_clone_file_range(real_in.file, pos_in, |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 638 | real_out.file, pos_out, len, flags); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 639 | break; |
| 640 | |
| 641 | case OVL_DEDUPE: |
| 642 | ret = vfs_dedupe_file_range_one(real_in.file, pos_in, |
Darrick J. Wong | df36583 | 2018-10-30 10:42:03 +1100 | [diff] [blame] | 643 | real_out.file, pos_out, len, |
| 644 | flags); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 645 | break; |
| 646 | } |
| 647 | revert_creds(old_cred); |
| 648 | |
| 649 | /* Update size */ |
| 650 | ovl_copyattr(ovl_inode_real(inode_out), inode_out); |
| 651 | |
| 652 | fdput(real_in); |
| 653 | fdput(real_out); |
| 654 | |
| 655 | return ret; |
| 656 | } |
| 657 | |
| 658 | static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in, |
| 659 | struct file *file_out, loff_t pos_out, |
| 660 | size_t len, unsigned int flags) |
| 661 | { |
| 662 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags, |
| 663 | OVL_COPY); |
| 664 | } |
| 665 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 666 | static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in, |
| 667 | struct file *file_out, loff_t pos_out, |
| 668 | loff_t len, unsigned int remap_flags) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 669 | { |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 670 | enum ovl_copyop op; |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 671 | |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 672 | if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) |
| 673 | return -EINVAL; |
| 674 | |
| 675 | if (remap_flags & REMAP_FILE_DEDUP) |
| 676 | op = OVL_DEDUPE; |
| 677 | else |
| 678 | op = OVL_CLONE; |
| 679 | |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 680 | /* |
| 681 | * Don't copy up because of a dedupe request, this wouldn't make sense |
| 682 | * most of the time (data would be duplicated instead of deduplicated). |
| 683 | */ |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 684 | if (op == OVL_DEDUPE && |
| 685 | (!ovl_inode_upper(file_inode(file_in)) || |
| 686 | !ovl_inode_upper(file_inode(file_out)))) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 687 | return -EPERM; |
| 688 | |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 689 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, |
| 690 | remap_flags, op); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 691 | } |
| 692 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 693 | const struct file_operations ovl_file_operations = { |
| 694 | .open = ovl_open, |
| 695 | .release = ovl_release, |
| 696 | .llseek = ovl_llseek, |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 697 | .read_iter = ovl_read_iter, |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 698 | .write_iter = ovl_write_iter, |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 699 | .fsync = ovl_fsync, |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 700 | .mmap = ovl_mmap, |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 701 | .fallocate = ovl_fallocate, |
Amir Goldstein | b833a36 | 2018-08-28 10:58:41 +0300 | [diff] [blame] | 702 | .fadvise = ovl_fadvise, |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 703 | .unlocked_ioctl = ovl_ioctl, |
Amir Goldstein | 61536be | 2020-09-29 15:28:47 +0800 | [diff] [blame] | 704 | #ifdef CONFIG_COMPAT |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 705 | .compat_ioctl = ovl_compat_ioctl, |
Amir Goldstein | 61536be | 2020-09-29 15:28:47 +0800 | [diff] [blame] | 706 | #endif |
Miklos Szeredi | 82a763e | 2020-12-14 15:26:14 +0100 | [diff] [blame^] | 707 | .splice_read = generic_file_splice_read, |
| 708 | .splice_write = iter_file_splice_write, |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 709 | |
| 710 | .copy_file_range = ovl_copy_file_range, |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 711 | .remap_file_range = ovl_remap_file_range, |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 712 | }; |
Jiufei Xue | 2406a30 | 2019-11-20 17:45:26 +0800 | [diff] [blame] | 713 | |
| 714 | int __init ovl_aio_request_cache_init(void) |
| 715 | { |
| 716 | ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req", |
| 717 | sizeof(struct ovl_aio_req), |
| 718 | 0, SLAB_HWCACHE_ALIGN, NULL); |
| 719 | if (!ovl_aio_request_cachep) |
| 720 | return -ENOMEM; |
| 721 | |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | void ovl_aio_request_cache_destroy(void) |
| 726 | { |
| 727 | kmem_cache_destroy(ovl_aio_request_cachep); |
| 728 | } |