blob: 3f13519436afc983ed5b1a62a4433387964b39d4 [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"
40#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110041#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042#include <linux/writeback.h>
43
Nathan Scottf51623b2006-03-14 13:26:27 +110044STATIC void
45xfs_count_page_state(
46 struct page *page,
47 int *delalloc,
48 int *unmapped,
49 int *unwritten)
50{
51 struct buffer_head *bh, *head;
52
53 *delalloc = *unmapped = *unwritten = 0;
54
55 bh = head = page_buffers(page);
56 do {
57 if (buffer_uptodate(bh) && !buffer_mapped(bh))
58 (*unmapped) = 1;
Nathan Scottf51623b2006-03-14 13:26:27 +110059 else if (buffer_unwritten(bh))
60 (*unwritten) = 1;
61 else if (buffer_delay(bh))
62 (*delalloc) = 1;
63 } while ((bh = bh->b_this_page) != head);
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#if defined(XFS_RW_TRACE)
67void
68xfs_page_trace(
69 int tag,
70 struct inode *inode,
71 struct page *page,
Nathan Scotted9d88f2006-09-28 10:56:43 +100072 unsigned long pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 xfs_inode_t *ip;
Nathan Scott67fcaa72006-06-09 17:00:52 +100075 bhv_vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 loff_t isize = i_size_read(inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110077 loff_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 int delalloc = -1, unmapped = -1, unwritten = -1;
79
80 if (page_has_buffers(page))
81 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
82
Christoph Hellwig75e17b32006-01-11 20:58:44 +110083 ip = xfs_vtoi(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (!ip->i_rwtrace)
85 return;
86
87 ktrace_enter(ip->i_rwtrace,
88 (void *)((unsigned long)tag),
89 (void *)ip,
90 (void *)inode,
91 (void *)page,
Nathan Scotted9d88f2006-09-28 10:56:43 +100092 (void *)pgoff,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
94 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
95 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
96 (void *)((unsigned long)(isize & 0xffffffff)),
97 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
98 (void *)((unsigned long)(offset & 0xffffffff)),
99 (void *)((unsigned long)delalloc),
100 (void *)((unsigned long)unmapped),
101 (void *)((unsigned long)unwritten),
Yingping Luf1fdc842006-03-22 12:44:15 +1100102 (void *)((unsigned long)current_pid()),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 (void *)NULL);
104}
105#else
Nathan Scotted9d88f2006-09-28 10:56:43 +1000106#define xfs_page_trace(tag, inode, page, pgoff)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107#endif
108
Christoph Hellwig0829c362005-09-02 16:58:49 +1000109/*
110 * Schedule IO completion handling on a xfsdatad if this was
David Chinnere927af92007-06-05 16:24:36 +1000111 * the final hold on this ioend. If we are asked to wait,
112 * flush the workqueue.
Christoph Hellwig0829c362005-09-02 16:58:49 +1000113 */
114STATIC void
115xfs_finish_ioend(
David Chinnere927af92007-06-05 16:24:36 +1000116 xfs_ioend_t *ioend,
117 int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
David Chinnere927af92007-06-05 16:24:36 +1000119 if (atomic_dec_and_test(&ioend->io_remaining)) {
Christoph Hellwig0829c362005-09-02 16:58:49 +1000120 queue_work(xfsdatad_workqueue, &ioend->io_work);
David Chinnere927af92007-06-05 16:24:36 +1000121 if (wait)
122 flush_workqueue(xfsdatad_workqueue);
123 }
Christoph Hellwig0829c362005-09-02 16:58:49 +1000124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100126/*
127 * 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;
137
138 for (bh = ioend->io_buffer_head; bh; bh = next) {
139 next = bh->b_private;
Nathan Scott7d04a332006-06-09 14:58:38 +1000140 bh->b_end_io(bh, !ioend->io_error);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100141 }
Nathan Scott7d04a332006-06-09 14:58:38 +1000142 if (unlikely(ioend->io_error))
143 vn_ioerror(ioend->io_vnode, ioend->io_error, __FILE__,__LINE__);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000144 vn_iowake(ioend->io_vnode);
145 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148/*
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000149 * Update on-disk file size now that data has been written to disk.
150 * The current in-memory file size is i_size. If a write is beyond
151 * eof io_new_size will be the intended file size until i_size is
152 * updated. If this write does not extend all the way to the valid
153 * file size then restrict this update to the end of the write.
154 */
155STATIC void
156xfs_setfilesize(
157 xfs_ioend_t *ioend)
158{
159 xfs_inode_t *ip;
160 xfs_fsize_t isize;
161 xfs_fsize_t bsize;
162
163 ip = xfs_vtoi(ioend->io_vnode);
David Chinnerb2826132007-06-05 16:24:44 +1000164 if (!ip)
165 return;
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000166
167 ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
168 ASSERT(ioend->io_type != IOMAP_READ);
169
170 if (unlikely(ioend->io_error))
171 return;
172
173 bsize = ioend->io_offset + ioend->io_size;
174
175 xfs_ilock(ip, XFS_ILOCK_EXCL);
176
177 isize = MAX(ip->i_size, ip->i_iocore.io_new_size);
178 isize = MIN(isize, bsize);
179
180 if (ip->i_d.di_size < isize) {
181 ip->i_d.di_size = isize;
182 ip->i_update_core = 1;
183 ip->i_update_size = 1;
Lachlan McIlroy776a75fa2007-09-14 15:22:50 +1000184 mark_inode_dirty_sync(vn_to_inode(ioend->io_vnode));
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000185 }
186
187 xfs_iunlock(ip, XFS_ILOCK_EXCL);
188}
189
190/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100191 * Buffered IO write completion for delayed allocate extents.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100192 */
193STATIC void
194xfs_end_bio_delalloc(
David Howellsc4028952006-11-22 14:57:56 +0000195 struct work_struct *work)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100196{
David Howellsc4028952006-11-22 14:57:56 +0000197 xfs_ioend_t *ioend =
198 container_of(work, xfs_ioend_t, io_work);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100199
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000200 xfs_setfilesize(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100201 xfs_destroy_ioend(ioend);
202}
203
204/*
205 * Buffered IO write completion for regular, written extents.
206 */
207STATIC void
208xfs_end_bio_written(
David Howellsc4028952006-11-22 14:57:56 +0000209 struct work_struct *work)
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100210{
David Howellsc4028952006-11-22 14:57:56 +0000211 xfs_ioend_t *ioend =
212 container_of(work, xfs_ioend_t, io_work);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100213
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000214 xfs_setfilesize(ioend);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100215 xfs_destroy_ioend(ioend);
216}
217
218/*
219 * IO write completion for unwritten extents.
220 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 * Issue transactions to convert a buffer range from unwritten
Christoph Hellwigf0973862005-09-05 08:22:52 +1000222 * to written extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 */
224STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000225xfs_end_bio_unwritten(
David Howellsc4028952006-11-22 14:57:56 +0000226 struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
David Howellsc4028952006-11-22 14:57:56 +0000228 xfs_ioend_t *ioend =
229 container_of(work, xfs_ioend_t, io_work);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000230 bhv_vnode_t *vp = ioend->io_vnode;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000231 xfs_off_t offset = ioend->io_offset;
232 size_t size = ioend->io_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000234 if (likely(!ioend->io_error)) {
Nathan Scott67fcaa72006-06-09 17:00:52 +1000235 bhv_vop_bmap(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000236 xfs_setfilesize(ioend);
237 }
238 xfs_destroy_ioend(ioend);
239}
240
241/*
242 * IO read completion for regular, written extents.
243 */
244STATIC void
245xfs_end_bio_read(
246 struct work_struct *work)
247{
248 xfs_ioend_t *ioend =
249 container_of(work, xfs_ioend_t, io_work);
250
Christoph Hellwig0829c362005-09-02 16:58:49 +1000251 xfs_destroy_ioend(ioend);
252}
253
254/*
255 * Allocate and initialise an IO completion structure.
256 * We need to track unwritten extent write completion here initially.
257 * We'll need to extend this for updating the ondisk inode size later
258 * (vs. incore size).
259 */
260STATIC xfs_ioend_t *
261xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100262 struct inode *inode,
263 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000264{
265 xfs_ioend_t *ioend;
266
267 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
268
269 /*
270 * Set the count to 1 initially, which will prevent an I/O
271 * completion callback from happening before we have started
272 * all the I/O from calling the completion routine too early.
273 */
274 atomic_set(&ioend->io_remaining, 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000275 ioend->io_error = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100276 ioend->io_list = NULL;
277 ioend->io_type = type;
Nathan Scottec86dc02006-03-17 17:25:36 +1100278 ioend->io_vnode = vn_from_inode(inode);
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000279 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100280 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000281 atomic_inc(&ioend->io_vnode->v_iocount);
282 ioend->io_offset = 0;
283 ioend->io_size = 0;
284
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100285 if (type == IOMAP_UNWRITTEN)
David Howellsc4028952006-11-22 14:57:56 +0000286 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100287 else if (type == IOMAP_DELAY)
David Howellsc4028952006-11-22 14:57:56 +0000288 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +1000289 else if (type == IOMAP_READ)
290 INIT_WORK(&ioend->io_work, xfs_end_bio_read);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100291 else
David Howellsc4028952006-11-22 14:57:56 +0000292 INIT_WORK(&ioend->io_work, xfs_end_bio_written);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000293
294 return ioend;
295}
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297STATIC int
298xfs_map_blocks(
299 struct inode *inode,
300 loff_t offset,
301 ssize_t count,
302 xfs_iomap_t *mapp,
303 int flags)
304{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000305 bhv_vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 int error, nmaps = 1;
307
Nathan Scott67fcaa72006-06-09 17:00:52 +1000308 error = bhv_vop_bmap(vp, offset, count, flags, mapp, &nmaps);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
310 VMODIFY(vp);
311 return -error;
312}
313
David Chinner7989cb82007-02-10 18:34:56 +1100314STATIC_INLINE int
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100315xfs_iomap_valid(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 xfs_iomap_t *iomapp,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100317 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100319 return offset >= iomapp->iomap_offset &&
320 offset < iomapp->iomap_offset + iomapp->iomap_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100323/*
324 * BIO completion handler for buffered IO.
325 */
326STATIC int
327xfs_end_bio(
328 struct bio *bio,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100329 int error)
330{
331 xfs_ioend_t *ioend = bio->bi_private;
332
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100333 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
Nathan Scott7d04a332006-06-09 14:58:38 +1000334 ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100335
336 /* Toss bio and pass work off to an xfsdatad thread */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100337 bio->bi_private = NULL;
338 bio->bi_end_io = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100339 bio_put(bio);
Nathan Scott7d04a332006-06-09 14:58:38 +1000340
David Chinnere927af92007-06-05 16:24:36 +1000341 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100342 return 0;
343}
344
345STATIC void
346xfs_submit_ioend_bio(
347 xfs_ioend_t *ioend,
348 struct bio *bio)
349{
350 atomic_inc(&ioend->io_remaining);
351
352 bio->bi_private = ioend;
353 bio->bi_end_io = xfs_end_bio;
354
355 submit_bio(WRITE, bio);
356 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
357 bio_put(bio);
358}
359
360STATIC struct bio *
361xfs_alloc_ioend_bio(
362 struct buffer_head *bh)
363{
364 struct bio *bio;
365 int nvecs = bio_get_nr_vecs(bh->b_bdev);
366
367 do {
368 bio = bio_alloc(GFP_NOIO, nvecs);
369 nvecs >>= 1;
370 } while (!bio);
371
372 ASSERT(bio->bi_private == NULL);
373 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
374 bio->bi_bdev = bh->b_bdev;
375 bio_get(bio);
376 return bio;
377}
378
379STATIC void
380xfs_start_buffer_writeback(
381 struct buffer_head *bh)
382{
383 ASSERT(buffer_mapped(bh));
384 ASSERT(buffer_locked(bh));
385 ASSERT(!buffer_delay(bh));
386 ASSERT(!buffer_unwritten(bh));
387
388 mark_buffer_async_write(bh);
389 set_buffer_uptodate(bh);
390 clear_buffer_dirty(bh);
391}
392
393STATIC void
394xfs_start_page_writeback(
395 struct page *page,
396 struct writeback_control *wbc,
397 int clear_dirty,
398 int buffers)
399{
400 ASSERT(PageLocked(page));
401 ASSERT(!PageWriteback(page));
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100402 if (clear_dirty)
David Chinner92132022006-12-21 10:24:01 +1100403 clear_page_dirty_for_io(page);
404 set_page_writeback(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100405 unlock_page(page);
406 if (!buffers) {
407 end_page_writeback(page);
408 wbc->pages_skipped++; /* We didn't write this page */
409 }
410}
411
412static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
413{
414 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
415}
416
417/*
David Chinnerd88992f2006-01-18 13:38:12 +1100418 * Submit all of the bios for all of the ioends we have saved up, covering the
419 * initial writepage page and also any probed pages.
420 *
421 * Because we may have multiple ioends spanning a page, we need to start
422 * writeback on all the buffers before we submit them for I/O. If we mark the
423 * buffers as we got, then we can end up with a page that only has buffers
424 * marked async write and I/O complete on can occur before we mark the other
425 * buffers async write.
426 *
427 * The end result of this is that we trip a bug in end_page_writeback() because
428 * we call it twice for the one page as the code in end_buffer_async_write()
429 * assumes that all buffers on the page are started at the same time.
430 *
431 * The fix is two passes across the ioend list - one to start writeback on the
Nathan Scottc41564b2006-03-29 08:55:14 +1000432 * buffer_heads, and then submit them for I/O on the second pass.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100433 */
434STATIC void
435xfs_submit_ioend(
436 xfs_ioend_t *ioend)
437{
David Chinnerd88992f2006-01-18 13:38:12 +1100438 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100439 xfs_ioend_t *next;
440 struct buffer_head *bh;
441 struct bio *bio;
442 sector_t lastblock = 0;
443
David Chinnerd88992f2006-01-18 13:38:12 +1100444 /* Pass 1 - start writeback */
445 do {
446 next = ioend->io_list;
447 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
448 xfs_start_buffer_writeback(bh);
449 }
450 } while ((ioend = next) != NULL);
451
452 /* Pass 2 - submit I/O */
453 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100454 do {
455 next = ioend->io_list;
456 bio = NULL;
457
458 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100459
460 if (!bio) {
461 retry:
462 bio = xfs_alloc_ioend_bio(bh);
463 } else if (bh->b_blocknr != lastblock + 1) {
464 xfs_submit_ioend_bio(ioend, bio);
465 goto retry;
466 }
467
468 if (bio_add_buffer(bio, bh) != bh->b_size) {
469 xfs_submit_ioend_bio(ioend, bio);
470 goto retry;
471 }
472
473 lastblock = bh->b_blocknr;
474 }
475 if (bio)
476 xfs_submit_ioend_bio(ioend, bio);
David Chinnere927af92007-06-05 16:24:36 +1000477 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100478 } while ((ioend = next) != NULL);
479}
480
481/*
482 * Cancel submission of all buffer_heads so far in this endio.
483 * Toss the endio too. Only ever called for the initial page
484 * in a writepage request, so only ever one page.
485 */
486STATIC void
487xfs_cancel_ioend(
488 xfs_ioend_t *ioend)
489{
490 xfs_ioend_t *next;
491 struct buffer_head *bh, *next_bh;
492
493 do {
494 next = ioend->io_list;
495 bh = ioend->io_buffer_head;
496 do {
497 next_bh = bh->b_private;
498 clear_buffer_async_write(bh);
499 unlock_buffer(bh);
500 } while ((bh = next_bh) != NULL);
501
502 vn_iowake(ioend->io_vnode);
503 mempool_free(ioend, xfs_ioend_pool);
504 } while ((ioend = next) != NULL);
505}
506
507/*
508 * Test to see if we've been building up a completion structure for
509 * earlier buffers -- if so, we try to append to this ioend if we
510 * can, otherwise we finish off any current ioend and start another.
511 * Return true if we've finished the given ioend.
512 */
513STATIC void
514xfs_add_to_ioend(
515 struct inode *inode,
516 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100517 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100518 unsigned int type,
519 xfs_ioend_t **result,
520 int need_ioend)
521{
522 xfs_ioend_t *ioend = *result;
523
524 if (!ioend || need_ioend || type != ioend->io_type) {
525 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100526
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100527 ioend = xfs_alloc_ioend(inode, type);
528 ioend->io_offset = offset;
529 ioend->io_buffer_head = bh;
530 ioend->io_buffer_tail = bh;
531 if (previous)
532 previous->io_list = ioend;
533 *result = ioend;
534 } else {
535 ioend->io_buffer_tail->b_private = bh;
536 ioend->io_buffer_tail = bh;
537 }
538
539 bh->b_private = NULL;
540 ioend->io_size += bh->b_size;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100544xfs_map_buffer(
545 struct buffer_head *bh,
546 xfs_iomap_t *mp,
547 xfs_off_t offset,
548 uint block_bits)
549{
550 sector_t bn;
551
552 ASSERT(mp->iomap_bn != IOMAP_DADDR_NULL);
553
554 bn = (mp->iomap_bn >> (block_bits - BBSHIFT)) +
555 ((offset - mp->iomap_offset) >> block_bits);
556
557 ASSERT(bn || (mp->iomap_flags & IOMAP_REALTIME));
558
559 bh->b_blocknr = bn;
560 set_buffer_mapped(bh);
561}
562
563STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564xfs_map_at_offset(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct buffer_head *bh,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100566 loff_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 int block_bits,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100568 xfs_iomap_t *iomapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
571 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
573 lock_buffer(bh);
Nathan Scott87cbc492006-03-14 13:26:43 +1100574 xfs_map_buffer(bh, iomapp, offset, block_bits);
Nathan Scottce8e9222006-01-11 15:39:08 +1100575 bh->b_bdev = iomapp->iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 set_buffer_mapped(bh);
577 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100578 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581/*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100582 * Look for a page at index that is suitable for clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 */
584STATIC unsigned int
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100585xfs_probe_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100586 struct page *page,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100587 unsigned int pg_offset,
588 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 int ret = 0;
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100593 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (page->mapping && PageDirty(page)) {
596 if (page_has_buffers(page)) {
597 struct buffer_head *bh, *head;
598
599 bh = head = page_buffers(page);
600 do {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100601 if (!buffer_uptodate(bh))
602 break;
603 if (mapped != buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 ret += bh->b_size;
606 if (ret >= pg_offset)
607 break;
608 } while ((bh = bh->b_this_page) != head);
609 } else
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100610 ret = mapped ? 0 : PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 }
612
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 return ret;
614}
615
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100616STATIC size_t
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100617xfs_probe_cluster(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 struct inode *inode,
619 struct page *startpage,
620 struct buffer_head *bh,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100621 struct buffer_head *head,
622 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100624 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100626 size_t total = 0;
627 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* First sum forwards in this page */
630 do {
Eric Sandeen2353e8e2006-02-28 12:30:30 +1100631 if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100632 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 total += bh->b_size;
634 } while ((bh = bh->b_this_page) != head);
635
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100636 /* if we reached the end of the page, sum forwards in following pages */
637 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
638 tindex = startpage->index + 1;
639
640 /* Prune this back to avoid pathological behavior */
641 tloff = min(tlast, startpage->index + 64);
642
643 pagevec_init(&pvec, 0);
644 while (!done && tindex <= tloff) {
645 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
646
647 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
648 break;
649
650 for (i = 0; i < pagevec_count(&pvec); i++) {
651 struct page *page = pvec.pages[i];
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000652 size_t pg_offset, pg_len = 0;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100653
654 if (tindex == tlast) {
655 pg_offset =
656 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100657 if (!pg_offset) {
658 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100659 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100660 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100661 } else
662 pg_offset = PAGE_CACHE_SIZE;
663
664 if (page->index == tindex && !TestSetPageLocked(page)) {
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000665 pg_len = xfs_probe_page(page, pg_offset, mapped);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100666 unlock_page(page);
667 }
668
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000669 if (!pg_len) {
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100670 done = 1;
671 break;
672 }
673
Christoph Hellwig265c1fa2007-08-16 15:38:19 +1000674 total += pg_len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100675 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100677
678 pagevec_release(&pvec);
679 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100681
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return total;
683}
684
685/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100686 * Test if a given page is suitable for writing as part of an unwritten
687 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100689STATIC int
690xfs_is_delayed_page(
691 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100692 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100695 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
697 if (page->mapping && page_has_buffers(page)) {
698 struct buffer_head *bh, *head;
699 int acceptable = 0;
700
701 bh = head = page_buffers(page);
702 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100703 if (buffer_unwritten(bh))
704 acceptable = (type == IOMAP_UNWRITTEN);
705 else if (buffer_delay(bh))
706 acceptable = (type == IOMAP_DELAY);
David Chinner2ddee842006-03-22 12:47:40 +1100707 else if (buffer_dirty(bh) && buffer_mapped(bh))
David Chinnerdf3c7242007-05-24 15:27:03 +1000708 acceptable = (type == IOMAP_NEW);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100709 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 } while ((bh = bh->b_this_page) != head);
712
713 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100714 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
716
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100717 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720/*
721 * Allocate & map buffers for page given the extent map. Write it out.
722 * except for the original page of a writepage, this is called on
723 * delalloc/unwritten pages only, for the original page it is possible
724 * that the page has no mapping at all.
725 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100726STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727xfs_convert_page(
728 struct inode *inode,
729 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100730 loff_t tindex,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100731 xfs_iomap_t *mp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100732 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int startio,
735 int all_bh)
736{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100737 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100738 xfs_off_t end_offset;
739 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100740 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 int bbits = inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700742 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100743 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100744 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100746 if (page->index != tindex)
747 goto fail;
748 if (TestSetPageLocked(page))
749 goto fail;
750 if (PageWriteback(page))
751 goto fail_unlock_page;
752 if (page->mapping != inode->i_mapping)
753 goto fail_unlock_page;
754 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
755 goto fail_unlock_page;
756
Nathan Scott24e17b52005-05-05 13:33:20 -0700757 /*
758 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000759 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100760 *
761 * Derivation:
762 *
763 * End offset is the highest offset that this page should represent.
764 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
765 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
766 * hence give us the correct page_dirty count. On any other page,
767 * it will be zero and in that case we need page_dirty to be the
768 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700769 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100770 end_offset = min_t(unsigned long long,
771 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
772 i_size_read(inode));
773
Nathan Scott24e17b52005-05-05 13:33:20 -0700774 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100775 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
776 PAGE_CACHE_SIZE);
777 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
778 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 bh = head = page_buffers(page);
781 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100782 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100784 if (!buffer_uptodate(bh))
785 uptodate = 0;
786 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
787 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100789 }
790
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100791 if (buffer_unwritten(bh) || buffer_delay(bh)) {
792 if (buffer_unwritten(bh))
793 type = IOMAP_UNWRITTEN;
794 else
795 type = IOMAP_DELAY;
796
797 if (!xfs_iomap_valid(mp, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100798 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100799 continue;
800 }
801
802 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
803 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
804
805 xfs_map_at_offset(bh, offset, bbits, mp);
806 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100807 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100808 type, ioendp, done);
809 } else {
810 set_buffer_dirty(bh);
811 unlock_buffer(bh);
812 mark_buffer_dirty(bh);
813 }
814 page_dirty--;
815 count++;
816 } else {
David Chinnerdf3c7242007-05-24 15:27:03 +1000817 type = IOMAP_NEW;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100818 if (buffer_mapped(bh) && all_bh && startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100820 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100821 type, ioendp, done);
822 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700823 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100824 } else {
825 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100828 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100830 if (uptodate && bh == head)
831 SetPageUptodate(page);
832
833 if (startio) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100834 if (count) {
835 struct backing_dev_info *bdi;
836
837 bdi = inode->i_mapping->backing_dev_info;
David Chinner9fddaca2006-02-07 20:27:24 +1100838 wbc->nr_to_write--;
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100839 if (bdi_write_congested(bdi)) {
840 wbc->encountered_congestion = 1;
841 done = 1;
David Chinner9fddaca2006-02-07 20:27:24 +1100842 } else if (wbc->nr_to_write <= 0) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100843 done = 1;
844 }
845 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100846 xfs_start_page_writeback(page, wbc, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100848
849 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100850 fail_unlock_page:
851 unlock_page(page);
852 fail:
853 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
856/*
857 * Convert & write out a cluster of pages in the same extent as defined
858 * by mp and following the start page.
859 */
860STATIC void
861xfs_cluster_write(
862 struct inode *inode,
863 pgoff_t tindex,
864 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100865 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 struct writeback_control *wbc,
867 int startio,
868 int all_bh,
869 pgoff_t tlast)
870{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100871 struct pagevec pvec;
872 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100874 pagevec_init(&pvec, 0);
875 while (!done && tindex <= tlast) {
876 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
877
878 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100880
881 for (i = 0; i < pagevec_count(&pvec); i++) {
882 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
883 iomapp, ioendp, wbc, startio, all_bh);
884 if (done)
885 break;
886 }
887
888 pagevec_release(&pvec);
889 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891}
892
893/*
894 * Calling this without startio set means we are being asked to make a dirty
895 * page ready for freeing it's buffers. When called with startio set then
896 * we are coming from writepage.
897 *
898 * When called with startio set it is important that we write the WHOLE
899 * page if possible.
900 * The bh->b_state's cannot know if any of the blocks or which block for
901 * that matter are dirty due to mmap writes, and therefore bh uptodate is
Nathan Scottc41564b2006-03-29 08:55:14 +1000902 * only valid if the page itself isn't completely uptodate. Some layers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 * may clear the page dirty flag prior to calling write page, under the
904 * assumption the entire page will be written out; by not writing out the
905 * whole page the page can be reused before all valid dirty data is
906 * written out. Note: in the case of a page that has been dirty'd by
907 * mapwrite and but partially setup by block_prepare_write the
908 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
909 * valid state, thus the whole page must be written out thing.
910 */
911
912STATIC int
913xfs_page_state_convert(
914 struct inode *inode,
915 struct page *page,
916 struct writeback_control *wbc,
917 int startio,
918 int unmapped) /* also implies page uptodate */
919{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100920 struct buffer_head *bh, *head;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100921 xfs_iomap_t iomap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100922 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 loff_t offset;
924 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100925 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 __uint64_t end_offset;
927 pgoff_t end_index, last_index, tlast;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100928 ssize_t size, len;
929 int flags, err, iomap_valid = 0, uptodate = 1;
Nathan Scott82721452006-04-11 15:10:55 +1000930 int page_dirty, count = 0;
931 int trylock = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100932 int all_bh = unmapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Nathan Scott82721452006-04-11 15:10:55 +1000934 if (startio) {
935 if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
936 trylock |= BMAPI_TRYLOCK;
937 }
Daniel Moore3ba08152005-05-05 13:31:34 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 /* Is this page beyond the end of the file? */
940 offset = i_size_read(inode);
941 end_index = offset >> PAGE_CACHE_SHIFT;
942 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
943 if (page->index >= end_index) {
944 if ((page->index >= end_index + 1) ||
945 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100946 if (startio)
947 unlock_page(page);
948 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 }
951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 /*
Nathan Scott24e17b52005-05-05 13:33:20 -0700953 * page_dirty is initially a count of buffers on the page before
Nathan Scottc41564b2006-03-29 08:55:14 +1000954 * EOF and is decremented as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100955 *
956 * Derivation:
957 *
958 * End offset is the highest offset that this page should represent.
959 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
960 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
961 * hence give us the correct page_dirty count. On any other page,
962 * it will be zero and in that case we need page_dirty to be the
963 * count of buffers on the page.
964 */
965 end_offset = min_t(unsigned long long,
966 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700967 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100968 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
969 PAGE_CACHE_SIZE);
970 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -0700971 page_dirty = p_offset / len;
972
Nathan Scott24e17b52005-05-05 13:33:20 -0700973 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100974 offset = page_offset(page);
David Chinnerdf3c7242007-05-24 15:27:03 +1000975 flags = BMAPI_READ;
976 type = IOMAP_NEW;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100977
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100978 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 do {
981 if (offset >= end_offset)
982 break;
983 if (!buffer_uptodate(bh))
984 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100985 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100986 /*
987 * the iomap is actually still valid, but the ioend
988 * isn't. shouldn't happen too often.
989 */
990 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100994 if (iomap_valid)
995 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
997 /*
998 * First case, map an unwritten extent and prepare for
999 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001000 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 * Second case, allocate space for a delalloc buffer.
1002 * We can return EAGAIN here in the release page case.
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001003 *
1004 * Third case, an unmapped buffer was found, and we are
1005 * in a path where we need to write the whole page out.
David Chinnerdf3c7242007-05-24 15:27:03 +10001006 */
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001007 if (buffer_unwritten(bh) || buffer_delay(bh) ||
1008 ((buffer_uptodate(bh) || PageUptodate(page)) &&
1009 !buffer_mapped(bh) && (unmapped || startio))) {
David Chinnereffd1202007-06-18 16:49:58 +10001010 int new_ioend = 0;
1011
David Chinnerdf3c7242007-05-24 15:27:03 +10001012 /*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001013 * Make sure we don't use a read-only iomap
1014 */
David Chinnerdf3c7242007-05-24 15:27:03 +10001015 if (flags == BMAPI_READ)
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001016 iomap_valid = 0;
1017
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001018 if (buffer_unwritten(bh)) {
1019 type = IOMAP_UNWRITTEN;
Nathan Scott82721452006-04-11 15:10:55 +10001020 flags = BMAPI_WRITE | BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001021 } else if (buffer_delay(bh)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001022 type = IOMAP_DELAY;
Nathan Scott82721452006-04-11 15:10:55 +10001023 flags = BMAPI_ALLOCATE | trylock;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001024 } else {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001025 type = IOMAP_NEW;
Nathan Scott82721452006-04-11 15:10:55 +10001026 flags = BMAPI_WRITE | BMAPI_MMAP;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001027 }
1028
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001029 if (!iomap_valid) {
David Chinnereffd1202007-06-18 16:49:58 +10001030 /*
1031 * if we didn't have a valid mapping then we
1032 * need to ensure that we put the new mapping
1033 * in a new ioend structure. This needs to be
1034 * done to ensure that the ioends correctly
1035 * reflect the block mappings at io completion
1036 * for unwritten extent conversion.
1037 */
1038 new_ioend = 1;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001039 if (type == IOMAP_NEW) {
1040 size = xfs_probe_cluster(inode,
1041 page, bh, head, 0);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001042 } else {
1043 size = len;
1044 }
1045
1046 err = xfs_map_blocks(inode, offset, size,
1047 &iomap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001048 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 goto error;
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001050 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001052 if (iomap_valid) {
1053 xfs_map_at_offset(bh, offset,
1054 inode->i_blkbits, &iomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001056 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001057 type, &ioend,
David Chinnereffd1202007-06-18 16:49:58 +10001058 new_ioend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 } else {
1060 set_buffer_dirty(bh);
1061 unlock_buffer(bh);
1062 mark_buffer_dirty(bh);
1063 }
1064 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001065 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001067 } else if (buffer_uptodate(bh) && startio) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001068 /*
1069 * we got here because the buffer is already mapped.
1070 * That means it must already have extents allocated
1071 * underneath it. Map the extent by reading it.
1072 */
David Chinnerdf3c7242007-05-24 15:27:03 +10001073 if (!iomap_valid || flags != BMAPI_READ) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001074 flags = BMAPI_READ;
1075 size = xfs_probe_cluster(inode, page, bh,
1076 head, 1);
1077 err = xfs_map_blocks(inode, offset, size,
1078 &iomap, flags);
1079 if (err)
1080 goto error;
1081 iomap_valid = xfs_iomap_valid(&iomap, offset);
1082 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
David Chinnerdf3c7242007-05-24 15:27:03 +10001084 /*
1085 * We set the type to IOMAP_NEW in case we are doing a
1086 * small write at EOF that is extending the file but
1087 * without needing an allocation. We need to update the
1088 * file size on I/O completion in this case so it is
1089 * the same case as having just allocated a new extent
1090 * that we are writing into for the first time.
1091 */
1092 type = IOMAP_NEW;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001093 if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
1094 ASSERT(buffer_mapped(bh));
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001095 if (iomap_valid)
1096 all_bh = 1;
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001097 xfs_add_to_ioend(inode, bh, offset, type,
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001098 &ioend, !iomap_valid);
1099 page_dirty--;
1100 count++;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001101 } else {
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001102 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001104 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
1105 (unmapped || startio)) {
1106 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001108
1109 if (!iohead)
1110 iohead = ioend;
1111
1112 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 if (uptodate && bh == head)
1115 SetPageUptodate(page);
1116
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001117 if (startio)
1118 xfs_start_page_writeback(page, wbc, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001120 if (ioend && iomap_valid) {
1121 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -07001123 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001124 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001125 wbc, startio, all_bh, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001128 if (iohead)
1129 xfs_submit_ioend(iohead);
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return page_dirty;
1132
1133error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001134 if (iohead)
1135 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 /*
1138 * If it's delalloc and we have nowhere to put it,
1139 * throw it away, unless the lower layers told
1140 * us to try again.
1141 */
1142 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001143 if (!unmapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 block_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 ClearPageUptodate(page);
1146 }
1147 return err;
1148}
1149
Nathan Scottf51623b2006-03-14 13:26:27 +11001150/*
1151 * writepage: Called from one of two places:
1152 *
1153 * 1. we are flushing a delalloc buffer head.
1154 *
1155 * 2. we are writing out a dirty page. Typically the page dirty
1156 * state is cleared before we get here. In this case is it
1157 * conceivable we have no buffer heads.
1158 *
1159 * For delalloc space on the page we need to allocate space and
1160 * flush it. For unmapped buffer heads on the page we should
1161 * allocate space if the page is uptodate. For any other dirty
1162 * buffer heads on the page we should flush them.
1163 *
1164 * If we detect that a transaction would be required to flush
1165 * the page, we have to check the process flags first, if we
1166 * are already in a transaction or disk I/O during allocations
1167 * is off, we need to fail the writepage and redirty the page.
1168 */
1169
1170STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001171xfs_vm_writepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001172 struct page *page,
1173 struct writeback_control *wbc)
1174{
1175 int error;
1176 int need_trans;
1177 int delalloc, unmapped, unwritten;
1178 struct inode *inode = page->mapping->host;
1179
1180 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1181
1182 /*
1183 * We need a transaction if:
1184 * 1. There are delalloc buffers on the page
1185 * 2. The page is uptodate and we have unmapped buffers
1186 * 3. The page is uptodate and we have no buffers
1187 * 4. There are unwritten buffers on the page
1188 */
1189
1190 if (!page_has_buffers(page)) {
1191 unmapped = 1;
1192 need_trans = 1;
1193 } else {
1194 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1195 if (!PageUptodate(page))
1196 unmapped = 0;
1197 need_trans = delalloc + unmapped + unwritten;
1198 }
1199
1200 /*
1201 * If we need a transaction and the process flags say
1202 * we are already in a transaction, or no IO is allowed
1203 * then mark the page dirty again and leave the page
1204 * as is.
1205 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001206 if (current_test_flags(PF_FSTRANS) && need_trans)
Nathan Scottf51623b2006-03-14 13:26:27 +11001207 goto out_fail;
1208
1209 /*
1210 * Delay hooking up buffer heads until we have
1211 * made our go/no-go decision.
1212 */
1213 if (!page_has_buffers(page))
1214 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1215
1216 /*
1217 * Convert delayed allocate, unwritten or unmapped space
1218 * to real space and flush out to disk.
1219 */
1220 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1221 if (error == -EAGAIN)
1222 goto out_fail;
1223 if (unlikely(error < 0))
1224 goto out_unlock;
1225
1226 return 0;
1227
1228out_fail:
1229 redirty_page_for_writepage(wbc, page);
1230 unlock_page(page);
1231 return 0;
1232out_unlock:
1233 unlock_page(page);
1234 return error;
1235}
1236
Nathan Scott7d4fb402006-06-09 15:27:16 +10001237STATIC int
1238xfs_vm_writepages(
1239 struct address_space *mapping,
1240 struct writeback_control *wbc)
1241{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001242 struct bhv_vnode *vp = vn_from_inode(mapping->host);
Nathan Scott7d4fb402006-06-09 15:27:16 +10001243
1244 if (VN_TRUNC(vp))
1245 VUNTRUNCATE(vp);
1246 return generic_writepages(mapping, wbc);
1247}
1248
Nathan Scottf51623b2006-03-14 13:26:27 +11001249/*
1250 * Called to move a page into cleanable state - and from there
1251 * to be released. Possibly the page is already clean. We always
1252 * have buffer heads in this call.
1253 *
1254 * Returns 0 if the page is ok to release, 1 otherwise.
1255 *
1256 * Possible scenarios are:
1257 *
1258 * 1. We are being called to release a page which has been written
1259 * to via regular I/O. buffer heads will be dirty and possibly
1260 * delalloc. If no delalloc buffer heads in this case then we
1261 * can just return zero.
1262 *
1263 * 2. We are called to release a page which has been written via
1264 * mmap, all we need to do is ensure there is no delalloc
1265 * state in the buffer heads, if not we can let the caller
1266 * free them and we should come back later via writepage.
1267 */
1268STATIC int
Nathan Scott238f4c52006-03-17 17:26:25 +11001269xfs_vm_releasepage(
Nathan Scottf51623b2006-03-14 13:26:27 +11001270 struct page *page,
1271 gfp_t gfp_mask)
1272{
1273 struct inode *inode = page->mapping->host;
1274 int dirty, delalloc, unmapped, unwritten;
1275 struct writeback_control wbc = {
1276 .sync_mode = WB_SYNC_ALL,
1277 .nr_to_write = 1,
1278 };
1279
Nathan Scotted9d88f2006-09-28 10:56:43 +10001280 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, 0);
Nathan Scottf51623b2006-03-14 13:26:27 +11001281
Nathan Scott238f4c52006-03-17 17:26:25 +11001282 if (!page_has_buffers(page))
1283 return 0;
1284
Nathan Scottf51623b2006-03-14 13:26:27 +11001285 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1286 if (!delalloc && !unwritten)
1287 goto free_buffers;
1288
1289 if (!(gfp_mask & __GFP_FS))
1290 return 0;
1291
1292 /* If we are already inside a transaction or the thread cannot
1293 * do I/O, we cannot release this page.
1294 */
Nathan Scott59c1b082006-06-09 14:59:13 +10001295 if (current_test_flags(PF_FSTRANS))
Nathan Scottf51623b2006-03-14 13:26:27 +11001296 return 0;
1297
1298 /*
1299 * Convert delalloc space to real space, do not flush the
1300 * data out to disk, that will be done by the caller.
1301 * Never need to allocate space here - we will always
1302 * come back to writepage in that case.
1303 */
1304 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1305 if (dirty == 0 && !unwritten)
1306 goto free_buffers;
1307 return 0;
1308
1309free_buffers:
1310 return try_to_free_buffers(page);
1311}
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313STATIC int
Nathan Scottc2536662006-03-29 10:44:40 +10001314__xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 struct inode *inode,
1316 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 struct buffer_head *bh_result,
1318 int create,
1319 int direct,
1320 bmapi_flags_t flags)
1321{
Nathan Scott67fcaa72006-06-09 17:00:52 +10001322 bhv_vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 xfs_iomap_t iomap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001324 xfs_off_t offset;
1325 ssize_t size;
Nathan Scottc2536662006-03-29 10:44:40 +10001326 int niomap = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001329 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scottc2536662006-03-29 10:44:40 +10001330 ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1331 size = bh_result->b_size;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001332 error = bhv_vop_bmap(vp, offset, size,
1333 create ? flags : BMAPI_READ, &iomap, &niomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 if (error)
1335 return -error;
Nathan Scottc2536662006-03-29 10:44:40 +10001336 if (niomap == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 return 0;
1338
1339 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001340 /*
1341 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 * the read case (treat as if we're reading into a hole).
1343 */
1344 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001345 xfs_map_buffer(bh_result, &iomap, offset,
1346 inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
1348 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1349 if (direct)
1350 bh_result->b_private = inode;
1351 set_buffer_unwritten(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 }
1354
Nathan Scottc2536662006-03-29 10:44:40 +10001355 /*
1356 * If this is a realtime file, data may be on a different device.
1357 * to that pointed to from the buffer_head b_bdev currently.
1358 */
Nathan Scottce8e9222006-01-11 15:39:08 +11001359 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
Nathan Scottc2536662006-03-29 10:44:40 +10001361 /*
David Chinner549054a2007-02-10 18:36:35 +11001362 * If we previously allocated a block out beyond eof and we are now
1363 * coming back to use it then we will need to flag it as new even if it
1364 * has a disk address.
1365 *
1366 * With sub-block writes into unwritten extents we also need to mark
1367 * the buffer as new so that the unwritten parts of the buffer gets
1368 * correctly zeroed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 */
1370 if (create &&
1371 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
David Chinner549054a2007-02-10 18:36:35 +11001372 (offset >= i_size_read(inode)) ||
1373 (iomap.iomap_flags & (IOMAP_NEW|IOMAP_UNWRITTEN))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 if (iomap.iomap_flags & IOMAP_DELAY) {
1377 BUG_ON(direct);
1378 if (create) {
1379 set_buffer_uptodate(bh_result);
1380 set_buffer_mapped(bh_result);
1381 set_buffer_delay(bh_result);
1382 }
1383 }
1384
Nathan Scottc2536662006-03-29 10:44:40 +10001385 if (direct || size > (1 << inode->i_blkbits)) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001386 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1387 offset = min_t(xfs_off_t,
Nathan Scottc2536662006-03-29 10:44:40 +10001388 iomap.iomap_bsize - iomap.iomap_delta, size);
1389 bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
1391
1392 return 0;
1393}
1394
1395int
Nathan Scottc2536662006-03-29 10:44:40 +10001396xfs_get_blocks(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 struct inode *inode,
1398 sector_t iblock,
1399 struct buffer_head *bh_result,
1400 int create)
1401{
Nathan Scottc2536662006-03-29 10:44:40 +10001402 return __xfs_get_blocks(inode, iblock,
Badari Pulavartyfa30bd02006-03-26 01:38:01 -08001403 bh_result, create, 0, BMAPI_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404}
1405
1406STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001407xfs_get_blocks_direct(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 struct inode *inode,
1409 sector_t iblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 struct buffer_head *bh_result,
1411 int create)
1412{
Nathan Scottc2536662006-03-29 10:44:40 +10001413 return __xfs_get_blocks(inode, iblock,
Badari Pulavarty1d8fa7a2006-03-26 01:38:02 -08001414 bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Christoph Hellwigf0973862005-09-05 08:22:52 +10001417STATIC void
Nathan Scotte4c573b2006-03-14 13:54:26 +11001418xfs_end_io_direct(
Christoph Hellwigf0973862005-09-05 08:22:52 +10001419 struct kiocb *iocb,
1420 loff_t offset,
1421 ssize_t size,
1422 void *private)
1423{
1424 xfs_ioend_t *ioend = iocb->private;
1425
1426 /*
1427 * Non-NULL private data means we need to issue a transaction to
1428 * convert a range from unwritten to written extents. This needs
Nathan Scottc41564b2006-03-29 08:55:14 +10001429 * to happen from process context but aio+dio I/O completion
Christoph Hellwigf0973862005-09-05 08:22:52 +10001430 * happens from irq context so we need to defer it to a workqueue.
Nathan Scottc41564b2006-03-29 08:55:14 +10001431 * This is not necessary for synchronous direct I/O, but we do
Christoph Hellwigf0973862005-09-05 08:22:52 +10001432 * it anyway to keep the code uniform and simpler.
1433 *
David Chinnere927af92007-06-05 16:24:36 +10001434 * Well, if only it were that simple. Because synchronous direct I/O
1435 * requires extent conversion to occur *before* we return to userspace,
1436 * we have to wait for extent conversion to complete. Look at the
1437 * iocb that has been passed to us to determine if this is AIO or
1438 * not. If it is synchronous, tell xfs_finish_ioend() to kick the
1439 * workqueue and wait for it to complete.
1440 *
Christoph Hellwigf0973862005-09-05 08:22:52 +10001441 * The core direct I/O code might be changed to always call the
1442 * completion handler in the future, in which case all this can
1443 * go away.
1444 */
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001445 ioend->io_offset = offset;
1446 ioend->io_size = size;
1447 if (ioend->io_type == IOMAP_READ) {
David Chinnere927af92007-06-05 16:24:36 +10001448 xfs_finish_ioend(ioend, 0);
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001449 } else if (private && size > 0) {
David Chinnere927af92007-06-05 16:24:36 +10001450 xfs_finish_ioend(ioend, is_sync_kiocb(iocb));
Christoph Hellwigf0973862005-09-05 08:22:52 +10001451 } else {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001452 /*
1453 * A direct I/O write ioend starts it's life in unwritten
1454 * state in case they map an unwritten extent. This write
1455 * didn't map an unwritten extent so switch it's completion
1456 * handler.
1457 */
1458 INIT_WORK(&ioend->io_work, xfs_end_bio_written);
David Chinnere927af92007-06-05 16:24:36 +10001459 xfs_finish_ioend(ioend, 0);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001460 }
1461
1462 /*
Nathan Scottc41564b2006-03-29 08:55:14 +10001463 * blockdev_direct_IO can return an error even after the I/O
Christoph Hellwigf0973862005-09-05 08:22:52 +10001464 * completion handler was called. Thus we need to protect
1465 * against double-freeing.
1466 */
1467 iocb->private = NULL;
1468}
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470STATIC ssize_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001471xfs_vm_direct_IO(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 int rw,
1473 struct kiocb *iocb,
1474 const struct iovec *iov,
1475 loff_t offset,
1476 unsigned long nr_segs)
1477{
1478 struct file *file = iocb->ki_filp;
1479 struct inode *inode = file->f_mapping->host;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001480 bhv_vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 xfs_iomap_t iomap;
1482 int maps = 1;
1483 int error;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001484 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Nathan Scott67fcaa72006-06-09 17:00:52 +10001486 error = bhv_vop_bmap(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 if (error)
1488 return -error;
1489
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001490 if (rw == WRITE) {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001491 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001492 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
1493 iomap.iomap_target->bt_bdev,
1494 iov, offset, nr_segs,
1495 xfs_get_blocks_direct,
1496 xfs_end_io_direct);
1497 } else {
Lachlan McIlroyba87ea62007-05-08 13:49:46 +10001498 iocb->private = xfs_alloc_ioend(inode, IOMAP_READ);
Lachlan McIlroy721259b2006-09-07 14:27:05 +10001499 ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
1500 iomap.iomap_target->bt_bdev,
1501 iov, offset, nr_segs,
1502 xfs_get_blocks_direct,
1503 xfs_end_io_direct);
1504 }
Christoph Hellwigf0973862005-09-05 08:22:52 +10001505
Zach Brown8459d862006-12-10 02:21:05 -08001506 if (unlikely(ret != -EIOCBQUEUED && iocb->private))
Christoph Hellwigf0973862005-09-05 08:22:52 +10001507 xfs_destroy_ioend(iocb->private);
1508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Nathan Scottf51623b2006-03-14 13:26:27 +11001511STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001512xfs_vm_prepare_write(
Nathan Scottf51623b2006-03-14 13:26:27 +11001513 struct file *file,
1514 struct page *page,
1515 unsigned int from,
1516 unsigned int to)
1517{
Nathan Scottc2536662006-03-29 10:44:40 +10001518 return block_prepare_write(page, from, to, xfs_get_blocks);
Nathan Scottf51623b2006-03-14 13:26:27 +11001519}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521STATIC sector_t
Nathan Scotte4c573b2006-03-14 13:54:26 +11001522xfs_vm_bmap(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 struct address_space *mapping,
1524 sector_t block)
1525{
1526 struct inode *inode = (struct inode *)mapping->host;
Nathan Scott67fcaa72006-06-09 17:00:52 +10001527 bhv_vnode_t *vp = vn_from_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Nathan Scotte4c573b2006-03-14 13:54:26 +11001529 vn_trace_entry(vp, __FUNCTION__, (inst_t *)__return_address);
Nathan Scott67fcaa72006-06-09 17:00:52 +10001530 bhv_vop_rwlock(vp, VRWLOCK_READ);
1531 bhv_vop_flush_pages(vp, (xfs_off_t)0, -1, 0, FI_REMAPF);
1532 bhv_vop_rwunlock(vp, VRWLOCK_READ);
Nathan Scottc2536662006-03-29 10:44:40 +10001533 return generic_block_bmap(mapping, block, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001537xfs_vm_readpage(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 struct file *unused,
1539 struct page *page)
1540{
Nathan Scottc2536662006-03-29 10:44:40 +10001541 return mpage_readpage(page, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544STATIC int
Nathan Scotte4c573b2006-03-14 13:54:26 +11001545xfs_vm_readpages(
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 struct file *unused,
1547 struct address_space *mapping,
1548 struct list_head *pages,
1549 unsigned nr_pages)
1550{
Nathan Scottc2536662006-03-29 10:44:40 +10001551 return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553
NeilBrown2ff28e22006-03-26 01:37:18 -08001554STATIC void
Nathan Scott238f4c52006-03-17 17:26:25 +11001555xfs_vm_invalidatepage(
Nathan Scottbcec2b72005-09-02 16:40:17 +10001556 struct page *page,
1557 unsigned long offset)
1558{
1559 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1560 page->mapping->host, page, offset);
NeilBrown2ff28e22006-03-26 01:37:18 -08001561 block_invalidatepage(page, offset);
Nathan Scottbcec2b72005-09-02 16:40:17 +10001562}
1563
Christoph Hellwigf5e54d62006-06-28 04:26:44 -07001564const struct address_space_operations xfs_address_space_operations = {
Nathan Scotte4c573b2006-03-14 13:54:26 +11001565 .readpage = xfs_vm_readpage,
1566 .readpages = xfs_vm_readpages,
1567 .writepage = xfs_vm_writepage,
Nathan Scott7d4fb402006-06-09 15:27:16 +10001568 .writepages = xfs_vm_writepages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 .sync_page = block_sync_page,
Nathan Scott238f4c52006-03-17 17:26:25 +11001570 .releasepage = xfs_vm_releasepage,
1571 .invalidatepage = xfs_vm_invalidatepage,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001572 .prepare_write = xfs_vm_prepare_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 .commit_write = generic_commit_write,
Nathan Scotte4c573b2006-03-14 13:54:26 +11001574 .bmap = xfs_vm_bmap,
1575 .direct_IO = xfs_vm_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001576 .migratepage = buffer_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577};