| 1 | |
| 2 | |
| 3 | * NILFS B-tree node cache
|
| 4 | *
|
| 5 | * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
|
| 6 | *
|
| 7 | * Originally written by Seiji Kihara.
|
| 8 | * Fully revised by Ryusuke Konishi for stabilization and simplification.
|
| 9 | *
|
| 10 |
|
| 11 |
|
| 12 | #include <linux/types.h>
|
| 13 | #include <linux/buffer_head.h>
|
| 14 | #include <linux/mm.h>
|
| 15 | #include <linux/backing-dev.h>
|
| 16 | #include <linux/gfp.h>
|
| 17 | #include "nilfs.h"
|
| 18 | #include "mdt.h"
|
| 19 | #include "dat.h"
|
| 20 | #include "page.h"
|
| 21 | #include "btnode.h"
|
| 22 |
|
| 23 |
|
| 24 | |
| 25 | * nilfs_init_btnc_inode - initialize B-tree node cache inode
|
| 26 | * @btnc_inode: inode to be initialized
|
| 27 | *
|
| 28 | * nilfs_init_btnc_inode() sets up an inode for B-tree node cache.
|
| 29 |
|
| 30 | void nilfs_init_btnc_inode(struct inode *btnc_inode)
|
| 31 | {
|
| 32 | struct nilfs_inode_info *ii = NILFS_I(btnc_inode);
|
| 33 |
|
| 34 | btnc_inode->i_mode = S_IFREG;
|
| 35 | ii->i_flags = 0;
|
| 36 | memset(&ii->i_bmap_data, 0, sizeof(struct nilfs_bmap));
|
| 37 | mapping_set_gfp_mask(btnc_inode->i_mapping, GFP_NOFS);
|
| 38 | btnc_inode->i_mapping->a_ops = &nilfs_buffer_cache_aops;
|
| 39 | }
|
| 40 |
|
| 41 | void nilfs_btnode_cache_clear(struct address_space *btnc)
|
| 42 | {
|
| 43 | invalidate_mapping_pages(btnc, 0, -1);
|
| 44 | truncate_inode_pages(btnc, 0);
|
| 45 | }
|
| 46 |
|
| 47 | struct buffer_head *
|
| 48 | nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
|
| 49 | {
|
| 50 | struct inode *inode = btnc->host;
|
| 51 | struct buffer_head *bh;
|
| 52 |
|
| 53 | bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
|
| 54 | if (unlikely(!bh))
|
| 55 | return ERR_PTR(-ENOMEM);
|
| 56 |
|
| 57 | if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
|
| 58 | buffer_dirty(bh))) {
|
| 59 | |
| 60 | * The block buffer at the specified new address was already
|
| 61 | * in use. This can happen if it is a virtual block number
|
| 62 | * and has been reallocated due to corruption of the bitmap
|
| 63 | * used to manage its allocation state (if not, the buffer
|
| 64 | * clearing of an abandoned b-tree node is missing somewhere).
|
| 65 |
|
| 66 | nilfs_error(inode->i_sb,
|
| 67 | "state inconsistency probably due to duplicate use of b-tree node block address %llu (ino=%llu)",
|
| 68 | (unsigned long long)blocknr, inode->i_ino);
|
| 69 | goto failed;
|
| 70 | }
|
| 71 | memset(bh->b_data, 0, i_blocksize(inode));
|
| 72 | bh->b_blocknr = blocknr;
|
| 73 | set_buffer_mapped(bh);
|
| 74 | set_buffer_uptodate(bh);
|
| 75 |
|
| 76 | folio_unlock(bh->b_folio);
|
| 77 | folio_put(bh->b_folio);
|
| 78 | return bh;
|
| 79 |
|
| 80 | failed:
|
| 81 | folio_unlock(bh->b_folio);
|
| 82 | folio_put(bh->b_folio);
|
| 83 | brelse(bh);
|
| 84 | return ERR_PTR(-EIO);
|
| 85 | }
|
| 86 |
|
| 87 | int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
|
| 88 | sector_t pblocknr, blk_opf_t opf,
|
| 89 | struct buffer_head **pbh, sector_t *submit_ptr)
|
| 90 | {
|
| 91 | struct buffer_head *bh;
|
| 92 | struct inode *inode = btnc->host;
|
| 93 | struct folio *folio;
|
| 94 | int err;
|
| 95 |
|
| 96 | bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
|
| 97 | if (unlikely(!bh))
|
| 98 | return -ENOMEM;
|
| 99 |
|
| 100 | err = -EEXIST;
|
| 101 | folio = bh->b_folio;
|
| 102 |
|
| 103 | if (buffer_uptodate(bh) || buffer_dirty(bh))
|
| 104 | goto found;
|
| 105 |
|
| 106 | if (pblocknr == 0) {
|
| 107 | pblocknr = blocknr;
|
| 108 | if (inode->i_ino != NILFS_DAT_INO) {
|
| 109 | struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
|
| 110 |
|
| 111 |
|
| 112 | err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
|
| 113 | &pblocknr);
|
| 114 | if (unlikely(err)) {
|
| 115 | brelse(bh);
|
| 116 | goto out_locked;
|
| 117 | }
|
| 118 | }
|
| 119 | }
|
| 120 |
|
| 121 | if (opf & REQ_RAHEAD) {
|
| 122 | if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
|
| 123 | err = -EBUSY;
|
| 124 | brelse(bh);
|
| 125 | goto out_locked;
|
| 126 | }
|
| 127 | } else {
|
| 128 | lock_buffer(bh);
|
| 129 | }
|
| 130 | if (buffer_uptodate(bh)) {
|
| 131 | unlock_buffer(bh);
|
| 132 | err = -EEXIST;
|
| 133 | goto found;
|
| 134 | }
|
| 135 | set_buffer_mapped(bh);
|
| 136 | bh->b_blocknr = pblocknr;
|
| 137 | bh_submit(bh, opf, bh_end_read);
|
| 138 | bh->b_blocknr = blocknr;
|
| 139 | *submit_ptr = pblocknr;
|
| 140 | err = 0;
|
| 141 | found:
|
| 142 | *pbh = bh;
|
| 143 |
|
| 144 | out_locked:
|
| 145 | folio_unlock(folio);
|
| 146 | folio_put(folio);
|
| 147 | return err;
|
| 148 | }
|
| 149 |
|
| 150 | |
| 151 | * nilfs_btnode_delete - delete B-tree node buffer
|
| 152 | * @bh: buffer to be deleted
|
| 153 | *
|
| 154 | * nilfs_btnode_delete() invalidates the specified buffer and delete the page
|
| 155 | * including the buffer if the page gets unbusy.
|
| 156 |
|
| 157 | void nilfs_btnode_delete(struct buffer_head *bh)
|
| 158 | {
|
| 159 | struct address_space *mapping;
|
| 160 | struct folio *folio = bh->b_folio;
|
| 161 | pgoff_t index = folio->index;
|
| 162 | int still_dirty;
|
| 163 |
|
| 164 | folio_get(folio);
|
| 165 | folio_lock(folio);
|
| 166 | folio_wait_writeback(folio);
|
| 167 |
|
| 168 | nilfs_forget_buffer(bh);
|
| 169 | still_dirty = folio_test_dirty(folio);
|
| 170 | mapping = folio->mapping;
|
| 171 | folio_unlock(folio);
|
| 172 | folio_put(folio);
|
| 173 |
|
| 174 | if (!still_dirty && mapping)
|
| 175 | invalidate_inode_pages2_range(mapping, index, index);
|
| 176 | }
|
| 177 |
|
| 178 | |
| 179 | * nilfs_btnode_prepare_change_key - prepare to change the search key of a
|
| 180 | * b-tree node block
|
| 181 | * @btnc: page cache in which the b-tree node block is buffered
|
| 182 | * @ctxt: structure for exchanging context information for key change
|
| 183 | *
|
| 184 | * nilfs_btnode_prepare_change_key() prepares to move the contents of the
|
| 185 | * b-tree node block of the old key given in the "oldkey" member of @ctxt to
|
| 186 | * the position of the new key given in the "newkey" member of @ctxt in the
|
| 187 | * page cache @btnc. Here, the key of the block is an index in units of
|
| 188 | * blocks, and if the page and block sizes match, it matches the page index
|
| 189 | * in the page cache.
|
| 190 | *
|
| 191 | * If the page size and block size match, this function attempts to move the
|
| 192 | * entire folio, and in preparation for this, inserts the original folio into
|
| 193 | * the new index of the cache. If this insertion fails or if the page size
|
| 194 | * and block size are different, it falls back to a copy preparation using
|
| 195 | * nilfs_btnode_create_block(), inserts a new block at the position
|
| 196 | * corresponding to "newkey", and stores the buffer head pointer in the
|
| 197 | * "newbh" member of @ctxt.
|
| 198 | *
|
| 199 | * Note that the current implementation does not support folio sizes larger
|
| 200 | * than the page size.
|
| 201 | *
|
| 202 | * Return: 0 on success, or one of the following negative error codes on
|
| 203 | * failure:
|
| 204 | * * %-EIO - I/O error (metadata corruption).
|
| 205 | * * %-ENOMEM - Insufficient memory available.
|
| 206 |
|
| 207 | int nilfs_btnode_prepare_change_key(struct address_space *btnc,
|
| 208 | struct nilfs_btnode_chkey_ctxt *ctxt)
|
| 209 | {
|
| 210 | struct buffer_head *obh, *nbh;
|
| 211 | struct inode *inode = btnc->host;
|
| 212 | __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
|
| 213 | int err;
|
| 214 |
|
| 215 | if (oldkey == newkey)
|
| 216 | return 0;
|
| 217 |
|
| 218 | obh = ctxt->bh;
|
| 219 | ctxt->newbh = NULL;
|
| 220 |
|
| 221 | if (inode->i_blkbits == PAGE_SHIFT) {
|
| 222 | struct folio *ofolio = obh->b_folio;
|
| 223 | folio_lock(ofolio);
|
| 224 | retry:
|
| 225 |
|
| 226 | if (unlikely(oldkey != ofolio->index))
|
| 227 | NILFS_FOLIO_BUG(ofolio,
|
| 228 | "invalid oldkey %lld (newkey=%lld)",
|
| 229 | (unsigned long long)oldkey,
|
| 230 | (unsigned long long)newkey);
|
| 231 |
|
| 232 | xa_lock_irq(&btnc->i_pages);
|
| 233 | err = __xa_insert(&btnc->i_pages, newkey, ofolio, GFP_NOFS);
|
| 234 | xa_unlock_irq(&btnc->i_pages);
|
| 235 | |
| 236 | * Note: folio->index will not change to newkey until
|
| 237 | * nilfs_btnode_commit_change_key() will be called.
|
| 238 | * To protect the folio in intermediate state, the folio lock
|
| 239 | * is held.
|
| 240 |
|
| 241 | if (!err)
|
| 242 | return 0;
|
| 243 | else if (err != -EBUSY)
|
| 244 | goto failed_unlock;
|
| 245 |
|
| 246 | err = invalidate_inode_pages2_range(btnc, newkey, newkey);
|
| 247 | if (!err)
|
| 248 | goto retry;
|
| 249 |
|
| 250 | folio_unlock(ofolio);
|
| 251 | }
|
| 252 |
|
| 253 | nbh = nilfs_btnode_create_block(btnc, newkey);
|
| 254 | if (IS_ERR(nbh))
|
| 255 | return PTR_ERR(nbh);
|
| 256 |
|
| 257 | BUG_ON(nbh == obh);
|
| 258 | ctxt->newbh = nbh;
|
| 259 | return 0;
|
| 260 |
|
| 261 | failed_unlock:
|
| 262 | folio_unlock(obh->b_folio);
|
| 263 | return err;
|
| 264 | }
|
| 265 |
|
| 266 | |
| 267 | * nilfs_btnode_commit_change_key - commit the change of the search key of
|
| 268 | * a b-tree node block
|
| 269 | * @btnc: page cache in which the b-tree node block is buffered
|
| 270 | * @ctxt: structure for exchanging context information for key change
|
| 271 | *
|
| 272 | * nilfs_btnode_commit_change_key() executes the key change based on the
|
| 273 | * context @ctxt prepared by nilfs_btnode_prepare_change_key(). If no valid
|
| 274 | * block buffer is prepared in "newbh" of @ctxt (i.e., a full folio move),
|
| 275 | * this function removes the folio from the old index and completes the move.
|
| 276 | * Otherwise, it copies the block data and inherited flag states of "oldbh"
|
| 277 | * to "newbh" and clears the "oldbh" from the cache. In either case, the
|
| 278 | * relocated buffer is marked as dirty.
|
| 279 | *
|
| 280 | * As with nilfs_btnode_prepare_change_key(), the current implementation does
|
| 281 | * not support folio sizes larger than the page size.
|
| 282 |
|
| 283 | void nilfs_btnode_commit_change_key(struct address_space *btnc,
|
| 284 | struct nilfs_btnode_chkey_ctxt *ctxt)
|
| 285 | {
|
| 286 | struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
|
| 287 | __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
|
| 288 | struct folio *ofolio;
|
| 289 |
|
| 290 | if (oldkey == newkey)
|
| 291 | return;
|
| 292 |
|
| 293 | if (nbh == NULL) {
|
| 294 | ofolio = obh->b_folio;
|
| 295 | if (unlikely(oldkey != ofolio->index))
|
| 296 | NILFS_FOLIO_BUG(ofolio,
|
| 297 | "invalid oldkey %lld (newkey=%lld)",
|
| 298 | (unsigned long long)oldkey,
|
| 299 | (unsigned long long)newkey);
|
| 300 | mark_buffer_dirty(obh);
|
| 301 |
|
| 302 | xa_lock_irq(&btnc->i_pages);
|
| 303 | __xa_erase(&btnc->i_pages, oldkey);
|
| 304 | __xa_set_mark(&btnc->i_pages, newkey, PAGECACHE_TAG_DIRTY);
|
| 305 | xa_unlock_irq(&btnc->i_pages);
|
| 306 |
|
| 307 | ofolio->index = obh->b_blocknr = newkey;
|
| 308 | folio_unlock(ofolio);
|
| 309 | } else {
|
| 310 | nilfs_copy_buffer(nbh, obh);
|
| 311 | mark_buffer_dirty(nbh);
|
| 312 |
|
| 313 | nbh->b_blocknr = newkey;
|
| 314 | ctxt->bh = nbh;
|
| 315 | nilfs_btnode_delete(obh);
|
| 316 | }
|
| 317 | }
|
| 318 |
|
| 319 | |
| 320 | * nilfs_btnode_abort_change_key - abort the change of the search key of a
|
| 321 | * b-tree node block
|
| 322 | * @btnc: page cache in which the b-tree node block is buffered
|
| 323 | * @ctxt: structure for exchanging context information for key change
|
| 324 | *
|
| 325 | * nilfs_btnode_abort_change_key() cancels the key change associated with the
|
| 326 | * context @ctxt prepared via nilfs_btnode_prepare_change_key() and performs
|
| 327 | * any necessary cleanup. If no valid block buffer is prepared in "newbh" of
|
| 328 | * @ctxt, this function removes the folio from the destination index and aborts
|
| 329 | * the move. Otherwise, it clears "newbh" from the cache.
|
| 330 | *
|
| 331 | * As with nilfs_btnode_prepare_change_key(), the current implementation does
|
| 332 | * not support folio sizes larger than the page size.
|
| 333 |
|
| 334 | void nilfs_btnode_abort_change_key(struct address_space *btnc,
|
| 335 | struct nilfs_btnode_chkey_ctxt *ctxt)
|
| 336 | {
|
| 337 | struct buffer_head *nbh = ctxt->newbh;
|
| 338 | __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
|
| 339 |
|
| 340 | if (oldkey == newkey)
|
| 341 | return;
|
| 342 |
|
| 343 | if (nbh == NULL) {
|
| 344 | xa_erase_irq(&btnc->i_pages, newkey);
|
| 345 | folio_unlock(ctxt->bh->b_folio);
|
| 346 | } else {
|
| 347 | |
| 348 | * When canceling a buffer that a prepare operation has
|
| 349 | * allocated to copy a node block to another location, use
|
| 350 | * nilfs_btnode_delete() to initialize and release the buffer
|
| 351 | * so that the buffer flags will not be in an inconsistent
|
| 352 | * state when it is reallocated.
|
| 353 |
|
| 354 | nilfs_btnode_delete(nbh);
|
| 355 | }
|
| 356 | }
|