]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/linux-2.6/xfs_aops.c
[XFS] use pagevec lookups This reduces the time spend in the radix tree
[net-next-2.6.git] / fs / xfs / linux-2.6 / xfs_aops.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * 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.
1da177e4 13 *
7b718769
NS
14 * 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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_bit.h"
1da177e4 20#include "xfs_log.h"
a844f451 21#include "xfs_inum.h"
1da177e4 22#include "xfs_sb.h"
a844f451 23#include "xfs_ag.h"
1da177e4
LT
24#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"
1da177e4
LT
32#include "xfs_dir_sf.h"
33#include "xfs_dir2_sf.h"
a844f451 34#include "xfs_attr_sf.h"
1da177e4
LT
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_alloc.h"
38#include "xfs_btree.h"
1da177e4
LT
39#include "xfs_error.h"
40#include "xfs_rw.h"
41#include "xfs_iomap.h"
42#include <linux/mpage.h>
10ce4444 43#include <linux/pagevec.h>
1da177e4
LT
44#include <linux/writeback.h>
45
46STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
1da177e4
LT
47
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;
57 bhv_desc_t *bdp;
58 vnode_t *vp = LINVFS_GET_VP(inode);
59 loff_t isize = i_size_read(inode);
f6d6d4fc 60 loff_t offset = page_offset(page);
1da177e4
LT
61 int delalloc = -1, unmapped = -1, unwritten = -1;
62
63 if (page_has_buffers(page))
64 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
65
66 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
67 ip = XFS_BHVTOI(bdp);
68 if (!ip->i_rwtrace)
69 return;
70
71 ktrace_enter(ip->i_rwtrace,
72 (void *)((unsigned long)tag),
73 (void *)ip,
74 (void *)inode,
75 (void *)page,
76 (void *)((unsigned long)mask),
77 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
79 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
80 (void *)((unsigned long)(isize & 0xffffffff)),
81 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
82 (void *)((unsigned long)(offset & 0xffffffff)),
83 (void *)((unsigned long)delalloc),
84 (void *)((unsigned long)unmapped),
85 (void *)((unsigned long)unwritten),
86 (void *)NULL,
87 (void *)NULL);
88}
89#else
90#define xfs_page_trace(tag, inode, page, mask)
91#endif
92
0829c360
CH
93/*
94 * Schedule IO completion handling on a xfsdatad if this was
95 * the final hold on this ioend.
96 */
97STATIC void
98xfs_finish_ioend(
99 xfs_ioend_t *ioend)
100{
101 if (atomic_dec_and_test(&ioend->io_remaining))
102 queue_work(xfsdatad_workqueue, &ioend->io_work);
103}
104
f6d6d4fc
CH
105/*
106 * We're now finished for good with this ioend structure.
107 * Update the page state via the associated buffer_heads,
108 * release holds on the inode and bio, and finally free
109 * up memory. Do not use the ioend after this.
110 */
0829c360
CH
111STATIC void
112xfs_destroy_ioend(
113 xfs_ioend_t *ioend)
114{
f6d6d4fc
CH
115 struct buffer_head *bh, *next;
116
117 for (bh = ioend->io_buffer_head; bh; bh = next) {
118 next = bh->b_private;
119 bh->b_end_io(bh, ioend->io_uptodate);
120 }
121
0829c360
CH
122 vn_iowake(ioend->io_vnode);
123 mempool_free(ioend, xfs_ioend_pool);
124}
125
126/*
f6d6d4fc
CH
127 * Buffered IO write completion for delayed allocate extents.
128 * TODO: Update ondisk isize now that we know the file data
129 * has been flushed (i.e. the notorious "NULL file" problem).
130 */
131STATIC void
132xfs_end_bio_delalloc(
133 void *data)
134{
135 xfs_ioend_t *ioend = data;
136
137 xfs_destroy_ioend(ioend);
138}
139
140/*
141 * Buffered IO write completion for regular, written extents.
142 */
143STATIC void
144xfs_end_bio_written(
145 void *data)
146{
147 xfs_ioend_t *ioend = data;
148
149 xfs_destroy_ioend(ioend);
150}
151
152/*
153 * IO write completion for unwritten extents.
154 *
0829c360 155 * Issue transactions to convert a buffer range from unwritten
f0973863 156 * to written extents.
0829c360
CH
157 */
158STATIC void
159xfs_end_bio_unwritten(
160 void *data)
161{
162 xfs_ioend_t *ioend = data;
163 vnode_t *vp = ioend->io_vnode;
164 xfs_off_t offset = ioend->io_offset;
165 size_t size = ioend->io_size;
166 int error;
167
168 if (ioend->io_uptodate)
169 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
170 xfs_destroy_ioend(ioend);
171}
172
173/*
174 * Allocate and initialise an IO completion structure.
175 * We need to track unwritten extent write completion here initially.
176 * We'll need to extend this for updating the ondisk inode size later
177 * (vs. incore size).
178 */
179STATIC xfs_ioend_t *
180xfs_alloc_ioend(
f6d6d4fc
CH
181 struct inode *inode,
182 unsigned int type)
0829c360
CH
183{
184 xfs_ioend_t *ioend;
185
186 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
187
188 /*
189 * Set the count to 1 initially, which will prevent an I/O
190 * completion callback from happening before we have started
191 * all the I/O from calling the completion routine too early.
192 */
193 atomic_set(&ioend->io_remaining, 1);
194 ioend->io_uptodate = 1; /* cleared if any I/O fails */
f6d6d4fc
CH
195 ioend->io_list = NULL;
196 ioend->io_type = type;
0829c360 197 ioend->io_vnode = LINVFS_GET_VP(inode);
c1a073bd 198 ioend->io_buffer_head = NULL;
f6d6d4fc 199 ioend->io_buffer_tail = NULL;
0829c360
CH
200 atomic_inc(&ioend->io_vnode->v_iocount);
201 ioend->io_offset = 0;
202 ioend->io_size = 0;
203
f6d6d4fc
CH
204 if (type == IOMAP_UNWRITTEN)
205 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
206 else if (type == IOMAP_DELAY)
207 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
208 else
209 INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
0829c360
CH
210
211 return ioend;
212}
213
1da177e4
LT
214STATIC int
215xfs_map_blocks(
216 struct inode *inode,
217 loff_t offset,
218 ssize_t count,
219 xfs_iomap_t *mapp,
220 int flags)
221{
222 vnode_t *vp = LINVFS_GET_VP(inode);
223 int error, nmaps = 1;
224
225 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
226 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
227 VMODIFY(vp);
228 return -error;
229}
230
231/*
232 * Finds the corresponding mapping in block @map array of the
233 * given @offset within a @page.
234 */
235STATIC xfs_iomap_t *
236xfs_offset_to_map(
237 struct page *page,
238 xfs_iomap_t *iomapp,
239 unsigned long offset)
240{
f6d6d4fc 241 xfs_off_t full_offset; /* offset from start of file */
1da177e4
LT
242
243 ASSERT(offset < PAGE_CACHE_SIZE);
244
245 full_offset = page->index; /* NB: using 64bit number */
246 full_offset <<= PAGE_CACHE_SHIFT; /* offset from file start */
247 full_offset += offset; /* offset from page start */
248
249 if (full_offset < iomapp->iomap_offset)
250 return NULL;
251 if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
252 return iomapp;
253 return NULL;
254}
255
f6d6d4fc
CH
256/*
257 * BIO completion handler for buffered IO.
258 */
259STATIC int
260xfs_end_bio(
261 struct bio *bio,
262 unsigned int bytes_done,
263 int error)
264{
265 xfs_ioend_t *ioend = bio->bi_private;
266
267 if (bio->bi_size)
268 return 1;
269
270 ASSERT(ioend);
271 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
272
273 /* Toss bio and pass work off to an xfsdatad thread */
274 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
275 ioend->io_uptodate = 0;
276 bio->bi_private = NULL;
277 bio->bi_end_io = NULL;
278
279 bio_put(bio);
280 xfs_finish_ioend(ioend);
281 return 0;
282}
283
284STATIC void
285xfs_submit_ioend_bio(
286 xfs_ioend_t *ioend,
287 struct bio *bio)
288{
289 atomic_inc(&ioend->io_remaining);
290
291 bio->bi_private = ioend;
292 bio->bi_end_io = xfs_end_bio;
293
294 submit_bio(WRITE, bio);
295 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
296 bio_put(bio);
297}
298
299STATIC struct bio *
300xfs_alloc_ioend_bio(
301 struct buffer_head *bh)
302{
303 struct bio *bio;
304 int nvecs = bio_get_nr_vecs(bh->b_bdev);
305
306 do {
307 bio = bio_alloc(GFP_NOIO, nvecs);
308 nvecs >>= 1;
309 } while (!bio);
310
311 ASSERT(bio->bi_private == NULL);
312 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
313 bio->bi_bdev = bh->b_bdev;
314 bio_get(bio);
315 return bio;
316}
317
318STATIC void
319xfs_start_buffer_writeback(
320 struct buffer_head *bh)
321{
322 ASSERT(buffer_mapped(bh));
323 ASSERT(buffer_locked(bh));
324 ASSERT(!buffer_delay(bh));
325 ASSERT(!buffer_unwritten(bh));
326
327 mark_buffer_async_write(bh);
328 set_buffer_uptodate(bh);
329 clear_buffer_dirty(bh);
330}
331
332STATIC void
333xfs_start_page_writeback(
334 struct page *page,
335 struct writeback_control *wbc,
336 int clear_dirty,
337 int buffers)
338{
339 ASSERT(PageLocked(page));
340 ASSERT(!PageWriteback(page));
341 set_page_writeback(page);
342 if (clear_dirty)
343 clear_page_dirty(page);
344 unlock_page(page);
345 if (!buffers) {
346 end_page_writeback(page);
347 wbc->pages_skipped++; /* We didn't write this page */
348 }
349}
350
351static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
352{
353 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
354}
355
356/*
357 * Submit all of the bios for all of the ioends we have saved up,
358 * covering the initial writepage page and also any probed pages.
359 */
360STATIC void
361xfs_submit_ioend(
362 xfs_ioend_t *ioend)
363{
364 xfs_ioend_t *next;
365 struct buffer_head *bh;
366 struct bio *bio;
367 sector_t lastblock = 0;
368
369 do {
370 next = ioend->io_list;
371 bio = NULL;
372
373 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
374 xfs_start_buffer_writeback(bh);
375
376 if (!bio) {
377 retry:
378 bio = xfs_alloc_ioend_bio(bh);
379 } else if (bh->b_blocknr != lastblock + 1) {
380 xfs_submit_ioend_bio(ioend, bio);
381 goto retry;
382 }
383
384 if (bio_add_buffer(bio, bh) != bh->b_size) {
385 xfs_submit_ioend_bio(ioend, bio);
386 goto retry;
387 }
388
389 lastblock = bh->b_blocknr;
390 }
391 if (bio)
392 xfs_submit_ioend_bio(ioend, bio);
393 xfs_finish_ioend(ioend);
394 } while ((ioend = next) != NULL);
395}
396
397/*
398 * Cancel submission of all buffer_heads so far in this endio.
399 * Toss the endio too. Only ever called for the initial page
400 * in a writepage request, so only ever one page.
401 */
402STATIC void
403xfs_cancel_ioend(
404 xfs_ioend_t *ioend)
405{
406 xfs_ioend_t *next;
407 struct buffer_head *bh, *next_bh;
408
409 do {
410 next = ioend->io_list;
411 bh = ioend->io_buffer_head;
412 do {
413 next_bh = bh->b_private;
414 clear_buffer_async_write(bh);
415 unlock_buffer(bh);
416 } while ((bh = next_bh) != NULL);
417
418 vn_iowake(ioend->io_vnode);
419 mempool_free(ioend, xfs_ioend_pool);
420 } while ((ioend = next) != NULL);
421}
422
423/*
424 * Test to see if we've been building up a completion structure for
425 * earlier buffers -- if so, we try to append to this ioend if we
426 * can, otherwise we finish off any current ioend and start another.
427 * Return true if we've finished the given ioend.
428 */
429STATIC void
430xfs_add_to_ioend(
431 struct inode *inode,
432 struct buffer_head *bh,
433 unsigned int p_offset,
434 unsigned int type,
435 xfs_ioend_t **result,
436 int need_ioend)
437{
438 xfs_ioend_t *ioend = *result;
439
440 if (!ioend || need_ioend || type != ioend->io_type) {
441 xfs_ioend_t *previous = *result;
442 xfs_off_t offset;
443
444 offset = (xfs_off_t)bh->b_page->index << PAGE_CACHE_SHIFT;
445 offset += p_offset;
446 ioend = xfs_alloc_ioend(inode, type);
447 ioend->io_offset = offset;
448 ioend->io_buffer_head = bh;
449 ioend->io_buffer_tail = bh;
450 if (previous)
451 previous->io_list = ioend;
452 *result = ioend;
453 } else {
454 ioend->io_buffer_tail->b_private = bh;
455 ioend->io_buffer_tail = bh;
456 }
457
458 bh->b_private = NULL;
459 ioend->io_size += bh->b_size;
460}
461
1da177e4
LT
462STATIC void
463xfs_map_at_offset(
464 struct page *page,
465 struct buffer_head *bh,
466 unsigned long offset,
467 int block_bits,
f6d6d4fc
CH
468 xfs_iomap_t *iomapp,
469 xfs_ioend_t *ioend)
1da177e4
LT
470{
471 xfs_daddr_t bn;
f6d6d4fc 472 xfs_off_t delta;
1da177e4
LT
473 int sector_shift;
474
475 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
476 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
477 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
478
479 delta = page->index;
480 delta <<= PAGE_CACHE_SHIFT;
481 delta += offset;
482 delta -= iomapp->iomap_offset;
483 delta >>= block_bits;
484
485 sector_shift = block_bits - BBSHIFT;
486 bn = iomapp->iomap_bn >> sector_shift;
487 bn += delta;
488 BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
489 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
490
491 lock_buffer(bh);
492 bh->b_blocknr = bn;
ce8e922c 493 bh->b_bdev = iomapp->iomap_target->bt_bdev;
1da177e4
LT
494 set_buffer_mapped(bh);
495 clear_buffer_delay(bh);
f6d6d4fc 496 clear_buffer_unwritten(bh);
1da177e4
LT
497}
498
499/*
500 * Look for a page at index which is unlocked and not mapped
501 * yet - clustering for mmap write case.
502 */
503STATIC unsigned int
504xfs_probe_unmapped_page(
10ce4444 505 struct page *page,
1da177e4
LT
506 unsigned int pg_offset)
507{
1da177e4
LT
508 int ret = 0;
509
1da177e4 510 if (PageWriteback(page))
10ce4444 511 return 0;
1da177e4
LT
512
513 if (page->mapping && PageDirty(page)) {
514 if (page_has_buffers(page)) {
515 struct buffer_head *bh, *head;
516
517 bh = head = page_buffers(page);
518 do {
519 if (buffer_mapped(bh) || !buffer_uptodate(bh))
520 break;
521 ret += bh->b_size;
522 if (ret >= pg_offset)
523 break;
524 } while ((bh = bh->b_this_page) != head);
525 } else
526 ret = PAGE_CACHE_SIZE;
527 }
528
1da177e4
LT
529 return ret;
530}
531
f6d6d4fc 532STATIC size_t
1da177e4
LT
533xfs_probe_unmapped_cluster(
534 struct inode *inode,
535 struct page *startpage,
536 struct buffer_head *bh,
537 struct buffer_head *head)
538{
10ce4444 539 struct pagevec pvec;
1da177e4 540 pgoff_t tindex, tlast, tloff;
10ce4444
CH
541 size_t total = 0;
542 int done = 0, i;
1da177e4
LT
543
544 /* First sum forwards in this page */
545 do {
546 if (buffer_mapped(bh))
10ce4444 547 return total;
1da177e4
LT
548 total += bh->b_size;
549 } while ((bh = bh->b_this_page) != head);
550
10ce4444
CH
551 /* if we reached the end of the page, sum forwards in following pages */
552 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
553 tindex = startpage->index + 1;
554
555 /* Prune this back to avoid pathological behavior */
556 tloff = min(tlast, startpage->index + 64);
557
558 pagevec_init(&pvec, 0);
559 while (!done && tindex <= tloff) {
560 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
561
562 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
563 break;
564
565 for (i = 0; i < pagevec_count(&pvec); i++) {
566 struct page *page = pvec.pages[i];
567 size_t pg_offset, len = 0;
568
569 if (tindex == tlast) {
570 pg_offset =
571 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
572 if (!pg_offset)
573 break;
574 } else
575 pg_offset = PAGE_CACHE_SIZE;
576
577 if (page->index == tindex && !TestSetPageLocked(page)) {
578 len = xfs_probe_unmapped_page(page, pg_offset);
579 unlock_page(page);
580 }
581
582 if (!len) {
583 done = 1;
584 break;
585 }
586
1da177e4
LT
587 total += len;
588 }
10ce4444
CH
589
590 pagevec_release(&pvec);
591 cond_resched();
1da177e4 592 }
10ce4444 593
1da177e4
LT
594 return total;
595}
596
597/*
10ce4444
CH
598 * Test if a given page is suitable for writing as part of an unwritten
599 * or delayed allocate extent.
1da177e4 600 */
10ce4444
CH
601STATIC int
602xfs_is_delayed_page(
603 struct page *page,
f6d6d4fc 604 unsigned int type)
1da177e4 605{
1da177e4 606 if (PageWriteback(page))
10ce4444 607 return 0;
1da177e4
LT
608
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 {
f6d6d4fc
CH
615 if (buffer_unwritten(bh))
616 acceptable = (type == IOMAP_UNWRITTEN);
617 else if (buffer_delay(bh))
618 acceptable = (type == IOMAP_DELAY);
619 else
1da177e4 620 break;
1da177e4
LT
621 } while ((bh = bh->b_this_page) != head);
622
623 if (acceptable)
10ce4444 624 return 1;
1da177e4
LT
625 }
626
10ce4444 627 return 0;
1da177e4
LT
628}
629
1da177e4
LT
630/*
631 * Allocate & map buffers for page given the extent map. Write it out.
632 * except for the original page of a writepage, this is called on
633 * delalloc/unwritten pages only, for the original page it is possible
634 * that the page has no mapping at all.
635 */
f6d6d4fc 636STATIC int
1da177e4
LT
637xfs_convert_page(
638 struct inode *inode,
639 struct page *page,
10ce4444 640 loff_t tindex,
1da177e4 641 xfs_iomap_t *iomapp,
f6d6d4fc 642 xfs_ioend_t **ioendp,
1da177e4 643 struct writeback_control *wbc,
1da177e4
LT
644 int startio,
645 int all_bh)
646{
f6d6d4fc 647 struct buffer_head *bh, *head;
1da177e4 648 xfs_iomap_t *mp = iomapp, *tmp;
f6d6d4fc
CH
649 unsigned long p_offset, end_offset;
650 unsigned int type;
1da177e4 651 int bbits = inode->i_blkbits;
24e17b5f 652 int len, page_dirty;
f6d6d4fc 653 int count = 0, done = 0, uptodate = 1;
1da177e4 654
10ce4444
CH
655 if (page->index != tindex)
656 goto fail;
657 if (TestSetPageLocked(page))
658 goto fail;
659 if (PageWriteback(page))
660 goto fail_unlock_page;
661 if (page->mapping != inode->i_mapping)
662 goto fail_unlock_page;
663 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
664 goto fail_unlock_page;
665
24e17b5f
NS
666 end_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1));
667
668 /*
669 * page_dirty is initially a count of buffers on the page before
670 * EOF and is decrememted as we move each into a cleanable state.
671 */
672 len = 1 << inode->i_blkbits;
673 end_offset = max(end_offset, PAGE_CACHE_SIZE);
674 end_offset = roundup(end_offset, len);
675 page_dirty = end_offset / len;
676
f6d6d4fc 677 p_offset = 0;
1da177e4
LT
678 bh = head = page_buffers(page);
679 do {
f6d6d4fc 680 if (p_offset >= end_offset)
1da177e4 681 break;
f6d6d4fc
CH
682 if (!buffer_uptodate(bh))
683 uptodate = 0;
684 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
685 done = 1;
1da177e4 686 continue;
f6d6d4fc
CH
687 }
688
689 if (buffer_unwritten(bh))
690 type = IOMAP_UNWRITTEN;
691 else if (buffer_delay(bh))
692 type = IOMAP_DELAY;
693 else {
694 type = 0;
695 if (!(buffer_mapped(bh) && all_bh && startio)) {
696 done = 1;
697 } else if (startio) {
1da177e4 698 lock_buffer(bh);
f6d6d4fc
CH
699 xfs_add_to_ioend(inode, bh, p_offset,
700 type, ioendp, done);
701 count++;
24e17b5f 702 page_dirty--;
1da177e4
LT
703 }
704 continue;
705 }
f6d6d4fc
CH
706 tmp = xfs_offset_to_map(page, mp, p_offset);
707 if (!tmp) {
708 done = 1;
1da177e4 709 continue;
f6d6d4fc 710 }
1da177e4
LT
711 ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
712 ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
713
f6d6d4fc 714 xfs_map_at_offset(page, bh, p_offset, bbits, tmp, *ioendp);
1da177e4 715 if (startio) {
f6d6d4fc
CH
716 xfs_add_to_ioend(inode, bh, p_offset,
717 type, ioendp, done);
718 count++;
1da177e4
LT
719 } else {
720 set_buffer_dirty(bh);
721 unlock_buffer(bh);
722 mark_buffer_dirty(bh);
723 }
24e17b5f 724 page_dirty--;
f6d6d4fc 725 } while (p_offset += len, (bh = bh->b_this_page) != head);
1da177e4 726
f6d6d4fc
CH
727 if (uptodate && bh == head)
728 SetPageUptodate(page);
729
730 if (startio) {
731 if (count)
732 wbc->nr_to_write--;
733 xfs_start_page_writeback(page, wbc, !page_dirty, count);
1da177e4 734 }
f6d6d4fc
CH
735
736 return done;
10ce4444
CH
737 fail_unlock_page:
738 unlock_page(page);
739 fail:
740 return 1;
1da177e4
LT
741}
742
743/*
744 * Convert & write out a cluster of pages in the same extent as defined
745 * by mp and following the start page.
746 */
747STATIC void
748xfs_cluster_write(
749 struct inode *inode,
750 pgoff_t tindex,
751 xfs_iomap_t *iomapp,
f6d6d4fc 752 xfs_ioend_t **ioendp,
1da177e4
LT
753 struct writeback_control *wbc,
754 int startio,
755 int all_bh,
756 pgoff_t tlast)
757{
10ce4444
CH
758 struct pagevec pvec;
759 int done = 0, i;
1da177e4 760
10ce4444
CH
761 pagevec_init(&pvec, 0);
762 while (!done && tindex <= tlast) {
763 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
764
765 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
1da177e4 766 break;
10ce4444
CH
767
768 for (i = 0; i < pagevec_count(&pvec); i++) {
769 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
770 iomapp, ioendp, wbc, startio, all_bh);
771 if (done)
772 break;
773 }
774
775 pagevec_release(&pvec);
776 cond_resched();
1da177e4
LT
777 }
778}
779
780/*
781 * Calling this without startio set means we are being asked to make a dirty
782 * page ready for freeing it's buffers. When called with startio set then
783 * we are coming from writepage.
784 *
785 * When called with startio set it is important that we write the WHOLE
786 * page if possible.
787 * The bh->b_state's cannot know if any of the blocks or which block for
788 * that matter are dirty due to mmap writes, and therefore bh uptodate is
789 * only vaild if the page itself isn't completely uptodate. Some layers
790 * may clear the page dirty flag prior to calling write page, under the
791 * assumption the entire page will be written out; by not writing out the
792 * whole page the page can be reused before all valid dirty data is
793 * written out. Note: in the case of a page that has been dirty'd by
794 * mapwrite and but partially setup by block_prepare_write the
795 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
796 * valid state, thus the whole page must be written out thing.
797 */
798
799STATIC int
800xfs_page_state_convert(
801 struct inode *inode,
802 struct page *page,
803 struct writeback_control *wbc,
804 int startio,
805 int unmapped) /* also implies page uptodate */
806{
f6d6d4fc 807 struct buffer_head *bh, *head;
1da177e4 808 xfs_iomap_t *iomp, iomap;
f6d6d4fc 809 xfs_ioend_t *ioend = NULL, *iohead = NULL;
1da177e4
LT
810 loff_t offset;
811 unsigned long p_offset = 0;
f6d6d4fc 812 unsigned int type;
1da177e4
LT
813 __uint64_t end_offset;
814 pgoff_t end_index, last_index, tlast;
f6d6d4fc
CH
815 int flags, len, err, done = 1;
816 int uptodate = 1;
817 int page_dirty, count = 0, trylock_flag = 0;
1da177e4 818
3ba0815a 819 /* wait for other IO threads? */
f6d6d4fc
CH
820 if (startio && wbc->sync_mode != WB_SYNC_NONE)
821 trylock_flag |= BMAPI_TRYLOCK;
3ba0815a 822
1da177e4
LT
823 /* Is this page beyond the end of the file? */
824 offset = i_size_read(inode);
825 end_index = offset >> PAGE_CACHE_SHIFT;
826 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
827 if (page->index >= end_index) {
828 if ((page->index >= end_index + 1) ||
829 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
19d5bcf3
NS
830 if (startio)
831 unlock_page(page);
832 return 0;
1da177e4
LT
833 }
834 }
835
1da177e4 836 /*
24e17b5f
NS
837 * page_dirty is initially a count of buffers on the page before
838 * EOF and is decrememted as we move each into a cleanable state.
f6d6d4fc
CH
839 *
840 * Derivation:
841 *
842 * End offset is the highest offset that this page should represent.
843 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
844 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
845 * hence give us the correct page_dirty count. On any other page,
846 * it will be zero and in that case we need page_dirty to be the
847 * count of buffers on the page.
848 */
849 end_offset = min_t(unsigned long long,
850 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
24e17b5f 851 len = 1 << inode->i_blkbits;
f6d6d4fc
CH
852 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
853 PAGE_CACHE_SIZE);
854 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
24e17b5f
NS
855 page_dirty = p_offset / len;
856
857 iomp = NULL;
24e17b5f 858 bh = head = page_buffers(page);
f6d6d4fc
CH
859 offset = page_offset(page);
860
861 /* TODO: fix up "done" variable and iomap pointer (boolean) */
862 /* TODO: cleanup count and page_dirty */
1da177e4
LT
863
864 do {
865 if (offset >= end_offset)
866 break;
867 if (!buffer_uptodate(bh))
868 uptodate = 0;
f6d6d4fc
CH
869 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
870 done = 1;
1da177e4 871 continue;
f6d6d4fc 872 }
1da177e4
LT
873
874 if (iomp) {
875 iomp = xfs_offset_to_map(page, &iomap, p_offset);
f6d6d4fc 876 done = (iomp == NULL);
1da177e4
LT
877 }
878
879 /*
880 * First case, map an unwritten extent and prepare for
881 * extent state conversion transaction on completion.
f6d6d4fc 882 *
1da177e4
LT
883 * Second case, allocate space for a delalloc buffer.
884 * We can return EAGAIN here in the release page case.
885 */
f6d6d4fc
CH
886 if (buffer_unwritten(bh) || buffer_delay(bh)) {
887 if (buffer_unwritten(bh)) {
888 type = IOMAP_UNWRITTEN;
889 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
890 } else {
891 type = IOMAP_DELAY;
892 flags = BMAPI_ALLOCATE;
893 if (!startio)
894 flags |= trylock_flag;
895 }
896
1da177e4 897 if (!iomp) {
f6d6d4fc 898 done = 1;
1da177e4 899 err = xfs_map_blocks(inode, offset, len, &iomap,
f6d6d4fc
CH
900 flags);
901 if (err)
1da177e4 902 goto error;
1da177e4
LT
903 iomp = xfs_offset_to_map(page, &iomap,
904 p_offset);
f6d6d4fc 905 done = (iomp == NULL);
1da177e4
LT
906 }
907 if (iomp) {
908 xfs_map_at_offset(page, bh, p_offset,
f6d6d4fc 909 inode->i_blkbits, iomp, ioend);
1da177e4 910 if (startio) {
f6d6d4fc
CH
911 xfs_add_to_ioend(inode, bh, p_offset,
912 type, &ioend, done);
1da177e4
LT
913 } else {
914 set_buffer_dirty(bh);
915 unlock_buffer(bh);
916 mark_buffer_dirty(bh);
917 }
918 page_dirty--;
f6d6d4fc
CH
919 count++;
920 } else {
921 done = 1;
1da177e4
LT
922 }
923 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
924 (unmapped || startio)) {
925
f6d6d4fc 926 type = 0;
1da177e4 927 if (!buffer_mapped(bh)) {
1da177e4
LT
928
929 /*
930 * Getting here implies an unmapped buffer
931 * was found, and we are in a path where we
932 * need to write the whole page out.
933 */
934 if (!iomp) {
f6d6d4fc
CH
935 int size;
936
1da177e4
LT
937 size = xfs_probe_unmapped_cluster(
938 inode, page, bh, head);
939 err = xfs_map_blocks(inode, offset,
940 size, &iomap,
941 BMAPI_WRITE|BMAPI_MMAP);
942 if (err) {
943 goto error;
944 }
945 iomp = xfs_offset_to_map(page, &iomap,
946 p_offset);
f6d6d4fc 947 done = (iomp == NULL);
1da177e4
LT
948 }
949 if (iomp) {
f6d6d4fc
CH
950 xfs_map_at_offset(page, bh, p_offset,
951 inode->i_blkbits, iomp,
952 ioend);
1da177e4 953 if (startio) {
f6d6d4fc
CH
954 xfs_add_to_ioend(inode,
955 bh, p_offset, type,
956 &ioend, done);
1da177e4
LT
957 } else {
958 set_buffer_dirty(bh);
959 unlock_buffer(bh);
960 mark_buffer_dirty(bh);
961 }
962 page_dirty--;
f6d6d4fc
CH
963 count++;
964 } else {
965 done = 1;
1da177e4
LT
966 }
967 } else if (startio) {
968 if (buffer_uptodate(bh) &&
969 !test_and_set_bit(BH_Lock, &bh->b_state)) {
f6d6d4fc
CH
970 ASSERT(buffer_mapped(bh));
971 xfs_add_to_ioend(inode,
972 bh, p_offset, type,
973 &ioend, done);
1da177e4 974 page_dirty--;
f6d6d4fc
CH
975 count++;
976 } else {
977 done = 1;
1da177e4 978 }
f6d6d4fc
CH
979 } else {
980 done = 1;
1da177e4
LT
981 }
982 }
f6d6d4fc
CH
983
984 if (!iohead)
985 iohead = ioend;
986
987 } while (offset += len, ((bh = bh->b_this_page) != head));
1da177e4
LT
988
989 if (uptodate && bh == head)
990 SetPageUptodate(page);
991
f6d6d4fc
CH
992 if (startio)
993 xfs_start_page_writeback(page, wbc, 1, count);
1da177e4 994
f6d6d4fc 995 if (ioend && iomp && !done) {
775bf6c9 996 offset = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
1da177e4 997 PAGE_CACHE_SHIFT;
775bf6c9 998 tlast = min_t(pgoff_t, offset, last_index);
f6d6d4fc
CH
999 xfs_cluster_write(inode, page->index + 1, iomp, &ioend,
1000 wbc, startio, unmapped, tlast);
1da177e4
LT
1001 }
1002
f6d6d4fc
CH
1003 if (iohead)
1004 xfs_submit_ioend(iohead);
1005
1da177e4
LT
1006 return page_dirty;
1007
1008error:
f6d6d4fc
CH
1009 if (iohead)
1010 xfs_cancel_ioend(iohead);
1da177e4
LT
1011
1012 /*
1013 * If it's delalloc and we have nowhere to put it,
1014 * throw it away, unless the lower layers told
1015 * us to try again.
1016 */
1017 if (err != -EAGAIN) {
f6d6d4fc 1018 if (!unmapped)
1da177e4 1019 block_invalidatepage(page, 0);
1da177e4
LT
1020 ClearPageUptodate(page);
1021 }
1022 return err;
1023}
1024
1025STATIC int
1026__linvfs_get_block(
1027 struct inode *inode,
1028 sector_t iblock,
1029 unsigned long blocks,
1030 struct buffer_head *bh_result,
1031 int create,
1032 int direct,
1033 bmapi_flags_t flags)
1034{
1035 vnode_t *vp = LINVFS_GET_VP(inode);
1036 xfs_iomap_t iomap;
fdc7ed75
NS
1037 xfs_off_t offset;
1038 ssize_t size;
1da177e4
LT
1039 int retpbbm = 1;
1040 int error;
1da177e4 1041
fdc7ed75 1042 offset = (xfs_off_t)iblock << inode->i_blkbits;
a4656391
NS
1043 if (blocks)
1044 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1045 (xfs_off_t)blocks << inode->i_blkbits);
1046 else
1047 size = 1 << inode->i_blkbits;
1da177e4
LT
1048
1049 VOP_BMAP(vp, offset, size,
1050 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1051 if (error)
1052 return -error;
1053
1054 if (retpbbm == 0)
1055 return 0;
1056
1057 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
fdc7ed75
NS
1058 xfs_daddr_t bn;
1059 xfs_off_t delta;
1da177e4
LT
1060
1061 /* For unwritten extents do not report a disk address on
1062 * the read case (treat as if we're reading into a hole).
1063 */
1064 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1065 delta = offset - iomap.iomap_offset;
1066 delta >>= inode->i_blkbits;
1067
1068 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1069 bn += delta;
1070 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1071 bh_result->b_blocknr = bn;
1072 set_buffer_mapped(bh_result);
1073 }
1074 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1075 if (direct)
1076 bh_result->b_private = inode;
1077 set_buffer_unwritten(bh_result);
1078 set_buffer_delay(bh_result);
1079 }
1080 }
1081
1082 /* If this is a realtime file, data might be on a new device */
ce8e922c 1083 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
1da177e4
LT
1084
1085 /* If we previously allocated a block out beyond eof and
1086 * we are now coming back to use it then we will need to
1087 * flag it as new even if it has a disk address.
1088 */
1089 if (create &&
1090 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
fdc7ed75 1091 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
1da177e4 1092 set_buffer_new(bh_result);
1da177e4
LT
1093
1094 if (iomap.iomap_flags & IOMAP_DELAY) {
1095 BUG_ON(direct);
1096 if (create) {
1097 set_buffer_uptodate(bh_result);
1098 set_buffer_mapped(bh_result);
1099 set_buffer_delay(bh_result);
1100 }
1101 }
1102
1103 if (blocks) {
fdc7ed75
NS
1104 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1105 offset = min_t(xfs_off_t,
1106 iomap.iomap_bsize - iomap.iomap_delta,
a4656391 1107 (xfs_off_t)blocks << inode->i_blkbits);
fdc7ed75 1108 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
1da177e4
LT
1109 }
1110
1111 return 0;
1112}
1113
1114int
1115linvfs_get_block(
1116 struct inode *inode,
1117 sector_t iblock,
1118 struct buffer_head *bh_result,
1119 int create)
1120{
1121 return __linvfs_get_block(inode, iblock, 0, bh_result,
1122 create, 0, BMAPI_WRITE);
1123}
1124
1125STATIC int
1126linvfs_get_blocks_direct(
1127 struct inode *inode,
1128 sector_t iblock,
1129 unsigned long max_blocks,
1130 struct buffer_head *bh_result,
1131 int create)
1132{
1133 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1134 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1135}
1136
f0973863
CH
1137STATIC void
1138linvfs_end_io_direct(
1139 struct kiocb *iocb,
1140 loff_t offset,
1141 ssize_t size,
1142 void *private)
1143{
1144 xfs_ioend_t *ioend = iocb->private;
1145
1146 /*
1147 * Non-NULL private data means we need to issue a transaction to
1148 * convert a range from unwritten to written extents. This needs
1149 * to happen from process contect but aio+dio I/O completion
1150 * happens from irq context so we need to defer it to a workqueue.
1151 * This is not nessecary for synchronous direct I/O, but we do
1152 * it anyway to keep the code uniform and simpler.
1153 *
1154 * The core direct I/O code might be changed to always call the
1155 * completion handler in the future, in which case all this can
1156 * go away.
1157 */
1158 if (private && size > 0) {
1159 ioend->io_offset = offset;
1160 ioend->io_size = size;
1161 xfs_finish_ioend(ioend);
1162 } else {
1163 ASSERT(size >= 0);
1164 xfs_destroy_ioend(ioend);
1165 }
1166
1167 /*
1168 * blockdev_direct_IO can return an error even afer the I/O
1169 * completion handler was called. Thus we need to protect
1170 * against double-freeing.
1171 */
1172 iocb->private = NULL;
1173}
1174
1da177e4
LT
1175STATIC ssize_t
1176linvfs_direct_IO(
1177 int rw,
1178 struct kiocb *iocb,
1179 const struct iovec *iov,
1180 loff_t offset,
1181 unsigned long nr_segs)
1182{
1183 struct file *file = iocb->ki_filp;
1184 struct inode *inode = file->f_mapping->host;
1185 vnode_t *vp = LINVFS_GET_VP(inode);
1186 xfs_iomap_t iomap;
1187 int maps = 1;
1188 int error;
f0973863 1189 ssize_t ret;
1da177e4
LT
1190
1191 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1192 if (error)
1193 return -error;
1194
f6d6d4fc 1195 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
f0973863
CH
1196
1197 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
ce8e922c 1198 iomap.iomap_target->bt_bdev,
1da177e4
LT
1199 iov, offset, nr_segs,
1200 linvfs_get_blocks_direct,
f0973863
CH
1201 linvfs_end_io_direct);
1202
1203 if (unlikely(ret <= 0 && iocb->private))
1204 xfs_destroy_ioend(iocb->private);
1205 return ret;
1da177e4
LT
1206}
1207
1208
1209STATIC sector_t
1210linvfs_bmap(
1211 struct address_space *mapping,
1212 sector_t block)
1213{
1214 struct inode *inode = (struct inode *)mapping->host;
1215 vnode_t *vp = LINVFS_GET_VP(inode);
1216 int error;
1217
1218 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1219
1220 VOP_RWLOCK(vp, VRWLOCK_READ);
1221 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1222 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1223 return generic_block_bmap(mapping, block, linvfs_get_block);
1224}
1225
1226STATIC int
1227linvfs_readpage(
1228 struct file *unused,
1229 struct page *page)
1230{
1231 return mpage_readpage(page, linvfs_get_block);
1232}
1233
1234STATIC int
1235linvfs_readpages(
1236 struct file *unused,
1237 struct address_space *mapping,
1238 struct list_head *pages,
1239 unsigned nr_pages)
1240{
1241 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1242}
1243
1244STATIC void
1245xfs_count_page_state(
1246 struct page *page,
1247 int *delalloc,
1248 int *unmapped,
1249 int *unwritten)
1250{
1251 struct buffer_head *bh, *head;
1252
1253 *delalloc = *unmapped = *unwritten = 0;
1254
1255 bh = head = page_buffers(page);
1256 do {
1257 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1258 (*unmapped) = 1;
1259 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1260 clear_buffer_unwritten(bh);
1261 else if (buffer_unwritten(bh))
1262 (*unwritten) = 1;
1263 else if (buffer_delay(bh))
1264 (*delalloc) = 1;
1265 } while ((bh = bh->b_this_page) != head);
1266}
1267
1268
1269/*
1270 * writepage: Called from one of two places:
1271 *
1272 * 1. we are flushing a delalloc buffer head.
1273 *
1274 * 2. we are writing out a dirty page. Typically the page dirty
1275 * state is cleared before we get here. In this case is it
1276 * conceivable we have no buffer heads.
1277 *
1278 * For delalloc space on the page we need to allocate space and
1279 * flush it. For unmapped buffer heads on the page we should
1280 * allocate space if the page is uptodate. For any other dirty
1281 * buffer heads on the page we should flush them.
1282 *
1283 * If we detect that a transaction would be required to flush
1284 * the page, we have to check the process flags first, if we
1285 * are already in a transaction or disk I/O during allocations
1286 * is off, we need to fail the writepage and redirty the page.
1287 */
1288
1289STATIC int
1290linvfs_writepage(
1291 struct page *page,
1292 struct writeback_control *wbc)
1293{
1294 int error;
1295 int need_trans;
1296 int delalloc, unmapped, unwritten;
1297 struct inode *inode = page->mapping->host;
1298
1299 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1300
1301 /*
1302 * We need a transaction if:
1303 * 1. There are delalloc buffers on the page
1304 * 2. The page is uptodate and we have unmapped buffers
1305 * 3. The page is uptodate and we have no buffers
1306 * 4. There are unwritten buffers on the page
1307 */
1308
1309 if (!page_has_buffers(page)) {
1310 unmapped = 1;
1311 need_trans = 1;
1312 } else {
1313 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1314 if (!PageUptodate(page))
1315 unmapped = 0;
1316 need_trans = delalloc + unmapped + unwritten;
1317 }
1318
1319 /*
1320 * If we need a transaction and the process flags say
1321 * we are already in a transaction, or no IO is allowed
1322 * then mark the page dirty again and leave the page
1323 * as is.
1324 */
1325 if (PFLAGS_TEST_FSTRANS() && need_trans)
1326 goto out_fail;
1327
1328 /*
1329 * Delay hooking up buffer heads until we have
1330 * made our go/no-go decision.
1331 */
1332 if (!page_has_buffers(page))
1333 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1334
1335 /*
1336 * Convert delayed allocate, unwritten or unmapped space
1337 * to real space and flush out to disk.
1338 */
1339 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1340 if (error == -EAGAIN)
1341 goto out_fail;
1342 if (unlikely(error < 0))
1343 goto out_unlock;
1344
1345 return 0;
1346
1347out_fail:
1348 redirty_page_for_writepage(wbc, page);
1349 unlock_page(page);
1350 return 0;
1351out_unlock:
1352 unlock_page(page);
1353 return error;
1354}
1355
bcec2b7f
NS
1356STATIC int
1357linvfs_invalidate_page(
1358 struct page *page,
1359 unsigned long offset)
1360{
1361 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1362 page->mapping->host, page, offset);
1363 return block_invalidatepage(page, offset);
1364}
1365
1da177e4
LT
1366/*
1367 * Called to move a page into cleanable state - and from there
1368 * to be released. Possibly the page is already clean. We always
1369 * have buffer heads in this call.
1370 *
1371 * Returns 0 if the page is ok to release, 1 otherwise.
1372 *
1373 * Possible scenarios are:
1374 *
1375 * 1. We are being called to release a page which has been written
1376 * to via regular I/O. buffer heads will be dirty and possibly
1377 * delalloc. If no delalloc buffer heads in this case then we
1378 * can just return zero.
1379 *
1380 * 2. We are called to release a page which has been written via
1381 * mmap, all we need to do is ensure there is no delalloc
1382 * state in the buffer heads, if not we can let the caller
1383 * free them and we should come back later via writepage.
1384 */
1385STATIC int
1386linvfs_release_page(
1387 struct page *page,
27496a8c 1388 gfp_t gfp_mask)
1da177e4
LT
1389{
1390 struct inode *inode = page->mapping->host;
1391 int dirty, delalloc, unmapped, unwritten;
1392 struct writeback_control wbc = {
1393 .sync_mode = WB_SYNC_ALL,
1394 .nr_to_write = 1,
1395 };
1396
1397 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1398
1399 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1400 if (!delalloc && !unwritten)
1401 goto free_buffers;
1402
1403 if (!(gfp_mask & __GFP_FS))
1404 return 0;
1405
1406 /* If we are already inside a transaction or the thread cannot
1407 * do I/O, we cannot release this page.
1408 */
1409 if (PFLAGS_TEST_FSTRANS())
1410 return 0;
1411
1412 /*
1413 * Convert delalloc space to real space, do not flush the
1414 * data out to disk, that will be done by the caller.
1415 * Never need to allocate space here - we will always
1416 * come back to writepage in that case.
1417 */
1418 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1419 if (dirty == 0 && !unwritten)
1420 goto free_buffers;
1421 return 0;
1422
1423free_buffers:
1424 return try_to_free_buffers(page);
1425}
1426
1427STATIC int
1428linvfs_prepare_write(
1429 struct file *file,
1430 struct page *page,
1431 unsigned int from,
1432 unsigned int to)
1433{
1434 return block_prepare_write(page, from, to, linvfs_get_block);
1435}
1436
1437struct address_space_operations linvfs_aops = {
1438 .readpage = linvfs_readpage,
1439 .readpages = linvfs_readpages,
1440 .writepage = linvfs_writepage,
1441 .sync_page = block_sync_page,
1442 .releasepage = linvfs_release_page,
bcec2b7f 1443 .invalidatepage = linvfs_invalidate_page,
1da177e4
LT
1444 .prepare_write = linvfs_prepare_write,
1445 .commit_write = generic_commit_write,
1446 .bmap = linvfs_bmap,
1447 .direct_IO = linvfs_direct_IO,
1448};