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))
42 static struct buffer_head
*
43 __nilfs_get_page_block(struct page
*page
, unsigned long block
, pgoff_t index
,
44 int blkbits
, unsigned long b_state
)
47 unsigned long first_block
;
48 struct buffer_head
*bh
;
50 if (!page_has_buffers(page
))
51 create_empty_buffers(page
, 1 << blkbits
, b_state
);
53 first_block
= (unsigned long)index
<< (PAGE_CACHE_SHIFT
- blkbits
);
54 bh
= nilfs_page_get_nth_block(page
, block
- first_block
);
62 * Since the page cache of B-tree node pages or data page cache of pseudo
63 * inodes does not have a valid mapping->host pointer, calling
64 * mark_buffer_dirty() for their buffers causes a NULL pointer dereference;
65 * it calls __mark_inode_dirty(NULL) through __set_page_dirty().
66 * To avoid this problem, the old style mark_buffer_dirty() is used instead.
68 void nilfs_mark_buffer_dirty(struct buffer_head
*bh
)
70 if (!buffer_dirty(bh
) && !test_set_buffer_dirty(bh
))
71 __set_page_dirty_nobuffers(bh
->b_page
);
74 struct buffer_head
*nilfs_grab_buffer(struct inode
*inode
,
75 struct address_space
*mapping
,
77 unsigned long b_state
)
79 int blkbits
= inode
->i_blkbits
;
80 pgoff_t index
= blkoff
>> (PAGE_CACHE_SHIFT
- blkbits
);
81 struct page
*page
, *opage
;
82 struct buffer_head
*bh
, *obh
;
84 page
= grab_cache_page(mapping
, index
);
88 bh
= __nilfs_get_page_block(page
, blkoff
, index
, blkbits
, b_state
);
91 page_cache_release(page
);
94 if (!buffer_uptodate(bh
) && mapping
->assoc_mapping
!= NULL
) {
96 * Shadow page cache uses assoc_mapping to point its original
97 * page cache. The following code tries the original cache
98 * if the given cache is a shadow and it didn't hit.
100 opage
= find_lock_page(mapping
->assoc_mapping
, index
);
104 obh
= __nilfs_get_page_block(opage
, blkoff
, index
, blkbits
,
106 if (buffer_uptodate(obh
)) {
107 nilfs_copy_buffer(bh
, obh
);
108 if (buffer_dirty(obh
)) {
109 nilfs_mark_buffer_dirty(bh
);
110 if (!buffer_nilfs_node(bh
) && NILFS_MDT(inode
))
111 nilfs_mdt_mark_dirty(inode
);
116 page_cache_release(opage
);
122 * nilfs_forget_buffer - discard dirty state
123 * @inode: owner inode of the buffer
124 * @bh: buffer head of the buffer to be discarded
126 void nilfs_forget_buffer(struct buffer_head
*bh
)
128 struct page
*page
= bh
->b_page
;
131 clear_buffer_nilfs_volatile(bh
);
132 clear_buffer_dirty(bh
);
133 if (nilfs_page_buffers_clean(page
))
134 __nilfs_clear_page_dirty(page
);
136 clear_buffer_uptodate(bh
);
137 clear_buffer_mapped(bh
);
139 ClearPageUptodate(page
);
140 ClearPageMappedToDisk(page
);
146 * nilfs_copy_buffer -- copy buffer data and flags
147 * @dbh: destination buffer
148 * @sbh: source buffer
150 void nilfs_copy_buffer(struct buffer_head
*dbh
, struct buffer_head
*sbh
)
152 void *kaddr0
, *kaddr1
;
154 struct page
*spage
= sbh
->b_page
, *dpage
= dbh
->b_page
;
155 struct buffer_head
*bh
;
157 kaddr0
= kmap_atomic(spage
, KM_USER0
);
158 kaddr1
= kmap_atomic(dpage
, KM_USER1
);
159 memcpy(kaddr1
+ bh_offset(dbh
), kaddr0
+ bh_offset(sbh
), sbh
->b_size
);
160 kunmap_atomic(kaddr1
, KM_USER1
);
161 kunmap_atomic(kaddr0
, KM_USER0
);
163 dbh
->b_state
= sbh
->b_state
& NILFS_BUFFER_INHERENT_BITS
;
164 dbh
->b_blocknr
= sbh
->b_blocknr
;
165 dbh
->b_bdev
= sbh
->b_bdev
;
168 bits
= sbh
->b_state
& ((1UL << BH_Uptodate
) | (1UL << BH_Mapped
));
169 while ((bh
= bh
->b_this_page
) != dbh
) {
174 if (bits
& (1UL << BH_Uptodate
))
175 SetPageUptodate(dpage
);
177 ClearPageUptodate(dpage
);
178 if (bits
& (1UL << BH_Mapped
))
179 SetPageMappedToDisk(dpage
);
181 ClearPageMappedToDisk(dpage
);
185 * nilfs_page_buffers_clean - check if a page has dirty buffers or not.
186 * @page: page to be checked
188 * nilfs_page_buffers_clean() returns zero if the page has dirty buffers.
189 * Otherwise, it returns non-zero value.
191 int nilfs_page_buffers_clean(struct page
*page
)
193 struct buffer_head
*bh
, *head
;
195 bh
= head
= page_buffers(page
);
197 if (buffer_dirty(bh
))
199 bh
= bh
->b_this_page
;
200 } while (bh
!= head
);
204 void nilfs_page_bug(struct page
*page
)
206 struct address_space
*m
;
207 unsigned long ino
= 0;
209 if (unlikely(!page
)) {
210 printk(KERN_CRIT
"NILFS_PAGE_BUG(NULL)\n");
216 struct inode
*inode
= NILFS_AS_I(m
);
220 printk(KERN_CRIT
"NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
221 "mapping=%p ino=%lu\n",
222 page
, atomic_read(&page
->_count
),
223 (unsigned long long)page
->index
, page
->flags
, m
, ino
);
225 if (page_has_buffers(page
)) {
226 struct buffer_head
*bh
, *head
;
229 bh
= head
= page_buffers(page
);
232 " BH[%d] %p: cnt=%d block#=%llu state=0x%lx\n",
233 i
++, bh
, atomic_read(&bh
->b_count
),
234 (unsigned long long)bh
->b_blocknr
, bh
->b_state
);
235 bh
= bh
->b_this_page
;
236 } while (bh
!= head
);
241 * nilfs_alloc_private_page - allocate a private page with buffer heads
243 * Return Value: On success, a pointer to the allocated page is returned.
244 * On error, NULL is returned.
246 struct page
*nilfs_alloc_private_page(struct block_device
*bdev
, int size
,
249 struct buffer_head
*bh
, *head
, *tail
;
252 page
= alloc_page(GFP_NOFS
); /* page_count of the returned page is 1 */
257 head
= alloc_page_buffers(page
, size
, 0);
258 if (unlikely(!head
)) {
266 bh
->b_state
= (1UL << BH_NILFS_Allocated
) | state
;
269 bh
= bh
->b_this_page
;
272 tail
->b_this_page
= head
;
273 attach_page_buffers(page
, head
);
278 void nilfs_free_private_page(struct page
*page
)
280 BUG_ON(!PageLocked(page
));
281 BUG_ON(page
->mapping
);
283 if (page_has_buffers(page
) && !try_to_free_buffers(page
))
284 NILFS_PAGE_BUG(page
, "failed to free page");
291 * nilfs_copy_page -- copy the page with buffers
292 * @dst: destination page
294 * @copy_dirty: flag whether to copy dirty states on the page's buffer heads.
296 * This function is for both data pages and btnode pages. The dirty flag
297 * should be treated by caller. The page must not be under i/o.
298 * Both src and dst page must be locked
300 static void nilfs_copy_page(struct page
*dst
, struct page
*src
, int copy_dirty
)
302 struct buffer_head
*dbh
, *dbufs
, *sbh
, *sbufs
;
303 unsigned long mask
= NILFS_BUFFER_INHERENT_BITS
;
305 BUG_ON(PageWriteback(dst
));
307 sbh
= sbufs
= page_buffers(src
);
308 if (!page_has_buffers(dst
))
309 create_empty_buffers(dst
, sbh
->b_size
, 0);
312 mask
|= (1UL << BH_Dirty
);
314 dbh
= dbufs
= page_buffers(dst
);
318 dbh
->b_state
= sbh
->b_state
& mask
;
319 dbh
->b_blocknr
= sbh
->b_blocknr
;
320 dbh
->b_bdev
= sbh
->b_bdev
;
321 sbh
= sbh
->b_this_page
;
322 dbh
= dbh
->b_this_page
;
323 } while (dbh
!= dbufs
);
325 copy_highpage(dst
, src
);
327 if (PageUptodate(src
) && !PageUptodate(dst
))
328 SetPageUptodate(dst
);
329 else if (!PageUptodate(src
) && PageUptodate(dst
))
330 ClearPageUptodate(dst
);
331 if (PageMappedToDisk(src
) && !PageMappedToDisk(dst
))
332 SetPageMappedToDisk(dst
);
333 else if (!PageMappedToDisk(src
) && PageMappedToDisk(dst
))
334 ClearPageMappedToDisk(dst
);
339 sbh
= sbh
->b_this_page
;
340 dbh
= dbh
->b_this_page
;
341 } while (dbh
!= dbufs
);
344 int nilfs_copy_dirty_pages(struct address_space
*dmap
,
345 struct address_space
*smap
)
352 pagevec_init(&pvec
, 0);
354 if (!pagevec_lookup_tag(&pvec
, smap
, &index
, PAGECACHE_TAG_DIRTY
,
358 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
359 struct page
*page
= pvec
.pages
[i
], *dpage
;
362 if (unlikely(!PageDirty(page
)))
363 NILFS_PAGE_BUG(page
, "inconsistent dirty state");
365 dpage
= grab_cache_page(dmap
, page
->index
);
366 if (unlikely(!dpage
)) {
367 /* No empty page is added to the page cache */
372 if (unlikely(!page_has_buffers(page
)))
374 "found empty page in dat page cache");
376 nilfs_copy_page(dpage
, page
, 1);
377 __set_page_dirty_nobuffers(dpage
);
380 page_cache_release(dpage
);
383 pagevec_release(&pvec
);
392 * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
393 * @dmap: destination page cache
394 * @smap: source page cache
396 * No pages must no be added to the cache during this process.
397 * This must be ensured by the caller.
399 void nilfs_copy_back_pages(struct address_space
*dmap
,
400 struct address_space
*smap
)
407 pagevec_init(&pvec
, 0);
409 n
= pagevec_lookup(&pvec
, smap
, index
, PAGEVEC_SIZE
);
412 index
= pvec
.pages
[n
- 1]->index
+ 1;
414 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
415 struct page
*page
= pvec
.pages
[i
], *dpage
;
416 pgoff_t offset
= page
->index
;
419 dpage
= find_lock_page(dmap
, offset
);
421 /* override existing page on the destination cache */
422 WARN_ON(PageDirty(dpage
));
423 nilfs_copy_page(dpage
, page
, 0);
425 page_cache_release(dpage
);
429 /* move the page to the destination cache */
430 spin_lock_irq(&smap
->tree_lock
);
431 page2
= radix_tree_delete(&smap
->page_tree
, offset
);
432 WARN_ON(page2
!= page
);
435 spin_unlock_irq(&smap
->tree_lock
);
437 spin_lock_irq(&dmap
->tree_lock
);
438 err
= radix_tree_insert(&dmap
->page_tree
, offset
, page
);
439 if (unlikely(err
< 0)) {
440 WARN_ON(err
== -EEXIST
);
441 page
->mapping
= NULL
;
442 page_cache_release(page
); /* for cache */
444 page
->mapping
= dmap
;
447 radix_tree_tag_set(&dmap
->page_tree
,
449 PAGECACHE_TAG_DIRTY
);
451 spin_unlock_irq(&dmap
->tree_lock
);
455 pagevec_release(&pvec
);
461 void nilfs_clear_dirty_pages(struct address_space
*mapping
)
467 pagevec_init(&pvec
, 0);
469 while (pagevec_lookup_tag(&pvec
, mapping
, &index
, PAGECACHE_TAG_DIRTY
,
471 for (i
= 0; i
< pagevec_count(&pvec
); i
++) {
472 struct page
*page
= pvec
.pages
[i
];
473 struct buffer_head
*bh
, *head
;
476 ClearPageUptodate(page
);
477 ClearPageMappedToDisk(page
);
478 bh
= head
= page_buffers(page
);
481 clear_buffer_dirty(bh
);
482 clear_buffer_nilfs_volatile(bh
);
483 clear_buffer_uptodate(bh
);
484 clear_buffer_mapped(bh
);
486 bh
= bh
->b_this_page
;
487 } while (bh
!= head
);
489 __nilfs_clear_page_dirty(page
);
492 pagevec_release(&pvec
);
497 unsigned nilfs_page_count_clean_buffers(struct page
*page
,
498 unsigned from
, unsigned to
)
500 unsigned block_start
, block_end
;
501 struct buffer_head
*bh
, *head
;
504 for (bh
= head
= page_buffers(page
), block_start
= 0;
505 bh
!= head
|| !block_start
;
506 block_start
= block_end
, bh
= bh
->b_this_page
) {
507 block_end
= block_start
+ bh
->b_size
;
508 if (block_end
> from
&& block_start
< to
&& !buffer_dirty(bh
))
515 * NILFS2 needs clear_page_dirty() in the following two cases:
517 * 1) For B-tree node pages and data pages of the dat/gcdat, NILFS2 clears
518 * page dirty flags when it copies back pages from the shadow cache
519 * (gcdat->{i_mapping,i_btnode_cache}) to its original cache
520 * (dat->{i_mapping,i_btnode_cache}).
522 * 2) Some B-tree operations like insertion or deletion may dispose buffers
523 * in dirty state, and this needs to cancel the dirty state of their pages.
525 int __nilfs_clear_page_dirty(struct page
*page
)
527 struct address_space
*mapping
= page
->mapping
;
530 spin_lock_irq(&mapping
->tree_lock
);
531 if (test_bit(PG_dirty
, &page
->flags
)) {
532 radix_tree_tag_clear(&mapping
->page_tree
,
534 PAGECACHE_TAG_DIRTY
);
535 spin_unlock_irq(&mapping
->tree_lock
);
536 return clear_page_dirty_for_io(page
);
538 spin_unlock_irq(&mapping
->tree_lock
);
541 return TestClearPageDirty(page
);