blob: 40d2226060d177b72fa27e44962202687da0aaae [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110042#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/writeback.h>
44
Christoph Hellwig25e41b32008-12-03 12:20:39 +010045
46/*
47 * Prime number of hash buckets since address is used as the key.
48 */
49#define NVSYNC 37
50#define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
51static wait_queue_head_t xfs_ioend_wq[NVSYNC];
52
53void __init
54xfs_ioend_init(void)
55{
56 int i;
57
58 for (i = 0; i < NVSYNC; i++)
59 init_waitqueue_head(&xfs_ioend_wq[i]);
60}
61
62void
63xfs_ioend_wait(
64 xfs_inode_t *ip)
65{
66 wait_queue_head_t *wq = to_ioend_wq(ip);
67
68 wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
69}
70
71STATIC void
72xfs_ioend_wake(
73 xfs_inode_t *ip)
74{
75 if (atomic_dec_and_test(&ip->i_iocount))
76 wake_up(to_ioend_wq(ip));
77}
78
Nathan Scottf51623b2006-03-14 13:26:27 +110079STATIC void
80xfs_count_page_state(
81 struct page *page,
82 int *delalloc,
83 int *unmapped,
84 int *unwritten)
85{
86 struct buffer_head *bh, *head;
87
88 *delalloc = *unmapped = *unwritten = 0;
89
90 bh = head = page_buffers(page);
91 do {
92 if (buffer_uptodate(bh) && !buffer_mapped(bh))
93 (*unmapped) = 1;
Nathan Scottf51623b2006-03-14 13:26:27 +110094 else if (buffer_unwritten(bh))
95 (*unwritten) = 1;
96 else if (buffer_delay(bh))
97 (*delalloc) = 1;
98 } while ((bh = bh->b_this_page) != head);
99}
100
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#if defined(XFS_RW_TRACE)
102void
103xfs_page_trace(
104 int tag,
105 struct inode *inode,
106 struct page *page,
Nathan Scotted9d88f2006-09-28 10:56:43 +1000107 unsigned long pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 loff_t isize = i_size_read(inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100111 loff_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 int delalloc = -1, unmapped = -1, unwritten = -1;
113
114 if (page_has_buffers(page))
115 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
116
David Chinnere6064d32008-08-13 16:01:45 +1000117 ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (!ip->i_rwtrace)
119 return;
120
121 ktrace_enter(ip->i_rwtrace,
122 (void *)((unsigned long)tag),
123 (void *)ip,
124 (void *)inode,
125 (void *)page,
Nathan Scotted9d88f2006-09-28 10:56:43 +1000126 (void *)pgoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
128 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
129 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
130 (void *)((unsigned long)(isize & 0xffffffff)),
131 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
132 (void *)((unsigned long)(offset & 0xffffffff)),
133 (void *)((unsigned long)delalloc),
134 (void *)((unsigned long)unmapped),
135 (void *)((unsigned long)unwritten),
Yingping Luf1fdc842006-03-22 12:44:15 +1100136 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 (void *)NULL);
138}
139#else
Nathan Scotted9d88f2006-09-28 10:56:43 +1000140#define xfs_page_trace(tag, inode, page, pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141#endif
142
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000143STATIC struct block_device *
144xfs_find_bdev_for_inode(
145 struct xfs_inode *ip)
146{
147 struct xfs_mount *mp = ip->i_mount;
148
Eric Sandeen71ddabb2007-11-23 16:29:42 +1100149 if (XFS_IS_REALTIME_INODE(ip))
Christoph Hellwig6214ed42007-09-14 15:23:17 +1000150 return mp->m_rtdev_targp->bt_bdev;
151 else
152 return mp->m_ddev_targp->bt_bdev;
153}
154
Christoph Hellwig0829c362005-09-02 16:58:49 +1000155/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100156 * We're now finished for good with this ioend structure.
157 * Update the page state via the associated buffer_heads,
158 * release holds on the inode and bio, and finally free
159 * up memory. Do not use the ioend after this.
160 */
Christoph Hellwig0829c362005-09-02 16:58:49 +1000161STATIC void
162xfs_destroy_ioend(
163 xfs_ioend_t *ioend)
164{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100165 struct buffer_head *bh, *next;
Christoph Hellwig583fa582008-12-03 12:20:38 +0100166 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100167
168 for (bh = ioend->io_buffer_head; bh; bh = next) {
169 next = bh->b_private;
Nathan Scott7d04a332006-06-09 14:58:38 +1000170 bh->b_end_io(bh, !ioend->io_error);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100171 }
Christoph Hellwig583fa582008-12-03 12:20:38 +0100172
173 /*
174 * Volume managers supporting multiple paths can send back ENODEV
175 * when the final path disappears. In this case continuing to fill
176 * the page cache with dirty data which cannot be written out is
177 * evil, so prevent that.
178 */
179 if (unlikely(ioend->io_error == -ENODEV)) {
180 xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
181 __FILE__, __LINE__);
Christoph Hellwigb677c212007-08-29 11:46:28 +1000182 }
Christoph Hellwig583fa582008-12-03 12:20:38 +0100183
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100184 xfs_ioend_wake(ip);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000185 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
188/*
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000189 * Update on-disk file size now that data has been written to disk.
190 * The current in-memory file size is i_size. If a write is beyond
Christoph Hellwig613d7042007-10-11 17:44:08 +1000191 * eof i_new_size will be the intended file size until i_size is
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000192 * updated. If this write does not extend all the way to the valid
193 * file size then restrict this update to the end of the write.
194 */
195STATIC void
196xfs_setfilesize(
197 xfs_ioend_t *ioend)
198{
Christoph Hellwigb677c212007-08-29 11:46:28 +1000199 xfs_inode_t *ip = XFS_I(ioend->io_inode);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000200 xfs_fsize_t isize;
201 xfs_fsize_t bsize;
202
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000203 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
204 ASSERT(ioend->io_type != IOMAP_READ);
205
206 if (unlikely(ioend->io_error))
207 return;
208
209 bsize = ioend->io_offset + ioend->io_size;
210
211 xfs_ilock(ip, XFS_ILOCK_EXCL);
212
Christoph Hellwig613d7042007-10-11 17:44:08 +1000213 isize = MAX(ip->i_size, ip->i_new_size);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000214 isize = MIN(isize, bsize);
215
216 if (ip->i_d.di_size < isize) {
217 ip->i_d.di_size = isize;
David Chinner94b97e32008-10-30 17:21:30 +1100218 xfs_mark_inode_dirty_sync(ip);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000219 }
220
221 xfs_iunlock(ip, XFS_ILOCK_EXCL);
222}
223
224/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100225 * Buffered IO write completion for delayed allocate extents.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100226 */
227STATIC void
228xfs_end_bio_delalloc(
David Howellsc4028952006-11-22 14:57:56 +0000229 struct work_struct *work)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100230{
David Howellsc4028952006-11-22 14:57:56 +0000231 xfs_ioend_t *ioend =
232 container_of(work, xfs_ioend_t, io_work);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100233
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000234 xfs_setfilesize(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100235 xfs_destroy_ioend(ioend);
236}
237
238/*
239 * Buffered IO write completion for regular, written extents.
240 */
241STATIC void
242xfs_end_bio_written(
David Howellsc4028952006-11-22 14:57:56 +0000243 struct work_struct *work)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100244{
David Howellsc4028952006-11-22 14:57:56 +0000245 xfs_ioend_t *ioend =
246 container_of(work, xfs_ioend_t, io_work);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100247
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000248 xfs_setfilesize(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100249 xfs_destroy_ioend(ioend);
250}
251
252/*
253 * IO write completion for unwritten extents.
254 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 * Issue transactions to convert a buffer range from unwritten
Christoph Hellwigf0973862005-09-05 08:22:52 +1000256 * to written extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 */
258STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000259xfs_end_bio_unwritten(
David Howellsc4028952006-11-22 14:57:56 +0000260 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
David Howellsc4028952006-11-22 14:57:56 +0000262 xfs_ioend_t *ioend =
263 container_of(work, xfs_ioend_t, io_work);
Christoph Hellwig76428612007-09-14 15:23:31 +1000264 struct xfs_inode *ip = XFS_I(ioend->io_inode);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000265 xfs_off_t offset = ioend->io_offset;
266 size_t size = ioend->io_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000268 if (likely(!ioend->io_error)) {
David Chinnercc884662008-04-10 12:23:52 +1000269 if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
270 int error;
271 error = xfs_iomap_write_unwritten(ip, offset, size);
272 if (error)
273 ioend->io_error = error;
274 }
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000275 xfs_setfilesize(ioend);
276 }
277 xfs_destroy_ioend(ioend);
278}
279
280/*
281 * IO read completion for regular, written extents.
282 */
283STATIC void
284xfs_end_bio_read(
285 struct work_struct *work)
286{
287 xfs_ioend_t *ioend =
288 container_of(work, xfs_ioend_t, io_work);
289
Christoph Hellwig0829c362005-09-02 16:58:49 +1000290 xfs_destroy_ioend(ioend);
291}
292
293/*
Dave Chinnerc626d172009-04-06 18:42:11 +0200294 * Schedule IO completion handling on a xfsdatad if this was
295 * the final hold on this ioend. If we are asked to wait,
296 * flush the workqueue.
297 */
298STATIC void
299xfs_finish_ioend(
300 xfs_ioend_t *ioend,
301 int wait)
302{
303 if (atomic_dec_and_test(&ioend->io_remaining)) {
304 struct workqueue_struct *wq = xfsdatad_workqueue;
305 if (ioend->io_work.func == xfs_end_bio_unwritten)
306 wq = xfsconvertd_workqueue;
307
308 queue_work(wq, &ioend->io_work);
309 if (wait)
310 flush_workqueue(wq);
311 }
312}
313
314/*
Christoph Hellwig0829c362005-09-02 16:58:49 +1000315 * Allocate and initialise an IO completion structure.
316 * We need to track unwritten extent write completion here initially.
317 * We'll need to extend this for updating the ondisk inode size later
318 * (vs. incore size).
319 */
320STATIC xfs_ioend_t *
321xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100322 struct inode *inode,
323 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000324{
325 xfs_ioend_t *ioend;
326
327 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
328
329 /*
330 * Set the count to 1 initially, which will prevent an I/O
331 * completion callback from happening before we have started
332 * all the I/O from calling the completion routine too early.
333 */
334 atomic_set(&ioend->io_remaining, 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000335 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100336 ioend->io_list = NULL;
337 ioend->io_type = type;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000338 ioend->io_inode = inode;
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000339 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100340 ioend->io_buffer_tail = NULL;
Christoph Hellwigb677c212007-08-29 11:46:28 +1000341 atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000342 ioend->io_offset = 0;
343 ioend->io_size = 0;
344
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100345 if (type == IOMAP_UNWRITTEN)
David Howellsc4028952006-11-22 14:57:56 +0000346 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100347 else if (type == IOMAP_DELAY)
David Howellsc4028952006-11-22 14:57:56 +0000348 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000349 else if (type == IOMAP_READ)
350 INIT_WORK(&ioend->io_work, xfs_end_bio_read);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100351 else
David Howellsc4028952006-11-22 14:57:56 +0000352 INIT_WORK(&ioend->io_work, xfs_end_bio_written);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000353
354 return ioend;
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357STATIC int
358xfs_map_blocks(
359 struct inode *inode,
360 loff_t offset,
361 ssize_t count,
362 xfs_iomap_t *mapp,
363 int flags)
364{
Christoph Hellwig6bd16ff2008-12-03 12:20:32 +0100365 int nmaps = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Christoph Hellwig6bd16ff2008-12-03 12:20:32 +0100367 return -xfs_iomap(XFS_I(inode), offset, count, flags, mapp, &nmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368}
369
David Chinner7989cb82007-02-10 18:34:56 +1100370STATIC_INLINE int
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100371xfs_iomap_valid(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 xfs_iomap_t *iomapp,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100373 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100375 return offset >= iomapp->iomap_offset &&
376 offset < iomapp->iomap_offset + iomapp->iomap_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100379/*
380 * BIO completion handler for buffered IO.
381 */
Al Viro782e3b32007-10-12 07:17:47 +0100382STATIC void
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100383xfs_end_bio(
384 struct bio *bio,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100385 int error)
386{
387 xfs_ioend_t *ioend = bio->bi_private;
388
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100389 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000390 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100391
392 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100393 bio->bi_private = NULL;
394 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100395 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000396
David Chinnere927af92007-06-05 16:24:36 +1000397 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100398}
399
400STATIC void
401xfs_submit_ioend_bio(
402 xfs_ioend_t *ioend,
403 struct bio *bio)
404{
405 atomic_inc(&ioend->io_remaining);
406
407 bio->bi_private = ioend;
408 bio->bi_end_io = xfs_end_bio;
409
410 submit_bio(WRITE, bio);
411 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
412 bio_put(bio);
413}
414
415STATIC struct bio *
416xfs_alloc_ioend_bio(
417 struct buffer_head *bh)
418{
419 struct bio *bio;
420 int nvecs = bio_get_nr_vecs(bh->b_bdev);
421
422 do {
423 bio = bio_alloc(GFP_NOIO, nvecs);
424 nvecs >>= 1;
425 } while (!bio);
426
427 ASSERT(bio->bi_private == NULL);
428 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
429 bio->bi_bdev = bh->b_bdev;
430 bio_get(bio);
431 return bio;
432}
433
434STATIC void
435xfs_start_buffer_writeback(
436 struct buffer_head *bh)
437{
438 ASSERT(buffer_mapped(bh));
439 ASSERT(buffer_locked(bh));
440 ASSERT(!buffer_delay(bh));
441 ASSERT(!buffer_unwritten(bh));
442
443 mark_buffer_async_write(bh);
444 set_buffer_uptodate(bh);
445 clear_buffer_dirty(bh);
446}
447
448STATIC void
449xfs_start_page_writeback(
450 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100451 int clear_dirty,
452 int buffers)
453{
454 ASSERT(PageLocked(page));
455 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100456 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100457 clear_page_dirty_for_io(page);
458 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100459 unlock_page(page);
Fengguang Wu1f7decf2007-10-16 23:30:42 -0700460 /* If no buffers on the page are to be written, finish it here */
461 if (!buffers)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100462 end_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100463}
464
465static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
466{
467 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
468}
469
470/*
David Chinnerd88992f2006-01-18 13:38:12 +1100471 * Submit all of the bios for all of the ioends we have saved up, covering the
472 * initial writepage page and also any probed pages.
473 *
474 * Because we may have multiple ioends spanning a page, we need to start
475 * writeback on all the buffers before we submit them for I/O. If we mark the
476 * buffers as we got, then we can end up with a page that only has buffers
477 * marked async write and I/O complete on can occur before we mark the other
478 * buffers async write.
479 *
480 * The end result of this is that we trip a bug in end_page_writeback() because
481 * we call it twice for the one page as the code in end_buffer_async_write()
482 * assumes that all buffers on the page are started at the same time.
483 *
484 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000485 * buffer_heads, and then submit them for I/O on the second pass.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100486 */
487STATIC void
488xfs_submit_ioend(
489 xfs_ioend_t *ioend)
490{
David Chinnerd88992f2006-01-18 13:38:12 +1100491 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100492 xfs_ioend_t *next;
493 struct buffer_head *bh;
494 struct bio *bio;
495 sector_t lastblock = 0;
496
David Chinnerd88992f2006-01-18 13:38:12 +1100497 /* Pass 1 - start writeback */
498 do {
499 next = ioend->io_list;
500 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
501 xfs_start_buffer_writeback(bh);
502 }
503 } while ((ioend = next) != NULL);
504
505 /* Pass 2 - submit I/O */
506 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100507 do {
508 next = ioend->io_list;
509 bio = NULL;
510
511 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100512
513 if (!bio) {
514 retry:
515 bio = xfs_alloc_ioend_bio(bh);
516 } else if (bh->b_blocknr != lastblock + 1) {
517 xfs_submit_ioend_bio(ioend, bio);
518 goto retry;
519 }
520
521 if (bio_add_buffer(bio, bh) != bh->b_size) {
522 xfs_submit_ioend_bio(ioend, bio);
523 goto retry;
524 }
525
526 lastblock = bh->b_blocknr;
527 }
528 if (bio)
529 xfs_submit_ioend_bio(ioend, bio);
David Chinnere927af92007-06-05 16:24:36 +1000530 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100531 } while ((ioend = next) != NULL);
532}
533
534/*
535 * Cancel submission of all buffer_heads so far in this endio.
536 * Toss the endio too. Only ever called for the initial page
537 * in a writepage request, so only ever one page.
538 */
539STATIC void
540xfs_cancel_ioend(
541 xfs_ioend_t *ioend)
542{
543 xfs_ioend_t *next;
544 struct buffer_head *bh, *next_bh;
545
546 do {
547 next = ioend->io_list;
548 bh = ioend->io_buffer_head;
549 do {
550 next_bh = bh->b_private;
551 clear_buffer_async_write(bh);
552 unlock_buffer(bh);
553 } while ((bh = next_bh) != NULL);
554
Christoph Hellwig25e41b32008-12-03 12:20:39 +0100555 xfs_ioend_wake(XFS_I(ioend->io_inode));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100556 mempool_free(ioend, xfs_ioend_pool);
557 } while ((ioend = next) != NULL);
558}
559
560/*
561 * Test to see if we've been building up a completion structure for
562 * earlier buffers -- if so, we try to append to this ioend if we
563 * can, otherwise we finish off any current ioend and start another.
564 * Return true if we've finished the given ioend.
565 */
566STATIC void
567xfs_add_to_ioend(
568 struct inode *inode,
569 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100570 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100571 unsigned int type,
572 xfs_ioend_t **result,
573 int need_ioend)
574{
575 xfs_ioend_t *ioend = *result;
576
577 if (!ioend || need_ioend || type != ioend->io_type) {
578 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100579
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100580 ioend = xfs_alloc_ioend(inode, type);
581 ioend->io_offset = offset;
582 ioend->io_buffer_head = bh;
583 ioend->io_buffer_tail = bh;
584 if (previous)
585 previous->io_list = ioend;
586 *result = ioend;
587 } else {
588 ioend->io_buffer_tail->b_private = bh;
589 ioend->io_buffer_tail = bh;
590 }
591
592 bh->b_private = NULL;
593 ioend->io_size += bh->b_size;
594}
595
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100597xfs_map_buffer(
598 struct buffer_head *bh,
599 xfs_iomap_t *mp,
600 xfs_off_t offset,
601 uint block_bits)
602{
603 sector_t bn;
604
605 ASSERT(mp->iomap_bn != IOMAP_DADDR_NULL);
606
607 bn = (mp->iomap_bn >> (block_bits - BBSHIFT)) +
608 ((offset - mp->iomap_offset) >> block_bits);
609
610 ASSERT(bn || (mp->iomap_flags & IOMAP_REALTIME));
611
612 bh->b_blocknr = bn;
613 set_buffer_mapped(bh);
614}
615
616STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617xfs_map_at_offset(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct buffer_head *bh,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100619 loff_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 int block_bits,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100621 xfs_iomap_t *iomapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
624 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
626 lock_buffer(bh);
Nathan Scott87cbc492006-03-14 13:26:43 +1100627 xfs_map_buffer(bh, iomapp, offset, block_bits);
Nathan Scottce8e9222006-01-11 15:39:08 +1100628 bh->b_bdev = iomapp->iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 set_buffer_mapped(bh);
630 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100631 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100635 * Look for a page at index that is suitable for clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
637STATIC unsigned int
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100638xfs_probe_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100639 struct page *page,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100640 unsigned int pg_offset,
641 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int ret = 0;
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100646 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 if (page->mapping && PageDirty(page)) {
649 if (page_has_buffers(page)) {
650 struct buffer_head *bh, *head;
651
652 bh = head = page_buffers(page);
653 do {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100654 if (!buffer_uptodate(bh))
655 break;
656 if (mapped != buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 break;
658 ret += bh->b_size;
659 if (ret >= pg_offset)
660 break;
661 } while ((bh = bh->b_this_page) != head);
662 } else
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100663 ret = mapped ? 0 : PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 return ret;
667}
668
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100669STATIC size_t
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100670xfs_probe_cluster(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct inode *inode,
672 struct page *startpage,
673 struct buffer_head *bh,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100674 struct buffer_head *head,
675 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100677 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100679 size_t total = 0;
680 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 /* First sum forwards in this page */
683 do {
Eric Sandeen2353e8e2006-02-28 12:30:30 +1100684 if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100685 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 total += bh->b_size;
687 } while ((bh = bh->b_this_page) != head);
688
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100689 /* if we reached the end of the page, sum forwards in following pages */
690 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
691 tindex = startpage->index + 1;
692
693 /* Prune this back to avoid pathological behavior */
694 tloff = min(tlast, startpage->index + 64);
695
696 pagevec_init(&pvec, 0);
697 while (!done && tindex <= tloff) {
698 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
699
700 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
701 break;
702
703 for (i = 0; i < pagevec_count(&pvec); i++) {
704 struct page *page = pvec.pages[i];
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000705 size_t pg_offset, pg_len = 0;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100706
707 if (tindex == tlast) {
708 pg_offset =
709 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100710 if (!pg_offset) {
711 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100712 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100713 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100714 } else
715 pg_offset = PAGE_CACHE_SIZE;
716
Nick Piggin529ae9a2008-08-02 12:01:03 +0200717 if (page->index == tindex && trylock_page(page)) {
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000718 pg_len = xfs_probe_page(page, pg_offset, mapped);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100719 unlock_page(page);
720 }
721
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000722 if (!pg_len) {
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100723 done = 1;
724 break;
725 }
726
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000727 total += pg_len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100728 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100730
731 pagevec_release(&pvec);
732 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return total;
736}
737
738/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100739 * Test if a given page is suitable for writing as part of an unwritten
740 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100742STATIC int
743xfs_is_delayed_page(
744 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100745 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100748 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750 if (page->mapping && page_has_buffers(page)) {
751 struct buffer_head *bh, *head;
752 int acceptable = 0;
753
754 bh = head = page_buffers(page);
755 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100756 if (buffer_unwritten(bh))
757 acceptable = (type == IOMAP_UNWRITTEN);
758 else if (buffer_delay(bh))
759 acceptable = (type == IOMAP_DELAY);
David Chinner2ddee842006-03-22 12:47:40 +1100760 else if (buffer_dirty(bh) && buffer_mapped(bh))
David Chinnerdf3c7242007-05-24 15:27:03 +1000761 acceptable = (type == IOMAP_NEW);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100762 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 } while ((bh = bh->b_this_page) != head);
765
766 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100767 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773/*
774 * Allocate & map buffers for page given the extent map. Write it out.
775 * except for the original page of a writepage, this is called on
776 * delalloc/unwritten pages only, for the original page it is possible
777 * that the page has no mapping at all.
778 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100779STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780xfs_convert_page(
781 struct inode *inode,
782 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100783 loff_t tindex,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100784 xfs_iomap_t *mp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100785 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 int startio,
788 int all_bh)
789{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100790 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100791 xfs_off_t end_offset;
792 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100793 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 int bbits = inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700795 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100796 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100797 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100799 if (page->index != tindex)
800 goto fail;
Nick Piggin529ae9a2008-08-02 12:01:03 +0200801 if (!trylock_page(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100802 goto fail;
803 if (PageWriteback(page))
804 goto fail_unlock_page;
805 if (page->mapping != inode->i_mapping)
806 goto fail_unlock_page;
807 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
808 goto fail_unlock_page;
809
Nathan Scott24e17b52005-05-05 13:33:20 -0700810 /*
811 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000812 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100813 *
814 * Derivation:
815 *
816 * End offset is the highest offset that this page should represent.
817 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
818 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
819 * hence give us the correct page_dirty count. On any other page,
820 * it will be zero and in that case we need page_dirty to be the
821 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700822 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100823 end_offset = min_t(unsigned long long,
824 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
825 i_size_read(inode));
826
Nathan Scott24e17b52005-05-05 13:33:20 -0700827 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100828 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
829 PAGE_CACHE_SIZE);
830 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
831 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 bh = head = page_buffers(page);
834 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100835 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100837 if (!buffer_uptodate(bh))
838 uptodate = 0;
839 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
840 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100842 }
843
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100844 if (buffer_unwritten(bh) || buffer_delay(bh)) {
845 if (buffer_unwritten(bh))
846 type = IOMAP_UNWRITTEN;
847 else
848 type = IOMAP_DELAY;
849
850 if (!xfs_iomap_valid(mp, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100851 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100852 continue;
853 }
854
855 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
856 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
857
858 xfs_map_at_offset(bh, offset, bbits, mp);
859 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100860 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100861 type, ioendp, done);
862 } else {
863 set_buffer_dirty(bh);
864 unlock_buffer(bh);
865 mark_buffer_dirty(bh);
866 }
867 page_dirty--;
868 count++;
869 } else {
David Chinnerdf3c7242007-05-24 15:27:03 +1000870 type = IOMAP_NEW;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100871 if (buffer_mapped(bh) && all_bh && startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100873 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100874 type, ioendp, done);
875 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700876 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100877 } else {
878 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100881 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100883 if (uptodate && bh == head)
884 SetPageUptodate(page);
885
886 if (startio) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100887 if (count) {
888 struct backing_dev_info *bdi;
889
890 bdi = inode->i_mapping->backing_dev_info;
David Chinner9fddaca2006-02-07 20:27:24 +1100891 wbc->nr_to_write--;
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100892 if (bdi_write_congested(bdi)) {
893 wbc->encountered_congestion = 1;
894 done = 1;
David Chinner9fddaca2006-02-07 20:27:24 +1100895 } else if (wbc->nr_to_write <= 0) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100896 done = 1;
897 }
898 }
Denys Vlasenkob41759c2008-05-19 16:34:11 +1000899 xfs_start_page_writeback(page, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100901
902 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100903 fail_unlock_page:
904 unlock_page(page);
905 fail:
906 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907}
908
909/*
910 * Convert & write out a cluster of pages in the same extent as defined
911 * by mp and following the start page.
912 */
913STATIC void
914xfs_cluster_write(
915 struct inode *inode,
916 pgoff_t tindex,
917 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100918 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 struct writeback_control *wbc,
920 int startio,
921 int all_bh,
922 pgoff_t tlast)
923{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100924 struct pagevec pvec;
925 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100927 pagevec_init(&pvec, 0);
928 while (!done && tindex <= tlast) {
929 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
930
931 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100933
934 for (i = 0; i < pagevec_count(&pvec); i++) {
935 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
936 iomapp, ioendp, wbc, startio, all_bh);
937 if (done)
938 break;
939 }
940
941 pagevec_release(&pvec);
942 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944}
945
946/*
947 * Calling this without startio set means we are being asked to make a dirty
948 * page ready for freeing it's buffers. When called with startio set then
949 * we are coming from writepage.
950 *
951 * When called with startio set it is important that we write the WHOLE
952 * page if possible.
953 * The bh->b_state's cannot know if any of the blocks or which block for
954 * that matter are dirty due to mmap writes, and therefore bh uptodate is
Nathan Scottc41564b2006-03-29 08:55:14 +1000955 * only valid if the page itself isn't completely uptodate. Some layers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 * may clear the page dirty flag prior to calling write page, under the
957 * assumption the entire page will be written out; by not writing out the
958 * whole page the page can be reused before all valid dirty data is
959 * written out. Note: in the case of a page that has been dirty'd by
960 * mapwrite and but partially setup by block_prepare_write the
961 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
962 * valid state, thus the whole page must be written out thing.
963 */
964
965STATIC int
966xfs_page_state_convert(
967 struct inode *inode,
968 struct page *page,
969 struct writeback_control *wbc,
970 int startio,
971 int unmapped) /* also implies page uptodate */
972{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100973 struct buffer_head *bh, *head;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100974 xfs_iomap_t iomap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100975 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 loff_t offset;
977 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100978 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 __uint64_t end_offset;
980 pgoff_t end_index, last_index, tlast;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100981 ssize_t size, len;
982 int flags, err, iomap_valid = 0, uptodate = 1;
Nathan Scott82721452006-04-11 15:10:55 +1000983 int page_dirty, count = 0;
984 int trylock = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100985 int all_bh = unmapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Nathan Scott82721452006-04-11 15:10:55 +1000987 if (startio) {
988 if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
989 trylock |= BMAPI_TRYLOCK;
990 }
Daniel Moore3ba08152005-05-05 13:31:34 -0700991
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 /* Is this page beyond the end of the file? */
993 offset = i_size_read(inode);
994 end_index = offset >> PAGE_CACHE_SHIFT;
995 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
996 if (page->index >= end_index) {
997 if ((page->index >= end_index + 1) ||
998 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100999 if (startio)
1000 unlock_page(page);
1001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 }
1003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
Nathan Scott24e17b52005-05-05 13:33:20 -07001006 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +10001007 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001008 *
1009 * Derivation:
1010 *
1011 * End offset is the highest offset that this page should represent.
1012 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
1013 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
1014 * hence give us the correct page_dirty count. On any other page,
1015 * it will be zero and in that case we need page_dirty to be the
1016 * count of buffers on the page.
1017 */
1018 end_offset = min_t(unsigned long long,
1019 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -07001020 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001021 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
1022 PAGE_CACHE_SIZE);
1023 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -07001024 page_dirty = p_offset / len;
1025
Nathan Scott24e17b52005-05-05 13:33:20 -07001026 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001027 offset = page_offset(page);
David Chinnerdf3c7242007-05-24 15:27:03 +10001028 flags = BMAPI_READ;
1029 type = IOMAP_NEW;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001030
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001031 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 do {
1034 if (offset >= end_offset)
1035 break;
1036 if (!buffer_uptodate(bh))
1037 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001038 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001039 /*
1040 * the iomap is actually still valid, but the ioend
1041 * isn't. shouldn't happen too often.
1042 */
1043 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001047 if (iomap_valid)
1048 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /*
1051 * First case, map an unwritten extent and prepare for
1052 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001053 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 * Second case, allocate space for a delalloc buffer.
1055 * We can return EAGAIN here in the release page case.
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001056 *
1057 * Third case, an unmapped buffer was found, and we are
1058 * in a path where we need to write the whole page out.
David Chinnerdf3c7242007-05-24 15:27:03 +10001059 */
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001060 if (buffer_unwritten(bh) || buffer_delay(bh) ||
1061 ((buffer_uptodate(bh) || PageUptodate(page)) &&
1062 !buffer_mapped(bh) && (unmapped || startio))) {
David Chinnereffd1202007-06-18 16:49:58 +10001063 int new_ioend = 0;
1064
David Chinnerdf3c7242007-05-24 15:27:03 +10001065 /*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001066 * Make sure we don't use a read-only iomap
1067 */
David Chinnerdf3c7242007-05-24 15:27:03 +10001068 if (flags == BMAPI_READ)
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001069 iomap_valid = 0;
1070
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001071 if (buffer_unwritten(bh)) {
1072 type = IOMAP_UNWRITTEN;
Nathan Scott82721452006-04-11 15:10:55 +10001073 flags = BMAPI_WRITE | BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001074 } else if (buffer_delay(bh)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001075 type = IOMAP_DELAY;
Nathan Scott82721452006-04-11 15:10:55 +10001076 flags = BMAPI_ALLOCATE | trylock;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001077 } else {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001078 type = IOMAP_NEW;
Nathan Scott82721452006-04-11 15:10:55 +10001079 flags = BMAPI_WRITE | BMAPI_MMAP;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001080 }
1081
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001082 if (!iomap_valid) {
David Chinnereffd1202007-06-18 16:49:58 +10001083 /*
1084 * if we didn't have a valid mapping then we
1085 * need to ensure that we put the new mapping
1086 * in a new ioend structure. This needs to be
1087 * done to ensure that the ioends correctly
1088 * reflect the block mappings at io completion
1089 * for unwritten extent conversion.
1090 */
1091 new_ioend = 1;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001092 if (type == IOMAP_NEW) {
1093 size = xfs_probe_cluster(inode,
1094 page, bh, head, 0);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001095 } else {
1096 size = len;
1097 }
1098
1099 err = xfs_map_blocks(inode, offset, size,
1100 &iomap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001101 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 goto error;
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001103 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001105 if (iomap_valid) {
1106 xfs_map_at_offset(bh, offset,
1107 inode->i_blkbits, &iomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001109 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001110 type, &ioend,
David Chinnereffd1202007-06-18 16:49:58 +10001111 new_ioend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 } else {
1113 set_buffer_dirty(bh);
1114 unlock_buffer(bh);
1115 mark_buffer_dirty(bh);
1116 }
1117 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001118 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001120 } else if (buffer_uptodate(bh) && startio) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001121 /*
1122 * we got here because the buffer is already mapped.
1123 * That means it must already have extents allocated
1124 * underneath it. Map the extent by reading it.
1125 */
David Chinnerdf3c7242007-05-24 15:27:03 +10001126 if (!iomap_valid || flags != BMAPI_READ) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001127 flags = BMAPI_READ;
1128 size = xfs_probe_cluster(inode, page, bh,
1129 head, 1);
1130 err = xfs_map_blocks(inode, offset, size,
1131 &iomap, flags);
1132 if (err)
1133 goto error;
1134 iomap_valid = xfs_iomap_valid(&iomap, offset);
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
David Chinnerdf3c7242007-05-24 15:27:03 +10001137 /*
1138 * We set the type to IOMAP_NEW in case we are doing a
1139 * small write at EOF that is extending the file but
1140 * without needing an allocation. We need to update the
1141 * file size on I/O completion in this case so it is
1142 * the same case as having just allocated a new extent
1143 * that we are writing into for the first time.
1144 */
1145 type = IOMAP_NEW;
Nick Pigginca5de402008-08-02 12:02:13 +02001146 if (trylock_buffer(bh)) {
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001147 ASSERT(buffer_mapped(bh));
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001148 if (iomap_valid)
1149 all_bh = 1;
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001150 xfs_add_to_ioend(inode, bh, offset, type,
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001151 &ioend, !iomap_valid);
1152 page_dirty--;
1153 count++;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001154 } else {
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001155 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001157 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
1158 (unmapped || startio)) {
1159 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001161
1162 if (!iohead)
1163 iohead = ioend;
1164
1165 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 if (uptodate && bh == head)
1168 SetPageUptodate(page);
1169
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001170 if (startio)
Denys Vlasenkob41759c2008-05-19 16:34:11 +10001171 xfs_start_page_writeback(page, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001173 if (ioend && iomap_valid) {
1174 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -07001176 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001177 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001178 wbc, startio, all_bh, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001181 if (iohead)
1182 xfs_submit_ioend(iohead);
1183
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 return page_dirty;
1185
1186error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001187 if (iohead)
1188 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /*
1191 * If it's delalloc and we have nowhere to put it,
1192 * throw it away, unless the lower layers told
1193 * us to try again.
1194 */
1195 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001196 if (!unmapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 block_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 ClearPageUptodate(page);
1199 }
1200 return err;
1201}
1202
Nathan Scottf51623b2006-03-14 13:26:27 +11001203/*
1204 * writepage: Called from one of two places:
1205 *
1206 * 1. we are flushing a delalloc buffer head.
1207 *
1208 * 2. we are writing out a dirty page. Typically the page dirty
1209 * state is cleared before we get here. In this case is it
1210 * conceivable we have no buffer heads.
1211 *
1212 * For delalloc space on the page we need to allocate space and
1213 * flush it. For unmapped buffer heads on the page we should
1214 * allocate space if the page is uptodate. For any other dirty
1215 * buffer heads on the page we should flush them.
1216 *
1217 * If we detect that a transaction would be required to flush
1218 * the page, we have to check the process flags first, if we
1219 * are already in a transaction or disk I/O during allocations
1220 * is off, we need to fail the writepage and redirty the page.
1221 */
1222
1223STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001224xfs_vm_writepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001225 struct page *page,
1226 struct writeback_control *wbc)
1227{
1228 int error;
1229 int need_trans;
1230 int delalloc, unmapped, unwritten;
1231 struct inode *inode = page->mapping->host;
1232
1233 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1234
1235 /*
1236 * We need a transaction if:
1237 * 1. There are delalloc buffers on the page
1238 * 2. The page is uptodate and we have unmapped buffers
1239 * 3. The page is uptodate and we have no buffers
1240 * 4. There are unwritten buffers on the page
1241 */
1242
1243 if (!page_has_buffers(page)) {
1244 unmapped = 1;
1245 need_trans = 1;
1246 } else {
1247 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1248 if (!PageUptodate(page))
1249 unmapped = 0;
1250 need_trans = delalloc + unmapped + unwritten;
1251 }
1252
1253 /*
1254 * If we need a transaction and the process flags say
1255 * we are already in a transaction, or no IO is allowed
1256 * then mark the page dirty again and leave the page
1257 * as is.
1258 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001259 if (current_test_flags(PF_FSTRANS) && need_trans)
Nathan Scottf51623b2006-03-14 13:26:27 +11001260 goto out_fail;
1261
1262 /*
1263 * Delay hooking up buffer heads until we have
1264 * made our go/no-go decision.
1265 */
1266 if (!page_has_buffers(page))
1267 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1268
Eric Sandeenc8a40512009-07-31 00:02:17 -05001269
1270 /*
1271 * VM calculation for nr_to_write seems off. Bump it way
1272 * up, this gets simple streaming writes zippy again.
1273 * To be reviewed again after Jens' writeback changes.
1274 */
1275 wbc->nr_to_write *= 4;
1276
Nathan Scottf51623b2006-03-14 13:26:27 +11001277 /*
1278 * Convert delayed allocate, unwritten or unmapped space
1279 * to real space and flush out to disk.
1280 */
1281 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1282 if (error == -EAGAIN)
1283 goto out_fail;
1284 if (unlikely(error < 0))
1285 goto out_unlock;
1286
1287 return 0;
1288
1289out_fail:
1290 redirty_page_for_writepage(wbc, page);
1291 unlock_page(page);
1292 return 0;
1293out_unlock:
1294 unlock_page(page);
1295 return error;
1296}
1297
Nathan Scott7d4fb402006-06-09 15:27:16 +10001298STATIC int
1299xfs_vm_writepages(
1300 struct address_space *mapping,
1301 struct writeback_control *wbc)
1302{
Christoph Hellwigb3aea4e2007-08-29 11:44:37 +10001303 xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001304 return generic_writepages(mapping, wbc);
1305}
1306
Nathan Scottf51623b2006-03-14 13:26:27 +11001307/*
1308 * Called to move a page into cleanable state - and from there
1309 * to be released. Possibly the page is already clean. We always
1310 * have buffer heads in this call.
1311 *
1312 * Returns 0 if the page is ok to release, 1 otherwise.
1313 *
1314 * Possible scenarios are:
1315 *
1316 * 1. We are being called to release a page which has been written
1317 * to via regular I/O. buffer heads will be dirty and possibly
1318 * delalloc. If no delalloc buffer heads in this case then we
1319 * can just return zero.
1320 *
1321 * 2. We are called to release a page which has been written via
1322 * mmap, all we need to do is ensure there is no delalloc
1323 * state in the buffer heads, if not we can let the caller
1324 * free them and we should come back later via writepage.
1325 */
1326STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001327xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001328 struct page *page,
1329 gfp_t gfp_mask)
1330{
1331 struct inode *inode = page->mapping->host;
1332 int dirty, delalloc, unmapped, unwritten;
1333 struct writeback_control wbc = {
1334 .sync_mode = WB_SYNC_ALL,
1335 .nr_to_write = 1,
1336 };
1337
Nathan Scotted9d88f2006-09-28 10:56:43 +10001338 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, 0);
Nathan Scottf51623b2006-03-14 13:26:27 +11001339
Nathan Scott238f4c52006-03-17 17:26:25 +11001340 if (!page_has_buffers(page))
1341 return 0;
1342
Nathan Scottf51623b2006-03-14 13:26:27 +11001343 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1344 if (!delalloc && !unwritten)
1345 goto free_buffers;
1346
1347 if (!(gfp_mask & __GFP_FS))
1348 return 0;
1349
1350 /* If we are already inside a transaction or the thread cannot
1351 * do I/O, we cannot release this page.
1352 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001353 if (current_test_flags(PF_FSTRANS))
Nathan Scottf51623b2006-03-14 13:26:27 +11001354 return 0;
1355
1356 /*
1357 * Convert delalloc space to real space, do not flush the
1358 * data out to disk, that will be done by the caller.
1359 * Never need to allocate space here - we will always
1360 * come back to writepage in that case.
1361 */
1362 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1363 if (dirty == 0 && !unwritten)
1364 goto free_buffers;
1365 return 0;
1366
1367free_buffers:
1368 return try_to_free_buffers(page);
1369}
1370
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001372__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct inode *inode,
1374 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 struct buffer_head *bh_result,
1376 int create,
1377 int direct,
1378 bmapi_flags_t flags)
1379{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 xfs_iomap_t iomap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001381 xfs_off_t offset;
1382 ssize_t size;
Nathan Scottc2536662006-03-29 10:44:40 +10001383 int niomap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001386 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001387 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1388 size = bh_result->b_size;
Lachlan McIlroy364f3582008-09-17 16:50:14 +10001389
1390 if (!create && direct && offset >= i_size_read(inode))
1391 return 0;
1392
Lachlan McIlroy541d7d32007-10-11 17:34:33 +10001393 error = xfs_iomap(XFS_I(inode), offset, size,
Nathan Scott67fcaa72006-06-09 17:00:52 +10001394 create ? flags : BMAPI_READ, &iomap, &niomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (error)
1396 return -error;
Nathan Scottc2536662006-03-29 10:44:40 +10001397 if (niomap == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 return 0;
1399
1400 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001401 /*
1402 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * the read case (treat as if we're reading into a hole).
1404 */
1405 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001406 xfs_map_buffer(bh_result, &iomap, offset,
1407 inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 }
1409 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1410 if (direct)
1411 bh_result->b_private = inode;
1412 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414 }
1415
Nathan Scottc2536662006-03-29 10:44:40 +10001416 /*
1417 * If this is a realtime file, data may be on a different device.
1418 * to that pointed to from the buffer_head b_bdev currently.
1419 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001420 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
Nathan Scottc2536662006-03-29 10:44:40 +10001422 /*
David Chinner549054a2007-02-10 18:36:35 +11001423 * If we previously allocated a block out beyond eof and we are now
1424 * coming back to use it then we will need to flag it as new even if it
1425 * has a disk address.
1426 *
1427 * With sub-block writes into unwritten extents we also need to mark
1428 * the buffer as new so that the unwritten parts of the buffer gets
1429 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 */
1431 if (create &&
1432 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001433 (offset >= i_size_read(inode)) ||
1434 (iomap.iomap_flags & (IOMAP_NEW|IOMAP_UNWRITTEN))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
1437 if (iomap.iomap_flags & IOMAP_DELAY) {
1438 BUG_ON(direct);
1439 if (create) {
1440 set_buffer_uptodate(bh_result);
1441 set_buffer_mapped(bh_result);
1442 set_buffer_delay(bh_result);
1443 }
1444 }
1445
Nathan Scottc2536662006-03-29 10:44:40 +10001446 if (direct || size > (1 << inode->i_blkbits)) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001447 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1448 offset = min_t(xfs_off_t,
Nathan Scottc2536662006-03-29 10:44:40 +10001449 iomap.iomap_bsize - iomap.iomap_delta, size);
1450 bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
1452
1453 return 0;
1454}
1455
1456int
Nathan Scottc2536662006-03-29 10:44:40 +10001457xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 struct inode *inode,
1459 sector_t iblock,
1460 struct buffer_head *bh_result,
1461 int create)
1462{
Nathan Scottc2536662006-03-29 10:44:40 +10001463 return __xfs_get_blocks(inode, iblock,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -08001464 bh_result, create, 0, BMAPI_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465}
1466
1467STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001468xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 struct inode *inode,
1470 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 struct buffer_head *bh_result,
1472 int create)
1473{
Nathan Scottc2536662006-03-29 10:44:40 +10001474 return __xfs_get_blocks(inode, iblock,
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -08001475 bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476}
1477
Christoph Hellwigf0973862005-09-05 08:22:52 +10001478STATIC void
Nathan Scotte4c573b2006-03-14 13:54:26 +11001479xfs_end_io_direct(
Christoph Hellwigf0973862005-09-05 08:22:52 +10001480 struct kiocb *iocb,
1481 loff_t offset,
1482 ssize_t size,
1483 void *private)
1484{
1485 xfs_ioend_t *ioend = iocb->private;
1486
1487 /*
1488 * Non-NULL private data means we need to issue a transaction to
1489 * convert a range from unwritten to written extents. This needs
Nathan Scottc41564b2006-03-29 08:55:14 +10001490 * to happen from process context but aio+dio I/O completion
Christoph Hellwigf0973862005-09-05 08:22:52 +10001491 * happens from irq context so we need to defer it to a workqueue.
Nathan Scottc41564b2006-03-29 08:55:14 +10001492 * This is not necessary for synchronous direct I/O, but we do
Christoph Hellwigf0973862005-09-05 08:22:52 +10001493 * it anyway to keep the code uniform and simpler.
1494 *
David Chinnere927af92007-06-05 16:24:36 +10001495 * Well, if only it were that simple. Because synchronous direct I/O
1496 * requires extent conversion to occur *before* we return to userspace,
1497 * we have to wait for extent conversion to complete. Look at the
1498 * iocb that has been passed to us to determine if this is AIO or
1499 * not. If it is synchronous, tell xfs_finish_ioend() to kick the
1500 * workqueue and wait for it to complete.
1501 *
Christoph Hellwigf0973862005-09-05 08:22:52 +10001502 * The core direct I/O code might be changed to always call the
1503 * completion handler in the future, in which case all this can
1504 * go away.
1505 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001506 ioend->io_offset = offset;
1507 ioend->io_size = size;
1508 if (ioend->io_type == IOMAP_READ) {
David Chinnere927af92007-06-05 16:24:36 +10001509 xfs_finish_ioend(ioend, 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001510 } else if (private && size > 0) {
David Chinnere927af92007-06-05 16:24:36 +10001511 xfs_finish_ioend(ioend, is_sync_kiocb(iocb));
Christoph Hellwigf0973862005-09-05 08:22:52 +10001512 } else {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001513 /*
1514 * A direct I/O write ioend starts it's life in unwritten
1515 * state in case they map an unwritten extent. This write
1516 * didn't map an unwritten extent so switch it's completion
1517 * handler.
1518 */
1519 INIT_WORK(&ioend->io_work, xfs_end_bio_written);
David Chinnere927af92007-06-05 16:24:36 +10001520 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001521 }
1522
1523 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001524 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001525 * completion handler was called. Thus we need to protect
1526 * against double-freeing.
1527 */
1528 iocb->private = NULL;
1529}
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001532xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 int rw,
1534 struct kiocb *iocb,
1535 const struct iovec *iov,
1536 loff_t offset,
1537 unsigned long nr_segs)
1538{
1539 struct file *file = iocb->ki_filp;
1540 struct inode *inode = file->f_mapping->host;
Christoph Hellwig6214ed42007-09-14 15:23:17 +10001541 struct block_device *bdev;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001542 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Christoph Hellwig6214ed42007-09-14 15:23:17 +10001544 bdev = xfs_find_bdev_for_inode(XFS_I(inode));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001546 if (rw == WRITE) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001547 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001548 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
Christoph Hellwig6214ed42007-09-14 15:23:17 +10001549 bdev, iov, offset, nr_segs,
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001550 xfs_get_blocks_direct,
1551 xfs_end_io_direct);
1552 } else {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001553 iocb->private = xfs_alloc_ioend(inode, IOMAP_READ);
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001554 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
Christoph Hellwig6214ed42007-09-14 15:23:17 +10001555 bdev, iov, offset, nr_segs,
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001556 xfs_get_blocks_direct,
1557 xfs_end_io_direct);
1558 }
Christoph Hellwigf0973862005-09-05 08:22:52 +10001559
Zach Brown8459d862006-12-10 02:21:05 -08001560 if (unlikely(ret != -EIOCBQUEUED && iocb->private))
Christoph Hellwigf0973862005-09-05 08:22:52 +10001561 xfs_destroy_ioend(iocb->private);
1562 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Nathan Scottf51623b2006-03-14 13:26:27 +11001565STATIC int
Nick Piggind79689c2007-10-16 01:25:06 -07001566xfs_vm_write_begin(
Nathan Scottf51623b2006-03-14 13:26:27 +11001567 struct file *file,
Nick Piggind79689c2007-10-16 01:25:06 -07001568 struct address_space *mapping,
1569 loff_t pos,
1570 unsigned len,
1571 unsigned flags,
1572 struct page **pagep,
1573 void **fsdata)
Nathan Scottf51623b2006-03-14 13:26:27 +11001574{
Nick Piggind79689c2007-10-16 01:25:06 -07001575 *pagep = NULL;
1576 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
1577 xfs_get_blocks);
Nathan Scottf51623b2006-03-14 13:26:27 +11001578}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001581xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 struct address_space *mapping,
1583 sector_t block)
1584{
1585 struct inode *inode = (struct inode *)mapping->host;
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001586 struct xfs_inode *ip = XFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
Lachlan McIlroycf441ee2008-02-07 16:42:19 +11001588 xfs_itrace_entry(XFS_I(inode));
Christoph Hellwig126468b2008-03-06 13:44:57 +11001589 xfs_ilock(ip, XFS_IOLOCK_SHARED);
Christoph Hellwig739bfb22007-08-29 10:58:01 +10001590 xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
Christoph Hellwig126468b2008-03-06 13:44:57 +11001591 xfs_iunlock(ip, XFS_IOLOCK_SHARED);
Nathan Scottc2536662006-03-29 10:44:40 +10001592 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593}
1594
1595STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001596xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 struct file *unused,
1598 struct page *page)
1599{
Nathan Scottc2536662006-03-29 10:44:40 +10001600 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601}
1602
1603STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001604xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 struct file *unused,
1606 struct address_space *mapping,
1607 struct list_head *pages,
1608 unsigned nr_pages)
1609{
Nathan Scottc2536662006-03-29 10:44:40 +10001610 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612
NeilBrown2ff28e22006-03-26 01:37:18 -08001613STATIC void
Nathan Scott238f4c52006-03-17 17:26:25 +11001614xfs_vm_invalidatepage(
Nathan Scottbcec2b72005-09-02 16:40:17 +10001615 struct page *page,
1616 unsigned long offset)
1617{
1618 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1619 page->mapping->host, page, offset);
NeilBrown2ff28e22006-03-26 01:37:18 -08001620 block_invalidatepage(page, offset);
Nathan Scottbcec2b72005-09-02 16:40:17 +10001621}
1622
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001623const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001624 .readpage = xfs_vm_readpage,
1625 .readpages = xfs_vm_readpages,
1626 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001627 .writepages = xfs_vm_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 .sync_page = block_sync_page,
Nathan Scott238f4c52006-03-17 17:26:25 +11001629 .releasepage = xfs_vm_releasepage,
1630 .invalidatepage = xfs_vm_invalidatepage,
Nick Piggind79689c2007-10-16 01:25:06 -07001631 .write_begin = xfs_vm_write_begin,
1632 .write_end = generic_write_end,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001633 .bmap = xfs_vm_bmap,
1634 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001635 .migratepage = buffer_migrate_page,
Hisashi Hifumibddaafa2009-03-29 09:53:38 +02001636 .is_partially_uptodate = block_is_partially_uptodate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637};