2 * page.c - buffer/page management specific to NILFS
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>,
21 * Seiji Kihara <kihara@osrg.net>.
24 #include <linux/pagemap.h>
25 #include <linux/writeback.h>
26 #include <linux/swap.h>
27 #include <linux/bitops.h>
28 #include <linux/page-flags.h>
29 #include <linux/list.h>
30 #include <linux/highmem.h>
31 #include <linux/pagevec.h>
32 #include <linux/gfp.h>
38 #define NILFS_BUFFER_INHERENT_BITS \
39 ((1UL << BH_Uptodate) | (1UL << BH_Mapped) | (1UL << BH_NILFS_Node) | \
40 (1UL << BH_NILFS_Volatile) | (1UL << BH_NILFS_Allocated) | \
41 (1UL << BH_NILFS_Checked))
43 static struct buffer_head
*
44 __nilfs_get_page_block(struct page
*page
, unsigned long block
, pgoff_t index
,
45 int blkbits
, unsigned long b_state
)
48 unsigned long first_block
;
49 struct buffer_head
*bh
;
51 if (!page_has_buffers(page
))
52 create_empty_buffers(page
, 1 << blkbits
, b_state
);
54 first_block
= (unsigned long)index
<< (PAGE_CACHE_SHIFT
- blkbits
);
55 bh
= nilfs_page_get_nth_block(page
, block
- first_block
);
63 * Since the page cache of B-tree node pages or data page cache of pseudo
64 * inodes does not have a valid mapping->host pointer, calling
65 * mark_buffer_dirty() for their buffers causes a NULL pointer dereference;
66 * it calls __mark_inode_dirty(NULL) through __set_page_dirty().
67 * To avoid this problem, the old style mark_buffer_dirty() is used instead.
69 void nilfs_mark_buffer_dirty(struct buffer_head
*bh
)
71 if (!buffer_dirty(bh
) && !test_set_buffer_dirty(bh
))
72 __set_page_dirty_nobuffers(bh
->b_page
);
75 struct buffer_head
*nilfs_grab_buffer(struct inode
*inode
,
76 struct address_space
*mapping
,
78 unsigned long b_state
)
80 int blkbits
= inode
->i_blkbits
;
81 pgoff_t index
= blkoff
>> (PAGE_CACHE_SHIFT
- blkbits
);
83 struct buffer_head
*bh
;
85 page
= grab_cache_page(mapping
, index
);
89 bh
= __nilfs_get_page_block(page
, blkoff
, index
, blkbits
, b_state
);
92 page_cache_release(page
);
99 * nilfs_forget_buffer - discard dirty state
100 * @inode: owner inode of the buffer
101 * @bh: buffer head of the buffer to be discarded
103 void nilfs_forget_buffer(struct buffer_head
*bh
)
105 struct page
*page
= bh
->b_page
;
108 clear_buffer_nilfs_volatile(bh
);
109 clear_buffer_nilfs_checked(bh
);
110 clear_buffer_nilfs_redirected(bh
);
111 clear_buffer_dirty(bh
);
112 if (nilfs_page_buffers_clean(page
))
113 __nilfs_clear_page_dirty(page
);
115 clear_buffer_uptodate(bh
);
116 clear_buffer_mapped(bh
);
118 ClearPageUptodate(page
);
119 ClearPageMappedToDisk(page
);
125 * nilfs_copy_buffer -- copy buffer data and flags
126 * @dbh: destination buffer
127 * @sbh: source buffer
129 void nilfs_copy_buffer(struct buffer_head
*dbh
, struct buffer_head
*sbh
)
131 void *kaddr0
, *kaddr1
;
133 struct page
*spage
= sbh
->b_page
, *dpage
= dbh
->b_page
;
134 struct buffer_head
*bh
;
136 kaddr0
= kmap_atomic(spage
, KM_USER0
);
137 kaddr1
= kmap_atomic(dpage
, KM_USER1
);
138 memcpy(kaddr1
+ bh_offset(dbh
), kaddr0
+ bh_offset(sbh
), sbh
->b_size
);
139 kunmap_atomic(kaddr1
, KM_USER1
);
140 kunmap_atomic(kaddr0
, KM_USER0
);
142 dbh
->b_state
= sbh
->b_state
& NILFS_BUFFER_INHERENT_BITS
;
143 dbh
->b_blocknr
= sbh
->b_blocknr
;
144 dbh
->b_bdev
= sbh
->b_bdev
;
147 bits
= sbh
->b_state
& ((1UL << BH_Uptodate
) | (1UL << BH_Mapped
));
148 while ((bh
= bh
->b_this_page
) != dbh
) {
153 if (bits
& (1UL << BH_Uptodate
))
154 SetPageUptodate(dpage
);
156 ClearPageUptodate(dpage
);
157 if (bits
& (1UL << BH_Mapped
))
158 SetPageMappedToDisk(dpage
);
160 ClearPageMappedToDisk(dpage
);
164 * nilfs_page_buffers_clean - check if a page has dirty buffers or not.
165 * @page: page to be checked
167 * nilfs_page_buffers_clean() returns zero if the page has dirty buffers.
168 * Otherwise, it returns non-zero value.
170 int nilfs_page_buffers_clean(struct page
*page
)
172 struct buffer_head
*bh
, *head
;
174 bh
= head
= page_buffers(page
);
176 if (buffer_dirty(bh
))
178 bh
= bh
->b_this_page
;
179 } while (bh
!= head
);
183 void nilfs_page_bug(struct page
*page
)
185 struct address_space
*m
;
186 unsigned long ino
= 0;
188 if (unlikely(!page
)) {
189 printk(KERN_CRIT
"NILFS_PAGE_BUG(NULL)\n");
195 struct inode
*inode
= NILFS_AS_I(m
);
199 printk(KERN_CRIT
"NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
200 "mapping=%p ino=%lu\n",
201 page
, atomic_read(&page
->_count
),
202 (unsigned long long)page
->index
, page
->flags
, m
, ino
);
204 if (page_has_buffers(page
)) {
205 struct buffer_head
*bh
, *head
;
208 bh
= head
= page_buffers(page
);
211 " BH[%d] %p: cnt=%d block#=%llu state=0x%lx\n",
212 i
++, bh
, atomic_read(&bh
->b_count
),
213 (unsigned long long)bh
->b_blocknr
, bh
->b_state
);
214 bh
= bh
->b_this_page
;
215 } while (bh
!= head
);
220 * nilfs_alloc_private_page - allocate a private page with buffer heads
222 * Return Value: On success, a pointer to the allocated page is returned.
223 * On error, NULL is returned.
225 struct page
*nilfs_alloc_private_page(struct block_device
*bdev
, int size
,
228 struct buffer_head
*bh
, *head
, *tail
;
231 page
= alloc_page(GFP_NOFS
); /* page_count of the returned page is 1 */
236 head
= alloc_page_buffers(page
, size
, 0);
237 if (unlikely(!head
)) {
245 bh
->b_state
= (1UL << BH_NILFS_Allocated
) | state
;
248 bh
= bh
->b_this_page
;
251 tail
->b_this_page
= head
;
252 attach_page_buffers(page
, head
);
257 void nilfs_free_private_page(struct page
*page
)
259 BUG_ON(!PageLocked(page
));
260 BUG_ON(page
->mapping
);
262 if (page_has_buffers(page
) && !try_to_free_buffers(page
))
263 NILFS_PAGE_BUG(page
, "failed to free page");
270 * nilfs_copy_page -- copy the page with buffers
271 * @dst: destination page
273 * @copy_dirty: flag whether to copy dirty states on the page's buffer heads.
275 * This function is for both data pages and btnode pages. The dirty flag
276 * should be treated by caller. The page must not be under i/o.
277 * Both src and dst page must be locked
279 static void nilfs_copy_page(struct page
*dst
, struct page
*src
, int copy_dirty
)
281 struct buffer_head
*dbh
, *dbufs
, *sbh
, *sbufs
;
282 unsigned long mask
= NILFS_BUFFER_INHERENT_BITS
;
284 BUG_ON(PageWriteback(dst
));
286 sbh
= sbufs
= page_buffers(src
);
287 if (!page_has_buffers(dst
))
288 create_empty_buffers(dst
, sbh
->b_size
, 0);
291 mask
|= (1UL << BH_Dirty
);
293 dbh
= dbufs
= page_buffers(dst
);
297 dbh
->b_state
= sbh
->b_state
& mask
;
298 dbh
->b_blocknr
= sbh
->b_blocknr
;
299 dbh
->b_bdev
= sbh
->b_bdev
;
300 sbh
= sbh
->b_this_page
;
301 dbh
= dbh
->b_this_page
;
302 } while (dbh
!= dbufs
);
304 copy_highpage(dst
, src
);
306 if (PageUptodate(src
) && !PageUptodate(dst
))
307 SetPageUptodate(dst
);
308 else if (!PageUptodate(src
) && PageUptodate(dst
))
309 ClearPageUptodate(dst
);
310 if (PageMappedToDisk(src
) && !PageMappedToDisk(dst
))
311 SetPageMappedToDisk(dst
);
312 else if (!PageMappedToDisk(src
) && PageMappedToDisk(dst
))
313 ClearPageMappedToDisk(dst
);
318 sbh
= sbh
->b_this_page
;
319 dbh
= dbh
->b_this_page
;
320 } while (dbh
!= dbufs
);
323 int nilfs_copy_dirty_pages(struct address_space
*dmap
,
324 struct address_space
*smap
)
331 pagevec_init(&pvec
, 0);
333 if (!pagevec_lookup_tag(&pvec
, smap
, &index
, PAGECACHE_TAG_DIRTY
,
337 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
338 struct page
*page
= pvec
.pages
[i
], *dpage
;
341 if (unlikely(!PageDirty(page
)))
342 NILFS_PAGE_BUG(page
, "inconsistent dirty state");
344 dpage
= grab_cache_page(dmap
, page
->index
);
345 if (unlikely(!dpage
)) {
346 /* No empty page is added to the page cache */
351 if (unlikely(!page_has_buffers(page
)))
353 "found empty page in dat page cache");
355 nilfs_copy_page(dpage
, page
, 1);
356 __set_page_dirty_nobuffers(dpage
);
359 page_cache_release(dpage
);
362 pagevec_release(&pvec
);
371 * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
372 * @dmap: destination page cache
373 * @smap: source page cache
375 * No pages must no be added to the cache during this process.
376 * This must be ensured by the caller.
378 void nilfs_copy_back_pages(struct address_space
*dmap
,
379 struct address_space
*smap
)
386 pagevec_init(&pvec
, 0);
388 n
= pagevec_lookup(&pvec
, smap
, index
, PAGEVEC_SIZE
);
391 index
= pvec
.pages
[n
- 1]->index
+ 1;
393 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
394 struct page
*page
= pvec
.pages
[i
], *dpage
;
395 pgoff_t offset
= page
->index
;
398 dpage
= find_lock_page(dmap
, offset
);
400 /* override existing page on the destination cache */
401 WARN_ON(PageDirty(dpage
));
402 nilfs_copy_page(dpage
, page
, 0);
404 page_cache_release(dpage
);
408 /* move the page to the destination cache */
409 spin_lock_irq(&smap
->tree_lock
);
410 page2
= radix_tree_delete(&smap
->page_tree
, offset
);
411 WARN_ON(page2
!= page
);
414 spin_unlock_irq(&smap
->tree_lock
);
416 spin_lock_irq(&dmap
->tree_lock
);
417 err
= radix_tree_insert(&dmap
->page_tree
, offset
, page
);
418 if (unlikely(err
< 0)) {
419 WARN_ON(err
== -EEXIST
);
420 page
->mapping
= NULL
;
421 page_cache_release(page
); /* for cache */
423 page
->mapping
= dmap
;
426 radix_tree_tag_set(&dmap
->page_tree
,
428 PAGECACHE_TAG_DIRTY
);
430 spin_unlock_irq(&dmap
->tree_lock
);
434 pagevec_release(&pvec
);
440 void nilfs_clear_dirty_pages(struct address_space
*mapping
)
446 pagevec_init(&pvec
, 0);
448 while (pagevec_lookup_tag(&pvec
, mapping
, &index
, PAGECACHE_TAG_DIRTY
,
450 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
451 struct page
*page
= pvec
.pages
[i
];
452 struct buffer_head
*bh
, *head
;
455 ClearPageUptodate(page
);
456 ClearPageMappedToDisk(page
);
457 bh
= head
= page_buffers(page
);
460 clear_buffer_dirty(bh
);
461 clear_buffer_nilfs_volatile(bh
);
462 clear_buffer_nilfs_checked(bh
);
463 clear_buffer_nilfs_redirected(bh
);
464 clear_buffer_uptodate(bh
);
465 clear_buffer_mapped(bh
);
467 bh
= bh
->b_this_page
;
468 } while (bh
!= head
);
470 __nilfs_clear_page_dirty(page
);
473 pagevec_release(&pvec
);
478 unsigned nilfs_page_count_clean_buffers(struct page
*page
,
479 unsigned from
, unsigned to
)
481 unsigned block_start
, block_end
;
482 struct buffer_head
*bh
, *head
;
485 for (bh
= head
= page_buffers(page
), block_start
= 0;
486 bh
!= head
|| !block_start
;
487 block_start
= block_end
, bh
= bh
->b_this_page
) {
488 block_end
= block_start
+ bh
->b_size
;
489 if (block_end
> from
&& block_start
< to
&& !buffer_dirty(bh
))
495 void nilfs_mapping_init(struct address_space
*mapping
,
496 struct backing_dev_info
*bdi
)
498 mapping
->host
= NULL
;
500 mapping_set_gfp_mask(mapping
, GFP_NOFS
);
501 mapping
->assoc_mapping
= NULL
;
502 mapping
->backing_dev_info
= bdi
;
503 mapping
->a_ops
= NULL
;
507 * NILFS2 needs clear_page_dirty() in the following two cases:
509 * 1) For B-tree node pages and data pages of the dat/gcdat, NILFS2 clears
510 * page dirty flags when it copies back pages from the shadow cache
511 * (gcdat->{i_mapping,i_btnode_cache}) to its original cache
512 * (dat->{i_mapping,i_btnode_cache}).
514 * 2) Some B-tree operations like insertion or deletion may dispose buffers
515 * in dirty state, and this needs to cancel the dirty state of their pages.
517 int __nilfs_clear_page_dirty(struct page
*page
)
519 struct address_space
*mapping
= page
->mapping
;
522 spin_lock_irq(&mapping
->tree_lock
);
523 if (test_bit(PG_dirty
, &page
->flags
)) {
524 radix_tree_tag_clear(&mapping
->page_tree
,
526 PAGECACHE_TAG_DIRTY
);
527 spin_unlock_irq(&mapping
->tree_lock
);
528 return clear_page_dirty_for_io(page
);
530 spin_unlock_irq(&mapping
->tree_lock
);
533 return TestClearPageDirty(page
);
537 * nilfs_find_uncommitted_extent - find extent of uncommitted data
539 * @start_blk: start block offset (in)
540 * @blkoff: start offset of the found extent (out)
542 * This function searches an extent of buffers marked "delayed" which
543 * starts from a block offset equal to or larger than @start_blk. If
544 * such an extent was found, this will store the start offset in
545 * @blkoff and return its length in blocks. Otherwise, zero is
548 unsigned long nilfs_find_uncommitted_extent(struct inode
*inode
,
554 unsigned int nblocks_in_page
;
555 unsigned long length
= 0;
560 if (inode
->i_mapping
->nrpages
== 0)
563 index
= start_blk
>> (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
564 nblocks_in_page
= 1U << (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
566 pagevec_init(&pvec
, 0);
569 pvec
.nr
= find_get_pages_contig(inode
->i_mapping
, index
, PAGEVEC_SIZE
,
574 if (length
> 0 && pvec
.pages
[0]->index
> index
)
577 b
= pvec
.pages
[0]->index
<< (PAGE_CACHE_SHIFT
- inode
->i_blkbits
);
580 page
= pvec
.pages
[i
];
583 if (page_has_buffers(page
)) {
584 struct buffer_head
*bh
, *head
;
586 bh
= head
= page_buffers(page
);
590 if (buffer_delay(bh
)) {
594 } else if (length
> 0) {
597 } while (++b
, bh
= bh
->b_this_page
, bh
!= head
);
602 b
+= nblocks_in_page
;
606 } while (++i
< pagevec_count(&pvec
));
608 index
= page
->index
+ 1;
609 pagevec_release(&pvec
);
616 pagevec_release(&pvec
);