blob: 55241effa3c0a8422476728ba2668d788b4148e0 [file] [log] [blame]
Ryusuke Konishia60be982009-04-06 19:01:25 -07001/*
2 * btnode.c - NILFS B-tree node cache
3 *
4 * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
Ryusuke Konishia60be982009-04-06 19:01:25 -070016 * This file was originally written by Seiji Kihara <kihara@osrg.net>
17 * and fully revised by Ryusuke Konishi <ryusuke@osrg.net> for
18 * stabilization and simplification.
19 *
20 */
21
22#include <linux/types.h>
23#include <linux/buffer_head.h>
24#include <linux/mm.h>
25#include <linux/backing-dev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/gfp.h>
Ryusuke Konishia60be982009-04-06 19:01:25 -070027#include "nilfs.h"
28#include "mdt.h"
29#include "dat.h"
30#include "page.h"
31#include "btnode.h"
32
Ryusuke Konishia60be982009-04-06 19:01:25 -070033void nilfs_btnode_cache_clear(struct address_space *btnc)
34{
35 invalidate_mapping_pages(btnc, 0, -1);
36 truncate_inode_pages(btnc, 0);
37}
38
Ryusuke Konishid501d732009-11-13 16:04:11 +090039struct buffer_head *
40nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
41{
42 struct inode *inode = NILFS_BTNC_I(btnc);
43 struct buffer_head *bh;
44
45 bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node);
46 if (unlikely(!bh))
47 return NULL;
48
49 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
50 buffer_dirty(bh))) {
51 brelse(bh);
52 BUG();
53 }
54 memset(bh->b_data, 0, 1 << inode->i_blkbits);
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090055 bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishid501d732009-11-13 16:04:11 +090056 bh->b_blocknr = blocknr;
57 set_buffer_mapped(bh);
58 set_buffer_uptodate(bh);
59
60 unlock_page(bh->b_page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +030061 put_page(bh->b_page);
Ryusuke Konishid501d732009-11-13 16:04:11 +090062 return bh;
63}
64
Ryusuke Konishia60be982009-04-06 19:01:25 -070065int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +090066 sector_t pblocknr, int mode,
67 struct buffer_head **pbh, sector_t *submit_ptr)
Ryusuke Konishia60be982009-04-06 19:01:25 -070068{
69 struct buffer_head *bh;
70 struct inode *inode = NILFS_BTNC_I(btnc);
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +090071 struct page *page;
Ryusuke Konishia60be982009-04-06 19:01:25 -070072 int err;
73
74 bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node);
75 if (unlikely(!bh))
76 return -ENOMEM;
77
78 err = -EEXIST; /* internal code */
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +090079 page = bh->b_page;
Ryusuke Konishia60be982009-04-06 19:01:25 -070080
81 if (buffer_uptodate(bh) || buffer_dirty(bh))
82 goto found;
83
84 if (pblocknr == 0) {
85 pblocknr = blocknr;
86 if (inode->i_ino != NILFS_DAT_INO) {
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090087 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
Ryusuke Konishia60be982009-04-06 19:01:25 -070088
89 /* blocknr is a virtual block number */
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090090 err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
91 &pblocknr);
Ryusuke Konishia60be982009-04-06 19:01:25 -070092 if (unlikely(err)) {
93 brelse(bh);
94 goto out_locked;
95 }
96 }
97 }
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +090098
99 if (mode == READA) {
100 if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
101 err = -EBUSY; /* internal code */
102 brelse(bh);
103 goto out_locked;
104 }
105 } else { /* mode == READ */
106 lock_buffer(bh);
107 }
Ryusuke Konishia60be982009-04-06 19:01:25 -0700108 if (buffer_uptodate(bh)) {
109 unlock_buffer(bh);
110 err = -EEXIST; /* internal code */
111 goto found;
112 }
113 set_buffer_mapped(bh);
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900114 bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700115 bh->b_blocknr = pblocknr; /* set block address for read */
116 bh->b_end_io = end_buffer_read_sync;
117 get_bh(bh);
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900118 submit_bh(mode, bh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700119 bh->b_blocknr = blocknr; /* set back to the given block address */
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900120 *submit_ptr = pblocknr;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700121 err = 0;
122found:
123 *pbh = bh;
124
125out_locked:
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +0900126 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300127 put_page(page);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700128 return err;
129}
130
Ryusuke Konishia60be982009-04-06 19:01:25 -0700131/**
132 * nilfs_btnode_delete - delete B-tree node buffer
133 * @bh: buffer to be deleted
134 *
135 * nilfs_btnode_delete() invalidates the specified buffer and delete the page
136 * including the buffer if the page gets unbusy.
137 */
138void nilfs_btnode_delete(struct buffer_head *bh)
139{
140 struct address_space *mapping;
141 struct page *page = bh->b_page;
142 pgoff_t index = page_index(page);
143 int still_dirty;
144
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300145 get_page(page);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700146 lock_page(page);
147 wait_on_page_writeback(page);
148
149 nilfs_forget_buffer(bh);
150 still_dirty = PageDirty(page);
151 mapping = page->mapping;
152 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300153 put_page(page);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700154
155 if (!still_dirty && mapping)
156 invalidate_inode_pages2_range(mapping, index, index);
157}
158
159/**
160 * nilfs_btnode_prepare_change_key
161 * prepare to move contents of the block for old key to one of new key.
162 * the old buffer will not be removed, but might be reused for new buffer.
163 * it might return -ENOMEM because of memory allocation errors,
164 * and might return -EIO because of disk read errors.
165 */
166int nilfs_btnode_prepare_change_key(struct address_space *btnc,
167 struct nilfs_btnode_chkey_ctxt *ctxt)
168{
169 struct buffer_head *obh, *nbh;
170 struct inode *inode = NILFS_BTNC_I(btnc);
171 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
172 int err;
173
174 if (oldkey == newkey)
175 return 0;
176
177 obh = ctxt->bh;
178 ctxt->newbh = NULL;
179
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300180 if (inode->i_blkbits == PAGE_SHIFT) {
Ryusuke Konishia60be982009-04-06 19:01:25 -0700181 lock_page(obh->b_page);
182 /*
183 * We cannot call radix_tree_preload for the kernels older
184 * than 2.6.23, because it is not exported for modules.
185 */
Ryusuke Konishib1f1b8c2009-08-30 04:21:41 +0900186retry:
Ryusuke Konishia60be982009-04-06 19:01:25 -0700187 err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
188 if (err)
189 goto failed_unlock;
190 /* BUG_ON(oldkey != obh->b_page->index); */
191 if (unlikely(oldkey != obh->b_page->index))
192 NILFS_PAGE_BUG(obh->b_page,
193 "invalid oldkey %lld (newkey=%lld)",
194 (unsigned long long)oldkey,
195 (unsigned long long)newkey);
196
Ryusuke Konishia60be982009-04-06 19:01:25 -0700197 spin_lock_irq(&btnc->tree_lock);
198 err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page);
199 spin_unlock_irq(&btnc->tree_lock);
200 /*
201 * Note: page->index will not change to newkey until
202 * nilfs_btnode_commit_change_key() will be called.
203 * To protect the page in intermediate state, the page lock
204 * is held.
205 */
206 radix_tree_preload_end();
207 if (!err)
208 return 0;
209 else if (err != -EEXIST)
210 goto failed_unlock;
211
212 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
213 if (!err)
214 goto retry;
215 /* fallback to copy mode */
216 unlock_page(obh->b_page);
217 }
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
255 spin_lock_irq(&btnc->tree_lock);
256 radix_tree_delete(&btnc->page_tree, oldkey);
257 radix_tree_tag_set(&btnc->page_tree, newkey,
258 PAGECACHE_TAG_DIRTY);
259 spin_unlock_irq(&btnc->tree_lock);
260
261 opage->index = obh->b_blocknr = newkey;
262 unlock_page(opage);
263 } else {
264 nilfs_copy_buffer(nbh, obh);
Ryusuke Konishi5fc7b142011-05-05 12:56:51 +0900265 mark_buffer_dirty(nbh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700266
267 nbh->b_blocknr = newkey;
268 ctxt->bh = nbh;
269 nilfs_btnode_delete(obh); /* will decrement bh->b_count */
270 }
271}
272
273/**
274 * nilfs_btnode_abort_change_key
275 * abort the change_key operation prepared by prepare_change_key().
276 */
277void nilfs_btnode_abort_change_key(struct address_space *btnc,
278 struct nilfs_btnode_chkey_ctxt *ctxt)
279{
280 struct buffer_head *nbh = ctxt->newbh;
281 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
282
283 if (oldkey == newkey)
284 return;
285
286 if (nbh == NULL) { /* blocksize == pagesize */
287 spin_lock_irq(&btnc->tree_lock);
288 radix_tree_delete(&btnc->page_tree, newkey);
289 spin_unlock_irq(&btnc->tree_lock);
290 unlock_page(ctxt->bh->b_page);
291 } else
292 brelse(nbh);
293}