blob: ca611ac09f7c18314dfd802d359ebffa73748412 [file] [log] [blame]
Ryusuke Konishiae980432018-09-04 15:46:30 -07001// SPDX-License-Identifier: GPL-2.0+
Ryusuke Konishia60be982009-04-06 19:01:25 -07002/*
Ryusuke Konishi94ee1d92021-11-08 18:35:01 -08003 * NILFS B-tree node cache
Ryusuke Konishia60be982009-04-06 19:01:25 -07004 *
5 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
6 *
Ryusuke Konishi4b420ab2016-05-23 16:23:09 -07007 * Originally written by Seiji Kihara.
8 * Fully revised by Ryusuke Konishi for stabilization and simplification.
Ryusuke Konishia60be982009-04-06 19:01:25 -07009 *
10 */
11
12#include <linux/types.h>
13#include <linux/buffer_head.h>
14#include <linux/mm.h>
15#include <linux/backing-dev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/gfp.h>
Ryusuke Konishia60be982009-04-06 19:01:25 -070017#include "nilfs.h"
18#include "mdt.h"
19#include "dat.h"
20#include "page.h"
21#include "btnode.h"
22
Ryusuke Konishie897be12022-04-01 11:28:18 -070023
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 */
30void 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
Ryusuke Konishia60be982009-04-06 19:01:25 -070040void nilfs_btnode_cache_clear(struct address_space *btnc)
41{
42 invalidate_mapping_pages(btnc, 0, -1);
43 truncate_inode_pages(btnc, 0);
44}
45
Ryusuke Konishid501d732009-11-13 16:04:11 +090046struct buffer_head *
47nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
48{
Ryusuke Konishie897be12022-04-01 11:28:18 -070049 struct inode *inode = btnc->host;
Ryusuke Konishid501d732009-11-13 16:04:11 +090050 struct buffer_head *bh;
51
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -070052 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
Ryusuke Konishid501d732009-11-13 16:04:11 +090053 if (unlikely(!bh))
54 return NULL;
55
56 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
57 buffer_dirty(bh))) {
58 brelse(bh);
59 BUG();
60 }
Fabian Frederick93407472017-02-27 14:28:32 -080061 memset(bh->b_data, 0, i_blocksize(inode));
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090062 bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishid501d732009-11-13 16:04:11 +090063 bh->b_blocknr = blocknr;
64 set_buffer_mapped(bh);
65 set_buffer_uptodate(bh);
66
67 unlock_page(bh->b_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030068 put_page(bh->b_page);
Ryusuke Konishid501d732009-11-13 16:04:11 +090069 return bh;
70}
71
Ryusuke Konishia60be982009-04-06 19:01:25 -070072int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
Mike Christie2a222ca2016-06-05 14:31:43 -050073 sector_t pblocknr, int mode, int mode_flags,
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +090074 struct buffer_head **pbh, sector_t *submit_ptr)
Ryusuke Konishia60be982009-04-06 19:01:25 -070075{
76 struct buffer_head *bh;
Ryusuke Konishie897be12022-04-01 11:28:18 -070077 struct inode *inode = btnc->host;
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +090078 struct page *page;
Ryusuke Konishia60be982009-04-06 19:01:25 -070079 int err;
80
Ryusuke Konishi4ce5c342016-08-02 14:05:28 -070081 bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
Ryusuke Konishia60be982009-04-06 19:01:25 -070082 if (unlikely(!bh))
83 return -ENOMEM;
84
85 err = -EEXIST; /* internal code */
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +090086 page = bh->b_page;
Ryusuke Konishia60be982009-04-06 19:01:25 -070087
88 if (buffer_uptodate(bh) || buffer_dirty(bh))
89 goto found;
90
91 if (pblocknr == 0) {
92 pblocknr = blocknr;
93 if (inode->i_ino != NILFS_DAT_INO) {
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090094 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
Ryusuke Konishia60be982009-04-06 19:01:25 -070095
96 /* blocknr is a virtual block number */
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090097 err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
98 &pblocknr);
Ryusuke Konishia60be982009-04-06 19:01:25 -070099 if (unlikely(err)) {
100 brelse(bh);
101 goto out_locked;
102 }
103 }
104 }
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900105
Mike Christie2a222ca2016-06-05 14:31:43 -0500106 if (mode_flags & REQ_RAHEAD) {
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900107 if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
108 err = -EBUSY; /* internal code */
109 brelse(bh);
110 goto out_locked;
111 }
112 } else { /* mode == READ */
113 lock_buffer(bh);
114 }
Ryusuke Konishia60be982009-04-06 19:01:25 -0700115 if (buffer_uptodate(bh)) {
116 unlock_buffer(bh);
117 err = -EEXIST; /* internal code */
118 goto found;
119 }
120 set_buffer_mapped(bh);
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900121 bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700122 bh->b_blocknr = pblocknr; /* set block address for read */
123 bh->b_end_io = end_buffer_read_sync;
124 get_bh(bh);
Mike Christie2a222ca2016-06-05 14:31:43 -0500125 submit_bh(mode, mode_flags, bh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700126 bh->b_blocknr = blocknr; /* set back to the given block address */
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900127 *submit_ptr = pblocknr;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700128 err = 0;
129found:
130 *pbh = bh;
131
132out_locked:
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +0900133 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300134 put_page(page);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700135 return err;
136}
137
Ryusuke Konishia60be982009-04-06 19:01:25 -0700138/**
139 * nilfs_btnode_delete - delete B-tree node buffer
140 * @bh: buffer to be deleted
141 *
142 * nilfs_btnode_delete() invalidates the specified buffer and delete the page
143 * including the buffer if the page gets unbusy.
144 */
145void nilfs_btnode_delete(struct buffer_head *bh)
146{
147 struct address_space *mapping;
148 struct page *page = bh->b_page;
149 pgoff_t index = page_index(page);
150 int still_dirty;
151
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300152 get_page(page);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700153 lock_page(page);
154 wait_on_page_writeback(page);
155
156 nilfs_forget_buffer(bh);
157 still_dirty = PageDirty(page);
158 mapping = page->mapping;
159 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300160 put_page(page);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700161
162 if (!still_dirty && mapping)
163 invalidate_inode_pages2_range(mapping, index, index);
164}
165
166/**
167 * nilfs_btnode_prepare_change_key
168 * prepare to move contents of the block for old key to one of new key.
169 * the old buffer will not be removed, but might be reused for new buffer.
170 * it might return -ENOMEM because of memory allocation errors,
171 * and might return -EIO because of disk read errors.
172 */
173int nilfs_btnode_prepare_change_key(struct address_space *btnc,
174 struct nilfs_btnode_chkey_ctxt *ctxt)
175{
176 struct buffer_head *obh, *nbh;
Ryusuke Konishie897be12022-04-01 11:28:18 -0700177 struct inode *inode = btnc->host;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700178 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
179 int err;
180
181 if (oldkey == newkey)
182 return 0;
183
184 obh = ctxt->bh;
185 ctxt->newbh = NULL;
186
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300187 if (inode->i_blkbits == PAGE_SHIFT) {
Matthew Wilcoxf611ff62017-12-04 19:33:30 -0500188 struct page *opage = obh->b_page;
189 lock_page(opage);
Ryusuke Konishib1f1b8c2009-08-30 04:21:41 +0900190retry:
Ryusuke Konishia60be982009-04-06 19:01:25 -0700191 /* BUG_ON(oldkey != obh->b_page->index); */
Matthew Wilcoxf611ff62017-12-04 19:33:30 -0500192 if (unlikely(oldkey != opage->index))
193 NILFS_PAGE_BUG(opage,
Ryusuke Konishia60be982009-04-06 19:01:25 -0700194 "invalid oldkey %lld (newkey=%lld)",
195 (unsigned long long)oldkey,
196 (unsigned long long)newkey);
197
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700198 xa_lock_irq(&btnc->i_pages);
Matthew Wilcoxf611ff62017-12-04 19:33:30 -0500199 err = __xa_insert(&btnc->i_pages, newkey, opage, GFP_NOFS);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700200 xa_unlock_irq(&btnc->i_pages);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700201 /*
202 * Note: page->index will not change to newkey until
203 * nilfs_btnode_commit_change_key() will be called.
204 * To protect the page in intermediate state, the page lock
205 * is held.
206 */
Ryusuke Konishia60be982009-04-06 19:01:25 -0700207 if (!err)
208 return 0;
Matthew Wilcoxfd9dc932019-02-06 13:07:11 -0500209 else if (err != -EBUSY)
Ryusuke Konishia60be982009-04-06 19:01:25 -0700210 goto failed_unlock;
211
212 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
213 if (!err)
214 goto retry;
215 /* fallback to copy mode */
Matthew Wilcoxf611ff62017-12-04 19:33:30 -0500216 unlock_page(opage);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700217 }
218
Ryusuke Konishi45f49102009-11-13 16:25:19 +0900219 nbh = nilfs_btnode_create_block(btnc, newkey);
220 if (!nbh)
221 return -ENOMEM;
222
223 BUG_ON(nbh == obh);
224 ctxt->newbh = nbh;
225 return 0;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700226
227 failed_unlock:
228 unlock_page(obh->b_page);
229 return err;
230}
231
232/**
233 * nilfs_btnode_commit_change_key
234 * commit the change_key operation prepared by prepare_change_key().
235 */
236void nilfs_btnode_commit_change_key(struct address_space *btnc,
237 struct nilfs_btnode_chkey_ctxt *ctxt)
238{
239 struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
240 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
241 struct page *opage;
242
243 if (oldkey == newkey)
244 return;
245
246 if (nbh == NULL) { /* blocksize == pagesize */
247 opage = obh->b_page;
248 if (unlikely(oldkey != opage->index))
249 NILFS_PAGE_BUG(opage,
250 "invalid oldkey %lld (newkey=%lld)",
251 (unsigned long long)oldkey,
252 (unsigned long long)newkey);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900253 mark_buffer_dirty(obh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700254
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700255 xa_lock_irq(&btnc->i_pages);
Matthew Wilcoxf611ff62017-12-04 19:33:30 -0500256 __xa_erase(&btnc->i_pages, oldkey);
257 __xa_set_mark(&btnc->i_pages, newkey, PAGECACHE_TAG_DIRTY);
Matthew Wilcoxb93b0162018-04-10 16:36:56 -0700258 xa_unlock_irq(&btnc->i_pages);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700259
260 opage->index = obh->b_blocknr = newkey;
261 unlock_page(opage);
262 } else {
263 nilfs_copy_buffer(nbh, obh);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900264 mark_buffer_dirty(nbh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700265
266 nbh->b_blocknr = newkey;
267 ctxt->bh = nbh;
268 nilfs_btnode_delete(obh); /* will decrement bh->b_count */
269 }
270}
271
272/**
273 * nilfs_btnode_abort_change_key
274 * abort the change_key operation prepared by prepare_change_key().
275 */
276void nilfs_btnode_abort_change_key(struct address_space *btnc,
277 struct nilfs_btnode_chkey_ctxt *ctxt)
278{
279 struct buffer_head *nbh = ctxt->newbh;
280 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
281
282 if (oldkey == newkey)
283 return;
284
285 if (nbh == NULL) { /* blocksize == pagesize */
Matthew Wilcoxfe2b5112017-12-04 19:33:30 -0500286 xa_erase_irq(&btnc->i_pages, newkey);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700287 unlock_page(ctxt->bh->b_page);
288 } else
289 brelse(nbh);
290}