blob: 1ace78bfbea740721d00eb4ead28b624171857d0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include "xfs_mount.h"
26#include "xfs_bmap_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include "xfs_dinode.h"
28#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110029#include "xfs_alloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "xfs_error.h"
31#include "xfs_rw.h"
32#include "xfs_iomap.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100033#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000034#include "xfs_trace.h"
Dave Chinner3ed3a432010-03-05 02:00:42 +000035#include "xfs_bmap.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110038#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/writeback.h>
40
Christoph Hellwig34a52c62010-04-28 12:28:57 +000041/*
42 * Types of I/O for bmap clustering and I/O completion tracking.
43 */
44enum {
45 IO_READ, /* mapping for a read */
46 IO_DELAY, /* mapping covers delalloc region */
47 IO_UNWRITTEN, /* mapping covers allocated but uninitialized data */
48 IO_NEW /* just allocated */
49};
Christoph Hellwig25e41b32008-12-03 12:20:39 +010050
51/*
52 * Prime number of hash buckets since address is used as the key.
53 */
54#define NVSYNC 37
55#define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
56static wait_queue_head_t xfs_ioend_wq[NVSYNC];
57
58void __init
59xfs_ioend_init(void)
60{
61 int i;
62
63 for (i = 0; i < NVSYNC; i++)
64 init_waitqueue_head(&xfs_ioend_wq[i]);
65}
66
67void
68xfs_ioend_wait(
69 xfs_inode_t *ip)
70{
71 wait_queue_head_t *wq = to_ioend_wq(ip);
72
73 wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
74}
75
76STATIC void
77xfs_ioend_wake(
78 xfs_inode_t *ip)
79{
80 if (atomic_dec_and_test(&ip->i_iocount))
81 wake_up(to_ioend_wq(ip));
82}
83
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000084void
Nathan Scottf51623b2006-03-14 13:26:27 +110085xfs_count_page_state(
86 struct page *page,
87 int *delalloc,
Nathan Scottf51623b2006-03-14 13:26:27 +110088 int *unwritten)
89{
90 struct buffer_head *bh, *head;
91
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100092 *delalloc = *unwritten = 0;
Nathan Scottf51623b2006-03-14 13:26:27 +110093
94 bh = head = page_buffers(page);
95 do {
Christoph Hellwig20cb52e2010-06-24 09:46:01 +100096 if (buffer_unwritten(bh))
Nathan Scottf51623b2006-03-14 13:26:27 +110097 (*unwritten) = 1;
98 else if (buffer_delay(bh))
99 (*delalloc) = 1;
100 } while ((bh = bh->b_this_page) != head);
101}
102
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000103STATIC struct block_device *
104xfs_find_bdev_for_inode(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000105 struct inode *inode)
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000106{
Christoph Hellwig046f1682010-04-28 12:28:52 +0000107 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000108 struct xfs_mount *mp = ip->i_mount;
109
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100110 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000111 return mp->m_rtdev_targp->bt_bdev;
112 else
113 return mp->m_ddev_targp->bt_bdev;
114}
115
Christoph Hellwig0829c362005-09-02 16:58:49 +1000116/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100117 * We're now finished for good with this ioend structure.
118 * Update the page state via the associated buffer_heads,
119 * release holds on the inode and bio, and finally free
120 * up memory. Do not use the ioend after this.
121 */
Christoph Hellwig0829c362005-09-02 16:58:49 +1000122STATIC void
123xfs_destroy_ioend(
124 xfs_ioend_t *ioend)
125{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100126 struct buffer_head *bh, *next;
Christoph Hellwig583fa582008-12-03 12:20:38 +0100127 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100128
129 for (bh = ioend->io_buffer_head; bh; bh = next) {
130 next = bh->b_private;
Nathan Scott7d04a332006-06-09 14:58:38 +1000131 bh->b_end_io(bh, !ioend->io_error);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100132 }
Christoph Hellwig583fa582008-12-03 12:20:38 +0100133
134 /*
135 * Volume managers supporting multiple paths can send back ENODEV
136 * when the final path disappears. In this case continuing to fill
137 * the page cache with dirty data which cannot be written out is
138 * evil, so prevent that.
139 */
140 if (unlikely(ioend->io_error == -ENODEV)) {
141 xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
142 __FILE__, __LINE__);
Christoph Hellwigb677c212007-08-29 11:46:28 +1000143 }
Christoph Hellwig583fa582008-12-03 12:20:38 +0100144
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100145 xfs_ioend_wake(ip);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000146 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/*
Dave Chinner932640e2009-10-06 20:29:29 +0000150 * If the end of the current ioend is beyond the current EOF,
151 * return the new EOF value, otherwise zero.
152 */
153STATIC xfs_fsize_t
154xfs_ioend_new_eof(
155 xfs_ioend_t *ioend)
156{
157 xfs_inode_t *ip = XFS_I(ioend->io_inode);
158 xfs_fsize_t isize;
159 xfs_fsize_t bsize;
160
161 bsize = ioend->io_offset + ioend->io_size;
162 isize = MAX(ip->i_size, ip->i_new_size);
163 isize = MIN(isize, bsize);
164 return isize > ip->i_d.di_size ? isize : 0;
165}
166
167/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000168 * Update on-disk file size now that data has been written to disk. The
169 * current in-memory file size is i_size. If a write is beyond eof i_new_size
170 * will be the intended file size until i_size is updated. If this write does
171 * not extend all the way to the valid file size then restrict this update to
172 * the end of the write.
173 *
174 * This function does not block as blocking on the inode lock in IO completion
175 * can lead to IO completion order dependency deadlocks.. If it can't get the
176 * inode ilock it will return EAGAIN. Callers must handle this.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000177 */
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000178STATIC int
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000179xfs_setfilesize(
180 xfs_ioend_t *ioend)
181{
Christoph Hellwigb677c212007-08-29 11:46:28 +1000182 xfs_inode_t *ip = XFS_I(ioend->io_inode);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000183 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000184
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000185 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000186 ASSERT(ioend->io_type != IO_READ);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000187
188 if (unlikely(ioend->io_error))
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000189 return 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000190
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000191 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
192 return EAGAIN;
193
Dave Chinner932640e2009-10-06 20:29:29 +0000194 isize = xfs_ioend_new_eof(ioend);
195 if (isize) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000196 ip->i_d.di_size = isize;
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000197 xfs_mark_inode_dirty(ip);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000198 }
199
200 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000201 return 0;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000202}
203
204/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000205 * Schedule IO completion handling on the final put of an ioend.
Dave Chinnerc626d172009-04-06 18:42:11 +0200206 */
207STATIC void
208xfs_finish_ioend(
Christoph Hellwig209fb872010-07-18 21:17:11 +0000209 struct xfs_ioend *ioend)
Dave Chinnerc626d172009-04-06 18:42:11 +0200210{
211 if (atomic_dec_and_test(&ioend->io_remaining)) {
Christoph Hellwig209fb872010-07-18 21:17:11 +0000212 if (ioend->io_type == IO_UNWRITTEN)
213 queue_work(xfsconvertd_workqueue, &ioend->io_work);
214 else
215 queue_work(xfsdatad_workqueue, &ioend->io_work);
Dave Chinnerc626d172009-04-06 18:42:11 +0200216 }
217}
218
219/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000220 * IO write completion.
221 */
222STATIC void
223xfs_end_io(
224 struct work_struct *work)
225{
226 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
227 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Dave Chinner69418932010-03-04 00:57:09 +0000228 int error = 0;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000229
230 /*
231 * For unwritten extents we need to issue transactions to convert a
232 * range to normal written extens after the data I/O has finished.
233 */
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000234 if (ioend->io_type == IO_UNWRITTEN &&
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000235 likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
236
237 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
238 ioend->io_size);
239 if (error)
240 ioend->io_error = error;
241 }
242
243 /*
244 * We might have to update the on-disk file size after extending
245 * writes.
246 */
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000247 if (ioend->io_type != IO_READ) {
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000248 error = xfs_setfilesize(ioend);
249 ASSERT(!error || error == EAGAIN);
250 }
251
252 /*
253 * If we didn't complete processing of the ioend, requeue it to the
254 * tail of the workqueue for another attempt later. Otherwise destroy
255 * it.
256 */
257 if (error == EAGAIN) {
258 atomic_inc(&ioend->io_remaining);
Christoph Hellwig209fb872010-07-18 21:17:11 +0000259 xfs_finish_ioend(ioend);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000260 /* ensure we don't spin on blocked ioends */
261 delay(1);
Christoph Hellwigfb511f22010-07-18 21:17:10 +0000262 } else {
263 if (ioend->io_iocb)
264 aio_complete(ioend->io_iocb, ioend->io_result, 0);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000265 xfs_destroy_ioend(ioend);
Christoph Hellwigfb511f22010-07-18 21:17:10 +0000266 }
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000267}
268
269/*
Christoph Hellwig209fb872010-07-18 21:17:11 +0000270 * Call IO completion handling in caller context on the final put of an ioend.
271 */
272STATIC void
273xfs_finish_ioend_sync(
274 struct xfs_ioend *ioend)
275{
276 if (atomic_dec_and_test(&ioend->io_remaining))
277 xfs_end_io(&ioend->io_work);
278}
279
280/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000281 * Allocate and initialise an IO completion structure.
282 * We need to track unwritten extent write completion here initially.
283 * We'll need to extend this for updating the ondisk inode size later
284 * (vs. incore size).
285 */
286STATIC xfs_ioend_t *
287xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100288 struct inode *inode,
289 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000290{
291 xfs_ioend_t *ioend;
292
293 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
294
295 /*
296 * Set the count to 1 initially, which will prevent an I/O
297 * completion callback from happening before we have started
298 * all the I/O from calling the completion routine too early.
299 */
300 atomic_set(&ioend->io_remaining, 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000301 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100302 ioend->io_list = NULL;
303 ioend->io_type = type;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000304 ioend->io_inode = inode;
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000305 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100306 ioend->io_buffer_tail = NULL;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000307 atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000308 ioend->io_offset = 0;
309 ioend->io_size = 0;
Christoph Hellwigfb511f22010-07-18 21:17:10 +0000310 ioend->io_iocb = NULL;
311 ioend->io_result = 0;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000312
Christoph Hellwig5ec4fab2009-10-30 09:11:47 +0000313 INIT_WORK(&ioend->io_work, xfs_end_io);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000314 return ioend;
315}
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317STATIC int
318xfs_map_blocks(
319 struct inode *inode,
320 loff_t offset,
321 ssize_t count,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000322 struct xfs_bmbt_irec *imap,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 int flags)
324{
Christoph Hellwig6bd16ff2008-12-03 12:20:32 +0100325 int nmaps = 1;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000326 int new = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Christoph Hellwig207d0412010-04-28 12:28:56 +0000328 return -xfs_iomap(XFS_I(inode), offset, count, flags, imap, &nmaps, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Christoph Hellwigb8f82a42009-11-14 16:17:22 +0000331STATIC int
Christoph Hellwig558e6892010-04-28 12:28:58 +0000332xfs_imap_valid(
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000333 struct inode *inode,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000334 struct xfs_bmbt_irec *imap,
Christoph Hellwig558e6892010-04-28 12:28:58 +0000335 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Christoph Hellwig558e6892010-04-28 12:28:58 +0000337 offset >>= inode->i_blkbits;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000338
Christoph Hellwig558e6892010-04-28 12:28:58 +0000339 return offset >= imap->br_startoff &&
340 offset < imap->br_startoff + imap->br_blockcount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341}
342
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100343/*
344 * BIO completion handler for buffered IO.
345 */
Al Viro782e3b32007-10-12 07:17:47 +0100346STATIC void
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100347xfs_end_bio(
348 struct bio *bio,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100349 int error)
350{
351 xfs_ioend_t *ioend = bio->bi_private;
352
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100353 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000354 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100355
356 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100357 bio->bi_private = NULL;
358 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100359 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000360
Christoph Hellwig209fb872010-07-18 21:17:11 +0000361 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100362}
363
364STATIC void
365xfs_submit_ioend_bio(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000366 struct writeback_control *wbc,
367 xfs_ioend_t *ioend,
368 struct bio *bio)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100369{
370 atomic_inc(&ioend->io_remaining);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100371 bio->bi_private = ioend;
372 bio->bi_end_io = xfs_end_bio;
373
Dave Chinner932640e2009-10-06 20:29:29 +0000374 /*
375 * If the I/O is beyond EOF we mark the inode dirty immediately
376 * but don't update the inode size until I/O completion.
377 */
378 if (xfs_ioend_new_eof(ioend))
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000379 xfs_mark_inode_dirty(XFS_I(ioend->io_inode));
Dave Chinner932640e2009-10-06 20:29:29 +0000380
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000381 submit_bio(wbc->sync_mode == WB_SYNC_ALL ?
382 WRITE_SYNC_PLUG : WRITE, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100383}
384
385STATIC struct bio *
386xfs_alloc_ioend_bio(
387 struct buffer_head *bh)
388{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100389 int nvecs = bio_get_nr_vecs(bh->b_bdev);
Christoph Hellwig221cb252010-12-10 08:42:17 +0000390 struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100391
392 ASSERT(bio->bi_private == NULL);
393 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
394 bio->bi_bdev = bh->b_bdev;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100395 return bio;
396}
397
398STATIC void
399xfs_start_buffer_writeback(
400 struct buffer_head *bh)
401{
402 ASSERT(buffer_mapped(bh));
403 ASSERT(buffer_locked(bh));
404 ASSERT(!buffer_delay(bh));
405 ASSERT(!buffer_unwritten(bh));
406
407 mark_buffer_async_write(bh);
408 set_buffer_uptodate(bh);
409 clear_buffer_dirty(bh);
410}
411
412STATIC void
413xfs_start_page_writeback(
414 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100415 int clear_dirty,
416 int buffers)
417{
418 ASSERT(PageLocked(page));
419 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100420 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100421 clear_page_dirty_for_io(page);
422 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100423 unlock_page(page);
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700424 /* If no buffers on the page are to be written, finish it here */
425 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100426 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100427}
428
429static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
430{
431 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
432}
433
434/*
David Chinnerd88992f2006-01-18 13:38:12 +1100435 * Submit all of the bios for all of the ioends we have saved up, covering the
436 * initial writepage page and also any probed pages.
437 *
438 * Because we may have multiple ioends spanning a page, we need to start
439 * writeback on all the buffers before we submit them for I/O. If we mark the
440 * buffers as we got, then we can end up with a page that only has buffers
441 * marked async write and I/O complete on can occur before we mark the other
442 * buffers async write.
443 *
444 * The end result of this is that we trip a bug in end_page_writeback() because
445 * we call it twice for the one page as the code in end_buffer_async_write()
446 * assumes that all buffers on the page are started at the same time.
447 *
448 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000449 * buffer_heads, and then submit them for I/O on the second pass.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100450 */
451STATIC void
452xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000453 struct writeback_control *wbc,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100454 xfs_ioend_t *ioend)
455{
David Chinnerd88992f2006-01-18 13:38:12 +1100456 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100457 xfs_ioend_t *next;
458 struct buffer_head *bh;
459 struct bio *bio;
460 sector_t lastblock = 0;
461
David Chinnerd88992f2006-01-18 13:38:12 +1100462 /* Pass 1 - start writeback */
463 do {
464 next = ioend->io_list;
Christoph Hellwig221cb252010-12-10 08:42:17 +0000465 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
David Chinnerd88992f2006-01-18 13:38:12 +1100466 xfs_start_buffer_writeback(bh);
David Chinnerd88992f2006-01-18 13:38:12 +1100467 } while ((ioend = next) != NULL);
468
469 /* Pass 2 - submit I/O */
470 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100471 do {
472 next = ioend->io_list;
473 bio = NULL;
474
475 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100476
477 if (!bio) {
478 retry:
479 bio = xfs_alloc_ioend_bio(bh);
480 } else if (bh->b_blocknr != lastblock + 1) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000481 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100482 goto retry;
483 }
484
485 if (bio_add_buffer(bio, bh) != bh->b_size) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000486 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100487 goto retry;
488 }
489
490 lastblock = bh->b_blocknr;
491 }
492 if (bio)
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000493 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwig209fb872010-07-18 21:17:11 +0000494 xfs_finish_ioend(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100495 } while ((ioend = next) != NULL);
496}
497
498/*
499 * Cancel submission of all buffer_heads so far in this endio.
500 * Toss the endio too. Only ever called for the initial page
501 * in a writepage request, so only ever one page.
502 */
503STATIC void
504xfs_cancel_ioend(
505 xfs_ioend_t *ioend)
506{
507 xfs_ioend_t *next;
508 struct buffer_head *bh, *next_bh;
509
510 do {
511 next = ioend->io_list;
512 bh = ioend->io_buffer_head;
513 do {
514 next_bh = bh->b_private;
515 clear_buffer_async_write(bh);
516 unlock_buffer(bh);
517 } while ((bh = next_bh) != NULL);
518
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100519 xfs_ioend_wake(XFS_I(ioend->io_inode));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100520 mempool_free(ioend, xfs_ioend_pool);
521 } while ((ioend = next) != NULL);
522}
523
524/*
525 * Test to see if we've been building up a completion structure for
526 * earlier buffers -- if so, we try to append to this ioend if we
527 * can, otherwise we finish off any current ioend and start another.
528 * Return true if we've finished the given ioend.
529 */
530STATIC void
531xfs_add_to_ioend(
532 struct inode *inode,
533 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100534 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100535 unsigned int type,
536 xfs_ioend_t **result,
537 int need_ioend)
538{
539 xfs_ioend_t *ioend = *result;
540
541 if (!ioend || need_ioend || type != ioend->io_type) {
542 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100543
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100544 ioend = xfs_alloc_ioend(inode, type);
545 ioend->io_offset = offset;
546 ioend->io_buffer_head = bh;
547 ioend->io_buffer_tail = bh;
548 if (previous)
549 previous->io_list = ioend;
550 *result = ioend;
551 } else {
552 ioend->io_buffer_tail->b_private = bh;
553 ioend->io_buffer_tail = bh;
554 }
555
556 bh->b_private = NULL;
557 ioend->io_size += bh->b_size;
558}
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100561xfs_map_buffer(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000562 struct inode *inode,
Nathan Scott87cbc492006-03-14 13:26:43 +1100563 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000564 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000565 xfs_off_t offset)
Nathan Scott87cbc492006-03-14 13:26:43 +1100566{
567 sector_t bn;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000568 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000569 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
570 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
Nathan Scott87cbc492006-03-14 13:26:43 +1100571
Christoph Hellwig207d0412010-04-28 12:28:56 +0000572 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
573 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Nathan Scott87cbc492006-03-14 13:26:43 +1100574
Christoph Hellwige5131822010-04-28 12:28:55 +0000575 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000576 ((offset - iomap_offset) >> inode->i_blkbits);
Nathan Scott87cbc492006-03-14 13:26:43 +1100577
Christoph Hellwig046f1682010-04-28 12:28:52 +0000578 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
Nathan Scott87cbc492006-03-14 13:26:43 +1100579
580 bh->b_blocknr = bn;
581 set_buffer_mapped(bh);
582}
583
584STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585xfs_map_at_offset(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000586 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000588 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000589 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Christoph Hellwig207d0412010-04-28 12:28:56 +0000591 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
592 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 lock_buffer(bh);
Christoph Hellwig207d0412010-04-28 12:28:56 +0000595 xfs_map_buffer(inode, bh, imap, offset);
Christoph Hellwig046f1682010-04-28 12:28:52 +0000596 bh->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 set_buffer_mapped(bh);
598 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100599 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
602/*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100603 * Look for a page at index that is suitable for clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
605STATIC unsigned int
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100606xfs_probe_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100607 struct page *page,
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000608 unsigned int pg_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000610 struct buffer_head *bh, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 int ret = 0;
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100614 return 0;
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000615 if (!PageDirty(page))
616 return 0;
617 if (!page->mapping)
618 return 0;
619 if (!page_has_buffers(page))
620 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000622 bh = head = page_buffers(page);
623 do {
624 if (!buffer_uptodate(bh))
625 break;
626 if (!buffer_mapped(bh))
627 break;
628 ret += bh->b_size;
629 if (ret >= pg_offset)
630 break;
631 } while ((bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 return ret;
634}
635
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100636STATIC size_t
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100637xfs_probe_cluster(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 struct inode *inode,
639 struct page *startpage,
640 struct buffer_head *bh,
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000641 struct buffer_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100643 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100645 size_t total = 0;
646 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 /* First sum forwards in this page */
649 do {
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000650 if (!buffer_uptodate(bh) || !buffer_mapped(bh))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100651 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 total += bh->b_size;
653 } while ((bh = bh->b_this_page) != head);
654
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100655 /* if we reached the end of the page, sum forwards in following pages */
656 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
657 tindex = startpage->index + 1;
658
659 /* Prune this back to avoid pathological behavior */
660 tloff = min(tlast, startpage->index + 64);
661
662 pagevec_init(&pvec, 0);
663 while (!done && tindex <= tloff) {
664 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
665
666 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
667 break;
668
669 for (i = 0; i < pagevec_count(&pvec); i++) {
670 struct page *page = pvec.pages[i];
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000671 size_t pg_offset, pg_len = 0;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100672
673 if (tindex == tlast) {
674 pg_offset =
675 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100676 if (!pg_offset) {
677 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100678 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100679 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100680 } else
681 pg_offset = PAGE_CACHE_SIZE;
682
Nick Piggin529ae9a2008-08-02 12:01:03 +0200683 if (page->index == tindex && trylock_page(page)) {
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000684 pg_len = xfs_probe_page(page, pg_offset);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100685 unlock_page(page);
686 }
687
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000688 if (!pg_len) {
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100689 done = 1;
690 break;
691 }
692
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000693 total += pg_len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100694 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100696
697 pagevec_release(&pvec);
698 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 return total;
702}
703
704/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100705 * Test if a given page is suitable for writing as part of an unwritten
706 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100708STATIC int
709xfs_is_delayed_page(
710 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100711 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
716 if (page->mapping && page_has_buffers(page)) {
717 struct buffer_head *bh, *head;
718 int acceptable = 0;
719
720 bh = head = page_buffers(page);
721 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100722 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000723 acceptable = (type == IO_UNWRITTEN);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100724 else if (buffer_delay(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000725 acceptable = (type == IO_DELAY);
David Chinner2ddee842006-03-22 12:47:40 +1100726 else if (buffer_dirty(bh) && buffer_mapped(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000727 acceptable = (type == IO_NEW);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100728 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 } while ((bh = bh->b_this_page) != head);
731
732 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100733 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100736 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739/*
740 * Allocate & map buffers for page given the extent map. Write it out.
741 * except for the original page of a writepage, this is called on
742 * delalloc/unwritten pages only, for the original page it is possible
743 * that the page has no mapping at all.
744 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100745STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746xfs_convert_page(
747 struct inode *inode,
748 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100749 loff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000750 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100751 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 int all_bh)
754{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100755 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100756 xfs_off_t end_offset;
757 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100758 unsigned int type;
Nathan Scott24e17b52005-05-05 13:33:20 -0700759 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100760 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100761 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100763 if (page->index != tindex)
764 goto fail;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200765 if (!trylock_page(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100766 goto fail;
767 if (PageWriteback(page))
768 goto fail_unlock_page;
769 if (page->mapping != inode->i_mapping)
770 goto fail_unlock_page;
771 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
772 goto fail_unlock_page;
773
Nathan Scott24e17b52005-05-05 13:33:20 -0700774 /*
775 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000776 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100777 *
778 * Derivation:
779 *
780 * End offset is the highest offset that this page should represent.
781 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
782 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
783 * hence give us the correct page_dirty count. On any other page,
784 * it will be zero and in that case we need page_dirty to be the
785 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700786 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100787 end_offset = min_t(unsigned long long,
788 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
789 i_size_read(inode));
790
Nathan Scott24e17b52005-05-05 13:33:20 -0700791 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100792 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
793 PAGE_CACHE_SIZE);
794 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
795 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 bh = head = page_buffers(page);
798 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100799 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100801 if (!buffer_uptodate(bh))
802 uptodate = 0;
803 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
804 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100806 }
807
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100808 if (buffer_unwritten(bh) || buffer_delay(bh)) {
809 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000810 type = IO_UNWRITTEN;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100811 else
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000812 type = IO_DELAY;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100813
Christoph Hellwig558e6892010-04-28 12:28:58 +0000814 if (!xfs_imap_valid(inode, imap, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100815 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100816 continue;
817 }
818
Christoph Hellwig207d0412010-04-28 12:28:56 +0000819 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
820 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100821
Christoph Hellwig207d0412010-04-28 12:28:56 +0000822 xfs_map_at_offset(inode, bh, imap, offset);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000823 xfs_add_to_ioend(inode, bh, offset, type,
824 ioendp, done);
825
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100826 page_dirty--;
827 count++;
828 } else {
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000829 type = IO_NEW;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000830 if (buffer_mapped(bh) && all_bh) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100832 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100833 type, ioendp, done);
834 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700835 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100836 } else {
837 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100840 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100842 if (uptodate && bh == head)
843 SetPageUptodate(page);
844
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000845 if (count) {
Dave Chinnerefceab12010-08-24 11:44:56 +1000846 if (--wbc->nr_to_write <= 0 &&
847 wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000848 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000850 xfs_start_page_writeback(page, !page_dirty, count);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100851
852 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100853 fail_unlock_page:
854 unlock_page(page);
855 fail:
856 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
859/*
860 * Convert & write out a cluster of pages in the same extent as defined
861 * by mp and following the start page.
862 */
863STATIC void
864xfs_cluster_write(
865 struct inode *inode,
866 pgoff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000867 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100868 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 int all_bh,
871 pgoff_t tlast)
872{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100873 struct pagevec pvec;
874 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100876 pagevec_init(&pvec, 0);
877 while (!done && tindex <= tlast) {
878 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
879
880 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100882
883 for (i = 0; i < pagevec_count(&pvec); i++) {
884 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000885 imap, ioendp, wbc, all_bh);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100886 if (done)
887 break;
888 }
889
890 pagevec_release(&pvec);
891 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893}
894
Dave Chinner3ed3a432010-03-05 02:00:42 +0000895STATIC void
896xfs_vm_invalidatepage(
897 struct page *page,
898 unsigned long offset)
899{
900 trace_xfs_invalidatepage(page->mapping->host, page, offset);
901 block_invalidatepage(page, offset);
902}
903
904/*
905 * If the page has delalloc buffers on it, we need to punch them out before we
906 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
907 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
908 * is done on that same region - the delalloc extent is returned when none is
909 * supposed to be there.
910 *
911 * We prevent this by truncating away the delalloc regions on the page before
912 * invalidating it. Because they are delalloc, we can do this without needing a
913 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
914 * truncation without a transaction as there is no space left for block
915 * reservation (typically why we see a ENOSPC in writeback).
916 *
917 * This is not a performance critical path, so for now just do the punching a
918 * buffer head at a time.
919 */
920STATIC void
921xfs_aops_discard_page(
922 struct page *page)
923{
924 struct inode *inode = page->mapping->host;
925 struct xfs_inode *ip = XFS_I(inode);
926 struct buffer_head *bh, *head;
927 loff_t offset = page_offset(page);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000928
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000929 if (!xfs_is_delayed_page(page, IO_DELAY))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000930 goto out_invalidate;
931
Dave Chinnere8c37532010-03-15 02:36:35 +0000932 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
933 goto out_invalidate;
934
Dave Chinner3ed3a432010-03-05 02:00:42 +0000935 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
936 "page discard on page %p, inode 0x%llx, offset %llu.",
937 page, ip->i_ino, offset);
938
939 xfs_ilock(ip, XFS_ILOCK_EXCL);
940 bh = head = page_buffers(page);
941 do {
Dave Chinner3ed3a432010-03-05 02:00:42 +0000942 int error;
Dave Chinnerc726de42010-11-30 15:14:39 +1100943 xfs_fileoff_t start_fsb;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000944
945 if (!buffer_delay(bh))
946 goto next_buffer;
947
Dave Chinnerc726de42010-11-30 15:14:39 +1100948 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
949 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
Dave Chinner3ed3a432010-03-05 02:00:42 +0000950 if (error) {
951 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +0000952 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
953 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +0000954 "page discard unable to remove delalloc mapping.");
Dave Chinnere8c37532010-03-15 02:36:35 +0000955 }
Dave Chinner3ed3a432010-03-05 02:00:42 +0000956 break;
957 }
958next_buffer:
Dave Chinnerc726de42010-11-30 15:14:39 +1100959 offset += 1 << inode->i_blkbits;
Dave Chinner3ed3a432010-03-05 02:00:42 +0000960
961 } while ((bh = bh->b_this_page) != head);
962
963 xfs_iunlock(ip, XFS_ILOCK_EXCL);
964out_invalidate:
965 xfs_vm_invalidatepage(page, 0);
966 return;
967}
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969/*
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000970 * Write out a dirty page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000972 * For delalloc space on the page we need to allocate space and flush it.
973 * For unwritten space on the page we need to start the conversion to
974 * regular allocated space.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000975 * For any other dirty buffer heads on the page we should flush them.
976 *
977 * If we detect that a transaction would be required to flush the page, we
978 * have to check the process flags first, if we are already in a transaction
979 * or disk I/O during allocations is off, we need to fail the writepage and
980 * redirty the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982STATIC int
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000983xfs_vm_writepage(
984 struct page *page,
985 struct writeback_control *wbc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000987 struct inode *inode = page->mapping->host;
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000988 int delalloc, unwritten;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100989 struct buffer_head *bh, *head;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000990 struct xfs_bmbt_irec imap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100991 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 loff_t offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100993 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 __uint64_t end_offset;
Christoph Hellwigbd1556a2010-04-28 12:29:00 +0000995 pgoff_t end_index, last_index;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100996 ssize_t size, len;
Christoph Hellwig558e6892010-04-28 12:28:58 +0000997 int flags, err, imap_valid = 0, uptodate = 1;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +1000998 int count = 0;
Christoph Hellwig20cb52e2010-06-24 09:46:01 +1000999 int all_bh = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001001 trace_xfs_writepage(inode, page, 0);
1002
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001003 ASSERT(page_has_buffers(page));
1004
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001005 /*
1006 * Refuse to write the page out if we are called from reclaim context.
1007 *
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -04001008 * This avoids stack overflows when called from deeply used stacks in
1009 * random callers for direct reclaim or memcg reclaim. We explicitly
1010 * allow reclaim from kswapd as the stack usage there is relatively low.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001011 *
1012 * This should really be done by the core VM, but until that happens
1013 * filesystems like XFS, btrfs and ext4 have to take care of this
1014 * by themselves.
1015 */
Christoph Hellwigd4f7a5c2010-06-28 10:34:44 -04001016 if ((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC)
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001017 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001018
1019 /*
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001020 * We need a transaction if there are delalloc or unwritten buffers
1021 * on the page.
1022 *
1023 * If we need a transaction and the process flags say we are already
1024 * in a transaction, or no IO is allowed then mark the page dirty
1025 * again and leave the page as is.
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001026 */
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001027 xfs_count_page_state(page, &delalloc, &unwritten);
1028 if ((current->flags & PF_FSTRANS) && (delalloc || unwritten))
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001029 goto redirty;
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 /* Is this page beyond the end of the file? */
1032 offset = i_size_read(inode);
1033 end_index = offset >> PAGE_CACHE_SHIFT;
1034 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
1035 if (page->index >= end_index) {
1036 if ((page->index >= end_index + 1) ||
1037 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001038 unlock_page(page);
Nathan Scott19d5bcf2005-11-02 15:14:09 +11001039 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041 }
1042
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001043 end_offset = min_t(unsigned long long,
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001044 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
1045 offset);
Nathan Scott24e17b52005-05-05 13:33:20 -07001046 len = 1 << inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -07001047
Nathan Scott24e17b52005-05-05 13:33:20 -07001048 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001049 offset = page_offset(page);
David Chinnerdf3c7242007-05-24 15:27:03 +10001050 flags = BMAPI_READ;
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001051 type = IO_NEW;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 do {
Christoph Hellwig6ac72482010-12-10 08:42:18 +00001054 int new_ioend = 0;
1055
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 if (offset >= end_offset)
1057 break;
1058 if (!buffer_uptodate(bh))
1059 uptodate = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Eric Sandeen3d9b02e2010-06-24 09:45:30 +10001061 /*
Christoph Hellwigece413f2010-11-10 21:39:11 +00001062 * set_page_dirty dirties all buffers in a page, independent
1063 * of their state. The dirty state however is entirely
1064 * meaningless for holes (!mapped && uptodate), so skip
1065 * buffers covering holes here.
Eric Sandeen3d9b02e2010-06-24 09:45:30 +10001066 */
1067 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
Eric Sandeen3d9b02e2010-06-24 09:45:30 +10001068 imap_valid = 0;
1069 continue;
1070 }
1071
Christoph Hellwig558e6892010-04-28 12:28:58 +00001072 if (imap_valid)
1073 imap_valid = xfs_imap_valid(inode, &imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001075 if (buffer_unwritten(bh) || buffer_delay(bh)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001076 if (buffer_unwritten(bh)) {
Christoph Hellwig85da94c2010-12-10 08:42:16 +00001077 if (type != IO_UNWRITTEN) {
1078 type = IO_UNWRITTEN;
1079 imap_valid = 0;
1080 }
Nathan Scott82721452006-04-11 15:10:55 +10001081 flags = BMAPI_WRITE | BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001082 } else if (buffer_delay(bh)) {
Christoph Hellwig85da94c2010-12-10 08:42:16 +00001083 if (type != IO_DELAY) {
1084 type = IO_DELAY;
1085 imap_valid = 0;
1086 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001087 flags = BMAPI_ALLOCATE;
1088
Wu Fengguang1b430be2010-10-26 14:21:26 -07001089 if (wbc->sync_mode == WB_SYNC_NONE)
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001090 flags |= BMAPI_TRYLOCK;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001091 }
1092
Christoph Hellwig558e6892010-04-28 12:28:58 +00001093 if (!imap_valid) {
David Chinnereffd1202007-06-18 16:49:58 +10001094 /*
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001095 * If we didn't have a valid mapping then we
David Chinnereffd1202007-06-18 16:49:58 +10001096 * need to ensure that we put the new mapping
1097 * in a new ioend structure. This needs to be
1098 * done to ensure that the ioends correctly
1099 * reflect the block mappings at io completion
1100 * for unwritten extent conversion.
1101 */
1102 new_ioend = 1;
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001103 err = xfs_map_blocks(inode, offset, len,
Christoph Hellwig207d0412010-04-28 12:28:56 +00001104 &imap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001105 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 goto error;
Christoph Hellwig558e6892010-04-28 12:28:58 +00001107 imap_valid = xfs_imap_valid(inode, &imap,
1108 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
Christoph Hellwig558e6892010-04-28 12:28:58 +00001110 if (imap_valid) {
Christoph Hellwig207d0412010-04-28 12:28:56 +00001111 xfs_map_at_offset(inode, bh, &imap, offset);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001112 xfs_add_to_ioend(inode, bh, offset, type,
1113 &ioend, new_ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001114 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001116 } else if (buffer_uptodate(bh)) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001117 /*
1118 * we got here because the buffer is already mapped.
1119 * That means it must already have extents allocated
1120 * underneath it. Map the extent by reading it.
1121 */
Christoph Hellwig85da94c2010-12-10 08:42:16 +00001122 if (flags != BMAPI_READ) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001123 flags = BMAPI_READ;
Christoph Hellwig85da94c2010-12-10 08:42:16 +00001124 imap_valid = 0;
1125 }
1126 if (!imap_valid) {
Christoph Hellwig6ac72482010-12-10 08:42:18 +00001127 new_ioend = 1;
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001128 size = xfs_probe_cluster(inode, page, bh, head);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001129 err = xfs_map_blocks(inode, offset, size,
Christoph Hellwig207d0412010-04-28 12:28:56 +00001130 &imap, flags);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001131 if (err)
1132 goto error;
Christoph Hellwig558e6892010-04-28 12:28:58 +00001133 imap_valid = xfs_imap_valid(inode, &imap,
1134 offset);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
David Chinnerdf3c7242007-05-24 15:27:03 +10001137 /*
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001138 * We set the type to IO_NEW in case we are doing a
David Chinnerdf3c7242007-05-24 15:27:03 +10001139 * small write at EOF that is extending the file but
1140 * without needing an allocation. We need to update the
1141 * file size on I/O completion in this case so it is
1142 * the same case as having just allocated a new extent
1143 * that we are writing into for the first time.
1144 */
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001145 type = IO_NEW;
Christoph Hellwig6ac72482010-12-10 08:42:18 +00001146 if (imap_valid) {
1147 all_bh = 1;
1148 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001149 xfs_add_to_ioend(inode, bh, offset, type,
Christoph Hellwig6ac72482010-12-10 08:42:18 +00001150 &ioend, new_ioend);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001151 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001153 } else if (PageUptodate(page)) {
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001154 ASSERT(buffer_mapped(bh));
Christoph Hellwig558e6892010-04-28 12:28:58 +00001155 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001157
1158 if (!iohead)
1159 iohead = ioend;
1160
1161 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
1163 if (uptodate && bh == head)
1164 SetPageUptodate(page);
1165
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001166 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Christoph Hellwig558e6892010-04-28 12:28:58 +00001168 if (ioend && imap_valid) {
Christoph Hellwigbd1556a2010-04-28 12:29:00 +00001169 xfs_off_t end_index;
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001170
Christoph Hellwigbd1556a2010-04-28 12:29:00 +00001171 end_index = imap.br_startoff + imap.br_blockcount;
1172
1173 /* to bytes */
1174 end_index <<= inode->i_blkbits;
1175
1176 /* to pages */
1177 end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
1178
1179 /* check against file size */
1180 if (end_index > last_index)
1181 end_index = last_index;
1182
Christoph Hellwig207d0412010-04-28 12:28:56 +00001183 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001184 wbc, all_bh, end_index);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001187 if (iohead)
Christoph Hellwig06342cf2009-10-30 09:09:15 +00001188 xfs_submit_ioend(wbc, iohead);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001189
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001190 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001193 if (iohead)
1194 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001196 if (err == -EAGAIN)
1197 goto redirty;
1198
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001199 xfs_aops_discard_page(page);
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001200 ClearPageUptodate(page);
1201 unlock_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return err;
Nathan Scottf51623b2006-03-14 13:26:27 +11001203
Christoph Hellwigb5420f22010-08-24 11:47:51 +10001204redirty:
Nathan Scottf51623b2006-03-14 13:26:27 +11001205 redirty_page_for_writepage(wbc, page);
1206 unlock_page(page);
1207 return 0;
Nathan Scottf51623b2006-03-14 13:26:27 +11001208}
1209
Nathan Scott7d4fb402006-06-09 15:27:16 +10001210STATIC int
1211xfs_vm_writepages(
1212 struct address_space *mapping,
1213 struct writeback_control *wbc)
1214{
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001215 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001216 return generic_writepages(mapping, wbc);
1217}
1218
Nathan Scottf51623b2006-03-14 13:26:27 +11001219/*
1220 * Called to move a page into cleanable state - and from there
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001221 * to be released. The page should already be clean. We always
Nathan Scottf51623b2006-03-14 13:26:27 +11001222 * have buffer heads in this call.
1223 *
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001224 * Returns 1 if the page is ok to release, 0 otherwise.
Nathan Scottf51623b2006-03-14 13:26:27 +11001225 */
1226STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001227xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001228 struct page *page,
1229 gfp_t gfp_mask)
1230{
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001231 int delalloc, unwritten;
Nathan Scottf51623b2006-03-14 13:26:27 +11001232
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001233 trace_xfs_releasepage(page->mapping->host, page, 0);
Nathan Scott238f4c52006-03-17 17:26:25 +11001234
Christoph Hellwig20cb52e2010-06-24 09:46:01 +10001235 xfs_count_page_state(page, &delalloc, &unwritten);
Nathan Scottf51623b2006-03-14 13:26:27 +11001236
Christoph Hellwig89f3b3632010-06-24 09:45:48 +10001237 if (WARN_ON(delalloc))
1238 return 0;
1239 if (WARN_ON(unwritten))
Nathan Scottf51623b2006-03-14 13:26:27 +11001240 return 0;
1241
Nathan Scottf51623b2006-03-14 13:26:27 +11001242 return try_to_free_buffers(page);
1243}
1244
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001246__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 struct inode *inode,
1248 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 struct buffer_head *bh_result,
1250 int create,
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001251 int direct)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001253 int flags = create ? BMAPI_WRITE : BMAPI_READ;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001254 struct xfs_bmbt_irec imap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001255 xfs_off_t offset;
1256 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001257 int nimap = 1;
1258 int new = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001261 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001262 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1263 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001264
1265 if (!create && direct && offset >= i_size_read(inode))
1266 return 0;
1267
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001268 if (direct && create)
1269 flags |= BMAPI_DIRECT;
1270
1271 error = xfs_iomap(XFS_I(inode), offset, size, flags, &imap, &nimap,
1272 &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (error)
1274 return -error;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001275 if (nimap == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 return 0;
1277
Christoph Hellwig207d0412010-04-28 12:28:56 +00001278 if (imap.br_startblock != HOLESTARTBLOCK &&
1279 imap.br_startblock != DELAYSTARTBLOCK) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001280 /*
1281 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 * the read case (treat as if we're reading into a hole).
1283 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001284 if (create || !ISUNWRITTEN(&imap))
1285 xfs_map_buffer(inode, bh_result, &imap, offset);
1286 if (create && ISUNWRITTEN(&imap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (direct)
1288 bh_result->b_private = inode;
1289 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
1291 }
1292
Nathan Scottc2536662006-03-29 10:44:40 +10001293 /*
1294 * If this is a realtime file, data may be on a different device.
1295 * to that pointed to from the buffer_head b_bdev currently.
1296 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001297 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Nathan Scottc2536662006-03-29 10:44:40 +10001299 /*
David Chinner549054a2007-02-10 18:36:35 +11001300 * If we previously allocated a block out beyond eof and we are now
1301 * coming back to use it then we will need to flag it as new even if it
1302 * has a disk address.
1303 *
1304 * With sub-block writes into unwritten extents we also need to mark
1305 * the buffer as new so that the unwritten parts of the buffer gets
1306 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
1308 if (create &&
1309 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001310 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001311 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Christoph Hellwig207d0412010-04-28 12:28:56 +00001314 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 BUG_ON(direct);
1316 if (create) {
1317 set_buffer_uptodate(bh_result);
1318 set_buffer_mapped(bh_result);
1319 set_buffer_delay(bh_result);
1320 }
1321 }
1322
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001323 /*
1324 * If this is O_DIRECT or the mpage code calling tell them how large
1325 * the mapping is, so that we can avoid repeated get_blocks calls.
1326 */
Nathan Scottc2536662006-03-29 10:44:40 +10001327 if (direct || size > (1 << inode->i_blkbits)) {
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001328 xfs_off_t mapping_size;
Christoph Hellwig9563b3d2010-04-28 12:28:53 +00001329
Christoph Hellwig2b8f12b2010-04-28 12:28:59 +00001330 mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
1331 mapping_size <<= inode->i_blkbits;
1332
1333 ASSERT(mapping_size > 0);
1334 if (mapping_size > size)
1335 mapping_size = size;
1336 if (mapping_size > LONG_MAX)
1337 mapping_size = LONG_MAX;
1338
1339 bh_result->b_size = mapping_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341
1342 return 0;
1343}
1344
1345int
Nathan Scottc2536662006-03-29 10:44:40 +10001346xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct inode *inode,
1348 sector_t iblock,
1349 struct buffer_head *bh_result,
1350 int create)
1351{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001352 return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353}
1354
1355STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001356xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 struct inode *inode,
1358 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct buffer_head *bh_result,
1360 int create)
1361{
Christoph Hellwigf2bde9b2010-06-24 11:44:35 +10001362 return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Christoph Hellwig209fb872010-07-18 21:17:11 +00001365/*
1366 * Complete a direct I/O write request.
1367 *
1368 * If the private argument is non-NULL __xfs_get_blocks signals us that we
1369 * need to issue a transaction to convert the range from unwritten to written
1370 * extents. In case this is regular synchronous I/O we just call xfs_end_io
1371 * to do this and we are done. But in case this was a successfull AIO
1372 * request this handler is called from interrupt context, from which we
1373 * can't start transactions. In that case offload the I/O completion to
1374 * the workqueues we also use for buffered I/O completion.
1375 */
Christoph Hellwigf0973862005-09-05 08:22:52 +10001376STATIC void
Christoph Hellwig209fb872010-07-18 21:17:11 +00001377xfs_end_io_direct_write(
1378 struct kiocb *iocb,
1379 loff_t offset,
1380 ssize_t size,
1381 void *private,
1382 int ret,
1383 bool is_async)
Christoph Hellwigf0973862005-09-05 08:22:52 +10001384{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001385 struct xfs_ioend *ioend = iocb->private;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001386
1387 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001388 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001389 * completion handler was called. Thus we need to protect
1390 * against double-freeing.
1391 */
1392 iocb->private = NULL;
Christoph Hellwig40e2e972010-07-18 21:17:09 +00001393
Christoph Hellwig209fb872010-07-18 21:17:11 +00001394 ioend->io_offset = offset;
1395 ioend->io_size = size;
1396 if (private && size > 0)
1397 ioend->io_type = IO_UNWRITTEN;
1398
1399 if (is_async) {
1400 /*
1401 * If we are converting an unwritten extent we need to delay
1402 * the AIO completion until after the unwrittent extent
1403 * conversion has completed, otherwise do it ASAP.
1404 */
1405 if (ioend->io_type == IO_UNWRITTEN) {
1406 ioend->io_iocb = iocb;
1407 ioend->io_result = ret;
1408 } else {
1409 aio_complete(iocb, ret, 0);
1410 }
1411 xfs_finish_ioend(ioend);
1412 } else {
1413 xfs_finish_ioend_sync(ioend);
1414 }
Christoph Hellwigf0973862005-09-05 08:22:52 +10001415}
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001418xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 int rw,
1420 struct kiocb *iocb,
1421 const struct iovec *iov,
1422 loff_t offset,
1423 unsigned long nr_segs)
1424{
Christoph Hellwig209fb872010-07-18 21:17:11 +00001425 struct inode *inode = iocb->ki_filp->f_mapping->host;
1426 struct block_device *bdev = xfs_find_bdev_for_inode(inode);
1427 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Christoph Hellwig209fb872010-07-18 21:17:11 +00001429 if (rw & WRITE) {
1430 iocb->private = xfs_alloc_ioend(inode, IO_NEW);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001432 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1433 offset, nr_segs,
1434 xfs_get_blocks_direct,
1435 xfs_end_io_direct_write, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001436 if (ret != -EIOCBQUEUED && iocb->private)
1437 xfs_destroy_ioend(iocb->private);
1438 } else {
Christoph Hellwigeafdc7d2010-06-04 11:29:53 +02001439 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1440 offset, nr_segs,
1441 xfs_get_blocks_direct,
1442 NULL, NULL, 0);
Christoph Hellwig209fb872010-07-18 21:17:11 +00001443 }
Christoph Hellwig5fe878ae2009-12-15 16:47:50 -08001444
Christoph Hellwigf0973862005-09-05 08:22:52 +10001445 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446}
1447
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001448STATIC void
1449xfs_vm_write_failed(
1450 struct address_space *mapping,
1451 loff_t to)
1452{
1453 struct inode *inode = mapping->host;
1454
1455 if (to > inode->i_size) {
Dave Chinnerc726de42010-11-30 15:14:39 +11001456 /*
1457 * punch out the delalloc blocks we have already allocated. We
1458 * don't call xfs_setattr() to do this as we may be in the
1459 * middle of a multi-iovec write and so the vfs inode->i_size
1460 * will not match the xfs ip->i_size and so it will zero too
1461 * much. Hence we jus truncate the page cache to zero what is
1462 * necessary and punch the delalloc blocks directly.
1463 */
1464 struct xfs_inode *ip = XFS_I(inode);
1465 xfs_fileoff_t start_fsb;
1466 xfs_fileoff_t end_fsb;
1467 int error;
1468
1469 truncate_pagecache(inode, to, inode->i_size);
1470
1471 /*
1472 * Check if there are any blocks that are outside of i_size
1473 * that need to be trimmed back.
1474 */
1475 start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
1476 end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
1477 if (end_fsb <= start_fsb)
1478 return;
1479
1480 xfs_ilock(ip, XFS_ILOCK_EXCL);
1481 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1482 end_fsb - start_fsb);
1483 if (error) {
1484 /* something screwed, just bail */
1485 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1486 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
1487 "xfs_vm_write_failed: unable to clean up ino %lld",
1488 ip->i_ino);
1489 }
1490 }
1491 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001492 }
1493}
1494
Nathan Scottf51623b2006-03-14 13:26:27 +11001495STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001496xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001497 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001498 struct address_space *mapping,
1499 loff_t pos,
1500 unsigned len,
1501 unsigned flags,
1502 struct page **pagep,
1503 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001504{
Christoph Hellwig155130a2010-06-04 11:29:58 +02001505 int ret;
1506
1507 ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
1508 pagep, xfs_get_blocks);
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001509 if (unlikely(ret))
1510 xfs_vm_write_failed(mapping, pos + len);
1511 return ret;
1512}
Christoph Hellwig155130a2010-06-04 11:29:58 +02001513
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001514STATIC int
1515xfs_vm_write_end(
1516 struct file *file,
1517 struct address_space *mapping,
1518 loff_t pos,
1519 unsigned len,
1520 unsigned copied,
1521 struct page *page,
1522 void *fsdata)
1523{
1524 int ret;
1525
1526 ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
1527 if (unlikely(ret < len))
1528 xfs_vm_write_failed(mapping, pos + len);
Christoph Hellwig155130a2010-06-04 11:29:58 +02001529 return ret;
Nathan Scottf51623b2006-03-14 13:26:27 +11001530}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001533xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 struct address_space *mapping,
1535 sector_t block)
1536{
1537 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001538 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539
Christoph Hellwigcca28fb2010-06-24 11:57:09 +10001540 trace_xfs_vm_bmap(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001541 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001542 xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001543 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001544 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
1547STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001548xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 struct file *unused,
1550 struct page *page)
1551{
Nathan Scottc2536662006-03-29 10:44:40 +10001552 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553}
1554
1555STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001556xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 struct file *unused,
1558 struct address_space *mapping,
1559 struct list_head *pages,
1560 unsigned nr_pages)
1561{
Nathan Scottc2536662006-03-29 10:44:40 +10001562 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001565const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001566 .readpage = xfs_vm_readpage,
1567 .readpages = xfs_vm_readpages,
1568 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001569 .writepages = xfs_vm_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 .sync_page = block_sync_page,
Nathan Scott238f4c52006-03-17 17:26:25 +11001571 .releasepage = xfs_vm_releasepage,
1572 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001573 .write_begin = xfs_vm_write_begin,
Christoph Hellwigfa9b2272010-06-14 05:17:31 -04001574 .write_end = xfs_vm_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001575 .bmap = xfs_vm_bmap,
1576 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001577 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001578 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001579 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580};