]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * linux/fs/ext2/file.c | |
4 | * | |
5 | * Copyright (C) 1992, 1993, 1994, 1995 | |
6 | * Remy Card ([email protected]) | |
7 | * Laboratoire MASI - Institut Blaise Pascal | |
8 | * Universite Pierre et Marie Curie (Paris VI) | |
9 | * | |
10 | * from | |
11 | * | |
12 | * linux/fs/minix/file.c | |
13 | * | |
14 | * Copyright (C) 1991, 1992 Linus Torvalds | |
15 | * | |
16 | * ext2 fs regular file handling primitives | |
17 | * | |
18 | * 64-bit file support on 64-bit platforms by Jakub Jelinek | |
19 | * ([email protected]) | |
20 | */ | |
21 | ||
22 | #include <linux/time.h> | |
48bde86d | 23 | #include <linux/pagemap.h> |
c94c2acf | 24 | #include <linux/dax.h> |
871a2931 | 25 | #include <linux/quotaops.h> |
25f4e702 CH |
26 | #include <linux/iomap.h> |
27 | #include <linux/uio.h> | |
1da177e4 LT |
28 | #include "ext2.h" |
29 | #include "xattr.h" | |
30 | #include "acl.h" | |
31 | ||
6cd176a5 | 32 | #ifdef CONFIG_FS_DAX |
25f4e702 CH |
33 | static ssize_t ext2_dax_read_iter(struct kiocb *iocb, struct iov_iter *to) |
34 | { | |
35 | struct inode *inode = iocb->ki_filp->f_mapping->host; | |
36 | ssize_t ret; | |
37 | ||
38 | if (!iov_iter_count(to)) | |
39 | return 0; /* skip atime */ | |
40 | ||
41 | inode_lock_shared(inode); | |
11c59c92 | 42 | ret = dax_iomap_rw(iocb, to, &ext2_iomap_ops); |
25f4e702 CH |
43 | inode_unlock_shared(inode); |
44 | ||
45 | file_accessed(iocb->ki_filp); | |
46 | return ret; | |
47 | } | |
48 | ||
49 | static ssize_t ext2_dax_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
50 | { | |
51 | struct file *file = iocb->ki_filp; | |
52 | struct inode *inode = file->f_mapping->host; | |
53 | ssize_t ret; | |
54 | ||
55 | inode_lock(inode); | |
56 | ret = generic_write_checks(iocb, from); | |
57 | if (ret <= 0) | |
58 | goto out_unlock; | |
59 | ret = file_remove_privs(file); | |
60 | if (ret) | |
61 | goto out_unlock; | |
62 | ret = file_update_time(file); | |
63 | if (ret) | |
64 | goto out_unlock; | |
65 | ||
11c59c92 | 66 | ret = dax_iomap_rw(iocb, from, &ext2_iomap_ops); |
25f4e702 CH |
67 | if (ret > 0 && iocb->ki_pos > i_size_read(inode)) { |
68 | i_size_write(inode, iocb->ki_pos); | |
69 | mark_inode_dirty(inode); | |
70 | } | |
71 | ||
72 | out_unlock: | |
73 | inode_unlock(inode); | |
74 | if (ret > 0) | |
75 | ret = generic_write_sync(iocb, ret); | |
76 | return ret; | |
77 | } | |
78 | ||
5726b27b RZ |
79 | /* |
80 | * The lock ordering for ext2 DAX fault paths is: | |
81 | * | |
82 | * mmap_sem (MM) | |
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 | |
87 | * | |
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. | |
90 | */ | |
06856938 | 91 | static vm_fault_t ext2_dax_fault(struct vm_fault *vmf) |
f7ca90b1 | 92 | { |
11bac800 | 93 | struct inode *inode = file_inode(vmf->vma->vm_file); |
5726b27b | 94 | struct ext2_inode_info *ei = EXT2_I(inode); |
06856938 | 95 | vm_fault_t ret; |
5726b27b RZ |
96 | |
97 | if (vmf->flags & FAULT_FLAG_WRITE) { | |
98 | sb_start_pagefault(inode->i_sb); | |
11bac800 | 99 | file_update_time(vmf->vma->vm_file); |
5726b27b RZ |
100 | } |
101 | down_read(&ei->dax_sem); | |
102 | ||
c0b24625 | 103 | ret = dax_iomap_fault(vmf, PE_SIZE_PTE, NULL, NULL, &ext2_iomap_ops); |
5726b27b RZ |
104 | |
105 | up_read(&ei->dax_sem); | |
106 | if (vmf->flags & FAULT_FLAG_WRITE) | |
107 | sb_end_pagefault(inode->i_sb); | |
108 | return ret; | |
f7ca90b1 MW |
109 | } |
110 | ||
f7ca90b1 MW |
111 | static const struct vm_operations_struct ext2_dax_vm_ops = { |
112 | .fault = ext2_dax_fault, | |
03e0990f | 113 | /* |
a2d58167 | 114 | * .huge_fault is not supported for DAX because allocation in ext2 |
03e0990f RZ |
115 | * cannot be reliably aligned to huge page sizes and so pmd faults |
116 | * will always fail and fail back to regular faults. | |
117 | */ | |
1e9d180b | 118 | .page_mkwrite = ext2_dax_fault, |
91d25ba8 | 119 | .pfn_mkwrite = ext2_dax_fault, |
f7ca90b1 MW |
120 | }; |
121 | ||
122 | static int ext2_file_mmap(struct file *file, struct vm_area_struct *vma) | |
123 | { | |
124 | if (!IS_DAX(file_inode(file))) | |
125 | return generic_file_mmap(file, vma); | |
126 | ||
127 | file_accessed(file); | |
128 | vma->vm_ops = &ext2_dax_vm_ops; | |
f7ca90b1 MW |
129 | return 0; |
130 | } | |
131 | #else | |
132 | #define ext2_file_mmap generic_file_mmap | |
133 | #endif | |
134 | ||
1da177e4 | 135 | /* |
a6739af8 JK |
136 | * Called when filp is released. This happens when all file descriptors |
137 | * for a single struct file are closed. Note that different open() calls | |
138 | * for the same file yield different struct file structures. | |
1da177e4 LT |
139 | */ |
140 | static int ext2_release_file (struct inode * inode, struct file * filp) | |
141 | { | |
a686cd89 MB |
142 | if (filp->f_mode & FMODE_WRITE) { |
143 | mutex_lock(&EXT2_I(inode)->truncate_mutex); | |
144 | ext2_discard_reservation(inode); | |
145 | mutex_unlock(&EXT2_I(inode)->truncate_mutex); | |
146 | } | |
1da177e4 LT |
147 | return 0; |
148 | } | |
149 | ||
02c24a82 | 150 | int ext2_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
48bde86d JK |
151 | { |
152 | int ret; | |
7ea80859 | 153 | struct super_block *sb = file->f_mapping->host->i_sb; |
48bde86d | 154 | |
02c24a82 | 155 | ret = generic_file_fsync(file, start, end, datasync); |
dac257f7 | 156 | if (ret == -EIO) |
48bde86d JK |
157 | /* We don't really know where the IO error happened... */ |
158 | ext2_error(sb, __func__, | |
159 | "detected IO error when writing metadata buffers"); | |
48bde86d JK |
160 | return ret; |
161 | } | |
162 | ||
25f4e702 CH |
163 | static ssize_t ext2_file_read_iter(struct kiocb *iocb, struct iov_iter *to) |
164 | { | |
165 | #ifdef CONFIG_FS_DAX | |
166 | if (IS_DAX(iocb->ki_filp->f_mapping->host)) | |
167 | return ext2_dax_read_iter(iocb, to); | |
168 | #endif | |
169 | return generic_file_read_iter(iocb, to); | |
170 | } | |
171 | ||
172 | static ssize_t ext2_file_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
173 | { | |
174 | #ifdef CONFIG_FS_DAX | |
175 | if (IS_DAX(iocb->ki_filp->f_mapping->host)) | |
176 | return ext2_dax_write_iter(iocb, from); | |
177 | #endif | |
178 | return generic_file_write_iter(iocb, from); | |
179 | } | |
180 | ||
4b6f5d20 | 181 | const struct file_operations ext2_file_operations = { |
1da177e4 | 182 | .llseek = generic_file_llseek, |
25f4e702 CH |
183 | .read_iter = ext2_file_read_iter, |
184 | .write_iter = ext2_file_write_iter, | |
14f9f7b2 | 185 | .unlocked_ioctl = ext2_ioctl, |
e322ff07 DH |
186 | #ifdef CONFIG_COMPAT |
187 | .compat_ioctl = ext2_compat_ioctl, | |
188 | #endif | |
f7ca90b1 | 189 | .mmap = ext2_file_mmap, |
907f4554 | 190 | .open = dquot_file_open, |
1da177e4 | 191 | .release = ext2_release_file, |
48bde86d | 192 | .fsync = ext2_fsync, |
dbe6ec81 | 193 | .get_unmapped_area = thp_get_unmapped_area, |
5274f052 | 194 | .splice_read = generic_file_splice_read, |
8d020765 | 195 | .splice_write = iter_file_splice_write, |
1da177e4 LT |
196 | }; |
197 | ||
754661f1 | 198 | const struct inode_operations ext2_file_inode_operations = { |
1da177e4 | 199 | #ifdef CONFIG_EXT2_FS_XATTR |
1da177e4 | 200 | .listxattr = ext2_listxattr, |
1da177e4 LT |
201 | #endif |
202 | .setattr = ext2_setattr, | |
4e34e719 | 203 | .get_acl = ext2_get_acl, |
64e178a7 | 204 | .set_acl = ext2_set_acl, |
68c9d702 | 205 | .fiemap = ext2_fiemap, |
1da177e4 | 206 | }; |