blob: e99d04d3fe82214a17e141fd6b86f32f006ff666 [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>
43#include <linux/writeback.h>
44
45STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#if defined(XFS_RW_TRACE)
48void
49xfs_page_trace(
50 int tag,
51 struct inode *inode,
52 struct page *page,
53 int mask)
54{
55 xfs_inode_t *ip;
56 bhv_desc_t *bdp;
57 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
65 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
66 ip = XFS_BHVTOI(bdp);
67 if (!ip->i_rwtrace)
68 return;
69
70 ktrace_enter(ip->i_rwtrace,
71 (void *)((unsigned long)tag),
72 (void *)ip,
73 (void *)inode,
74 (void *)page,
75 (void *)((unsigned long)mask),
76 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
77 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
78 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
79 (void *)((unsigned long)(isize & 0xffffffff)),
80 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
81 (void *)((unsigned long)(offset & 0xffffffff)),
82 (void *)((unsigned long)delalloc),
83 (void *)((unsigned long)unmapped),
84 (void *)((unsigned long)unwritten),
85 (void *)NULL,
86 (void *)NULL);
87}
88#else
89#define xfs_page_trace(tag, inode, page, mask)
90#endif
91
Christoph Hellwig0829c362005-09-02 16:58:49 +100092/*
93 * Schedule IO completion handling on a xfsdatad if this was
94 * the final hold on this ioend.
95 */
96STATIC void
97xfs_finish_ioend(
98 xfs_ioend_t *ioend)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000100 if (atomic_dec_and_test(&ioend->io_remaining))
101 queue_work(xfsdatad_workqueue, &ioend->io_work);
102}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100104/*
105 * We're now finished for good with this ioend structure.
106 * Update the page state via the associated buffer_heads,
107 * release holds on the inode and bio, and finally free
108 * up memory. Do not use the ioend after this.
109 */
Christoph Hellwig0829c362005-09-02 16:58:49 +1000110STATIC void
111xfs_destroy_ioend(
112 xfs_ioend_t *ioend)
113{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100114 struct buffer_head *bh, *next;
115
116 for (bh = ioend->io_buffer_head; bh; bh = next) {
117 next = bh->b_private;
118 bh->b_end_io(bh, ioend->io_uptodate);
119 }
120
Christoph Hellwig0829c362005-09-02 16:58:49 +1000121 vn_iowake(ioend->io_vnode);
122 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
125/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100126 * Buffered IO write completion for delayed allocate extents.
127 * TODO: Update ondisk isize now that we know the file data
128 * has been flushed (i.e. the notorious "NULL file" problem).
129 */
130STATIC void
131xfs_end_bio_delalloc(
132 void *data)
133{
134 xfs_ioend_t *ioend = data;
135
136 xfs_destroy_ioend(ioend);
137}
138
139/*
140 * Buffered IO write completion for regular, written extents.
141 */
142STATIC void
143xfs_end_bio_written(
144 void *data)
145{
146 xfs_ioend_t *ioend = data;
147
148 xfs_destroy_ioend(ioend);
149}
150
151/*
152 * IO write completion for unwritten extents.
153 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 * Issue transactions to convert a buffer range from unwritten
Christoph Hellwigf0973862005-09-05 08:22:52 +1000155 * to written extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
157STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000158xfs_end_bio_unwritten(
159 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000161 xfs_ioend_t *ioend = data;
162 vnode_t *vp = ioend->io_vnode;
163 xfs_off_t offset = ioend->io_offset;
164 size_t size = ioend->io_size;
165 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwig0829c362005-09-02 16:58:49 +1000167 if (ioend->io_uptodate)
168 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
169 xfs_destroy_ioend(ioend);
170}
171
172/*
173 * Allocate and initialise an IO completion structure.
174 * We need to track unwritten extent write completion here initially.
175 * We'll need to extend this for updating the ondisk inode size later
176 * (vs. incore size).
177 */
178STATIC xfs_ioend_t *
179xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100180 struct inode *inode,
181 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000182{
183 xfs_ioend_t *ioend;
184
185 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
186
187 /*
188 * Set the count to 1 initially, which will prevent an I/O
189 * completion callback from happening before we have started
190 * all the I/O from calling the completion routine too early.
191 */
192 atomic_set(&ioend->io_remaining, 1);
193 ioend->io_uptodate = 1; /* cleared if any I/O fails */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100194 ioend->io_list = NULL;
195 ioend->io_type = type;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000196 ioend->io_vnode = LINVFS_GET_VP(inode);
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000197 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100198 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000199 atomic_inc(&ioend->io_vnode->v_iocount);
200 ioend->io_offset = 0;
201 ioend->io_size = 0;
202
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100203 if (type == IOMAP_UNWRITTEN)
204 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
205 else if (type == IOMAP_DELAY)
206 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
207 else
208 INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000209
210 return ioend;
211}
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213STATIC int
214xfs_map_blocks(
215 struct inode *inode,
216 loff_t offset,
217 ssize_t count,
218 xfs_iomap_t *mapp,
219 int flags)
220{
221 vnode_t *vp = LINVFS_GET_VP(inode);
222 int error, nmaps = 1;
223
224 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
225 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
226 VMODIFY(vp);
227 return -error;
228}
229
230/*
231 * Finds the corresponding mapping in block @map array of the
232 * given @offset within a @page.
233 */
234STATIC xfs_iomap_t *
235xfs_offset_to_map(
236 struct page *page,
237 xfs_iomap_t *iomapp,
238 unsigned long offset)
239{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100240 xfs_off_t full_offset; /* offset from start of file */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 ASSERT(offset < PAGE_CACHE_SIZE);
243
244 full_offset = page->index; /* NB: using 64bit number */
245 full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
246 full_offset += offset; /* offset from page start */
247
248 if (full_offset < iomapp->iomap_offset)
249 return NULL;
250 if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
251 return iomapp;
252 return NULL;
253}
254
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100255/*
256 * BIO completion handler for buffered IO.
257 */
258STATIC int
259xfs_end_bio(
260 struct bio *bio,
261 unsigned int bytes_done,
262 int error)
263{
264 xfs_ioend_t *ioend = bio->bi_private;
265
266 if (bio->bi_size)
267 return 1;
268
269 ASSERT(ioend);
270 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
271
272 /* Toss bio and pass work off to an xfsdatad thread */
273 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
274 ioend->io_uptodate = 0;
275 bio->bi_private = NULL;
276 bio->bi_end_io = NULL;
277
278 bio_put(bio);
279 xfs_finish_ioend(ioend);
280 return 0;
281}
282
283STATIC void
284xfs_submit_ioend_bio(
285 xfs_ioend_t *ioend,
286 struct bio *bio)
287{
288 atomic_inc(&ioend->io_remaining);
289
290 bio->bi_private = ioend;
291 bio->bi_end_io = xfs_end_bio;
292
293 submit_bio(WRITE, bio);
294 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
295 bio_put(bio);
296}
297
298STATIC struct bio *
299xfs_alloc_ioend_bio(
300 struct buffer_head *bh)
301{
302 struct bio *bio;
303 int nvecs = bio_get_nr_vecs(bh->b_bdev);
304
305 do {
306 bio = bio_alloc(GFP_NOIO, nvecs);
307 nvecs >>= 1;
308 } while (!bio);
309
310 ASSERT(bio->bi_private == NULL);
311 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
312 bio->bi_bdev = bh->b_bdev;
313 bio_get(bio);
314 return bio;
315}
316
317STATIC void
318xfs_start_buffer_writeback(
319 struct buffer_head *bh)
320{
321 ASSERT(buffer_mapped(bh));
322 ASSERT(buffer_locked(bh));
323 ASSERT(!buffer_delay(bh));
324 ASSERT(!buffer_unwritten(bh));
325
326 mark_buffer_async_write(bh);
327 set_buffer_uptodate(bh);
328 clear_buffer_dirty(bh);
329}
330
331STATIC void
332xfs_start_page_writeback(
333 struct page *page,
334 struct writeback_control *wbc,
335 int clear_dirty,
336 int buffers)
337{
338 ASSERT(PageLocked(page));
339 ASSERT(!PageWriteback(page));
340 set_page_writeback(page);
341 if (clear_dirty)
342 clear_page_dirty(page);
343 unlock_page(page);
344 if (!buffers) {
345 end_page_writeback(page);
346 wbc->pages_skipped++; /* We didn't write this page */
347 }
348}
349
350static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
351{
352 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
353}
354
355/*
356 * Submit all of the bios for all of the ioends we have saved up,
357 * covering the initial writepage page and also any probed pages.
358 */
359STATIC void
360xfs_submit_ioend(
361 xfs_ioend_t *ioend)
362{
363 xfs_ioend_t *next;
364 struct buffer_head *bh;
365 struct bio *bio;
366 sector_t lastblock = 0;
367
368 do {
369 next = ioend->io_list;
370 bio = NULL;
371
372 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
373 xfs_start_buffer_writeback(bh);
374
375 if (!bio) {
376 retry:
377 bio = xfs_alloc_ioend_bio(bh);
378 } else if (bh->b_blocknr != lastblock + 1) {
379 xfs_submit_ioend_bio(ioend, bio);
380 goto retry;
381 }
382
383 if (bio_add_buffer(bio, bh) != bh->b_size) {
384 xfs_submit_ioend_bio(ioend, bio);
385 goto retry;
386 }
387
388 lastblock = bh->b_blocknr;
389 }
390 if (bio)
391 xfs_submit_ioend_bio(ioend, bio);
392 xfs_finish_ioend(ioend);
393 } while ((ioend = next) != NULL);
394}
395
396/*
397 * Cancel submission of all buffer_heads so far in this endio.
398 * Toss the endio too. Only ever called for the initial page
399 * in a writepage request, so only ever one page.
400 */
401STATIC void
402xfs_cancel_ioend(
403 xfs_ioend_t *ioend)
404{
405 xfs_ioend_t *next;
406 struct buffer_head *bh, *next_bh;
407
408 do {
409 next = ioend->io_list;
410 bh = ioend->io_buffer_head;
411 do {
412 next_bh = bh->b_private;
413 clear_buffer_async_write(bh);
414 unlock_buffer(bh);
415 } while ((bh = next_bh) != NULL);
416
417 vn_iowake(ioend->io_vnode);
418 mempool_free(ioend, xfs_ioend_pool);
419 } while ((ioend = next) != NULL);
420}
421
422/*
423 * Test to see if we've been building up a completion structure for
424 * earlier buffers -- if so, we try to append to this ioend if we
425 * can, otherwise we finish off any current ioend and start another.
426 * Return true if we've finished the given ioend.
427 */
428STATIC void
429xfs_add_to_ioend(
430 struct inode *inode,
431 struct buffer_head *bh,
432 unsigned int p_offset,
433 unsigned int type,
434 xfs_ioend_t **result,
435 int need_ioend)
436{
437 xfs_ioend_t *ioend = *result;
438
439 if (!ioend || need_ioend || type != ioend->io_type) {
440 xfs_ioend_t *previous = *result;
441 xfs_off_t offset;
442
443 offset = (xfs_off_t)bh->b_page->index << PAGE_CACHE_SHIFT;
444 offset += p_offset;
445 ioend = xfs_alloc_ioend(inode, type);
446 ioend->io_offset = offset;
447 ioend->io_buffer_head = bh;
448 ioend->io_buffer_tail = bh;
449 if (previous)
450 previous->io_list = ioend;
451 *result = ioend;
452 } else {
453 ioend->io_buffer_tail->b_private = bh;
454 ioend->io_buffer_tail = bh;
455 }
456
457 bh->b_private = NULL;
458 ioend->io_size += bh->b_size;
459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461STATIC void
462xfs_map_at_offset(
463 struct page *page,
464 struct buffer_head *bh,
465 unsigned long offset,
466 int block_bits,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100467 xfs_iomap_t *iomapp,
468 xfs_ioend_t *ioend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 xfs_daddr_t bn;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100471 xfs_off_t delta;
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
478 delta = page->index;
479 delta <<= PAGE_CACHE_SHIFT;
480 delta += offset;
481 delta -= iomapp->iomap_offset;
482 delta >>= block_bits;
483
484 sector_shift = block_bits - BBSHIFT;
485 bn = iomapp->iomap_bn >> sector_shift;
486 bn += delta;
487 BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
488 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
489
490 lock_buffer(bh);
491 bh->b_blocknr = bn;
Nathan Scottce8e9222006-01-11 15:39:08 +1100492 bh->b_bdev = iomapp->iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 set_buffer_mapped(bh);
494 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100495 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
498/*
499 * Look for a page at index which is unlocked and not mapped
500 * yet - clustering for mmap write case.
501 */
502STATIC unsigned int
503xfs_probe_unmapped_page(
504 struct address_space *mapping,
505 pgoff_t index,
506 unsigned int pg_offset)
507{
508 struct page *page;
509 int ret = 0;
510
511 page = find_trylock_page(mapping, index);
512 if (!page)
513 return 0;
514 if (PageWriteback(page))
515 goto out;
516
517 if (page->mapping && PageDirty(page)) {
518 if (page_has_buffers(page)) {
519 struct buffer_head *bh, *head;
520
521 bh = head = page_buffers(page);
522 do {
523 if (buffer_mapped(bh) || !buffer_uptodate(bh))
524 break;
525 ret += bh->b_size;
526 if (ret >= pg_offset)
527 break;
528 } while ((bh = bh->b_this_page) != head);
529 } else
530 ret = PAGE_CACHE_SIZE;
531 }
532
533out:
534 unlock_page(page);
535 return ret;
536}
537
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100538STATIC size_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539xfs_probe_unmapped_cluster(
540 struct inode *inode,
541 struct page *startpage,
542 struct buffer_head *bh,
543 struct buffer_head *head)
544{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100545 size_t len, total = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 pgoff_t tindex, tlast, tloff;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100547 unsigned int pg_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct address_space *mapping = inode->i_mapping;
549
550 /* First sum forwards in this page */
551 do {
552 if (buffer_mapped(bh))
553 break;
554 total += bh->b_size;
555 } while ((bh = bh->b_this_page) != head);
556
557 /* If we reached the end of the page, sum forwards in
558 * following pages.
559 */
560 if (bh == head) {
561 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
562 /* Prune this back to avoid pathological behavior */
563 tloff = min(tlast, startpage->index + 64);
564 for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
565 len = xfs_probe_unmapped_page(mapping, tindex,
566 PAGE_CACHE_SIZE);
567 if (!len)
568 return total;
569 total += len;
570 }
571 if (tindex == tlast &&
572 (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
573 total += xfs_probe_unmapped_page(mapping,
574 tindex, pg_offset);
575 }
576 }
577 return total;
578}
579
580/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100581 * Probe for a given page (index) in the inode and test if it is suitable
582 * for writing as part of an unwritten or delayed allocate extent.
583 * Returns page locked and with an extra reference count if so, else NULL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 */
585STATIC struct page *
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100586xfs_probe_delayed_page(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 struct inode *inode,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100588 pgoff_t index,
589 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
591 struct page *page;
592
593 page = find_trylock_page(inode->i_mapping, index);
594 if (!page)
595 return NULL;
596 if (PageWriteback(page))
597 goto out;
598
599 if (page->mapping && page_has_buffers(page)) {
600 struct buffer_head *bh, *head;
601 int acceptable = 0;
602
603 bh = head = page_buffers(page);
604 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100605 if (buffer_unwritten(bh))
606 acceptable = (type == IOMAP_UNWRITTEN);
607 else if (buffer_delay(bh))
608 acceptable = (type == IOMAP_DELAY);
609 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 } while ((bh = bh->b_this_page) != head);
612
613 if (acceptable)
614 return page;
615 }
616
617out:
618 unlock_page(page);
619 return NULL;
620}
621
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622/*
623 * Allocate & map buffers for page given the extent map. Write it out.
624 * except for the original page of a writepage, this is called on
625 * delalloc/unwritten pages only, for the original page it is possible
626 * that the page has no mapping at all.
627 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100628STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629xfs_convert_page(
630 struct inode *inode,
631 struct page *page,
632 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100633 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 struct writeback_control *wbc,
635 void *private,
636 int startio,
637 int all_bh)
638{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100639 struct buffer_head *bh, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 xfs_iomap_t *mp = iomapp, *tmp;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100641 unsigned long p_offset, end_offset;
642 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 int bbits = inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700644 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100645 int count = 0, done = 0, uptodate = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Nathan Scott24e17b52005-05-05 13:33:20 -0700647 end_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1));
648
649 /*
650 * page_dirty is initially a count of buffers on the page before
651 * EOF and is decrememted as we move each into a cleanable state.
652 */
653 len = 1 << inode->i_blkbits;
654 end_offset = max(end_offset, PAGE_CACHE_SIZE);
655 end_offset = roundup(end_offset, len);
656 page_dirty = end_offset / len;
657
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100658 p_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 bh = head = page_buffers(page);
660 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100661 if (p_offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100663 if (!buffer_uptodate(bh))
664 uptodate = 0;
665 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
666 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100668 }
669
670 if (buffer_unwritten(bh))
671 type = IOMAP_UNWRITTEN;
672 else if (buffer_delay(bh))
673 type = IOMAP_DELAY;
674 else {
675 type = 0;
676 if (!(buffer_mapped(bh) && all_bh && startio)) {
677 done = 1;
678 } else if (startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 lock_buffer(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100680 xfs_add_to_ioend(inode, bh, p_offset,
681 type, ioendp, done);
682 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700683 page_dirty--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 }
685 continue;
686 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100687 tmp = xfs_offset_to_map(page, mp, p_offset);
688 if (!tmp) {
689 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
693 ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
694
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100695 xfs_map_at_offset(page, bh, p_offset, bbits, tmp, *ioendp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 if (startio) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100697 xfs_add_to_ioend(inode, bh, p_offset,
698 type, ioendp, done);
699 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 } else {
701 set_buffer_dirty(bh);
702 unlock_buffer(bh);
703 mark_buffer_dirty(bh);
704 }
Nathan Scott24e17b52005-05-05 13:33:20 -0700705 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100706 } while (p_offset += len, (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100708 if (uptodate && bh == head)
709 SetPageUptodate(page);
710
711 if (startio) {
712 if (count)
713 wbc->nr_to_write--;
714 xfs_start_page_writeback(page, wbc, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100716
717 return done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718}
719
720/*
721 * Convert & write out a cluster of pages in the same extent as defined
722 * by mp and following the start page.
723 */
724STATIC void
725xfs_cluster_write(
726 struct inode *inode,
727 pgoff_t tindex,
728 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100729 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 struct writeback_control *wbc,
731 int startio,
732 int all_bh,
733 pgoff_t tlast)
734{
735 struct page *page;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100736 unsigned int type = (*ioendp)->io_type;
737 int done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100739 for (done = 0; tindex <= tlast && !done; tindex++) {
740 page = xfs_probe_delayed_page(inode, tindex, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!page)
742 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100743 done = xfs_convert_page(inode, page, iomapp, ioendp,
744 wbc, NULL, startio, all_bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 }
746}
747
748/*
749 * Calling this without startio set means we are being asked to make a dirty
750 * page ready for freeing it's buffers. When called with startio set then
751 * we are coming from writepage.
752 *
753 * When called with startio set it is important that we write the WHOLE
754 * page if possible.
755 * The bh->b_state's cannot know if any of the blocks or which block for
756 * that matter are dirty due to mmap writes, and therefore bh uptodate is
757 * only vaild if the page itself isn't completely uptodate. Some layers
758 * may clear the page dirty flag prior to calling write page, under the
759 * assumption the entire page will be written out; by not writing out the
760 * whole page the page can be reused before all valid dirty data is
761 * written out. Note: in the case of a page that has been dirty'd by
762 * mapwrite and but partially setup by block_prepare_write the
763 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
764 * valid state, thus the whole page must be written out thing.
765 */
766
767STATIC int
768xfs_page_state_convert(
769 struct inode *inode,
770 struct page *page,
771 struct writeback_control *wbc,
772 int startio,
773 int unmapped) /* also implies page uptodate */
774{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100775 struct buffer_head *bh, *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 xfs_iomap_t *iomp, iomap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100777 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 loff_t offset;
779 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100780 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 __uint64_t end_offset;
782 pgoff_t end_index, last_index, tlast;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100783 int flags, len, err, done = 1;
784 int uptodate = 1;
785 int page_dirty, count = 0, trylock_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Daniel Moore3ba08152005-05-05 13:31:34 -0700787 /* wait for other IO threads? */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100788 if (startio && wbc->sync_mode != WB_SYNC_NONE)
789 trylock_flag |= BMAPI_TRYLOCK;
Daniel Moore3ba08152005-05-05 13:31:34 -0700790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 /* Is this page beyond the end of the file? */
792 offset = i_size_read(inode);
793 end_index = offset >> PAGE_CACHE_SHIFT;
794 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
795 if (page->index >= end_index) {
796 if ((page->index >= end_index + 1) ||
797 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100798 if (startio)
799 unlock_page(page);
800 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 /*
Nathan Scott24e17b52005-05-05 13:33:20 -0700805 * page_dirty is initially a count of buffers on the page before
806 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100807 *
808 * Derivation:
809 *
810 * End offset is the highest offset that this page should represent.
811 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
812 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
813 * hence give us the correct page_dirty count. On any other page,
814 * it will be zero and in that case we need page_dirty to be the
815 * count of buffers on the page.
816 */
817 end_offset = min_t(unsigned long long,
818 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700819 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100820 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
821 PAGE_CACHE_SIZE);
822 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -0700823 page_dirty = p_offset / len;
824
825 iomp = NULL;
Nathan Scott24e17b52005-05-05 13:33:20 -0700826 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100827 offset = page_offset(page);
828
829 /* TODO: fix up "done" variable and iomap pointer (boolean) */
830 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 do {
833 if (offset >= end_offset)
834 break;
835 if (!buffer_uptodate(bh))
836 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100837 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
838 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 if (iomp) {
843 iomp = xfs_offset_to_map(page, &iomap, p_offset);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100844 done = (iomp == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846
847 /*
848 * First case, map an unwritten extent and prepare for
849 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100850 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 * Second case, allocate space for a delalloc buffer.
852 * We can return EAGAIN here in the release page case.
853 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100854 if (buffer_unwritten(bh) || buffer_delay(bh)) {
855 if (buffer_unwritten(bh)) {
856 type = IOMAP_UNWRITTEN;
857 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
858 } else {
859 type = IOMAP_DELAY;
860 flags = BMAPI_ALLOCATE;
861 if (!startio)
862 flags |= trylock_flag;
863 }
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 if (!iomp) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100866 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 err = xfs_map_blocks(inode, offset, len, &iomap,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100868 flags);
869 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 iomp = xfs_offset_to_map(page, &iomap,
872 p_offset);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100873 done = (iomp == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875 if (iomp) {
876 xfs_map_at_offset(page, bh, p_offset,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100877 inode->i_blkbits, iomp, ioend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (startio) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100879 xfs_add_to_ioend(inode, bh, p_offset,
880 type, &ioend, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 } else {
882 set_buffer_dirty(bh);
883 unlock_buffer(bh);
884 mark_buffer_dirty(bh);
885 }
886 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100887 count++;
888 } else {
889 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 }
891 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
892 (unmapped || startio)) {
893
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100894 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!buffer_mapped(bh)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
897 /*
898 * Getting here implies an unmapped buffer
899 * was found, and we are in a path where we
900 * need to write the whole page out.
901 */
902 if (!iomp) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100903 int size;
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 size = xfs_probe_unmapped_cluster(
906 inode, page, bh, head);
907 err = xfs_map_blocks(inode, offset,
908 size, &iomap,
909 BMAPI_WRITE|BMAPI_MMAP);
910 if (err) {
911 goto error;
912 }
913 iomp = xfs_offset_to_map(page, &iomap,
914 p_offset);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100915 done = (iomp == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 }
917 if (iomp) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100918 xfs_map_at_offset(page, bh, p_offset,
919 inode->i_blkbits, iomp,
920 ioend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (startio) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100922 xfs_add_to_ioend(inode,
923 bh, p_offset, type,
924 &ioend, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 } else {
926 set_buffer_dirty(bh);
927 unlock_buffer(bh);
928 mark_buffer_dirty(bh);
929 }
930 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100931 count++;
932 } else {
933 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 }
935 } else if (startio) {
936 if (buffer_uptodate(bh) &&
937 !test_and_set_bit(BH_Lock, &bh->b_state)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100938 ASSERT(buffer_mapped(bh));
939 xfs_add_to_ioend(inode,
940 bh, p_offset, type,
941 &ioend, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100943 count++;
944 } else {
945 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100947 } else {
948 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100951
952 if (!iohead)
953 iohead = ioend;
954
955 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
957 if (uptodate && bh == head)
958 SetPageUptodate(page);
959
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100960 if (startio)
961 xfs_start_page_writeback(page, wbc, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100963 if (ioend && iomp && !done) {
Nathan Scott775bf6c2005-05-05 13:33:01 -0700964 offset = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -0700966 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100967 xfs_cluster_write(inode, page->index + 1, iomp, &ioend,
968 wbc, startio, unmapped, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100971 if (iohead)
972 xfs_submit_ioend(iohead);
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return page_dirty;
975
976error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100977 if (iohead)
978 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 /*
981 * If it's delalloc and we have nowhere to put it,
982 * throw it away, unless the lower layers told
983 * us to try again.
984 */
985 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100986 if (!unmapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 block_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 ClearPageUptodate(page);
989 }
990 return err;
991}
992
993STATIC int
994__linvfs_get_block(
995 struct inode *inode,
996 sector_t iblock,
997 unsigned long blocks,
998 struct buffer_head *bh_result,
999 int create,
1000 int direct,
1001 bmapi_flags_t flags)
1002{
1003 vnode_t *vp = LINVFS_GET_VP(inode);
1004 xfs_iomap_t iomap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001005 xfs_off_t offset;
1006 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 int retpbbm = 1;
1008 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001010 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scotta4656392005-11-25 16:41:57 +11001011 if (blocks)
1012 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1013 (xfs_off_t)blocks << inode->i_blkbits);
1014 else
1015 size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
1017 VOP_BMAP(vp, offset, size,
1018 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1019 if (error)
1020 return -error;
1021
1022 if (retpbbm == 0)
1023 return 0;
1024
1025 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001026 xfs_daddr_t bn;
1027 xfs_off_t delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029 /* For unwritten extents do not report a disk address on
1030 * the read case (treat as if we're reading into a hole).
1031 */
1032 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1033 delta = offset - iomap.iomap_offset;
1034 delta >>= inode->i_blkbits;
1035
1036 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1037 bn += delta;
1038 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1039 bh_result->b_blocknr = bn;
1040 set_buffer_mapped(bh_result);
1041 }
1042 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1043 if (direct)
1044 bh_result->b_private = inode;
1045 set_buffer_unwritten(bh_result);
1046 set_buffer_delay(bh_result);
1047 }
1048 }
1049
1050 /* If this is a realtime file, data might be on a new device */
Nathan Scottce8e9222006-01-11 15:39:08 +11001051 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 /* If we previously allocated a block out beyond eof and
1054 * we are now coming back to use it then we will need to
1055 * flag it as new even if it has a disk address.
1056 */
1057 if (create &&
1058 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001059 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 if (iomap.iomap_flags & IOMAP_DELAY) {
1063 BUG_ON(direct);
1064 if (create) {
1065 set_buffer_uptodate(bh_result);
1066 set_buffer_mapped(bh_result);
1067 set_buffer_delay(bh_result);
1068 }
1069 }
1070
1071 if (blocks) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001072 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1073 offset = min_t(xfs_off_t,
1074 iomap.iomap_bsize - iomap.iomap_delta,
Nathan Scotta4656392005-11-25 16:41:57 +11001075 (xfs_off_t)blocks << inode->i_blkbits);
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001076 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
1079 return 0;
1080}
1081
1082int
1083linvfs_get_block(
1084 struct inode *inode,
1085 sector_t iblock,
1086 struct buffer_head *bh_result,
1087 int create)
1088{
1089 return __linvfs_get_block(inode, iblock, 0, bh_result,
1090 create, 0, BMAPI_WRITE);
1091}
1092
1093STATIC int
1094linvfs_get_blocks_direct(
1095 struct inode *inode,
1096 sector_t iblock,
1097 unsigned long max_blocks,
1098 struct buffer_head *bh_result,
1099 int create)
1100{
1101 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1102 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1103}
1104
Christoph Hellwigf0973862005-09-05 08:22:52 +10001105STATIC void
1106linvfs_end_io_direct(
1107 struct kiocb *iocb,
1108 loff_t offset,
1109 ssize_t size,
1110 void *private)
1111{
1112 xfs_ioend_t *ioend = iocb->private;
1113
1114 /*
1115 * Non-NULL private data means we need to issue a transaction to
1116 * convert a range from unwritten to written extents. This needs
1117 * to happen from process contect but aio+dio I/O completion
1118 * happens from irq context so we need to defer it to a workqueue.
1119 * This is not nessecary for synchronous direct I/O, but we do
1120 * it anyway to keep the code uniform and simpler.
1121 *
1122 * The core direct I/O code might be changed to always call the
1123 * completion handler in the future, in which case all this can
1124 * go away.
1125 */
1126 if (private && size > 0) {
1127 ioend->io_offset = offset;
1128 ioend->io_size = size;
1129 xfs_finish_ioend(ioend);
1130 } else {
1131 ASSERT(size >= 0);
1132 xfs_destroy_ioend(ioend);
1133 }
1134
1135 /*
1136 * blockdev_direct_IO can return an error even afer the I/O
1137 * completion handler was called. Thus we need to protect
1138 * against double-freeing.
1139 */
1140 iocb->private = NULL;
1141}
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143STATIC ssize_t
1144linvfs_direct_IO(
1145 int rw,
1146 struct kiocb *iocb,
1147 const struct iovec *iov,
1148 loff_t offset,
1149 unsigned long nr_segs)
1150{
1151 struct file *file = iocb->ki_filp;
1152 struct inode *inode = file->f_mapping->host;
1153 vnode_t *vp = LINVFS_GET_VP(inode);
1154 xfs_iomap_t iomap;
1155 int maps = 1;
1156 int error;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001157 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1160 if (error)
1161 return -error;
1162
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001163 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001164
1165 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
Nathan Scottce8e9222006-01-11 15:39:08 +11001166 iomap.iomap_target->bt_bdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 iov, offset, nr_segs,
1168 linvfs_get_blocks_direct,
Christoph Hellwigf0973862005-09-05 08:22:52 +10001169 linvfs_end_io_direct);
1170
1171 if (unlikely(ret <= 0 && iocb->private))
1172 xfs_destroy_ioend(iocb->private);
1173 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
1176
1177STATIC sector_t
1178linvfs_bmap(
1179 struct address_space *mapping,
1180 sector_t block)
1181{
1182 struct inode *inode = (struct inode *)mapping->host;
1183 vnode_t *vp = LINVFS_GET_VP(inode);
1184 int error;
1185
1186 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1187
1188 VOP_RWLOCK(vp, VRWLOCK_READ);
1189 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1190 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1191 return generic_block_bmap(mapping, block, linvfs_get_block);
1192}
1193
1194STATIC int
1195linvfs_readpage(
1196 struct file *unused,
1197 struct page *page)
1198{
1199 return mpage_readpage(page, linvfs_get_block);
1200}
1201
1202STATIC int
1203linvfs_readpages(
1204 struct file *unused,
1205 struct address_space *mapping,
1206 struct list_head *pages,
1207 unsigned nr_pages)
1208{
1209 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1210}
1211
1212STATIC void
1213xfs_count_page_state(
1214 struct page *page,
1215 int *delalloc,
1216 int *unmapped,
1217 int *unwritten)
1218{
1219 struct buffer_head *bh, *head;
1220
1221 *delalloc = *unmapped = *unwritten = 0;
1222
1223 bh = head = page_buffers(page);
1224 do {
1225 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1226 (*unmapped) = 1;
1227 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1228 clear_buffer_unwritten(bh);
1229 else if (buffer_unwritten(bh))
1230 (*unwritten) = 1;
1231 else if (buffer_delay(bh))
1232 (*delalloc) = 1;
1233 } while ((bh = bh->b_this_page) != head);
1234}
1235
1236
1237/*
1238 * writepage: Called from one of two places:
1239 *
1240 * 1. we are flushing a delalloc buffer head.
1241 *
1242 * 2. we are writing out a dirty page. Typically the page dirty
1243 * state is cleared before we get here. In this case is it
1244 * conceivable we have no buffer heads.
1245 *
1246 * For delalloc space on the page we need to allocate space and
1247 * flush it. For unmapped buffer heads on the page we should
1248 * allocate space if the page is uptodate. For any other dirty
1249 * buffer heads on the page we should flush them.
1250 *
1251 * If we detect that a transaction would be required to flush
1252 * the page, we have to check the process flags first, if we
1253 * are already in a transaction or disk I/O during allocations
1254 * is off, we need to fail the writepage and redirty the page.
1255 */
1256
1257STATIC int
1258linvfs_writepage(
1259 struct page *page,
1260 struct writeback_control *wbc)
1261{
1262 int error;
1263 int need_trans;
1264 int delalloc, unmapped, unwritten;
1265 struct inode *inode = page->mapping->host;
1266
1267 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1268
1269 /*
1270 * We need a transaction if:
1271 * 1. There are delalloc buffers on the page
1272 * 2. The page is uptodate and we have unmapped buffers
1273 * 3. The page is uptodate and we have no buffers
1274 * 4. There are unwritten buffers on the page
1275 */
1276
1277 if (!page_has_buffers(page)) {
1278 unmapped = 1;
1279 need_trans = 1;
1280 } else {
1281 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1282 if (!PageUptodate(page))
1283 unmapped = 0;
1284 need_trans = delalloc + unmapped + unwritten;
1285 }
1286
1287 /*
1288 * If we need a transaction and the process flags say
1289 * we are already in a transaction, or no IO is allowed
1290 * then mark the page dirty again and leave the page
1291 * as is.
1292 */
1293 if (PFLAGS_TEST_FSTRANS() && need_trans)
1294 goto out_fail;
1295
1296 /*
1297 * Delay hooking up buffer heads until we have
1298 * made our go/no-go decision.
1299 */
1300 if (!page_has_buffers(page))
1301 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1302
1303 /*
1304 * Convert delayed allocate, unwritten or unmapped space
1305 * to real space and flush out to disk.
1306 */
1307 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1308 if (error == -EAGAIN)
1309 goto out_fail;
1310 if (unlikely(error < 0))
1311 goto out_unlock;
1312
1313 return 0;
1314
1315out_fail:
1316 redirty_page_for_writepage(wbc, page);
1317 unlock_page(page);
1318 return 0;
1319out_unlock:
1320 unlock_page(page);
1321 return error;
1322}
1323
Nathan Scottbcec2b72005-09-02 16:40:17 +10001324STATIC int
1325linvfs_invalidate_page(
1326 struct page *page,
1327 unsigned long offset)
1328{
1329 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1330 page->mapping->host, page, offset);
1331 return block_invalidatepage(page, offset);
1332}
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334/*
1335 * Called to move a page into cleanable state - and from there
1336 * to be released. Possibly the page is already clean. We always
1337 * have buffer heads in this call.
1338 *
1339 * Returns 0 if the page is ok to release, 1 otherwise.
1340 *
1341 * Possible scenarios are:
1342 *
1343 * 1. We are being called to release a page which has been written
1344 * to via regular I/O. buffer heads will be dirty and possibly
1345 * delalloc. If no delalloc buffer heads in this case then we
1346 * can just return zero.
1347 *
1348 * 2. We are called to release a page which has been written via
1349 * mmap, all we need to do is ensure there is no delalloc
1350 * state in the buffer heads, if not we can let the caller
1351 * free them and we should come back later via writepage.
1352 */
1353STATIC int
1354linvfs_release_page(
1355 struct page *page,
Al Viro27496a82005-10-21 03:20:48 -04001356 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
1358 struct inode *inode = page->mapping->host;
1359 int dirty, delalloc, unmapped, unwritten;
1360 struct writeback_control wbc = {
1361 .sync_mode = WB_SYNC_ALL,
1362 .nr_to_write = 1,
1363 };
1364
1365 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1366
1367 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1368 if (!delalloc && !unwritten)
1369 goto free_buffers;
1370
1371 if (!(gfp_mask & __GFP_FS))
1372 return 0;
1373
1374 /* If we are already inside a transaction or the thread cannot
1375 * do I/O, we cannot release this page.
1376 */
1377 if (PFLAGS_TEST_FSTRANS())
1378 return 0;
1379
1380 /*
1381 * Convert delalloc space to real space, do not flush the
1382 * data out to disk, that will be done by the caller.
1383 * Never need to allocate space here - we will always
1384 * come back to writepage in that case.
1385 */
1386 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1387 if (dirty == 0 && !unwritten)
1388 goto free_buffers;
1389 return 0;
1390
1391free_buffers:
1392 return try_to_free_buffers(page);
1393}
1394
1395STATIC int
1396linvfs_prepare_write(
1397 struct file *file,
1398 struct page *page,
1399 unsigned int from,
1400 unsigned int to)
1401{
1402 return block_prepare_write(page, from, to, linvfs_get_block);
1403}
1404
1405struct address_space_operations linvfs_aops = {
1406 .readpage = linvfs_readpage,
1407 .readpages = linvfs_readpages,
1408 .writepage = linvfs_writepage,
1409 .sync_page = block_sync_page,
1410 .releasepage = linvfs_release_page,
Nathan Scottbcec2b72005-09-02 16:40:17 +10001411 .invalidatepage = linvfs_invalidate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 .prepare_write = linvfs_prepare_write,
1413 .commit_write = generic_commit_write,
1414 .bmap = linvfs_bmap,
1415 .direct_IO = linvfs_direct_IO,
1416};