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