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