]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/xfs/linux-2.6/xfs_aops.c
[XFS] Move some code around to avoid prototypes and prep for future
[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
1da177e4
LT
489STATIC void
490xfs_map_at_offset(
1da177e4 491 struct buffer_head *bh,
1defeac9 492 loff_t offset,
1da177e4 493 int block_bits,
1defeac9 494 xfs_iomap_t *iomapp)
1da177e4
LT
495{
496 xfs_daddr_t bn;
1da177e4
LT
497 int sector_shift;
498
499 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
500 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
501 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
502
1da177e4 503 sector_shift = block_bits - BBSHIFT;
1defeac9
CH
504 bn = (iomapp->iomap_bn >> sector_shift) +
505 ((offset - iomapp->iomap_offset) >> block_bits);
506
507 ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
1da177e4
LT
508 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
509
510 lock_buffer(bh);
511 bh->b_blocknr = bn;
ce8e922c 512 bh->b_bdev = iomapp->iomap_target->bt_bdev;
1da177e4
LT
513 set_buffer_mapped(bh);
514 clear_buffer_delay(bh);
f6d6d4fc 515 clear_buffer_unwritten(bh);
1da177e4
LT
516}
517
518/*
6c4fe19f 519 * Look for a page at index that is suitable for clustering.
1da177e4
LT
520 */
521STATIC unsigned int
6c4fe19f 522xfs_probe_page(
10ce4444 523 struct page *page,
6c4fe19f
CH
524 unsigned int pg_offset,
525 int mapped)
1da177e4 526{
1da177e4
LT
527 int ret = 0;
528
1da177e4 529 if (PageWriteback(page))
10ce4444 530 return 0;
1da177e4
LT
531
532 if (page->mapping && PageDirty(page)) {
533 if (page_has_buffers(page)) {
534 struct buffer_head *bh, *head;
535
536 bh = head = page_buffers(page);
537 do {
6c4fe19f
CH
538 if (!buffer_uptodate(bh))
539 break;
540 if (mapped != buffer_mapped(bh))
1da177e4
LT
541 break;
542 ret += bh->b_size;
543 if (ret >= pg_offset)
544 break;
545 } while ((bh = bh->b_this_page) != head);
546 } else
6c4fe19f 547 ret = mapped ? 0 : PAGE_CACHE_SIZE;
1da177e4
LT
548 }
549
1da177e4
LT
550 return ret;
551}
552
f6d6d4fc 553STATIC size_t
6c4fe19f 554xfs_probe_cluster(
1da177e4
LT
555 struct inode *inode,
556 struct page *startpage,
557 struct buffer_head *bh,
6c4fe19f
CH
558 struct buffer_head *head,
559 int mapped)
1da177e4 560{
10ce4444 561 struct pagevec pvec;
1da177e4 562 pgoff_t tindex, tlast, tloff;
10ce4444
CH
563 size_t total = 0;
564 int done = 0, i;
1da177e4
LT
565
566 /* First sum forwards in this page */
567 do {
2353e8e9 568 if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
10ce4444 569 return total;
1da177e4
LT
570 total += bh->b_size;
571 } while ((bh = bh->b_this_page) != head);
572
10ce4444
CH
573 /* if we reached the end of the page, sum forwards in following pages */
574 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
575 tindex = startpage->index + 1;
576
577 /* Prune this back to avoid pathological behavior */
578 tloff = min(tlast, startpage->index + 64);
579
580 pagevec_init(&pvec, 0);
581 while (!done && tindex <= tloff) {
582 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
583
584 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
585 break;
586
587 for (i = 0; i < pagevec_count(&pvec); i++) {
588 struct page *page = pvec.pages[i];
589 size_t pg_offset, len = 0;
590
591 if (tindex == tlast) {
592 pg_offset =
593 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
1defeac9
CH
594 if (!pg_offset) {
595 done = 1;
10ce4444 596 break;
1defeac9 597 }
10ce4444
CH
598 } else
599 pg_offset = PAGE_CACHE_SIZE;
600
601 if (page->index == tindex && !TestSetPageLocked(page)) {
6c4fe19f 602 len = xfs_probe_page(page, pg_offset, mapped);
10ce4444
CH
603 unlock_page(page);
604 }
605
606 if (!len) {
607 done = 1;
608 break;
609 }
610
1da177e4 611 total += len;
1defeac9 612 tindex++;
1da177e4 613 }
10ce4444
CH
614
615 pagevec_release(&pvec);
616 cond_resched();
1da177e4 617 }
10ce4444 618
1da177e4
LT
619 return total;
620}
621
622/*
10ce4444
CH
623 * Test if a given page is suitable for writing as part of an unwritten
624 * or delayed allocate extent.
1da177e4 625 */
10ce4444
CH
626STATIC int
627xfs_is_delayed_page(
628 struct page *page,
f6d6d4fc 629 unsigned int type)
1da177e4 630{
1da177e4 631 if (PageWriteback(page))
10ce4444 632 return 0;
1da177e4
LT
633
634 if (page->mapping && page_has_buffers(page)) {
635 struct buffer_head *bh, *head;
636 int acceptable = 0;
637
638 bh = head = page_buffers(page);
639 do {
f6d6d4fc
CH
640 if (buffer_unwritten(bh))
641 acceptable = (type == IOMAP_UNWRITTEN);
642 else if (buffer_delay(bh))
643 acceptable = (type == IOMAP_DELAY);
6c4fe19f
CH
644 else if (buffer_mapped(bh))
645 acceptable = (type == 0);
f6d6d4fc 646 else
1da177e4 647 break;
1da177e4
LT
648 } while ((bh = bh->b_this_page) != head);
649
650 if (acceptable)
10ce4444 651 return 1;
1da177e4
LT
652 }
653
10ce4444 654 return 0;
1da177e4
LT
655}
656
1da177e4
LT
657/*
658 * Allocate & map buffers for page given the extent map. Write it out.
659 * except for the original page of a writepage, this is called on
660 * delalloc/unwritten pages only, for the original page it is possible
661 * that the page has no mapping at all.
662 */
f6d6d4fc 663STATIC int
1da177e4
LT
664xfs_convert_page(
665 struct inode *inode,
666 struct page *page,
10ce4444 667 loff_t tindex,
1defeac9 668 xfs_iomap_t *mp,
f6d6d4fc 669 xfs_ioend_t **ioendp,
1da177e4 670 struct writeback_control *wbc,
1da177e4
LT
671 int startio,
672 int all_bh)
673{
f6d6d4fc 674 struct buffer_head *bh, *head;
9260dc6b
CH
675 xfs_off_t end_offset;
676 unsigned long p_offset;
f6d6d4fc 677 unsigned int type;
1da177e4 678 int bbits = inode->i_blkbits;
24e17b5f 679 int len, page_dirty;
f6d6d4fc 680 int count = 0, done = 0, uptodate = 1;
9260dc6b 681 xfs_off_t offset = page_offset(page);
1da177e4 682
10ce4444
CH
683 if (page->index != tindex)
684 goto fail;
685 if (TestSetPageLocked(page))
686 goto fail;
687 if (PageWriteback(page))
688 goto fail_unlock_page;
689 if (page->mapping != inode->i_mapping)
690 goto fail_unlock_page;
691 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
692 goto fail_unlock_page;
693
24e17b5f
NS
694 /*
695 * page_dirty is initially a count of buffers on the page before
696 * EOF and is decrememted as we move each into a cleanable state.
9260dc6b
CH
697 *
698 * Derivation:
699 *
700 * End offset is the highest offset that this page should represent.
701 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
702 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
703 * hence give us the correct page_dirty count. On any other page,
704 * it will be zero and in that case we need page_dirty to be the
705 * count of buffers on the page.
24e17b5f 706 */
9260dc6b
CH
707 end_offset = min_t(unsigned long long,
708 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
709 i_size_read(inode));
710
24e17b5f 711 len = 1 << inode->i_blkbits;
9260dc6b
CH
712 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
713 PAGE_CACHE_SIZE);
714 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
715 page_dirty = p_offset / len;
24e17b5f 716
1da177e4
LT
717 bh = head = page_buffers(page);
718 do {
9260dc6b 719 if (offset >= end_offset)
1da177e4 720 break;
f6d6d4fc
CH
721 if (!buffer_uptodate(bh))
722 uptodate = 0;
723 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
724 done = 1;
1da177e4 725 continue;
f6d6d4fc
CH
726 }
727
9260dc6b
CH
728 if (buffer_unwritten(bh) || buffer_delay(bh)) {
729 if (buffer_unwritten(bh))
730 type = IOMAP_UNWRITTEN;
731 else
732 type = IOMAP_DELAY;
733
734 if (!xfs_iomap_valid(mp, offset)) {
f6d6d4fc 735 done = 1;
9260dc6b
CH
736 continue;
737 }
738
739 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
740 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
741
742 xfs_map_at_offset(bh, offset, bbits, mp);
743 if (startio) {
7336cea8 744 xfs_add_to_ioend(inode, bh, offset,
9260dc6b
CH
745 type, ioendp, done);
746 } else {
747 set_buffer_dirty(bh);
748 unlock_buffer(bh);
749 mark_buffer_dirty(bh);
750 }
751 page_dirty--;
752 count++;
753 } else {
754 type = 0;
755 if (buffer_mapped(bh) && all_bh && startio) {
1da177e4 756 lock_buffer(bh);
7336cea8 757 xfs_add_to_ioend(inode, bh, offset,
f6d6d4fc
CH
758 type, ioendp, done);
759 count++;
24e17b5f 760 page_dirty--;
9260dc6b
CH
761 } else {
762 done = 1;
1da177e4 763 }
1da177e4 764 }
7336cea8 765 } while (offset += len, (bh = bh->b_this_page) != head);
1da177e4 766
f6d6d4fc
CH
767 if (uptodate && bh == head)
768 SetPageUptodate(page);
769
770 if (startio) {
f5e596bb
CH
771 if (count) {
772 struct backing_dev_info *bdi;
773
774 bdi = inode->i_mapping->backing_dev_info;
9fddaca2 775 wbc->nr_to_write--;
f5e596bb
CH
776 if (bdi_write_congested(bdi)) {
777 wbc->encountered_congestion = 1;
778 done = 1;
9fddaca2 779 } else if (wbc->nr_to_write <= 0) {
f5e596bb
CH
780 done = 1;
781 }
782 }
f6d6d4fc 783 xfs_start_page_writeback(page, wbc, !page_dirty, count);
1da177e4 784 }
f6d6d4fc
CH
785
786 return done;
10ce4444
CH
787 fail_unlock_page:
788 unlock_page(page);
789 fail:
790 return 1;
1da177e4
LT
791}
792
793/*
794 * Convert & write out a cluster of pages in the same extent as defined
795 * by mp and following the start page.
796 */
797STATIC void
798xfs_cluster_write(
799 struct inode *inode,
800 pgoff_t tindex,
801 xfs_iomap_t *iomapp,
f6d6d4fc 802 xfs_ioend_t **ioendp,
1da177e4
LT
803 struct writeback_control *wbc,
804 int startio,
805 int all_bh,
806 pgoff_t tlast)
807{
10ce4444
CH
808 struct pagevec pvec;
809 int done = 0, i;
1da177e4 810
10ce4444
CH
811 pagevec_init(&pvec, 0);
812 while (!done && tindex <= tlast) {
813 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
814
815 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
1da177e4 816 break;
10ce4444
CH
817
818 for (i = 0; i < pagevec_count(&pvec); i++) {
819 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
820 iomapp, ioendp, wbc, startio, all_bh);
821 if (done)
822 break;
823 }
824
825 pagevec_release(&pvec);
826 cond_resched();
1da177e4
LT
827 }
828}
829
830/*
831 * Calling this without startio set means we are being asked to make a dirty
832 * page ready for freeing it's buffers. When called with startio set then
833 * we are coming from writepage.
834 *
835 * When called with startio set it is important that we write the WHOLE
836 * page if possible.
837 * The bh->b_state's cannot know if any of the blocks or which block for
838 * that matter are dirty due to mmap writes, and therefore bh uptodate is
839 * only vaild if the page itself isn't completely uptodate. Some layers
840 * may clear the page dirty flag prior to calling write page, under the
841 * assumption the entire page will be written out; by not writing out the
842 * whole page the page can be reused before all valid dirty data is
843 * written out. Note: in the case of a page that has been dirty'd by
844 * mapwrite and but partially setup by block_prepare_write the
845 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
846 * valid state, thus the whole page must be written out thing.
847 */
848
849STATIC int
850xfs_page_state_convert(
851 struct inode *inode,
852 struct page *page,
853 struct writeback_control *wbc,
854 int startio,
855 int unmapped) /* also implies page uptodate */
856{
f6d6d4fc 857 struct buffer_head *bh, *head;
1defeac9 858 xfs_iomap_t iomap;
f6d6d4fc 859 xfs_ioend_t *ioend = NULL, *iohead = NULL;
1da177e4
LT
860 loff_t offset;
861 unsigned long p_offset = 0;
f6d6d4fc 862 unsigned int type;
1da177e4
LT
863 __uint64_t end_offset;
864 pgoff_t end_index, last_index, tlast;
d5cb48aa
CH
865 ssize_t size, len;
866 int flags, err, iomap_valid = 0, uptodate = 1;
f6d6d4fc 867 int page_dirty, count = 0, trylock_flag = 0;
6c4fe19f 868 int all_bh = unmapped;
1da177e4 869
3ba0815a 870 /* wait for other IO threads? */
f5e596bb 871 if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
f6d6d4fc 872 trylock_flag |= BMAPI_TRYLOCK;
3ba0815a 873
1da177e4
LT
874 /* Is this page beyond the end of the file? */
875 offset = i_size_read(inode);
876 end_index = offset >> PAGE_CACHE_SHIFT;
877 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
878 if (page->index >= end_index) {
879 if ((page->index >= end_index + 1) ||
880 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
19d5bcf3
NS
881 if (startio)
882 unlock_page(page);
883 return 0;
1da177e4
LT
884 }
885 }
886
1da177e4 887 /*
24e17b5f
NS
888 * page_dirty is initially a count of buffers on the page before
889 * EOF and is decrememted as we move each into a cleanable state.
f6d6d4fc
CH
890 *
891 * Derivation:
892 *
893 * End offset is the highest offset that this page should represent.
894 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
895 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
896 * hence give us the correct page_dirty count. On any other page,
897 * it will be zero and in that case we need page_dirty to be the
898 * count of buffers on the page.
899 */
900 end_offset = min_t(unsigned long long,
901 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
24e17b5f 902 len = 1 << inode->i_blkbits;
f6d6d4fc
CH
903 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
904 PAGE_CACHE_SIZE);
905 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
24e17b5f
NS
906 page_dirty = p_offset / len;
907
24e17b5f 908 bh = head = page_buffers(page);
f6d6d4fc 909 offset = page_offset(page);
6c4fe19f
CH
910 flags = -1;
911 type = 0;
f6d6d4fc 912
f6d6d4fc 913 /* TODO: cleanup count and page_dirty */
1da177e4
LT
914
915 do {
916 if (offset >= end_offset)
917 break;
918 if (!buffer_uptodate(bh))
919 uptodate = 0;
f6d6d4fc 920 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
1defeac9
CH
921 /*
922 * the iomap is actually still valid, but the ioend
923 * isn't. shouldn't happen too often.
924 */
925 iomap_valid = 0;
1da177e4 926 continue;
f6d6d4fc 927 }
1da177e4 928
1defeac9
CH
929 if (iomap_valid)
930 iomap_valid = xfs_iomap_valid(&iomap, offset);
1da177e4
LT
931
932 /*
933 * First case, map an unwritten extent and prepare for
934 * extent state conversion transaction on completion.
f6d6d4fc 935 *
1da177e4
LT
936 * Second case, allocate space for a delalloc buffer.
937 * We can return EAGAIN here in the release page case.
d5cb48aa
CH
938 *
939 * Third case, an unmapped buffer was found, and we are
940 * in a path where we need to write the whole page out.
941 */
942 if (buffer_unwritten(bh) || buffer_delay(bh) ||
943 ((buffer_uptodate(bh) || PageUptodate(page)) &&
944 !buffer_mapped(bh) && (unmapped || startio))) {
6c4fe19f
CH
945 /*
946 * Make sure we don't use a read-only iomap
947 */
948 if (flags == BMAPI_READ)
949 iomap_valid = 0;
950
f6d6d4fc
CH
951 if (buffer_unwritten(bh)) {
952 type = IOMAP_UNWRITTEN;
953 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
d5cb48aa 954 } else if (buffer_delay(bh)) {
f6d6d4fc
CH
955 type = IOMAP_DELAY;
956 flags = BMAPI_ALLOCATE;
957 if (!startio)
958 flags |= trylock_flag;
d5cb48aa 959 } else {
6c4fe19f 960 type = IOMAP_NEW;
d5cb48aa 961 flags = BMAPI_WRITE|BMAPI_MMAP;
f6d6d4fc
CH
962 }
963
1defeac9 964 if (!iomap_valid) {
6c4fe19f
CH
965 if (type == IOMAP_NEW) {
966 size = xfs_probe_cluster(inode,
967 page, bh, head, 0);
d5cb48aa
CH
968 } else {
969 size = len;
970 }
971
972 err = xfs_map_blocks(inode, offset, size,
973 &iomap, flags);
f6d6d4fc 974 if (err)
1da177e4 975 goto error;
1defeac9 976 iomap_valid = xfs_iomap_valid(&iomap, offset);
1da177e4 977 }
1defeac9
CH
978 if (iomap_valid) {
979 xfs_map_at_offset(bh, offset,
980 inode->i_blkbits, &iomap);
1da177e4 981 if (startio) {
7336cea8 982 xfs_add_to_ioend(inode, bh, offset,
1defeac9
CH
983 type, &ioend,
984 !iomap_valid);
1da177e4
LT
985 } else {
986 set_buffer_dirty(bh);
987 unlock_buffer(bh);
988 mark_buffer_dirty(bh);
989 }
990 page_dirty--;
f6d6d4fc 991 count++;
1da177e4 992 }
d5cb48aa 993 } else if (buffer_uptodate(bh) && startio) {
6c4fe19f
CH
994 /*
995 * we got here because the buffer is already mapped.
996 * That means it must already have extents allocated
997 * underneath it. Map the extent by reading it.
998 */
999 if (!iomap_valid || type != 0) {
1000 flags = BMAPI_READ;
1001 size = xfs_probe_cluster(inode, page, bh,
1002 head, 1);
1003 err = xfs_map_blocks(inode, offset, size,
1004 &iomap, flags);
1005 if (err)
1006 goto error;
1007 iomap_valid = xfs_iomap_valid(&iomap, offset);
1008 }
d5cb48aa 1009
6c4fe19f 1010 type = 0;
d5cb48aa
CH
1011 if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
1012 ASSERT(buffer_mapped(bh));
6c4fe19f
CH
1013 if (iomap_valid)
1014 all_bh = 1;
7336cea8 1015 xfs_add_to_ioend(inode, bh, offset, type,
d5cb48aa
CH
1016 &ioend, !iomap_valid);
1017 page_dirty--;
1018 count++;
f6d6d4fc 1019 } else {
1defeac9 1020 iomap_valid = 0;
1da177e4 1021 }
d5cb48aa
CH
1022 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
1023 (unmapped || startio)) {
1024 iomap_valid = 0;
1da177e4 1025 }
f6d6d4fc
CH
1026
1027 if (!iohead)
1028 iohead = ioend;
1029
1030 } while (offset += len, ((bh = bh->b_this_page) != head));
1da177e4
LT
1031
1032 if (uptodate && bh == head)
1033 SetPageUptodate(page);
1034
f6d6d4fc
CH
1035 if (startio)
1036 xfs_start_page_writeback(page, wbc, 1, count);
1da177e4 1037
1defeac9
CH
1038 if (ioend && iomap_valid) {
1039 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
1da177e4 1040 PAGE_CACHE_SHIFT;
775bf6c9 1041 tlast = min_t(pgoff_t, offset, last_index);
1defeac9 1042 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
6c4fe19f 1043 wbc, startio, all_bh, tlast);
1da177e4
LT
1044 }
1045
f6d6d4fc
CH
1046 if (iohead)
1047 xfs_submit_ioend(iohead);
1048
1da177e4
LT
1049 return page_dirty;
1050
1051error:
f6d6d4fc
CH
1052 if (iohead)
1053 xfs_cancel_ioend(iohead);
1da177e4
LT
1054
1055 /*
1056 * If it's delalloc and we have nowhere to put it,
1057 * throw it away, unless the lower layers told
1058 * us to try again.
1059 */
1060 if (err != -EAGAIN) {
f6d6d4fc 1061 if (!unmapped)
1da177e4 1062 block_invalidatepage(page, 0);
1da177e4
LT
1063 ClearPageUptodate(page);
1064 }
1065 return err;
1066}
1067
f51623b2
NS
1068/*
1069 * writepage: Called from one of two places:
1070 *
1071 * 1. we are flushing a delalloc buffer head.
1072 *
1073 * 2. we are writing out a dirty page. Typically the page dirty
1074 * state is cleared before we get here. In this case is it
1075 * conceivable we have no buffer heads.
1076 *
1077 * For delalloc space on the page we need to allocate space and
1078 * flush it. For unmapped buffer heads on the page we should
1079 * allocate space if the page is uptodate. For any other dirty
1080 * buffer heads on the page we should flush them.
1081 *
1082 * If we detect that a transaction would be required to flush
1083 * the page, we have to check the process flags first, if we
1084 * are already in a transaction or disk I/O during allocations
1085 * is off, we need to fail the writepage and redirty the page.
1086 */
1087
1088STATIC int
1089linvfs_writepage(
1090 struct page *page,
1091 struct writeback_control *wbc)
1092{
1093 int error;
1094 int need_trans;
1095 int delalloc, unmapped, unwritten;
1096 struct inode *inode = page->mapping->host;
1097
1098 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1099
1100 /*
1101 * We need a transaction if:
1102 * 1. There are delalloc buffers on the page
1103 * 2. The page is uptodate and we have unmapped buffers
1104 * 3. The page is uptodate and we have no buffers
1105 * 4. There are unwritten buffers on the page
1106 */
1107
1108 if (!page_has_buffers(page)) {
1109 unmapped = 1;
1110 need_trans = 1;
1111 } else {
1112 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1113 if (!PageUptodate(page))
1114 unmapped = 0;
1115 need_trans = delalloc + unmapped + unwritten;
1116 }
1117
1118 /*
1119 * If we need a transaction and the process flags say
1120 * we are already in a transaction, or no IO is allowed
1121 * then mark the page dirty again and leave the page
1122 * as is.
1123 */
1124 if (PFLAGS_TEST_FSTRANS() && need_trans)
1125 goto out_fail;
1126
1127 /*
1128 * Delay hooking up buffer heads until we have
1129 * made our go/no-go decision.
1130 */
1131 if (!page_has_buffers(page))
1132 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1133
1134 /*
1135 * Convert delayed allocate, unwritten or unmapped space
1136 * to real space and flush out to disk.
1137 */
1138 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1139 if (error == -EAGAIN)
1140 goto out_fail;
1141 if (unlikely(error < 0))
1142 goto out_unlock;
1143
1144 return 0;
1145
1146out_fail:
1147 redirty_page_for_writepage(wbc, page);
1148 unlock_page(page);
1149 return 0;
1150out_unlock:
1151 unlock_page(page);
1152 return error;
1153}
1154
1155/*
1156 * Called to move a page into cleanable state - and from there
1157 * to be released. Possibly the page is already clean. We always
1158 * have buffer heads in this call.
1159 *
1160 * Returns 0 if the page is ok to release, 1 otherwise.
1161 *
1162 * Possible scenarios are:
1163 *
1164 * 1. We are being called to release a page which has been written
1165 * to via regular I/O. buffer heads will be dirty and possibly
1166 * delalloc. If no delalloc buffer heads in this case then we
1167 * can just return zero.
1168 *
1169 * 2. We are called to release a page which has been written via
1170 * mmap, all we need to do is ensure there is no delalloc
1171 * state in the buffer heads, if not we can let the caller
1172 * free them and we should come back later via writepage.
1173 */
1174STATIC int
1175linvfs_release_page(
1176 struct page *page,
1177 gfp_t gfp_mask)
1178{
1179 struct inode *inode = page->mapping->host;
1180 int dirty, delalloc, unmapped, unwritten;
1181 struct writeback_control wbc = {
1182 .sync_mode = WB_SYNC_ALL,
1183 .nr_to_write = 1,
1184 };
1185
1186 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1187
1188 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1189 if (!delalloc && !unwritten)
1190 goto free_buffers;
1191
1192 if (!(gfp_mask & __GFP_FS))
1193 return 0;
1194
1195 /* If we are already inside a transaction or the thread cannot
1196 * do I/O, we cannot release this page.
1197 */
1198 if (PFLAGS_TEST_FSTRANS())
1199 return 0;
1200
1201 /*
1202 * Convert delalloc space to real space, do not flush the
1203 * data out to disk, that will be done by the caller.
1204 * Never need to allocate space here - we will always
1205 * come back to writepage in that case.
1206 */
1207 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1208 if (dirty == 0 && !unwritten)
1209 goto free_buffers;
1210 return 0;
1211
1212free_buffers:
1213 return try_to_free_buffers(page);
1214}
1215
1da177e4
LT
1216STATIC int
1217__linvfs_get_block(
1218 struct inode *inode,
1219 sector_t iblock,
1220 unsigned long blocks,
1221 struct buffer_head *bh_result,
1222 int create,
1223 int direct,
1224 bmapi_flags_t flags)
1225{
1226 vnode_t *vp = LINVFS_GET_VP(inode);
1227 xfs_iomap_t iomap;
fdc7ed75
NS
1228 xfs_off_t offset;
1229 ssize_t size;
1da177e4
LT
1230 int retpbbm = 1;
1231 int error;
1da177e4 1232
fdc7ed75 1233 offset = (xfs_off_t)iblock << inode->i_blkbits;
a4656391
NS
1234 if (blocks)
1235 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1236 (xfs_off_t)blocks << inode->i_blkbits);
1237 else
1238 size = 1 << inode->i_blkbits;
1da177e4
LT
1239
1240 VOP_BMAP(vp, offset, size,
1241 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1242 if (error)
1243 return -error;
1244
1245 if (retpbbm == 0)
1246 return 0;
1247
1248 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
fdc7ed75
NS
1249 xfs_daddr_t bn;
1250 xfs_off_t delta;
1da177e4
LT
1251
1252 /* For unwritten extents do not report a disk address on
1253 * the read case (treat as if we're reading into a hole).
1254 */
1255 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1256 delta = offset - iomap.iomap_offset;
1257 delta >>= inode->i_blkbits;
1258
1259 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1260 bn += delta;
1261 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1262 bh_result->b_blocknr = bn;
1263 set_buffer_mapped(bh_result);
1264 }
1265 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1266 if (direct)
1267 bh_result->b_private = inode;
1268 set_buffer_unwritten(bh_result);
1269 set_buffer_delay(bh_result);
1270 }
1271 }
1272
1273 /* If this is a realtime file, data might be on a new device */
ce8e922c 1274 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
1da177e4
LT
1275
1276 /* If we previously allocated a block out beyond eof and
1277 * we are now coming back to use it then we will need to
1278 * flag it as new even if it has a disk address.
1279 */
1280 if (create &&
1281 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
fdc7ed75 1282 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
1da177e4 1283 set_buffer_new(bh_result);
1da177e4
LT
1284
1285 if (iomap.iomap_flags & IOMAP_DELAY) {
1286 BUG_ON(direct);
1287 if (create) {
1288 set_buffer_uptodate(bh_result);
1289 set_buffer_mapped(bh_result);
1290 set_buffer_delay(bh_result);
1291 }
1292 }
1293
1294 if (blocks) {
fdc7ed75
NS
1295 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1296 offset = min_t(xfs_off_t,
1297 iomap.iomap_bsize - iomap.iomap_delta,
a4656391 1298 (xfs_off_t)blocks << inode->i_blkbits);
fdc7ed75 1299 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
1da177e4
LT
1300 }
1301
1302 return 0;
1303}
1304
1305int
1306linvfs_get_block(
1307 struct inode *inode,
1308 sector_t iblock,
1309 struct buffer_head *bh_result,
1310 int create)
1311{
1312 return __linvfs_get_block(inode, iblock, 0, bh_result,
1313 create, 0, BMAPI_WRITE);
1314}
1315
1316STATIC int
1317linvfs_get_blocks_direct(
1318 struct inode *inode,
1319 sector_t iblock,
1320 unsigned long max_blocks,
1321 struct buffer_head *bh_result,
1322 int create)
1323{
1324 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1325 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1326}
1327
f0973863
CH
1328STATIC void
1329linvfs_end_io_direct(
1330 struct kiocb *iocb,
1331 loff_t offset,
1332 ssize_t size,
1333 void *private)
1334{
1335 xfs_ioend_t *ioend = iocb->private;
1336
1337 /*
1338 * Non-NULL private data means we need to issue a transaction to
1339 * convert a range from unwritten to written extents. This needs
1340 * to happen from process contect but aio+dio I/O completion
1341 * happens from irq context so we need to defer it to a workqueue.
1342 * This is not nessecary for synchronous direct I/O, but we do
1343 * it anyway to keep the code uniform and simpler.
1344 *
1345 * The core direct I/O code might be changed to always call the
1346 * completion handler in the future, in which case all this can
1347 * go away.
1348 */
1349 if (private && size > 0) {
1350 ioend->io_offset = offset;
1351 ioend->io_size = size;
1352 xfs_finish_ioend(ioend);
1353 } else {
1354 ASSERT(size >= 0);
1355 xfs_destroy_ioend(ioend);
1356 }
1357
1358 /*
1359 * blockdev_direct_IO can return an error even afer the I/O
1360 * completion handler was called. Thus we need to protect
1361 * against double-freeing.
1362 */
1363 iocb->private = NULL;
1364}
1365
1da177e4
LT
1366STATIC ssize_t
1367linvfs_direct_IO(
1368 int rw,
1369 struct kiocb *iocb,
1370 const struct iovec *iov,
1371 loff_t offset,
1372 unsigned long nr_segs)
1373{
1374 struct file *file = iocb->ki_filp;
1375 struct inode *inode = file->f_mapping->host;
1376 vnode_t *vp = LINVFS_GET_VP(inode);
1377 xfs_iomap_t iomap;
1378 int maps = 1;
1379 int error;
f0973863 1380 ssize_t ret;
1da177e4
LT
1381
1382 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1383 if (error)
1384 return -error;
1385
f6d6d4fc 1386 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
f0973863
CH
1387
1388 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
ce8e922c 1389 iomap.iomap_target->bt_bdev,
1da177e4
LT
1390 iov, offset, nr_segs,
1391 linvfs_get_blocks_direct,
f0973863
CH
1392 linvfs_end_io_direct);
1393
1394 if (unlikely(ret <= 0 && iocb->private))
1395 xfs_destroy_ioend(iocb->private);
1396 return ret;
1da177e4
LT
1397}
1398
f51623b2
NS
1399STATIC int
1400linvfs_prepare_write(
1401 struct file *file,
1402 struct page *page,
1403 unsigned int from,
1404 unsigned int to)
1405{
1406 return block_prepare_write(page, from, to, linvfs_get_block);
1407}
1da177e4
LT
1408
1409STATIC sector_t
1410linvfs_bmap(
1411 struct address_space *mapping,
1412 sector_t block)
1413{
1414 struct inode *inode = (struct inode *)mapping->host;
1415 vnode_t *vp = LINVFS_GET_VP(inode);
1416 int error;
1417
1418 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1419
1420 VOP_RWLOCK(vp, VRWLOCK_READ);
1421 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1422 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1423 return generic_block_bmap(mapping, block, linvfs_get_block);
1424}
1425
1426STATIC int
1427linvfs_readpage(
1428 struct file *unused,
1429 struct page *page)
1430{
1431 return mpage_readpage(page, linvfs_get_block);
1432}
1433
1434STATIC int
1435linvfs_readpages(
1436 struct file *unused,
1437 struct address_space *mapping,
1438 struct list_head *pages,
1439 unsigned nr_pages)
1440{
1441 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1442}
1443
bcec2b7f
NS
1444STATIC int
1445linvfs_invalidate_page(
1446 struct page *page,
1447 unsigned long offset)
1448{
1449 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1450 page->mapping->host, page, offset);
1451 return block_invalidatepage(page, offset);
1452}
1453
1da177e4
LT
1454struct address_space_operations linvfs_aops = {
1455 .readpage = linvfs_readpage,
1456 .readpages = linvfs_readpages,
1457 .writepage = linvfs_writepage,
1458 .sync_page = block_sync_page,
1459 .releasepage = linvfs_release_page,
bcec2b7f 1460 .invalidatepage = linvfs_invalidate_page,
1da177e4
LT
1461 .prepare_write = linvfs_prepare_write,
1462 .commit_write = generic_commit_write,
1463 .bmap = linvfs_bmap,
1464 .direct_IO = linvfs_direct_IO,
e965f963 1465 .migratepage = buffer_migrate_page,
1da177e4 1466};