blob: 7d8b84c715b3e4c24523d1939560b96ea6d7632b [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Miklos Szeredid1d04ef2018-07-18 15:44:41 +02002/*
3 * Copyright (C) 2017 Red Hat, Inc.
Miklos Szeredid1d04ef2018-07-18 15:44:41 +02004 */
5
6#include <linux/cred.h>
7#include <linux/file.h>
Miklos Szeredidab5ca82018-07-18 15:44:42 +02008#include <linux/mount.h>
Miklos Szeredid1d04ef2018-07-18 15:44:41 +02009#include <linux/xattr.h>
Miklos Szeredi16914e62018-07-18 15:44:41 +020010#include <linux/uio.h>
Jiufei Xue98487de2019-05-06 15:41:02 +080011#include <linux/uaccess.h>
Murphy Zhou1a980b82020-01-17 20:49:29 +080012#include <linux/splice.h>
Miklos Szeredi292f9022020-06-02 22:20:26 +020013#include <linux/security.h>
Murphy Zhou1a980b82020-01-17 20:49:29 +080014#include <linux/mm.h>
15#include <linux/fs.h>
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020016#include "overlayfs.h"
17
Jiufei Xue2406a302019-11-20 17:45:26 +080018struct ovl_aio_req {
19 struct kiocb iocb;
20 struct kiocb *orig_iocb;
21 struct fd fd;
22};
23
24static struct kmem_cache *ovl_aio_request_cachep;
25
Vivek Goyal8c444d22018-05-11 11:49:31 -040026static 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
Amir Goldstein81a33c12020-06-18 18:43:53 +030036/* No atime modificaton nor notify on underlying */
37#define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
38
Vivek Goyal8c444d22018-05-11 11:49:31 -040039static struct file *ovl_open_realfile(const struct file *file,
40 struct inode *realinode)
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020041{
42 struct inode *inode = file_inode(file);
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020043 struct file *realfile;
44 const struct cred *old_cred;
Amir Goldstein81a33c12020-06-18 18:43:53 +030045 int flags = file->f_flags | OVL_OPEN_FLAGS;
Miklos Szeredi05acefb2020-06-02 22:20:26 +020046 int acc_mode = ACC_MODE(flags);
47 int err;
48
49 if (flags & O_APPEND)
50 acc_mode |= MAY_APPEND;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020051
52 old_cred = ovl_override_creds(inode->i_sb);
Christian Brauner47291ba2021-01-21 14:19:24 +010053 err = inode_permission(&init_user_ns, realinode, MAY_OPEN | acc_mode);
Miklos Szeredi05acefb2020-06-02 22:20:26 +020054 if (err) {
55 realfile = ERR_PTR(err);
Miklos Szeredi05acefb2020-06-02 22:20:26 +020056 } else {
Christian Brauner21cb47b2021-01-21 14:19:25 +010057 if (!inode_owner_or_capable(&init_user_ns, realinode))
Miklos Szeredib6650da2020-12-14 15:26:14 +010058 flags &= ~O_NOATIME;
59
Miklos Szeredi05acefb2020-06-02 22:20:26 +020060 realfile = open_with_fake_path(&file->f_path, flags, realinode,
61 current_cred());
62 }
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020063 revert_creds(old_cred);
64
65 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
Vivek Goyal8c444d22018-05-11 11:49:31 -040066 file, file, ovl_whatisit(inode, realinode), file->f_flags,
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020067 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
68
69 return realfile;
70}
71
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020072#define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
73
74static int ovl_change_flags(struct file *file, unsigned int flags)
75{
76 struct inode *inode = file_inode(file);
77 int err;
78
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020079 flags &= OVL_SETFL_MASK;
80
81 if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
82 return -EPERM;
83
84 if (flags & O_DIRECT) {
85 if (!file->f_mapping->a_ops ||
86 !file->f_mapping->a_ops->direct_IO)
87 return -EINVAL;
88 }
89
90 if (file->f_op->check_flags) {
91 err = file->f_op->check_flags(flags);
92 if (err)
93 return err;
94 }
95
96 spin_lock(&file->f_lock);
97 file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
98 spin_unlock(&file->f_lock);
99
100 return 0;
101}
102
Vivek Goyal8c444d22018-05-11 11:49:31 -0400103static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
104 bool allow_meta)
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200105{
106 struct inode *inode = file_inode(file);
Vivek Goyal8c444d22018-05-11 11:49:31 -0400107 struct inode *realinode;
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200108
109 real->flags = 0;
110 real->file = file->private_data;
111
Vivek Goyal8c444d22018-05-11 11:49:31 -0400112 if (allow_meta)
113 realinode = ovl_inode_real(inode);
114 else
115 realinode = ovl_inode_realdata(inode);
116
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200117 /* Has it been copied up since we'd opened it? */
Vivek Goyal8c444d22018-05-11 11:49:31 -0400118 if (unlikely(file_inode(real->file) != realinode)) {
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200119 real->flags = FDPUT_FPUT;
Vivek Goyal8c444d22018-05-11 11:49:31 -0400120 real->file = ovl_open_realfile(file, realinode);
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200121
122 return PTR_ERR_OR_ZERO(real->file);
123 }
124
125 /* Did the flags change since open? */
Amir Goldstein81a33c12020-06-18 18:43:53 +0300126 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200127 return ovl_change_flags(real->file, file->f_flags);
128
129 return 0;
130}
131
Vivek Goyal8c444d22018-05-11 11:49:31 -0400132static int ovl_real_fdget(const struct file *file, struct fd *real)
133{
Amir Goldstein61536be2020-09-29 15:28:47 +0800134 if (d_is_dir(file_dentry(file))) {
135 real->flags = 0;
136 real->file = ovl_dir_real_file(file, false);
137
138 return PTR_ERR_OR_ZERO(real->file);
139 }
140
Vivek Goyal8c444d22018-05-11 11:49:31 -0400141 return ovl_real_fdget_meta(file, real, false);
142}
143
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200144static int ovl_open(struct inode *inode, struct file *file)
145{
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200146 struct file *realfile;
147 int err;
148
Amir Goldstein34280302019-01-22 07:01:39 +0200149 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200150 if (err)
151 return err;
152
153 /* No longer need these flags, so don't pass them on to underlying fs */
154 file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
155
Vivek Goyal8c444d22018-05-11 11:49:31 -0400156 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200157 if (IS_ERR(realfile))
158 return PTR_ERR(realfile);
159
160 file->private_data = realfile;
161
162 return 0;
163}
164
165static int ovl_release(struct inode *inode, struct file *file)
166{
167 fput(file->private_data);
168
169 return 0;
170}
171
172static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
173{
Amir Goldstein9e46b842019-02-27 13:32:11 +0200174 struct inode *inode = file_inode(file);
175 struct fd real;
176 const struct cred *old_cred;
Miklos Szeredia4ac9d42020-02-03 11:41:53 +0100177 loff_t ret;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200178
Amir Goldstein9e46b842019-02-27 13:32:11 +0200179 /*
180 * The two special cases below do not need to involve real fs,
181 * so we can optimizing concurrent callers.
182 */
183 if (offset == 0) {
184 if (whence == SEEK_CUR)
185 return file->f_pos;
186
187 if (whence == SEEK_SET)
188 return vfs_setpos(file, 0, 0);
189 }
190
191 ret = ovl_real_fdget(file, &real);
192 if (ret)
193 return ret;
194
195 /*
196 * Overlay file f_pos is the master copy that is preserved
197 * through copy up and modified on read/write, but only real
198 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
199 * limitations that are more strict than ->s_maxbytes for specific
200 * files, so we use the real file to perform seeks.
201 */
Amir Goldsteinb1f9d382019-12-21 11:42:29 +0200202 ovl_inode_lock(inode);
Amir Goldstein9e46b842019-02-27 13:32:11 +0200203 real.file->f_pos = file->f_pos;
204
205 old_cred = ovl_override_creds(inode->i_sb);
206 ret = vfs_llseek(real.file, offset, whence);
207 revert_creds(old_cred);
208
209 file->f_pos = real.file->f_pos;
Amir Goldsteinb1f9d382019-12-21 11:42:29 +0200210 ovl_inode_unlock(inode);
Amir Goldstein9e46b842019-02-27 13:32:11 +0200211
212 fdput(real);
213
214 return ret;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200215}
216
Miklos Szeredi16914e62018-07-18 15:44:41 +0200217static void ovl_file_accessed(struct file *file)
218{
219 struct inode *inode, *upperinode;
220
221 if (file->f_flags & O_NOATIME)
222 return;
223
224 inode = file_inode(file);
225 upperinode = ovl_inode_upper(inode);
226
227 if (!upperinode)
228 return;
229
230 if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
231 !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
232 inode->i_mtime = upperinode->i_mtime;
233 inode->i_ctime = upperinode->i_ctime;
234 }
235
236 touch_atime(&file->f_path);
237}
238
Miklos Szeredib778e1e2020-06-04 10:48:19 +0200239static rwf_t ovl_iocb_to_rwf(int ifl)
Miklos Szeredi16914e62018-07-18 15:44:41 +0200240{
Miklos Szeredi16914e62018-07-18 15:44:41 +0200241 rwf_t flags = 0;
242
243 if (ifl & IOCB_NOWAIT)
244 flags |= RWF_NOWAIT;
245 if (ifl & IOCB_HIPRI)
246 flags |= RWF_HIPRI;
247 if (ifl & IOCB_DSYNC)
248 flags |= RWF_DSYNC;
249 if (ifl & IOCB_SYNC)
250 flags |= RWF_SYNC;
251
252 return flags;
253}
254
Jiufei Xue2406a302019-11-20 17:45:26 +0800255static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
256{
257 struct kiocb *iocb = &aio_req->iocb;
258 struct kiocb *orig_iocb = aio_req->orig_iocb;
259
260 if (iocb->ki_flags & IOCB_WRITE) {
261 struct inode *inode = file_inode(orig_iocb->ki_filp);
262
Miklos Szeredic8536802020-03-13 15:42:20 +0100263 /* Actually acquired in ovl_write_iter() */
264 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
265 SB_FREEZE_WRITE);
Jiufei Xue2406a302019-11-20 17:45:26 +0800266 file_end_write(iocb->ki_filp);
267 ovl_copyattr(ovl_inode_real(inode), inode);
268 }
269
270 orig_iocb->ki_pos = iocb->ki_pos;
271 fdput(aio_req->fd);
272 kmem_cache_free(ovl_aio_request_cachep, aio_req);
273}
274
275static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2)
276{
277 struct ovl_aio_req *aio_req = container_of(iocb,
278 struct ovl_aio_req, iocb);
279 struct kiocb *orig_iocb = aio_req->orig_iocb;
280
281 ovl_aio_cleanup_handler(aio_req);
282 orig_iocb->ki_complete(orig_iocb, res, res2);
283}
284
Miklos Szeredi16914e62018-07-18 15:44:41 +0200285static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
286{
287 struct file *file = iocb->ki_filp;
288 struct fd real;
289 const struct cred *old_cred;
290 ssize_t ret;
291
292 if (!iov_iter_count(iter))
293 return 0;
294
295 ret = ovl_real_fdget(file, &real);
296 if (ret)
297 return ret;
298
299 old_cred = ovl_override_creds(file_inode(file)->i_sb);
Jiufei Xue2406a302019-11-20 17:45:26 +0800300 if (is_sync_kiocb(iocb)) {
301 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
Miklos Szeredib778e1e2020-06-04 10:48:19 +0200302 ovl_iocb_to_rwf(iocb->ki_flags));
Jiufei Xue2406a302019-11-20 17:45:26 +0800303 } else {
304 struct ovl_aio_req *aio_req;
Miklos Szeredi16914e62018-07-18 15:44:41 +0200305
Jiufei Xue2406a302019-11-20 17:45:26 +0800306 ret = -ENOMEM;
307 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
308 if (!aio_req)
309 goto out;
310
311 aio_req->fd = real;
312 real.flags = 0;
313 aio_req->orig_iocb = iocb;
314 kiocb_clone(&aio_req->iocb, iocb, real.file);
315 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
316 ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
317 if (ret != -EIOCBQUEUED)
318 ovl_aio_cleanup_handler(aio_req);
319 }
320out:
321 revert_creds(old_cred);
Miklos Szeredi16914e62018-07-18 15:44:41 +0200322 ovl_file_accessed(file);
323
324 fdput(real);
325
326 return ret;
327}
328
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200329static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
330{
331 struct file *file = iocb->ki_filp;
332 struct inode *inode = file_inode(file);
333 struct fd real;
334 const struct cred *old_cred;
335 ssize_t ret;
Vivek Goyalc86243b02020-08-31 14:15:29 -0400336 int ifl = iocb->ki_flags;
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200337
338 if (!iov_iter_count(iter))
339 return 0;
340
341 inode_lock(inode);
342 /* Update mode */
343 ovl_copyattr(ovl_inode_real(inode), inode);
344 ret = file_remove_privs(file);
345 if (ret)
346 goto out_unlock;
347
348 ret = ovl_real_fdget(file, &real);
349 if (ret)
350 goto out_unlock;
351
Vivek Goyalc86243b02020-08-31 14:15:29 -0400352 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
353 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
354
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200355 old_cred = ovl_override_creds(file_inode(file)->i_sb);
Jiufei Xue2406a302019-11-20 17:45:26 +0800356 if (is_sync_kiocb(iocb)) {
357 file_start_write(real.file);
358 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
Vivek Goyalc86243b02020-08-31 14:15:29 -0400359 ovl_iocb_to_rwf(ifl));
Jiufei Xue2406a302019-11-20 17:45:26 +0800360 file_end_write(real.file);
361 /* Update size */
362 ovl_copyattr(ovl_inode_real(inode), inode);
363 } else {
364 struct ovl_aio_req *aio_req;
365
366 ret = -ENOMEM;
367 aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
368 if (!aio_req)
369 goto out;
370
371 file_start_write(real.file);
Miklos Szeredic8536802020-03-13 15:42:20 +0100372 /* Pacify lockdep, same trick as done in aio_write() */
373 __sb_writers_release(file_inode(real.file)->i_sb,
374 SB_FREEZE_WRITE);
Jiufei Xue2406a302019-11-20 17:45:26 +0800375 aio_req->fd = real;
376 real.flags = 0;
377 aio_req->orig_iocb = iocb;
378 kiocb_clone(&aio_req->iocb, iocb, real.file);
Vivek Goyalc86243b02020-08-31 14:15:29 -0400379 aio_req->iocb.ki_flags = ifl;
Jiufei Xue2406a302019-11-20 17:45:26 +0800380 aio_req->iocb.ki_complete = ovl_aio_rw_complete;
381 ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
382 if (ret != -EIOCBQUEUED)
383 ovl_aio_cleanup_handler(aio_req);
384 }
385out:
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200386 revert_creds(old_cred);
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200387 fdput(real);
388
389out_unlock:
390 inode_unlock(inode);
391
392 return ret;
393}
394
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200395static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
396{
397 struct fd real;
398 const struct cred *old_cred;
399 int ret;
400
Vivek Goyalc86243b02020-08-31 14:15:29 -0400401 if (!ovl_should_sync(OVL_FS(file_inode(file)->i_sb)))
402 return 0;
403
Vivek Goyal8c444d22018-05-11 11:49:31 -0400404 ret = ovl_real_fdget_meta(file, &real, !datasync);
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200405 if (ret)
406 return ret;
407
408 /* Don't sync lower file for fear of receiving EROFS error */
409 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
410 old_cred = ovl_override_creds(file_inode(file)->i_sb);
411 ret = vfs_fsync_range(real.file, start, end, datasync);
412 revert_creds(old_cred);
413 }
414
415 fdput(real);
416
417 return ret;
418}
419
Miklos Szeredi2f502832018-07-18 15:44:42 +0200420static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
421{
422 struct file *realfile = file->private_data;
423 const struct cred *old_cred;
424 int ret;
425
426 if (!realfile->f_op->mmap)
427 return -ENODEV;
428
429 if (WARN_ON(file != vma->vm_file))
430 return -EIO;
431
432 vma->vm_file = get_file(realfile);
433
434 old_cred = ovl_override_creds(file_inode(file)->i_sb);
435 ret = call_mmap(vma->vm_file, vma);
436 revert_creds(old_cred);
437
438 if (ret) {
439 /* Drop reference count from new vm_file value */
440 fput(realfile);
441 } else {
442 /* Drop reference count from previous vm_file value */
443 fput(file);
444 }
445
446 ovl_file_accessed(file);
447
448 return ret;
449}
450
Miklos Szerediaab88482018-07-18 15:44:42 +0200451static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
452{
453 struct inode *inode = file_inode(file);
454 struct fd real;
455 const struct cred *old_cred;
456 int ret;
457
458 ret = ovl_real_fdget(file, &real);
459 if (ret)
460 return ret;
461
462 old_cred = ovl_override_creds(file_inode(file)->i_sb);
463 ret = vfs_fallocate(real.file, mode, offset, len);
464 revert_creds(old_cred);
465
466 /* Update size */
467 ovl_copyattr(ovl_inode_real(inode), inode);
468
469 fdput(real);
470
471 return ret;
472}
473
Amir Goldsteinb833a362018-08-28 10:58:41 +0300474static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
475{
476 struct fd real;
477 const struct cred *old_cred;
478 int ret;
479
480 ret = ovl_real_fdget(file, &real);
481 if (ret)
482 return ret;
483
484 old_cred = ovl_override_creds(file_inode(file)->i_sb);
485 ret = vfs_fadvise(real.file, offset, len, advice);
486 revert_creds(old_cred);
487
488 fdput(real);
489
490 return ret;
491}
492
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200493static long ovl_real_ioctl(struct file *file, unsigned int cmd,
494 unsigned long arg)
495{
496 struct fd real;
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200497 long ret;
498
499 ret = ovl_real_fdget(file, &real);
500 if (ret)
501 return ret;
502
Miklos Szeredi292f9022020-06-02 22:20:26 +0200503 ret = security_file_ioctl(real.file, cmd, arg);
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100504 if (!ret) {
505 /*
506 * Don't override creds, since we currently can't safely check
507 * permissions before doing so.
508 */
Miklos Szeredi292f9022020-06-02 22:20:26 +0200509 ret = vfs_ioctl(real.file, cmd, arg);
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100510 }
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200511
512 fdput(real);
513
514 return ret;
515}
516
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300517static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100518 unsigned long arg)
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200519{
520 long ret;
521 struct inode *inode = file_inode(file);
Jiufei Xue98487de2019-05-06 15:41:02 +0800522
Christian Brauner21cb47b2021-01-21 14:19:25 +0100523 if (!inode_owner_or_capable(&init_user_ns, inode))
Jiufei Xue98487de2019-05-06 15:41:02 +0800524 return -EACCES;
525
Jiufei Xue98487de2019-05-06 15:41:02 +0800526 ret = mnt_want_write_file(file);
527 if (ret)
528 return ret;
529
530 inode_lock(inode);
531
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100532 /*
533 * Prevent copy up if immutable and has no CAP_LINUX_IMMUTABLE
534 * capability.
535 */
536 ret = -EPERM;
537 if (!ovl_has_upperdata(inode) && IS_IMMUTABLE(inode) &&
538 !capable(CAP_LINUX_IMMUTABLE))
Jiufei Xue98487de2019-05-06 15:41:02 +0800539 goto unlock;
540
541 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
542 if (ret)
543 goto unlock;
544
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300545 ret = ovl_real_ioctl(file, cmd, arg);
Jiufei Xue98487de2019-05-06 15:41:02 +0800546
547 ovl_copyflags(ovl_inode_real(inode), inode);
548unlock:
549 inode_unlock(inode);
550
551 mnt_drop_write_file(file);
552
553 return ret;
554
555}
556
Amir Goldstein61536be2020-09-29 15:28:47 +0800557long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jiufei Xue98487de2019-05-06 15:41:02 +0800558{
559 long ret;
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200560
561 switch (cmd) {
562 case FS_IOC_GETFLAGS:
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300563 case FS_IOC_FSGETXATTR:
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200564 ret = ovl_real_ioctl(file, cmd, arg);
565 break;
566
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300567 case FS_IOC_FSSETXATTR:
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100568 case FS_IOC_SETFLAGS:
569 ret = ovl_ioctl_set_flags(file, cmd, arg);
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200570 break;
571
572 default:
573 ret = -ENOTTY;
574 }
575
576 return ret;
577}
578
Amir Goldstein61536be2020-09-29 15:28:47 +0800579#ifdef CONFIG_COMPAT
580long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200581{
582 switch (cmd) {
583 case FS_IOC32_GETFLAGS:
584 cmd = FS_IOC_GETFLAGS;
585 break;
586
587 case FS_IOC32_SETFLAGS:
588 cmd = FS_IOC_SETFLAGS;
589 break;
590
591 default:
592 return -ENOIOCTLCMD;
593 }
594
595 return ovl_ioctl(file, cmd, arg);
596}
Amir Goldstein61536be2020-09-29 15:28:47 +0800597#endif
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200598
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200599enum ovl_copyop {
600 OVL_COPY,
601 OVL_CLONE,
602 OVL_DEDUPE,
603};
604
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100605static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200606 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100607 loff_t len, unsigned int flags, enum ovl_copyop op)
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200608{
609 struct inode *inode_out = file_inode(file_out);
610 struct fd real_in, real_out;
611 const struct cred *old_cred;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100612 loff_t ret;
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200613
614 ret = ovl_real_fdget(file_out, &real_out);
615 if (ret)
616 return ret;
617
618 ret = ovl_real_fdget(file_in, &real_in);
619 if (ret) {
620 fdput(real_out);
621 return ret;
622 }
623
624 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
625 switch (op) {
626 case OVL_COPY:
627 ret = vfs_copy_file_range(real_in.file, pos_in,
628 real_out.file, pos_out, len, flags);
629 break;
630
631 case OVL_CLONE:
Amir Goldsteina7253562018-09-18 16:34:34 +0300632 ret = vfs_clone_file_range(real_in.file, pos_in,
Darrick J. Wong452ce652018-10-30 10:41:56 +1100633 real_out.file, pos_out, len, flags);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200634 break;
635
636 case OVL_DEDUPE:
637 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
Darrick J. Wongdf365832018-10-30 10:42:03 +1100638 real_out.file, pos_out, len,
639 flags);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200640 break;
641 }
642 revert_creds(old_cred);
643
644 /* Update size */
645 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
646
647 fdput(real_in);
648 fdput(real_out);
649
650 return ret;
651}
652
653static 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
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100661static 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)
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200664{
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100665 enum ovl_copyop op;
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200666
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100667 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
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200675 /*
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 */
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100679 if (op == OVL_DEDUPE &&
680 (!ovl_inode_upper(file_inode(file_in)) ||
681 !ovl_inode_upper(file_inode(file_out))))
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200682 return -EPERM;
683
Darrick J. Wong452ce652018-10-30 10:41:56 +1100684 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
685 remap_flags, op);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200686}
687
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200688const struct file_operations ovl_file_operations = {
689 .open = ovl_open,
690 .release = ovl_release,
691 .llseek = ovl_llseek,
Miklos Szeredi16914e62018-07-18 15:44:41 +0200692 .read_iter = ovl_read_iter,
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200693 .write_iter = ovl_write_iter,
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200694 .fsync = ovl_fsync,
Miklos Szeredi2f502832018-07-18 15:44:42 +0200695 .mmap = ovl_mmap,
Miklos Szerediaab88482018-07-18 15:44:42 +0200696 .fallocate = ovl_fallocate,
Amir Goldsteinb833a362018-08-28 10:58:41 +0300697 .fadvise = ovl_fadvise,
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200698 .unlocked_ioctl = ovl_ioctl,
Amir Goldstein61536be2020-09-29 15:28:47 +0800699#ifdef CONFIG_COMPAT
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200700 .compat_ioctl = ovl_compat_ioctl,
Amir Goldstein61536be2020-09-29 15:28:47 +0800701#endif
Miklos Szeredi82a763e2020-12-14 15:26:14 +0100702 .splice_read = generic_file_splice_read,
703 .splice_write = iter_file_splice_write,
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200704
705 .copy_file_range = ovl_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100706 .remap_file_range = ovl_remap_file_range,
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200707};
Jiufei Xue2406a302019-11-20 17:45:26 +0800708
709int __init ovl_aio_request_cache_init(void)
710{
711 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
712 sizeof(struct ovl_aio_req),
713 0, SLAB_HWCACHE_ALIGN, NULL);
714 if (!ovl_aio_request_cachep)
715 return -ENOMEM;
716
717 return 0;
718}
719
720void ovl_aio_request_cache_destroy(void)
721{
722 kmem_cache_destroy(ovl_aio_request_cachep);
723}