blob: 4b6bfdb8251c6771253476bbea652171794c85f0 [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_dir.h"
25#include "xfs_dir2.h"
26#include "xfs_trans.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_alloc_btree.h"
31#include "xfs_ialloc_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dir_sf.h"
33#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_alloc.h"
38#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
40#include "xfs_rw.h"
41#include "xfs_iomap.h"
42#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110043#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/writeback.h>
45
Nathan Scottf51623b2006-03-14 13:26:27 +110046
47STATIC void
48xfs_count_page_state(
49 struct page *page,
50 int *delalloc,
51 int *unmapped,
52 int *unwritten)
53{
54 struct buffer_head *bh, *head;
55
56 *delalloc = *unmapped = *unwritten = 0;
57
58 bh = head = page_buffers(page);
59 do {
60 if (buffer_uptodate(bh) && !buffer_mapped(bh))
61 (*unmapped) = 1;
62 else if (buffer_unwritten(bh) && !buffer_delay(bh))
63 clear_buffer_unwritten(bh);
64 else if (buffer_unwritten(bh))
65 (*unwritten) = 1;
66 else if (buffer_delay(bh))
67 (*delalloc) = 1;
68 } while ((bh = bh->b_this_page) != head);
69}
70
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73#if defined(XFS_RW_TRACE)
74void
75xfs_page_trace(
76 int tag,
77 struct inode *inode,
78 struct page *page,
79 int mask)
80{
81 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 vnode_t *vp = LINVFS_GET_VP(inode);
83 loff_t isize = i_size_read(inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110084 loff_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 int delalloc = -1, unmapped = -1, unwritten = -1;
86
87 if (page_has_buffers(page))
88 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
89
Christoph Hellwig75e17b32006-01-11 20:58:44 +110090 ip = xfs_vtoi(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (!ip->i_rwtrace)
92 return;
93
94 ktrace_enter(ip->i_rwtrace,
95 (void *)((unsigned long)tag),
96 (void *)ip,
97 (void *)inode,
98 (void *)page,
99 (void *)((unsigned long)mask),
100 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
101 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
102 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
103 (void *)((unsigned long)(isize & 0xffffffff)),
104 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
105 (void *)((unsigned long)(offset & 0xffffffff)),
106 (void *)((unsigned long)delalloc),
107 (void *)((unsigned long)unmapped),
108 (void *)((unsigned long)unwritten),
109 (void *)NULL,
110 (void *)NULL);
111}
112#else
113#define xfs_page_trace(tag, inode, page, mask)
114#endif
115
Christoph Hellwig0829c362005-09-02 16:58:49 +1000116/*
117 * Schedule IO completion handling on a xfsdatad if this was
118 * the final hold on this ioend.
119 */
120STATIC void
121xfs_finish_ioend(
122 xfs_ioend_t *ioend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000124 if (atomic_dec_and_test(&ioend->io_remaining))
125 queue_work(xfsdatad_workqueue, &ioend->io_work);
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100128/*
129 * We're now finished for good with this ioend structure.
130 * Update the page state via the associated buffer_heads,
131 * release holds on the inode and bio, and finally free
132 * up memory. Do not use the ioend after this.
133 */
Christoph Hellwig0829c362005-09-02 16:58:49 +1000134STATIC void
135xfs_destroy_ioend(
136 xfs_ioend_t *ioend)
137{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100138 struct buffer_head *bh, *next;
139
140 for (bh = ioend->io_buffer_head; bh; bh = next) {
141 next = bh->b_private;
142 bh->b_end_io(bh, ioend->io_uptodate);
143 }
144
Christoph Hellwig0829c362005-09-02 16:58:49 +1000145 vn_iowake(ioend->io_vnode);
146 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100150 * Buffered IO write completion for delayed allocate extents.
151 * TODO: Update ondisk isize now that we know the file data
152 * has been flushed (i.e. the notorious "NULL file" problem).
153 */
154STATIC void
155xfs_end_bio_delalloc(
156 void *data)
157{
158 xfs_ioend_t *ioend = data;
159
160 xfs_destroy_ioend(ioend);
161}
162
163/*
164 * Buffered IO write completion for regular, written extents.
165 */
166STATIC void
167xfs_end_bio_written(
168 void *data)
169{
170 xfs_ioend_t *ioend = data;
171
172 xfs_destroy_ioend(ioend);
173}
174
175/*
176 * IO write completion for unwritten extents.
177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * Issue transactions to convert a buffer range from unwritten
Christoph Hellwigf0973862005-09-05 08:22:52 +1000179 * to written extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
181STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000182xfs_end_bio_unwritten(
183 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000185 xfs_ioend_t *ioend = data;
186 vnode_t *vp = ioend->io_vnode;
187 xfs_off_t offset = ioend->io_offset;
188 size_t size = ioend->io_size;
189 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Christoph Hellwig0829c362005-09-02 16:58:49 +1000191 if (ioend->io_uptodate)
192 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
193 xfs_destroy_ioend(ioend);
194}
195
196/*
197 * Allocate and initialise an IO completion structure.
198 * We need to track unwritten extent write completion here initially.
199 * We'll need to extend this for updating the ondisk inode size later
200 * (vs. incore size).
201 */
202STATIC xfs_ioend_t *
203xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100204 struct inode *inode,
205 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000206{
207 xfs_ioend_t *ioend;
208
209 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
210
211 /*
212 * Set the count to 1 initially, which will prevent an I/O
213 * completion callback from happening before we have started
214 * all the I/O from calling the completion routine too early.
215 */
216 atomic_set(&ioend->io_remaining, 1);
217 ioend->io_uptodate = 1; /* cleared if any I/O fails */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100218 ioend->io_list = NULL;
219 ioend->io_type = type;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000220 ioend->io_vnode = LINVFS_GET_VP(inode);
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000221 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100222 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000223 atomic_inc(&ioend->io_vnode->v_iocount);
224 ioend->io_offset = 0;
225 ioend->io_size = 0;
226
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100227 if (type == IOMAP_UNWRITTEN)
228 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
229 else if (type == IOMAP_DELAY)
230 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
231 else
232 INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000233
234 return ioend;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237STATIC int
238xfs_map_blocks(
239 struct inode *inode,
240 loff_t offset,
241 ssize_t count,
242 xfs_iomap_t *mapp,
243 int flags)
244{
245 vnode_t *vp = LINVFS_GET_VP(inode);
246 int error, nmaps = 1;
247
248 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
249 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
250 VMODIFY(vp);
251 return -error;
252}
253
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100254STATIC inline int
255xfs_iomap_valid(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 xfs_iomap_t *iomapp,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100257 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100259 return offset >= iomapp->iomap_offset &&
260 offset < iomapp->iomap_offset + iomapp->iomap_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100263/*
264 * BIO completion handler for buffered IO.
265 */
266STATIC int
267xfs_end_bio(
268 struct bio *bio,
269 unsigned int bytes_done,
270 int error)
271{
272 xfs_ioend_t *ioend = bio->bi_private;
273
274 if (bio->bi_size)
275 return 1;
276
277 ASSERT(ioend);
278 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
279
280 /* Toss bio and pass work off to an xfsdatad thread */
281 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
282 ioend->io_uptodate = 0;
283 bio->bi_private = NULL;
284 bio->bi_end_io = NULL;
285
286 bio_put(bio);
287 xfs_finish_ioend(ioend);
288 return 0;
289}
290
291STATIC void
292xfs_submit_ioend_bio(
293 xfs_ioend_t *ioend,
294 struct bio *bio)
295{
296 atomic_inc(&ioend->io_remaining);
297
298 bio->bi_private = ioend;
299 bio->bi_end_io = xfs_end_bio;
300
301 submit_bio(WRITE, bio);
302 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
303 bio_put(bio);
304}
305
306STATIC struct bio *
307xfs_alloc_ioend_bio(
308 struct buffer_head *bh)
309{
310 struct bio *bio;
311 int nvecs = bio_get_nr_vecs(bh->b_bdev);
312
313 do {
314 bio = bio_alloc(GFP_NOIO, nvecs);
315 nvecs >>= 1;
316 } while (!bio);
317
318 ASSERT(bio->bi_private == NULL);
319 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
320 bio->bi_bdev = bh->b_bdev;
321 bio_get(bio);
322 return bio;
323}
324
325STATIC void
326xfs_start_buffer_writeback(
327 struct buffer_head *bh)
328{
329 ASSERT(buffer_mapped(bh));
330 ASSERT(buffer_locked(bh));
331 ASSERT(!buffer_delay(bh));
332 ASSERT(!buffer_unwritten(bh));
333
334 mark_buffer_async_write(bh);
335 set_buffer_uptodate(bh);
336 clear_buffer_dirty(bh);
337}
338
339STATIC void
340xfs_start_page_writeback(
341 struct page *page,
342 struct writeback_control *wbc,
343 int clear_dirty,
344 int buffers)
345{
346 ASSERT(PageLocked(page));
347 ASSERT(!PageWriteback(page));
348 set_page_writeback(page);
349 if (clear_dirty)
350 clear_page_dirty(page);
351 unlock_page(page);
352 if (!buffers) {
353 end_page_writeback(page);
354 wbc->pages_skipped++; /* We didn't write this page */
355 }
356}
357
358static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
359{
360 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
361}
362
363/*
David Chinnerd88992f2006-01-18 13:38:12 +1100364 * Submit all of the bios for all of the ioends we have saved up, covering the
365 * initial writepage page and also any probed pages.
366 *
367 * Because we may have multiple ioends spanning a page, we need to start
368 * writeback on all the buffers before we submit them for I/O. If we mark the
369 * buffers as we got, then we can end up with a page that only has buffers
370 * marked async write and I/O complete on can occur before we mark the other
371 * buffers async write.
372 *
373 * The end result of this is that we trip a bug in end_page_writeback() because
374 * we call it twice for the one page as the code in end_buffer_async_write()
375 * assumes that all buffers on the page are started at the same time.
376 *
377 * The fix is two passes across the ioend list - one to start writeback on the
378 * bufferheads, and then the second one submit them for I/O.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100379 */
380STATIC void
381xfs_submit_ioend(
382 xfs_ioend_t *ioend)
383{
David Chinnerd88992f2006-01-18 13:38:12 +1100384 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100385 xfs_ioend_t *next;
386 struct buffer_head *bh;
387 struct bio *bio;
388 sector_t lastblock = 0;
389
David Chinnerd88992f2006-01-18 13:38:12 +1100390 /* Pass 1 - start writeback */
391 do {
392 next = ioend->io_list;
393 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
394 xfs_start_buffer_writeback(bh);
395 }
396 } while ((ioend = next) != NULL);
397
398 /* Pass 2 - submit I/O */
399 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100400 do {
401 next = ioend->io_list;
402 bio = NULL;
403
404 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100405
406 if (!bio) {
407 retry:
408 bio = xfs_alloc_ioend_bio(bh);
409 } else if (bh->b_blocknr != lastblock + 1) {
410 xfs_submit_ioend_bio(ioend, bio);
411 goto retry;
412 }
413
414 if (bio_add_buffer(bio, bh) != bh->b_size) {
415 xfs_submit_ioend_bio(ioend, bio);
416 goto retry;
417 }
418
419 lastblock = bh->b_blocknr;
420 }
421 if (bio)
422 xfs_submit_ioend_bio(ioend, bio);
423 xfs_finish_ioend(ioend);
424 } while ((ioend = next) != NULL);
425}
426
427/*
428 * Cancel submission of all buffer_heads so far in this endio.
429 * Toss the endio too. Only ever called for the initial page
430 * in a writepage request, so only ever one page.
431 */
432STATIC void
433xfs_cancel_ioend(
434 xfs_ioend_t *ioend)
435{
436 xfs_ioend_t *next;
437 struct buffer_head *bh, *next_bh;
438
439 do {
440 next = ioend->io_list;
441 bh = ioend->io_buffer_head;
442 do {
443 next_bh = bh->b_private;
444 clear_buffer_async_write(bh);
445 unlock_buffer(bh);
446 } while ((bh = next_bh) != NULL);
447
448 vn_iowake(ioend->io_vnode);
449 mempool_free(ioend, xfs_ioend_pool);
450 } while ((ioend = next) != NULL);
451}
452
453/*
454 * Test to see if we've been building up a completion structure for
455 * earlier buffers -- if so, we try to append to this ioend if we
456 * can, otherwise we finish off any current ioend and start another.
457 * Return true if we've finished the given ioend.
458 */
459STATIC void
460xfs_add_to_ioend(
461 struct inode *inode,
462 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100463 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100464 unsigned int type,
465 xfs_ioend_t **result,
466 int need_ioend)
467{
468 xfs_ioend_t *ioend = *result;
469
470 if (!ioend || need_ioend || type != ioend->io_type) {
471 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100472
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100473 ioend = xfs_alloc_ioend(inode, type);
474 ioend->io_offset = offset;
475 ioend->io_buffer_head = bh;
476 ioend->io_buffer_tail = bh;
477 if (previous)
478 previous->io_list = ioend;
479 *result = ioend;
480 } else {
481 ioend->io_buffer_tail->b_private = bh;
482 ioend->io_buffer_tail = bh;
483 }
484
485 bh->b_private = NULL;
486 ioend->io_size += bh->b_size;
487}
488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489STATIC void
Nathan Scott87cbc492006-03-14 13:26:43 +1100490xfs_map_buffer(
491 struct buffer_head *bh,
492 xfs_iomap_t *mp,
493 xfs_off_t offset,
494 uint block_bits)
495{
496 sector_t bn;
497
498 ASSERT(mp->iomap_bn != IOMAP_DADDR_NULL);
499
500 bn = (mp->iomap_bn >> (block_bits - BBSHIFT)) +
501 ((offset - mp->iomap_offset) >> block_bits);
502
503 ASSERT(bn || (mp->iomap_flags & IOMAP_REALTIME));
504
505 bh->b_blocknr = bn;
506 set_buffer_mapped(bh);
507}
508
509STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510xfs_map_at_offset(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct buffer_head *bh,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100512 loff_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 int block_bits,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100514 xfs_iomap_t *iomapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
517 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519 lock_buffer(bh);
Nathan Scott87cbc492006-03-14 13:26:43 +1100520 xfs_map_buffer(bh, iomapp, offset, block_bits);
Nathan Scottce8e9222006-01-11 15:39:08 +1100521 bh->b_bdev = iomapp->iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 set_buffer_mapped(bh);
523 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100524 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100528 * Look for a page at index that is suitable for clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
530STATIC unsigned int
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100531xfs_probe_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100532 struct page *page,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100533 unsigned int pg_offset,
534 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 int ret = 0;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100539 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 if (page->mapping && PageDirty(page)) {
542 if (page_has_buffers(page)) {
543 struct buffer_head *bh, *head;
544
545 bh = head = page_buffers(page);
546 do {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100547 if (!buffer_uptodate(bh))
548 break;
549 if (mapped != buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 break;
551 ret += bh->b_size;
552 if (ret >= pg_offset)
553 break;
554 } while ((bh = bh->b_this_page) != head);
555 } else
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100556 ret = mapped ? 0 : PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 return ret;
560}
561
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100562STATIC size_t
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100563xfs_probe_cluster(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 struct inode *inode,
565 struct page *startpage,
566 struct buffer_head *bh,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100567 struct buffer_head *head,
568 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100570 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100572 size_t total = 0;
573 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 /* First sum forwards in this page */
576 do {
Eric Sandeen2353e8e2006-02-28 12:30:30 +1100577 if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100578 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 total += bh->b_size;
580 } while ((bh = bh->b_this_page) != head);
581
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100582 /* if we reached the end of the page, sum forwards in following pages */
583 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
584 tindex = startpage->index + 1;
585
586 /* Prune this back to avoid pathological behavior */
587 tloff = min(tlast, startpage->index + 64);
588
589 pagevec_init(&pvec, 0);
590 while (!done && tindex <= tloff) {
591 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
592
593 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
594 break;
595
596 for (i = 0; i < pagevec_count(&pvec); i++) {
597 struct page *page = pvec.pages[i];
598 size_t pg_offset, len = 0;
599
600 if (tindex == tlast) {
601 pg_offset =
602 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100603 if (!pg_offset) {
604 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100605 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100606 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100607 } else
608 pg_offset = PAGE_CACHE_SIZE;
609
610 if (page->index == tindex && !TestSetPageLocked(page)) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100611 len = xfs_probe_page(page, pg_offset, mapped);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100612 unlock_page(page);
613 }
614
615 if (!len) {
616 done = 1;
617 break;
618 }
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 total += len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100621 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100623
624 pagevec_release(&pvec);
625 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return total;
629}
630
631/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100632 * Test if a given page is suitable for writing as part of an unwritten
633 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100635STATIC int
636xfs_is_delayed_page(
637 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100638 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 if (page->mapping && page_has_buffers(page)) {
644 struct buffer_head *bh, *head;
645 int acceptable = 0;
646
647 bh = head = page_buffers(page);
648 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100649 if (buffer_unwritten(bh))
650 acceptable = (type == IOMAP_UNWRITTEN);
651 else if (buffer_delay(bh))
652 acceptable = (type == IOMAP_DELAY);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100653 else if (buffer_mapped(bh))
654 acceptable = (type == 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100655 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 } while ((bh = bh->b_this_page) != head);
658
659 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100660 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100663 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666/*
667 * Allocate & map buffers for page given the extent map. Write it out.
668 * except for the original page of a writepage, this is called on
669 * delalloc/unwritten pages only, for the original page it is possible
670 * that the page has no mapping at all.
671 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100672STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673xfs_convert_page(
674 struct inode *inode,
675 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100676 loff_t tindex,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100677 xfs_iomap_t *mp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100678 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 int startio,
681 int all_bh)
682{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100683 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100684 xfs_off_t end_offset;
685 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100686 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 int bbits = inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700688 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100689 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100690 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100692 if (page->index != tindex)
693 goto fail;
694 if (TestSetPageLocked(page))
695 goto fail;
696 if (PageWriteback(page))
697 goto fail_unlock_page;
698 if (page->mapping != inode->i_mapping)
699 goto fail_unlock_page;
700 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
701 goto fail_unlock_page;
702
Nathan Scott24e17b52005-05-05 13:33:20 -0700703 /*
704 * page_dirty is initially a count of buffers on the page before
705 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100706 *
707 * Derivation:
708 *
709 * End offset is the highest offset that this page should represent.
710 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
711 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
712 * hence give us the correct page_dirty count. On any other page,
713 * it will be zero and in that case we need page_dirty to be the
714 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700715 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100716 end_offset = min_t(unsigned long long,
717 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
718 i_size_read(inode));
719
Nathan Scott24e17b52005-05-05 13:33:20 -0700720 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100721 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
722 PAGE_CACHE_SIZE);
723 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
724 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 bh = head = page_buffers(page);
727 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100728 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100730 if (!buffer_uptodate(bh))
731 uptodate = 0;
732 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
733 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100735 }
736
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100737 if (buffer_unwritten(bh) || buffer_delay(bh)) {
738 if (buffer_unwritten(bh))
739 type = IOMAP_UNWRITTEN;
740 else
741 type = IOMAP_DELAY;
742
743 if (!xfs_iomap_valid(mp, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100744 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100745 continue;
746 }
747
748 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
749 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
750
751 xfs_map_at_offset(bh, offset, bbits, mp);
752 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100753 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100754 type, ioendp, done);
755 } else {
756 set_buffer_dirty(bh);
757 unlock_buffer(bh);
758 mark_buffer_dirty(bh);
759 }
760 page_dirty--;
761 count++;
762 } else {
763 type = 0;
764 if (buffer_mapped(bh) && all_bh && startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100766 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100767 type, ioendp, done);
768 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700769 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100770 } else {
771 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100774 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100776 if (uptodate && bh == head)
777 SetPageUptodate(page);
778
779 if (startio) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100780 if (count) {
781 struct backing_dev_info *bdi;
782
783 bdi = inode->i_mapping->backing_dev_info;
David Chinner9fddaca2006-02-07 20:27:24 +1100784 wbc->nr_to_write--;
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100785 if (bdi_write_congested(bdi)) {
786 wbc->encountered_congestion = 1;
787 done = 1;
David Chinner9fddaca2006-02-07 20:27:24 +1100788 } else if (wbc->nr_to_write <= 0) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100789 done = 1;
790 }
791 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100792 xfs_start_page_writeback(page, wbc, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100794
795 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100796 fail_unlock_page:
797 unlock_page(page);
798 fail:
799 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
802/*
803 * Convert & write out a cluster of pages in the same extent as defined
804 * by mp and following the start page.
805 */
806STATIC void
807xfs_cluster_write(
808 struct inode *inode,
809 pgoff_t tindex,
810 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100811 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 struct writeback_control *wbc,
813 int startio,
814 int all_bh,
815 pgoff_t tlast)
816{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100817 struct pagevec pvec;
818 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100820 pagevec_init(&pvec, 0);
821 while (!done && tindex <= tlast) {
822 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
823
824 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100826
827 for (i = 0; i < pagevec_count(&pvec); i++) {
828 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
829 iomapp, ioendp, wbc, startio, all_bh);
830 if (done)
831 break;
832 }
833
834 pagevec_release(&pvec);
835 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837}
838
839/*
840 * Calling this without startio set means we are being asked to make a dirty
841 * page ready for freeing it's buffers. When called with startio set then
842 * we are coming from writepage.
843 *
844 * When called with startio set it is important that we write the WHOLE
845 * page if possible.
846 * The bh->b_state's cannot know if any of the blocks or which block for
847 * that matter are dirty due to mmap writes, and therefore bh uptodate is
848 * only vaild if the page itself isn't completely uptodate. Some layers
849 * may clear the page dirty flag prior to calling write page, under the
850 * assumption the entire page will be written out; by not writing out the
851 * whole page the page can be reused before all valid dirty data is
852 * written out. Note: in the case of a page that has been dirty'd by
853 * mapwrite and but partially setup by block_prepare_write the
854 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
855 * valid state, thus the whole page must be written out thing.
856 */
857
858STATIC int
859xfs_page_state_convert(
860 struct inode *inode,
861 struct page *page,
862 struct writeback_control *wbc,
863 int startio,
864 int unmapped) /* also implies page uptodate */
865{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100866 struct buffer_head *bh, *head;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100867 xfs_iomap_t iomap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100868 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 loff_t offset;
870 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100871 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 __uint64_t end_offset;
873 pgoff_t end_index, last_index, tlast;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100874 ssize_t size, len;
875 int flags, err, iomap_valid = 0, uptodate = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100876 int page_dirty, count = 0, trylock_flag = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100877 int all_bh = unmapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Daniel Moore3ba08152005-05-05 13:31:34 -0700879 /* wait for other IO threads? */
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100880 if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100881 trylock_flag |= BMAPI_TRYLOCK;
Daniel Moore3ba08152005-05-05 13:31:34 -0700882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 /* Is this page beyond the end of the file? */
884 offset = i_size_read(inode);
885 end_index = offset >> PAGE_CACHE_SHIFT;
886 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
887 if (page->index >= end_index) {
888 if ((page->index >= end_index + 1) ||
889 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100890 if (startio)
891 unlock_page(page);
892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894 }
895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 /*
Nathan Scott24e17b52005-05-05 13:33:20 -0700897 * page_dirty is initially a count of buffers on the page before
898 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100899 *
900 * Derivation:
901 *
902 * End offset is the highest offset that this page should represent.
903 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
904 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
905 * hence give us the correct page_dirty count. On any other page,
906 * it will be zero and in that case we need page_dirty to be the
907 * count of buffers on the page.
908 */
909 end_offset = min_t(unsigned long long,
910 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700911 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100912 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
913 PAGE_CACHE_SIZE);
914 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -0700915 page_dirty = p_offset / len;
916
Nathan Scott24e17b52005-05-05 13:33:20 -0700917 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100918 offset = page_offset(page);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100919 flags = -1;
920 type = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100921
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100922 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 do {
925 if (offset >= end_offset)
926 break;
927 if (!buffer_uptodate(bh))
928 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100929 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100930 /*
931 * the iomap is actually still valid, but the ioend
932 * isn't. shouldn't happen too often.
933 */
934 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100938 if (iomap_valid)
939 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /*
942 * First case, map an unwritten extent and prepare for
943 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100944 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * Second case, allocate space for a delalloc buffer.
946 * We can return EAGAIN here in the release page case.
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100947 *
948 * Third case, an unmapped buffer was found, and we are
949 * in a path where we need to write the whole page out.
950 */
951 if (buffer_unwritten(bh) || buffer_delay(bh) ||
952 ((buffer_uptodate(bh) || PageUptodate(page)) &&
953 !buffer_mapped(bh) && (unmapped || startio))) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100954 /*
955 * Make sure we don't use a read-only iomap
956 */
957 if (flags == BMAPI_READ)
958 iomap_valid = 0;
959
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100960 if (buffer_unwritten(bh)) {
961 type = IOMAP_UNWRITTEN;
962 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100963 } else if (buffer_delay(bh)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100964 type = IOMAP_DELAY;
965 flags = BMAPI_ALLOCATE;
966 if (!startio)
967 flags |= trylock_flag;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100968 } else {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100969 type = IOMAP_NEW;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100970 flags = BMAPI_WRITE|BMAPI_MMAP;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100971 }
972
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100973 if (!iomap_valid) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100974 if (type == IOMAP_NEW) {
975 size = xfs_probe_cluster(inode,
976 page, bh, head, 0);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100977 } else {
978 size = len;
979 }
980
981 err = xfs_map_blocks(inode, offset, size,
982 &iomap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100983 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 goto error;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100985 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 }
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100987 if (iomap_valid) {
988 xfs_map_at_offset(bh, offset,
989 inode->i_blkbits, &iomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100991 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100992 type, &ioend,
993 !iomap_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 } else {
995 set_buffer_dirty(bh);
996 unlock_buffer(bh);
997 mark_buffer_dirty(bh);
998 }
999 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001000 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001002 } else if (buffer_uptodate(bh) && startio) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001003 /*
1004 * we got here because the buffer is already mapped.
1005 * That means it must already have extents allocated
1006 * underneath it. Map the extent by reading it.
1007 */
1008 if (!iomap_valid || type != 0) {
1009 flags = BMAPI_READ;
1010 size = xfs_probe_cluster(inode, page, bh,
1011 head, 1);
1012 err = xfs_map_blocks(inode, offset, size,
1013 &iomap, flags);
1014 if (err)
1015 goto error;
1016 iomap_valid = xfs_iomap_valid(&iomap, offset);
1017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001019 type = 0;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001020 if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
1021 ASSERT(buffer_mapped(bh));
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001022 if (iomap_valid)
1023 all_bh = 1;
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001024 xfs_add_to_ioend(inode, bh, offset, type,
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001025 &ioend, !iomap_valid);
1026 page_dirty--;
1027 count++;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001028 } else {
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001029 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001031 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
1032 (unmapped || startio)) {
1033 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001035
1036 if (!iohead)
1037 iohead = ioend;
1038
1039 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
1041 if (uptodate && bh == head)
1042 SetPageUptodate(page);
1043
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001044 if (startio)
1045 xfs_start_page_writeback(page, wbc, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001047 if (ioend && iomap_valid) {
1048 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -07001050 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001051 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001052 wbc, startio, all_bh, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001055 if (iohead)
1056 xfs_submit_ioend(iohead);
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return page_dirty;
1059
1060error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001061 if (iohead)
1062 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 /*
1065 * If it's delalloc and we have nowhere to put it,
1066 * throw it away, unless the lower layers told
1067 * us to try again.
1068 */
1069 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001070 if (!unmapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 block_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 ClearPageUptodate(page);
1073 }
1074 return err;
1075}
1076
Nathan Scottf51623b2006-03-14 13:26:27 +11001077/*
1078 * writepage: Called from one of two places:
1079 *
1080 * 1. we are flushing a delalloc buffer head.
1081 *
1082 * 2. we are writing out a dirty page. Typically the page dirty
1083 * state is cleared before we get here. In this case is it
1084 * conceivable we have no buffer heads.
1085 *
1086 * For delalloc space on the page we need to allocate space and
1087 * flush it. For unmapped buffer heads on the page we should
1088 * allocate space if the page is uptodate. For any other dirty
1089 * buffer heads on the page we should flush them.
1090 *
1091 * If we detect that a transaction would be required to flush
1092 * the page, we have to check the process flags first, if we
1093 * are already in a transaction or disk I/O during allocations
1094 * is off, we need to fail the writepage and redirty the page.
1095 */
1096
1097STATIC int
1098linvfs_writepage(
1099 struct page *page,
1100 struct writeback_control *wbc)
1101{
1102 int error;
1103 int need_trans;
1104 int delalloc, unmapped, unwritten;
1105 struct inode *inode = page->mapping->host;
1106
1107 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1108
1109 /*
1110 * We need a transaction if:
1111 * 1. There are delalloc buffers on the page
1112 * 2. The page is uptodate and we have unmapped buffers
1113 * 3. The page is uptodate and we have no buffers
1114 * 4. There are unwritten buffers on the page
1115 */
1116
1117 if (!page_has_buffers(page)) {
1118 unmapped = 1;
1119 need_trans = 1;
1120 } else {
1121 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1122 if (!PageUptodate(page))
1123 unmapped = 0;
1124 need_trans = delalloc + unmapped + unwritten;
1125 }
1126
1127 /*
1128 * If we need a transaction and the process flags say
1129 * we are already in a transaction, or no IO is allowed
1130 * then mark the page dirty again and leave the page
1131 * as is.
1132 */
1133 if (PFLAGS_TEST_FSTRANS() && need_trans)
1134 goto out_fail;
1135
1136 /*
1137 * Delay hooking up buffer heads until we have
1138 * made our go/no-go decision.
1139 */
1140 if (!page_has_buffers(page))
1141 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1142
1143 /*
1144 * Convert delayed allocate, unwritten or unmapped space
1145 * to real space and flush out to disk.
1146 */
1147 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1148 if (error == -EAGAIN)
1149 goto out_fail;
1150 if (unlikely(error < 0))
1151 goto out_unlock;
1152
1153 return 0;
1154
1155out_fail:
1156 redirty_page_for_writepage(wbc, page);
1157 unlock_page(page);
1158 return 0;
1159out_unlock:
1160 unlock_page(page);
1161 return error;
1162}
1163
1164/*
1165 * Called to move a page into cleanable state - and from there
1166 * to be released. Possibly the page is already clean. We always
1167 * have buffer heads in this call.
1168 *
1169 * Returns 0 if the page is ok to release, 1 otherwise.
1170 *
1171 * Possible scenarios are:
1172 *
1173 * 1. We are being called to release a page which has been written
1174 * to via regular I/O. buffer heads will be dirty and possibly
1175 * delalloc. If no delalloc buffer heads in this case then we
1176 * can just return zero.
1177 *
1178 * 2. We are called to release a page which has been written via
1179 * mmap, all we need to do is ensure there is no delalloc
1180 * state in the buffer heads, if not we can let the caller
1181 * free them and we should come back later via writepage.
1182 */
1183STATIC int
1184linvfs_release_page(
1185 struct page *page,
1186 gfp_t gfp_mask)
1187{
1188 struct inode *inode = page->mapping->host;
1189 int dirty, delalloc, unmapped, unwritten;
1190 struct writeback_control wbc = {
1191 .sync_mode = WB_SYNC_ALL,
1192 .nr_to_write = 1,
1193 };
1194
1195 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1196
1197 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1198 if (!delalloc && !unwritten)
1199 goto free_buffers;
1200
1201 if (!(gfp_mask & __GFP_FS))
1202 return 0;
1203
1204 /* If we are already inside a transaction or the thread cannot
1205 * do I/O, we cannot release this page.
1206 */
1207 if (PFLAGS_TEST_FSTRANS())
1208 return 0;
1209
1210 /*
1211 * Convert delalloc space to real space, do not flush the
1212 * data out to disk, that will be done by the caller.
1213 * Never need to allocate space here - we will always
1214 * come back to writepage in that case.
1215 */
1216 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1217 if (dirty == 0 && !unwritten)
1218 goto free_buffers;
1219 return 0;
1220
1221free_buffers:
1222 return try_to_free_buffers(page);
1223}
1224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225STATIC int
1226__linvfs_get_block(
1227 struct inode *inode,
1228 sector_t iblock,
1229 unsigned long blocks,
1230 struct buffer_head *bh_result,
1231 int create,
1232 int direct,
1233 bmapi_flags_t flags)
1234{
1235 vnode_t *vp = LINVFS_GET_VP(inode);
1236 xfs_iomap_t iomap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001237 xfs_off_t offset;
1238 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 int retpbbm = 1;
1240 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001242 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scotta4656392005-11-25 16:41:57 +11001243 if (blocks)
1244 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1245 (xfs_off_t)blocks << inode->i_blkbits);
1246 else
1247 size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
1249 VOP_BMAP(vp, offset, size,
1250 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1251 if (error)
1252 return -error;
1253
1254 if (retpbbm == 0)
1255 return 0;
1256
1257 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001258 /*
1259 * For unwritten extents do not report a disk address on
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 * the read case (treat as if we're reading into a hole).
1261 */
1262 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
Nathan Scott87cbc492006-03-14 13:26:43 +11001263 xfs_map_buffer(bh_result, &iomap, offset,
1264 inode->i_blkbits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1267 if (direct)
1268 bh_result->b_private = inode;
1269 set_buffer_unwritten(bh_result);
1270 set_buffer_delay(bh_result);
1271 }
1272 }
1273
1274 /* If this is a realtime file, data might be on a new device */
Nathan Scottce8e9222006-01-11 15:39:08 +11001275 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
1277 /* If we previously allocated a block out beyond eof and
1278 * we are now coming back to use it then we will need to
1279 * flag it as new even if it has a disk address.
1280 */
1281 if (create &&
1282 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001283 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (iomap.iomap_flags & IOMAP_DELAY) {
1287 BUG_ON(direct);
1288 if (create) {
1289 set_buffer_uptodate(bh_result);
1290 set_buffer_mapped(bh_result);
1291 set_buffer_delay(bh_result);
1292 }
1293 }
1294
1295 if (blocks) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001296 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1297 offset = min_t(xfs_off_t,
1298 iomap.iomap_bsize - iomap.iomap_delta,
Nathan Scotta4656392005-11-25 16:41:57 +11001299 (xfs_off_t)blocks << inode->i_blkbits);
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001300 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
1303 return 0;
1304}
1305
1306int
1307linvfs_get_block(
1308 struct inode *inode,
1309 sector_t iblock,
1310 struct buffer_head *bh_result,
1311 int create)
1312{
1313 return __linvfs_get_block(inode, iblock, 0, bh_result,
1314 create, 0, BMAPI_WRITE);
1315}
1316
1317STATIC int
1318linvfs_get_blocks_direct(
1319 struct inode *inode,
1320 sector_t iblock,
1321 unsigned long max_blocks,
1322 struct buffer_head *bh_result,
1323 int create)
1324{
1325 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1326 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1327}
1328
Christoph Hellwigf0973862005-09-05 08:22:52 +10001329STATIC void
1330linvfs_end_io_direct(
1331 struct kiocb *iocb,
1332 loff_t offset,
1333 ssize_t size,
1334 void *private)
1335{
1336 xfs_ioend_t *ioend = iocb->private;
1337
1338 /*
1339 * Non-NULL private data means we need to issue a transaction to
1340 * convert a range from unwritten to written extents. This needs
1341 * to happen from process contect but aio+dio I/O completion
1342 * happens from irq context so we need to defer it to a workqueue.
1343 * This is not nessecary for synchronous direct I/O, but we do
1344 * it anyway to keep the code uniform and simpler.
1345 *
1346 * The core direct I/O code might be changed to always call the
1347 * completion handler in the future, in which case all this can
1348 * go away.
1349 */
1350 if (private && size > 0) {
1351 ioend->io_offset = offset;
1352 ioend->io_size = size;
1353 xfs_finish_ioend(ioend);
1354 } else {
1355 ASSERT(size >= 0);
1356 xfs_destroy_ioend(ioend);
1357 }
1358
1359 /*
1360 * blockdev_direct_IO can return an error even afer the I/O
1361 * completion handler was called. Thus we need to protect
1362 * against double-freeing.
1363 */
1364 iocb->private = NULL;
1365}
1366
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367STATIC ssize_t
1368linvfs_direct_IO(
1369 int rw,
1370 struct kiocb *iocb,
1371 const struct iovec *iov,
1372 loff_t offset,
1373 unsigned long nr_segs)
1374{
1375 struct file *file = iocb->ki_filp;
1376 struct inode *inode = file->f_mapping->host;
1377 vnode_t *vp = LINVFS_GET_VP(inode);
1378 xfs_iomap_t iomap;
1379 int maps = 1;
1380 int error;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001381 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1384 if (error)
1385 return -error;
1386
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001387 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001388
1389 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
Nathan Scottce8e9222006-01-11 15:39:08 +11001390 iomap.iomap_target->bt_bdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 iov, offset, nr_segs,
1392 linvfs_get_blocks_direct,
Christoph Hellwigf0973862005-09-05 08:22:52 +10001393 linvfs_end_io_direct);
1394
1395 if (unlikely(ret <= 0 && iocb->private))
1396 xfs_destroy_ioend(iocb->private);
1397 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398}
1399
Nathan Scottf51623b2006-03-14 13:26:27 +11001400STATIC int
1401linvfs_prepare_write(
1402 struct file *file,
1403 struct page *page,
1404 unsigned int from,
1405 unsigned int to)
1406{
1407 return block_prepare_write(page, from, to, linvfs_get_block);
1408}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410STATIC sector_t
1411linvfs_bmap(
1412 struct address_space *mapping,
1413 sector_t block)
1414{
1415 struct inode *inode = (struct inode *)mapping->host;
1416 vnode_t *vp = LINVFS_GET_VP(inode);
1417 int error;
1418
1419 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1420
1421 VOP_RWLOCK(vp, VRWLOCK_READ);
1422 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1423 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1424 return generic_block_bmap(mapping, block, linvfs_get_block);
1425}
1426
1427STATIC int
1428linvfs_readpage(
1429 struct file *unused,
1430 struct page *page)
1431{
1432 return mpage_readpage(page, linvfs_get_block);
1433}
1434
1435STATIC int
1436linvfs_readpages(
1437 struct file *unused,
1438 struct address_space *mapping,
1439 struct list_head *pages,
1440 unsigned nr_pages)
1441{
1442 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1443}
1444
Nathan Scottbcec2b72005-09-02 16:40:17 +10001445STATIC int
1446linvfs_invalidate_page(
1447 struct page *page,
1448 unsigned long offset)
1449{
1450 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1451 page->mapping->host, page, offset);
1452 return block_invalidatepage(page, offset);
1453}
1454
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455struct address_space_operations linvfs_aops = {
1456 .readpage = linvfs_readpage,
1457 .readpages = linvfs_readpages,
1458 .writepage = linvfs_writepage,
1459 .sync_page = block_sync_page,
1460 .releasepage = linvfs_release_page,
Nathan Scottbcec2b72005-09-02 16:40:17 +10001461 .invalidatepage = linvfs_invalidate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 .prepare_write = linvfs_prepare_write,
1463 .commit_write = generic_commit_write,
1464 .bmap = linvfs_bmap,
1465 .direct_IO = linvfs_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001466 .migratepage = buffer_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467};