0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/types.h>
0013 #include <linux/buffer_head.h>
0014 #include <linux/mm.h>
0015 #include <linux/backing-dev.h>
0016 #include <linux/gfp.h>
0017 #include "nilfs.h"
0018 #include "mdt.h"
0019 #include "dat.h"
0020 #include "page.h"
0021 #include "btnode.h"
0022
0023
0024
0025
0026
0027
0028
0029
0030 void nilfs_init_btnc_inode(struct inode *btnc_inode)
0031 {
0032 struct nilfs_inode_info *ii = NILFS_I(btnc_inode);
0033
0034 btnc_inode->i_mode = S_IFREG;
0035 ii->i_flags = 0;
0036 memset(&ii->i_bmap_data, 0, sizeof(struct nilfs_bmap));
0037 mapping_set_gfp_mask(btnc_inode->i_mapping, GFP_NOFS);
0038 }
0039
0040 void nilfs_btnode_cache_clear(struct address_space *btnc)
0041 {
0042 invalidate_mapping_pages(btnc, 0, -1);
0043 truncate_inode_pages(btnc, 0);
0044 }
0045
0046 struct buffer_head *
0047 nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
0048 {
0049 struct inode *inode = btnc->host;
0050 struct buffer_head *bh;
0051
0052 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
0053 if (unlikely(!bh))
0054 return NULL;
0055
0056 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
0057 buffer_dirty(bh))) {
0058 brelse(bh);
0059 BUG();
0060 }
0061 memset(bh->b_data, 0, i_blocksize(inode));
0062 bh->b_bdev = inode->i_sb->s_bdev;
0063 bh->b_blocknr = blocknr;
0064 set_buffer_mapped(bh);
0065 set_buffer_uptodate(bh);
0066
0067 unlock_page(bh->b_page);
0068 put_page(bh->b_page);
0069 return bh;
0070 }
0071
0072 int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
0073 sector_t pblocknr, blk_opf_t opf,
0074 struct buffer_head **pbh, sector_t *submit_ptr)
0075 {
0076 struct buffer_head *bh;
0077 struct inode *inode = btnc->host;
0078 struct page *page;
0079 int err;
0080
0081 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
0082 if (unlikely(!bh))
0083 return -ENOMEM;
0084
0085 err = -EEXIST;
0086 page = bh->b_page;
0087
0088 if (buffer_uptodate(bh) || buffer_dirty(bh))
0089 goto found;
0090
0091 if (pblocknr == 0) {
0092 pblocknr = blocknr;
0093 if (inode->i_ino != NILFS_DAT_INO) {
0094 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
0095
0096
0097 err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
0098 &pblocknr);
0099 if (unlikely(err)) {
0100 brelse(bh);
0101 goto out_locked;
0102 }
0103 }
0104 }
0105
0106 if (opf & REQ_RAHEAD) {
0107 if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
0108 err = -EBUSY;
0109 brelse(bh);
0110 goto out_locked;
0111 }
0112 } else {
0113 lock_buffer(bh);
0114 }
0115 if (buffer_uptodate(bh)) {
0116 unlock_buffer(bh);
0117 err = -EEXIST;
0118 goto found;
0119 }
0120 set_buffer_mapped(bh);
0121 bh->b_bdev = inode->i_sb->s_bdev;
0122 bh->b_blocknr = pblocknr;
0123 bh->b_end_io = end_buffer_read_sync;
0124 get_bh(bh);
0125 submit_bh(opf, bh);
0126 bh->b_blocknr = blocknr;
0127 *submit_ptr = pblocknr;
0128 err = 0;
0129 found:
0130 *pbh = bh;
0131
0132 out_locked:
0133 unlock_page(page);
0134 put_page(page);
0135 return err;
0136 }
0137
0138
0139
0140
0141
0142
0143
0144
0145 void nilfs_btnode_delete(struct buffer_head *bh)
0146 {
0147 struct address_space *mapping;
0148 struct page *page = bh->b_page;
0149 pgoff_t index = page_index(page);
0150 int still_dirty;
0151
0152 get_page(page);
0153 lock_page(page);
0154 wait_on_page_writeback(page);
0155
0156 nilfs_forget_buffer(bh);
0157 still_dirty = PageDirty(page);
0158 mapping = page->mapping;
0159 unlock_page(page);
0160 put_page(page);
0161
0162 if (!still_dirty && mapping)
0163 invalidate_inode_pages2_range(mapping, index, index);
0164 }
0165
0166
0167
0168
0169
0170
0171
0172
0173 int nilfs_btnode_prepare_change_key(struct address_space *btnc,
0174 struct nilfs_btnode_chkey_ctxt *ctxt)
0175 {
0176 struct buffer_head *obh, *nbh;
0177 struct inode *inode = btnc->host;
0178 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
0179 int err;
0180
0181 if (oldkey == newkey)
0182 return 0;
0183
0184 obh = ctxt->bh;
0185 ctxt->newbh = NULL;
0186
0187 if (inode->i_blkbits == PAGE_SHIFT) {
0188 struct page *opage = obh->b_page;
0189 lock_page(opage);
0190 retry:
0191
0192 if (unlikely(oldkey != opage->index))
0193 NILFS_PAGE_BUG(opage,
0194 "invalid oldkey %lld (newkey=%lld)",
0195 (unsigned long long)oldkey,
0196 (unsigned long long)newkey);
0197
0198 xa_lock_irq(&btnc->i_pages);
0199 err = __xa_insert(&btnc->i_pages, newkey, opage, GFP_NOFS);
0200 xa_unlock_irq(&btnc->i_pages);
0201
0202
0203
0204
0205
0206
0207 if (!err)
0208 return 0;
0209 else if (err != -EBUSY)
0210 goto failed_unlock;
0211
0212 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
0213 if (!err)
0214 goto retry;
0215
0216 unlock_page(opage);
0217 }
0218
0219 nbh = nilfs_btnode_create_block(btnc, newkey);
0220 if (!nbh)
0221 return -ENOMEM;
0222
0223 BUG_ON(nbh == obh);
0224 ctxt->newbh = nbh;
0225 return 0;
0226
0227 failed_unlock:
0228 unlock_page(obh->b_page);
0229 return err;
0230 }
0231
0232
0233
0234
0235
0236 void nilfs_btnode_commit_change_key(struct address_space *btnc,
0237 struct nilfs_btnode_chkey_ctxt *ctxt)
0238 {
0239 struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
0240 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
0241 struct page *opage;
0242
0243 if (oldkey == newkey)
0244 return;
0245
0246 if (nbh == NULL) {
0247 opage = obh->b_page;
0248 if (unlikely(oldkey != opage->index))
0249 NILFS_PAGE_BUG(opage,
0250 "invalid oldkey %lld (newkey=%lld)",
0251 (unsigned long long)oldkey,
0252 (unsigned long long)newkey);
0253 mark_buffer_dirty(obh);
0254
0255 xa_lock_irq(&btnc->i_pages);
0256 __xa_erase(&btnc->i_pages, oldkey);
0257 __xa_set_mark(&btnc->i_pages, newkey, PAGECACHE_TAG_DIRTY);
0258 xa_unlock_irq(&btnc->i_pages);
0259
0260 opage->index = obh->b_blocknr = newkey;
0261 unlock_page(opage);
0262 } else {
0263 nilfs_copy_buffer(nbh, obh);
0264 mark_buffer_dirty(nbh);
0265
0266 nbh->b_blocknr = newkey;
0267 ctxt->bh = nbh;
0268 nilfs_btnode_delete(obh);
0269 }
0270 }
0271
0272
0273
0274
0275
0276 void nilfs_btnode_abort_change_key(struct address_space *btnc,
0277 struct nilfs_btnode_chkey_ctxt *ctxt)
0278 {
0279 struct buffer_head *nbh = ctxt->newbh;
0280 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
0281
0282 if (oldkey == newkey)
0283 return;
0284
0285 if (nbh == NULL) {
0286 xa_erase_irq(&btnc->i_pages, newkey);
0287 unlock_page(ctxt->bh->b_page);
0288 } else
0289 brelse(nbh);
0290 }