blob: 98d26c8e05659f1892527cc5a4a12b388560068b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_log.h"
Nathan Scotta844f452005-11-02 14:38:42 +110021#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_sb.h"
Nathan Scotta844f452005-11-02 14:38:42 +110023#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include "xfs_dir_sf.h"
33#include "xfs_dir2_sf.h"
Nathan Scotta844f452005-11-02 14:38:42 +110034#include "xfs_attr_sf.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_dinode.h"
36#include "xfs_inode.h"
Nathan Scotta844f452005-11-02 14:38:42 +110037#include "xfs_alloc.h"
38#include "xfs_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include "xfs_error.h"
40#include "xfs_rw.h"
41#include "xfs_iomap.h"
42#include <linux/mpage.h>
Christoph Hellwig10ce4442006-01-11 20:48:14 +110043#include <linux/pagevec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/writeback.h>
45
46STATIC void xfs_count_page_state(struct page *, int *, int *, int *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
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;
57 bhv_desc_t *bdp;
58 vnode_t *vp = LINVFS_GET_VP(inode);
59 loff_t isize = i_size_read(inode);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +110060 loff_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 int delalloc = -1, unmapped = -1, unwritten = -1;
62
63 if (page_has_buffers(page))
64 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
65
66 bdp = vn_bhv_lookup(VN_BHV_HEAD(vp), &xfs_vnodeops);
67 ip = XFS_BHVTOI(bdp);
68 if (!ip->i_rwtrace)
69 return;
70
71 ktrace_enter(ip->i_rwtrace,
72 (void *)((unsigned long)tag),
73 (void *)ip,
74 (void *)inode,
75 (void *)page,
76 (void *)((unsigned long)mask),
77 (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
78 (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
79 (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
80 (void *)((unsigned long)(isize & 0xffffffff)),
81 (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
82 (void *)((unsigned long)(offset & 0xffffffff)),
83 (void *)((unsigned long)delalloc),
84 (void *)((unsigned long)unmapped),
85 (void *)((unsigned long)unwritten),
86 (void *)NULL,
87 (void *)NULL);
88}
89#else
90#define xfs_page_trace(tag, inode, page, mask)
91#endif
92
Christoph Hellwig0829c362005-09-02 16:58:49 +100093/*
94 * Schedule IO completion handling on a xfsdatad if this was
95 * the final hold on this ioend.
96 */
97STATIC void
98xfs_finish_ioend(
99 xfs_ioend_t *ioend)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000101 if (atomic_dec_and_test(&ioend->io_remaining))
102 queue_work(xfsdatad_workqueue, &ioend->io_work);
103}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100105/*
106 * We're now finished for good with this ioend structure.
107 * Update the page state via the associated buffer_heads,
108 * release holds on the inode and bio, and finally free
109 * up memory. Do not use the ioend after this.
110 */
Christoph Hellwig0829c362005-09-02 16:58:49 +1000111STATIC void
112xfs_destroy_ioend(
113 xfs_ioend_t *ioend)
114{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100115 struct buffer_head *bh, *next;
116
117 for (bh = ioend->io_buffer_head; bh; bh = next) {
118 next = bh->b_private;
119 bh->b_end_io(bh, ioend->io_uptodate);
120 }
121
Christoph Hellwig0829c362005-09-02 16:58:49 +1000122 vn_iowake(ioend->io_vnode);
123 mempool_free(ioend, xfs_ioend_pool);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100127 * Buffered IO write completion for delayed allocate extents.
128 * TODO: Update ondisk isize now that we know the file data
129 * has been flushed (i.e. the notorious "NULL file" problem).
130 */
131STATIC void
132xfs_end_bio_delalloc(
133 void *data)
134{
135 xfs_ioend_t *ioend = data;
136
137 xfs_destroy_ioend(ioend);
138}
139
140/*
141 * Buffered IO write completion for regular, written extents.
142 */
143STATIC void
144xfs_end_bio_written(
145 void *data)
146{
147 xfs_ioend_t *ioend = data;
148
149 xfs_destroy_ioend(ioend);
150}
151
152/*
153 * IO write completion for unwritten extents.
154 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 * Issue transactions to convert a buffer range from unwritten
Christoph Hellwigf0973862005-09-05 08:22:52 +1000156 * to written extents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158STATIC void
Christoph Hellwig0829c362005-09-02 16:58:49 +1000159xfs_end_bio_unwritten(
160 void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Christoph Hellwig0829c362005-09-02 16:58:49 +1000162 xfs_ioend_t *ioend = data;
163 vnode_t *vp = ioend->io_vnode;
164 xfs_off_t offset = ioend->io_offset;
165 size_t size = ioend->io_size;
166 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Christoph Hellwig0829c362005-09-02 16:58:49 +1000168 if (ioend->io_uptodate)
169 VOP_BMAP(vp, offset, size, BMAPI_UNWRITTEN, NULL, NULL, error);
170 xfs_destroy_ioend(ioend);
171}
172
173/*
174 * Allocate and initialise an IO completion structure.
175 * We need to track unwritten extent write completion here initially.
176 * We'll need to extend this for updating the ondisk inode size later
177 * (vs. incore size).
178 */
179STATIC xfs_ioend_t *
180xfs_alloc_ioend(
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100181 struct inode *inode,
182 unsigned int type)
Christoph Hellwig0829c362005-09-02 16:58:49 +1000183{
184 xfs_ioend_t *ioend;
185
186 ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
187
188 /*
189 * Set the count to 1 initially, which will prevent an I/O
190 * completion callback from happening before we have started
191 * all the I/O from calling the completion routine too early.
192 */
193 atomic_set(&ioend->io_remaining, 1);
194 ioend->io_uptodate = 1; /* cleared if any I/O fails */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100195 ioend->io_list = NULL;
196 ioend->io_type = type;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000197 ioend->io_vnode = LINVFS_GET_VP(inode);
Christoph Hellwigc1a073b2005-09-05 08:23:35 +1000198 ioend->io_buffer_head = NULL;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100199 ioend->io_buffer_tail = NULL;
Christoph Hellwig0829c362005-09-02 16:58:49 +1000200 atomic_inc(&ioend->io_vnode->v_iocount);
201 ioend->io_offset = 0;
202 ioend->io_size = 0;
203
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100204 if (type == IOMAP_UNWRITTEN)
205 INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten, ioend);
206 else if (type == IOMAP_DELAY)
207 INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc, ioend);
208 else
209 INIT_WORK(&ioend->io_work, xfs_end_bio_written, ioend);
Christoph Hellwig0829c362005-09-02 16:58:49 +1000210
211 return ioend;
212}
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214STATIC int
215xfs_map_blocks(
216 struct inode *inode,
217 loff_t offset,
218 ssize_t count,
219 xfs_iomap_t *mapp,
220 int flags)
221{
222 vnode_t *vp = LINVFS_GET_VP(inode);
223 int error, nmaps = 1;
224
225 VOP_BMAP(vp, offset, count, flags, mapp, &nmaps, error);
226 if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
227 VMODIFY(vp);
228 return -error;
229}
230
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100231STATIC inline int
232xfs_iomap_valid(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 xfs_iomap_t *iomapp,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100234 loff_t offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100236 return offset >= iomapp->iomap_offset &&
237 offset < iomapp->iomap_offset + iomapp->iomap_bsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100240/*
241 * BIO completion handler for buffered IO.
242 */
243STATIC int
244xfs_end_bio(
245 struct bio *bio,
246 unsigned int bytes_done,
247 int error)
248{
249 xfs_ioend_t *ioend = bio->bi_private;
250
251 if (bio->bi_size)
252 return 1;
253
254 ASSERT(ioend);
255 ASSERT(atomic_read(&bio->bi_cnt) >= 1);
256
257 /* Toss bio and pass work off to an xfsdatad thread */
258 if (!test_bit(BIO_UPTODATE, &bio->bi_flags))
259 ioend->io_uptodate = 0;
260 bio->bi_private = NULL;
261 bio->bi_end_io = NULL;
262
263 bio_put(bio);
264 xfs_finish_ioend(ioend);
265 return 0;
266}
267
268STATIC void
269xfs_submit_ioend_bio(
270 xfs_ioend_t *ioend,
271 struct bio *bio)
272{
273 atomic_inc(&ioend->io_remaining);
274
275 bio->bi_private = ioend;
276 bio->bi_end_io = xfs_end_bio;
277
278 submit_bio(WRITE, bio);
279 ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
280 bio_put(bio);
281}
282
283STATIC struct bio *
284xfs_alloc_ioend_bio(
285 struct buffer_head *bh)
286{
287 struct bio *bio;
288 int nvecs = bio_get_nr_vecs(bh->b_bdev);
289
290 do {
291 bio = bio_alloc(GFP_NOIO, nvecs);
292 nvecs >>= 1;
293 } while (!bio);
294
295 ASSERT(bio->bi_private == NULL);
296 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
297 bio->bi_bdev = bh->b_bdev;
298 bio_get(bio);
299 return bio;
300}
301
302STATIC void
303xfs_start_buffer_writeback(
304 struct buffer_head *bh)
305{
306 ASSERT(buffer_mapped(bh));
307 ASSERT(buffer_locked(bh));
308 ASSERT(!buffer_delay(bh));
309 ASSERT(!buffer_unwritten(bh));
310
311 mark_buffer_async_write(bh);
312 set_buffer_uptodate(bh);
313 clear_buffer_dirty(bh);
314}
315
316STATIC void
317xfs_start_page_writeback(
318 struct page *page,
319 struct writeback_control *wbc,
320 int clear_dirty,
321 int buffers)
322{
323 ASSERT(PageLocked(page));
324 ASSERT(!PageWriteback(page));
325 set_page_writeback(page);
326 if (clear_dirty)
327 clear_page_dirty(page);
328 unlock_page(page);
329 if (!buffers) {
330 end_page_writeback(page);
331 wbc->pages_skipped++; /* We didn't write this page */
332 }
333}
334
335static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
336{
337 return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
338}
339
340/*
341 * Submit all of the bios for all of the ioends we have saved up,
342 * covering the initial writepage page and also any probed pages.
343 */
344STATIC void
345xfs_submit_ioend(
346 xfs_ioend_t *ioend)
347{
348 xfs_ioend_t *next;
349 struct buffer_head *bh;
350 struct bio *bio;
351 sector_t lastblock = 0;
352
353 do {
354 next = ioend->io_list;
355 bio = NULL;
356
357 for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
358 xfs_start_buffer_writeback(bh);
359
360 if (!bio) {
361 retry:
362 bio = xfs_alloc_ioend_bio(bh);
363 } else if (bh->b_blocknr != lastblock + 1) {
364 xfs_submit_ioend_bio(ioend, bio);
365 goto retry;
366 }
367
368 if (bio_add_buffer(bio, bh) != bh->b_size) {
369 xfs_submit_ioend_bio(ioend, bio);
370 goto retry;
371 }
372
373 lastblock = bh->b_blocknr;
374 }
375 if (bio)
376 xfs_submit_ioend_bio(ioend, bio);
377 xfs_finish_ioend(ioend);
378 } while ((ioend = next) != NULL);
379}
380
381/*
382 * Cancel submission of all buffer_heads so far in this endio.
383 * Toss the endio too. Only ever called for the initial page
384 * in a writepage request, so only ever one page.
385 */
386STATIC void
387xfs_cancel_ioend(
388 xfs_ioend_t *ioend)
389{
390 xfs_ioend_t *next;
391 struct buffer_head *bh, *next_bh;
392
393 do {
394 next = ioend->io_list;
395 bh = ioend->io_buffer_head;
396 do {
397 next_bh = bh->b_private;
398 clear_buffer_async_write(bh);
399 unlock_buffer(bh);
400 } while ((bh = next_bh) != NULL);
401
402 vn_iowake(ioend->io_vnode);
403 mempool_free(ioend, xfs_ioend_pool);
404 } while ((ioend = next) != NULL);
405}
406
407/*
408 * Test to see if we've been building up a completion structure for
409 * earlier buffers -- if so, we try to append to this ioend if we
410 * can, otherwise we finish off any current ioend and start another.
411 * Return true if we've finished the given ioend.
412 */
413STATIC void
414xfs_add_to_ioend(
415 struct inode *inode,
416 struct buffer_head *bh,
417 unsigned int p_offset,
418 unsigned int type,
419 xfs_ioend_t **result,
420 int need_ioend)
421{
422 xfs_ioend_t *ioend = *result;
423
424 if (!ioend || need_ioend || type != ioend->io_type) {
425 xfs_ioend_t *previous = *result;
426 xfs_off_t offset;
427
428 offset = (xfs_off_t)bh->b_page->index << PAGE_CACHE_SHIFT;
429 offset += p_offset;
430 ioend = xfs_alloc_ioend(inode, type);
431 ioend->io_offset = offset;
432 ioend->io_buffer_head = bh;
433 ioend->io_buffer_tail = bh;
434 if (previous)
435 previous->io_list = ioend;
436 *result = ioend;
437 } else {
438 ioend->io_buffer_tail->b_private = bh;
439 ioend->io_buffer_tail = bh;
440 }
441
442 bh->b_private = NULL;
443 ioend->io_size += bh->b_size;
444}
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446STATIC void
447xfs_map_at_offset(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct buffer_head *bh,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100449 loff_t offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int block_bits,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100451 xfs_iomap_t *iomapp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 xfs_daddr_t bn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int sector_shift;
455
456 ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
457 ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
458 ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 sector_shift = block_bits - BBSHIFT;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100461 bn = (iomapp->iomap_bn >> sector_shift) +
462 ((offset - iomapp->iomap_offset) >> block_bits);
463
464 ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
466
467 lock_buffer(bh);
468 bh->b_blocknr = bn;
Nathan Scottce8e9222006-01-11 15:39:08 +1100469 bh->b_bdev = iomapp->iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 set_buffer_mapped(bh);
471 clear_buffer_delay(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100472 clear_buffer_unwritten(bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475/*
476 * Look for a page at index which is unlocked and not mapped
477 * yet - clustering for mmap write case.
478 */
479STATIC unsigned int
480xfs_probe_unmapped_page(
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100481 struct page *page,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned int pg_offset)
483{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 int ret = 0;
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489 if (page->mapping && PageDirty(page)) {
490 if (page_has_buffers(page)) {
491 struct buffer_head *bh, *head;
492
493 bh = head = page_buffers(page);
494 do {
495 if (buffer_mapped(bh) || !buffer_uptodate(bh))
496 break;
497 ret += bh->b_size;
498 if (ret >= pg_offset)
499 break;
500 } while ((bh = bh->b_this_page) != head);
501 } else
502 ret = PAGE_CACHE_SIZE;
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return ret;
506}
507
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100508STATIC size_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509xfs_probe_unmapped_cluster(
510 struct inode *inode,
511 struct page *startpage,
512 struct buffer_head *bh,
513 struct buffer_head *head)
514{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100515 struct pagevec pvec;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 pgoff_t tindex, tlast, tloff;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100517 size_t total = 0;
518 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 /* First sum forwards in this page */
521 do {
522 if (buffer_mapped(bh))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100523 return total;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 total += bh->b_size;
525 } while ((bh = bh->b_this_page) != head);
526
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100527 /* if we reached the end of the page, sum forwards in following pages */
528 tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
529 tindex = startpage->index + 1;
530
531 /* Prune this back to avoid pathological behavior */
532 tloff = min(tlast, startpage->index + 64);
533
534 pagevec_init(&pvec, 0);
535 while (!done && tindex <= tloff) {
536 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
537
538 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
539 break;
540
541 for (i = 0; i < pagevec_count(&pvec); i++) {
542 struct page *page = pvec.pages[i];
543 size_t pg_offset, len = 0;
544
545 if (tindex == tlast) {
546 pg_offset =
547 i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100548 if (!pg_offset) {
549 done = 1;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100550 break;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100551 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100552 } else
553 pg_offset = PAGE_CACHE_SIZE;
554
555 if (page->index == tindex && !TestSetPageLocked(page)) {
556 len = xfs_probe_unmapped_page(page, pg_offset);
557 unlock_page(page);
558 }
559
560 if (!len) {
561 done = 1;
562 break;
563 }
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 total += len;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100566 tindex++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100568
569 pagevec_release(&pvec);
570 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 return total;
574}
575
576/*
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100577 * Test if a given page is suitable for writing as part of an unwritten
578 * or delayed allocate extent.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100580STATIC int
581xfs_is_delayed_page(
582 struct page *page,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100583 unsigned int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (PageWriteback(page))
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100586 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (page->mapping && page_has_buffers(page)) {
589 struct buffer_head *bh, *head;
590 int acceptable = 0;
591
592 bh = head = page_buffers(page);
593 do {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100594 if (buffer_unwritten(bh))
595 acceptable = (type == IOMAP_UNWRITTEN);
596 else if (buffer_delay(bh))
597 acceptable = (type == IOMAP_DELAY);
598 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 } while ((bh = bh->b_this_page) != head);
601
602 if (acceptable)
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100603 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609/*
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 */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100615STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616xfs_convert_page(
617 struct inode *inode,
618 struct page *page,
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100619 loff_t tindex,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100620 xfs_iomap_t *mp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100621 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct writeback_control *wbc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 int startio,
624 int all_bh)
625{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100626 struct buffer_head *bh, *head;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100627 xfs_off_t end_offset;
628 unsigned long p_offset;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100629 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 int bbits = inode->i_blkbits;
Nathan Scott24e17b52005-05-05 13:33:20 -0700631 int len, page_dirty;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100632 int count = 0, done = 0, uptodate = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100633 xfs_off_t offset = page_offset(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100635 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
Nathan Scott24e17b52005-05-05 13:33:20 -0700646 /*
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.
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100649 *
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.
Nathan Scott24e17b52005-05-05 13:33:20 -0700658 */
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100659 end_offset = min_t(unsigned long long,
660 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
661 i_size_read(inode));
662
Nathan Scott24e17b52005-05-05 13:33:20 -0700663 len = 1 << inode->i_blkbits;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100664 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;
Nathan Scott24e17b52005-05-05 13:33:20 -0700668
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100669 p_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 bh = head = page_buffers(page);
671 do {
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100672 if (offset >= end_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100674 if (!buffer_uptodate(bh))
675 uptodate = 0;
676 if (!(PageUptodate(page) || buffer_uptodate(bh))) {
677 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100679 }
680
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100681 if (buffer_unwritten(bh) || buffer_delay(bh)) {
682 if (buffer_unwritten(bh))
683 type = IOMAP_UNWRITTEN;
684 else
685 type = IOMAP_DELAY;
686
687 if (!xfs_iomap_valid(mp, offset)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100688 done = 1;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100689 continue;
690 }
691
692 ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
693 ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
694
695 xfs_map_at_offset(bh, offset, bbits, mp);
696 if (startio) {
697 xfs_add_to_ioend(inode, bh, p_offset,
698 type, ioendp, done);
699 } else {
700 set_buffer_dirty(bh);
701 unlock_buffer(bh);
702 mark_buffer_dirty(bh);
703 }
704 page_dirty--;
705 count++;
706 } else {
707 type = 0;
708 if (buffer_mapped(bh) && all_bh && startio) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 lock_buffer(bh);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100710 xfs_add_to_ioend(inode, bh, p_offset,
711 type, ioendp, done);
712 count++;
Nathan Scott24e17b52005-05-05 13:33:20 -0700713 page_dirty--;
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100714 } else {
715 done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 }
Christoph Hellwig9260dc62006-01-11 20:48:47 +1100718 } while (offset += len, p_offset += len,
719 (bh = bh->b_this_page) != head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100721 if (uptodate && bh == head)
722 SetPageUptodate(page);
723
724 if (startio) {
725 if (count)
726 wbc->nr_to_write--;
727 xfs_start_page_writeback(page, wbc, !page_dirty, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100729
730 return done;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100731 fail_unlock_page:
732 unlock_page(page);
733 fail:
734 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
737/*
738 * Convert & write out a cluster of pages in the same extent as defined
739 * by mp and following the start page.
740 */
741STATIC void
742xfs_cluster_write(
743 struct inode *inode,
744 pgoff_t tindex,
745 xfs_iomap_t *iomapp,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100746 xfs_ioend_t **ioendp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 struct writeback_control *wbc,
748 int startio,
749 int all_bh,
750 pgoff_t tlast)
751{
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100752 struct pagevec pvec;
753 int done = 0, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100755 pagevec_init(&pvec, 0);
756 while (!done && tindex <= tlast) {
757 unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
758
759 if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 break;
Christoph Hellwig10ce4442006-01-11 20:48:14 +1100761
762 for (i = 0; i < pagevec_count(&pvec); i++) {
763 done = xfs_convert_page(inode, pvec.pages[i], tindex++,
764 iomapp, ioendp, wbc, startio, all_bh);
765 if (done)
766 break;
767 }
768
769 pagevec_release(&pvec);
770 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 }
772}
773
774/*
775 * Calling this without startio set means we are being asked to make a dirty
776 * page ready for freeing it's buffers. When called with startio set then
777 * we are coming from writepage.
778 *
779 * When called with startio set it is important that we write the WHOLE
780 * page if possible.
781 * The bh->b_state's cannot know if any of the blocks or which block for
782 * that matter are dirty due to mmap writes, and therefore bh uptodate is
783 * only vaild if the page itself isn't completely uptodate. Some layers
784 * may clear the page dirty flag prior to calling write page, under the
785 * assumption the entire page will be written out; by not writing out the
786 * whole page the page can be reused before all valid dirty data is
787 * written out. Note: in the case of a page that has been dirty'd by
788 * mapwrite and but partially setup by block_prepare_write the
789 * bh->b_states's will not agree and only ones setup by BPW/BCW will have
790 * valid state, thus the whole page must be written out thing.
791 */
792
793STATIC int
794xfs_page_state_convert(
795 struct inode *inode,
796 struct page *page,
797 struct writeback_control *wbc,
798 int startio,
799 int unmapped) /* also implies page uptodate */
800{
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100801 struct buffer_head *bh, *head;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100802 xfs_iomap_t iomap;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100803 xfs_ioend_t *ioend = NULL, *iohead = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 loff_t offset;
805 unsigned long p_offset = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100806 unsigned int type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 __uint64_t end_offset;
808 pgoff_t end_index, last_index, tlast;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100809 ssize_t size, len;
810 int flags, err, iomap_valid = 0, uptodate = 1;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100811 int page_dirty, count = 0, trylock_flag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Daniel Moore3ba08152005-05-05 13:31:34 -0700813 /* wait for other IO threads? */
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100814 if (startio && wbc->sync_mode != WB_SYNC_NONE)
815 trylock_flag |= BMAPI_TRYLOCK;
Daniel Moore3ba08152005-05-05 13:31:34 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* Is this page beyond the end of the file? */
818 offset = i_size_read(inode);
819 end_index = offset >> PAGE_CACHE_SHIFT;
820 last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
821 if (page->index >= end_index) {
822 if ((page->index >= end_index + 1) ||
823 !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
Nathan Scott19d5bcf2005-11-02 15:14:09 +1100824 if (startio)
825 unlock_page(page);
826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 /*
Nathan Scott24e17b52005-05-05 13:33:20 -0700831 * page_dirty is initially a count of buffers on the page before
832 * EOF and is decrememted as we move each into a cleanable state.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100833 *
834 * Derivation:
835 *
836 * End offset is the highest offset that this page should represent.
837 * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
838 * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
839 * hence give us the correct page_dirty count. On any other page,
840 * it will be zero and in that case we need page_dirty to be the
841 * count of buffers on the page.
842 */
843 end_offset = min_t(unsigned long long,
844 (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
Nathan Scott24e17b52005-05-05 13:33:20 -0700845 len = 1 << inode->i_blkbits;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100846 p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
847 PAGE_CACHE_SIZE);
848 p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
Nathan Scott24e17b52005-05-05 13:33:20 -0700849 page_dirty = p_offset / len;
850
Nathan Scott24e17b52005-05-05 13:33:20 -0700851 bh = head = page_buffers(page);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100852 offset = page_offset(page);
853
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100854 /* TODO: cleanup count and page_dirty */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 do {
857 if (offset >= end_offset)
858 break;
859 if (!buffer_uptodate(bh))
860 uptodate = 0;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100861 if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100862 /*
863 * the iomap is actually still valid, but the ioend
864 * isn't. shouldn't happen too often.
865 */
866 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 continue;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100870 if (iomap_valid)
871 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 /*
874 * First case, map an unwritten extent and prepare for
875 * extent state conversion transaction on completion.
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100876 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 * Second case, allocate space for a delalloc buffer.
878 * We can return EAGAIN here in the release page case.
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100879 *
880 * Third case, an unmapped buffer was found, and we are
881 * in a path where we need to write the whole page out.
882 */
883 if (buffer_unwritten(bh) || buffer_delay(bh) ||
884 ((buffer_uptodate(bh) || PageUptodate(page)) &&
885 !buffer_mapped(bh) && (unmapped || startio))) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100886 if (buffer_unwritten(bh)) {
887 type = IOMAP_UNWRITTEN;
888 flags = BMAPI_WRITE|BMAPI_IGNSTATE;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100889 } else if (buffer_delay(bh)) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100890 type = IOMAP_DELAY;
891 flags = BMAPI_ALLOCATE;
892 if (!startio)
893 flags |= trylock_flag;
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100894 } else {
895 type = 0;
896 flags = BMAPI_WRITE|BMAPI_MMAP;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100897 }
898
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100899 if (!iomap_valid) {
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100900 if (type == 0) {
901 size = xfs_probe_unmapped_cluster(inode,
902 page, bh, head);
903 } else {
904 size = len;
905 }
906
907 err = xfs_map_blocks(inode, offset, size,
908 &iomap, flags);
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100909 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto error;
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100911 iomap_valid = xfs_iomap_valid(&iomap, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100913 if (iomap_valid) {
914 xfs_map_at_offset(bh, offset,
915 inode->i_blkbits, &iomap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (startio) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100917 xfs_add_to_ioend(inode, bh, p_offset,
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100918 type, &ioend,
919 !iomap_valid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 } else {
921 set_buffer_dirty(bh);
922 unlock_buffer(bh);
923 mark_buffer_dirty(bh);
924 }
925 page_dirty--;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100926 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100928 } else if (buffer_uptodate(bh) && startio) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100929 type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100931 if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
932 ASSERT(buffer_mapped(bh));
933 xfs_add_to_ioend(inode,
934 bh, p_offset, type,
935 &ioend, !iomap_valid);
936 page_dirty--;
937 count++;
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100938 } else {
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100939 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Christoph Hellwigd5cb48a2006-01-11 20:49:02 +1100941 } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
942 (unmapped || startio)) {
943 iomap_valid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100945
946 if (!iohead)
947 iohead = ioend;
948
949 } while (offset += len, ((bh = bh->b_this_page) != head));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
951 if (uptodate && bh == head)
952 SetPageUptodate(page);
953
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100954 if (startio)
955 xfs_start_page_writeback(page, wbc, 1, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100957 if (ioend && iomap_valid) {
958 offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 PAGE_CACHE_SHIFT;
Nathan Scott775bf6c2005-05-05 13:33:01 -0700960 tlast = min_t(pgoff_t, offset, last_index);
Christoph Hellwig1defeac2006-01-11 20:48:33 +1100961 xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100962 wbc, startio, unmapped, tlast);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100965 if (iohead)
966 xfs_submit_ioend(iohead);
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return page_dirty;
969
970error:
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100971 if (iohead)
972 xfs_cancel_ioend(iohead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
974 /*
975 * If it's delalloc and we have nowhere to put it,
976 * throw it away, unless the lower layers told
977 * us to try again.
978 */
979 if (err != -EAGAIN) {
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +1100980 if (!unmapped)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 block_invalidatepage(page, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 ClearPageUptodate(page);
983 }
984 return err;
985}
986
987STATIC int
988__linvfs_get_block(
989 struct inode *inode,
990 sector_t iblock,
991 unsigned long blocks,
992 struct buffer_head *bh_result,
993 int create,
994 int direct,
995 bmapi_flags_t flags)
996{
997 vnode_t *vp = LINVFS_GET_VP(inode);
998 xfs_iomap_t iomap;
Nathan Scottfdc7ed72005-11-02 15:13:13 +1100999 xfs_off_t offset;
1000 ssize_t size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 int retpbbm = 1;
1002 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001004 offset = (xfs_off_t)iblock << inode->i_blkbits;
Nathan Scotta4656392005-11-25 16:41:57 +11001005 if (blocks)
1006 size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
1007 (xfs_off_t)blocks << inode->i_blkbits);
1008 else
1009 size = 1 << inode->i_blkbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 VOP_BMAP(vp, offset, size,
1012 create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
1013 if (error)
1014 return -error;
1015
1016 if (retpbbm == 0)
1017 return 0;
1018
1019 if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001020 xfs_daddr_t bn;
1021 xfs_off_t delta;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
1023 /* For unwritten extents do not report a disk address on
1024 * the read case (treat as if we're reading into a hole).
1025 */
1026 if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1027 delta = offset - iomap.iomap_offset;
1028 delta >>= inode->i_blkbits;
1029
1030 bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
1031 bn += delta;
1032 BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
1033 bh_result->b_blocknr = bn;
1034 set_buffer_mapped(bh_result);
1035 }
1036 if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
1037 if (direct)
1038 bh_result->b_private = inode;
1039 set_buffer_unwritten(bh_result);
1040 set_buffer_delay(bh_result);
1041 }
1042 }
1043
1044 /* If this is a realtime file, data might be on a new device */
Nathan Scottce8e9222006-01-11 15:39:08 +11001045 bh_result->b_bdev = iomap.iomap_target->bt_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
1047 /* If we previously allocated a block out beyond eof and
1048 * we are now coming back to use it then we will need to
1049 * flag it as new even if it has a disk address.
1050 */
1051 if (create &&
1052 ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001053 (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 set_buffer_new(bh_result);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 if (iomap.iomap_flags & IOMAP_DELAY) {
1057 BUG_ON(direct);
1058 if (create) {
1059 set_buffer_uptodate(bh_result);
1060 set_buffer_mapped(bh_result);
1061 set_buffer_delay(bh_result);
1062 }
1063 }
1064
1065 if (blocks) {
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001066 ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
1067 offset = min_t(xfs_off_t,
1068 iomap.iomap_bsize - iomap.iomap_delta,
Nathan Scotta4656392005-11-25 16:41:57 +11001069 (xfs_off_t)blocks << inode->i_blkbits);
Nathan Scottfdc7ed72005-11-02 15:13:13 +11001070 bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072
1073 return 0;
1074}
1075
1076int
1077linvfs_get_block(
1078 struct inode *inode,
1079 sector_t iblock,
1080 struct buffer_head *bh_result,
1081 int create)
1082{
1083 return __linvfs_get_block(inode, iblock, 0, bh_result,
1084 create, 0, BMAPI_WRITE);
1085}
1086
1087STATIC int
1088linvfs_get_blocks_direct(
1089 struct inode *inode,
1090 sector_t iblock,
1091 unsigned long max_blocks,
1092 struct buffer_head *bh_result,
1093 int create)
1094{
1095 return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
1096 create, 1, BMAPI_WRITE|BMAPI_DIRECT);
1097}
1098
Christoph Hellwigf0973862005-09-05 08:22:52 +10001099STATIC void
1100linvfs_end_io_direct(
1101 struct kiocb *iocb,
1102 loff_t offset,
1103 ssize_t size,
1104 void *private)
1105{
1106 xfs_ioend_t *ioend = iocb->private;
1107
1108 /*
1109 * Non-NULL private data means we need to issue a transaction to
1110 * convert a range from unwritten to written extents. This needs
1111 * to happen from process contect but aio+dio I/O completion
1112 * happens from irq context so we need to defer it to a workqueue.
1113 * This is not nessecary for synchronous direct I/O, but we do
1114 * it anyway to keep the code uniform and simpler.
1115 *
1116 * The core direct I/O code might be changed to always call the
1117 * completion handler in the future, in which case all this can
1118 * go away.
1119 */
1120 if (private && size > 0) {
1121 ioend->io_offset = offset;
1122 ioend->io_size = size;
1123 xfs_finish_ioend(ioend);
1124 } else {
1125 ASSERT(size >= 0);
1126 xfs_destroy_ioend(ioend);
1127 }
1128
1129 /*
1130 * blockdev_direct_IO can return an error even afer the I/O
1131 * completion handler was called. Thus we need to protect
1132 * against double-freeing.
1133 */
1134 iocb->private = NULL;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137STATIC ssize_t
1138linvfs_direct_IO(
1139 int rw,
1140 struct kiocb *iocb,
1141 const struct iovec *iov,
1142 loff_t offset,
1143 unsigned long nr_segs)
1144{
1145 struct file *file = iocb->ki_filp;
1146 struct inode *inode = file->f_mapping->host;
1147 vnode_t *vp = LINVFS_GET_VP(inode);
1148 xfs_iomap_t iomap;
1149 int maps = 1;
1150 int error;
Christoph Hellwigf0973862005-09-05 08:22:52 +10001151 ssize_t ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
1154 if (error)
1155 return -error;
1156
Christoph Hellwigf6d6d4f2006-01-11 15:40:13 +11001157 iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
Christoph Hellwigf0973862005-09-05 08:22:52 +10001158
1159 ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
Nathan Scottce8e9222006-01-11 15:39:08 +11001160 iomap.iomap_target->bt_bdev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 iov, offset, nr_segs,
1162 linvfs_get_blocks_direct,
Christoph Hellwigf0973862005-09-05 08:22:52 +10001163 linvfs_end_io_direct);
1164
1165 if (unlikely(ret <= 0 && iocb->private))
1166 xfs_destroy_ioend(iocb->private);
1167 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168}
1169
1170
1171STATIC sector_t
1172linvfs_bmap(
1173 struct address_space *mapping,
1174 sector_t block)
1175{
1176 struct inode *inode = (struct inode *)mapping->host;
1177 vnode_t *vp = LINVFS_GET_VP(inode);
1178 int error;
1179
1180 vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
1181
1182 VOP_RWLOCK(vp, VRWLOCK_READ);
1183 VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
1184 VOP_RWUNLOCK(vp, VRWLOCK_READ);
1185 return generic_block_bmap(mapping, block, linvfs_get_block);
1186}
1187
1188STATIC int
1189linvfs_readpage(
1190 struct file *unused,
1191 struct page *page)
1192{
1193 return mpage_readpage(page, linvfs_get_block);
1194}
1195
1196STATIC int
1197linvfs_readpages(
1198 struct file *unused,
1199 struct address_space *mapping,
1200 struct list_head *pages,
1201 unsigned nr_pages)
1202{
1203 return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
1204}
1205
1206STATIC void
1207xfs_count_page_state(
1208 struct page *page,
1209 int *delalloc,
1210 int *unmapped,
1211 int *unwritten)
1212{
1213 struct buffer_head *bh, *head;
1214
1215 *delalloc = *unmapped = *unwritten = 0;
1216
1217 bh = head = page_buffers(page);
1218 do {
1219 if (buffer_uptodate(bh) && !buffer_mapped(bh))
1220 (*unmapped) = 1;
1221 else if (buffer_unwritten(bh) && !buffer_delay(bh))
1222 clear_buffer_unwritten(bh);
1223 else if (buffer_unwritten(bh))
1224 (*unwritten) = 1;
1225 else if (buffer_delay(bh))
1226 (*delalloc) = 1;
1227 } while ((bh = bh->b_this_page) != head);
1228}
1229
1230
1231/*
1232 * writepage: Called from one of two places:
1233 *
1234 * 1. we are flushing a delalloc buffer head.
1235 *
1236 * 2. we are writing out a dirty page. Typically the page dirty
1237 * state is cleared before we get here. In this case is it
1238 * conceivable we have no buffer heads.
1239 *
1240 * For delalloc space on the page we need to allocate space and
1241 * flush it. For unmapped buffer heads on the page we should
1242 * allocate space if the page is uptodate. For any other dirty
1243 * buffer heads on the page we should flush them.
1244 *
1245 * If we detect that a transaction would be required to flush
1246 * the page, we have to check the process flags first, if we
1247 * are already in a transaction or disk I/O during allocations
1248 * is off, we need to fail the writepage and redirty the page.
1249 */
1250
1251STATIC int
1252linvfs_writepage(
1253 struct page *page,
1254 struct writeback_control *wbc)
1255{
1256 int error;
1257 int need_trans;
1258 int delalloc, unmapped, unwritten;
1259 struct inode *inode = page->mapping->host;
1260
1261 xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
1262
1263 /*
1264 * We need a transaction if:
1265 * 1. There are delalloc buffers on the page
1266 * 2. The page is uptodate and we have unmapped buffers
1267 * 3. The page is uptodate and we have no buffers
1268 * 4. There are unwritten buffers on the page
1269 */
1270
1271 if (!page_has_buffers(page)) {
1272 unmapped = 1;
1273 need_trans = 1;
1274 } else {
1275 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1276 if (!PageUptodate(page))
1277 unmapped = 0;
1278 need_trans = delalloc + unmapped + unwritten;
1279 }
1280
1281 /*
1282 * If we need a transaction and the process flags say
1283 * we are already in a transaction, or no IO is allowed
1284 * then mark the page dirty again and leave the page
1285 * as is.
1286 */
1287 if (PFLAGS_TEST_FSTRANS() && need_trans)
1288 goto out_fail;
1289
1290 /*
1291 * Delay hooking up buffer heads until we have
1292 * made our go/no-go decision.
1293 */
1294 if (!page_has_buffers(page))
1295 create_empty_buffers(page, 1 << inode->i_blkbits, 0);
1296
1297 /*
1298 * Convert delayed allocate, unwritten or unmapped space
1299 * to real space and flush out to disk.
1300 */
1301 error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
1302 if (error == -EAGAIN)
1303 goto out_fail;
1304 if (unlikely(error < 0))
1305 goto out_unlock;
1306
1307 return 0;
1308
1309out_fail:
1310 redirty_page_for_writepage(wbc, page);
1311 unlock_page(page);
1312 return 0;
1313out_unlock:
1314 unlock_page(page);
1315 return error;
1316}
1317
Nathan Scottbcec2b72005-09-02 16:40:17 +10001318STATIC int
1319linvfs_invalidate_page(
1320 struct page *page,
1321 unsigned long offset)
1322{
1323 xfs_page_trace(XFS_INVALIDPAGE_ENTER,
1324 page->mapping->host, page, offset);
1325 return block_invalidatepage(page, offset);
1326}
1327
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328/*
1329 * Called to move a page into cleanable state - and from there
1330 * to be released. Possibly the page is already clean. We always
1331 * have buffer heads in this call.
1332 *
1333 * Returns 0 if the page is ok to release, 1 otherwise.
1334 *
1335 * Possible scenarios are:
1336 *
1337 * 1. We are being called to release a page which has been written
1338 * to via regular I/O. buffer heads will be dirty and possibly
1339 * delalloc. If no delalloc buffer heads in this case then we
1340 * can just return zero.
1341 *
1342 * 2. We are called to release a page which has been written via
1343 * mmap, all we need to do is ensure there is no delalloc
1344 * state in the buffer heads, if not we can let the caller
1345 * free them and we should come back later via writepage.
1346 */
1347STATIC int
1348linvfs_release_page(
1349 struct page *page,
Al Viro27496a82005-10-21 03:20:48 -04001350 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
1352 struct inode *inode = page->mapping->host;
1353 int dirty, delalloc, unmapped, unwritten;
1354 struct writeback_control wbc = {
1355 .sync_mode = WB_SYNC_ALL,
1356 .nr_to_write = 1,
1357 };
1358
1359 xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
1360
1361 xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
1362 if (!delalloc && !unwritten)
1363 goto free_buffers;
1364
1365 if (!(gfp_mask & __GFP_FS))
1366 return 0;
1367
1368 /* If we are already inside a transaction or the thread cannot
1369 * do I/O, we cannot release this page.
1370 */
1371 if (PFLAGS_TEST_FSTRANS())
1372 return 0;
1373
1374 /*
1375 * Convert delalloc space to real space, do not flush the
1376 * data out to disk, that will be done by the caller.
1377 * Never need to allocate space here - we will always
1378 * come back to writepage in that case.
1379 */
1380 dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
1381 if (dirty == 0 && !unwritten)
1382 goto free_buffers;
1383 return 0;
1384
1385free_buffers:
1386 return try_to_free_buffers(page);
1387}
1388
1389STATIC int
1390linvfs_prepare_write(
1391 struct file *file,
1392 struct page *page,
1393 unsigned int from,
1394 unsigned int to)
1395{
1396 return block_prepare_write(page, from, to, linvfs_get_block);
1397}
1398
1399struct address_space_operations linvfs_aops = {
1400 .readpage = linvfs_readpage,
1401 .readpages = linvfs_readpages,
1402 .writepage = linvfs_writepage,
1403 .sync_page = block_sync_page,
1404 .releasepage = linvfs_release_page,
Nathan Scottbcec2b72005-09-02 16:40:17 +10001405 .invalidatepage = linvfs_invalidate_page,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 .prepare_write = linvfs_prepare_write,
1407 .commit_write = generic_commit_write,
1408 .bmap = linvfs_bmap,
1409 .direct_IO = linvfs_direct_IO,
1410};