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