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