]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_aops.c
xfs: xfs_cluster_write is redundant
[mirror_ubuntu-zesty-kernel.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"
70a9883c 19#include "xfs_shared.h"
239880ef
DC
20#include "xfs_format.h"
21#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
1da177e4 23#include "xfs_mount.h"
1da177e4 24#include "xfs_inode.h"
239880ef 25#include "xfs_trans.h"
281627df 26#include "xfs_inode_item.h"
a844f451 27#include "xfs_alloc.h"
1da177e4 28#include "xfs_error.h"
1da177e4 29#include "xfs_iomap.h"
0b1b213f 30#include "xfs_trace.h"
3ed3a434 31#include "xfs_bmap.h"
68988114 32#include "xfs_bmap_util.h"
a4fbe6ab 33#include "xfs_bmap_btree.h"
5a0e3ad6 34#include <linux/gfp.h>
1da177e4 35#include <linux/mpage.h>
10ce4444 36#include <linux/pagevec.h>
1da177e4
LT
37#include <linux/writeback.h>
38
fbcc0256
DC
39/*
40 * structure owned by writepages passed to individual writepage calls
41 */
42struct xfs_writepage_ctx {
43 struct xfs_bmbt_irec imap;
44 bool imap_valid;
45 unsigned int io_type;
46 struct xfs_ioend *iohead;
47 struct xfs_ioend *ioend;
48 sector_t last_block;
49};
50
0b1b213f 51void
f51623b2
NS
52xfs_count_page_state(
53 struct page *page,
54 int *delalloc,
f51623b2
NS
55 int *unwritten)
56{
57 struct buffer_head *bh, *head;
58
20cb52eb 59 *delalloc = *unwritten = 0;
f51623b2
NS
60
61 bh = head = page_buffers(page);
62 do {
20cb52eb 63 if (buffer_unwritten(bh))
f51623b2
NS
64 (*unwritten) = 1;
65 else if (buffer_delay(bh))
66 (*delalloc) = 1;
67 } while ((bh = bh->b_this_page) != head);
68}
69
6214ed44
CH
70STATIC struct block_device *
71xfs_find_bdev_for_inode(
046f1685 72 struct inode *inode)
6214ed44 73{
046f1685 74 struct xfs_inode *ip = XFS_I(inode);
6214ed44
CH
75 struct xfs_mount *mp = ip->i_mount;
76
71ddabb9 77 if (XFS_IS_REALTIME_INODE(ip))
6214ed44
CH
78 return mp->m_rtdev_targp->bt_bdev;
79 else
80 return mp->m_ddev_targp->bt_bdev;
81}
82
f6d6d4fc
CH
83/*
84 * We're now finished for good with this ioend structure.
85 * Update the page state via the associated buffer_heads,
86 * release holds on the inode and bio, and finally free
87 * up memory. Do not use the ioend after this.
88 */
0829c360
CH
89STATIC void
90xfs_destroy_ioend(
91 xfs_ioend_t *ioend)
92{
f6d6d4fc
CH
93 struct buffer_head *bh, *next;
94
95 for (bh = ioend->io_buffer_head; bh; bh = next) {
96 next = bh->b_private;
7d04a335 97 bh->b_end_io(bh, !ioend->io_error);
f6d6d4fc 98 }
583fa586 99
0829c360
CH
100 mempool_free(ioend, xfs_ioend_pool);
101}
102
fc0063c4
CH
103/*
104 * Fast and loose check if this write could update the on-disk inode size.
105 */
106static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
107{
108 return ioend->io_offset + ioend->io_size >
109 XFS_I(ioend->io_inode)->i_d.di_size;
110}
111
281627df
CH
112STATIC int
113xfs_setfilesize_trans_alloc(
114 struct xfs_ioend *ioend)
115{
116 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
117 struct xfs_trans *tp;
118 int error;
119
120 tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
121
3d3c8b52 122 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_fsyncts, 0, 0);
281627df 123 if (error) {
4906e215 124 xfs_trans_cancel(tp);
281627df
CH
125 return error;
126 }
127
128 ioend->io_append_trans = tp;
129
d9457dc0 130 /*
437a255a 131 * We may pass freeze protection with a transaction. So tell lockdep
d9457dc0
JK
132 * we released it.
133 */
bee9182d 134 __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
281627df
CH
135 /*
136 * We hand off the transaction to the completion thread now, so
137 * clear the flag here.
138 */
139 current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
140 return 0;
141}
142
ba87ea69 143/*
2813d682 144 * Update on-disk file size now that data has been written to disk.
ba87ea69 145 */
281627df 146STATIC int
ba87ea69 147xfs_setfilesize(
2ba66237
CH
148 struct xfs_inode *ip,
149 struct xfs_trans *tp,
150 xfs_off_t offset,
151 size_t size)
ba87ea69 152{
ba87ea69 153 xfs_fsize_t isize;
ba87ea69 154
aa6bf01d 155 xfs_ilock(ip, XFS_ILOCK_EXCL);
2ba66237 156 isize = xfs_new_eof(ip, offset + size);
281627df
CH
157 if (!isize) {
158 xfs_iunlock(ip, XFS_ILOCK_EXCL);
4906e215 159 xfs_trans_cancel(tp);
281627df 160 return 0;
ba87ea69
LM
161 }
162
2ba66237 163 trace_xfs_setfilesize(ip, offset, size);
281627df
CH
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
70393313 169 return xfs_trans_commit(tp);
77d7a0c2
DC
170}
171
2ba66237
CH
172STATIC int
173xfs_setfilesize_ioend(
174 struct xfs_ioend *ioend)
175{
176 struct xfs_inode *ip = XFS_I(ioend->io_inode);
177 struct xfs_trans *tp = ioend->io_append_trans;
178
179 /*
180 * The transaction may have been allocated in the I/O submission thread,
181 * thus we need to mark ourselves as being in a transaction manually.
182 * Similarly for freeze protection.
183 */
184 current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
bee9182d 185 __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
2ba66237 186
5cb13dcd
Z
187 /* we abort the update if there was an IO error */
188 if (ioend->io_error) {
189 xfs_trans_cancel(tp);
190 return ioend->io_error;
191 }
192
2ba66237
CH
193 return xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
194}
195
77d7a0c2 196/*
209fb87a 197 * Schedule IO completion handling on the final put of an ioend.
fc0063c4
CH
198 *
199 * If there is no work to do we might as well call it a day and free the
200 * ioend right now.
77d7a0c2
DC
201 */
202STATIC void
203xfs_finish_ioend(
209fb87a 204 struct xfs_ioend *ioend)
77d7a0c2
DC
205{
206 if (atomic_dec_and_test(&ioend->io_remaining)) {
aa6bf01d
CH
207 struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
208
0d882a36 209 if (ioend->io_type == XFS_IO_UNWRITTEN)
aa6bf01d 210 queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
2ba66237 211 else if (ioend->io_append_trans)
aa6bf01d 212 queue_work(mp->m_data_workqueue, &ioend->io_work);
fc0063c4
CH
213 else
214 xfs_destroy_ioend(ioend);
77d7a0c2 215 }
ba87ea69
LM
216}
217
0829c360 218/*
5ec4fabb 219 * IO write completion.
f6d6d4fc
CH
220 */
221STATIC void
5ec4fabb 222xfs_end_io(
77d7a0c2 223 struct work_struct *work)
0829c360 224{
77d7a0c2
DC
225 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
226 struct xfs_inode *ip = XFS_I(ioend->io_inode);
69418932 227 int error = 0;
ba87ea69 228
04f658ee 229 if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
810627d9 230 ioend->io_error = -EIO;
04f658ee
CH
231 goto done;
232 }
04f658ee 233
5ec4fabb
CH
234 /*
235 * For unwritten extents we need to issue transactions to convert a
236 * range to normal written extens after the data I/O has finished.
5cb13dcd
Z
237 * Detecting and handling completion IO errors is done individually
238 * for each case as different cleanup operations need to be performed
239 * on error.
5ec4fabb 240 */
0d882a36 241 if (ioend->io_type == XFS_IO_UNWRITTEN) {
5cb13dcd
Z
242 if (ioend->io_error)
243 goto done;
437a255a
DC
244 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
245 ioend->io_size);
281627df 246 } else if (ioend->io_append_trans) {
2ba66237 247 error = xfs_setfilesize_ioend(ioend);
84803fb7 248 } else {
281627df 249 ASSERT(!xfs_ioend_is_append(ioend));
5ec4fabb 250 }
ba87ea69 251
04f658ee 252done:
437a255a 253 if (error)
2451337d 254 ioend->io_error = error;
aa6bf01d 255 xfs_destroy_ioend(ioend);
c626d174
DC
256}
257
0829c360
CH
258/*
259 * Allocate and initialise an IO completion structure.
260 * We need to track unwritten extent write completion here initially.
261 * We'll need to extend this for updating the ondisk inode size later
262 * (vs. incore size).
263 */
264STATIC xfs_ioend_t *
265xfs_alloc_ioend(
f6d6d4fc
CH
266 struct inode *inode,
267 unsigned int type)
0829c360
CH
268{
269 xfs_ioend_t *ioend;
270
271 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
272
273 /*
274 * Set the count to 1 initially, which will prevent an I/O
275 * completion callback from happening before we have started
276 * all the I/O from calling the completion routine too early.
277 */
278 atomic_set(&ioend->io_remaining, 1);
7d04a335 279 ioend->io_error = 0;
f6d6d4fc
CH
280 ioend->io_list = NULL;
281 ioend->io_type = type;
b677c210 282 ioend->io_inode = inode;
c1a073bd 283 ioend->io_buffer_head = NULL;
f6d6d4fc 284 ioend->io_buffer_tail = NULL;
0829c360
CH
285 ioend->io_offset = 0;
286 ioend->io_size = 0;
281627df 287 ioend->io_append_trans = NULL;
0829c360 288
5ec4fabb 289 INIT_WORK(&ioend->io_work, xfs_end_io);
0829c360
CH
290 return ioend;
291}
292
1da177e4
LT
293STATIC int
294xfs_map_blocks(
295 struct inode *inode,
296 loff_t offset,
207d0416 297 struct xfs_bmbt_irec *imap,
988ef927 298 int type)
1da177e4 299{
a206c817
CH
300 struct xfs_inode *ip = XFS_I(inode);
301 struct xfs_mount *mp = ip->i_mount;
ed1e7b7e 302 ssize_t count = 1 << inode->i_blkbits;
a206c817
CH
303 xfs_fileoff_t offset_fsb, end_fsb;
304 int error = 0;
a206c817
CH
305 int bmapi_flags = XFS_BMAPI_ENTIRE;
306 int nimaps = 1;
307
308 if (XFS_FORCED_SHUTDOWN(mp))
b474c7ae 309 return -EIO;
a206c817 310
0d882a36 311 if (type == XFS_IO_UNWRITTEN)
a206c817 312 bmapi_flags |= XFS_BMAPI_IGSTATE;
8ff2957d 313
988ef927 314 xfs_ilock(ip, XFS_ILOCK_SHARED);
8ff2957d
CH
315 ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
316 (ip->i_df.if_flags & XFS_IFEXTENTS));
d2c28191 317 ASSERT(offset <= mp->m_super->s_maxbytes);
8ff2957d 318
d2c28191
DC
319 if (offset + count > mp->m_super->s_maxbytes)
320 count = mp->m_super->s_maxbytes - offset;
a206c817
CH
321 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
322 offset_fsb = XFS_B_TO_FSBT(mp, offset);
5c8ed202
DC
323 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
324 imap, &nimaps, bmapi_flags);
8ff2957d 325 xfs_iunlock(ip, XFS_ILOCK_SHARED);
a206c817 326
8ff2957d 327 if (error)
2451337d 328 return error;
a206c817 329
0d882a36 330 if (type == XFS_IO_DELALLOC &&
8ff2957d 331 (!nimaps || isnullstartblock(imap->br_startblock))) {
0799a3e8 332 error = xfs_iomap_write_allocate(ip, offset, imap);
a206c817
CH
333 if (!error)
334 trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
2451337d 335 return error;
a206c817
CH
336 }
337
8ff2957d 338#ifdef DEBUG
0d882a36 339 if (type == XFS_IO_UNWRITTEN) {
8ff2957d
CH
340 ASSERT(nimaps);
341 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
342 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
343 }
344#endif
345 if (nimaps)
346 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
347 return 0;
1da177e4
LT
348}
349
fbcc0256 350STATIC bool
558e6891 351xfs_imap_valid(
8699bb0a 352 struct inode *inode,
207d0416 353 struct xfs_bmbt_irec *imap,
558e6891 354 xfs_off_t offset)
1da177e4 355{
558e6891 356 offset >>= inode->i_blkbits;
8699bb0a 357
558e6891
CH
358 return offset >= imap->br_startoff &&
359 offset < imap->br_startoff + imap->br_blockcount;
1da177e4
LT
360}
361
f6d6d4fc
CH
362/*
363 * BIO completion handler for buffered IO.
364 */
782e3b3b 365STATIC void
f6d6d4fc 366xfs_end_bio(
4246a0b6 367 struct bio *bio)
f6d6d4fc
CH
368{
369 xfs_ioend_t *ioend = bio->bi_private;
370
77a78806
LT
371 if (!ioend->io_error)
372 ioend->io_error = bio->bi_error;
f6d6d4fc
CH
373
374 /* Toss bio and pass work off to an xfsdatad thread */
f6d6d4fc
CH
375 bio->bi_private = NULL;
376 bio->bi_end_io = NULL;
f6d6d4fc 377 bio_put(bio);
7d04a335 378
209fb87a 379 xfs_finish_ioend(ioend);
f6d6d4fc
CH
380}
381
382STATIC void
383xfs_submit_ioend_bio(
06342cf8
CH
384 struct writeback_control *wbc,
385 xfs_ioend_t *ioend,
386 struct bio *bio)
f6d6d4fc
CH
387{
388 atomic_inc(&ioend->io_remaining);
f6d6d4fc
CH
389 bio->bi_private = ioend;
390 bio->bi_end_io = xfs_end_bio;
721a9602 391 submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
f6d6d4fc
CH
392}
393
394STATIC struct bio *
395xfs_alloc_ioend_bio(
396 struct buffer_head *bh)
397{
b54ffb73 398 struct bio *bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
f6d6d4fc
CH
399
400 ASSERT(bio->bi_private == NULL);
4f024f37 401 bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
f6d6d4fc 402 bio->bi_bdev = bh->b_bdev;
f6d6d4fc
CH
403 return bio;
404}
405
406STATIC void
407xfs_start_buffer_writeback(
408 struct buffer_head *bh)
409{
410 ASSERT(buffer_mapped(bh));
411 ASSERT(buffer_locked(bh));
412 ASSERT(!buffer_delay(bh));
413 ASSERT(!buffer_unwritten(bh));
414
415 mark_buffer_async_write(bh);
416 set_buffer_uptodate(bh);
417 clear_buffer_dirty(bh);
418}
419
420STATIC void
421xfs_start_page_writeback(
422 struct page *page,
f6d6d4fc
CH
423 int clear_dirty,
424 int buffers)
425{
426 ASSERT(PageLocked(page));
427 ASSERT(!PageWriteback(page));
0d085a52
DC
428
429 /*
430 * if the page was not fully cleaned, we need to ensure that the higher
431 * layers come back to it correctly. That means we need to keep the page
432 * dirty, and for WB_SYNC_ALL writeback we need to ensure the
433 * PAGECACHE_TAG_TOWRITE index mark is not removed so another attempt to
434 * write this page in this writeback sweep will be made.
435 */
436 if (clear_dirty) {
92132021 437 clear_page_dirty_for_io(page);
0d085a52
DC
438 set_page_writeback(page);
439 } else
440 set_page_writeback_keepwrite(page);
441
f6d6d4fc 442 unlock_page(page);
0d085a52 443
1f7decf6
FW
444 /* If no buffers on the page are to be written, finish it here */
445 if (!buffers)
f6d6d4fc 446 end_page_writeback(page);
f6d6d4fc
CH
447}
448
c7c1a7d8 449static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh)
f6d6d4fc
CH
450{
451 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
452}
453
454/*
d88992f6
DC
455 * Submit all of the bios for all of the ioends we have saved up, covering the
456 * initial writepage page and also any probed pages.
457 *
458 * Because we may have multiple ioends spanning a page, we need to start
459 * writeback on all the buffers before we submit them for I/O. If we mark the
460 * buffers as we got, then we can end up with a page that only has buffers
461 * marked async write and I/O complete on can occur before we mark the other
462 * buffers async write.
463 *
464 * The end result of this is that we trip a bug in end_page_writeback() because
465 * we call it twice for the one page as the code in end_buffer_async_write()
466 * assumes that all buffers on the page are started at the same time.
467 *
468 * The fix is two passes across the ioend list - one to start writeback on the
c41564b5 469 * buffer_heads, and then submit them for I/O on the second pass.
7bf7f352
DC
470 *
471 * If @fail is non-zero, it means that we have a situation where some part of
472 * the submission process has failed after we have marked paged for writeback
473 * and unlocked them. In this situation, we need to fail the ioend chain rather
474 * than submit it to IO. This typically only happens on a filesystem shutdown.
f6d6d4fc
CH
475 */
476STATIC void
477xfs_submit_ioend(
06342cf8 478 struct writeback_control *wbc,
7bf7f352
DC
479 xfs_ioend_t *ioend,
480 int fail)
f6d6d4fc 481{
d88992f6 482 xfs_ioend_t *head = ioend;
f6d6d4fc
CH
483 xfs_ioend_t *next;
484 struct buffer_head *bh;
485 struct bio *bio;
486 sector_t lastblock = 0;
487
d88992f6
DC
488 /* Pass 1 - start writeback */
489 do {
490 next = ioend->io_list;
221cb251 491 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
d88992f6 492 xfs_start_buffer_writeback(bh);
d88992f6
DC
493 } while ((ioend = next) != NULL);
494
495 /* Pass 2 - submit I/O */
496 ioend = head;
f6d6d4fc
CH
497 do {
498 next = ioend->io_list;
499 bio = NULL;
500
7bf7f352
DC
501 /*
502 * If we are failing the IO now, just mark the ioend with an
503 * error and finish it. This will run IO completion immediately
504 * as there is only one reference to the ioend at this point in
505 * time.
506 */
507 if (fail) {
2451337d 508 ioend->io_error = fail;
7bf7f352
DC
509 xfs_finish_ioend(ioend);
510 continue;
511 }
512
f6d6d4fc 513 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
f6d6d4fc
CH
514
515 if (!bio) {
516 retry:
517 bio = xfs_alloc_ioend_bio(bh);
518 } else if (bh->b_blocknr != lastblock + 1) {
06342cf8 519 xfs_submit_ioend_bio(wbc, ioend, bio);
f6d6d4fc
CH
520 goto retry;
521 }
522
c7c1a7d8 523 if (xfs_bio_add_buffer(bio, bh) != bh->b_size) {
06342cf8 524 xfs_submit_ioend_bio(wbc, ioend, bio);
f6d6d4fc
CH
525 goto retry;
526 }
527
528 lastblock = bh->b_blocknr;
529 }
530 if (bio)
06342cf8 531 xfs_submit_ioend_bio(wbc, ioend, bio);
209fb87a 532 xfs_finish_ioend(ioend);
f6d6d4fc
CH
533 } while ((ioend = next) != NULL);
534}
535
f6d6d4fc
CH
536/*
537 * Test to see if we've been building up a completion structure for
538 * earlier buffers -- if so, we try to append to this ioend if we
539 * can, otherwise we finish off any current ioend and start another.
540 * Return true if we've finished the given ioend.
541 */
542STATIC void
543xfs_add_to_ioend(
544 struct inode *inode,
545 struct buffer_head *bh,
7336cea8 546 xfs_off_t offset,
fbcc0256 547 struct xfs_writepage_ctx *wpc)
f6d6d4fc 548{
fbcc0256
DC
549 if (!wpc->ioend || wpc->io_type != wpc->ioend->io_type ||
550 bh->b_blocknr != wpc->last_block + 1) {
551 struct xfs_ioend *new;
552
553 new = xfs_alloc_ioend(inode, wpc->io_type);
554 new->io_offset = offset;
555 new->io_buffer_head = bh;
556 new->io_buffer_tail = bh;
557 if (wpc->ioend)
558 wpc->ioend->io_list = new;
559 wpc->ioend = new;
f6d6d4fc 560 } else {
fbcc0256
DC
561 wpc->ioend->io_buffer_tail->b_private = bh;
562 wpc->ioend->io_buffer_tail = bh;
f6d6d4fc
CH
563 }
564
565 bh->b_private = NULL;
fbcc0256
DC
566 wpc->ioend->io_size += bh->b_size;
567 wpc->last_block = bh->b_blocknr;
f6d6d4fc
CH
568}
569
87cbc49c
NS
570STATIC void
571xfs_map_buffer(
046f1685 572 struct inode *inode,
87cbc49c 573 struct buffer_head *bh,
207d0416 574 struct xfs_bmbt_irec *imap,
046f1685 575 xfs_off_t offset)
87cbc49c
NS
576{
577 sector_t bn;
8699bb0a 578 struct xfs_mount *m = XFS_I(inode)->i_mount;
207d0416
CH
579 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
580 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
87cbc49c 581
207d0416
CH
582 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
583 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
87cbc49c 584
e513182d 585 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
8699bb0a 586 ((offset - iomap_offset) >> inode->i_blkbits);
87cbc49c 587
046f1685 588 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
87cbc49c
NS
589
590 bh->b_blocknr = bn;
591 set_buffer_mapped(bh);
592}
593
1da177e4
LT
594STATIC void
595xfs_map_at_offset(
046f1685 596 struct inode *inode,
1da177e4 597 struct buffer_head *bh,
207d0416 598 struct xfs_bmbt_irec *imap,
046f1685 599 xfs_off_t offset)
1da177e4 600{
207d0416
CH
601 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
602 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
1da177e4 603
207d0416 604 xfs_map_buffer(inode, bh, imap, offset);
1da177e4
LT
605 set_buffer_mapped(bh);
606 clear_buffer_delay(bh);
f6d6d4fc 607 clear_buffer_unwritten(bh);
1da177e4
LT
608}
609
1da177e4 610/*
a49935f2
DC
611 * Test if a given page contains at least one buffer of a given @type.
612 * If @check_all_buffers is true, then we walk all the buffers in the page to
613 * try to find one of the type passed in. If it is not set, then the caller only
614 * needs to check the first buffer on the page for a match.
1da177e4 615 */
a49935f2 616STATIC bool
6ffc4db5 617xfs_check_page_type(
10ce4444 618 struct page *page,
a49935f2
DC
619 unsigned int type,
620 bool check_all_buffers)
1da177e4 621{
a49935f2
DC
622 struct buffer_head *bh;
623 struct buffer_head *head;
1da177e4 624
a49935f2
DC
625 if (PageWriteback(page))
626 return false;
627 if (!page->mapping)
628 return false;
629 if (!page_has_buffers(page))
630 return false;
1da177e4 631
a49935f2
DC
632 bh = head = page_buffers(page);
633 do {
634 if (buffer_unwritten(bh)) {
635 if (type == XFS_IO_UNWRITTEN)
636 return true;
637 } else if (buffer_delay(bh)) {
805eeb8e 638 if (type == XFS_IO_DELALLOC)
a49935f2
DC
639 return true;
640 } else if (buffer_dirty(bh) && buffer_mapped(bh)) {
805eeb8e 641 if (type == XFS_IO_OVERWRITE)
a49935f2
DC
642 return true;
643 }
1da177e4 644
a49935f2
DC
645 /* If we are only checking the first buffer, we are done now. */
646 if (!check_all_buffers)
647 break;
648 } while ((bh = bh->b_this_page) != head);
1da177e4 649
a49935f2 650 return false;
1da177e4
LT
651}
652
3ed3a434
DC
653STATIC void
654xfs_vm_invalidatepage(
655 struct page *page,
d47992f8
LC
656 unsigned int offset,
657 unsigned int length)
3ed3a434 658{
34097dfe
LC
659 trace_xfs_invalidatepage(page->mapping->host, page, offset,
660 length);
661 block_invalidatepage(page, offset, length);
3ed3a434
DC
662}
663
664/*
665 * If the page has delalloc buffers on it, we need to punch them out before we
666 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
667 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
668 * is done on that same region - the delalloc extent is returned when none is
669 * supposed to be there.
670 *
671 * We prevent this by truncating away the delalloc regions on the page before
672 * invalidating it. Because they are delalloc, we can do this without needing a
673 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
674 * truncation without a transaction as there is no space left for block
675 * reservation (typically why we see a ENOSPC in writeback).
676 *
677 * This is not a performance critical path, so for now just do the punching a
678 * buffer head at a time.
679 */
680STATIC void
681xfs_aops_discard_page(
682 struct page *page)
683{
684 struct inode *inode = page->mapping->host;
685 struct xfs_inode *ip = XFS_I(inode);
686 struct buffer_head *bh, *head;
687 loff_t offset = page_offset(page);
3ed3a434 688
a49935f2 689 if (!xfs_check_page_type(page, XFS_IO_DELALLOC, true))
3ed3a434
DC
690 goto out_invalidate;
691
e8c3753c
DC
692 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
693 goto out_invalidate;
694
4f10700a 695 xfs_alert(ip->i_mount,
3ed3a434
DC
696 "page discard on page %p, inode 0x%llx, offset %llu.",
697 page, ip->i_ino, offset);
698
699 xfs_ilock(ip, XFS_ILOCK_EXCL);
700 bh = head = page_buffers(page);
701 do {
3ed3a434 702 int error;
c726de44 703 xfs_fileoff_t start_fsb;
3ed3a434
DC
704
705 if (!buffer_delay(bh))
706 goto next_buffer;
707
c726de44
DC
708 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
709 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
3ed3a434
DC
710 if (error) {
711 /* something screwed, just bail */
e8c3753c 712 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
4f10700a 713 xfs_alert(ip->i_mount,
3ed3a434 714 "page discard unable to remove delalloc mapping.");
e8c3753c 715 }
3ed3a434
DC
716 break;
717 }
718next_buffer:
c726de44 719 offset += 1 << inode->i_blkbits;
3ed3a434
DC
720
721 } while ((bh = bh->b_this_page) != head);
722
723 xfs_iunlock(ip, XFS_ILOCK_EXCL);
724out_invalidate:
d47992f8 725 xfs_vm_invalidatepage(page, 0, PAGE_CACHE_SIZE);
3ed3a434
DC
726 return;
727}
728
150d5be0
DC
729static int
730xfs_writepage_submit(
fbcc0256 731 struct xfs_writepage_ctx *wpc,
150d5be0
DC
732 struct writeback_control *wbc,
733 int status)
734{
735 struct blk_plug plug;
736
737 /* Reserve log space if we might write beyond the on-disk inode size. */
fbcc0256
DC
738 if (!status && wpc->ioend && wpc->ioend->io_type != XFS_IO_UNWRITTEN &&
739 xfs_ioend_is_append(wpc->ioend))
740 status = xfs_setfilesize_trans_alloc(wpc->ioend);
150d5be0 741
fbcc0256 742 if (wpc->iohead) {
150d5be0 743 blk_start_plug(&plug);
fbcc0256 744 xfs_submit_ioend(wbc, wpc->iohead, status);
150d5be0
DC
745 blk_finish_plug(&plug);
746 }
747 return status;
748}
749
1da177e4 750/*
89f3b363
CH
751 * Write out a dirty page.
752 *
753 * For delalloc space on the page we need to allocate space and flush it.
754 * For unwritten space on the page we need to start the conversion to
755 * regular allocated space.
89f3b363 756 * For any other dirty buffer heads on the page we should flush them.
1da177e4 757 */
1da177e4 758STATIC int
fbcc0256 759xfs_do_writepage(
89f3b363 760 struct page *page,
fbcc0256
DC
761 struct writeback_control *wbc,
762 void *data)
1da177e4 763{
fbcc0256 764 struct xfs_writepage_ctx *wpc = data;
89f3b363 765 struct inode *inode = page->mapping->host;
f6d6d4fc 766 struct buffer_head *bh, *head;
1da177e4 767 loff_t offset;
1da177e4 768 __uint64_t end_offset;
ad68972a 769 pgoff_t end_index;
ed1e7b7e 770 ssize_t len;
fbcc0256 771 int err, uptodate = 1;
89f3b363 772 int count = 0;
89f3b363 773
34097dfe 774 trace_xfs_writepage(inode, page, 0, 0);
89f3b363 775
20cb52eb
CH
776 ASSERT(page_has_buffers(page));
777
89f3b363
CH
778 /*
779 * Refuse to write the page out if we are called from reclaim context.
780 *
d4f7a5cb
CH
781 * This avoids stack overflows when called from deeply used stacks in
782 * random callers for direct reclaim or memcg reclaim. We explicitly
783 * allow reclaim from kswapd as the stack usage there is relatively low.
89f3b363 784 *
94054fa3
MG
785 * This should never happen except in the case of a VM regression so
786 * warn about it.
89f3b363 787 */
94054fa3
MG
788 if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
789 PF_MEMALLOC))
b5420f23 790 goto redirty;
1da177e4 791
89f3b363 792 /*
680a647b
CH
793 * Given that we do not allow direct reclaim to call us, we should
794 * never be called while in a filesystem transaction.
89f3b363 795 */
448011e2 796 if (WARN_ON_ONCE(current->flags & PF_FSTRANS))
b5420f23 797 goto redirty;
89f3b363 798
8695d27e 799 /*
ad68972a
DC
800 * Is this page beyond the end of the file?
801 *
8695d27e
JL
802 * The page index is less than the end_index, adjust the end_offset
803 * to the highest offset that this page should represent.
804 * -----------------------------------------------------
805 * | file mapping | <EOF> |
806 * -----------------------------------------------------
807 * | Page ... | Page N-2 | Page N-1 | Page N | |
808 * ^--------------------------------^----------|--------
809 * | desired writeback range | see else |
810 * ---------------------------------^------------------|
811 */
ad68972a
DC
812 offset = i_size_read(inode);
813 end_index = offset >> PAGE_CACHE_SHIFT;
8695d27e
JL
814 if (page->index < end_index)
815 end_offset = (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT;
816 else {
817 /*
818 * Check whether the page to write out is beyond or straddles
819 * i_size or not.
820 * -------------------------------------------------------
821 * | file mapping | <EOF> |
822 * -------------------------------------------------------
823 * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
824 * ^--------------------------------^-----------|---------
825 * | | Straddles |
826 * ---------------------------------^-----------|--------|
827 */
6b7a03f0
CH
828 unsigned offset_into_page = offset & (PAGE_CACHE_SIZE - 1);
829
830 /*
ff9a28f6
JK
831 * Skip the page if it is fully outside i_size, e.g. due to a
832 * truncate operation that is in progress. We must redirty the
833 * page so that reclaim stops reclaiming it. Otherwise
834 * xfs_vm_releasepage() is called on it and gets confused.
8695d27e
JL
835 *
836 * Note that the end_index is unsigned long, it would overflow
837 * if the given offset is greater than 16TB on 32-bit system
838 * and if we do check the page is fully outside i_size or not
839 * via "if (page->index >= end_index + 1)" as "end_index + 1"
840 * will be evaluated to 0. Hence this page will be redirtied
841 * and be written out repeatedly which would result in an
842 * infinite loop, the user program that perform this operation
843 * will hang. Instead, we can verify this situation by checking
844 * if the page to write is totally beyond the i_size or if it's
845 * offset is just equal to the EOF.
6b7a03f0 846 */
8695d27e
JL
847 if (page->index > end_index ||
848 (page->index == end_index && offset_into_page == 0))
ff9a28f6 849 goto redirty;
6b7a03f0
CH
850
851 /*
852 * The page straddles i_size. It must be zeroed out on each
853 * and every writepage invocation because it may be mmapped.
854 * "A file is mapped in multiples of the page size. For a file
8695d27e 855 * that is not a multiple of the page size, the remaining
6b7a03f0
CH
856 * memory is zeroed when mapped, and writes to that region are
857 * not written out to the file."
858 */
859 zero_user_segment(page, offset_into_page, PAGE_CACHE_SIZE);
8695d27e
JL
860
861 /* Adjust the end_offset to the end of file */
862 end_offset = offset;
1da177e4
LT
863 }
864
24e17b5f 865 len = 1 << inode->i_blkbits;
24e17b5f 866
24e17b5f 867 bh = head = page_buffers(page);
f6d6d4fc 868 offset = page_offset(page);
a206c817 869
1da177e4
LT
870 do {
871 if (offset >= end_offset)
872 break;
873 if (!buffer_uptodate(bh))
874 uptodate = 0;
1da177e4 875
3d9b02e3 876 /*
ece413f5
CH
877 * set_page_dirty dirties all buffers in a page, independent
878 * of their state. The dirty state however is entirely
879 * meaningless for holes (!mapped && uptodate), so skip
880 * buffers covering holes here.
3d9b02e3
ES
881 */
882 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
fbcc0256 883 wpc->imap_valid = false;
3d9b02e3
ES
884 continue;
885 }
886
aeea1b1f 887 if (buffer_unwritten(bh)) {
fbcc0256
DC
888 if (wpc->io_type != XFS_IO_UNWRITTEN) {
889 wpc->io_type = XFS_IO_UNWRITTEN;
890 wpc->imap_valid = false;
1da177e4 891 }
aeea1b1f 892 } else if (buffer_delay(bh)) {
fbcc0256
DC
893 if (wpc->io_type != XFS_IO_DELALLOC) {
894 wpc->io_type = XFS_IO_DELALLOC;
895 wpc->imap_valid = false;
1da177e4 896 }
89f3b363 897 } else if (buffer_uptodate(bh)) {
fbcc0256
DC
898 if (wpc->io_type != XFS_IO_OVERWRITE) {
899 wpc->io_type = XFS_IO_OVERWRITE;
900 wpc->imap_valid = false;
85da94c6 901 }
aeea1b1f 902 } else {
7d0fa3ec 903 if (PageUptodate(page))
aeea1b1f 904 ASSERT(buffer_mapped(bh));
7d0fa3ec
AR
905 /*
906 * This buffer is not uptodate and will not be
907 * written to disk. Ensure that we will put any
908 * subsequent writeable buffers into a new
909 * ioend.
910 */
fbcc0256 911 wpc->imap_valid = 0;
aeea1b1f
CH
912 continue;
913 }
d5cb48aa 914
fbcc0256
DC
915 if (wpc->imap_valid)
916 wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset);
917 if (!wpc->imap_valid) {
918 err = xfs_map_blocks(inode, offset, &wpc->imap,
919 wpc->io_type);
aeea1b1f
CH
920 if (err)
921 goto error;
fbcc0256 922 wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset);
aeea1b1f 923 }
fbcc0256 924 if (wpc->imap_valid) {
ecff71e6 925 lock_buffer(bh);
fbcc0256
DC
926 if (wpc->io_type != XFS_IO_OVERWRITE)
927 xfs_map_at_offset(inode, bh, &wpc->imap, offset);
928 xfs_add_to_ioend(inode, bh, offset, wpc);
aeea1b1f 929 count++;
1da177e4 930 }
f6d6d4fc 931
fbcc0256
DC
932 if (!wpc->iohead)
933 wpc->iohead = wpc->ioend;
f6d6d4fc
CH
934
935 } while (offset += len, ((bh = bh->b_this_page) != head));
1da177e4
LT
936
937 if (uptodate && bh == head)
938 SetPageUptodate(page);
939
89f3b363 940 xfs_start_page_writeback(page, 1, count);
1da177e4 941
ad68972a 942 ASSERT(wpc->iohead || !count);
fbcc0256 943 return 0;
281627df 944
150d5be0 945error:
7bf7f352 946 /*
150d5be0
DC
947 * On error, we have to fail the iohead here because we buffers locked
948 * in the ioend chain. If we don't do this, we'll deadlock invalidating
949 * the page as that tries to lock the buffers on the page. Also, because
fbcc0256
DC
950 * we may have set pages under writeback, we have to make sure we run
951 * IO completion to mark the error state of the IO appropriately, so we
952 * can't cancel the ioend directly here. That means we have to mark this
953 * page as under writeback if we included any buffers from it in the
954 * ioend chain so that completion treats it correctly.
955 *
956 * If we didn't include the page in the ioend, then we can simply
957 * discard and unlock it as there are no other users of the page or it's
958 * buffers right now. The caller will still need to trigger submission
959 * of outstanding ioends on the writepage context so they are treated
960 * correctly on error.
7bf7f352 961 */
150d5be0
DC
962 if (count)
963 xfs_start_page_writeback(page, 0, count);
fbcc0256 964 else {
150d5be0
DC
965 xfs_aops_discard_page(page);
966 ClearPageUptodate(page);
967 unlock_page(page);
968 }
969 mapping_set_error(page->mapping, err);
1da177e4 970 return err;
f51623b2 971
b5420f23 972redirty:
f51623b2
NS
973 redirty_page_for_writepage(wbc, page);
974 unlock_page(page);
975 return 0;
f51623b2
NS
976}
977
fbcc0256
DC
978STATIC int
979xfs_vm_writepage(
980 struct page *page,
981 struct writeback_control *wbc)
982{
983 struct xfs_writepage_ctx wpc = {
984 .io_type = XFS_IO_INVALID,
985 };
986 int ret;
987
988 ret = xfs_do_writepage(page, wbc, &wpc);
989 return xfs_writepage_submit(&wpc, wbc, ret);
990}
991
7d4fb40a
NS
992STATIC int
993xfs_vm_writepages(
994 struct address_space *mapping,
995 struct writeback_control *wbc)
996{
fbcc0256
DC
997 struct xfs_writepage_ctx wpc = {
998 .io_type = XFS_IO_INVALID,
999 };
1000 int ret;
1001
b3aea4ed 1002 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
fbcc0256
DC
1003 ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc);
1004 return xfs_writepage_submit(&wpc, wbc, ret);
7d4fb40a
NS
1005}
1006
f51623b2
NS
1007/*
1008 * Called to move a page into cleanable state - and from there
89f3b363 1009 * to be released. The page should already be clean. We always
f51623b2
NS
1010 * have buffer heads in this call.
1011 *
89f3b363 1012 * Returns 1 if the page is ok to release, 0 otherwise.
f51623b2
NS
1013 */
1014STATIC int
238f4c54 1015xfs_vm_releasepage(
f51623b2
NS
1016 struct page *page,
1017 gfp_t gfp_mask)
1018{
20cb52eb 1019 int delalloc, unwritten;
f51623b2 1020
34097dfe 1021 trace_xfs_releasepage(page->mapping->host, page, 0, 0);
238f4c54 1022
20cb52eb 1023 xfs_count_page_state(page, &delalloc, &unwritten);
f51623b2 1024
448011e2 1025 if (WARN_ON_ONCE(delalloc))
f51623b2 1026 return 0;
448011e2 1027 if (WARN_ON_ONCE(unwritten))
f51623b2
NS
1028 return 0;
1029
f51623b2
NS
1030 return try_to_free_buffers(page);
1031}
1032
a719370b 1033/*
a06c277a
DC
1034 * When we map a DIO buffer, we may need to attach an ioend that describes the
1035 * type of write IO we are doing. This passes to the completion function the
1036 * operations it needs to perform. If the mapping is for an overwrite wholly
1037 * within the EOF then we don't need an ioend and so we don't allocate one.
1038 * This avoids the unnecessary overhead of allocating and freeing ioends for
1039 * workloads that don't require transactions on IO completion.
d5cc2e3f
DC
1040 *
1041 * If we get multiple mappings in a single IO, we might be mapping different
1042 * types. But because the direct IO can only have a single private pointer, we
1043 * need to ensure that:
1044 *
a06c277a
DC
1045 * a) i) the ioend spans the entire region of unwritten mappings; or
1046 * ii) the ioend spans all the mappings that cross or are beyond EOF; and
d5cc2e3f
DC
1047 * b) if it contains unwritten extents, it is *permanently* marked as such
1048 *
1049 * We could do this by chaining ioends like buffered IO does, but we only
1050 * actually get one IO completion callback from the direct IO, and that spans
1051 * the entire IO regardless of how many mappings and IOs are needed to complete
1052 * the DIO. There is only going to be one reference to the ioend and its life
1053 * cycle is constrained by the DIO completion code. hence we don't need
1054 * reference counting here.
3e12dbbd
DC
1055 *
1056 * Note that for DIO, an IO to the highest supported file block offset (i.e.
1057 * 2^63 - 1FSB bytes) will result in the offset + count overflowing a signed 64
1058 * bit variable. Hence if we see this overflow, we have to assume that the IO is
1059 * extending the file size. We won't know for sure until IO completion is run
1060 * and the actual max write offset is communicated to the IO completion
1061 * routine.
1062 *
1063 * For DAX page faults, we are preparing to never see unwritten extents here,
1064 * nor should we ever extend the inode size. Hence we will soon have nothing to
1065 * do here for this case, ensuring we don't have to provide an IO completion
1066 * callback to free an ioend that we don't actually need for a fault into the
1067 * page at offset (2^63 - 1FSB) bytes.
a719370b 1068 */
3e12dbbd 1069
a719370b
DC
1070static void
1071xfs_map_direct(
1072 struct inode *inode,
1073 struct buffer_head *bh_result,
1074 struct xfs_bmbt_irec *imap,
3e12dbbd
DC
1075 xfs_off_t offset,
1076 bool dax_fault)
a719370b 1077{
d5cc2e3f
DC
1078 struct xfs_ioend *ioend;
1079 xfs_off_t size = bh_result->b_size;
1080 int type;
1081
1082 if (ISUNWRITTEN(imap))
1083 type = XFS_IO_UNWRITTEN;
1084 else
1085 type = XFS_IO_OVERWRITE;
1086
1087 trace_xfs_gbmap_direct(XFS_I(inode), offset, size, type, imap);
1088
3e12dbbd
DC
1089 if (dax_fault) {
1090 ASSERT(type == XFS_IO_OVERWRITE);
1091 trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type,
1092 imap);
1093 return;
1094 }
3e12dbbd 1095
d5cc2e3f
DC
1096 if (bh_result->b_private) {
1097 ioend = bh_result->b_private;
1098 ASSERT(ioend->io_size > 0);
1099 ASSERT(offset >= ioend->io_offset);
1100 if (offset + size > ioend->io_offset + ioend->io_size)
1101 ioend->io_size = offset - ioend->io_offset + size;
1102
1103 if (type == XFS_IO_UNWRITTEN && type != ioend->io_type)
1104 ioend->io_type = XFS_IO_UNWRITTEN;
1105
1106 trace_xfs_gbmap_direct_update(XFS_I(inode), ioend->io_offset,
1107 ioend->io_size, ioend->io_type,
1108 imap);
a06c277a 1109 } else if (type == XFS_IO_UNWRITTEN ||
3e12dbbd
DC
1110 offset + size > i_size_read(inode) ||
1111 offset + size < 0) {
d5cc2e3f
DC
1112 ioend = xfs_alloc_ioend(inode, type);
1113 ioend->io_offset = offset;
1114 ioend->io_size = size;
a06c277a 1115
d5cc2e3f 1116 bh_result->b_private = ioend;
a06c277a 1117 set_buffer_defer_completion(bh_result);
d5cc2e3f
DC
1118
1119 trace_xfs_gbmap_direct_new(XFS_I(inode), offset, size, type,
1120 imap);
a06c277a
DC
1121 } else {
1122 trace_xfs_gbmap_direct_none(XFS_I(inode), offset, size, type,
1123 imap);
a719370b
DC
1124 }
1125}
1126
1fdca9c2
DC
1127/*
1128 * If this is O_DIRECT or the mpage code calling tell them how large the mapping
1129 * is, so that we can avoid repeated get_blocks calls.
1130 *
1131 * If the mapping spans EOF, then we have to break the mapping up as the mapping
1132 * for blocks beyond EOF must be marked new so that sub block regions can be
1133 * correctly zeroed. We can't do this for mappings within EOF unless the mapping
1134 * was just allocated or is unwritten, otherwise the callers would overwrite
1135 * existing data with zeros. Hence we have to split the mapping into a range up
1136 * to and including EOF, and a second mapping for beyond EOF.
1137 */
1138static void
1139xfs_map_trim_size(
1140 struct inode *inode,
1141 sector_t iblock,
1142 struct buffer_head *bh_result,
1143 struct xfs_bmbt_irec *imap,
1144 xfs_off_t offset,
1145 ssize_t size)
1146{
1147 xfs_off_t mapping_size;
1148
1149 mapping_size = imap->br_startoff + imap->br_blockcount - iblock;
1150 mapping_size <<= inode->i_blkbits;
1151
1152 ASSERT(mapping_size > 0);
1153 if (mapping_size > size)
1154 mapping_size = size;
1155 if (offset < i_size_read(inode) &&
1156 offset + mapping_size >= i_size_read(inode)) {
1157 /* limit mapping to block that spans EOF */
1158 mapping_size = roundup_64(i_size_read(inode) - offset,
1159 1 << inode->i_blkbits);
1160 }
1161 if (mapping_size > LONG_MAX)
1162 mapping_size = LONG_MAX;
1163
1164 bh_result->b_size = mapping_size;
1165}
1166
1da177e4 1167STATIC int
c2536668 1168__xfs_get_blocks(
1da177e4
LT
1169 struct inode *inode,
1170 sector_t iblock,
1da177e4
LT
1171 struct buffer_head *bh_result,
1172 int create,
3e12dbbd
DC
1173 bool direct,
1174 bool dax_fault)
1da177e4 1175{
a206c817
CH
1176 struct xfs_inode *ip = XFS_I(inode);
1177 struct xfs_mount *mp = ip->i_mount;
1178 xfs_fileoff_t offset_fsb, end_fsb;
1179 int error = 0;
1180 int lockmode = 0;
207d0416 1181 struct xfs_bmbt_irec imap;
a206c817 1182 int nimaps = 1;
fdc7ed75
NS
1183 xfs_off_t offset;
1184 ssize_t size;
207d0416 1185 int new = 0;
a206c817
CH
1186
1187 if (XFS_FORCED_SHUTDOWN(mp))
b474c7ae 1188 return -EIO;
1da177e4 1189
fdc7ed75 1190 offset = (xfs_off_t)iblock << inode->i_blkbits;
c2536668
NS
1191 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1192 size = bh_result->b_size;
364f358a
LM
1193
1194 if (!create && direct && offset >= i_size_read(inode))
1195 return 0;
1196
507630b2
DC
1197 /*
1198 * Direct I/O is usually done on preallocated files, so try getting
1199 * a block mapping without an exclusive lock first. For buffered
1200 * writes we already have the exclusive iolock anyway, so avoiding
1201 * a lock roundtrip here by taking the ilock exclusive from the
1202 * beginning is a useful micro optimization.
1203 */
1204 if (create && !direct) {
a206c817
CH
1205 lockmode = XFS_ILOCK_EXCL;
1206 xfs_ilock(ip, lockmode);
1207 } else {
309ecac8 1208 lockmode = xfs_ilock_data_map_shared(ip);
a206c817 1209 }
f2bde9b8 1210
d2c28191
DC
1211 ASSERT(offset <= mp->m_super->s_maxbytes);
1212 if (offset + size > mp->m_super->s_maxbytes)
1213 size = mp->m_super->s_maxbytes - offset;
a206c817
CH
1214 end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1215 offset_fsb = XFS_B_TO_FSBT(mp, offset);
1216
5c8ed202
DC
1217 error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
1218 &imap, &nimaps, XFS_BMAPI_ENTIRE);
1da177e4 1219 if (error)
a206c817
CH
1220 goto out_unlock;
1221
1ca19157 1222 /* for DAX, we convert unwritten extents directly */
a206c817
CH
1223 if (create &&
1224 (!nimaps ||
1225 (imap.br_startblock == HOLESTARTBLOCK ||
1ca19157
DC
1226 imap.br_startblock == DELAYSTARTBLOCK) ||
1227 (IS_DAX(inode) && ISUNWRITTEN(&imap)))) {
aff3a9ed 1228 if (direct || xfs_get_extsz_hint(ip)) {
507630b2 1229 /*
009c6e87
BF
1230 * xfs_iomap_write_direct() expects the shared lock. It
1231 * is unlocked on return.
507630b2 1232 */
009c6e87
BF
1233 if (lockmode == XFS_ILOCK_EXCL)
1234 xfs_ilock_demote(ip, lockmode);
1235
a206c817
CH
1236 error = xfs_iomap_write_direct(ip, offset, size,
1237 &imap, nimaps);
507630b2 1238 if (error)
2451337d 1239 return error;
d3bc815a 1240 new = 1;
6b698ede 1241
a206c817 1242 } else {
507630b2
DC
1243 /*
1244 * Delalloc reservations do not require a transaction,
d3bc815a
DC
1245 * we can go on without dropping the lock here. If we
1246 * are allocating a new delalloc block, make sure that
1247 * we set the new flag so that we mark the buffer new so
1248 * that we know that it is newly allocated if the write
1249 * fails.
507630b2 1250 */
d3bc815a
DC
1251 if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
1252 new = 1;
a206c817 1253 error = xfs_iomap_write_delay(ip, offset, size, &imap);
507630b2
DC
1254 if (error)
1255 goto out_unlock;
1256
1257 xfs_iunlock(ip, lockmode);
a206c817 1258 }
d5cc2e3f
DC
1259 trace_xfs_get_blocks_alloc(ip, offset, size,
1260 ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
1261 : XFS_IO_DELALLOC, &imap);
a206c817 1262 } else if (nimaps) {
d5cc2e3f
DC
1263 trace_xfs_get_blocks_found(ip, offset, size,
1264 ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
1265 : XFS_IO_OVERWRITE, &imap);
507630b2 1266 xfs_iunlock(ip, lockmode);
a206c817
CH
1267 } else {
1268 trace_xfs_get_blocks_notfound(ip, offset, size);
1269 goto out_unlock;
1270 }
1da177e4 1271
1ca19157
DC
1272 if (IS_DAX(inode) && create) {
1273 ASSERT(!ISUNWRITTEN(&imap));
1274 /* zeroing is not needed at a higher layer */
1275 new = 0;
1276 }
1277
1fdca9c2
DC
1278 /* trim mapping down to size requested */
1279 if (direct || size > (1 << inode->i_blkbits))
1280 xfs_map_trim_size(inode, iblock, bh_result,
1281 &imap, offset, size);
1282
a719370b
DC
1283 /*
1284 * For unwritten extents do not report a disk address in the buffered
1285 * read case (treat as if we're reading into a hole).
1286 */
207d0416 1287 if (imap.br_startblock != HOLESTARTBLOCK &&
a719370b
DC
1288 imap.br_startblock != DELAYSTARTBLOCK &&
1289 (create || !ISUNWRITTEN(&imap))) {
1290 xfs_map_buffer(inode, bh_result, &imap, offset);
1291 if (ISUNWRITTEN(&imap))
1da177e4 1292 set_buffer_unwritten(bh_result);
a719370b
DC
1293 /* direct IO needs special help */
1294 if (create && direct)
3e12dbbd
DC
1295 xfs_map_direct(inode, bh_result, &imap, offset,
1296 dax_fault);
1da177e4
LT
1297 }
1298
c2536668
NS
1299 /*
1300 * If this is a realtime file, data may be on a different device.
1301 * to that pointed to from the buffer_head b_bdev currently.
1302 */
046f1685 1303 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
1da177e4 1304
c2536668 1305 /*
549054af
DC
1306 * If we previously allocated a block out beyond eof and we are now
1307 * coming back to use it then we will need to flag it as new even if it
1308 * has a disk address.
1309 *
1310 * With sub-block writes into unwritten extents we also need to mark
1311 * the buffer as new so that the unwritten parts of the buffer gets
1312 * correctly zeroed.
1da177e4
LT
1313 */
1314 if (create &&
1315 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
549054af 1316 (offset >= i_size_read(inode)) ||
207d0416 1317 (new || ISUNWRITTEN(&imap))))
1da177e4 1318 set_buffer_new(bh_result);
1da177e4 1319
207d0416 1320 if (imap.br_startblock == DELAYSTARTBLOCK) {
1da177e4
LT
1321 BUG_ON(direct);
1322 if (create) {
1323 set_buffer_uptodate(bh_result);
1324 set_buffer_mapped(bh_result);
1325 set_buffer_delay(bh_result);
1326 }
1327 }
1328
1da177e4 1329 return 0;
a206c817
CH
1330
1331out_unlock:
1332 xfs_iunlock(ip, lockmode);
2451337d 1333 return error;
1da177e4
LT
1334}
1335
1336int
c2536668 1337xfs_get_blocks(
1da177e4
LT
1338 struct inode *inode,
1339 sector_t iblock,
1340 struct buffer_head *bh_result,
1341 int create)
1342{
3e12dbbd 1343 return __xfs_get_blocks(inode, iblock, bh_result, create, false, false);
1da177e4
LT
1344}
1345
6b698ede 1346int
e4c573bb 1347xfs_get_blocks_direct(
1da177e4
LT
1348 struct inode *inode,
1349 sector_t iblock,
1da177e4
LT
1350 struct buffer_head *bh_result,
1351 int create)
1352{
3e12dbbd
DC
1353 return __xfs_get_blocks(inode, iblock, bh_result, create, true, false);
1354}
1355
1356int
1357xfs_get_blocks_dax_fault(
1358 struct inode *inode,
1359 sector_t iblock,
1360 struct buffer_head *bh_result,
1361 int create)
1362{
1363 return __xfs_get_blocks(inode, iblock, bh_result, create, true, true);
1da177e4
LT
1364}
1365
6b698ede
DC
1366static void
1367__xfs_end_io_direct_write(
1368 struct inode *inode,
1369 struct xfs_ioend *ioend,
209fb87a 1370 loff_t offset,
6b698ede 1371 ssize_t size)
f0973863 1372{
6b698ede 1373 struct xfs_mount *mp = XFS_I(inode)->i_mount;
a06c277a 1374
6b698ede 1375 if (XFS_FORCED_SHUTDOWN(mp) || ioend->io_error)
6dfa1b67 1376 goto out_end_io;
f0973863 1377
2813d682 1378 /*
d5cc2e3f
DC
1379 * dio completion end_io functions are only called on writes if more
1380 * than 0 bytes was written.
2813d682 1381 */
d5cc2e3f
DC
1382 ASSERT(size > 0);
1383
1384 /*
1385 * The ioend only maps whole blocks, while the IO may be sector aligned.
a06c277a
DC
1386 * Hence the ioend offset/size may not match the IO offset/size exactly.
1387 * Because we don't map overwrites within EOF into the ioend, the offset
1388 * may not match, but only if the endio spans EOF. Either way, write
1389 * the IO sizes into the ioend so that completion processing does the
1390 * right thing.
d5cc2e3f 1391 */
d5cc2e3f
DC
1392 ASSERT(offset + size <= ioend->io_offset + ioend->io_size);
1393 ioend->io_size = size;
1394 ioend->io_offset = offset;
f0973863 1395
2813d682 1396 /*
6dfa1b67
DC
1397 * The ioend tells us whether we are doing unwritten extent conversion
1398 * or an append transaction that updates the on-disk file size. These
1399 * cases are the only cases where we should *potentially* be needing
a06c277a 1400 * to update the VFS inode size.
6dfa1b67
DC
1401 *
1402 * We need to update the in-core inode size here so that we don't end up
a06c277a
DC
1403 * with the on-disk inode size being outside the in-core inode size. We
1404 * have no other method of updating EOF for AIO, so always do it here
1405 * if necessary.
b9d59846
DC
1406 *
1407 * We need to lock the test/set EOF update as we can be racing with
1408 * other IO completions here to update the EOF. Failing to serialise
1409 * here can result in EOF moving backwards and Bad Things Happen when
1410 * that occurs.
2813d682 1411 */
6b698ede 1412 spin_lock(&XFS_I(inode)->i_flags_lock);
2ba66237
CH
1413 if (offset + size > i_size_read(inode))
1414 i_size_write(inode, offset + size);
6b698ede 1415 spin_unlock(&XFS_I(inode)->i_flags_lock);
2813d682 1416
f0973863 1417 /*
6dfa1b67
DC
1418 * If we are doing an append IO that needs to update the EOF on disk,
1419 * do the transaction reserve now so we can use common end io
1420 * processing. Stashing the error (if there is one) in the ioend will
1421 * result in the ioend processing passing on the error if it is
1422 * possible as we can't return it from here.
f0973863 1423 */
a06c277a 1424 if (ioend->io_type == XFS_IO_OVERWRITE)
6dfa1b67 1425 ioend->io_error = xfs_setfilesize_trans_alloc(ioend);
209fb87a 1426
6dfa1b67
DC
1427out_end_io:
1428 xfs_end_io(&ioend->io_work);
1429 return;
f0973863
CH
1430}
1431
6b698ede
DC
1432/*
1433 * Complete a direct I/O write request.
1434 *
1435 * The ioend structure is passed from __xfs_get_blocks() to tell us what to do.
1436 * If no ioend exists (i.e. @private == NULL) then the write IO is an overwrite
1437 * wholly within the EOF and so there is nothing for us to do. Note that in this
1438 * case the completion can be called in interrupt context, whereas if we have an
1439 * ioend we will always be called in task context (i.e. from a workqueue).
1440 */
1441STATIC void
1442xfs_end_io_direct_write(
1443 struct kiocb *iocb,
1444 loff_t offset,
1445 ssize_t size,
1446 void *private)
1447{
1448 struct inode *inode = file_inode(iocb->ki_filp);
1449 struct xfs_ioend *ioend = private;
1450
1451 trace_xfs_gbmap_direct_endio(XFS_I(inode), offset, size,
1452 ioend ? ioend->io_type : 0, NULL);
1453
1454 if (!ioend) {
1455 ASSERT(offset + size <= i_size_read(inode));
1456 return;
1457 }
1458
1459 __xfs_end_io_direct_write(inode, ioend, offset, size);
1460}
1461
6e1ba0bc
DC
1462static inline ssize_t
1463xfs_vm_do_dio(
1464 struct inode *inode,
1465 struct kiocb *iocb,
1466 struct iov_iter *iter,
1467 loff_t offset,
1468 void (*endio)(struct kiocb *iocb,
1469 loff_t offset,
1470 ssize_t size,
1471 void *private),
1472 int flags)
1473{
1474 struct block_device *bdev;
1475
1476 if (IS_DAX(inode))
1477 return dax_do_io(iocb, inode, iter, offset,
1478 xfs_get_blocks_direct, endio, 0);
1479
1480 bdev = xfs_find_bdev_for_inode(inode);
1481 return __blockdev_direct_IO(iocb, inode, bdev, iter, offset,
1482 xfs_get_blocks_direct, endio, NULL, flags);
1483}
1484
1da177e4 1485STATIC ssize_t
e4c573bb 1486xfs_vm_direct_IO(
1da177e4 1487 struct kiocb *iocb,
d8d3d94b
AV
1488 struct iov_iter *iter,
1489 loff_t offset)
1da177e4 1490{
209fb87a 1491 struct inode *inode = iocb->ki_filp->f_mapping->host;
209fb87a 1492
6e1ba0bc
DC
1493 if (iov_iter_rw(iter) == WRITE)
1494 return xfs_vm_do_dio(inode, iocb, iter, offset,
1495 xfs_end_io_direct_write, DIO_ASYNC_EXTEND);
1496 return xfs_vm_do_dio(inode, iocb, iter, offset, NULL, 0);
1da177e4
LT
1497}
1498
d3bc815a
DC
1499/*
1500 * Punch out the delalloc blocks we have already allocated.
1501 *
1502 * Don't bother with xfs_setattr given that nothing can have made it to disk yet
1503 * as the page is still locked at this point.
1504 */
1505STATIC void
1506xfs_vm_kill_delalloc_range(
1507 struct inode *inode,
1508 loff_t start,
1509 loff_t end)
1510{
1511 struct xfs_inode *ip = XFS_I(inode);
1512 xfs_fileoff_t start_fsb;
1513 xfs_fileoff_t end_fsb;
1514 int error;
1515
1516 start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
1517 end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
1518 if (end_fsb <= start_fsb)
1519 return;
1520
1521 xfs_ilock(ip, XFS_ILOCK_EXCL);
1522 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1523 end_fsb - start_fsb);
1524 if (error) {
1525 /* something screwed, just bail */
1526 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1527 xfs_alert(ip->i_mount,
1528 "xfs_vm_write_failed: unable to clean up ino %lld",
1529 ip->i_ino);
1530 }
1531 }
1532 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1533}
1534
fa9b227e
CH
1535STATIC void
1536xfs_vm_write_failed(
d3bc815a
DC
1537 struct inode *inode,
1538 struct page *page,
1539 loff_t pos,
1540 unsigned len)
fa9b227e 1541{
58e59854 1542 loff_t block_offset;
d3bc815a
DC
1543 loff_t block_start;
1544 loff_t block_end;
1545 loff_t from = pos & (PAGE_CACHE_SIZE - 1);
1546 loff_t to = from + len;
1547 struct buffer_head *bh, *head;
fa9b227e 1548
58e59854
JL
1549 /*
1550 * The request pos offset might be 32 or 64 bit, this is all fine
1551 * on 64-bit platform. However, for 64-bit pos request on 32-bit
1552 * platform, the high 32-bit will be masked off if we evaluate the
1553 * block_offset via (pos & PAGE_MASK) because the PAGE_MASK is
1554 * 0xfffff000 as an unsigned long, hence the result is incorrect
1555 * which could cause the following ASSERT failed in most cases.
1556 * In order to avoid this, we can evaluate the block_offset of the
1557 * start of the page by using shifts rather than masks the mismatch
1558 * problem.
1559 */
1560 block_offset = (pos >> PAGE_CACHE_SHIFT) << PAGE_CACHE_SHIFT;
1561
d3bc815a 1562 ASSERT(block_offset + from == pos);
c726de44 1563
d3bc815a
DC
1564 head = page_buffers(page);
1565 block_start = 0;
1566 for (bh = head; bh != head || !block_start;
1567 bh = bh->b_this_page, block_start = block_end,
1568 block_offset += bh->b_size) {
1569 block_end = block_start + bh->b_size;
c726de44 1570
d3bc815a
DC
1571 /* skip buffers before the write */
1572 if (block_end <= from)
1573 continue;
1574
1575 /* if the buffer is after the write, we're done */
1576 if (block_start >= to)
1577 break;
1578
1579 if (!buffer_delay(bh))
1580 continue;
1581
1582 if (!buffer_new(bh) && block_offset < i_size_read(inode))
1583 continue;
1584
1585 xfs_vm_kill_delalloc_range(inode, block_offset,
1586 block_offset + bh->b_size);
4ab9ed57
DC
1587
1588 /*
1589 * This buffer does not contain data anymore. make sure anyone
1590 * who finds it knows that for certain.
1591 */
1592 clear_buffer_delay(bh);
1593 clear_buffer_uptodate(bh);
1594 clear_buffer_mapped(bh);
1595 clear_buffer_new(bh);
1596 clear_buffer_dirty(bh);
fa9b227e 1597 }
d3bc815a 1598
fa9b227e
CH
1599}
1600
d3bc815a
DC
1601/*
1602 * This used to call block_write_begin(), but it unlocks and releases the page
1603 * on error, and we need that page to be able to punch stale delalloc blocks out
1604 * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
1605 * the appropriate point.
1606 */
f51623b2 1607STATIC int
d79689c7 1608xfs_vm_write_begin(
f51623b2 1609 struct file *file,
d79689c7
NP
1610 struct address_space *mapping,
1611 loff_t pos,
1612 unsigned len,
1613 unsigned flags,
1614 struct page **pagep,
1615 void **fsdata)
f51623b2 1616{
d3bc815a
DC
1617 pgoff_t index = pos >> PAGE_CACHE_SHIFT;
1618 struct page *page;
1619 int status;
155130a4 1620
d3bc815a
DC
1621 ASSERT(len <= PAGE_CACHE_SIZE);
1622
ad22c7a0 1623 page = grab_cache_page_write_begin(mapping, index, flags);
d3bc815a
DC
1624 if (!page)
1625 return -ENOMEM;
1626
1627 status = __block_write_begin(page, pos, len, xfs_get_blocks);
1628 if (unlikely(status)) {
1629 struct inode *inode = mapping->host;
72ab70a1 1630 size_t isize = i_size_read(inode);
d3bc815a
DC
1631
1632 xfs_vm_write_failed(inode, page, pos, len);
1633 unlock_page(page);
1634
72ab70a1
DC
1635 /*
1636 * If the write is beyond EOF, we only want to kill blocks
1637 * allocated in this write, not blocks that were previously
1638 * written successfully.
1639 */
1640 if (pos + len > isize) {
1641 ssize_t start = max_t(ssize_t, pos, isize);
1642
1643 truncate_pagecache_range(inode, start, pos + len);
1644 }
d3bc815a
DC
1645
1646 page_cache_release(page);
1647 page = NULL;
1648 }
1649
1650 *pagep = page;
1651 return status;
fa9b227e
CH
1652}
1653
d3bc815a 1654/*
aad3f375
DC
1655 * On failure, we only need to kill delalloc blocks beyond EOF in the range of
1656 * this specific write because they will never be written. Previous writes
1657 * beyond EOF where block allocation succeeded do not need to be trashed, so
1658 * only new blocks from this write should be trashed. For blocks within
1659 * EOF, generic_write_end() zeros them so they are safe to leave alone and be
1660 * written with all the other valid data.
d3bc815a 1661 */
fa9b227e
CH
1662STATIC int
1663xfs_vm_write_end(
1664 struct file *file,
1665 struct address_space *mapping,
1666 loff_t pos,
1667 unsigned len,
1668 unsigned copied,
1669 struct page *page,
1670 void *fsdata)
1671{
1672 int ret;
155130a4 1673
d3bc815a
DC
1674 ASSERT(len <= PAGE_CACHE_SIZE);
1675
fa9b227e 1676 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
d3bc815a
DC
1677 if (unlikely(ret < len)) {
1678 struct inode *inode = mapping->host;
1679 size_t isize = i_size_read(inode);
1680 loff_t to = pos + len;
1681
1682 if (to > isize) {
aad3f375
DC
1683 /* only kill blocks in this write beyond EOF */
1684 if (pos > isize)
1685 isize = pos;
d3bc815a 1686 xfs_vm_kill_delalloc_range(inode, isize, to);
aad3f375 1687 truncate_pagecache_range(inode, isize, to);
d3bc815a
DC
1688 }
1689 }
155130a4 1690 return ret;
f51623b2 1691}
1da177e4
LT
1692
1693STATIC sector_t
e4c573bb 1694xfs_vm_bmap(
1da177e4
LT
1695 struct address_space *mapping,
1696 sector_t block)
1697{
1698 struct inode *inode = (struct inode *)mapping->host;
739bfb2a 1699 struct xfs_inode *ip = XFS_I(inode);
1da177e4 1700
cca28fb8 1701 trace_xfs_vm_bmap(XFS_I(inode));
126468b1 1702 xfs_ilock(ip, XFS_IOLOCK_SHARED);
4bc1ea6b 1703 filemap_write_and_wait(mapping);
126468b1 1704 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
c2536668 1705 return generic_block_bmap(mapping, block, xfs_get_blocks);
1da177e4
LT
1706}
1707
1708STATIC int
e4c573bb 1709xfs_vm_readpage(
1da177e4
LT
1710 struct file *unused,
1711 struct page *page)
1712{
121e213e 1713 trace_xfs_vm_readpage(page->mapping->host, 1);
c2536668 1714 return mpage_readpage(page, xfs_get_blocks);
1da177e4
LT
1715}
1716
1717STATIC int
e4c573bb 1718xfs_vm_readpages(
1da177e4
LT
1719 struct file *unused,
1720 struct address_space *mapping,
1721 struct list_head *pages,
1722 unsigned nr_pages)
1723{
121e213e 1724 trace_xfs_vm_readpages(mapping->host, nr_pages);
c2536668 1725 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
1da177e4
LT
1726}
1727
22e757a4
DC
1728/*
1729 * This is basically a copy of __set_page_dirty_buffers() with one
1730 * small tweak: buffers beyond EOF do not get marked dirty. If we mark them
1731 * dirty, we'll never be able to clean them because we don't write buffers
1732 * beyond EOF, and that means we can't invalidate pages that span EOF
1733 * that have been marked dirty. Further, the dirty state can leak into
1734 * the file interior if the file is extended, resulting in all sorts of
1735 * bad things happening as the state does not match the underlying data.
1736 *
1737 * XXX: this really indicates that bufferheads in XFS need to die. Warts like
1738 * this only exist because of bufferheads and how the generic code manages them.
1739 */
1740STATIC int
1741xfs_vm_set_page_dirty(
1742 struct page *page)
1743{
1744 struct address_space *mapping = page->mapping;
1745 struct inode *inode = mapping->host;
1746 loff_t end_offset;
1747 loff_t offset;
1748 int newly_dirty;
c4843a75 1749 struct mem_cgroup *memcg;
22e757a4
DC
1750
1751 if (unlikely(!mapping))
1752 return !TestSetPageDirty(page);
1753
1754 end_offset = i_size_read(inode);
1755 offset = page_offset(page);
1756
1757 spin_lock(&mapping->private_lock);
1758 if (page_has_buffers(page)) {
1759 struct buffer_head *head = page_buffers(page);
1760 struct buffer_head *bh = head;
1761
1762 do {
1763 if (offset < end_offset)
1764 set_buffer_dirty(bh);
1765 bh = bh->b_this_page;
1766 offset += 1 << inode->i_blkbits;
1767 } while (bh != head);
1768 }
c4843a75
GT
1769 /*
1770 * Use mem_group_begin_page_stat() to keep PageDirty synchronized with
1771 * per-memcg dirty page counters.
1772 */
1773 memcg = mem_cgroup_begin_page_stat(page);
22e757a4
DC
1774 newly_dirty = !TestSetPageDirty(page);
1775 spin_unlock(&mapping->private_lock);
1776
1777 if (newly_dirty) {
1778 /* sigh - __set_page_dirty() is static, so copy it here, too */
1779 unsigned long flags;
1780
1781 spin_lock_irqsave(&mapping->tree_lock, flags);
1782 if (page->mapping) { /* Race with truncate? */
1783 WARN_ON_ONCE(!PageUptodate(page));
c4843a75 1784 account_page_dirtied(page, mapping, memcg);
22e757a4
DC
1785 radix_tree_tag_set(&mapping->page_tree,
1786 page_index(page), PAGECACHE_TAG_DIRTY);
1787 }
1788 spin_unlock_irqrestore(&mapping->tree_lock, flags);
22e757a4 1789 }
c4843a75
GT
1790 mem_cgroup_end_page_stat(memcg);
1791 if (newly_dirty)
1792 __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
22e757a4
DC
1793 return newly_dirty;
1794}
1795
f5e54d6e 1796const struct address_space_operations xfs_address_space_operations = {
e4c573bb
NS
1797 .readpage = xfs_vm_readpage,
1798 .readpages = xfs_vm_readpages,
1799 .writepage = xfs_vm_writepage,
7d4fb40a 1800 .writepages = xfs_vm_writepages,
22e757a4 1801 .set_page_dirty = xfs_vm_set_page_dirty,
238f4c54
NS
1802 .releasepage = xfs_vm_releasepage,
1803 .invalidatepage = xfs_vm_invalidatepage,
d79689c7 1804 .write_begin = xfs_vm_write_begin,
fa9b227e 1805 .write_end = xfs_vm_write_end,
e4c573bb
NS
1806 .bmap = xfs_vm_bmap,
1807 .direct_IO = xfs_vm_direct_IO,
e965f963 1808 .migratepage = buffer_migrate_page,
bddaafa1 1809 .is_partially_uptodate = block_is_partially_uptodate,
aa261f54 1810 .error_remove_page = generic_error_remove_page,
1da177e4 1811};