blob: 6feecd27947048f5d074b9bf86d5c94add89142c [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_dir2.h"
25#include "xfs_trans.h"
26#include "xfs_dmapi.h"
27#include "xfs_mount.h"
28#include "xfs_bmap_btree.h"
29#include "xfs_alloc_btree.h"
30#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110032#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include "xfs_dinode.h"
34#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110035#include "xfs_alloc.h"
36#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_error.h"
38#include "xfs_rw.h"
39#include "xfs_iomap.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100040#include "xfs_vnodeops.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000041#include "xfs_trace.h"
Dave Chinner3ed3a432010-03-05 02:00:42 +000042#include "xfs_bmap.h"
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110045#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/writeback.h>
47
Christoph Hellwig34a52c62010-04-28 12:28:57 +000048/*
49 * Types of I/O for bmap clustering and I/O completion tracking.
50 */
51enum {
52 IO_READ, /* mapping for a read */
53 IO_DELAY, /* mapping covers delalloc region */
54 IO_UNWRITTEN, /* mapping covers allocated but uninitialized data */
55 IO_NEW /* just allocated */
56};
Christoph Hellwig25e41b32008-12-03 12:20:39 +010057
58/*
59 * Prime number of hash buckets since address is used as the key.
60 */
61#define NVSYNC 37
62#define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
63static wait_queue_head_t xfs_ioend_wq[NVSYNC];
64
65void __init
66xfs_ioend_init(void)
67{
68 int i;
69
70 for (i = 0; i < NVSYNC; i++)
71 init_waitqueue_head(&xfs_ioend_wq[i]);
72}
73
74void
75xfs_ioend_wait(
76 xfs_inode_t *ip)
77{
78 wait_queue_head_t *wq = to_ioend_wq(ip);
79
80 wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
81}
82
83STATIC void
84xfs_ioend_wake(
85 xfs_inode_t *ip)
86{
87 if (atomic_dec_and_test(&ip->i_iocount))
88 wake_up(to_ioend_wq(ip));
89}
90
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000091void
Nathan Scottf51623b2006-03-14 13:26:27 +110092xfs_count_page_state(
93 struct page *page,
94 int *delalloc,
95 int *unmapped,
96 int *unwritten)
97{
98 struct buffer_head *bh, *head;
99
100 *delalloc = *unmapped = *unwritten = 0;
101
102 bh = head = page_buffers(page);
103 do {
104 if (buffer_uptodate(bh) && !buffer_mapped(bh))
105 (*unmapped) = 1;
Nathan Scottf51623b2006-03-14 13:26:27 +1100106 else if (buffer_unwritten(bh))
107 (*unwritten) = 1;
108 else if (buffer_delay(bh))
109 (*delalloc) = 1;
110 } while ((bh = bh->b_this_page) != head);
111}
112
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000113STATIC struct block_device *
114xfs_find_bdev_for_inode(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000115 struct inode *inode)
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000116{
Christoph Hellwig046f1682010-04-28 12:28:52 +0000117 struct xfs_inode *ip = XFS_I(inode);
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000118 struct xfs_mount *mp = ip->i_mount;
119
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100120 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000121 return mp->m_rtdev_targp->bt_bdev;
122 else
123 return mp->m_ddev_targp->bt_bdev;
124}
125
Christoph Hellwig0829c362005-09-02 16:58:49 +1000126/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100127 * We're now finished for good with this ioend structure.
128 * Update the page state via the associated buffer_heads,
129 * release holds on the inode and bio, and finally free
130 * up memory. Do not use the ioend after this.
131 */
Christoph Hellwig0829c362005-09-02 16:58:49 +1000132STATIC void
133xfs_destroy_ioend(
134 xfs_ioend_t *ioend)
135{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100136 struct buffer_head *bh, *next;
Christoph Hellwig583fa582008-12-03 12:20:38 +0100137 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100138
139 for (bh = ioend->io_buffer_head; bh; bh = next) {
140 next = bh->b_private;
Nathan Scott7d04a332006-06-09 14:58:38 +1000141 bh->b_end_io(bh, !ioend->io_error);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100142 }
Christoph Hellwig583fa582008-12-03 12:20:38 +0100143
144 /*
145 * Volume managers supporting multiple paths can send back ENODEV
146 * when the final path disappears. In this case continuing to fill
147 * the page cache with dirty data which cannot be written out is
148 * evil, so prevent that.
149 */
150 if (unlikely(ioend->io_error == -ENODEV)) {
151 xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
152 __FILE__, __LINE__);
Christoph Hellwigb677c212007-08-29 11:46:28 +1000153 }
Christoph Hellwig583fa582008-12-03 12:20:38 +0100154
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100155 xfs_ioend_wake(ip);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000156 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159/*
Dave Chinner932640e2009-10-06 20:29:29 +0000160 * If the end of the current ioend is beyond the current EOF,
161 * return the new EOF value, otherwise zero.
162 */
163STATIC xfs_fsize_t
164xfs_ioend_new_eof(
165 xfs_ioend_t *ioend)
166{
167 xfs_inode_t *ip = XFS_I(ioend->io_inode);
168 xfs_fsize_t isize;
169 xfs_fsize_t bsize;
170
171 bsize = ioend->io_offset + ioend->io_size;
172 isize = MAX(ip->i_size, ip->i_new_size);
173 isize = MIN(isize, bsize);
174 return isize > ip->i_d.di_size ? isize : 0;
175}
176
177/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000178 * Update on-disk file size now that data has been written to disk. The
179 * current in-memory file size is i_size. If a write is beyond eof i_new_size
180 * will be the intended file size until i_size is updated. If this write does
181 * not extend all the way to the valid file size then restrict this update to
182 * the end of the write.
183 *
184 * This function does not block as blocking on the inode lock in IO completion
185 * can lead to IO completion order dependency deadlocks.. If it can't get the
186 * inode ilock it will return EAGAIN. Callers must handle this.
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000187 */
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000188STATIC int
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000189xfs_setfilesize(
190 xfs_ioend_t *ioend)
191{
Christoph Hellwigb677c212007-08-29 11:46:28 +1000192 xfs_inode_t *ip = XFS_I(ioend->io_inode);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000193 xfs_fsize_t isize;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000194
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000195 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000196 ASSERT(ioend->io_type != IO_READ);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000197
198 if (unlikely(ioend->io_error))
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000199 return 0;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000200
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000201 if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
202 return EAGAIN;
203
Dave Chinner932640e2009-10-06 20:29:29 +0000204 isize = xfs_ioend_new_eof(ioend);
205 if (isize) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000206 ip->i_d.di_size = isize;
Christoph Hellwig66d834e2010-02-15 09:44:49 +0000207 xfs_mark_inode_dirty(ip);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000208 }
209
210 xfs_iunlock(ip, XFS_ILOCK_EXCL);
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000211 return 0;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000212}
213
214/*
Dave Chinnerc626d172009-04-06 18:42:11 +0200215 * Schedule IO completion handling on a xfsdatad if this was
216 * the final hold on this ioend. If we are asked to wait,
217 * flush the workqueue.
218 */
219STATIC void
220xfs_finish_ioend(
221 xfs_ioend_t *ioend,
222 int wait)
223{
224 if (atomic_dec_and_test(&ioend->io_remaining)) {
Christoph Hellwig5ec4fab2009-10-30 09:11:47 +0000225 struct workqueue_struct *wq;
Dave Chinnerc626d172009-04-06 18:42:11 +0200226
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000227 wq = (ioend->io_type == IO_UNWRITTEN) ?
Christoph Hellwig5ec4fab2009-10-30 09:11:47 +0000228 xfsconvertd_workqueue : xfsdatad_workqueue;
Dave Chinnerc626d172009-04-06 18:42:11 +0200229 queue_work(wq, &ioend->io_work);
230 if (wait)
231 flush_workqueue(wq);
232 }
233}
234
235/*
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000236 * IO write completion.
237 */
238STATIC void
239xfs_end_io(
240 struct work_struct *work)
241{
242 xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
243 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Dave Chinner69418932010-03-04 00:57:09 +0000244 int error = 0;
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000245
246 /*
247 * For unwritten extents we need to issue transactions to convert a
248 * range to normal written extens after the data I/O has finished.
249 */
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000250 if (ioend->io_type == IO_UNWRITTEN &&
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000251 likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
252
253 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
254 ioend->io_size);
255 if (error)
256 ioend->io_error = error;
257 }
258
259 /*
260 * We might have to update the on-disk file size after extending
261 * writes.
262 */
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000263 if (ioend->io_type != IO_READ) {
Dave Chinner77d7a0c2010-02-17 05:36:29 +0000264 error = xfs_setfilesize(ioend);
265 ASSERT(!error || error == EAGAIN);
266 }
267
268 /*
269 * If we didn't complete processing of the ioend, requeue it to the
270 * tail of the workqueue for another attempt later. Otherwise destroy
271 * it.
272 */
273 if (error == EAGAIN) {
274 atomic_inc(&ioend->io_remaining);
275 xfs_finish_ioend(ioend, 0);
276 /* ensure we don't spin on blocked ioends */
277 delay(1);
278 } else
279 xfs_destroy_ioend(ioend);
280}
281
282/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000283 * Allocate and initialise an IO completion structure.
284 * We need to track unwritten extent write completion here initially.
285 * We'll need to extend this for updating the ondisk inode size later
286 * (vs. incore size).
287 */
288STATIC xfs_ioend_t *
289xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100290 struct inode *inode,
291 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000292{
293 xfs_ioend_t *ioend;
294
295 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
296
297 /*
298 * Set the count to 1 initially, which will prevent an I/O
299 * completion callback from happening before we have started
300 * all the I/O from calling the completion routine too early.
301 */
302 atomic_set(&ioend->io_remaining, 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000303 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100304 ioend->io_list = NULL;
305 ioend->io_type = type;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000306 ioend->io_inode = inode;
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000307 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100308 ioend->io_buffer_tail = NULL;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000309 atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000310 ioend->io_offset = 0;
311 ioend->io_size = 0;
312
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
David Chinnere927af92007-06-05 16:24:36 +1000361 xfs_finish_ioend(ioend, 0);
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 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
384 bio_put(bio);
385}
386
387STATIC struct bio *
388xfs_alloc_ioend_bio(
389 struct buffer_head *bh)
390{
391 struct bio *bio;
392 int nvecs = bio_get_nr_vecs(bh->b_bdev);
393
394 do {
395 bio = bio_alloc(GFP_NOIO, nvecs);
396 nvecs >>= 1;
397 } while (!bio);
398
399 ASSERT(bio->bi_private == NULL);
400 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
401 bio->bi_bdev = bh->b_bdev;
402 bio_get(bio);
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,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100423 int clear_dirty,
424 int buffers)
425{
426 ASSERT(PageLocked(page));
427 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100428 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100429 clear_page_dirty_for_io(page);
430 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100431 unlock_page(page);
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700432 /* If no buffers on the page are to be written, finish it here */
433 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100434 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100435}
436
437static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
438{
439 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
440}
441
442/*
David Chinnerd88992f2006-01-18 13:38:12 +1100443 * Submit all of the bios for all of the ioends we have saved up, covering the
444 * initial writepage page and also any probed pages.
445 *
446 * Because we may have multiple ioends spanning a page, we need to start
447 * writeback on all the buffers before we submit them for I/O. If we mark the
448 * buffers as we got, then we can end up with a page that only has buffers
449 * marked async write and I/O complete on can occur before we mark the other
450 * buffers async write.
451 *
452 * The end result of this is that we trip a bug in end_page_writeback() because
453 * we call it twice for the one page as the code in end_buffer_async_write()
454 * assumes that all buffers on the page are started at the same time.
455 *
456 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000457 * buffer_heads, and then submit them for I/O on the second pass.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100458 */
459STATIC void
460xfs_submit_ioend(
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000461 struct writeback_control *wbc,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100462 xfs_ioend_t *ioend)
463{
David Chinnerd88992f2006-01-18 13:38:12 +1100464 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100465 xfs_ioend_t *next;
466 struct buffer_head *bh;
467 struct bio *bio;
468 sector_t lastblock = 0;
469
David Chinnerd88992f2006-01-18 13:38:12 +1100470 /* Pass 1 - start writeback */
471 do {
472 next = ioend->io_list;
473 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
474 xfs_start_buffer_writeback(bh);
475 }
476 } while ((ioend = next) != NULL);
477
478 /* Pass 2 - submit I/O */
479 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100480 do {
481 next = ioend->io_list;
482 bio = NULL;
483
484 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100485
486 if (!bio) {
487 retry:
488 bio = xfs_alloc_ioend_bio(bh);
489 } else if (bh->b_blocknr != lastblock + 1) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000490 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100491 goto retry;
492 }
493
494 if (bio_add_buffer(bio, bh) != bh->b_size) {
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000495 xfs_submit_ioend_bio(wbc, ioend, bio);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100496 goto retry;
497 }
498
499 lastblock = bh->b_blocknr;
500 }
501 if (bio)
Christoph Hellwig06342cf2009-10-30 09:09:15 +0000502 xfs_submit_ioend_bio(wbc, ioend, bio);
David Chinnere927af92007-06-05 16:24:36 +1000503 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100504 } while ((ioend = next) != NULL);
505}
506
507/*
508 * Cancel submission of all buffer_heads so far in this endio.
509 * Toss the endio too. Only ever called for the initial page
510 * in a writepage request, so only ever one page.
511 */
512STATIC void
513xfs_cancel_ioend(
514 xfs_ioend_t *ioend)
515{
516 xfs_ioend_t *next;
517 struct buffer_head *bh, *next_bh;
518
519 do {
520 next = ioend->io_list;
521 bh = ioend->io_buffer_head;
522 do {
523 next_bh = bh->b_private;
524 clear_buffer_async_write(bh);
525 unlock_buffer(bh);
526 } while ((bh = next_bh) != NULL);
527
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100528 xfs_ioend_wake(XFS_I(ioend->io_inode));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100529 mempool_free(ioend, xfs_ioend_pool);
530 } while ((ioend = next) != NULL);
531}
532
533/*
534 * Test to see if we've been building up a completion structure for
535 * earlier buffers -- if so, we try to append to this ioend if we
536 * can, otherwise we finish off any current ioend and start another.
537 * Return true if we've finished the given ioend.
538 */
539STATIC void
540xfs_add_to_ioend(
541 struct inode *inode,
542 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100543 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100544 unsigned int type,
545 xfs_ioend_t **result,
546 int need_ioend)
547{
548 xfs_ioend_t *ioend = *result;
549
550 if (!ioend || need_ioend || type != ioend->io_type) {
551 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100552
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100553 ioend = xfs_alloc_ioend(inode, type);
554 ioend->io_offset = offset;
555 ioend->io_buffer_head = bh;
556 ioend->io_buffer_tail = bh;
557 if (previous)
558 previous->io_list = ioend;
559 *result = ioend;
560 } else {
561 ioend->io_buffer_tail->b_private = bh;
562 ioend->io_buffer_tail = bh;
563 }
564
565 bh->b_private = NULL;
566 ioend->io_size += bh->b_size;
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100570xfs_map_buffer(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000571 struct inode *inode,
Nathan Scott87cbc492006-03-14 13:26:43 +1100572 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000573 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000574 xfs_off_t offset)
Nathan Scott87cbc492006-03-14 13:26:43 +1100575{
576 sector_t bn;
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000577 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +0000578 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
579 xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
Nathan Scott87cbc492006-03-14 13:26:43 +1100580
Christoph Hellwig207d0412010-04-28 12:28:56 +0000581 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
582 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Nathan Scott87cbc492006-03-14 13:26:43 +1100583
Christoph Hellwige5131822010-04-28 12:28:55 +0000584 bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
Christoph Hellwig8699bb02010-04-28 12:28:54 +0000585 ((offset - iomap_offset) >> inode->i_blkbits);
Nathan Scott87cbc492006-03-14 13:26:43 +1100586
Christoph Hellwig046f1682010-04-28 12:28:52 +0000587 ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
Nathan Scott87cbc492006-03-14 13:26:43 +1100588
589 bh->b_blocknr = bn;
590 set_buffer_mapped(bh);
591}
592
593STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594xfs_map_at_offset(
Christoph Hellwig046f1682010-04-28 12:28:52 +0000595 struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct buffer_head *bh,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000597 struct xfs_bmbt_irec *imap,
Christoph Hellwig046f1682010-04-28 12:28:52 +0000598 xfs_off_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599{
Christoph Hellwig207d0412010-04-28 12:28:56 +0000600 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
601 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 lock_buffer(bh);
Christoph Hellwig207d0412010-04-28 12:28:56 +0000604 xfs_map_buffer(inode, bh, imap, offset);
Christoph Hellwig046f1682010-04-28 12:28:52 +0000605 bh->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 set_buffer_mapped(bh);
607 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100608 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100612 * Look for a page at index that is suitable for clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 */
614STATIC unsigned int
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100615xfs_probe_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100616 struct page *page,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100617 unsigned int pg_offset,
618 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int ret = 0;
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 if (page->mapping && PageDirty(page)) {
626 if (page_has_buffers(page)) {
627 struct buffer_head *bh, *head;
628
629 bh = head = page_buffers(page);
630 do {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100631 if (!buffer_uptodate(bh))
632 break;
633 if (mapped != buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 break;
635 ret += bh->b_size;
636 if (ret >= pg_offset)
637 break;
638 } while ((bh = bh->b_this_page) != head);
639 } else
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100640 ret = mapped ? 0 : PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return ret;
644}
645
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100646STATIC size_t
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100647xfs_probe_cluster(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct inode *inode,
649 struct page *startpage,
650 struct buffer_head *bh,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100651 struct buffer_head *head,
652 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100654 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100656 size_t total = 0;
657 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* First sum forwards in this page */
660 do {
Eric Sandeen2353e8e2006-02-28 12:30:30 +1100661 if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100662 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 total += bh->b_size;
664 } while ((bh = bh->b_this_page) != head);
665
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100666 /* if we reached the end of the page, sum forwards in following pages */
667 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
668 tindex = startpage->index + 1;
669
670 /* Prune this back to avoid pathological behavior */
671 tloff = min(tlast, startpage->index + 64);
672
673 pagevec_init(&pvec, 0);
674 while (!done && tindex <= tloff) {
675 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
676
677 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
678 break;
679
680 for (i = 0; i < pagevec_count(&pvec); i++) {
681 struct page *page = pvec.pages[i];
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000682 size_t pg_offset, pg_len = 0;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100683
684 if (tindex == tlast) {
685 pg_offset =
686 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100687 if (!pg_offset) {
688 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100689 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100690 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100691 } else
692 pg_offset = PAGE_CACHE_SIZE;
693
Nick Piggin529ae9a2008-08-02 12:01:03 +0200694 if (page->index == tindex && trylock_page(page)) {
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000695 pg_len = xfs_probe_page(page, pg_offset, mapped);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100696 unlock_page(page);
697 }
698
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000699 if (!pg_len) {
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100700 done = 1;
701 break;
702 }
703
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000704 total += pg_len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100705 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100707
708 pagevec_release(&pvec);
709 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return total;
713}
714
715/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100716 * Test if a given page is suitable for writing as part of an unwritten
717 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100719STATIC int
720xfs_is_delayed_page(
721 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100722 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100725 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (page->mapping && page_has_buffers(page)) {
728 struct buffer_head *bh, *head;
729 int acceptable = 0;
730
731 bh = head = page_buffers(page);
732 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100733 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000734 acceptable = (type == IO_UNWRITTEN);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100735 else if (buffer_delay(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000736 acceptable = (type == IO_DELAY);
David Chinner2ddee842006-03-22 12:47:40 +1100737 else if (buffer_dirty(bh) && buffer_mapped(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000738 acceptable = (type == IO_NEW);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100739 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 } while ((bh = bh->b_this_page) != head);
742
743 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100744 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100747 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/*
751 * Allocate & map buffers for page given the extent map. Write it out.
752 * except for the original page of a writepage, this is called on
753 * delalloc/unwritten pages only, for the original page it is possible
754 * that the page has no mapping at all.
755 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100756STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757xfs_convert_page(
758 struct inode *inode,
759 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100760 loff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000761 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100762 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 int startio,
765 int all_bh)
766{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100767 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100768 xfs_off_t end_offset;
769 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100770 unsigned int type;
Nathan Scott24e17b52005-05-05 13:33:20 -0700771 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100772 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100773 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100775 if (page->index != tindex)
776 goto fail;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200777 if (!trylock_page(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100778 goto fail;
779 if (PageWriteback(page))
780 goto fail_unlock_page;
781 if (page->mapping != inode->i_mapping)
782 goto fail_unlock_page;
783 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
784 goto fail_unlock_page;
785
Nathan Scott24e17b52005-05-05 13:33:20 -0700786 /*
787 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000788 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100789 *
790 * Derivation:
791 *
792 * End offset is the highest offset that this page should represent.
793 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
794 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
795 * hence give us the correct page_dirty count. On any other page,
796 * it will be zero and in that case we need page_dirty to be the
797 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700798 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100799 end_offset = min_t(unsigned long long,
800 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
801 i_size_read(inode));
802
Nathan Scott24e17b52005-05-05 13:33:20 -0700803 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100804 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
805 PAGE_CACHE_SIZE);
806 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
807 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 bh = head = page_buffers(page);
810 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100811 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100813 if (!buffer_uptodate(bh))
814 uptodate = 0;
815 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
816 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100818 }
819
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100820 if (buffer_unwritten(bh) || buffer_delay(bh)) {
821 if (buffer_unwritten(bh))
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000822 type = IO_UNWRITTEN;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100823 else
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000824 type = IO_DELAY;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100825
Christoph Hellwig558e6892010-04-28 12:28:58 +0000826 if (!xfs_imap_valid(inode, imap, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100827 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100828 continue;
829 }
830
Christoph Hellwig207d0412010-04-28 12:28:56 +0000831 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
832 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100833
Christoph Hellwig207d0412010-04-28 12:28:56 +0000834 xfs_map_at_offset(inode, bh, imap, offset);
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100835 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100836 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100837 type, ioendp, done);
838 } else {
839 set_buffer_dirty(bh);
840 unlock_buffer(bh);
841 mark_buffer_dirty(bh);
842 }
843 page_dirty--;
844 count++;
845 } else {
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000846 type = IO_NEW;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100847 if (buffer_mapped(bh) && all_bh && startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100849 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100850 type, ioendp, done);
851 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700852 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100853 } else {
854 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100857 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100859 if (uptodate && bh == head)
860 SetPageUptodate(page);
861
862 if (startio) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100863 if (count) {
David Chinner9fddaca2006-02-07 20:27:24 +1100864 wbc->nr_to_write--;
Wu Fengguang0d995192009-12-03 13:54:25 +0100865 if (wbc->nr_to_write <= 0)
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100866 done = 1;
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100867 }
Denys Vlasenkob41759c2008-05-19 16:34:11 +1000868 xfs_start_page_writeback(page, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100870
871 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100872 fail_unlock_page:
873 unlock_page(page);
874 fail:
875 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876}
877
878/*
879 * Convert & write out a cluster of pages in the same extent as defined
880 * by mp and following the start page.
881 */
882STATIC void
883xfs_cluster_write(
884 struct inode *inode,
885 pgoff_t tindex,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000886 struct xfs_bmbt_irec *imap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100887 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct writeback_control *wbc,
889 int startio,
890 int all_bh,
891 pgoff_t tlast)
892{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100893 struct pagevec pvec;
894 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100896 pagevec_init(&pvec, 0);
897 while (!done && tindex <= tlast) {
898 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
899
900 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100902
903 for (i = 0; i < pagevec_count(&pvec); i++) {
904 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
Christoph Hellwig207d0412010-04-28 12:28:56 +0000905 imap, ioendp, wbc, startio, all_bh);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100906 if (done)
907 break;
908 }
909
910 pagevec_release(&pvec);
911 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
913}
914
Dave Chinner3ed3a432010-03-05 02:00:42 +0000915STATIC void
916xfs_vm_invalidatepage(
917 struct page *page,
918 unsigned long offset)
919{
920 trace_xfs_invalidatepage(page->mapping->host, page, offset);
921 block_invalidatepage(page, offset);
922}
923
924/*
925 * If the page has delalloc buffers on it, we need to punch them out before we
926 * invalidate the page. If we don't, we leave a stale delalloc mapping on the
927 * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
928 * is done on that same region - the delalloc extent is returned when none is
929 * supposed to be there.
930 *
931 * We prevent this by truncating away the delalloc regions on the page before
932 * invalidating it. Because they are delalloc, we can do this without needing a
933 * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
934 * truncation without a transaction as there is no space left for block
935 * reservation (typically why we see a ENOSPC in writeback).
936 *
937 * This is not a performance critical path, so for now just do the punching a
938 * buffer head at a time.
939 */
940STATIC void
941xfs_aops_discard_page(
942 struct page *page)
943{
944 struct inode *inode = page->mapping->host;
945 struct xfs_inode *ip = XFS_I(inode);
946 struct buffer_head *bh, *head;
947 loff_t offset = page_offset(page);
948 ssize_t len = 1 << inode->i_blkbits;
949
Christoph Hellwig34a52c62010-04-28 12:28:57 +0000950 if (!xfs_is_delayed_page(page, IO_DELAY))
Dave Chinner3ed3a432010-03-05 02:00:42 +0000951 goto out_invalidate;
952
Dave Chinnere8c37532010-03-15 02:36:35 +0000953 if (XFS_FORCED_SHUTDOWN(ip->i_mount))
954 goto out_invalidate;
955
Dave Chinner3ed3a432010-03-05 02:00:42 +0000956 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
957 "page discard on page %p, inode 0x%llx, offset %llu.",
958 page, ip->i_ino, offset);
959
960 xfs_ilock(ip, XFS_ILOCK_EXCL);
961 bh = head = page_buffers(page);
962 do {
963 int done;
964 xfs_fileoff_t offset_fsb;
965 xfs_bmbt_irec_t imap;
966 int nimaps = 1;
967 int error;
968 xfs_fsblock_t firstblock;
969 xfs_bmap_free_t flist;
970
971 if (!buffer_delay(bh))
972 goto next_buffer;
973
974 offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
975
976 /*
977 * Map the range first and check that it is a delalloc extent
978 * before trying to unmap the range. Otherwise we will be
979 * trying to remove a real extent (which requires a
980 * transaction) or a hole, which is probably a bad idea...
981 */
982 error = xfs_bmapi(NULL, ip, offset_fsb, 1,
983 XFS_BMAPI_ENTIRE, NULL, 0, &imap,
984 &nimaps, NULL, NULL);
985
986 if (error) {
987 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +0000988 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
989 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
990 "page discard failed delalloc mapping lookup.");
991 }
Dave Chinner3ed3a432010-03-05 02:00:42 +0000992 break;
993 }
994 if (!nimaps) {
995 /* nothing there */
996 goto next_buffer;
997 }
998 if (imap.br_startblock != DELAYSTARTBLOCK) {
999 /* been converted, ignore */
1000 goto next_buffer;
1001 }
1002 WARN_ON(imap.br_blockcount == 0);
1003
1004 /*
1005 * Note: while we initialise the firstblock/flist pair, they
1006 * should never be used because blocks should never be
1007 * allocated or freed for a delalloc extent and hence we need
1008 * don't cancel or finish them after the xfs_bunmapi() call.
1009 */
1010 xfs_bmap_init(&flist, &firstblock);
1011 error = xfs_bunmapi(NULL, ip, offset_fsb, 1, 0, 1, &firstblock,
1012 &flist, NULL, &done);
1013
1014 ASSERT(!flist.xbf_count && !flist.xbf_first);
1015 if (error) {
1016 /* something screwed, just bail */
Dave Chinnere8c37532010-03-15 02:36:35 +00001017 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1018 xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
Dave Chinner3ed3a432010-03-05 02:00:42 +00001019 "page discard unable to remove delalloc mapping.");
Dave Chinnere8c37532010-03-15 02:36:35 +00001020 }
Dave Chinner3ed3a432010-03-05 02:00:42 +00001021 break;
1022 }
1023next_buffer:
1024 offset += len;
1025
1026 } while ((bh = bh->b_this_page) != head);
1027
1028 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1029out_invalidate:
1030 xfs_vm_invalidatepage(page, 0);
1031 return;
1032}
1033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034/*
1035 * Calling this without startio set means we are being asked to make a dirty
1036 * page ready for freeing it's buffers. When called with startio set then
1037 * we are coming from writepage.
1038 *
1039 * When called with startio set it is important that we write the WHOLE
1040 * page if possible.
1041 * The bh->b_state's cannot know if any of the blocks or which block for
1042 * that matter are dirty due to mmap writes, and therefore bh uptodate is
Nathan Scottc41564b2006-03-29 08:55:14 +10001043 * only valid if the page itself isn't completely uptodate. Some layers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 * may clear the page dirty flag prior to calling write page, under the
1045 * assumption the entire page will be written out; by not writing out the
1046 * whole page the page can be reused before all valid dirty data is
1047 * written out. Note: in the case of a page that has been dirty'd by
1048 * mapwrite and but partially setup by block_prepare_write the
1049 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
1050 * valid state, thus the whole page must be written out thing.
1051 */
1052
1053STATIC int
1054xfs_page_state_convert(
1055 struct inode *inode,
1056 struct page *page,
1057 struct writeback_control *wbc,
1058 int startio,
1059 int unmapped) /* also implies page uptodate */
1060{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001061 struct buffer_head *bh, *head;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001062 struct xfs_bmbt_irec imap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001063 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 loff_t offset;
1065 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001066 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 __uint64_t end_offset;
1068 pgoff_t end_index, last_index, tlast;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001069 ssize_t size, len;
Christoph Hellwig558e6892010-04-28 12:28:58 +00001070 int flags, err, imap_valid = 0, uptodate = 1;
Nathan Scott82721452006-04-11 15:10:55 +10001071 int page_dirty, count = 0;
1072 int trylock = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001073 int all_bh = unmapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Nathan Scott82721452006-04-11 15:10:55 +10001075 if (startio) {
1076 if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
1077 trylock |= BMAPI_TRYLOCK;
1078 }
Daniel Moore3ba08152005-05-05 13:31:34 -07001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 /* Is this page beyond the end of the file? */
1081 offset = i_size_read(inode);
1082 end_index = offset >> PAGE_CACHE_SHIFT;
1083 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
1084 if (page->index >= end_index) {
1085 if ((page->index >= end_index + 1) ||
1086 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +11001087 if (startio)
1088 unlock_page(page);
1089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091 }
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
Nathan Scott24e17b52005-05-05 13:33:20 -07001094 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +10001095 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001096 *
1097 * Derivation:
1098 *
1099 * End offset is the highest offset that this page should represent.
1100 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
1101 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
1102 * hence give us the correct page_dirty count. On any other page,
1103 * it will be zero and in that case we need page_dirty to be the
1104 * count of buffers on the page.
1105 */
1106 end_offset = min_t(unsigned long long,
1107 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -07001108 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001109 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
1110 PAGE_CACHE_SIZE);
1111 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -07001112 page_dirty = p_offset / len;
1113
Nathan Scott24e17b52005-05-05 13:33:20 -07001114 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001115 offset = page_offset(page);
David Chinnerdf3c7242007-05-24 15:27:03 +10001116 flags = BMAPI_READ;
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001117 type = IO_NEW;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001118
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001119 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 do {
1122 if (offset >= end_offset)
1123 break;
1124 if (!buffer_uptodate(bh))
1125 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001126 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001127 /*
1128 * the iomap is actually still valid, but the ioend
1129 * isn't. shouldn't happen too often.
1130 */
Christoph Hellwig558e6892010-04-28 12:28:58 +00001131 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Christoph Hellwig558e6892010-04-28 12:28:58 +00001135 if (imap_valid)
1136 imap_valid = xfs_imap_valid(inode, &imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
1138 /*
1139 * First case, map an unwritten extent and prepare for
1140 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001141 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 * Second case, allocate space for a delalloc buffer.
1143 * We can return EAGAIN here in the release page case.
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001144 *
1145 * Third case, an unmapped buffer was found, and we are
1146 * in a path where we need to write the whole page out.
David Chinnerdf3c7242007-05-24 15:27:03 +10001147 */
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001148 if (buffer_unwritten(bh) || buffer_delay(bh) ||
1149 ((buffer_uptodate(bh) || PageUptodate(page)) &&
1150 !buffer_mapped(bh) && (unmapped || startio))) {
David Chinnereffd1202007-06-18 16:49:58 +10001151 int new_ioend = 0;
1152
David Chinnerdf3c7242007-05-24 15:27:03 +10001153 /*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001154 * Make sure we don't use a read-only iomap
1155 */
David Chinnerdf3c7242007-05-24 15:27:03 +10001156 if (flags == BMAPI_READ)
Christoph Hellwig558e6892010-04-28 12:28:58 +00001157 imap_valid = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001158
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001159 if (buffer_unwritten(bh)) {
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001160 type = IO_UNWRITTEN;
Nathan Scott82721452006-04-11 15:10:55 +10001161 flags = BMAPI_WRITE | BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001162 } else if (buffer_delay(bh)) {
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001163 type = IO_DELAY;
Nathan Scott82721452006-04-11 15:10:55 +10001164 flags = BMAPI_ALLOCATE | trylock;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001165 } else {
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001166 type = IO_NEW;
Nathan Scott82721452006-04-11 15:10:55 +10001167 flags = BMAPI_WRITE | BMAPI_MMAP;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001168 }
1169
Christoph Hellwig558e6892010-04-28 12:28:58 +00001170 if (!imap_valid) {
David Chinnereffd1202007-06-18 16:49:58 +10001171 /*
1172 * if we didn't have a valid mapping then we
1173 * need to ensure that we put the new mapping
1174 * in a new ioend structure. This needs to be
1175 * done to ensure that the ioends correctly
1176 * reflect the block mappings at io completion
1177 * for unwritten extent conversion.
1178 */
1179 new_ioend = 1;
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001180 if (type == IO_NEW) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001181 size = xfs_probe_cluster(inode,
1182 page, bh, head, 0);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001183 } else {
1184 size = len;
1185 }
1186
1187 err = xfs_map_blocks(inode, offset, size,
Christoph Hellwig207d0412010-04-28 12:28:56 +00001188 &imap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001189 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 goto error;
Christoph Hellwig558e6892010-04-28 12:28:58 +00001191 imap_valid = xfs_imap_valid(inode, &imap,
1192 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 }
Christoph Hellwig558e6892010-04-28 12:28:58 +00001194 if (imap_valid) {
Christoph Hellwig207d0412010-04-28 12:28:56 +00001195 xfs_map_at_offset(inode, bh, &imap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001197 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001198 type, &ioend,
David Chinnereffd1202007-06-18 16:49:58 +10001199 new_ioend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 } else {
1201 set_buffer_dirty(bh);
1202 unlock_buffer(bh);
1203 mark_buffer_dirty(bh);
1204 }
1205 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001206 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001208 } else if (buffer_uptodate(bh) && startio) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001209 /*
1210 * we got here because the buffer is already mapped.
1211 * That means it must already have extents allocated
1212 * underneath it. Map the extent by reading it.
1213 */
Christoph Hellwig558e6892010-04-28 12:28:58 +00001214 if (!imap_valid || flags != BMAPI_READ) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001215 flags = BMAPI_READ;
1216 size = xfs_probe_cluster(inode, page, bh,
1217 head, 1);
1218 err = xfs_map_blocks(inode, offset, size,
Christoph Hellwig207d0412010-04-28 12:28:56 +00001219 &imap, flags);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001220 if (err)
1221 goto error;
Christoph Hellwig558e6892010-04-28 12:28:58 +00001222 imap_valid = xfs_imap_valid(inode, &imap,
1223 offset);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
David Chinnerdf3c7242007-05-24 15:27:03 +10001226 /*
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001227 * We set the type to IO_NEW in case we are doing a
David Chinnerdf3c7242007-05-24 15:27:03 +10001228 * small write at EOF that is extending the file but
1229 * without needing an allocation. We need to update the
1230 * file size on I/O completion in this case so it is
1231 * the same case as having just allocated a new extent
1232 * that we are writing into for the first time.
1233 */
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001234 type = IO_NEW;
Nick Pigginca5de402008-08-02 12:02:13 +02001235 if (trylock_buffer(bh)) {
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001236 ASSERT(buffer_mapped(bh));
Christoph Hellwig558e6892010-04-28 12:28:58 +00001237 if (imap_valid)
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001238 all_bh = 1;
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001239 xfs_add_to_ioend(inode, bh, offset, type,
Christoph Hellwig558e6892010-04-28 12:28:58 +00001240 &ioend, !imap_valid);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001241 page_dirty--;
1242 count++;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001243 } else {
Christoph Hellwig558e6892010-04-28 12:28:58 +00001244 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001246 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
1247 (unmapped || startio)) {
Christoph Hellwig558e6892010-04-28 12:28:58 +00001248 imap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001250
1251 if (!iohead)
1252 iohead = ioend;
1253
1254 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (uptodate && bh == head)
1257 SetPageUptodate(page);
1258
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001259 if (startio)
Denys Vlasenkob41759c2008-05-19 16:34:11 +10001260 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Christoph Hellwig558e6892010-04-28 12:28:58 +00001262 if (ioend && imap_valid) {
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001263 struct xfs_mount *m = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001264 xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap.br_startoff);
1265 xfs_off_t iomap_bsize = XFS_FSB_TO_B(m, imap.br_blockcount);
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001266
1267 offset = (iomap_offset + iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -07001269 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwig207d0412010-04-28 12:28:56 +00001270 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001271 wbc, startio, all_bh, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 }
1273
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001274 if (iohead)
Christoph Hellwig06342cf2009-10-30 09:09:15 +00001275 xfs_submit_ioend(wbc, iohead);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 return page_dirty;
1278
1279error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001280 if (iohead)
1281 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /*
1284 * If it's delalloc and we have nowhere to put it,
1285 * throw it away, unless the lower layers told
1286 * us to try again.
1287 */
1288 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001289 if (!unmapped)
Dave Chinner3ed3a432010-03-05 02:00:42 +00001290 xfs_aops_discard_page(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 ClearPageUptodate(page);
1292 }
1293 return err;
1294}
1295
Nathan Scottf51623b2006-03-14 13:26:27 +11001296/*
1297 * writepage: Called from one of two places:
1298 *
1299 * 1. we are flushing a delalloc buffer head.
1300 *
1301 * 2. we are writing out a dirty page. Typically the page dirty
1302 * state is cleared before we get here. In this case is it
1303 * conceivable we have no buffer heads.
1304 *
1305 * For delalloc space on the page we need to allocate space and
1306 * flush it. For unmapped buffer heads on the page we should
1307 * allocate space if the page is uptodate. For any other dirty
1308 * buffer heads on the page we should flush them.
1309 *
1310 * If we detect that a transaction would be required to flush
1311 * the page, we have to check the process flags first, if we
1312 * are already in a transaction or disk I/O during allocations
1313 * is off, we need to fail the writepage and redirty the page.
1314 */
1315
1316STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001317xfs_vm_writepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001318 struct page *page,
1319 struct writeback_control *wbc)
1320{
1321 int error;
1322 int need_trans;
1323 int delalloc, unmapped, unwritten;
1324 struct inode *inode = page->mapping->host;
1325
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001326 trace_xfs_writepage(inode, page, 0);
Nathan Scottf51623b2006-03-14 13:26:27 +11001327
1328 /*
1329 * We need a transaction if:
1330 * 1. There are delalloc buffers on the page
1331 * 2. The page is uptodate and we have unmapped buffers
1332 * 3. The page is uptodate and we have no buffers
1333 * 4. There are unwritten buffers on the page
1334 */
1335
1336 if (!page_has_buffers(page)) {
1337 unmapped = 1;
1338 need_trans = 1;
1339 } else {
1340 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1341 if (!PageUptodate(page))
1342 unmapped = 0;
1343 need_trans = delalloc + unmapped + unwritten;
1344 }
1345
1346 /*
1347 * If we need a transaction and the process flags say
1348 * we are already in a transaction, or no IO is allowed
1349 * then mark the page dirty again and leave the page
1350 * as is.
1351 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001352 if (current_test_flags(PF_FSTRANS) && need_trans)
Nathan Scottf51623b2006-03-14 13:26:27 +11001353 goto out_fail;
1354
1355 /*
1356 * Delay hooking up buffer heads until we have
1357 * made our go/no-go decision.
1358 */
1359 if (!page_has_buffers(page))
1360 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1361
Eric Sandeenc8a40512009-07-31 00:02:17 -05001362
1363 /*
1364 * VM calculation for nr_to_write seems off. Bump it way
1365 * up, this gets simple streaming writes zippy again.
1366 * To be reviewed again after Jens' writeback changes.
1367 */
1368 wbc->nr_to_write *= 4;
1369
Nathan Scottf51623b2006-03-14 13:26:27 +11001370 /*
1371 * Convert delayed allocate, unwritten or unmapped space
1372 * to real space and flush out to disk.
1373 */
1374 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1375 if (error == -EAGAIN)
1376 goto out_fail;
1377 if (unlikely(error < 0))
1378 goto out_unlock;
1379
1380 return 0;
1381
1382out_fail:
1383 redirty_page_for_writepage(wbc, page);
1384 unlock_page(page);
1385 return 0;
1386out_unlock:
1387 unlock_page(page);
1388 return error;
1389}
1390
Nathan Scott7d4fb402006-06-09 15:27:16 +10001391STATIC int
1392xfs_vm_writepages(
1393 struct address_space *mapping,
1394 struct writeback_control *wbc)
1395{
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001396 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001397 return generic_writepages(mapping, wbc);
1398}
1399
Nathan Scottf51623b2006-03-14 13:26:27 +11001400/*
1401 * Called to move a page into cleanable state - and from there
1402 * to be released. Possibly the page is already clean. We always
1403 * have buffer heads in this call.
1404 *
1405 * Returns 0 if the page is ok to release, 1 otherwise.
1406 *
1407 * Possible scenarios are:
1408 *
1409 * 1. We are being called to release a page which has been written
1410 * to via regular I/O. buffer heads will be dirty and possibly
1411 * delalloc. If no delalloc buffer heads in this case then we
1412 * can just return zero.
1413 *
1414 * 2. We are called to release a page which has been written via
1415 * mmap, all we need to do is ensure there is no delalloc
1416 * state in the buffer heads, if not we can let the caller
1417 * free them and we should come back later via writepage.
1418 */
1419STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001420xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001421 struct page *page,
1422 gfp_t gfp_mask)
1423{
1424 struct inode *inode = page->mapping->host;
1425 int dirty, delalloc, unmapped, unwritten;
1426 struct writeback_control wbc = {
1427 .sync_mode = WB_SYNC_ALL,
1428 .nr_to_write = 1,
1429 };
1430
Christoph Hellwig0b1b2132009-12-14 23:14:59 +00001431 trace_xfs_releasepage(inode, page, 0);
Nathan Scottf51623b2006-03-14 13:26:27 +11001432
Nathan Scott238f4c52006-03-17 17:26:25 +11001433 if (!page_has_buffers(page))
1434 return 0;
1435
Nathan Scottf51623b2006-03-14 13:26:27 +11001436 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1437 if (!delalloc && !unwritten)
1438 goto free_buffers;
1439
1440 if (!(gfp_mask & __GFP_FS))
1441 return 0;
1442
1443 /* If we are already inside a transaction or the thread cannot
1444 * do I/O, we cannot release this page.
1445 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001446 if (current_test_flags(PF_FSTRANS))
Nathan Scottf51623b2006-03-14 13:26:27 +11001447 return 0;
1448
1449 /*
1450 * Convert delalloc space to real space, do not flush the
1451 * data out to disk, that will be done by the caller.
1452 * Never need to allocate space here - we will always
1453 * come back to writepage in that case.
1454 */
1455 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1456 if (dirty == 0 && !unwritten)
1457 goto free_buffers;
1458 return 0;
1459
1460free_buffers:
1461 return try_to_free_buffers(page);
1462}
1463
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001465__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 struct inode *inode,
1467 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 struct buffer_head *bh_result,
1469 int create,
1470 int direct,
1471 bmapi_flags_t flags)
1472{
Christoph Hellwig207d0412010-04-28 12:28:56 +00001473 struct xfs_bmbt_irec imap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001474 xfs_off_t offset;
1475 ssize_t size;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001476 int nimap = 1;
1477 int new = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001480 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001481 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1482 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001483
1484 if (!create && direct && offset >= i_size_read(inode))
1485 return 0;
1486
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001487 error = xfs_iomap(XFS_I(inode), offset, size,
Christoph Hellwig207d0412010-04-28 12:28:56 +00001488 create ? flags : BMAPI_READ, &imap, &nimap, &new);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 if (error)
1490 return -error;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001491 if (nimap == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
1493
Christoph Hellwig207d0412010-04-28 12:28:56 +00001494 if (imap.br_startblock != HOLESTARTBLOCK &&
1495 imap.br_startblock != DELAYSTARTBLOCK) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001496 /*
1497 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 * the read case (treat as if we're reading into a hole).
1499 */
Christoph Hellwig207d0412010-04-28 12:28:56 +00001500 if (create || !ISUNWRITTEN(&imap))
1501 xfs_map_buffer(inode, bh_result, &imap, offset);
1502 if (create && ISUNWRITTEN(&imap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 if (direct)
1504 bh_result->b_private = inode;
1505 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
1507 }
1508
Nathan Scottc2536662006-03-29 10:44:40 +10001509 /*
1510 * If this is a realtime file, data may be on a different device.
1511 * to that pointed to from the buffer_head b_bdev currently.
1512 */
Christoph Hellwig046f1682010-04-28 12:28:52 +00001513 bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Nathan Scottc2536662006-03-29 10:44:40 +10001515 /*
David Chinner549054a2007-02-10 18:36:35 +11001516 * If we previously allocated a block out beyond eof and we are now
1517 * coming back to use it then we will need to flag it as new even if it
1518 * has a disk address.
1519 *
1520 * With sub-block writes into unwritten extents we also need to mark
1521 * the buffer as new so that the unwritten parts of the buffer gets
1522 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 */
1524 if (create &&
1525 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001526 (offset >= i_size_read(inode)) ||
Christoph Hellwig207d0412010-04-28 12:28:56 +00001527 (new || ISUNWRITTEN(&imap))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Christoph Hellwig207d0412010-04-28 12:28:56 +00001530 if (imap.br_startblock == DELAYSTARTBLOCK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 BUG_ON(direct);
1532 if (create) {
1533 set_buffer_uptodate(bh_result);
1534 set_buffer_mapped(bh_result);
1535 set_buffer_delay(bh_result);
1536 }
1537 }
1538
Nathan Scottc2536662006-03-29 10:44:40 +10001539 if (direct || size > (1 << inode->i_blkbits)) {
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001540 struct xfs_mount *mp = XFS_I(inode)->i_mount;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001541 xfs_off_t iomap_offset = XFS_FSB_TO_B(mp, imap.br_startoff);
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001542 xfs_off_t iomap_delta = offset - iomap_offset;
Christoph Hellwig207d0412010-04-28 12:28:56 +00001543 xfs_off_t iomap_bsize = XFS_FSB_TO_B(mp, imap.br_blockcount);
Christoph Hellwig9563b3d2010-04-28 12:28:53 +00001544
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001545 ASSERT(iomap_bsize - iomap_delta > 0);
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001546 offset = min_t(xfs_off_t,
Christoph Hellwig8699bb02010-04-28 12:28:54 +00001547 iomap_bsize - iomap_delta, size);
Nathan Scottc2536662006-03-29 10:44:40 +10001548 bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
1550
1551 return 0;
1552}
1553
1554int
Nathan Scottc2536662006-03-29 10:44:40 +10001555xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 struct inode *inode,
1557 sector_t iblock,
1558 struct buffer_head *bh_result,
1559 int create)
1560{
Nathan Scottc2536662006-03-29 10:44:40 +10001561 return __xfs_get_blocks(inode, iblock,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -08001562 bh_result, create, 0, BMAPI_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
1565STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001566xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 struct inode *inode,
1568 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 struct buffer_head *bh_result,
1570 int create)
1571{
Nathan Scottc2536662006-03-29 10:44:40 +10001572 return __xfs_get_blocks(inode, iblock,
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -08001573 bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574}
1575
Christoph Hellwigf0973862005-09-05 08:22:52 +10001576STATIC void
Nathan Scotte4c573b2006-03-14 13:54:26 +11001577xfs_end_io_direct(
Christoph Hellwigf0973862005-09-05 08:22:52 +10001578 struct kiocb *iocb,
1579 loff_t offset,
1580 ssize_t size,
1581 void *private)
1582{
1583 xfs_ioend_t *ioend = iocb->private;
1584
1585 /*
1586 * Non-NULL private data means we need to issue a transaction to
1587 * convert a range from unwritten to written extents. This needs
Nathan Scottc41564b2006-03-29 08:55:14 +10001588 * to happen from process context but aio+dio I/O completion
Christoph Hellwigf0973862005-09-05 08:22:52 +10001589 * happens from irq context so we need to defer it to a workqueue.
Nathan Scottc41564b2006-03-29 08:55:14 +10001590 * This is not necessary for synchronous direct I/O, but we do
Christoph Hellwigf0973862005-09-05 08:22:52 +10001591 * it anyway to keep the code uniform and simpler.
1592 *
David Chinnere927af92007-06-05 16:24:36 +10001593 * Well, if only it were that simple. Because synchronous direct I/O
1594 * requires extent conversion to occur *before* we return to userspace,
1595 * we have to wait for extent conversion to complete. Look at the
1596 * iocb that has been passed to us to determine if this is AIO or
1597 * not. If it is synchronous, tell xfs_finish_ioend() to kick the
1598 * workqueue and wait for it to complete.
1599 *
Christoph Hellwigf0973862005-09-05 08:22:52 +10001600 * The core direct I/O code might be changed to always call the
1601 * completion handler in the future, in which case all this can
1602 * go away.
1603 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001604 ioend->io_offset = offset;
1605 ioend->io_size = size;
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001606 if (ioend->io_type == IO_READ) {
David Chinnere927af92007-06-05 16:24:36 +10001607 xfs_finish_ioend(ioend, 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001608 } else if (private && size > 0) {
David Chinnere927af92007-06-05 16:24:36 +10001609 xfs_finish_ioend(ioend, is_sync_kiocb(iocb));
Christoph Hellwigf0973862005-09-05 08:22:52 +10001610 } else {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001611 /*
1612 * A direct I/O write ioend starts it's life in unwritten
1613 * state in case they map an unwritten extent. This write
1614 * didn't map an unwritten extent so switch it's completion
1615 * handler.
1616 */
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001617 ioend->io_type = IO_NEW;
David Chinnere927af92007-06-05 16:24:36 +10001618 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001619 }
1620
1621 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001622 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001623 * completion handler was called. Thus we need to protect
1624 * against double-freeing.
1625 */
1626 iocb->private = NULL;
1627}
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001630xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int rw,
1632 struct kiocb *iocb,
1633 const struct iovec *iov,
1634 loff_t offset,
1635 unsigned long nr_segs)
1636{
1637 struct file *file = iocb->ki_filp;
1638 struct inode *inode = file->f_mapping->host;
Christoph Hellwig6214ed42007-09-14 15:23:17 +10001639 struct block_device *bdev;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001640 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Christoph Hellwig046f1682010-04-28 12:28:52 +00001642 bdev = xfs_find_bdev_for_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001644 iocb->private = xfs_alloc_ioend(inode, rw == WRITE ?
Christoph Hellwig34a52c62010-04-28 12:28:57 +00001645 IO_UNWRITTEN : IO_READ);
Christoph Hellwig5fe878a2009-12-15 16:47:50 -08001646
1647 ret = blockdev_direct_IO_no_locking(rw, iocb, inode, bdev, iov,
1648 offset, nr_segs,
1649 xfs_get_blocks_direct,
1650 xfs_end_io_direct);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001651
Zach Brown8459d862006-12-10 02:21:05 -08001652 if (unlikely(ret != -EIOCBQUEUED && iocb->private))
Christoph Hellwigf0973862005-09-05 08:22:52 +10001653 xfs_destroy_ioend(iocb->private);
1654 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655}
1656
Nathan Scottf51623b2006-03-14 13:26:27 +11001657STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001658xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001659 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001660 struct address_space *mapping,
1661 loff_t pos,
1662 unsigned len,
1663 unsigned flags,
1664 struct page **pagep,
1665 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001666{
Nick Piggind79689c2007-10-16 01:25:06 -07001667 *pagep = NULL;
1668 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1669 xfs_get_blocks);
Nathan Scottf51623b2006-03-14 13:26:27 +11001670}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001673xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 struct address_space *mapping,
1675 sector_t block)
1676{
1677 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001678 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001680 xfs_itrace_entry(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001681 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001682 xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001683 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001684 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685}
1686
1687STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001688xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 struct file *unused,
1690 struct page *page)
1691{
Nathan Scottc2536662006-03-29 10:44:40 +10001692 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
1695STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001696xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct file *unused,
1698 struct address_space *mapping,
1699 struct list_head *pages,
1700 unsigned nr_pages)
1701{
Nathan Scottc2536662006-03-29 10:44:40 +10001702 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703}
1704
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001705const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001706 .readpage = xfs_vm_readpage,
1707 .readpages = xfs_vm_readpages,
1708 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001709 .writepages = xfs_vm_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 .sync_page = block_sync_page,
Nathan Scott238f4c52006-03-17 17:26:25 +11001711 .releasepage = xfs_vm_releasepage,
1712 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001713 .write_begin = xfs_vm_write_begin,
1714 .write_end = generic_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001715 .bmap = xfs_vm_bmap,
1716 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001717 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001718 .is_partially_uptodate = block_is_partially_uptodate,
Andi Kleenaa261f52009-09-16 11:50:16 +02001719 .error_remove_page = generic_error_remove_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720};