]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_bit.h" |
1da177e4 | 20 | #include "xfs_log.h" |
a844f451 | 21 | #include "xfs_inum.h" |
1da177e4 | 22 | #include "xfs_sb.h" |
a844f451 | 23 | #include "xfs_ag.h" |
1da177e4 LT |
24 | #include "xfs_dir.h" |
25 | #include "xfs_dir2.h" | |
26 | #include "xfs_trans.h" | |
27 | #include "xfs_dmapi.h" | |
28 | #include "xfs_mount.h" | |
29 | #include "xfs_bmap_btree.h" | |
30 | #include "xfs_alloc_btree.h" | |
31 | #include "xfs_ialloc_btree.h" | |
1da177e4 LT |
32 | #include "xfs_dir_sf.h" |
33 | #include "xfs_dir2_sf.h" | |
a844f451 | 34 | #include "xfs_attr_sf.h" |
1da177e4 LT |
35 | #include "xfs_dinode.h" |
36 | #include "xfs_inode.h" | |
a844f451 NS |
37 | #include "xfs_alloc.h" |
38 | #include "xfs_btree.h" | |
1da177e4 LT |
39 | #include "xfs_error.h" |
40 | #include "xfs_rw.h" | |
41 | #include "xfs_iomap.h" | |
42 | #include <linux/mpage.h> | |
10ce4444 | 43 | #include <linux/pagevec.h> |
1da177e4 LT |
44 | #include <linux/writeback.h> |
45 | ||
46 | STATIC void xfs_count_page_state(struct page *, int *, int *, int *); | |
1da177e4 LT |
47 | |
48 | #if defined(XFS_RW_TRACE) | |
49 | void | |
50 | xfs_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); | |
f6d6d4fc | 60 | loff_t offset = page_offset(page); |
1da177e4 LT |
61 | 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 | ||
0829c360 CH |
93 | /* |
94 | * Schedule IO completion handling on a xfsdatad if this was | |
95 | * the final hold on this ioend. | |
96 | */ | |
97 | STATIC void | |
98 | xfs_finish_ioend( | |
99 | xfs_ioend_t *ioend) | |
100 | { | |
101 | if (atomic_dec_and_test(&ioend->io_remaining)) | |
102 | queue_work(xfsdatad_workqueue, &ioend->io_work); | |
103 | } | |
104 | ||
f6d6d4fc CH |
105 | /* |
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 | */ | |
0829c360 CH |
111 | STATIC void |
112 | xfs_destroy_ioend( | |
113 | xfs_ioend_t *ioend) | |
114 | { | |
f6d6d4fc CH |
115 | 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 | ||
0829c360 CH |
122 | vn_iowake(ioend->io_vnode); |
123 | mempool_free(ioend, xfs_ioend_pool); | |
124 | } | |
125 | ||
126 | /* | |
f6d6d4fc CH |
127 | * 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 | */ | |
131 | STATIC void | |
132 | xfs_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 | */ | |
143 | STATIC void | |
144 | xfs_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 | * | |
0829c360 | 155 | * Issue transactions to convert a buffer range from unwritten |
f0973863 | 156 | * to written extents. |
0829c360 CH |
157 | */ |
158 | STATIC void | |
159 | xfs_end_bio_unwritten( | |
160 | void *data) | |
161 | { | |
162 | 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; | |
167 | ||
168 | 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 | */ | |
179 | STATIC xfs_ioend_t * | |
180 | xfs_alloc_ioend( | |
f6d6d4fc CH |
181 | struct inode *inode, |
182 | unsigned int type) | |
0829c360 CH |
183 | { |
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 */ | |
f6d6d4fc CH |
195 | ioend->io_list = NULL; |
196 | ioend->io_type = type; | |
0829c360 | 197 | ioend->io_vnode = LINVFS_GET_VP(inode); |
c1a073bd | 198 | ioend->io_buffer_head = NULL; |
f6d6d4fc | 199 | ioend->io_buffer_tail = NULL; |
0829c360 CH |
200 | atomic_inc(&ioend->io_vnode->v_iocount); |
201 | ioend->io_offset = 0; | |
202 | ioend->io_size = 0; | |
203 | ||
f6d6d4fc CH |
204 | 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); | |
0829c360 CH |
210 | |
211 | return ioend; | |
212 | } | |
213 | ||
1da177e4 LT |
214 | STATIC int |
215 | xfs_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 | ||
1defeac9 CH |
231 | STATIC inline int |
232 | xfs_iomap_valid( | |
1da177e4 | 233 | xfs_iomap_t *iomapp, |
1defeac9 | 234 | loff_t offset) |
1da177e4 | 235 | { |
1defeac9 CH |
236 | return offset >= iomapp->iomap_offset && |
237 | offset < iomapp->iomap_offset + iomapp->iomap_bsize; | |
1da177e4 LT |
238 | } |
239 | ||
f6d6d4fc CH |
240 | /* |
241 | * BIO completion handler for buffered IO. | |
242 | */ | |
243 | STATIC int | |
244 | xfs_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 | ||
268 | STATIC void | |
269 | xfs_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 | ||
283 | STATIC struct bio * | |
284 | xfs_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 | ||
302 | STATIC void | |
303 | xfs_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 | ||
316 | STATIC void | |
317 | xfs_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 | ||
335 | static 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 | */ | |
344 | STATIC void | |
345 | xfs_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 | */ | |
386 | STATIC void | |
387 | xfs_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 | */ | |
413 | STATIC void | |
414 | xfs_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 | ||
1da177e4 LT |
446 | STATIC void |
447 | xfs_map_at_offset( | |
1da177e4 | 448 | struct buffer_head *bh, |
1defeac9 | 449 | loff_t offset, |
1da177e4 | 450 | int block_bits, |
1defeac9 | 451 | xfs_iomap_t *iomapp) |
1da177e4 LT |
452 | { |
453 | xfs_daddr_t bn; | |
1da177e4 LT |
454 | 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 | ||
1da177e4 | 460 | sector_shift = block_bits - BBSHIFT; |
1defeac9 CH |
461 | bn = (iomapp->iomap_bn >> sector_shift) + |
462 | ((offset - iomapp->iomap_offset) >> block_bits); | |
463 | ||
464 | ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME)); | |
1da177e4 LT |
465 | ASSERT((bn << sector_shift) >= iomapp->iomap_bn); |
466 | ||
467 | lock_buffer(bh); | |
468 | bh->b_blocknr = bn; | |
ce8e922c | 469 | bh->b_bdev = iomapp->iomap_target->bt_bdev; |
1da177e4 LT |
470 | set_buffer_mapped(bh); |
471 | clear_buffer_delay(bh); | |
f6d6d4fc | 472 | clear_buffer_unwritten(bh); |
1da177e4 LT |
473 | } |
474 | ||
475 | /* | |
476 | * Look for a page at index which is unlocked and not mapped | |
477 | * yet - clustering for mmap write case. | |
478 | */ | |
479 | STATIC unsigned int | |
480 | xfs_probe_unmapped_page( | |
10ce4444 | 481 | struct page *page, |
1da177e4 LT |
482 | unsigned int pg_offset) |
483 | { | |
1da177e4 LT |
484 | int ret = 0; |
485 | ||
1da177e4 | 486 | if (PageWriteback(page)) |
10ce4444 | 487 | return 0; |
1da177e4 LT |
488 | |
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 | ||
1da177e4 LT |
505 | return ret; |
506 | } | |
507 | ||
f6d6d4fc | 508 | STATIC size_t |
1da177e4 LT |
509 | xfs_probe_unmapped_cluster( |
510 | struct inode *inode, | |
511 | struct page *startpage, | |
512 | struct buffer_head *bh, | |
513 | struct buffer_head *head) | |
514 | { | |
10ce4444 | 515 | struct pagevec pvec; |
1da177e4 | 516 | pgoff_t tindex, tlast, tloff; |
10ce4444 CH |
517 | size_t total = 0; |
518 | int done = 0, i; | |
1da177e4 LT |
519 | |
520 | /* First sum forwards in this page */ | |
521 | do { | |
522 | if (buffer_mapped(bh)) | |
10ce4444 | 523 | return total; |
1da177e4 LT |
524 | total += bh->b_size; |
525 | } while ((bh = bh->b_this_page) != head); | |
526 | ||
10ce4444 CH |
527 | /* 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); | |
1defeac9 CH |
548 | if (!pg_offset) { |
549 | done = 1; | |
10ce4444 | 550 | break; |
1defeac9 | 551 | } |
10ce4444 CH |
552 | } 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 | ||
1da177e4 | 565 | total += len; |
1defeac9 | 566 | tindex++; |
1da177e4 | 567 | } |
10ce4444 CH |
568 | |
569 | pagevec_release(&pvec); | |
570 | cond_resched(); | |
1da177e4 | 571 | } |
10ce4444 | 572 | |
1da177e4 LT |
573 | return total; |
574 | } | |
575 | ||
576 | /* | |
10ce4444 CH |
577 | * Test if a given page is suitable for writing as part of an unwritten |
578 | * or delayed allocate extent. | |
1da177e4 | 579 | */ |
10ce4444 CH |
580 | STATIC int |
581 | xfs_is_delayed_page( | |
582 | struct page *page, | |
f6d6d4fc | 583 | unsigned int type) |
1da177e4 | 584 | { |
1da177e4 | 585 | if (PageWriteback(page)) |
10ce4444 | 586 | return 0; |
1da177e4 LT |
587 | |
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 { | |
f6d6d4fc CH |
594 | if (buffer_unwritten(bh)) |
595 | acceptable = (type == IOMAP_UNWRITTEN); | |
596 | else if (buffer_delay(bh)) | |
597 | acceptable = (type == IOMAP_DELAY); | |
598 | else | |
1da177e4 | 599 | break; |
1da177e4 LT |
600 | } while ((bh = bh->b_this_page) != head); |
601 | ||
602 | if (acceptable) | |
10ce4444 | 603 | return 1; |
1da177e4 LT |
604 | } |
605 | ||
10ce4444 | 606 | return 0; |
1da177e4 LT |
607 | } |
608 | ||
1da177e4 LT |
609 | /* |
610 | * Allocate & map buffers for page given the extent map. Write it out. | |
611 | * except for the original page of a writepage, this is called on | |
612 | * delalloc/unwritten pages only, for the original page it is possible | |
613 | * that the page has no mapping at all. | |
614 | */ | |
f6d6d4fc | 615 | STATIC int |
1da177e4 LT |
616 | xfs_convert_page( |
617 | struct inode *inode, | |
618 | struct page *page, | |
10ce4444 | 619 | loff_t tindex, |
1defeac9 | 620 | xfs_iomap_t *mp, |
f6d6d4fc | 621 | xfs_ioend_t **ioendp, |
1da177e4 | 622 | struct writeback_control *wbc, |
1da177e4 LT |
623 | int startio, |
624 | int all_bh) | |
625 | { | |
f6d6d4fc | 626 | struct buffer_head *bh, *head; |
9260dc6b CH |
627 | xfs_off_t end_offset; |
628 | unsigned long p_offset; | |
f6d6d4fc | 629 | unsigned int type; |
1da177e4 | 630 | int bbits = inode->i_blkbits; |
24e17b5f | 631 | int len, page_dirty; |
f6d6d4fc | 632 | int count = 0, done = 0, uptodate = 1; |
9260dc6b | 633 | xfs_off_t offset = page_offset(page); |
1da177e4 | 634 | |
10ce4444 CH |
635 | if (page->index != tindex) |
636 | goto fail; | |
637 | if (TestSetPageLocked(page)) | |
638 | goto fail; | |
639 | if (PageWriteback(page)) | |
640 | goto fail_unlock_page; | |
641 | if (page->mapping != inode->i_mapping) | |
642 | goto fail_unlock_page; | |
643 | if (!xfs_is_delayed_page(page, (*ioendp)->io_type)) | |
644 | goto fail_unlock_page; | |
645 | ||
24e17b5f NS |
646 | /* |
647 | * page_dirty is initially a count of buffers on the page before | |
648 | * EOF and is decrememted as we move each into a cleanable state. | |
9260dc6b CH |
649 | * |
650 | * Derivation: | |
651 | * | |
652 | * End offset is the highest offset that this page should represent. | |
653 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) | |
654 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and | |
655 | * hence give us the correct page_dirty count. On any other page, | |
656 | * it will be zero and in that case we need page_dirty to be the | |
657 | * count of buffers on the page. | |
24e17b5f | 658 | */ |
9260dc6b CH |
659 | end_offset = min_t(unsigned long long, |
660 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, | |
661 | i_size_read(inode)); | |
662 | ||
24e17b5f | 663 | len = 1 << inode->i_blkbits; |
9260dc6b CH |
664 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
665 | PAGE_CACHE_SIZE); | |
666 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; | |
667 | page_dirty = p_offset / len; | |
24e17b5f | 668 | |
f6d6d4fc | 669 | p_offset = 0; |
1da177e4 LT |
670 | bh = head = page_buffers(page); |
671 | do { | |
9260dc6b | 672 | if (offset >= end_offset) |
1da177e4 | 673 | break; |
f6d6d4fc CH |
674 | if (!buffer_uptodate(bh)) |
675 | uptodate = 0; | |
676 | if (!(PageUptodate(page) || buffer_uptodate(bh))) { | |
677 | done = 1; | |
1da177e4 | 678 | continue; |
f6d6d4fc CH |
679 | } |
680 | ||
9260dc6b CH |
681 | 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)) { | |
f6d6d4fc | 688 | done = 1; |
9260dc6b CH |
689 | 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) { | |
1da177e4 | 709 | lock_buffer(bh); |
f6d6d4fc CH |
710 | xfs_add_to_ioend(inode, bh, p_offset, |
711 | type, ioendp, done); | |
712 | count++; | |
24e17b5f | 713 | page_dirty--; |
9260dc6b CH |
714 | } else { |
715 | done = 1; | |
1da177e4 | 716 | } |
1da177e4 | 717 | } |
9260dc6b CH |
718 | } while (offset += len, p_offset += len, |
719 | (bh = bh->b_this_page) != head); | |
1da177e4 | 720 | |
f6d6d4fc CH |
721 | 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); | |
1da177e4 | 728 | } |
f6d6d4fc CH |
729 | |
730 | return done; | |
10ce4444 CH |
731 | fail_unlock_page: |
732 | unlock_page(page); | |
733 | fail: | |
734 | return 1; | |
1da177e4 LT |
735 | } |
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 | */ | |
741 | STATIC void | |
742 | xfs_cluster_write( | |
743 | struct inode *inode, | |
744 | pgoff_t tindex, | |
745 | xfs_iomap_t *iomapp, | |
f6d6d4fc | 746 | xfs_ioend_t **ioendp, |
1da177e4 LT |
747 | struct writeback_control *wbc, |
748 | int startio, | |
749 | int all_bh, | |
750 | pgoff_t tlast) | |
751 | { | |
10ce4444 CH |
752 | struct pagevec pvec; |
753 | int done = 0, i; | |
1da177e4 | 754 | |
10ce4444 CH |
755 | 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)) | |
1da177e4 | 760 | break; |
10ce4444 CH |
761 | |
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(); | |
1da177e4 LT |
771 | } |
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 | ||
793 | STATIC int | |
794 | xfs_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 | { | |
f6d6d4fc | 801 | struct buffer_head *bh, *head; |
1defeac9 | 802 | xfs_iomap_t iomap; |
f6d6d4fc | 803 | xfs_ioend_t *ioend = NULL, *iohead = NULL; |
1da177e4 LT |
804 | loff_t offset; |
805 | unsigned long p_offset = 0; | |
f6d6d4fc | 806 | unsigned int type; |
1da177e4 LT |
807 | __uint64_t end_offset; |
808 | pgoff_t end_index, last_index, tlast; | |
1defeac9 | 809 | int flags, len, err, iomap_valid = 0, uptodate = 1; |
f6d6d4fc | 810 | int page_dirty, count = 0, trylock_flag = 0; |
1da177e4 | 811 | |
3ba0815a | 812 | /* wait for other IO threads? */ |
f6d6d4fc CH |
813 | if (startio && wbc->sync_mode != WB_SYNC_NONE) |
814 | trylock_flag |= BMAPI_TRYLOCK; | |
3ba0815a | 815 | |
1da177e4 LT |
816 | /* Is this page beyond the end of the file? */ |
817 | offset = i_size_read(inode); | |
818 | end_index = offset >> PAGE_CACHE_SHIFT; | |
819 | last_index = (offset - 1) >> PAGE_CACHE_SHIFT; | |
820 | if (page->index >= end_index) { | |
821 | if ((page->index >= end_index + 1) || | |
822 | !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) { | |
19d5bcf3 NS |
823 | if (startio) |
824 | unlock_page(page); | |
825 | return 0; | |
1da177e4 LT |
826 | } |
827 | } | |
828 | ||
1da177e4 | 829 | /* |
24e17b5f NS |
830 | * page_dirty is initially a count of buffers on the page before |
831 | * EOF and is decrememted as we move each into a cleanable state. | |
f6d6d4fc CH |
832 | * |
833 | * Derivation: | |
834 | * | |
835 | * End offset is the highest offset that this page should represent. | |
836 | * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1)) | |
837 | * will evaluate non-zero and be less than PAGE_CACHE_SIZE and | |
838 | * hence give us the correct page_dirty count. On any other page, | |
839 | * it will be zero and in that case we need page_dirty to be the | |
840 | * count of buffers on the page. | |
841 | */ | |
842 | end_offset = min_t(unsigned long long, | |
843 | (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset); | |
24e17b5f | 844 | len = 1 << inode->i_blkbits; |
f6d6d4fc CH |
845 | p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1), |
846 | PAGE_CACHE_SIZE); | |
847 | p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE; | |
24e17b5f NS |
848 | page_dirty = p_offset / len; |
849 | ||
24e17b5f | 850 | bh = head = page_buffers(page); |
f6d6d4fc CH |
851 | offset = page_offset(page); |
852 | ||
f6d6d4fc | 853 | /* TODO: cleanup count and page_dirty */ |
1da177e4 LT |
854 | |
855 | do { | |
856 | if (offset >= end_offset) | |
857 | break; | |
858 | if (!buffer_uptodate(bh)) | |
859 | uptodate = 0; | |
f6d6d4fc | 860 | if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) { |
1defeac9 CH |
861 | /* |
862 | * the iomap is actually still valid, but the ioend | |
863 | * isn't. shouldn't happen too often. | |
864 | */ | |
865 | iomap_valid = 0; | |
1da177e4 | 866 | continue; |
f6d6d4fc | 867 | } |
1da177e4 | 868 | |
1defeac9 CH |
869 | if (iomap_valid) |
870 | iomap_valid = xfs_iomap_valid(&iomap, offset); | |
1da177e4 LT |
871 | |
872 | /* | |
873 | * First case, map an unwritten extent and prepare for | |
874 | * extent state conversion transaction on completion. | |
f6d6d4fc | 875 | * |
1da177e4 LT |
876 | * Second case, allocate space for a delalloc buffer. |
877 | * We can return EAGAIN here in the release page case. | |
878 | */ | |
f6d6d4fc CH |
879 | if (buffer_unwritten(bh) || buffer_delay(bh)) { |
880 | if (buffer_unwritten(bh)) { | |
881 | type = IOMAP_UNWRITTEN; | |
882 | flags = BMAPI_WRITE|BMAPI_IGNSTATE; | |
883 | } else { | |
884 | type = IOMAP_DELAY; | |
885 | flags = BMAPI_ALLOCATE; | |
886 | if (!startio) | |
887 | flags |= trylock_flag; | |
888 | } | |
889 | ||
1defeac9 | 890 | if (!iomap_valid) { |
1da177e4 | 891 | err = xfs_map_blocks(inode, offset, len, &iomap, |
f6d6d4fc CH |
892 | flags); |
893 | if (err) | |
1da177e4 | 894 | goto error; |
1defeac9 | 895 | iomap_valid = xfs_iomap_valid(&iomap, offset); |
1da177e4 | 896 | } |
1defeac9 CH |
897 | if (iomap_valid) { |
898 | xfs_map_at_offset(bh, offset, | |
899 | inode->i_blkbits, &iomap); | |
1da177e4 | 900 | if (startio) { |
f6d6d4fc | 901 | xfs_add_to_ioend(inode, bh, p_offset, |
1defeac9 CH |
902 | type, &ioend, |
903 | !iomap_valid); | |
1da177e4 LT |
904 | } else { |
905 | set_buffer_dirty(bh); | |
906 | unlock_buffer(bh); | |
907 | mark_buffer_dirty(bh); | |
908 | } | |
909 | page_dirty--; | |
f6d6d4fc | 910 | count++; |
1da177e4 LT |
911 | } |
912 | } else if ((buffer_uptodate(bh) || PageUptodate(page)) && | |
913 | (unmapped || startio)) { | |
914 | ||
f6d6d4fc | 915 | type = 0; |
1da177e4 | 916 | if (!buffer_mapped(bh)) { |
1da177e4 LT |
917 | |
918 | /* | |
919 | * Getting here implies an unmapped buffer | |
920 | * was found, and we are in a path where we | |
921 | * need to write the whole page out. | |
922 | */ | |
1defeac9 | 923 | if (!iomap_valid) { |
f6d6d4fc CH |
924 | int size; |
925 | ||
1da177e4 LT |
926 | size = xfs_probe_unmapped_cluster( |
927 | inode, page, bh, head); | |
928 | err = xfs_map_blocks(inode, offset, | |
929 | size, &iomap, | |
930 | BMAPI_WRITE|BMAPI_MMAP); | |
1defeac9 | 931 | if (err) |
1da177e4 | 932 | goto error; |
1defeac9 CH |
933 | iomap_valid = xfs_iomap_valid(&iomap, |
934 | offset); | |
1da177e4 | 935 | } |
1defeac9 CH |
936 | if (iomap_valid) { |
937 | xfs_map_at_offset(bh, offset, | |
938 | inode->i_blkbits, | |
939 | &iomap); | |
1da177e4 | 940 | if (startio) { |
f6d6d4fc CH |
941 | xfs_add_to_ioend(inode, |
942 | bh, p_offset, type, | |
1defeac9 | 943 | &ioend, !iomap_valid); |
1da177e4 LT |
944 | } else { |
945 | set_buffer_dirty(bh); | |
946 | unlock_buffer(bh); | |
947 | mark_buffer_dirty(bh); | |
948 | } | |
949 | page_dirty--; | |
f6d6d4fc | 950 | count++; |
1da177e4 LT |
951 | } |
952 | } else if (startio) { | |
953 | if (buffer_uptodate(bh) && | |
954 | !test_and_set_bit(BH_Lock, &bh->b_state)) { | |
f6d6d4fc CH |
955 | ASSERT(buffer_mapped(bh)); |
956 | xfs_add_to_ioend(inode, | |
957 | bh, p_offset, type, | |
1defeac9 | 958 | &ioend, !iomap_valid); |
1da177e4 | 959 | page_dirty--; |
f6d6d4fc CH |
960 | count++; |
961 | } else { | |
1defeac9 | 962 | iomap_valid = 0; |
1da177e4 | 963 | } |
f6d6d4fc | 964 | } else { |
1defeac9 | 965 | iomap_valid = 0; |
1da177e4 LT |
966 | } |
967 | } | |
f6d6d4fc CH |
968 | |
969 | if (!iohead) | |
970 | iohead = ioend; | |
971 | ||
972 | } while (offset += len, ((bh = bh->b_this_page) != head)); | |
1da177e4 LT |
973 | |
974 | if (uptodate && bh == head) | |
975 | SetPageUptodate(page); | |
976 | ||
f6d6d4fc CH |
977 | if (startio) |
978 | xfs_start_page_writeback(page, wbc, 1, count); | |
1da177e4 | 979 | |
1defeac9 CH |
980 | if (ioend && iomap_valid) { |
981 | offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >> | |
1da177e4 | 982 | PAGE_CACHE_SHIFT; |
775bf6c9 | 983 | tlast = min_t(pgoff_t, offset, last_index); |
1defeac9 | 984 | xfs_cluster_write(inode, page->index + 1, &iomap, &ioend, |
f6d6d4fc | 985 | wbc, startio, unmapped, tlast); |
1da177e4 LT |
986 | } |
987 | ||
f6d6d4fc CH |
988 | if (iohead) |
989 | xfs_submit_ioend(iohead); | |
990 | ||
1da177e4 LT |
991 | return page_dirty; |
992 | ||
993 | error: | |
f6d6d4fc CH |
994 | if (iohead) |
995 | xfs_cancel_ioend(iohead); | |
1da177e4 LT |
996 | |
997 | /* | |
998 | * If it's delalloc and we have nowhere to put it, | |
999 | * throw it away, unless the lower layers told | |
1000 | * us to try again. | |
1001 | */ | |
1002 | if (err != -EAGAIN) { | |
f6d6d4fc | 1003 | if (!unmapped) |
1da177e4 | 1004 | block_invalidatepage(page, 0); |
1da177e4 LT |
1005 | ClearPageUptodate(page); |
1006 | } | |
1007 | return err; | |
1008 | } | |
1009 | ||
1010 | STATIC int | |
1011 | __linvfs_get_block( | |
1012 | struct inode *inode, | |
1013 | sector_t iblock, | |
1014 | unsigned long blocks, | |
1015 | struct buffer_head *bh_result, | |
1016 | int create, | |
1017 | int direct, | |
1018 | bmapi_flags_t flags) | |
1019 | { | |
1020 | vnode_t *vp = LINVFS_GET_VP(inode); | |
1021 | xfs_iomap_t iomap; | |
fdc7ed75 NS |
1022 | xfs_off_t offset; |
1023 | ssize_t size; | |
1da177e4 LT |
1024 | int retpbbm = 1; |
1025 | int error; | |
1da177e4 | 1026 | |
fdc7ed75 | 1027 | offset = (xfs_off_t)iblock << inode->i_blkbits; |
a4656391 NS |
1028 | if (blocks) |
1029 | size = (ssize_t) min_t(xfs_off_t, LONG_MAX, | |
1030 | (xfs_off_t)blocks << inode->i_blkbits); | |
1031 | else | |
1032 | size = 1 << inode->i_blkbits; | |
1da177e4 LT |
1033 | |
1034 | VOP_BMAP(vp, offset, size, | |
1035 | create ? flags : BMAPI_READ, &iomap, &retpbbm, error); | |
1036 | if (error) | |
1037 | return -error; | |
1038 | ||
1039 | if (retpbbm == 0) | |
1040 | return 0; | |
1041 | ||
1042 | if (iomap.iomap_bn != IOMAP_DADDR_NULL) { | |
fdc7ed75 NS |
1043 | xfs_daddr_t bn; |
1044 | xfs_off_t delta; | |
1da177e4 LT |
1045 | |
1046 | /* For unwritten extents do not report a disk address on | |
1047 | * the read case (treat as if we're reading into a hole). | |
1048 | */ | |
1049 | if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) { | |
1050 | delta = offset - iomap.iomap_offset; | |
1051 | delta >>= inode->i_blkbits; | |
1052 | ||
1053 | bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT); | |
1054 | bn += delta; | |
1055 | BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME)); | |
1056 | bh_result->b_blocknr = bn; | |
1057 | set_buffer_mapped(bh_result); | |
1058 | } | |
1059 | if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) { | |
1060 | if (direct) | |
1061 | bh_result->b_private = inode; | |
1062 | set_buffer_unwritten(bh_result); | |
1063 | set_buffer_delay(bh_result); | |
1064 | } | |
1065 | } | |
1066 | ||
1067 | /* If this is a realtime file, data might be on a new device */ | |
ce8e922c | 1068 | bh_result->b_bdev = iomap.iomap_target->bt_bdev; |
1da177e4 LT |
1069 | |
1070 | /* If we previously allocated a block out beyond eof and | |
1071 | * we are now coming back to use it then we will need to | |
1072 | * flag it as new even if it has a disk address. | |
1073 | */ | |
1074 | if (create && | |
1075 | ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) || | |
fdc7ed75 | 1076 | (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW))) |
1da177e4 | 1077 | set_buffer_new(bh_result); |
1da177e4 LT |
1078 | |
1079 | if (iomap.iomap_flags & IOMAP_DELAY) { | |
1080 | BUG_ON(direct); | |
1081 | if (create) { | |
1082 | set_buffer_uptodate(bh_result); | |
1083 | set_buffer_mapped(bh_result); | |
1084 | set_buffer_delay(bh_result); | |
1085 | } | |
1086 | } | |
1087 | ||
1088 | if (blocks) { | |
fdc7ed75 NS |
1089 | ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0); |
1090 | offset = min_t(xfs_off_t, | |
1091 | iomap.iomap_bsize - iomap.iomap_delta, | |
a4656391 | 1092 | (xfs_off_t)blocks << inode->i_blkbits); |
fdc7ed75 | 1093 | bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset); |
1da177e4 LT |
1094 | } |
1095 | ||
1096 | return 0; | |
1097 | } | |
1098 | ||
1099 | int | |
1100 | linvfs_get_block( | |
1101 | struct inode *inode, | |
1102 | sector_t iblock, | |
1103 | struct buffer_head *bh_result, | |
1104 | int create) | |
1105 | { | |
1106 | return __linvfs_get_block(inode, iblock, 0, bh_result, | |
1107 | create, 0, BMAPI_WRITE); | |
1108 | } | |
1109 | ||
1110 | STATIC int | |
1111 | linvfs_get_blocks_direct( | |
1112 | struct inode *inode, | |
1113 | sector_t iblock, | |
1114 | unsigned long max_blocks, | |
1115 | struct buffer_head *bh_result, | |
1116 | int create) | |
1117 | { | |
1118 | return __linvfs_get_block(inode, iblock, max_blocks, bh_result, | |
1119 | create, 1, BMAPI_WRITE|BMAPI_DIRECT); | |
1120 | } | |
1121 | ||
f0973863 CH |
1122 | STATIC void |
1123 | linvfs_end_io_direct( | |
1124 | struct kiocb *iocb, | |
1125 | loff_t offset, | |
1126 | ssize_t size, | |
1127 | void *private) | |
1128 | { | |
1129 | xfs_ioend_t *ioend = iocb->private; | |
1130 | ||
1131 | /* | |
1132 | * Non-NULL private data means we need to issue a transaction to | |
1133 | * convert a range from unwritten to written extents. This needs | |
1134 | * to happen from process contect but aio+dio I/O completion | |
1135 | * happens from irq context so we need to defer it to a workqueue. | |
1136 | * This is not nessecary for synchronous direct I/O, but we do | |
1137 | * it anyway to keep the code uniform and simpler. | |
1138 | * | |
1139 | * The core direct I/O code might be changed to always call the | |
1140 | * completion handler in the future, in which case all this can | |
1141 | * go away. | |
1142 | */ | |
1143 | if (private && size > 0) { | |
1144 | ioend->io_offset = offset; | |
1145 | ioend->io_size = size; | |
1146 | xfs_finish_ioend(ioend); | |
1147 | } else { | |
1148 | ASSERT(size >= 0); | |
1149 | xfs_destroy_ioend(ioend); | |
1150 | } | |
1151 | ||
1152 | /* | |
1153 | * blockdev_direct_IO can return an error even afer the I/O | |
1154 | * completion handler was called. Thus we need to protect | |
1155 | * against double-freeing. | |
1156 | */ | |
1157 | iocb->private = NULL; | |
1158 | } | |
1159 | ||
1da177e4 LT |
1160 | STATIC ssize_t |
1161 | linvfs_direct_IO( | |
1162 | int rw, | |
1163 | struct kiocb *iocb, | |
1164 | const struct iovec *iov, | |
1165 | loff_t offset, | |
1166 | unsigned long nr_segs) | |
1167 | { | |
1168 | struct file *file = iocb->ki_filp; | |
1169 | struct inode *inode = file->f_mapping->host; | |
1170 | vnode_t *vp = LINVFS_GET_VP(inode); | |
1171 | xfs_iomap_t iomap; | |
1172 | int maps = 1; | |
1173 | int error; | |
f0973863 | 1174 | ssize_t ret; |
1da177e4 LT |
1175 | |
1176 | VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error); | |
1177 | if (error) | |
1178 | return -error; | |
1179 | ||
f6d6d4fc | 1180 | iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN); |
f0973863 CH |
1181 | |
1182 | ret = blockdev_direct_IO_own_locking(rw, iocb, inode, | |
ce8e922c | 1183 | iomap.iomap_target->bt_bdev, |
1da177e4 LT |
1184 | iov, offset, nr_segs, |
1185 | linvfs_get_blocks_direct, | |
f0973863 CH |
1186 | linvfs_end_io_direct); |
1187 | ||
1188 | if (unlikely(ret <= 0 && iocb->private)) | |
1189 | xfs_destroy_ioend(iocb->private); | |
1190 | return ret; | |
1da177e4 LT |
1191 | } |
1192 | ||
1193 | ||
1194 | STATIC sector_t | |
1195 | linvfs_bmap( | |
1196 | struct address_space *mapping, | |
1197 | sector_t block) | |
1198 | { | |
1199 | struct inode *inode = (struct inode *)mapping->host; | |
1200 | vnode_t *vp = LINVFS_GET_VP(inode); | |
1201 | int error; | |
1202 | ||
1203 | vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address); | |
1204 | ||
1205 | VOP_RWLOCK(vp, VRWLOCK_READ); | |
1206 | VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error); | |
1207 | VOP_RWUNLOCK(vp, VRWLOCK_READ); | |
1208 | return generic_block_bmap(mapping, block, linvfs_get_block); | |
1209 | } | |
1210 | ||
1211 | STATIC int | |
1212 | linvfs_readpage( | |
1213 | struct file *unused, | |
1214 | struct page *page) | |
1215 | { | |
1216 | return mpage_readpage(page, linvfs_get_block); | |
1217 | } | |
1218 | ||
1219 | STATIC int | |
1220 | linvfs_readpages( | |
1221 | struct file *unused, | |
1222 | struct address_space *mapping, | |
1223 | struct list_head *pages, | |
1224 | unsigned nr_pages) | |
1225 | { | |
1226 | return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block); | |
1227 | } | |
1228 | ||
1229 | STATIC void | |
1230 | xfs_count_page_state( | |
1231 | struct page *page, | |
1232 | int *delalloc, | |
1233 | int *unmapped, | |
1234 | int *unwritten) | |
1235 | { | |
1236 | struct buffer_head *bh, *head; | |
1237 | ||
1238 | *delalloc = *unmapped = *unwritten = 0; | |
1239 | ||
1240 | bh = head = page_buffers(page); | |
1241 | do { | |
1242 | if (buffer_uptodate(bh) && !buffer_mapped(bh)) | |
1243 | (*unmapped) = 1; | |
1244 | else if (buffer_unwritten(bh) && !buffer_delay(bh)) | |
1245 | clear_buffer_unwritten(bh); | |
1246 | else if (buffer_unwritten(bh)) | |
1247 | (*unwritten) = 1; | |
1248 | else if (buffer_delay(bh)) | |
1249 | (*delalloc) = 1; | |
1250 | } while ((bh = bh->b_this_page) != head); | |
1251 | } | |
1252 | ||
1253 | ||
1254 | /* | |
1255 | * writepage: Called from one of two places: | |
1256 | * | |
1257 | * 1. we are flushing a delalloc buffer head. | |
1258 | * | |
1259 | * 2. we are writing out a dirty page. Typically the page dirty | |
1260 | * state is cleared before we get here. In this case is it | |
1261 | * conceivable we have no buffer heads. | |
1262 | * | |
1263 | * For delalloc space on the page we need to allocate space and | |
1264 | * flush it. For unmapped buffer heads on the page we should | |
1265 | * allocate space if the page is uptodate. For any other dirty | |
1266 | * buffer heads on the page we should flush them. | |
1267 | * | |
1268 | * If we detect that a transaction would be required to flush | |
1269 | * the page, we have to check the process flags first, if we | |
1270 | * are already in a transaction or disk I/O during allocations | |
1271 | * is off, we need to fail the writepage and redirty the page. | |
1272 | */ | |
1273 | ||
1274 | STATIC int | |
1275 | linvfs_writepage( | |
1276 | struct page *page, | |
1277 | struct writeback_control *wbc) | |
1278 | { | |
1279 | int error; | |
1280 | int need_trans; | |
1281 | int delalloc, unmapped, unwritten; | |
1282 | struct inode *inode = page->mapping->host; | |
1283 | ||
1284 | xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0); | |
1285 | ||
1286 | /* | |
1287 | * We need a transaction if: | |
1288 | * 1. There are delalloc buffers on the page | |
1289 | * 2. The page is uptodate and we have unmapped buffers | |
1290 | * 3. The page is uptodate and we have no buffers | |
1291 | * 4. There are unwritten buffers on the page | |
1292 | */ | |
1293 | ||
1294 | if (!page_has_buffers(page)) { | |
1295 | unmapped = 1; | |
1296 | need_trans = 1; | |
1297 | } else { | |
1298 | xfs_count_page_state(page, &delalloc, &unmapped, &unwritten); | |
1299 | if (!PageUptodate(page)) | |
1300 | unmapped = 0; | |
1301 | need_trans = delalloc + unmapped + unwritten; | |
1302 | } | |
1303 | ||
1304 | /* | |
1305 | * If we need a transaction and the process flags say | |
1306 | * we are already in a transaction, or no IO is allowed | |
1307 | * then mark the page dirty again and leave the page | |
1308 | * as is. | |
1309 | */ | |
1310 | if (PFLAGS_TEST_FSTRANS() && need_trans) | |
1311 | goto out_fail; | |
1312 | ||
1313 | /* | |
1314 | * Delay hooking up buffer heads until we have | |
1315 | * made our go/no-go decision. | |
1316 | */ | |
1317 | if (!page_has_buffers(page)) | |
1318 | create_empty_buffers(page, 1 << inode->i_blkbits, 0); | |
1319 | ||
1320 | /* | |
1321 | * Convert delayed allocate, unwritten or unmapped space | |
1322 | * to real space and flush out to disk. | |
1323 | */ | |
1324 | error = xfs_page_state_convert(inode, page, wbc, 1, unmapped); | |
1325 | if (error == -EAGAIN) | |
1326 | goto out_fail; | |
1327 | if (unlikely(error < 0)) | |
1328 | goto out_unlock; | |
1329 | ||
1330 | return 0; | |
1331 | ||
1332 | out_fail: | |
1333 | redirty_page_for_writepage(wbc, page); | |
1334 | unlock_page(page); | |
1335 | return 0; | |
1336 | out_unlock: | |
1337 | unlock_page(page); | |
1338 | return error; | |
1339 | } | |
1340 | ||
bcec2b7f NS |
1341 | STATIC int |
1342 | linvfs_invalidate_page( | |
1343 | struct page *page, | |
1344 | unsigned long offset) | |
1345 | { | |
1346 | xfs_page_trace(XFS_INVALIDPAGE_ENTER, | |
1347 | page->mapping->host, page, offset); | |
1348 | return block_invalidatepage(page, offset); | |
1349 | } | |
1350 | ||
1da177e4 LT |
1351 | /* |
1352 | * Called to move a page into cleanable state - and from there | |
1353 | * to be released. Possibly the page is already clean. We always | |
1354 | * have buffer heads in this call. | |
1355 | * | |
1356 | * Returns 0 if the page is ok to release, 1 otherwise. | |
1357 | * | |
1358 | * Possible scenarios are: | |
1359 | * | |
1360 | * 1. We are being called to release a page which has been written | |
1361 | * to via regular I/O. buffer heads will be dirty and possibly | |
1362 | * delalloc. If no delalloc buffer heads in this case then we | |
1363 | * can just return zero. | |
1364 | * | |
1365 | * 2. We are called to release a page which has been written via | |
1366 | * mmap, all we need to do is ensure there is no delalloc | |
1367 | * state in the buffer heads, if not we can let the caller | |
1368 | * free them and we should come back later via writepage. | |
1369 | */ | |
1370 | STATIC int | |
1371 | linvfs_release_page( | |
1372 | struct page *page, | |
27496a8c | 1373 | gfp_t gfp_mask) |
1da177e4 LT |
1374 | { |
1375 | struct inode *inode = page->mapping->host; | |
1376 | int dirty, delalloc, unmapped, unwritten; | |
1377 | struct writeback_control wbc = { | |
1378 | .sync_mode = WB_SYNC_ALL, | |
1379 | .nr_to_write = 1, | |
1380 | }; | |
1381 | ||
1382 | xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask); | |
1383 | ||
1384 | xfs_count_page_state(page, &delalloc, &unmapped, &unwritten); | |
1385 | if (!delalloc && !unwritten) | |
1386 | goto free_buffers; | |
1387 | ||
1388 | if (!(gfp_mask & __GFP_FS)) | |
1389 | return 0; | |
1390 | ||
1391 | /* If we are already inside a transaction or the thread cannot | |
1392 | * do I/O, we cannot release this page. | |
1393 | */ | |
1394 | if (PFLAGS_TEST_FSTRANS()) | |
1395 | return 0; | |
1396 | ||
1397 | /* | |
1398 | * Convert delalloc space to real space, do not flush the | |
1399 | * data out to disk, that will be done by the caller. | |
1400 | * Never need to allocate space here - we will always | |
1401 | * come back to writepage in that case. | |
1402 | */ | |
1403 | dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0); | |
1404 | if (dirty == 0 && !unwritten) | |
1405 | goto free_buffers; | |
1406 | return 0; | |
1407 | ||
1408 | free_buffers: | |
1409 | return try_to_free_buffers(page); | |
1410 | } | |
1411 | ||
1412 | STATIC int | |
1413 | linvfs_prepare_write( | |
1414 | struct file *file, | |
1415 | struct page *page, | |
1416 | unsigned int from, | |
1417 | unsigned int to) | |
1418 | { | |
1419 | return block_prepare_write(page, from, to, linvfs_get_block); | |
1420 | } | |
1421 | ||
1422 | struct address_space_operations linvfs_aops = { | |
1423 | .readpage = linvfs_readpage, | |
1424 | .readpages = linvfs_readpages, | |
1425 | .writepage = linvfs_writepage, | |
1426 | .sync_page = block_sync_page, | |
1427 | .releasepage = linvfs_release_page, | |
bcec2b7f | 1428 | .invalidatepage = linvfs_invalidate_page, |
1da177e4 LT |
1429 | .prepare_write = linvfs_prepare_write, |
1430 | .commit_write = generic_commit_write, | |
1431 | .bmap = linvfs_bmap, | |
1432 | .direct_IO = linvfs_direct_IO, | |
1433 | }; |