blob: 50186ea90158a2da76edb54c1455665d03ca9940 [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);
Miklos Szeredi05acefb2020-06-02 22:20:26 +020053 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 }
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020062 revert_creds(old_cred);
63
64 pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
Vivek Goyal8c444d22018-05-11 11:49:31 -040065 file, file, ovl_whatisit(inode, realinode), file->f_flags,
Miklos Szeredid1d04ef2018-07-18 15:44:41 +020066 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
67
68 return realfile;
69}
70
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020071#define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
72
73static int ovl_change_flags(struct file *file, unsigned int flags)
74{
75 struct inode *inode = file_inode(file);
76 int err;
77
Amir Goldstein81a33c12020-06-18 18:43:53 +030078 flags |= OVL_OPEN_FLAGS;
Miklos Szeredi2ef66b82018-07-18 15:44:41 +020079
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
Vivek Goyal8c444d22018-05-11 11:49:31 -0400108static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
109 bool allow_meta)
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200110{
111 struct inode *inode = file_inode(file);
Vivek Goyal8c444d22018-05-11 11:49:31 -0400112 struct inode *realinode;
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200113
114 real->flags = 0;
115 real->file = file->private_data;
116
Vivek Goyal8c444d22018-05-11 11:49:31 -0400117 if (allow_meta)
118 realinode = ovl_inode_real(inode);
119 else
120 realinode = ovl_inode_realdata(inode);
121
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200122 /* Has it been copied up since we'd opened it? */
Vivek Goyal8c444d22018-05-11 11:49:31 -0400123 if (unlikely(file_inode(real->file) != realinode)) {
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200124 real->flags = FDPUT_FPUT;
Vivek Goyal8c444d22018-05-11 11:49:31 -0400125 real->file = ovl_open_realfile(file, realinode);
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200126
127 return PTR_ERR_OR_ZERO(real->file);
128 }
129
130 /* Did the flags change since open? */
Amir Goldstein81a33c12020-06-18 18:43:53 +0300131 if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
Miklos Szeredi2ef66b82018-07-18 15:44:41 +0200132 return ovl_change_flags(real->file, file->f_flags);
133
134 return 0;
135}
136
Vivek Goyal8c444d22018-05-11 11:49:31 -0400137static int ovl_real_fdget(const struct file *file, struct fd *real)
138{
Amir Goldstein61536be2020-09-29 15:28:47 +0800139 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
Vivek Goyal8c444d22018-05-11 11:49:31 -0400146 return ovl_real_fdget_meta(file, real, false);
147}
148
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200149static int ovl_open(struct inode *inode, struct file *file)
150{
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200151 struct file *realfile;
152 int err;
153
Amir Goldstein34280302019-01-22 07:01:39 +0200154 err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200155 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
Vivek Goyal8c444d22018-05-11 11:49:31 -0400161 realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200162 if (IS_ERR(realfile))
163 return PTR_ERR(realfile);
164
165 file->private_data = realfile;
166
167 return 0;
168}
169
170static int ovl_release(struct inode *inode, struct file *file)
171{
172 fput(file->private_data);
173
174 return 0;
175}
176
177static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
178{
Amir Goldstein9e46b842019-02-27 13:32:11 +0200179 struct inode *inode = file_inode(file);
180 struct fd real;
181 const struct cred *old_cred;
Miklos Szeredia4ac9d42020-02-03 11:41:53 +0100182 loff_t ret;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200183
Amir Goldstein9e46b842019-02-27 13:32:11 +0200184 /*
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 */
Amir Goldsteinb1f9d382019-12-21 11:42:29 +0200207 ovl_inode_lock(inode);
Amir Goldstein9e46b842019-02-27 13:32:11 +0200208 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;
Amir Goldsteinb1f9d382019-12-21 11:42:29 +0200215 ovl_inode_unlock(inode);
Amir Goldstein9e46b842019-02-27 13:32:11 +0200216
217 fdput(real);
218
219 return ret;
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200220}
221
Miklos Szeredi16914e62018-07-18 15:44:41 +0200222static 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
Miklos Szeredib778e1e2020-06-04 10:48:19 +0200244static rwf_t ovl_iocb_to_rwf(int ifl)
Miklos Szeredi16914e62018-07-18 15:44:41 +0200245{
Miklos Szeredi16914e62018-07-18 15:44:41 +0200246 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
Jiufei Xue2406a302019-11-20 17:45:26 +0800260static 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
Miklos Szeredic8536802020-03-13 15:42:20 +0100268 /* Actually acquired in ovl_write_iter() */
269 __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
270 SB_FREEZE_WRITE);
Jiufei Xue2406a302019-11-20 17:45:26 +0800271 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
280static 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
Miklos Szeredi16914e62018-07-18 15:44:41 +0200290static 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);
Jiufei Xue2406a302019-11-20 17:45:26 +0800305 if (is_sync_kiocb(iocb)) {
306 ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
Miklos Szeredib778e1e2020-06-04 10:48:19 +0200307 ovl_iocb_to_rwf(iocb->ki_flags));
Jiufei Xue2406a302019-11-20 17:45:26 +0800308 } else {
309 struct ovl_aio_req *aio_req;
Miklos Szeredi16914e62018-07-18 15:44:41 +0200310
Jiufei Xue2406a302019-11-20 17:45:26 +0800311 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 }
325out:
326 revert_creds(old_cred);
Miklos Szeredi16914e62018-07-18 15:44:41 +0200327 ovl_file_accessed(file);
328
329 fdput(real);
330
331 return ret;
332}
333
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200334static 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;
Vivek Goyalc86243b02020-08-31 14:15:29 -0400341 int ifl = iocb->ki_flags;
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200342
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
Vivek Goyalc86243b02020-08-31 14:15:29 -0400357 if (!ovl_should_sync(OVL_FS(inode->i_sb)))
358 ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
359
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200360 old_cred = ovl_override_creds(file_inode(file)->i_sb);
Jiufei Xue2406a302019-11-20 17:45:26 +0800361 if (is_sync_kiocb(iocb)) {
362 file_start_write(real.file);
363 ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
Vivek Goyalc86243b02020-08-31 14:15:29 -0400364 ovl_iocb_to_rwf(ifl));
Jiufei Xue2406a302019-11-20 17:45:26 +0800365 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);
Miklos Szeredic8536802020-03-13 15:42:20 +0100377 /* Pacify lockdep, same trick as done in aio_write() */
378 __sb_writers_release(file_inode(real.file)->i_sb,
379 SB_FREEZE_WRITE);
Jiufei Xue2406a302019-11-20 17:45:26 +0800380 aio_req->fd = real;
381 real.flags = 0;
382 aio_req->orig_iocb = iocb;
383 kiocb_clone(&aio_req->iocb, iocb, real.file);
Vivek Goyalc86243b02020-08-31 14:15:29 -0400384 aio_req->iocb.ki_flags = ifl;
Jiufei Xue2406a302019-11-20 17:45:26 +0800385 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 }
390out:
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200391 revert_creds(old_cred);
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200392 fdput(real);
393
394out_unlock:
395 inode_unlock(inode);
396
397 return ret;
398}
399
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200400static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
401{
402 struct fd real;
403 const struct cred *old_cred;
404 int ret;
405
Vivek Goyalc86243b02020-08-31 14:15:29 -0400406 if (!ovl_should_sync(OVL_FS(file_inode(file)->i_sb)))
407 return 0;
408
Vivek Goyal8c444d22018-05-11 11:49:31 -0400409 ret = ovl_real_fdget_meta(file, &real, !datasync);
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200410 if (ret)
411 return ret;
412
413 /* Don't sync lower file for fear of receiving EROFS error */
414 if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
415 old_cred = ovl_override_creds(file_inode(file)->i_sb);
416 ret = vfs_fsync_range(real.file, start, end, datasync);
417 revert_creds(old_cred);
418 }
419
420 fdput(real);
421
422 return ret;
423}
424
Miklos Szeredi2f502832018-07-18 15:44:42 +0200425static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
426{
427 struct file *realfile = file->private_data;
428 const struct cred *old_cred;
429 int ret;
430
431 if (!realfile->f_op->mmap)
432 return -ENODEV;
433
434 if (WARN_ON(file != vma->vm_file))
435 return -EIO;
436
437 vma->vm_file = get_file(realfile);
438
439 old_cred = ovl_override_creds(file_inode(file)->i_sb);
440 ret = call_mmap(vma->vm_file, vma);
441 revert_creds(old_cred);
442
443 if (ret) {
444 /* Drop reference count from new vm_file value */
445 fput(realfile);
446 } else {
447 /* Drop reference count from previous vm_file value */
448 fput(file);
449 }
450
451 ovl_file_accessed(file);
452
453 return ret;
454}
455
Miklos Szerediaab88482018-07-18 15:44:42 +0200456static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
457{
458 struct inode *inode = file_inode(file);
459 struct fd real;
460 const struct cred *old_cred;
461 int ret;
462
463 ret = ovl_real_fdget(file, &real);
464 if (ret)
465 return ret;
466
467 old_cred = ovl_override_creds(file_inode(file)->i_sb);
468 ret = vfs_fallocate(real.file, mode, offset, len);
469 revert_creds(old_cred);
470
471 /* Update size */
472 ovl_copyattr(ovl_inode_real(inode), inode);
473
474 fdput(real);
475
476 return ret;
477}
478
Amir Goldsteinb833a362018-08-28 10:58:41 +0300479static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
480{
481 struct fd real;
482 const struct cred *old_cred;
483 int ret;
484
485 ret = ovl_real_fdget(file, &real);
486 if (ret)
487 return ret;
488
489 old_cred = ovl_override_creds(file_inode(file)->i_sb);
490 ret = vfs_fadvise(real.file, offset, len, advice);
491 revert_creds(old_cred);
492
493 fdput(real);
494
495 return ret;
496}
497
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200498static long ovl_real_ioctl(struct file *file, unsigned int cmd,
499 unsigned long arg)
500{
501 struct fd real;
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200502 long ret;
503
504 ret = ovl_real_fdget(file, &real);
505 if (ret)
506 return ret;
507
Miklos Szeredi292f9022020-06-02 22:20:26 +0200508 ret = security_file_ioctl(real.file, cmd, arg);
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100509 if (!ret) {
510 /*
511 * Don't override creds, since we currently can't safely check
512 * permissions before doing so.
513 */
Miklos Szeredi292f9022020-06-02 22:20:26 +0200514 ret = vfs_ioctl(real.file, cmd, arg);
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100515 }
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200516
517 fdput(real);
518
519 return ret;
520}
521
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300522static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd,
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100523 unsigned long arg)
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200524{
525 long ret;
526 struct inode *inode = file_inode(file);
Jiufei Xue98487de2019-05-06 15:41:02 +0800527
528 if (!inode_owner_or_capable(inode))
529 return -EACCES;
530
Jiufei Xue98487de2019-05-06 15:41:02 +0800531 ret = mnt_want_write_file(file);
532 if (ret)
533 return ret;
534
535 inode_lock(inode);
536
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100537 /*
538 * Prevent copy up if immutable and has no CAP_LINUX_IMMUTABLE
539 * capability.
540 */
541 ret = -EPERM;
542 if (!ovl_has_upperdata(inode) && IS_IMMUTABLE(inode) &&
543 !capable(CAP_LINUX_IMMUTABLE))
Jiufei Xue98487de2019-05-06 15:41:02 +0800544 goto unlock;
545
546 ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY);
547 if (ret)
548 goto unlock;
549
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300550 ret = ovl_real_ioctl(file, cmd, arg);
Jiufei Xue98487de2019-05-06 15:41:02 +0800551
552 ovl_copyflags(ovl_inode_real(inode), inode);
553unlock:
554 inode_unlock(inode);
555
556 mnt_drop_write_file(file);
557
558 return ret;
559
560}
561
Amir Goldstein61536be2020-09-29 15:28:47 +0800562long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Jiufei Xue98487de2019-05-06 15:41:02 +0800563{
564 long ret;
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200565
566 switch (cmd) {
567 case FS_IOC_GETFLAGS:
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300568 case FS_IOC_FSGETXATTR:
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200569 ret = ovl_real_ioctl(file, cmd, arg);
570 break;
571
Amir Goldsteinb21d9c432019-05-26 09:28:25 +0300572 case FS_IOC_FSSETXATTR:
Miklos Szeredi89bdfaf2020-12-14 15:26:14 +0100573 case FS_IOC_SETFLAGS:
574 ret = ovl_ioctl_set_flags(file, cmd, arg);
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200575 break;
576
577 default:
578 ret = -ENOTTY;
579 }
580
581 return ret;
582}
583
Amir Goldstein61536be2020-09-29 15:28:47 +0800584#ifdef CONFIG_COMPAT
585long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200586{
587 switch (cmd) {
588 case FS_IOC32_GETFLAGS:
589 cmd = FS_IOC_GETFLAGS;
590 break;
591
592 case FS_IOC32_SETFLAGS:
593 cmd = FS_IOC_SETFLAGS;
594 break;
595
596 default:
597 return -ENOIOCTLCMD;
598 }
599
600 return ovl_ioctl(file, cmd, arg);
601}
Amir Goldstein61536be2020-09-29 15:28:47 +0800602#endif
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200603
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200604enum ovl_copyop {
605 OVL_COPY,
606 OVL_CLONE,
607 OVL_DEDUPE,
608};
609
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100610static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200611 struct file *file_out, loff_t pos_out,
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100612 loff_t len, unsigned int flags, enum ovl_copyop op)
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200613{
614 struct inode *inode_out = file_inode(file_out);
615 struct fd real_in, real_out;
616 const struct cred *old_cred;
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100617 loff_t ret;
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200618
619 ret = ovl_real_fdget(file_out, &real_out);
620 if (ret)
621 return ret;
622
623 ret = ovl_real_fdget(file_in, &real_in);
624 if (ret) {
625 fdput(real_out);
626 return ret;
627 }
628
629 old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
630 switch (op) {
631 case OVL_COPY:
632 ret = vfs_copy_file_range(real_in.file, pos_in,
633 real_out.file, pos_out, len, flags);
634 break;
635
636 case OVL_CLONE:
Amir Goldsteina7253562018-09-18 16:34:34 +0300637 ret = vfs_clone_file_range(real_in.file, pos_in,
Darrick J. Wong452ce652018-10-30 10:41:56 +1100638 real_out.file, pos_out, len, flags);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200639 break;
640
641 case OVL_DEDUPE:
642 ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
Darrick J. Wongdf365832018-10-30 10:42:03 +1100643 real_out.file, pos_out, len,
644 flags);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200645 break;
646 }
647 revert_creds(old_cred);
648
649 /* Update size */
650 ovl_copyattr(ovl_inode_real(inode_out), inode_out);
651
652 fdput(real_in);
653 fdput(real_out);
654
655 return ret;
656}
657
658static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
659 struct file *file_out, loff_t pos_out,
660 size_t len, unsigned int flags)
661{
662 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
663 OVL_COPY);
664}
665
Darrick J. Wong42ec3d42018-10-30 10:41:49 +1100666static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
667 struct file *file_out, loff_t pos_out,
668 loff_t len, unsigned int remap_flags)
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200669{
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100670 enum ovl_copyop op;
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200671
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100672 if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
673 return -EINVAL;
674
675 if (remap_flags & REMAP_FILE_DEDUP)
676 op = OVL_DEDUPE;
677 else
678 op = OVL_CLONE;
679
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200680 /*
681 * Don't copy up because of a dedupe request, this wouldn't make sense
682 * most of the time (data would be duplicated instead of deduplicated).
683 */
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100684 if (op == OVL_DEDUPE &&
685 (!ovl_inode_upper(file_inode(file_in)) ||
686 !ovl_inode_upper(file_inode(file_out))))
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200687 return -EPERM;
688
Darrick J. Wong452ce652018-10-30 10:41:56 +1100689 return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
690 remap_flags, op);
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200691}
692
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200693const struct file_operations ovl_file_operations = {
694 .open = ovl_open,
695 .release = ovl_release,
696 .llseek = ovl_llseek,
Miklos Szeredi16914e62018-07-18 15:44:41 +0200697 .read_iter = ovl_read_iter,
Miklos Szeredi2a92e072018-07-18 15:44:41 +0200698 .write_iter = ovl_write_iter,
Miklos Szeredide30dfd2018-07-18 15:44:42 +0200699 .fsync = ovl_fsync,
Miklos Szeredi2f502832018-07-18 15:44:42 +0200700 .mmap = ovl_mmap,
Miklos Szerediaab88482018-07-18 15:44:42 +0200701 .fallocate = ovl_fallocate,
Amir Goldsteinb833a362018-08-28 10:58:41 +0300702 .fadvise = ovl_fadvise,
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200703 .unlocked_ioctl = ovl_ioctl,
Amir Goldstein61536be2020-09-29 15:28:47 +0800704#ifdef CONFIG_COMPAT
Miklos Szeredidab5ca82018-07-18 15:44:42 +0200705 .compat_ioctl = ovl_compat_ioctl,
Amir Goldstein61536be2020-09-29 15:28:47 +0800706#endif
Miklos Szeredi82a763e2020-12-14 15:26:14 +0100707 .splice_read = generic_file_splice_read,
708 .splice_write = iter_file_splice_write,
Miklos Szeredi8ede2052018-07-18 15:44:42 +0200709
710 .copy_file_range = ovl_copy_file_range,
Darrick J. Wong2e5dfc92018-10-30 10:41:21 +1100711 .remap_file_range = ovl_remap_file_range,
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200712};
Jiufei Xue2406a302019-11-20 17:45:26 +0800713
714int __init ovl_aio_request_cache_init(void)
715{
716 ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
717 sizeof(struct ovl_aio_req),
718 0, SLAB_HWCACHE_ALIGN, NULL);
719 if (!ovl_aio_request_cachep)
720 return -ENOMEM;
721
722 return 0;
723}
724
725void ovl_aio_request_cache_destroy(void)
726{
727 kmem_cache_destroy(ovl_aio_request_cachep);
728}