1 // SPDX-License-Identifier: GPL-2.0+ 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 } 39 40 void nilfs_btnode_cache_clear(struct address_space *btnc) 41 { 42 invalidate_mapping_pages(btnc, 0, -1); 43 truncate_inode_pages(btnc, 0); 44 } 45 46 struct buffer_head * 47 nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr) 48 { 49 struct inode *inode = btnc->host; 50 struct buffer_head *bh; 51 52 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node)); 53 if (unlikely(!bh)) 54 return ERR_PTR(-ENOMEM); 55 56 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) || 57 buffer_dirty(bh))) { 58 /* 59 * The block buffer at the specified new address was already 60 * in use. This can happen if it is a virtual block number 61 * and has been reallocated due to corruption of the bitmap 62 * used to manage its allocation state (if not, the buffer 63 * clearing of an abandoned b-tree node is missing somewhere). 64 */ 65 nilfs_error(inode->i_sb, 66 "state inconsistency probably due to duplicate use of b-tree node block address %llu (ino=%lu)", 67 (unsigned long long)blocknr, inode->i_ino); 68 goto failed; 69 } 70 memset(bh->b_data, 0, i_blocksize(inode)); 71 bh->b_blocknr = blocknr; 72 set_buffer_mapped(bh); 73 set_buffer_uptodate(bh); 74 75 unlock_page(bh->b_page); 76 put_page(bh->b_page); 77 return bh; 78 79 failed: 80 unlock_page(bh->b_page); 81 put_page(bh->b_page); 82 brelse(bh); 83 return ERR_PTR(-EIO); 84 } 85 86 int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr, 87 sector_t pblocknr, blk_opf_t opf, 88 struct buffer_head **pbh, sector_t *submit_ptr) 89 { 90 struct buffer_head *bh; 91 struct inode *inode = btnc->host; 92 struct page *page; 93 int err; 94 95 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node)); 96 if (unlikely(!bh)) 97 return -ENOMEM; 98 99 err = -EEXIST; /* internal code */ 100 page = bh->b_page; 101 102 if (buffer_uptodate(bh) || buffer_dirty(bh)) 103 goto found; 104 105 if (pblocknr == 0) { 106 pblocknr = blocknr; 107 if (inode->i_ino != NILFS_DAT_INO) { 108 struct the_nilfs *nilfs = inode->i_sb->s_fs_info; 109 110 /* blocknr is a virtual block number */ 111 err = nilfs_dat_translate(nilfs->ns_dat, blocknr, 112 &pblocknr); 113 if (unlikely(err)) { 114 brelse(bh); 115 goto out_locked; 116 } 117 } 118 } 119 120 if (opf & REQ_RAHEAD) { 121 if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) { 122 err = -EBUSY; /* internal code */ 123 brelse(bh); 124 goto out_locked; 125 } 126 } else { /* opf == REQ_OP_READ */ 127 lock_buffer(bh); 128 } 129 if (buffer_uptodate(bh)) { 130 unlock_buffer(bh); 131 err = -EEXIST; /* internal code */ 132 goto found; 133 } 134 set_buffer_mapped(bh); 135 bh->b_blocknr = pblocknr; /* set block address for read */ 136 bh->b_end_io = end_buffer_read_sync; 137 get_bh(bh); 138 submit_bh(opf, bh); 139 bh->b_blocknr = blocknr; /* set back to the given block address */ 140 *submit_ptr = pblocknr; 141 err = 0; 142 found: 143 *pbh = bh; 144 145 out_locked: 146 unlock_page(page); 147 put_page(page); 148 return err; 149 } 150 151 /** 152 * nilfs_btnode_delete - delete B-tree node buffer 153 * @bh: buffer to be deleted 154 * 155 * nilfs_btnode_delete() invalidates the specified buffer and delete the page 156 * including the buffer if the page gets unbusy. 157 */ 158 void nilfs_btnode_delete(struct buffer_head *bh) 159 { 160 struct address_space *mapping; 161 struct page *page = bh->b_page; 162 pgoff_t index = page_index(page); 163 int still_dirty; 164 165 get_page(page); 166 lock_page(page); 167 wait_on_page_writeback(page); 168 169 nilfs_forget_buffer(bh); 170 still_dirty = PageDirty(page); 171 mapping = page->mapping; 172 unlock_page(page); 173 put_page(page); 174 175 if (!still_dirty && mapping) 176 invalidate_inode_pages2_range(mapping, index, index); 177 } 178 179 /** 180 * nilfs_btnode_prepare_change_key 181 * prepare to move contents of the block for old key to one of new key. 182 * the old buffer will not be removed, but might be reused for new buffer. 183 * it might return -ENOMEM because of memory allocation errors, 184 * and might return -EIO because of disk read errors. 185 */ 186 int nilfs_btnode_prepare_change_key(struct address_space *btnc, 187 struct nilfs_btnode_chkey_ctxt *ctxt) 188 { 189 struct buffer_head *obh, *nbh; 190 struct inode *inode = btnc->host; 191 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; 192 int err; 193 194 if (oldkey == newkey) 195 return 0; 196 197 obh = ctxt->bh; 198 ctxt->newbh = NULL; 199 200 if (inode->i_blkbits == PAGE_SHIFT) { 201 struct page *opage = obh->b_page; 202 lock_page(opage); 203 retry: 204 /* BUG_ON(oldkey != obh->b_folio->index); */ 205 if (unlikely(oldkey != opage->index)) 206 NILFS_PAGE_BUG(opage, 207 "invalid oldkey %lld (newkey=%lld)", 208 (unsigned long long)oldkey, 209 (unsigned long long)newkey); 210 211 xa_lock_irq(&btnc->i_pages); 212 err = __xa_insert(&btnc->i_pages, newkey, opage, GFP_NOFS); 213 xa_unlock_irq(&btnc->i_pages); 214 /* 215 * Note: page->index will not change to newkey until 216 * nilfs_btnode_commit_change_key() will be called. 217 * To protect the page in intermediate state, the page lock 218 * is held. 219 */ 220 if (!err) 221 return 0; 222 else if (err != -EBUSY) 223 goto failed_unlock; 224 225 err = invalidate_inode_pages2_range(btnc, newkey, newkey); 226 if (!err) 227 goto retry; 228 /* fallback to copy mode */ 229 unlock_page(opage); 230 } 231 232 nbh = nilfs_btnode_create_block(btnc, newkey); 233 if (IS_ERR(nbh)) 234 return PTR_ERR(nbh); 235 236 BUG_ON(nbh == obh); 237 ctxt->newbh = nbh; 238 return 0; 239 240 failed_unlock: 241 unlock_page(obh->b_page); 242 return err; 243 } 244 245 /** 246 * nilfs_btnode_commit_change_key 247 * commit the change_key operation prepared by prepare_change_key(). 248 */ 249 void nilfs_btnode_commit_change_key(struct address_space *btnc, 250 struct nilfs_btnode_chkey_ctxt *ctxt) 251 { 252 struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh; 253 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; 254 struct page *opage; 255 256 if (oldkey == newkey) 257 return; 258 259 if (nbh == NULL) { /* blocksize == pagesize */ 260 opage = obh->b_page; 261 if (unlikely(oldkey != opage->index)) 262 NILFS_PAGE_BUG(opage, 263 "invalid oldkey %lld (newkey=%lld)", 264 (unsigned long long)oldkey, 265 (unsigned long long)newkey); 266 mark_buffer_dirty(obh); 267 268 xa_lock_irq(&btnc->i_pages); 269 __xa_erase(&btnc->i_pages, oldkey); 270 __xa_set_mark(&btnc->i_pages, newkey, PAGECACHE_TAG_DIRTY); 271 xa_unlock_irq(&btnc->i_pages); 272 273 opage->index = obh->b_blocknr = newkey; 274 unlock_page(opage); 275 } else { 276 nilfs_copy_buffer(nbh, obh); 277 mark_buffer_dirty(nbh); 278 279 nbh->b_blocknr = newkey; 280 ctxt->bh = nbh; 281 nilfs_btnode_delete(obh); /* will decrement bh->b_count */ 282 } 283 } 284 285 /** 286 * nilfs_btnode_abort_change_key 287 * abort the change_key operation prepared by prepare_change_key(). 288 */ 289 void nilfs_btnode_abort_change_key(struct address_space *btnc, 290 struct nilfs_btnode_chkey_ctxt *ctxt) 291 { 292 struct buffer_head *nbh = ctxt->newbh; 293 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey; 294 295 if (oldkey == newkey) 296 return; 297 298 if (nbh == NULL) { /* blocksize == pagesize */ 299 xa_erase_irq(&btnc->i_pages, newkey); 300 unlock_page(ctxt->bh->b_page); 301 } else { 302 /* 303 * When canceling a buffer that a prepare operation has 304 * allocated to copy a node block to another location, use 305 * nilfs_btnode_delete() to initialize and release the buffer 306 * so that the buffer flags will not be in an inconsistent 307 * state when it is reallocated. 308 */ 309 nilfs_btnode_delete(nbh); 310 } 311 } 312