]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/nilfs2/page.c
nilfs2: get rid of NILFS_I_NILFS
[mirror_ubuntu-zesty-kernel.git] / fs / nilfs2 / page.c
CommitLineData
0bd49f94
RK
1/*
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>
5a0e3ad6 32#include <linux/gfp.h>
0bd49f94
RK
33#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) | \
1cb2d38c 40 (1UL << BH_NILFS_Volatile) | (1UL << BH_NILFS_Checked))
0bd49f94
RK
41
42static struct buffer_head *
43__nilfs_get_page_block(struct page *page, unsigned long block, pgoff_t index,
44 int blkbits, unsigned long b_state)
45
46{
47 unsigned long first_block;
48 struct buffer_head *bh;
49
50 if (!page_has_buffers(page))
51 create_empty_buffers(page, 1 << blkbits, b_state);
52
53 first_block = (unsigned long)index << (PAGE_CACHE_SHIFT - blkbits);
54 bh = nilfs_page_get_nth_block(page, block - first_block);
55
56 touch_buffer(bh);
57 wait_on_buffer(bh);
58 return bh;
59}
60
61/*
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.
67 */
68void nilfs_mark_buffer_dirty(struct buffer_head *bh)
69{
70 if (!buffer_dirty(bh) && !test_set_buffer_dirty(bh))
71 __set_page_dirty_nobuffers(bh->b_page);
72}
73
74struct buffer_head *nilfs_grab_buffer(struct inode *inode,
75 struct address_space *mapping,
76 unsigned long blkoff,
77 unsigned long b_state)
78{
79 int blkbits = inode->i_blkbits;
80 pgoff_t index = blkoff >> (PAGE_CACHE_SHIFT - blkbits);
c1c1d709
RK
81 struct page *page;
82 struct buffer_head *bh;
0bd49f94
RK
83
84 page = grab_cache_page(mapping, index);
85 if (unlikely(!page))
86 return NULL;
87
88 bh = __nilfs_get_page_block(page, blkoff, index, blkbits, b_state);
89 if (unlikely(!bh)) {
90 unlock_page(page);
91 page_cache_release(page);
92 return NULL;
93 }
0bd49f94
RK
94 return bh;
95}
96
97/**
98 * nilfs_forget_buffer - discard dirty state
99 * @inode: owner inode of the buffer
100 * @bh: buffer head of the buffer to be discarded
101 */
102void nilfs_forget_buffer(struct buffer_head *bh)
103{
104 struct page *page = bh->b_page;
105
106 lock_buffer(bh);
107 clear_buffer_nilfs_volatile(bh);
4e13e66b 108 clear_buffer_nilfs_checked(bh);
b1f6a4f2 109 clear_buffer_nilfs_redirected(bh);
84338237
RK
110 clear_buffer_dirty(bh);
111 if (nilfs_page_buffers_clean(page))
0bd49f94
RK
112 __nilfs_clear_page_dirty(page);
113
114 clear_buffer_uptodate(bh);
115 clear_buffer_mapped(bh);
116 bh->b_blocknr = -1;
117 ClearPageUptodate(page);
118 ClearPageMappedToDisk(page);
119 unlock_buffer(bh);
120 brelse(bh);
121}
122
123/**
124 * nilfs_copy_buffer -- copy buffer data and flags
125 * @dbh: destination buffer
126 * @sbh: source buffer
127 */
128void nilfs_copy_buffer(struct buffer_head *dbh, struct buffer_head *sbh)
129{
130 void *kaddr0, *kaddr1;
131 unsigned long bits;
132 struct page *spage = sbh->b_page, *dpage = dbh->b_page;
133 struct buffer_head *bh;
134
135 kaddr0 = kmap_atomic(spage, KM_USER0);
136 kaddr1 = kmap_atomic(dpage, KM_USER1);
137 memcpy(kaddr1 + bh_offset(dbh), kaddr0 + bh_offset(sbh), sbh->b_size);
138 kunmap_atomic(kaddr1, KM_USER1);
139 kunmap_atomic(kaddr0, KM_USER0);
140
141 dbh->b_state = sbh->b_state & NILFS_BUFFER_INHERENT_BITS;
142 dbh->b_blocknr = sbh->b_blocknr;
143 dbh->b_bdev = sbh->b_bdev;
144
145 bh = dbh;
146 bits = sbh->b_state & ((1UL << BH_Uptodate) | (1UL << BH_Mapped));
147 while ((bh = bh->b_this_page) != dbh) {
148 lock_buffer(bh);
149 bits &= bh->b_state;
150 unlock_buffer(bh);
151 }
152 if (bits & (1UL << BH_Uptodate))
153 SetPageUptodate(dpage);
154 else
155 ClearPageUptodate(dpage);
156 if (bits & (1UL << BH_Mapped))
157 SetPageMappedToDisk(dpage);
158 else
159 ClearPageMappedToDisk(dpage);
160}
161
162/**
163 * nilfs_page_buffers_clean - check if a page has dirty buffers or not.
164 * @page: page to be checked
165 *
166 * nilfs_page_buffers_clean() returns zero if the page has dirty buffers.
167 * Otherwise, it returns non-zero value.
168 */
169int nilfs_page_buffers_clean(struct page *page)
170{
171 struct buffer_head *bh, *head;
172
173 bh = head = page_buffers(page);
174 do {
175 if (buffer_dirty(bh))
176 return 0;
177 bh = bh->b_this_page;
178 } while (bh != head);
179 return 1;
180}
181
182void nilfs_page_bug(struct page *page)
183{
184 struct address_space *m;
185 unsigned long ino = 0;
186
187 if (unlikely(!page)) {
188 printk(KERN_CRIT "NILFS_PAGE_BUG(NULL)\n");
189 return;
190 }
191
192 m = page->mapping;
193 if (m) {
194 struct inode *inode = NILFS_AS_I(m);
195 if (inode != NULL)
196 ino = inode->i_ino;
197 }
198 printk(KERN_CRIT "NILFS_PAGE_BUG(%p): cnt=%d index#=%llu flags=0x%lx "
199 "mapping=%p ino=%lu\n",
200 page, atomic_read(&page->_count),
201 (unsigned long long)page->index, page->flags, m, ino);
202
203 if (page_has_buffers(page)) {
204 struct buffer_head *bh, *head;
205 int i = 0;
206
207 bh = head = page_buffers(page);
208 do {
209 printk(KERN_CRIT
210 " BH[%d] %p: cnt=%d block#=%llu state=0x%lx\n",
211 i++, bh, atomic_read(&bh->b_count),
212 (unsigned long long)bh->b_blocknr, bh->b_state);
213 bh = bh->b_this_page;
214 } while (bh != head);
215 }
216}
217
0bd49f94
RK
218/**
219 * nilfs_copy_page -- copy the page with buffers
220 * @dst: destination page
221 * @src: source page
222 * @copy_dirty: flag whether to copy dirty states on the page's buffer heads.
223 *
7a65004b 224 * This function is for both data pages and btnode pages. The dirty flag
0bd49f94
RK
225 * should be treated by caller. The page must not be under i/o.
226 * Both src and dst page must be locked
227 */
228static void nilfs_copy_page(struct page *dst, struct page *src, int copy_dirty)
229{
230 struct buffer_head *dbh, *dbufs, *sbh, *sbufs;
231 unsigned long mask = NILFS_BUFFER_INHERENT_BITS;
232
233 BUG_ON(PageWriteback(dst));
234
235 sbh = sbufs = page_buffers(src);
236 if (!page_has_buffers(dst))
237 create_empty_buffers(dst, sbh->b_size, 0);
238
239 if (copy_dirty)
240 mask |= (1UL << BH_Dirty);
241
242 dbh = dbufs = page_buffers(dst);
243 do {
244 lock_buffer(sbh);
245 lock_buffer(dbh);
246 dbh->b_state = sbh->b_state & mask;
247 dbh->b_blocknr = sbh->b_blocknr;
248 dbh->b_bdev = sbh->b_bdev;
249 sbh = sbh->b_this_page;
250 dbh = dbh->b_this_page;
251 } while (dbh != dbufs);
252
253 copy_highpage(dst, src);
254
255 if (PageUptodate(src) && !PageUptodate(dst))
256 SetPageUptodate(dst);
257 else if (!PageUptodate(src) && PageUptodate(dst))
258 ClearPageUptodate(dst);
259 if (PageMappedToDisk(src) && !PageMappedToDisk(dst))
260 SetPageMappedToDisk(dst);
261 else if (!PageMappedToDisk(src) && PageMappedToDisk(dst))
262 ClearPageMappedToDisk(dst);
263
264 do {
265 unlock_buffer(sbh);
266 unlock_buffer(dbh);
267 sbh = sbh->b_this_page;
268 dbh = dbh->b_this_page;
269 } while (dbh != dbufs);
270}
271
272int nilfs_copy_dirty_pages(struct address_space *dmap,
273 struct address_space *smap)
274{
275 struct pagevec pvec;
276 unsigned int i;
277 pgoff_t index = 0;
278 int err = 0;
279
280 pagevec_init(&pvec, 0);
281repeat:
282 if (!pagevec_lookup_tag(&pvec, smap, &index, PAGECACHE_TAG_DIRTY,
283 PAGEVEC_SIZE))
284 return 0;
285
286 for (i = 0; i < pagevec_count(&pvec); i++) {
287 struct page *page = pvec.pages[i], *dpage;
288
289 lock_page(page);
290 if (unlikely(!PageDirty(page)))
291 NILFS_PAGE_BUG(page, "inconsistent dirty state");
292
293 dpage = grab_cache_page(dmap, page->index);
294 if (unlikely(!dpage)) {
295 /* No empty page is added to the page cache */
296 err = -ENOMEM;
297 unlock_page(page);
298 break;
299 }
300 if (unlikely(!page_has_buffers(page)))
301 NILFS_PAGE_BUG(page,
302 "found empty page in dat page cache");
303
304 nilfs_copy_page(dpage, page, 1);
305 __set_page_dirty_nobuffers(dpage);
306
307 unlock_page(dpage);
308 page_cache_release(dpage);
309 unlock_page(page);
310 }
311 pagevec_release(&pvec);
312 cond_resched();
313
314 if (likely(!err))
315 goto repeat;
316 return err;
317}
318
319/**
7a65004b 320 * nilfs_copy_back_pages -- copy back pages to original cache from shadow cache
0bd49f94
RK
321 * @dmap: destination page cache
322 * @smap: source page cache
323 *
324 * No pages must no be added to the cache during this process.
325 * This must be ensured by the caller.
326 */
327void nilfs_copy_back_pages(struct address_space *dmap,
328 struct address_space *smap)
329{
330 struct pagevec pvec;
331 unsigned int i, n;
332 pgoff_t index = 0;
333 int err;
334
335 pagevec_init(&pvec, 0);
336repeat:
337 n = pagevec_lookup(&pvec, smap, index, PAGEVEC_SIZE);
338 if (!n)
339 return;
340 index = pvec.pages[n - 1]->index + 1;
341
342 for (i = 0; i < pagevec_count(&pvec); i++) {
343 struct page *page = pvec.pages[i], *dpage;
344 pgoff_t offset = page->index;
345
346 lock_page(page);
347 dpage = find_lock_page(dmap, offset);
348 if (dpage) {
349 /* override existing page on the destination cache */
1f5abe7e 350 WARN_ON(PageDirty(dpage));
0bd49f94
RK
351 nilfs_copy_page(dpage, page, 0);
352 unlock_page(dpage);
353 page_cache_release(dpage);
354 } else {
355 struct page *page2;
356
357 /* move the page to the destination cache */
358 spin_lock_irq(&smap->tree_lock);
359 page2 = radix_tree_delete(&smap->page_tree, offset);
1f5abe7e
RK
360 WARN_ON(page2 != page);
361
0bd49f94
RK
362 smap->nrpages--;
363 spin_unlock_irq(&smap->tree_lock);
364
365 spin_lock_irq(&dmap->tree_lock);
366 err = radix_tree_insert(&dmap->page_tree, offset, page);
367 if (unlikely(err < 0)) {
1f5abe7e 368 WARN_ON(err == -EEXIST);
0bd49f94
RK
369 page->mapping = NULL;
370 page_cache_release(page); /* for cache */
371 } else {
372 page->mapping = dmap;
373 dmap->nrpages++;
374 if (PageDirty(page))
375 radix_tree_tag_set(&dmap->page_tree,
376 offset,
377 PAGECACHE_TAG_DIRTY);
378 }
379 spin_unlock_irq(&dmap->tree_lock);
380 }
381 unlock_page(page);
382 }
383 pagevec_release(&pvec);
384 cond_resched();
385
386 goto repeat;
387}
388
389void nilfs_clear_dirty_pages(struct address_space *mapping)
390{
391 struct pagevec pvec;
392 unsigned int i;
393 pgoff_t index = 0;
394
395 pagevec_init(&pvec, 0);
396
397 while (pagevec_lookup_tag(&pvec, mapping, &index, PAGECACHE_TAG_DIRTY,
398 PAGEVEC_SIZE)) {
399 for (i = 0; i < pagevec_count(&pvec); i++) {
400 struct page *page = pvec.pages[i];
401 struct buffer_head *bh, *head;
402
403 lock_page(page);
404 ClearPageUptodate(page);
405 ClearPageMappedToDisk(page);
406 bh = head = page_buffers(page);
407 do {
408 lock_buffer(bh);
409 clear_buffer_dirty(bh);
410 clear_buffer_nilfs_volatile(bh);
4e13e66b 411 clear_buffer_nilfs_checked(bh);
b1f6a4f2 412 clear_buffer_nilfs_redirected(bh);
0bd49f94
RK
413 clear_buffer_uptodate(bh);
414 clear_buffer_mapped(bh);
415 unlock_buffer(bh);
416 bh = bh->b_this_page;
417 } while (bh != head);
418
419 __nilfs_clear_page_dirty(page);
420 unlock_page(page);
421 }
422 pagevec_release(&pvec);
423 cond_resched();
424 }
425}
426
427unsigned nilfs_page_count_clean_buffers(struct page *page,
428 unsigned from, unsigned to)
429{
430 unsigned block_start, block_end;
431 struct buffer_head *bh, *head;
432 unsigned nc = 0;
433
434 for (bh = head = page_buffers(page), block_start = 0;
435 bh != head || !block_start;
436 block_start = block_end, bh = bh->b_this_page) {
437 block_end = block_start + bh->b_size;
438 if (block_end > from && block_start < to && !buffer_dirty(bh))
439 nc++;
440 }
441 return nc;
442}
ae53a0a2 443
ebdfed4d 444void nilfs_mapping_init(struct address_space *mapping,
7eaceacc 445 struct backing_dev_info *bdi)
ebdfed4d
RK
446{
447 mapping->host = NULL;
448 mapping->flags = 0;
449 mapping_set_gfp_mask(mapping, GFP_NOFS);
450 mapping->assoc_mapping = NULL;
451 mapping->backing_dev_info = bdi;
d611b22f 452 mapping->a_ops = &empty_aops;
ebdfed4d 453}
0bd49f94
RK
454
455/*
456 * NILFS2 needs clear_page_dirty() in the following two cases:
457 *
458 * 1) For B-tree node pages and data pages of the dat/gcdat, NILFS2 clears
459 * page dirty flags when it copies back pages from the shadow cache
460 * (gcdat->{i_mapping,i_btnode_cache}) to its original cache
461 * (dat->{i_mapping,i_btnode_cache}).
462 *
463 * 2) Some B-tree operations like insertion or deletion may dispose buffers
464 * in dirty state, and this needs to cancel the dirty state of their pages.
465 */
466int __nilfs_clear_page_dirty(struct page *page)
467{
468 struct address_space *mapping = page->mapping;
469
470 if (mapping) {
471 spin_lock_irq(&mapping->tree_lock);
472 if (test_bit(PG_dirty, &page->flags)) {
473 radix_tree_tag_clear(&mapping->page_tree,
474 page_index(page),
475 PAGECACHE_TAG_DIRTY);
476 spin_unlock_irq(&mapping->tree_lock);
477 return clear_page_dirty_for_io(page);
478 }
479 spin_unlock_irq(&mapping->tree_lock);
480 return 0;
481 }
482 return TestClearPageDirty(page);
483}
622daaff
RK
484
485/**
486 * nilfs_find_uncommitted_extent - find extent of uncommitted data
487 * @inode: inode
488 * @start_blk: start block offset (in)
489 * @blkoff: start offset of the found extent (out)
490 *
491 * This function searches an extent of buffers marked "delayed" which
492 * starts from a block offset equal to or larger than @start_blk. If
493 * such an extent was found, this will store the start offset in
494 * @blkoff and return its length in blocks. Otherwise, zero is
495 * returned.
496 */
497unsigned long nilfs_find_uncommitted_extent(struct inode *inode,
498 sector_t start_blk,
499 sector_t *blkoff)
500{
501 unsigned int i;
502 pgoff_t index;
503 unsigned int nblocks_in_page;
504 unsigned long length = 0;
505 sector_t b;
506 struct pagevec pvec;
507 struct page *page;
508
509 if (inode->i_mapping->nrpages == 0)
510 return 0;
511
512 index = start_blk >> (PAGE_CACHE_SHIFT - inode->i_blkbits);
513 nblocks_in_page = 1U << (PAGE_CACHE_SHIFT - inode->i_blkbits);
514
515 pagevec_init(&pvec, 0);
516
517repeat:
518 pvec.nr = find_get_pages_contig(inode->i_mapping, index, PAGEVEC_SIZE,
519 pvec.pages);
520 if (pvec.nr == 0)
521 return length;
522
523 if (length > 0 && pvec.pages[0]->index > index)
524 goto out;
525
526 b = pvec.pages[0]->index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
527 i = 0;
528 do {
529 page = pvec.pages[i];
530
531 lock_page(page);
532 if (page_has_buffers(page)) {
533 struct buffer_head *bh, *head;
534
535 bh = head = page_buffers(page);
536 do {
537 if (b < start_blk)
538 continue;
539 if (buffer_delay(bh)) {
540 if (length == 0)
541 *blkoff = b;
542 length++;
543 } else if (length > 0) {
544 goto out_locked;
545 }
546 } while (++b, bh = bh->b_this_page, bh != head);
547 } else {
548 if (length > 0)
549 goto out_locked;
550
551 b += nblocks_in_page;
552 }
553 unlock_page(page);
554
555 } while (++i < pagevec_count(&pvec));
556
557 index = page->index + 1;
558 pagevec_release(&pvec);
559 cond_resched();
560 goto repeat;
561
562out_locked:
563 unlock_page(page);
564out:
565 pagevec_release(&pvec);
566 return length;
567}