]>
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" |
70a9883c | 19 | #include "xfs_shared.h" |
239880ef DC |
20 | #include "xfs_format.h" |
21 | #include "xfs_log_format.h" | |
22 | #include "xfs_trans_resv.h" | |
1da177e4 | 23 | #include "xfs_mount.h" |
1da177e4 | 24 | #include "xfs_inode.h" |
239880ef | 25 | #include "xfs_trans.h" |
281627df | 26 | #include "xfs_inode_item.h" |
a844f451 | 27 | #include "xfs_alloc.h" |
1da177e4 | 28 | #include "xfs_error.h" |
1da177e4 | 29 | #include "xfs_iomap.h" |
0b1b213f | 30 | #include "xfs_trace.h" |
3ed3a434 | 31 | #include "xfs_bmap.h" |
68988114 | 32 | #include "xfs_bmap_util.h" |
a4fbe6ab | 33 | #include "xfs_bmap_btree.h" |
5a0e3ad6 | 34 | #include <linux/gfp.h> |
1da177e4 | 35 | #include <linux/mpage.h> |
10ce4444 | 36 | #include <linux/pagevec.h> |
1da177e4 LT |
37 | #include <linux/writeback.h> |
38 | ||
273dda76 CH |
39 | /* flags for direct write completions */ |
40 | #define XFS_DIO_FLAG_UNWRITTEN (1 << 0) | |
41 | #define XFS_DIO_FLAG_APPEND (1 << 1) | |
42 | ||
fbcc0256 DC |
43 | /* |
44 | * structure owned by writepages passed to individual writepage calls | |
45 | */ | |
46 | struct xfs_writepage_ctx { | |
47 | struct xfs_bmbt_irec imap; | |
48 | bool imap_valid; | |
49 | unsigned int io_type; | |
fbcc0256 DC |
50 | struct xfs_ioend *ioend; |
51 | sector_t last_block; | |
52 | }; | |
53 | ||
0b1b213f | 54 | void |
f51623b2 NS |
55 | xfs_count_page_state( |
56 | struct page *page, | |
57 | int *delalloc, | |
f51623b2 NS |
58 | int *unwritten) |
59 | { | |
60 | struct buffer_head *bh, *head; | |
61 | ||
20cb52eb | 62 | *delalloc = *unwritten = 0; |
f51623b2 NS |
63 | |
64 | bh = head = page_buffers(page); | |
65 | do { | |
20cb52eb | 66 | if (buffer_unwritten(bh)) |
f51623b2 NS |
67 | (*unwritten) = 1; |
68 | else if (buffer_delay(bh)) | |
69 | (*delalloc) = 1; | |
70 | } while ((bh = bh->b_this_page) != head); | |
71 | } | |
72 | ||
20a90f58 | 73 | struct block_device * |
6214ed44 | 74 | xfs_find_bdev_for_inode( |
046f1685 | 75 | struct inode *inode) |
6214ed44 | 76 | { |
046f1685 | 77 | struct xfs_inode *ip = XFS_I(inode); |
6214ed44 CH |
78 | struct xfs_mount *mp = ip->i_mount; |
79 | ||
71ddabb9 | 80 | if (XFS_IS_REALTIME_INODE(ip)) |
6214ed44 CH |
81 | return mp->m_rtdev_targp->bt_bdev; |
82 | else | |
83 | return mp->m_ddev_targp->bt_bdev; | |
84 | } | |
85 | ||
f6d6d4fc CH |
86 | /* |
87 | * We're now finished for good with this ioend structure. | |
88 | * Update the page state via the associated buffer_heads, | |
89 | * release holds on the inode and bio, and finally free | |
90 | * up memory. Do not use the ioend after this. | |
91 | */ | |
0829c360 CH |
92 | STATIC void |
93 | xfs_destroy_ioend( | |
94 | xfs_ioend_t *ioend) | |
95 | { | |
f6d6d4fc CH |
96 | struct buffer_head *bh, *next; |
97 | ||
98 | for (bh = ioend->io_buffer_head; bh; bh = next) { | |
99 | next = bh->b_private; | |
7d04a335 | 100 | bh->b_end_io(bh, !ioend->io_error); |
f6d6d4fc | 101 | } |
583fa586 | 102 | |
0829c360 CH |
103 | mempool_free(ioend, xfs_ioend_pool); |
104 | } | |
105 | ||
fc0063c4 CH |
106 | /* |
107 | * Fast and loose check if this write could update the on-disk inode size. | |
108 | */ | |
109 | static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend) | |
110 | { | |
111 | return ioend->io_offset + ioend->io_size > | |
112 | XFS_I(ioend->io_inode)->i_d.di_size; | |
113 | } | |
114 | ||
281627df CH |
115 | STATIC int |
116 | xfs_setfilesize_trans_alloc( | |
117 | struct xfs_ioend *ioend) | |
118 | { | |
119 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; | |
120 | struct xfs_trans *tp; | |
121 | int error; | |
122 | ||
253f4911 CH |
123 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp); |
124 | if (error) | |
281627df | 125 | return error; |
281627df CH |
126 | |
127 | ioend->io_append_trans = tp; | |
128 | ||
d9457dc0 | 129 | /* |
437a255a | 130 | * We may pass freeze protection with a transaction. So tell lockdep |
d9457dc0 JK |
131 | * we released it. |
132 | */ | |
bee9182d | 133 | __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS); |
281627df CH |
134 | /* |
135 | * We hand off the transaction to the completion thread now, so | |
136 | * clear the flag here. | |
137 | */ | |
138 | current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS); | |
139 | return 0; | |
140 | } | |
141 | ||
ba87ea69 | 142 | /* |
2813d682 | 143 | * Update on-disk file size now that data has been written to disk. |
ba87ea69 | 144 | */ |
281627df | 145 | STATIC int |
ba87ea69 | 146 | xfs_setfilesize( |
2ba66237 CH |
147 | struct xfs_inode *ip, |
148 | struct xfs_trans *tp, | |
149 | xfs_off_t offset, | |
150 | size_t size) | |
ba87ea69 | 151 | { |
ba87ea69 | 152 | xfs_fsize_t isize; |
ba87ea69 | 153 | |
aa6bf01d | 154 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
2ba66237 | 155 | isize = xfs_new_eof(ip, offset + size); |
281627df CH |
156 | if (!isize) { |
157 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
4906e215 | 158 | xfs_trans_cancel(tp); |
281627df | 159 | return 0; |
ba87ea69 LM |
160 | } |
161 | ||
2ba66237 | 162 | trace_xfs_setfilesize(ip, offset, size); |
281627df CH |
163 | |
164 | ip->i_d.di_size = isize; | |
165 | xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL); | |
166 | xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE); | |
167 | ||
70393313 | 168 | return xfs_trans_commit(tp); |
77d7a0c2 DC |
169 | } |
170 | ||
2ba66237 CH |
171 | STATIC int |
172 | xfs_setfilesize_ioend( | |
173 | struct xfs_ioend *ioend) | |
174 | { | |
175 | struct xfs_inode *ip = XFS_I(ioend->io_inode); | |
176 | struct xfs_trans *tp = ioend->io_append_trans; | |
177 | ||
178 | /* | |
179 | * The transaction may have been allocated in the I/O submission thread, | |
180 | * thus we need to mark ourselves as being in a transaction manually. | |
181 | * Similarly for freeze protection. | |
182 | */ | |
183 | current_set_flags_nested(&tp->t_pflags, PF_FSTRANS); | |
bee9182d | 184 | __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS); |
2ba66237 | 185 | |
5cb13dcd Z |
186 | /* we abort the update if there was an IO error */ |
187 | if (ioend->io_error) { | |
188 | xfs_trans_cancel(tp); | |
189 | return ioend->io_error; | |
190 | } | |
191 | ||
2ba66237 CH |
192 | return xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size); |
193 | } | |
194 | ||
77d7a0c2 | 195 | /* |
209fb87a | 196 | * Schedule IO completion handling on the final put of an ioend. |
fc0063c4 CH |
197 | * |
198 | * If there is no work to do we might as well call it a day and free the | |
199 | * ioend right now. | |
77d7a0c2 DC |
200 | */ |
201 | STATIC void | |
202 | xfs_finish_ioend( | |
209fb87a | 203 | struct xfs_ioend *ioend) |
77d7a0c2 DC |
204 | { |
205 | if (atomic_dec_and_test(&ioend->io_remaining)) { | |
aa6bf01d CH |
206 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
207 | ||
0d882a36 | 208 | if (ioend->io_type == XFS_IO_UNWRITTEN) |
aa6bf01d | 209 | queue_work(mp->m_unwritten_workqueue, &ioend->io_work); |
2ba66237 | 210 | else if (ioend->io_append_trans) |
aa6bf01d | 211 | queue_work(mp->m_data_workqueue, &ioend->io_work); |
fc0063c4 CH |
212 | else |
213 | xfs_destroy_ioend(ioend); | |
77d7a0c2 | 214 | } |
ba87ea69 LM |
215 | } |
216 | ||
0829c360 | 217 | /* |
5ec4fabb | 218 | * IO write completion. |
f6d6d4fc CH |
219 | */ |
220 | STATIC void | |
5ec4fabb | 221 | xfs_end_io( |
77d7a0c2 | 222 | struct work_struct *work) |
0829c360 | 223 | { |
77d7a0c2 DC |
224 | xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work); |
225 | struct xfs_inode *ip = XFS_I(ioend->io_inode); | |
69418932 | 226 | int error = 0; |
ba87ea69 | 227 | |
af055e37 BF |
228 | /* |
229 | * Set an error if the mount has shut down and proceed with end I/O | |
230 | * processing so it can perform whatever cleanups are necessary. | |
231 | */ | |
232 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) | |
810627d9 | 233 | ioend->io_error = -EIO; |
04f658ee | 234 | |
5ec4fabb CH |
235 | /* |
236 | * For unwritten extents we need to issue transactions to convert a | |
237 | * range to normal written extens after the data I/O has finished. | |
5cb13dcd Z |
238 | * Detecting and handling completion IO errors is done individually |
239 | * for each case as different cleanup operations need to be performed | |
240 | * on error. | |
5ec4fabb | 241 | */ |
0d882a36 | 242 | if (ioend->io_type == XFS_IO_UNWRITTEN) { |
5cb13dcd Z |
243 | if (ioend->io_error) |
244 | goto done; | |
437a255a DC |
245 | error = xfs_iomap_write_unwritten(ip, ioend->io_offset, |
246 | ioend->io_size); | |
281627df | 247 | } else if (ioend->io_append_trans) { |
2ba66237 | 248 | error = xfs_setfilesize_ioend(ioend); |
84803fb7 | 249 | } else { |
281627df | 250 | ASSERT(!xfs_ioend_is_append(ioend)); |
5ec4fabb | 251 | } |
ba87ea69 | 252 | |
04f658ee | 253 | done: |
437a255a | 254 | if (error) |
2451337d | 255 | ioend->io_error = error; |
aa6bf01d | 256 | xfs_destroy_ioend(ioend); |
c626d174 DC |
257 | } |
258 | ||
0829c360 CH |
259 | /* |
260 | * Allocate and initialise an IO completion structure. | |
261 | * We need to track unwritten extent write completion here initially. | |
262 | * We'll need to extend this for updating the ondisk inode size later | |
263 | * (vs. incore size). | |
264 | */ | |
265 | STATIC xfs_ioend_t * | |
266 | xfs_alloc_ioend( | |
f6d6d4fc CH |
267 | struct inode *inode, |
268 | unsigned int type) | |
0829c360 CH |
269 | { |
270 | xfs_ioend_t *ioend; | |
271 | ||
272 | ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS); | |
273 | ||
274 | /* | |
275 | * Set the count to 1 initially, which will prevent an I/O | |
276 | * completion callback from happening before we have started | |
277 | * all the I/O from calling the completion routine too early. | |
278 | */ | |
279 | atomic_set(&ioend->io_remaining, 1); | |
7d04a335 | 280 | ioend->io_error = 0; |
e10de372 | 281 | INIT_LIST_HEAD(&ioend->io_list); |
f6d6d4fc | 282 | ioend->io_type = type; |
b677c210 | 283 | ioend->io_inode = inode; |
c1a073bd | 284 | ioend->io_buffer_head = NULL; |
f6d6d4fc | 285 | ioend->io_buffer_tail = NULL; |
0829c360 CH |
286 | ioend->io_offset = 0; |
287 | ioend->io_size = 0; | |
281627df | 288 | ioend->io_append_trans = NULL; |
0829c360 | 289 | |
5ec4fabb | 290 | INIT_WORK(&ioend->io_work, xfs_end_io); |
0829c360 CH |
291 | return ioend; |
292 | } | |
293 | ||
1da177e4 LT |
294 | STATIC int |
295 | xfs_map_blocks( | |
296 | struct inode *inode, | |
297 | loff_t offset, | |
207d0416 | 298 | struct xfs_bmbt_irec *imap, |
988ef927 | 299 | int type) |
1da177e4 | 300 | { |
a206c817 CH |
301 | struct xfs_inode *ip = XFS_I(inode); |
302 | struct xfs_mount *mp = ip->i_mount; | |
ed1e7b7e | 303 | ssize_t count = 1 << inode->i_blkbits; |
a206c817 CH |
304 | xfs_fileoff_t offset_fsb, end_fsb; |
305 | int error = 0; | |
a206c817 CH |
306 | int bmapi_flags = XFS_BMAPI_ENTIRE; |
307 | int nimaps = 1; | |
308 | ||
309 | if (XFS_FORCED_SHUTDOWN(mp)) | |
b474c7ae | 310 | return -EIO; |
a206c817 | 311 | |
0d882a36 | 312 | if (type == XFS_IO_UNWRITTEN) |
a206c817 | 313 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
8ff2957d | 314 | |
988ef927 | 315 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
8ff2957d CH |
316 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
317 | (ip->i_df.if_flags & XFS_IFEXTENTS)); | |
d2c28191 | 318 | ASSERT(offset <= mp->m_super->s_maxbytes); |
8ff2957d | 319 | |
d2c28191 DC |
320 | if (offset + count > mp->m_super->s_maxbytes) |
321 | count = mp->m_super->s_maxbytes - offset; | |
a206c817 CH |
322 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); |
323 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
5c8ed202 DC |
324 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
325 | imap, &nimaps, bmapi_flags); | |
8ff2957d | 326 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
a206c817 | 327 | |
8ff2957d | 328 | if (error) |
2451337d | 329 | return error; |
a206c817 | 330 | |
0d882a36 | 331 | if (type == XFS_IO_DELALLOC && |
8ff2957d | 332 | (!nimaps || isnullstartblock(imap->br_startblock))) { |
0799a3e8 | 333 | error = xfs_iomap_write_allocate(ip, offset, imap); |
a206c817 CH |
334 | if (!error) |
335 | trace_xfs_map_blocks_alloc(ip, offset, count, type, imap); | |
2451337d | 336 | return error; |
a206c817 CH |
337 | } |
338 | ||
8ff2957d | 339 | #ifdef DEBUG |
0d882a36 | 340 | if (type == XFS_IO_UNWRITTEN) { |
8ff2957d CH |
341 | ASSERT(nimaps); |
342 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); | |
343 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); | |
344 | } | |
345 | #endif | |
346 | if (nimaps) | |
347 | trace_xfs_map_blocks_found(ip, offset, count, type, imap); | |
348 | return 0; | |
1da177e4 LT |
349 | } |
350 | ||
fbcc0256 | 351 | STATIC bool |
558e6891 | 352 | xfs_imap_valid( |
8699bb0a | 353 | struct inode *inode, |
207d0416 | 354 | struct xfs_bmbt_irec *imap, |
558e6891 | 355 | xfs_off_t offset) |
1da177e4 | 356 | { |
558e6891 | 357 | offset >>= inode->i_blkbits; |
8699bb0a | 358 | |
558e6891 CH |
359 | return offset >= imap->br_startoff && |
360 | offset < imap->br_startoff + imap->br_blockcount; | |
1da177e4 LT |
361 | } |
362 | ||
f6d6d4fc CH |
363 | /* |
364 | * BIO completion handler for buffered IO. | |
365 | */ | |
782e3b3b | 366 | STATIC void |
f6d6d4fc | 367 | xfs_end_bio( |
4246a0b6 | 368 | struct bio *bio) |
f6d6d4fc CH |
369 | { |
370 | xfs_ioend_t *ioend = bio->bi_private; | |
371 | ||
77a78806 LT |
372 | if (!ioend->io_error) |
373 | ioend->io_error = bio->bi_error; | |
f6d6d4fc CH |
374 | |
375 | /* Toss bio and pass work off to an xfsdatad thread */ | |
f6d6d4fc CH |
376 | bio->bi_private = NULL; |
377 | bio->bi_end_io = NULL; | |
f6d6d4fc | 378 | bio_put(bio); |
7d04a335 | 379 | |
209fb87a | 380 | xfs_finish_ioend(ioend); |
f6d6d4fc CH |
381 | } |
382 | ||
383 | STATIC void | |
384 | xfs_submit_ioend_bio( | |
06342cf8 CH |
385 | struct writeback_control *wbc, |
386 | xfs_ioend_t *ioend, | |
387 | struct bio *bio) | |
f6d6d4fc CH |
388 | { |
389 | atomic_inc(&ioend->io_remaining); | |
f6d6d4fc CH |
390 | bio->bi_private = ioend; |
391 | bio->bi_end_io = xfs_end_bio; | |
721a9602 | 392 | submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio); |
f6d6d4fc CH |
393 | } |
394 | ||
395 | STATIC struct bio * | |
396 | xfs_alloc_ioend_bio( | |
397 | struct buffer_head *bh) | |
398 | { | |
b54ffb73 | 399 | struct bio *bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES); |
f6d6d4fc CH |
400 | |
401 | ASSERT(bio->bi_private == NULL); | |
4f024f37 | 402 | bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
f6d6d4fc | 403 | bio->bi_bdev = bh->b_bdev; |
f6d6d4fc CH |
404 | return bio; |
405 | } | |
406 | ||
407 | STATIC void | |
408 | xfs_start_buffer_writeback( | |
409 | struct buffer_head *bh) | |
410 | { | |
411 | ASSERT(buffer_mapped(bh)); | |
412 | ASSERT(buffer_locked(bh)); | |
413 | ASSERT(!buffer_delay(bh)); | |
414 | ASSERT(!buffer_unwritten(bh)); | |
415 | ||
416 | mark_buffer_async_write(bh); | |
417 | set_buffer_uptodate(bh); | |
418 | clear_buffer_dirty(bh); | |
419 | } | |
420 | ||
421 | STATIC void | |
422 | xfs_start_page_writeback( | |
423 | struct page *page, | |
e10de372 | 424 | int clear_dirty) |
f6d6d4fc CH |
425 | { |
426 | ASSERT(PageLocked(page)); | |
427 | ASSERT(!PageWriteback(page)); | |
0d085a52 DC |
428 | |
429 | /* | |
430 | * if the page was not fully cleaned, we need to ensure that the higher | |
431 | * layers come back to it correctly. That means we need to keep the page | |
432 | * dirty, and for WB_SYNC_ALL writeback we need to ensure the | |
433 | * PAGECACHE_TAG_TOWRITE index mark is not removed so another attempt to | |
434 | * write this page in this writeback sweep will be made. | |
435 | */ | |
436 | if (clear_dirty) { | |
92132021 | 437 | clear_page_dirty_for_io(page); |
0d085a52 DC |
438 | set_page_writeback(page); |
439 | } else | |
440 | set_page_writeback_keepwrite(page); | |
441 | ||
f6d6d4fc | 442 | unlock_page(page); |
f6d6d4fc CH |
443 | } |
444 | ||
c7c1a7d8 | 445 | static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh) |
f6d6d4fc CH |
446 | { |
447 | return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); | |
448 | } | |
449 | ||
450 | /* | |
e10de372 DC |
451 | * Submit all of the bios for an ioend. We are only passed a single ioend at a |
452 | * time; the caller is responsible for chaining prior to submission. | |
7bf7f352 DC |
453 | * |
454 | * If @fail is non-zero, it means that we have a situation where some part of | |
455 | * the submission process has failed after we have marked paged for writeback | |
456 | * and unlocked them. In this situation, we need to fail the ioend chain rather | |
457 | * than submit it to IO. This typically only happens on a filesystem shutdown. | |
f6d6d4fc | 458 | */ |
e10de372 | 459 | STATIC int |
f6d6d4fc | 460 | xfs_submit_ioend( |
06342cf8 | 461 | struct writeback_control *wbc, |
7bf7f352 | 462 | xfs_ioend_t *ioend, |
e10de372 | 463 | int status) |
f6d6d4fc | 464 | { |
f6d6d4fc CH |
465 | struct buffer_head *bh; |
466 | struct bio *bio; | |
467 | sector_t lastblock = 0; | |
468 | ||
e10de372 DC |
469 | /* Reserve log space if we might write beyond the on-disk inode size. */ |
470 | if (!status && | |
471 | ioend->io_type != XFS_IO_UNWRITTEN && xfs_ioend_is_append(ioend)) | |
472 | status = xfs_setfilesize_trans_alloc(ioend); | |
473 | /* | |
474 | * If we are failing the IO now, just mark the ioend with an | |
475 | * error and finish it. This will run IO completion immediately | |
476 | * as there is only one reference to the ioend at this point in | |
477 | * time. | |
478 | */ | |
479 | if (status) { | |
480 | ioend->io_error = status; | |
481 | xfs_finish_ioend(ioend); | |
482 | return status; | |
483 | } | |
d88992f6 | 484 | |
e10de372 DC |
485 | bio = NULL; |
486 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) { | |
f6d6d4fc | 487 | |
e10de372 DC |
488 | if (!bio) { |
489 | retry: | |
490 | bio = xfs_alloc_ioend_bio(bh); | |
491 | } else if (bh->b_blocknr != lastblock + 1) { | |
492 | xfs_submit_ioend_bio(wbc, ioend, bio); | |
493 | goto retry; | |
7bf7f352 DC |
494 | } |
495 | ||
e10de372 | 496 | if (xfs_bio_add_buffer(bio, bh) != bh->b_size) { |
06342cf8 | 497 | xfs_submit_ioend_bio(wbc, ioend, bio); |
e10de372 DC |
498 | goto retry; |
499 | } | |
f6d6d4fc | 500 | |
e10de372 DC |
501 | lastblock = bh->b_blocknr; |
502 | } | |
503 | if (bio) | |
504 | xfs_submit_ioend_bio(wbc, ioend, bio); | |
505 | xfs_finish_ioend(ioend); | |
506 | return 0; | |
f6d6d4fc CH |
507 | } |
508 | ||
509 | /* | |
510 | * Test to see if we've been building up a completion structure for | |
511 | * earlier buffers -- if so, we try to append to this ioend if we | |
512 | * can, otherwise we finish off any current ioend and start another. | |
e10de372 DC |
513 | * Return the ioend we finished off so that the caller can submit it |
514 | * once it has finished processing the dirty page. | |
f6d6d4fc CH |
515 | */ |
516 | STATIC void | |
517 | xfs_add_to_ioend( | |
518 | struct inode *inode, | |
519 | struct buffer_head *bh, | |
7336cea8 | 520 | xfs_off_t offset, |
e10de372 DC |
521 | struct xfs_writepage_ctx *wpc, |
522 | struct list_head *iolist) | |
f6d6d4fc | 523 | { |
fbcc0256 | 524 | if (!wpc->ioend || wpc->io_type != wpc->ioend->io_type || |
0df61da8 DW |
525 | bh->b_blocknr != wpc->last_block + 1 || |
526 | offset != wpc->ioend->io_offset + wpc->ioend->io_size) { | |
fbcc0256 DC |
527 | struct xfs_ioend *new; |
528 | ||
e10de372 DC |
529 | if (wpc->ioend) |
530 | list_add(&wpc->ioend->io_list, iolist); | |
531 | ||
fbcc0256 DC |
532 | new = xfs_alloc_ioend(inode, wpc->io_type); |
533 | new->io_offset = offset; | |
534 | new->io_buffer_head = bh; | |
535 | new->io_buffer_tail = bh; | |
fbcc0256 | 536 | wpc->ioend = new; |
f6d6d4fc | 537 | } else { |
fbcc0256 DC |
538 | wpc->ioend->io_buffer_tail->b_private = bh; |
539 | wpc->ioend->io_buffer_tail = bh; | |
f6d6d4fc CH |
540 | } |
541 | ||
542 | bh->b_private = NULL; | |
fbcc0256 DC |
543 | wpc->ioend->io_size += bh->b_size; |
544 | wpc->last_block = bh->b_blocknr; | |
e10de372 | 545 | xfs_start_buffer_writeback(bh); |
f6d6d4fc CH |
546 | } |
547 | ||
87cbc49c NS |
548 | STATIC void |
549 | xfs_map_buffer( | |
046f1685 | 550 | struct inode *inode, |
87cbc49c | 551 | struct buffer_head *bh, |
207d0416 | 552 | struct xfs_bmbt_irec *imap, |
046f1685 | 553 | xfs_off_t offset) |
87cbc49c NS |
554 | { |
555 | sector_t bn; | |
8699bb0a | 556 | struct xfs_mount *m = XFS_I(inode)->i_mount; |
207d0416 CH |
557 | xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff); |
558 | xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock); | |
87cbc49c | 559 | |
207d0416 CH |
560 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
561 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); | |
87cbc49c | 562 | |
e513182d | 563 | bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) + |
8699bb0a | 564 | ((offset - iomap_offset) >> inode->i_blkbits); |
87cbc49c | 565 | |
046f1685 | 566 | ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode))); |
87cbc49c NS |
567 | |
568 | bh->b_blocknr = bn; | |
569 | set_buffer_mapped(bh); | |
570 | } | |
571 | ||
1da177e4 LT |
572 | STATIC void |
573 | xfs_map_at_offset( | |
046f1685 | 574 | struct inode *inode, |
1da177e4 | 575 | struct buffer_head *bh, |
207d0416 | 576 | struct xfs_bmbt_irec *imap, |
046f1685 | 577 | xfs_off_t offset) |
1da177e4 | 578 | { |
207d0416 CH |
579 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
580 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); | |
1da177e4 | 581 | |
207d0416 | 582 | xfs_map_buffer(inode, bh, imap, offset); |
1da177e4 LT |
583 | set_buffer_mapped(bh); |
584 | clear_buffer_delay(bh); | |
f6d6d4fc | 585 | clear_buffer_unwritten(bh); |
1da177e4 LT |
586 | } |
587 | ||
1da177e4 | 588 | /* |
a49935f2 DC |
589 | * Test if a given page contains at least one buffer of a given @type. |
590 | * If @check_all_buffers is true, then we walk all the buffers in the page to | |
591 | * try to find one of the type passed in. If it is not set, then the caller only | |
592 | * needs to check the first buffer on the page for a match. | |
1da177e4 | 593 | */ |
a49935f2 | 594 | STATIC bool |
6ffc4db5 | 595 | xfs_check_page_type( |
10ce4444 | 596 | struct page *page, |
a49935f2 DC |
597 | unsigned int type, |
598 | bool check_all_buffers) | |
1da177e4 | 599 | { |
a49935f2 DC |
600 | struct buffer_head *bh; |
601 | struct buffer_head *head; | |
1da177e4 | 602 | |
a49935f2 DC |
603 | if (PageWriteback(page)) |
604 | return false; | |
605 | if (!page->mapping) | |
606 | return false; | |
607 | if (!page_has_buffers(page)) | |
608 | return false; | |
1da177e4 | 609 | |
a49935f2 DC |
610 | bh = head = page_buffers(page); |
611 | do { | |
612 | if (buffer_unwritten(bh)) { | |
613 | if (type == XFS_IO_UNWRITTEN) | |
614 | return true; | |
615 | } else if (buffer_delay(bh)) { | |
805eeb8e | 616 | if (type == XFS_IO_DELALLOC) |
a49935f2 DC |
617 | return true; |
618 | } else if (buffer_dirty(bh) && buffer_mapped(bh)) { | |
805eeb8e | 619 | if (type == XFS_IO_OVERWRITE) |
a49935f2 DC |
620 | return true; |
621 | } | |
1da177e4 | 622 | |
a49935f2 DC |
623 | /* If we are only checking the first buffer, we are done now. */ |
624 | if (!check_all_buffers) | |
625 | break; | |
626 | } while ((bh = bh->b_this_page) != head); | |
1da177e4 | 627 | |
a49935f2 | 628 | return false; |
1da177e4 LT |
629 | } |
630 | ||
3ed3a434 DC |
631 | STATIC void |
632 | xfs_vm_invalidatepage( | |
633 | struct page *page, | |
d47992f8 LC |
634 | unsigned int offset, |
635 | unsigned int length) | |
3ed3a434 | 636 | { |
34097dfe LC |
637 | trace_xfs_invalidatepage(page->mapping->host, page, offset, |
638 | length); | |
639 | block_invalidatepage(page, offset, length); | |
3ed3a434 DC |
640 | } |
641 | ||
642 | /* | |
643 | * If the page has delalloc buffers on it, we need to punch them out before we | |
644 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the | |
645 | * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read | |
646 | * is done on that same region - the delalloc extent is returned when none is | |
647 | * supposed to be there. | |
648 | * | |
649 | * We prevent this by truncating away the delalloc regions on the page before | |
650 | * invalidating it. Because they are delalloc, we can do this without needing a | |
651 | * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this | |
652 | * truncation without a transaction as there is no space left for block | |
653 | * reservation (typically why we see a ENOSPC in writeback). | |
654 | * | |
655 | * This is not a performance critical path, so for now just do the punching a | |
656 | * buffer head at a time. | |
657 | */ | |
658 | STATIC void | |
659 | xfs_aops_discard_page( | |
660 | struct page *page) | |
661 | { | |
662 | struct inode *inode = page->mapping->host; | |
663 | struct xfs_inode *ip = XFS_I(inode); | |
664 | struct buffer_head *bh, *head; | |
665 | loff_t offset = page_offset(page); | |
3ed3a434 | 666 | |
a49935f2 | 667 | if (!xfs_check_page_type(page, XFS_IO_DELALLOC, true)) |
3ed3a434 DC |
668 | goto out_invalidate; |
669 | ||
e8c3753c DC |
670 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
671 | goto out_invalidate; | |
672 | ||
4f10700a | 673 | xfs_alert(ip->i_mount, |
3ed3a434 DC |
674 | "page discard on page %p, inode 0x%llx, offset %llu.", |
675 | page, ip->i_ino, offset); | |
676 | ||
677 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
678 | bh = head = page_buffers(page); | |
679 | do { | |
3ed3a434 | 680 | int error; |
c726de44 | 681 | xfs_fileoff_t start_fsb; |
3ed3a434 DC |
682 | |
683 | if (!buffer_delay(bh)) | |
684 | goto next_buffer; | |
685 | ||
c726de44 DC |
686 | start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
687 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1); | |
3ed3a434 DC |
688 | if (error) { |
689 | /* something screwed, just bail */ | |
e8c3753c | 690 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
4f10700a | 691 | xfs_alert(ip->i_mount, |
3ed3a434 | 692 | "page discard unable to remove delalloc mapping."); |
e8c3753c | 693 | } |
3ed3a434 DC |
694 | break; |
695 | } | |
696 | next_buffer: | |
c726de44 | 697 | offset += 1 << inode->i_blkbits; |
3ed3a434 DC |
698 | |
699 | } while ((bh = bh->b_this_page) != head); | |
700 | ||
701 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
702 | out_invalidate: | |
d47992f8 | 703 | xfs_vm_invalidatepage(page, 0, PAGE_CACHE_SIZE); |
3ed3a434 DC |
704 | return; |
705 | } | |
706 | ||
e10de372 DC |
707 | /* |
708 | * We implement an immediate ioend submission policy here to avoid needing to | |
709 | * chain multiple ioends and hence nest mempool allocations which can violate | |
710 | * forward progress guarantees we need to provide. The current ioend we are | |
711 | * adding buffers to is cached on the writepage context, and if the new buffer | |
712 | * does not append to the cached ioend it will create a new ioend and cache that | |
713 | * instead. | |
714 | * | |
715 | * If a new ioend is created and cached, the old ioend is returned and queued | |
716 | * locally for submission once the entire page is processed or an error has been | |
717 | * detected. While ioends are submitted immediately after they are completed, | |
718 | * batching optimisations are provided by higher level block plugging. | |
719 | * | |
720 | * At the end of a writeback pass, there will be a cached ioend remaining on the | |
721 | * writepage context that the caller will need to submit. | |
722 | */ | |
bfce7d2e DC |
723 | static int |
724 | xfs_writepage_map( | |
725 | struct xfs_writepage_ctx *wpc, | |
e10de372 | 726 | struct writeback_control *wbc, |
bfce7d2e DC |
727 | struct inode *inode, |
728 | struct page *page, | |
729 | loff_t offset, | |
730 | __uint64_t end_offset) | |
731 | { | |
e10de372 DC |
732 | LIST_HEAD(submit_list); |
733 | struct xfs_ioend *ioend, *next; | |
bfce7d2e DC |
734 | struct buffer_head *bh, *head; |
735 | ssize_t len = 1 << inode->i_blkbits; | |
736 | int error = 0; | |
bfce7d2e | 737 | int count = 0; |
e10de372 | 738 | int uptodate = 1; |
bfce7d2e DC |
739 | |
740 | bh = head = page_buffers(page); | |
741 | offset = page_offset(page); | |
bfce7d2e DC |
742 | do { |
743 | if (offset >= end_offset) | |
744 | break; | |
745 | if (!buffer_uptodate(bh)) | |
746 | uptodate = 0; | |
747 | ||
748 | /* | |
749 | * set_page_dirty dirties all buffers in a page, independent | |
750 | * of their state. The dirty state however is entirely | |
751 | * meaningless for holes (!mapped && uptodate), so skip | |
752 | * buffers covering holes here. | |
753 | */ | |
754 | if (!buffer_mapped(bh) && buffer_uptodate(bh)) { | |
755 | wpc->imap_valid = false; | |
756 | continue; | |
757 | } | |
758 | ||
759 | if (buffer_unwritten(bh)) { | |
760 | if (wpc->io_type != XFS_IO_UNWRITTEN) { | |
761 | wpc->io_type = XFS_IO_UNWRITTEN; | |
762 | wpc->imap_valid = false; | |
763 | } | |
764 | } else if (buffer_delay(bh)) { | |
765 | if (wpc->io_type != XFS_IO_DELALLOC) { | |
766 | wpc->io_type = XFS_IO_DELALLOC; | |
767 | wpc->imap_valid = false; | |
768 | } | |
769 | } else if (buffer_uptodate(bh)) { | |
770 | if (wpc->io_type != XFS_IO_OVERWRITE) { | |
771 | wpc->io_type = XFS_IO_OVERWRITE; | |
772 | wpc->imap_valid = false; | |
773 | } | |
774 | } else { | |
775 | if (PageUptodate(page)) | |
776 | ASSERT(buffer_mapped(bh)); | |
777 | /* | |
778 | * This buffer is not uptodate and will not be | |
779 | * written to disk. Ensure that we will put any | |
780 | * subsequent writeable buffers into a new | |
781 | * ioend. | |
782 | */ | |
783 | wpc->imap_valid = false; | |
784 | continue; | |
785 | } | |
786 | ||
787 | if (wpc->imap_valid) | |
788 | wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, | |
789 | offset); | |
790 | if (!wpc->imap_valid) { | |
791 | error = xfs_map_blocks(inode, offset, &wpc->imap, | |
792 | wpc->io_type); | |
793 | if (error) | |
e10de372 | 794 | goto out; |
bfce7d2e DC |
795 | wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, |
796 | offset); | |
797 | } | |
798 | if (wpc->imap_valid) { | |
799 | lock_buffer(bh); | |
800 | if (wpc->io_type != XFS_IO_OVERWRITE) | |
801 | xfs_map_at_offset(inode, bh, &wpc->imap, offset); | |
e10de372 | 802 | xfs_add_to_ioend(inode, bh, offset, wpc, &submit_list); |
bfce7d2e DC |
803 | count++; |
804 | } | |
805 | ||
bfce7d2e DC |
806 | } while (offset += len, ((bh = bh->b_this_page) != head)); |
807 | ||
808 | if (uptodate && bh == head) | |
809 | SetPageUptodate(page); | |
810 | ||
e10de372 | 811 | ASSERT(wpc->ioend || list_empty(&submit_list)); |
bfce7d2e | 812 | |
e10de372 | 813 | out: |
bfce7d2e | 814 | /* |
e10de372 DC |
815 | * On error, we have to fail the ioend here because we have locked |
816 | * buffers in the ioend. If we don't do this, we'll deadlock | |
817 | * invalidating the page as that tries to lock the buffers on the page. | |
818 | * Also, because we may have set pages under writeback, we have to make | |
819 | * sure we run IO completion to mark the error state of the IO | |
820 | * appropriately, so we can't cancel the ioend directly here. That means | |
821 | * we have to mark this page as under writeback if we included any | |
822 | * buffers from it in the ioend chain so that completion treats it | |
823 | * correctly. | |
bfce7d2e | 824 | * |
e10de372 DC |
825 | * If we didn't include the page in the ioend, the on error we can |
826 | * simply discard and unlock it as there are no other users of the page | |
827 | * or it's buffers right now. The caller will still need to trigger | |
828 | * submission of outstanding ioends on the writepage context so they are | |
829 | * treated correctly on error. | |
bfce7d2e | 830 | */ |
e10de372 DC |
831 | if (count) { |
832 | xfs_start_page_writeback(page, !error); | |
833 | ||
834 | /* | |
835 | * Preserve the original error if there was one, otherwise catch | |
836 | * submission errors here and propagate into subsequent ioend | |
837 | * submissions. | |
838 | */ | |
839 | list_for_each_entry_safe(ioend, next, &submit_list, io_list) { | |
840 | int error2; | |
841 | ||
842 | list_del_init(&ioend->io_list); | |
843 | error2 = xfs_submit_ioend(wbc, ioend, error); | |
844 | if (error2 && !error) | |
845 | error = error2; | |
846 | } | |
847 | } else if (error) { | |
bfce7d2e DC |
848 | xfs_aops_discard_page(page); |
849 | ClearPageUptodate(page); | |
850 | unlock_page(page); | |
e10de372 DC |
851 | } else { |
852 | /* | |
853 | * We can end up here with no error and nothing to write if we | |
854 | * race with a partial page truncate on a sub-page block sized | |
855 | * filesystem. In that case we need to mark the page clean. | |
856 | */ | |
857 | xfs_start_page_writeback(page, 1); | |
858 | end_page_writeback(page); | |
bfce7d2e | 859 | } |
e10de372 | 860 | |
bfce7d2e DC |
861 | mapping_set_error(page->mapping, error); |
862 | return error; | |
863 | } | |
864 | ||
1da177e4 | 865 | /* |
89f3b363 CH |
866 | * Write out a dirty page. |
867 | * | |
868 | * For delalloc space on the page we need to allocate space and flush it. | |
869 | * For unwritten space on the page we need to start the conversion to | |
870 | * regular allocated space. | |
89f3b363 | 871 | * For any other dirty buffer heads on the page we should flush them. |
1da177e4 | 872 | */ |
1da177e4 | 873 | STATIC int |
fbcc0256 | 874 | xfs_do_writepage( |
89f3b363 | 875 | struct page *page, |
fbcc0256 DC |
876 | struct writeback_control *wbc, |
877 | void *data) | |
1da177e4 | 878 | { |
fbcc0256 | 879 | struct xfs_writepage_ctx *wpc = data; |
89f3b363 | 880 | struct inode *inode = page->mapping->host; |
1da177e4 | 881 | loff_t offset; |
1da177e4 | 882 | __uint64_t end_offset; |
ad68972a | 883 | pgoff_t end_index; |
89f3b363 | 884 | |
34097dfe | 885 | trace_xfs_writepage(inode, page, 0, 0); |
89f3b363 | 886 | |
20cb52eb CH |
887 | ASSERT(page_has_buffers(page)); |
888 | ||
89f3b363 CH |
889 | /* |
890 | * Refuse to write the page out if we are called from reclaim context. | |
891 | * | |
d4f7a5cb CH |
892 | * This avoids stack overflows when called from deeply used stacks in |
893 | * random callers for direct reclaim or memcg reclaim. We explicitly | |
894 | * allow reclaim from kswapd as the stack usage there is relatively low. | |
89f3b363 | 895 | * |
94054fa3 MG |
896 | * This should never happen except in the case of a VM regression so |
897 | * warn about it. | |
89f3b363 | 898 | */ |
94054fa3 MG |
899 | if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == |
900 | PF_MEMALLOC)) | |
b5420f23 | 901 | goto redirty; |
1da177e4 | 902 | |
89f3b363 | 903 | /* |
680a647b CH |
904 | * Given that we do not allow direct reclaim to call us, we should |
905 | * never be called while in a filesystem transaction. | |
89f3b363 | 906 | */ |
448011e2 | 907 | if (WARN_ON_ONCE(current->flags & PF_FSTRANS)) |
b5420f23 | 908 | goto redirty; |
89f3b363 | 909 | |
8695d27e | 910 | /* |
ad68972a DC |
911 | * Is this page beyond the end of the file? |
912 | * | |
8695d27e JL |
913 | * The page index is less than the end_index, adjust the end_offset |
914 | * to the highest offset that this page should represent. | |
915 | * ----------------------------------------------------- | |
916 | * | file mapping | <EOF> | | |
917 | * ----------------------------------------------------- | |
918 | * | Page ... | Page N-2 | Page N-1 | Page N | | | |
919 | * ^--------------------------------^----------|-------- | |
920 | * | desired writeback range | see else | | |
921 | * ---------------------------------^------------------| | |
922 | */ | |
ad68972a DC |
923 | offset = i_size_read(inode); |
924 | end_index = offset >> PAGE_CACHE_SHIFT; | |
8695d27e JL |
925 | if (page->index < end_index) |
926 | end_offset = (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT; | |
927 | else { | |
928 | /* | |
929 | * Check whether the page to write out is beyond or straddles | |
930 | * i_size or not. | |
931 | * ------------------------------------------------------- | |
932 | * | file mapping | <EOF> | | |
933 | * ------------------------------------------------------- | |
934 | * | Page ... | Page N-2 | Page N-1 | Page N | Beyond | | |
935 | * ^--------------------------------^-----------|--------- | |
936 | * | | Straddles | | |
937 | * ---------------------------------^-----------|--------| | |
938 | */ | |
6b7a03f0 CH |
939 | unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1); |
940 | ||
941 | /* | |
ff9a28f6 JK |
942 | * Skip the page if it is fully outside i_size, e.g. due to a |
943 | * truncate operation that is in progress. We must redirty the | |
944 | * page so that reclaim stops reclaiming it. Otherwise | |
945 | * xfs_vm_releasepage() is called on it and gets confused. | |
8695d27e JL |
946 | * |
947 | * Note that the end_index is unsigned long, it would overflow | |
948 | * if the given offset is greater than 16TB on 32-bit system | |
949 | * and if we do check the page is fully outside i_size or not | |
950 | * via "if (page->index >= end_index + 1)" as "end_index + 1" | |
951 | * will be evaluated to 0. Hence this page will be redirtied | |
952 | * and be written out repeatedly which would result in an | |
953 | * infinite loop, the user program that perform this operation | |
954 | * will hang. Instead, we can verify this situation by checking | |
955 | * if the page to write is totally beyond the i_size or if it's | |
956 | * offset is just equal to the EOF. | |
6b7a03f0 | 957 | */ |
8695d27e JL |
958 | if (page->index > end_index || |
959 | (page->index == end_index && offset_into_page == 0)) | |
ff9a28f6 | 960 | goto redirty; |
6b7a03f0 CH |
961 | |
962 | /* | |
963 | * The page straddles i_size. It must be zeroed out on each | |
964 | * and every writepage invocation because it may be mmapped. | |
965 | * "A file is mapped in multiples of the page size. For a file | |
8695d27e | 966 | * that is not a multiple of the page size, the remaining |
6b7a03f0 CH |
967 | * memory is zeroed when mapped, and writes to that region are |
968 | * not written out to the file." | |
969 | */ | |
970 | zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE); | |
8695d27e JL |
971 | |
972 | /* Adjust the end_offset to the end of file */ | |
973 | end_offset = offset; | |
1da177e4 LT |
974 | } |
975 | ||
e10de372 | 976 | return xfs_writepage_map(wpc, wbc, inode, page, offset, end_offset); |
f51623b2 | 977 | |
b5420f23 | 978 | redirty: |
f51623b2 NS |
979 | redirty_page_for_writepage(wbc, page); |
980 | unlock_page(page); | |
981 | return 0; | |
f51623b2 NS |
982 | } |
983 | ||
fbcc0256 DC |
984 | STATIC int |
985 | xfs_vm_writepage( | |
986 | struct page *page, | |
987 | struct writeback_control *wbc) | |
988 | { | |
989 | struct xfs_writepage_ctx wpc = { | |
990 | .io_type = XFS_IO_INVALID, | |
991 | }; | |
992 | int ret; | |
993 | ||
994 | ret = xfs_do_writepage(page, wbc, &wpc); | |
e10de372 DC |
995 | if (wpc.ioend) |
996 | ret = xfs_submit_ioend(wbc, wpc.ioend, ret); | |
997 | return ret; | |
fbcc0256 DC |
998 | } |
999 | ||
7d4fb40a NS |
1000 | STATIC int |
1001 | xfs_vm_writepages( | |
1002 | struct address_space *mapping, | |
1003 | struct writeback_control *wbc) | |
1004 | { | |
fbcc0256 DC |
1005 | struct xfs_writepage_ctx wpc = { |
1006 | .io_type = XFS_IO_INVALID, | |
1007 | }; | |
1008 | int ret; | |
1009 | ||
b3aea4ed | 1010 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
7f6d5b52 RZ |
1011 | if (dax_mapping(mapping)) |
1012 | return dax_writeback_mapping_range(mapping, | |
1013 | xfs_find_bdev_for_inode(mapping->host), wbc); | |
1014 | ||
fbcc0256 | 1015 | ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc); |
e10de372 DC |
1016 | if (wpc.ioend) |
1017 | ret = xfs_submit_ioend(wbc, wpc.ioend, ret); | |
1018 | return ret; | |
7d4fb40a NS |
1019 | } |
1020 | ||
f51623b2 NS |
1021 | /* |
1022 | * Called to move a page into cleanable state - and from there | |
89f3b363 | 1023 | * to be released. The page should already be clean. We always |
f51623b2 NS |
1024 | * have buffer heads in this call. |
1025 | * | |
89f3b363 | 1026 | * Returns 1 if the page is ok to release, 0 otherwise. |
f51623b2 NS |
1027 | */ |
1028 | STATIC int | |
238f4c54 | 1029 | xfs_vm_releasepage( |
f51623b2 NS |
1030 | struct page *page, |
1031 | gfp_t gfp_mask) | |
1032 | { | |
20cb52eb | 1033 | int delalloc, unwritten; |
f51623b2 | 1034 | |
34097dfe | 1035 | trace_xfs_releasepage(page->mapping->host, page, 0, 0); |
238f4c54 | 1036 | |
20cb52eb | 1037 | xfs_count_page_state(page, &delalloc, &unwritten); |
f51623b2 | 1038 | |
448011e2 | 1039 | if (WARN_ON_ONCE(delalloc)) |
f51623b2 | 1040 | return 0; |
448011e2 | 1041 | if (WARN_ON_ONCE(unwritten)) |
f51623b2 NS |
1042 | return 0; |
1043 | ||
f51623b2 NS |
1044 | return try_to_free_buffers(page); |
1045 | } | |
1046 | ||
a719370b | 1047 | /* |
273dda76 CH |
1048 | * When we map a DIO buffer, we may need to pass flags to |
1049 | * xfs_end_io_direct_write to tell it what kind of write IO we are doing. | |
3e12dbbd DC |
1050 | * |
1051 | * Note that for DIO, an IO to the highest supported file block offset (i.e. | |
1052 | * 2^63 - 1FSB bytes) will result in the offset + count overflowing a signed 64 | |
1053 | * bit variable. Hence if we see this overflow, we have to assume that the IO is | |
1054 | * extending the file size. We won't know for sure until IO completion is run | |
1055 | * and the actual max write offset is communicated to the IO completion | |
1056 | * routine. | |
a719370b DC |
1057 | */ |
1058 | static void | |
1059 | xfs_map_direct( | |
1060 | struct inode *inode, | |
1061 | struct buffer_head *bh_result, | |
1062 | struct xfs_bmbt_irec *imap, | |
273dda76 | 1063 | xfs_off_t offset) |
a719370b | 1064 | { |
273dda76 | 1065 | uintptr_t *flags = (uintptr_t *)&bh_result->b_private; |
d5cc2e3f | 1066 | xfs_off_t size = bh_result->b_size; |
d5cc2e3f | 1067 | |
273dda76 CH |
1068 | trace_xfs_get_blocks_map_direct(XFS_I(inode), offset, size, |
1069 | ISUNWRITTEN(imap) ? XFS_IO_UNWRITTEN : XFS_IO_OVERWRITE, imap); | |
d5cc2e3f | 1070 | |
273dda76 CH |
1071 | if (ISUNWRITTEN(imap)) { |
1072 | *flags |= XFS_DIO_FLAG_UNWRITTEN; | |
1073 | set_buffer_defer_completion(bh_result); | |
1074 | } else if (offset + size > i_size_read(inode) || offset + size < 0) { | |
1075 | *flags |= XFS_DIO_FLAG_APPEND; | |
a06c277a | 1076 | set_buffer_defer_completion(bh_result); |
a719370b DC |
1077 | } |
1078 | } | |
1079 | ||
1fdca9c2 DC |
1080 | /* |
1081 | * If this is O_DIRECT or the mpage code calling tell them how large the mapping | |
1082 | * is, so that we can avoid repeated get_blocks calls. | |
1083 | * | |
1084 | * If the mapping spans EOF, then we have to break the mapping up as the mapping | |
1085 | * for blocks beyond EOF must be marked new so that sub block regions can be | |
1086 | * correctly zeroed. We can't do this for mappings within EOF unless the mapping | |
1087 | * was just allocated or is unwritten, otherwise the callers would overwrite | |
1088 | * existing data with zeros. Hence we have to split the mapping into a range up | |
1089 | * to and including EOF, and a second mapping for beyond EOF. | |
1090 | */ | |
1091 | static void | |
1092 | xfs_map_trim_size( | |
1093 | struct inode *inode, | |
1094 | sector_t iblock, | |
1095 | struct buffer_head *bh_result, | |
1096 | struct xfs_bmbt_irec *imap, | |
1097 | xfs_off_t offset, | |
1098 | ssize_t size) | |
1099 | { | |
1100 | xfs_off_t mapping_size; | |
1101 | ||
1102 | mapping_size = imap->br_startoff + imap->br_blockcount - iblock; | |
1103 | mapping_size <<= inode->i_blkbits; | |
1104 | ||
1105 | ASSERT(mapping_size > 0); | |
1106 | if (mapping_size > size) | |
1107 | mapping_size = size; | |
1108 | if (offset < i_size_read(inode) && | |
1109 | offset + mapping_size >= i_size_read(inode)) { | |
1110 | /* limit mapping to block that spans EOF */ | |
1111 | mapping_size = roundup_64(i_size_read(inode) - offset, | |
1112 | 1 << inode->i_blkbits); | |
1113 | } | |
1114 | if (mapping_size > LONG_MAX) | |
1115 | mapping_size = LONG_MAX; | |
1116 | ||
1117 | bh_result->b_size = mapping_size; | |
1118 | } | |
1119 | ||
1da177e4 | 1120 | STATIC int |
c2536668 | 1121 | __xfs_get_blocks( |
1da177e4 LT |
1122 | struct inode *inode, |
1123 | sector_t iblock, | |
1da177e4 LT |
1124 | struct buffer_head *bh_result, |
1125 | int create, | |
3e12dbbd DC |
1126 | bool direct, |
1127 | bool dax_fault) | |
1da177e4 | 1128 | { |
a206c817 CH |
1129 | struct xfs_inode *ip = XFS_I(inode); |
1130 | struct xfs_mount *mp = ip->i_mount; | |
1131 | xfs_fileoff_t offset_fsb, end_fsb; | |
1132 | int error = 0; | |
1133 | int lockmode = 0; | |
207d0416 | 1134 | struct xfs_bmbt_irec imap; |
a206c817 | 1135 | int nimaps = 1; |
fdc7ed75 NS |
1136 | xfs_off_t offset; |
1137 | ssize_t size; | |
207d0416 | 1138 | int new = 0; |
a206c817 CH |
1139 | |
1140 | if (XFS_FORCED_SHUTDOWN(mp)) | |
b474c7ae | 1141 | return -EIO; |
1da177e4 | 1142 | |
fdc7ed75 | 1143 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
c2536668 NS |
1144 | ASSERT(bh_result->b_size >= (1 << inode->i_blkbits)); |
1145 | size = bh_result->b_size; | |
364f358a LM |
1146 | |
1147 | if (!create && direct && offset >= i_size_read(inode)) | |
1148 | return 0; | |
1149 | ||
507630b2 DC |
1150 | /* |
1151 | * Direct I/O is usually done on preallocated files, so try getting | |
1152 | * a block mapping without an exclusive lock first. For buffered | |
1153 | * writes we already have the exclusive iolock anyway, so avoiding | |
1154 | * a lock roundtrip here by taking the ilock exclusive from the | |
1155 | * beginning is a useful micro optimization. | |
1156 | */ | |
1157 | if (create && !direct) { | |
a206c817 CH |
1158 | lockmode = XFS_ILOCK_EXCL; |
1159 | xfs_ilock(ip, lockmode); | |
1160 | } else { | |
309ecac8 | 1161 | lockmode = xfs_ilock_data_map_shared(ip); |
a206c817 | 1162 | } |
f2bde9b8 | 1163 | |
d2c28191 DC |
1164 | ASSERT(offset <= mp->m_super->s_maxbytes); |
1165 | if (offset + size > mp->m_super->s_maxbytes) | |
1166 | size = mp->m_super->s_maxbytes - offset; | |
a206c817 CH |
1167 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size); |
1168 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
1169 | ||
5c8ed202 DC |
1170 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
1171 | &imap, &nimaps, XFS_BMAPI_ENTIRE); | |
1da177e4 | 1172 | if (error) |
a206c817 CH |
1173 | goto out_unlock; |
1174 | ||
1ca19157 | 1175 | /* for DAX, we convert unwritten extents directly */ |
a206c817 CH |
1176 | if (create && |
1177 | (!nimaps || | |
1178 | (imap.br_startblock == HOLESTARTBLOCK || | |
1ca19157 DC |
1179 | imap.br_startblock == DELAYSTARTBLOCK) || |
1180 | (IS_DAX(inode) && ISUNWRITTEN(&imap)))) { | |
aff3a9ed | 1181 | if (direct || xfs_get_extsz_hint(ip)) { |
507630b2 | 1182 | /* |
009c6e87 BF |
1183 | * xfs_iomap_write_direct() expects the shared lock. It |
1184 | * is unlocked on return. | |
507630b2 | 1185 | */ |
009c6e87 BF |
1186 | if (lockmode == XFS_ILOCK_EXCL) |
1187 | xfs_ilock_demote(ip, lockmode); | |
1188 | ||
a206c817 CH |
1189 | error = xfs_iomap_write_direct(ip, offset, size, |
1190 | &imap, nimaps); | |
507630b2 | 1191 | if (error) |
2451337d | 1192 | return error; |
d3bc815a | 1193 | new = 1; |
6b698ede | 1194 | |
a206c817 | 1195 | } else { |
507630b2 DC |
1196 | /* |
1197 | * Delalloc reservations do not require a transaction, | |
d3bc815a DC |
1198 | * we can go on without dropping the lock here. If we |
1199 | * are allocating a new delalloc block, make sure that | |
1200 | * we set the new flag so that we mark the buffer new so | |
1201 | * that we know that it is newly allocated if the write | |
1202 | * fails. | |
507630b2 | 1203 | */ |
d3bc815a DC |
1204 | if (nimaps && imap.br_startblock == HOLESTARTBLOCK) |
1205 | new = 1; | |
a206c817 | 1206 | error = xfs_iomap_write_delay(ip, offset, size, &imap); |
507630b2 DC |
1207 | if (error) |
1208 | goto out_unlock; | |
1209 | ||
1210 | xfs_iunlock(ip, lockmode); | |
a206c817 | 1211 | } |
d5cc2e3f DC |
1212 | trace_xfs_get_blocks_alloc(ip, offset, size, |
1213 | ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN | |
1214 | : XFS_IO_DELALLOC, &imap); | |
a206c817 | 1215 | } else if (nimaps) { |
d5cc2e3f DC |
1216 | trace_xfs_get_blocks_found(ip, offset, size, |
1217 | ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN | |
1218 | : XFS_IO_OVERWRITE, &imap); | |
507630b2 | 1219 | xfs_iunlock(ip, lockmode); |
a206c817 CH |
1220 | } else { |
1221 | trace_xfs_get_blocks_notfound(ip, offset, size); | |
1222 | goto out_unlock; | |
1223 | } | |
1da177e4 | 1224 | |
1ca19157 DC |
1225 | if (IS_DAX(inode) && create) { |
1226 | ASSERT(!ISUNWRITTEN(&imap)); | |
1227 | /* zeroing is not needed at a higher layer */ | |
1228 | new = 0; | |
1229 | } | |
1230 | ||
1fdca9c2 DC |
1231 | /* trim mapping down to size requested */ |
1232 | if (direct || size > (1 << inode->i_blkbits)) | |
1233 | xfs_map_trim_size(inode, iblock, bh_result, | |
1234 | &imap, offset, size); | |
1235 | ||
a719370b DC |
1236 | /* |
1237 | * For unwritten extents do not report a disk address in the buffered | |
1238 | * read case (treat as if we're reading into a hole). | |
1239 | */ | |
207d0416 | 1240 | if (imap.br_startblock != HOLESTARTBLOCK && |
a719370b DC |
1241 | imap.br_startblock != DELAYSTARTBLOCK && |
1242 | (create || !ISUNWRITTEN(&imap))) { | |
1243 | xfs_map_buffer(inode, bh_result, &imap, offset); | |
1244 | if (ISUNWRITTEN(&imap)) | |
1da177e4 | 1245 | set_buffer_unwritten(bh_result); |
a719370b | 1246 | /* direct IO needs special help */ |
273dda76 CH |
1247 | if (create && direct) { |
1248 | if (dax_fault) | |
1249 | ASSERT(!ISUNWRITTEN(&imap)); | |
1250 | else | |
1251 | xfs_map_direct(inode, bh_result, &imap, offset); | |
1252 | } | |
1da177e4 LT |
1253 | } |
1254 | ||
c2536668 NS |
1255 | /* |
1256 | * If this is a realtime file, data may be on a different device. | |
1257 | * to that pointed to from the buffer_head b_bdev currently. | |
1258 | */ | |
046f1685 | 1259 | bh_result->b_bdev = xfs_find_bdev_for_inode(inode); |
1da177e4 | 1260 | |
c2536668 | 1261 | /* |
549054af DC |
1262 | * If we previously allocated a block out beyond eof and we are now |
1263 | * coming back to use it then we will need to flag it as new even if it | |
1264 | * has a disk address. | |
1265 | * | |
1266 | * With sub-block writes into unwritten extents we also need to mark | |
1267 | * the buffer as new so that the unwritten parts of the buffer gets | |
1268 | * correctly zeroed. | |
1da177e4 LT |
1269 | */ |
1270 | if (create && | |
1271 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || | |
549054af | 1272 | (offset >= i_size_read(inode)) || |
207d0416 | 1273 | (new || ISUNWRITTEN(&imap)))) |
1da177e4 | 1274 | set_buffer_new(bh_result); |
1da177e4 | 1275 | |
207d0416 | 1276 | if (imap.br_startblock == DELAYSTARTBLOCK) { |
1da177e4 LT |
1277 | BUG_ON(direct); |
1278 | if (create) { | |
1279 | set_buffer_uptodate(bh_result); | |
1280 | set_buffer_mapped(bh_result); | |
1281 | set_buffer_delay(bh_result); | |
1282 | } | |
1283 | } | |
1284 | ||
1da177e4 | 1285 | return 0; |
a206c817 CH |
1286 | |
1287 | out_unlock: | |
1288 | xfs_iunlock(ip, lockmode); | |
2451337d | 1289 | return error; |
1da177e4 LT |
1290 | } |
1291 | ||
1292 | int | |
c2536668 | 1293 | xfs_get_blocks( |
1da177e4 LT |
1294 | struct inode *inode, |
1295 | sector_t iblock, | |
1296 | struct buffer_head *bh_result, | |
1297 | int create) | |
1298 | { | |
3e12dbbd | 1299 | return __xfs_get_blocks(inode, iblock, bh_result, create, false, false); |
1da177e4 LT |
1300 | } |
1301 | ||
6b698ede | 1302 | int |
e4c573bb | 1303 | xfs_get_blocks_direct( |
1da177e4 LT |
1304 | struct inode *inode, |
1305 | sector_t iblock, | |
1da177e4 LT |
1306 | struct buffer_head *bh_result, |
1307 | int create) | |
1308 | { | |
3e12dbbd DC |
1309 | return __xfs_get_blocks(inode, iblock, bh_result, create, true, false); |
1310 | } | |
1311 | ||
1312 | int | |
1313 | xfs_get_blocks_dax_fault( | |
1314 | struct inode *inode, | |
1315 | sector_t iblock, | |
1316 | struct buffer_head *bh_result, | |
1317 | int create) | |
1318 | { | |
1319 | return __xfs_get_blocks(inode, iblock, bh_result, create, true, true); | |
1da177e4 LT |
1320 | } |
1321 | ||
273dda76 CH |
1322 | /* |
1323 | * Complete a direct I/O write request. | |
1324 | * | |
1325 | * xfs_map_direct passes us some flags in the private data to tell us what to | |
1326 | * do. If no flags are set, then the write IO is an overwrite wholly within | |
1327 | * the existing allocated file size and so there is nothing for us to do. | |
1328 | * | |
1329 | * Note that in this case the completion can be called in interrupt context, | |
1330 | * whereas if we have flags set we will always be called in task context | |
1331 | * (i.e. from a workqueue). | |
1332 | */ | |
1333 | STATIC int | |
1334 | xfs_end_io_direct_write( | |
1335 | struct kiocb *iocb, | |
209fb87a | 1336 | loff_t offset, |
273dda76 CH |
1337 | ssize_t size, |
1338 | void *private) | |
f0973863 | 1339 | { |
273dda76 CH |
1340 | struct inode *inode = file_inode(iocb->ki_filp); |
1341 | struct xfs_inode *ip = XFS_I(inode); | |
1342 | struct xfs_mount *mp = ip->i_mount; | |
1343 | uintptr_t flags = (uintptr_t)private; | |
1344 | int error = 0; | |
a06c277a | 1345 | |
273dda76 | 1346 | trace_xfs_end_io_direct_write(ip, offset, size); |
f0973863 | 1347 | |
273dda76 CH |
1348 | if (XFS_FORCED_SHUTDOWN(mp)) |
1349 | return -EIO; | |
d5cc2e3f | 1350 | |
273dda76 CH |
1351 | if (size <= 0) |
1352 | return size; | |
f0973863 | 1353 | |
2813d682 | 1354 | /* |
273dda76 | 1355 | * The flags tell us whether we are doing unwritten extent conversions |
6dfa1b67 DC |
1356 | * or an append transaction that updates the on-disk file size. These |
1357 | * cases are the only cases where we should *potentially* be needing | |
a06c277a | 1358 | * to update the VFS inode size. |
273dda76 CH |
1359 | */ |
1360 | if (flags == 0) { | |
1361 | ASSERT(offset + size <= i_size_read(inode)); | |
1362 | return 0; | |
1363 | } | |
1364 | ||
1365 | /* | |
6dfa1b67 | 1366 | * We need to update the in-core inode size here so that we don't end up |
a06c277a DC |
1367 | * with the on-disk inode size being outside the in-core inode size. We |
1368 | * have no other method of updating EOF for AIO, so always do it here | |
1369 | * if necessary. | |
b9d59846 DC |
1370 | * |
1371 | * We need to lock the test/set EOF update as we can be racing with | |
1372 | * other IO completions here to update the EOF. Failing to serialise | |
1373 | * here can result in EOF moving backwards and Bad Things Happen when | |
1374 | * that occurs. | |
2813d682 | 1375 | */ |
273dda76 | 1376 | spin_lock(&ip->i_flags_lock); |
2ba66237 CH |
1377 | if (offset + size > i_size_read(inode)) |
1378 | i_size_write(inode, offset + size); | |
273dda76 | 1379 | spin_unlock(&ip->i_flags_lock); |
2813d682 | 1380 | |
273dda76 CH |
1381 | if (flags & XFS_DIO_FLAG_UNWRITTEN) { |
1382 | trace_xfs_end_io_direct_write_unwritten(ip, offset, size); | |
209fb87a | 1383 | |
273dda76 CH |
1384 | error = xfs_iomap_write_unwritten(ip, offset, size); |
1385 | } else if (flags & XFS_DIO_FLAG_APPEND) { | |
1386 | struct xfs_trans *tp; | |
f0973863 | 1387 | |
273dda76 | 1388 | trace_xfs_end_io_direct_write_append(ip, offset, size); |
6b698ede | 1389 | |
253f4911 CH |
1390 | error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, |
1391 | &tp); | |
1392 | if (!error) | |
1393 | error = xfs_setfilesize(ip, tp, offset, size); | |
6b698ede DC |
1394 | } |
1395 | ||
273dda76 | 1396 | return error; |
6b698ede DC |
1397 | } |
1398 | ||
c19b104a CH |
1399 | STATIC ssize_t |
1400 | xfs_vm_direct_IO( | |
6e1ba0bc DC |
1401 | struct kiocb *iocb, |
1402 | struct iov_iter *iter, | |
c19b104a | 1403 | loff_t offset) |
6e1ba0bc | 1404 | { |
c19b104a CH |
1405 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
1406 | dio_iodone_t *endio = NULL; | |
1407 | int flags = 0; | |
6e1ba0bc DC |
1408 | struct block_device *bdev; |
1409 | ||
c19b104a CH |
1410 | if (iov_iter_rw(iter) == WRITE) { |
1411 | endio = xfs_end_io_direct_write; | |
1412 | flags = DIO_ASYNC_EXTEND; | |
1413 | } | |
1414 | ||
1415 | if (IS_DAX(inode)) { | |
6e1ba0bc DC |
1416 | return dax_do_io(iocb, inode, iter, offset, |
1417 | xfs_get_blocks_direct, endio, 0); | |
c19b104a | 1418 | } |
6e1ba0bc DC |
1419 | |
1420 | bdev = xfs_find_bdev_for_inode(inode); | |
1421 | return __blockdev_direct_IO(iocb, inode, bdev, iter, offset, | |
c19b104a | 1422 | xfs_get_blocks_direct, endio, NULL, flags); |
1da177e4 LT |
1423 | } |
1424 | ||
d3bc815a DC |
1425 | /* |
1426 | * Punch out the delalloc blocks we have already allocated. | |
1427 | * | |
1428 | * Don't bother with xfs_setattr given that nothing can have made it to disk yet | |
1429 | * as the page is still locked at this point. | |
1430 | */ | |
1431 | STATIC void | |
1432 | xfs_vm_kill_delalloc_range( | |
1433 | struct inode *inode, | |
1434 | loff_t start, | |
1435 | loff_t end) | |
1436 | { | |
1437 | struct xfs_inode *ip = XFS_I(inode); | |
1438 | xfs_fileoff_t start_fsb; | |
1439 | xfs_fileoff_t end_fsb; | |
1440 | int error; | |
1441 | ||
1442 | start_fsb = XFS_B_TO_FSB(ip->i_mount, start); | |
1443 | end_fsb = XFS_B_TO_FSB(ip->i_mount, end); | |
1444 | if (end_fsb <= start_fsb) | |
1445 | return; | |
1446 | ||
1447 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
1448 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, | |
1449 | end_fsb - start_fsb); | |
1450 | if (error) { | |
1451 | /* something screwed, just bail */ | |
1452 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { | |
1453 | xfs_alert(ip->i_mount, | |
1454 | "xfs_vm_write_failed: unable to clean up ino %lld", | |
1455 | ip->i_ino); | |
1456 | } | |
1457 | } | |
1458 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
1459 | } | |
1460 | ||
fa9b227e CH |
1461 | STATIC void |
1462 | xfs_vm_write_failed( | |
d3bc815a DC |
1463 | struct inode *inode, |
1464 | struct page *page, | |
1465 | loff_t pos, | |
1466 | unsigned len) | |
fa9b227e | 1467 | { |
58e59854 | 1468 | loff_t block_offset; |
d3bc815a DC |
1469 | loff_t block_start; |
1470 | loff_t block_end; | |
1471 | loff_t from = pos & (PAGE_CACHE_SIZE - 1); | |
1472 | loff_t to = from + len; | |
1473 | struct buffer_head *bh, *head; | |
801cc4e1 | 1474 | struct xfs_mount *mp = XFS_I(inode)->i_mount; |
fa9b227e | 1475 | |
58e59854 JL |
1476 | /* |
1477 | * The request pos offset might be 32 or 64 bit, this is all fine | |
1478 | * on 64-bit platform. However, for 64-bit pos request on 32-bit | |
1479 | * platform, the high 32-bit will be masked off if we evaluate the | |
1480 | * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is | |
1481 | * 0xfffff000 as an unsigned long, hence the result is incorrect | |
1482 | * which could cause the following ASSERT failed in most cases. | |
1483 | * In order to avoid this, we can evaluate the block_offset of the | |
1484 | * start of the page by using shifts rather than masks the mismatch | |
1485 | * problem. | |
1486 | */ | |
1487 | block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT; | |
1488 | ||
d3bc815a | 1489 | ASSERT(block_offset + from == pos); |
c726de44 | 1490 | |
d3bc815a DC |
1491 | head = page_buffers(page); |
1492 | block_start = 0; | |
1493 | for (bh = head; bh != head || !block_start; | |
1494 | bh = bh->b_this_page, block_start = block_end, | |
1495 | block_offset += bh->b_size) { | |
1496 | block_end = block_start + bh->b_size; | |
c726de44 | 1497 | |
d3bc815a DC |
1498 | /* skip buffers before the write */ |
1499 | if (block_end <= from) | |
1500 | continue; | |
1501 | ||
1502 | /* if the buffer is after the write, we're done */ | |
1503 | if (block_start >= to) | |
1504 | break; | |
1505 | ||
60630fe6 BF |
1506 | /* |
1507 | * Process delalloc and unwritten buffers beyond EOF. We can | |
1508 | * encounter unwritten buffers in the event that a file has | |
1509 | * post-EOF unwritten extents and an extending write happens to | |
1510 | * fail (e.g., an unaligned write that also involves a delalloc | |
1511 | * to the same page). | |
1512 | */ | |
1513 | if (!buffer_delay(bh) && !buffer_unwritten(bh)) | |
d3bc815a DC |
1514 | continue; |
1515 | ||
801cc4e1 BF |
1516 | if (!xfs_mp_fail_writes(mp) && !buffer_new(bh) && |
1517 | block_offset < i_size_read(inode)) | |
d3bc815a DC |
1518 | continue; |
1519 | ||
60630fe6 BF |
1520 | if (buffer_delay(bh)) |
1521 | xfs_vm_kill_delalloc_range(inode, block_offset, | |
1522 | block_offset + bh->b_size); | |
4ab9ed57 DC |
1523 | |
1524 | /* | |
1525 | * This buffer does not contain data anymore. make sure anyone | |
1526 | * who finds it knows that for certain. | |
1527 | */ | |
1528 | clear_buffer_delay(bh); | |
1529 | clear_buffer_uptodate(bh); | |
1530 | clear_buffer_mapped(bh); | |
1531 | clear_buffer_new(bh); | |
1532 | clear_buffer_dirty(bh); | |
60630fe6 | 1533 | clear_buffer_unwritten(bh); |
fa9b227e | 1534 | } |
d3bc815a | 1535 | |
fa9b227e CH |
1536 | } |
1537 | ||
d3bc815a DC |
1538 | /* |
1539 | * This used to call block_write_begin(), but it unlocks and releases the page | |
1540 | * on error, and we need that page to be able to punch stale delalloc blocks out | |
1541 | * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at | |
1542 | * the appropriate point. | |
1543 | */ | |
f51623b2 | 1544 | STATIC int |
d79689c7 | 1545 | xfs_vm_write_begin( |
f51623b2 | 1546 | struct file *file, |
d79689c7 NP |
1547 | struct address_space *mapping, |
1548 | loff_t pos, | |
1549 | unsigned len, | |
1550 | unsigned flags, | |
1551 | struct page **pagep, | |
1552 | void **fsdata) | |
f51623b2 | 1553 | { |
d3bc815a DC |
1554 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; |
1555 | struct page *page; | |
1556 | int status; | |
801cc4e1 | 1557 | struct xfs_mount *mp = XFS_I(mapping->host)->i_mount; |
155130a4 | 1558 | |
d3bc815a DC |
1559 | ASSERT(len <= PAGE_CACHE_SIZE); |
1560 | ||
ad22c7a0 | 1561 | page = grab_cache_page_write_begin(mapping, index, flags); |
d3bc815a DC |
1562 | if (!page) |
1563 | return -ENOMEM; | |
1564 | ||
1565 | status = __block_write_begin(page, pos, len, xfs_get_blocks); | |
801cc4e1 BF |
1566 | if (xfs_mp_fail_writes(mp)) |
1567 | status = -EIO; | |
d3bc815a DC |
1568 | if (unlikely(status)) { |
1569 | struct inode *inode = mapping->host; | |
72ab70a1 | 1570 | size_t isize = i_size_read(inode); |
d3bc815a DC |
1571 | |
1572 | xfs_vm_write_failed(inode, page, pos, len); | |
1573 | unlock_page(page); | |
1574 | ||
72ab70a1 DC |
1575 | /* |
1576 | * If the write is beyond EOF, we only want to kill blocks | |
1577 | * allocated in this write, not blocks that were previously | |
1578 | * written successfully. | |
1579 | */ | |
801cc4e1 BF |
1580 | if (xfs_mp_fail_writes(mp)) |
1581 | isize = 0; | |
72ab70a1 DC |
1582 | if (pos + len > isize) { |
1583 | ssize_t start = max_t(ssize_t, pos, isize); | |
1584 | ||
1585 | truncate_pagecache_range(inode, start, pos + len); | |
1586 | } | |
d3bc815a DC |
1587 | |
1588 | page_cache_release(page); | |
1589 | page = NULL; | |
1590 | } | |
1591 | ||
1592 | *pagep = page; | |
1593 | return status; | |
fa9b227e CH |
1594 | } |
1595 | ||
d3bc815a | 1596 | /* |
aad3f375 DC |
1597 | * On failure, we only need to kill delalloc blocks beyond EOF in the range of |
1598 | * this specific write because they will never be written. Previous writes | |
1599 | * beyond EOF where block allocation succeeded do not need to be trashed, so | |
1600 | * only new blocks from this write should be trashed. For blocks within | |
1601 | * EOF, generic_write_end() zeros them so they are safe to leave alone and be | |
1602 | * written with all the other valid data. | |
d3bc815a | 1603 | */ |
fa9b227e CH |
1604 | STATIC int |
1605 | xfs_vm_write_end( | |
1606 | struct file *file, | |
1607 | struct address_space *mapping, | |
1608 | loff_t pos, | |
1609 | unsigned len, | |
1610 | unsigned copied, | |
1611 | struct page *page, | |
1612 | void *fsdata) | |
1613 | { | |
1614 | int ret; | |
155130a4 | 1615 | |
d3bc815a DC |
1616 | ASSERT(len <= PAGE_CACHE_SIZE); |
1617 | ||
fa9b227e | 1618 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); |
d3bc815a DC |
1619 | if (unlikely(ret < len)) { |
1620 | struct inode *inode = mapping->host; | |
1621 | size_t isize = i_size_read(inode); | |
1622 | loff_t to = pos + len; | |
1623 | ||
1624 | if (to > isize) { | |
aad3f375 DC |
1625 | /* only kill blocks in this write beyond EOF */ |
1626 | if (pos > isize) | |
1627 | isize = pos; | |
d3bc815a | 1628 | xfs_vm_kill_delalloc_range(inode, isize, to); |
aad3f375 | 1629 | truncate_pagecache_range(inode, isize, to); |
d3bc815a DC |
1630 | } |
1631 | } | |
155130a4 | 1632 | return ret; |
f51623b2 | 1633 | } |
1da177e4 LT |
1634 | |
1635 | STATIC sector_t | |
e4c573bb | 1636 | xfs_vm_bmap( |
1da177e4 LT |
1637 | struct address_space *mapping, |
1638 | sector_t block) | |
1639 | { | |
1640 | struct inode *inode = (struct inode *)mapping->host; | |
739bfb2a | 1641 | struct xfs_inode *ip = XFS_I(inode); |
1da177e4 | 1642 | |
cca28fb8 | 1643 | trace_xfs_vm_bmap(XFS_I(inode)); |
126468b1 | 1644 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
4bc1ea6b | 1645 | filemap_write_and_wait(mapping); |
126468b1 | 1646 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
c2536668 | 1647 | return generic_block_bmap(mapping, block, xfs_get_blocks); |
1da177e4 LT |
1648 | } |
1649 | ||
1650 | STATIC int | |
e4c573bb | 1651 | xfs_vm_readpage( |
1da177e4 LT |
1652 | struct file *unused, |
1653 | struct page *page) | |
1654 | { | |
121e213e | 1655 | trace_xfs_vm_readpage(page->mapping->host, 1); |
c2536668 | 1656 | return mpage_readpage(page, xfs_get_blocks); |
1da177e4 LT |
1657 | } |
1658 | ||
1659 | STATIC int | |
e4c573bb | 1660 | xfs_vm_readpages( |
1da177e4 LT |
1661 | struct file *unused, |
1662 | struct address_space *mapping, | |
1663 | struct list_head *pages, | |
1664 | unsigned nr_pages) | |
1665 | { | |
121e213e | 1666 | trace_xfs_vm_readpages(mapping->host, nr_pages); |
c2536668 | 1667 | return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks); |
1da177e4 LT |
1668 | } |
1669 | ||
22e757a4 DC |
1670 | /* |
1671 | * This is basically a copy of __set_page_dirty_buffers() with one | |
1672 | * small tweak: buffers beyond EOF do not get marked dirty. If we mark them | |
1673 | * dirty, we'll never be able to clean them because we don't write buffers | |
1674 | * beyond EOF, and that means we can't invalidate pages that span EOF | |
1675 | * that have been marked dirty. Further, the dirty state can leak into | |
1676 | * the file interior if the file is extended, resulting in all sorts of | |
1677 | * bad things happening as the state does not match the underlying data. | |
1678 | * | |
1679 | * XXX: this really indicates that bufferheads in XFS need to die. Warts like | |
1680 | * this only exist because of bufferheads and how the generic code manages them. | |
1681 | */ | |
1682 | STATIC int | |
1683 | xfs_vm_set_page_dirty( | |
1684 | struct page *page) | |
1685 | { | |
1686 | struct address_space *mapping = page->mapping; | |
1687 | struct inode *inode = mapping->host; | |
1688 | loff_t end_offset; | |
1689 | loff_t offset; | |
1690 | int newly_dirty; | |
1691 | ||
1692 | if (unlikely(!mapping)) | |
1693 | return !TestSetPageDirty(page); | |
1694 | ||
1695 | end_offset = i_size_read(inode); | |
1696 | offset = page_offset(page); | |
1697 | ||
1698 | spin_lock(&mapping->private_lock); | |
1699 | if (page_has_buffers(page)) { | |
1700 | struct buffer_head *head = page_buffers(page); | |
1701 | struct buffer_head *bh = head; | |
1702 | ||
1703 | do { | |
1704 | if (offset < end_offset) | |
1705 | set_buffer_dirty(bh); | |
1706 | bh = bh->b_this_page; | |
1707 | offset += 1 << inode->i_blkbits; | |
1708 | } while (bh != head); | |
1709 | } | |
c4843a75 | 1710 | /* |
81f8c3a4 JW |
1711 | * Lock out page->mem_cgroup migration to keep PageDirty |
1712 | * synchronized with per-memcg dirty page counters. | |
c4843a75 | 1713 | */ |
62cccb8c | 1714 | lock_page_memcg(page); |
22e757a4 DC |
1715 | newly_dirty = !TestSetPageDirty(page); |
1716 | spin_unlock(&mapping->private_lock); | |
1717 | ||
1718 | if (newly_dirty) { | |
1719 | /* sigh - __set_page_dirty() is static, so copy it here, too */ | |
1720 | unsigned long flags; | |
1721 | ||
1722 | spin_lock_irqsave(&mapping->tree_lock, flags); | |
1723 | if (page->mapping) { /* Race with truncate? */ | |
1724 | WARN_ON_ONCE(!PageUptodate(page)); | |
62cccb8c | 1725 | account_page_dirtied(page, mapping); |
22e757a4 DC |
1726 | radix_tree_tag_set(&mapping->page_tree, |
1727 | page_index(page), PAGECACHE_TAG_DIRTY); | |
1728 | } | |
1729 | spin_unlock_irqrestore(&mapping->tree_lock, flags); | |
22e757a4 | 1730 | } |
62cccb8c | 1731 | unlock_page_memcg(page); |
c4843a75 GT |
1732 | if (newly_dirty) |
1733 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); | |
22e757a4 DC |
1734 | return newly_dirty; |
1735 | } | |
1736 | ||
f5e54d6e | 1737 | const struct address_space_operations xfs_address_space_operations = { |
e4c573bb NS |
1738 | .readpage = xfs_vm_readpage, |
1739 | .readpages = xfs_vm_readpages, | |
1740 | .writepage = xfs_vm_writepage, | |
7d4fb40a | 1741 | .writepages = xfs_vm_writepages, |
22e757a4 | 1742 | .set_page_dirty = xfs_vm_set_page_dirty, |
238f4c54 NS |
1743 | .releasepage = xfs_vm_releasepage, |
1744 | .invalidatepage = xfs_vm_invalidatepage, | |
d79689c7 | 1745 | .write_begin = xfs_vm_write_begin, |
fa9b227e | 1746 | .write_end = xfs_vm_write_end, |
e4c573bb NS |
1747 | .bmap = xfs_vm_bmap, |
1748 | .direct_IO = xfs_vm_direct_IO, | |
e965f963 | 1749 | .migratepage = buffer_migrate_page, |
bddaafa1 | 1750 | .is_partially_uptodate = block_is_partially_uptodate, |
aa261f54 | 1751 | .error_remove_page = generic_error_remove_page, |
1da177e4 | 1752 | }; |