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