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