Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
dda35b8f | 19 | #include "xfs_fs.h" |
70a9883c | 20 | #include "xfs_shared.h" |
a4fbe6ab | 21 | #include "xfs_format.h" |
239880ef DC |
22 | #include "xfs_log_format.h" |
23 | #include "xfs_trans_resv.h" | |
1da177e4 | 24 | #include "xfs_mount.h" |
57062787 DC |
25 | #include "xfs_da_format.h" |
26 | #include "xfs_da_btree.h" | |
1da177e4 | 27 | #include "xfs_inode.h" |
239880ef | 28 | #include "xfs_trans.h" |
fd3200be | 29 | #include "xfs_inode_item.h" |
dda35b8f | 30 | #include "xfs_bmap.h" |
c24b5dfa | 31 | #include "xfs_bmap_util.h" |
1da177e4 | 32 | #include "xfs_error.h" |
2b9ab5ab | 33 | #include "xfs_dir2.h" |
c24b5dfa | 34 | #include "xfs_dir2_priv.h" |
ddcd856d | 35 | #include "xfs_ioctl.h" |
dda35b8f | 36 | #include "xfs_trace.h" |
239880ef | 37 | #include "xfs_log.h" |
dc06f398 | 38 | #include "xfs_icache.h" |
781355c6 | 39 | #include "xfs_pnfs.h" |
68a9f5e7 | 40 | #include "xfs_iomap.h" |
0613f16c | 41 | #include "xfs_reflink.h" |
1da177e4 LT |
42 | |
43 | #include <linux/dcache.h> | |
2fe17c10 | 44 | #include <linux/falloc.h> |
d126d43f | 45 | #include <linux/pagevec.h> |
66114cad | 46 | #include <linux/backing-dev.h> |
1da177e4 | 47 | |
f0f37e2f | 48 | static const struct vm_operations_struct xfs_file_vm_ops; |
1da177e4 | 49 | |
dda35b8f | 50 | /* |
68a9f5e7 CH |
51 | * Clear the specified ranges to zero through either the pagecache or DAX. |
52 | * Holes and unwritten extents will be left as-is as they already are zeroed. | |
dda35b8f | 53 | */ |
ef9d8733 | 54 | int |
7bb41db3 | 55 | xfs_zero_range( |
68a9f5e7 | 56 | struct xfs_inode *ip, |
7bb41db3 CH |
57 | xfs_off_t pos, |
58 | xfs_off_t count, | |
59 | bool *did_zero) | |
dda35b8f | 60 | { |
459f0fbc | 61 | return iomap_zero_range(VFS_I(ip), pos, count, NULL, &xfs_iomap_ops); |
dda35b8f CH |
62 | } |
63 | ||
8add71ca CH |
64 | int |
65 | xfs_update_prealloc_flags( | |
66 | struct xfs_inode *ip, | |
67 | enum xfs_prealloc_flags flags) | |
68 | { | |
69 | struct xfs_trans *tp; | |
70 | int error; | |
71 | ||
253f4911 CH |
72 | error = xfs_trans_alloc(ip->i_mount, &M_RES(ip->i_mount)->tr_writeid, |
73 | 0, 0, 0, &tp); | |
74 | if (error) | |
8add71ca | 75 | return error; |
8add71ca CH |
76 | |
77 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
78 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
79 | ||
80 | if (!(flags & XFS_PREALLOC_INVISIBLE)) { | |
c19b3b05 DC |
81 | VFS_I(ip)->i_mode &= ~S_ISUID; |
82 | if (VFS_I(ip)->i_mode & S_IXGRP) | |
83 | VFS_I(ip)->i_mode &= ~S_ISGID; | |
8add71ca CH |
84 | xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG); |
85 | } | |
86 | ||
87 | if (flags & XFS_PREALLOC_SET) | |
88 | ip->i_d.di_flags |= XFS_DIFLAG_PREALLOC; | |
89 | if (flags & XFS_PREALLOC_CLEAR) | |
90 | ip->i_d.di_flags &= ~XFS_DIFLAG_PREALLOC; | |
91 | ||
92 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
93 | if (flags & XFS_PREALLOC_SYNC) | |
94 | xfs_trans_set_sync(tp); | |
70393313 | 95 | return xfs_trans_commit(tp); |
8add71ca CH |
96 | } |
97 | ||
1da2f2db CH |
98 | /* |
99 | * Fsync operations on directories are much simpler than on regular files, | |
100 | * as there is no file data to flush, and thus also no need for explicit | |
101 | * cache flush operations, and there are no non-transaction metadata updates | |
102 | * on directories either. | |
103 | */ | |
104 | STATIC int | |
105 | xfs_dir_fsync( | |
106 | struct file *file, | |
107 | loff_t start, | |
108 | loff_t end, | |
109 | int datasync) | |
110 | { | |
111 | struct xfs_inode *ip = XFS_I(file->f_mapping->host); | |
112 | struct xfs_mount *mp = ip->i_mount; | |
113 | xfs_lsn_t lsn = 0; | |
114 | ||
115 | trace_xfs_dir_fsync(ip); | |
116 | ||
117 | xfs_ilock(ip, XFS_ILOCK_SHARED); | |
118 | if (xfs_ipincount(ip)) | |
119 | lsn = ip->i_itemp->ili_last_lsn; | |
120 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | |
121 | ||
122 | if (!lsn) | |
123 | return 0; | |
2451337d | 124 | return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL); |
1da2f2db CH |
125 | } |
126 | ||
fd3200be CH |
127 | STATIC int |
128 | xfs_file_fsync( | |
129 | struct file *file, | |
02c24a82 JB |
130 | loff_t start, |
131 | loff_t end, | |
fd3200be CH |
132 | int datasync) |
133 | { | |
7ea80859 CH |
134 | struct inode *inode = file->f_mapping->host; |
135 | struct xfs_inode *ip = XFS_I(inode); | |
a27a263b | 136 | struct xfs_mount *mp = ip->i_mount; |
fd3200be CH |
137 | int error = 0; |
138 | int log_flushed = 0; | |
b1037058 | 139 | xfs_lsn_t lsn = 0; |
fd3200be | 140 | |
cca28fb8 | 141 | trace_xfs_file_fsync(ip); |
fd3200be | 142 | |
02c24a82 JB |
143 | error = filemap_write_and_wait_range(inode->i_mapping, start, end); |
144 | if (error) | |
145 | return error; | |
146 | ||
a27a263b | 147 | if (XFS_FORCED_SHUTDOWN(mp)) |
b474c7ae | 148 | return -EIO; |
fd3200be CH |
149 | |
150 | xfs_iflags_clear(ip, XFS_ITRUNCATED); | |
151 | ||
a27a263b CH |
152 | if (mp->m_flags & XFS_MOUNT_BARRIER) { |
153 | /* | |
154 | * If we have an RT and/or log subvolume we need to make sure | |
155 | * to flush the write cache the device used for file data | |
156 | * first. This is to ensure newly written file data make | |
157 | * it to disk before logging the new inode size in case of | |
158 | * an extending write. | |
159 | */ | |
160 | if (XFS_IS_REALTIME_INODE(ip)) | |
161 | xfs_blkdev_issue_flush(mp->m_rtdev_targp); | |
162 | else if (mp->m_logdev_targp != mp->m_ddev_targp) | |
163 | xfs_blkdev_issue_flush(mp->m_ddev_targp); | |
164 | } | |
165 | ||
fd3200be | 166 | /* |
fc0561ce DC |
167 | * All metadata updates are logged, which means that we just have to |
168 | * flush the log up to the latest LSN that touched the inode. If we have | |
169 | * concurrent fsync/fdatasync() calls, we need them to all block on the | |
170 | * log force before we clear the ili_fsync_fields field. This ensures | |
171 | * that we don't get a racing sync operation that does not wait for the | |
172 | * metadata to hit the journal before returning. If we race with | |
173 | * clearing the ili_fsync_fields, then all that will happen is the log | |
174 | * force will do nothing as the lsn will already be on disk. We can't | |
175 | * race with setting ili_fsync_fields because that is done under | |
176 | * XFS_ILOCK_EXCL, and that can't happen because we hold the lock shared | |
177 | * until after the ili_fsync_fields is cleared. | |
fd3200be CH |
178 | */ |
179 | xfs_ilock(ip, XFS_ILOCK_SHARED); | |
8f639dde CH |
180 | if (xfs_ipincount(ip)) { |
181 | if (!datasync || | |
fc0561ce | 182 | (ip->i_itemp->ili_fsync_fields & ~XFS_ILOG_TIMESTAMP)) |
8f639dde CH |
183 | lsn = ip->i_itemp->ili_last_lsn; |
184 | } | |
fd3200be | 185 | |
fc0561ce | 186 | if (lsn) { |
b1037058 | 187 | error = _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, &log_flushed); |
fc0561ce DC |
188 | ip->i_itemp->ili_fsync_fields = 0; |
189 | } | |
190 | xfs_iunlock(ip, XFS_ILOCK_SHARED); | |
b1037058 | 191 | |
a27a263b CH |
192 | /* |
193 | * If we only have a single device, and the log force about was | |
194 | * a no-op we might have to flush the data device cache here. | |
195 | * This can only happen for fdatasync/O_DSYNC if we were overwriting | |
196 | * an already allocated file and thus do not have any metadata to | |
197 | * commit. | |
198 | */ | |
199 | if ((mp->m_flags & XFS_MOUNT_BARRIER) && | |
200 | mp->m_logdev_targp == mp->m_ddev_targp && | |
201 | !XFS_IS_REALTIME_INODE(ip) && | |
202 | !log_flushed) | |
203 | xfs_blkdev_issue_flush(mp->m_ddev_targp); | |
fd3200be | 204 | |
2451337d | 205 | return error; |
fd3200be CH |
206 | } |
207 | ||
00258e36 | 208 | STATIC ssize_t |
bbc5a740 | 209 | xfs_file_dio_aio_read( |
dda35b8f | 210 | struct kiocb *iocb, |
b4f5d2c6 | 211 | struct iov_iter *to) |
dda35b8f | 212 | { |
bbc5a740 CH |
213 | struct address_space *mapping = iocb->ki_filp->f_mapping; |
214 | struct inode *inode = mapping->host; | |
00258e36 | 215 | struct xfs_inode *ip = XFS_I(inode); |
f1285ff0 | 216 | loff_t isize = i_size_read(inode); |
bbc5a740 | 217 | size_t count = iov_iter_count(to); |
0ee7a3f6 | 218 | loff_t end = iocb->ki_pos + count - 1; |
f1285ff0 | 219 | struct iov_iter data; |
bbc5a740 | 220 | struct xfs_buftarg *target; |
dda35b8f | 221 | ssize_t ret = 0; |
dda35b8f | 222 | |
bbc5a740 | 223 | trace_xfs_file_direct_read(ip, count, iocb->ki_pos); |
dda35b8f | 224 | |
f1285ff0 CH |
225 | if (!count) |
226 | return 0; /* skip atime */ | |
dda35b8f | 227 | |
bbc5a740 CH |
228 | if (XFS_IS_REALTIME_INODE(ip)) |
229 | target = ip->i_mount->m_rtdev_targp; | |
230 | else | |
231 | target = ip->i_mount->m_ddev_targp; | |
dda35b8f | 232 | |
16d4d435 CH |
233 | /* DIO must be aligned to device logical sector size */ |
234 | if ((iocb->ki_pos | count) & target->bt_logical_sectormask) { | |
235 | if (iocb->ki_pos == isize) | |
236 | return 0; | |
237 | return -EINVAL; | |
dda35b8f | 238 | } |
dda35b8f | 239 | |
a447d7cd CH |
240 | file_accessed(iocb->ki_filp); |
241 | ||
65523218 | 242 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
bbc5a740 | 243 | if (mapping->nrpages) { |
0ee7a3f6 CH |
244 | ret = filemap_write_and_wait_range(mapping, iocb->ki_pos, end); |
245 | if (ret) | |
246 | goto out_unlock; | |
487f84f3 | 247 | |
3d751af2 | 248 | /* |
0ee7a3f6 CH |
249 | * Invalidate whole pages. This can return an error if we fail |
250 | * to invalidate a page, but this should never happen on XFS. | |
251 | * Warn if it does fail. | |
3d751af2 | 252 | */ |
0ee7a3f6 CH |
253 | ret = invalidate_inode_pages2_range(mapping, |
254 | iocb->ki_pos >> PAGE_SHIFT, end >> PAGE_SHIFT); | |
255 | WARN_ON_ONCE(ret); | |
256 | ret = 0; | |
0c38a251 | 257 | } |
dda35b8f | 258 | |
f1285ff0 | 259 | data = *to; |
16d4d435 CH |
260 | ret = __blockdev_direct_IO(iocb, inode, target->bt_bdev, &data, |
261 | xfs_get_blocks_direct, NULL, NULL, 0); | |
c3a69024 | 262 | if (ret >= 0) { |
16d4d435 CH |
263 | iocb->ki_pos += ret; |
264 | iov_iter_advance(to, ret); | |
fa8d972d | 265 | } |
dda35b8f | 266 | |
0ee7a3f6 | 267 | out_unlock: |
65523218 | 268 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
16d4d435 CH |
269 | return ret; |
270 | } | |
271 | ||
f021bd07 | 272 | static noinline ssize_t |
16d4d435 CH |
273 | xfs_file_dax_read( |
274 | struct kiocb *iocb, | |
275 | struct iov_iter *to) | |
276 | { | |
6c31f495 | 277 | struct xfs_inode *ip = XFS_I(iocb->ki_filp->f_mapping->host); |
16d4d435 CH |
278 | size_t count = iov_iter_count(to); |
279 | ssize_t ret = 0; | |
280 | ||
281 | trace_xfs_file_dax_read(ip, count, iocb->ki_pos); | |
282 | ||
283 | if (!count) | |
284 | return 0; /* skip atime */ | |
285 | ||
65523218 | 286 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
11c59c92 | 287 | ret = dax_iomap_rw(iocb, to, &xfs_iomap_ops); |
65523218 | 288 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
bbc5a740 | 289 | |
f1285ff0 | 290 | file_accessed(iocb->ki_filp); |
bbc5a740 CH |
291 | return ret; |
292 | } | |
293 | ||
294 | STATIC ssize_t | |
295 | xfs_file_buffered_aio_read( | |
296 | struct kiocb *iocb, | |
297 | struct iov_iter *to) | |
298 | { | |
299 | struct xfs_inode *ip = XFS_I(file_inode(iocb->ki_filp)); | |
300 | ssize_t ret; | |
301 | ||
302 | trace_xfs_file_buffered_read(ip, iov_iter_count(to), iocb->ki_pos); | |
dda35b8f | 303 | |
65523218 | 304 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
b4f5d2c6 | 305 | ret = generic_file_read_iter(iocb, to); |
65523218 | 306 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
bbc5a740 CH |
307 | |
308 | return ret; | |
309 | } | |
310 | ||
311 | STATIC ssize_t | |
312 | xfs_file_read_iter( | |
313 | struct kiocb *iocb, | |
314 | struct iov_iter *to) | |
315 | { | |
16d4d435 CH |
316 | struct inode *inode = file_inode(iocb->ki_filp); |
317 | struct xfs_mount *mp = XFS_I(inode)->i_mount; | |
bbc5a740 CH |
318 | ssize_t ret = 0; |
319 | ||
320 | XFS_STATS_INC(mp, xs_read_calls); | |
321 | ||
322 | if (XFS_FORCED_SHUTDOWN(mp)) | |
323 | return -EIO; | |
324 | ||
16d4d435 CH |
325 | if (IS_DAX(inode)) |
326 | ret = xfs_file_dax_read(iocb, to); | |
327 | else if (iocb->ki_flags & IOCB_DIRECT) | |
bbc5a740 | 328 | ret = xfs_file_dio_aio_read(iocb, to); |
3176c3e0 | 329 | else |
bbc5a740 | 330 | ret = xfs_file_buffered_aio_read(iocb, to); |
dda35b8f | 331 | |
dda35b8f | 332 | if (ret > 0) |
ff6d6af2 | 333 | XFS_STATS_ADD(mp, xs_read_bytes, ret); |
dda35b8f CH |
334 | return ret; |
335 | } | |
336 | ||
dda35b8f | 337 | /* |
193aec10 CH |
338 | * Zero any on disk space between the current EOF and the new, larger EOF. |
339 | * | |
340 | * This handles the normal case of zeroing the remainder of the last block in | |
341 | * the file and the unusual case of zeroing blocks out beyond the size of the | |
342 | * file. This second case only happens with fixed size extents and when the | |
343 | * system crashes before the inode size was updated but after blocks were | |
344 | * allocated. | |
345 | * | |
346 | * Expects the iolock to be held exclusive, and will take the ilock internally. | |
dda35b8f | 347 | */ |
dda35b8f CH |
348 | int /* error (positive) */ |
349 | xfs_zero_eof( | |
193aec10 CH |
350 | struct xfs_inode *ip, |
351 | xfs_off_t offset, /* starting I/O offset */ | |
5885ebda DC |
352 | xfs_fsize_t isize, /* current inode size */ |
353 | bool *did_zeroing) | |
dda35b8f | 354 | { |
193aec10 | 355 | ASSERT(xfs_isilocked(ip, XFS_IOLOCK_EXCL)); |
dda35b8f CH |
356 | ASSERT(offset > isize); |
357 | ||
0a50f162 | 358 | trace_xfs_zero_eof(ip, isize, offset - isize); |
570b6211 | 359 | return xfs_zero_range(ip, isize, offset - isize, did_zeroing); |
dda35b8f CH |
360 | } |
361 | ||
4d8d1581 DC |
362 | /* |
363 | * Common pre-write limit and setup checks. | |
364 | * | |
5bf1f262 CH |
365 | * Called with the iolocked held either shared and exclusive according to |
366 | * @iolock, and returns with it held. Might upgrade the iolock to exclusive | |
367 | * if called for a direct write beyond i_size. | |
4d8d1581 DC |
368 | */ |
369 | STATIC ssize_t | |
370 | xfs_file_aio_write_checks( | |
99733fa3 AV |
371 | struct kiocb *iocb, |
372 | struct iov_iter *from, | |
4d8d1581 DC |
373 | int *iolock) |
374 | { | |
99733fa3 | 375 | struct file *file = iocb->ki_filp; |
4d8d1581 DC |
376 | struct inode *inode = file->f_mapping->host; |
377 | struct xfs_inode *ip = XFS_I(inode); | |
3309dd04 | 378 | ssize_t error = 0; |
99733fa3 | 379 | size_t count = iov_iter_count(from); |
3136e8bb | 380 | bool drained_dio = false; |
4d8d1581 | 381 | |
7271d243 | 382 | restart: |
3309dd04 AV |
383 | error = generic_write_checks(iocb, from); |
384 | if (error <= 0) | |
4d8d1581 | 385 | return error; |
4d8d1581 | 386 | |
65523218 | 387 | error = xfs_break_layouts(inode, iolock); |
781355c6 CH |
388 | if (error) |
389 | return error; | |
390 | ||
65523218 CH |
391 | /* |
392 | * For changing security info in file_remove_privs() we need i_rwsem | |
393 | * exclusively. | |
394 | */ | |
a6de82ca | 395 | if (*iolock == XFS_IOLOCK_SHARED && !IS_NOSEC(inode)) { |
65523218 | 396 | xfs_iunlock(ip, *iolock); |
a6de82ca | 397 | *iolock = XFS_IOLOCK_EXCL; |
65523218 | 398 | xfs_ilock(ip, *iolock); |
a6de82ca JK |
399 | goto restart; |
400 | } | |
4d8d1581 DC |
401 | /* |
402 | * If the offset is beyond the size of the file, we need to zero any | |
403 | * blocks that fall between the existing EOF and the start of this | |
2813d682 | 404 | * write. If zeroing is needed and we are currently holding the |
467f7899 CH |
405 | * iolock shared, we need to update it to exclusive which implies |
406 | * having to redo all checks before. | |
b9d59846 DC |
407 | * |
408 | * We need to serialise against EOF updates that occur in IO | |
409 | * completions here. We want to make sure that nobody is changing the | |
410 | * size while we do this check until we have placed an IO barrier (i.e. | |
411 | * hold the XFS_IOLOCK_EXCL) that prevents new IO from being dispatched. | |
412 | * The spinlock effectively forms a memory barrier once we have the | |
413 | * XFS_IOLOCK_EXCL so we are guaranteed to see the latest EOF value | |
414 | * and hence be able to correctly determine if we need to run zeroing. | |
4d8d1581 | 415 | */ |
b9d59846 | 416 | spin_lock(&ip->i_flags_lock); |
99733fa3 | 417 | if (iocb->ki_pos > i_size_read(inode)) { |
5885ebda DC |
418 | bool zero = false; |
419 | ||
b9d59846 | 420 | spin_unlock(&ip->i_flags_lock); |
3136e8bb BF |
421 | if (!drained_dio) { |
422 | if (*iolock == XFS_IOLOCK_SHARED) { | |
65523218 | 423 | xfs_iunlock(ip, *iolock); |
3136e8bb | 424 | *iolock = XFS_IOLOCK_EXCL; |
65523218 | 425 | xfs_ilock(ip, *iolock); |
3136e8bb BF |
426 | iov_iter_reexpand(from, count); |
427 | } | |
40c63fbc DC |
428 | /* |
429 | * We now have an IO submission barrier in place, but | |
430 | * AIO can do EOF updates during IO completion and hence | |
431 | * we now need to wait for all of them to drain. Non-AIO | |
432 | * DIO will have drained before we are given the | |
433 | * XFS_IOLOCK_EXCL, and so for most cases this wait is a | |
434 | * no-op. | |
435 | */ | |
436 | inode_dio_wait(inode); | |
3136e8bb | 437 | drained_dio = true; |
7271d243 DC |
438 | goto restart; |
439 | } | |
99733fa3 | 440 | error = xfs_zero_eof(ip, iocb->ki_pos, i_size_read(inode), &zero); |
467f7899 CH |
441 | if (error) |
442 | return error; | |
b9d59846 DC |
443 | } else |
444 | spin_unlock(&ip->i_flags_lock); | |
4d8d1581 | 445 | |
8a9c9980 CH |
446 | /* |
447 | * Updating the timestamps will grab the ilock again from | |
448 | * xfs_fs_dirty_inode, so we have to call it after dropping the | |
449 | * lock above. Eventually we should look into a way to avoid | |
450 | * the pointless lock roundtrip. | |
451 | */ | |
c3b2da31 JB |
452 | if (likely(!(file->f_mode & FMODE_NOCMTIME))) { |
453 | error = file_update_time(file); | |
454 | if (error) | |
455 | return error; | |
456 | } | |
8a9c9980 | 457 | |
4d8d1581 DC |
458 | /* |
459 | * If we're writing the file then make sure to clear the setuid and | |
460 | * setgid bits if the process is not being run by root. This keeps | |
461 | * people from modifying setuid and setgid binaries. | |
462 | */ | |
a6de82ca JK |
463 | if (!IS_NOSEC(inode)) |
464 | return file_remove_privs(file); | |
465 | return 0; | |
4d8d1581 DC |
466 | } |
467 | ||
f0d26e86 DC |
468 | /* |
469 | * xfs_file_dio_aio_write - handle direct IO writes | |
470 | * | |
471 | * Lock the inode appropriately to prepare for and issue a direct IO write. | |
eda77982 | 472 | * By separating it from the buffered write path we remove all the tricky to |
f0d26e86 DC |
473 | * follow locking changes and looping. |
474 | * | |
eda77982 DC |
475 | * If there are cached pages or we're extending the file, we need IOLOCK_EXCL |
476 | * until we're sure the bytes at the new EOF have been zeroed and/or the cached | |
477 | * pages are flushed out. | |
478 | * | |
479 | * In most cases the direct IO writes will be done holding IOLOCK_SHARED | |
480 | * allowing them to be done in parallel with reads and other direct IO writes. | |
481 | * However, if the IO is not aligned to filesystem blocks, the direct IO layer | |
482 | * needs to do sub-block zeroing and that requires serialisation against other | |
483 | * direct IOs to the same block. In this case we need to serialise the | |
484 | * submission of the unaligned IOs so that we don't get racing block zeroing in | |
485 | * the dio layer. To avoid the problem with aio, we also need to wait for | |
486 | * outstanding IOs to complete so that unwritten extent conversion is completed | |
487 | * before we try to map the overlapping block. This is currently implemented by | |
4a06fd26 | 488 | * hitting it with a big hammer (i.e. inode_dio_wait()). |
eda77982 | 489 | * |
f0d26e86 DC |
490 | * Returns with locks held indicated by @iolock and errors indicated by |
491 | * negative return values. | |
492 | */ | |
493 | STATIC ssize_t | |
494 | xfs_file_dio_aio_write( | |
495 | struct kiocb *iocb, | |
b3188919 | 496 | struct iov_iter *from) |
f0d26e86 DC |
497 | { |
498 | struct file *file = iocb->ki_filp; | |
499 | struct address_space *mapping = file->f_mapping; | |
500 | struct inode *inode = mapping->host; | |
501 | struct xfs_inode *ip = XFS_I(inode); | |
502 | struct xfs_mount *mp = ip->i_mount; | |
503 | ssize_t ret = 0; | |
eda77982 | 504 | int unaligned_io = 0; |
d0606464 | 505 | int iolock; |
b3188919 | 506 | size_t count = iov_iter_count(from); |
0cefb29e DC |
507 | loff_t end; |
508 | struct iov_iter data; | |
f0d26e86 DC |
509 | struct xfs_buftarg *target = XFS_IS_REALTIME_INODE(ip) ? |
510 | mp->m_rtdev_targp : mp->m_ddev_targp; | |
511 | ||
7c71ee78 | 512 | /* DIO must be aligned to device logical sector size */ |
16d4d435 | 513 | if ((iocb->ki_pos | count) & target->bt_logical_sectormask) |
b474c7ae | 514 | return -EINVAL; |
f0d26e86 | 515 | |
7271d243 | 516 | /* |
0ee7a3f6 CH |
517 | * Don't take the exclusive iolock here unless the I/O is unaligned to |
518 | * the file system block size. We don't need to consider the EOF | |
519 | * extension case here because xfs_file_aio_write_checks() will relock | |
520 | * the inode as necessary for EOF zeroing cases and fill out the new | |
521 | * inode size as appropriate. | |
7271d243 | 522 | */ |
0ee7a3f6 CH |
523 | if ((iocb->ki_pos & mp->m_blockmask) || |
524 | ((iocb->ki_pos + count) & mp->m_blockmask)) { | |
525 | unaligned_io = 1; | |
d0606464 | 526 | iolock = XFS_IOLOCK_EXCL; |
0ee7a3f6 | 527 | } else { |
d0606464 | 528 | iolock = XFS_IOLOCK_SHARED; |
c58cb165 | 529 | } |
f0d26e86 | 530 | |
65523218 | 531 | xfs_ilock(ip, iolock); |
0ee7a3f6 | 532 | |
99733fa3 | 533 | ret = xfs_file_aio_write_checks(iocb, from, &iolock); |
4d8d1581 | 534 | if (ret) |
d0606464 | 535 | goto out; |
99733fa3 | 536 | count = iov_iter_count(from); |
13712713 | 537 | end = iocb->ki_pos + count - 1; |
f0d26e86 DC |
538 | |
539 | if (mapping->nrpages) { | |
0ee7a3f6 | 540 | ret = filemap_write_and_wait_range(mapping, iocb->ki_pos, end); |
f0d26e86 | 541 | if (ret) |
d0606464 | 542 | goto out; |
0ee7a3f6 | 543 | |
834ffca6 | 544 | /* |
3d751af2 BF |
545 | * Invalidate whole pages. This can return an error if we fail |
546 | * to invalidate a page, but this should never happen on XFS. | |
547 | * Warn if it does fail. | |
834ffca6 | 548 | */ |
0ee7a3f6 CH |
549 | ret = invalidate_inode_pages2_range(mapping, |
550 | iocb->ki_pos >> PAGE_SHIFT, end >> PAGE_SHIFT); | |
834ffca6 DC |
551 | WARN_ON_ONCE(ret); |
552 | ret = 0; | |
f0d26e86 DC |
553 | } |
554 | ||
eda77982 DC |
555 | /* |
556 | * If we are doing unaligned IO, wait for all other IO to drain, | |
0ee7a3f6 CH |
557 | * otherwise demote the lock if we had to take the exclusive lock |
558 | * for other reasons in xfs_file_aio_write_checks. | |
eda77982 DC |
559 | */ |
560 | if (unaligned_io) | |
4a06fd26 | 561 | inode_dio_wait(inode); |
d0606464 | 562 | else if (iolock == XFS_IOLOCK_EXCL) { |
65523218 | 563 | xfs_ilock_demote(ip, XFS_IOLOCK_EXCL); |
d0606464 | 564 | iolock = XFS_IOLOCK_SHARED; |
f0d26e86 DC |
565 | } |
566 | ||
3176c3e0 | 567 | trace_xfs_file_direct_write(ip, count, iocb->ki_pos); |
f0d26e86 | 568 | |
0613f16c DW |
569 | /* If this is a block-aligned directio CoW, remap immediately. */ |
570 | if (xfs_is_reflink_inode(ip) && !unaligned_io) { | |
571 | ret = xfs_reflink_allocate_cow_range(ip, iocb->ki_pos, count); | |
572 | if (ret) | |
573 | goto out; | |
574 | } | |
575 | ||
0cefb29e | 576 | data = *from; |
16d4d435 CH |
577 | ret = __blockdev_direct_IO(iocb, inode, target->bt_bdev, &data, |
578 | xfs_get_blocks_direct, xfs_end_io_direct_write, | |
579 | NULL, DIO_ASYNC_EXTEND); | |
0cefb29e DC |
580 | |
581 | /* see generic_file_direct_write() for why this is necessary */ | |
582 | if (mapping->nrpages) { | |
583 | invalidate_inode_pages2_range(mapping, | |
13712713 | 584 | iocb->ki_pos >> PAGE_SHIFT, |
09cbfeaf | 585 | end >> PAGE_SHIFT); |
0cefb29e DC |
586 | } |
587 | ||
588 | if (ret > 0) { | |
13712713 | 589 | iocb->ki_pos += ret; |
0cefb29e | 590 | iov_iter_advance(from, ret); |
0cefb29e | 591 | } |
d0606464 | 592 | out: |
65523218 | 593 | xfs_iunlock(ip, iolock); |
d0606464 | 594 | |
6b698ede | 595 | /* |
16d4d435 CH |
596 | * No fallback to buffered IO on errors for XFS, direct IO will either |
597 | * complete fully or fail. | |
6b698ede | 598 | */ |
16d4d435 CH |
599 | ASSERT(ret < 0 || ret == count); |
600 | return ret; | |
601 | } | |
602 | ||
f021bd07 | 603 | static noinline ssize_t |
16d4d435 CH |
604 | xfs_file_dax_write( |
605 | struct kiocb *iocb, | |
606 | struct iov_iter *from) | |
607 | { | |
6c31f495 | 608 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
16d4d435 | 609 | struct xfs_inode *ip = XFS_I(inode); |
17879e8f | 610 | int iolock = XFS_IOLOCK_EXCL; |
6c31f495 CH |
611 | ssize_t ret, error = 0; |
612 | size_t count; | |
613 | loff_t pos; | |
16d4d435 | 614 | |
65523218 | 615 | xfs_ilock(ip, iolock); |
16d4d435 CH |
616 | ret = xfs_file_aio_write_checks(iocb, from, &iolock); |
617 | if (ret) | |
618 | goto out; | |
619 | ||
6c31f495 CH |
620 | pos = iocb->ki_pos; |
621 | count = iov_iter_count(from); | |
8b2180b3 | 622 | |
6c31f495 | 623 | trace_xfs_file_dax_write(ip, count, pos); |
11c59c92 | 624 | ret = dax_iomap_rw(iocb, from, &xfs_iomap_ops); |
6c31f495 CH |
625 | if (ret > 0 && iocb->ki_pos > i_size_read(inode)) { |
626 | i_size_write(inode, iocb->ki_pos); | |
627 | error = xfs_setfilesize(ip, pos, ret); | |
16d4d435 | 628 | } |
16d4d435 | 629 | out: |
65523218 | 630 | xfs_iunlock(ip, iolock); |
6c31f495 | 631 | return error ? error : ret; |
f0d26e86 DC |
632 | } |
633 | ||
00258e36 | 634 | STATIC ssize_t |
637bbc75 | 635 | xfs_file_buffered_aio_write( |
dda35b8f | 636 | struct kiocb *iocb, |
b3188919 | 637 | struct iov_iter *from) |
dda35b8f CH |
638 | { |
639 | struct file *file = iocb->ki_filp; | |
640 | struct address_space *mapping = file->f_mapping; | |
641 | struct inode *inode = mapping->host; | |
00258e36 | 642 | struct xfs_inode *ip = XFS_I(inode); |
637bbc75 DC |
643 | ssize_t ret; |
644 | int enospc = 0; | |
d0606464 | 645 | int iolock = XFS_IOLOCK_EXCL; |
dda35b8f | 646 | |
65523218 | 647 | xfs_ilock(ip, iolock); |
dda35b8f | 648 | |
99733fa3 | 649 | ret = xfs_file_aio_write_checks(iocb, from, &iolock); |
4d8d1581 | 650 | if (ret) |
d0606464 | 651 | goto out; |
dda35b8f CH |
652 | |
653 | /* We can write back this queue in page reclaim */ | |
de1414a6 | 654 | current->backing_dev_info = inode_to_bdi(inode); |
dda35b8f | 655 | |
dda35b8f | 656 | write_retry: |
3176c3e0 | 657 | trace_xfs_file_buffered_write(ip, iov_iter_count(from), iocb->ki_pos); |
68a9f5e7 | 658 | ret = iomap_file_buffered_write(iocb, from, &xfs_iomap_ops); |
0a64bc2c | 659 | if (likely(ret >= 0)) |
99733fa3 | 660 | iocb->ki_pos += ret; |
dc06f398 | 661 | |
637bbc75 | 662 | /* |
dc06f398 BF |
663 | * If we hit a space limit, try to free up some lingering preallocated |
664 | * space before returning an error. In the case of ENOSPC, first try to | |
665 | * write back all dirty inodes to free up some of the excess reserved | |
666 | * metadata space. This reduces the chances that the eofblocks scan | |
667 | * waits on dirty mappings. Since xfs_flush_inodes() is serialized, this | |
668 | * also behaves as a filter to prevent too many eofblocks scans from | |
669 | * running at the same time. | |
637bbc75 | 670 | */ |
dc06f398 BF |
671 | if (ret == -EDQUOT && !enospc) { |
672 | enospc = xfs_inode_free_quota_eofblocks(ip); | |
673 | if (enospc) | |
674 | goto write_retry; | |
83104d44 DW |
675 | enospc = xfs_inode_free_quota_cowblocks(ip); |
676 | if (enospc) | |
677 | goto write_retry; | |
dc06f398 BF |
678 | } else if (ret == -ENOSPC && !enospc) { |
679 | struct xfs_eofblocks eofb = {0}; | |
680 | ||
637bbc75 | 681 | enospc = 1; |
9aa05000 | 682 | xfs_flush_inodes(ip->i_mount); |
dc06f398 BF |
683 | eofb.eof_scan_owner = ip->i_ino; /* for locking */ |
684 | eofb.eof_flags = XFS_EOF_FLAGS_SYNC; | |
685 | xfs_icache_free_eofblocks(ip->i_mount, &eofb); | |
9aa05000 | 686 | goto write_retry; |
dda35b8f | 687 | } |
d0606464 | 688 | |
dda35b8f | 689 | current->backing_dev_info = NULL; |
d0606464 | 690 | out: |
65523218 | 691 | xfs_iunlock(ip, iolock); |
637bbc75 DC |
692 | return ret; |
693 | } | |
694 | ||
695 | STATIC ssize_t | |
bf97f3bc | 696 | xfs_file_write_iter( |
637bbc75 | 697 | struct kiocb *iocb, |
bf97f3bc | 698 | struct iov_iter *from) |
637bbc75 DC |
699 | { |
700 | struct file *file = iocb->ki_filp; | |
701 | struct address_space *mapping = file->f_mapping; | |
702 | struct inode *inode = mapping->host; | |
703 | struct xfs_inode *ip = XFS_I(inode); | |
704 | ssize_t ret; | |
bf97f3bc | 705 | size_t ocount = iov_iter_count(from); |
637bbc75 | 706 | |
ff6d6af2 | 707 | XFS_STATS_INC(ip->i_mount, xs_write_calls); |
637bbc75 | 708 | |
637bbc75 DC |
709 | if (ocount == 0) |
710 | return 0; | |
711 | ||
bf97f3bc AV |
712 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
713 | return -EIO; | |
637bbc75 | 714 | |
16d4d435 CH |
715 | if (IS_DAX(inode)) |
716 | ret = xfs_file_dax_write(iocb, from); | |
0613f16c DW |
717 | else if (iocb->ki_flags & IOCB_DIRECT) { |
718 | /* | |
719 | * Allow a directio write to fall back to a buffered | |
720 | * write *only* in the case that we're doing a reflink | |
721 | * CoW. In all other directio scenarios we do not | |
722 | * allow an operation to fall back to buffered mode. | |
723 | */ | |
bf97f3bc | 724 | ret = xfs_file_dio_aio_write(iocb, from); |
0613f16c DW |
725 | if (ret == -EREMCHG) |
726 | goto buffered; | |
727 | } else { | |
728 | buffered: | |
bf97f3bc | 729 | ret = xfs_file_buffered_aio_write(iocb, from); |
0613f16c | 730 | } |
dda35b8f | 731 | |
d0606464 | 732 | if (ret > 0) { |
ff6d6af2 | 733 | XFS_STATS_ADD(ip->i_mount, xs_write_bytes, ret); |
dda35b8f | 734 | |
d0606464 | 735 | /* Handle various SYNC-type writes */ |
e2592217 | 736 | ret = generic_write_sync(iocb, ret); |
dda35b8f | 737 | } |
a363f0c2 | 738 | return ret; |
dda35b8f CH |
739 | } |
740 | ||
a904b1ca NJ |
741 | #define XFS_FALLOC_FL_SUPPORTED \ |
742 | (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \ | |
743 | FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_ZERO_RANGE | \ | |
98cc2db5 | 744 | FALLOC_FL_INSERT_RANGE | FALLOC_FL_UNSHARE_RANGE) |
a904b1ca | 745 | |
2fe17c10 CH |
746 | STATIC long |
747 | xfs_file_fallocate( | |
83aee9e4 CH |
748 | struct file *file, |
749 | int mode, | |
750 | loff_t offset, | |
751 | loff_t len) | |
2fe17c10 | 752 | { |
83aee9e4 CH |
753 | struct inode *inode = file_inode(file); |
754 | struct xfs_inode *ip = XFS_I(inode); | |
83aee9e4 | 755 | long error; |
8add71ca | 756 | enum xfs_prealloc_flags flags = 0; |
781355c6 | 757 | uint iolock = XFS_IOLOCK_EXCL; |
83aee9e4 | 758 | loff_t new_size = 0; |
a904b1ca | 759 | bool do_file_insert = 0; |
2fe17c10 | 760 | |
83aee9e4 CH |
761 | if (!S_ISREG(inode->i_mode)) |
762 | return -EINVAL; | |
a904b1ca | 763 | if (mode & ~XFS_FALLOC_FL_SUPPORTED) |
2fe17c10 CH |
764 | return -EOPNOTSUPP; |
765 | ||
781355c6 | 766 | xfs_ilock(ip, iolock); |
65523218 | 767 | error = xfs_break_layouts(inode, &iolock); |
781355c6 CH |
768 | if (error) |
769 | goto out_unlock; | |
770 | ||
e8e9ad42 DC |
771 | xfs_ilock(ip, XFS_MMAPLOCK_EXCL); |
772 | iolock |= XFS_MMAPLOCK_EXCL; | |
773 | ||
83aee9e4 CH |
774 | if (mode & FALLOC_FL_PUNCH_HOLE) { |
775 | error = xfs_free_file_space(ip, offset, len); | |
776 | if (error) | |
777 | goto out_unlock; | |
e1d8fb88 NJ |
778 | } else if (mode & FALLOC_FL_COLLAPSE_RANGE) { |
779 | unsigned blksize_mask = (1 << inode->i_blkbits) - 1; | |
780 | ||
781 | if (offset & blksize_mask || len & blksize_mask) { | |
2451337d | 782 | error = -EINVAL; |
e1d8fb88 NJ |
783 | goto out_unlock; |
784 | } | |
785 | ||
23fffa92 LC |
786 | /* |
787 | * There is no need to overlap collapse range with EOF, | |
788 | * in which case it is effectively a truncate operation | |
789 | */ | |
790 | if (offset + len >= i_size_read(inode)) { | |
2451337d | 791 | error = -EINVAL; |
23fffa92 LC |
792 | goto out_unlock; |
793 | } | |
794 | ||
e1d8fb88 NJ |
795 | new_size = i_size_read(inode) - len; |
796 | ||
797 | error = xfs_collapse_file_space(ip, offset, len); | |
798 | if (error) | |
799 | goto out_unlock; | |
a904b1ca NJ |
800 | } else if (mode & FALLOC_FL_INSERT_RANGE) { |
801 | unsigned blksize_mask = (1 << inode->i_blkbits) - 1; | |
802 | ||
803 | new_size = i_size_read(inode) + len; | |
804 | if (offset & blksize_mask || len & blksize_mask) { | |
805 | error = -EINVAL; | |
806 | goto out_unlock; | |
807 | } | |
808 | ||
809 | /* check the new inode size does not wrap through zero */ | |
810 | if (new_size > inode->i_sb->s_maxbytes) { | |
811 | error = -EFBIG; | |
812 | goto out_unlock; | |
813 | } | |
814 | ||
815 | /* Offset should be less than i_size */ | |
816 | if (offset >= i_size_read(inode)) { | |
817 | error = -EINVAL; | |
818 | goto out_unlock; | |
819 | } | |
820 | do_file_insert = 1; | |
83aee9e4 | 821 | } else { |
8add71ca CH |
822 | flags |= XFS_PREALLOC_SET; |
823 | ||
83aee9e4 CH |
824 | if (!(mode & FALLOC_FL_KEEP_SIZE) && |
825 | offset + len > i_size_read(inode)) { | |
826 | new_size = offset + len; | |
2451337d | 827 | error = inode_newsize_ok(inode, new_size); |
83aee9e4 CH |
828 | if (error) |
829 | goto out_unlock; | |
830 | } | |
2fe17c10 | 831 | |
376ba313 LC |
832 | if (mode & FALLOC_FL_ZERO_RANGE) |
833 | error = xfs_zero_file_space(ip, offset, len); | |
98cc2db5 DW |
834 | else { |
835 | if (mode & FALLOC_FL_UNSHARE_RANGE) { | |
836 | error = xfs_reflink_unshare(ip, offset, len); | |
837 | if (error) | |
838 | goto out_unlock; | |
839 | } | |
376ba313 LC |
840 | error = xfs_alloc_file_space(ip, offset, len, |
841 | XFS_BMAPI_PREALLOC); | |
98cc2db5 | 842 | } |
2fe17c10 CH |
843 | if (error) |
844 | goto out_unlock; | |
845 | } | |
846 | ||
83aee9e4 | 847 | if (file->f_flags & O_DSYNC) |
8add71ca CH |
848 | flags |= XFS_PREALLOC_SYNC; |
849 | ||
850 | error = xfs_update_prealloc_flags(ip, flags); | |
2fe17c10 CH |
851 | if (error) |
852 | goto out_unlock; | |
853 | ||
854 | /* Change file size if needed */ | |
855 | if (new_size) { | |
856 | struct iattr iattr; | |
857 | ||
858 | iattr.ia_valid = ATTR_SIZE; | |
859 | iattr.ia_size = new_size; | |
69bca807 | 860 | error = xfs_vn_setattr_size(file_dentry(file), &iattr); |
a904b1ca NJ |
861 | if (error) |
862 | goto out_unlock; | |
2fe17c10 CH |
863 | } |
864 | ||
a904b1ca NJ |
865 | /* |
866 | * Perform hole insertion now that the file size has been | |
867 | * updated so that if we crash during the operation we don't | |
868 | * leave shifted extents past EOF and hence losing access to | |
869 | * the data that is contained within them. | |
870 | */ | |
871 | if (do_file_insert) | |
872 | error = xfs_insert_file_space(ip, offset, len); | |
873 | ||
2fe17c10 | 874 | out_unlock: |
781355c6 | 875 | xfs_iunlock(ip, iolock); |
2451337d | 876 | return error; |
2fe17c10 CH |
877 | } |
878 | ||
9fe26045 DW |
879 | STATIC ssize_t |
880 | xfs_file_copy_range( | |
881 | struct file *file_in, | |
882 | loff_t pos_in, | |
883 | struct file *file_out, | |
884 | loff_t pos_out, | |
885 | size_t len, | |
886 | unsigned int flags) | |
887 | { | |
888 | int error; | |
889 | ||
5faaf4fa | 890 | error = xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out, |
cc714660 | 891 | len, false); |
9fe26045 DW |
892 | if (error) |
893 | return error; | |
894 | return len; | |
895 | } | |
896 | ||
897 | STATIC int | |
898 | xfs_file_clone_range( | |
899 | struct file *file_in, | |
900 | loff_t pos_in, | |
901 | struct file *file_out, | |
902 | loff_t pos_out, | |
903 | u64 len) | |
904 | { | |
5faaf4fa | 905 | return xfs_reflink_remap_range(file_in, pos_in, file_out, pos_out, |
cc714660 DW |
906 | len, false); |
907 | } | |
908 | ||
909 | #define XFS_MAX_DEDUPE_LEN (16 * 1024 * 1024) | |
910 | STATIC ssize_t | |
911 | xfs_file_dedupe_range( | |
912 | struct file *src_file, | |
913 | u64 loff, | |
914 | u64 len, | |
915 | struct file *dst_file, | |
916 | u64 dst_loff) | |
917 | { | |
918 | int error; | |
919 | ||
920 | /* | |
921 | * Limit the total length we will dedupe for each operation. | |
922 | * This is intended to bound the total time spent in this | |
923 | * ioctl to something sane. | |
924 | */ | |
925 | if (len > XFS_MAX_DEDUPE_LEN) | |
926 | len = XFS_MAX_DEDUPE_LEN; | |
927 | ||
5faaf4fa | 928 | error = xfs_reflink_remap_range(src_file, loff, dst_file, dst_loff, |
cc714660 DW |
929 | len, true); |
930 | if (error) | |
931 | return error; | |
932 | return len; | |
9fe26045 | 933 | } |
2fe17c10 | 934 | |
1da177e4 | 935 | STATIC int |
3562fd45 | 936 | xfs_file_open( |
1da177e4 | 937 | struct inode *inode, |
f999a5bf | 938 | struct file *file) |
1da177e4 | 939 | { |
f999a5bf | 940 | if (!(file->f_flags & O_LARGEFILE) && i_size_read(inode) > MAX_NON_LFS) |
1da177e4 | 941 | return -EFBIG; |
f999a5bf CH |
942 | if (XFS_FORCED_SHUTDOWN(XFS_M(inode->i_sb))) |
943 | return -EIO; | |
944 | return 0; | |
945 | } | |
946 | ||
947 | STATIC int | |
948 | xfs_dir_open( | |
949 | struct inode *inode, | |
950 | struct file *file) | |
951 | { | |
952 | struct xfs_inode *ip = XFS_I(inode); | |
953 | int mode; | |
954 | int error; | |
955 | ||
956 | error = xfs_file_open(inode, file); | |
957 | if (error) | |
958 | return error; | |
959 | ||
960 | /* | |
961 | * If there are any blocks, read-ahead block 0 as we're almost | |
962 | * certain to have the next operation be a read there. | |
963 | */ | |
309ecac8 | 964 | mode = xfs_ilock_data_map_shared(ip); |
f999a5bf | 965 | if (ip->i_d.di_nextents > 0) |
9df2dd0b | 966 | xfs_dir3_data_readahead(ip, 0, -1); |
f999a5bf CH |
967 | xfs_iunlock(ip, mode); |
968 | return 0; | |
1da177e4 LT |
969 | } |
970 | ||
1da177e4 | 971 | STATIC int |
3562fd45 | 972 | xfs_file_release( |
1da177e4 LT |
973 | struct inode *inode, |
974 | struct file *filp) | |
975 | { | |
2451337d | 976 | return xfs_release(XFS_I(inode)); |
1da177e4 LT |
977 | } |
978 | ||
1da177e4 | 979 | STATIC int |
3562fd45 | 980 | xfs_file_readdir( |
b8227554 AV |
981 | struct file *file, |
982 | struct dir_context *ctx) | |
1da177e4 | 983 | { |
b8227554 | 984 | struct inode *inode = file_inode(file); |
739bfb2a | 985 | xfs_inode_t *ip = XFS_I(inode); |
051e7cd4 CH |
986 | size_t bufsize; |
987 | ||
988 | /* | |
989 | * The Linux API doesn't pass down the total size of the buffer | |
990 | * we read into down to the filesystem. With the filldir concept | |
991 | * it's not needed for correct information, but the XFS dir2 leaf | |
992 | * code wants an estimate of the buffer size to calculate it's | |
993 | * readahead window and size the buffers used for mapping to | |
994 | * physical blocks. | |
995 | * | |
996 | * Try to give it an estimate that's good enough, maybe at some | |
997 | * point we can change the ->readdir prototype to include the | |
a9cc799e | 998 | * buffer size. For now we use the current glibc buffer size. |
051e7cd4 | 999 | */ |
a9cc799e | 1000 | bufsize = (size_t)min_t(loff_t, 32768, ip->i_d.di_size); |
051e7cd4 | 1001 | |
8300475e | 1002 | return xfs_readdir(ip, ctx, bufsize); |
1da177e4 LT |
1003 | } |
1004 | ||
d126d43f JL |
1005 | /* |
1006 | * This type is designed to indicate the type of offset we would like | |
49c69591 | 1007 | * to search from page cache for xfs_seek_hole_data(). |
d126d43f JL |
1008 | */ |
1009 | enum { | |
1010 | HOLE_OFF = 0, | |
1011 | DATA_OFF, | |
1012 | }; | |
1013 | ||
1014 | /* | |
1015 | * Lookup the desired type of offset from the given page. | |
1016 | * | |
1017 | * On success, return true and the offset argument will point to the | |
1018 | * start of the region that was found. Otherwise this function will | |
1019 | * return false and keep the offset argument unchanged. | |
1020 | */ | |
1021 | STATIC bool | |
1022 | xfs_lookup_buffer_offset( | |
1023 | struct page *page, | |
1024 | loff_t *offset, | |
1025 | unsigned int type) | |
1026 | { | |
1027 | loff_t lastoff = page_offset(page); | |
1028 | bool found = false; | |
1029 | struct buffer_head *bh, *head; | |
1030 | ||
1031 | bh = head = page_buffers(page); | |
1032 | do { | |
1033 | /* | |
1034 | * Unwritten extents that have data in the page | |
1035 | * cache covering them can be identified by the | |
1036 | * BH_Unwritten state flag. Pages with multiple | |
1037 | * buffers might have a mix of holes, data and | |
1038 | * unwritten extents - any buffer with valid | |
1039 | * data in it should have BH_Uptodate flag set | |
1040 | * on it. | |
1041 | */ | |
1042 | if (buffer_unwritten(bh) || | |
1043 | buffer_uptodate(bh)) { | |
1044 | if (type == DATA_OFF) | |
1045 | found = true; | |
1046 | } else { | |
1047 | if (type == HOLE_OFF) | |
1048 | found = true; | |
1049 | } | |
1050 | ||
1051 | if (found) { | |
1052 | *offset = lastoff; | |
1053 | break; | |
1054 | } | |
1055 | lastoff += bh->b_size; | |
1056 | } while ((bh = bh->b_this_page) != head); | |
1057 | ||
1058 | return found; | |
1059 | } | |
1060 | ||
1061 | /* | |
1062 | * This routine is called to find out and return a data or hole offset | |
1063 | * from the page cache for unwritten extents according to the desired | |
49c69591 | 1064 | * type for xfs_seek_hole_data(). |
d126d43f JL |
1065 | * |
1066 | * The argument offset is used to tell where we start to search from the | |
1067 | * page cache. Map is used to figure out the end points of the range to | |
1068 | * lookup pages. | |
1069 | * | |
1070 | * Return true if the desired type of offset was found, and the argument | |
1071 | * offset is filled with that address. Otherwise, return false and keep | |
1072 | * offset unchanged. | |
1073 | */ | |
1074 | STATIC bool | |
1075 | xfs_find_get_desired_pgoff( | |
1076 | struct inode *inode, | |
1077 | struct xfs_bmbt_irec *map, | |
1078 | unsigned int type, | |
1079 | loff_t *offset) | |
1080 | { | |
1081 | struct xfs_inode *ip = XFS_I(inode); | |
1082 | struct xfs_mount *mp = ip->i_mount; | |
1083 | struct pagevec pvec; | |
1084 | pgoff_t index; | |
1085 | pgoff_t end; | |
1086 | loff_t endoff; | |
1087 | loff_t startoff = *offset; | |
1088 | loff_t lastoff = startoff; | |
1089 | bool found = false; | |
1090 | ||
1091 | pagevec_init(&pvec, 0); | |
1092 | ||
09cbfeaf | 1093 | index = startoff >> PAGE_SHIFT; |
d126d43f | 1094 | endoff = XFS_FSB_TO_B(mp, map->br_startoff + map->br_blockcount); |
09cbfeaf | 1095 | end = endoff >> PAGE_SHIFT; |
d126d43f JL |
1096 | do { |
1097 | int want; | |
1098 | unsigned nr_pages; | |
1099 | unsigned int i; | |
1100 | ||
1101 | want = min_t(pgoff_t, end - index, PAGEVEC_SIZE); | |
1102 | nr_pages = pagevec_lookup(&pvec, inode->i_mapping, index, | |
1103 | want); | |
1104 | /* | |
1105 | * No page mapped into given range. If we are searching holes | |
1106 | * and if this is the first time we got into the loop, it means | |
1107 | * that the given offset is landed in a hole, return it. | |
1108 | * | |
1109 | * If we have already stepped through some block buffers to find | |
1110 | * holes but they all contains data. In this case, the last | |
1111 | * offset is already updated and pointed to the end of the last | |
1112 | * mapped page, if it does not reach the endpoint to search, | |
1113 | * that means there should be a hole between them. | |
1114 | */ | |
1115 | if (nr_pages == 0) { | |
1116 | /* Data search found nothing */ | |
1117 | if (type == DATA_OFF) | |
1118 | break; | |
1119 | ||
1120 | ASSERT(type == HOLE_OFF); | |
1121 | if (lastoff == startoff || lastoff < endoff) { | |
1122 | found = true; | |
1123 | *offset = lastoff; | |
1124 | } | |
1125 | break; | |
1126 | } | |
1127 | ||
1128 | /* | |
1129 | * At lease we found one page. If this is the first time we | |
1130 | * step into the loop, and if the first page index offset is | |
1131 | * greater than the given search offset, a hole was found. | |
1132 | */ | |
1133 | if (type == HOLE_OFF && lastoff == startoff && | |
1134 | lastoff < page_offset(pvec.pages[0])) { | |
1135 | found = true; | |
1136 | break; | |
1137 | } | |
1138 | ||
1139 | for (i = 0; i < nr_pages; i++) { | |
1140 | struct page *page = pvec.pages[i]; | |
1141 | loff_t b_offset; | |
1142 | ||
1143 | /* | |
1144 | * At this point, the page may be truncated or | |
1145 | * invalidated (changing page->mapping to NULL), | |
1146 | * or even swizzled back from swapper_space to tmpfs | |
1147 | * file mapping. However, page->index will not change | |
1148 | * because we have a reference on the page. | |
1149 | * | |
1150 | * Searching done if the page index is out of range. | |
1151 | * If the current offset is not reaches the end of | |
1152 | * the specified search range, there should be a hole | |
1153 | * between them. | |
1154 | */ | |
1155 | if (page->index > end) { | |
1156 | if (type == HOLE_OFF && lastoff < endoff) { | |
1157 | *offset = lastoff; | |
1158 | found = true; | |
1159 | } | |
1160 | goto out; | |
1161 | } | |
1162 | ||
1163 | lock_page(page); | |
1164 | /* | |
1165 | * Page truncated or invalidated(page->mapping == NULL). | |
1166 | * We can freely skip it and proceed to check the next | |
1167 | * page. | |
1168 | */ | |
1169 | if (unlikely(page->mapping != inode->i_mapping)) { | |
1170 | unlock_page(page); | |
1171 | continue; | |
1172 | } | |
1173 | ||
1174 | if (!page_has_buffers(page)) { | |
1175 | unlock_page(page); | |
1176 | continue; | |
1177 | } | |
1178 | ||
1179 | found = xfs_lookup_buffer_offset(page, &b_offset, type); | |
1180 | if (found) { | |
1181 | /* | |
1182 | * The found offset may be less than the start | |
1183 | * point to search if this is the first time to | |
1184 | * come here. | |
1185 | */ | |
1186 | *offset = max_t(loff_t, startoff, b_offset); | |
1187 | unlock_page(page); | |
1188 | goto out; | |
1189 | } | |
1190 | ||
1191 | /* | |
1192 | * We either searching data but nothing was found, or | |
1193 | * searching hole but found a data buffer. In either | |
1194 | * case, probably the next page contains the desired | |
1195 | * things, update the last offset to it so. | |
1196 | */ | |
1197 | lastoff = page_offset(page) + PAGE_SIZE; | |
1198 | unlock_page(page); | |
1199 | } | |
1200 | ||
1201 | /* | |
1202 | * The number of returned pages less than our desired, search | |
1203 | * done. In this case, nothing was found for searching data, | |
1204 | * but we found a hole behind the last offset. | |
1205 | */ | |
1206 | if (nr_pages < want) { | |
1207 | if (type == HOLE_OFF) { | |
1208 | *offset = lastoff; | |
1209 | found = true; | |
1210 | } | |
1211 | break; | |
1212 | } | |
1213 | ||
1214 | index = pvec.pages[i - 1]->index + 1; | |
1215 | pagevec_release(&pvec); | |
1216 | } while (index <= end); | |
1217 | ||
1218 | out: | |
1219 | pagevec_release(&pvec); | |
1220 | return found; | |
1221 | } | |
1222 | ||
8aa7d37e ES |
1223 | /* |
1224 | * caller must lock inode with xfs_ilock_data_map_shared, | |
1225 | * can we craft an appropriate ASSERT? | |
1226 | * | |
1227 | * end is because the VFS-level lseek interface is defined such that any | |
1228 | * offset past i_size shall return -ENXIO, but we use this for quota code | |
1229 | * which does not maintain i_size, and we want to SEEK_DATA past i_size. | |
1230 | */ | |
1231 | loff_t | |
1232 | __xfs_seek_hole_data( | |
1233 | struct inode *inode, | |
49c69591 | 1234 | loff_t start, |
8aa7d37e | 1235 | loff_t end, |
49c69591 | 1236 | int whence) |
3fe3e6b1 | 1237 | { |
3fe3e6b1 JL |
1238 | struct xfs_inode *ip = XFS_I(inode); |
1239 | struct xfs_mount *mp = ip->i_mount; | |
3fe3e6b1 | 1240 | loff_t uninitialized_var(offset); |
3fe3e6b1 | 1241 | xfs_fileoff_t fsbno; |
8aa7d37e | 1242 | xfs_filblks_t lastbno; |
3fe3e6b1 JL |
1243 | int error; |
1244 | ||
8aa7d37e | 1245 | if (start >= end) { |
2451337d | 1246 | error = -ENXIO; |
8aa7d37e | 1247 | goto out_error; |
3fe3e6b1 JL |
1248 | } |
1249 | ||
3fe3e6b1 JL |
1250 | /* |
1251 | * Try to read extents from the first block indicated | |
1252 | * by fsbno to the end block of the file. | |
1253 | */ | |
52f1acc8 | 1254 | fsbno = XFS_B_TO_FSBT(mp, start); |
8aa7d37e | 1255 | lastbno = XFS_B_TO_FSB(mp, end); |
49c69591 | 1256 | |
52f1acc8 JL |
1257 | for (;;) { |
1258 | struct xfs_bmbt_irec map[2]; | |
1259 | int nmap = 2; | |
1260 | unsigned int i; | |
3fe3e6b1 | 1261 | |
8aa7d37e | 1262 | error = xfs_bmapi_read(ip, fsbno, lastbno - fsbno, map, &nmap, |
52f1acc8 JL |
1263 | XFS_BMAPI_ENTIRE); |
1264 | if (error) | |
8aa7d37e | 1265 | goto out_error; |
3fe3e6b1 | 1266 | |
52f1acc8 JL |
1267 | /* No extents at given offset, must be beyond EOF */ |
1268 | if (nmap == 0) { | |
2451337d | 1269 | error = -ENXIO; |
8aa7d37e | 1270 | goto out_error; |
52f1acc8 JL |
1271 | } |
1272 | ||
1273 | for (i = 0; i < nmap; i++) { | |
1274 | offset = max_t(loff_t, start, | |
1275 | XFS_FSB_TO_B(mp, map[i].br_startoff)); | |
1276 | ||
49c69591 ES |
1277 | /* Landed in the hole we wanted? */ |
1278 | if (whence == SEEK_HOLE && | |
1279 | map[i].br_startblock == HOLESTARTBLOCK) | |
1280 | goto out; | |
1281 | ||
1282 | /* Landed in the data extent we wanted? */ | |
1283 | if (whence == SEEK_DATA && | |
1284 | (map[i].br_startblock == DELAYSTARTBLOCK || | |
1285 | (map[i].br_state == XFS_EXT_NORM && | |
1286 | !isnullstartblock(map[i].br_startblock)))) | |
52f1acc8 JL |
1287 | goto out; |
1288 | ||
1289 | /* | |
49c69591 ES |
1290 | * Landed in an unwritten extent, try to search |
1291 | * for hole or data from page cache. | |
52f1acc8 JL |
1292 | */ |
1293 | if (map[i].br_state == XFS_EXT_UNWRITTEN) { | |
1294 | if (xfs_find_get_desired_pgoff(inode, &map[i], | |
49c69591 ES |
1295 | whence == SEEK_HOLE ? HOLE_OFF : DATA_OFF, |
1296 | &offset)) | |
52f1acc8 JL |
1297 | goto out; |
1298 | } | |
1299 | } | |
1300 | ||
1301 | /* | |
49c69591 ES |
1302 | * We only received one extent out of the two requested. This |
1303 | * means we've hit EOF and didn't find what we are looking for. | |
52f1acc8 | 1304 | */ |
3fe3e6b1 | 1305 | if (nmap == 1) { |
49c69591 ES |
1306 | /* |
1307 | * If we were looking for a hole, set offset to | |
1308 | * the end of the file (i.e., there is an implicit | |
1309 | * hole at the end of any file). | |
1310 | */ | |
1311 | if (whence == SEEK_HOLE) { | |
8aa7d37e | 1312 | offset = end; |
49c69591 ES |
1313 | break; |
1314 | } | |
1315 | /* | |
1316 | * If we were looking for data, it's nowhere to be found | |
1317 | */ | |
1318 | ASSERT(whence == SEEK_DATA); | |
2451337d | 1319 | error = -ENXIO; |
8aa7d37e | 1320 | goto out_error; |
3fe3e6b1 JL |
1321 | } |
1322 | ||
52f1acc8 JL |
1323 | ASSERT(i > 1); |
1324 | ||
1325 | /* | |
1326 | * Nothing was found, proceed to the next round of search | |
49c69591 | 1327 | * if the next reading offset is not at or beyond EOF. |
52f1acc8 JL |
1328 | */ |
1329 | fsbno = map[i - 1].br_startoff + map[i - 1].br_blockcount; | |
1330 | start = XFS_FSB_TO_B(mp, fsbno); | |
8aa7d37e | 1331 | if (start >= end) { |
49c69591 | 1332 | if (whence == SEEK_HOLE) { |
8aa7d37e | 1333 | offset = end; |
49c69591 ES |
1334 | break; |
1335 | } | |
1336 | ASSERT(whence == SEEK_DATA); | |
2451337d | 1337 | error = -ENXIO; |
8aa7d37e | 1338 | goto out_error; |
52f1acc8 | 1339 | } |
3fe3e6b1 JL |
1340 | } |
1341 | ||
b686d1f7 JL |
1342 | out: |
1343 | /* | |
49c69591 | 1344 | * If at this point we have found the hole we wanted, the returned |
b686d1f7 | 1345 | * offset may be bigger than the file size as it may be aligned to |
49c69591 | 1346 | * page boundary for unwritten extents. We need to deal with this |
b686d1f7 JL |
1347 | * situation in particular. |
1348 | */ | |
49c69591 | 1349 | if (whence == SEEK_HOLE) |
8aa7d37e ES |
1350 | offset = min_t(loff_t, offset, end); |
1351 | ||
1352 | return offset; | |
1353 | ||
1354 | out_error: | |
1355 | return error; | |
1356 | } | |
1357 | ||
1358 | STATIC loff_t | |
1359 | xfs_seek_hole_data( | |
1360 | struct file *file, | |
1361 | loff_t start, | |
1362 | int whence) | |
1363 | { | |
1364 | struct inode *inode = file->f_mapping->host; | |
1365 | struct xfs_inode *ip = XFS_I(inode); | |
1366 | struct xfs_mount *mp = ip->i_mount; | |
1367 | uint lock; | |
1368 | loff_t offset, end; | |
1369 | int error = 0; | |
1370 | ||
1371 | if (XFS_FORCED_SHUTDOWN(mp)) | |
1372 | return -EIO; | |
1373 | ||
1374 | lock = xfs_ilock_data_map_shared(ip); | |
1375 | ||
1376 | end = i_size_read(inode); | |
1377 | offset = __xfs_seek_hole_data(inode, start, end, whence); | |
1378 | if (offset < 0) { | |
1379 | error = offset; | |
1380 | goto out_unlock; | |
1381 | } | |
1382 | ||
46a1c2c7 | 1383 | offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes); |
3fe3e6b1 JL |
1384 | |
1385 | out_unlock: | |
01f4f327 | 1386 | xfs_iunlock(ip, lock); |
3fe3e6b1 JL |
1387 | |
1388 | if (error) | |
2451337d | 1389 | return error; |
3fe3e6b1 JL |
1390 | return offset; |
1391 | } | |
1392 | ||
1393 | STATIC loff_t | |
1394 | xfs_file_llseek( | |
1395 | struct file *file, | |
1396 | loff_t offset, | |
59f9c004 | 1397 | int whence) |
3fe3e6b1 | 1398 | { |
59f9c004 | 1399 | switch (whence) { |
3fe3e6b1 JL |
1400 | case SEEK_END: |
1401 | case SEEK_CUR: | |
1402 | case SEEK_SET: | |
59f9c004 | 1403 | return generic_file_llseek(file, offset, whence); |
3fe3e6b1 | 1404 | case SEEK_HOLE: |
49c69591 | 1405 | case SEEK_DATA: |
59f9c004 | 1406 | return xfs_seek_hole_data(file, offset, whence); |
3fe3e6b1 JL |
1407 | default: |
1408 | return -EINVAL; | |
1409 | } | |
1410 | } | |
1411 | ||
de0e8c20 DC |
1412 | /* |
1413 | * Locking for serialisation of IO during page faults. This results in a lock | |
1414 | * ordering of: | |
1415 | * | |
1416 | * mmap_sem (MM) | |
6b698ede | 1417 | * sb_start_pagefault(vfs, freeze) |
13ad4fe3 | 1418 | * i_mmaplock (XFS - truncate serialisation) |
6b698ede DC |
1419 | * page_lock (MM) |
1420 | * i_lock (XFS - extent map serialisation) | |
de0e8c20 | 1421 | */ |
de0e8c20 | 1422 | |
075a924d DC |
1423 | /* |
1424 | * mmap()d file has taken write protection fault and is being made writable. We | |
1425 | * can set the page state up correctly for a writable page, which means we can | |
1426 | * do correct delalloc accounting (ENOSPC checking!) and unwritten extent | |
1427 | * mapping. | |
de0e8c20 DC |
1428 | */ |
1429 | STATIC int | |
075a924d | 1430 | xfs_filemap_page_mkwrite( |
de0e8c20 DC |
1431 | struct vm_area_struct *vma, |
1432 | struct vm_fault *vmf) | |
1433 | { | |
6b698ede | 1434 | struct inode *inode = file_inode(vma->vm_file); |
ec56b1f1 | 1435 | int ret; |
de0e8c20 | 1436 | |
6b698ede | 1437 | trace_xfs_filemap_page_mkwrite(XFS_I(inode)); |
de0e8c20 | 1438 | |
6b698ede | 1439 | sb_start_pagefault(inode->i_sb); |
ec56b1f1 | 1440 | file_update_time(vma->vm_file); |
6b698ede | 1441 | xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED); |
de0e8c20 | 1442 | |
6b698ede | 1443 | if (IS_DAX(inode)) { |
11c59c92 | 1444 | ret = dax_iomap_fault(vma, vmf, &xfs_iomap_ops); |
6b698ede | 1445 | } else { |
68a9f5e7 | 1446 | ret = iomap_page_mkwrite(vma, vmf, &xfs_iomap_ops); |
6b698ede DC |
1447 | ret = block_page_mkwrite_return(ret); |
1448 | } | |
1449 | ||
1450 | xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED); | |
1451 | sb_end_pagefault(inode->i_sb); | |
1452 | ||
1453 | return ret; | |
de0e8c20 DC |
1454 | } |
1455 | ||
075a924d | 1456 | STATIC int |
6b698ede | 1457 | xfs_filemap_fault( |
075a924d DC |
1458 | struct vm_area_struct *vma, |
1459 | struct vm_fault *vmf) | |
1460 | { | |
b2442c5a | 1461 | struct inode *inode = file_inode(vma->vm_file); |
6b698ede | 1462 | int ret; |
ec56b1f1 | 1463 | |
b2442c5a | 1464 | trace_xfs_filemap_fault(XFS_I(inode)); |
075a924d | 1465 | |
6b698ede | 1466 | /* DAX can shortcut the normal fault path on write faults! */ |
b2442c5a | 1467 | if ((vmf->flags & FAULT_FLAG_WRITE) && IS_DAX(inode)) |
6b698ede | 1468 | return xfs_filemap_page_mkwrite(vma, vmf); |
075a924d | 1469 | |
b2442c5a DC |
1470 | xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED); |
1471 | if (IS_DAX(inode)) { | |
1472 | /* | |
1473 | * we do not want to trigger unwritten extent conversion on read | |
1474 | * faults - that is unnecessary overhead and would also require | |
1475 | * changes to xfs_get_blocks_direct() to map unwritten extent | |
1476 | * ioend for conversion on read-only mappings. | |
1477 | */ | |
11c59c92 | 1478 | ret = dax_iomap_fault(vma, vmf, &xfs_iomap_ops); |
b2442c5a DC |
1479 | } else |
1480 | ret = filemap_fault(vma, vmf); | |
1481 | xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED); | |
075a924d | 1482 | |
6b698ede DC |
1483 | return ret; |
1484 | } | |
1485 | ||
13ad4fe3 DC |
1486 | /* |
1487 | * Similar to xfs_filemap_fault(), the DAX fault path can call into here on | |
1488 | * both read and write faults. Hence we need to handle both cases. There is no | |
1489 | * ->pmd_mkwrite callout for huge pages, so we have a single function here to | |
1490 | * handle both cases here. @flags carries the information on the type of fault | |
1491 | * occuring. | |
1492 | */ | |
acd76e74 MW |
1493 | STATIC int |
1494 | xfs_filemap_pmd_fault( | |
1495 | struct vm_area_struct *vma, | |
1496 | unsigned long addr, | |
1497 | pmd_t *pmd, | |
1498 | unsigned int flags) | |
1499 | { | |
1500 | struct inode *inode = file_inode(vma->vm_file); | |
1501 | struct xfs_inode *ip = XFS_I(inode); | |
1502 | int ret; | |
1503 | ||
1504 | if (!IS_DAX(inode)) | |
1505 | return VM_FAULT_FALLBACK; | |
1506 | ||
1507 | trace_xfs_filemap_pmd_fault(ip); | |
1508 | ||
13ad4fe3 DC |
1509 | if (flags & FAULT_FLAG_WRITE) { |
1510 | sb_start_pagefault(inode->i_sb); | |
1511 | file_update_time(vma->vm_file); | |
1512 | } | |
1513 | ||
acd76e74 | 1514 | xfs_ilock(XFS_I(inode), XFS_MMAPLOCK_SHARED); |
862f1b9d | 1515 | ret = dax_iomap_pmd_fault(vma, addr, pmd, flags, &xfs_iomap_ops); |
acd76e74 | 1516 | xfs_iunlock(XFS_I(inode), XFS_MMAPLOCK_SHARED); |
acd76e74 | 1517 | |
13ad4fe3 DC |
1518 | if (flags & FAULT_FLAG_WRITE) |
1519 | sb_end_pagefault(inode->i_sb); | |
acd76e74 MW |
1520 | |
1521 | return ret; | |
1522 | } | |
1523 | ||
3af49285 DC |
1524 | /* |
1525 | * pfn_mkwrite was originally inteneded to ensure we capture time stamp | |
1526 | * updates on write faults. In reality, it's need to serialise against | |
5eb88dca RZ |
1527 | * truncate similar to page_mkwrite. Hence we cycle the XFS_MMAPLOCK_SHARED |
1528 | * to ensure we serialise the fault barrier in place. | |
3af49285 DC |
1529 | */ |
1530 | static int | |
1531 | xfs_filemap_pfn_mkwrite( | |
1532 | struct vm_area_struct *vma, | |
1533 | struct vm_fault *vmf) | |
1534 | { | |
1535 | ||
1536 | struct inode *inode = file_inode(vma->vm_file); | |
1537 | struct xfs_inode *ip = XFS_I(inode); | |
1538 | int ret = VM_FAULT_NOPAGE; | |
1539 | loff_t size; | |
1540 | ||
1541 | trace_xfs_filemap_pfn_mkwrite(ip); | |
1542 | ||
1543 | sb_start_pagefault(inode->i_sb); | |
1544 | file_update_time(vma->vm_file); | |
1545 | ||
1546 | /* check if the faulting page hasn't raced with truncate */ | |
1547 | xfs_ilock(ip, XFS_MMAPLOCK_SHARED); | |
1548 | size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT; | |
1549 | if (vmf->pgoff >= size) | |
1550 | ret = VM_FAULT_SIGBUS; | |
5eb88dca RZ |
1551 | else if (IS_DAX(inode)) |
1552 | ret = dax_pfn_mkwrite(vma, vmf); | |
3af49285 DC |
1553 | xfs_iunlock(ip, XFS_MMAPLOCK_SHARED); |
1554 | sb_end_pagefault(inode->i_sb); | |
acd76e74 | 1555 | return ret; |
3af49285 | 1556 | |
acd76e74 MW |
1557 | } |
1558 | ||
6b698ede DC |
1559 | static const struct vm_operations_struct xfs_file_vm_ops = { |
1560 | .fault = xfs_filemap_fault, | |
acd76e74 | 1561 | .pmd_fault = xfs_filemap_pmd_fault, |
6b698ede DC |
1562 | .map_pages = filemap_map_pages, |
1563 | .page_mkwrite = xfs_filemap_page_mkwrite, | |
3af49285 | 1564 | .pfn_mkwrite = xfs_filemap_pfn_mkwrite, |
6b698ede DC |
1565 | }; |
1566 | ||
1567 | STATIC int | |
1568 | xfs_file_mmap( | |
1569 | struct file *filp, | |
1570 | struct vm_area_struct *vma) | |
1571 | { | |
1572 | file_accessed(filp); | |
1573 | vma->vm_ops = &xfs_file_vm_ops; | |
1574 | if (IS_DAX(file_inode(filp))) | |
acd76e74 | 1575 | vma->vm_flags |= VM_MIXEDMAP | VM_HUGEPAGE; |
6b698ede | 1576 | return 0; |
075a924d DC |
1577 | } |
1578 | ||
4b6f5d20 | 1579 | const struct file_operations xfs_file_operations = { |
3fe3e6b1 | 1580 | .llseek = xfs_file_llseek, |
b4f5d2c6 | 1581 | .read_iter = xfs_file_read_iter, |
bf97f3bc | 1582 | .write_iter = xfs_file_write_iter, |
82c156f8 | 1583 | .splice_read = generic_file_splice_read, |
8d020765 | 1584 | .splice_write = iter_file_splice_write, |
3562fd45 | 1585 | .unlocked_ioctl = xfs_file_ioctl, |
1da177e4 | 1586 | #ifdef CONFIG_COMPAT |
3562fd45 | 1587 | .compat_ioctl = xfs_file_compat_ioctl, |
1da177e4 | 1588 | #endif |
3562fd45 NS |
1589 | .mmap = xfs_file_mmap, |
1590 | .open = xfs_file_open, | |
1591 | .release = xfs_file_release, | |
1592 | .fsync = xfs_file_fsync, | |
dbe6ec81 | 1593 | .get_unmapped_area = thp_get_unmapped_area, |
2fe17c10 | 1594 | .fallocate = xfs_file_fallocate, |
9fe26045 DW |
1595 | .copy_file_range = xfs_file_copy_range, |
1596 | .clone_file_range = xfs_file_clone_range, | |
cc714660 | 1597 | .dedupe_file_range = xfs_file_dedupe_range, |
1da177e4 LT |
1598 | }; |
1599 | ||
4b6f5d20 | 1600 | const struct file_operations xfs_dir_file_operations = { |
f999a5bf | 1601 | .open = xfs_dir_open, |
1da177e4 | 1602 | .read = generic_read_dir, |
3b0a3c1a | 1603 | .iterate_shared = xfs_file_readdir, |
59af1584 | 1604 | .llseek = generic_file_llseek, |
3562fd45 | 1605 | .unlocked_ioctl = xfs_file_ioctl, |
d3870398 | 1606 | #ifdef CONFIG_COMPAT |
3562fd45 | 1607 | .compat_ioctl = xfs_file_compat_ioctl, |
d3870398 | 1608 | #endif |
1da2f2db | 1609 | .fsync = xfs_dir_fsync, |
1da177e4 | 1610 | }; |