Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Red Hat, Inc. |
| 3 | * |
| 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. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/cred.h> |
| 10 | #include <linux/file.h> |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 11 | #include <linux/mount.h> |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 12 | #include <linux/xattr.h> |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 13 | #include <linux/uio.h> |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 14 | #include "overlayfs.h" |
| 15 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 16 | static char ovl_whatisit(struct inode *inode, struct inode *realinode) |
| 17 | { |
| 18 | if (realinode != ovl_inode_upper(inode)) |
| 19 | return 'l'; |
| 20 | if (ovl_has_upperdata(inode)) |
| 21 | return 'u'; |
| 22 | else |
| 23 | return 'm'; |
| 24 | } |
| 25 | |
| 26 | static struct file *ovl_open_realfile(const struct file *file, |
| 27 | struct inode *realinode) |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 28 | { |
| 29 | struct inode *inode = file_inode(file); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 30 | struct file *realfile; |
| 31 | const struct cred *old_cred; |
| 32 | |
| 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); |
| 37 | |
| 38 | pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 39 | file, file, ovl_whatisit(inode, realinode), file->f_flags, |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 40 | realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); |
| 41 | |
| 42 | return realfile; |
| 43 | } |
| 44 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 45 | #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT) |
| 46 | |
| 47 | static int ovl_change_flags(struct file *file, unsigned int flags) |
| 48 | { |
| 49 | struct inode *inode = file_inode(file); |
| 50 | int err; |
| 51 | |
| 52 | /* No atime modificaton on underlying */ |
| 53 | flags |= O_NOATIME; |
| 54 | |
| 55 | /* If some flag changed that cannot be changed then something's amiss */ |
| 56 | if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK)) |
| 57 | return -EIO; |
| 58 | |
| 59 | flags &= OVL_SETFL_MASK; |
| 60 | |
| 61 | if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) |
| 62 | return -EPERM; |
| 63 | |
| 64 | if (flags & O_DIRECT) { |
| 65 | if (!file->f_mapping->a_ops || |
| 66 | !file->f_mapping->a_ops->direct_IO) |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
| 70 | if (file->f_op->check_flags) { |
| 71 | err = file->f_op->check_flags(flags); |
| 72 | if (err) |
| 73 | return err; |
| 74 | } |
| 75 | |
| 76 | spin_lock(&file->f_lock); |
| 77 | file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags; |
| 78 | spin_unlock(&file->f_lock); |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 83 | static int ovl_real_fdget_meta(const struct file *file, struct fd *real, |
| 84 | bool allow_meta) |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 85 | { |
| 86 | struct inode *inode = file_inode(file); |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 87 | struct inode *realinode; |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 88 | |
| 89 | real->flags = 0; |
| 90 | real->file = file->private_data; |
| 91 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 92 | if (allow_meta) |
| 93 | realinode = ovl_inode_real(inode); |
| 94 | else |
| 95 | realinode = ovl_inode_realdata(inode); |
| 96 | |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 97 | /* Has it been copied up since we'd opened it? */ |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 98 | if (unlikely(file_inode(real->file) != realinode)) { |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 99 | real->flags = FDPUT_FPUT; |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 100 | real->file = ovl_open_realfile(file, realinode); |
Miklos Szeredi | 2ef66b8 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 101 | |
| 102 | return PTR_ERR_OR_ZERO(real->file); |
| 103 | } |
| 104 | |
| 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); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 112 | static int ovl_real_fdget(const struct file *file, struct fd *real) |
| 113 | { |
| 114 | return ovl_real_fdget_meta(file, real, false); |
| 115 | } |
| 116 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 117 | static int ovl_open(struct inode *inode, struct file *file) |
| 118 | { |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 119 | struct file *realfile; |
| 120 | int err; |
| 121 | |
Amir Goldstein | 3428030 | 2019-01-22 07:01:39 +0200 | [diff] [blame^] | 122 | err = ovl_maybe_copy_up(file_dentry(file), file->f_flags); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 123 | if (err) |
| 124 | return err; |
| 125 | |
| 126 | /* No longer need these flags, so don't pass them on to underlying fs */ |
| 127 | file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); |
| 128 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 129 | realfile = ovl_open_realfile(file, ovl_inode_realdata(inode)); |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 130 | if (IS_ERR(realfile)) |
| 131 | return PTR_ERR(realfile); |
| 132 | |
| 133 | file->private_data = realfile; |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | static int ovl_release(struct inode *inode, struct file *file) |
| 139 | { |
| 140 | fput(file->private_data); |
| 141 | |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static loff_t ovl_llseek(struct file *file, loff_t offset, int whence) |
| 146 | { |
| 147 | struct inode *realinode = ovl_inode_real(file_inode(file)); |
| 148 | |
| 149 | return generic_file_llseek_size(file, offset, whence, |
| 150 | realinode->i_sb->s_maxbytes, |
| 151 | i_size_read(realinode)); |
| 152 | } |
| 153 | |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 154 | static void ovl_file_accessed(struct file *file) |
| 155 | { |
| 156 | struct inode *inode, *upperinode; |
| 157 | |
| 158 | if (file->f_flags & O_NOATIME) |
| 159 | return; |
| 160 | |
| 161 | inode = file_inode(file); |
| 162 | upperinode = ovl_inode_upper(inode); |
| 163 | |
| 164 | if (!upperinode) |
| 165 | return; |
| 166 | |
| 167 | if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) || |
| 168 | !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) { |
| 169 | inode->i_mtime = upperinode->i_mtime; |
| 170 | inode->i_ctime = upperinode->i_ctime; |
| 171 | } |
| 172 | |
| 173 | touch_atime(&file->f_path); |
| 174 | } |
| 175 | |
| 176 | static rwf_t ovl_iocb_to_rwf(struct kiocb *iocb) |
| 177 | { |
| 178 | int ifl = iocb->ki_flags; |
| 179 | rwf_t flags = 0; |
| 180 | |
| 181 | if (ifl & IOCB_NOWAIT) |
| 182 | flags |= RWF_NOWAIT; |
| 183 | if (ifl & IOCB_HIPRI) |
| 184 | flags |= RWF_HIPRI; |
| 185 | if (ifl & IOCB_DSYNC) |
| 186 | flags |= RWF_DSYNC; |
| 187 | if (ifl & IOCB_SYNC) |
| 188 | flags |= RWF_SYNC; |
| 189 | |
| 190 | return flags; |
| 191 | } |
| 192 | |
| 193 | static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 194 | { |
| 195 | struct file *file = iocb->ki_filp; |
| 196 | struct fd real; |
| 197 | const struct cred *old_cred; |
| 198 | ssize_t ret; |
| 199 | |
| 200 | if (!iov_iter_count(iter)) |
| 201 | return 0; |
| 202 | |
| 203 | ret = ovl_real_fdget(file, &real); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 208 | ret = vfs_iter_read(real.file, iter, &iocb->ki_pos, |
| 209 | ovl_iocb_to_rwf(iocb)); |
| 210 | revert_creds(old_cred); |
| 211 | |
| 212 | ovl_file_accessed(file); |
| 213 | |
| 214 | fdput(real); |
| 215 | |
| 216 | return ret; |
| 217 | } |
| 218 | |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 219 | static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
| 220 | { |
| 221 | struct file *file = iocb->ki_filp; |
| 222 | struct inode *inode = file_inode(file); |
| 223 | struct fd real; |
| 224 | const struct cred *old_cred; |
| 225 | ssize_t ret; |
| 226 | |
| 227 | if (!iov_iter_count(iter)) |
| 228 | return 0; |
| 229 | |
| 230 | inode_lock(inode); |
| 231 | /* Update mode */ |
| 232 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 233 | ret = file_remove_privs(file); |
| 234 | if (ret) |
| 235 | goto out_unlock; |
| 236 | |
| 237 | ret = ovl_real_fdget(file, &real); |
| 238 | if (ret) |
| 239 | goto out_unlock; |
| 240 | |
| 241 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
Amir Goldstein | 898cc19 | 2018-09-18 16:34:32 +0300 | [diff] [blame] | 242 | file_start_write(real.file); |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 243 | ret = vfs_iter_write(real.file, iter, &iocb->ki_pos, |
| 244 | ovl_iocb_to_rwf(iocb)); |
Amir Goldstein | 898cc19 | 2018-09-18 16:34:32 +0300 | [diff] [blame] | 245 | file_end_write(real.file); |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 246 | revert_creds(old_cred); |
| 247 | |
| 248 | /* Update size */ |
| 249 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 250 | |
| 251 | fdput(real); |
| 252 | |
| 253 | out_unlock: |
| 254 | inode_unlock(inode); |
| 255 | |
| 256 | return ret; |
| 257 | } |
| 258 | |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 259 | static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
| 260 | { |
| 261 | struct fd real; |
| 262 | const struct cred *old_cred; |
| 263 | int ret; |
| 264 | |
Vivek Goyal | 8c444d2 | 2018-05-11 11:49:31 -0400 | [diff] [blame] | 265 | ret = ovl_real_fdget_meta(file, &real, !datasync); |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 266 | if (ret) |
| 267 | return ret; |
| 268 | |
| 269 | /* Don't sync lower file for fear of receiving EROFS error */ |
| 270 | if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) { |
| 271 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 272 | ret = vfs_fsync_range(real.file, start, end, datasync); |
| 273 | revert_creds(old_cred); |
| 274 | } |
| 275 | |
| 276 | fdput(real); |
| 277 | |
| 278 | return ret; |
| 279 | } |
| 280 | |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 281 | static int ovl_mmap(struct file *file, struct vm_area_struct *vma) |
| 282 | { |
| 283 | struct file *realfile = file->private_data; |
| 284 | const struct cred *old_cred; |
| 285 | int ret; |
| 286 | |
| 287 | if (!realfile->f_op->mmap) |
| 288 | return -ENODEV; |
| 289 | |
| 290 | if (WARN_ON(file != vma->vm_file)) |
| 291 | return -EIO; |
| 292 | |
| 293 | vma->vm_file = get_file(realfile); |
| 294 | |
| 295 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 296 | ret = call_mmap(vma->vm_file, vma); |
| 297 | revert_creds(old_cred); |
| 298 | |
| 299 | if (ret) { |
| 300 | /* Drop reference count from new vm_file value */ |
| 301 | fput(realfile); |
| 302 | } else { |
| 303 | /* Drop reference count from previous vm_file value */ |
| 304 | fput(file); |
| 305 | } |
| 306 | |
| 307 | ovl_file_accessed(file); |
| 308 | |
| 309 | return ret; |
| 310 | } |
| 311 | |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 312 | static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len) |
| 313 | { |
| 314 | struct inode *inode = file_inode(file); |
| 315 | struct fd real; |
| 316 | const struct cred *old_cred; |
| 317 | int ret; |
| 318 | |
| 319 | ret = ovl_real_fdget(file, &real); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | |
| 323 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 324 | ret = vfs_fallocate(real.file, mode, offset, len); |
| 325 | revert_creds(old_cred); |
| 326 | |
| 327 | /* Update size */ |
| 328 | ovl_copyattr(ovl_inode_real(inode), inode); |
| 329 | |
| 330 | fdput(real); |
| 331 | |
| 332 | return ret; |
| 333 | } |
| 334 | |
Amir Goldstein | b833a36 | 2018-08-28 10:58:41 +0300 | [diff] [blame] | 335 | static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice) |
| 336 | { |
| 337 | struct fd real; |
| 338 | const struct cred *old_cred; |
| 339 | int ret; |
| 340 | |
| 341 | ret = ovl_real_fdget(file, &real); |
| 342 | if (ret) |
| 343 | return ret; |
| 344 | |
| 345 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 346 | ret = vfs_fadvise(real.file, offset, len, advice); |
| 347 | revert_creds(old_cred); |
| 348 | |
| 349 | fdput(real); |
| 350 | |
| 351 | return ret; |
| 352 | } |
| 353 | |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 354 | static long ovl_real_ioctl(struct file *file, unsigned int cmd, |
| 355 | unsigned long arg) |
| 356 | { |
| 357 | struct fd real; |
| 358 | const struct cred *old_cred; |
| 359 | long ret; |
| 360 | |
| 361 | ret = ovl_real_fdget(file, &real); |
| 362 | if (ret) |
| 363 | return ret; |
| 364 | |
| 365 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
| 366 | ret = vfs_ioctl(real.file, cmd, arg); |
| 367 | revert_creds(old_cred); |
| 368 | |
| 369 | fdput(real); |
| 370 | |
| 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
| 375 | { |
| 376 | long ret; |
| 377 | struct inode *inode = file_inode(file); |
| 378 | |
| 379 | switch (cmd) { |
| 380 | case FS_IOC_GETFLAGS: |
| 381 | ret = ovl_real_ioctl(file, cmd, arg); |
| 382 | break; |
| 383 | |
| 384 | case FS_IOC_SETFLAGS: |
| 385 | if (!inode_owner_or_capable(inode)) |
| 386 | return -EACCES; |
| 387 | |
| 388 | ret = mnt_want_write_file(file); |
| 389 | if (ret) |
| 390 | return ret; |
| 391 | |
Amir Goldstein | 3428030 | 2019-01-22 07:01:39 +0200 | [diff] [blame^] | 392 | ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY); |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 393 | if (!ret) { |
| 394 | ret = ovl_real_ioctl(file, cmd, arg); |
| 395 | |
| 396 | inode_lock(inode); |
| 397 | ovl_copyflags(ovl_inode_real(inode), inode); |
| 398 | inode_unlock(inode); |
| 399 | } |
| 400 | |
| 401 | mnt_drop_write_file(file); |
| 402 | break; |
| 403 | |
| 404 | default: |
| 405 | ret = -ENOTTY; |
| 406 | } |
| 407 | |
| 408 | return ret; |
| 409 | } |
| 410 | |
| 411 | static long ovl_compat_ioctl(struct file *file, unsigned int cmd, |
| 412 | unsigned long arg) |
| 413 | { |
| 414 | switch (cmd) { |
| 415 | case FS_IOC32_GETFLAGS: |
| 416 | cmd = FS_IOC_GETFLAGS; |
| 417 | break; |
| 418 | |
| 419 | case FS_IOC32_SETFLAGS: |
| 420 | cmd = FS_IOC_SETFLAGS; |
| 421 | break; |
| 422 | |
| 423 | default: |
| 424 | return -ENOIOCTLCMD; |
| 425 | } |
| 426 | |
| 427 | return ovl_ioctl(file, cmd, arg); |
| 428 | } |
| 429 | |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 430 | enum ovl_copyop { |
| 431 | OVL_COPY, |
| 432 | OVL_CLONE, |
| 433 | OVL_DEDUPE, |
| 434 | }; |
| 435 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 436 | 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] | 437 | struct file *file_out, loff_t pos_out, |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 438 | loff_t len, unsigned int flags, enum ovl_copyop op) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 439 | { |
| 440 | struct inode *inode_out = file_inode(file_out); |
| 441 | struct fd real_in, real_out; |
| 442 | const struct cred *old_cred; |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 443 | loff_t ret; |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 444 | |
| 445 | ret = ovl_real_fdget(file_out, &real_out); |
| 446 | if (ret) |
| 447 | return ret; |
| 448 | |
| 449 | ret = ovl_real_fdget(file_in, &real_in); |
| 450 | if (ret) { |
| 451 | fdput(real_out); |
| 452 | return ret; |
| 453 | } |
| 454 | |
| 455 | old_cred = ovl_override_creds(file_inode(file_out)->i_sb); |
| 456 | switch (op) { |
| 457 | case OVL_COPY: |
| 458 | ret = vfs_copy_file_range(real_in.file, pos_in, |
| 459 | real_out.file, pos_out, len, flags); |
| 460 | break; |
| 461 | |
| 462 | case OVL_CLONE: |
Amir Goldstein | a725356 | 2018-09-18 16:34:34 +0300 | [diff] [blame] | 463 | ret = vfs_clone_file_range(real_in.file, pos_in, |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 464 | real_out.file, pos_out, len, flags); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 465 | break; |
| 466 | |
| 467 | case OVL_DEDUPE: |
| 468 | ret = vfs_dedupe_file_range_one(real_in.file, pos_in, |
Darrick J. Wong | df36583 | 2018-10-30 10:42:03 +1100 | [diff] [blame] | 469 | real_out.file, pos_out, len, |
| 470 | flags); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 471 | break; |
| 472 | } |
| 473 | revert_creds(old_cred); |
| 474 | |
| 475 | /* Update size */ |
| 476 | ovl_copyattr(ovl_inode_real(inode_out), inode_out); |
| 477 | |
| 478 | fdput(real_in); |
| 479 | fdput(real_out); |
| 480 | |
| 481 | return ret; |
| 482 | } |
| 483 | |
| 484 | static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in, |
| 485 | struct file *file_out, loff_t pos_out, |
| 486 | size_t len, unsigned int flags) |
| 487 | { |
| 488 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags, |
| 489 | OVL_COPY); |
| 490 | } |
| 491 | |
Darrick J. Wong | 42ec3d4 | 2018-10-30 10:41:49 +1100 | [diff] [blame] | 492 | static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in, |
| 493 | struct file *file_out, loff_t pos_out, |
| 494 | loff_t len, unsigned int remap_flags) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 495 | { |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 496 | enum ovl_copyop op; |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 497 | |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 498 | if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY)) |
| 499 | return -EINVAL; |
| 500 | |
| 501 | if (remap_flags & REMAP_FILE_DEDUP) |
| 502 | op = OVL_DEDUPE; |
| 503 | else |
| 504 | op = OVL_CLONE; |
| 505 | |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 506 | /* |
| 507 | * Don't copy up because of a dedupe request, this wouldn't make sense |
| 508 | * most of the time (data would be duplicated instead of deduplicated). |
| 509 | */ |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 510 | if (op == OVL_DEDUPE && |
| 511 | (!ovl_inode_upper(file_inode(file_in)) || |
| 512 | !ovl_inode_upper(file_inode(file_out)))) |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 513 | return -EPERM; |
| 514 | |
Darrick J. Wong | 452ce65 | 2018-10-30 10:41:56 +1100 | [diff] [blame] | 515 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, |
| 516 | remap_flags, op); |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 517 | } |
| 518 | |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 519 | const struct file_operations ovl_file_operations = { |
| 520 | .open = ovl_open, |
| 521 | .release = ovl_release, |
| 522 | .llseek = ovl_llseek, |
Miklos Szeredi | 16914e6 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 523 | .read_iter = ovl_read_iter, |
Miklos Szeredi | 2a92e07 | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 524 | .write_iter = ovl_write_iter, |
Miklos Szeredi | de30dfd | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 525 | .fsync = ovl_fsync, |
Miklos Szeredi | 2f50283 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 526 | .mmap = ovl_mmap, |
Miklos Szeredi | aab8848 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 527 | .fallocate = ovl_fallocate, |
Amir Goldstein | b833a36 | 2018-08-28 10:58:41 +0300 | [diff] [blame] | 528 | .fadvise = ovl_fadvise, |
Miklos Szeredi | dab5ca8 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 529 | .unlocked_ioctl = ovl_ioctl, |
| 530 | .compat_ioctl = ovl_compat_ioctl, |
Miklos Szeredi | 8ede205 | 2018-07-18 15:44:42 +0200 | [diff] [blame] | 531 | |
| 532 | .copy_file_range = ovl_copy_file_range, |
Darrick J. Wong | 2e5dfc9 | 2018-10-30 10:41:21 +1100 | [diff] [blame] | 533 | .remap_file_range = ovl_remap_file_range, |
Miklos Szeredi | d1d04ef | 2018-07-18 15:44:41 +0200 | [diff] [blame] | 534 | }; |