]>
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" |
a844f451 | 19 | #include "xfs_bit.h" |
1da177e4 | 20 | #include "xfs_log.h" |
a844f451 | 21 | #include "xfs_inum.h" |
1da177e4 | 22 | #include "xfs_sb.h" |
a844f451 | 23 | #include "xfs_ag.h" |
1da177e4 | 24 | #include "xfs_trans.h" |
1da177e4 LT |
25 | #include "xfs_mount.h" |
26 | #include "xfs_bmap_btree.h" | |
1da177e4 LT |
27 | #include "xfs_dinode.h" |
28 | #include "xfs_inode.h" | |
a844f451 | 29 | #include "xfs_alloc.h" |
1da177e4 LT |
30 | #include "xfs_error.h" |
31 | #include "xfs_rw.h" | |
32 | #include "xfs_iomap.h" | |
739bfb2a | 33 | #include "xfs_vnodeops.h" |
0b1b213f | 34 | #include "xfs_trace.h" |
3ed3a434 | 35 | #include "xfs_bmap.h" |
5a0e3ad6 | 36 | #include <linux/gfp.h> |
1da177e4 | 37 | #include <linux/mpage.h> |
10ce4444 | 38 | #include <linux/pagevec.h> |
1da177e4 LT |
39 | #include <linux/writeback.h> |
40 | ||
0b1b213f | 41 | void |
f51623b2 NS |
42 | xfs_count_page_state( |
43 | struct page *page, | |
44 | int *delalloc, | |
f51623b2 NS |
45 | int *unwritten) |
46 | { | |
47 | struct buffer_head *bh, *head; | |
48 | ||
20cb52eb | 49 | *delalloc = *unwritten = 0; |
f51623b2 NS |
50 | |
51 | bh = head = page_buffers(page); | |
52 | do { | |
20cb52eb | 53 | if (buffer_unwritten(bh)) |
f51623b2 NS |
54 | (*unwritten) = 1; |
55 | else if (buffer_delay(bh)) | |
56 | (*delalloc) = 1; | |
57 | } while ((bh = bh->b_this_page) != head); | |
58 | } | |
59 | ||
6214ed44 CH |
60 | STATIC struct block_device * |
61 | xfs_find_bdev_for_inode( | |
046f1685 | 62 | struct inode *inode) |
6214ed44 | 63 | { |
046f1685 | 64 | struct xfs_inode *ip = XFS_I(inode); |
6214ed44 CH |
65 | struct xfs_mount *mp = ip->i_mount; |
66 | ||
71ddabb9 | 67 | if (XFS_IS_REALTIME_INODE(ip)) |
6214ed44 CH |
68 | return mp->m_rtdev_targp->bt_bdev; |
69 | else | |
70 | return mp->m_ddev_targp->bt_bdev; | |
71 | } | |
72 | ||
f6d6d4fc CH |
73 | /* |
74 | * We're now finished for good with this ioend structure. | |
75 | * Update the page state via the associated buffer_heads, | |
76 | * release holds on the inode and bio, and finally free | |
77 | * up memory. Do not use the ioend after this. | |
78 | */ | |
0829c360 CH |
79 | STATIC void |
80 | xfs_destroy_ioend( | |
81 | xfs_ioend_t *ioend) | |
82 | { | |
f6d6d4fc CH |
83 | struct buffer_head *bh, *next; |
84 | ||
85 | for (bh = ioend->io_buffer_head; bh; bh = next) { | |
86 | next = bh->b_private; | |
7d04a335 | 87 | bh->b_end_io(bh, !ioend->io_error); |
f6d6d4fc | 88 | } |
583fa586 | 89 | |
c859cdd1 | 90 | if (ioend->io_iocb) { |
04f658ee CH |
91 | if (ioend->io_isasync) { |
92 | aio_complete(ioend->io_iocb, ioend->io_error ? | |
93 | ioend->io_error : ioend->io_result, 0); | |
94 | } | |
c859cdd1 CH |
95 | inode_dio_done(ioend->io_inode); |
96 | } | |
4a06fd26 | 97 | |
0829c360 CH |
98 | mempool_free(ioend, xfs_ioend_pool); |
99 | } | |
100 | ||
932640e8 DC |
101 | /* |
102 | * If the end of the current ioend is beyond the current EOF, | |
103 | * return the new EOF value, otherwise zero. | |
104 | */ | |
105 | STATIC xfs_fsize_t | |
106 | xfs_ioend_new_eof( | |
107 | xfs_ioend_t *ioend) | |
108 | { | |
109 | xfs_inode_t *ip = XFS_I(ioend->io_inode); | |
110 | xfs_fsize_t isize; | |
111 | xfs_fsize_t bsize; | |
112 | ||
113 | bsize = ioend->io_offset + ioend->io_size; | |
2813d682 | 114 | isize = MIN(i_size_read(VFS_I(ip)), bsize); |
932640e8 DC |
115 | return isize > ip->i_d.di_size ? isize : 0; |
116 | } | |
117 | ||
fc0063c4 CH |
118 | /* |
119 | * Fast and loose check if this write could update the on-disk inode size. | |
120 | */ | |
121 | static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend) | |
122 | { | |
123 | return ioend->io_offset + ioend->io_size > | |
124 | XFS_I(ioend->io_inode)->i_d.di_size; | |
125 | } | |
126 | ||
ba87ea69 | 127 | /* |
2813d682 | 128 | * Update on-disk file size now that data has been written to disk. |
ba87ea69 | 129 | */ |
aa6bf01d | 130 | STATIC void |
ba87ea69 | 131 | xfs_setfilesize( |
aa6bf01d | 132 | struct xfs_ioend *ioend) |
ba87ea69 | 133 | { |
aa6bf01d | 134 | struct xfs_inode *ip = XFS_I(ioend->io_inode); |
ba87ea69 | 135 | xfs_fsize_t isize; |
ba87ea69 | 136 | |
aa6bf01d | 137 | xfs_ilock(ip, XFS_ILOCK_EXCL); |
932640e8 DC |
138 | isize = xfs_ioend_new_eof(ioend); |
139 | if (isize) { | |
55fb25d5 | 140 | trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size); |
ba87ea69 | 141 | ip->i_d.di_size = isize; |
66d834ea | 142 | xfs_mark_inode_dirty(ip); |
ba87ea69 LM |
143 | } |
144 | ||
145 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
77d7a0c2 DC |
146 | } |
147 | ||
148 | /* | |
209fb87a | 149 | * Schedule IO completion handling on the final put of an ioend. |
fc0063c4 CH |
150 | * |
151 | * If there is no work to do we might as well call it a day and free the | |
152 | * ioend right now. | |
77d7a0c2 DC |
153 | */ |
154 | STATIC void | |
155 | xfs_finish_ioend( | |
209fb87a | 156 | struct xfs_ioend *ioend) |
77d7a0c2 DC |
157 | { |
158 | if (atomic_dec_and_test(&ioend->io_remaining)) { | |
aa6bf01d CH |
159 | struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount; |
160 | ||
209fb87a | 161 | if (ioend->io_type == IO_UNWRITTEN) |
aa6bf01d | 162 | queue_work(mp->m_unwritten_workqueue, &ioend->io_work); |
fc0063c4 | 163 | else if (xfs_ioend_is_append(ioend)) |
aa6bf01d | 164 | queue_work(mp->m_data_workqueue, &ioend->io_work); |
fc0063c4 CH |
165 | else |
166 | xfs_destroy_ioend(ioend); | |
77d7a0c2 | 167 | } |
ba87ea69 LM |
168 | } |
169 | ||
0829c360 | 170 | /* |
5ec4fabb | 171 | * IO write completion. |
f6d6d4fc CH |
172 | */ |
173 | STATIC void | |
5ec4fabb | 174 | xfs_end_io( |
77d7a0c2 | 175 | struct work_struct *work) |
0829c360 | 176 | { |
77d7a0c2 DC |
177 | xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work); |
178 | struct xfs_inode *ip = XFS_I(ioend->io_inode); | |
69418932 | 179 | int error = 0; |
ba87ea69 | 180 | |
04f658ee | 181 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
810627d9 | 182 | ioend->io_error = -EIO; |
04f658ee CH |
183 | goto done; |
184 | } | |
185 | if (ioend->io_error) | |
186 | goto done; | |
187 | ||
5ec4fabb CH |
188 | /* |
189 | * For unwritten extents we need to issue transactions to convert a | |
190 | * range to normal written extens after the data I/O has finished. | |
191 | */ | |
04f658ee | 192 | if (ioend->io_type == IO_UNWRITTEN) { |
5ec4fabb CH |
193 | error = xfs_iomap_write_unwritten(ip, ioend->io_offset, |
194 | ioend->io_size); | |
04f658ee CH |
195 | if (error) { |
196 | ioend->io_error = -error; | |
197 | goto done; | |
198 | } | |
5ec4fabb | 199 | } |
ba87ea69 | 200 | |
5ec4fabb CH |
201 | /* |
202 | * We might have to update the on-disk file size after extending | |
203 | * writes. | |
204 | */ | |
aa6bf01d | 205 | xfs_setfilesize(ioend); |
04f658ee | 206 | done: |
aa6bf01d | 207 | xfs_destroy_ioend(ioend); |
c626d174 DC |
208 | } |
209 | ||
209fb87a CH |
210 | /* |
211 | * Call IO completion handling in caller context on the final put of an ioend. | |
212 | */ | |
213 | STATIC void | |
214 | xfs_finish_ioend_sync( | |
215 | struct xfs_ioend *ioend) | |
216 | { | |
217 | if (atomic_dec_and_test(&ioend->io_remaining)) | |
218 | xfs_end_io(&ioend->io_work); | |
219 | } | |
220 | ||
0829c360 CH |
221 | /* |
222 | * Allocate and initialise an IO completion structure. | |
223 | * We need to track unwritten extent write completion here initially. | |
224 | * We'll need to extend this for updating the ondisk inode size later | |
225 | * (vs. incore size). | |
226 | */ | |
227 | STATIC xfs_ioend_t * | |
228 | xfs_alloc_ioend( | |
f6d6d4fc CH |
229 | struct inode *inode, |
230 | unsigned int type) | |
0829c360 CH |
231 | { |
232 | xfs_ioend_t *ioend; | |
233 | ||
234 | ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS); | |
235 | ||
236 | /* | |
237 | * Set the count to 1 initially, which will prevent an I/O | |
238 | * completion callback from happening before we have started | |
239 | * all the I/O from calling the completion routine too early. | |
240 | */ | |
241 | atomic_set(&ioend->io_remaining, 1); | |
c859cdd1 | 242 | ioend->io_isasync = 0; |
7d04a335 | 243 | ioend->io_error = 0; |
f6d6d4fc CH |
244 | ioend->io_list = NULL; |
245 | ioend->io_type = type; | |
b677c210 | 246 | ioend->io_inode = inode; |
c1a073bd | 247 | ioend->io_buffer_head = NULL; |
f6d6d4fc | 248 | ioend->io_buffer_tail = NULL; |
0829c360 CH |
249 | ioend->io_offset = 0; |
250 | ioend->io_size = 0; | |
fb511f21 CH |
251 | ioend->io_iocb = NULL; |
252 | ioend->io_result = 0; | |
0829c360 | 253 | |
5ec4fabb | 254 | INIT_WORK(&ioend->io_work, xfs_end_io); |
0829c360 CH |
255 | return ioend; |
256 | } | |
257 | ||
1da177e4 LT |
258 | STATIC int |
259 | xfs_map_blocks( | |
260 | struct inode *inode, | |
261 | loff_t offset, | |
207d0416 | 262 | struct xfs_bmbt_irec *imap, |
a206c817 CH |
263 | int type, |
264 | int nonblocking) | |
1da177e4 | 265 | { |
a206c817 CH |
266 | struct xfs_inode *ip = XFS_I(inode); |
267 | struct xfs_mount *mp = ip->i_mount; | |
ed1e7b7e | 268 | ssize_t count = 1 << inode->i_blkbits; |
a206c817 CH |
269 | xfs_fileoff_t offset_fsb, end_fsb; |
270 | int error = 0; | |
a206c817 CH |
271 | int bmapi_flags = XFS_BMAPI_ENTIRE; |
272 | int nimaps = 1; | |
273 | ||
274 | if (XFS_FORCED_SHUTDOWN(mp)) | |
275 | return -XFS_ERROR(EIO); | |
276 | ||
8ff2957d | 277 | if (type == IO_UNWRITTEN) |
a206c817 | 278 | bmapi_flags |= XFS_BMAPI_IGSTATE; |
8ff2957d CH |
279 | |
280 | if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) { | |
281 | if (nonblocking) | |
282 | return -XFS_ERROR(EAGAIN); | |
283 | xfs_ilock(ip, XFS_ILOCK_SHARED); | |
a206c817 CH |
284 | } |
285 | ||
8ff2957d CH |
286 | ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE || |
287 | (ip->i_df.if_flags & XFS_IFEXTENTS)); | |
a206c817 | 288 | ASSERT(offset <= mp->m_maxioffset); |
8ff2957d | 289 | |
a206c817 CH |
290 | if (offset + count > mp->m_maxioffset) |
291 | count = mp->m_maxioffset - offset; | |
292 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count); | |
293 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
5c8ed202 DC |
294 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
295 | imap, &nimaps, bmapi_flags); | |
8ff2957d | 296 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
a206c817 | 297 | |
8ff2957d CH |
298 | if (error) |
299 | return -XFS_ERROR(error); | |
a206c817 | 300 | |
8ff2957d CH |
301 | if (type == IO_DELALLOC && |
302 | (!nimaps || isnullstartblock(imap->br_startblock))) { | |
a206c817 CH |
303 | error = xfs_iomap_write_allocate(ip, offset, count, imap); |
304 | if (!error) | |
305 | trace_xfs_map_blocks_alloc(ip, offset, count, type, imap); | |
8ff2957d | 306 | return -XFS_ERROR(error); |
a206c817 CH |
307 | } |
308 | ||
8ff2957d CH |
309 | #ifdef DEBUG |
310 | if (type == IO_UNWRITTEN) { | |
311 | ASSERT(nimaps); | |
312 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); | |
313 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); | |
314 | } | |
315 | #endif | |
316 | if (nimaps) | |
317 | trace_xfs_map_blocks_found(ip, offset, count, type, imap); | |
318 | return 0; | |
1da177e4 LT |
319 | } |
320 | ||
b8f82a4a | 321 | STATIC int |
558e6891 | 322 | xfs_imap_valid( |
8699bb0a | 323 | struct inode *inode, |
207d0416 | 324 | struct xfs_bmbt_irec *imap, |
558e6891 | 325 | xfs_off_t offset) |
1da177e4 | 326 | { |
558e6891 | 327 | offset >>= inode->i_blkbits; |
8699bb0a | 328 | |
558e6891 CH |
329 | return offset >= imap->br_startoff && |
330 | offset < imap->br_startoff + imap->br_blockcount; | |
1da177e4 LT |
331 | } |
332 | ||
f6d6d4fc CH |
333 | /* |
334 | * BIO completion handler for buffered IO. | |
335 | */ | |
782e3b3b | 336 | STATIC void |
f6d6d4fc CH |
337 | xfs_end_bio( |
338 | struct bio *bio, | |
f6d6d4fc CH |
339 | int error) |
340 | { | |
341 | xfs_ioend_t *ioend = bio->bi_private; | |
342 | ||
f6d6d4fc | 343 | ASSERT(atomic_read(&bio->bi_cnt) >= 1); |
7d04a335 | 344 | ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error; |
f6d6d4fc CH |
345 | |
346 | /* Toss bio and pass work off to an xfsdatad thread */ | |
f6d6d4fc CH |
347 | bio->bi_private = NULL; |
348 | bio->bi_end_io = NULL; | |
f6d6d4fc | 349 | bio_put(bio); |
7d04a335 | 350 | |
209fb87a | 351 | xfs_finish_ioend(ioend); |
f6d6d4fc CH |
352 | } |
353 | ||
354 | STATIC void | |
355 | xfs_submit_ioend_bio( | |
06342cf8 CH |
356 | struct writeback_control *wbc, |
357 | xfs_ioend_t *ioend, | |
358 | struct bio *bio) | |
f6d6d4fc CH |
359 | { |
360 | atomic_inc(&ioend->io_remaining); | |
f6d6d4fc CH |
361 | bio->bi_private = ioend; |
362 | bio->bi_end_io = xfs_end_bio; | |
363 | ||
932640e8 DC |
364 | /* |
365 | * If the I/O is beyond EOF we mark the inode dirty immediately | |
366 | * but don't update the inode size until I/O completion. | |
367 | */ | |
368 | if (xfs_ioend_new_eof(ioend)) | |
66d834ea | 369 | xfs_mark_inode_dirty(XFS_I(ioend->io_inode)); |
932640e8 | 370 | |
721a9602 | 371 | submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio); |
f6d6d4fc CH |
372 | } |
373 | ||
374 | STATIC struct bio * | |
375 | xfs_alloc_ioend_bio( | |
376 | struct buffer_head *bh) | |
377 | { | |
f6d6d4fc | 378 | int nvecs = bio_get_nr_vecs(bh->b_bdev); |
221cb251 | 379 | struct bio *bio = bio_alloc(GFP_NOIO, nvecs); |
f6d6d4fc CH |
380 | |
381 | ASSERT(bio->bi_private == NULL); | |
382 | bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); | |
383 | bio->bi_bdev = bh->b_bdev; | |
f6d6d4fc CH |
384 | return bio; |
385 | } | |
386 | ||
387 | STATIC void | |
388 | xfs_start_buffer_writeback( | |
389 | struct buffer_head *bh) | |
390 | { | |
391 | ASSERT(buffer_mapped(bh)); | |
392 | ASSERT(buffer_locked(bh)); | |
393 | ASSERT(!buffer_delay(bh)); | |
394 | ASSERT(!buffer_unwritten(bh)); | |
395 | ||
396 | mark_buffer_async_write(bh); | |
397 | set_buffer_uptodate(bh); | |
398 | clear_buffer_dirty(bh); | |
399 | } | |
400 | ||
401 | STATIC void | |
402 | xfs_start_page_writeback( | |
403 | struct page *page, | |
f6d6d4fc CH |
404 | int clear_dirty, |
405 | int buffers) | |
406 | { | |
407 | ASSERT(PageLocked(page)); | |
408 | ASSERT(!PageWriteback(page)); | |
f6d6d4fc | 409 | if (clear_dirty) |
92132021 DC |
410 | clear_page_dirty_for_io(page); |
411 | set_page_writeback(page); | |
f6d6d4fc | 412 | unlock_page(page); |
1f7decf6 FW |
413 | /* If no buffers on the page are to be written, finish it here */ |
414 | if (!buffers) | |
f6d6d4fc | 415 | end_page_writeback(page); |
f6d6d4fc CH |
416 | } |
417 | ||
418 | static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh) | |
419 | { | |
420 | return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh)); | |
421 | } | |
422 | ||
423 | /* | |
d88992f6 DC |
424 | * Submit all of the bios for all of the ioends we have saved up, covering the |
425 | * initial writepage page and also any probed pages. | |
426 | * | |
427 | * Because we may have multiple ioends spanning a page, we need to start | |
428 | * writeback on all the buffers before we submit them for I/O. If we mark the | |
429 | * buffers as we got, then we can end up with a page that only has buffers | |
430 | * marked async write and I/O complete on can occur before we mark the other | |
431 | * buffers async write. | |
432 | * | |
433 | * The end result of this is that we trip a bug in end_page_writeback() because | |
434 | * we call it twice for the one page as the code in end_buffer_async_write() | |
435 | * assumes that all buffers on the page are started at the same time. | |
436 | * | |
437 | * The fix is two passes across the ioend list - one to start writeback on the | |
c41564b5 | 438 | * buffer_heads, and then submit them for I/O on the second pass. |
f6d6d4fc CH |
439 | */ |
440 | STATIC void | |
441 | xfs_submit_ioend( | |
06342cf8 | 442 | struct writeback_control *wbc, |
f6d6d4fc CH |
443 | xfs_ioend_t *ioend) |
444 | { | |
d88992f6 | 445 | xfs_ioend_t *head = ioend; |
f6d6d4fc CH |
446 | xfs_ioend_t *next; |
447 | struct buffer_head *bh; | |
448 | struct bio *bio; | |
449 | sector_t lastblock = 0; | |
450 | ||
d88992f6 DC |
451 | /* Pass 1 - start writeback */ |
452 | do { | |
453 | next = ioend->io_list; | |
221cb251 | 454 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) |
d88992f6 | 455 | xfs_start_buffer_writeback(bh); |
d88992f6 DC |
456 | } while ((ioend = next) != NULL); |
457 | ||
458 | /* Pass 2 - submit I/O */ | |
459 | ioend = head; | |
f6d6d4fc CH |
460 | do { |
461 | next = ioend->io_list; | |
462 | bio = NULL; | |
463 | ||
464 | for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) { | |
f6d6d4fc CH |
465 | |
466 | if (!bio) { | |
467 | retry: | |
468 | bio = xfs_alloc_ioend_bio(bh); | |
469 | } else if (bh->b_blocknr != lastblock + 1) { | |
06342cf8 | 470 | xfs_submit_ioend_bio(wbc, ioend, bio); |
f6d6d4fc CH |
471 | goto retry; |
472 | } | |
473 | ||
474 | if (bio_add_buffer(bio, bh) != bh->b_size) { | |
06342cf8 | 475 | xfs_submit_ioend_bio(wbc, ioend, bio); |
f6d6d4fc CH |
476 | goto retry; |
477 | } | |
478 | ||
479 | lastblock = bh->b_blocknr; | |
480 | } | |
481 | if (bio) | |
06342cf8 | 482 | xfs_submit_ioend_bio(wbc, ioend, bio); |
209fb87a | 483 | xfs_finish_ioend(ioend); |
f6d6d4fc CH |
484 | } while ((ioend = next) != NULL); |
485 | } | |
486 | ||
487 | /* | |
488 | * Cancel submission of all buffer_heads so far in this endio. | |
489 | * Toss the endio too. Only ever called for the initial page | |
490 | * in a writepage request, so only ever one page. | |
491 | */ | |
492 | STATIC void | |
493 | xfs_cancel_ioend( | |
494 | xfs_ioend_t *ioend) | |
495 | { | |
496 | xfs_ioend_t *next; | |
497 | struct buffer_head *bh, *next_bh; | |
498 | ||
499 | do { | |
500 | next = ioend->io_list; | |
501 | bh = ioend->io_buffer_head; | |
502 | do { | |
503 | next_bh = bh->b_private; | |
504 | clear_buffer_async_write(bh); | |
505 | unlock_buffer(bh); | |
506 | } while ((bh = next_bh) != NULL); | |
507 | ||
f6d6d4fc CH |
508 | mempool_free(ioend, xfs_ioend_pool); |
509 | } while ((ioend = next) != NULL); | |
510 | } | |
511 | ||
512 | /* | |
513 | * Test to see if we've been building up a completion structure for | |
514 | * earlier buffers -- if so, we try to append to this ioend if we | |
515 | * can, otherwise we finish off any current ioend and start another. | |
516 | * Return true if we've finished the given ioend. | |
517 | */ | |
518 | STATIC void | |
519 | xfs_add_to_ioend( | |
520 | struct inode *inode, | |
521 | struct buffer_head *bh, | |
7336cea8 | 522 | xfs_off_t offset, |
f6d6d4fc CH |
523 | unsigned int type, |
524 | xfs_ioend_t **result, | |
525 | int need_ioend) | |
526 | { | |
527 | xfs_ioend_t *ioend = *result; | |
528 | ||
529 | if (!ioend || need_ioend || type != ioend->io_type) { | |
530 | xfs_ioend_t *previous = *result; | |
f6d6d4fc | 531 | |
f6d6d4fc CH |
532 | ioend = xfs_alloc_ioend(inode, type); |
533 | ioend->io_offset = offset; | |
534 | ioend->io_buffer_head = bh; | |
535 | ioend->io_buffer_tail = bh; | |
536 | if (previous) | |
537 | previous->io_list = ioend; | |
538 | *result = ioend; | |
539 | } else { | |
540 | ioend->io_buffer_tail->b_private = bh; | |
541 | ioend->io_buffer_tail = bh; | |
542 | } | |
543 | ||
544 | bh->b_private = NULL; | |
545 | ioend->io_size += bh->b_size; | |
546 | } | |
547 | ||
87cbc49c NS |
548 | STATIC void |
549 | xfs_map_buffer( | |
046f1685 | 550 | struct inode *inode, |
87cbc49c | 551 | struct buffer_head *bh, |
207d0416 | 552 | struct xfs_bmbt_irec *imap, |
046f1685 | 553 | xfs_off_t offset) |
87cbc49c NS |
554 | { |
555 | sector_t bn; | |
8699bb0a | 556 | struct xfs_mount *m = XFS_I(inode)->i_mount; |
207d0416 CH |
557 | xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff); |
558 | xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock); | |
87cbc49c | 559 | |
207d0416 CH |
560 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
561 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); | |
87cbc49c | 562 | |
e513182d | 563 | bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) + |
8699bb0a | 564 | ((offset - iomap_offset) >> inode->i_blkbits); |
87cbc49c | 565 | |
046f1685 | 566 | ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode))); |
87cbc49c NS |
567 | |
568 | bh->b_blocknr = bn; | |
569 | set_buffer_mapped(bh); | |
570 | } | |
571 | ||
1da177e4 LT |
572 | STATIC void |
573 | xfs_map_at_offset( | |
046f1685 | 574 | struct inode *inode, |
1da177e4 | 575 | struct buffer_head *bh, |
207d0416 | 576 | struct xfs_bmbt_irec *imap, |
046f1685 | 577 | xfs_off_t offset) |
1da177e4 | 578 | { |
207d0416 CH |
579 | ASSERT(imap->br_startblock != HOLESTARTBLOCK); |
580 | ASSERT(imap->br_startblock != DELAYSTARTBLOCK); | |
1da177e4 | 581 | |
207d0416 | 582 | xfs_map_buffer(inode, bh, imap, offset); |
1da177e4 LT |
583 | set_buffer_mapped(bh); |
584 | clear_buffer_delay(bh); | |
f6d6d4fc | 585 | clear_buffer_unwritten(bh); |
1da177e4 LT |
586 | } |
587 | ||
1da177e4 | 588 | /* |
10ce4444 CH |
589 | * Test if a given page is suitable for writing as part of an unwritten |
590 | * or delayed allocate extent. | |
1da177e4 | 591 | */ |
10ce4444 CH |
592 | STATIC int |
593 | xfs_is_delayed_page( | |
594 | struct page *page, | |
f6d6d4fc | 595 | unsigned int type) |
1da177e4 | 596 | { |
1da177e4 | 597 | if (PageWriteback(page)) |
10ce4444 | 598 | return 0; |
1da177e4 LT |
599 | |
600 | if (page->mapping && page_has_buffers(page)) { | |
601 | struct buffer_head *bh, *head; | |
602 | int acceptable = 0; | |
603 | ||
604 | bh = head = page_buffers(page); | |
605 | do { | |
f6d6d4fc | 606 | if (buffer_unwritten(bh)) |
34a52c6c | 607 | acceptable = (type == IO_UNWRITTEN); |
f6d6d4fc | 608 | else if (buffer_delay(bh)) |
a206c817 | 609 | acceptable = (type == IO_DELALLOC); |
2ddee844 | 610 | else if (buffer_dirty(bh) && buffer_mapped(bh)) |
a206c817 | 611 | acceptable = (type == IO_OVERWRITE); |
f6d6d4fc | 612 | else |
1da177e4 | 613 | break; |
1da177e4 LT |
614 | } while ((bh = bh->b_this_page) != head); |
615 | ||
616 | if (acceptable) | |
10ce4444 | 617 | return 1; |
1da177e4 LT |
618 | } |
619 | ||
10ce4444 | 620 | return 0; |
1da177e4 LT |
621 | } |
622 | ||
1da177e4 LT |
623 | /* |
624 | * Allocate & map buffers for page given the extent map. Write it out. | |
625 | * except for the original page of a writepage, this is called on | |
626 | * delalloc/unwritten pages only, for the original page it is possible | |
627 | * that the page has no mapping at all. | |
628 | */ | |
f6d6d4fc | 629 | STATIC int |
1da177e4 LT |
630 | xfs_convert_page( |
631 | struct inode *inode, | |
632 | struct page *page, | |
10ce4444 | 633 | loff_t tindex, |
207d0416 | 634 | struct xfs_bmbt_irec *imap, |
f6d6d4fc | 635 | xfs_ioend_t **ioendp, |
2fa24f92 | 636 | struct writeback_control *wbc) |
1da177e4 | 637 | { |
f6d6d4fc | 638 | struct buffer_head *bh, *head; |
9260dc6b CH |
639 | xfs_off_t end_offset; |
640 | unsigned long p_offset; | |
f6d6d4fc | 641 | unsigned int type; |
24e17b5f | 642 | int len, page_dirty; |
f6d6d4fc | 643 | int count = 0, done = 0, uptodate = 1; |
9260dc6b | 644 | xfs_off_t offset = page_offset(page); |
1da177e4 | 645 | |
10ce4444 CH |
646 | if (page->index != tindex) |
647 | goto fail; | |
529ae9aa | 648 | if (!trylock_page(page)) |
10ce4444 CH |
649 | goto fail; |
650 | if (PageWriteback(page)) | |
651 | goto fail_unlock_page; | |
652 | if (page->mapping != inode->i_mapping) | |
653 | goto fail_unlock_page; | |
654 | if (!xfs_is_delayed_page(page, (*ioendp)->io_type)) | |
655 | goto fail_unlock_page; | |
656 | ||
24e17b5f NS |
657 | /* |
658 | * page_dirty is initially a count of buffers on the page before | |
c41564b5 | 659 | * EOF and is decremented as we move each into a cleanable state. |
9260dc6b CH |
660 | * |
661 | * Derivation: | |
662 | * | |
663 | * End offset is the highest offset that this page should represent. | |
664 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) | |
665 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and | |
666 | * hence give us the correct page_dirty count. On any other page, | |
667 | * it will be zero and in that case we need page_dirty to be the | |
668 | * count of buffers on the page. | |
24e17b5f | 669 | */ |
9260dc6b CH |
670 | end_offset = min_t(unsigned long long, |
671 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, | |
672 | i_size_read(inode)); | |
673 | ||
24e17b5f | 674 | len = 1 << inode->i_blkbits; |
9260dc6b CH |
675 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
676 | PAGE_CACHE_SIZE); | |
677 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; | |
678 | page_dirty = p_offset / len; | |
24e17b5f | 679 | |
1da177e4 LT |
680 | bh = head = page_buffers(page); |
681 | do { | |
9260dc6b | 682 | if (offset >= end_offset) |
1da177e4 | 683 | break; |
f6d6d4fc CH |
684 | if (!buffer_uptodate(bh)) |
685 | uptodate = 0; | |
686 | if (!(PageUptodate(page) || buffer_uptodate(bh))) { | |
687 | done = 1; | |
1da177e4 | 688 | continue; |
f6d6d4fc CH |
689 | } |
690 | ||
2fa24f92 CH |
691 | if (buffer_unwritten(bh) || buffer_delay(bh) || |
692 | buffer_mapped(bh)) { | |
9260dc6b | 693 | if (buffer_unwritten(bh)) |
34a52c6c | 694 | type = IO_UNWRITTEN; |
2fa24f92 | 695 | else if (buffer_delay(bh)) |
a206c817 | 696 | type = IO_DELALLOC; |
2fa24f92 CH |
697 | else |
698 | type = IO_OVERWRITE; | |
9260dc6b | 699 | |
558e6891 | 700 | if (!xfs_imap_valid(inode, imap, offset)) { |
f6d6d4fc | 701 | done = 1; |
9260dc6b CH |
702 | continue; |
703 | } | |
704 | ||
ecff71e6 CH |
705 | lock_buffer(bh); |
706 | if (type != IO_OVERWRITE) | |
2fa24f92 | 707 | xfs_map_at_offset(inode, bh, imap, offset); |
89f3b363 CH |
708 | xfs_add_to_ioend(inode, bh, offset, type, |
709 | ioendp, done); | |
710 | ||
9260dc6b CH |
711 | page_dirty--; |
712 | count++; | |
713 | } else { | |
2fa24f92 | 714 | done = 1; |
1da177e4 | 715 | } |
7336cea8 | 716 | } while (offset += len, (bh = bh->b_this_page) != head); |
1da177e4 | 717 | |
f6d6d4fc CH |
718 | if (uptodate && bh == head) |
719 | SetPageUptodate(page); | |
720 | ||
89f3b363 | 721 | if (count) { |
efceab1d DC |
722 | if (--wbc->nr_to_write <= 0 && |
723 | wbc->sync_mode == WB_SYNC_NONE) | |
89f3b363 | 724 | done = 1; |
1da177e4 | 725 | } |
89f3b363 | 726 | xfs_start_page_writeback(page, !page_dirty, count); |
f6d6d4fc CH |
727 | |
728 | return done; | |
10ce4444 CH |
729 | fail_unlock_page: |
730 | unlock_page(page); | |
731 | fail: | |
732 | return 1; | |
1da177e4 LT |
733 | } |
734 | ||
735 | /* | |
736 | * Convert & write out a cluster of pages in the same extent as defined | |
737 | * by mp and following the start page. | |
738 | */ | |
739 | STATIC void | |
740 | xfs_cluster_write( | |
741 | struct inode *inode, | |
742 | pgoff_t tindex, | |
207d0416 | 743 | struct xfs_bmbt_irec *imap, |
f6d6d4fc | 744 | xfs_ioend_t **ioendp, |
1da177e4 | 745 | struct writeback_control *wbc, |
1da177e4 LT |
746 | pgoff_t tlast) |
747 | { | |
10ce4444 CH |
748 | struct pagevec pvec; |
749 | int done = 0, i; | |
1da177e4 | 750 | |
10ce4444 CH |
751 | pagevec_init(&pvec, 0); |
752 | while (!done && tindex <= tlast) { | |
753 | unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1); | |
754 | ||
755 | if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len)) | |
1da177e4 | 756 | break; |
10ce4444 CH |
757 | |
758 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
759 | done = xfs_convert_page(inode, pvec.pages[i], tindex++, | |
2fa24f92 | 760 | imap, ioendp, wbc); |
10ce4444 CH |
761 | if (done) |
762 | break; | |
763 | } | |
764 | ||
765 | pagevec_release(&pvec); | |
766 | cond_resched(); | |
1da177e4 LT |
767 | } |
768 | } | |
769 | ||
3ed3a434 DC |
770 | STATIC void |
771 | xfs_vm_invalidatepage( | |
772 | struct page *page, | |
773 | unsigned long offset) | |
774 | { | |
775 | trace_xfs_invalidatepage(page->mapping->host, page, offset); | |
776 | block_invalidatepage(page, offset); | |
777 | } | |
778 | ||
779 | /* | |
780 | * If the page has delalloc buffers on it, we need to punch them out before we | |
781 | * invalidate the page. If we don't, we leave a stale delalloc mapping on the | |
782 | * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read | |
783 | * is done on that same region - the delalloc extent is returned when none is | |
784 | * supposed to be there. | |
785 | * | |
786 | * We prevent this by truncating away the delalloc regions on the page before | |
787 | * invalidating it. Because they are delalloc, we can do this without needing a | |
788 | * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this | |
789 | * truncation without a transaction as there is no space left for block | |
790 | * reservation (typically why we see a ENOSPC in writeback). | |
791 | * | |
792 | * This is not a performance critical path, so for now just do the punching a | |
793 | * buffer head at a time. | |
794 | */ | |
795 | STATIC void | |
796 | xfs_aops_discard_page( | |
797 | struct page *page) | |
798 | { | |
799 | struct inode *inode = page->mapping->host; | |
800 | struct xfs_inode *ip = XFS_I(inode); | |
801 | struct buffer_head *bh, *head; | |
802 | loff_t offset = page_offset(page); | |
3ed3a434 | 803 | |
a206c817 | 804 | if (!xfs_is_delayed_page(page, IO_DELALLOC)) |
3ed3a434 DC |
805 | goto out_invalidate; |
806 | ||
e8c3753c DC |
807 | if (XFS_FORCED_SHUTDOWN(ip->i_mount)) |
808 | goto out_invalidate; | |
809 | ||
4f10700a | 810 | xfs_alert(ip->i_mount, |
3ed3a434 DC |
811 | "page discard on page %p, inode 0x%llx, offset %llu.", |
812 | page, ip->i_ino, offset); | |
813 | ||
814 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
815 | bh = head = page_buffers(page); | |
816 | do { | |
3ed3a434 | 817 | int error; |
c726de44 | 818 | xfs_fileoff_t start_fsb; |
3ed3a434 DC |
819 | |
820 | if (!buffer_delay(bh)) | |
821 | goto next_buffer; | |
822 | ||
c726de44 DC |
823 | start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset); |
824 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1); | |
3ed3a434 DC |
825 | if (error) { |
826 | /* something screwed, just bail */ | |
e8c3753c | 827 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { |
4f10700a | 828 | xfs_alert(ip->i_mount, |
3ed3a434 | 829 | "page discard unable to remove delalloc mapping."); |
e8c3753c | 830 | } |
3ed3a434 DC |
831 | break; |
832 | } | |
833 | next_buffer: | |
c726de44 | 834 | offset += 1 << inode->i_blkbits; |
3ed3a434 DC |
835 | |
836 | } while ((bh = bh->b_this_page) != head); | |
837 | ||
838 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
839 | out_invalidate: | |
840 | xfs_vm_invalidatepage(page, 0); | |
841 | return; | |
842 | } | |
843 | ||
1da177e4 | 844 | /* |
89f3b363 CH |
845 | * Write out a dirty page. |
846 | * | |
847 | * For delalloc space on the page we need to allocate space and flush it. | |
848 | * For unwritten space on the page we need to start the conversion to | |
849 | * regular allocated space. | |
89f3b363 | 850 | * For any other dirty buffer heads on the page we should flush them. |
1da177e4 | 851 | */ |
1da177e4 | 852 | STATIC int |
89f3b363 CH |
853 | xfs_vm_writepage( |
854 | struct page *page, | |
855 | struct writeback_control *wbc) | |
1da177e4 | 856 | { |
89f3b363 | 857 | struct inode *inode = page->mapping->host; |
f6d6d4fc | 858 | struct buffer_head *bh, *head; |
207d0416 | 859 | struct xfs_bmbt_irec imap; |
f6d6d4fc | 860 | xfs_ioend_t *ioend = NULL, *iohead = NULL; |
1da177e4 | 861 | loff_t offset; |
f6d6d4fc | 862 | unsigned int type; |
1da177e4 | 863 | __uint64_t end_offset; |
bd1556a1 | 864 | pgoff_t end_index, last_index; |
ed1e7b7e | 865 | ssize_t len; |
a206c817 | 866 | int err, imap_valid = 0, uptodate = 1; |
89f3b363 | 867 | int count = 0; |
a206c817 | 868 | int nonblocking = 0; |
89f3b363 CH |
869 | |
870 | trace_xfs_writepage(inode, page, 0); | |
871 | ||
20cb52eb CH |
872 | ASSERT(page_has_buffers(page)); |
873 | ||
89f3b363 CH |
874 | /* |
875 | * Refuse to write the page out if we are called from reclaim context. | |
876 | * | |
d4f7a5cb CH |
877 | * This avoids stack overflows when called from deeply used stacks in |
878 | * random callers for direct reclaim or memcg reclaim. We explicitly | |
879 | * allow reclaim from kswapd as the stack usage there is relatively low. | |
89f3b363 | 880 | * |
94054fa3 MG |
881 | * This should never happen except in the case of a VM regression so |
882 | * warn about it. | |
89f3b363 | 883 | */ |
94054fa3 MG |
884 | if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == |
885 | PF_MEMALLOC)) | |
b5420f23 | 886 | goto redirty; |
1da177e4 | 887 | |
89f3b363 | 888 | /* |
680a647b CH |
889 | * Given that we do not allow direct reclaim to call us, we should |
890 | * never be called while in a filesystem transaction. | |
89f3b363 | 891 | */ |
680a647b | 892 | if (WARN_ON(current->flags & PF_FSTRANS)) |
b5420f23 | 893 | goto redirty; |
89f3b363 | 894 | |
1da177e4 LT |
895 | /* Is this page beyond the end of the file? */ |
896 | offset = i_size_read(inode); | |
897 | end_index = offset >> PAGE_CACHE_SHIFT; | |
898 | last_index = (offset - 1) >> PAGE_CACHE_SHIFT; | |
899 | if (page->index >= end_index) { | |
900 | if ((page->index >= end_index + 1) || | |
901 | !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) { | |
89f3b363 | 902 | unlock_page(page); |
19d5bcf3 | 903 | return 0; |
1da177e4 LT |
904 | } |
905 | } | |
906 | ||
f6d6d4fc | 907 | end_offset = min_t(unsigned long long, |
20cb52eb CH |
908 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, |
909 | offset); | |
24e17b5f | 910 | len = 1 << inode->i_blkbits; |
24e17b5f | 911 | |
24e17b5f | 912 | bh = head = page_buffers(page); |
f6d6d4fc | 913 | offset = page_offset(page); |
a206c817 CH |
914 | type = IO_OVERWRITE; |
915 | ||
dbcdde3e | 916 | if (wbc->sync_mode == WB_SYNC_NONE) |
a206c817 | 917 | nonblocking = 1; |
f6d6d4fc | 918 | |
1da177e4 | 919 | do { |
6ac7248e CH |
920 | int new_ioend = 0; |
921 | ||
1da177e4 LT |
922 | if (offset >= end_offset) |
923 | break; | |
924 | if (!buffer_uptodate(bh)) | |
925 | uptodate = 0; | |
1da177e4 | 926 | |
3d9b02e3 | 927 | /* |
ece413f5 CH |
928 | * set_page_dirty dirties all buffers in a page, independent |
929 | * of their state. The dirty state however is entirely | |
930 | * meaningless for holes (!mapped && uptodate), so skip | |
931 | * buffers covering holes here. | |
3d9b02e3 ES |
932 | */ |
933 | if (!buffer_mapped(bh) && buffer_uptodate(bh)) { | |
3d9b02e3 ES |
934 | imap_valid = 0; |
935 | continue; | |
936 | } | |
937 | ||
aeea1b1f CH |
938 | if (buffer_unwritten(bh)) { |
939 | if (type != IO_UNWRITTEN) { | |
940 | type = IO_UNWRITTEN; | |
941 | imap_valid = 0; | |
1da177e4 | 942 | } |
aeea1b1f CH |
943 | } else if (buffer_delay(bh)) { |
944 | if (type != IO_DELALLOC) { | |
945 | type = IO_DELALLOC; | |
946 | imap_valid = 0; | |
1da177e4 | 947 | } |
89f3b363 | 948 | } else if (buffer_uptodate(bh)) { |
a206c817 CH |
949 | if (type != IO_OVERWRITE) { |
950 | type = IO_OVERWRITE; | |
85da94c6 CH |
951 | imap_valid = 0; |
952 | } | |
aeea1b1f CH |
953 | } else { |
954 | if (PageUptodate(page)) { | |
955 | ASSERT(buffer_mapped(bh)); | |
956 | imap_valid = 0; | |
6c4fe19f | 957 | } |
aeea1b1f CH |
958 | continue; |
959 | } | |
d5cb48aa | 960 | |
aeea1b1f CH |
961 | if (imap_valid) |
962 | imap_valid = xfs_imap_valid(inode, &imap, offset); | |
963 | if (!imap_valid) { | |
964 | /* | |
965 | * If we didn't have a valid mapping then we need to | |
966 | * put the new mapping into a separate ioend structure. | |
967 | * This ensures non-contiguous extents always have | |
968 | * separate ioends, which is particularly important | |
969 | * for unwritten extent conversion at I/O completion | |
970 | * time. | |
971 | */ | |
972 | new_ioend = 1; | |
973 | err = xfs_map_blocks(inode, offset, &imap, type, | |
974 | nonblocking); | |
975 | if (err) | |
976 | goto error; | |
977 | imap_valid = xfs_imap_valid(inode, &imap, offset); | |
978 | } | |
979 | if (imap_valid) { | |
ecff71e6 CH |
980 | lock_buffer(bh); |
981 | if (type != IO_OVERWRITE) | |
aeea1b1f CH |
982 | xfs_map_at_offset(inode, bh, &imap, offset); |
983 | xfs_add_to_ioend(inode, bh, offset, type, &ioend, | |
984 | new_ioend); | |
985 | count++; | |
1da177e4 | 986 | } |
f6d6d4fc CH |
987 | |
988 | if (!iohead) | |
989 | iohead = ioend; | |
990 | ||
991 | } while (offset += len, ((bh = bh->b_this_page) != head)); | |
1da177e4 LT |
992 | |
993 | if (uptodate && bh == head) | |
994 | SetPageUptodate(page); | |
995 | ||
89f3b363 | 996 | xfs_start_page_writeback(page, 1, count); |
1da177e4 | 997 | |
558e6891 | 998 | if (ioend && imap_valid) { |
bd1556a1 CH |
999 | xfs_off_t end_index; |
1000 | ||
1001 | end_index = imap.br_startoff + imap.br_blockcount; | |
1002 | ||
1003 | /* to bytes */ | |
1004 | end_index <<= inode->i_blkbits; | |
1005 | ||
1006 | /* to pages */ | |
1007 | end_index = (end_index - 1) >> PAGE_CACHE_SHIFT; | |
1008 | ||
1009 | /* check against file size */ | |
1010 | if (end_index > last_index) | |
1011 | end_index = last_index; | |
8699bb0a | 1012 | |
207d0416 | 1013 | xfs_cluster_write(inode, page->index + 1, &imap, &ioend, |
2fa24f92 | 1014 | wbc, end_index); |
1da177e4 LT |
1015 | } |
1016 | ||
f6d6d4fc | 1017 | if (iohead) |
06342cf8 | 1018 | xfs_submit_ioend(wbc, iohead); |
f6d6d4fc | 1019 | |
89f3b363 | 1020 | return 0; |
1da177e4 LT |
1021 | |
1022 | error: | |
f6d6d4fc CH |
1023 | if (iohead) |
1024 | xfs_cancel_ioend(iohead); | |
1da177e4 | 1025 | |
b5420f23 CH |
1026 | if (err == -EAGAIN) |
1027 | goto redirty; | |
1028 | ||
20cb52eb | 1029 | xfs_aops_discard_page(page); |
89f3b363 CH |
1030 | ClearPageUptodate(page); |
1031 | unlock_page(page); | |
1da177e4 | 1032 | return err; |
f51623b2 | 1033 | |
b5420f23 | 1034 | redirty: |
f51623b2 NS |
1035 | redirty_page_for_writepage(wbc, page); |
1036 | unlock_page(page); | |
1037 | return 0; | |
f51623b2 NS |
1038 | } |
1039 | ||
7d4fb40a NS |
1040 | STATIC int |
1041 | xfs_vm_writepages( | |
1042 | struct address_space *mapping, | |
1043 | struct writeback_control *wbc) | |
1044 | { | |
b3aea4ed | 1045 | xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED); |
7d4fb40a NS |
1046 | return generic_writepages(mapping, wbc); |
1047 | } | |
1048 | ||
f51623b2 NS |
1049 | /* |
1050 | * Called to move a page into cleanable state - and from there | |
89f3b363 | 1051 | * to be released. The page should already be clean. We always |
f51623b2 NS |
1052 | * have buffer heads in this call. |
1053 | * | |
89f3b363 | 1054 | * Returns 1 if the page is ok to release, 0 otherwise. |
f51623b2 NS |
1055 | */ |
1056 | STATIC int | |
238f4c54 | 1057 | xfs_vm_releasepage( |
f51623b2 NS |
1058 | struct page *page, |
1059 | gfp_t gfp_mask) | |
1060 | { | |
20cb52eb | 1061 | int delalloc, unwritten; |
f51623b2 | 1062 | |
89f3b363 | 1063 | trace_xfs_releasepage(page->mapping->host, page, 0); |
238f4c54 | 1064 | |
20cb52eb | 1065 | xfs_count_page_state(page, &delalloc, &unwritten); |
f51623b2 | 1066 | |
89f3b363 | 1067 | if (WARN_ON(delalloc)) |
f51623b2 | 1068 | return 0; |
89f3b363 | 1069 | if (WARN_ON(unwritten)) |
f51623b2 NS |
1070 | return 0; |
1071 | ||
f51623b2 NS |
1072 | return try_to_free_buffers(page); |
1073 | } | |
1074 | ||
1da177e4 | 1075 | STATIC int |
c2536668 | 1076 | __xfs_get_blocks( |
1da177e4 LT |
1077 | struct inode *inode, |
1078 | sector_t iblock, | |
1da177e4 LT |
1079 | struct buffer_head *bh_result, |
1080 | int create, | |
f2bde9b8 | 1081 | int direct) |
1da177e4 | 1082 | { |
a206c817 CH |
1083 | struct xfs_inode *ip = XFS_I(inode); |
1084 | struct xfs_mount *mp = ip->i_mount; | |
1085 | xfs_fileoff_t offset_fsb, end_fsb; | |
1086 | int error = 0; | |
1087 | int lockmode = 0; | |
207d0416 | 1088 | struct xfs_bmbt_irec imap; |
a206c817 | 1089 | int nimaps = 1; |
fdc7ed75 NS |
1090 | xfs_off_t offset; |
1091 | ssize_t size; | |
207d0416 | 1092 | int new = 0; |
a206c817 CH |
1093 | |
1094 | if (XFS_FORCED_SHUTDOWN(mp)) | |
1095 | return -XFS_ERROR(EIO); | |
1da177e4 | 1096 | |
fdc7ed75 | 1097 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
c2536668 NS |
1098 | ASSERT(bh_result->b_size >= (1 << inode->i_blkbits)); |
1099 | size = bh_result->b_size; | |
364f358a LM |
1100 | |
1101 | if (!create && direct && offset >= i_size_read(inode)) | |
1102 | return 0; | |
1103 | ||
a206c817 CH |
1104 | if (create) { |
1105 | lockmode = XFS_ILOCK_EXCL; | |
1106 | xfs_ilock(ip, lockmode); | |
1107 | } else { | |
1108 | lockmode = xfs_ilock_map_shared(ip); | |
1109 | } | |
f2bde9b8 | 1110 | |
a206c817 CH |
1111 | ASSERT(offset <= mp->m_maxioffset); |
1112 | if (offset + size > mp->m_maxioffset) | |
1113 | size = mp->m_maxioffset - offset; | |
1114 | end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size); | |
1115 | offset_fsb = XFS_B_TO_FSBT(mp, offset); | |
1116 | ||
5c8ed202 DC |
1117 | error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb, |
1118 | &imap, &nimaps, XFS_BMAPI_ENTIRE); | |
1da177e4 | 1119 | if (error) |
a206c817 CH |
1120 | goto out_unlock; |
1121 | ||
1122 | if (create && | |
1123 | (!nimaps || | |
1124 | (imap.br_startblock == HOLESTARTBLOCK || | |
1125 | imap.br_startblock == DELAYSTARTBLOCK))) { | |
1126 | if (direct) { | |
1127 | error = xfs_iomap_write_direct(ip, offset, size, | |
1128 | &imap, nimaps); | |
1129 | } else { | |
1130 | error = xfs_iomap_write_delay(ip, offset, size, &imap); | |
1131 | } | |
1132 | if (error) | |
1133 | goto out_unlock; | |
1134 | ||
1135 | trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap); | |
1136 | } else if (nimaps) { | |
1137 | trace_xfs_get_blocks_found(ip, offset, size, 0, &imap); | |
1138 | } else { | |
1139 | trace_xfs_get_blocks_notfound(ip, offset, size); | |
1140 | goto out_unlock; | |
1141 | } | |
1142 | xfs_iunlock(ip, lockmode); | |
1da177e4 | 1143 | |
207d0416 CH |
1144 | if (imap.br_startblock != HOLESTARTBLOCK && |
1145 | imap.br_startblock != DELAYSTARTBLOCK) { | |
87cbc49c NS |
1146 | /* |
1147 | * For unwritten extents do not report a disk address on | |
1da177e4 LT |
1148 | * the read case (treat as if we're reading into a hole). |
1149 | */ | |
207d0416 CH |
1150 | if (create || !ISUNWRITTEN(&imap)) |
1151 | xfs_map_buffer(inode, bh_result, &imap, offset); | |
1152 | if (create && ISUNWRITTEN(&imap)) { | |
1da177e4 LT |
1153 | if (direct) |
1154 | bh_result->b_private = inode; | |
1155 | set_buffer_unwritten(bh_result); | |
1da177e4 LT |
1156 | } |
1157 | } | |
1158 | ||
c2536668 NS |
1159 | /* |
1160 | * If this is a realtime file, data may be on a different device. | |
1161 | * to that pointed to from the buffer_head b_bdev currently. | |
1162 | */ | |
046f1685 | 1163 | bh_result->b_bdev = xfs_find_bdev_for_inode(inode); |
1da177e4 | 1164 | |
c2536668 | 1165 | /* |
549054af DC |
1166 | * If we previously allocated a block out beyond eof and we are now |
1167 | * coming back to use it then we will need to flag it as new even if it | |
1168 | * has a disk address. | |
1169 | * | |
1170 | * With sub-block writes into unwritten extents we also need to mark | |
1171 | * the buffer as new so that the unwritten parts of the buffer gets | |
1172 | * correctly zeroed. | |
1da177e4 LT |
1173 | */ |
1174 | if (create && | |
1175 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || | |
549054af | 1176 | (offset >= i_size_read(inode)) || |
207d0416 | 1177 | (new || ISUNWRITTEN(&imap)))) |
1da177e4 | 1178 | set_buffer_new(bh_result); |
1da177e4 | 1179 | |
207d0416 | 1180 | if (imap.br_startblock == DELAYSTARTBLOCK) { |
1da177e4 LT |
1181 | BUG_ON(direct); |
1182 | if (create) { | |
1183 | set_buffer_uptodate(bh_result); | |
1184 | set_buffer_mapped(bh_result); | |
1185 | set_buffer_delay(bh_result); | |
1186 | } | |
1187 | } | |
1188 | ||
2b8f12b7 CH |
1189 | /* |
1190 | * If this is O_DIRECT or the mpage code calling tell them how large | |
1191 | * the mapping is, so that we can avoid repeated get_blocks calls. | |
1192 | */ | |
c2536668 | 1193 | if (direct || size > (1 << inode->i_blkbits)) { |
2b8f12b7 CH |
1194 | xfs_off_t mapping_size; |
1195 | ||
1196 | mapping_size = imap.br_startoff + imap.br_blockcount - iblock; | |
1197 | mapping_size <<= inode->i_blkbits; | |
1198 | ||
1199 | ASSERT(mapping_size > 0); | |
1200 | if (mapping_size > size) | |
1201 | mapping_size = size; | |
1202 | if (mapping_size > LONG_MAX) | |
1203 | mapping_size = LONG_MAX; | |
1204 | ||
1205 | bh_result->b_size = mapping_size; | |
1da177e4 LT |
1206 | } |
1207 | ||
1208 | return 0; | |
a206c817 CH |
1209 | |
1210 | out_unlock: | |
1211 | xfs_iunlock(ip, lockmode); | |
1212 | return -error; | |
1da177e4 LT |
1213 | } |
1214 | ||
1215 | int | |
c2536668 | 1216 | xfs_get_blocks( |
1da177e4 LT |
1217 | struct inode *inode, |
1218 | sector_t iblock, | |
1219 | struct buffer_head *bh_result, | |
1220 | int create) | |
1221 | { | |
f2bde9b8 | 1222 | return __xfs_get_blocks(inode, iblock, bh_result, create, 0); |
1da177e4 LT |
1223 | } |
1224 | ||
1225 | STATIC int | |
e4c573bb | 1226 | xfs_get_blocks_direct( |
1da177e4 LT |
1227 | struct inode *inode, |
1228 | sector_t iblock, | |
1da177e4 LT |
1229 | struct buffer_head *bh_result, |
1230 | int create) | |
1231 | { | |
f2bde9b8 | 1232 | return __xfs_get_blocks(inode, iblock, bh_result, create, 1); |
1da177e4 LT |
1233 | } |
1234 | ||
209fb87a CH |
1235 | /* |
1236 | * Complete a direct I/O write request. | |
1237 | * | |
1238 | * If the private argument is non-NULL __xfs_get_blocks signals us that we | |
1239 | * need to issue a transaction to convert the range from unwritten to written | |
1240 | * extents. In case this is regular synchronous I/O we just call xfs_end_io | |
25985edc | 1241 | * to do this and we are done. But in case this was a successful AIO |
209fb87a CH |
1242 | * request this handler is called from interrupt context, from which we |
1243 | * can't start transactions. In that case offload the I/O completion to | |
1244 | * the workqueues we also use for buffered I/O completion. | |
1245 | */ | |
f0973863 | 1246 | STATIC void |
209fb87a CH |
1247 | xfs_end_io_direct_write( |
1248 | struct kiocb *iocb, | |
1249 | loff_t offset, | |
1250 | ssize_t size, | |
1251 | void *private, | |
1252 | int ret, | |
1253 | bool is_async) | |
f0973863 | 1254 | { |
209fb87a | 1255 | struct xfs_ioend *ioend = iocb->private; |
f0973863 | 1256 | |
2813d682 CH |
1257 | /* |
1258 | * While the generic direct I/O code updates the inode size, it does | |
1259 | * so only after the end_io handler is called, which means our | |
1260 | * end_io handler thinks the on-disk size is outside the in-core | |
1261 | * size. To prevent this just update it a little bit earlier here. | |
1262 | */ | |
1263 | if (offset + size > i_size_read(ioend->io_inode)) | |
1264 | i_size_write(ioend->io_inode, offset + size); | |
1265 | ||
f0973863 | 1266 | /* |
209fb87a CH |
1267 | * blockdev_direct_IO can return an error even after the I/O |
1268 | * completion handler was called. Thus we need to protect | |
1269 | * against double-freeing. | |
f0973863 | 1270 | */ |
209fb87a CH |
1271 | iocb->private = NULL; |
1272 | ||
ba87ea69 LM |
1273 | ioend->io_offset = offset; |
1274 | ioend->io_size = size; | |
c859cdd1 CH |
1275 | ioend->io_iocb = iocb; |
1276 | ioend->io_result = ret; | |
209fb87a CH |
1277 | if (private && size > 0) |
1278 | ioend->io_type = IO_UNWRITTEN; | |
1279 | ||
1280 | if (is_async) { | |
c859cdd1 | 1281 | ioend->io_isasync = 1; |
209fb87a | 1282 | xfs_finish_ioend(ioend); |
f0973863 | 1283 | } else { |
209fb87a | 1284 | xfs_finish_ioend_sync(ioend); |
f0973863 | 1285 | } |
f0973863 CH |
1286 | } |
1287 | ||
1da177e4 | 1288 | STATIC ssize_t |
e4c573bb | 1289 | xfs_vm_direct_IO( |
1da177e4 LT |
1290 | int rw, |
1291 | struct kiocb *iocb, | |
1292 | const struct iovec *iov, | |
1293 | loff_t offset, | |
1294 | unsigned long nr_segs) | |
1295 | { | |
209fb87a CH |
1296 | struct inode *inode = iocb->ki_filp->f_mapping->host; |
1297 | struct block_device *bdev = xfs_find_bdev_for_inode(inode); | |
1298 | ssize_t ret; | |
1299 | ||
1300 | if (rw & WRITE) { | |
a206c817 | 1301 | iocb->private = xfs_alloc_ioend(inode, IO_DIRECT); |
209fb87a | 1302 | |
eafdc7d1 CH |
1303 | ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov, |
1304 | offset, nr_segs, | |
1305 | xfs_get_blocks_direct, | |
1306 | xfs_end_io_direct_write, NULL, 0); | |
209fb87a CH |
1307 | if (ret != -EIOCBQUEUED && iocb->private) |
1308 | xfs_destroy_ioend(iocb->private); | |
1309 | } else { | |
eafdc7d1 CH |
1310 | ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov, |
1311 | offset, nr_segs, | |
1312 | xfs_get_blocks_direct, | |
1313 | NULL, NULL, 0); | |
209fb87a | 1314 | } |
f0973863 | 1315 | |
f0973863 | 1316 | return ret; |
1da177e4 LT |
1317 | } |
1318 | ||
fa9b227e CH |
1319 | STATIC void |
1320 | xfs_vm_write_failed( | |
1321 | struct address_space *mapping, | |
1322 | loff_t to) | |
1323 | { | |
1324 | struct inode *inode = mapping->host; | |
1325 | ||
1326 | if (to > inode->i_size) { | |
c726de44 | 1327 | /* |
2813d682 CH |
1328 | * Punch out the delalloc blocks we have already allocated. |
1329 | * | |
1330 | * Don't bother with xfs_setattr given that nothing can have | |
1331 | * made it to disk yet as the page is still locked at this | |
1332 | * point. | |
c726de44 DC |
1333 | */ |
1334 | struct xfs_inode *ip = XFS_I(inode); | |
1335 | xfs_fileoff_t start_fsb; | |
1336 | xfs_fileoff_t end_fsb; | |
1337 | int error; | |
1338 | ||
1339 | truncate_pagecache(inode, to, inode->i_size); | |
1340 | ||
1341 | /* | |
1342 | * Check if there are any blocks that are outside of i_size | |
1343 | * that need to be trimmed back. | |
1344 | */ | |
1345 | start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1; | |
1346 | end_fsb = XFS_B_TO_FSB(ip->i_mount, to); | |
1347 | if (end_fsb <= start_fsb) | |
1348 | return; | |
1349 | ||
1350 | xfs_ilock(ip, XFS_ILOCK_EXCL); | |
1351 | error = xfs_bmap_punch_delalloc_range(ip, start_fsb, | |
1352 | end_fsb - start_fsb); | |
1353 | if (error) { | |
1354 | /* something screwed, just bail */ | |
1355 | if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) { | |
4f10700a | 1356 | xfs_alert(ip->i_mount, |
c726de44 DC |
1357 | "xfs_vm_write_failed: unable to clean up ino %lld", |
1358 | ip->i_ino); | |
1359 | } | |
1360 | } | |
1361 | xfs_iunlock(ip, XFS_ILOCK_EXCL); | |
fa9b227e CH |
1362 | } |
1363 | } | |
1364 | ||
f51623b2 | 1365 | STATIC int |
d79689c7 | 1366 | xfs_vm_write_begin( |
f51623b2 | 1367 | struct file *file, |
d79689c7 NP |
1368 | struct address_space *mapping, |
1369 | loff_t pos, | |
1370 | unsigned len, | |
1371 | unsigned flags, | |
1372 | struct page **pagep, | |
1373 | void **fsdata) | |
f51623b2 | 1374 | { |
155130a4 CH |
1375 | int ret; |
1376 | ||
1377 | ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS, | |
1378 | pagep, xfs_get_blocks); | |
fa9b227e CH |
1379 | if (unlikely(ret)) |
1380 | xfs_vm_write_failed(mapping, pos + len); | |
1381 | return ret; | |
1382 | } | |
1383 | ||
1384 | STATIC int | |
1385 | xfs_vm_write_end( | |
1386 | struct file *file, | |
1387 | struct address_space *mapping, | |
1388 | loff_t pos, | |
1389 | unsigned len, | |
1390 | unsigned copied, | |
1391 | struct page *page, | |
1392 | void *fsdata) | |
1393 | { | |
1394 | int ret; | |
155130a4 | 1395 | |
fa9b227e CH |
1396 | ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata); |
1397 | if (unlikely(ret < len)) | |
1398 | xfs_vm_write_failed(mapping, pos + len); | |
155130a4 | 1399 | return ret; |
f51623b2 | 1400 | } |
1da177e4 LT |
1401 | |
1402 | STATIC sector_t | |
e4c573bb | 1403 | xfs_vm_bmap( |
1da177e4 LT |
1404 | struct address_space *mapping, |
1405 | sector_t block) | |
1406 | { | |
1407 | struct inode *inode = (struct inode *)mapping->host; | |
739bfb2a | 1408 | struct xfs_inode *ip = XFS_I(inode); |
1da177e4 | 1409 | |
cca28fb8 | 1410 | trace_xfs_vm_bmap(XFS_I(inode)); |
126468b1 | 1411 | xfs_ilock(ip, XFS_IOLOCK_SHARED); |
739bfb2a | 1412 | xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF); |
126468b1 | 1413 | xfs_iunlock(ip, XFS_IOLOCK_SHARED); |
c2536668 | 1414 | return generic_block_bmap(mapping, block, xfs_get_blocks); |
1da177e4 LT |
1415 | } |
1416 | ||
1417 | STATIC int | |
e4c573bb | 1418 | xfs_vm_readpage( |
1da177e4 LT |
1419 | struct file *unused, |
1420 | struct page *page) | |
1421 | { | |
c2536668 | 1422 | return mpage_readpage(page, xfs_get_blocks); |
1da177e4 LT |
1423 | } |
1424 | ||
1425 | STATIC int | |
e4c573bb | 1426 | xfs_vm_readpages( |
1da177e4 LT |
1427 | struct file *unused, |
1428 | struct address_space *mapping, | |
1429 | struct list_head *pages, | |
1430 | unsigned nr_pages) | |
1431 | { | |
c2536668 | 1432 | return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks); |
1da177e4 LT |
1433 | } |
1434 | ||
f5e54d6e | 1435 | const struct address_space_operations xfs_address_space_operations = { |
e4c573bb NS |
1436 | .readpage = xfs_vm_readpage, |
1437 | .readpages = xfs_vm_readpages, | |
1438 | .writepage = xfs_vm_writepage, | |
7d4fb40a | 1439 | .writepages = xfs_vm_writepages, |
238f4c54 NS |
1440 | .releasepage = xfs_vm_releasepage, |
1441 | .invalidatepage = xfs_vm_invalidatepage, | |
d79689c7 | 1442 | .write_begin = xfs_vm_write_begin, |
fa9b227e | 1443 | .write_end = xfs_vm_write_end, |
e4c573bb NS |
1444 | .bmap = xfs_vm_bmap, |
1445 | .direct_IO = xfs_vm_direct_IO, | |
e965f963 | 1446 | .migratepage = buffer_migrate_page, |
bddaafa1 | 1447 | .is_partially_uptodate = block_is_partially_uptodate, |
aa261f54 | 1448 | .error_remove_page = generic_error_remove_page, |
1da177e4 | 1449 | }; |