Commit | Line | Data |
---|---|---|
d2912cb1 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
d1d04ef8 MS |
2 | /* |
3 | * Copyright (C) 2017 Red Hat, Inc. | |
d1d04ef8 MS |
4 | */ |
5 | ||
6 | #include <linux/cred.h> | |
7 | #include <linux/file.h> | |
dab5ca8f | 8 | #include <linux/mount.h> |
d1d04ef8 | 9 | #include <linux/xattr.h> |
16914e6f | 10 | #include <linux/uio.h> |
98487de3 | 11 | #include <linux/uaccess.h> |
1a980b8c | 12 | #include <linux/splice.h> |
292f902a | 13 | #include <linux/security.h> |
1a980b8c MZ |
14 | #include <linux/mm.h> |
15 | #include <linux/fs.h> | |
d1d04ef8 MS |
16 | #include "overlayfs.h" |
17 | ||
2406a307 JX |
18 | struct ovl_aio_req { |
19 | struct kiocb iocb; | |
20 | struct kiocb *orig_iocb; | |
21 | struct fd fd; | |
22 | }; | |
23 | ||
24 | static struct kmem_cache *ovl_aio_request_cachep; | |
25 | ||
8c444d2a VG |
26 | static char ovl_whatisit(struct inode *inode, struct inode *realinode) |
27 | { | |
28 | if (realinode != ovl_inode_upper(inode)) | |
29 | return 'l'; | |
30 | if (ovl_has_upperdata(inode)) | |
31 | return 'u'; | |
32 | else | |
33 | return 'm'; | |
34 | } | |
35 | ||
81a33c1e AG |
36 | /* No atime modificaton nor notify on underlying */ |
37 | #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY) | |
38 | ||
8c444d2a VG |
39 | static struct file *ovl_open_realfile(const struct file *file, |
40 | struct inode *realinode) | |
d1d04ef8 MS |
41 | { |
42 | struct inode *inode = file_inode(file); | |
d1d04ef8 MS |
43 | struct file *realfile; |
44 | const struct cred *old_cred; | |
81a33c1e | 45 | int flags = file->f_flags | OVL_OPEN_FLAGS; |
05acefb4 MS |
46 | int acc_mode = ACC_MODE(flags); |
47 | int err; | |
48 | ||
49 | if (flags & O_APPEND) | |
50 | acc_mode |= MAY_APPEND; | |
d1d04ef8 MS |
51 | |
52 | old_cred = ovl_override_creds(inode->i_sb); | |
05acefb4 MS |
53 | err = inode_permission(realinode, MAY_OPEN | acc_mode); |
54 | if (err) { | |
55 | realfile = ERR_PTR(err); | |
56 | } else if (!inode_owner_or_capable(realinode)) { | |
57 | realfile = ERR_PTR(-EPERM); | |
58 | } else { | |
59 | realfile = open_with_fake_path(&file->f_path, flags, realinode, | |
60 | current_cred()); | |
61 | } | |
d1d04ef8 MS |
62 | revert_creds(old_cred); |
63 | ||
64 | pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", | |
8c444d2a | 65 | file, file, ovl_whatisit(inode, realinode), file->f_flags, |
d1d04ef8 MS |
66 | realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); |
67 | ||
68 | return realfile; | |
69 | } | |
70 | ||
2ef66b8a MS |
71 | #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT) |
72 | ||
73 | static int ovl_change_flags(struct file *file, unsigned int flags) | |
74 | { | |
75 | struct inode *inode = file_inode(file); | |
76 | int err; | |
77 | ||
81a33c1e | 78 | flags |= OVL_OPEN_FLAGS; |
2ef66b8a MS |
79 | |
80 | /* If some flag changed that cannot be changed then something's amiss */ | |
81 | if (WARN_ON((file->f_flags ^ flags) & ~OVL_SETFL_MASK)) | |
82 | return -EIO; | |
83 | ||
84 | flags &= OVL_SETFL_MASK; | |
85 | ||
86 | if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) | |
87 | return -EPERM; | |
88 | ||
89 | if (flags & O_DIRECT) { | |
90 | if (!file->f_mapping->a_ops || | |
91 | !file->f_mapping->a_ops->direct_IO) | |
92 | return -EINVAL; | |
93 | } | |
94 | ||
95 | if (file->f_op->check_flags) { | |
96 | err = file->f_op->check_flags(flags); | |
97 | if (err) | |
98 | return err; | |
99 | } | |
100 | ||
101 | spin_lock(&file->f_lock); | |
102 | file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags; | |
103 | spin_unlock(&file->f_lock); | |
104 | ||
105 | return 0; | |
106 | } | |
107 | ||
8c444d2a VG |
108 | static int ovl_real_fdget_meta(const struct file *file, struct fd *real, |
109 | bool allow_meta) | |
2ef66b8a MS |
110 | { |
111 | struct inode *inode = file_inode(file); | |
8c444d2a | 112 | struct inode *realinode; |
2ef66b8a MS |
113 | |
114 | real->flags = 0; | |
115 | real->file = file->private_data; | |
116 | ||
8c444d2a VG |
117 | if (allow_meta) |
118 | realinode = ovl_inode_real(inode); | |
119 | else | |
120 | realinode = ovl_inode_realdata(inode); | |
121 | ||
2ef66b8a | 122 | /* Has it been copied up since we'd opened it? */ |
8c444d2a | 123 | if (unlikely(file_inode(real->file) != realinode)) { |
2ef66b8a | 124 | real->flags = FDPUT_FPUT; |
8c444d2a | 125 | real->file = ovl_open_realfile(file, realinode); |
2ef66b8a MS |
126 | |
127 | return PTR_ERR_OR_ZERO(real->file); | |
128 | } | |
129 | ||
130 | /* Did the flags change since open? */ | |
81a33c1e | 131 | if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS)) |
2ef66b8a MS |
132 | return ovl_change_flags(real->file, file->f_flags); |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
8c444d2a VG |
137 | static int ovl_real_fdget(const struct file *file, struct fd *real) |
138 | { | |
61536bed AG |
139 | if (d_is_dir(file_dentry(file))) { |
140 | real->flags = 0; | |
141 | real->file = ovl_dir_real_file(file, false); | |
142 | ||
143 | return PTR_ERR_OR_ZERO(real->file); | |
144 | } | |
145 | ||
8c444d2a VG |
146 | return ovl_real_fdget_meta(file, real, false); |
147 | } | |
148 | ||
d1d04ef8 MS |
149 | static int ovl_open(struct inode *inode, struct file *file) |
150 | { | |
d1d04ef8 MS |
151 | struct file *realfile; |
152 | int err; | |
153 | ||
3428030d | 154 | err = ovl_maybe_copy_up(file_dentry(file), file->f_flags); |
d1d04ef8 MS |
155 | if (err) |
156 | return err; | |
157 | ||
158 | /* No longer need these flags, so don't pass them on to underlying fs */ | |
159 | file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC); | |
160 | ||
8c444d2a | 161 | realfile = ovl_open_realfile(file, ovl_inode_realdata(inode)); |
d1d04ef8 MS |
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 | { | |
9e46b840 AG |
179 | struct inode *inode = file_inode(file); |
180 | struct fd real; | |
181 | const struct cred *old_cred; | |
a4ac9d45 | 182 | loff_t ret; |
9e46b840 AG |
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 | */ | |
b1f9d385 | 207 | ovl_inode_lock(inode); |
9e46b840 AG |
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; | |
b1f9d385 | 215 | ovl_inode_unlock(inode); |
9e46b840 AG |
216 | |
217 | fdput(real); | |
d1d04ef8 | 218 | |
9e46b840 | 219 | return ret; |
d1d04ef8 MS |
220 | } |
221 | ||
16914e6f MS |
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 | ||
b778e1ee | 244 | static rwf_t ovl_iocb_to_rwf(int ifl) |
16914e6f | 245 | { |
16914e6f MS |
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 | ||
2406a307 JX |
260 | static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req) |
261 | { | |
262 | struct kiocb *iocb = &aio_req->iocb; | |
263 | struct kiocb *orig_iocb = aio_req->orig_iocb; | |
264 | ||
265 | if (iocb->ki_flags & IOCB_WRITE) { | |
266 | struct inode *inode = file_inode(orig_iocb->ki_filp); | |
267 | ||
c8536804 MS |
268 | /* Actually acquired in ovl_write_iter() */ |
269 | __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb, | |
270 | SB_FREEZE_WRITE); | |
2406a307 JX |
271 | file_end_write(iocb->ki_filp); |
272 | ovl_copyattr(ovl_inode_real(inode), inode); | |
273 | } | |
274 | ||
275 | orig_iocb->ki_pos = iocb->ki_pos; | |
276 | fdput(aio_req->fd); | |
277 | kmem_cache_free(ovl_aio_request_cachep, aio_req); | |
278 | } | |
279 | ||
280 | static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2) | |
281 | { | |
282 | struct ovl_aio_req *aio_req = container_of(iocb, | |
283 | struct ovl_aio_req, iocb); | |
284 | struct kiocb *orig_iocb = aio_req->orig_iocb; | |
285 | ||
286 | ovl_aio_cleanup_handler(aio_req); | |
287 | orig_iocb->ki_complete(orig_iocb, res, res2); | |
288 | } | |
289 | ||
16914e6f MS |
290 | static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter) |
291 | { | |
292 | struct file *file = iocb->ki_filp; | |
293 | struct fd real; | |
294 | const struct cred *old_cred; | |
295 | ssize_t ret; | |
296 | ||
297 | if (!iov_iter_count(iter)) | |
298 | return 0; | |
299 | ||
300 | ret = ovl_real_fdget(file, &real); | |
301 | if (ret) | |
302 | return ret; | |
303 | ||
304 | old_cred = ovl_override_creds(file_inode(file)->i_sb); | |
2406a307 JX |
305 | if (is_sync_kiocb(iocb)) { |
306 | ret = vfs_iter_read(real.file, iter, &iocb->ki_pos, | |
b778e1ee | 307 | ovl_iocb_to_rwf(iocb->ki_flags)); |
2406a307 JX |
308 | } else { |
309 | struct ovl_aio_req *aio_req; | |
310 | ||
311 | ret = -ENOMEM; | |
312 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); | |
313 | if (!aio_req) | |
314 | goto out; | |
315 | ||
316 | aio_req->fd = real; | |
317 | real.flags = 0; | |
318 | aio_req->orig_iocb = iocb; | |
319 | kiocb_clone(&aio_req->iocb, iocb, real.file); | |
320 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; | |
321 | ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter); | |
322 | if (ret != -EIOCBQUEUED) | |
323 | ovl_aio_cleanup_handler(aio_req); | |
324 | } | |
325 | out: | |
16914e6f | 326 | revert_creds(old_cred); |
16914e6f MS |
327 | ovl_file_accessed(file); |
328 | ||
329 | fdput(real); | |
330 | ||
331 | return ret; | |
332 | } | |
333 | ||
2a92e07e MS |
334 | static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter) |
335 | { | |
336 | struct file *file = iocb->ki_filp; | |
337 | struct inode *inode = file_inode(file); | |
338 | struct fd real; | |
339 | const struct cred *old_cred; | |
340 | ssize_t ret; | |
c86243b0 | 341 | int ifl = iocb->ki_flags; |
2a92e07e MS |
342 | |
343 | if (!iov_iter_count(iter)) | |
344 | return 0; | |
345 | ||
346 | inode_lock(inode); | |
347 | /* Update mode */ | |
348 | ovl_copyattr(ovl_inode_real(inode), inode); | |
349 | ret = file_remove_privs(file); | |
350 | if (ret) | |
351 | goto out_unlock; | |
352 | ||
353 | ret = ovl_real_fdget(file, &real); | |
354 | if (ret) | |
355 | goto out_unlock; | |
356 | ||
c86243b0 VG |
357 | if (!ovl_should_sync(OVL_FS(inode->i_sb))) |
358 | ifl &= ~(IOCB_DSYNC | IOCB_SYNC); | |
359 | ||
2a92e07e | 360 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
2406a307 JX |
361 | if (is_sync_kiocb(iocb)) { |
362 | file_start_write(real.file); | |
363 | ret = vfs_iter_write(real.file, iter, &iocb->ki_pos, | |
c86243b0 | 364 | ovl_iocb_to_rwf(ifl)); |
2406a307 JX |
365 | file_end_write(real.file); |
366 | /* Update size */ | |
367 | ovl_copyattr(ovl_inode_real(inode), inode); | |
368 | } else { | |
369 | struct ovl_aio_req *aio_req; | |
370 | ||
371 | ret = -ENOMEM; | |
372 | aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL); | |
373 | if (!aio_req) | |
374 | goto out; | |
375 | ||
376 | file_start_write(real.file); | |
c8536804 MS |
377 | /* Pacify lockdep, same trick as done in aio_write() */ |
378 | __sb_writers_release(file_inode(real.file)->i_sb, | |
379 | SB_FREEZE_WRITE); | |
2406a307 JX |
380 | aio_req->fd = real; |
381 | real.flags = 0; | |
382 | aio_req->orig_iocb = iocb; | |
383 | kiocb_clone(&aio_req->iocb, iocb, real.file); | |
c86243b0 | 384 | aio_req->iocb.ki_flags = ifl; |
2406a307 JX |
385 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; |
386 | ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter); | |
387 | if (ret != -EIOCBQUEUED) | |
388 | ovl_aio_cleanup_handler(aio_req); | |
389 | } | |
390 | out: | |
2a92e07e | 391 | revert_creds(old_cred); |
2a92e07e MS |
392 | fdput(real); |
393 | ||
394 | out_unlock: | |
395 | inode_unlock(inode); | |
396 | ||
397 | return ret; | |
398 | } | |
399 | ||
de30dfd6 MS |
400 | static 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 | ||
c86243b0 VG |
406 | if (!ovl_should_sync(OVL_FS(file_inode(file)->i_sb))) |
407 | return 0; | |
408 | ||
8c444d2a | 409 | ret = ovl_real_fdget_meta(file, &real, !datasync); |
de30dfd6 MS |
410 | 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 | ||
2f502839 MS |
425 | static 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 | ||
aab8848c MS |
456 | static 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 | ||
b833a366 AG |
479 | static 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 | ||
dab5ca8f MS |
498 | static long ovl_real_ioctl(struct file *file, unsigned int cmd, |
499 | unsigned long arg) | |
500 | { | |
501 | struct fd real; | |
dab5ca8f MS |
502 | long ret; |
503 | ||
504 | ret = ovl_real_fdget(file, &real); | |
505 | if (ret) | |
506 | return ret; | |
507 | ||
292f902a | 508 | ret = security_file_ioctl(real.file, cmd, arg); |
89bdfaf9 MS |
509 | if (!ret) { |
510 | /* | |
511 | * Don't override creds, since we currently can't safely check | |
512 | * permissions before doing so. | |
513 | */ | |
292f902a | 514 | ret = vfs_ioctl(real.file, cmd, arg); |
89bdfaf9 | 515 | } |
dab5ca8f MS |
516 | |
517 | fdput(real); | |
518 | ||
519 | return ret; | |
520 | } | |
521 | ||
b21d9c43 | 522 | static long ovl_ioctl_set_flags(struct file *file, unsigned int cmd, |
89bdfaf9 | 523 | unsigned long arg) |
dab5ca8f MS |
524 | { |
525 | long ret; | |
526 | struct inode *inode = file_inode(file); | |
98487de3 JX |
527 | |
528 | if (!inode_owner_or_capable(inode)) | |
529 | return -EACCES; | |
530 | ||
98487de3 JX |
531 | ret = mnt_want_write_file(file); |
532 | if (ret) | |
533 | return ret; | |
534 | ||
535 | inode_lock(inode); | |
536 | ||
89bdfaf9 MS |
537 | /* |
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)) | |
98487de3 JX |
544 | goto unlock; |
545 | ||
546 | ret = ovl_maybe_copy_up(file_dentry(file), O_WRONLY); | |
547 | if (ret) | |
548 | goto unlock; | |
549 | ||
b21d9c43 | 550 | ret = ovl_real_ioctl(file, cmd, arg); |
98487de3 JX |
551 | |
552 | ovl_copyflags(ovl_inode_real(inode), inode); | |
553 | unlock: | |
554 | inode_unlock(inode); | |
555 | ||
556 | mnt_drop_write_file(file); | |
557 | ||
558 | return ret; | |
559 | ||
560 | } | |
561 | ||
61536bed | 562 | long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
98487de3 JX |
563 | { |
564 | long ret; | |
dab5ca8f MS |
565 | |
566 | switch (cmd) { | |
567 | case FS_IOC_GETFLAGS: | |
b21d9c43 | 568 | case FS_IOC_FSGETXATTR: |
dab5ca8f MS |
569 | ret = ovl_real_ioctl(file, cmd, arg); |
570 | break; | |
571 | ||
b21d9c43 | 572 | case FS_IOC_FSSETXATTR: |
89bdfaf9 MS |
573 | case FS_IOC_SETFLAGS: |
574 | ret = ovl_ioctl_set_flags(file, cmd, arg); | |
dab5ca8f MS |
575 | break; |
576 | ||
577 | default: | |
578 | ret = -ENOTTY; | |
579 | } | |
580 | ||
581 | return ret; | |
582 | } | |
583 | ||
61536bed AG |
584 | #ifdef CONFIG_COMPAT |
585 | long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | |
dab5ca8f MS |
586 | { |
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 | } | |
61536bed | 602 | #endif |
dab5ca8f | 603 | |
8ede2055 MS |
604 | enum ovl_copyop { |
605 | OVL_COPY, | |
606 | OVL_CLONE, | |
607 | OVL_DEDUPE, | |
608 | }; | |
609 | ||
42ec3d4c | 610 | static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, |
8ede2055 | 611 | struct file *file_out, loff_t pos_out, |
42ec3d4c | 612 | loff_t len, unsigned int flags, enum ovl_copyop op) |
8ede2055 MS |
613 | { |
614 | struct inode *inode_out = file_inode(file_out); | |
615 | struct fd real_in, real_out; | |
616 | const struct cred *old_cred; | |
42ec3d4c | 617 | loff_t ret; |
8ede2055 MS |
618 | |
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: | |
a725356b | 637 | ret = vfs_clone_file_range(real_in.file, pos_in, |
452ce659 | 638 | real_out.file, pos_out, len, flags); |
8ede2055 MS |
639 | break; |
640 | ||
641 | case OVL_DEDUPE: | |
642 | ret = vfs_dedupe_file_range_one(real_in.file, pos_in, | |
df365836 DW |
643 | real_out.file, pos_out, len, |
644 | flags); | |
8ede2055 MS |
645 | 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 | ||
658 | static 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 | ||
42ec3d4c DW |
666 | static 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) | |
8ede2055 | 669 | { |
2e5dfc99 DW |
670 | enum ovl_copyop op; |
671 | ||
672 | 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; | |
8ede2055 | 679 | |
8ede2055 MS |
680 | /* |
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 | */ | |
2e5dfc99 DW |
684 | if (op == OVL_DEDUPE && |
685 | (!ovl_inode_upper(file_inode(file_in)) || | |
686 | !ovl_inode_upper(file_inode(file_out)))) | |
8ede2055 MS |
687 | return -EPERM; |
688 | ||
452ce659 DW |
689 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, |
690 | remap_flags, op); | |
8ede2055 MS |
691 | } |
692 | ||
d1d04ef8 MS |
693 | const struct file_operations ovl_file_operations = { |
694 | .open = ovl_open, | |
695 | .release = ovl_release, | |
696 | .llseek = ovl_llseek, | |
16914e6f | 697 | .read_iter = ovl_read_iter, |
2a92e07e | 698 | .write_iter = ovl_write_iter, |
de30dfd6 | 699 | .fsync = ovl_fsync, |
2f502839 | 700 | .mmap = ovl_mmap, |
aab8848c | 701 | .fallocate = ovl_fallocate, |
b833a366 | 702 | .fadvise = ovl_fadvise, |
dab5ca8f | 703 | .unlocked_ioctl = ovl_ioctl, |
61536bed | 704 | #ifdef CONFIG_COMPAT |
dab5ca8f | 705 | .compat_ioctl = ovl_compat_ioctl, |
61536bed | 706 | #endif |
82a763e6 MS |
707 | .splice_read = generic_file_splice_read, |
708 | .splice_write = iter_file_splice_write, | |
8ede2055 MS |
709 | |
710 | .copy_file_range = ovl_copy_file_range, | |
2e5dfc99 | 711 | .remap_file_range = ovl_remap_file_range, |
d1d04ef8 | 712 | }; |
2406a307 JX |
713 | |
714 | int __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 | ||
725 | void ovl_aio_request_cache_destroy(void) | |
726 | { | |
727 | kmem_cache_destroy(ovl_aio_request_cachep); | |
728 | } |