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