xref: /openbmc/linux/fs/nilfs2/btnode.c (revision 55e43d6abd078ed6d219902ce8cb4d68e3c993ba)
1ae98043fSRyusuke Konishi // SPDX-License-Identifier: GPL-2.0+
2a60be987SRyusuke Konishi /*
394ee1d91SRyusuke Konishi  * NILFS B-tree node cache
4a60be987SRyusuke Konishi  *
5a60be987SRyusuke Konishi  * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6a60be987SRyusuke Konishi  *
74b420ab4SRyusuke Konishi  * Originally written by Seiji Kihara.
84b420ab4SRyusuke Konishi  * Fully revised by Ryusuke Konishi for stabilization and simplification.
9a60be987SRyusuke Konishi  *
10a60be987SRyusuke Konishi  */
11a60be987SRyusuke Konishi 
12a60be987SRyusuke Konishi #include <linux/types.h>
13a60be987SRyusuke Konishi #include <linux/buffer_head.h>
14a60be987SRyusuke Konishi #include <linux/mm.h>
15a60be987SRyusuke Konishi #include <linux/backing-dev.h>
165a0e3ad6STejun Heo #include <linux/gfp.h>
17a60be987SRyusuke Konishi #include "nilfs.h"
18a60be987SRyusuke Konishi #include "mdt.h"
19a60be987SRyusuke Konishi #include "dat.h"
20a60be987SRyusuke Konishi #include "page.h"
21a60be987SRyusuke Konishi #include "btnode.h"
22a60be987SRyusuke Konishi 
23e897be17SRyusuke Konishi 
24e897be17SRyusuke Konishi /**
25e897be17SRyusuke Konishi  * nilfs_init_btnc_inode - initialize B-tree node cache inode
26e897be17SRyusuke Konishi  * @btnc_inode: inode to be initialized
27e897be17SRyusuke Konishi  *
28e897be17SRyusuke Konishi  * nilfs_init_btnc_inode() sets up an inode for B-tree node cache.
29e897be17SRyusuke Konishi  */
nilfs_init_btnc_inode(struct inode * btnc_inode)30e897be17SRyusuke Konishi void nilfs_init_btnc_inode(struct inode *btnc_inode)
31e897be17SRyusuke Konishi {
32e897be17SRyusuke Konishi 	struct nilfs_inode_info *ii = NILFS_I(btnc_inode);
33e897be17SRyusuke Konishi 
34e897be17SRyusuke Konishi 	btnc_inode->i_mode = S_IFREG;
35e897be17SRyusuke Konishi 	ii->i_flags = 0;
36e897be17SRyusuke Konishi 	memset(&ii->i_bmap_data, 0, sizeof(struct nilfs_bmap));
37e897be17SRyusuke Konishi 	mapping_set_gfp_mask(btnc_inode->i_mapping, GFP_NOFS);
38*cfb608b4SRyusuke Konishi 	btnc_inode->i_mapping->a_ops = &nilfs_buffer_cache_aops;
39e897be17SRyusuke Konishi }
40e897be17SRyusuke Konishi 
nilfs_btnode_cache_clear(struct address_space * btnc)41a60be987SRyusuke Konishi void nilfs_btnode_cache_clear(struct address_space *btnc)
42a60be987SRyusuke Konishi {
43a60be987SRyusuke Konishi 	invalidate_mapping_pages(btnc, 0, -1);
44a60be987SRyusuke Konishi 	truncate_inode_pages(btnc, 0);
45a60be987SRyusuke Konishi }
46a60be987SRyusuke Konishi 
47d501d736SRyusuke Konishi struct buffer_head *
nilfs_btnode_create_block(struct address_space * btnc,__u64 blocknr)48d501d736SRyusuke Konishi nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
49d501d736SRyusuke Konishi {
50e897be17SRyusuke Konishi 	struct inode *inode = btnc->host;
51d501d736SRyusuke Konishi 	struct buffer_head *bh;
52d501d736SRyusuke Konishi 
534ce5c342SRyusuke Konishi 	bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
54d501d736SRyusuke Konishi 	if (unlikely(!bh))
55be56dfc9SRyusuke Konishi 		return ERR_PTR(-ENOMEM);
56d501d736SRyusuke Konishi 
57d501d736SRyusuke Konishi 	if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
58d501d736SRyusuke Konishi 		     buffer_dirty(bh))) {
59be56dfc9SRyusuke Konishi 		/*
60be56dfc9SRyusuke Konishi 		 * The block buffer at the specified new address was already
61be56dfc9SRyusuke Konishi 		 * in use.  This can happen if it is a virtual block number
62be56dfc9SRyusuke Konishi 		 * and has been reallocated due to corruption of the bitmap
63be56dfc9SRyusuke Konishi 		 * used to manage its allocation state (if not, the buffer
64be56dfc9SRyusuke Konishi 		 * clearing of an abandoned b-tree node is missing somewhere).
65be56dfc9SRyusuke Konishi 		 */
66be56dfc9SRyusuke Konishi 		nilfs_error(inode->i_sb,
67be56dfc9SRyusuke Konishi 			    "state inconsistency probably due to duplicate use of b-tree node block address %llu (ino=%lu)",
68be56dfc9SRyusuke Konishi 			    (unsigned long long)blocknr, inode->i_ino);
69be56dfc9SRyusuke Konishi 		goto failed;
70d501d736SRyusuke Konishi 	}
7193407472SFabian Frederick 	memset(bh->b_data, 0, i_blocksize(inode));
72d501d736SRyusuke Konishi 	bh->b_blocknr = blocknr;
73d501d736SRyusuke Konishi 	set_buffer_mapped(bh);
74d501d736SRyusuke Konishi 	set_buffer_uptodate(bh);
75d501d736SRyusuke Konishi 
76d501d736SRyusuke Konishi 	unlock_page(bh->b_page);
7709cbfeafSKirill A. Shutemov 	put_page(bh->b_page);
78d501d736SRyusuke Konishi 	return bh;
79be56dfc9SRyusuke Konishi 
80be56dfc9SRyusuke Konishi failed:
81be56dfc9SRyusuke Konishi 	unlock_page(bh->b_page);
82be56dfc9SRyusuke Konishi 	put_page(bh->b_page);
83be56dfc9SRyusuke Konishi 	brelse(bh);
84be56dfc9SRyusuke Konishi 	return ERR_PTR(-EIO);
85d501d736SRyusuke Konishi }
86d501d736SRyusuke Konishi 
nilfs_btnode_submit_block(struct address_space * btnc,__u64 blocknr,sector_t pblocknr,blk_opf_t opf,struct buffer_head ** pbh,sector_t * submit_ptr)87a60be987SRyusuke Konishi int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
88ed451259SBart Van Assche 			      sector_t pblocknr, blk_opf_t opf,
8926dfdd8eSRyusuke Konishi 			      struct buffer_head **pbh, sector_t *submit_ptr)
90a60be987SRyusuke Konishi {
91a60be987SRyusuke Konishi 	struct buffer_head *bh;
92e897be17SRyusuke Konishi 	struct inode *inode = btnc->host;
93f8e6cc01SRyusuke Konishi 	struct page *page;
94a60be987SRyusuke Konishi 	int err;
95a60be987SRyusuke Konishi 
964ce5c342SRyusuke Konishi 	bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
97a60be987SRyusuke Konishi 	if (unlikely(!bh))
98a60be987SRyusuke Konishi 		return -ENOMEM;
99a60be987SRyusuke Konishi 
100a60be987SRyusuke Konishi 	err = -EEXIST; /* internal code */
101f8e6cc01SRyusuke Konishi 	page = bh->b_page;
102a60be987SRyusuke Konishi 
103a60be987SRyusuke Konishi 	if (buffer_uptodate(bh) || buffer_dirty(bh))
104a60be987SRyusuke Konishi 		goto found;
105a60be987SRyusuke Konishi 
106a60be987SRyusuke Konishi 	if (pblocknr == 0) {
107a60be987SRyusuke Konishi 		pblocknr = blocknr;
108a60be987SRyusuke Konishi 		if (inode->i_ino != NILFS_DAT_INO) {
1090ef28f9aSRyusuke Konishi 			struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
110a60be987SRyusuke Konishi 
111a60be987SRyusuke Konishi 			/* blocknr is a virtual block number */
1120ef28f9aSRyusuke Konishi 			err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
1130ef28f9aSRyusuke Konishi 						  &pblocknr);
114a60be987SRyusuke Konishi 			if (unlikely(err)) {
115a60be987SRyusuke Konishi 				brelse(bh);
116a60be987SRyusuke Konishi 				goto out_locked;
117a60be987SRyusuke Konishi 			}
118a60be987SRyusuke Konishi 		}
119a60be987SRyusuke Konishi 	}
12026dfdd8eSRyusuke Konishi 
121ed451259SBart Van Assche 	if (opf & REQ_RAHEAD) {
12226dfdd8eSRyusuke Konishi 		if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
12326dfdd8eSRyusuke Konishi 			err = -EBUSY; /* internal code */
12426dfdd8eSRyusuke Konishi 			brelse(bh);
12526dfdd8eSRyusuke Konishi 			goto out_locked;
12626dfdd8eSRyusuke Konishi 		}
127ed451259SBart Van Assche 	} else { /* opf == REQ_OP_READ */
128a60be987SRyusuke Konishi 		lock_buffer(bh);
12926dfdd8eSRyusuke Konishi 	}
130a60be987SRyusuke Konishi 	if (buffer_uptodate(bh)) {
131a60be987SRyusuke Konishi 		unlock_buffer(bh);
132a60be987SRyusuke Konishi 		err = -EEXIST; /* internal code */
133a60be987SRyusuke Konishi 		goto found;
134a60be987SRyusuke Konishi 	}
135a60be987SRyusuke Konishi 	set_buffer_mapped(bh);
136a60be987SRyusuke Konishi 	bh->b_blocknr = pblocknr; /* set block address for read */
137a60be987SRyusuke Konishi 	bh->b_end_io = end_buffer_read_sync;
138a60be987SRyusuke Konishi 	get_bh(bh);
139ed451259SBart Van Assche 	submit_bh(opf, bh);
140a60be987SRyusuke Konishi 	bh->b_blocknr = blocknr; /* set back to the given block address */
14126dfdd8eSRyusuke Konishi 	*submit_ptr = pblocknr;
142a60be987SRyusuke Konishi 	err = 0;
143a60be987SRyusuke Konishi found:
144a60be987SRyusuke Konishi 	*pbh = bh;
145a60be987SRyusuke Konishi 
146a60be987SRyusuke Konishi out_locked:
147f8e6cc01SRyusuke Konishi 	unlock_page(page);
14809cbfeafSKirill A. Shutemov 	put_page(page);
149a60be987SRyusuke Konishi 	return err;
150a60be987SRyusuke Konishi }
151a60be987SRyusuke Konishi 
152a60be987SRyusuke Konishi /**
153a60be987SRyusuke Konishi  * nilfs_btnode_delete - delete B-tree node buffer
154a60be987SRyusuke Konishi  * @bh: buffer to be deleted
155a60be987SRyusuke Konishi  *
156a60be987SRyusuke Konishi  * nilfs_btnode_delete() invalidates the specified buffer and delete the page
157a60be987SRyusuke Konishi  * including the buffer if the page gets unbusy.
158a60be987SRyusuke Konishi  */
nilfs_btnode_delete(struct buffer_head * bh)159a60be987SRyusuke Konishi void nilfs_btnode_delete(struct buffer_head *bh)
160a60be987SRyusuke Konishi {
161a60be987SRyusuke Konishi 	struct address_space *mapping;
162a60be987SRyusuke Konishi 	struct page *page = bh->b_page;
163a60be987SRyusuke Konishi 	pgoff_t index = page_index(page);
164a60be987SRyusuke Konishi 	int still_dirty;
165a60be987SRyusuke Konishi 
16609cbfeafSKirill A. Shutemov 	get_page(page);
167a60be987SRyusuke Konishi 	lock_page(page);
168a60be987SRyusuke Konishi 	wait_on_page_writeback(page);
169a60be987SRyusuke Konishi 
170a60be987SRyusuke Konishi 	nilfs_forget_buffer(bh);
171a60be987SRyusuke Konishi 	still_dirty = PageDirty(page);
172a60be987SRyusuke Konishi 	mapping = page->mapping;
173a60be987SRyusuke Konishi 	unlock_page(page);
17409cbfeafSKirill A. Shutemov 	put_page(page);
175a60be987SRyusuke Konishi 
176a60be987SRyusuke Konishi 	if (!still_dirty && mapping)
177a60be987SRyusuke Konishi 		invalidate_inode_pages2_range(mapping, index, index);
178a60be987SRyusuke Konishi }
179a60be987SRyusuke Konishi 
180a60be987SRyusuke Konishi /**
181a60be987SRyusuke Konishi  * nilfs_btnode_prepare_change_key
182a60be987SRyusuke Konishi  *  prepare to move contents of the block for old key to one of new key.
183a60be987SRyusuke Konishi  *  the old buffer will not be removed, but might be reused for new buffer.
184a60be987SRyusuke Konishi  *  it might return -ENOMEM because of memory allocation errors,
185a60be987SRyusuke Konishi  *  and might return -EIO because of disk read errors.
186a60be987SRyusuke Konishi  */
nilfs_btnode_prepare_change_key(struct address_space * btnc,struct nilfs_btnode_chkey_ctxt * ctxt)187a60be987SRyusuke Konishi int nilfs_btnode_prepare_change_key(struct address_space *btnc,
188a60be987SRyusuke Konishi 				    struct nilfs_btnode_chkey_ctxt *ctxt)
189a60be987SRyusuke Konishi {
190a60be987SRyusuke Konishi 	struct buffer_head *obh, *nbh;
191e897be17SRyusuke Konishi 	struct inode *inode = btnc->host;
192a60be987SRyusuke Konishi 	__u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
193a60be987SRyusuke Konishi 	int err;
194a60be987SRyusuke Konishi 
195a60be987SRyusuke Konishi 	if (oldkey == newkey)
196a60be987SRyusuke Konishi 		return 0;
197a60be987SRyusuke Konishi 
198a60be987SRyusuke Konishi 	obh = ctxt->bh;
199a60be987SRyusuke Konishi 	ctxt->newbh = NULL;
200a60be987SRyusuke Konishi 
20109cbfeafSKirill A. Shutemov 	if (inode->i_blkbits == PAGE_SHIFT) {
202f611ff63SMatthew Wilcox 		struct page *opage = obh->b_page;
203f611ff63SMatthew Wilcox 		lock_page(opage);
204b1f1b8ceSRyusuke Konishi retry:
2056ad4cd7fSMatthew Wilcox (Oracle) 		/* BUG_ON(oldkey != obh->b_folio->index); */
206f611ff63SMatthew Wilcox 		if (unlikely(oldkey != opage->index))
207f611ff63SMatthew Wilcox 			NILFS_PAGE_BUG(opage,
208a60be987SRyusuke Konishi 				       "invalid oldkey %lld (newkey=%lld)",
209a60be987SRyusuke Konishi 				       (unsigned long long)oldkey,
210a60be987SRyusuke Konishi 				       (unsigned long long)newkey);
211a60be987SRyusuke Konishi 
212b93b0163SMatthew Wilcox 		xa_lock_irq(&btnc->i_pages);
213f611ff63SMatthew Wilcox 		err = __xa_insert(&btnc->i_pages, newkey, opage, GFP_NOFS);
214b93b0163SMatthew Wilcox 		xa_unlock_irq(&btnc->i_pages);
215a60be987SRyusuke Konishi 		/*
216a60be987SRyusuke Konishi 		 * Note: page->index will not change to newkey until
217a60be987SRyusuke Konishi 		 * nilfs_btnode_commit_change_key() will be called.
218a60be987SRyusuke Konishi 		 * To protect the page in intermediate state, the page lock
219a60be987SRyusuke Konishi 		 * is held.
220a60be987SRyusuke Konishi 		 */
221a60be987SRyusuke Konishi 		if (!err)
222a60be987SRyusuke Konishi 			return 0;
223fd9dc93eSMatthew Wilcox 		else if (err != -EBUSY)
224a60be987SRyusuke Konishi 			goto failed_unlock;
225a60be987SRyusuke Konishi 
226a60be987SRyusuke Konishi 		err = invalidate_inode_pages2_range(btnc, newkey, newkey);
227a60be987SRyusuke Konishi 		if (!err)
228a60be987SRyusuke Konishi 			goto retry;
229a60be987SRyusuke Konishi 		/* fallback to copy mode */
230f611ff63SMatthew Wilcox 		unlock_page(opage);
231a60be987SRyusuke Konishi 	}
232a60be987SRyusuke Konishi 
23345f4910bSRyusuke Konishi 	nbh = nilfs_btnode_create_block(btnc, newkey);
234be56dfc9SRyusuke Konishi 	if (IS_ERR(nbh))
235be56dfc9SRyusuke Konishi 		return PTR_ERR(nbh);
23645f4910bSRyusuke Konishi 
237a60be987SRyusuke Konishi 	BUG_ON(nbh == obh);
238a60be987SRyusuke Konishi 	ctxt->newbh = nbh;
23945f4910bSRyusuke Konishi 	return 0;
240a60be987SRyusuke Konishi 
241a60be987SRyusuke Konishi  failed_unlock:
242a60be987SRyusuke Konishi 	unlock_page(obh->b_page);
243a60be987SRyusuke Konishi 	return err;
244a60be987SRyusuke Konishi }
245a60be987SRyusuke Konishi 
246a60be987SRyusuke Konishi /**
247a60be987SRyusuke Konishi  * nilfs_btnode_commit_change_key
248a60be987SRyusuke Konishi  *  commit the change_key operation prepared by prepare_change_key().
249a60be987SRyusuke Konishi  */
nilfs_btnode_commit_change_key(struct address_space * btnc,struct nilfs_btnode_chkey_ctxt * ctxt)250a60be987SRyusuke Konishi void nilfs_btnode_commit_change_key(struct address_space *btnc,
251a60be987SRyusuke Konishi 				    struct nilfs_btnode_chkey_ctxt *ctxt)
252a60be987SRyusuke Konishi {
253a60be987SRyusuke Konishi 	struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
254a60be987SRyusuke Konishi 	__u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
255a60be987SRyusuke Konishi 	struct page *opage;
256a60be987SRyusuke Konishi 
257a60be987SRyusuke Konishi 	if (oldkey == newkey)
258a60be987SRyusuke Konishi 		return;
259a60be987SRyusuke Konishi 
260a60be987SRyusuke Konishi 	if (nbh == NULL) {	/* blocksize == pagesize */
261a60be987SRyusuke Konishi 		opage = obh->b_page;
262a60be987SRyusuke Konishi 		if (unlikely(oldkey != opage->index))
263a60be987SRyusuke Konishi 			NILFS_PAGE_BUG(opage,
264a60be987SRyusuke Konishi 				       "invalid oldkey %lld (newkey=%lld)",
265a60be987SRyusuke Konishi 				       (unsigned long long)oldkey,
266a60be987SRyusuke Konishi 				       (unsigned long long)newkey);
2675fc7b141SRyusuke Konishi 		mark_buffer_dirty(obh);
268a60be987SRyusuke Konishi 
269b93b0163SMatthew Wilcox 		xa_lock_irq(&btnc->i_pages);
270f611ff63SMatthew Wilcox 		__xa_erase(&btnc->i_pages, oldkey);
271f611ff63SMatthew Wilcox 		__xa_set_mark(&btnc->i_pages, newkey, PAGECACHE_TAG_DIRTY);
272b93b0163SMatthew Wilcox 		xa_unlock_irq(&btnc->i_pages);
273a60be987SRyusuke Konishi 
274a60be987SRyusuke Konishi 		opage->index = obh->b_blocknr = newkey;
275a60be987SRyusuke Konishi 		unlock_page(opage);
276a60be987SRyusuke Konishi 	} else {
277a60be987SRyusuke Konishi 		nilfs_copy_buffer(nbh, obh);
2785fc7b141SRyusuke Konishi 		mark_buffer_dirty(nbh);
279a60be987SRyusuke Konishi 
280a60be987SRyusuke Konishi 		nbh->b_blocknr = newkey;
281a60be987SRyusuke Konishi 		ctxt->bh = nbh;
282a60be987SRyusuke Konishi 		nilfs_btnode_delete(obh); /* will decrement bh->b_count */
283a60be987SRyusuke Konishi 	}
284a60be987SRyusuke Konishi }
285a60be987SRyusuke Konishi 
286a60be987SRyusuke Konishi /**
287a60be987SRyusuke Konishi  * nilfs_btnode_abort_change_key
288a60be987SRyusuke Konishi  *  abort the change_key operation prepared by prepare_change_key().
289a60be987SRyusuke Konishi  */
nilfs_btnode_abort_change_key(struct address_space * btnc,struct nilfs_btnode_chkey_ctxt * ctxt)290a60be987SRyusuke Konishi void nilfs_btnode_abort_change_key(struct address_space *btnc,
291a60be987SRyusuke Konishi 				   struct nilfs_btnode_chkey_ctxt *ctxt)
292a60be987SRyusuke Konishi {
293a60be987SRyusuke Konishi 	struct buffer_head *nbh = ctxt->newbh;
294a60be987SRyusuke Konishi 	__u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
295a60be987SRyusuke Konishi 
296a60be987SRyusuke Konishi 	if (oldkey == newkey)
297a60be987SRyusuke Konishi 		return;
298a60be987SRyusuke Konishi 
299a60be987SRyusuke Konishi 	if (nbh == NULL) {	/* blocksize == pagesize */
300fe2b5114SMatthew Wilcox 		xa_erase_irq(&btnc->i_pages, newkey);
301a60be987SRyusuke Konishi 		unlock_page(ctxt->bh->b_page);
3022f012f2bSRyusuke Konishi 	} else {
3032f012f2bSRyusuke Konishi 		/*
3042f012f2bSRyusuke Konishi 		 * When canceling a buffer that a prepare operation has
3052f012f2bSRyusuke Konishi 		 * allocated to copy a node block to another location, use
3062f012f2bSRyusuke Konishi 		 * nilfs_btnode_delete() to initialize and release the buffer
3072f012f2bSRyusuke Konishi 		 * so that the buffer flags will not be in an inconsistent
3082f012f2bSRyusuke Konishi 		 * state when it is reallocated.
3092f012f2bSRyusuke Konishi 		 */
3102f012f2bSRyusuke Konishi 		nilfs_btnode_delete(nbh);
3112f012f2bSRyusuke Konishi 	}
312a60be987SRyusuke Konishi }
313