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