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