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