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