4 * Copyright (C) 1992, 1993, 1994, 1995
5 * Remy Card (card@masi.ibp.fr)
6 * Laboratoire MASI - Institut Blaise Pascal
7 * Universite Pierre et Marie Curie (Paris VI)
11 * linux/fs/minix/file.c
13 * Copyright (C) 1991, 1992 Linus Torvalds
15 * ext2 fs regular file handling primitives
17 * 64-bit file support on 64-bit platforms by Jakub Jelinek
18 * (jj@sunsite.ms.mff.cuni.cz)
21 #include <linux/time.h>
22 #include <linux/pagemap.h>
23 #include <linux/dax.h>
24 #include <linux/quotaops.h>
25 #include <linux/iomap.h>
26 #include <linux/uio.h>
32 static ssize_t
ext2_dax_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
34 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
37 if (!iov_iter_count(to
))
38 return 0; /* skip atime */
40 inode_lock_shared(inode
);
41 ret
= iomap_dax_rw(iocb
, to
, &ext2_iomap_ops
);
42 inode_unlock_shared(inode
);
44 file_accessed(iocb
->ki_filp
);
48 static ssize_t
ext2_dax_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
50 struct file
*file
= iocb
->ki_filp
;
51 struct inode
*inode
= file
->f_mapping
->host
;
55 ret
= generic_write_checks(iocb
, from
);
58 ret
= file_remove_privs(file
);
61 ret
= file_update_time(file
);
65 ret
= iomap_dax_rw(iocb
, from
, &ext2_iomap_ops
);
66 if (ret
> 0 && iocb
->ki_pos
> i_size_read(inode
)) {
67 i_size_write(inode
, iocb
->ki_pos
);
68 mark_inode_dirty(inode
);
74 ret
= generic_write_sync(iocb
, ret
);
79 * The lock ordering for ext2 DAX fault paths is:
82 * sb_start_pagefault (vfs, freeze)
83 * ext2_inode_info->dax_sem
84 * address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX)
85 * ext2_inode_info->truncate_mutex
87 * The default page_lock and i_size verification done by non-DAX fault paths
88 * is sufficient because ext2 doesn't support hole punching.
90 static int ext2_dax_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
92 struct inode
*inode
= file_inode(vma
->vm_file
);
93 struct ext2_inode_info
*ei
= EXT2_I(inode
);
96 if (vmf
->flags
& FAULT_FLAG_WRITE
) {
97 sb_start_pagefault(inode
->i_sb
);
98 file_update_time(vma
->vm_file
);
100 down_read(&ei
->dax_sem
);
102 ret
= iomap_dax_fault(vma
, vmf
, &ext2_iomap_ops
);
104 up_read(&ei
->dax_sem
);
105 if (vmf
->flags
& FAULT_FLAG_WRITE
)
106 sb_end_pagefault(inode
->i_sb
);
110 static int ext2_dax_pmd_fault(struct vm_area_struct
*vma
, unsigned long addr
,
111 pmd_t
*pmd
, unsigned int flags
)
113 struct inode
*inode
= file_inode(vma
->vm_file
);
114 struct ext2_inode_info
*ei
= EXT2_I(inode
);
117 if (flags
& FAULT_FLAG_WRITE
) {
118 sb_start_pagefault(inode
->i_sb
);
119 file_update_time(vma
->vm_file
);
121 down_read(&ei
->dax_sem
);
123 ret
= dax_pmd_fault(vma
, addr
, pmd
, flags
, ext2_get_block
);
125 up_read(&ei
->dax_sem
);
126 if (flags
& FAULT_FLAG_WRITE
)
127 sb_end_pagefault(inode
->i_sb
);
131 static int ext2_dax_pfn_mkwrite(struct vm_area_struct
*vma
,
132 struct vm_fault
*vmf
)
134 struct inode
*inode
= file_inode(vma
->vm_file
);
135 struct ext2_inode_info
*ei
= EXT2_I(inode
);
139 sb_start_pagefault(inode
->i_sb
);
140 file_update_time(vma
->vm_file
);
141 down_read(&ei
->dax_sem
);
143 /* check that the faulting page hasn't raced with truncate */
144 size
= (i_size_read(inode
) + PAGE_SIZE
- 1) >> PAGE_SHIFT
;
145 if (vmf
->pgoff
>= size
)
146 ret
= VM_FAULT_SIGBUS
;
148 ret
= dax_pfn_mkwrite(vma
, vmf
);
150 up_read(&ei
->dax_sem
);
151 sb_end_pagefault(inode
->i_sb
);
155 static const struct vm_operations_struct ext2_dax_vm_ops
= {
156 .fault
= ext2_dax_fault
,
157 .pmd_fault
= ext2_dax_pmd_fault
,
158 .page_mkwrite
= ext2_dax_fault
,
159 .pfn_mkwrite
= ext2_dax_pfn_mkwrite
,
162 static int ext2_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
164 if (!IS_DAX(file_inode(file
)))
165 return generic_file_mmap(file
, vma
);
168 vma
->vm_ops
= &ext2_dax_vm_ops
;
169 vma
->vm_flags
|= VM_MIXEDMAP
| VM_HUGEPAGE
;
173 #define ext2_file_mmap generic_file_mmap
177 * Called when filp is released. This happens when all file descriptors
178 * for a single struct file are closed. Note that different open() calls
179 * for the same file yield different struct file structures.
181 static int ext2_release_file (struct inode
* inode
, struct file
* filp
)
183 if (filp
->f_mode
& FMODE_WRITE
) {
184 mutex_lock(&EXT2_I(inode
)->truncate_mutex
);
185 ext2_discard_reservation(inode
);
186 mutex_unlock(&EXT2_I(inode
)->truncate_mutex
);
191 int ext2_fsync(struct file
*file
, loff_t start
, loff_t end
, int datasync
)
194 struct super_block
*sb
= file
->f_mapping
->host
->i_sb
;
195 struct address_space
*mapping
= sb
->s_bdev
->bd_inode
->i_mapping
;
197 ret
= generic_file_fsync(file
, start
, end
, datasync
);
198 if (ret
== -EIO
|| test_and_clear_bit(AS_EIO
, &mapping
->flags
)) {
199 /* We don't really know where the IO error happened... */
200 ext2_error(sb
, __func__
,
201 "detected IO error when writing metadata buffers");
207 static ssize_t
ext2_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
210 if (IS_DAX(iocb
->ki_filp
->f_mapping
->host
))
211 return ext2_dax_read_iter(iocb
, to
);
213 return generic_file_read_iter(iocb
, to
);
216 static ssize_t
ext2_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
219 if (IS_DAX(iocb
->ki_filp
->f_mapping
->host
))
220 return ext2_dax_write_iter(iocb
, from
);
222 return generic_file_write_iter(iocb
, from
);
225 const struct file_operations ext2_file_operations
= {
226 .llseek
= generic_file_llseek
,
227 .read_iter
= ext2_file_read_iter
,
228 .write_iter
= ext2_file_write_iter
,
229 .unlocked_ioctl
= ext2_ioctl
,
231 .compat_ioctl
= ext2_compat_ioctl
,
233 .mmap
= ext2_file_mmap
,
234 .open
= dquot_file_open
,
235 .release
= ext2_release_file
,
237 .get_unmapped_area
= thp_get_unmapped_area
,
238 .splice_read
= generic_file_splice_read
,
239 .splice_write
= iter_file_splice_write
,
242 const struct inode_operations ext2_file_inode_operations
= {
243 #ifdef CONFIG_EXT2_FS_XATTR
244 .listxattr
= ext2_listxattr
,
246 .setattr
= ext2_setattr
,
247 .get_acl
= ext2_get_acl
,
248 .set_acl
= ext2_set_acl
,
249 .fiemap
= ext2_fiemap
,