]>
Commit | Line | Data |
---|---|---|
0b61f8a4 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 | 2 | /* |
7b718769 | 3 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
98c1a7c0 | 4 | * Copyright (c) 2016-2018 Christoph Hellwig. |
7b718769 | 5 | * All Rights Reserved. |
1da177e4 | 6 | */ |
1da177e4 | 7 | #include "xfs.h" |
70a9883c | 8 | #include "xfs_shared.h" |
239880ef DC |
9 | #include "xfs_format.h" |
10 | #include "xfs_log_format.h" | |
11 | #include "xfs_trans_resv.h" | |
1da177e4 | 12 | #include "xfs_mount.h" |
1da177e4 | 13 | #include "xfs_inode.h" |
239880ef | 14 | #include "xfs_trans.h" |
1da177e4 | 15 | #include "xfs_iomap.h" |
0b1b213f | 16 | #include "xfs_trace.h" |
3ed3a434 | 17 | #include "xfs_bmap.h" |
68988114 | 18 | #include "xfs_bmap_util.h" |
ef473667 | 19 | #include "xfs_reflink.h" |
1da177e4 | 20 | |
fbcc0256 | 21 | struct xfs_writepage_ctx { |
598ecfba | 22 | struct iomap_writepage_ctx ctx; |
d9252d52 | 23 | unsigned int data_seq; |
e666aa37 | 24 | unsigned int cow_seq; |
fbcc0256 DC |
25 | }; |
26 | ||
598ecfba CH |
27 | static inline struct xfs_writepage_ctx * |
28 | XFS_WPC(struct iomap_writepage_ctx *ctx) | |
29 | { | |
30 | return container_of(ctx, struct xfs_writepage_ctx, ctx); | |
31 | } | |
32 | ||
fc0063c4 CH |
33 | /* |
34 | * Fast and loose check if this write could update the on-disk inode size. | |
35 | */ | |
598ecfba | 36 | static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend) |
fc0063c4 CH |
37 | { |
38 | return ioend->io_offset + ioend->io_size > | |
39 | XFS_I(ioend->io_inode)->i_d.di_size; | |
40 | } | |
41 | ||
281627df CH |
42 | STATIC int |
43 | xfs_setfilesize_trans_alloc( | |
598ecfba | 44 | struct iomap_ioend *ioend) |
281627df CH |
45 | { |
46 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; | |
47 | struct xfs_trans *tp; | |
48 | int error; | |
49 | ||
73d30d48 | 50 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); |
253f4911 | 51 | if (error) |
281627df | 52 | return error; |
281627df | 53 | |
5653017b | 54 | ioend->io_private = tp; |
281627df | 55 | |
d9457dc0 | 56 | /* |
437a255a | 57 | * We may pass freeze protection with a transaction. So tell lockdep |
d9457dc0 JK |
58 | * we released it. |
59 | */ | |
bee9182d | 60 | __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS); |
281627df CH |
61 | /* |
62 | * We hand off the transaction to the completion thread now, so | |
63 | * clear the flag here. | |
64 | */ | |
9070733b | 65 | current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
281627df CH |
66 | return 0; |
67 | } | |
68 | ||
ba87ea69 | 69 | /* |
2813d682 | 70 | * Update on-disk file size now that data has been written to disk. |
ba87ea69 | 71 | */ |
281627df | 72 | STATIC int |
e372843a | 73 | __xfs_setfilesize( |
2ba66237 CH |
74 | struct xfs_inode *ip, |
75 | struct xfs_trans *tp, | |
76 | xfs_off_t offset, | |
77 | size_t size) | |
ba87ea69 | 78 | { |
ba87ea69 | 79 | xfs_fsize_t isize; |
ba87ea69 | 80 | |
aa6bf01d | 81 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
2ba66237 | 82 | isize = xfs_new_eof(ip, offset + size); |
281627df CH |
83 | if (!isize) { |
84 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
4906e215 | 85 | xfs_trans_cancel(tp); |
281627df | 86 | return 0; |
ba87ea69 LM |
87 | } |
88 | ||
2ba66237 | 89 | trace_xfs_setfilesize(ip, offset, size); |
281627df CH |
90 | |
91 | ip->i_d.di_size = isize; | |
92 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
93 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
94 | ||
70393313 | 95 | return xfs_trans_commit(tp); |
77d7a0c2 DC |
96 | } |
97 | ||
e372843a CH |
98 | int |
99 | xfs_setfilesize( | |
100 | struct xfs_inode *ip, | |
101 | xfs_off_t offset, | |
102 | size_t size) | |
103 | { | |
104 | struct xfs_mount *mp = ip->i_mount; | |
105 | struct xfs_trans *tp; | |
106 | int error; | |
107 | ||
108 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); | |
109 | if (error) | |
110 | return error; | |
111 | ||
112 | return __xfs_setfilesize(ip, tp, offset, size); | |
113 | } | |
114 | ||
2ba66237 CH |
115 | STATIC int |
116 | xfs_setfilesize_ioend( | |
598ecfba | 117 | struct iomap_ioend *ioend, |
0e51a8e1 | 118 | int error) |
2ba66237 CH |
119 | { |
120 | struct xfs_inode *ip = XFS_I(ioend->io_inode); | |
5653017b | 121 | struct xfs_trans *tp = ioend->io_private; |
2ba66237 CH |
122 | |
123 | /* | |
124 | * The transaction may have been allocated in the I/O submission thread, | |
125 | * thus we need to mark ourselves as being in a transaction manually. | |
126 | * Similarly for freeze protection. | |
127 | */ | |
9070733b | 128 | current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS); |
bee9182d | 129 | __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS); |
2ba66237 | 130 | |
5cb13dcd | 131 | /* we abort the update if there was an IO error */ |
0e51a8e1 | 132 | if (error) { |
5cb13dcd | 133 | xfs_trans_cancel(tp); |
0e51a8e1 | 134 | return error; |
5cb13dcd Z |
135 | } |
136 | ||
e372843a | 137 | return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size); |
2ba66237 CH |
138 | } |
139 | ||
0829c360 | 140 | /* |
5ec4fabb | 141 | * IO write completion. |
f6d6d4fc CH |
142 | */ |
143 | STATIC void | |
cb357bf3 | 144 | xfs_end_ioend( |
598ecfba | 145 | struct iomap_ioend *ioend) |
0829c360 | 146 | { |
0e51a8e1 | 147 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
787eb485 CH |
148 | xfs_off_t offset = ioend->io_offset; |
149 | size_t size = ioend->io_size; | |
73d30d48 | 150 | unsigned int nofs_flag; |
4e4cbee9 | 151 | int error; |
ba87ea69 | 152 | |
73d30d48 CH |
153 | /* |
154 | * We can allocate memory here while doing writeback on behalf of | |
155 | * memory reclaim. To avoid memory allocation deadlocks set the | |
156 | * task-wide nofs context for the following operations. | |
157 | */ | |
158 | nofs_flag = memalloc_nofs_save(); | |
159 | ||
af055e37 | 160 | /* |
787eb485 | 161 | * Just clean up the in-memory strutures if the fs has been shut down. |
af055e37 | 162 | */ |
787eb485 | 163 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
0e51a8e1 | 164 | error = -EIO; |
787eb485 CH |
165 | goto done; |
166 | } | |
04f658ee | 167 | |
43caeb18 | 168 | /* |
787eb485 | 169 | * Clean up any COW blocks on an I/O error. |
43caeb18 | 170 | */ |
4e4cbee9 | 171 | error = blk_status_to_errno(ioend->io_bio->bi_status); |
787eb485 | 172 | if (unlikely(error)) { |
760fea8b | 173 | if (ioend->io_flags & IOMAP_F_SHARED) |
787eb485 | 174 | xfs_reflink_cancel_cow_range(ip, offset, size, true); |
787eb485 | 175 | goto done; |
43caeb18 DW |
176 | } |
177 | ||
5ec4fabb | 178 | /* |
be225fec | 179 | * Success: commit the COW or unwritten blocks if needed. |
5ec4fabb | 180 | */ |
760fea8b | 181 | if (ioend->io_flags & IOMAP_F_SHARED) |
787eb485 | 182 | error = xfs_reflink_end_cow(ip, offset, size); |
4e087a3b | 183 | else if (ioend->io_type == IOMAP_UNWRITTEN) |
ee70daab | 184 | error = xfs_iomap_write_unwritten(ip, offset, size, false); |
be225fec | 185 | else |
5653017b | 186 | ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_private); |
ba87ea69 | 187 | |
04f658ee | 188 | done: |
5653017b | 189 | if (ioend->io_private) |
787eb485 | 190 | error = xfs_setfilesize_ioend(ioend, error); |
598ecfba | 191 | iomap_finish_ioends(ioend, error); |
73d30d48 | 192 | memalloc_nofs_restore(nofs_flag); |
3994fc48 DW |
193 | } |
194 | ||
7dbae9fb CH |
195 | /* |
196 | * If the to be merged ioend has a preallocated transaction for file | |
197 | * size updates we need to ensure the ioend it is merged into also | |
198 | * has one. If it already has one we can simply cancel the transaction | |
199 | * as it is guaranteed to be clean. | |
200 | */ | |
201 | static void | |
5653017b | 202 | xfs_ioend_merge_private( |
598ecfba CH |
203 | struct iomap_ioend *ioend, |
204 | struct iomap_ioend *next) | |
7dbae9fb | 205 | { |
5653017b CH |
206 | if (!ioend->io_private) { |
207 | ioend->io_private = next->io_private; | |
208 | next->io_private = NULL; | |
7dbae9fb CH |
209 | } else { |
210 | xfs_setfilesize_ioend(next, -ECANCELED); | |
211 | } | |
212 | } | |
213 | ||
cb357bf3 DW |
214 | /* Finish all pending io completions. */ |
215 | void | |
216 | xfs_end_io( | |
217 | struct work_struct *work) | |
218 | { | |
433dad94 CH |
219 | struct xfs_inode *ip = |
220 | container_of(work, struct xfs_inode, i_ioend_work); | |
598ecfba | 221 | struct iomap_ioend *ioend; |
433dad94 | 222 | struct list_head tmp; |
cb357bf3 DW |
223 | unsigned long flags; |
224 | ||
cb357bf3 | 225 | spin_lock_irqsave(&ip->i_ioend_lock, flags); |
433dad94 | 226 | list_replace_init(&ip->i_ioend_list, &tmp); |
cb357bf3 DW |
227 | spin_unlock_irqrestore(&ip->i_ioend_lock, flags); |
228 | ||
598ecfba CH |
229 | iomap_sort_ioends(&tmp); |
230 | while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend, | |
433dad94 | 231 | io_list))) { |
cb357bf3 | 232 | list_del_init(&ioend->io_list); |
598ecfba | 233 | iomap_ioend_try_merge(ioend, &tmp, xfs_ioend_merge_private); |
cb357bf3 DW |
234 | xfs_end_ioend(ioend); |
235 | } | |
236 | } | |
237 | ||
598ecfba | 238 | static inline bool xfs_ioend_needs_workqueue(struct iomap_ioend *ioend) |
760fea8b CH |
239 | { |
240 | return ioend->io_private || | |
241 | ioend->io_type == IOMAP_UNWRITTEN || | |
242 | (ioend->io_flags & IOMAP_F_SHARED); | |
243 | } | |
244 | ||
0e51a8e1 CH |
245 | STATIC void |
246 | xfs_end_bio( | |
247 | struct bio *bio) | |
0829c360 | 248 | { |
598ecfba | 249 | struct iomap_ioend *ioend = bio->bi_private; |
cb357bf3 | 250 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
cb357bf3 | 251 | unsigned long flags; |
0829c360 | 252 | |
598ecfba CH |
253 | ASSERT(xfs_ioend_needs_workqueue(ioend)); |
254 | ||
255 | spin_lock_irqsave(&ip->i_ioend_lock, flags); | |
256 | if (list_empty(&ip->i_ioend_list)) | |
257 | WARN_ON_ONCE(!queue_work(ip->i_mount->m_unwritten_workqueue, | |
258 | &ip->i_ioend_work)); | |
259 | list_add_tail(&ioend->io_list, &ip->i_ioend_list); | |
260 | spin_unlock_irqrestore(&ip->i_ioend_lock, flags); | |
0829c360 CH |
261 | } |
262 | ||
d9252d52 BF |
263 | /* |
264 | * Fast revalidation of the cached writeback mapping. Return true if the current | |
265 | * mapping is valid, false otherwise. | |
266 | */ | |
267 | static bool | |
268 | xfs_imap_valid( | |
598ecfba | 269 | struct iomap_writepage_ctx *wpc, |
d9252d52 | 270 | struct xfs_inode *ip, |
4e087a3b | 271 | loff_t offset) |
d9252d52 | 272 | { |
4e087a3b CH |
273 | if (offset < wpc->iomap.offset || |
274 | offset >= wpc->iomap.offset + wpc->iomap.length) | |
d9252d52 BF |
275 | return false; |
276 | /* | |
277 | * If this is a COW mapping, it is sufficient to check that the mapping | |
278 | * covers the offset. Be careful to check this first because the caller | |
279 | * can revalidate a COW mapping without updating the data seqno. | |
280 | */ | |
760fea8b | 281 | if (wpc->iomap.flags & IOMAP_F_SHARED) |
d9252d52 BF |
282 | return true; |
283 | ||
284 | /* | |
285 | * This is not a COW mapping. Check the sequence number of the data fork | |
286 | * because concurrent changes could have invalidated the extent. Check | |
287 | * the COW fork because concurrent changes since the last time we | |
288 | * checked (and found nothing at this offset) could have added | |
289 | * overlapping blocks. | |
290 | */ | |
598ecfba | 291 | if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq)) |
d9252d52 BF |
292 | return false; |
293 | if (xfs_inode_has_cow_data(ip) && | |
598ecfba | 294 | XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq)) |
d9252d52 BF |
295 | return false; |
296 | return true; | |
297 | } | |
298 | ||
4ad765ed CH |
299 | /* |
300 | * Pass in a dellalloc extent and convert it to real extents, return the real | |
4e087a3b | 301 | * extent that maps offset_fsb in wpc->iomap. |
4ad765ed CH |
302 | * |
303 | * The current page is held locked so nothing could have removed the block | |
7588cbee CH |
304 | * backing offset_fsb, although it could have moved from the COW to the data |
305 | * fork by another thread. | |
4ad765ed CH |
306 | */ |
307 | static int | |
308 | xfs_convert_blocks( | |
598ecfba | 309 | struct iomap_writepage_ctx *wpc, |
4ad765ed | 310 | struct xfs_inode *ip, |
760fea8b | 311 | int whichfork, |
4e087a3b | 312 | loff_t offset) |
4ad765ed CH |
313 | { |
314 | int error; | |
598ecfba CH |
315 | unsigned *seq; |
316 | ||
317 | if (whichfork == XFS_COW_FORK) | |
318 | seq = &XFS_WPC(wpc)->cow_seq; | |
319 | else | |
320 | seq = &XFS_WPC(wpc)->data_seq; | |
4ad765ed CH |
321 | |
322 | /* | |
4e087a3b CH |
323 | * Attempt to allocate whatever delalloc extent currently backs offset |
324 | * and put the result into wpc->iomap. Allocate in a loop because it | |
325 | * may take several attempts to allocate real blocks for a contiguous | |
326 | * delalloc extent if free space is sufficiently fragmented. | |
4ad765ed CH |
327 | */ |
328 | do { | |
760fea8b | 329 | error = xfs_bmapi_convert_delalloc(ip, whichfork, offset, |
598ecfba | 330 | &wpc->iomap, seq); |
4ad765ed CH |
331 | if (error) |
332 | return error; | |
4e087a3b | 333 | } while (wpc->iomap.offset + wpc->iomap.length <= offset); |
4ad765ed CH |
334 | |
335 | return 0; | |
336 | } | |
337 | ||
598ecfba | 338 | static int |
1da177e4 | 339 | xfs_map_blocks( |
598ecfba | 340 | struct iomap_writepage_ctx *wpc, |
1da177e4 | 341 | struct inode *inode, |
5c665e5b | 342 | loff_t offset) |
1da177e4 | 343 | { |
a206c817 CH |
344 | struct xfs_inode *ip = XFS_I(inode); |
345 | struct xfs_mount *mp = ip->i_mount; | |
93407472 | 346 | ssize_t count = i_blocksize(inode); |
b4e29032 CH |
347 | xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset); |
348 | xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count); | |
c2f09217 DW |
349 | xfs_fileoff_t cow_fsb; |
350 | int whichfork; | |
5c665e5b | 351 | struct xfs_bmbt_irec imap; |
060d4eaa | 352 | struct xfs_iext_cursor icur; |
7588cbee | 353 | int retries = 0; |
a206c817 | 354 | int error = 0; |
a206c817 | 355 | |
d9252d52 BF |
356 | if (XFS_FORCED_SHUTDOWN(mp)) |
357 | return -EIO; | |
358 | ||
889c65b3 CH |
359 | /* |
360 | * COW fork blocks can overlap data fork blocks even if the blocks | |
361 | * aren't shared. COW I/O always takes precedent, so we must always | |
362 | * check for overlap on reflink inodes unless the mapping is already a | |
e666aa37 CH |
363 | * COW one, or the COW fork hasn't changed from the last time we looked |
364 | * at it. | |
365 | * | |
366 | * It's safe to check the COW fork if_seq here without the ILOCK because | |
367 | * we've indirectly protected against concurrent updates: writeback has | |
368 | * the page locked, which prevents concurrent invalidations by reflink | |
369 | * and directio and prevents concurrent buffered writes to the same | |
370 | * page. Changes to if_seq always happen under i_lock, which protects | |
371 | * against concurrent updates and provides a memory barrier on the way | |
372 | * out that ensures that we always see the current value. | |
889c65b3 | 373 | */ |
4e087a3b | 374 | if (xfs_imap_valid(wpc, ip, offset)) |
889c65b3 CH |
375 | return 0; |
376 | ||
889c65b3 CH |
377 | /* |
378 | * If we don't have a valid map, now it's time to get a new one for this | |
379 | * offset. This will convert delayed allocations (including COW ones) | |
380 | * into real extents. If we return without a valid map, it means we | |
381 | * landed in a hole and we skip the block. | |
382 | */ | |
7588cbee | 383 | retry: |
c2f09217 DW |
384 | cow_fsb = NULLFILEOFF; |
385 | whichfork = XFS_DATA_FORK; | |
988ef927 | 386 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
f7e67b20 | 387 | ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE || |
8ff2957d | 388 | (ip->i_df.if_flags & XFS_IFEXTENTS)); |
060d4eaa CH |
389 | |
390 | /* | |
391 | * Check if this is offset is covered by a COW extents, and if yes use | |
392 | * it directly instead of looking up anything in the data fork. | |
393 | */ | |
51d62690 | 394 | if (xfs_inode_has_cow_data(ip) && |
e666aa37 CH |
395 | xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap)) |
396 | cow_fsb = imap.br_startoff; | |
397 | if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) { | |
598ecfba | 398 | XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq); |
5c665e5b | 399 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
be225fec | 400 | |
760fea8b | 401 | whichfork = XFS_COW_FORK; |
5c665e5b CH |
402 | goto allocate_blocks; |
403 | } | |
404 | ||
405 | /* | |
d9252d52 BF |
406 | * No COW extent overlap. Revalidate now that we may have updated |
407 | * ->cow_seq. If the data mapping is still valid, we're done. | |
5c665e5b | 408 | */ |
4e087a3b | 409 | if (xfs_imap_valid(wpc, ip, offset)) { |
5c665e5b CH |
410 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
411 | return 0; | |
412 | } | |
413 | ||
414 | /* | |
415 | * If we don't have a valid map, now it's time to get a new one for this | |
416 | * offset. This will convert delayed allocations (including COW ones) | |
417 | * into real extents. | |
418 | */ | |
3345746e CH |
419 | if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap)) |
420 | imap.br_startoff = end_fsb; /* fake a hole past EOF */ | |
598ecfba | 421 | XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq); |
8ff2957d | 422 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
a206c817 | 423 | |
12df89f2 | 424 | /* landed in a hole or beyond EOF? */ |
3345746e | 425 | if (imap.br_startoff > offset_fsb) { |
3345746e | 426 | imap.br_blockcount = imap.br_startoff - offset_fsb; |
5c665e5b | 427 | imap.br_startoff = offset_fsb; |
5c665e5b | 428 | imap.br_startblock = HOLESTARTBLOCK; |
be225fec | 429 | imap.br_state = XFS_EXT_NORM; |
8ff2957d | 430 | } |
e2f6ad46 | 431 | |
12df89f2 CH |
432 | /* |
433 | * Truncate to the next COW extent if there is one. This is the only | |
434 | * opportunity to do this because we can skip COW fork lookups for the | |
435 | * subsequent blocks in the mapping; however, the requirement to treat | |
436 | * the COW range separately remains. | |
437 | */ | |
438 | if (cow_fsb != NULLFILEOFF && | |
439 | cow_fsb < imap.br_startoff + imap.br_blockcount) | |
440 | imap.br_blockcount = cow_fsb - imap.br_startoff; | |
441 | ||
442 | /* got a delalloc extent? */ | |
443 | if (imap.br_startblock != HOLESTARTBLOCK && | |
444 | isnullstartblock(imap.br_startblock)) | |
445 | goto allocate_blocks; | |
446 | ||
4e087a3b | 447 | xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0); |
760fea8b | 448 | trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap); |
5c665e5b CH |
449 | return 0; |
450 | allocate_blocks: | |
760fea8b | 451 | error = xfs_convert_blocks(wpc, ip, whichfork, offset); |
7588cbee CH |
452 | if (error) { |
453 | /* | |
454 | * If we failed to find the extent in the COW fork we might have | |
455 | * raced with a COW to data fork conversion or truncate. | |
456 | * Restart the lookup to catch the extent in the data fork for | |
457 | * the former case, but prevent additional retries to avoid | |
458 | * looping forever for the latter case. | |
459 | */ | |
760fea8b | 460 | if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++) |
7588cbee CH |
461 | goto retry; |
462 | ASSERT(error != -EAGAIN); | |
5c665e5b | 463 | return error; |
7588cbee | 464 | } |
4ad765ed CH |
465 | |
466 | /* | |
467 | * Due to merging the return real extent might be larger than the | |
468 | * original delalloc one. Trim the return extent to the next COW | |
469 | * boundary again to force a re-lookup. | |
470 | */ | |
760fea8b | 471 | if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) { |
4e087a3b CH |
472 | loff_t cow_offset = XFS_FSB_TO_B(mp, cow_fsb); |
473 | ||
474 | if (cow_offset < wpc->iomap.offset + wpc->iomap.length) | |
475 | wpc->iomap.length = cow_offset - wpc->iomap.offset; | |
476 | } | |
4ad765ed | 477 | |
4e087a3b CH |
478 | ASSERT(wpc->iomap.offset <= offset); |
479 | ASSERT(wpc->iomap.offset + wpc->iomap.length > offset); | |
760fea8b | 480 | trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap); |
8ff2957d | 481 | return 0; |
1da177e4 LT |
482 | } |
483 | ||
598ecfba CH |
484 | static int |
485 | xfs_prepare_ioend( | |
486 | struct iomap_ioend *ioend, | |
e10de372 | 487 | int status) |
f6d6d4fc | 488 | { |
73d30d48 CH |
489 | unsigned int nofs_flag; |
490 | ||
491 | /* | |
492 | * We can allocate memory here while doing writeback on behalf of | |
493 | * memory reclaim. To avoid memory allocation deadlocks set the | |
494 | * task-wide nofs context for the following operations. | |
495 | */ | |
496 | nofs_flag = memalloc_nofs_save(); | |
497 | ||
5eda4300 | 498 | /* Convert CoW extents to regular */ |
760fea8b | 499 | if (!status && (ioend->io_flags & IOMAP_F_SHARED)) { |
5eda4300 DW |
500 | status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode), |
501 | ioend->io_offset, ioend->io_size); | |
502 | } | |
503 | ||
e10de372 DC |
504 | /* Reserve log space if we might write beyond the on-disk inode size. */ |
505 | if (!status && | |
760fea8b | 506 | ((ioend->io_flags & IOMAP_F_SHARED) || |
4e087a3b | 507 | ioend->io_type != IOMAP_UNWRITTEN) && |
bb18782a | 508 | xfs_ioend_is_append(ioend) && |
5653017b | 509 | !ioend->io_private) |
e10de372 | 510 | status = xfs_setfilesize_trans_alloc(ioend); |
bb18782a | 511 | |
73d30d48 CH |
512 | memalloc_nofs_restore(nofs_flag); |
513 | ||
598ecfba CH |
514 | if (xfs_ioend_needs_workqueue(ioend)) |
515 | ioend->io_bio->bi_end_io = xfs_end_bio; | |
516 | return status; | |
f6d6d4fc CH |
517 | } |
518 | ||
3ed3a434 | 519 | /* |
82cb1417 CH |
520 | * If the page has delalloc blocks on it, we need to punch them out before we |
521 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the | |
522 | * inode that can trip up a later direct I/O read operation on the same region. | |
3ed3a434 | 523 | * |
82cb1417 CH |
524 | * We prevent this by truncating away the delalloc regions on the page. Because |
525 | * they are delalloc, we can do this without needing a transaction. Indeed - if | |
526 | * we get ENOSPC errors, we have to be able to do this truncation without a | |
527 | * transaction as there is no space left for block reservation (typically why we | |
528 | * see a ENOSPC in writeback). | |
3ed3a434 | 529 | */ |
598ecfba CH |
530 | static void |
531 | xfs_discard_page( | |
763e4cdc BF |
532 | struct page *page, |
533 | loff_t fileoff) | |
3ed3a434 DC |
534 | { |
535 | struct inode *inode = page->mapping->host; | |
536 | struct xfs_inode *ip = XFS_I(inode); | |
03625721 | 537 | struct xfs_mount *mp = ip->i_mount; |
763e4cdc BF |
538 | unsigned int pageoff = offset_in_page(fileoff); |
539 | xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, fileoff); | |
540 | xfs_fileoff_t pageoff_fsb = XFS_B_TO_FSBT(mp, pageoff); | |
03625721 | 541 | int error; |
3ed3a434 | 542 | |
03625721 | 543 | if (XFS_FORCED_SHUTDOWN(mp)) |
e8c3753c DC |
544 | goto out_invalidate; |
545 | ||
4ab45e25 | 546 | xfs_alert_ratelimited(mp, |
c9690043 | 547 | "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.", |
763e4cdc | 548 | page, ip->i_ino, fileoff); |
3ed3a434 | 549 | |
03625721 | 550 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, |
763e4cdc | 551 | i_blocks_per_page(inode, page) - pageoff_fsb); |
03625721 CH |
552 | if (error && !XFS_FORCED_SHUTDOWN(mp)) |
553 | xfs_alert(mp, "page discard unable to remove delalloc mapping."); | |
3ed3a434 | 554 | out_invalidate: |
763e4cdc | 555 | iomap_invalidatepage(page, pageoff, PAGE_SIZE - pageoff); |
3ed3a434 DC |
556 | } |
557 | ||
598ecfba CH |
558 | static const struct iomap_writeback_ops xfs_writeback_ops = { |
559 | .map_blocks = xfs_map_blocks, | |
560 | .prepare_ioend = xfs_prepare_ioend, | |
561 | .discard_page = xfs_discard_page, | |
562 | }; | |
f51623b2 | 563 | |
fbcc0256 DC |
564 | STATIC int |
565 | xfs_vm_writepage( | |
566 | struct page *page, | |
567 | struct writeback_control *wbc) | |
568 | { | |
be225fec | 569 | struct xfs_writepage_ctx wpc = { }; |
fbcc0256 | 570 | |
598ecfba | 571 | return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops); |
fbcc0256 DC |
572 | } |
573 | ||
7d4fb40a NS |
574 | STATIC int |
575 | xfs_vm_writepages( | |
576 | struct address_space *mapping, | |
577 | struct writeback_control *wbc) | |
578 | { | |
be225fec | 579 | struct xfs_writepage_ctx wpc = { }; |
fbcc0256 | 580 | |
b3aea4ed | 581 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
598ecfba | 582 | return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops); |
7d4fb40a NS |
583 | } |
584 | ||
6e2608df DW |
585 | STATIC int |
586 | xfs_dax_writepages( | |
587 | struct address_space *mapping, | |
588 | struct writeback_control *wbc) | |
589 | { | |
30fa529e CH |
590 | struct xfs_inode *ip = XFS_I(mapping->host); |
591 | ||
592 | xfs_iflags_clear(ip, XFS_ITRUNCATED); | |
6e2608df | 593 | return dax_writeback_mapping_range(mapping, |
3f666c56 | 594 | xfs_inode_buftarg(ip)->bt_daxdev, wbc); |
6e2608df DW |
595 | } |
596 | ||
1da177e4 | 597 | STATIC sector_t |
e4c573bb | 598 | xfs_vm_bmap( |
1da177e4 LT |
599 | struct address_space *mapping, |
600 | sector_t block) | |
601 | { | |
b84e7722 | 602 | struct xfs_inode *ip = XFS_I(mapping->host); |
1da177e4 | 603 | |
b84e7722 | 604 | trace_xfs_vm_bmap(ip); |
db1327b1 DW |
605 | |
606 | /* | |
607 | * The swap code (ab-)uses ->bmap to get a block mapping and then | |
793057e1 | 608 | * bypasses the file system for actual I/O. We really can't allow |
db1327b1 | 609 | * that on reflinks inodes, so we have to skip out here. And yes, |
eb5e248d DW |
610 | * 0 is the magic code for a bmap error. |
611 | * | |
612 | * Since we don't pass back blockdev info, we can't return bmap | |
613 | * information for rt files either. | |
db1327b1 | 614 | */ |
66ae56a5 | 615 | if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip)) |
db1327b1 | 616 | return 0; |
690c2a38 | 617 | return iomap_bmap(mapping, block, &xfs_read_iomap_ops); |
1da177e4 LT |
618 | } |
619 | ||
620 | STATIC int | |
e4c573bb | 621 | xfs_vm_readpage( |
1da177e4 LT |
622 | struct file *unused, |
623 | struct page *page) | |
624 | { | |
690c2a38 | 625 | return iomap_readpage(page, &xfs_read_iomap_ops); |
1da177e4 LT |
626 | } |
627 | ||
9d24a13a MWO |
628 | STATIC void |
629 | xfs_vm_readahead( | |
630 | struct readahead_control *rac) | |
1da177e4 | 631 | { |
9d24a13a | 632 | iomap_readahead(rac, &xfs_read_iomap_ops); |
22e757a4 DC |
633 | } |
634 | ||
67482129 DW |
635 | static int |
636 | xfs_iomap_swapfile_activate( | |
637 | struct swap_info_struct *sis, | |
638 | struct file *swap_file, | |
639 | sector_t *span) | |
640 | { | |
30fa529e | 641 | sis->bdev = xfs_inode_buftarg(XFS_I(file_inode(swap_file)))->bt_bdev; |
690c2a38 CH |
642 | return iomap_swapfile_activate(sis, swap_file, span, |
643 | &xfs_read_iomap_ops); | |
67482129 DW |
644 | } |
645 | ||
f5e54d6e | 646 | const struct address_space_operations xfs_address_space_operations = { |
e4c573bb | 647 | .readpage = xfs_vm_readpage, |
9d24a13a | 648 | .readahead = xfs_vm_readahead, |
e4c573bb | 649 | .writepage = xfs_vm_writepage, |
7d4fb40a | 650 | .writepages = xfs_vm_writepages, |
82cb1417 | 651 | .set_page_dirty = iomap_set_page_dirty, |
9e91c572 CH |
652 | .releasepage = iomap_releasepage, |
653 | .invalidatepage = iomap_invalidatepage, | |
e4c573bb | 654 | .bmap = xfs_vm_bmap, |
6e2608df | 655 | .direct_IO = noop_direct_IO, |
82cb1417 CH |
656 | .migratepage = iomap_migrate_page, |
657 | .is_partially_uptodate = iomap_is_partially_uptodate, | |
aa261f54 | 658 | .error_remove_page = generic_error_remove_page, |
67482129 | 659 | .swap_activate = xfs_iomap_swapfile_activate, |
1da177e4 | 660 | }; |
6e2608df DW |
661 | |
662 | const struct address_space_operations xfs_dax_aops = { | |
663 | .writepages = xfs_dax_writepages, | |
664 | .direct_IO = noop_direct_IO, | |
665 | .set_page_dirty = noop_set_page_dirty, | |
666 | .invalidatepage = noop_invalidatepage, | |
67482129 | 667 | .swap_activate = xfs_iomap_swapfile_activate, |
6e2608df | 668 | }; |