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