• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) 2017 Red Hat, Inc.
4  */
5 
6 #include <linux/cred.h>
7 #include <linux/file.h>
8 #include <linux/mount.h>
9 #include <linux/xattr.h>
10 #include <linux/uio.h>
11 #include <linux/uaccess.h>
12 #include <linux/splice.h>
13 #include <linux/security.h>
14 #include <linux/mm.h>
15 #include <linux/fs.h>
16 #include "overlayfs.h"
17 
18 #define OVL_IOCB_MASK (IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC)
19 
20 struct ovl_aio_req {
21 	struct kiocb iocb;
22 	refcount_t ref;
23 	struct kiocb *orig_iocb;
24 };
25 
26 static struct kmem_cache *ovl_aio_request_cachep;
27 
ovl_whatisit(struct inode * inode,struct inode * realinode)28 static char ovl_whatisit(struct inode *inode, struct inode *realinode)
29 {
30 	if (realinode != ovl_inode_upper(inode))
31 		return 'l';
32 	if (ovl_has_upperdata(inode))
33 		return 'u';
34 	else
35 		return 'm';
36 }
37 
38 /* No atime modificaton nor notify on underlying */
39 #define OVL_OPEN_FLAGS (O_NOATIME | FMODE_NONOTIFY)
40 
ovl_open_realfile(const struct file * file,struct inode * realinode)41 static struct file *ovl_open_realfile(const struct file *file,
42 				      struct inode *realinode)
43 {
44 	struct inode *inode = file_inode(file);
45 	struct file *realfile;
46 	const struct cred *old_cred;
47 	int flags = file->f_flags | OVL_OPEN_FLAGS;
48 	int acc_mode = ACC_MODE(flags);
49 	int err;
50 
51 	if (flags & O_APPEND)
52 		acc_mode |= MAY_APPEND;
53 
54 	old_cred = ovl_override_creds(inode->i_sb);
55 	err = inode_permission(&init_user_ns, realinode, MAY_OPEN | acc_mode);
56 	if (err) {
57 		realfile = ERR_PTR(err);
58 	} else {
59 		if (old_cred && !inode_owner_or_capable(&init_user_ns,
60 							realinode))
61 			flags &= ~O_NOATIME;
62 
63 		realfile = open_with_fake_path(&file->f_path, flags, realinode,
64 					       current_cred());
65 	}
66 	ovl_revert_creds(inode->i_sb, old_cred);
67 
68 	pr_debug("open(%p[%pD2/%c], 0%o) -> (%p, 0%o)\n",
69 		 file, file, ovl_whatisit(inode, realinode), file->f_flags,
70 		 realfile, IS_ERR(realfile) ? 0 : realfile->f_flags);
71 
72 	return realfile;
73 }
74 
75 #define OVL_SETFL_MASK (O_APPEND | O_NONBLOCK | O_NDELAY | O_DIRECT)
76 
ovl_change_flags(struct file * file,unsigned int flags)77 static int ovl_change_flags(struct file *file, unsigned int flags)
78 {
79 	struct inode *inode = file_inode(file);
80 	int err;
81 
82 	flags &= OVL_SETFL_MASK;
83 
84 	if (((flags ^ file->f_flags) & O_APPEND) && IS_APPEND(inode))
85 		return -EPERM;
86 
87 	if (flags & O_DIRECT) {
88 		if (!file->f_mapping->a_ops ||
89 		    !file->f_mapping->a_ops->direct_IO)
90 			return -EINVAL;
91 	}
92 
93 	if (file->f_op->check_flags) {
94 		err = file->f_op->check_flags(flags);
95 		if (err)
96 			return err;
97 	}
98 
99 	spin_lock(&file->f_lock);
100 	file->f_flags = (file->f_flags & ~OVL_SETFL_MASK) | flags;
101 	spin_unlock(&file->f_lock);
102 
103 	return 0;
104 }
105 
ovl_real_fdget_meta(const struct file * file,struct fd * real,bool allow_meta)106 static int ovl_real_fdget_meta(const struct file *file, struct fd *real,
107 			       bool allow_meta)
108 {
109 	struct inode *inode = file_inode(file);
110 	struct inode *realinode;
111 
112 	real->flags = 0;
113 	real->file = file->private_data;
114 
115 	if (allow_meta)
116 		realinode = ovl_inode_real(inode);
117 	else
118 		realinode = ovl_inode_realdata(inode);
119 
120 	/* Has it been copied up since we'd opened it? */
121 	if (unlikely(file_inode(real->file) != realinode)) {
122 		real->flags = FDPUT_FPUT;
123 		real->file = ovl_open_realfile(file, realinode);
124 
125 		return PTR_ERR_OR_ZERO(real->file);
126 	}
127 
128 	/* Did the flags change since open? */
129 	if (unlikely((file->f_flags ^ real->file->f_flags) & ~OVL_OPEN_FLAGS))
130 		return ovl_change_flags(real->file, file->f_flags);
131 
132 	return 0;
133 }
134 
ovl_real_fdget(const struct file * file,struct fd * real)135 static int ovl_real_fdget(const struct file *file, struct fd *real)
136 {
137 	if (d_is_dir(file_dentry(file))) {
138 		real->flags = 0;
139 		real->file = ovl_dir_real_file(file, false);
140 
141 		return PTR_ERR_OR_ZERO(real->file);
142 	}
143 
144 	return ovl_real_fdget_meta(file, real, false);
145 }
146 
ovl_open(struct inode * inode,struct file * file)147 static int ovl_open(struct inode *inode, struct file *file)
148 {
149 	struct file *realfile;
150 	int err;
151 
152 	err = ovl_maybe_copy_up(file_dentry(file), file->f_flags);
153 	if (err)
154 		return err;
155 
156 	/* No longer need these flags, so don't pass them on to underlying fs */
157 	file->f_flags &= ~(O_CREAT | O_EXCL | O_NOCTTY | O_TRUNC);
158 
159 	realfile = ovl_open_realfile(file, ovl_inode_realdata(inode));
160 	if (IS_ERR(realfile))
161 		return PTR_ERR(realfile);
162 
163 	file->private_data = realfile;
164 
165 	return 0;
166 }
167 
ovl_release(struct inode * inode,struct file * file)168 static int ovl_release(struct inode *inode, struct file *file)
169 {
170 	fput(file->private_data);
171 
172 	return 0;
173 }
174 
ovl_llseek(struct file * file,loff_t offset,int whence)175 static loff_t ovl_llseek(struct file *file, loff_t offset, int whence)
176 {
177 	struct inode *inode = file_inode(file);
178 	struct fd real;
179 	const struct cred *old_cred;
180 	loff_t ret;
181 
182 	/*
183 	 * The two special cases below do not need to involve real fs,
184 	 * so we can optimizing concurrent callers.
185 	 */
186 	if (offset == 0) {
187 		if (whence == SEEK_CUR)
188 			return file->f_pos;
189 
190 		if (whence == SEEK_SET)
191 			return vfs_setpos(file, 0, 0);
192 	}
193 
194 	ret = ovl_real_fdget(file, &real);
195 	if (ret)
196 		return ret;
197 
198 	/*
199 	 * Overlay file f_pos is the master copy that is preserved
200 	 * through copy up and modified on read/write, but only real
201 	 * fs knows how to SEEK_HOLE/SEEK_DATA and real fs may impose
202 	 * limitations that are more strict than ->s_maxbytes for specific
203 	 * files, so we use the real file to perform seeks.
204 	 */
205 	ovl_inode_lock(inode);
206 	real.file->f_pos = file->f_pos;
207 
208 	old_cred = ovl_override_creds(inode->i_sb);
209 	ret = vfs_llseek(real.file, offset, whence);
210 	ovl_revert_creds(inode->i_sb, old_cred);
211 
212 	file->f_pos = real.file->f_pos;
213 	ovl_inode_unlock(inode);
214 
215 	fdput(real);
216 
217 	return ret;
218 }
219 
ovl_file_accessed(struct file * file)220 static void ovl_file_accessed(struct file *file)
221 {
222 	struct inode *inode, *upperinode;
223 
224 	if (file->f_flags & O_NOATIME)
225 		return;
226 
227 	inode = file_inode(file);
228 	upperinode = ovl_inode_upper(inode);
229 
230 	if (!upperinode)
231 		return;
232 
233 	if ((!timespec64_equal(&inode->i_mtime, &upperinode->i_mtime) ||
234 	     !timespec64_equal(&inode->i_ctime, &upperinode->i_ctime))) {
235 		inode->i_mtime = upperinode->i_mtime;
236 		inode->i_ctime = upperinode->i_ctime;
237 	}
238 
239 	touch_atime(&file->f_path);
240 }
241 
ovl_aio_put(struct ovl_aio_req * aio_req)242 static inline void ovl_aio_put(struct ovl_aio_req *aio_req)
243 {
244 	if (refcount_dec_and_test(&aio_req->ref)) {
245 		fput(aio_req->iocb.ki_filp);
246 		kmem_cache_free(ovl_aio_request_cachep, aio_req);
247 	}
248 }
249 
ovl_aio_cleanup_handler(struct ovl_aio_req * aio_req)250 static void ovl_aio_cleanup_handler(struct ovl_aio_req *aio_req)
251 {
252 	struct kiocb *iocb = &aio_req->iocb;
253 	struct kiocb *orig_iocb = aio_req->orig_iocb;
254 
255 	if (iocb->ki_flags & IOCB_WRITE) {
256 		struct inode *inode = file_inode(orig_iocb->ki_filp);
257 
258 		/* Actually acquired in ovl_write_iter() */
259 		__sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
260 				      SB_FREEZE_WRITE);
261 		file_end_write(iocb->ki_filp);
262 		ovl_copyattr(inode);
263 	}
264 
265 	orig_iocb->ki_pos = iocb->ki_pos;
266 	ovl_aio_put(aio_req);
267 }
268 
ovl_aio_rw_complete(struct kiocb * iocb,long res,long res2)269 static void ovl_aio_rw_complete(struct kiocb *iocb, long res, long res2)
270 {
271 	struct ovl_aio_req *aio_req = container_of(iocb,
272 						   struct ovl_aio_req, iocb);
273 	struct kiocb *orig_iocb = aio_req->orig_iocb;
274 
275 	ovl_aio_cleanup_handler(aio_req);
276 	orig_iocb->ki_complete(orig_iocb, res, res2);
277 }
278 
ovl_read_iter(struct kiocb * iocb,struct iov_iter * iter)279 static ssize_t ovl_read_iter(struct kiocb *iocb, struct iov_iter *iter)
280 {
281 	struct file *file = iocb->ki_filp;
282 	struct fd real;
283 	const struct cred *old_cred;
284 	ssize_t ret;
285 
286 	if (!iov_iter_count(iter))
287 		return 0;
288 
289 	ret = ovl_real_fdget(file, &real);
290 	if (ret)
291 		return ret;
292 
293 	ret = -EINVAL;
294 	if (iocb->ki_flags & IOCB_DIRECT &&
295 	    (!real.file->f_mapping->a_ops ||
296 	     !real.file->f_mapping->a_ops->direct_IO))
297 		goto out_fdput;
298 
299 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
300 	if (is_sync_kiocb(iocb)) {
301 		ret = vfs_iter_read(real.file, iter, &iocb->ki_pos,
302 				    iocb_to_rw_flags(iocb->ki_flags,
303 						     OVL_IOCB_MASK));
304 	} else {
305 		struct ovl_aio_req *aio_req;
306 
307 		ret = -ENOMEM;
308 		aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
309 		if (!aio_req)
310 			goto out;
311 
312 		real.flags = 0;
313 		aio_req->orig_iocb = iocb;
314 		kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
315 		aio_req->iocb.ki_complete = ovl_aio_rw_complete;
316 		refcount_set(&aio_req->ref, 2);
317 		ret = vfs_iocb_iter_read(real.file, &aio_req->iocb, iter);
318 		ovl_aio_put(aio_req);
319 		if (ret != -EIOCBQUEUED)
320 			ovl_aio_cleanup_handler(aio_req);
321 	}
322 out:
323 	ovl_revert_creds(file_inode(file)->i_sb, old_cred);
324 	ovl_file_accessed(file);
325 out_fdput:
326 	fdput(real);
327 
328 	return ret;
329 }
330 
ovl_write_iter(struct kiocb * iocb,struct iov_iter * iter)331 static ssize_t ovl_write_iter(struct kiocb *iocb, struct iov_iter *iter)
332 {
333 	struct file *file = iocb->ki_filp;
334 	struct inode *inode = file_inode(file);
335 	struct fd real;
336 	const struct cred *old_cred;
337 	ssize_t ret;
338 	int ifl = iocb->ki_flags;
339 
340 	if (!iov_iter_count(iter))
341 		return 0;
342 
343 	inode_lock(inode);
344 	/* Update mode */
345 	ovl_copyattr(inode);
346 	ret = file_remove_privs(file);
347 	if (ret)
348 		goto out_unlock;
349 
350 	ret = ovl_real_fdget(file, &real);
351 	if (ret)
352 		goto out_unlock;
353 
354 	ret = -EINVAL;
355 	if (iocb->ki_flags & IOCB_DIRECT &&
356 	    (!real.file->f_mapping->a_ops ||
357 	     !real.file->f_mapping->a_ops->direct_IO))
358 		goto out_fdput;
359 
360 	if (!ovl_should_sync(OVL_FS(inode->i_sb)))
361 		ifl &= ~(IOCB_DSYNC | IOCB_SYNC);
362 
363 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
364 	if (is_sync_kiocb(iocb)) {
365 		file_start_write(real.file);
366 		ret = vfs_iter_write(real.file, iter, &iocb->ki_pos,
367 				     iocb_to_rw_flags(ifl, OVL_IOCB_MASK));
368 		file_end_write(real.file);
369 		/* Update size */
370 		ovl_copyattr(inode);
371 	} else {
372 		struct ovl_aio_req *aio_req;
373 
374 		ret = -ENOMEM;
375 		aio_req = kmem_cache_zalloc(ovl_aio_request_cachep, GFP_KERNEL);
376 		if (!aio_req)
377 			goto out;
378 
379 		file_start_write(real.file);
380 		/* Pacify lockdep, same trick as done in aio_write() */
381 		__sb_writers_release(file_inode(real.file)->i_sb,
382 				     SB_FREEZE_WRITE);
383 		real.flags = 0;
384 		aio_req->orig_iocb = iocb;
385 		kiocb_clone(&aio_req->iocb, iocb, get_file(real.file));
386 		aio_req->iocb.ki_flags = ifl;
387 		aio_req->iocb.ki_complete = ovl_aio_rw_complete;
388 		refcount_set(&aio_req->ref, 2);
389 		ret = vfs_iocb_iter_write(real.file, &aio_req->iocb, iter);
390 		ovl_aio_put(aio_req);
391 		if (ret != -EIOCBQUEUED)
392 			ovl_aio_cleanup_handler(aio_req);
393 	}
394 out:
395 	ovl_revert_creds(file_inode(file)->i_sb, old_cred);
396 out_fdput:
397 	fdput(real);
398 
399 out_unlock:
400 	inode_unlock(inode);
401 
402 	return ret;
403 }
404 
405 /*
406  * Calling iter_file_splice_write() directly from overlay's f_op may deadlock
407  * due to lock order inversion between pipe->mutex in iter_file_splice_write()
408  * and file_start_write(real.file) in ovl_write_iter().
409  *
410  * So do everything ovl_write_iter() does and call iter_file_splice_write() on
411  * the real file.
412  */
ovl_splice_write(struct pipe_inode_info * pipe,struct file * out,loff_t * ppos,size_t len,unsigned int flags)413 static ssize_t ovl_splice_write(struct pipe_inode_info *pipe, struct file *out,
414 				loff_t *ppos, size_t len, unsigned int flags)
415 {
416 	struct fd real;
417 	const struct cred *old_cred;
418 	struct inode *inode = file_inode(out);
419 	ssize_t ret;
420 
421 	inode_lock(inode);
422 	/* Update mode */
423 	ovl_copyattr(inode);
424 	ret = file_remove_privs(out);
425 	if (ret)
426 		goto out_unlock;
427 
428 	ret = ovl_real_fdget(out, &real);
429 	if (ret)
430 		goto out_unlock;
431 
432 	old_cred = ovl_override_creds(inode->i_sb);
433 	file_start_write(real.file);
434 
435 	ret = iter_file_splice_write(pipe, real.file, ppos, len, flags);
436 
437 	file_end_write(real.file);
438 	/* Update size */
439 	ovl_copyattr(inode);
440 	ovl_revert_creds(inode->i_sb, old_cred);
441 	fdput(real);
442 
443 out_unlock:
444 	inode_unlock(inode);
445 
446 	return ret;
447 }
448 
ovl_fsync(struct file * file,loff_t start,loff_t end,int datasync)449 static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
450 {
451 	struct fd real;
452 	const struct cred *old_cred;
453 	int ret;
454 
455 	ret = ovl_sync_status(OVL_FS(file_inode(file)->i_sb));
456 	if (ret <= 0)
457 		return ret;
458 
459 	ret = ovl_real_fdget_meta(file, &real, !datasync);
460 	if (ret)
461 		return ret;
462 
463 	/* Don't sync lower file for fear of receiving EROFS error */
464 	if (file_inode(real.file) == ovl_inode_upper(file_inode(file))) {
465 		old_cred = ovl_override_creds(file_inode(file)->i_sb);
466 		ret = vfs_fsync_range(real.file, start, end, datasync);
467 		ovl_revert_creds(file_inode(file)->i_sb, old_cred);
468 	}
469 
470 	fdput(real);
471 
472 	return ret;
473 }
474 
ovl_mmap(struct file * file,struct vm_area_struct * vma)475 static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
476 {
477 	struct file *realfile = file->private_data;
478 	const struct cred *old_cred;
479 	int ret;
480 
481 	if (!realfile->f_op->mmap)
482 		return -ENODEV;
483 
484 	if (WARN_ON(file != vma->vm_file))
485 		return -EIO;
486 
487 	vma_set_file(vma, realfile);
488 
489 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
490 	ret = call_mmap(vma->vm_file, vma);
491 	ovl_revert_creds(file_inode(file)->i_sb, old_cred);
492 	ovl_file_accessed(file);
493 
494 	return ret;
495 }
496 
ovl_fallocate(struct file * file,int mode,loff_t offset,loff_t len)497 static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
498 {
499 	struct inode *inode = file_inode(file);
500 	struct fd real;
501 	const struct cred *old_cred;
502 	int ret;
503 
504 	inode_lock(inode);
505 	/* Update mode */
506 	ovl_copyattr(inode);
507 	ret = file_remove_privs(file);
508 	if (ret)
509 		goto out_unlock;
510 
511 	ret = ovl_real_fdget(file, &real);
512 	if (ret)
513 		goto out_unlock;
514 
515 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
516 	ret = vfs_fallocate(real.file, mode, offset, len);
517 	ovl_revert_creds(file_inode(file)->i_sb, old_cred);
518 
519 	/* Update size */
520 	ovl_copyattr(inode);
521 
522 	fdput(real);
523 
524 out_unlock:
525 	inode_unlock(inode);
526 
527 	return ret;
528 }
529 
ovl_fadvise(struct file * file,loff_t offset,loff_t len,int advice)530 static int ovl_fadvise(struct file *file, loff_t offset, loff_t len, int advice)
531 {
532 	struct fd real;
533 	const struct cred *old_cred;
534 	int ret;
535 
536 	ret = ovl_real_fdget(file, &real);
537 	if (ret)
538 		return ret;
539 
540 	old_cred = ovl_override_creds(file_inode(file)->i_sb);
541 	ret = vfs_fadvise(real.file, offset, len, advice);
542 	ovl_revert_creds(file_inode(file)->i_sb, old_cred);
543 
544 	fdput(real);
545 
546 	return ret;
547 }
548 
549 enum ovl_copyop {
550 	OVL_COPY,
551 	OVL_CLONE,
552 	OVL_DEDUPE,
553 };
554 
ovl_copyfile(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t len,unsigned int flags,enum ovl_copyop op)555 static loff_t ovl_copyfile(struct file *file_in, loff_t pos_in,
556 			    struct file *file_out, loff_t pos_out,
557 			    loff_t len, unsigned int flags, enum ovl_copyop op)
558 {
559 	struct inode *inode_out = file_inode(file_out);
560 	struct fd real_in, real_out;
561 	const struct cred *old_cred;
562 	loff_t ret;
563 
564 	inode_lock(inode_out);
565 	if (op != OVL_DEDUPE) {
566 		/* Update mode */
567 		ovl_copyattr(inode_out);
568 		ret = file_remove_privs(file_out);
569 		if (ret)
570 			goto out_unlock;
571 	}
572 
573 	ret = ovl_real_fdget(file_out, &real_out);
574 	if (ret)
575 		goto out_unlock;
576 
577 	ret = ovl_real_fdget(file_in, &real_in);
578 	if (ret) {
579 		fdput(real_out);
580 		goto out_unlock;
581 	}
582 
583 	old_cred = ovl_override_creds(file_inode(file_out)->i_sb);
584 	switch (op) {
585 	case OVL_COPY:
586 		ret = vfs_copy_file_range(real_in.file, pos_in,
587 					  real_out.file, pos_out, len, flags);
588 		break;
589 
590 	case OVL_CLONE:
591 		ret = vfs_clone_file_range(real_in.file, pos_in,
592 					   real_out.file, pos_out, len, flags);
593 		break;
594 
595 	case OVL_DEDUPE:
596 		ret = vfs_dedupe_file_range_one(real_in.file, pos_in,
597 						real_out.file, pos_out, len,
598 						flags);
599 		break;
600 	}
601 	ovl_revert_creds(file_inode(file_out)->i_sb, old_cred);
602 
603 	/* Update size */
604 	ovl_copyattr(inode_out);
605 
606 	fdput(real_in);
607 	fdput(real_out);
608 
609 out_unlock:
610 	inode_unlock(inode_out);
611 
612 	return ret;
613 }
614 
ovl_copy_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,size_t len,unsigned int flags)615 static ssize_t ovl_copy_file_range(struct file *file_in, loff_t pos_in,
616 				   struct file *file_out, loff_t pos_out,
617 				   size_t len, unsigned int flags)
618 {
619 	return ovl_copyfile(file_in, pos_in, file_out, pos_out, len, flags,
620 			    OVL_COPY);
621 }
622 
ovl_remap_file_range(struct file * file_in,loff_t pos_in,struct file * file_out,loff_t pos_out,loff_t len,unsigned int remap_flags)623 static loff_t ovl_remap_file_range(struct file *file_in, loff_t pos_in,
624 				   struct file *file_out, loff_t pos_out,
625 				   loff_t len, unsigned int remap_flags)
626 {
627 	enum ovl_copyop op;
628 
629 	if (remap_flags & ~(REMAP_FILE_DEDUP | REMAP_FILE_ADVISORY))
630 		return -EINVAL;
631 
632 	if (remap_flags & REMAP_FILE_DEDUP)
633 		op = OVL_DEDUPE;
634 	else
635 		op = OVL_CLONE;
636 
637 	/*
638 	 * Don't copy up because of a dedupe request, this wouldn't make sense
639 	 * most of the time (data would be duplicated instead of deduplicated).
640 	 */
641 	if (op == OVL_DEDUPE &&
642 	    (!ovl_inode_upper(file_inode(file_in)) ||
643 	     !ovl_inode_upper(file_inode(file_out))))
644 		return -EPERM;
645 
646 	return ovl_copyfile(file_in, pos_in, file_out, pos_out, len,
647 			    remap_flags, op);
648 }
649 
ovl_flush(struct file * file,fl_owner_t id)650 static int ovl_flush(struct file *file, fl_owner_t id)
651 {
652 	struct fd real;
653 	const struct cred *old_cred;
654 	int err;
655 
656 	err = ovl_real_fdget(file, &real);
657 	if (err)
658 		return err;
659 
660 	if (real.file->f_op->flush) {
661 		old_cred = ovl_override_creds(file_inode(file)->i_sb);
662 		err = real.file->f_op->flush(real.file, id);
663 		ovl_revert_creds(file_inode(file)->i_sb, old_cred);
664 	}
665 	fdput(real);
666 
667 	return err;
668 }
669 
670 const struct file_operations ovl_file_operations = {
671 	.open		= ovl_open,
672 	.release	= ovl_release,
673 	.llseek		= ovl_llseek,
674 	.read_iter	= ovl_read_iter,
675 	.write_iter	= ovl_write_iter,
676 	.fsync		= ovl_fsync,
677 	.mmap		= ovl_mmap,
678 	.fallocate	= ovl_fallocate,
679 	.fadvise	= ovl_fadvise,
680 	.flush		= ovl_flush,
681 	.splice_read    = generic_file_splice_read,
682 	.splice_write   = ovl_splice_write,
683 
684 	.copy_file_range	= ovl_copy_file_range,
685 	.remap_file_range	= ovl_remap_file_range,
686 };
687 
ovl_aio_request_cache_init(void)688 int __init ovl_aio_request_cache_init(void)
689 {
690 	ovl_aio_request_cachep = kmem_cache_create("ovl_aio_req",
691 						   sizeof(struct ovl_aio_req),
692 						   0, SLAB_HWCACHE_ALIGN, NULL);
693 	if (!ovl_aio_request_cachep)
694 		return -ENOMEM;
695 
696 	return 0;
697 }
698 
ovl_aio_request_cache_destroy(void)699 void ovl_aio_request_cache_destroy(void)
700 {
701 	kmem_cache_destroy(ovl_aio_request_cachep);
702 }
703