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