Skip to content

Commit f567377

Browse files
committed
fs: factor out backing_file_mmap() helper
Assert that the file object is allocated in a backing_file container so that file_user_path() could be used to display the user path and not the backing file's path in /proc/<pid>/maps. Signed-off-by: Amir Goldstein <amir73il@gmail.com>
1 parent 9b7e9e2 commit f567377

3 files changed

Lines changed: 35 additions & 17 deletions

File tree

fs/backing-file.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <linux/fs.h>
1212
#include <linux/backing-file.h>
1313
#include <linux/splice.h>
14+
#include <linux/mm.h>
1415

1516
#include "internal.h"
1617

@@ -296,6 +297,32 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
296297
}
297298
EXPORT_SYMBOL_GPL(backing_file_splice_write);
298299

300+
int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
301+
struct backing_file_ctx *ctx)
302+
{
303+
const struct cred *old_cred;
304+
int ret;
305+
306+
if (WARN_ON_ONCE(!(file->f_mode & FMODE_BACKING)) ||
307+
WARN_ON_ONCE(ctx->user_file != vma->vm_file))
308+
return -EIO;
309+
310+
if (!file->f_op->mmap)
311+
return -ENODEV;
312+
313+
vma_set_file(vma, file);
314+
315+
old_cred = override_creds(ctx->cred);
316+
ret = call_mmap(vma->vm_file, vma);
317+
revert_creds(old_cred);
318+
319+
if (ctx->accessed)
320+
ctx->accessed(ctx->user_file);
321+
322+
return ret;
323+
}
324+
EXPORT_SYMBOL_GPL(backing_file_mmap);
325+
299326
static int __init backing_aio_init(void)
300327
{
301328
backing_aio_cachep = kmem_cache_create("backing_aio",

fs/overlayfs/file.c

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <linux/uio.h>
1111
#include <linux/uaccess.h>
1212
#include <linux/security.h>
13-
#include <linux/mm.h>
1413
#include <linux/fs.h>
1514
#include <linux/backing-file.h>
1615
#include "overlayfs.h"
@@ -415,23 +414,13 @@ static int ovl_fsync(struct file *file, loff_t start, loff_t end, int datasync)
415414
static int ovl_mmap(struct file *file, struct vm_area_struct *vma)
416415
{
417416
struct file *realfile = file->private_data;
418-
const struct cred *old_cred;
419-
int ret;
420-
421-
if (!realfile->f_op->mmap)
422-
return -ENODEV;
423-
424-
if (WARN_ON(file != vma->vm_file))
425-
return -EIO;
426-
427-
vma_set_file(vma, realfile);
428-
429-
old_cred = ovl_override_creds(file_inode(file)->i_sb);
430-
ret = call_mmap(vma->vm_file, vma);
431-
revert_creds(old_cred);
432-
ovl_file_accessed(file);
417+
struct backing_file_ctx ctx = {
418+
.cred = ovl_creds(file_inode(file)->i_sb),
419+
.user_file = file,
420+
.accessed = ovl_file_accessed,
421+
};
433422

434-
return ret;
423+
return backing_file_mmap(realfile, vma, &ctx);
435424
}
436425

437426
static long ovl_fallocate(struct file *file, int mode, loff_t offset, loff_t len)

include/linux/backing-file.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,7 @@ ssize_t backing_file_splice_write(struct pipe_inode_info *pipe,
3636
struct file *out, loff_t *ppos, size_t len,
3737
unsigned int flags,
3838
struct backing_file_ctx *ctx);
39+
int backing_file_mmap(struct file *file, struct vm_area_struct *vma,
40+
struct backing_file_ctx *ctx);
3941

4042
#endif /* _LINUX_BACKING_FILE_H */

0 commit comments

Comments
 (0)