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