1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 1992, 1993, 1994, 1995
6 * Remy Card (card@masi.ibp.fr)
7 * Laboratoire MASI - Institut Blaise Pascal
8 * Universite Pierre et Marie Curie (Paris VI)
12 * linux/fs/minix/file.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * ext2 fs regular file handling primitives
18 * 64-bit file support on 64-bit platforms by Jakub Jelinek
19 * (jj@sunsite.ms.mff.cuni.cz)
22 #include <linux/time.h>
23 #include <linux/pagemap.h>
24 #include <linux/dax.h>
25 #include <linux/quotaops.h>
26 #include <linux/iomap.h>
27 #include <linux/uio.h>
33 static ssize_t ext2_dax_read_iter(struct kiocb *iocb, struct iov_iter *to)
35 struct inode *inode = iocb->ki_filp->f_mapping->host;
38 if (!iov_iter_count(to))
39 return 0; /* skip atime */
41 inode_lock_shared(inode);
42 ret = dax_iomap_rw(iocb, to, &ext2_iomap_ops);
43 inode_unlock_shared(inode);
45 file_accessed(iocb->ki_filp);
49 static ssize_t ext2_dax_write_iter(struct kiocb *iocb, struct iov_iter *from)
51 struct file *file = iocb->ki_filp;
52 struct inode *inode = file->f_mapping->host;
56 ret = generic_write_checks(iocb, from);
59 ret = file_remove_privs(file);
62 ret = file_update_time(file);
66 ret = dax_iomap_rw(iocb, from, &ext2_iomap_ops);
67 if (ret > 0 && iocb->ki_pos > i_size_read(inode)) {
68 i_size_write(inode, iocb->ki_pos);
69 mark_inode_dirty(inode);
75 ret = generic_write_sync(iocb, ret);
80 * The lock ordering for ext2 DAX fault paths is:
83 * sb_start_pagefault (vfs, freeze)
84 * ext2_inode_info->dax_sem
85 * address_space->i_mmap_rwsem or page_lock (mutually exclusive in DAX)
86 * ext2_inode_info->truncate_mutex
88 * The default page_lock and i_size verification done by non-DAX fault paths
89 * is sufficient because ext2 doesn't support hole punching.
91 static vm_fault_t ext2_dax_fault(struct vm_fault *vmf)
93 struct inode *inode = file_inode(vmf->vma->vm_file);
94 struct ext2_inode_info *ei = EXT2_I(inode);
96 bool write = (vmf->flags & FAULT_FLAG_WRITE) &&
97 (vmf->vma->vm_flags & VM_SHARED);
100 sb_start_pagefault(inode->i_sb);
101 file_update_time(vmf->vma->vm_file);
103 down_read(&ei->dax_sem);
105 ret = dax_iomap_fault(vmf, PE_SIZE_PTE, NULL, NULL, &ext2_iomap_ops);
107 up_read(&ei->dax_sem);
109 sb_end_pagefault(inode->i_sb);
113 static const struct vm_operations_struct ext2_dax_vm_ops = {
114 .fault = ext2_dax_fault,
116 * .huge_fault is not supported for DAX because allocation in ext2
117 * cannot be reliably aligned to huge page sizes and so pmd faults
118 * will always fail and fail back to regular faults.
120 .page_mkwrite = ext2_dax_fault,
121 .pfn_mkwrite = ext2_dax_fault,
124 static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma)
126 if (!IS_DAX(file_inode(file)))
127 return generic_file_mmap(file, vma);
130 vma->vm_ops = &ext2_dax_vm_ops;
134 #define ext2_file_mmap generic_file_mmap
138 * Called when filp is released. This happens when all file descriptors
139 * for a single struct file are closed. Note that different open() calls
140 * for the same file yield different struct file structures.
142 static int ext2_release_file (struct inode * inode, struct file * filp)
144 if (filp->f_mode & FMODE_WRITE) {
145 mutex_lock(&EXT2_I(inode)->truncate_mutex);
146 ext2_discard_reservation(inode);
147 mutex_unlock(&EXT2_I(inode)->truncate_mutex);
152 int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
155 struct super_block *sb = file->f_mapping->host->i_sb;
157 ret = generic_file_fsync(file, start, end, datasync);
159 /* We don't really know where the IO error happened... */
160 ext2_error(sb, __func__,
161 "detected IO error when writing metadata buffers");
165 static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
168 if (IS_DAX(iocb->ki_filp->f_mapping->host))
169 return ext2_dax_read_iter(iocb, to);
171 return generic_file_read_iter(iocb, to);
174 static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
177 if (IS_DAX(iocb->ki_filp->f_mapping->host))
178 return ext2_dax_write_iter(iocb, from);
180 return generic_file_write_iter(iocb, from);
183 const struct file_operations ext2_file_operations = {
184 .llseek = generic_file_llseek,
185 .read_iter = ext2_file_read_iter,
186 .write_iter = ext2_file_write_iter,
187 .unlocked_ioctl = ext2_ioctl,
189 .compat_ioctl = ext2_compat_ioctl,
191 .mmap = ext2_file_mmap,
192 .open = dquot_file_open,
193 .release = ext2_release_file,
195 .get_unmapped_area = thp_get_unmapped_area,
196 .splice_read = generic_file_splice_read,
197 .splice_write = iter_file_splice_write,
200 const struct inode_operations ext2_file_inode_operations = {
201 #ifdef CONFIG_EXT2_FS_XATTR
202 .listxattr = ext2_listxattr,
204 .setattr = ext2_setattr,
205 .get_acl = ext2_get_acl,
206 .set_acl = ext2_set_acl,
207 .fiemap = ext2_fiemap,