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