Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 1 | /* |
| 2 | * btnode.c - NILFS B-tree node cache |
| 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 | * This file was originally written by Seiji Kihara <kihara@osrg.net> |
| 21 | * and fully revised by Ryusuke Konishi <ryusuke@osrg.net> for |
| 22 | * stabilization and simplification. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <linux/types.h> |
| 27 | #include <linux/buffer_head.h> |
| 28 | #include <linux/mm.h> |
| 29 | #include <linux/backing-dev.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 30 | #include <linux/gfp.h> |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 31 | #include "nilfs.h" |
| 32 | #include "mdt.h" |
| 33 | #include "dat.h" |
| 34 | #include "page.h" |
| 35 | #include "btnode.h" |
| 36 | |
Ryusuke Konishi | a53b475 | 2009-05-27 22:11:46 +0900 | [diff] [blame] | 37 | void nilfs_btnode_cache_init(struct address_space *btnc, |
| 38 | struct backing_dev_info *bdi) |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 39 | { |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 40 | nilfs_mapping_init(btnc, bdi); |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | void nilfs_btnode_cache_clear(struct address_space *btnc) |
| 44 | { |
| 45 | invalidate_mapping_pages(btnc, 0, -1); |
| 46 | truncate_inode_pages(btnc, 0); |
| 47 | } |
| 48 | |
Ryusuke Konishi | d501d73 | 2009-11-13 16:04:11 +0900 | [diff] [blame] | 49 | struct buffer_head * |
| 50 | nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr) |
| 51 | { |
| 52 | struct inode *inode = NILFS_BTNC_I(btnc); |
| 53 | struct buffer_head *bh; |
| 54 | |
| 55 | bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node); |
| 56 | if (unlikely(!bh)) |
| 57 | return NULL; |
| 58 | |
| 59 | if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) || |
| 60 | buffer_dirty(bh))) { |
| 61 | brelse(bh); |
| 62 | BUG(); |
| 63 | } |
| 64 | memset(bh->b_data, 0, 1 << inode->i_blkbits); |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame^] | 65 | bh->b_bdev = inode->i_sb->s_bdev; |
Ryusuke Konishi | d501d73 | 2009-11-13 16:04:11 +0900 | [diff] [blame] | 66 | bh->b_blocknr = blocknr; |
| 67 | set_buffer_mapped(bh); |
| 68 | set_buffer_uptodate(bh); |
| 69 | |
| 70 | unlock_page(bh->b_page); |
| 71 | page_cache_release(bh->b_page); |
| 72 | return bh; |
| 73 | } |
| 74 | |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 75 | int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr, |
Ryusuke Konishi | 26dfdd8 | 2010-07-18 10:42:23 +0900 | [diff] [blame] | 76 | sector_t pblocknr, int mode, |
| 77 | struct buffer_head **pbh, sector_t *submit_ptr) |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 78 | { |
| 79 | struct buffer_head *bh; |
| 80 | struct inode *inode = NILFS_BTNC_I(btnc); |
Ryusuke Konishi | f8e6cc0 | 2010-07-15 11:39:10 +0900 | [diff] [blame] | 81 | struct page *page; |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 82 | int err; |
| 83 | |
| 84 | bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node); |
| 85 | if (unlikely(!bh)) |
| 86 | return -ENOMEM; |
| 87 | |
| 88 | err = -EEXIST; /* internal code */ |
Ryusuke Konishi | f8e6cc0 | 2010-07-15 11:39:10 +0900 | [diff] [blame] | 89 | page = bh->b_page; |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 90 | |
| 91 | if (buffer_uptodate(bh) || buffer_dirty(bh)) |
| 92 | goto found; |
| 93 | |
| 94 | if (pblocknr == 0) { |
| 95 | pblocknr = blocknr; |
| 96 | if (inode->i_ino != NILFS_DAT_INO) { |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame^] | 97 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 98 | |
| 99 | /* blocknr is a virtual block number */ |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame^] | 100 | err = nilfs_dat_translate(nilfs->ns_dat, blocknr, |
| 101 | &pblocknr); |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 102 | if (unlikely(err)) { |
| 103 | brelse(bh); |
| 104 | goto out_locked; |
| 105 | } |
| 106 | } |
| 107 | } |
Ryusuke Konishi | 26dfdd8 | 2010-07-18 10:42:23 +0900 | [diff] [blame] | 108 | |
| 109 | if (mode == READA) { |
| 110 | if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) { |
| 111 | err = -EBUSY; /* internal code */ |
| 112 | brelse(bh); |
| 113 | goto out_locked; |
| 114 | } |
| 115 | } else { /* mode == READ */ |
| 116 | lock_buffer(bh); |
| 117 | } |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 118 | if (buffer_uptodate(bh)) { |
| 119 | unlock_buffer(bh); |
| 120 | err = -EEXIST; /* internal code */ |
| 121 | goto found; |
| 122 | } |
| 123 | set_buffer_mapped(bh); |
Ryusuke Konishi | 0ef28f9 | 2011-05-05 12:56:51 +0900 | [diff] [blame^] | 124 | bh->b_bdev = inode->i_sb->s_bdev; |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 125 | bh->b_blocknr = pblocknr; /* set block address for read */ |
| 126 | bh->b_end_io = end_buffer_read_sync; |
| 127 | get_bh(bh); |
Ryusuke Konishi | 26dfdd8 | 2010-07-18 10:42:23 +0900 | [diff] [blame] | 128 | submit_bh(mode, bh); |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 129 | bh->b_blocknr = blocknr; /* set back to the given block address */ |
Ryusuke Konishi | 26dfdd8 | 2010-07-18 10:42:23 +0900 | [diff] [blame] | 130 | *submit_ptr = pblocknr; |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 131 | err = 0; |
| 132 | found: |
| 133 | *pbh = bh; |
| 134 | |
| 135 | out_locked: |
Ryusuke Konishi | f8e6cc0 | 2010-07-15 11:39:10 +0900 | [diff] [blame] | 136 | unlock_page(page); |
| 137 | page_cache_release(page); |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 138 | return err; |
| 139 | } |
| 140 | |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 141 | /** |
| 142 | * nilfs_btnode_delete - delete B-tree node buffer |
| 143 | * @bh: buffer to be deleted |
| 144 | * |
| 145 | * nilfs_btnode_delete() invalidates the specified buffer and delete the page |
| 146 | * including the buffer if the page gets unbusy. |
| 147 | */ |
| 148 | void nilfs_btnode_delete(struct buffer_head *bh) |
| 149 | { |
| 150 | struct address_space *mapping; |
| 151 | struct page *page = bh->b_page; |
| 152 | pgoff_t index = page_index(page); |
| 153 | int still_dirty; |
| 154 | |
| 155 | page_cache_get(page); |
| 156 | lock_page(page); |
| 157 | wait_on_page_writeback(page); |
| 158 | |
| 159 | nilfs_forget_buffer(bh); |
| 160 | still_dirty = PageDirty(page); |
| 161 | mapping = page->mapping; |
| 162 | unlock_page(page); |
| 163 | page_cache_release(page); |
| 164 | |
| 165 | if (!still_dirty && mapping) |
| 166 | invalidate_inode_pages2_range(mapping, index, index); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * nilfs_btnode_prepare_change_key |
| 171 | * prepare to move contents of the block for old key to one of new key. |
| 172 | * the old buffer will not be removed, but might be reused for new buffer. |
| 173 | * it might return -ENOMEM because of memory allocation errors, |
| 174 | * and might return -EIO because of disk read errors. |
| 175 | */ |
| 176 | int nilfs_btnode_prepare_change_key(struct address_space *btnc, |
| 177 | struct nilfs_btnode_chkey_ctxt *ctxt) |
| 178 | { |
| 179 | struct buffer_head *obh, *nbh; |
| 180 | struct inode *inode = NILFS_BTNC_I(btnc); |
| 181 | __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; |
| 182 | int err; |
| 183 | |
| 184 | if (oldkey == newkey) |
| 185 | return 0; |
| 186 | |
| 187 | obh = ctxt->bh; |
| 188 | ctxt->newbh = NULL; |
| 189 | |
| 190 | if (inode->i_blkbits == PAGE_CACHE_SHIFT) { |
| 191 | lock_page(obh->b_page); |
| 192 | /* |
| 193 | * We cannot call radix_tree_preload for the kernels older |
| 194 | * than 2.6.23, because it is not exported for modules. |
| 195 | */ |
Ryusuke Konishi | b1f1b8c | 2009-08-30 04:21:41 +0900 | [diff] [blame] | 196 | retry: |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 197 | err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM); |
| 198 | if (err) |
| 199 | goto failed_unlock; |
| 200 | /* BUG_ON(oldkey != obh->b_page->index); */ |
| 201 | if (unlikely(oldkey != obh->b_page->index)) |
| 202 | NILFS_PAGE_BUG(obh->b_page, |
| 203 | "invalid oldkey %lld (newkey=%lld)", |
| 204 | (unsigned long long)oldkey, |
| 205 | (unsigned long long)newkey); |
| 206 | |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 207 | spin_lock_irq(&btnc->tree_lock); |
| 208 | err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page); |
| 209 | spin_unlock_irq(&btnc->tree_lock); |
| 210 | /* |
| 211 | * Note: page->index will not change to newkey until |
| 212 | * nilfs_btnode_commit_change_key() will be called. |
| 213 | * To protect the page in intermediate state, the page lock |
| 214 | * is held. |
| 215 | */ |
| 216 | radix_tree_preload_end(); |
| 217 | if (!err) |
| 218 | return 0; |
| 219 | else if (err != -EEXIST) |
| 220 | goto failed_unlock; |
| 221 | |
| 222 | err = invalidate_inode_pages2_range(btnc, newkey, newkey); |
| 223 | if (!err) |
| 224 | goto retry; |
| 225 | /* fallback to copy mode */ |
| 226 | unlock_page(obh->b_page); |
| 227 | } |
| 228 | |
Ryusuke Konishi | 45f4910 | 2009-11-13 16:25:19 +0900 | [diff] [blame] | 229 | nbh = nilfs_btnode_create_block(btnc, newkey); |
| 230 | if (!nbh) |
| 231 | return -ENOMEM; |
| 232 | |
| 233 | BUG_ON(nbh == obh); |
| 234 | ctxt->newbh = nbh; |
| 235 | return 0; |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 236 | |
| 237 | failed_unlock: |
| 238 | unlock_page(obh->b_page); |
| 239 | return err; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * nilfs_btnode_commit_change_key |
| 244 | * commit the change_key operation prepared by prepare_change_key(). |
| 245 | */ |
| 246 | void nilfs_btnode_commit_change_key(struct address_space *btnc, |
| 247 | struct nilfs_btnode_chkey_ctxt *ctxt) |
| 248 | { |
| 249 | struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh; |
| 250 | __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; |
| 251 | struct page *opage; |
| 252 | |
| 253 | if (oldkey == newkey) |
| 254 | return; |
| 255 | |
| 256 | if (nbh == NULL) { /* blocksize == pagesize */ |
| 257 | opage = obh->b_page; |
| 258 | if (unlikely(oldkey != opage->index)) |
| 259 | NILFS_PAGE_BUG(opage, |
| 260 | "invalid oldkey %lld (newkey=%lld)", |
| 261 | (unsigned long long)oldkey, |
| 262 | (unsigned long long)newkey); |
Ryusuke Konishi | b1e19e5 | 2009-11-03 00:25:53 +0900 | [diff] [blame] | 263 | nilfs_btnode_mark_dirty(obh); |
Ryusuke Konishi | a60be98 | 2009-04-06 19:01:25 -0700 | [diff] [blame] | 264 | |
| 265 | spin_lock_irq(&btnc->tree_lock); |
| 266 | radix_tree_delete(&btnc->page_tree, oldkey); |
| 267 | radix_tree_tag_set(&btnc->page_tree, newkey, |
| 268 | PAGECACHE_TAG_DIRTY); |
| 269 | spin_unlock_irq(&btnc->tree_lock); |
| 270 | |
| 271 | opage->index = obh->b_blocknr = newkey; |
| 272 | unlock_page(opage); |
| 273 | } else { |
| 274 | nilfs_copy_buffer(nbh, obh); |
| 275 | nilfs_btnode_mark_dirty(nbh); |
| 276 | |
| 277 | nbh->b_blocknr = newkey; |
| 278 | ctxt->bh = nbh; |
| 279 | nilfs_btnode_delete(obh); /* will decrement bh->b_count */ |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * nilfs_btnode_abort_change_key |
| 285 | * abort the change_key operation prepared by prepare_change_key(). |
| 286 | */ |
| 287 | void nilfs_btnode_abort_change_key(struct address_space *btnc, |
| 288 | struct nilfs_btnode_chkey_ctxt *ctxt) |
| 289 | { |
| 290 | struct buffer_head *nbh = ctxt->newbh; |
| 291 | __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; |
| 292 | |
| 293 | if (oldkey == newkey) |
| 294 | return; |
| 295 | |
| 296 | if (nbh == NULL) { /* blocksize == pagesize */ |
| 297 | spin_lock_irq(&btnc->tree_lock); |
| 298 | radix_tree_delete(&btnc->page_tree, newkey); |
| 299 | spin_unlock_irq(&btnc->tree_lock); |
| 300 | unlock_page(ctxt->bh->b_page); |
| 301 | } else |
| 302 | brelse(nbh); |
| 303 | } |