blob: 4817560424236ec6c5456a573e0d131834daacc9 [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 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 * This file was originally written by Seiji Kihara <kihara@osrg.net>
21 * and fully revised by Ryusuke Konishi <ryusuke@osrg.net> for
22 * stabilization and simplification.
23 *
24 */
25
26#include <linux/types.h>
27#include <linux/buffer_head.h>
28#include <linux/mm.h>
29#include <linux/backing-dev.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/gfp.h>
Ryusuke Konishia60be982009-04-06 19:01:25 -070031#include "nilfs.h"
32#include "mdt.h"
33#include "dat.h"
34#include "page.h"
35#include "btnode.h"
36
Ryusuke Konishia53b4752009-05-27 22:11:46 +090037void nilfs_btnode_cache_init(struct address_space *btnc,
38 struct backing_dev_info *bdi)
Ryusuke Konishia60be982009-04-06 19:01:25 -070039{
Jens Axboe7eaceac2011-03-10 08:52:07 +010040 nilfs_mapping_init(btnc, bdi);
Ryusuke Konishia60be982009-04-06 19:01:25 -070041}
42
43void nilfs_btnode_cache_clear(struct address_space *btnc)
44{
45 invalidate_mapping_pages(btnc, 0, -1);
46 truncate_inode_pages(btnc, 0);
47}
48
Ryusuke Konishid501d732009-11-13 16:04:11 +090049struct buffer_head *
50nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
51{
52 struct inode *inode = NILFS_BTNC_I(btnc);
53 struct buffer_head *bh;
54
55 bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node);
56 if (unlikely(!bh))
57 return NULL;
58
59 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
60 buffer_dirty(bh))) {
61 brelse(bh);
62 BUG();
63 }
64 memset(bh->b_data, 0, 1 << inode->i_blkbits);
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090065 bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishid501d732009-11-13 16:04:11 +090066 bh->b_blocknr = blocknr;
67 set_buffer_mapped(bh);
68 set_buffer_uptodate(bh);
69
70 unlock_page(bh->b_page);
71 page_cache_release(bh->b_page);
72 return bh;
73}
74
Ryusuke Konishia60be982009-04-06 19:01:25 -070075int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +090076 sector_t pblocknr, int mode,
77 struct buffer_head **pbh, sector_t *submit_ptr)
Ryusuke Konishia60be982009-04-06 19:01:25 -070078{
79 struct buffer_head *bh;
80 struct inode *inode = NILFS_BTNC_I(btnc);
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +090081 struct page *page;
Ryusuke Konishia60be982009-04-06 19:01:25 -070082 int err;
83
84 bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node);
85 if (unlikely(!bh))
86 return -ENOMEM;
87
88 err = -EEXIST; /* internal code */
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +090089 page = bh->b_page;
Ryusuke Konishia60be982009-04-06 19:01:25 -070090
91 if (buffer_uptodate(bh) || buffer_dirty(bh))
92 goto found;
93
94 if (pblocknr == 0) {
95 pblocknr = blocknr;
96 if (inode->i_ino != NILFS_DAT_INO) {
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +090097 struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
Ryusuke Konishia60be982009-04-06 19:01:25 -070098
99 /* blocknr is a virtual block number */
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900100 err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
101 &pblocknr);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700102 if (unlikely(err)) {
103 brelse(bh);
104 goto out_locked;
105 }
106 }
107 }
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900108
109 if (mode == READA) {
110 if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
111 err = -EBUSY; /* internal code */
112 brelse(bh);
113 goto out_locked;
114 }
115 } else { /* mode == READ */
116 lock_buffer(bh);
117 }
Ryusuke Konishia60be982009-04-06 19:01:25 -0700118 if (buffer_uptodate(bh)) {
119 unlock_buffer(bh);
120 err = -EEXIST; /* internal code */
121 goto found;
122 }
123 set_buffer_mapped(bh);
Ryusuke Konishi0ef28f92011-05-05 12:56:51 +0900124 bh->b_bdev = inode->i_sb->s_bdev;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700125 bh->b_blocknr = pblocknr; /* set block address for read */
126 bh->b_end_io = end_buffer_read_sync;
127 get_bh(bh);
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900128 submit_bh(mode, bh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700129 bh->b_blocknr = blocknr; /* set back to the given block address */
Ryusuke Konishi26dfdd82010-07-18 10:42:23 +0900130 *submit_ptr = pblocknr;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700131 err = 0;
132found:
133 *pbh = bh;
134
135out_locked:
Ryusuke Konishif8e6cc02010-07-15 11:39:10 +0900136 unlock_page(page);
137 page_cache_release(page);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700138 return err;
139}
140
Ryusuke Konishia60be982009-04-06 19:01:25 -0700141/**
142 * nilfs_btnode_delete - delete B-tree node buffer
143 * @bh: buffer to be deleted
144 *
145 * nilfs_btnode_delete() invalidates the specified buffer and delete the page
146 * including the buffer if the page gets unbusy.
147 */
148void nilfs_btnode_delete(struct buffer_head *bh)
149{
150 struct address_space *mapping;
151 struct page *page = bh->b_page;
152 pgoff_t index = page_index(page);
153 int still_dirty;
154
155 page_cache_get(page);
156 lock_page(page);
157 wait_on_page_writeback(page);
158
159 nilfs_forget_buffer(bh);
160 still_dirty = PageDirty(page);
161 mapping = page->mapping;
162 unlock_page(page);
163 page_cache_release(page);
164
165 if (!still_dirty && mapping)
166 invalidate_inode_pages2_range(mapping, index, index);
167}
168
169/**
170 * nilfs_btnode_prepare_change_key
171 * prepare to move contents of the block for old key to one of new key.
172 * the old buffer will not be removed, but might be reused for new buffer.
173 * it might return -ENOMEM because of memory allocation errors,
174 * and might return -EIO because of disk read errors.
175 */
176int nilfs_btnode_prepare_change_key(struct address_space *btnc,
177 struct nilfs_btnode_chkey_ctxt *ctxt)
178{
179 struct buffer_head *obh, *nbh;
180 struct inode *inode = NILFS_BTNC_I(btnc);
181 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
182 int err;
183
184 if (oldkey == newkey)
185 return 0;
186
187 obh = ctxt->bh;
188 ctxt->newbh = NULL;
189
190 if (inode->i_blkbits == PAGE_CACHE_SHIFT) {
191 lock_page(obh->b_page);
192 /*
193 * We cannot call radix_tree_preload for the kernels older
194 * than 2.6.23, because it is not exported for modules.
195 */
Ryusuke Konishib1f1b8c2009-08-30 04:21:41 +0900196retry:
Ryusuke Konishia60be982009-04-06 19:01:25 -0700197 err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
198 if (err)
199 goto failed_unlock;
200 /* BUG_ON(oldkey != obh->b_page->index); */
201 if (unlikely(oldkey != obh->b_page->index))
202 NILFS_PAGE_BUG(obh->b_page,
203 "invalid oldkey %lld (newkey=%lld)",
204 (unsigned long long)oldkey,
205 (unsigned long long)newkey);
206
Ryusuke Konishia60be982009-04-06 19:01:25 -0700207 spin_lock_irq(&btnc->tree_lock);
208 err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page);
209 spin_unlock_irq(&btnc->tree_lock);
210 /*
211 * Note: page->index will not change to newkey until
212 * nilfs_btnode_commit_change_key() will be called.
213 * To protect the page in intermediate state, the page lock
214 * is held.
215 */
216 radix_tree_preload_end();
217 if (!err)
218 return 0;
219 else if (err != -EEXIST)
220 goto failed_unlock;
221
222 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
223 if (!err)
224 goto retry;
225 /* fallback to copy mode */
226 unlock_page(obh->b_page);
227 }
228
Ryusuke Konishi45f49102009-11-13 16:25:19 +0900229 nbh = nilfs_btnode_create_block(btnc, newkey);
230 if (!nbh)
231 return -ENOMEM;
232
233 BUG_ON(nbh == obh);
234 ctxt->newbh = nbh;
235 return 0;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700236
237 failed_unlock:
238 unlock_page(obh->b_page);
239 return err;
240}
241
242/**
243 * nilfs_btnode_commit_change_key
244 * commit the change_key operation prepared by prepare_change_key().
245 */
246void nilfs_btnode_commit_change_key(struct address_space *btnc,
247 struct nilfs_btnode_chkey_ctxt *ctxt)
248{
249 struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
250 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
251 struct page *opage;
252
253 if (oldkey == newkey)
254 return;
255
256 if (nbh == NULL) { /* blocksize == pagesize */
257 opage = obh->b_page;
258 if (unlikely(oldkey != opage->index))
259 NILFS_PAGE_BUG(opage,
260 "invalid oldkey %lld (newkey=%lld)",
261 (unsigned long long)oldkey,
262 (unsigned long long)newkey);
Ryusuke Konishib1e19e52009-11-03 00:25:53 +0900263 nilfs_btnode_mark_dirty(obh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700264
265 spin_lock_irq(&btnc->tree_lock);
266 radix_tree_delete(&btnc->page_tree, oldkey);
267 radix_tree_tag_set(&btnc->page_tree, newkey,
268 PAGECACHE_TAG_DIRTY);
269 spin_unlock_irq(&btnc->tree_lock);
270
271 opage->index = obh->b_blocknr = newkey;
272 unlock_page(opage);
273 } else {
274 nilfs_copy_buffer(nbh, obh);
275 nilfs_btnode_mark_dirty(nbh);
276
277 nbh->b_blocknr = newkey;
278 ctxt->bh = nbh;
279 nilfs_btnode_delete(obh); /* will decrement bh->b_count */
280 }
281}
282
283/**
284 * nilfs_btnode_abort_change_key
285 * abort the change_key operation prepared by prepare_change_key().
286 */
287void nilfs_btnode_abort_change_key(struct address_space *btnc,
288 struct nilfs_btnode_chkey_ctxt *ctxt)
289{
290 struct buffer_head *nbh = ctxt->newbh;
291 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
292
293 if (oldkey == newkey)
294 return;
295
296 if (nbh == NULL) { /* blocksize == pagesize */
297 spin_lock_irq(&btnc->tree_lock);
298 radix_tree_delete(&btnc->page_tree, newkey);
299 spin_unlock_irq(&btnc->tree_lock);
300 unlock_page(ctxt->bh->b_page);
301 } else
302 brelse(nbh);
303}