ovl: support [S|G]ETFLAGS and FS[S|G]ETXATTR ioctls for directories
[linux-2.6-microblaze.git] / fs / overlayfs / file.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Red Hat, Inc.
4  */
5
6 #include <linux/cred.h>
7 #include <linux/file.h>
8 #include <linux/mount.h>
9 #include <linux/xattr.h>
10 #include <linux/uio.h>
11 #include <linux/uaccess.h>
12 #include <linux/splice.h>
13 #include <linux/security.h>
14 #include <linux/mm.h>
15 #include <linux/fs.h>
16 #include "overlayfs.h"
17
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
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
36 /* No atime modificaton nor notify on underlying */
37 #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
38
39 static struct file *ovl_open_realfile(const struct file *file,
40                                       struct inode *realinode)
41 {
42         struct inode *inode = file_inode(file);
43         struct file *realfile;
44         const struct cred *old_cred;
45         int flags = file->f_flags | OVL_OPEN_FLAGS;
46         int acc_mode = ACC_MODE(flags);
47         int err;
48
49         if (flags & O_APPEND)
50                 acc_mode |= MAY_APPEND;
51
52         old_cred = ovl_override_creds(inode->i_sb);
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         }
62         revert_creds(old_cred);
63
64         pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
65                  file, file, ovl_whatisit(inode, realinode), file->f_flags,
66                  realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
67
68         return realfile;
69 }
70
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
78         flags |= OVL_OPEN_FLAGS;
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
108 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
109                                bool allow_meta)
110 {
111         struct inode *inode = file_inode(file);
112         struct inode *realinode;
113
114         real->flags = 0;
115         real->file = file->private_data;
116
117         if (allow_meta)
118                 realinode = ovl_inode_real(inode);
119         else
120                 realinode = ovl_inode_realdata(inode);
121
122         /* Has it been copied up since we'd opened it? */
123         if (unlikely(file_inode(real->file) != realinode)) {
124                 real->flags = FDPUT_FPUT;
125                 real->file = ovl_open_realfile(file, realinode);
126
127                 return PTR_ERR_OR_ZERO(real->file);
128         }
129
130         /* Did the flags change since open? */
131         if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
132                 return ovl_change_flags(real->file, file->f_flags);
133
134         return 0;
135 }
136
137 static int ovl_real_fdget(const struct file *file, struct fd *real)
138 {
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
146         return ovl_real_fdget_meta(file, real, false);
147 }
148
149 static int ovl_open(struct inode *inode, struct file *file)
150 {
151         struct file *realfile;
152         int err;
153
154         err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
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
161         realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
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 {
179         struct inode *inode = file_inode(file);
180         struct fd real;
181         const struct cred *old_cred;
182         loff_t ret;
183
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          */
207         ovl_inode_lock(inode);
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;
215         ovl_inode_unlock(inode);
216
217         fdput(real);
218
219         return ret;
220 }
221
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
244 static rwf_t ovl_iocb_to_rwf(int ifl)
245 {
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
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
268                 /* Actually acquired in ovl_write_iter() */
269                 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
270                                       SB_FREEZE_WRITE);
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
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);
305         if (is_sync_kiocb(iocb)) {
306                 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
307                                     ovl_iocb_to_rwf(iocb->ki_flags));
308         } else {
309                 struct ovl_aio_req *aio_req;
310
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);
327         ovl_file_accessed(file);
328
329         fdput(real);
330
331         return ret;
332 }
333
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;
341         int ifl = iocb->ki_flags;
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
357         if (!ovl_should_sync(OVL_FS(inode->i_sb)))
358                 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
359
360         old_cred = ovl_override_creds(file_inode(file)->i_sb);
361         if (is_sync_kiocb(iocb)) {
362                 file_start_write(real.file);
363                 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
364                                      ovl_iocb_to_rwf(ifl));
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);
377                 /* Pacify lockdep, same trick as done in aio_write() */
378                 __sb_writers_release(file_inode(real.file)->i_sb,
379                                      SB_FREEZE_WRITE);
380                 aio_req->fd = real;
381                 real.flags = 0;
382                 aio_req->orig_iocb = iocb;
383                 kiocb_clone(&aio_req->iocb, iocb, real.file);
384                 aio_req->iocb.ki_flags = ifl;
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:
391         revert_creds(old_cred);
392         fdput(real);
393
394 out_unlock:
395         inode_unlock(inode);
396
397         return ret;
398 }
399
400 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
401                          struct pipe_inode_info *pipe, size_t len,
402                          unsigned int flags)
403 {
404         ssize_t ret;
405         struct fd real;
406         const struct cred *old_cred;
407
408         ret = ovl_real_fdget(in, &real);
409         if (ret)
410                 return ret;
411
412         old_cred = ovl_override_creds(file_inode(in)->i_sb);
413         ret = generic_file_splice_read(real.file, ppos, pipe, len, flags);
414         revert_creds(old_cred);
415
416         ovl_file_accessed(in);
417         fdput(real);
418         return ret;
419 }
420
421 static ssize_t
422 ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
423                           loff_t *ppos, size_t len, unsigned int flags)
424 {
425         struct fd real;
426         const struct cred *old_cred;
427         ssize_t ret;
428
429         ret = ovl_real_fdget(out, &real);
430         if (ret)
431                 return ret;
432
433         old_cred = ovl_override_creds(file_inode(out)->i_sb);
434         ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
435         revert_creds(old_cred);
436
437         ovl_file_accessed(out);
438         fdput(real);
439         return ret;
440 }
441
442 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
443 {
444         struct fd real;
445         const struct cred *old_cred;
446         int ret;
447
448         if (!ovl_should_sync(OVL_FS(file_inode(file)->i_sb)))
449                 return 0;
450
451         ret = ovl_real_fdget_meta(file, &real, !datasync);
452         if (ret)
453                 return ret;
454
455         /* Don't sync lower file for fear of receiving EROFS error */
456         if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
457                 old_cred = ovl_override_creds(file_inode(file)->i_sb);
458                 ret = vfs_fsync_range(real.file, start, end, datasync);
459                 revert_creds(old_cred);
460         }
461
462         fdput(real);
463
464         return ret;
465 }
466
467 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
468 {
469         struct file *realfile = file->private_data;
470         const struct cred *old_cred;
471         int ret;
472
473         if (!realfile->f_op->mmap)
474                 return -ENODEV;
475
476         if (WARN_ON(file != vma->vm_file))
477                 return -EIO;
478
479         vma->vm_file = get_file(realfile);
480
481         old_cred = ovl_override_creds(file_inode(file)->i_sb);
482         ret = call_mmap(vma->vm_file, vma);
483         revert_creds(old_cred);
484
485         if (ret) {
486                 /* Drop reference count from new vm_file value */
487                 fput(realfile);
488         } else {
489                 /* Drop reference count from previous vm_file value */
490                 fput(file);
491         }
492
493         ovl_file_accessed(file);
494
495         return ret;
496 }
497
498 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
499 {
500         struct inode *inode = file_inode(file);
501         struct fd real;
502         const struct cred *old_cred;
503         int ret;
504
505         ret = ovl_real_fdget(file, &real);
506         if (ret)
507                 return ret;
508
509         old_cred = ovl_override_creds(file_inode(file)->i_sb);
510         ret = vfs_fallocate(real.file, mode, offset, len);
511         revert_creds(old_cred);
512
513         /* Update size */
514         ovl_copyattr(ovl_inode_real(inode), inode);
515
516         fdput(real);
517
518         return ret;
519 }
520
521 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
522 {
523         struct fd real;
524         const struct cred *old_cred;
525         int ret;
526
527         ret = ovl_real_fdget(file, &real);
528         if (ret)
529                 return ret;
530
531         old_cred = ovl_override_creds(file_inode(file)->i_sb);
532         ret = vfs_fadvise(real.file, offset, len, advice);
533         revert_creds(old_cred);
534
535         fdput(real);
536
537         return ret;
538 }
539
540 static long ovl_real_ioctl(struct file *file, unsigned int cmd,
541                            unsigned long arg)
542 {
543         struct fd real;
544         const struct cred *old_cred;
545         long ret;
546
547         ret = ovl_real_fdget(file, &real);
548         if (ret)
549                 return ret;
550
551         old_cred = ovl_override_creds(file_inode(file)->i_sb);
552         ret = security_file_ioctl(real.file, cmd, arg);
553         if (!ret)
554                 ret = vfs_ioctl(real.file, cmd, arg);
555         revert_creds(old_cred);
556
557         fdput(real);
558
559         return ret;
560 }
561
562 static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
563                                 unsigned long arg, unsigned int iflags)
564 {
565         long ret;
566         struct inode *inode = file_inode(file);
567         unsigned int old_iflags;
568
569         if (!inode_owner_or_capable(inode))
570                 return -EACCES;
571
572         ret = mnt_want_write_file(file);
573         if (ret)
574                 return ret;
575
576         inode_lock(inode);
577
578         /* Check the capability before cred override */
579         ret = -EPERM;
580         old_iflags = READ_ONCE(inode->i_flags);
581         if (((iflags ^ old_iflags) & (S_APPEND | S_IMMUTABLE)) &&
582             !capable(CAP_LINUX_IMMUTABLE))
583                 goto unlock;
584
585         ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
586         if (ret)
587                 goto unlock;
588
589         ret = ovl_real_ioctl(file, cmd, arg);
590
591         ovl_copyflags(ovl_inode_real(inode), inode);
592 unlock:
593         inode_unlock(inode);
594
595         mnt_drop_write_file(file);
596
597         return ret;
598
599 }
600
601 static unsigned int ovl_fsflags_to_iflags(unsigned int flags)
602 {
603         unsigned int iflags = 0;
604
605         if (flags & FS_SYNC_FL)
606                 iflags |= S_SYNC;
607         if (flags & FS_APPEND_FL)
608                 iflags |= S_APPEND;
609         if (flags & FS_IMMUTABLE_FL)
610                 iflags |= S_IMMUTABLE;
611         if (flags & FS_NOATIME_FL)
612                 iflags |= S_NOATIME;
613
614         return iflags;
615 }
616
617 static long ovl_ioctl_set_fsflags(struct file *file, unsigned int cmd,
618                                   unsigned long arg)
619 {
620         unsigned int flags;
621
622         if (get_user(flags, (int __user *) arg))
623                 return -EFAULT;
624
625         return ovl_ioctl_set_flags(file, cmd, arg,
626                                    ovl_fsflags_to_iflags(flags));
627 }
628
629 static unsigned int ovl_fsxflags_to_iflags(unsigned int xflags)
630 {
631         unsigned int iflags = 0;
632
633         if (xflags & FS_XFLAG_SYNC)
634                 iflags |= S_SYNC;
635         if (xflags & FS_XFLAG_APPEND)
636                 iflags |= S_APPEND;
637         if (xflags & FS_XFLAG_IMMUTABLE)
638                 iflags |= S_IMMUTABLE;
639         if (xflags & FS_XFLAG_NOATIME)
640                 iflags |= S_NOATIME;
641
642         return iflags;
643 }
644
645 static long ovl_ioctl_set_fsxflags(struct file *file, unsigned int cmd,
646                                    unsigned long arg)
647 {
648         struct fsxattr fa;
649
650         memset(&fa, 0, sizeof(fa));
651         if (copy_from_user(&fa, (void __user *) arg, sizeof(fa)))
652                 return -EFAULT;
653
654         return ovl_ioctl_set_flags(file, cmd, arg,
655                                    ovl_fsxflags_to_iflags(fa.fsx_xflags));
656 }
657
658 long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
659 {
660         long ret;
661
662         switch (cmd) {
663         case FS_IOC_GETFLAGS:
664         case FS_IOC_FSGETXATTR:
665                 ret = ovl_real_ioctl(file, cmd, arg);
666                 break;
667
668         case FS_IOC_SETFLAGS:
669                 ret = ovl_ioctl_set_fsflags(file, cmd, arg);
670                 break;
671
672         case FS_IOC_FSSETXATTR:
673                 ret = ovl_ioctl_set_fsxflags(file, cmd, arg);
674                 break;
675
676         default:
677                 ret = -ENOTTY;
678         }
679
680         return ret;
681 }
682
683 #ifdef CONFIG_COMPAT
684 long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
685 {
686         switch (cmd) {
687         case FS_IOC32_GETFLAGS:
688                 cmd = FS_IOC_GETFLAGS;
689                 break;
690
691         case FS_IOC32_SETFLAGS:
692                 cmd = FS_IOC_SETFLAGS;
693                 break;
694
695         default:
696                 return -ENOIOCTLCMD;
697         }
698
699         return ovl_ioctl(file, cmd, arg);
700 }
701 #endif
702
703 enum ovl_copyop {
704         OVL_COPY,
705         OVL_CLONE,
706         OVL_DEDUPE,
707 };
708
709 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
710                             struct file *file_out, loff_t pos_out,
711                             loff_t len, unsigned int flags, enum ovl_copyop op)
712 {
713         struct inode *inode_out = file_inode(file_out);
714         struct fd real_in, real_out;
715         const struct cred *old_cred;
716         loff_t ret;
717
718         ret = ovl_real_fdget(file_out, &real_out);
719         if (ret)
720                 return ret;
721
722         ret = ovl_real_fdget(file_in, &real_in);
723         if (ret) {
724                 fdput(real_out);
725                 return ret;
726         }
727
728         old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
729         switch (op) {
730         case OVL_COPY:
731                 ret = vfs_copy_file_range(real_in.file, pos_in,
732                                           real_out.file, pos_out, len, flags);
733                 break;
734
735         case OVL_CLONE:
736                 ret = vfs_clone_file_range(real_in.file, pos_in,
737                                            real_out.file, pos_out, len, flags);
738                 break;
739
740         case OVL_DEDUPE:
741                 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
742                                                 real_out.file, pos_out, len,
743                                                 flags);
744                 break;
745         }
746         revert_creds(old_cred);
747
748         /* Update size */
749         ovl_copyattr(ovl_inode_real(inode_out), inode_out);
750
751         fdput(real_in);
752         fdput(real_out);
753
754         return ret;
755 }
756
757 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
758                                    struct file *file_out, loff_t pos_out,
759                                    size_t len, unsigned int flags)
760 {
761         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
762                             OVL_COPY);
763 }
764
765 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
766                                    struct file *file_out, loff_t pos_out,
767                                    loff_t len, unsigned int remap_flags)
768 {
769         enum ovl_copyop op;
770
771         if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
772                 return -EINVAL;
773
774         if (remap_flags & REMAP_FILE_DEDUP)
775                 op = OVL_DEDUPE;
776         else
777                 op = OVL_CLONE;
778
779         /*
780          * Don't copy up because of a dedupe request, this wouldn't make sense
781          * most of the time (data would be duplicated instead of deduplicated).
782          */
783         if (op == OVL_DEDUPE &&
784             (!ovl_inode_upper(file_inode(file_in)) ||
785              !ovl_inode_upper(file_inode(file_out))))
786                 return -EPERM;
787
788         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
789                             remap_flags, op);
790 }
791
792 const struct file_operations ovl_file_operations = {
793         .open           = ovl_open,
794         .release        = ovl_release,
795         .llseek         = ovl_llseek,
796         .read_iter      = ovl_read_iter,
797         .write_iter     = ovl_write_iter,
798         .fsync          = ovl_fsync,
799         .mmap           = ovl_mmap,
800         .fallocate      = ovl_fallocate,
801         .fadvise        = ovl_fadvise,
802         .unlocked_ioctl = ovl_ioctl,
803 #ifdef CONFIG_COMPAT
804         .compat_ioctl   = ovl_compat_ioctl,
805 #endif
806         .splice_read    = ovl_splice_read,
807         .splice_write   = ovl_splice_write,
808
809         .copy_file_range        = ovl_copy_file_range,
810         .remap_file_range       = ovl_remap_file_range,
811 };
812
813 int __init ovl_aio_request_cache_init(void)
814 {
815         ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
816                                                    sizeof(struct ovl_aio_req),
817                                                    0, SLAB_HWCACHE_ALIGN, NULL);
818         if (!ovl_aio_request_cachep)
819                 return -ENOMEM;
820
821         return 0;
822 }
823
824 void ovl_aio_request_cache_destroy(void)
825 {
826         kmem_cache_destroy(ovl_aio_request_cachep);
827 }