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