blob: 74d8be87f983d4995c32bc87ebcc89ff33b577bb [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
46STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#if defined(XFS_RW_TRACE)
49void
50xfs_page_trace(
51 int tag,
52 struct inode *inode,
53 struct page *page,
54 int mask)
55{
56 xfs_inode_t *ip;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 vnode_t *vp = LINVFS_GET_VP(inode);
58 loff_t isize = i_size_read(inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110059 loff_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 int delalloc = -1, unmapped = -1, unwritten = -1;
61
62 if (page_has_buffers(page))
63 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
64
Christoph Hellwig75e17b32006-01-11 20:58:44 +110065 ip = xfs_vtoi(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 if (!ip->i_rwtrace)
67 return;
68
69 ktrace_enter(ip->i_rwtrace,
70 (void *)((unsigned long)tag),
71 (void *)ip,
72 (void *)inode,
73 (void *)page,
74 (void *)((unsigned long)mask),
75 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
76 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
77 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(isize & 0xffffffff)),
79 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
80 (void *)((unsigned long)(offset & 0xffffffff)),
81 (void *)((unsigned long)delalloc),
82 (void *)((unsigned long)unmapped),
83 (void *)((unsigned long)unwritten),
84 (void *)NULL,
85 (void *)NULL);
86}
87#else
88#define xfs_page_trace(tag, inode, page, mask)
89#endif
90
Christoph Hellwig0829c362005-09-02 16:58:49 +100091/*
92 * Schedule IO completion handling on a xfsdatad if this was
93 * the final hold on this ioend.
94 */
95STATIC void
96xfs_finish_ioend(
97 xfs_ioend_t *ioend)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Christoph Hellwig0829c362005-09-02 16:58:49 +100099 if (atomic_dec_and_test(&ioend->io_remaining))
100 queue_work(xfsdatad_workqueue, &ioend->io_work);
101}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100103/*
104 * We're now finished for good with this ioend structure.
105 * Update the page state via the associated buffer_heads,
106 * release holds on the inode and bio, and finally free
107 * up memory. Do not use the ioend after this.
108 */
Christoph Hellwig0829c362005-09-02 16:58:49 +1000109STATIC void
110xfs_destroy_ioend(
111 xfs_ioend_t *ioend)
112{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100113 struct buffer_head *bh, *next;
114
115 for (bh = ioend->io_buffer_head; bh; bh = next) {
116 next = bh->b_private;
117 bh->b_end_io(bh, ioend->io_uptodate);
118 }
119
Christoph Hellwig0829c362005-09-02 16:58:49 +1000120 vn_iowake(ioend->io_vnode);
121 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
124/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100125 * Buffered IO write completion for delayed allocate extents.
126 * TODO: Update ondisk isize now that we know the file data
127 * has been flushed (i.e. the notorious "NULL file" problem).
128 */
129STATIC void
130xfs_end_bio_delalloc(
131 void *data)
132{
133 xfs_ioend_t *ioend = data;
134
135 xfs_destroy_ioend(ioend);
136}
137
138/*
139 * Buffered IO write completion for regular, written extents.
140 */
141STATIC void
142xfs_end_bio_written(
143 void *data)
144{
145 xfs_ioend_t *ioend = data;
146
147 xfs_destroy_ioend(ioend);
148}
149
150/*
151 * IO write completion for unwritten extents.
152 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 * Issue transactions to convert a buffer range from unwritten
Christoph Hellwigf0973862005-09-05 08:22:52 +1000154 * to written extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
156STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000157xfs_end_bio_unwritten(
158 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000160 xfs_ioend_t *ioend = data;
161 vnode_t *vp = ioend->io_vnode;
162 xfs_off_t offset = ioend->io_offset;
163 size_t size = ioend->io_size;
164 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Christoph Hellwig0829c362005-09-02 16:58:49 +1000166 if (ioend->io_uptodate)
167 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
168 xfs_destroy_ioend(ioend);
169}
170
171/*
172 * Allocate and initialise an IO completion structure.
173 * We need to track unwritten extent write completion here initially.
174 * We'll need to extend this for updating the ondisk inode size later
175 * (vs. incore size).
176 */
177STATIC xfs_ioend_t *
178xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100179 struct inode *inode,
180 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000181{
182 xfs_ioend_t *ioend;
183
184 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
185
186 /*
187 * Set the count to 1 initially, which will prevent an I/O
188 * completion callback from happening before we have started
189 * all the I/O from calling the completion routine too early.
190 */
191 atomic_set(&ioend->io_remaining, 1);
192 ioend->io_uptodate = 1; /* cleared if any I/O fails */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100193 ioend->io_list = NULL;
194 ioend->io_type = type;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000195 ioend->io_vnode = LINVFS_GET_VP(inode);
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000196 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100197 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000198 atomic_inc(&ioend->io_vnode->v_iocount);
199 ioend->io_offset = 0;
200 ioend->io_size = 0;
201
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100202 if (type == IOMAP_UNWRITTEN)
203 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
204 else if (type == IOMAP_DELAY)
205 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
206 else
207 INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000208
209 return ioend;
210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212STATIC int
213xfs_map_blocks(
214 struct inode *inode,
215 loff_t offset,
216 ssize_t count,
217 xfs_iomap_t *mapp,
218 int flags)
219{
220 vnode_t *vp = LINVFS_GET_VP(inode);
221 int error, nmaps = 1;
222
223 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
224 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
225 VMODIFY(vp);
226 return -error;
227}
228
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100229STATIC inline int
230xfs_iomap_valid(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 xfs_iomap_t *iomapp,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100232 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100234 return offset >= iomapp->iomap_offset &&
235 offset < iomapp->iomap_offset + iomapp->iomap_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100238/*
239 * BIO completion handler for buffered IO.
240 */
241STATIC int
242xfs_end_bio(
243 struct bio *bio,
244 unsigned int bytes_done,
245 int error)
246{
247 xfs_ioend_t *ioend = bio->bi_private;
248
249 if (bio->bi_size)
250 return 1;
251
252 ASSERT(ioend);
253 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
254
255 /* Toss bio and pass work off to an xfsdatad thread */
256 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
257 ioend->io_uptodate = 0;
258 bio->bi_private = NULL;
259 bio->bi_end_io = NULL;
260
261 bio_put(bio);
262 xfs_finish_ioend(ioend);
263 return 0;
264}
265
266STATIC void
267xfs_submit_ioend_bio(
268 xfs_ioend_t *ioend,
269 struct bio *bio)
270{
271 atomic_inc(&ioend->io_remaining);
272
273 bio->bi_private = ioend;
274 bio->bi_end_io = xfs_end_bio;
275
276 submit_bio(WRITE, bio);
277 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
278 bio_put(bio);
279}
280
281STATIC struct bio *
282xfs_alloc_ioend_bio(
283 struct buffer_head *bh)
284{
285 struct bio *bio;
286 int nvecs = bio_get_nr_vecs(bh->b_bdev);
287
288 do {
289 bio = bio_alloc(GFP_NOIO, nvecs);
290 nvecs >>= 1;
291 } while (!bio);
292
293 ASSERT(bio->bi_private == NULL);
294 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
295 bio->bi_bdev = bh->b_bdev;
296 bio_get(bio);
297 return bio;
298}
299
300STATIC void
301xfs_start_buffer_writeback(
302 struct buffer_head *bh)
303{
304 ASSERT(buffer_mapped(bh));
305 ASSERT(buffer_locked(bh));
306 ASSERT(!buffer_delay(bh));
307 ASSERT(!buffer_unwritten(bh));
308
309 mark_buffer_async_write(bh);
310 set_buffer_uptodate(bh);
311 clear_buffer_dirty(bh);
312}
313
314STATIC void
315xfs_start_page_writeback(
316 struct page *page,
317 struct writeback_control *wbc,
318 int clear_dirty,
319 int buffers)
320{
321 ASSERT(PageLocked(page));
322 ASSERT(!PageWriteback(page));
323 set_page_writeback(page);
324 if (clear_dirty)
325 clear_page_dirty(page);
326 unlock_page(page);
327 if (!buffers) {
328 end_page_writeback(page);
329 wbc->pages_skipped++; /* We didn't write this page */
330 }
331}
332
333static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
334{
335 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
336}
337
338/*
David Chinnerd88992f2006-01-18 13:38:12 +1100339 * Submit all of the bios for all of the ioends we have saved up, covering the
340 * initial writepage page and also any probed pages.
341 *
342 * Because we may have multiple ioends spanning a page, we need to start
343 * writeback on all the buffers before we submit them for I/O. If we mark the
344 * buffers as we got, then we can end up with a page that only has buffers
345 * marked async write and I/O complete on can occur before we mark the other
346 * buffers async write.
347 *
348 * The end result of this is that we trip a bug in end_page_writeback() because
349 * we call it twice for the one page as the code in end_buffer_async_write()
350 * assumes that all buffers on the page are started at the same time.
351 *
352 * The fix is two passes across the ioend list - one to start writeback on the
353 * bufferheads, and then the second one submit them for I/O.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100354 */
355STATIC void
356xfs_submit_ioend(
357 xfs_ioend_t *ioend)
358{
David Chinnerd88992f2006-01-18 13:38:12 +1100359 xfs_ioend_t *head = ioend;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100360 xfs_ioend_t *next;
361 struct buffer_head *bh;
362 struct bio *bio;
363 sector_t lastblock = 0;
364
David Chinnerd88992f2006-01-18 13:38:12 +1100365 /* Pass 1 - start writeback */
366 do {
367 next = ioend->io_list;
368 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
369 xfs_start_buffer_writeback(bh);
370 }
371 } while ((ioend = next) != NULL);
372
373 /* Pass 2 - submit I/O */
374 ioend = head;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100375 do {
376 next = ioend->io_list;
377 bio = NULL;
378
379 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100380
381 if (!bio) {
382 retry:
383 bio = xfs_alloc_ioend_bio(bh);
384 } else if (bh->b_blocknr != lastblock + 1) {
385 xfs_submit_ioend_bio(ioend, bio);
386 goto retry;
387 }
388
389 if (bio_add_buffer(bio, bh) != bh->b_size) {
390 xfs_submit_ioend_bio(ioend, bio);
391 goto retry;
392 }
393
394 lastblock = bh->b_blocknr;
395 }
396 if (bio)
397 xfs_submit_ioend_bio(ioend, bio);
398 xfs_finish_ioend(ioend);
399 } while ((ioend = next) != NULL);
400}
401
402/*
403 * Cancel submission of all buffer_heads so far in this endio.
404 * Toss the endio too. Only ever called for the initial page
405 * in a writepage request, so only ever one page.
406 */
407STATIC void
408xfs_cancel_ioend(
409 xfs_ioend_t *ioend)
410{
411 xfs_ioend_t *next;
412 struct buffer_head *bh, *next_bh;
413
414 do {
415 next = ioend->io_list;
416 bh = ioend->io_buffer_head;
417 do {
418 next_bh = bh->b_private;
419 clear_buffer_async_write(bh);
420 unlock_buffer(bh);
421 } while ((bh = next_bh) != NULL);
422
423 vn_iowake(ioend->io_vnode);
424 mempool_free(ioend, xfs_ioend_pool);
425 } while ((ioend = next) != NULL);
426}
427
428/*
429 * Test to see if we've been building up a completion structure for
430 * earlier buffers -- if so, we try to append to this ioend if we
431 * can, otherwise we finish off any current ioend and start another.
432 * Return true if we've finished the given ioend.
433 */
434STATIC void
435xfs_add_to_ioend(
436 struct inode *inode,
437 struct buffer_head *bh,
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100438 xfs_off_t offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100439 unsigned int type,
440 xfs_ioend_t **result,
441 int need_ioend)
442{
443 xfs_ioend_t *ioend = *result;
444
445 if (!ioend || need_ioend || type != ioend->io_type) {
446 xfs_ioend_t *previous = *result;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100447
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100448 ioend = xfs_alloc_ioend(inode, type);
449 ioend->io_offset = offset;
450 ioend->io_buffer_head = bh;
451 ioend->io_buffer_tail = bh;
452 if (previous)
453 previous->io_list = ioend;
454 *result = ioend;
455 } else {
456 ioend->io_buffer_tail->b_private = bh;
457 ioend->io_buffer_tail = bh;
458 }
459
460 bh->b_private = NULL;
461 ioend->io_size += bh->b_size;
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464STATIC void
465xfs_map_at_offset(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 struct buffer_head *bh,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100467 loff_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int block_bits,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100469 xfs_iomap_t *iomapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 xfs_daddr_t bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 int sector_shift;
473
474 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
475 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
476 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 sector_shift = block_bits - BBSHIFT;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100479 bn = (iomapp->iomap_bn >> sector_shift) +
480 ((offset - iomapp->iomap_offset) >> block_bits);
481
482 ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
484
485 lock_buffer(bh);
486 bh->b_blocknr = bn;
Nathan Scottce8e9222006-01-11 15:39:08 +1100487 bh->b_bdev = iomapp->iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 set_buffer_mapped(bh);
489 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100490 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/*
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100494 * Look for a page at index that is suitable for clustering.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 */
496STATIC unsigned int
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100497xfs_probe_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100498 struct page *page,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100499 unsigned int pg_offset,
500 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 int ret = 0;
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100505 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 if (page->mapping && PageDirty(page)) {
508 if (page_has_buffers(page)) {
509 struct buffer_head *bh, *head;
510
511 bh = head = page_buffers(page);
512 do {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100513 if (!buffer_uptodate(bh))
514 break;
515 if (mapped != buffer_mapped(bh))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 break;
517 ret += bh->b_size;
518 if (ret >= pg_offset)
519 break;
520 } while ((bh = bh->b_this_page) != head);
521 } else
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100522 ret = mapped ? 0 : PAGE_CACHE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return ret;
526}
527
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100528STATIC size_t
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100529xfs_probe_cluster(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 struct inode *inode,
531 struct page *startpage,
532 struct buffer_head *bh,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100533 struct buffer_head *head,
534 int mapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100536 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100538 size_t total = 0;
539 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 /* First sum forwards in this page */
542 do {
Eric Sandeen2353e8e2006-02-28 12:30:30 +1100543 if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100544 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 total += bh->b_size;
546 } while ((bh = bh->b_this_page) != head);
547
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100548 /* if we reached the end of the page, sum forwards in following pages */
549 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
550 tindex = startpage->index + 1;
551
552 /* Prune this back to avoid pathological behavior */
553 tloff = min(tlast, startpage->index + 64);
554
555 pagevec_init(&pvec, 0);
556 while (!done && tindex <= tloff) {
557 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
558
559 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
560 break;
561
562 for (i = 0; i < pagevec_count(&pvec); i++) {
563 struct page *page = pvec.pages[i];
564 size_t pg_offset, len = 0;
565
566 if (tindex == tlast) {
567 pg_offset =
568 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100569 if (!pg_offset) {
570 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100571 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100572 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100573 } else
574 pg_offset = PAGE_CACHE_SIZE;
575
576 if (page->index == tindex && !TestSetPageLocked(page)) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100577 len = xfs_probe_page(page, pg_offset, mapped);
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100578 unlock_page(page);
579 }
580
581 if (!len) {
582 done = 1;
583 break;
584 }
585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 total += len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100587 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100589
590 pagevec_release(&pvec);
591 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return total;
595}
596
597/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100598 * Test if a given page is suitable for writing as part of an unwritten
599 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100601STATIC int
602xfs_is_delayed_page(
603 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100604 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
609 if (page->mapping && page_has_buffers(page)) {
610 struct buffer_head *bh, *head;
611 int acceptable = 0;
612
613 bh = head = page_buffers(page);
614 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100615 if (buffer_unwritten(bh))
616 acceptable = (type == IOMAP_UNWRITTEN);
617 else if (buffer_delay(bh))
618 acceptable = (type == IOMAP_DELAY);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100619 else if (buffer_mapped(bh))
620 acceptable = (type == 0);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100621 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 } while ((bh = bh->b_this_page) != head);
624
625 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100626 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
628
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632/*
633 * Allocate & map buffers for page given the extent map. Write it out.
634 * except for the original page of a writepage, this is called on
635 * delalloc/unwritten pages only, for the original page it is possible
636 * that the page has no mapping at all.
637 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100638STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639xfs_convert_page(
640 struct inode *inode,
641 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100642 loff_t tindex,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100643 xfs_iomap_t *mp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100644 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 int startio,
647 int all_bh)
648{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100649 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100650 xfs_off_t end_offset;
651 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100652 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 int bbits = inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700654 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100655 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100656 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100658 if (page->index != tindex)
659 goto fail;
660 if (TestSetPageLocked(page))
661 goto fail;
662 if (PageWriteback(page))
663 goto fail_unlock_page;
664 if (page->mapping != inode->i_mapping)
665 goto fail_unlock_page;
666 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
667 goto fail_unlock_page;
668
Nathan Scott24e17b52005-05-05 13:33:20 -0700669 /*
670 * page_dirty is initially a count of buffers on the page before
671 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100672 *
673 * Derivation:
674 *
675 * End offset is the highest offset that this page should represent.
676 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
677 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
678 * hence give us the correct page_dirty count. On any other page,
679 * it will be zero and in that case we need page_dirty to be the
680 * count of buffers on the page.
Nathan Scott24e17b52005-05-05 13:33:20 -0700681 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100682 end_offset = min_t(unsigned long long,
683 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
684 i_size_read(inode));
685
Nathan Scott24e17b52005-05-05 13:33:20 -0700686 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100687 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
688 PAGE_CACHE_SIZE);
689 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
690 page_dirty = p_offset / len;
Nathan Scott24e17b52005-05-05 13:33:20 -0700691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 bh = head = page_buffers(page);
693 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100694 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100696 if (!buffer_uptodate(bh))
697 uptodate = 0;
698 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
699 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100701 }
702
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100703 if (buffer_unwritten(bh) || buffer_delay(bh)) {
704 if (buffer_unwritten(bh))
705 type = IOMAP_UNWRITTEN;
706 else
707 type = IOMAP_DELAY;
708
709 if (!xfs_iomap_valid(mp, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100710 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100711 continue;
712 }
713
714 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
715 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
716
717 xfs_map_at_offset(bh, offset, bbits, mp);
718 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100719 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100720 type, ioendp, done);
721 } else {
722 set_buffer_dirty(bh);
723 unlock_buffer(bh);
724 mark_buffer_dirty(bh);
725 }
726 page_dirty--;
727 count++;
728 } else {
729 type = 0;
730 if (buffer_mapped(bh) && all_bh && startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 lock_buffer(bh);
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100732 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100733 type, ioendp, done);
734 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700735 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100736 } else {
737 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100740 } while (offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100742 if (uptodate && bh == head)
743 SetPageUptodate(page);
744
745 if (startio) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100746 if (count) {
747 struct backing_dev_info *bdi;
748
749 bdi = inode->i_mapping->backing_dev_info;
David Chinner9fddaca2006-02-07 20:27:24 +1100750 wbc->nr_to_write--;
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100751 if (bdi_write_congested(bdi)) {
752 wbc->encountered_congestion = 1;
753 done = 1;
David Chinner9fddaca2006-02-07 20:27:24 +1100754 } else if (wbc->nr_to_write <= 0) {
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100755 done = 1;
756 }
757 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100758 xfs_start_page_writeback(page, wbc, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100760
761 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100762 fail_unlock_page:
763 unlock_page(page);
764 fail:
765 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
768/*
769 * Convert & write out a cluster of pages in the same extent as defined
770 * by mp and following the start page.
771 */
772STATIC void
773xfs_cluster_write(
774 struct inode *inode,
775 pgoff_t tindex,
776 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100777 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 struct writeback_control *wbc,
779 int startio,
780 int all_bh,
781 pgoff_t tlast)
782{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100783 struct pagevec pvec;
784 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100786 pagevec_init(&pvec, 0);
787 while (!done && tindex <= tlast) {
788 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
789
790 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100792
793 for (i = 0; i < pagevec_count(&pvec); i++) {
794 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
795 iomapp, ioendp, wbc, startio, all_bh);
796 if (done)
797 break;
798 }
799
800 pagevec_release(&pvec);
801 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803}
804
805/*
806 * Calling this without startio set means we are being asked to make a dirty
807 * page ready for freeing it's buffers. When called with startio set then
808 * we are coming from writepage.
809 *
810 * When called with startio set it is important that we write the WHOLE
811 * page if possible.
812 * The bh->b_state's cannot know if any of the blocks or which block for
813 * that matter are dirty due to mmap writes, and therefore bh uptodate is
814 * only vaild if the page itself isn't completely uptodate. Some layers
815 * may clear the page dirty flag prior to calling write page, under the
816 * assumption the entire page will be written out; by not writing out the
817 * whole page the page can be reused before all valid dirty data is
818 * written out. Note: in the case of a page that has been dirty'd by
819 * mapwrite and but partially setup by block_prepare_write the
820 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
821 * valid state, thus the whole page must be written out thing.
822 */
823
824STATIC int
825xfs_page_state_convert(
826 struct inode *inode,
827 struct page *page,
828 struct writeback_control *wbc,
829 int startio,
830 int unmapped) /* also implies page uptodate */
831{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100832 struct buffer_head *bh, *head;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100833 xfs_iomap_t iomap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100834 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 loff_t offset;
836 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100837 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 __uint64_t end_offset;
839 pgoff_t end_index, last_index, tlast;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100840 ssize_t size, len;
841 int flags, err, iomap_valid = 0, uptodate = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100842 int page_dirty, count = 0, trylock_flag = 0;
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100843 int all_bh = unmapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Daniel Moore3ba08152005-05-05 13:31:34 -0700845 /* wait for other IO threads? */
Christoph Hellwigf5e596b2006-01-11 20:49:42 +1100846 if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100847 trylock_flag |= BMAPI_TRYLOCK;
Daniel Moore3ba08152005-05-05 13:31:34 -0700848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 /* Is this page beyond the end of the file? */
850 offset = i_size_read(inode);
851 end_index = offset >> PAGE_CACHE_SHIFT;
852 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
853 if (page->index >= end_index) {
854 if ((page->index >= end_index + 1) ||
855 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100856 if (startio)
857 unlock_page(page);
858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860 }
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /*
Nathan Scott24e17b52005-05-05 13:33:20 -0700863 * page_dirty is initially a count of buffers on the page before
864 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100865 *
866 * Derivation:
867 *
868 * End offset is the highest offset that this page should represent.
869 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
870 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
871 * hence give us the correct page_dirty count. On any other page,
872 * it will be zero and in that case we need page_dirty to be the
873 * count of buffers on the page.
874 */
875 end_offset = min_t(unsigned long long,
876 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700877 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100878 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
879 PAGE_CACHE_SIZE);
880 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -0700881 page_dirty = p_offset / len;
882
Nathan Scott24e17b52005-05-05 13:33:20 -0700883 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100884 offset = page_offset(page);
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100885 flags = -1;
886 type = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100887
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100888 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
890 do {
891 if (offset >= end_offset)
892 break;
893 if (!buffer_uptodate(bh))
894 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100895 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100896 /*
897 * the iomap is actually still valid, but the ioend
898 * isn't. shouldn't happen too often.
899 */
900 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100904 if (iomap_valid)
905 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /*
908 * First case, map an unwritten extent and prepare for
909 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100910 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 * Second case, allocate space for a delalloc buffer.
912 * We can return EAGAIN here in the release page case.
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100913 *
914 * Third case, an unmapped buffer was found, and we are
915 * in a path where we need to write the whole page out.
916 */
917 if (buffer_unwritten(bh) || buffer_delay(bh) ||
918 ((buffer_uptodate(bh) || PageUptodate(page)) &&
919 !buffer_mapped(bh) && (unmapped || startio))) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100920 /*
921 * Make sure we don't use a read-only iomap
922 */
923 if (flags == BMAPI_READ)
924 iomap_valid = 0;
925
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100926 if (buffer_unwritten(bh)) {
927 type = IOMAP_UNWRITTEN;
928 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100929 } else if (buffer_delay(bh)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100930 type = IOMAP_DELAY;
931 flags = BMAPI_ALLOCATE;
932 if (!startio)
933 flags |= trylock_flag;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100934 } else {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100935 type = IOMAP_NEW;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100936 flags = BMAPI_WRITE|BMAPI_MMAP;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100937 }
938
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100939 if (!iomap_valid) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100940 if (type == IOMAP_NEW) {
941 size = xfs_probe_cluster(inode,
942 page, bh, head, 0);
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100943 } else {
944 size = len;
945 }
946
947 err = xfs_map_blocks(inode, offset, size,
948 &iomap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100949 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 goto error;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100951 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100953 if (iomap_valid) {
954 xfs_map_at_offset(bh, offset,
955 inode->i_blkbits, &iomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 if (startio) {
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100957 xfs_add_to_ioend(inode, bh, offset,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100958 type, &ioend,
959 !iomap_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 } else {
961 set_buffer_dirty(bh);
962 unlock_buffer(bh);
963 mark_buffer_dirty(bh);
964 }
965 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100966 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100968 } else if (buffer_uptodate(bh) && startio) {
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100969 /*
970 * we got here because the buffer is already mapped.
971 * That means it must already have extents allocated
972 * underneath it. Map the extent by reading it.
973 */
974 if (!iomap_valid || type != 0) {
975 flags = BMAPI_READ;
976 size = xfs_probe_cluster(inode, page, bh,
977 head, 1);
978 err = xfs_map_blocks(inode, offset, size,
979 &iomap, flags);
980 if (err)
981 goto error;
982 iomap_valid = xfs_iomap_valid(&iomap, offset);
983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100985 type = 0;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100986 if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
987 ASSERT(buffer_mapped(bh));
Christoph Hellwig6c4fe192006-01-11 20:49:28 +1100988 if (iomap_valid)
989 all_bh = 1;
Christoph Hellwig7336cea2006-01-11 20:49:16 +1100990 xfs_add_to_ioend(inode, bh, offset, type,
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100991 &ioend, !iomap_valid);
992 page_dirty--;
993 count++;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100994 } else {
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100995 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100997 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
998 (unmapped || startio)) {
999 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001001
1002 if (!iohead)
1003 iohead = ioend;
1004
1005 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (uptodate && bh == head)
1008 SetPageUptodate(page);
1009
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001010 if (startio)
1011 xfs_start_page_writeback(page, wbc, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001013 if (ioend && iomap_valid) {
1014 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -07001016 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwig1defeac2006-01-11 20:48:33 +11001017 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
Christoph Hellwig6c4fe192006-01-11 20:49:28 +11001018 wbc, startio, all_bh, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 }
1020
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001021 if (iohead)
1022 xfs_submit_ioend(iohead);
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return page_dirty;
1025
1026error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001027 if (iohead)
1028 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /*
1031 * If it's delalloc and we have nowhere to put it,
1032 * throw it away, unless the lower layers told
1033 * us to try again.
1034 */
1035 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001036 if (!unmapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 block_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 ClearPageUptodate(page);
1039 }
1040 return err;
1041}
1042
1043STATIC int
1044__linvfs_get_block(
1045 struct inode *inode,
1046 sector_t iblock,
1047 unsigned long blocks,
1048 struct buffer_head *bh_result,
1049 int create,
1050 int direct,
1051 bmapi_flags_t flags)
1052{
1053 vnode_t *vp = LINVFS_GET_VP(inode);
1054 xfs_iomap_t iomap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001055 xfs_off_t offset;
1056 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 int retpbbm = 1;
1058 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001060 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scotta4656392005-11-25 16:41:57 +11001061 if (blocks)
1062 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1063 (xfs_off_t)blocks << inode->i_blkbits);
1064 else
1065 size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 VOP_BMAP(vp, offset, size,
1068 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1069 if (error)
1070 return -error;
1071
1072 if (retpbbm == 0)
1073 return 0;
1074
1075 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001076 xfs_daddr_t bn;
1077 xfs_off_t delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /* For unwritten extents do not report a disk address on
1080 * the read case (treat as if we're reading into a hole).
1081 */
1082 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1083 delta = offset - iomap.iomap_offset;
1084 delta >>= inode->i_blkbits;
1085
1086 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1087 bn += delta;
1088 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1089 bh_result->b_blocknr = bn;
1090 set_buffer_mapped(bh_result);
1091 }
1092 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1093 if (direct)
1094 bh_result->b_private = inode;
1095 set_buffer_unwritten(bh_result);
1096 set_buffer_delay(bh_result);
1097 }
1098 }
1099
1100 /* If this is a realtime file, data might be on a new device */
Nathan Scottce8e9222006-01-11 15:39:08 +11001101 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* If we previously allocated a block out beyond eof and
1104 * we are now coming back to use it then we will need to
1105 * flag it as new even if it has a disk address.
1106 */
1107 if (create &&
1108 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001109 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (iomap.iomap_flags & IOMAP_DELAY) {
1113 BUG_ON(direct);
1114 if (create) {
1115 set_buffer_uptodate(bh_result);
1116 set_buffer_mapped(bh_result);
1117 set_buffer_delay(bh_result);
1118 }
1119 }
1120
1121 if (blocks) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001122 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1123 offset = min_t(xfs_off_t,
1124 iomap.iomap_bsize - iomap.iomap_delta,
Nathan Scotta4656392005-11-25 16:41:57 +11001125 (xfs_off_t)blocks << inode->i_blkbits);
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001126 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 }
1128
1129 return 0;
1130}
1131
1132int
1133linvfs_get_block(
1134 struct inode *inode,
1135 sector_t iblock,
1136 struct buffer_head *bh_result,
1137 int create)
1138{
1139 return __linvfs_get_block(inode, iblock, 0, bh_result,
1140 create, 0, BMAPI_WRITE);
1141}
1142
1143STATIC int
1144linvfs_get_blocks_direct(
1145 struct inode *inode,
1146 sector_t iblock,
1147 unsigned long max_blocks,
1148 struct buffer_head *bh_result,
1149 int create)
1150{
1151 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1152 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1153}
1154
Christoph Hellwigf0973862005-09-05 08:22:52 +10001155STATIC void
1156linvfs_end_io_direct(
1157 struct kiocb *iocb,
1158 loff_t offset,
1159 ssize_t size,
1160 void *private)
1161{
1162 xfs_ioend_t *ioend = iocb->private;
1163
1164 /*
1165 * Non-NULL private data means we need to issue a transaction to
1166 * convert a range from unwritten to written extents. This needs
1167 * to happen from process contect but aio+dio I/O completion
1168 * happens from irq context so we need to defer it to a workqueue.
1169 * This is not nessecary for synchronous direct I/O, but we do
1170 * it anyway to keep the code uniform and simpler.
1171 *
1172 * The core direct I/O code might be changed to always call the
1173 * completion handler in the future, in which case all this can
1174 * go away.
1175 */
1176 if (private && size > 0) {
1177 ioend->io_offset = offset;
1178 ioend->io_size = size;
1179 xfs_finish_ioend(ioend);
1180 } else {
1181 ASSERT(size >= 0);
1182 xfs_destroy_ioend(ioend);
1183 }
1184
1185 /*
1186 * blockdev_direct_IO can return an error even afer the I/O
1187 * completion handler was called. Thus we need to protect
1188 * against double-freeing.
1189 */
1190 iocb->private = NULL;
1191}
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193STATIC ssize_t
1194linvfs_direct_IO(
1195 int rw,
1196 struct kiocb *iocb,
1197 const struct iovec *iov,
1198 loff_t offset,
1199 unsigned long nr_segs)
1200{
1201 struct file *file = iocb->ki_filp;
1202 struct inode *inode = file->f_mapping->host;
1203 vnode_t *vp = LINVFS_GET_VP(inode);
1204 xfs_iomap_t iomap;
1205 int maps = 1;
1206 int error;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001207 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
1209 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1210 if (error)
1211 return -error;
1212
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001213 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001214
1215 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
Nathan Scottce8e9222006-01-11 15:39:08 +11001216 iomap.iomap_target->bt_bdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 iov, offset, nr_segs,
1218 linvfs_get_blocks_direct,
Christoph Hellwigf0973862005-09-05 08:22:52 +10001219 linvfs_end_io_direct);
1220
1221 if (unlikely(ret <= 0 && iocb->private))
1222 xfs_destroy_ioend(iocb->private);
1223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224}
1225
1226
1227STATIC sector_t
1228linvfs_bmap(
1229 struct address_space *mapping,
1230 sector_t block)
1231{
1232 struct inode *inode = (struct inode *)mapping->host;
1233 vnode_t *vp = LINVFS_GET_VP(inode);
1234 int error;
1235
1236 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1237
1238 VOP_RWLOCK(vp, VRWLOCK_READ);
1239 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1240 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1241 return generic_block_bmap(mapping, block, linvfs_get_block);
1242}
1243
1244STATIC int
1245linvfs_readpage(
1246 struct file *unused,
1247 struct page *page)
1248{
1249 return mpage_readpage(page, linvfs_get_block);
1250}
1251
1252STATIC int
1253linvfs_readpages(
1254 struct file *unused,
1255 struct address_space *mapping,
1256 struct list_head *pages,
1257 unsigned nr_pages)
1258{
1259 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1260}
1261
1262STATIC void
1263xfs_count_page_state(
1264 struct page *page,
1265 int *delalloc,
1266 int *unmapped,
1267 int *unwritten)
1268{
1269 struct buffer_head *bh, *head;
1270
1271 *delalloc = *unmapped = *unwritten = 0;
1272
1273 bh = head = page_buffers(page);
1274 do {
1275 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1276 (*unmapped) = 1;
1277 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1278 clear_buffer_unwritten(bh);
1279 else if (buffer_unwritten(bh))
1280 (*unwritten) = 1;
1281 else if (buffer_delay(bh))
1282 (*delalloc) = 1;
1283 } while ((bh = bh->b_this_page) != head);
1284}
1285
1286
1287/*
1288 * writepage: Called from one of two places:
1289 *
1290 * 1. we are flushing a delalloc buffer head.
1291 *
1292 * 2. we are writing out a dirty page. Typically the page dirty
1293 * state is cleared before we get here. In this case is it
1294 * conceivable we have no buffer heads.
1295 *
1296 * For delalloc space on the page we need to allocate space and
1297 * flush it. For unmapped buffer heads on the page we should
1298 * allocate space if the page is uptodate. For any other dirty
1299 * buffer heads on the page we should flush them.
1300 *
1301 * If we detect that a transaction would be required to flush
1302 * the page, we have to check the process flags first, if we
1303 * are already in a transaction or disk I/O during allocations
1304 * is off, we need to fail the writepage and redirty the page.
1305 */
1306
1307STATIC int
1308linvfs_writepage(
1309 struct page *page,
1310 struct writeback_control *wbc)
1311{
1312 int error;
1313 int need_trans;
1314 int delalloc, unmapped, unwritten;
1315 struct inode *inode = page->mapping->host;
1316
1317 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1318
1319 /*
1320 * We need a transaction if:
1321 * 1. There are delalloc buffers on the page
1322 * 2. The page is uptodate and we have unmapped buffers
1323 * 3. The page is uptodate and we have no buffers
1324 * 4. There are unwritten buffers on the page
1325 */
1326
1327 if (!page_has_buffers(page)) {
1328 unmapped = 1;
1329 need_trans = 1;
1330 } else {
1331 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1332 if (!PageUptodate(page))
1333 unmapped = 0;
1334 need_trans = delalloc + unmapped + unwritten;
1335 }
1336
1337 /*
1338 * If we need a transaction and the process flags say
1339 * we are already in a transaction, or no IO is allowed
1340 * then mark the page dirty again and leave the page
1341 * as is.
1342 */
1343 if (PFLAGS_TEST_FSTRANS() && need_trans)
1344 goto out_fail;
1345
1346 /*
1347 * Delay hooking up buffer heads until we have
1348 * made our go/no-go decision.
1349 */
1350 if (!page_has_buffers(page))
1351 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1352
1353 /*
1354 * Convert delayed allocate, unwritten or unmapped space
1355 * to real space and flush out to disk.
1356 */
1357 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1358 if (error == -EAGAIN)
1359 goto out_fail;
1360 if (unlikely(error < 0))
1361 goto out_unlock;
1362
1363 return 0;
1364
1365out_fail:
1366 redirty_page_for_writepage(wbc, page);
1367 unlock_page(page);
1368 return 0;
1369out_unlock:
1370 unlock_page(page);
1371 return error;
1372}
1373
Nathan Scottbcec2b72005-09-02 16:40:17 +10001374STATIC int
1375linvfs_invalidate_page(
1376 struct page *page,
1377 unsigned long offset)
1378{
1379 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1380 page->mapping->host, page, offset);
1381 return block_invalidatepage(page, offset);
1382}
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384/*
1385 * Called to move a page into cleanable state - and from there
1386 * to be released. Possibly the page is already clean. We always
1387 * have buffer heads in this call.
1388 *
1389 * Returns 0 if the page is ok to release, 1 otherwise.
1390 *
1391 * Possible scenarios are:
1392 *
1393 * 1. We are being called to release a page which has been written
1394 * to via regular I/O. buffer heads will be dirty and possibly
1395 * delalloc. If no delalloc buffer heads in this case then we
1396 * can just return zero.
1397 *
1398 * 2. We are called to release a page which has been written via
1399 * mmap, all we need to do is ensure there is no delalloc
1400 * state in the buffer heads, if not we can let the caller
1401 * free them and we should come back later via writepage.
1402 */
1403STATIC int
1404linvfs_release_page(
1405 struct page *page,
Al Viro27496a82005-10-21 03:20:48 -04001406 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407{
1408 struct inode *inode = page->mapping->host;
1409 int dirty, delalloc, unmapped, unwritten;
1410 struct writeback_control wbc = {
1411 .sync_mode = WB_SYNC_ALL,
1412 .nr_to_write = 1,
1413 };
1414
1415 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1416
1417 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1418 if (!delalloc && !unwritten)
1419 goto free_buffers;
1420
1421 if (!(gfp_mask & __GFP_FS))
1422 return 0;
1423
1424 /* If we are already inside a transaction or the thread cannot
1425 * do I/O, we cannot release this page.
1426 */
1427 if (PFLAGS_TEST_FSTRANS())
1428 return 0;
1429
1430 /*
1431 * Convert delalloc space to real space, do not flush the
1432 * data out to disk, that will be done by the caller.
1433 * Never need to allocate space here - we will always
1434 * come back to writepage in that case.
1435 */
1436 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1437 if (dirty == 0 && !unwritten)
1438 goto free_buffers;
1439 return 0;
1440
1441free_buffers:
1442 return try_to_free_buffers(page);
1443}
1444
1445STATIC int
1446linvfs_prepare_write(
1447 struct file *file,
1448 struct page *page,
1449 unsigned int from,
1450 unsigned int to)
1451{
1452 return block_prepare_write(page, from, to, linvfs_get_block);
1453}
1454
1455struct 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};