2 * btnode.c - NILFS B-tree node cache
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 * Originally written by Seiji Kihara.
17 * Fully revised by Ryusuke Konishi for stabilization and simplification.
21 #include <linux/types.h>
22 #include <linux/buffer_head.h>
24 #include <linux/backing-dev.h>
25 #include <linux/gfp.h>
34 * nilfs_init_btnc_inode - initialize B-tree node cache inode
35 * @btnc_inode: inode to be initialized
37 * nilfs_init_btnc_inode() sets up an inode for B-tree node cache.
39 void nilfs_init_btnc_inode(struct inode *btnc_inode)
41 struct nilfs_inode_info *ii = NILFS_I(btnc_inode);
43 btnc_inode->i_mode = S_IFREG;
45 memset(&ii->i_bmap_data, 0, sizeof(struct nilfs_bmap));
46 mapping_set_gfp_mask(btnc_inode->i_mapping, GFP_NOFS);
49 void nilfs_btnode_cache_clear(struct address_space *btnc)
51 invalidate_mapping_pages(btnc, 0, -1);
52 truncate_inode_pages(btnc, 0);
56 nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
58 struct inode *inode = btnc->host;
59 struct buffer_head *bh;
61 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
65 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
70 memset(bh->b_data, 0, i_blocksize(inode));
71 bh->b_bdev = inode->i_sb->s_bdev;
72 bh->b_blocknr = blocknr;
73 set_buffer_mapped(bh);
74 set_buffer_uptodate(bh);
76 unlock_page(bh->b_page);
81 int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
82 sector_t pblocknr, int mode, int mode_flags,
83 struct buffer_head **pbh, sector_t *submit_ptr)
85 struct buffer_head *bh;
86 struct inode *inode = btnc->host;
90 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
94 err = -EEXIST; /* internal code */
97 if (buffer_uptodate(bh) || buffer_dirty(bh))
102 if (inode->i_ino != NILFS_DAT_INO) {
103 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
105 /* blocknr is a virtual block number */
106 err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
115 if (mode_flags & REQ_RAHEAD) {
116 if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
117 err = -EBUSY; /* internal code */
121 } else { /* mode == READ */
124 if (buffer_uptodate(bh)) {
126 err = -EEXIST; /* internal code */
129 set_buffer_mapped(bh);
130 bh->b_bdev = inode->i_sb->s_bdev;
131 bh->b_blocknr = pblocknr; /* set block address for read */
132 bh->b_end_io = end_buffer_read_sync;
134 submit_bh(mode, mode_flags, bh);
135 bh->b_blocknr = blocknr; /* set back to the given block address */
136 *submit_ptr = pblocknr;
148 * nilfs_btnode_delete - delete B-tree node buffer
149 * @bh: buffer to be deleted
151 * nilfs_btnode_delete() invalidates the specified buffer and delete the page
152 * including the buffer if the page gets unbusy.
154 void nilfs_btnode_delete(struct buffer_head *bh)
156 struct address_space *mapping;
157 struct page *page = bh->b_page;
158 pgoff_t index = page_index(page);
163 wait_on_page_writeback(page);
165 nilfs_forget_buffer(bh);
166 still_dirty = PageDirty(page);
167 mapping = page->mapping;
171 if (!still_dirty && mapping)
172 invalidate_inode_pages2_range(mapping, index, index);
176 * nilfs_btnode_prepare_change_key
177 * prepare to move contents of the block for old key to one of new key.
178 * the old buffer will not be removed, but might be reused for new buffer.
179 * it might return -ENOMEM because of memory allocation errors,
180 * and might return -EIO because of disk read errors.
182 int nilfs_btnode_prepare_change_key(struct address_space *btnc,
183 struct nilfs_btnode_chkey_ctxt *ctxt)
185 struct buffer_head *obh, *nbh;
186 struct inode *inode = btnc->host;
187 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
190 if (oldkey == newkey)
196 if (inode->i_blkbits == PAGE_SHIFT) {
197 lock_page(obh->b_page);
199 * We cannot call radix_tree_preload for the kernels older
200 * than 2.6.23, because it is not exported for modules.
203 err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
206 /* BUG_ON(oldkey != obh->b_page->index); */
207 if (unlikely(oldkey != obh->b_page->index))
208 NILFS_PAGE_BUG(obh->b_page,
209 "invalid oldkey %lld (newkey=%lld)",
210 (unsigned long long)oldkey,
211 (unsigned long long)newkey);
213 spin_lock_irq(&btnc->tree_lock);
214 err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page);
215 spin_unlock_irq(&btnc->tree_lock);
217 * Note: page->index will not change to newkey until
218 * nilfs_btnode_commit_change_key() will be called.
219 * To protect the page in intermediate state, the page lock
222 radix_tree_preload_end();
225 else if (err != -EEXIST)
228 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
231 /* fallback to copy mode */
232 unlock_page(obh->b_page);
235 nbh = nilfs_btnode_create_block(btnc, newkey);
244 unlock_page(obh->b_page);
249 * nilfs_btnode_commit_change_key
250 * commit the change_key operation prepared by prepare_change_key().
252 void nilfs_btnode_commit_change_key(struct address_space *btnc,
253 struct nilfs_btnode_chkey_ctxt *ctxt)
255 struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
256 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
259 if (oldkey == newkey)
262 if (nbh == NULL) { /* blocksize == pagesize */
264 if (unlikely(oldkey != opage->index))
265 NILFS_PAGE_BUG(opage,
266 "invalid oldkey %lld (newkey=%lld)",
267 (unsigned long long)oldkey,
268 (unsigned long long)newkey);
269 mark_buffer_dirty(obh);
271 spin_lock_irq(&btnc->tree_lock);
272 radix_tree_delete(&btnc->page_tree, oldkey);
273 radix_tree_tag_set(&btnc->page_tree, newkey,
274 PAGECACHE_TAG_DIRTY);
275 spin_unlock_irq(&btnc->tree_lock);
277 opage->index = obh->b_blocknr = newkey;
280 nilfs_copy_buffer(nbh, obh);
281 mark_buffer_dirty(nbh);
283 nbh->b_blocknr = newkey;
285 nilfs_btnode_delete(obh); /* will decrement bh->b_count */
290 * nilfs_btnode_abort_change_key
291 * abort the change_key operation prepared by prepare_change_key().
293 void nilfs_btnode_abort_change_key(struct address_space *btnc,
294 struct nilfs_btnode_chkey_ctxt *ctxt)
296 struct buffer_head *nbh = ctxt->newbh;
297 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
299 if (oldkey == newkey)
302 if (nbh == NULL) { /* blocksize == pagesize */
303 spin_lock_irq(&btnc->tree_lock);
304 radix_tree_delete(&btnc->page_tree, newkey);
305 spin_unlock_irq(&btnc->tree_lock);
306 unlock_page(ctxt->bh->b_page);
309 * When canceling a buffer that a prepare operation has
310 * allocated to copy a node block to another location, use
311 * nilfs_btnode_delete() to initialize and release the buffer
312 * so that the buffer flags will not be in an inconsistent
313 * state when it is reallocated.
315 nilfs_btnode_delete(nbh);