btnode.c 7.93 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
Ryusuke Konishi's avatar
Ryusuke Konishi committed
2
/*
3
 * NILFS B-tree node cache
Ryusuke Konishi's avatar
Ryusuke Konishi committed
4 5 6
 *
 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
 *
7 8
 * Originally written by Seiji Kihara.
 * Fully revised by Ryusuke Konishi for stabilization and simplification.
Ryusuke Konishi's avatar
Ryusuke Konishi committed
9 10 11 12 13 14 15
 *
 */

#include <linux/types.h>
#include <linux/buffer_head.h>
#include <linux/mm.h>
#include <linux/backing-dev.h>
16
#include <linux/gfp.h>
Ryusuke Konishi's avatar
Ryusuke Konishi committed
17 18 19 20 21 22
#include "nilfs.h"
#include "mdt.h"
#include "dat.h"
#include "page.h"
#include "btnode.h"

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

/**
 * nilfs_init_btnc_inode - initialize B-tree node cache inode
 * @btnc_inode: inode to be initialized
 *
 * nilfs_init_btnc_inode() sets up an inode for B-tree node cache.
 */
void nilfs_init_btnc_inode(struct inode *btnc_inode)
{
	struct nilfs_inode_info *ii = NILFS_I(btnc_inode);

	btnc_inode->i_mode = S_IFREG;
	ii->i_flags = 0;
	memset(&ii->i_bmap_data, 0, sizeof(struct nilfs_bmap));
	mapping_set_gfp_mask(btnc_inode->i_mapping, GFP_NOFS);
}

Ryusuke Konishi's avatar
Ryusuke Konishi committed
40 41 42 43 44 45
void nilfs_btnode_cache_clear(struct address_space *btnc)
{
	invalidate_mapping_pages(btnc, 0, -1);
	truncate_inode_pages(btnc, 0);
}

46 47 48
struct buffer_head *
nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
{
49
	struct inode *inode = btnc->host;
50 51
	struct buffer_head *bh;

Ryusuke Konishi's avatar
Ryusuke Konishi committed
52
	bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
53
	if (unlikely(!bh))
54
		return ERR_PTR(-ENOMEM);
55 56 57

	if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
		     buffer_dirty(bh))) {
58 59 60 61 62 63 64 65 66 67 68
		/*
		 * The block buffer at the specified new address was already
		 * in use.  This can happen if it is a virtual block number
		 * and has been reallocated due to corruption of the bitmap
		 * used to manage its allocation state (if not, the buffer
		 * clearing of an abandoned b-tree node is missing somewhere).
		 */
		nilfs_error(inode->i_sb,
			    "state inconsistency probably due to duplicate use of b-tree node block address %llu (ino=%lu)",
			    (unsigned long long)blocknr, inode->i_ino);
		goto failed;
69
	}
Fabian Frederick's avatar
Fabian Frederick committed
70
	memset(bh->b_data, 0, i_blocksize(inode));
71
	bh->b_bdev = inode->i_sb->s_bdev;
72 73 74 75
	bh->b_blocknr = blocknr;
	set_buffer_mapped(bh);
	set_buffer_uptodate(bh);

76 77
	folio_unlock(bh->b_folio);
	folio_put(bh->b_folio);
78
	return bh;
79 80 81 82 83 84

failed:
	folio_unlock(bh->b_folio);
	folio_put(bh->b_folio);
	brelse(bh);
	return ERR_PTR(-EIO);
85 86
}

Ryusuke Konishi's avatar
Ryusuke Konishi committed
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)
Ryusuke Konishi's avatar
Ryusuke Konishi committed
90 91
{
	struct buffer_head *bh;
92
	struct inode *inode = btnc->host;
93
	struct folio *folio;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
94 95
	int err;

Ryusuke Konishi's avatar
Ryusuke Konishi committed
96
	bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
Ryusuke Konishi's avatar
Ryusuke Konishi committed
97 98 99 100
	if (unlikely(!bh))
		return -ENOMEM;

	err = -EEXIST; /* internal code */
101
	folio = bh->b_folio;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
102 103 104 105 106 107 108

	if (buffer_uptodate(bh) || buffer_dirty(bh))
		goto found;

	if (pblocknr == 0) {
		pblocknr = blocknr;
		if (inode->i_ino != NILFS_DAT_INO) {
109
			struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
110 111

			/* blocknr is a virtual block number */
112 113
			err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
						  &pblocknr);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
114 115 116 117 118 119
			if (unlikely(err)) {
				brelse(bh);
				goto out_locked;
			}
		}
	}
120

121
	if (opf & REQ_RAHEAD) {
122 123 124 125 126
		if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
			err = -EBUSY; /* internal code */
			brelse(bh);
			goto out_locked;
		}
127
	} else { /* opf == REQ_OP_READ */
128 129
		lock_buffer(bh);
	}
Ryusuke Konishi's avatar
Ryusuke Konishi committed
130 131 132 133 134 135
	if (buffer_uptodate(bh)) {
		unlock_buffer(bh);
		err = -EEXIST; /* internal code */
		goto found;
	}
	set_buffer_mapped(bh);
136
	bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
137 138 139
	bh->b_blocknr = pblocknr; /* set block address for read */
	bh->b_end_io = end_buffer_read_sync;
	get_bh(bh);
140
	submit_bh(opf, bh);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
141
	bh->b_blocknr = blocknr; /* set back to the given block address */
142
	*submit_ptr = pblocknr;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
143 144 145 146 147
	err = 0;
found:
	*pbh = bh;

out_locked:
148 149
	folio_unlock(folio);
	folio_put(folio);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
150 151 152 153 154 155 156 157 158 159 160 161 162
	return err;
}

/**
 * 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;
163 164
	struct folio *folio = bh->b_folio;
	pgoff_t index = folio->index;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
165 166
	int still_dirty;

167 168 169
	folio_get(folio);
	folio_lock(folio);
	folio_wait_writeback(folio);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
170 171

	nilfs_forget_buffer(bh);
172 173 174 175
	still_dirty = folio_test_dirty(folio);
	mapping = folio->mapping;
	folio_unlock(folio);
	folio_put(folio);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191

	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;
192
	struct inode *inode = btnc->host;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
193 194 195 196 197 198 199 200 201
	__u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
	int err;

	if (oldkey == newkey)
		return 0;

	obh = ctxt->bh;
	ctxt->newbh = NULL;

202
	if (inode->i_blkbits == PAGE_SHIFT) {
203 204
		struct folio *ofolio = obh->b_folio;
		folio_lock(ofolio);
205
retry:
206
		/* BUG_ON(oldkey != obh->b_folio->index); */
207
		if (unlikely(oldkey != ofolio->index))
208
			NILFS_FOLIO_BUG(ofolio,
Ryusuke Konishi's avatar
Ryusuke Konishi committed
209 210 211 212
				       "invalid oldkey %lld (newkey=%lld)",
				       (unsigned long long)oldkey,
				       (unsigned long long)newkey);

Matthew Wilcox's avatar
Matthew Wilcox committed
213
		xa_lock_irq(&btnc->i_pages);
214
		err = __xa_insert(&btnc->i_pages, newkey, ofolio, GFP_NOFS);
Matthew Wilcox's avatar
Matthew Wilcox committed
215
		xa_unlock_irq(&btnc->i_pages);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
216
		/*
217
		 * Note: folio->index will not change to newkey until
Ryusuke Konishi's avatar
Ryusuke Konishi committed
218
		 * nilfs_btnode_commit_change_key() will be called.
219
		 * To protect the folio in intermediate state, the folio lock
Ryusuke Konishi's avatar
Ryusuke Konishi committed
220 221 222 223
		 * is held.
		 */
		if (!err)
			return 0;
224
		else if (err != -EBUSY)
Ryusuke Konishi's avatar
Ryusuke Konishi committed
225 226 227 228 229 230
			goto failed_unlock;

		err = invalidate_inode_pages2_range(btnc, newkey, newkey);
		if (!err)
			goto retry;
		/* fallback to copy mode */
231
		folio_unlock(ofolio);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
232 233
	}

234
	nbh = nilfs_btnode_create_block(btnc, newkey);
235 236
	if (IS_ERR(nbh))
		return PTR_ERR(nbh);
237 238 239 240

	BUG_ON(nbh == obh);
	ctxt->newbh = nbh;
	return 0;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
241 242

 failed_unlock:
243
	folio_unlock(obh->b_folio);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
244 245 246 247 248 249 250 251 252 253 254 255
	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;
256
	struct folio *ofolio;
Ryusuke Konishi's avatar
Ryusuke Konishi committed
257 258 259 260 261

	if (oldkey == newkey)
		return;

	if (nbh == NULL) {	/* blocksize == pagesize */
262 263
		ofolio = obh->b_folio;
		if (unlikely(oldkey != ofolio->index))
264
			NILFS_FOLIO_BUG(ofolio,
Ryusuke Konishi's avatar
Ryusuke Konishi committed
265 266 267
				       "invalid oldkey %lld (newkey=%lld)",
				       (unsigned long long)oldkey,
				       (unsigned long long)newkey);
268
		mark_buffer_dirty(obh);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
269

Matthew Wilcox's avatar
Matthew Wilcox committed
270
		xa_lock_irq(&btnc->i_pages);
Matthew Wilcox's avatar
Matthew Wilcox committed
271 272
		__xa_erase(&btnc->i_pages, oldkey);
		__xa_set_mark(&btnc->i_pages, newkey, PAGECACHE_TAG_DIRTY);
Matthew Wilcox's avatar
Matthew Wilcox committed
273
		xa_unlock_irq(&btnc->i_pages);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
274

275 276
		ofolio->index = obh->b_blocknr = newkey;
		folio_unlock(ofolio);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
277 278
	} else {
		nilfs_copy_buffer(nbh, obh);
279
		mark_buffer_dirty(nbh);
Ryusuke Konishi's avatar
Ryusuke Konishi committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300

		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 */
Matthew Wilcox's avatar
Matthew Wilcox committed
301
		xa_erase_irq(&btnc->i_pages, newkey);
302
		folio_unlock(ctxt->bh->b_folio);
303 304 305 306 307 308 309 310 311 312
	} else {
		/*
		 * When canceling a buffer that a prepare operation has
		 * allocated to copy a node block to another location, use
		 * nilfs_btnode_delete() to initialize and release the buffer
		 * so that the buffer flags will not be in an inconsistent
		 * state when it is reallocated.
		 */
		nilfs_btnode_delete(nbh);
	}
Ryusuke Konishi's avatar
Ryusuke Konishi committed
313
}