Blame view
fs/nilfs2/btnode.c
7.39 KB
a60be987d nilfs2: B-tree no... |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
/* * btnode.c - NILFS B-tree node cache * * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * |
4b420ab4e nilfs2: clean up ... |
16 17 |
* Originally written by Seiji Kihara. * Fully revised by Ryusuke Konishi for stabilization and simplification. |
a60be987d nilfs2: B-tree no... |
18 19 20 21 22 23 24 |
* */ #include <linux/types.h> #include <linux/buffer_head.h> #include <linux/mm.h> #include <linux/backing-dev.h> |
5a0e3ad6a include cleanup: ... |
25 |
#include <linux/gfp.h> |
a60be987d nilfs2: B-tree no... |
26 27 28 29 30 |
#include "nilfs.h" #include "mdt.h" #include "dat.h" #include "page.h" #include "btnode.h" |
a60be987d nilfs2: B-tree no... |
31 32 33 34 35 |
void nilfs_btnode_cache_clear(struct address_space *btnc) { invalidate_mapping_pages(btnc, 0, -1); truncate_inode_pages(btnc, 0); } |
d501d7368 nilfs2: separate ... |
36 37 38 39 40 |
struct buffer_head * nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr) { struct inode *inode = NILFS_BTNC_I(btnc); struct buffer_head *bh; |
4ce5c3426 nilfs2: use BIT()... |
41 |
bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node)); |
d501d7368 nilfs2: separate ... |
42 43 44 45 46 47 48 49 |
if (unlikely(!bh)) return NULL; if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) || buffer_dirty(bh))) { brelse(bh); BUG(); } |
93407472a fs: add i_blocksi... |
50 |
memset(bh->b_data, 0, i_blocksize(inode)); |
0ef28f9ae nilfs2: get rid o... |
51 |
bh->b_bdev = inode->i_sb->s_bdev; |
d501d7368 nilfs2: separate ... |
52 53 54 55 56 |
bh->b_blocknr = blocknr; set_buffer_mapped(bh); set_buffer_uptodate(bh); unlock_page(bh->b_page); |
09cbfeaf1 mm, fs: get rid o... |
57 |
put_page(bh->b_page); |
d501d7368 nilfs2: separate ... |
58 59 |
return bh; } |
a60be987d nilfs2: B-tree no... |
60 |
int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr, |
2a222ca99 fs: have submit_b... |
61 |
sector_t pblocknr, int mode, int mode_flags, |
26dfdd8e2 nilfs2: add read ... |
62 |
struct buffer_head **pbh, sector_t *submit_ptr) |
a60be987d nilfs2: B-tree no... |
63 64 65 |
{ struct buffer_head *bh; struct inode *inode = NILFS_BTNC_I(btnc); |
f8e6cc013 nilfs2: fix buffe... |
66 |
struct page *page; |
a60be987d nilfs2: B-tree no... |
67 |
int err; |
4ce5c3426 nilfs2: use BIT()... |
68 |
bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node)); |
a60be987d nilfs2: B-tree no... |
69 70 71 72 |
if (unlikely(!bh)) return -ENOMEM; err = -EEXIST; /* internal code */ |
f8e6cc013 nilfs2: fix buffe... |
73 |
page = bh->b_page; |
a60be987d nilfs2: B-tree no... |
74 75 76 77 78 79 80 |
if (buffer_uptodate(bh) || buffer_dirty(bh)) goto found; if (pblocknr == 0) { pblocknr = blocknr; if (inode->i_ino != NILFS_DAT_INO) { |
0ef28f9ae nilfs2: get rid o... |
81 |
struct the_nilfs *nilfs = inode->i_sb->s_fs_info; |
a60be987d nilfs2: B-tree no... |
82 83 |
/* blocknr is a virtual block number */ |
0ef28f9ae nilfs2: get rid o... |
84 85 |
err = nilfs_dat_translate(nilfs->ns_dat, blocknr, &pblocknr); |
a60be987d nilfs2: B-tree no... |
86 87 88 89 90 91 |
if (unlikely(err)) { brelse(bh); goto out_locked; } } } |
26dfdd8e2 nilfs2: add read ... |
92 |
|
2a222ca99 fs: have submit_b... |
93 |
if (mode_flags & REQ_RAHEAD) { |
26dfdd8e2 nilfs2: add read ... |
94 95 96 97 98 99 100 101 |
if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) { err = -EBUSY; /* internal code */ brelse(bh); goto out_locked; } } else { /* mode == READ */ lock_buffer(bh); } |
a60be987d nilfs2: B-tree no... |
102 103 104 105 106 107 |
if (buffer_uptodate(bh)) { unlock_buffer(bh); err = -EEXIST; /* internal code */ goto found; } set_buffer_mapped(bh); |
0ef28f9ae nilfs2: get rid o... |
108 |
bh->b_bdev = inode->i_sb->s_bdev; |
a60be987d nilfs2: B-tree no... |
109 110 111 |
bh->b_blocknr = pblocknr; /* set block address for read */ bh->b_end_io = end_buffer_read_sync; get_bh(bh); |
2a222ca99 fs: have submit_b... |
112 |
submit_bh(mode, mode_flags, bh); |
a60be987d nilfs2: B-tree no... |
113 |
bh->b_blocknr = blocknr; /* set back to the given block address */ |
26dfdd8e2 nilfs2: add read ... |
114 |
*submit_ptr = pblocknr; |
a60be987d nilfs2: B-tree no... |
115 116 117 118 119 |
err = 0; found: *pbh = bh; out_locked: |
f8e6cc013 nilfs2: fix buffe... |
120 |
unlock_page(page); |
09cbfeaf1 mm, fs: get rid o... |
121 |
put_page(page); |
a60be987d nilfs2: B-tree no... |
122 123 |
return err; } |
a60be987d nilfs2: B-tree no... |
124 125 126 127 128 129 130 131 132 133 134 135 136 |
/** * nilfs_btnode_delete - delete B-tree node buffer * @bh: buffer to be deleted * * nilfs_btnode_delete() invalidates the specified buffer and delete the page * including the buffer if the page gets unbusy. */ void nilfs_btnode_delete(struct buffer_head *bh) { struct address_space *mapping; struct page *page = bh->b_page; pgoff_t index = page_index(page); int still_dirty; |
09cbfeaf1 mm, fs: get rid o... |
137 |
get_page(page); |
a60be987d nilfs2: B-tree no... |
138 139 140 141 142 143 144 |
lock_page(page); wait_on_page_writeback(page); nilfs_forget_buffer(bh); still_dirty = PageDirty(page); mapping = page->mapping; unlock_page(page); |
09cbfeaf1 mm, fs: get rid o... |
145 |
put_page(page); |
a60be987d nilfs2: B-tree no... |
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
if (!still_dirty && mapping) invalidate_inode_pages2_range(mapping, index, index); } /** * 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; |
09cbfeaf1 mm, fs: get rid o... |
171 |
if (inode->i_blkbits == PAGE_SHIFT) { |
a60be987d nilfs2: B-tree no... |
172 173 174 175 176 |
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. */ |
b1f1b8ce0 nilfs2: fix preem... |
177 |
retry: |
a60be987d nilfs2: B-tree no... |
178 179 180 181 182 183 184 185 186 |
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); |
a60be987d nilfs2: B-tree no... |
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
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); } |
45f4910bc nilfs2: use nilfs... |
208 209 210 211 212 213 214 |
nbh = nilfs_btnode_create_block(btnc, newkey); if (!nbh) return -ENOMEM; BUG_ON(nbh == obh); ctxt->newbh = nbh; return 0; |
a60be987d nilfs2: B-tree no... |
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
failed_unlock: unlock_page(obh->b_page); return err; } /** * 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); |
5fc7b1417 nilfs2: use mark_... |
242 |
mark_buffer_dirty(obh); |
a60be987d nilfs2: B-tree no... |
243 244 245 246 247 248 249 250 251 252 253 |
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); |
5fc7b1417 nilfs2: use mark_... |
254 |
mark_buffer_dirty(nbh); |
a60be987d nilfs2: B-tree no... |
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 |
nbh->b_blocknr = newkey; ctxt->bh = nbh; nilfs_btnode_delete(obh); /* will decrement bh->b_count */ } } /** * nilfs_btnode_abort_change_key * abort the change_key operation prepared by prepare_change_key(). */ void nilfs_btnode_abort_change_key(struct address_space *btnc, struct nilfs_btnode_chkey_ctxt *ctxt) { struct buffer_head *nbh = ctxt->newbh; __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; if (oldkey == newkey) return; if (nbh == NULL) { /* blocksize == pagesize */ spin_lock_irq(&btnc->tree_lock); radix_tree_delete(&btnc->page_tree, newkey); spin_unlock_irq(&btnc->tree_lock); unlock_page(ctxt->bh->b_page); } else brelse(nbh); } |