int nilfs_copy_dirty_pages(struct address_space *dmap, struct address_space *smap) { struct pagevec pvec; unsigned int i; pgoff_t index = 0; int err = 0; pagevec_init(&pvec, 0); repeat: if (!pagevec_lookup_tag(&pvec, smap, &index, PAGECACHE_TAG_DIRTY, PAGEVEC_SIZE)) return 0; for (i = 0; i < pagevec_count(&pvec); i++) { struct page *page = pvec.pages[i], *dpage; lock_page(page); if (unlikely(!PageDirty(page))) NILFS_PAGE_BUG(page, "inconsistent dirty state"); dpage = grab_cache_page(dmap, page->index); if (unlikely(!dpage)) { /* No empty page is added to the page cache */ err = -ENOMEM; unlock_page(page); break; } if (unlikely(!page_has_buffers(page))) NILFS_PAGE_BUG(page, "found empty page in dat page cache"); nilfs_copy_page(dpage, page, 1); __set_page_dirty_nobuffers(dpage); unlock_page(dpage); page_cache_release(dpage); unlock_page(page); } pagevec_release(&pvec); cond_resched(); if (likely(!err)) goto repeat; return err; }
int nilfs_btnode_prepare_change_key(struct address_space *btnc, struct nilfs_btnode_chkey_ctxt *ctxt) { struct buffer_head *obh, *nbh; struct inode *inode = NILFS_BTNC_I(btnc); __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; int err; if (oldkey == newkey) return 0; obh = ctxt->bh; ctxt->newbh = NULL; if (inode->i_blkbits == PAGE_CACHE_SHIFT) { lock_page(obh->b_page); retry: err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); if (err) goto failed_unlock; if (unlikely(oldkey != obh->b_page->index)) NILFS_PAGE_BUG(obh->b_page, "invalid oldkey %lld (newkey=%lld)", (unsigned long long)oldkey, (unsigned long long)newkey); spin_lock_irq(&btnc->tree_lock); err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page); spin_unlock_irq(&btnc->tree_lock); radix_tree_preload_end(); if (!err) return 0; else if (err != -EEXIST) goto failed_unlock; err = invalidate_inode_pages2_range(btnc, newkey, newkey); if (!err) goto retry; unlock_page(obh->b_page); } nbh = nilfs_btnode_create_block(btnc, newkey); if (!nbh) return -ENOMEM; BUG_ON(nbh == obh); ctxt->newbh = nbh; return 0; failed_unlock: unlock_page(obh->b_page); return err; }
void nilfs_free_private_page(struct page *page) { BUG_ON(!PageLocked(page)); BUG_ON(page->mapping); if (page_has_buffers(page) && !try_to_free_buffers(page)) NILFS_PAGE_BUG(page, "failed to free page"); unlock_page(page); __free_page(page); }
/* * For inode and page debug */ int nilfs_releasepage(struct page *page, gfp_t gfp_mask) { struct address_space *mapping = page->mapping; struct inode *inode; int verbose = (nilfs_debug_info.verbose[NILFS_VERBOSE_PAGE] > 1); int ret; if (!verbose && mapping) { inode = NILFS_AS_I(mapping); if (inode->i_sb && !(inode->i_sb->s_flags & MS_ACTIVE)) verbose = 1; } if (unlikely(!PagePrivate(page))) NILFS_PAGE_BUG(page, "no buffers"); if (buffer_nilfs_allocated(page_buffers(page))) NILFS_PAGE_BUG(page, "nilfs allocated page"); /* * Note that non-busy buffer heads may be discarded though the * try_to_free_buffers() call. This may happen when the page is not * dirty, not in writeback, not locked, and belongs to a mapping. * Before changing the state of buffer heads to busy, the page lock * must be held to protect them. */ ret = try_to_free_buffers(page); if (verbose && ret && mapping && mapping->host) { if (page_count(page) > 2 + !PageLRU(page)) /* * This may happen when the other task just happen to * find and get the page during this invalidation. */ PAGE_DEBUG(page, "too many page count"); } return ret; }
/** * nilfs_btnode_commit_change_key * commit the change_key operation prepared by prepare_change_key(). */ void nilfs_btnode_commit_change_key(struct address_space *btnc, struct nilfs_btnode_chkey_ctxt *ctxt) { struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh; __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; struct page *opage; if (oldkey == newkey) return; if (nbh == NULL) { /* blocksize == pagesize */ opage = obh->b_page; if (unlikely(oldkey != opage->index)) NILFS_PAGE_BUG(opage, "invalid oldkey %lld (newkey=%lld)", (unsigned long long)oldkey, (unsigned long long)newkey); if (!test_set_buffer_dirty(obh) && TestSetPageDirty(opage)) BUG(); WRITE_LOCK_IRQ(&btnc->tree_lock); radix_tree_delete(&btnc->page_tree, oldkey); radix_tree_tag_set(&btnc->page_tree, newkey, PAGECACHE_TAG_DIRTY); WRITE_UNLOCK_IRQ(&btnc->tree_lock); opage->index = obh->b_blocknr = newkey; unlock_page(opage); } else { nilfs_copy_buffer(nbh, obh); nilfs_btnode_mark_dirty(nbh); nbh->b_blocknr = newkey; ctxt->bh = nbh; nilfs_btnode_delete(obh); /* will decrement bh->b_count */ } }
void nilfs_btnode_commit_change_key(struct address_space *btnc, struct nilfs_btnode_chkey_ctxt *ctxt) { struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh; __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; struct page *opage; if (oldkey == newkey) return; if (nbh == NULL) { opage = obh->b_page; if (unlikely(oldkey != opage->index)) NILFS_PAGE_BUG(opage, "invalid oldkey %lld (newkey=%lld)", (unsigned long long)oldkey, (unsigned long long)newkey); mark_buffer_dirty(obh); spin_lock_irq(&btnc->tree_lock); radix_tree_delete(&btnc->page_tree, oldkey); radix_tree_tag_set(&btnc->page_tree, newkey, PAGECACHE_TAG_DIRTY); spin_unlock_irq(&btnc->tree_lock); opage->index = obh->b_blocknr = newkey; unlock_page(opage); } else { nilfs_copy_buffer(nbh, obh); mark_buffer_dirty(nbh); nbh->b_blocknr = newkey; ctxt->bh = nbh; nilfs_btnode_delete(obh); } }
/** * nilfs_btnode_prepare_change_key * prepare to move contents of the block for old key to one of new key. * the old buffer will not be removed, but might be reused for new buffer. * it might return -ENOMEM because of memory allocation errors, * and might return -EIO because of disk read errors. */ int nilfs_btnode_prepare_change_key(struct address_space *btnc, struct nilfs_btnode_chkey_ctxt *ctxt) { struct buffer_head *obh, *nbh; struct inode *inode = NILFS_BTNC_I(btnc); __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; int err; if (oldkey == newkey) return 0; obh = ctxt->bh; ctxt->newbh = NULL; if (inode->i_blkbits == PAGE_CACHE_SHIFT) { lock_page(obh->b_page); /* * We cannot call radix_tree_preload for the kernels older * than 2.6.23, because it is not exported for modules. */ retry: err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); if (err) goto failed_unlock; /* BUG_ON(oldkey != obh->b_page->index); */ if (unlikely(oldkey != obh->b_page->index)) NILFS_PAGE_BUG(obh->b_page, "invalid oldkey %lld (newkey=%lld)", (unsigned long long)oldkey, (unsigned long long)newkey); spin_lock_irq(&btnc->tree_lock); err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page); spin_unlock_irq(&btnc->tree_lock); /* * Note: page->index will not change to newkey until * nilfs_btnode_commit_change_key() will be called. * To protect the page in intermediate state, the page lock * is held. */ radix_tree_preload_end(); if (!err) return 0; else if (err != -EEXIST) goto failed_unlock; err = invalidate_inode_pages2_range(btnc, newkey, newkey); if (!err) goto retry; /* fallback to copy mode */ unlock_page(obh->b_page); } nbh = nilfs_btnode_create_block(btnc, newkey); if (!nbh) return -ENOMEM; BUG_ON(nbh == obh); ctxt->newbh = nbh; return 0; failed_unlock: unlock_page(obh->b_page); return err; }