]> git.karo-electronics.de Git - mv-sheeva.git/blob - fs/xfs/xfs_aops.c
xfs: remove i_iocount
[mv-sheeva.git] / fs / xfs / 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_trans.h"
25 #include "xfs_mount.h"
26 #include "xfs_bmap_btree.h"
27 #include "xfs_dinode.h"
28 #include "xfs_inode.h"
29 #include "xfs_alloc.h"
30 #include "xfs_error.h"
31 #include "xfs_rw.h"
32 #include "xfs_iomap.h"
33 #include "xfs_vnodeops.h"
34 #include "xfs_trace.h"
35 #include "xfs_bmap.h"
36 #include <linux/gfp.h>
37 #include <linux/mpage.h>
38 #include <linux/pagevec.h>
39 #include <linux/writeback.h>
40
41 void
42 xfs_count_page_state(
43         struct page             *page,
44         int                     *delalloc,
45         int                     *unwritten)
46 {
47         struct buffer_head      *bh, *head;
48
49         *delalloc = *unwritten = 0;
50
51         bh = head = page_buffers(page);
52         do {
53                 if (buffer_unwritten(bh))
54                         (*unwritten) = 1;
55                 else if (buffer_delay(bh))
56                         (*delalloc) = 1;
57         } while ((bh = bh->b_this_page) != head);
58 }
59
60 STATIC struct block_device *
61 xfs_find_bdev_for_inode(
62         struct inode            *inode)
63 {
64         struct xfs_inode        *ip = XFS_I(inode);
65         struct xfs_mount        *mp = ip->i_mount;
66
67         if (XFS_IS_REALTIME_INODE(ip))
68                 return mp->m_rtdev_targp->bt_bdev;
69         else
70                 return mp->m_ddev_targp->bt_bdev;
71 }
72
73 /*
74  * We're now finished for good with this ioend structure.
75  * Update the page state via the associated buffer_heads,
76  * release holds on the inode and bio, and finally free
77  * up memory.  Do not use the ioend after this.
78  */
79 STATIC void
80 xfs_destroy_ioend(
81         xfs_ioend_t             *ioend)
82 {
83         struct buffer_head      *bh, *next;
84
85         for (bh = ioend->io_buffer_head; bh; bh = next) {
86                 next = bh->b_private;
87                 bh->b_end_io(bh, !ioend->io_error);
88         }
89
90         if (ioend->io_iocb) {
91                 if (ioend->io_isasync)
92                         aio_complete(ioend->io_iocb, ioend->io_result, 0);
93                 inode_dio_done(ioend->io_inode);
94         }
95
96         mempool_free(ioend, xfs_ioend_pool);
97 }
98
99 /*
100  * If the end of the current ioend is beyond the current EOF,
101  * return the new EOF value, otherwise zero.
102  */
103 STATIC xfs_fsize_t
104 xfs_ioend_new_eof(
105         xfs_ioend_t             *ioend)
106 {
107         xfs_inode_t             *ip = XFS_I(ioend->io_inode);
108         xfs_fsize_t             isize;
109         xfs_fsize_t             bsize;
110
111         bsize = ioend->io_offset + ioend->io_size;
112         isize = MAX(ip->i_size, ip->i_new_size);
113         isize = MIN(isize, bsize);
114         return isize > ip->i_d.di_size ? isize : 0;
115 }
116
117 /*
118  * Fast and loose check if this write could update the on-disk inode size.
119  */
120 static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
121 {
122         return ioend->io_offset + ioend->io_size >
123                 XFS_I(ioend->io_inode)->i_d.di_size;
124 }
125
126 /*
127  * Update on-disk file size now that data has been written to disk.  The
128  * current in-memory file size is i_size.  If a write is beyond eof i_new_size
129  * will be the intended file size until i_size is updated.  If this write does
130  * not extend all the way to the valid file size then restrict this update to
131  * the end of the write.
132  *
133  * This function does not block as blocking on the inode lock in IO completion
134  * can lead to IO completion order dependency deadlocks.. If it can't get the
135  * inode ilock it will return EAGAIN. Callers must handle this.
136  */
137 STATIC int
138 xfs_setfilesize(
139         xfs_ioend_t             *ioend)
140 {
141         xfs_inode_t             *ip = XFS_I(ioend->io_inode);
142         xfs_fsize_t             isize;
143
144         if (unlikely(ioend->io_error))
145                 return 0;
146
147         if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
148                 return EAGAIN;
149
150         isize = xfs_ioend_new_eof(ioend);
151         if (isize) {
152                 trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
153                 ip->i_d.di_size = isize;
154                 xfs_mark_inode_dirty(ip);
155         }
156
157         xfs_iunlock(ip, XFS_ILOCK_EXCL);
158         return 0;
159 }
160
161 /*
162  * Schedule IO completion handling on the final put of an ioend.
163  *
164  * If there is no work to do we might as well call it a day and free the
165  * ioend right now.
166  */
167 STATIC void
168 xfs_finish_ioend(
169         struct xfs_ioend        *ioend)
170 {
171         if (atomic_dec_and_test(&ioend->io_remaining)) {
172                 if (ioend->io_type == IO_UNWRITTEN)
173                         queue_work(xfsconvertd_workqueue, &ioend->io_work);
174                 else if (xfs_ioend_is_append(ioend))
175                         queue_work(xfsdatad_workqueue, &ioend->io_work);
176                 else
177                         xfs_destroy_ioend(ioend);
178         }
179 }
180
181 /*
182  * IO write completion.
183  */
184 STATIC void
185 xfs_end_io(
186         struct work_struct *work)
187 {
188         xfs_ioend_t     *ioend = container_of(work, xfs_ioend_t, io_work);
189         struct xfs_inode *ip = XFS_I(ioend->io_inode);
190         int             error = 0;
191
192         /*
193          * For unwritten extents we need to issue transactions to convert a
194          * range to normal written extens after the data I/O has finished.
195          */
196         if (ioend->io_type == IO_UNWRITTEN &&
197             likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
198
199                 error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
200                                                  ioend->io_size);
201                 if (error)
202                         ioend->io_error = error;
203         }
204
205         /*
206          * We might have to update the on-disk file size after extending
207          * writes.
208          */
209         error = xfs_setfilesize(ioend);
210         ASSERT(!error || error == EAGAIN);
211
212         /*
213          * If we didn't complete processing of the ioend, requeue it to the
214          * tail of the workqueue for another attempt later. Otherwise destroy
215          * it.
216          */
217         if (error == EAGAIN) {
218                 atomic_inc(&ioend->io_remaining);
219                 xfs_finish_ioend(ioend);
220                 /* ensure we don't spin on blocked ioends */
221                 delay(1);
222         } else {
223                 xfs_destroy_ioend(ioend);
224         }
225 }
226
227 /*
228  * Call IO completion handling in caller context on the final put of an ioend.
229  */
230 STATIC void
231 xfs_finish_ioend_sync(
232         struct xfs_ioend        *ioend)
233 {
234         if (atomic_dec_and_test(&ioend->io_remaining))
235                 xfs_end_io(&ioend->io_work);
236 }
237
238 /*
239  * Allocate and initialise an IO completion structure.
240  * We need to track unwritten extent write completion here initially.
241  * We'll need to extend this for updating the ondisk inode size later
242  * (vs. incore size).
243  */
244 STATIC xfs_ioend_t *
245 xfs_alloc_ioend(
246         struct inode            *inode,
247         unsigned int            type)
248 {
249         xfs_ioend_t             *ioend;
250
251         ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
252
253         /*
254          * Set the count to 1 initially, which will prevent an I/O
255          * completion callback from happening before we have started
256          * all the I/O from calling the completion routine too early.
257          */
258         atomic_set(&ioend->io_remaining, 1);
259         ioend->io_isasync = 0;
260         ioend->io_error = 0;
261         ioend->io_list = NULL;
262         ioend->io_type = type;
263         ioend->io_inode = inode;
264         ioend->io_buffer_head = NULL;
265         ioend->io_buffer_tail = NULL;
266         ioend->io_offset = 0;
267         ioend->io_size = 0;
268         ioend->io_iocb = NULL;
269         ioend->io_result = 0;
270
271         INIT_WORK(&ioend->io_work, xfs_end_io);
272         return ioend;
273 }
274
275 STATIC int
276 xfs_map_blocks(
277         struct inode            *inode,
278         loff_t                  offset,
279         struct xfs_bmbt_irec    *imap,
280         int                     type,
281         int                     nonblocking)
282 {
283         struct xfs_inode        *ip = XFS_I(inode);
284         struct xfs_mount        *mp = ip->i_mount;
285         ssize_t                 count = 1 << inode->i_blkbits;
286         xfs_fileoff_t           offset_fsb, end_fsb;
287         int                     error = 0;
288         int                     bmapi_flags = XFS_BMAPI_ENTIRE;
289         int                     nimaps = 1;
290
291         if (XFS_FORCED_SHUTDOWN(mp))
292                 return -XFS_ERROR(EIO);
293
294         if (type == IO_UNWRITTEN)
295                 bmapi_flags |= XFS_BMAPI_IGSTATE;
296
297         if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
298                 if (nonblocking)
299                         return -XFS_ERROR(EAGAIN);
300                 xfs_ilock(ip, XFS_ILOCK_SHARED);
301         }
302
303         ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
304                (ip->i_df.if_flags & XFS_IFEXTENTS));
305         ASSERT(offset <= mp->m_maxioffset);
306
307         if (offset + count > mp->m_maxioffset)
308                 count = mp->m_maxioffset - offset;
309         end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
310         offset_fsb = XFS_B_TO_FSBT(mp, offset);
311         error = xfs_bmapi(NULL, ip, offset_fsb, end_fsb - offset_fsb,
312                           bmapi_flags,  NULL, 0, imap, &nimaps, NULL);
313         xfs_iunlock(ip, XFS_ILOCK_SHARED);
314
315         if (error)
316                 return -XFS_ERROR(error);
317
318         if (type == IO_DELALLOC &&
319             (!nimaps || isnullstartblock(imap->br_startblock))) {
320                 error = xfs_iomap_write_allocate(ip, offset, count, imap);
321                 if (!error)
322                         trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
323                 return -XFS_ERROR(error);
324         }
325
326 #ifdef DEBUG
327         if (type == IO_UNWRITTEN) {
328                 ASSERT(nimaps);
329                 ASSERT(imap->br_startblock != HOLESTARTBLOCK);
330                 ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
331         }
332 #endif
333         if (nimaps)
334                 trace_xfs_map_blocks_found(ip, offset, count, type, imap);
335         return 0;
336 }
337
338 STATIC int
339 xfs_imap_valid(
340         struct inode            *inode,
341         struct xfs_bmbt_irec    *imap,
342         xfs_off_t               offset)
343 {
344         offset >>= inode->i_blkbits;
345
346         return offset >= imap->br_startoff &&
347                 offset < imap->br_startoff + imap->br_blockcount;
348 }
349
350 /*
351  * BIO completion handler for buffered IO.
352  */
353 STATIC void
354 xfs_end_bio(
355         struct bio              *bio,
356         int                     error)
357 {
358         xfs_ioend_t             *ioend = bio->bi_private;
359
360         ASSERT(atomic_read(&bio->bi_cnt) >= 1);
361         ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
362
363         /* Toss bio and pass work off to an xfsdatad thread */
364         bio->bi_private = NULL;
365         bio->bi_end_io = NULL;
366         bio_put(bio);
367
368         xfs_finish_ioend(ioend);
369 }
370
371 STATIC void
372 xfs_submit_ioend_bio(
373         struct writeback_control *wbc,
374         xfs_ioend_t             *ioend,
375         struct bio              *bio)
376 {
377         atomic_inc(&ioend->io_remaining);
378         bio->bi_private = ioend;
379         bio->bi_end_io = xfs_end_bio;
380
381         /*
382          * If the I/O is beyond EOF we mark the inode dirty immediately
383          * but don't update the inode size until I/O completion.
384          */
385         if (xfs_ioend_new_eof(ioend))
386                 xfs_mark_inode_dirty(XFS_I(ioend->io_inode));
387
388         submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
389 }
390
391 STATIC struct bio *
392 xfs_alloc_ioend_bio(
393         struct buffer_head      *bh)
394 {
395         int                     nvecs = bio_get_nr_vecs(bh->b_bdev);
396         struct bio              *bio = bio_alloc(GFP_NOIO, nvecs);
397
398         ASSERT(bio->bi_private == NULL);
399         bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
400         bio->bi_bdev = bh->b_bdev;
401         return bio;
402 }
403
404 STATIC void
405 xfs_start_buffer_writeback(
406         struct buffer_head      *bh)
407 {
408         ASSERT(buffer_mapped(bh));
409         ASSERT(buffer_locked(bh));
410         ASSERT(!buffer_delay(bh));
411         ASSERT(!buffer_unwritten(bh));
412
413         mark_buffer_async_write(bh);
414         set_buffer_uptodate(bh);
415         clear_buffer_dirty(bh);
416 }
417
418 STATIC void
419 xfs_start_page_writeback(
420         struct page             *page,
421         int                     clear_dirty,
422         int                     buffers)
423 {
424         ASSERT(PageLocked(page));
425         ASSERT(!PageWriteback(page));
426         if (clear_dirty)
427                 clear_page_dirty_for_io(page);
428         set_page_writeback(page);
429         unlock_page(page);
430         /* If no buffers on the page are to be written, finish it here */
431         if (!buffers)
432                 end_page_writeback(page);
433 }
434
435 static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
436 {
437         return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
438 }
439
440 /*
441  * Submit all of the bios for all of the ioends we have saved up, covering the
442  * initial writepage page and also any probed pages.
443  *
444  * Because we may have multiple ioends spanning a page, we need to start
445  * writeback on all the buffers before we submit them for I/O. If we mark the
446  * buffers as we got, then we can end up with a page that only has buffers
447  * marked async write and I/O complete on can occur before we mark the other
448  * buffers async write.
449  *
450  * The end result of this is that we trip a bug in end_page_writeback() because
451  * we call it twice for the one page as the code in end_buffer_async_write()
452  * assumes that all buffers on the page are started at the same time.
453  *
454  * The fix is two passes across the ioend list - one to start writeback on the
455  * buffer_heads, and then submit them for I/O on the second pass.
456  */
457 STATIC void
458 xfs_submit_ioend(
459         struct writeback_control *wbc,
460         xfs_ioend_t             *ioend)
461 {
462         xfs_ioend_t             *head = ioend;
463         xfs_ioend_t             *next;
464         struct buffer_head      *bh;
465         struct bio              *bio;
466         sector_t                lastblock = 0;
467
468         /* Pass 1 - start writeback */
469         do {
470                 next = ioend->io_list;
471                 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
472                         xfs_start_buffer_writeback(bh);
473         } while ((ioend = next) != NULL);
474
475         /* Pass 2 - submit I/O */
476         ioend = head;
477         do {
478                 next = ioend->io_list;
479                 bio = NULL;
480
481                 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
482
483                         if (!bio) {
484  retry:
485                                 bio = xfs_alloc_ioend_bio(bh);
486                         } else if (bh->b_blocknr != lastblock + 1) {
487                                 xfs_submit_ioend_bio(wbc, ioend, bio);
488                                 goto retry;
489                         }
490
491                         if (bio_add_buffer(bio, bh) != bh->b_size) {
492                                 xfs_submit_ioend_bio(wbc, ioend, bio);
493                                 goto retry;
494                         }
495
496                         lastblock = bh->b_blocknr;
497                 }
498                 if (bio)
499                         xfs_submit_ioend_bio(wbc, ioend, bio);
500                 xfs_finish_ioend(ioend);
501         } while ((ioend = next) != NULL);
502 }
503
504 /*
505  * Cancel submission of all buffer_heads so far in this endio.
506  * Toss the endio too.  Only ever called for the initial page
507  * in a writepage request, so only ever one page.
508  */
509 STATIC void
510 xfs_cancel_ioend(
511         xfs_ioend_t             *ioend)
512 {
513         xfs_ioend_t             *next;
514         struct buffer_head      *bh, *next_bh;
515
516         do {
517                 next = ioend->io_list;
518                 bh = ioend->io_buffer_head;
519                 do {
520                         next_bh = bh->b_private;
521                         clear_buffer_async_write(bh);
522                         unlock_buffer(bh);
523                 } while ((bh = next_bh) != NULL);
524
525                 mempool_free(ioend, xfs_ioend_pool);
526         } while ((ioend = next) != NULL);
527 }
528
529 /*
530  * Test to see if we've been building up a completion structure for
531  * earlier buffers -- if so, we try to append to this ioend if we
532  * can, otherwise we finish off any current ioend and start another.
533  * Return true if we've finished the given ioend.
534  */
535 STATIC void
536 xfs_add_to_ioend(
537         struct inode            *inode,
538         struct buffer_head      *bh,
539         xfs_off_t               offset,
540         unsigned int            type,
541         xfs_ioend_t             **result,
542         int                     need_ioend)
543 {
544         xfs_ioend_t             *ioend = *result;
545
546         if (!ioend || need_ioend || type != ioend->io_type) {
547                 xfs_ioend_t     *previous = *result;
548
549                 ioend = xfs_alloc_ioend(inode, type);
550                 ioend->io_offset = offset;
551                 ioend->io_buffer_head = bh;
552                 ioend->io_buffer_tail = bh;
553                 if (previous)
554                         previous->io_list = ioend;
555                 *result = ioend;
556         } else {
557                 ioend->io_buffer_tail->b_private = bh;
558                 ioend->io_buffer_tail = bh;
559         }
560
561         bh->b_private = NULL;
562         ioend->io_size += bh->b_size;
563 }
564
565 STATIC void
566 xfs_map_buffer(
567         struct inode            *inode,
568         struct buffer_head      *bh,
569         struct xfs_bmbt_irec    *imap,
570         xfs_off_t               offset)
571 {
572         sector_t                bn;
573         struct xfs_mount        *m = XFS_I(inode)->i_mount;
574         xfs_off_t               iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
575         xfs_daddr_t             iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
576
577         ASSERT(imap->br_startblock != HOLESTARTBLOCK);
578         ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
579
580         bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
581               ((offset - iomap_offset) >> inode->i_blkbits);
582
583         ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
584
585         bh->b_blocknr = bn;
586         set_buffer_mapped(bh);
587 }
588
589 STATIC void
590 xfs_map_at_offset(
591         struct inode            *inode,
592         struct buffer_head      *bh,
593         struct xfs_bmbt_irec    *imap,
594         xfs_off_t               offset)
595 {
596         ASSERT(imap->br_startblock != HOLESTARTBLOCK);
597         ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
598
599         xfs_map_buffer(inode, bh, imap, offset);
600         set_buffer_mapped(bh);
601         clear_buffer_delay(bh);
602         clear_buffer_unwritten(bh);
603 }
604
605 /*
606  * Test if a given page is suitable for writing as part of an unwritten
607  * or delayed allocate extent.
608  */
609 STATIC int
610 xfs_is_delayed_page(
611         struct page             *page,
612         unsigned int            type)
613 {
614         if (PageWriteback(page))
615                 return 0;
616
617         if (page->mapping && page_has_buffers(page)) {
618                 struct buffer_head      *bh, *head;
619                 int                     acceptable = 0;
620
621                 bh = head = page_buffers(page);
622                 do {
623                         if (buffer_unwritten(bh))
624                                 acceptable = (type == IO_UNWRITTEN);
625                         else if (buffer_delay(bh))
626                                 acceptable = (type == IO_DELALLOC);
627                         else if (buffer_dirty(bh) && buffer_mapped(bh))
628                                 acceptable = (type == IO_OVERWRITE);
629                         else
630                                 break;
631                 } while ((bh = bh->b_this_page) != head);
632
633                 if (acceptable)
634                         return 1;
635         }
636
637         return 0;
638 }
639
640 /*
641  * Allocate & map buffers for page given the extent map. Write it out.
642  * except for the original page of a writepage, this is called on
643  * delalloc/unwritten pages only, for the original page it is possible
644  * that the page has no mapping at all.
645  */
646 STATIC int
647 xfs_convert_page(
648         struct inode            *inode,
649         struct page             *page,
650         loff_t                  tindex,
651         struct xfs_bmbt_irec    *imap,
652         xfs_ioend_t             **ioendp,
653         struct writeback_control *wbc)
654 {
655         struct buffer_head      *bh, *head;
656         xfs_off_t               end_offset;
657         unsigned long           p_offset;
658         unsigned int            type;
659         int                     len, page_dirty;
660         int                     count = 0, done = 0, uptodate = 1;
661         xfs_off_t               offset = page_offset(page);
662
663         if (page->index != tindex)
664                 goto fail;
665         if (!trylock_page(page))
666                 goto fail;
667         if (PageWriteback(page))
668                 goto fail_unlock_page;
669         if (page->mapping != inode->i_mapping)
670                 goto fail_unlock_page;
671         if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
672                 goto fail_unlock_page;
673
674         /*
675          * page_dirty is initially a count of buffers on the page before
676          * EOF and is decremented as we move each into a cleanable state.
677          *
678          * Derivation:
679          *
680          * End offset is the highest offset that this page should represent.
681          * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
682          * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
683          * hence give us the correct page_dirty count. On any other page,
684          * it will be zero and in that case we need page_dirty to be the
685          * count of buffers on the page.
686          */
687         end_offset = min_t(unsigned long long,
688                         (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
689                         i_size_read(inode));
690
691         len = 1 << inode->i_blkbits;
692         p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
693                                         PAGE_CACHE_SIZE);
694         p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
695         page_dirty = p_offset / len;
696
697         bh = head = page_buffers(page);
698         do {
699                 if (offset >= end_offset)
700                         break;
701                 if (!buffer_uptodate(bh))
702                         uptodate = 0;
703                 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
704                         done = 1;
705                         continue;
706                 }
707
708                 if (buffer_unwritten(bh) || buffer_delay(bh) ||
709                     buffer_mapped(bh)) {
710                         if (buffer_unwritten(bh))
711                                 type = IO_UNWRITTEN;
712                         else if (buffer_delay(bh))
713                                 type = IO_DELALLOC;
714                         else
715                                 type = IO_OVERWRITE;
716
717                         if (!xfs_imap_valid(inode, imap, offset)) {
718                                 done = 1;
719                                 continue;
720                         }
721
722                         lock_buffer(bh);
723                         if (type != IO_OVERWRITE)
724                                 xfs_map_at_offset(inode, bh, imap, offset);
725                         xfs_add_to_ioend(inode, bh, offset, type,
726                                          ioendp, done);
727
728                         page_dirty--;
729                         count++;
730                 } else {
731                         done = 1;
732                 }
733         } while (offset += len, (bh = bh->b_this_page) != head);
734
735         if (uptodate && bh == head)
736                 SetPageUptodate(page);
737
738         if (count) {
739                 if (--wbc->nr_to_write <= 0 &&
740                     wbc->sync_mode == WB_SYNC_NONE)
741                         done = 1;
742         }
743         xfs_start_page_writeback(page, !page_dirty, count);
744
745         return done;
746  fail_unlock_page:
747         unlock_page(page);
748  fail:
749         return 1;
750 }
751
752 /*
753  * Convert & write out a cluster of pages in the same extent as defined
754  * by mp and following the start page.
755  */
756 STATIC void
757 xfs_cluster_write(
758         struct inode            *inode,
759         pgoff_t                 tindex,
760         struct xfs_bmbt_irec    *imap,
761         xfs_ioend_t             **ioendp,
762         struct writeback_control *wbc,
763         pgoff_t                 tlast)
764 {
765         struct pagevec          pvec;
766         int                     done = 0, i;
767
768         pagevec_init(&pvec, 0);
769         while (!done && tindex <= tlast) {
770                 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
771
772                 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
773                         break;
774
775                 for (i = 0; i < pagevec_count(&pvec); i++) {
776                         done = xfs_convert_page(inode, pvec.pages[i], tindex++,
777                                         imap, ioendp, wbc);
778                         if (done)
779                                 break;
780                 }
781
782                 pagevec_release(&pvec);
783                 cond_resched();
784         }
785 }
786
787 STATIC void
788 xfs_vm_invalidatepage(
789         struct page             *page,
790         unsigned long           offset)
791 {
792         trace_xfs_invalidatepage(page->mapping->host, page, offset);
793         block_invalidatepage(page, offset);
794 }
795
796 /*
797  * If the page has delalloc buffers on it, we need to punch them out before we
798  * invalidate the page. If we don't, we leave a stale delalloc mapping on the
799  * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
800  * is done on that same region - the delalloc extent is returned when none is
801  * supposed to be there.
802  *
803  * We prevent this by truncating away the delalloc regions on the page before
804  * invalidating it. Because they are delalloc, we can do this without needing a
805  * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
806  * truncation without a transaction as there is no space left for block
807  * reservation (typically why we see a ENOSPC in writeback).
808  *
809  * This is not a performance critical path, so for now just do the punching a
810  * buffer head at a time.
811  */
812 STATIC void
813 xfs_aops_discard_page(
814         struct page             *page)
815 {
816         struct inode            *inode = page->mapping->host;
817         struct xfs_inode        *ip = XFS_I(inode);
818         struct buffer_head      *bh, *head;
819         loff_t                  offset = page_offset(page);
820
821         if (!xfs_is_delayed_page(page, IO_DELALLOC))
822                 goto out_invalidate;
823
824         if (XFS_FORCED_SHUTDOWN(ip->i_mount))
825                 goto out_invalidate;
826
827         xfs_alert(ip->i_mount,
828                 "page discard on page %p, inode 0x%llx, offset %llu.",
829                         page, ip->i_ino, offset);
830
831         xfs_ilock(ip, XFS_ILOCK_EXCL);
832         bh = head = page_buffers(page);
833         do {
834                 int             error;
835                 xfs_fileoff_t   start_fsb;
836
837                 if (!buffer_delay(bh))
838                         goto next_buffer;
839
840                 start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
841                 error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
842                 if (error) {
843                         /* something screwed, just bail */
844                         if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
845                                 xfs_alert(ip->i_mount,
846                         "page discard unable to remove delalloc mapping.");
847                         }
848                         break;
849                 }
850 next_buffer:
851                 offset += 1 << inode->i_blkbits;
852
853         } while ((bh = bh->b_this_page) != head);
854
855         xfs_iunlock(ip, XFS_ILOCK_EXCL);
856 out_invalidate:
857         xfs_vm_invalidatepage(page, 0);
858         return;
859 }
860
861 /*
862  * Write out a dirty page.
863  *
864  * For delalloc space on the page we need to allocate space and flush it.
865  * For unwritten space on the page we need to start the conversion to
866  * regular allocated space.
867  * For any other dirty buffer heads on the page we should flush them.
868  */
869 STATIC int
870 xfs_vm_writepage(
871         struct page             *page,
872         struct writeback_control *wbc)
873 {
874         struct inode            *inode = page->mapping->host;
875         struct buffer_head      *bh, *head;
876         struct xfs_bmbt_irec    imap;
877         xfs_ioend_t             *ioend = NULL, *iohead = NULL;
878         loff_t                  offset;
879         unsigned int            type;
880         __uint64_t              end_offset;
881         pgoff_t                 end_index, last_index;
882         ssize_t                 len;
883         int                     err, imap_valid = 0, uptodate = 1;
884         int                     count = 0;
885         int                     nonblocking = 0;
886
887         trace_xfs_writepage(inode, page, 0);
888
889         ASSERT(page_has_buffers(page));
890
891         /*
892          * Refuse to write the page out if we are called from reclaim context.
893          *
894          * This avoids stack overflows when called from deeply used stacks in
895          * random callers for direct reclaim or memcg reclaim.  We explicitly
896          * allow reclaim from kswapd as the stack usage there is relatively low.
897          *
898          * This should really be done by the core VM, but until that happens
899          * filesystems like XFS, btrfs and ext4 have to take care of this
900          * by themselves.
901          */
902         if ((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC)
903                 goto redirty;
904
905         /*
906          * Given that we do not allow direct reclaim to call us, we should
907          * never be called while in a filesystem transaction.
908          */
909         if (WARN_ON(current->flags & PF_FSTRANS))
910                 goto redirty;
911
912         /* Is this page beyond the end of the file? */
913         offset = i_size_read(inode);
914         end_index = offset >> PAGE_CACHE_SHIFT;
915         last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
916         if (page->index >= end_index) {
917                 if ((page->index >= end_index + 1) ||
918                     !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
919                         unlock_page(page);
920                         return 0;
921                 }
922         }
923
924         end_offset = min_t(unsigned long long,
925                         (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
926                         offset);
927         len = 1 << inode->i_blkbits;
928
929         bh = head = page_buffers(page);
930         offset = page_offset(page);
931         type = IO_OVERWRITE;
932
933         if (wbc->sync_mode == WB_SYNC_NONE)
934                 nonblocking = 1;
935
936         do {
937                 int new_ioend = 0;
938
939                 if (offset >= end_offset)
940                         break;
941                 if (!buffer_uptodate(bh))
942                         uptodate = 0;
943
944                 /*
945                  * set_page_dirty dirties all buffers in a page, independent
946                  * of their state.  The dirty state however is entirely
947                  * meaningless for holes (!mapped && uptodate), so skip
948                  * buffers covering holes here.
949                  */
950                 if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
951                         imap_valid = 0;
952                         continue;
953                 }
954
955                 if (buffer_unwritten(bh)) {
956                         if (type != IO_UNWRITTEN) {
957                                 type = IO_UNWRITTEN;
958                                 imap_valid = 0;
959                         }
960                 } else if (buffer_delay(bh)) {
961                         if (type != IO_DELALLOC) {
962                                 type = IO_DELALLOC;
963                                 imap_valid = 0;
964                         }
965                 } else if (buffer_uptodate(bh)) {
966                         if (type != IO_OVERWRITE) {
967                                 type = IO_OVERWRITE;
968                                 imap_valid = 0;
969                         }
970                 } else {
971                         if (PageUptodate(page)) {
972                                 ASSERT(buffer_mapped(bh));
973                                 imap_valid = 0;
974                         }
975                         continue;
976                 }
977
978                 if (imap_valid)
979                         imap_valid = xfs_imap_valid(inode, &imap, offset);
980                 if (!imap_valid) {
981                         /*
982                          * If we didn't have a valid mapping then we need to
983                          * put the new mapping into a separate ioend structure.
984                          * This ensures non-contiguous extents always have
985                          * separate ioends, which is particularly important
986                          * for unwritten extent conversion at I/O completion
987                          * time.
988                          */
989                         new_ioend = 1;
990                         err = xfs_map_blocks(inode, offset, &imap, type,
991                                              nonblocking);
992                         if (err)
993                                 goto error;
994                         imap_valid = xfs_imap_valid(inode, &imap, offset);
995                 }
996                 if (imap_valid) {
997                         lock_buffer(bh);
998                         if (type != IO_OVERWRITE)
999                                 xfs_map_at_offset(inode, bh, &imap, offset);
1000                         xfs_add_to_ioend(inode, bh, offset, type, &ioend,
1001                                          new_ioend);
1002                         count++;
1003                 }
1004
1005                 if (!iohead)
1006                         iohead = ioend;
1007
1008         } while (offset += len, ((bh = bh->b_this_page) != head));
1009
1010         if (uptodate && bh == head)
1011                 SetPageUptodate(page);
1012
1013         xfs_start_page_writeback(page, 1, count);
1014
1015         if (ioend && imap_valid) {
1016                 xfs_off_t               end_index;
1017
1018                 end_index = imap.br_startoff + imap.br_blockcount;
1019
1020                 /* to bytes */
1021                 end_index <<= inode->i_blkbits;
1022
1023                 /* to pages */
1024                 end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
1025
1026                 /* check against file size */
1027                 if (end_index > last_index)
1028                         end_index = last_index;
1029
1030                 xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
1031                                   wbc, end_index);
1032         }
1033
1034         if (iohead)
1035                 xfs_submit_ioend(wbc, iohead);
1036
1037         return 0;
1038
1039 error:
1040         if (iohead)
1041                 xfs_cancel_ioend(iohead);
1042
1043         if (err == -EAGAIN)
1044                 goto redirty;
1045
1046         xfs_aops_discard_page(page);
1047         ClearPageUptodate(page);
1048         unlock_page(page);
1049         return err;
1050
1051 redirty:
1052         redirty_page_for_writepage(wbc, page);
1053         unlock_page(page);
1054         return 0;
1055 }
1056
1057 STATIC int
1058 xfs_vm_writepages(
1059         struct address_space    *mapping,
1060         struct writeback_control *wbc)
1061 {
1062         xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
1063         return generic_writepages(mapping, wbc);
1064 }
1065
1066 /*
1067  * Called to move a page into cleanable state - and from there
1068  * to be released. The page should already be clean. We always
1069  * have buffer heads in this call.
1070  *
1071  * Returns 1 if the page is ok to release, 0 otherwise.
1072  */
1073 STATIC int
1074 xfs_vm_releasepage(
1075         struct page             *page,
1076         gfp_t                   gfp_mask)
1077 {
1078         int                     delalloc, unwritten;
1079
1080         trace_xfs_releasepage(page->mapping->host, page, 0);
1081
1082         xfs_count_page_state(page, &delalloc, &unwritten);
1083
1084         if (WARN_ON(delalloc))
1085                 return 0;
1086         if (WARN_ON(unwritten))
1087                 return 0;
1088
1089         return try_to_free_buffers(page);
1090 }
1091
1092 STATIC int
1093 __xfs_get_blocks(
1094         struct inode            *inode,
1095         sector_t                iblock,
1096         struct buffer_head      *bh_result,
1097         int                     create,
1098         int                     direct)
1099 {
1100         struct xfs_inode        *ip = XFS_I(inode);
1101         struct xfs_mount        *mp = ip->i_mount;
1102         xfs_fileoff_t           offset_fsb, end_fsb;
1103         int                     error = 0;
1104         int                     lockmode = 0;
1105         struct xfs_bmbt_irec    imap;
1106         int                     nimaps = 1;
1107         xfs_off_t               offset;
1108         ssize_t                 size;
1109         int                     new = 0;
1110
1111         if (XFS_FORCED_SHUTDOWN(mp))
1112                 return -XFS_ERROR(EIO);
1113
1114         offset = (xfs_off_t)iblock << inode->i_blkbits;
1115         ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
1116         size = bh_result->b_size;
1117
1118         if (!create && direct && offset >= i_size_read(inode))
1119                 return 0;
1120
1121         if (create) {
1122                 lockmode = XFS_ILOCK_EXCL;
1123                 xfs_ilock(ip, lockmode);
1124         } else {
1125                 lockmode = xfs_ilock_map_shared(ip);
1126         }
1127
1128         ASSERT(offset <= mp->m_maxioffset);
1129         if (offset + size > mp->m_maxioffset)
1130                 size = mp->m_maxioffset - offset;
1131         end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
1132         offset_fsb = XFS_B_TO_FSBT(mp, offset);
1133
1134         error = xfs_bmapi(NULL, ip, offset_fsb, end_fsb - offset_fsb,
1135                           XFS_BMAPI_ENTIRE,  NULL, 0, &imap, &nimaps, NULL);
1136         if (error)
1137                 goto out_unlock;
1138
1139         if (create &&
1140             (!nimaps ||
1141              (imap.br_startblock == HOLESTARTBLOCK ||
1142               imap.br_startblock == DELAYSTARTBLOCK))) {
1143                 if (direct) {
1144                         error = xfs_iomap_write_direct(ip, offset, size,
1145                                                        &imap, nimaps);
1146                 } else {
1147                         error = xfs_iomap_write_delay(ip, offset, size, &imap);
1148                 }
1149                 if (error)
1150                         goto out_unlock;
1151
1152                 trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
1153         } else if (nimaps) {
1154                 trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
1155         } else {
1156                 trace_xfs_get_blocks_notfound(ip, offset, size);
1157                 goto out_unlock;
1158         }
1159         xfs_iunlock(ip, lockmode);
1160
1161         if (imap.br_startblock != HOLESTARTBLOCK &&
1162             imap.br_startblock != DELAYSTARTBLOCK) {
1163                 /*
1164                  * For unwritten extents do not report a disk address on
1165                  * the read case (treat as if we're reading into a hole).
1166                  */
1167                 if (create || !ISUNWRITTEN(&imap))
1168                         xfs_map_buffer(inode, bh_result, &imap, offset);
1169                 if (create && ISUNWRITTEN(&imap)) {
1170                         if (direct)
1171                                 bh_result->b_private = inode;
1172                         set_buffer_unwritten(bh_result);
1173                 }
1174         }
1175
1176         /*
1177          * If this is a realtime file, data may be on a different device.
1178          * to that pointed to from the buffer_head b_bdev currently.
1179          */
1180         bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
1181
1182         /*
1183          * If we previously allocated a block out beyond eof and we are now
1184          * coming back to use it then we will need to flag it as new even if it
1185          * has a disk address.
1186          *
1187          * With sub-block writes into unwritten extents we also need to mark
1188          * the buffer as new so that the unwritten parts of the buffer gets
1189          * correctly zeroed.
1190          */
1191         if (create &&
1192             ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
1193              (offset >= i_size_read(inode)) ||
1194              (new || ISUNWRITTEN(&imap))))
1195                 set_buffer_new(bh_result);
1196
1197         if (imap.br_startblock == DELAYSTARTBLOCK) {
1198                 BUG_ON(direct);
1199                 if (create) {
1200                         set_buffer_uptodate(bh_result);
1201                         set_buffer_mapped(bh_result);
1202                         set_buffer_delay(bh_result);
1203                 }
1204         }
1205
1206         /*
1207          * If this is O_DIRECT or the mpage code calling tell them how large
1208          * the mapping is, so that we can avoid repeated get_blocks calls.
1209          */
1210         if (direct || size > (1 << inode->i_blkbits)) {
1211                 xfs_off_t               mapping_size;
1212
1213                 mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
1214                 mapping_size <<= inode->i_blkbits;
1215
1216                 ASSERT(mapping_size > 0);
1217                 if (mapping_size > size)
1218                         mapping_size = size;
1219                 if (mapping_size > LONG_MAX)
1220                         mapping_size = LONG_MAX;
1221
1222                 bh_result->b_size = mapping_size;
1223         }
1224
1225         return 0;
1226
1227 out_unlock:
1228         xfs_iunlock(ip, lockmode);
1229         return -error;
1230 }
1231
1232 int
1233 xfs_get_blocks(
1234         struct inode            *inode,
1235         sector_t                iblock,
1236         struct buffer_head      *bh_result,
1237         int                     create)
1238 {
1239         return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
1240 }
1241
1242 STATIC int
1243 xfs_get_blocks_direct(
1244         struct inode            *inode,
1245         sector_t                iblock,
1246         struct buffer_head      *bh_result,
1247         int                     create)
1248 {
1249         return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
1250 }
1251
1252 /*
1253  * Complete a direct I/O write request.
1254  *
1255  * If the private argument is non-NULL __xfs_get_blocks signals us that we
1256  * need to issue a transaction to convert the range from unwritten to written
1257  * extents.  In case this is regular synchronous I/O we just call xfs_end_io
1258  * to do this and we are done.  But in case this was a successful AIO
1259  * request this handler is called from interrupt context, from which we
1260  * can't start transactions.  In that case offload the I/O completion to
1261  * the workqueues we also use for buffered I/O completion.
1262  */
1263 STATIC void
1264 xfs_end_io_direct_write(
1265         struct kiocb            *iocb,
1266         loff_t                  offset,
1267         ssize_t                 size,
1268         void                    *private,
1269         int                     ret,
1270         bool                    is_async)
1271 {
1272         struct xfs_ioend        *ioend = iocb->private;
1273
1274         /*
1275          * blockdev_direct_IO can return an error even after the I/O
1276          * completion handler was called.  Thus we need to protect
1277          * against double-freeing.
1278          */
1279         iocb->private = NULL;
1280
1281         ioend->io_offset = offset;
1282         ioend->io_size = size;
1283         ioend->io_iocb = iocb;
1284         ioend->io_result = ret;
1285         if (private && size > 0)
1286                 ioend->io_type = IO_UNWRITTEN;
1287
1288         if (is_async) {
1289                 ioend->io_isasync = 1;
1290                 xfs_finish_ioend(ioend);
1291         } else {
1292                 xfs_finish_ioend_sync(ioend);
1293         }
1294 }
1295
1296 STATIC ssize_t
1297 xfs_vm_direct_IO(
1298         int                     rw,
1299         struct kiocb            *iocb,
1300         const struct iovec      *iov,
1301         loff_t                  offset,
1302         unsigned long           nr_segs)
1303 {
1304         struct inode            *inode = iocb->ki_filp->f_mapping->host;
1305         struct block_device     *bdev = xfs_find_bdev_for_inode(inode);
1306         ssize_t                 ret;
1307
1308         if (rw & WRITE) {
1309                 iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
1310
1311                 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1312                                             offset, nr_segs,
1313                                             xfs_get_blocks_direct,
1314                                             xfs_end_io_direct_write, NULL, 0);
1315                 if (ret != -EIOCBQUEUED && iocb->private)
1316                         xfs_destroy_ioend(iocb->private);
1317         } else {
1318                 ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
1319                                             offset, nr_segs,
1320                                             xfs_get_blocks_direct,
1321                                             NULL, NULL, 0);
1322         }
1323
1324         return ret;
1325 }
1326
1327 STATIC void
1328 xfs_vm_write_failed(
1329         struct address_space    *mapping,
1330         loff_t                  to)
1331 {
1332         struct inode            *inode = mapping->host;
1333
1334         if (to > inode->i_size) {
1335                 /*
1336                  * punch out the delalloc blocks we have already allocated. We
1337                  * don't call xfs_setattr() to do this as we may be in the
1338                  * middle of a multi-iovec write and so the vfs inode->i_size
1339                  * will not match the xfs ip->i_size and so it will zero too
1340                  * much. Hence we jus truncate the page cache to zero what is
1341                  * necessary and punch the delalloc blocks directly.
1342                  */
1343                 struct xfs_inode        *ip = XFS_I(inode);
1344                 xfs_fileoff_t           start_fsb;
1345                 xfs_fileoff_t           end_fsb;
1346                 int                     error;
1347
1348                 truncate_pagecache(inode, to, inode->i_size);
1349
1350                 /*
1351                  * Check if there are any blocks that are outside of i_size
1352                  * that need to be trimmed back.
1353                  */
1354                 start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
1355                 end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
1356                 if (end_fsb <= start_fsb)
1357                         return;
1358
1359                 xfs_ilock(ip, XFS_ILOCK_EXCL);
1360                 error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
1361                                                         end_fsb - start_fsb);
1362                 if (error) {
1363                         /* something screwed, just bail */
1364                         if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
1365                                 xfs_alert(ip->i_mount,
1366                         "xfs_vm_write_failed: unable to clean up ino %lld",
1367                                                 ip->i_ino);
1368                         }
1369                 }
1370                 xfs_iunlock(ip, XFS_ILOCK_EXCL);
1371         }
1372 }
1373
1374 STATIC int
1375 xfs_vm_write_begin(
1376         struct file             *file,
1377         struct address_space    *mapping,
1378         loff_t                  pos,
1379         unsigned                len,
1380         unsigned                flags,
1381         struct page             **pagep,
1382         void                    **fsdata)
1383 {
1384         int                     ret;
1385
1386         ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
1387                                 pagep, xfs_get_blocks);
1388         if (unlikely(ret))
1389                 xfs_vm_write_failed(mapping, pos + len);
1390         return ret;
1391 }
1392
1393 STATIC int
1394 xfs_vm_write_end(
1395         struct file             *file,
1396         struct address_space    *mapping,
1397         loff_t                  pos,
1398         unsigned                len,
1399         unsigned                copied,
1400         struct page             *page,
1401         void                    *fsdata)
1402 {
1403         int                     ret;
1404
1405         ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
1406         if (unlikely(ret < len))
1407                 xfs_vm_write_failed(mapping, pos + len);
1408         return ret;
1409 }
1410
1411 STATIC sector_t
1412 xfs_vm_bmap(
1413         struct address_space    *mapping,
1414         sector_t                block)
1415 {
1416         struct inode            *inode = (struct inode *)mapping->host;
1417         struct xfs_inode        *ip = XFS_I(inode);
1418
1419         trace_xfs_vm_bmap(XFS_I(inode));
1420         xfs_ilock(ip, XFS_IOLOCK_SHARED);
1421         xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
1422         xfs_iunlock(ip, XFS_IOLOCK_SHARED);
1423         return generic_block_bmap(mapping, block, xfs_get_blocks);
1424 }
1425
1426 STATIC int
1427 xfs_vm_readpage(
1428         struct file             *unused,
1429         struct page             *page)
1430 {
1431         return mpage_readpage(page, xfs_get_blocks);
1432 }
1433
1434 STATIC int
1435 xfs_vm_readpages(
1436         struct file             *unused,
1437         struct address_space    *mapping,
1438         struct list_head        *pages,
1439         unsigned                nr_pages)
1440 {
1441         return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
1442 }
1443
1444 const struct address_space_operations xfs_address_space_operations = {
1445         .readpage               = xfs_vm_readpage,
1446         .readpages              = xfs_vm_readpages,
1447         .writepage              = xfs_vm_writepage,
1448         .writepages             = xfs_vm_writepages,
1449         .releasepage            = xfs_vm_releasepage,
1450         .invalidatepage         = xfs_vm_invalidatepage,
1451         .write_begin            = xfs_vm_write_begin,
1452         .write_end              = xfs_vm_write_end,
1453         .bmap                   = xfs_vm_bmap,
1454         .direct_IO              = xfs_vm_direct_IO,
1455         .migratepage            = buffer_migrate_page,
1456         .is_partially_uptodate  = block_is_partially_uptodate,
1457         .error_remove_page      = generic_error_remove_page,
1458 };