blob: 7083344ac88153978ed7908951b06aae8aa0371f [file] [log] [blame]
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -07001/*
2 * page.c - buffer/page management specific to NILFS
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
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.
10 *
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.
15 *
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
19 *
20 * Written by Ryusuke Konishi <ryusuke@osrg.net>,
21 * Seiji Kihara <kihara@osrg.net>.
22 */
23
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/gfp.h>
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -070033#include "nilfs.h"
34#include "page.h"
35#include "mdt.h"
36
37
38#define NILFS_BUFFER_INHERENT_BITS \
39 ((1UL << BH_Uptodate) | (1UL << BH_Mapped) | (1UL << BH_NILFS_Node) | \
Ryusuke Konishi4e13e662010-07-18 10:42:25 +090040 (1UL << BH_NILFS_Volatile) | (1UL << BH_NILFS_Allocated) | \
41 (1UL << BH_NILFS_Checked))
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -070042
43static struct buffer_head *
44__nilfs_get_page_block(struct page *page, unsigned long block, pgoff_t index,
45 int blkbits, unsigned long b_state)
46
47{
48 unsigned long first_block;
49 struct buffer_head *bh;
50
51 if (!page_has_buffers(page))
52 create_empty_buffers(page, 1 << blkbits, b_state);
53
54 first_block = (unsigned long)index << (PAGE_CACHE_SHIFT - blkbits);
55 bh = nilfs_page_get_nth_block(page, block - first_block);
56
57 touch_buffer(bh);
58 wait_on_buffer(bh);
59 return bh;
60}
61
62/*
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.
68 */
69void nilfs_mark_buffer_dirty(struct buffer_head *bh)
70{
71 if (!buffer_dirty(bh) && !test_set_buffer_dirty(bh))
72 __set_page_dirty_nobuffers(bh->b_page);
73}
74
75struct buffer_head *nilfs_grab_buffer(struct inode *inode,
76 struct address_space *mapping,
77 unsigned long blkoff,
78 unsigned long b_state)
79{
80 int blkbits = inode->i_blkbits;
81 pgoff_t index = blkoff >> (PAGE_CACHE_SHIFT - blkbits);
82 struct page *page, *opage;
83 struct buffer_head *bh, *obh;
84
85 page = grab_cache_page(mapping, index);
86 if (unlikely(!page))
87 return NULL;
88
89 bh = __nilfs_get_page_block(page, blkoff, index, blkbits, b_state);
90 if (unlikely(!bh)) {
91 unlock_page(page);
92 page_cache_release(page);
93 return NULL;
94 }
95 if (!buffer_uptodate(bh) && mapping->assoc_mapping != NULL) {
96 /*
97 * Shadow page cache uses assoc_mapping to point its original
98 * page cache. The following code tries the original cache
99 * if the given cache is a shadow and it didn't hit.
100 */
101 opage = find_lock_page(mapping->assoc_mapping, index);
102 if (!opage)
103 return bh;
104
105 obh = __nilfs_get_page_block(opage, blkoff, index, blkbits,
106 b_state);
107 if (buffer_uptodate(obh)) {
108 nilfs_copy_buffer(bh, obh);
109 if (buffer_dirty(obh)) {
110 nilfs_mark_buffer_dirty(bh);
111 if (!buffer_nilfs_node(bh) && NILFS_MDT(inode))
112 nilfs_mdt_mark_dirty(inode);
113 }
114 }
115 brelse(obh);
116 unlock_page(opage);
117 page_cache_release(opage);
118 }
119 return bh;
120}
121
122/**
123 * nilfs_forget_buffer - discard dirty state
124 * @inode: owner inode of the buffer
125 * @bh: buffer head of the buffer to be discarded
126 */
127void nilfs_forget_buffer(struct buffer_head *bh)
128{
129 struct page *page = bh->b_page;
130
131 lock_buffer(bh);
132 clear_buffer_nilfs_volatile(bh);
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900133 clear_buffer_nilfs_checked(bh);
Ryusuke Konishib1f6a4f2010-08-31 11:40:34 +0900134 clear_buffer_nilfs_redirected(bh);
Ryusuke Konishi84338232009-05-05 21:52:06 +0900135 clear_buffer_dirty(bh);
136 if (nilfs_page_buffers_clean(page))
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700137 __nilfs_clear_page_dirty(page);
138
139 clear_buffer_uptodate(bh);
140 clear_buffer_mapped(bh);
141 bh->b_blocknr = -1;
142 ClearPageUptodate(page);
143 ClearPageMappedToDisk(page);
144 unlock_buffer(bh);
145 brelse(bh);
146}
147
148/**
149 * nilfs_copy_buffer -- copy buffer data and flags
150 * @dbh: destination buffer
151 * @sbh: source buffer
152 */
153void nilfs_copy_buffer(struct buffer_head *dbh, struct buffer_head *sbh)
154{
155 void *kaddr0, *kaddr1;
156 unsigned long bits;
157 struct page *spage = sbh->b_page, *dpage = dbh->b_page;
158 struct buffer_head *bh;
159
160 kaddr0 = kmap_atomic(spage, KM_USER0);
161 kaddr1 = kmap_atomic(dpage, KM_USER1);
162 memcpy(kaddr1 + bh_offset(dbh), kaddr0 + bh_offset(sbh), sbh->b_size);
163 kunmap_atomic(kaddr1, KM_USER1);
164 kunmap_atomic(kaddr0, KM_USER0);
165
166 dbh->b_state = sbh->b_state & NILFS_BUFFER_INHERENT_BITS;
167 dbh->b_blocknr = sbh->b_blocknr;
168 dbh->b_bdev = sbh->b_bdev;
169
170 bh = dbh;
171 bits = sbh->b_state & ((1UL << BH_Uptodate) | (1UL << BH_Mapped));
172 while ((bh = bh->b_this_page) != dbh) {
173 lock_buffer(bh);
174 bits &= bh->b_state;
175 unlock_buffer(bh);
176 }
177 if (bits & (1UL << BH_Uptodate))
178 SetPageUptodate(dpage);
179 else
180 ClearPageUptodate(dpage);
181 if (bits & (1UL << BH_Mapped))
182 SetPageMappedToDisk(dpage);
183 else
184 ClearPageMappedToDisk(dpage);
185}
186
187/**
188 * nilfs_page_buffers_clean - check if a page has dirty buffers or not.
189 * @page: page to be checked
190 *
191 * nilfs_page_buffers_clean() returns zero if the page has dirty buffers.
192 * Otherwise, it returns non-zero value.
193 */
194int nilfs_page_buffers_clean(struct page *page)
195{
196 struct buffer_head *bh, *head;
197
198 bh = head = page_buffers(page);
199 do {
200 if (buffer_dirty(bh))
201 return 0;
202 bh = bh->b_this_page;
203 } while (bh != head);
204 return 1;
205}
206
207void nilfs_page_bug(struct page *page)
208{
209 struct address_space *m;
210 unsigned long ino = 0;
211
212 if (unlikely(!page)) {
213 printk(KERN_CRIT "NILFS_PAGE_BUG(NULL)\n");
214 return;
215 }
216
217 m = page->mapping;
218 if (m) {
219 struct inode *inode = NILFS_AS_I(m);
220 if (inode != NULL)
221 ino = inode->i_ino;
222 }
223 printk(KERN_CRIT "NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
224 "mapping=%p ino=%lu\n",
225 page, atomic_read(&page->_count),
226 (unsigned long long)page->index, page->flags, m, ino);
227
228 if (page_has_buffers(page)) {
229 struct buffer_head *bh, *head;
230 int i = 0;
231
232 bh = head = page_buffers(page);
233 do {
234 printk(KERN_CRIT
235 " BH[%d] %p: cnt=%d block#=%llu state=0x%lx\n",
236 i++, bh, atomic_read(&bh->b_count),
237 (unsigned long long)bh->b_blocknr, bh->b_state);
238 bh = bh->b_this_page;
239 } while (bh != head);
240 }
241}
242
243/**
244 * nilfs_alloc_private_page - allocate a private page with buffer heads
245 *
246 * Return Value: On success, a pointer to the allocated page is returned.
247 * On error, NULL is returned.
248 */
249struct page *nilfs_alloc_private_page(struct block_device *bdev, int size,
250 unsigned long state)
251{
252 struct buffer_head *bh, *head, *tail;
253 struct page *page;
254
255 page = alloc_page(GFP_NOFS); /* page_count of the returned page is 1 */
256 if (unlikely(!page))
257 return NULL;
258
259 lock_page(page);
260 head = alloc_page_buffers(page, size, 0);
261 if (unlikely(!head)) {
262 unlock_page(page);
263 __free_page(page);
264 return NULL;
265 }
266
267 bh = head;
268 do {
269 bh->b_state = (1UL << BH_NILFS_Allocated) | state;
270 tail = bh;
271 bh->b_bdev = bdev;
272 bh = bh->b_this_page;
273 } while (bh);
274
275 tail->b_this_page = head;
276 attach_page_buffers(page, head);
277
278 return page;
279}
280
281void nilfs_free_private_page(struct page *page)
282{
283 BUG_ON(!PageLocked(page));
284 BUG_ON(page->mapping);
285
286 if (page_has_buffers(page) && !try_to_free_buffers(page))
287 NILFS_PAGE_BUG(page, "failed to free page");
288
289 unlock_page(page);
290 __free_page(page);
291}
292
293/**
294 * nilfs_copy_page -- copy the page with buffers
295 * @dst: destination page
296 * @src: source page
297 * @copy_dirty: flag whether to copy dirty states on the page's buffer heads.
298 *
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900299 * This function is for both data pages and btnode pages. The dirty flag
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700300 * should be treated by caller. The page must not be under i/o.
301 * Both src and dst page must be locked
302 */
303static void nilfs_copy_page(struct page *dst, struct page *src, int copy_dirty)
304{
305 struct buffer_head *dbh, *dbufs, *sbh, *sbufs;
306 unsigned long mask = NILFS_BUFFER_INHERENT_BITS;
307
308 BUG_ON(PageWriteback(dst));
309
310 sbh = sbufs = page_buffers(src);
311 if (!page_has_buffers(dst))
312 create_empty_buffers(dst, sbh->b_size, 0);
313
314 if (copy_dirty)
315 mask |= (1UL << BH_Dirty);
316
317 dbh = dbufs = page_buffers(dst);
318 do {
319 lock_buffer(sbh);
320 lock_buffer(dbh);
321 dbh->b_state = sbh->b_state & mask;
322 dbh->b_blocknr = sbh->b_blocknr;
323 dbh->b_bdev = sbh->b_bdev;
324 sbh = sbh->b_this_page;
325 dbh = dbh->b_this_page;
326 } while (dbh != dbufs);
327
328 copy_highpage(dst, src);
329
330 if (PageUptodate(src) && !PageUptodate(dst))
331 SetPageUptodate(dst);
332 else if (!PageUptodate(src) && PageUptodate(dst))
333 ClearPageUptodate(dst);
334 if (PageMappedToDisk(src) && !PageMappedToDisk(dst))
335 SetPageMappedToDisk(dst);
336 else if (!PageMappedToDisk(src) && PageMappedToDisk(dst))
337 ClearPageMappedToDisk(dst);
338
339 do {
340 unlock_buffer(sbh);
341 unlock_buffer(dbh);
342 sbh = sbh->b_this_page;
343 dbh = dbh->b_this_page;
344 } while (dbh != dbufs);
345}
346
347int nilfs_copy_dirty_pages(struct address_space *dmap,
348 struct address_space *smap)
349{
350 struct pagevec pvec;
351 unsigned int i;
352 pgoff_t index = 0;
353 int err = 0;
354
355 pagevec_init(&pvec, 0);
356repeat:
357 if (!pagevec_lookup_tag(&pvec, smap, &index, PAGECACHE_TAG_DIRTY,
358 PAGEVEC_SIZE))
359 return 0;
360
361 for (i = 0; i < pagevec_count(&pvec); i++) {
362 struct page *page = pvec.pages[i], *dpage;
363
364 lock_page(page);
365 if (unlikely(!PageDirty(page)))
366 NILFS_PAGE_BUG(page, "inconsistent dirty state");
367
368 dpage = grab_cache_page(dmap, page->index);
369 if (unlikely(!dpage)) {
370 /* No empty page is added to the page cache */
371 err = -ENOMEM;
372 unlock_page(page);
373 break;
374 }
375 if (unlikely(!page_has_buffers(page)))
376 NILFS_PAGE_BUG(page,
377 "found empty page in dat page cache");
378
379 nilfs_copy_page(dpage, page, 1);
380 __set_page_dirty_nobuffers(dpage);
381
382 unlock_page(dpage);
383 page_cache_release(dpage);
384 unlock_page(page);
385 }
386 pagevec_release(&pvec);
387 cond_resched();
388
389 if (likely(!err))
390 goto repeat;
391 return err;
392}
393
394/**
Ryusuke Konishi7a650042010-03-14 03:32:40 +0900395 * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700396 * @dmap: destination page cache
397 * @smap: source page cache
398 *
399 * No pages must no be added to the cache during this process.
400 * This must be ensured by the caller.
401 */
402void nilfs_copy_back_pages(struct address_space *dmap,
403 struct address_space *smap)
404{
405 struct pagevec pvec;
406 unsigned int i, n;
407 pgoff_t index = 0;
408 int err;
409
410 pagevec_init(&pvec, 0);
411repeat:
412 n = pagevec_lookup(&pvec, smap, index, PAGEVEC_SIZE);
413 if (!n)
414 return;
415 index = pvec.pages[n - 1]->index + 1;
416
417 for (i = 0; i < pagevec_count(&pvec); i++) {
418 struct page *page = pvec.pages[i], *dpage;
419 pgoff_t offset = page->index;
420
421 lock_page(page);
422 dpage = find_lock_page(dmap, offset);
423 if (dpage) {
424 /* override existing page on the destination cache */
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700425 WARN_ON(PageDirty(dpage));
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700426 nilfs_copy_page(dpage, page, 0);
427 unlock_page(dpage);
428 page_cache_release(dpage);
429 } else {
430 struct page *page2;
431
432 /* move the page to the destination cache */
433 spin_lock_irq(&smap->tree_lock);
434 page2 = radix_tree_delete(&smap->page_tree, offset);
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700435 WARN_ON(page2 != page);
436
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700437 smap->nrpages--;
438 spin_unlock_irq(&smap->tree_lock);
439
440 spin_lock_irq(&dmap->tree_lock);
441 err = radix_tree_insert(&dmap->page_tree, offset, page);
442 if (unlikely(err < 0)) {
Ryusuke Konishi1f5abe72009-04-06 19:01:55 -0700443 WARN_ON(err == -EEXIST);
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700444 page->mapping = NULL;
445 page_cache_release(page); /* for cache */
446 } else {
447 page->mapping = dmap;
448 dmap->nrpages++;
449 if (PageDirty(page))
450 radix_tree_tag_set(&dmap->page_tree,
451 offset,
452 PAGECACHE_TAG_DIRTY);
453 }
454 spin_unlock_irq(&dmap->tree_lock);
455 }
456 unlock_page(page);
457 }
458 pagevec_release(&pvec);
459 cond_resched();
460
461 goto repeat;
462}
463
464void nilfs_clear_dirty_pages(struct address_space *mapping)
465{
466 struct pagevec pvec;
467 unsigned int i;
468 pgoff_t index = 0;
469
470 pagevec_init(&pvec, 0);
471
472 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
473 PAGEVEC_SIZE)) {
474 for (i = 0; i < pagevec_count(&pvec); i++) {
475 struct page *page = pvec.pages[i];
476 struct buffer_head *bh, *head;
477
478 lock_page(page);
479 ClearPageUptodate(page);
480 ClearPageMappedToDisk(page);
481 bh = head = page_buffers(page);
482 do {
483 lock_buffer(bh);
484 clear_buffer_dirty(bh);
485 clear_buffer_nilfs_volatile(bh);
Ryusuke Konishi4e13e662010-07-18 10:42:25 +0900486 clear_buffer_nilfs_checked(bh);
Ryusuke Konishib1f6a4f2010-08-31 11:40:34 +0900487 clear_buffer_nilfs_redirected(bh);
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700488 clear_buffer_uptodate(bh);
489 clear_buffer_mapped(bh);
490 unlock_buffer(bh);
491 bh = bh->b_this_page;
492 } while (bh != head);
493
494 __nilfs_clear_page_dirty(page);
495 unlock_page(page);
496 }
497 pagevec_release(&pvec);
498 cond_resched();
499 }
500}
501
502unsigned nilfs_page_count_clean_buffers(struct page *page,
503 unsigned from, unsigned to)
504{
505 unsigned block_start, block_end;
506 struct buffer_head *bh, *head;
507 unsigned nc = 0;
508
509 for (bh = head = page_buffers(page), block_start = 0;
510 bh != head || !block_start;
511 block_start = block_end, bh = bh->b_this_page) {
512 block_end = block_start + bh->b_size;
513 if (block_end > from && block_start < to && !buffer_dirty(bh))
514 nc++;
515 }
516 return nc;
517}
Ryusuke Konishiebdfed42010-09-06 12:05:43 +0900518
519void nilfs_mapping_init_once(struct address_space *mapping)
520{
521 memset(mapping, 0, sizeof(*mapping));
522 INIT_RADIX_TREE(&mapping->page_tree, GFP_ATOMIC);
523 spin_lock_init(&mapping->tree_lock);
524 INIT_LIST_HEAD(&mapping->private_list);
525 spin_lock_init(&mapping->private_lock);
526
527 spin_lock_init(&mapping->i_mmap_lock);
528 INIT_RAW_PRIO_TREE_ROOT(&mapping->i_mmap);
529 INIT_LIST_HEAD(&mapping->i_mmap_nonlinear);
530}
531
532void nilfs_mapping_init(struct address_space *mapping,
533 struct backing_dev_info *bdi,
534 const struct address_space_operations *aops)
535{
536 mapping->host = NULL;
537 mapping->flags = 0;
538 mapping_set_gfp_mask(mapping, GFP_NOFS);
539 mapping->assoc_mapping = NULL;
540 mapping->backing_dev_info = bdi;
541 mapping->a_ops = aops;
542}
Ryusuke Konishi0bd49f92009-04-06 19:01:27 -0700543
544/*
545 * NILFS2 needs clear_page_dirty() in the following two cases:
546 *
547 * 1) For B-tree node pages and data pages of the dat/gcdat, NILFS2 clears
548 * page dirty flags when it copies back pages from the shadow cache
549 * (gcdat->{i_mapping,i_btnode_cache}) to its original cache
550 * (dat->{i_mapping,i_btnode_cache}).
551 *
552 * 2) Some B-tree operations like insertion or deletion may dispose buffers
553 * in dirty state, and this needs to cancel the dirty state of their pages.
554 */
555int __nilfs_clear_page_dirty(struct page *page)
556{
557 struct address_space *mapping = page->mapping;
558
559 if (mapping) {
560 spin_lock_irq(&mapping->tree_lock);
561 if (test_bit(PG_dirty, &page->flags)) {
562 radix_tree_tag_clear(&mapping->page_tree,
563 page_index(page),
564 PAGECACHE_TAG_DIRTY);
565 spin_unlock_irq(&mapping->tree_lock);
566 return clear_page_dirty_for_io(page);
567 }
568 spin_unlock_irq(&mapping->tree_lock);
569 return 0;
570 }
571 return TestClearPageDirty(page);
572}