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; | |
9a254403 | 20 | refcount_t ref; |
2406a307 JX |
21 | struct kiocb *orig_iocb; |
22 | struct fd fd; | |
23 | }; | |
24 | ||
25 | static struct kmem_cache *ovl_aio_request_cachep; | |
26 | ||
8c444d2a VG |
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 | ||
cdf5c9d1 | 37 | /* No atime modification nor notify on underlying */ |
81a33c1e AG |
38 | #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY) |
39 | ||
8c444d2a | 40 | static struct file *ovl_open_realfile(const struct file *file, |
2d343087 | 41 | const struct path *realpath) |
d1d04ef8 | 42 | { |
1248ea4b | 43 | struct inode *realinode = d_inode(realpath->dentry); |
d1d04ef8 | 44 | struct inode *inode = file_inode(file); |
4609e1f1 | 45 | struct mnt_idmap *real_idmap; |
d1d04ef8 MS |
46 | struct file *realfile; |
47 | const struct cred *old_cred; | |
81a33c1e | 48 | int flags = file->f_flags | OVL_OPEN_FLAGS; |
05acefb4 MS |
49 | int acc_mode = ACC_MODE(flags); |
50 | int err; | |
51 | ||
52 | if (flags & O_APPEND) | |
53 | acc_mode |= MAY_APPEND; | |
d1d04ef8 MS |
54 | |
55 | old_cred = ovl_override_creds(inode->i_sb); | |
4609e1f1 | 56 | real_idmap = mnt_idmap(realpath->mnt); |
4609e1f1 | 57 | err = inode_permission(real_idmap, realinode, MAY_OPEN | acc_mode); |
05acefb4 MS |
58 | if (err) { |
59 | realfile = ERR_PTR(err); | |
05acefb4 | 60 | } else { |
01beba79 | 61 | if (!inode_owner_or_capable(real_idmap, realinode)) |
b6650dab MS |
62 | flags &= ~O_NOATIME; |
63 | ||
05acefb4 MS |
64 | realfile = open_with_fake_path(&file->f_path, flags, realinode, |
65 | current_cred()); | |
66 | } | |
d1d04ef8 MS |
67 | revert_creds(old_cred); |
68 | ||
69 | pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n", | |
8c444d2a | 70 | file, file, ovl_whatisit(inode, realinode), file->f_flags, |
d1d04ef8 MS |
71 | realfile, IS_ERR(realfile) ? 0 : realfile->f_flags); |
72 | ||
73 | return realfile; | |
74 | } | |
75 | ||
2ef66b8a MS |
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 | ||
2ef66b8a MS |
83 | flags &= OVL_SETFL_MASK; |
84 | ||
85 | if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode)) | |
86 | return -EPERM; | |
87 | ||
a2ad63da N |
88 | if ((flags & O_DIRECT) && !(file->f_mode & FMODE_CAN_ODIRECT)) |
89 | return -EINVAL; | |
2ef66b8a MS |
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; | |
456b59e7 | 99 | file->f_iocb_flags = iocb_flags(file); |
2ef66b8a MS |
100 | spin_unlock(&file->f_lock); |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
8c444d2a VG |
105 | static int ovl_real_fdget_meta(const struct file *file, struct fd *real, |
106 | bool allow_meta) | |
2ef66b8a | 107 | { |
1248ea4b AG |
108 | struct dentry *dentry = file_dentry(file); |
109 | struct path realpath; | |
2ef66b8a MS |
110 | |
111 | real->flags = 0; | |
112 | real->file = file->private_data; | |
113 | ||
8c444d2a | 114 | if (allow_meta) |
1248ea4b | 115 | ovl_path_real(dentry, &realpath); |
8c444d2a | 116 | else |
1248ea4b | 117 | ovl_path_realdata(dentry, &realpath); |
8c444d2a | 118 | |
2ef66b8a | 119 | /* Has it been copied up since we'd opened it? */ |
1248ea4b | 120 | if (unlikely(file_inode(real->file) != d_inode(realpath.dentry))) { |
2ef66b8a | 121 | real->flags = FDPUT_FPUT; |
1248ea4b | 122 | real->file = ovl_open_realfile(file, &realpath); |
2ef66b8a MS |
123 | |
124 | return PTR_ERR_OR_ZERO(real->file); | |
125 | } | |
126 | ||
127 | /* Did the flags change since open? */ | |
81a33c1e | 128 | if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS)) |
2ef66b8a MS |
129 | return ovl_change_flags(real->file, file->f_flags); |
130 | ||
131 | return 0; | |
132 | } | |
133 | ||
8c444d2a VG |
134 | static int ovl_real_fdget(const struct file *file, struct fd *real) |
135 | { | |
61536bed AG |
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 | ||
8c444d2a VG |
143 | return ovl_real_fdget_meta(file, real, false); |
144 | } | |
145 | ||
d1d04ef8 MS |
146 | static int ovl_open(struct inode *inode, struct file *file) |
147 | { | |
1248ea4b | 148 | struct dentry *dentry = file_dentry(file); |
d1d04ef8 | 149 | struct file *realfile; |
1248ea4b | 150 | struct path realpath; |
d1d04ef8 MS |
151 | int err; |
152 | ||
1248ea4b | 153 | err = ovl_maybe_copy_up(dentry, file->f_flags); |
d1d04ef8 MS |
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 | ||
1248ea4b AG |
160 | ovl_path_realdata(dentry, &realpath); |
161 | realfile = ovl_open_realfile(file, &realpath); | |
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 | ||
9a254403 | 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 | ||
2406a307 JX |
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 | ||
c8536804 MS |
276 | /* Actually acquired in ovl_write_iter() */ |
277 | __sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb, | |
278 | SB_FREEZE_WRITE); | |
2406a307 | 279 | file_end_write(iocb->ki_filp); |
2878dffc | 280 | ovl_copyattr(inode); |
2406a307 JX |
281 | } |
282 | ||
283 | orig_iocb->ki_pos = iocb->ki_pos; | |
9a254403 | 284 | ovl_aio_put(aio_req); |
2406a307 JX |
285 | } |
286 | ||
6b19b766 | 287 | static void ovl_aio_rw_complete(struct kiocb *iocb, long res) |
2406a307 JX |
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); | |
6b19b766 | 294 | orig_iocb->ki_complete(orig_iocb, res); |
2406a307 JX |
295 | } |
296 | ||
16914e6f MS |
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 | ||
1dc1eed4 MS |
311 | ret = -EINVAL; |
312 | if (iocb->ki_flags & IOCB_DIRECT && | |
a2ad63da | 313 | !(real.file->f_mode & FMODE_CAN_ODIRECT)) |
1dc1eed4 MS |
314 | goto out_fdput; |
315 | ||
16914e6f | 316 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
2406a307 JX |
317 | if (is_sync_kiocb(iocb)) { |
318 | ret = vfs_iter_read(real.file, iter, &iocb->ki_pos, | |
b778e1ee | 319 | ovl_iocb_to_rwf(iocb->ki_flags)); |
2406a307 JX |
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; | |
9a254403 | 333 | refcount_set(&aio_req->ref, 2); |
2406a307 | 334 | ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter); |
9a254403 | 335 | ovl_aio_put(aio_req); |
2406a307 JX |
336 | if (ret != -EIOCBQUEUED) |
337 | ovl_aio_cleanup_handler(aio_req); | |
338 | } | |
339 | out: | |
16914e6f | 340 | revert_creds(old_cred); |
16914e6f | 341 | ovl_file_accessed(file); |
1dc1eed4 | 342 | out_fdput: |
16914e6f MS |
343 | fdput(real); |
344 | ||
345 | return ret; | |
346 | } | |
347 | ||
2a92e07e MS |
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; | |
c86243b0 | 355 | int ifl = iocb->ki_flags; |
2a92e07e MS |
356 | |
357 | if (!iov_iter_count(iter)) | |
358 | return 0; | |
359 | ||
360 | inode_lock(inode); | |
361 | /* Update mode */ | |
2878dffc | 362 | ovl_copyattr(inode); |
2a92e07e MS |
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 | ||
1dc1eed4 MS |
371 | ret = -EINVAL; |
372 | if (iocb->ki_flags & IOCB_DIRECT && | |
a2ad63da | 373 | !(real.file->f_mode & FMODE_CAN_ODIRECT)) |
1dc1eed4 MS |
374 | goto out_fdput; |
375 | ||
c86243b0 VG |
376 | if (!ovl_should_sync(OVL_FS(inode->i_sb))) |
377 | ifl &= ~(IOCB_DSYNC | IOCB_SYNC); | |
378 | ||
2a92e07e | 379 | old_cred = ovl_override_creds(file_inode(file)->i_sb); |
2406a307 JX |
380 | if (is_sync_kiocb(iocb)) { |
381 | file_start_write(real.file); | |
382 | ret = vfs_iter_write(real.file, iter, &iocb->ki_pos, | |
c86243b0 | 383 | ovl_iocb_to_rwf(ifl)); |
2406a307 JX |
384 | file_end_write(real.file); |
385 | /* Update size */ | |
2878dffc | 386 | ovl_copyattr(inode); |
2406a307 JX |
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); | |
c8536804 MS |
396 | /* Pacify lockdep, same trick as done in aio_write() */ |
397 | __sb_writers_release(file_inode(real.file)->i_sb, | |
398 | SB_FREEZE_WRITE); | |
2406a307 JX |
399 | aio_req->fd = real; |
400 | real.flags = 0; | |
401 | aio_req->orig_iocb = iocb; | |
402 | kiocb_clone(&aio_req->iocb, iocb, real.file); | |
c86243b0 | 403 | aio_req->iocb.ki_flags = ifl; |
2406a307 | 404 | aio_req->iocb.ki_complete = ovl_aio_rw_complete; |
9a254403 | 405 | refcount_set(&aio_req->ref, 2); |
2406a307 | 406 | ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter); |
9a254403 | 407 | ovl_aio_put(aio_req); |
2406a307 JX |
408 | if (ret != -EIOCBQUEUED) |
409 | ovl_aio_cleanup_handler(aio_req); | |
410 | } | |
411 | out: | |
2a92e07e | 412 | revert_creds(old_cred); |
1dc1eed4 | 413 | out_fdput: |
2a92e07e MS |
414 | fdput(real); |
415 | ||
416 | out_unlock: | |
417 | inode_unlock(inode); | |
418 | ||
419 | return ret; | |
420 | } | |
421 | ||
d4120d87 DH |
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 | ||
9b91b6b0 MS |
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); | |
9b91b6b0 MS |
457 | ssize_t ret; |
458 | ||
459 | inode_lock(inode); | |
460 | /* Update mode */ | |
2878dffc | 461 | ovl_copyattr(inode); |
9b91b6b0 MS |
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 */ | |
2878dffc | 477 | ovl_copyattr(inode); |
9b91b6b0 MS |
478 | revert_creds(old_cred); |
479 | fdput(real); | |
480 | ||
481 | out_unlock: | |
482 | inode_unlock(inode); | |
483 | ||
484 | return ret; | |
485 | } | |
486 | ||
de30dfd6 MS |
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 | ||
335d3fc5 SD |
493 | ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb)); |
494 | if (ret <= 0) | |
495 | return ret; | |
c86243b0 | 496 | |
8c444d2a | 497 | ret = ovl_real_fdget_meta(file, &real, !datasync); |
de30dfd6 MS |
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 | ||
2f502839 MS |
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 | ||
2896900e | 525 | vma_set_file(vma, realfile); |
2f502839 MS |
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); | |
2f502839 MS |
530 | ovl_file_accessed(file); |
531 | ||
532 | return ret; | |
533 | } | |
534 | ||
aab8848c MS |
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 | ||
23a8ce16 AG |
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 | ||
aab8848c MS |
549 | ret = ovl_real_fdget(file, &real); |
550 | if (ret) | |
23a8ce16 | 551 | goto out_unlock; |
aab8848c MS |
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 */ | |
2878dffc | 558 | ovl_copyattr(inode); |
aab8848c MS |
559 | |
560 | fdput(real); | |
561 | ||
23a8ce16 AG |
562 | out_unlock: |
563 | inode_unlock(inode); | |
564 | ||
aab8848c MS |
565 | return ret; |
566 | } | |
567 | ||
b833a366 AG |
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 | ||
8ede2055 MS |
587 | enum ovl_copyop { |
588 | OVL_COPY, | |
589 | OVL_CLONE, | |
590 | OVL_DEDUPE, | |
591 | }; | |
592 | ||
42ec3d4c | 593 | static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in, |
8ede2055 | 594 | struct file *file_out, loff_t pos_out, |
42ec3d4c | 595 | loff_t len, unsigned int flags, enum ovl_copyop op) |
8ede2055 MS |
596 | { |
597 | struct inode *inode_out = file_inode(file_out); | |
598 | struct fd real_in, real_out; | |
599 | const struct cred *old_cred; | |
42ec3d4c | 600 | loff_t ret; |
8ede2055 | 601 | |
b306e90f AG |
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 | ||
8ede2055 MS |
611 | ret = ovl_real_fdget(file_out, &real_out); |
612 | if (ret) | |
b306e90f | 613 | goto out_unlock; |
8ede2055 MS |
614 | |
615 | ret = ovl_real_fdget(file_in, &real_in); | |
616 | if (ret) { | |
617 | fdput(real_out); | |
b306e90f | 618 | goto out_unlock; |
8ede2055 MS |
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: | |
a725356b | 629 | ret = vfs_clone_file_range(real_in.file, pos_in, |
452ce659 | 630 | real_out.file, pos_out, len, flags); |
8ede2055 MS |
631 | break; |
632 | ||
633 | case OVL_DEDUPE: | |
634 | ret = vfs_dedupe_file_range_one(real_in.file, pos_in, | |
df365836 DW |
635 | real_out.file, pos_out, len, |
636 | flags); | |
8ede2055 MS |
637 | break; |
638 | } | |
639 | revert_creds(old_cred); | |
640 | ||
641 | /* Update size */ | |
2878dffc | 642 | ovl_copyattr(inode_out); |
8ede2055 MS |
643 | |
644 | fdput(real_in); | |
645 | fdput(real_out); | |
646 | ||
b306e90f AG |
647 | out_unlock: |
648 | inode_unlock(inode_out); | |
649 | ||
8ede2055 MS |
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 | ||
42ec3d4c DW |
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) | |
8ede2055 | 664 | { |
2e5dfc99 DW |
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; | |
8ede2055 | 674 | |
8ede2055 MS |
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 | */ | |
2e5dfc99 DW |
679 | if (op == OVL_DEDUPE && |
680 | (!ovl_inode_upper(file_inode(file_in)) || | |
681 | !ovl_inode_upper(file_inode(file_out)))) | |
8ede2055 MS |
682 | return -EPERM; |
683 | ||
452ce659 DW |
684 | return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, |
685 | remap_flags, op); | |
8ede2055 MS |
686 | } |
687 | ||
1f0cb8bc SD |
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 | ||
d1d04ef8 MS |
708 | const struct file_operations ovl_file_operations = { |
709 | .open = ovl_open, | |
710 | .release = ovl_release, | |
711 | .llseek = ovl_llseek, | |
16914e6f | 712 | .read_iter = ovl_read_iter, |
2a92e07e | 713 | .write_iter = ovl_write_iter, |
de30dfd6 | 714 | .fsync = ovl_fsync, |
2f502839 | 715 | .mmap = ovl_mmap, |
aab8848c | 716 | .fallocate = ovl_fallocate, |
b833a366 | 717 | .fadvise = ovl_fadvise, |
1f0cb8bc | 718 | .flush = ovl_flush, |
d4120d87 | 719 | .splice_read = ovl_splice_read, |
9b91b6b0 | 720 | .splice_write = ovl_splice_write, |
8ede2055 MS |
721 | |
722 | .copy_file_range = ovl_copy_file_range, | |
2e5dfc99 | 723 | .remap_file_range = ovl_remap_file_range, |
d1d04ef8 | 724 | }; |
2406a307 JX |
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 | } |