dm-crypt: use __bio_add_page to add single page to clone bio
[linux-block.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         refcount_t ref;
21         struct kiocb *orig_iocb;
22         struct fd fd;
23 };
24
25 static struct kmem_cache *ovl_aio_request_cachep;
26
27 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
28 {
29         if (realinode != ovl_inode_upper(inode))
30                 return 'l';
31         if (ovl_has_upperdata(inode))
32                 return 'u';
33         else
34                 return 'm';
35 }
36
37 /* No atime modification nor notify on underlying */
38 #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
39
40 static struct file *ovl_open_realfile(const struct file *file,
41                                       const struct path *realpath)
42 {
43         struct inode *realinode = d_inode(realpath->dentry);
44         struct inode *inode = file_inode(file);
45         struct mnt_idmap *real_idmap;
46         struct file *realfile;
47         const struct cred *old_cred;
48         int flags = file->f_flags | OVL_OPEN_FLAGS;
49         int acc_mode = ACC_MODE(flags);
50         int err;
51
52         if (flags & O_APPEND)
53                 acc_mode |= MAY_APPEND;
54
55         old_cred = ovl_override_creds(inode->i_sb);
56         real_idmap = mnt_idmap(realpath->mnt);
57         err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode);
58         if (err) {
59                 realfile = ERR_PTR(err);
60         } else {
61                 if (!inode_owner_or_capable(real_idmap, realinode))
62                         flags &= ~O_NOATIME;
63
64                 realfile = open_with_fake_path(&file->f_path, flags, realinode,
65                                                current_cred());
66         }
67         revert_creds(old_cred);
68
69         pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
70                  file, file, ovl_whatisit(inode, realinode), file->f_flags,
71                  realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
72
73         return realfile;
74 }
75
76 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
77
78 static int ovl_change_flags(struct file *file, unsigned int flags)
79 {
80         struct inode *inode = file_inode(file);
81         int err;
82
83         flags &= OVL_SETFL_MASK;
84
85         if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
86                 return -EPERM;
87
88         if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT))
89                 return -EINVAL;
90
91         if (file->f_op->check_flags) {
92                 err = file->f_op->check_flags(flags);
93                 if (err)
94                         return err;
95         }
96
97         spin_lock(&file->f_lock);
98         file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
99         file->f_iocb_flags = iocb_flags(file);
100         spin_unlock(&file->f_lock);
101
102         return 0;
103 }
104
105 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
106                                bool allow_meta)
107 {
108         struct dentry *dentry = file_dentry(file);
109         struct path realpath;
110
111         real->flags = 0;
112         real->file = file->private_data;
113
114         if (allow_meta)
115                 ovl_path_real(dentry, &realpath);
116         else
117                 ovl_path_realdata(dentry, &realpath);
118
119         /* Has it been copied up since we'd opened it? */
120         if (unlikely(file_inode(real->file) != d_inode(realpath.dentry))) {
121                 real->flags = FDPUT_FPUT;
122                 real->file = ovl_open_realfile(file, &realpath);
123
124                 return PTR_ERR_OR_ZERO(real->file);
125         }
126
127         /* Did the flags change since open? */
128         if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
129                 return ovl_change_flags(real->file, file->f_flags);
130
131         return 0;
132 }
133
134 static int ovl_real_fdget(const struct file *file, struct fd *real)
135 {
136         if (d_is_dir(file_dentry(file))) {
137                 real->flags = 0;
138                 real->file = ovl_dir_real_file(file, false);
139
140                 return PTR_ERR_OR_ZERO(real->file);
141         }
142
143         return ovl_real_fdget_meta(file, real, false);
144 }
145
146 static int ovl_open(struct inode *inode, struct file *file)
147 {
148         struct dentry *dentry = file_dentry(file);
149         struct file *realfile;
150         struct path realpath;
151         int err;
152
153         err = ovl_maybe_copy_up(dentry, file->f_flags);
154         if (err)
155                 return err;
156
157         /* No longer need these flags, so don't pass them on to underlying fs */
158         file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
159
160         ovl_path_realdata(dentry, &realpath);
161         realfile = ovl_open_realfile(file, &realpath);
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 inline void ovl_aio_put(struct ovl_aio_req *aio_req)
261 {
262         if (refcount_dec_and_test(&aio_req->ref)) {
263                 fdput(aio_req->fd);
264                 kmem_cache_free(ovl_aio_request_cachep, aio_req);
265         }
266 }
267
268 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
269 {
270         struct kiocb *iocb = &aio_req->iocb;
271         struct kiocb *orig_iocb = aio_req->orig_iocb;
272
273         if (iocb->ki_flags & IOCB_WRITE) {
274                 struct inode *inode = file_inode(orig_iocb->ki_filp);
275
276                 /* Actually acquired in ovl_write_iter() */
277                 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
278                                       SB_FREEZE_WRITE);
279                 file_end_write(iocb->ki_filp);
280                 ovl_copyattr(inode);
281         }
282
283         orig_iocb->ki_pos = iocb->ki_pos;
284         ovl_aio_put(aio_req);
285 }
286
287 static void ovl_aio_rw_complete(struct kiocb *iocb, long res)
288 {
289         struct ovl_aio_req *aio_req = container_of(iocb,
290                                                    struct ovl_aio_req, iocb);
291         struct kiocb *orig_iocb = aio_req->orig_iocb;
292
293         ovl_aio_cleanup_handler(aio_req);
294         orig_iocb->ki_complete(orig_iocb, res);
295 }
296
297 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
298 {
299         struct file *file = iocb->ki_filp;
300         struct fd real;
301         const struct cred *old_cred;
302         ssize_t ret;
303
304         if (!iov_iter_count(iter))
305                 return 0;
306
307         ret = ovl_real_fdget(file, &real);
308         if (ret)
309                 return ret;
310
311         ret = -EINVAL;
312         if (iocb->ki_flags & IOCB_DIRECT &&
313             !(real.file->f_mode & FMODE_CAN_ODIRECT))
314                 goto out_fdput;
315
316         old_cred = ovl_override_creds(file_inode(file)->i_sb);
317         if (is_sync_kiocb(iocb)) {
318                 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
319                                     ovl_iocb_to_rwf(iocb->ki_flags));
320         } else {
321                 struct ovl_aio_req *aio_req;
322
323                 ret = -ENOMEM;
324                 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
325                 if (!aio_req)
326                         goto out;
327
328                 aio_req->fd = real;
329                 real.flags = 0;
330                 aio_req->orig_iocb = iocb;
331                 kiocb_clone(&aio_req->iocb, iocb, real.file);
332                 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
333                 refcount_set(&aio_req->ref, 2);
334                 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
335                 ovl_aio_put(aio_req);
336                 if (ret != -EIOCBQUEUED)
337                         ovl_aio_cleanup_handler(aio_req);
338         }
339 out:
340         revert_creds(old_cred);
341         ovl_file_accessed(file);
342 out_fdput:
343         fdput(real);
344
345         return ret;
346 }
347
348 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
349 {
350         struct file *file = iocb->ki_filp;
351         struct inode *inode = file_inode(file);
352         struct fd real;
353         const struct cred *old_cred;
354         ssize_t ret;
355         int ifl = iocb->ki_flags;
356
357         if (!iov_iter_count(iter))
358                 return 0;
359
360         inode_lock(inode);
361         /* Update mode */
362         ovl_copyattr(inode);
363         ret = file_remove_privs(file);
364         if (ret)
365                 goto out_unlock;
366
367         ret = ovl_real_fdget(file, &real);
368         if (ret)
369                 goto out_unlock;
370
371         ret = -EINVAL;
372         if (iocb->ki_flags & IOCB_DIRECT &&
373             !(real.file->f_mode & FMODE_CAN_ODIRECT))
374                 goto out_fdput;
375
376         if (!ovl_should_sync(OVL_FS(inode->i_sb)))
377                 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
378
379         old_cred = ovl_override_creds(file_inode(file)->i_sb);
380         if (is_sync_kiocb(iocb)) {
381                 file_start_write(real.file);
382                 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
383                                      ovl_iocb_to_rwf(ifl));
384                 file_end_write(real.file);
385                 /* Update size */
386                 ovl_copyattr(inode);
387         } else {
388                 struct ovl_aio_req *aio_req;
389
390                 ret = -ENOMEM;
391                 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
392                 if (!aio_req)
393                         goto out;
394
395                 file_start_write(real.file);
396                 /* Pacify lockdep, same trick as done in aio_write() */
397                 __sb_writers_release(file_inode(real.file)->i_sb,
398                                      SB_FREEZE_WRITE);
399                 aio_req->fd = real;
400                 real.flags = 0;
401                 aio_req->orig_iocb = iocb;
402                 kiocb_clone(&aio_req->iocb, iocb, real.file);
403                 aio_req->iocb.ki_flags = ifl;
404                 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
405                 refcount_set(&aio_req->ref, 2);
406                 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
407                 ovl_aio_put(aio_req);
408                 if (ret != -EIOCBQUEUED)
409                         ovl_aio_cleanup_handler(aio_req);
410         }
411 out:
412         revert_creds(old_cred);
413 out_fdput:
414         fdput(real);
415
416 out_unlock:
417         inode_unlock(inode);
418
419         return ret;
420 }
421
422 static ssize_t ovl_splice_read(struct file *in, loff_t *ppos,
423                                struct pipe_inode_info *pipe, size_t len,
424                                unsigned int flags)
425 {
426         const struct cred *old_cred;
427         struct fd real;
428         ssize_t ret;
429
430         ret = ovl_real_fdget(in, &real);
431         if (ret)
432                 return ret;
433
434         old_cred = ovl_override_creds(file_inode(in)->i_sb);
435         ret = vfs_splice_read(real.file, ppos, pipe, len, flags);
436         revert_creds(old_cred);
437         ovl_file_accessed(in);
438
439         fdput(real);
440         return ret;
441 }
442
443 /*
444  * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
445  * due to lock order inversion between pipe->mutex in iter_file_splice_write()
446  * and file_start_write(real.file) in ovl_write_iter().
447  *
448  * So do everything ovl_write_iter() does and call iter_file_splice_write() on
449  * the real file.
450  */
451 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
452                                 loff_t *ppos, size_t len, unsigned int flags)
453 {
454         struct fd real;
455         const struct cred *old_cred;
456         struct inode *inode = file_inode(out);
457         ssize_t ret;
458
459         inode_lock(inode);
460         /* Update mode */
461         ovl_copyattr(inode);
462         ret = file_remove_privs(out);
463         if (ret)
464                 goto out_unlock;
465
466         ret = ovl_real_fdget(out, &real);
467         if (ret)
468                 goto out_unlock;
469
470         old_cred = ovl_override_creds(inode->i_sb);
471         file_start_write(real.file);
472
473         ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
474
475         file_end_write(real.file);
476         /* Update size */
477         ovl_copyattr(inode);
478         revert_creds(old_cred);
479         fdput(real);
480
481 out_unlock:
482         inode_unlock(inode);
483
484         return ret;
485 }
486
487 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
488 {
489         struct fd real;
490         const struct cred *old_cred;
491         int ret;
492
493         ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
494         if (ret <= 0)
495                 return ret;
496
497         ret = ovl_real_fdget_meta(file, &real, !datasync);
498         if (ret)
499                 return ret;
500
501         /* Don't sync lower file for fear of receiving EROFS error */
502         if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
503                 old_cred = ovl_override_creds(file_inode(file)->i_sb);
504                 ret = vfs_fsync_range(real.file, start, end, datasync);
505                 revert_creds(old_cred);
506         }
507
508         fdput(real);
509
510         return ret;
511 }
512
513 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
514 {
515         struct file *realfile = file->private_data;
516         const struct cred *old_cred;
517         int ret;
518
519         if (!realfile->f_op->mmap)
520                 return -ENODEV;
521
522         if (WARN_ON(file != vma->vm_file))
523                 return -EIO;
524
525         vma_set_file(vma, realfile);
526
527         old_cred = ovl_override_creds(file_inode(file)->i_sb);
528         ret = call_mmap(vma->vm_file, vma);
529         revert_creds(old_cred);
530         ovl_file_accessed(file);
531
532         return ret;
533 }
534
535 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
536 {
537         struct inode *inode = file_inode(file);
538         struct fd real;
539         const struct cred *old_cred;
540         int ret;
541
542         inode_lock(inode);
543         /* Update mode */
544         ovl_copyattr(inode);
545         ret = file_remove_privs(file);
546         if (ret)
547                 goto out_unlock;
548
549         ret = ovl_real_fdget(file, &real);
550         if (ret)
551                 goto out_unlock;
552
553         old_cred = ovl_override_creds(file_inode(file)->i_sb);
554         ret = vfs_fallocate(real.file, mode, offset, len);
555         revert_creds(old_cred);
556
557         /* Update size */
558         ovl_copyattr(inode);
559
560         fdput(real);
561
562 out_unlock:
563         inode_unlock(inode);
564
565         return ret;
566 }
567
568 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
569 {
570         struct fd real;
571         const struct cred *old_cred;
572         int ret;
573
574         ret = ovl_real_fdget(file, &real);
575         if (ret)
576                 return ret;
577
578         old_cred = ovl_override_creds(file_inode(file)->i_sb);
579         ret = vfs_fadvise(real.file, offset, len, advice);
580         revert_creds(old_cred);
581
582         fdput(real);
583
584         return ret;
585 }
586
587 enum ovl_copyop {
588         OVL_COPY,
589         OVL_CLONE,
590         OVL_DEDUPE,
591 };
592
593 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
594                             struct file *file_out, loff_t pos_out,
595                             loff_t len, unsigned int flags, enum ovl_copyop op)
596 {
597         struct inode *inode_out = file_inode(file_out);
598         struct fd real_in, real_out;
599         const struct cred *old_cred;
600         loff_t ret;
601
602         inode_lock(inode_out);
603         if (op != OVL_DEDUPE) {
604                 /* Update mode */
605                 ovl_copyattr(inode_out);
606                 ret = file_remove_privs(file_out);
607                 if (ret)
608                         goto out_unlock;
609         }
610
611         ret = ovl_real_fdget(file_out, &real_out);
612         if (ret)
613                 goto out_unlock;
614
615         ret = ovl_real_fdget(file_in, &real_in);
616         if (ret) {
617                 fdput(real_out);
618                 goto out_unlock;
619         }
620
621         old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
622         switch (op) {
623         case OVL_COPY:
624                 ret = vfs_copy_file_range(real_in.file, pos_in,
625                                           real_out.file, pos_out, len, flags);
626                 break;
627
628         case OVL_CLONE:
629                 ret = vfs_clone_file_range(real_in.file, pos_in,
630                                            real_out.file, pos_out, len, flags);
631                 break;
632
633         case OVL_DEDUPE:
634                 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
635                                                 real_out.file, pos_out, len,
636                                                 flags);
637                 break;
638         }
639         revert_creds(old_cred);
640
641         /* Update size */
642         ovl_copyattr(inode_out);
643
644         fdput(real_in);
645         fdput(real_out);
646
647 out_unlock:
648         inode_unlock(inode_out);
649
650         return ret;
651 }
652
653 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
654                                    struct file *file_out, loff_t pos_out,
655                                    size_t len, unsigned int flags)
656 {
657         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
658                             OVL_COPY);
659 }
660
661 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
662                                    struct file *file_out, loff_t pos_out,
663                                    loff_t len, unsigned int remap_flags)
664 {
665         enum ovl_copyop op;
666
667         if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
668                 return -EINVAL;
669
670         if (remap_flags & REMAP_FILE_DEDUP)
671                 op = OVL_DEDUPE;
672         else
673                 op = OVL_CLONE;
674
675         /*
676          * Don't copy up because of a dedupe request, this wouldn't make sense
677          * most of the time (data would be duplicated instead of deduplicated).
678          */
679         if (op == OVL_DEDUPE &&
680             (!ovl_inode_upper(file_inode(file_in)) ||
681              !ovl_inode_upper(file_inode(file_out))))
682                 return -EPERM;
683
684         return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
685                             remap_flags, op);
686 }
687
688 static int ovl_flush(struct file *file, fl_owner_t id)
689 {
690         struct fd real;
691         const struct cred *old_cred;
692         int err;
693
694         err = ovl_real_fdget(file, &real);
695         if (err)
696                 return err;
697
698         if (real.file->f_op->flush) {
699                 old_cred = ovl_override_creds(file_inode(file)->i_sb);
700                 err = real.file->f_op->flush(real.file, id);
701                 revert_creds(old_cred);
702         }
703         fdput(real);
704
705         return err;
706 }
707
708 const struct file_operations ovl_file_operations = {
709         .open           = ovl_open,
710         .release        = ovl_release,
711         .llseek         = ovl_llseek,
712         .read_iter      = ovl_read_iter,
713         .write_iter     = ovl_write_iter,
714         .fsync          = ovl_fsync,
715         .mmap           = ovl_mmap,
716         .fallocate      = ovl_fallocate,
717         .fadvise        = ovl_fadvise,
718         .flush          = ovl_flush,
719         .splice_read    = ovl_splice_read,
720         .splice_write   = ovl_splice_write,
721
722         .copy_file_range        = ovl_copy_file_range,
723         .remap_file_range       = ovl_remap_file_range,
724 };
725
726 int __init ovl_aio_request_cache_init(void)
727 {
728         ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
729                                                    sizeof(struct ovl_aio_req),
730                                                    0, SLAB_HWCACHE_ALIGN, NULL);
731         if (!ovl_aio_request_cachep)
732                 return -ENOMEM;
733
734         return 0;
735 }
736
737 void ovl_aio_request_cache_destroy(void)
738 {
739         kmem_cache_destroy(ovl_aio_request_cachep);
740 }