[XFS] add infrastructure for waiting on I/O completion at inode reclaim
[linux-2.6] / fs / xfs / linux-2.6 / xfs_aops.c
1 /*
2  * Copyright (c) 2000-2005 Silicon Graphics, Inc.  All Rights Reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of version 2 of the GNU General Public License as
6  * published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it would be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  *
12  * Further, this software is distributed without any warranty that it is
13  * free of the rightful claim of any third person regarding infringement
14  * or the like.  Any license provided herein, whether implied or
15  * otherwise, applies only to this software file.  Patent licenses, if
16  * any, provided herein do not apply to combinations of this program with
17  * other software, or any other product whatsoever.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write the Free Software Foundation, Inc., 59
21  * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22  *
23  * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24  * Mountain View, CA  94043, or:
25  *
26  * http://www.sgi.com
27  *
28  * For further information regarding this notice, see:
29  *
30  * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31  */
32
33 #include "xfs.h"
34 #include "xfs_inum.h"
35 #include "xfs_log.h"
36 #include "xfs_sb.h"
37 #include "xfs_dir.h"
38 #include "xfs_dir2.h"
39 #include "xfs_trans.h"
40 #include "xfs_dmapi.h"
41 #include "xfs_mount.h"
42 #include "xfs_bmap_btree.h"
43 #include "xfs_alloc_btree.h"
44 #include "xfs_ialloc_btree.h"
45 #include "xfs_alloc.h"
46 #include "xfs_btree.h"
47 #include "xfs_attr_sf.h"
48 #include "xfs_dir_sf.h"
49 #include "xfs_dir2_sf.h"
50 #include "xfs_dinode.h"
51 #include "xfs_inode.h"
52 #include "xfs_error.h"
53 #include "xfs_rw.h"
54 #include "xfs_iomap.h"
55 #include <linux/mpage.h>
56 #include <linux/writeback.h>
57
58 STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
59 STATIC void xfs_convert_page(struct inode *, struct page *, xfs_iomap_t *,
60                 struct writeback_control *wbc, void *, int, int);
61
62 #if defined(XFS_RW_TRACE)
63 void
64 xfs_page_trace(
65         int             tag,
66         struct inode    *inode,
67         struct page     *page,
68         int             mask)
69 {
70         xfs_inode_t     *ip;
71         bhv_desc_t      *bdp;
72         vnode_t         *vp = LINVFS_GET_VP(inode);
73         loff_t          isize = i_size_read(inode);
74         loff_t          offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
75         int             delalloc = -1, unmapped = -1, unwritten = -1;
76
77         if (page_has_buffers(page))
78                 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
79
80         bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
81         ip = XFS_BHVTOI(bdp);
82         if (!ip->i_rwtrace)
83                 return;
84
85         ktrace_enter(ip->i_rwtrace,
86                 (void *)((unsigned long)tag),
87                 (void *)ip,
88                 (void *)inode,
89                 (void *)page,
90                 (void *)((unsigned long)mask),
91                 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
92                 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
93                 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
94                 (void *)((unsigned long)(isize & 0xffffffff)),
95                 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
96                 (void *)((unsigned long)(offset & 0xffffffff)),
97                 (void *)((unsigned long)delalloc),
98                 (void *)((unsigned long)unmapped),
99                 (void *)((unsigned long)unwritten),
100                 (void *)NULL,
101                 (void *)NULL);
102 }
103 #else
104 #define xfs_page_trace(tag, inode, page, mask)
105 #endif
106
107 void
108 linvfs_unwritten_done(
109         struct buffer_head      *bh,
110         int                     uptodate)
111 {
112         xfs_buf_t               *pb = (xfs_buf_t *)bh->b_private;
113
114         ASSERT(buffer_unwritten(bh));
115         bh->b_end_io = NULL;
116         clear_buffer_unwritten(bh);
117         if (!uptodate)
118                 pagebuf_ioerror(pb, EIO);
119         if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
120                 pagebuf_iodone(pb, 1, 1);
121         }
122         end_buffer_async_write(bh, uptodate);
123 }
124
125 /*
126  * Issue transactions to convert a buffer range from unwritten
127  * to written extents (buffered IO).
128  */
129 STATIC void
130 linvfs_unwritten_convert(
131         xfs_buf_t       *bp)
132 {
133         vnode_t         *vp = XFS_BUF_FSPRIVATE(bp, vnode_t *);
134         int             error;
135
136         BUG_ON(atomic_read(&bp->pb_hold) < 1);
137         VOP_BMAP(vp, XFS_BUF_OFFSET(bp), XFS_BUF_SIZE(bp),
138                         BMAPI_UNWRITTEN, NULL, NULL, error);
139         XFS_BUF_SET_FSPRIVATE(bp, NULL);
140         XFS_BUF_CLR_IODONE_FUNC(bp);
141         XFS_BUF_UNDATAIO(bp);
142         vn_iowake(vp);
143         pagebuf_iodone(bp, 0, 0);
144 }
145
146 /*
147  * Issue transactions to convert a buffer range from unwritten
148  * to written extents (direct IO).
149  */
150 STATIC void
151 linvfs_unwritten_convert_direct(
152         struct kiocb    *iocb,
153         loff_t          offset,
154         ssize_t         size,
155         void            *private)
156 {
157         struct inode    *inode = iocb->ki_filp->f_dentry->d_inode;
158         ASSERT(!private || inode == (struct inode *)private);
159
160         /* private indicates an unwritten extent lay beneath this IO */
161         if (private && size > 0) {
162                 vnode_t *vp = LINVFS_GET_VP(inode);
163                 int     error;
164
165                 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
166         }
167 }
168
169 STATIC int
170 xfs_map_blocks(
171         struct inode            *inode,
172         loff_t                  offset,
173         ssize_t                 count,
174         xfs_iomap_t             *mapp,
175         int                     flags)
176 {
177         vnode_t                 *vp = LINVFS_GET_VP(inode);
178         int                     error, nmaps = 1;
179
180         VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
181         if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
182                 VMODIFY(vp);
183         return -error;
184 }
185
186 /*
187  * Finds the corresponding mapping in block @map array of the
188  * given @offset within a @page.
189  */
190 STATIC xfs_iomap_t *
191 xfs_offset_to_map(
192         struct page             *page,
193         xfs_iomap_t             *iomapp,
194         unsigned long           offset)
195 {
196         loff_t                  full_offset;    /* offset from start of file */
197
198         ASSERT(offset < PAGE_CACHE_SIZE);
199
200         full_offset = page->index;              /* NB: using 64bit number */
201         full_offset <<= PAGE_CACHE_SHIFT;       /* offset from file start */
202         full_offset += offset;                  /* offset from page start */
203
204         if (full_offset < iomapp->iomap_offset)
205                 return NULL;
206         if (iomapp->iomap_offset + (iomapp->iomap_bsize -1) >= full_offset)
207                 return iomapp;
208         return NULL;
209 }
210
211 STATIC void
212 xfs_map_at_offset(
213         struct page             *page,
214         struct buffer_head      *bh,
215         unsigned long           offset,
216         int                     block_bits,
217         xfs_iomap_t             *iomapp)
218 {
219         xfs_daddr_t             bn;
220         loff_t                  delta;
221         int                     sector_shift;
222
223         ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
224         ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
225         ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
226
227         delta = page->index;
228         delta <<= PAGE_CACHE_SHIFT;
229         delta += offset;
230         delta -= iomapp->iomap_offset;
231         delta >>= block_bits;
232
233         sector_shift = block_bits - BBSHIFT;
234         bn = iomapp->iomap_bn >> sector_shift;
235         bn += delta;
236         BUG_ON(!bn && !(iomapp->iomap_flags & IOMAP_REALTIME));
237         ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
238
239         lock_buffer(bh);
240         bh->b_blocknr = bn;
241         bh->b_bdev = iomapp->iomap_target->pbr_bdev;
242         set_buffer_mapped(bh);
243         clear_buffer_delay(bh);
244 }
245
246 /*
247  * Look for a page at index which is unlocked and contains our
248  * unwritten extent flagged buffers at its head.  Returns page
249  * locked and with an extra reference count, and length of the
250  * unwritten extent component on this page that we can write,
251  * in units of filesystem blocks.
252  */
253 STATIC struct page *
254 xfs_probe_unwritten_page(
255         struct address_space    *mapping,
256         pgoff_t                 index,
257         xfs_iomap_t             *iomapp,
258         xfs_buf_t               *pb,
259         unsigned long           max_offset,
260         unsigned long           *fsbs,
261         unsigned int            bbits)
262 {
263         struct page             *page;
264
265         page = find_trylock_page(mapping, index);
266         if (!page)
267                 return NULL;
268         if (PageWriteback(page))
269                 goto out;
270
271         if (page->mapping && page_has_buffers(page)) {
272                 struct buffer_head      *bh, *head;
273                 unsigned long           p_offset = 0;
274
275                 *fsbs = 0;
276                 bh = head = page_buffers(page);
277                 do {
278                         if (!buffer_unwritten(bh) || !buffer_uptodate(bh))
279                                 break;
280                         if (!xfs_offset_to_map(page, iomapp, p_offset))
281                                 break;
282                         if (p_offset >= max_offset)
283                                 break;
284                         xfs_map_at_offset(page, bh, p_offset, bbits, iomapp);
285                         set_buffer_unwritten_io(bh);
286                         bh->b_private = pb;
287                         p_offset += bh->b_size;
288                         (*fsbs)++;
289                 } while ((bh = bh->b_this_page) != head);
290
291                 if (p_offset)
292                         return page;
293         }
294
295 out:
296         unlock_page(page);
297         return NULL;
298 }
299
300 /*
301  * Look for a page at index which is unlocked and not mapped
302  * yet - clustering for mmap write case.
303  */
304 STATIC unsigned int
305 xfs_probe_unmapped_page(
306         struct address_space    *mapping,
307         pgoff_t                 index,
308         unsigned int            pg_offset)
309 {
310         struct page             *page;
311         int                     ret = 0;
312
313         page = find_trylock_page(mapping, index);
314         if (!page)
315                 return 0;
316         if (PageWriteback(page))
317                 goto out;
318
319         if (page->mapping && PageDirty(page)) {
320                 if (page_has_buffers(page)) {
321                         struct buffer_head      *bh, *head;
322
323                         bh = head = page_buffers(page);
324                         do {
325                                 if (buffer_mapped(bh) || !buffer_uptodate(bh))
326                                         break;
327                                 ret += bh->b_size;
328                                 if (ret >= pg_offset)
329                                         break;
330                         } while ((bh = bh->b_this_page) != head);
331                 } else
332                         ret = PAGE_CACHE_SIZE;
333         }
334
335 out:
336         unlock_page(page);
337         return ret;
338 }
339
340 STATIC unsigned int
341 xfs_probe_unmapped_cluster(
342         struct inode            *inode,
343         struct page             *startpage,
344         struct buffer_head      *bh,
345         struct buffer_head      *head)
346 {
347         pgoff_t                 tindex, tlast, tloff;
348         unsigned int            pg_offset, len, total = 0;
349         struct address_space    *mapping = inode->i_mapping;
350
351         /* First sum forwards in this page */
352         do {
353                 if (buffer_mapped(bh))
354                         break;
355                 total += bh->b_size;
356         } while ((bh = bh->b_this_page) != head);
357
358         /* If we reached the end of the page, sum forwards in
359          * following pages.
360          */
361         if (bh == head) {
362                 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
363                 /* Prune this back to avoid pathological behavior */
364                 tloff = min(tlast, startpage->index + 64);
365                 for (tindex = startpage->index + 1; tindex < tloff; tindex++) {
366                         len = xfs_probe_unmapped_page(mapping, tindex,
367                                                         PAGE_CACHE_SIZE);
368                         if (!len)
369                                 return total;
370                         total += len;
371                 }
372                 if (tindex == tlast &&
373                     (pg_offset = i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
374                         total += xfs_probe_unmapped_page(mapping,
375                                                         tindex, pg_offset);
376                 }
377         }
378         return total;
379 }
380
381 /*
382  * Probe for a given page (index) in the inode and test if it is delayed
383  * and without unwritten buffers.  Returns page locked and with an extra
384  * reference count.
385  */
386 STATIC struct page *
387 xfs_probe_delalloc_page(
388         struct inode            *inode,
389         pgoff_t                 index)
390 {
391         struct page             *page;
392
393         page = find_trylock_page(inode->i_mapping, index);
394         if (!page)
395                 return NULL;
396         if (PageWriteback(page))
397                 goto out;
398
399         if (page->mapping && page_has_buffers(page)) {
400                 struct buffer_head      *bh, *head;
401                 int                     acceptable = 0;
402
403                 bh = head = page_buffers(page);
404                 do {
405                         if (buffer_unwritten(bh)) {
406                                 acceptable = 0;
407                                 break;
408                         } else if (buffer_delay(bh)) {
409                                 acceptable = 1;
410                         }
411                 } while ((bh = bh->b_this_page) != head);
412
413                 if (acceptable)
414                         return page;
415         }
416
417 out:
418         unlock_page(page);
419         return NULL;
420 }
421
422 STATIC int
423 xfs_map_unwritten(
424         struct inode            *inode,
425         struct page             *start_page,
426         struct buffer_head      *head,
427         struct buffer_head      *curr,
428         unsigned long           p_offset,
429         int                     block_bits,
430         xfs_iomap_t             *iomapp,
431         struct writeback_control *wbc,
432         int                     startio,
433         int                     all_bh)
434 {
435         struct buffer_head      *bh = curr;
436         xfs_iomap_t             *tmp;
437         xfs_buf_t               *pb;
438         loff_t                  offset, size;
439         unsigned long           nblocks = 0;
440
441         offset = start_page->index;
442         offset <<= PAGE_CACHE_SHIFT;
443         offset += p_offset;
444
445         /* get an "empty" pagebuf to manage IO completion
446          * Proper values will be set before returning */
447         pb = pagebuf_lookup(iomapp->iomap_target, 0, 0, 0);
448         if (!pb)
449                 return -EAGAIN;
450
451         atomic_inc(&LINVFS_GET_VP(inode)->v_iocount);
452
453         /* Set the count to 1 initially, this will stop an I/O
454          * completion callout which happens before we have started
455          * all the I/O from calling pagebuf_iodone too early.
456          */
457         atomic_set(&pb->pb_io_remaining, 1);
458
459         /* First map forwards in the page consecutive buffers
460          * covering this unwritten extent
461          */
462         do {
463                 if (!buffer_unwritten(bh))
464                         break;
465                 tmp = xfs_offset_to_map(start_page, iomapp, p_offset);
466                 if (!tmp)
467                         break;
468                 xfs_map_at_offset(start_page, bh, p_offset, block_bits, iomapp);
469                 set_buffer_unwritten_io(bh);
470                 bh->b_private = pb;
471                 p_offset += bh->b_size;
472                 nblocks++;
473         } while ((bh = bh->b_this_page) != head);
474
475         atomic_add(nblocks, &pb->pb_io_remaining);
476
477         /* If we reached the end of the page, map forwards in any
478          * following pages which are also covered by this extent.
479          */
480         if (bh == head) {
481                 struct address_space    *mapping = inode->i_mapping;
482                 pgoff_t                 tindex, tloff, tlast;
483                 unsigned long           bs;
484                 unsigned int            pg_offset, bbits = inode->i_blkbits;
485                 struct page             *page;
486
487                 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
488                 tloff = (iomapp->iomap_offset + iomapp->iomap_bsize) >> PAGE_CACHE_SHIFT;
489                 tloff = min(tlast, tloff);
490                 for (tindex = start_page->index + 1; tindex < tloff; tindex++) {
491                         page = xfs_probe_unwritten_page(mapping,
492                                                 tindex, iomapp, pb,
493                                                 PAGE_CACHE_SIZE, &bs, bbits);
494                         if (!page)
495                                 break;
496                         nblocks += bs;
497                         atomic_add(bs, &pb->pb_io_remaining);
498                         xfs_convert_page(inode, page, iomapp, wbc, pb,
499                                                         startio, all_bh);
500                         /* stop if converting the next page might add
501                          * enough blocks that the corresponding byte
502                          * count won't fit in our ulong page buf length */
503                         if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
504                                 goto enough;
505                 }
506
507                 if (tindex == tlast &&
508                     (pg_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1)))) {
509                         page = xfs_probe_unwritten_page(mapping,
510                                                         tindex, iomapp, pb,
511                                                         pg_offset, &bs, bbits);
512                         if (page) {
513                                 nblocks += bs;
514                                 atomic_add(bs, &pb->pb_io_remaining);
515                                 xfs_convert_page(inode, page, iomapp, wbc, pb,
516                                                         startio, all_bh);
517                                 if (nblocks >= ((ULONG_MAX - PAGE_SIZE) >> block_bits))
518                                         goto enough;
519                         }
520                 }
521         }
522
523 enough:
524         size = nblocks;         /* NB: using 64bit number here */
525         size <<= block_bits;    /* convert fsb's to byte range */
526
527         XFS_BUF_DATAIO(pb);
528         XFS_BUF_ASYNC(pb);
529         XFS_BUF_SET_SIZE(pb, size);
530         XFS_BUF_SET_COUNT(pb, size);
531         XFS_BUF_SET_OFFSET(pb, offset);
532         XFS_BUF_SET_FSPRIVATE(pb, LINVFS_GET_VP(inode));
533         XFS_BUF_SET_IODONE_FUNC(pb, linvfs_unwritten_convert);
534
535         if (atomic_dec_and_test(&pb->pb_io_remaining) == 1) {
536                 pagebuf_iodone(pb, 1, 1);
537         }
538
539         return 0;
540 }
541
542 STATIC void
543 xfs_submit_page(
544         struct page             *page,
545         struct writeback_control *wbc,
546         struct buffer_head      *bh_arr[],
547         int                     bh_count,
548         int                     probed_page,
549         int                     clear_dirty)
550 {
551         struct buffer_head      *bh;
552         int                     i;
553
554         BUG_ON(PageWriteback(page));
555         if (bh_count)
556                 set_page_writeback(page);
557         if (clear_dirty)
558                 clear_page_dirty(page);
559         unlock_page(page);
560
561         if (bh_count) {
562                 for (i = 0; i < bh_count; i++) {
563                         bh = bh_arr[i];
564                         mark_buffer_async_write(bh);
565                         if (buffer_unwritten(bh))
566                                 set_buffer_unwritten_io(bh);
567                         set_buffer_uptodate(bh);
568                         clear_buffer_dirty(bh);
569                 }
570
571                 for (i = 0; i < bh_count; i++)
572                         submit_bh(WRITE, bh_arr[i]);
573
574                 if (probed_page && clear_dirty)
575                         wbc->nr_to_write--;     /* Wrote an "extra" page */
576         }
577 }
578
579 /*
580  * Allocate & map buffers for page given the extent map. Write it out.
581  * except for the original page of a writepage, this is called on
582  * delalloc/unwritten pages only, for the original page it is possible
583  * that the page has no mapping at all.
584  */
585 STATIC void
586 xfs_convert_page(
587         struct inode            *inode,
588         struct page             *page,
589         xfs_iomap_t             *iomapp,
590         struct writeback_control *wbc,
591         void                    *private,
592         int                     startio,
593         int                     all_bh)
594 {
595         struct buffer_head      *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
596         xfs_iomap_t             *mp = iomapp, *tmp;
597         unsigned long           offset, end_offset;
598         int                     index = 0;
599         int                     bbits = inode->i_blkbits;
600         int                     len, page_dirty;
601
602         end_offset = (i_size_read(inode) & (PAGE_CACHE_SIZE - 1));
603
604         /*
605          * page_dirty is initially a count of buffers on the page before
606          * EOF and is decrememted as we move each into a cleanable state.
607          */
608         len = 1 << inode->i_blkbits;
609         end_offset = max(end_offset, PAGE_CACHE_SIZE);
610         end_offset = roundup(end_offset, len);
611         page_dirty = end_offset / len;
612
613         offset = 0;
614         bh = head = page_buffers(page);
615         do {
616                 if (offset >= end_offset)
617                         break;
618                 if (!(PageUptodate(page) || buffer_uptodate(bh)))
619                         continue;
620                 if (buffer_mapped(bh) && all_bh &&
621                     !(buffer_unwritten(bh) || buffer_delay(bh))) {
622                         if (startio) {
623                                 lock_buffer(bh);
624                                 bh_arr[index++] = bh;
625                                 page_dirty--;
626                         }
627                         continue;
628                 }
629                 tmp = xfs_offset_to_map(page, mp, offset);
630                 if (!tmp)
631                         continue;
632                 ASSERT(!(tmp->iomap_flags & IOMAP_HOLE));
633                 ASSERT(!(tmp->iomap_flags & IOMAP_DELAY));
634
635                 /* If this is a new unwritten extent buffer (i.e. one
636                  * that we haven't passed in private data for, we must
637                  * now map this buffer too.
638                  */
639                 if (buffer_unwritten(bh) && !bh->b_end_io) {
640                         ASSERT(tmp->iomap_flags & IOMAP_UNWRITTEN);
641                         xfs_map_unwritten(inode, page, head, bh, offset,
642                                         bbits, tmp, wbc, startio, all_bh);
643                 } else if (! (buffer_unwritten(bh) && buffer_locked(bh))) {
644                         xfs_map_at_offset(page, bh, offset, bbits, tmp);
645                         if (buffer_unwritten(bh)) {
646                                 set_buffer_unwritten_io(bh);
647                                 bh->b_private = private;
648                                 ASSERT(private);
649                         }
650                 }
651                 if (startio) {
652                         bh_arr[index++] = bh;
653                 } else {
654                         set_buffer_dirty(bh);
655                         unlock_buffer(bh);
656                         mark_buffer_dirty(bh);
657                 }
658                 page_dirty--;
659         } while (offset += len, (bh = bh->b_this_page) != head);
660
661         if (startio && index) {
662                 xfs_submit_page(page, wbc, bh_arr, index, 1, !page_dirty);
663         } else {
664                 unlock_page(page);
665         }
666 }
667
668 /*
669  * Convert & write out a cluster of pages in the same extent as defined
670  * by mp and following the start page.
671  */
672 STATIC void
673 xfs_cluster_write(
674         struct inode            *inode,
675         pgoff_t                 tindex,
676         xfs_iomap_t             *iomapp,
677         struct writeback_control *wbc,
678         int                     startio,
679         int                     all_bh,
680         pgoff_t                 tlast)
681 {
682         struct page             *page;
683
684         for (; tindex <= tlast; tindex++) {
685                 page = xfs_probe_delalloc_page(inode, tindex);
686                 if (!page)
687                         break;
688                 xfs_convert_page(inode, page, iomapp, wbc, NULL,
689                                 startio, all_bh);
690         }
691 }
692
693 /*
694  * Calling this without startio set means we are being asked to make a dirty
695  * page ready for freeing it's buffers.  When called with startio set then
696  * we are coming from writepage.
697  *
698  * When called with startio set it is important that we write the WHOLE
699  * page if possible.
700  * The bh->b_state's cannot know if any of the blocks or which block for
701  * that matter are dirty due to mmap writes, and therefore bh uptodate is
702  * only vaild if the page itself isn't completely uptodate.  Some layers
703  * may clear the page dirty flag prior to calling write page, under the
704  * assumption the entire page will be written out; by not writing out the
705  * whole page the page can be reused before all valid dirty data is
706  * written out.  Note: in the case of a page that has been dirty'd by
707  * mapwrite and but partially setup by block_prepare_write the
708  * bh->b_states's will not agree and only ones setup by BPW/BCW will have
709  * valid state, thus the whole page must be written out thing.
710  */
711
712 STATIC int
713 xfs_page_state_convert(
714         struct inode    *inode,
715         struct page     *page,
716         struct writeback_control *wbc,
717         int             startio,
718         int             unmapped) /* also implies page uptodate */
719 {
720         struct buffer_head      *bh_arr[MAX_BUF_PER_PAGE], *bh, *head;
721         xfs_iomap_t             *iomp, iomap;
722         loff_t                  offset;
723         unsigned long           p_offset = 0;
724         __uint64_t              end_offset;
725         pgoff_t                 end_index, last_index, tlast;
726         int                     len, err, i, cnt = 0, uptodate = 1;
727         int                     flags;
728         int                     page_dirty;
729
730         /* wait for other IO threads? */
731         flags = (startio && wbc->sync_mode != WB_SYNC_NONE) ? 0 : BMAPI_TRYLOCK;
732
733         /* Is this page beyond the end of the file? */
734         offset = i_size_read(inode);
735         end_index = offset >> PAGE_CACHE_SHIFT;
736         last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
737         if (page->index >= end_index) {
738                 if ((page->index >= end_index + 1) ||
739                     !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
740                         err = -EIO;
741                         goto error;
742                 }
743         }
744
745         end_offset = min_t(unsigned long long,
746                         (loff_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
747         offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
748
749         /*
750          * page_dirty is initially a count of buffers on the page before
751          * EOF and is decrememted as we move each into a cleanable state.
752          */
753         len = 1 << inode->i_blkbits;
754         p_offset = max(p_offset, PAGE_CACHE_SIZE);
755         p_offset = roundup(p_offset, len);
756         page_dirty = p_offset / len;
757
758         iomp = NULL;
759         p_offset = 0;
760         bh = head = page_buffers(page);
761
762         do {
763                 if (offset >= end_offset)
764                         break;
765                 if (!buffer_uptodate(bh))
766                         uptodate = 0;
767                 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio)
768                         continue;
769
770                 if (iomp) {
771                         iomp = xfs_offset_to_map(page, &iomap, p_offset);
772                 }
773
774                 /*
775                  * First case, map an unwritten extent and prepare for
776                  * extent state conversion transaction on completion.
777                  */
778                 if (buffer_unwritten(bh)) {
779                         if (!startio)
780                                 continue;
781                         if (!iomp) {
782                                 err = xfs_map_blocks(inode, offset, len, &iomap,
783                                                 BMAPI_READ|BMAPI_IGNSTATE);
784                                 if (err) {
785                                         goto error;
786                                 }
787                                 iomp = xfs_offset_to_map(page, &iomap,
788                                                                 p_offset);
789                         }
790                         if (iomp) {
791                                 if (!bh->b_end_io) {
792                                         err = xfs_map_unwritten(inode, page,
793                                                         head, bh, p_offset,
794                                                         inode->i_blkbits, iomp,
795                                                         wbc, startio, unmapped);
796                                         if (err) {
797                                                 goto error;
798                                         }
799                                 } else {
800                                         set_bit(BH_Lock, &bh->b_state);
801                                 }
802                                 BUG_ON(!buffer_locked(bh));
803                                 bh_arr[cnt++] = bh;
804                                 page_dirty--;
805                         }
806                 /*
807                  * Second case, allocate space for a delalloc buffer.
808                  * We can return EAGAIN here in the release page case.
809                  */
810                 } else if (buffer_delay(bh)) {
811                         if (!iomp) {
812                                 err = xfs_map_blocks(inode, offset, len, &iomap,
813                                                 BMAPI_ALLOCATE | flags);
814                                 if (err) {
815                                         goto error;
816                                 }
817                                 iomp = xfs_offset_to_map(page, &iomap,
818                                                                 p_offset);
819                         }
820                         if (iomp) {
821                                 xfs_map_at_offset(page, bh, p_offset,
822                                                 inode->i_blkbits, iomp);
823                                 if (startio) {
824                                         bh_arr[cnt++] = bh;
825                                 } else {
826                                         set_buffer_dirty(bh);
827                                         unlock_buffer(bh);
828                                         mark_buffer_dirty(bh);
829                                 }
830                                 page_dirty--;
831                         }
832                 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
833                            (unmapped || startio)) {
834
835                         if (!buffer_mapped(bh)) {
836                                 int     size;
837
838                                 /*
839                                  * Getting here implies an unmapped buffer
840                                  * was found, and we are in a path where we
841                                  * need to write the whole page out.
842                                  */
843                                 if (!iomp) {
844                                         size = xfs_probe_unmapped_cluster(
845                                                         inode, page, bh, head);
846                                         err = xfs_map_blocks(inode, offset,
847                                                         size, &iomap,
848                                                         BMAPI_WRITE|BMAPI_MMAP);
849                                         if (err) {
850                                                 goto error;
851                                         }
852                                         iomp = xfs_offset_to_map(page, &iomap,
853                                                                      p_offset);
854                                 }
855                                 if (iomp) {
856                                         xfs_map_at_offset(page,
857                                                         bh, p_offset,
858                                                         inode->i_blkbits, iomp);
859                                         if (startio) {
860                                                 bh_arr[cnt++] = bh;
861                                         } else {
862                                                 set_buffer_dirty(bh);
863                                                 unlock_buffer(bh);
864                                                 mark_buffer_dirty(bh);
865                                         }
866                                         page_dirty--;
867                                 }
868                         } else if (startio) {
869                                 if (buffer_uptodate(bh) &&
870                                     !test_and_set_bit(BH_Lock, &bh->b_state)) {
871                                         bh_arr[cnt++] = bh;
872                                         page_dirty--;
873                                 }
874                         }
875                 }
876         } while (offset += len, p_offset += len,
877                 ((bh = bh->b_this_page) != head));
878
879         if (uptodate && bh == head)
880                 SetPageUptodate(page);
881
882         if (startio) {
883                 xfs_submit_page(page, wbc, bh_arr, cnt, 0, !page_dirty);
884         }
885
886         if (iomp) {
887                 offset = (iomp->iomap_offset + iomp->iomap_bsize - 1) >>
888                                         PAGE_CACHE_SHIFT;
889                 tlast = min_t(pgoff_t, offset, last_index);
890                 xfs_cluster_write(inode, page->index + 1, iomp, wbc,
891                                         startio, unmapped, tlast);
892         }
893
894         return page_dirty;
895
896 error:
897         for (i = 0; i < cnt; i++) {
898                 unlock_buffer(bh_arr[i]);
899         }
900
901         /*
902          * If it's delalloc and we have nowhere to put it,
903          * throw it away, unless the lower layers told
904          * us to try again.
905          */
906         if (err != -EAGAIN) {
907                 if (!unmapped) {
908                         block_invalidatepage(page, 0);
909                 }
910                 ClearPageUptodate(page);
911         }
912         return err;
913 }
914
915 STATIC int
916 __linvfs_get_block(
917         struct inode            *inode,
918         sector_t                iblock,
919         unsigned long           blocks,
920         struct buffer_head      *bh_result,
921         int                     create,
922         int                     direct,
923         bmapi_flags_t           flags)
924 {
925         vnode_t                 *vp = LINVFS_GET_VP(inode);
926         xfs_iomap_t             iomap;
927         int                     retpbbm = 1;
928         int                     error;
929         ssize_t                 size;
930         loff_t                  offset = (loff_t)iblock << inode->i_blkbits;
931
932         if (blocks)
933                 size = blocks << inode->i_blkbits;
934         else
935                 size = 1 << inode->i_blkbits;
936
937         VOP_BMAP(vp, offset, size,
938                 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
939         if (error)
940                 return -error;
941
942         if (retpbbm == 0)
943                 return 0;
944
945         if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
946                 xfs_daddr_t             bn;
947                 loff_t                  delta;
948
949                 /* For unwritten extents do not report a disk address on
950                  * the read case (treat as if we're reading into a hole).
951                  */
952                 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
953                         delta = offset - iomap.iomap_offset;
954                         delta >>= inode->i_blkbits;
955
956                         bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
957                         bn += delta;
958                         BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
959                         bh_result->b_blocknr = bn;
960                         set_buffer_mapped(bh_result);
961                 }
962                 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
963                         if (direct)
964                                 bh_result->b_private = inode;
965                         set_buffer_unwritten(bh_result);
966                         set_buffer_delay(bh_result);
967                 }
968         }
969
970         /* If this is a realtime file, data might be on a new device */
971         bh_result->b_bdev = iomap.iomap_target->pbr_bdev;
972
973         /* If we previously allocated a block out beyond eof and
974          * we are now coming back to use it then we will need to
975          * flag it as new even if it has a disk address.
976          */
977         if (create &&
978             ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
979              (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW))) {
980                 set_buffer_new(bh_result);
981         }
982
983         if (iomap.iomap_flags & IOMAP_DELAY) {
984                 BUG_ON(direct);
985                 if (create) {
986                         set_buffer_uptodate(bh_result);
987                         set_buffer_mapped(bh_result);
988                         set_buffer_delay(bh_result);
989                 }
990         }
991
992         if (blocks) {
993                 bh_result->b_size = (ssize_t)min(
994                         (loff_t)(iomap.iomap_bsize - iomap.iomap_delta),
995                         (loff_t)(blocks << inode->i_blkbits));
996         }
997
998         return 0;
999 }
1000
1001 int
1002 linvfs_get_block(
1003         struct inode            *inode,
1004         sector_t                iblock,
1005         struct buffer_head      *bh_result,
1006         int                     create)
1007 {
1008         return __linvfs_get_block(inode, iblock, 0, bh_result,
1009                                         create, 0, BMAPI_WRITE);
1010 }
1011
1012 STATIC int
1013 linvfs_get_blocks_direct(
1014         struct inode            *inode,
1015         sector_t                iblock,
1016         unsigned long           max_blocks,
1017         struct buffer_head      *bh_result,
1018         int                     create)
1019 {
1020         return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1021                                         create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1022 }
1023
1024 STATIC ssize_t
1025 linvfs_direct_IO(
1026         int                     rw,
1027         struct kiocb            *iocb,
1028         const struct iovec      *iov,
1029         loff_t                  offset,
1030         unsigned long           nr_segs)
1031 {
1032         struct file     *file = iocb->ki_filp;
1033         struct inode    *inode = file->f_mapping->host;
1034         vnode_t         *vp = LINVFS_GET_VP(inode);
1035         xfs_iomap_t     iomap;
1036         int             maps = 1;
1037         int             error;
1038
1039         VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1040         if (error)
1041                 return -error;
1042
1043         return blockdev_direct_IO_own_locking(rw, iocb, inode,
1044                 iomap.iomap_target->pbr_bdev,
1045                 iov, offset, nr_segs,
1046                 linvfs_get_blocks_direct,
1047                 linvfs_unwritten_convert_direct);
1048 }
1049
1050
1051 STATIC sector_t
1052 linvfs_bmap(
1053         struct address_space    *mapping,
1054         sector_t                block)
1055 {
1056         struct inode            *inode = (struct inode *)mapping->host;
1057         vnode_t                 *vp = LINVFS_GET_VP(inode);
1058         int                     error;
1059
1060         vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1061
1062         VOP_RWLOCK(vp, VRWLOCK_READ);
1063         VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1064         VOP_RWUNLOCK(vp, VRWLOCK_READ);
1065         return generic_block_bmap(mapping, block, linvfs_get_block);
1066 }
1067
1068 STATIC int
1069 linvfs_readpage(
1070         struct file             *unused,
1071         struct page             *page)
1072 {
1073         return mpage_readpage(page, linvfs_get_block);
1074 }
1075
1076 STATIC int
1077 linvfs_readpages(
1078         struct file             *unused,
1079         struct address_space    *mapping,
1080         struct list_head        *pages,
1081         unsigned                nr_pages)
1082 {
1083         return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1084 }
1085
1086 STATIC void
1087 xfs_count_page_state(
1088         struct page             *page,
1089         int                     *delalloc,
1090         int                     *unmapped,
1091         int                     *unwritten)
1092 {
1093         struct buffer_head      *bh, *head;
1094
1095         *delalloc = *unmapped = *unwritten = 0;
1096
1097         bh = head = page_buffers(page);
1098         do {
1099                 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1100                         (*unmapped) = 1;
1101                 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1102                         clear_buffer_unwritten(bh);
1103                 else if (buffer_unwritten(bh))
1104                         (*unwritten) = 1;
1105                 else if (buffer_delay(bh))
1106                         (*delalloc) = 1;
1107         } while ((bh = bh->b_this_page) != head);
1108 }
1109
1110
1111 /*
1112  * writepage: Called from one of two places:
1113  *
1114  * 1. we are flushing a delalloc buffer head.
1115  *
1116  * 2. we are writing out a dirty page. Typically the page dirty
1117  *    state is cleared before we get here. In this case is it
1118  *    conceivable we have no buffer heads.
1119  *
1120  * For delalloc space on the page we need to allocate space and
1121  * flush it. For unmapped buffer heads on the page we should
1122  * allocate space if the page is uptodate. For any other dirty
1123  * buffer heads on the page we should flush them.
1124  *
1125  * If we detect that a transaction would be required to flush
1126  * the page, we have to check the process flags first, if we
1127  * are already in a transaction or disk I/O during allocations
1128  * is off, we need to fail the writepage and redirty the page.
1129  */
1130
1131 STATIC int
1132 linvfs_writepage(
1133         struct page             *page,
1134         struct writeback_control *wbc)
1135 {
1136         int                     error;
1137         int                     need_trans;
1138         int                     delalloc, unmapped, unwritten;
1139         struct inode            *inode = page->mapping->host;
1140
1141         xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1142
1143         /*
1144          * We need a transaction if:
1145          *  1. There are delalloc buffers on the page
1146          *  2. The page is uptodate and we have unmapped buffers
1147          *  3. The page is uptodate and we have no buffers
1148          *  4. There are unwritten buffers on the page
1149          */
1150
1151         if (!page_has_buffers(page)) {
1152                 unmapped = 1;
1153                 need_trans = 1;
1154         } else {
1155                 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1156                 if (!PageUptodate(page))
1157                         unmapped = 0;
1158                 need_trans = delalloc + unmapped + unwritten;
1159         }
1160
1161         /*
1162          * If we need a transaction and the process flags say
1163          * we are already in a transaction, or no IO is allowed
1164          * then mark the page dirty again and leave the page
1165          * as is.
1166          */
1167         if (PFLAGS_TEST_FSTRANS() && need_trans)
1168                 goto out_fail;
1169
1170         /*
1171          * Delay hooking up buffer heads until we have
1172          * made our go/no-go decision.
1173          */
1174         if (!page_has_buffers(page))
1175                 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1176
1177         /*
1178          * Convert delayed allocate, unwritten or unmapped space
1179          * to real space and flush out to disk.
1180          */
1181         error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1182         if (error == -EAGAIN)
1183                 goto out_fail;
1184         if (unlikely(error < 0))
1185                 goto out_unlock;
1186
1187         return 0;
1188
1189 out_fail:
1190         redirty_page_for_writepage(wbc, page);
1191         unlock_page(page);
1192         return 0;
1193 out_unlock:
1194         unlock_page(page);
1195         return error;
1196 }
1197
1198 STATIC int
1199 linvfs_invalidate_page(
1200         struct page             *page,
1201         unsigned long           offset)
1202 {
1203         xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1204                         page->mapping->host, page, offset);
1205         return block_invalidatepage(page, offset);
1206 }
1207
1208 /*
1209  * Called to move a page into cleanable state - and from there
1210  * to be released. Possibly the page is already clean. We always
1211  * have buffer heads in this call.
1212  *
1213  * Returns 0 if the page is ok to release, 1 otherwise.
1214  *
1215  * Possible scenarios are:
1216  *
1217  * 1. We are being called to release a page which has been written
1218  *    to via regular I/O. buffer heads will be dirty and possibly
1219  *    delalloc. If no delalloc buffer heads in this case then we
1220  *    can just return zero.
1221  *
1222  * 2. We are called to release a page which has been written via
1223  *    mmap, all we need to do is ensure there is no delalloc
1224  *    state in the buffer heads, if not we can let the caller
1225  *    free them and we should come back later via writepage.
1226  */
1227 STATIC int
1228 linvfs_release_page(
1229         struct page             *page,
1230         int                     gfp_mask)
1231 {
1232         struct inode            *inode = page->mapping->host;
1233         int                     dirty, delalloc, unmapped, unwritten;
1234         struct writeback_control wbc = {
1235                 .sync_mode = WB_SYNC_ALL,
1236                 .nr_to_write = 1,
1237         };
1238
1239         xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1240
1241         xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1242         if (!delalloc && !unwritten)
1243                 goto free_buffers;
1244
1245         if (!(gfp_mask & __GFP_FS))
1246                 return 0;
1247
1248         /* If we are already inside a transaction or the thread cannot
1249          * do I/O, we cannot release this page.
1250          */
1251         if (PFLAGS_TEST_FSTRANS())
1252                 return 0;
1253
1254         /*
1255          * Convert delalloc space to real space, do not flush the
1256          * data out to disk, that will be done by the caller.
1257          * Never need to allocate space here - we will always
1258          * come back to writepage in that case.
1259          */
1260         dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1261         if (dirty == 0 && !unwritten)
1262                 goto free_buffers;
1263         return 0;
1264
1265 free_buffers:
1266         return try_to_free_buffers(page);
1267 }
1268
1269 STATIC int
1270 linvfs_prepare_write(
1271         struct file             *file,
1272         struct page             *page,
1273         unsigned int            from,
1274         unsigned int            to)
1275 {
1276         return block_prepare_write(page, from, to, linvfs_get_block);
1277 }
1278
1279 struct address_space_operations linvfs_aops = {
1280         .readpage               = linvfs_readpage,
1281         .readpages              = linvfs_readpages,
1282         .writepage              = linvfs_writepage,
1283         .sync_page              = block_sync_page,
1284         .releasepage            = linvfs_release_page,
1285         .invalidatepage         = linvfs_invalidate_page,
1286         .prepare_write          = linvfs_prepare_write,
1287         .commit_write           = generic_commit_write,
1288         .bmap                   = linvfs_bmap,
1289         .direct_IO              = linvfs_direct_IO,
1290 };