Skip to content

Commit 2e3b37a

Browse files
lorenzo-stoakesbrauner
authored andcommitted
fs: replace mmap hook with .mmap_prepare for simple mappings
Since commit c84bf6d ("mm: introduce new .mmap_prepare() file callback"), the f_op->mmap() hook has been deprecated in favour of f_op->mmap_prepare(). This callback is invoked in the mmap() logic far earlier, so error handling can be performed more safely without complicated and bug-prone state unwinding required should an error arise. This hook also avoids passing a pointer to a not-yet-correctly-established VMA avoiding any issues with referencing this data structure. It rather provides a pointer to the new struct vm_area_desc descriptor type which contains all required state and allows easy setting of required parameters without any consideration needing to be paid to locking or reference counts. Note that nested filesystems like overlayfs are compatible with an .mmap_prepare() callback since commit bb666b7 ("mm: add mmap_prepare() compatibility layer for nested file systems"). In this patch we apply this change to file systems with relatively simple mmap() hook logic - exfat, ceph, f2fs, bcachefs, zonefs, btrfs, ocfs2, orangefs, nilfs2, romfs, ramfs and aio. Signed-off-by: Lorenzo Stoakes <[email protected]> Link: https://lore.kernel.org/f528ac4f35b9378931bd800920fee53fc0c5c74d.1750099179.git.lorenzo.stoakes@oracle.com Acked-by: Damien Le Moal <[email protected]> Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Viacheslav Dubeyko <[email protected]> Signed-off-by: Christian Brauner <[email protected]>
1 parent 9d5403b commit 2e3b37a

File tree

16 files changed

+58
-49
lines changed

16 files changed

+58
-49
lines changed

fs/aio.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,15 @@ static const struct vm_operations_struct aio_ring_vm_ops = {
392392
#endif
393393
};
394394

395-
static int aio_ring_mmap(struct file *file, struct vm_area_struct *vma)
395+
static int aio_ring_mmap_prepare(struct vm_area_desc *desc)
396396
{
397-
vm_flags_set(vma, VM_DONTEXPAND);
398-
vma->vm_ops = &aio_ring_vm_ops;
397+
desc->vm_flags |= VM_DONTEXPAND;
398+
desc->vm_ops = &aio_ring_vm_ops;
399399
return 0;
400400
}
401401

402402
static const struct file_operations aio_ring_fops = {
403-
.mmap = aio_ring_mmap,
403+
.mmap_prepare = aio_ring_mmap_prepare,
404404
};
405405

406406
#if IS_ENABLED(CONFIG_MIGRATION)

fs/bcachefs/fs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,11 +1553,11 @@ static const struct vm_operations_struct bch_vm_ops = {
15531553
.page_mkwrite = bch2_page_mkwrite,
15541554
};
15551555

1556-
static int bch2_mmap(struct file *file, struct vm_area_struct *vma)
1556+
static int bch2_mmap_prepare(struct vm_area_desc *desc)
15571557
{
1558-
file_accessed(file);
1558+
file_accessed(desc->file);
15591559

1560-
vma->vm_ops = &bch_vm_ops;
1560+
desc->vm_ops = &bch_vm_ops;
15611561
return 0;
15621562
}
15631563

@@ -1740,7 +1740,7 @@ static const struct file_operations bch_file_operations = {
17401740
.llseek = bch2_llseek,
17411741
.read_iter = bch2_read_iter,
17421742
.write_iter = bch2_write_iter,
1743-
.mmap = bch2_mmap,
1743+
.mmap_prepare = bch2_mmap_prepare,
17441744
.get_unmapped_area = thp_get_unmapped_area,
17451745
.fsync = bch2_fsync,
17461746
.splice_read = filemap_splice_read,

fs/btrfs/file.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,15 +1978,16 @@ static const struct vm_operations_struct btrfs_file_vm_ops = {
19781978
.page_mkwrite = btrfs_page_mkwrite,
19791979
};
19801980

1981-
static int btrfs_file_mmap(struct file *filp, struct vm_area_struct *vma)
1981+
static int btrfs_file_mmap_prepare(struct vm_area_desc *desc)
19821982
{
1983+
struct file *filp = desc->file;
19831984
struct address_space *mapping = filp->f_mapping;
19841985

19851986
if (!mapping->a_ops->read_folio)
19861987
return -ENOEXEC;
19871988

19881989
file_accessed(filp);
1989-
vma->vm_ops = &btrfs_file_vm_ops;
1990+
desc->vm_ops = &btrfs_file_vm_ops;
19901991

19911992
return 0;
19921993
}
@@ -3765,7 +3766,7 @@ const struct file_operations btrfs_file_operations = {
37653766
.splice_read = filemap_splice_read,
37663767
.write_iter = btrfs_file_write_iter,
37673768
.splice_write = iter_file_splice_write,
3768-
.mmap = btrfs_file_mmap,
3769+
.mmap_prepare = btrfs_file_mmap_prepare,
37693770
.open = btrfs_file_open,
37703771
.release = btrfs_release_file,
37713772
.get_unmapped_area = thp_get_unmapped_area,

fs/ceph/addr.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2330,13 +2330,13 @@ static const struct vm_operations_struct ceph_vmops = {
23302330
.page_mkwrite = ceph_page_mkwrite,
23312331
};
23322332

2333-
int ceph_mmap(struct file *file, struct vm_area_struct *vma)
2333+
int ceph_mmap_prepare(struct vm_area_desc *desc)
23342334
{
2335-
struct address_space *mapping = file->f_mapping;
2335+
struct address_space *mapping = desc->file->f_mapping;
23362336

23372337
if (!mapping->a_ops->read_folio)
23382338
return -ENOEXEC;
2339-
vma->vm_ops = &ceph_vmops;
2339+
desc->vm_ops = &ceph_vmops;
23402340
return 0;
23412341
}
23422342

fs/ceph/file.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3171,7 +3171,7 @@ const struct file_operations ceph_file_fops = {
31713171
.llseek = ceph_llseek,
31723172
.read_iter = ceph_read_iter,
31733173
.write_iter = ceph_write_iter,
3174-
.mmap = ceph_mmap,
3174+
.mmap_prepare = ceph_mmap_prepare,
31753175
.fsync = ceph_fsync,
31763176
.lock = ceph_lock,
31773177
.setlease = simple_nosetlease,

fs/ceph/super.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1286,7 +1286,7 @@ extern void __ceph_touch_fmode(struct ceph_inode_info *ci,
12861286
/* addr.c */
12871287
extern const struct address_space_operations ceph_aops;
12881288
extern const struct netfs_request_ops ceph_netfs_ops;
1289-
extern int ceph_mmap(struct file *file, struct vm_area_struct *vma);
1289+
int ceph_mmap_prepare(struct vm_area_desc *desc);
12901290
extern int ceph_uninline_data(struct file *file);
12911291
extern int ceph_pool_perm_check(struct inode *inode, int need);
12921292
extern void ceph_pool_perm_destroy(struct ceph_mds_client* mdsc);

fs/exfat/file.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -683,13 +683,15 @@ static const struct vm_operations_struct exfat_file_vm_ops = {
683683
.page_mkwrite = exfat_page_mkwrite,
684684
};
685685

686-
static int exfat_file_mmap(struct file *file, struct vm_area_struct *vma)
686+
static int exfat_file_mmap_prepare(struct vm_area_desc *desc)
687687
{
688-
if (unlikely(exfat_forced_shutdown(file_inode(file)->i_sb)))
688+
struct file *file = desc->file;
689+
690+
if (unlikely(exfat_forced_shutdown(file_inode(desc->file)->i_sb)))
689691
return -EIO;
690692

691693
file_accessed(file);
692-
vma->vm_ops = &exfat_file_vm_ops;
694+
desc->vm_ops = &exfat_file_vm_ops;
693695
return 0;
694696
}
695697

@@ -710,7 +712,7 @@ const struct file_operations exfat_file_operations = {
710712
#ifdef CONFIG_COMPAT
711713
.compat_ioctl = exfat_compat_ioctl,
712714
#endif
713-
.mmap = exfat_file_mmap,
715+
.mmap_prepare = exfat_file_mmap_prepare,
714716
.fsync = exfat_file_fsync,
715717
.splice_read = exfat_splice_read,
716718
.splice_write = iter_file_splice_write,

fs/f2fs/file.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,8 +532,9 @@ static loff_t f2fs_llseek(struct file *file, loff_t offset, int whence)
532532
return -EINVAL;
533533
}
534534

535-
static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
535+
static int f2fs_file_mmap_prepare(struct vm_area_desc *desc)
536536
{
537+
struct file *file = desc->file;
537538
struct inode *inode = file_inode(file);
538539

539540
if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
@@ -543,7 +544,7 @@ static int f2fs_file_mmap(struct file *file, struct vm_area_struct *vma)
543544
return -EOPNOTSUPP;
544545

545546
file_accessed(file);
546-
vma->vm_ops = &f2fs_file_vm_ops;
547+
desc->vm_ops = &f2fs_file_vm_ops;
547548

548549
f2fs_down_read(&F2FS_I(inode)->i_sem);
549550
set_inode_flag(inode, FI_MMAP_FILE);
@@ -5376,7 +5377,7 @@ const struct file_operations f2fs_file_operations = {
53765377
.iopoll = iocb_bio_iopoll,
53775378
.open = f2fs_file_open,
53785379
.release = f2fs_release_file,
5379-
.mmap = f2fs_file_mmap,
5380+
.mmap_prepare = f2fs_file_mmap_prepare,
53805381
.flush = f2fs_file_flush,
53815382
.fsync = f2fs_sync_file,
53825383
.fallocate = f2fs_fallocate,

fs/nilfs2/file.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ static const struct vm_operations_struct nilfs_file_vm_ops = {
125125
.page_mkwrite = nilfs_page_mkwrite,
126126
};
127127

128-
static int nilfs_file_mmap(struct file *file, struct vm_area_struct *vma)
128+
static int nilfs_file_mmap_prepare(struct vm_area_desc *desc)
129129
{
130-
file_accessed(file);
131-
vma->vm_ops = &nilfs_file_vm_ops;
130+
file_accessed(desc->file);
131+
desc->vm_ops = &nilfs_file_vm_ops;
132132
return 0;
133133
}
134134

@@ -144,7 +144,7 @@ const struct file_operations nilfs_file_operations = {
144144
#ifdef CONFIG_COMPAT
145145
.compat_ioctl = nilfs_compat_ioctl,
146146
#endif /* CONFIG_COMPAT */
147-
.mmap = nilfs_file_mmap,
147+
.mmap_prepare = nilfs_file_mmap_prepare,
148148
.open = generic_file_open,
149149
/* .release = nilfs_release_file, */
150150
.fsync = nilfs_sync_file,

fs/ocfs2/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2800,7 +2800,7 @@ const struct inode_operations ocfs2_special_file_iops = {
28002800
*/
28012801
const struct file_operations ocfs2_fops = {
28022802
.llseek = ocfs2_file_llseek,
2803-
.mmap = ocfs2_mmap,
2803+
.mmap_prepare = ocfs2_mmap_prepare,
28042804
.fsync = ocfs2_sync_file,
28052805
.release = ocfs2_file_release,
28062806
.open = ocfs2_file_open,
@@ -2850,7 +2850,7 @@ const struct file_operations ocfs2_dops = {
28502850
*/
28512851
const struct file_operations ocfs2_fops_no_plocks = {
28522852
.llseek = ocfs2_file_llseek,
2853-
.mmap = ocfs2_mmap,
2853+
.mmap_prepare = ocfs2_mmap_prepare,
28542854
.fsync = ocfs2_sync_file,
28552855
.release = ocfs2_file_release,
28562856
.open = ocfs2_file_open,

0 commit comments

Comments
 (0)