]> git.ipfire.org Git - thirdparty/kernel/linux.git/blame - fs/overlayfs/file.c
ovl: fix file reference leak when submitting aio
[thirdparty/kernel/linux.git] / fs / overlayfs / file.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
d1d04ef8
MS
2/*
3 * Copyright (C) 2017 Red Hat, Inc.
d1d04ef8
MS
4 */
5
6#include <linux/cred.h>
7#include <linux/file.h>
dab5ca8f 8#include <linux/mount.h>
d1d04ef8 9#include <linux/xattr.h>
16914e6f 10#include <linux/uio.h>
98487de3 11#include <linux/uaccess.h>
1a980b8c 12#include <linux/splice.h>
292f902a 13#include <linux/security.h>
1a980b8c
MZ
14#include <linux/mm.h>
15#include <linux/fs.h>
d1d04ef8
MS
16#include "overlayfs.h"
17
2406a307
JX
18struct ovl_aio_req {
19 struct kiocb iocb;
9a254403 20 refcount_t ref;
2406a307 21 struct kiocb *orig_iocb;
2406a307
JX
22};
23
24static struct kmem_cache *ovl_aio_request_cachep;
25
8c444d2a
VG
26static 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
bc2473c9
AG
36/* No atime modification on underlying */
37#define OVL_OPEN_FLAGS (O_NOATIME)
81a33c1e 38
8c444d2a 39static struct file *ovl_open_realfile(const struct file *file,
2d343087 40 const struct path *realpath)
d1d04ef8 41{
1248ea4b 42 struct inode *realinode = d_inode(realpath->dentry);
d1d04ef8 43 struct inode *inode = file_inode(file);
4609e1f1 44 struct mnt_idmap *real_idmap;
d1d04ef8
MS
45 struct file *realfile;
46 const struct cred *old_cred;
81a33c1e 47 int flags = file->f_flags | OVL_OPEN_FLAGS;
05acefb4
MS
48 int acc_mode = ACC_MODE(flags);
49 int err;
50
51 if (flags & O_APPEND)
52 acc_mode |= MAY_APPEND;
d1d04ef8
MS
53
54 old_cred = ovl_override_creds(inode->i_sb);
4609e1f1 55 real_idmap = mnt_idmap(realpath->mnt);
4609e1f1 56 err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
05acefb4
MS
57 if (err) {
58 realfile = ERR_PTR(err);
05acefb4 59 } else {
01beba79 60 if (!inode_owner_or_capable(real_idmap, realinode))
b6650dab
MS
61 flags &= ~O_NOATIME;
62
62d53c4a
AG
63 realfile = backing_file_open(&file->f_path, flags, realpath,
64 current_cred());
05acefb4 65 }
d1d04ef8
MS
66 revert_creds(old_cred);
67
68 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
8c444d2a 69 file, file, ovl_whatisit(inode, realinode), file->f_flags,
d1d04ef8
MS
70 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
71
72 return realfile;
73}
74
2ef66b8a
MS
75#define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
76
77static int ovl_change_flags(struct file *file, unsigned int flags)
78{
79 struct inode *inode = file_inode(file);
80 int err;
81
2ef66b8a
MS
82 flags &= OVL_SETFL_MASK;
83
84 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
85 return -EPERM;
86
a2ad63da
N
87 if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
88 return -EINVAL;
2ef66b8a
MS
89
90 if (file->f_op->check_flags) {
91 err = file->f_op->check_flags(flags);
92 if (err)
93 return err;
94 }
95
96 spin_lock(&file->f_lock);
97 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
456b59e7 98 file->f_iocb_flags = iocb_flags(file);
2ef66b8a
MS
99 spin_unlock(&file->f_lock);
100
101 return 0;
102}
103
8c444d2a
VG
104static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
105 bool allow_meta)
2ef66b8a 106{
1248ea4b
AG
107 struct dentry *dentry = file_dentry(file);
108 struct path realpath;
42dd69ae 109 int err;
2ef66b8a
MS
110
111 real->flags = 0;
112 real->file = file->private_data;
113
42dd69ae 114 if (allow_meta) {
1248ea4b 115 ovl_path_real(dentry, &realpath);
42dd69ae 116 } else {
184996e9
AL
117 /* lazy lookup and verify of lowerdata */
118 err = ovl_verify_lowerdata(dentry);
42dd69ae
AG
119 if (err)
120 return err;
121
1248ea4b 122 ovl_path_realdata(dentry, &realpath);
42dd69ae 123 }
41665644
AG
124 if (!realpath.dentry)
125 return -EIO;
8c444d2a 126
2ef66b8a 127 /* Has it been copied up since we'd opened it? */
1248ea4b 128 if (unlikely(file_inode(real->file) != d_inode(realpath.dentry))) {
2ef66b8a 129 real->flags = FDPUT_FPUT;
1248ea4b 130 real->file = ovl_open_realfile(file, &realpath);
2ef66b8a
MS
131
132 return PTR_ERR_OR_ZERO(real->file);
133 }
134
135 /* Did the flags change since open? */
81a33c1e 136 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
2ef66b8a
MS
137 return ovl_change_flags(real->file, file->f_flags);
138
139 return 0;
140}
141
8c444d2a
VG
142static int ovl_real_fdget(const struct file *file, struct fd *real)
143{
61536bed
AG
144 if (d_is_dir(file_dentry(file))) {
145 real->flags = 0;
146 real->file = ovl_dir_real_file(file, false);
147
148 return PTR_ERR_OR_ZERO(real->file);
149 }
150
8c444d2a
VG
151 return ovl_real_fdget_meta(file, real, false);
152}
153
d1d04ef8
MS
154static int ovl_open(struct inode *inode, struct file *file)
155{
1248ea4b 156 struct dentry *dentry = file_dentry(file);
d1d04ef8 157 struct file *realfile;
1248ea4b 158 struct path realpath;
d1d04ef8
MS
159 int err;
160
184996e9
AL
161 /* lazy lookup and verify lowerdata */
162 err = ovl_verify_lowerdata(dentry);
42dd69ae
AG
163 if (err)
164 return err;
165
1248ea4b 166 err = ovl_maybe_copy_up(dentry, file->f_flags);
d1d04ef8
MS
167 if (err)
168 return err;
169
170 /* No longer need these flags, so don't pass them on to underlying fs */
171 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
172
1248ea4b 173 ovl_path_realdata(dentry, &realpath);
41665644
AG
174 if (!realpath.dentry)
175 return -EIO;
176
1248ea4b 177 realfile = ovl_open_realfile(file, &realpath);
d1d04ef8
MS
178 if (IS_ERR(realfile))
179 return PTR_ERR(realfile);
180
181 file->private_data = realfile;
182
183 return 0;
184}
185
186static int ovl_release(struct inode *inode, struct file *file)
187{
188 fput(file->private_data);
189
190 return 0;
191}
192
193static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
194{
9e46b840
AG
195 struct inode *inode = file_inode(file);
196 struct fd real;
197 const struct cred *old_cred;
a4ac9d45 198 loff_t ret;
9e46b840
AG
199
200 /*
201 * The two special cases below do not need to involve real fs,
202 * so we can optimizing concurrent callers.
203 */
204 if (offset == 0) {
205 if (whence == SEEK_CUR)
206 return file->f_pos;
207
208 if (whence == SEEK_SET)
209 return vfs_setpos(file, 0, 0);
210 }
211
212 ret = ovl_real_fdget(file, &real);
213 if (ret)
214 return ret;
215
216 /*
217 * Overlay file f_pos is the master copy that is preserved
218 * through copy up and modified on read/write, but only real
219 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
220 * limitations that are more strict than ->s_maxbytes for specific
221 * files, so we use the real file to perform seeks.
222 */
b1f9d385 223 ovl_inode_lock(inode);
9e46b840
AG
224 real.file->f_pos = file->f_pos;
225
226 old_cred = ovl_override_creds(inode->i_sb);
227 ret = vfs_llseek(real.file, offset, whence);
228 revert_creds(old_cred);
229
230 file->f_pos = real.file->f_pos;
b1f9d385 231 ovl_inode_unlock(inode);
9e46b840
AG
232
233 fdput(real);
d1d04ef8 234
9e46b840 235 return ret;
d1d04ef8
MS
236}
237
16914e6f
MS
238static void ovl_file_accessed(struct file *file)
239{
240 struct inode *inode, *upperinode;
9aa71115 241 struct timespec64 ctime, uctime;
16914e6f
MS
242
243 if (file->f_flags & O_NOATIME)
244 return;
245
246 inode = file_inode(file);
247 upperinode = ovl_inode_upper(inode);
248
249 if (!upperinode)
250 return;
251
9aa71115
JL
252 ctime = inode_get_ctime(inode);
253 uctime = inode_get_ctime(upperinode);
16914e6f 254 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
9aa71115 255 !timespec64_equal(&ctime, &uctime))) {
16914e6f 256 inode->i_mtime = upperinode->i_mtime;
9aa71115 257 inode_set_ctime_to_ts(inode, uctime);
16914e6f
MS
258 }
259
260 touch_atime(&file->f_path);
261}
262
b778e1ee 263static rwf_t ovl_iocb_to_rwf(int ifl)
16914e6f 264{
16914e6f
MS
265 rwf_t flags = 0;
266
267 if (ifl & IOCB_NOWAIT)
268 flags |= RWF_NOWAIT;
269 if (ifl & IOCB_HIPRI)
270 flags |= RWF_HIPRI;
271 if (ifl & IOCB_DSYNC)
272 flags |= RWF_DSYNC;
273 if (ifl & IOCB_SYNC)
274 flags |= RWF_SYNC;
275
276 return flags;
277}
278
9a254403 279static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
280{
281 if (refcount_dec_and_test(&aio_req->ref)) {
724768a3 282 fput(aio_req->iocb.ki_filp);
9a254403 283 kmem_cache_free(ovl_aio_request_cachep, aio_req);
284 }
285}
286
2406a307
JX
287static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
288{
289 struct kiocb *iocb = &aio_req->iocb;
290 struct kiocb *orig_iocb = aio_req->orig_iocb;
291
292 if (iocb->ki_flags & IOCB_WRITE) {
293 struct inode *inode = file_inode(orig_iocb->ki_filp);
294
8f737126 295 kiocb_end_write(iocb);
2878dffc 296 ovl_copyattr(inode);
2406a307
JX
297 }
298
299 orig_iocb->ki_pos = iocb->ki_pos;
9a254403 300 ovl_aio_put(aio_req);
2406a307
JX
301}
302
6b19b766 303static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
2406a307
JX
304{
305 struct ovl_aio_req *aio_req = container_of(iocb,
306 struct ovl_aio_req, iocb);
307 struct kiocb *orig_iocb = aio_req->orig_iocb;
308
309 ovl_aio_cleanup_handler(aio_req);
6b19b766 310 orig_iocb->ki_complete(orig_iocb, res);
2406a307
JX
311}
312
16914e6f
MS
313static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
314{
315 struct file *file = iocb->ki_filp;
316 struct fd real;
317 const struct cred *old_cred;
318 ssize_t ret;
319
320 if (!iov_iter_count(iter))
321 return 0;
322
323 ret = ovl_real_fdget(file, &real);
324 if (ret)
325 return ret;
326
1dc1eed4
MS
327 ret = -EINVAL;
328 if (iocb->ki_flags & IOCB_DIRECT &&
a2ad63da 329 !(real.file->f_mode & FMODE_CAN_ODIRECT))
1dc1eed4
MS
330 goto out_fdput;
331
16914e6f 332 old_cred = ovl_override_creds(file_inode(file)->i_sb);
2406a307
JX
333 if (is_sync_kiocb(iocb)) {
334 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
b778e1ee 335 ovl_iocb_to_rwf(iocb->ki_flags));
2406a307
JX
336 } else {
337 struct ovl_aio_req *aio_req;
338
339 ret = -ENOMEM;
340 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
341 if (!aio_req)
342 goto out;
343
2406a307 344 aio_req->orig_iocb = iocb;
724768a3 345 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
2406a307 346 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
9a254403 347 refcount_set(&aio_req->ref, 2);
2406a307 348 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
9a254403 349 ovl_aio_put(aio_req);
2406a307
JX
350 if (ret != -EIOCBQUEUED)
351 ovl_aio_cleanup_handler(aio_req);
352 }
353out:
16914e6f 354 revert_creds(old_cred);
16914e6f 355 ovl_file_accessed(file);
1dc1eed4 356out_fdput:
16914e6f
MS
357 fdput(real);
358
359 return ret;
360}
361
2a92e07e
MS
362static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
363{
364 struct file *file = iocb->ki_filp;
365 struct inode *inode = file_inode(file);
366 struct fd real;
367 const struct cred *old_cred;
368 ssize_t ret;
c86243b0 369 int ifl = iocb->ki_flags;
2a92e07e
MS
370
371 if (!iov_iter_count(iter))
372 return 0;
373
374 inode_lock(inode);
375 /* Update mode */
2878dffc 376 ovl_copyattr(inode);
2a92e07e
MS
377 ret = file_remove_privs(file);
378 if (ret)
379 goto out_unlock;
380
381 ret = ovl_real_fdget(file, &real);
382 if (ret)
383 goto out_unlock;
384
1dc1eed4
MS
385 ret = -EINVAL;
386 if (iocb->ki_flags & IOCB_DIRECT &&
a2ad63da 387 !(real.file->f_mode & FMODE_CAN_ODIRECT))
1dc1eed4
MS
388 goto out_fdput;
389
c86243b0
VG
390 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
391 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
392
2d1b3bbc
JA
393 /*
394 * Overlayfs doesn't support deferred completions, don't copy
395 * this property in case it is set by the issuer.
396 */
397 ifl &= ~IOCB_DIO_CALLER_COMP;
398
2a92e07e 399 old_cred = ovl_override_creds(file_inode(file)->i_sb);
2406a307
JX
400 if (is_sync_kiocb(iocb)) {
401 file_start_write(real.file);
402 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
c86243b0 403 ovl_iocb_to_rwf(ifl));
2406a307
JX
404 file_end_write(real.file);
405 /* Update size */
2878dffc 406 ovl_copyattr(inode);
2406a307
JX
407 } else {
408 struct ovl_aio_req *aio_req;
409
410 ret = -ENOMEM;
411 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
412 if (!aio_req)
413 goto out;
414
2406a307 415 aio_req->orig_iocb = iocb;
724768a3 416 kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
c86243b0 417 aio_req->iocb.ki_flags = ifl;
2406a307 418 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
9a254403 419 refcount_set(&aio_req->ref, 2);
8f737126 420 kiocb_start_write(&aio_req->iocb);
2406a307 421 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
9a254403 422 ovl_aio_put(aio_req);
2406a307
JX
423 if (ret != -EIOCBQUEUED)
424 ovl_aio_cleanup_handler(aio_req);
425 }
426out:
2a92e07e 427 revert_creds(old_cred);
1dc1eed4 428out_fdput:
2a92e07e
MS
429 fdput(real);
430
431out_unlock:
432 inode_unlock(inode);
433
434 return ret;
435}
436
d4120d87
DH
437static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
438 struct pipe_inode_info *pipe, size_t len,
439 unsigned int flags)
440{
441 const struct cred *old_cred;
442 struct fd real;
443 ssize_t ret;
444
445 ret = ovl_real_fdget(in, &real);
446 if (ret)
447 return ret;
448
449 old_cred = ovl_override_creds(file_inode(in)->i_sb);
450 ret = vfs_splice_read(real.file, ppos, pipe, len, flags);
451 revert_creds(old_cred);
452 ovl_file_accessed(in);
453
454 fdput(real);
455 return ret;
456}
457
9b91b6b0
MS
458/*
459 * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
460 * due to lock order inversion between pipe->mutex in iter_file_splice_write()
461 * and file_start_write(real.file) in ovl_write_iter().
462 *
463 * So do everything ovl_write_iter() does and call iter_file_splice_write() on
464 * the real file.
465 */
466static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
467 loff_t *ppos, size_t len, unsigned int flags)
468{
469 struct fd real;
470 const struct cred *old_cred;
471 struct inode *inode = file_inode(out);
9b91b6b0
MS
472 ssize_t ret;
473
474 inode_lock(inode);
475 /* Update mode */
2878dffc 476 ovl_copyattr(inode);
9b91b6b0
MS
477 ret = file_remove_privs(out);
478 if (ret)
479 goto out_unlock;
480
481 ret = ovl_real_fdget(out, &real);
482 if (ret)
483 goto out_unlock;
484
485 old_cred = ovl_override_creds(inode->i_sb);
486 file_start_write(real.file);
487
488 ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
489
490 file_end_write(real.file);
491 /* Update size */
2878dffc 492 ovl_copyattr(inode);
9b91b6b0
MS
493 revert_creds(old_cred);
494 fdput(real);
495
496out_unlock:
497 inode_unlock(inode);
498
499 return ret;
500}
501
de30dfd6
MS
502static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
503{
504 struct fd real;
505 const struct cred *old_cred;
506 int ret;
507
335d3fc5
SD
508 ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
509 if (ret <= 0)
510 return ret;
c86243b0 511
8c444d2a 512 ret = ovl_real_fdget_meta(file, &real, !datasync);
de30dfd6
MS
513 if (ret)
514 return ret;
515
516 /* Don't sync lower file for fear of receiving EROFS error */
517 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
518 old_cred = ovl_override_creds(file_inode(file)->i_sb);
519 ret = vfs_fsync_range(real.file, start, end, datasync);
520 revert_creds(old_cred);
521 }
522
523 fdput(real);
524
525 return ret;
526}
527
2f502839
MS
528static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
529{
530 struct file *realfile = file->private_data;
531 const struct cred *old_cred;
532 int ret;
533
534 if (!realfile->f_op->mmap)
535 return -ENODEV;
536
537 if (WARN_ON(file != vma->vm_file))
538 return -EIO;
539
2896900e 540 vma_set_file(vma, realfile);
2f502839
MS
541
542 old_cred = ovl_override_creds(file_inode(file)->i_sb);
543 ret = call_mmap(vma->vm_file, vma);
544 revert_creds(old_cred);
2f502839
MS
545 ovl_file_accessed(file);
546
547 return ret;
548}
549
aab8848c
MS
550static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
551{
552 struct inode *inode = file_inode(file);
553 struct fd real;
554 const struct cred *old_cred;
555 int ret;
556
23a8ce16
AG
557 inode_lock(inode);
558 /* Update mode */
559 ovl_copyattr(inode);
560 ret = file_remove_privs(file);
561 if (ret)
562 goto out_unlock;
563
aab8848c
MS
564 ret = ovl_real_fdget(file, &real);
565 if (ret)
23a8ce16 566 goto out_unlock;
aab8848c
MS
567
568 old_cred = ovl_override_creds(file_inode(file)->i_sb);
569 ret = vfs_fallocate(real.file, mode, offset, len);
570 revert_creds(old_cred);
571
572 /* Update size */
2878dffc 573 ovl_copyattr(inode);
aab8848c
MS
574
575 fdput(real);
576
23a8ce16
AG
577out_unlock:
578 inode_unlock(inode);
579
aab8848c
MS
580 return ret;
581}
582
b833a366
AG
583static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
584{
585 struct fd real;
586 const struct cred *old_cred;
587 int ret;
588
589 ret = ovl_real_fdget(file, &real);
590 if (ret)
591 return ret;
592
593 old_cred = ovl_override_creds(file_inode(file)->i_sb);
594 ret = vfs_fadvise(real.file, offset, len, advice);
595 revert_creds(old_cred);
596
597 fdput(real);
598
599 return ret;
600}
601
8ede2055
MS
602enum ovl_copyop {
603 OVL_COPY,
604 OVL_CLONE,
605 OVL_DEDUPE,
606};
607
42ec3d4c 608static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
8ede2055 609 struct file *file_out, loff_t pos_out,
42ec3d4c 610 loff_t len, unsigned int flags, enum ovl_copyop op)
8ede2055
MS
611{
612 struct inode *inode_out = file_inode(file_out);
613 struct fd real_in, real_out;
614 const struct cred *old_cred;
42ec3d4c 615 loff_t ret;
8ede2055 616
b306e90f
AG
617 inode_lock(inode_out);
618 if (op != OVL_DEDUPE) {
619 /* Update mode */
620 ovl_copyattr(inode_out);
621 ret = file_remove_privs(file_out);
622 if (ret)
623 goto out_unlock;
624 }
625
8ede2055
MS
626 ret = ovl_real_fdget(file_out, &real_out);
627 if (ret)
b306e90f 628 goto out_unlock;
8ede2055
MS
629
630 ret = ovl_real_fdget(file_in, &real_in);
631 if (ret) {
632 fdput(real_out);
b306e90f 633 goto out_unlock;
8ede2055
MS
634 }
635
636 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
637 switch (op) {
638 case OVL_COPY:
639 ret = vfs_copy_file_range(real_in.file, pos_in,
640 real_out.file, pos_out, len, flags);
641 break;
642
643 case OVL_CLONE:
a725356b 644 ret = vfs_clone_file_range(real_in.file, pos_in,
452ce659 645 real_out.file, pos_out, len, flags);
8ede2055
MS
646 break;
647
648 case OVL_DEDUPE:
649 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
df365836
DW
650 real_out.file, pos_out, len,
651 flags);
8ede2055
MS
652 break;
653 }
654 revert_creds(old_cred);
655
656 /* Update size */
2878dffc 657 ovl_copyattr(inode_out);
8ede2055
MS
658
659 fdput(real_in);
660 fdput(real_out);
661
b306e90f
AG
662out_unlock:
663 inode_unlock(inode_out);
664
8ede2055
MS
665 return ret;
666}
667
668static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
669 struct file *file_out, loff_t pos_out,
670 size_t len, unsigned int flags)
671{
672 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
673 OVL_COPY);
674}
675
42ec3d4c
DW
676static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
677 struct file *file_out, loff_t pos_out,
678 loff_t len, unsigned int remap_flags)
8ede2055 679{
2e5dfc99
DW
680 enum ovl_copyop op;
681
682 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
683 return -EINVAL;
684
685 if (remap_flags & REMAP_FILE_DEDUP)
686 op = OVL_DEDUPE;
687 else
688 op = OVL_CLONE;
8ede2055 689
8ede2055
MS
690 /*
691 * Don't copy up because of a dedupe request, this wouldn't make sense
692 * most of the time (data would be duplicated instead of deduplicated).
693 */
2e5dfc99
DW
694 if (op == OVL_DEDUPE &&
695 (!ovl_inode_upper(file_inode(file_in)) ||
696 !ovl_inode_upper(file_inode(file_out))))
8ede2055
MS
697 return -EPERM;
698
452ce659
DW
699 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
700 remap_flags, op);
8ede2055
MS
701}
702
1f0cb8bc
SD
703static int ovl_flush(struct file *file, fl_owner_t id)
704{
705 struct fd real;
706 const struct cred *old_cred;
707 int err;
708
709 err = ovl_real_fdget(file, &real);
710 if (err)
711 return err;
712
713 if (real.file->f_op->flush) {
714 old_cred = ovl_override_creds(file_inode(file)->i_sb);
715 err = real.file->f_op->flush(real.file, id);
716 revert_creds(old_cred);
717 }
718 fdput(real);
719
720 return err;
721}
722
d1d04ef8
MS
723const struct file_operations ovl_file_operations = {
724 .open = ovl_open,
725 .release = ovl_release,
726 .llseek = ovl_llseek,
16914e6f 727 .read_iter = ovl_read_iter,
2a92e07e 728 .write_iter = ovl_write_iter,
de30dfd6 729 .fsync = ovl_fsync,
2f502839 730 .mmap = ovl_mmap,
aab8848c 731 .fallocate = ovl_fallocate,
b833a366 732 .fadvise = ovl_fadvise,
1f0cb8bc 733 .flush = ovl_flush,
d4120d87 734 .splice_read = ovl_splice_read,
9b91b6b0 735 .splice_write = ovl_splice_write,
8ede2055
MS
736
737 .copy_file_range = ovl_copy_file_range,
2e5dfc99 738 .remap_file_range = ovl_remap_file_range,
d1d04ef8 739};
2406a307
JX
740
741int __init ovl_aio_request_cache_init(void)
742{
743 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
744 sizeof(struct ovl_aio_req),
745 0, SLAB_HWCACHE_ALIGN, NULL);
746 if (!ovl_aio_request_cachep)
747 return -ENOMEM;
748
749 return 0;
750}
751
752void ovl_aio_request_cache_destroy(void)
753{
754 kmem_cache_destroy(ovl_aio_request_cachep);
755}