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