blob: 58fc7ade90b53047bb467d95791ec0e7445bd671 [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
490xfs_map_at_offset(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 struct buffer_head *bh,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100492 loff_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 int block_bits,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100494 xfs_iomap_t *iomapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 xfs_daddr_t bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 int sector_shift;
498
499 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
500 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
501 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 sector_shift = block_bits - BBSHIFT;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100504 bn = (iomapp->iomap_bn >> sector_shift) +
505 ((offset - iomapp->iomap_offset) >> block_bits);
506
507 ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
509
510 lock_buffer(bh);
511 bh->b_blocknr = bn;
Nathan Scottce8e9222006-01-11 15:39:08 +1100512 bh->b_bdev = iomapp->iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 set_buffer_mapped(bh);
514 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100515 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
518/*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100519 * Look for a page at index that is suitable for clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
521STATIC unsigned int
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100522xfs_probe_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100523 struct page *page,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100524 unsigned int pg_offset,
525 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 int ret = 0;
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100530 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
532 if (page->mapping && PageDirty(page)) {
533 if (page_has_buffers(page)) {
534 struct buffer_head *bh, *head;
535
536 bh = head = page_buffers(page);
537 do {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100538 if (!buffer_uptodate(bh))
539 break;
540 if (mapped != buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542 ret += bh->b_size;
543 if (ret >= pg_offset)
544 break;
545 } while ((bh = bh->b_this_page) != head);
546 } else
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100547 ret = mapped ? 0 : PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return ret;
551}
552
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100553STATIC size_t
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100554xfs_probe_cluster(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 struct inode *inode,
556 struct page *startpage,
557 struct buffer_head *bh,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100558 struct buffer_head *head,
559 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100561 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100563 size_t total = 0;
564 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* First sum forwards in this page */
567 do {
Eric Sandeen2353e8e2006-02-28 12:30:30 +1100568 if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100569 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 total += bh->b_size;
571 } while ((bh = bh->b_this_page) != head);
572
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100573 /* if we reached the end of the page, sum forwards in following pages */
574 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
575 tindex = startpage->index + 1;
576
577 /* Prune this back to avoid pathological behavior */
578 tloff = min(tlast, startpage->index + 64);
579
580 pagevec_init(&pvec, 0);
581 while (!done && tindex <= tloff) {
582 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
583
584 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
585 break;
586
587 for (i = 0; i < pagevec_count(&pvec); i++) {
588 struct page *page = pvec.pages[i];
589 size_t pg_offset, len = 0;
590
591 if (tindex == tlast) {
592 pg_offset =
593 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100594 if (!pg_offset) {
595 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100596 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100597 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100598 } else
599 pg_offset = PAGE_CACHE_SIZE;
600
601 if (page->index == tindex && !TestSetPageLocked(page)) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100602 len = xfs_probe_page(page, pg_offset, mapped);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100603 unlock_page(page);
604 }
605
606 if (!len) {
607 done = 1;
608 break;
609 }
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 total += len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100612 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100614
615 pagevec_release(&pvec);
616 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 return total;
620}
621
622/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100623 * Test if a given page is suitable for writing as part of an unwritten
624 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100626STATIC int
627xfs_is_delayed_page(
628 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100629 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
634 if (page->mapping && page_has_buffers(page)) {
635 struct buffer_head *bh, *head;
636 int acceptable = 0;
637
638 bh = head = page_buffers(page);
639 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100640 if (buffer_unwritten(bh))
641 acceptable = (type == IOMAP_UNWRITTEN);
642 else if (buffer_delay(bh))
643 acceptable = (type == IOMAP_DELAY);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100644 else if (buffer_mapped(bh))
645 acceptable = (type == 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100646 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 } while ((bh = bh->b_this_page) != head);
649
650 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100651 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100654 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657/*
658 * Allocate & map buffers for page given the extent map. Write it out.
659 * except for the original page of a writepage, this is called on
660 * delalloc/unwritten pages only, for the original page it is possible
661 * that the page has no mapping at all.
662 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100663STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664xfs_convert_page(
665 struct inode *inode,
666 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100667 loff_t tindex,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100668 xfs_iomap_t *mp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100669 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int startio,
672 int all_bh)
673{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100674 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100675 xfs_off_t end_offset;
676 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100677 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 int bbits = inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700679 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100680 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100681 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100683 if (page->index != tindex)
684 goto fail;
685 if (TestSetPageLocked(page))
686 goto fail;
687 if (PageWriteback(page))
688 goto fail_unlock_page;
689 if (page->mapping != inode->i_mapping)
690 goto fail_unlock_page;
691 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
692 goto fail_unlock_page;
693
Nathan Scott24e17b52005-05-05 13:33:20 -0700694 /*
695 * page_dirty is initially a count of buffers on the page before
696 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100697 *
698 * Derivation:
699 *
700 * End offset is the highest offset that this page should represent.
701 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
702 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
703 * hence give us the correct page_dirty count. On any other page,
704 * it will be zero and in that case we need page_dirty to be the
705 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700706 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100707 end_offset = min_t(unsigned long long,
708 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
709 i_size_read(inode));
710
Nathan Scott24e17b52005-05-05 13:33:20 -0700711 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100712 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
713 PAGE_CACHE_SIZE);
714 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
715 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 bh = head = page_buffers(page);
718 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100719 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100721 if (!buffer_uptodate(bh))
722 uptodate = 0;
723 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
724 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100726 }
727
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100728 if (buffer_unwritten(bh) || buffer_delay(bh)) {
729 if (buffer_unwritten(bh))
730 type = IOMAP_UNWRITTEN;
731 else
732 type = IOMAP_DELAY;
733
734 if (!xfs_iomap_valid(mp, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100735 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100736 continue;
737 }
738
739 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
740 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
741
742 xfs_map_at_offset(bh, offset, bbits, mp);
743 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100744 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100745 type, ioendp, done);
746 } else {
747 set_buffer_dirty(bh);
748 unlock_buffer(bh);
749 mark_buffer_dirty(bh);
750 }
751 page_dirty--;
752 count++;
753 } else {
754 type = 0;
755 if (buffer_mapped(bh) && all_bh && startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100757 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100758 type, ioendp, done);
759 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700760 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100761 } else {
762 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100765 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100767 if (uptodate && bh == head)
768 SetPageUptodate(page);
769
770 if (startio) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100771 if (count) {
772 struct backing_dev_info *bdi;
773
774 bdi = inode->i_mapping->backing_dev_info;
David Chinner9fddaca2006-02-07 20:27:24 +1100775 wbc->nr_to_write--;
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100776 if (bdi_write_congested(bdi)) {
777 wbc->encountered_congestion = 1;
778 done = 1;
David Chinner9fddaca2006-02-07 20:27:24 +1100779 } else if (wbc->nr_to_write <= 0) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100780 done = 1;
781 }
782 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100783 xfs_start_page_writeback(page, wbc, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100785
786 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100787 fail_unlock_page:
788 unlock_page(page);
789 fail:
790 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
793/*
794 * Convert & write out a cluster of pages in the same extent as defined
795 * by mp and following the start page.
796 */
797STATIC void
798xfs_cluster_write(
799 struct inode *inode,
800 pgoff_t tindex,
801 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100802 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct writeback_control *wbc,
804 int startio,
805 int all_bh,
806 pgoff_t tlast)
807{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100808 struct pagevec pvec;
809 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100811 pagevec_init(&pvec, 0);
812 while (!done && tindex <= tlast) {
813 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
814
815 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100817
818 for (i = 0; i < pagevec_count(&pvec); i++) {
819 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
820 iomapp, ioendp, wbc, startio, all_bh);
821 if (done)
822 break;
823 }
824
825 pagevec_release(&pvec);
826 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828}
829
830/*
831 * Calling this without startio set means we are being asked to make a dirty
832 * page ready for freeing it's buffers. When called with startio set then
833 * we are coming from writepage.
834 *
835 * When called with startio set it is important that we write the WHOLE
836 * page if possible.
837 * The bh->b_state's cannot know if any of the blocks or which block for
838 * that matter are dirty due to mmap writes, and therefore bh uptodate is
839 * only vaild if the page itself isn't completely uptodate. Some layers
840 * may clear the page dirty flag prior to calling write page, under the
841 * assumption the entire page will be written out; by not writing out the
842 * whole page the page can be reused before all valid dirty data is
843 * written out. Note: in the case of a page that has been dirty'd by
844 * mapwrite and but partially setup by block_prepare_write the
845 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
846 * valid state, thus the whole page must be written out thing.
847 */
848
849STATIC int
850xfs_page_state_convert(
851 struct inode *inode,
852 struct page *page,
853 struct writeback_control *wbc,
854 int startio,
855 int unmapped) /* also implies page uptodate */
856{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100857 struct buffer_head *bh, *head;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100858 xfs_iomap_t iomap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100859 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 loff_t offset;
861 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100862 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 __uint64_t end_offset;
864 pgoff_t end_index, last_index, tlast;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100865 ssize_t size, len;
866 int flags, err, iomap_valid = 0, uptodate = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100867 int page_dirty, count = 0, trylock_flag = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100868 int all_bh = unmapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Daniel Moore3ba08152005-05-05 13:31:34 -0700870 /* wait for other IO threads? */
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100871 if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100872 trylock_flag |= BMAPI_TRYLOCK;
Daniel Moore3ba08152005-05-05 13:31:34 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /* Is this page beyond the end of the file? */
875 offset = i_size_read(inode);
876 end_index = offset >> PAGE_CACHE_SHIFT;
877 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
878 if (page->index >= end_index) {
879 if ((page->index >= end_index + 1) ||
880 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100881 if (startio)
882 unlock_page(page);
883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
885 }
886
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 /*
Nathan Scott24e17b52005-05-05 13:33:20 -0700888 * page_dirty is initially a count of buffers on the page before
889 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100890 *
891 * Derivation:
892 *
893 * End offset is the highest offset that this page should represent.
894 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
895 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
896 * hence give us the correct page_dirty count. On any other page,
897 * it will be zero and in that case we need page_dirty to be the
898 * count of buffers on the page.
899 */
900 end_offset = min_t(unsigned long long,
901 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700902 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100903 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
904 PAGE_CACHE_SIZE);
905 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -0700906 page_dirty = p_offset / len;
907
Nathan Scott24e17b52005-05-05 13:33:20 -0700908 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100909 offset = page_offset(page);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100910 flags = -1;
911 type = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100912
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100913 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 do {
916 if (offset >= end_offset)
917 break;
918 if (!buffer_uptodate(bh))
919 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100920 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100921 /*
922 * the iomap is actually still valid, but the ioend
923 * isn't. shouldn't happen too often.
924 */
925 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100929 if (iomap_valid)
930 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931
932 /*
933 * First case, map an unwritten extent and prepare for
934 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100935 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 * Second case, allocate space for a delalloc buffer.
937 * We can return EAGAIN here in the release page case.
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100938 *
939 * Third case, an unmapped buffer was found, and we are
940 * in a path where we need to write the whole page out.
941 */
942 if (buffer_unwritten(bh) || buffer_delay(bh) ||
943 ((buffer_uptodate(bh) || PageUptodate(page)) &&
944 !buffer_mapped(bh) && (unmapped || startio))) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100945 /*
946 * Make sure we don't use a read-only iomap
947 */
948 if (flags == BMAPI_READ)
949 iomap_valid = 0;
950
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100951 if (buffer_unwritten(bh)) {
952 type = IOMAP_UNWRITTEN;
953 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100954 } else if (buffer_delay(bh)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100955 type = IOMAP_DELAY;
956 flags = BMAPI_ALLOCATE;
957 if (!startio)
958 flags |= trylock_flag;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100959 } else {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100960 type = IOMAP_NEW;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100961 flags = BMAPI_WRITE|BMAPI_MMAP;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100962 }
963
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100964 if (!iomap_valid) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100965 if (type == IOMAP_NEW) {
966 size = xfs_probe_cluster(inode,
967 page, bh, head, 0);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100968 } else {
969 size = len;
970 }
971
972 err = xfs_map_blocks(inode, offset, size,
973 &iomap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100974 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 goto error;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100976 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 }
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100978 if (iomap_valid) {
979 xfs_map_at_offset(bh, offset,
980 inode->i_blkbits, &iomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100982 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100983 type, &ioend,
984 !iomap_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 } else {
986 set_buffer_dirty(bh);
987 unlock_buffer(bh);
988 mark_buffer_dirty(bh);
989 }
990 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100991 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100993 } else if (buffer_uptodate(bh) && startio) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100994 /*
995 * we got here because the buffer is already mapped.
996 * That means it must already have extents allocated
997 * underneath it. Map the extent by reading it.
998 */
999 if (!iomap_valid || type != 0) {
1000 flags = BMAPI_READ;
1001 size = xfs_probe_cluster(inode, page, bh,
1002 head, 1);
1003 err = xfs_map_blocks(inode, offset, size,
1004 &iomap, flags);
1005 if (err)
1006 goto error;
1007 iomap_valid = xfs_iomap_valid(&iomap, offset);
1008 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001010 type = 0;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001011 if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
1012 ASSERT(buffer_mapped(bh));
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001013 if (iomap_valid)
1014 all_bh = 1;
Christoph Hellwig7336cea2006-01-11 20:49:16 +11001015 xfs_add_to_ioend(inode, bh, offset, type,
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001016 &ioend, !iomap_valid);
1017 page_dirty--;
1018 count++;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001019 } else {
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001020 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +11001022 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
1023 (unmapped || startio)) {
1024 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001026
1027 if (!iohead)
1028 iohead = ioend;
1029
1030 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (uptodate && bh == head)
1033 SetPageUptodate(page);
1034
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001035 if (startio)
1036 xfs_start_page_writeback(page, wbc, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001038 if (ioend && iomap_valid) {
1039 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -07001041 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001042 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001043 wbc, startio, all_bh, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001046 if (iohead)
1047 xfs_submit_ioend(iohead);
1048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 return page_dirty;
1050
1051error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001052 if (iohead)
1053 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
1055 /*
1056 * If it's delalloc and we have nowhere to put it,
1057 * throw it away, unless the lower layers told
1058 * us to try again.
1059 */
1060 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001061 if (!unmapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 block_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 ClearPageUptodate(page);
1064 }
1065 return err;
1066}
1067
Nathan Scottf51623b2006-03-14 13:26:27 +11001068/*
1069 * writepage: Called from one of two places:
1070 *
1071 * 1. we are flushing a delalloc buffer head.
1072 *
1073 * 2. we are writing out a dirty page. Typically the page dirty
1074 * state is cleared before we get here. In this case is it
1075 * conceivable we have no buffer heads.
1076 *
1077 * For delalloc space on the page we need to allocate space and
1078 * flush it. For unmapped buffer heads on the page we should
1079 * allocate space if the page is uptodate. For any other dirty
1080 * buffer heads on the page we should flush them.
1081 *
1082 * If we detect that a transaction would be required to flush
1083 * the page, we have to check the process flags first, if we
1084 * are already in a transaction or disk I/O during allocations
1085 * is off, we need to fail the writepage and redirty the page.
1086 */
1087
1088STATIC int
1089linvfs_writepage(
1090 struct page *page,
1091 struct writeback_control *wbc)
1092{
1093 int error;
1094 int need_trans;
1095 int delalloc, unmapped, unwritten;
1096 struct inode *inode = page->mapping->host;
1097
1098 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1099
1100 /*
1101 * We need a transaction if:
1102 * 1. There are delalloc buffers on the page
1103 * 2. The page is uptodate and we have unmapped buffers
1104 * 3. The page is uptodate and we have no buffers
1105 * 4. There are unwritten buffers on the page
1106 */
1107
1108 if (!page_has_buffers(page)) {
1109 unmapped = 1;
1110 need_trans = 1;
1111 } else {
1112 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1113 if (!PageUptodate(page))
1114 unmapped = 0;
1115 need_trans = delalloc + unmapped + unwritten;
1116 }
1117
1118 /*
1119 * If we need a transaction and the process flags say
1120 * we are already in a transaction, or no IO is allowed
1121 * then mark the page dirty again and leave the page
1122 * as is.
1123 */
1124 if (PFLAGS_TEST_FSTRANS() && need_trans)
1125 goto out_fail;
1126
1127 /*
1128 * Delay hooking up buffer heads until we have
1129 * made our go/no-go decision.
1130 */
1131 if (!page_has_buffers(page))
1132 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1133
1134 /*
1135 * Convert delayed allocate, unwritten or unmapped space
1136 * to real space and flush out to disk.
1137 */
1138 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1139 if (error == -EAGAIN)
1140 goto out_fail;
1141 if (unlikely(error < 0))
1142 goto out_unlock;
1143
1144 return 0;
1145
1146out_fail:
1147 redirty_page_for_writepage(wbc, page);
1148 unlock_page(page);
1149 return 0;
1150out_unlock:
1151 unlock_page(page);
1152 return error;
1153}
1154
1155/*
1156 * Called to move a page into cleanable state - and from there
1157 * to be released. Possibly the page is already clean. We always
1158 * have buffer heads in this call.
1159 *
1160 * Returns 0 if the page is ok to release, 1 otherwise.
1161 *
1162 * Possible scenarios are:
1163 *
1164 * 1. We are being called to release a page which has been written
1165 * to via regular I/O. buffer heads will be dirty and possibly
1166 * delalloc. If no delalloc buffer heads in this case then we
1167 * can just return zero.
1168 *
1169 * 2. We are called to release a page which has been written via
1170 * mmap, all we need to do is ensure there is no delalloc
1171 * state in the buffer heads, if not we can let the caller
1172 * free them and we should come back later via writepage.
1173 */
1174STATIC int
1175linvfs_release_page(
1176 struct page *page,
1177 gfp_t gfp_mask)
1178{
1179 struct inode *inode = page->mapping->host;
1180 int dirty, delalloc, unmapped, unwritten;
1181 struct writeback_control wbc = {
1182 .sync_mode = WB_SYNC_ALL,
1183 .nr_to_write = 1,
1184 };
1185
1186 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1187
1188 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1189 if (!delalloc && !unwritten)
1190 goto free_buffers;
1191
1192 if (!(gfp_mask & __GFP_FS))
1193 return 0;
1194
1195 /* If we are already inside a transaction or the thread cannot
1196 * do I/O, we cannot release this page.
1197 */
1198 if (PFLAGS_TEST_FSTRANS())
1199 return 0;
1200
1201 /*
1202 * Convert delalloc space to real space, do not flush the
1203 * data out to disk, that will be done by the caller.
1204 * Never need to allocate space here - we will always
1205 * come back to writepage in that case.
1206 */
1207 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1208 if (dirty == 0 && !unwritten)
1209 goto free_buffers;
1210 return 0;
1211
1212free_buffers:
1213 return try_to_free_buffers(page);
1214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216STATIC int
1217__linvfs_get_block(
1218 struct inode *inode,
1219 sector_t iblock,
1220 unsigned long blocks,
1221 struct buffer_head *bh_result,
1222 int create,
1223 int direct,
1224 bmapi_flags_t flags)
1225{
1226 vnode_t *vp = LINVFS_GET_VP(inode);
1227 xfs_iomap_t iomap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001228 xfs_off_t offset;
1229 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 int retpbbm = 1;
1231 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001233 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scotta4656392005-11-25 16:41:57 +11001234 if (blocks)
1235 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1236 (xfs_off_t)blocks << inode->i_blkbits);
1237 else
1238 size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 VOP_BMAP(vp, offset, size,
1241 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1242 if (error)
1243 return -error;
1244
1245 if (retpbbm == 0)
1246 return 0;
1247
1248 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001249 xfs_daddr_t bn;
1250 xfs_off_t delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 /* For unwritten extents do not report a disk address on
1253 * the read case (treat as if we're reading into a hole).
1254 */
1255 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1256 delta = offset - iomap.iomap_offset;
1257 delta >>= inode->i_blkbits;
1258
1259 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1260 bn += delta;
1261 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1262 bh_result->b_blocknr = bn;
1263 set_buffer_mapped(bh_result);
1264 }
1265 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1266 if (direct)
1267 bh_result->b_private = inode;
1268 set_buffer_unwritten(bh_result);
1269 set_buffer_delay(bh_result);
1270 }
1271 }
1272
1273 /* If this is a realtime file, data might be on a new device */
Nathan Scottce8e9222006-01-11 15:39:08 +11001274 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 /* If we previously allocated a block out beyond eof and
1277 * we are now coming back to use it then we will need to
1278 * flag it as new even if it has a disk address.
1279 */
1280 if (create &&
1281 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001282 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 if (iomap.iomap_flags & IOMAP_DELAY) {
1286 BUG_ON(direct);
1287 if (create) {
1288 set_buffer_uptodate(bh_result);
1289 set_buffer_mapped(bh_result);
1290 set_buffer_delay(bh_result);
1291 }
1292 }
1293
1294 if (blocks) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001295 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1296 offset = min_t(xfs_off_t,
1297 iomap.iomap_bsize - iomap.iomap_delta,
Nathan Scotta4656392005-11-25 16:41:57 +11001298 (xfs_off_t)blocks << inode->i_blkbits);
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001299 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
1302 return 0;
1303}
1304
1305int
1306linvfs_get_block(
1307 struct inode *inode,
1308 sector_t iblock,
1309 struct buffer_head *bh_result,
1310 int create)
1311{
1312 return __linvfs_get_block(inode, iblock, 0, bh_result,
1313 create, 0, BMAPI_WRITE);
1314}
1315
1316STATIC int
1317linvfs_get_blocks_direct(
1318 struct inode *inode,
1319 sector_t iblock,
1320 unsigned long max_blocks,
1321 struct buffer_head *bh_result,
1322 int create)
1323{
1324 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1325 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1326}
1327
Christoph Hellwigf0973862005-09-05 08:22:52 +10001328STATIC void
1329linvfs_end_io_direct(
1330 struct kiocb *iocb,
1331 loff_t offset,
1332 ssize_t size,
1333 void *private)
1334{
1335 xfs_ioend_t *ioend = iocb->private;
1336
1337 /*
1338 * Non-NULL private data means we need to issue a transaction to
1339 * convert a range from unwritten to written extents. This needs
1340 * to happen from process contect but aio+dio I/O completion
1341 * happens from irq context so we need to defer it to a workqueue.
1342 * This is not nessecary for synchronous direct I/O, but we do
1343 * it anyway to keep the code uniform and simpler.
1344 *
1345 * The core direct I/O code might be changed to always call the
1346 * completion handler in the future, in which case all this can
1347 * go away.
1348 */
1349 if (private && size > 0) {
1350 ioend->io_offset = offset;
1351 ioend->io_size = size;
1352 xfs_finish_ioend(ioend);
1353 } else {
1354 ASSERT(size >= 0);
1355 xfs_destroy_ioend(ioend);
1356 }
1357
1358 /*
1359 * blockdev_direct_IO can return an error even afer the I/O
1360 * completion handler was called. Thus we need to protect
1361 * against double-freeing.
1362 */
1363 iocb->private = NULL;
1364}
1365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366STATIC ssize_t
1367linvfs_direct_IO(
1368 int rw,
1369 struct kiocb *iocb,
1370 const struct iovec *iov,
1371 loff_t offset,
1372 unsigned long nr_segs)
1373{
1374 struct file *file = iocb->ki_filp;
1375 struct inode *inode = file->f_mapping->host;
1376 vnode_t *vp = LINVFS_GET_VP(inode);
1377 xfs_iomap_t iomap;
1378 int maps = 1;
1379 int error;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001380 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1383 if (error)
1384 return -error;
1385
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001386 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001387
1388 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
Nathan Scottce8e9222006-01-11 15:39:08 +11001389 iomap.iomap_target->bt_bdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 iov, offset, nr_segs,
1391 linvfs_get_blocks_direct,
Christoph Hellwigf0973862005-09-05 08:22:52 +10001392 linvfs_end_io_direct);
1393
1394 if (unlikely(ret <= 0 && iocb->private))
1395 xfs_destroy_ioend(iocb->private);
1396 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397}
1398
Nathan Scottf51623b2006-03-14 13:26:27 +11001399STATIC int
1400linvfs_prepare_write(
1401 struct file *file,
1402 struct page *page,
1403 unsigned int from,
1404 unsigned int to)
1405{
1406 return block_prepare_write(page, from, to, linvfs_get_block);
1407}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409STATIC sector_t
1410linvfs_bmap(
1411 struct address_space *mapping,
1412 sector_t block)
1413{
1414 struct inode *inode = (struct inode *)mapping->host;
1415 vnode_t *vp = LINVFS_GET_VP(inode);
1416 int error;
1417
1418 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1419
1420 VOP_RWLOCK(vp, VRWLOCK_READ);
1421 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1422 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1423 return generic_block_bmap(mapping, block, linvfs_get_block);
1424}
1425
1426STATIC int
1427linvfs_readpage(
1428 struct file *unused,
1429 struct page *page)
1430{
1431 return mpage_readpage(page, linvfs_get_block);
1432}
1433
1434STATIC int
1435linvfs_readpages(
1436 struct file *unused,
1437 struct address_space *mapping,
1438 struct list_head *pages,
1439 unsigned nr_pages)
1440{
1441 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1442}
1443
Nathan Scottbcec2b72005-09-02 16:40:17 +10001444STATIC int
1445linvfs_invalidate_page(
1446 struct page *page,
1447 unsigned long offset)
1448{
1449 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1450 page->mapping->host, page, offset);
1451 return block_invalidatepage(page, offset);
1452}
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454struct address_space_operations linvfs_aops = {
1455 .readpage = linvfs_readpage,
1456 .readpages = linvfs_readpages,
1457 .writepage = linvfs_writepage,
1458 .sync_page = block_sync_page,
1459 .releasepage = linvfs_release_page,
Nathan Scottbcec2b72005-09-02 16:40:17 +10001460 .invalidatepage = linvfs_invalidate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 .prepare_write = linvfs_prepare_write,
1462 .commit_write = generic_commit_write,
1463 .bmap = linvfs_bmap,
1464 .direct_IO = linvfs_direct_IO,
Christoph Lametere965f962006-02-01 03:05:41 -08001465 .migratepage = buffer_migrate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466};