]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/xfs/linux-2.6/xfs_aops.c
[XFS] add helper to get xfs_inode from vnode
[mirror_ubuntu-jammy-kernel.git] / fs / xfs / linux-2.6 / xfs_aops.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
1da177e4 13 *
7b718769
NS
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_bit.h"
1da177e4 20#include "xfs_log.h"
a844f451 21#include "xfs_inum.h"
1da177e4 22#include "xfs_sb.h"
a844f451 23#include "xfs_ag.h"
1da177e4
LT
24#include "xfs_dir.h"
25#include "xfs_dir2.h"
26#include "xfs_trans.h"
27#include "xfs_dmapi.h"
28#include "xfs_mount.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_alloc_btree.h"
31#include "xfs_ialloc_btree.h"
1da177e4
LT
32#include "xfs_dir_sf.h"
33#include "xfs_dir2_sf.h"
a844f451 34#include "xfs_attr_sf.h"
1da177e4
LT
35#include "xfs_dinode.h"
36#include "xfs_inode.h"
a844f451
NS
37#include "xfs_alloc.h"
38#include "xfs_btree.h"
1da177e4
LT
39#include "xfs_error.h"
40#include "xfs_rw.h"
41#include "xfs_iomap.h"
42#include <linux/mpage.h>
10ce4444 43#include <linux/pagevec.h>
1da177e4
LT
44#include <linux/writeback.h>
45
46STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
1da177e4
LT
47
48#if defined(XFS_RW_TRACE)
49void
50xfs_page_trace(
51 int tag,
52 struct inode *inode,
53 struct page *page,
54 int mask)
55{
56 xfs_inode_t *ip;
1da177e4
LT
57 vnode_t *vp = LINVFS_GET_VP(inode);
58 loff_t isize = i_size_read(inode);
f6d6d4fc 59 loff_t offset = page_offset(page);
1da177e4
LT
60 int delalloc = -1, unmapped = -1, unwritten = -1;
61
62 if (page_has_buffers(page))
63 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
64
75e17b3c 65 ip = xfs_vtoi(vp);
1da177e4
LT
66 if (!ip->i_rwtrace)
67 return;
68
69 ktrace_enter(ip->i_rwtrace,
70 (void *)((unsigned long)tag),
71 (void *)ip,
72 (void *)inode,
73 (void *)page,
74 (void *)((unsigned long)mask),
75 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
76 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
77 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(isize & 0xffffffff)),
79 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
80 (void *)((unsigned long)(offset & 0xffffffff)),
81 (void *)((unsigned long)delalloc),
82 (void *)((unsigned long)unmapped),
83 (void *)((unsigned long)unwritten),
84 (void *)NULL,
85 (void *)NULL);
86}
87#else
88#define xfs_page_trace(tag, inode, page, mask)
89#endif
90
0829c360
CH
91/*
92 * Schedule IO completion handling on a xfsdatad if this was
93 * the final hold on this ioend.
94 */
95STATIC void
96xfs_finish_ioend(
97 xfs_ioend_t *ioend)
98{
99 if (atomic_dec_and_test(&ioend->io_remaining))
100 queue_work(xfsdatad_workqueue, &ioend->io_work);
101}
102
f6d6d4fc
CH
103/*
104 * We're now finished for good with this ioend structure.
105 * Update the page state via the associated buffer_heads,
106 * release holds on the inode and bio, and finally free
107 * up memory. Do not use the ioend after this.
108 */
0829c360
CH
109STATIC void
110xfs_destroy_ioend(
111 xfs_ioend_t *ioend)
112{
f6d6d4fc
CH
113 struct buffer_head *bh, *next;
114
115 for (bh = ioend->io_buffer_head; bh; bh = next) {
116 next = bh->b_private;
117 bh->b_end_io(bh, ioend->io_uptodate);
118 }
119
0829c360
CH
120 vn_iowake(ioend->io_vnode);
121 mempool_free(ioend, xfs_ioend_pool);
122}
123
124/*
f6d6d4fc
CH
125 * Buffered IO write completion for delayed allocate extents.
126 * TODO: Update ondisk isize now that we know the file data
127 * has been flushed (i.e. the notorious "NULL file" problem).
128 */
129STATIC void
130xfs_end_bio_delalloc(
131 void *data)
132{
133 xfs_ioend_t *ioend = data;
134
135 xfs_destroy_ioend(ioend);
136}
137
138/*
139 * Buffered IO write completion for regular, written extents.
140 */
141STATIC void
142xfs_end_bio_written(
143 void *data)
144{
145 xfs_ioend_t *ioend = data;
146
147 xfs_destroy_ioend(ioend);
148}
149
150/*
151 * IO write completion for unwritten extents.
152 *
0829c360 153 * Issue transactions to convert a buffer range from unwritten
f0973863 154 * to written extents.
0829c360
CH
155 */
156STATIC void
157xfs_end_bio_unwritten(
158 void *data)
159{
160 xfs_ioend_t *ioend = data;
161 vnode_t *vp = ioend->io_vnode;
162 xfs_off_t offset = ioend->io_offset;
163 size_t size = ioend->io_size;
164 int error;
165
166 if (ioend->io_uptodate)
167 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
168 xfs_destroy_ioend(ioend);
169}
170
171/*
172 * Allocate and initialise an IO completion structure.
173 * We need to track unwritten extent write completion here initially.
174 * We'll need to extend this for updating the ondisk inode size later
175 * (vs. incore size).
176 */
177STATIC xfs_ioend_t *
178xfs_alloc_ioend(
f6d6d4fc
CH
179 struct inode *inode,
180 unsigned int type)
0829c360
CH
181{
182 xfs_ioend_t *ioend;
183
184 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
185
186 /*
187 * Set the count to 1 initially, which will prevent an I/O
188 * completion callback from happening before we have started
189 * all the I/O from calling the completion routine too early.
190 */
191 atomic_set(&ioend->io_remaining, 1);
192 ioend->io_uptodate = 1; /* cleared if any I/O fails */
f6d6d4fc
CH
193 ioend->io_list = NULL;
194 ioend->io_type = type;
0829c360 195 ioend->io_vnode = LINVFS_GET_VP(inode);
c1a073bd 196 ioend->io_buffer_head = NULL;
f6d6d4fc 197 ioend->io_buffer_tail = NULL;
0829c360
CH
198 atomic_inc(&ioend->io_vnode->v_iocount);
199 ioend->io_offset = 0;
200 ioend->io_size = 0;
201
f6d6d4fc
CH
202 if (type == IOMAP_UNWRITTEN)
203 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
204 else if (type == IOMAP_DELAY)
205 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
206 else
207 INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
0829c360
CH
208
209 return ioend;
210}
211
1da177e4
LT
212STATIC int
213xfs_map_blocks(
214 struct inode *inode,
215 loff_t offset,
216 ssize_t count,
217 xfs_iomap_t *mapp,
218 int flags)
219{
220 vnode_t *vp = LINVFS_GET_VP(inode);
221 int error, nmaps = 1;
222
223 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
224 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
225 VMODIFY(vp);
226 return -error;
227}
228
1defeac9
CH
229STATIC inline int
230xfs_iomap_valid(
1da177e4 231 xfs_iomap_t *iomapp,
1defeac9 232 loff_t offset)
1da177e4 233{
1defeac9
CH
234 return offset >= iomapp->iomap_offset &&
235 offset < iomapp->iomap_offset + iomapp->iomap_bsize;
1da177e4
LT
236}
237
f6d6d4fc
CH
238/*
239 * BIO completion handler for buffered IO.
240 */
241STATIC int
242xfs_end_bio(
243 struct bio *bio,
244 unsigned int bytes_done,
245 int error)
246{
247 xfs_ioend_t *ioend = bio->bi_private;
248
249 if (bio->bi_size)
250 return 1;
251
252 ASSERT(ioend);
253 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
254
255 /* Toss bio and pass work off to an xfsdatad thread */
256 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
257 ioend->io_uptodate = 0;
258 bio->bi_private = NULL;
259 bio->bi_end_io = NULL;
260
261 bio_put(bio);
262 xfs_finish_ioend(ioend);
263 return 0;
264}
265
266STATIC void
267xfs_submit_ioend_bio(
268 xfs_ioend_t *ioend,
269 struct bio *bio)
270{
271 atomic_inc(&ioend->io_remaining);
272
273 bio->bi_private = ioend;
274 bio->bi_end_io = xfs_end_bio;
275
276 submit_bio(WRITE, bio);
277 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
278 bio_put(bio);
279}
280
281STATIC struct bio *
282xfs_alloc_ioend_bio(
283 struct buffer_head *bh)
284{
285 struct bio *bio;
286 int nvecs = bio_get_nr_vecs(bh->b_bdev);
287
288 do {
289 bio = bio_alloc(GFP_NOIO, nvecs);
290 nvecs >>= 1;
291 } while (!bio);
292
293 ASSERT(bio->bi_private == NULL);
294 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
295 bio->bi_bdev = bh->b_bdev;
296 bio_get(bio);
297 return bio;
298}
299
300STATIC void
301xfs_start_buffer_writeback(
302 struct buffer_head *bh)
303{
304 ASSERT(buffer_mapped(bh));
305 ASSERT(buffer_locked(bh));
306 ASSERT(!buffer_delay(bh));
307 ASSERT(!buffer_unwritten(bh));
308
309 mark_buffer_async_write(bh);
310 set_buffer_uptodate(bh);
311 clear_buffer_dirty(bh);
312}
313
314STATIC void
315xfs_start_page_writeback(
316 struct page *page,
317 struct writeback_control *wbc,
318 int clear_dirty,
319 int buffers)
320{
321 ASSERT(PageLocked(page));
322 ASSERT(!PageWriteback(page));
323 set_page_writeback(page);
324 if (clear_dirty)
325 clear_page_dirty(page);
326 unlock_page(page);
327 if (!buffers) {
328 end_page_writeback(page);
329 wbc->pages_skipped++; /* We didn't write this page */
330 }
331}
332
333static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
334{
335 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
336}
337
338/*
339 * Submit all of the bios for all of the ioends we have saved up,
340 * covering the initial writepage page and also any probed pages.
341 */
342STATIC void
343xfs_submit_ioend(
344 xfs_ioend_t *ioend)
345{
346 xfs_ioend_t *next;
347 struct buffer_head *bh;
348 struct bio *bio;
349 sector_t lastblock = 0;
350
351 do {
352 next = ioend->io_list;
353 bio = NULL;
354
355 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
356 xfs_start_buffer_writeback(bh);
357
358 if (!bio) {
359 retry:
360 bio = xfs_alloc_ioend_bio(bh);
361 } else if (bh->b_blocknr != lastblock + 1) {
362 xfs_submit_ioend_bio(ioend, bio);
363 goto retry;
364 }
365
366 if (bio_add_buffer(bio, bh) != bh->b_size) {
367 xfs_submit_ioend_bio(ioend, bio);
368 goto retry;
369 }
370
371 lastblock = bh->b_blocknr;
372 }
373 if (bio)
374 xfs_submit_ioend_bio(ioend, bio);
375 xfs_finish_ioend(ioend);
376 } while ((ioend = next) != NULL);
377}
378
379/*
380 * Cancel submission of all buffer_heads so far in this endio.
381 * Toss the endio too. Only ever called for the initial page
382 * in a writepage request, so only ever one page.
383 */
384STATIC void
385xfs_cancel_ioend(
386 xfs_ioend_t *ioend)
387{
388 xfs_ioend_t *next;
389 struct buffer_head *bh, *next_bh;
390
391 do {
392 next = ioend->io_list;
393 bh = ioend->io_buffer_head;
394 do {
395 next_bh = bh->b_private;
396 clear_buffer_async_write(bh);
397 unlock_buffer(bh);
398 } while ((bh = next_bh) != NULL);
399
400 vn_iowake(ioend->io_vnode);
401 mempool_free(ioend, xfs_ioend_pool);
402 } while ((ioend = next) != NULL);
403}
404
405/*
406 * Test to see if we've been building up a completion structure for
407 * earlier buffers -- if so, we try to append to this ioend if we
408 * can, otherwise we finish off any current ioend and start another.
409 * Return true if we've finished the given ioend.
410 */
411STATIC void
412xfs_add_to_ioend(
413 struct inode *inode,
414 struct buffer_head *bh,
7336cea8 415 xfs_off_t offset,
f6d6d4fc
CH
416 unsigned int type,
417 xfs_ioend_t **result,
418 int need_ioend)
419{
420 xfs_ioend_t *ioend = *result;
421
422 if (!ioend || need_ioend || type != ioend->io_type) {
423 xfs_ioend_t *previous = *result;
f6d6d4fc 424
f6d6d4fc
CH
425 ioend = xfs_alloc_ioend(inode, type);
426 ioend->io_offset = offset;
427 ioend->io_buffer_head = bh;
428 ioend->io_buffer_tail = bh;
429 if (previous)
430 previous->io_list = ioend;
431 *result = ioend;
432 } else {
433 ioend->io_buffer_tail->b_private = bh;
434 ioend->io_buffer_tail = bh;
435 }
436
437 bh->b_private = NULL;
438 ioend->io_size += bh->b_size;
439}
440
1da177e4
LT
441STATIC void
442xfs_map_at_offset(
1da177e4 443 struct buffer_head *bh,
1defeac9 444 loff_t offset,
1da177e4 445 int block_bits,
1defeac9 446 xfs_iomap_t *iomapp)
1da177e4
LT
447{
448 xfs_daddr_t bn;
1da177e4
LT
449 int sector_shift;
450
451 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
452 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
453 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
454
1da177e4 455 sector_shift = block_bits - BBSHIFT;
1defeac9
CH
456 bn = (iomapp->iomap_bn >> sector_shift) +
457 ((offset - iomapp->iomap_offset) >> block_bits);
458
459 ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
1da177e4
LT
460 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
461
462 lock_buffer(bh);
463 bh->b_blocknr = bn;
ce8e922c 464 bh->b_bdev = iomapp->iomap_target->bt_bdev;
1da177e4
LT
465 set_buffer_mapped(bh);
466 clear_buffer_delay(bh);
f6d6d4fc 467 clear_buffer_unwritten(bh);
1da177e4
LT
468}
469
470/*
6c4fe19f 471 * Look for a page at index that is suitable for clustering.
1da177e4
LT
472 */
473STATIC unsigned int
6c4fe19f 474xfs_probe_page(
10ce4444 475 struct page *page,
6c4fe19f
CH
476 unsigned int pg_offset,
477 int mapped)
1da177e4 478{
1da177e4
LT
479 int ret = 0;
480
1da177e4 481 if (PageWriteback(page))
10ce4444 482 return 0;
1da177e4
LT
483
484 if (page->mapping && PageDirty(page)) {
485 if (page_has_buffers(page)) {
486 struct buffer_head *bh, *head;
487
488 bh = head = page_buffers(page);
489 do {
6c4fe19f
CH
490 if (!buffer_uptodate(bh))
491 break;
492 if (mapped != buffer_mapped(bh))
1da177e4
LT
493 break;
494 ret += bh->b_size;
495 if (ret >= pg_offset)
496 break;
497 } while ((bh = bh->b_this_page) != head);
498 } else
6c4fe19f 499 ret = mapped ? 0 : PAGE_CACHE_SIZE;
1da177e4
LT
500 }
501
1da177e4
LT
502 return ret;
503}
504
f6d6d4fc 505STATIC size_t
6c4fe19f 506xfs_probe_cluster(
1da177e4
LT
507 struct inode *inode,
508 struct page *startpage,
509 struct buffer_head *bh,
6c4fe19f
CH
510 struct buffer_head *head,
511 int mapped)
1da177e4 512{
10ce4444 513 struct pagevec pvec;
1da177e4 514 pgoff_t tindex, tlast, tloff;
10ce4444
CH
515 size_t total = 0;
516 int done = 0, i;
1da177e4
LT
517
518 /* First sum forwards in this page */
519 do {
6c4fe19f 520 if (mapped != buffer_mapped(bh))
10ce4444 521 return total;
1da177e4
LT
522 total += bh->b_size;
523 } while ((bh = bh->b_this_page) != head);
524
10ce4444
CH
525 /* if we reached the end of the page, sum forwards in following pages */
526 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
527 tindex = startpage->index + 1;
528
529 /* Prune this back to avoid pathological behavior */
530 tloff = min(tlast, startpage->index + 64);
531
532 pagevec_init(&pvec, 0);
533 while (!done && tindex <= tloff) {
534 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
535
536 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
537 break;
538
539 for (i = 0; i < pagevec_count(&pvec); i++) {
540 struct page *page = pvec.pages[i];
541 size_t pg_offset, len = 0;
542
543 if (tindex == tlast) {
544 pg_offset =
545 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
1defeac9
CH
546 if (!pg_offset) {
547 done = 1;
10ce4444 548 break;
1defeac9 549 }
10ce4444
CH
550 } else
551 pg_offset = PAGE_CACHE_SIZE;
552
553 if (page->index == tindex && !TestSetPageLocked(page)) {
6c4fe19f 554 len = xfs_probe_page(page, pg_offset, mapped);
10ce4444
CH
555 unlock_page(page);
556 }
557
558 if (!len) {
559 done = 1;
560 break;
561 }
562
1da177e4 563 total += len;
1defeac9 564 tindex++;
1da177e4 565 }
10ce4444
CH
566
567 pagevec_release(&pvec);
568 cond_resched();
1da177e4 569 }
10ce4444 570
1da177e4
LT
571 return total;
572}
573
574/*
10ce4444
CH
575 * Test if a given page is suitable for writing as part of an unwritten
576 * or delayed allocate extent.
1da177e4 577 */
10ce4444
CH
578STATIC int
579xfs_is_delayed_page(
580 struct page *page,
f6d6d4fc 581 unsigned int type)
1da177e4 582{
1da177e4 583 if (PageWriteback(page))
10ce4444 584 return 0;
1da177e4
LT
585
586 if (page->mapping && page_has_buffers(page)) {
587 struct buffer_head *bh, *head;
588 int acceptable = 0;
589
590 bh = head = page_buffers(page);
591 do {
f6d6d4fc
CH
592 if (buffer_unwritten(bh))
593 acceptable = (type == IOMAP_UNWRITTEN);
594 else if (buffer_delay(bh))
595 acceptable = (type == IOMAP_DELAY);
6c4fe19f
CH
596 else if (buffer_mapped(bh))
597 acceptable = (type == 0);
f6d6d4fc 598 else
1da177e4 599 break;
1da177e4
LT
600 } while ((bh = bh->b_this_page) != head);
601
602 if (acceptable)
10ce4444 603 return 1;
1da177e4
LT
604 }
605
10ce4444 606 return 0;
1da177e4
LT
607}
608
1da177e4
LT
609/*
610 * Allocate & map buffers for page given the extent map. Write it out.
611 * except for the original page of a writepage, this is called on
612 * delalloc/unwritten pages only, for the original page it is possible
613 * that the page has no mapping at all.
614 */
f6d6d4fc 615STATIC int
1da177e4
LT
616xfs_convert_page(
617 struct inode *inode,
618 struct page *page,
10ce4444 619 loff_t tindex,
1defeac9 620 xfs_iomap_t *mp,
f6d6d4fc 621 xfs_ioend_t **ioendp,
1da177e4 622 struct writeback_control *wbc,
1da177e4
LT
623 int startio,
624 int all_bh)
625{
f6d6d4fc 626 struct buffer_head *bh, *head;
9260dc6b
CH
627 xfs_off_t end_offset;
628 unsigned long p_offset;
f6d6d4fc 629 unsigned int type;
1da177e4 630 int bbits = inode->i_blkbits;
24e17b5f 631 int len, page_dirty;
f6d6d4fc 632 int count = 0, done = 0, uptodate = 1;
9260dc6b 633 xfs_off_t offset = page_offset(page);
1da177e4 634
10ce4444
CH
635 if (page->index != tindex)
636 goto fail;
637 if (TestSetPageLocked(page))
638 goto fail;
639 if (PageWriteback(page))
640 goto fail_unlock_page;
641 if (page->mapping != inode->i_mapping)
642 goto fail_unlock_page;
643 if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
644 goto fail_unlock_page;
645
24e17b5f
NS
646 /*
647 * page_dirty is initially a count of buffers on the page before
648 * EOF and is decrememted as we move each into a cleanable state.
9260dc6b
CH
649 *
650 * Derivation:
651 *
652 * End offset is the highest offset that this page should represent.
653 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
654 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
655 * hence give us the correct page_dirty count. On any other page,
656 * it will be zero and in that case we need page_dirty to be the
657 * count of buffers on the page.
24e17b5f 658 */
9260dc6b
CH
659 end_offset = min_t(unsigned long long,
660 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
661 i_size_read(inode));
662
24e17b5f 663 len = 1 << inode->i_blkbits;
9260dc6b
CH
664 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
665 PAGE_CACHE_SIZE);
666 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
667 page_dirty = p_offset / len;
24e17b5f 668
1da177e4
LT
669 bh = head = page_buffers(page);
670 do {
9260dc6b 671 if (offset >= end_offset)
1da177e4 672 break;
f6d6d4fc
CH
673 if (!buffer_uptodate(bh))
674 uptodate = 0;
675 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
676 done = 1;
1da177e4 677 continue;
f6d6d4fc
CH
678 }
679
9260dc6b
CH
680 if (buffer_unwritten(bh) || buffer_delay(bh)) {
681 if (buffer_unwritten(bh))
682 type = IOMAP_UNWRITTEN;
683 else
684 type = IOMAP_DELAY;
685
686 if (!xfs_iomap_valid(mp, offset)) {
f6d6d4fc 687 done = 1;
9260dc6b
CH
688 continue;
689 }
690
691 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
692 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
693
694 xfs_map_at_offset(bh, offset, bbits, mp);
695 if (startio) {
7336cea8 696 xfs_add_to_ioend(inode, bh, offset,
9260dc6b
CH
697 type, ioendp, done);
698 } else {
699 set_buffer_dirty(bh);
700 unlock_buffer(bh);
701 mark_buffer_dirty(bh);
702 }
703 page_dirty--;
704 count++;
705 } else {
706 type = 0;
707 if (buffer_mapped(bh) && all_bh && startio) {
1da177e4 708 lock_buffer(bh);
7336cea8 709 xfs_add_to_ioend(inode, bh, offset,
f6d6d4fc
CH
710 type, ioendp, done);
711 count++;
24e17b5f 712 page_dirty--;
9260dc6b
CH
713 } else {
714 done = 1;
1da177e4 715 }
1da177e4 716 }
7336cea8 717 } while (offset += len, (bh = bh->b_this_page) != head);
1da177e4 718
f6d6d4fc
CH
719 if (uptodate && bh == head)
720 SetPageUptodate(page);
721
722 if (startio) {
f5e596bb
CH
723 if (count) {
724 struct backing_dev_info *bdi;
725
726 bdi = inode->i_mapping->backing_dev_info;
727 if (bdi_write_congested(bdi)) {
728 wbc->encountered_congestion = 1;
729 done = 1;
730 } else if (--wbc->nr_to_write <= 0) {
731 done = 1;
732 }
733 }
f6d6d4fc 734 xfs_start_page_writeback(page, wbc, !page_dirty, count);
1da177e4 735 }
f6d6d4fc
CH
736
737 return done;
10ce4444
CH
738 fail_unlock_page:
739 unlock_page(page);
740 fail:
741 return 1;
1da177e4
LT
742}
743
744/*
745 * Convert & write out a cluster of pages in the same extent as defined
746 * by mp and following the start page.
747 */
748STATIC void
749xfs_cluster_write(
750 struct inode *inode,
751 pgoff_t tindex,
752 xfs_iomap_t *iomapp,
f6d6d4fc 753 xfs_ioend_t **ioendp,
1da177e4
LT
754 struct writeback_control *wbc,
755 int startio,
756 int all_bh,
757 pgoff_t tlast)
758{
10ce4444
CH
759 struct pagevec pvec;
760 int done = 0, i;
1da177e4 761
10ce4444
CH
762 pagevec_init(&pvec, 0);
763 while (!done && tindex <= tlast) {
764 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
765
766 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
1da177e4 767 break;
10ce4444
CH
768
769 for (i = 0; i < pagevec_count(&pvec); i++) {
770 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
771 iomapp, ioendp, wbc, startio, all_bh);
772 if (done)
773 break;
774 }
775
776 pagevec_release(&pvec);
777 cond_resched();
1da177e4
LT
778 }
779}
780
781/*
782 * Calling this without startio set means we are being asked to make a dirty
783 * page ready for freeing it's buffers. When called with startio set then
784 * we are coming from writepage.
785 *
786 * When called with startio set it is important that we write the WHOLE
787 * page if possible.
788 * The bh->b_state's cannot know if any of the blocks or which block for
789 * that matter are dirty due to mmap writes, and therefore bh uptodate is
790 * only vaild if the page itself isn't completely uptodate. Some layers
791 * may clear the page dirty flag prior to calling write page, under the
792 * assumption the entire page will be written out; by not writing out the
793 * whole page the page can be reused before all valid dirty data is
794 * written out. Note: in the case of a page that has been dirty'd by
795 * mapwrite and but partially setup by block_prepare_write the
796 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
797 * valid state, thus the whole page must be written out thing.
798 */
799
800STATIC int
801xfs_page_state_convert(
802 struct inode *inode,
803 struct page *page,
804 struct writeback_control *wbc,
805 int startio,
806 int unmapped) /* also implies page uptodate */
807{
f6d6d4fc 808 struct buffer_head *bh, *head;
1defeac9 809 xfs_iomap_t iomap;
f6d6d4fc 810 xfs_ioend_t *ioend = NULL, *iohead = NULL;
1da177e4
LT
811 loff_t offset;
812 unsigned long p_offset = 0;
f6d6d4fc 813 unsigned int type;
1da177e4
LT
814 __uint64_t end_offset;
815 pgoff_t end_index, last_index, tlast;
d5cb48aa
CH
816 ssize_t size, len;
817 int flags, err, iomap_valid = 0, uptodate = 1;
f6d6d4fc 818 int page_dirty, count = 0, trylock_flag = 0;
6c4fe19f 819 int all_bh = unmapped;
1da177e4 820
3ba0815a 821 /* wait for other IO threads? */
f5e596bb 822 if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
f6d6d4fc 823 trylock_flag |= BMAPI_TRYLOCK;
3ba0815a 824
1da177e4
LT
825 /* Is this page beyond the end of the file? */
826 offset = i_size_read(inode);
827 end_index = offset >> PAGE_CACHE_SHIFT;
828 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
829 if (page->index >= end_index) {
830 if ((page->index >= end_index + 1) ||
831 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
19d5bcf3
NS
832 if (startio)
833 unlock_page(page);
834 return 0;
1da177e4
LT
835 }
836 }
837
1da177e4 838 /*
24e17b5f
NS
839 * page_dirty is initially a count of buffers on the page before
840 * EOF and is decrememted as we move each into a cleanable state.
f6d6d4fc
CH
841 *
842 * Derivation:
843 *
844 * End offset is the highest offset that this page should represent.
845 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
846 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
847 * hence give us the correct page_dirty count. On any other page,
848 * it will be zero and in that case we need page_dirty to be the
849 * count of buffers on the page.
850 */
851 end_offset = min_t(unsigned long long,
852 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
24e17b5f 853 len = 1 << inode->i_blkbits;
f6d6d4fc
CH
854 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
855 PAGE_CACHE_SIZE);
856 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
24e17b5f
NS
857 page_dirty = p_offset / len;
858
24e17b5f 859 bh = head = page_buffers(page);
f6d6d4fc 860 offset = page_offset(page);
6c4fe19f
CH
861 flags = -1;
862 type = 0;
f6d6d4fc 863
f6d6d4fc 864 /* TODO: cleanup count and page_dirty */
1da177e4
LT
865
866 do {
867 if (offset >= end_offset)
868 break;
869 if (!buffer_uptodate(bh))
870 uptodate = 0;
f6d6d4fc 871 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
1defeac9
CH
872 /*
873 * the iomap is actually still valid, but the ioend
874 * isn't. shouldn't happen too often.
875 */
876 iomap_valid = 0;
1da177e4 877 continue;
f6d6d4fc 878 }
1da177e4 879
1defeac9
CH
880 if (iomap_valid)
881 iomap_valid = xfs_iomap_valid(&iomap, offset);
1da177e4
LT
882
883 /*
884 * First case, map an unwritten extent and prepare for
885 * extent state conversion transaction on completion.
f6d6d4fc 886 *
1da177e4
LT
887 * Second case, allocate space for a delalloc buffer.
888 * We can return EAGAIN here in the release page case.
d5cb48aa
CH
889 *
890 * Third case, an unmapped buffer was found, and we are
891 * in a path where we need to write the whole page out.
892 */
893 if (buffer_unwritten(bh) || buffer_delay(bh) ||
894 ((buffer_uptodate(bh) || PageUptodate(page)) &&
895 !buffer_mapped(bh) && (unmapped || startio))) {
6c4fe19f
CH
896 /*
897 * Make sure we don't use a read-only iomap
898 */
899 if (flags == BMAPI_READ)
900 iomap_valid = 0;
901
f6d6d4fc
CH
902 if (buffer_unwritten(bh)) {
903 type = IOMAP_UNWRITTEN;
904 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
d5cb48aa 905 } else if (buffer_delay(bh)) {
f6d6d4fc
CH
906 type = IOMAP_DELAY;
907 flags = BMAPI_ALLOCATE;
908 if (!startio)
909 flags |= trylock_flag;
d5cb48aa 910 } else {
6c4fe19f 911 type = IOMAP_NEW;
d5cb48aa 912 flags = BMAPI_WRITE|BMAPI_MMAP;
f6d6d4fc
CH
913 }
914
1defeac9 915 if (!iomap_valid) {
6c4fe19f
CH
916 if (type == IOMAP_NEW) {
917 size = xfs_probe_cluster(inode,
918 page, bh, head, 0);
d5cb48aa
CH
919 } else {
920 size = len;
921 }
922
923 err = xfs_map_blocks(inode, offset, size,
924 &iomap, flags);
f6d6d4fc 925 if (err)
1da177e4 926 goto error;
1defeac9 927 iomap_valid = xfs_iomap_valid(&iomap, offset);
1da177e4 928 }
1defeac9
CH
929 if (iomap_valid) {
930 xfs_map_at_offset(bh, offset,
931 inode->i_blkbits, &iomap);
1da177e4 932 if (startio) {
7336cea8 933 xfs_add_to_ioend(inode, bh, offset,
1defeac9
CH
934 type, &ioend,
935 !iomap_valid);
1da177e4
LT
936 } else {
937 set_buffer_dirty(bh);
938 unlock_buffer(bh);
939 mark_buffer_dirty(bh);
940 }
941 page_dirty--;
f6d6d4fc 942 count++;
1da177e4 943 }
d5cb48aa 944 } else if (buffer_uptodate(bh) && startio) {
6c4fe19f
CH
945 /*
946 * we got here because the buffer is already mapped.
947 * That means it must already have extents allocated
948 * underneath it. Map the extent by reading it.
949 */
950 if (!iomap_valid || type != 0) {
951 flags = BMAPI_READ;
952 size = xfs_probe_cluster(inode, page, bh,
953 head, 1);
954 err = xfs_map_blocks(inode, offset, size,
955 &iomap, flags);
956 if (err)
957 goto error;
958 iomap_valid = xfs_iomap_valid(&iomap, offset);
959 }
d5cb48aa 960
6c4fe19f 961 type = 0;
d5cb48aa
CH
962 if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
963 ASSERT(buffer_mapped(bh));
6c4fe19f
CH
964 if (iomap_valid)
965 all_bh = 1;
7336cea8 966 xfs_add_to_ioend(inode, bh, offset, type,
d5cb48aa
CH
967 &ioend, !iomap_valid);
968 page_dirty--;
969 count++;
f6d6d4fc 970 } else {
1defeac9 971 iomap_valid = 0;
1da177e4 972 }
d5cb48aa
CH
973 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
974 (unmapped || startio)) {
975 iomap_valid = 0;
1da177e4 976 }
f6d6d4fc
CH
977
978 if (!iohead)
979 iohead = ioend;
980
981 } while (offset += len, ((bh = bh->b_this_page) != head));
1da177e4
LT
982
983 if (uptodate && bh == head)
984 SetPageUptodate(page);
985
f6d6d4fc
CH
986 if (startio)
987 xfs_start_page_writeback(page, wbc, 1, count);
1da177e4 988
1defeac9
CH
989 if (ioend && iomap_valid) {
990 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
1da177e4 991 PAGE_CACHE_SHIFT;
775bf6c9 992 tlast = min_t(pgoff_t, offset, last_index);
1defeac9 993 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
6c4fe19f 994 wbc, startio, all_bh, tlast);
1da177e4
LT
995 }
996
f6d6d4fc
CH
997 if (iohead)
998 xfs_submit_ioend(iohead);
999
1da177e4
LT
1000 return page_dirty;
1001
1002error:
f6d6d4fc
CH
1003 if (iohead)
1004 xfs_cancel_ioend(iohead);
1da177e4
LT
1005
1006 /*
1007 * If it's delalloc and we have nowhere to put it,
1008 * throw it away, unless the lower layers told
1009 * us to try again.
1010 */
1011 if (err != -EAGAIN) {
f6d6d4fc 1012 if (!unmapped)
1da177e4 1013 block_invalidatepage(page, 0);
1da177e4
LT
1014 ClearPageUptodate(page);
1015 }
1016 return err;
1017}
1018
1019STATIC int
1020__linvfs_get_block(
1021 struct inode *inode,
1022 sector_t iblock,
1023 unsigned long blocks,
1024 struct buffer_head *bh_result,
1025 int create,
1026 int direct,
1027 bmapi_flags_t flags)
1028{
1029 vnode_t *vp = LINVFS_GET_VP(inode);
1030 xfs_iomap_t iomap;
fdc7ed75
NS
1031 xfs_off_t offset;
1032 ssize_t size;
1da177e4
LT
1033 int retpbbm = 1;
1034 int error;
1da177e4 1035
fdc7ed75 1036 offset = (xfs_off_t)iblock << inode->i_blkbits;
a4656391
NS
1037 if (blocks)
1038 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1039 (xfs_off_t)blocks << inode->i_blkbits);
1040 else
1041 size = 1 << inode->i_blkbits;
1da177e4
LT
1042
1043 VOP_BMAP(vp, offset, size,
1044 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1045 if (error)
1046 return -error;
1047
1048 if (retpbbm == 0)
1049 return 0;
1050
1051 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
fdc7ed75
NS
1052 xfs_daddr_t bn;
1053 xfs_off_t delta;
1da177e4
LT
1054
1055 /* For unwritten extents do not report a disk address on
1056 * the read case (treat as if we're reading into a hole).
1057 */
1058 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1059 delta = offset - iomap.iomap_offset;
1060 delta >>= inode->i_blkbits;
1061
1062 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1063 bn += delta;
1064 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1065 bh_result->b_blocknr = bn;
1066 set_buffer_mapped(bh_result);
1067 }
1068 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1069 if (direct)
1070 bh_result->b_private = inode;
1071 set_buffer_unwritten(bh_result);
1072 set_buffer_delay(bh_result);
1073 }
1074 }
1075
1076 /* If this is a realtime file, data might be on a new device */
ce8e922c 1077 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
1da177e4
LT
1078
1079 /* If we previously allocated a block out beyond eof and
1080 * we are now coming back to use it then we will need to
1081 * flag it as new even if it has a disk address.
1082 */
1083 if (create &&
1084 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
fdc7ed75 1085 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
1da177e4 1086 set_buffer_new(bh_result);
1da177e4
LT
1087
1088 if (iomap.iomap_flags & IOMAP_DELAY) {
1089 BUG_ON(direct);
1090 if (create) {
1091 set_buffer_uptodate(bh_result);
1092 set_buffer_mapped(bh_result);
1093 set_buffer_delay(bh_result);
1094 }
1095 }
1096
1097 if (blocks) {
fdc7ed75
NS
1098 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1099 offset = min_t(xfs_off_t,
1100 iomap.iomap_bsize - iomap.iomap_delta,
a4656391 1101 (xfs_off_t)blocks << inode->i_blkbits);
fdc7ed75 1102 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
1da177e4
LT
1103 }
1104
1105 return 0;
1106}
1107
1108int
1109linvfs_get_block(
1110 struct inode *inode,
1111 sector_t iblock,
1112 struct buffer_head *bh_result,
1113 int create)
1114{
1115 return __linvfs_get_block(inode, iblock, 0, bh_result,
1116 create, 0, BMAPI_WRITE);
1117}
1118
1119STATIC int
1120linvfs_get_blocks_direct(
1121 struct inode *inode,
1122 sector_t iblock,
1123 unsigned long max_blocks,
1124 struct buffer_head *bh_result,
1125 int create)
1126{
1127 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1128 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1129}
1130
f0973863
CH
1131STATIC void
1132linvfs_end_io_direct(
1133 struct kiocb *iocb,
1134 loff_t offset,
1135 ssize_t size,
1136 void *private)
1137{
1138 xfs_ioend_t *ioend = iocb->private;
1139
1140 /*
1141 * Non-NULL private data means we need to issue a transaction to
1142 * convert a range from unwritten to written extents. This needs
1143 * to happen from process contect but aio+dio I/O completion
1144 * happens from irq context so we need to defer it to a workqueue.
1145 * This is not nessecary for synchronous direct I/O, but we do
1146 * it anyway to keep the code uniform and simpler.
1147 *
1148 * The core direct I/O code might be changed to always call the
1149 * completion handler in the future, in which case all this can
1150 * go away.
1151 */
1152 if (private && size > 0) {
1153 ioend->io_offset = offset;
1154 ioend->io_size = size;
1155 xfs_finish_ioend(ioend);
1156 } else {
1157 ASSERT(size >= 0);
1158 xfs_destroy_ioend(ioend);
1159 }
1160
1161 /*
1162 * blockdev_direct_IO can return an error even afer the I/O
1163 * completion handler was called. Thus we need to protect
1164 * against double-freeing.
1165 */
1166 iocb->private = NULL;
1167}
1168
1da177e4
LT
1169STATIC ssize_t
1170linvfs_direct_IO(
1171 int rw,
1172 struct kiocb *iocb,
1173 const struct iovec *iov,
1174 loff_t offset,
1175 unsigned long nr_segs)
1176{
1177 struct file *file = iocb->ki_filp;
1178 struct inode *inode = file->f_mapping->host;
1179 vnode_t *vp = LINVFS_GET_VP(inode);
1180 xfs_iomap_t iomap;
1181 int maps = 1;
1182 int error;
f0973863 1183 ssize_t ret;
1da177e4
LT
1184
1185 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1186 if (error)
1187 return -error;
1188
f6d6d4fc 1189 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
f0973863
CH
1190
1191 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
ce8e922c 1192 iomap.iomap_target->bt_bdev,
1da177e4
LT
1193 iov, offset, nr_segs,
1194 linvfs_get_blocks_direct,
f0973863
CH
1195 linvfs_end_io_direct);
1196
1197 if (unlikely(ret <= 0 && iocb->private))
1198 xfs_destroy_ioend(iocb->private);
1199 return ret;
1da177e4
LT
1200}
1201
1202
1203STATIC sector_t
1204linvfs_bmap(
1205 struct address_space *mapping,
1206 sector_t block)
1207{
1208 struct inode *inode = (struct inode *)mapping->host;
1209 vnode_t *vp = LINVFS_GET_VP(inode);
1210 int error;
1211
1212 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1213
1214 VOP_RWLOCK(vp, VRWLOCK_READ);
1215 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1216 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1217 return generic_block_bmap(mapping, block, linvfs_get_block);
1218}
1219
1220STATIC int
1221linvfs_readpage(
1222 struct file *unused,
1223 struct page *page)
1224{
1225 return mpage_readpage(page, linvfs_get_block);
1226}
1227
1228STATIC int
1229linvfs_readpages(
1230 struct file *unused,
1231 struct address_space *mapping,
1232 struct list_head *pages,
1233 unsigned nr_pages)
1234{
1235 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1236}
1237
1238STATIC void
1239xfs_count_page_state(
1240 struct page *page,
1241 int *delalloc,
1242 int *unmapped,
1243 int *unwritten)
1244{
1245 struct buffer_head *bh, *head;
1246
1247 *delalloc = *unmapped = *unwritten = 0;
1248
1249 bh = head = page_buffers(page);
1250 do {
1251 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1252 (*unmapped) = 1;
1253 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1254 clear_buffer_unwritten(bh);
1255 else if (buffer_unwritten(bh))
1256 (*unwritten) = 1;
1257 else if (buffer_delay(bh))
1258 (*delalloc) = 1;
1259 } while ((bh = bh->b_this_page) != head);
1260}
1261
1262
1263/*
1264 * writepage: Called from one of two places:
1265 *
1266 * 1. we are flushing a delalloc buffer head.
1267 *
1268 * 2. we are writing out a dirty page. Typically the page dirty
1269 * state is cleared before we get here. In this case is it
1270 * conceivable we have no buffer heads.
1271 *
1272 * For delalloc space on the page we need to allocate space and
1273 * flush it. For unmapped buffer heads on the page we should
1274 * allocate space if the page is uptodate. For any other dirty
1275 * buffer heads on the page we should flush them.
1276 *
1277 * If we detect that a transaction would be required to flush
1278 * the page, we have to check the process flags first, if we
1279 * are already in a transaction or disk I/O during allocations
1280 * is off, we need to fail the writepage and redirty the page.
1281 */
1282
1283STATIC int
1284linvfs_writepage(
1285 struct page *page,
1286 struct writeback_control *wbc)
1287{
1288 int error;
1289 int need_trans;
1290 int delalloc, unmapped, unwritten;
1291 struct inode *inode = page->mapping->host;
1292
1293 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1294
1295 /*
1296 * We need a transaction if:
1297 * 1. There are delalloc buffers on the page
1298 * 2. The page is uptodate and we have unmapped buffers
1299 * 3. The page is uptodate and we have no buffers
1300 * 4. There are unwritten buffers on the page
1301 */
1302
1303 if (!page_has_buffers(page)) {
1304 unmapped = 1;
1305 need_trans = 1;
1306 } else {
1307 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1308 if (!PageUptodate(page))
1309 unmapped = 0;
1310 need_trans = delalloc + unmapped + unwritten;
1311 }
1312
1313 /*
1314 * If we need a transaction and the process flags say
1315 * we are already in a transaction, or no IO is allowed
1316 * then mark the page dirty again and leave the page
1317 * as is.
1318 */
1319 if (PFLAGS_TEST_FSTRANS() && need_trans)
1320 goto out_fail;
1321
1322 /*
1323 * Delay hooking up buffer heads until we have
1324 * made our go/no-go decision.
1325 */
1326 if (!page_has_buffers(page))
1327 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1328
1329 /*
1330 * Convert delayed allocate, unwritten or unmapped space
1331 * to real space and flush out to disk.
1332 */
1333 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1334 if (error == -EAGAIN)
1335 goto out_fail;
1336 if (unlikely(error < 0))
1337 goto out_unlock;
1338
1339 return 0;
1340
1341out_fail:
1342 redirty_page_for_writepage(wbc, page);
1343 unlock_page(page);
1344 return 0;
1345out_unlock:
1346 unlock_page(page);
1347 return error;
1348}
1349
bcec2b7f
NS
1350STATIC int
1351linvfs_invalidate_page(
1352 struct page *page,
1353 unsigned long offset)
1354{
1355 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1356 page->mapping->host, page, offset);
1357 return block_invalidatepage(page, offset);
1358}
1359
1da177e4
LT
1360/*
1361 * Called to move a page into cleanable state - and from there
1362 * to be released. Possibly the page is already clean. We always
1363 * have buffer heads in this call.
1364 *
1365 * Returns 0 if the page is ok to release, 1 otherwise.
1366 *
1367 * Possible scenarios are:
1368 *
1369 * 1. We are being called to release a page which has been written
1370 * to via regular I/O. buffer heads will be dirty and possibly
1371 * delalloc. If no delalloc buffer heads in this case then we
1372 * can just return zero.
1373 *
1374 * 2. We are called to release a page which has been written via
1375 * mmap, all we need to do is ensure there is no delalloc
1376 * state in the buffer heads, if not we can let the caller
1377 * free them and we should come back later via writepage.
1378 */
1379STATIC int
1380linvfs_release_page(
1381 struct page *page,
27496a8c 1382 gfp_t gfp_mask)
1da177e4
LT
1383{
1384 struct inode *inode = page->mapping->host;
1385 int dirty, delalloc, unmapped, unwritten;
1386 struct writeback_control wbc = {
1387 .sync_mode = WB_SYNC_ALL,
1388 .nr_to_write = 1,
1389 };
1390
1391 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1392
1393 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1394 if (!delalloc && !unwritten)
1395 goto free_buffers;
1396
1397 if (!(gfp_mask & __GFP_FS))
1398 return 0;
1399
1400 /* If we are already inside a transaction or the thread cannot
1401 * do I/O, we cannot release this page.
1402 */
1403 if (PFLAGS_TEST_FSTRANS())
1404 return 0;
1405
1406 /*
1407 * Convert delalloc space to real space, do not flush the
1408 * data out to disk, that will be done by the caller.
1409 * Never need to allocate space here - we will always
1410 * come back to writepage in that case.
1411 */
1412 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1413 if (dirty == 0 && !unwritten)
1414 goto free_buffers;
1415 return 0;
1416
1417free_buffers:
1418 return try_to_free_buffers(page);
1419}
1420
1421STATIC int
1422linvfs_prepare_write(
1423 struct file *file,
1424 struct page *page,
1425 unsigned int from,
1426 unsigned int to)
1427{
1428 return block_prepare_write(page, from, to, linvfs_get_block);
1429}
1430
1431struct address_space_operations linvfs_aops = {
1432 .readpage = linvfs_readpage,
1433 .readpages = linvfs_readpages,
1434 .writepage = linvfs_writepage,
1435 .sync_page = block_sync_page,
1436 .releasepage = linvfs_release_page,
bcec2b7f 1437 .invalidatepage = linvfs_invalidate_page,
1da177e4
LT
1438 .prepare_write = linvfs_prepare_write,
1439 .commit_write = generic_commit_write,
1440 .bmap = linvfs_bmap,
1441 .direct_IO = linvfs_direct_IO,
1442};