blob: 59658f08d289115c3b02a5e2eafb54e64066e62a [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>
30#include "nilfs.h"
31#include "mdt.h"
32#include "dat.h"
33#include "page.h"
34#include "btnode.h"
35
36
37void nilfs_btnode_cache_init_once(struct address_space *btnc)
38{
Ryusuke Konishi1f28fcd2009-09-28 01:46:11 +090039 memset(btnc, 0, sizeof(*btnc));
Ryusuke Konishia60be982009-04-06 19:01:25 -070040 INIT_RADIX_TREE(&btnc->page_tree, GFP_ATOMIC);
41 spin_lock_init(&btnc->tree_lock);
42 INIT_LIST_HEAD(&btnc->private_list);
43 spin_lock_init(&btnc->private_lock);
44
45 spin_lock_init(&btnc->i_mmap_lock);
46 INIT_RAW_PRIO_TREE_ROOT(&btnc->i_mmap);
47 INIT_LIST_HEAD(&btnc->i_mmap_nonlinear);
48}
49
Alexey Dobriyan7f094102009-09-21 17:01:10 -070050static const struct address_space_operations def_btnode_aops = {
Ryusuke Konishifa032742009-05-27 22:44:34 +090051 .sync_page = block_sync_page,
52};
Ryusuke Konishia60be982009-04-06 19:01:25 -070053
Ryusuke Konishia53b4752009-05-27 22:11:46 +090054void nilfs_btnode_cache_init(struct address_space *btnc,
55 struct backing_dev_info *bdi)
Ryusuke Konishia60be982009-04-06 19:01:25 -070056{
57 btnc->host = NULL; /* can safely set to host inode ? */
58 btnc->flags = 0;
59 mapping_set_gfp_mask(btnc, GFP_NOFS);
60 btnc->assoc_mapping = NULL;
Ryusuke Konishia53b4752009-05-27 22:11:46 +090061 btnc->backing_dev_info = bdi;
Ryusuke Konishia60be982009-04-06 19:01:25 -070062 btnc->a_ops = &def_btnode_aops;
63}
64
65void nilfs_btnode_cache_clear(struct address_space *btnc)
66{
67 invalidate_mapping_pages(btnc, 0, -1);
68 truncate_inode_pages(btnc, 0);
69}
70
Ryusuke Konishid501d732009-11-13 16:04:11 +090071struct buffer_head *
72nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
73{
74 struct inode *inode = NILFS_BTNC_I(btnc);
75 struct buffer_head *bh;
76
77 bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node);
78 if (unlikely(!bh))
79 return NULL;
80
81 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
82 buffer_dirty(bh))) {
83 brelse(bh);
84 BUG();
85 }
86 memset(bh->b_data, 0, 1 << inode->i_blkbits);
87 bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
88 bh->b_blocknr = blocknr;
89 set_buffer_mapped(bh);
90 set_buffer_uptodate(bh);
91
92 unlock_page(bh->b_page);
93 page_cache_release(bh->b_page);
94 return bh;
95}
96
Ryusuke Konishia60be982009-04-06 19:01:25 -070097int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
Ryusuke Konishi75f65ed2009-11-13 16:30:41 +090098 sector_t pblocknr, struct buffer_head **pbh)
Ryusuke Konishia60be982009-04-06 19:01:25 -070099{
100 struct buffer_head *bh;
101 struct inode *inode = NILFS_BTNC_I(btnc);
102 int err;
103
104 bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node);
105 if (unlikely(!bh))
106 return -ENOMEM;
107
108 err = -EEXIST; /* internal code */
Ryusuke Konishia60be982009-04-06 19:01:25 -0700109
110 if (buffer_uptodate(bh) || buffer_dirty(bh))
111 goto found;
112
113 if (pblocknr == 0) {
114 pblocknr = blocknr;
115 if (inode->i_ino != NILFS_DAT_INO) {
116 struct inode *dat =
117 nilfs_dat_inode(NILFS_I_NILFS(inode));
118
119 /* blocknr is a virtual block number */
120 err = nilfs_dat_translate(dat, blocknr, &pblocknr);
121 if (unlikely(err)) {
122 brelse(bh);
123 goto out_locked;
124 }
125 }
126 }
127 lock_buffer(bh);
128 if (buffer_uptodate(bh)) {
129 unlock_buffer(bh);
130 err = -EEXIST; /* internal code */
131 goto found;
132 }
133 set_buffer_mapped(bh);
134 bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
135 bh->b_blocknr = pblocknr; /* set block address for read */
136 bh->b_end_io = end_buffer_read_sync;
137 get_bh(bh);
138 submit_bh(READ, bh);
139 bh->b_blocknr = blocknr; /* set back to the given block address */
140 err = 0;
141found:
142 *pbh = bh;
143
144out_locked:
145 unlock_page(bh->b_page);
146 page_cache_release(bh->b_page);
147 return err;
148}
149
150int nilfs_btnode_get(struct address_space *btnc, __u64 blocknr,
Ryusuke Konishi75f65ed2009-11-13 16:30:41 +0900151 sector_t pblocknr, struct buffer_head **pbh)
Ryusuke Konishia60be982009-04-06 19:01:25 -0700152{
153 struct buffer_head *bh;
154 int err;
155
Ryusuke Konishi75f65ed2009-11-13 16:30:41 +0900156 err = nilfs_btnode_submit_block(btnc, blocknr, pblocknr, pbh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700157 if (err == -EEXIST) /* internal code (cache hit) */
158 return 0;
159 if (unlikely(err))
160 return err;
161
162 bh = *pbh;
163 wait_on_buffer(bh);
164 if (!buffer_uptodate(bh)) {
165 brelse(bh);
166 return -EIO;
167 }
168 return 0;
169}
170
171/**
172 * nilfs_btnode_delete - delete B-tree node buffer
173 * @bh: buffer to be deleted
174 *
175 * nilfs_btnode_delete() invalidates the specified buffer and delete the page
176 * including the buffer if the page gets unbusy.
177 */
178void nilfs_btnode_delete(struct buffer_head *bh)
179{
180 struct address_space *mapping;
181 struct page *page = bh->b_page;
182 pgoff_t index = page_index(page);
183 int still_dirty;
184
185 page_cache_get(page);
186 lock_page(page);
187 wait_on_page_writeback(page);
188
189 nilfs_forget_buffer(bh);
190 still_dirty = PageDirty(page);
191 mapping = page->mapping;
192 unlock_page(page);
193 page_cache_release(page);
194
195 if (!still_dirty && mapping)
196 invalidate_inode_pages2_range(mapping, index, index);
197}
198
199/**
200 * nilfs_btnode_prepare_change_key
201 * prepare to move contents of the block for old key to one of new key.
202 * the old buffer will not be removed, but might be reused for new buffer.
203 * it might return -ENOMEM because of memory allocation errors,
204 * and might return -EIO because of disk read errors.
205 */
206int nilfs_btnode_prepare_change_key(struct address_space *btnc,
207 struct nilfs_btnode_chkey_ctxt *ctxt)
208{
209 struct buffer_head *obh, *nbh;
210 struct inode *inode = NILFS_BTNC_I(btnc);
211 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
212 int err;
213
214 if (oldkey == newkey)
215 return 0;
216
217 obh = ctxt->bh;
218 ctxt->newbh = NULL;
219
220 if (inode->i_blkbits == PAGE_CACHE_SHIFT) {
221 lock_page(obh->b_page);
222 /*
223 * We cannot call radix_tree_preload for the kernels older
224 * than 2.6.23, because it is not exported for modules.
225 */
Ryusuke Konishib1f1b8c2009-08-30 04:21:41 +0900226retry:
Ryusuke Konishia60be982009-04-06 19:01:25 -0700227 err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
228 if (err)
229 goto failed_unlock;
230 /* BUG_ON(oldkey != obh->b_page->index); */
231 if (unlikely(oldkey != obh->b_page->index))
232 NILFS_PAGE_BUG(obh->b_page,
233 "invalid oldkey %lld (newkey=%lld)",
234 (unsigned long long)oldkey,
235 (unsigned long long)newkey);
236
Ryusuke Konishia60be982009-04-06 19:01:25 -0700237 spin_lock_irq(&btnc->tree_lock);
238 err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page);
239 spin_unlock_irq(&btnc->tree_lock);
240 /*
241 * Note: page->index will not change to newkey until
242 * nilfs_btnode_commit_change_key() will be called.
243 * To protect the page in intermediate state, the page lock
244 * is held.
245 */
246 radix_tree_preload_end();
247 if (!err)
248 return 0;
249 else if (err != -EEXIST)
250 goto failed_unlock;
251
252 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
253 if (!err)
254 goto retry;
255 /* fallback to copy mode */
256 unlock_page(obh->b_page);
257 }
258
Ryusuke Konishi45f49102009-11-13 16:25:19 +0900259 nbh = nilfs_btnode_create_block(btnc, newkey);
260 if (!nbh)
261 return -ENOMEM;
262
263 BUG_ON(nbh == obh);
264 ctxt->newbh = nbh;
265 return 0;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700266
267 failed_unlock:
268 unlock_page(obh->b_page);
269 return err;
270}
271
272/**
273 * nilfs_btnode_commit_change_key
274 * commit the change_key operation prepared by prepare_change_key().
275 */
276void nilfs_btnode_commit_change_key(struct address_space *btnc,
277 struct nilfs_btnode_chkey_ctxt *ctxt)
278{
279 struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
280 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
281 struct page *opage;
282
283 if (oldkey == newkey)
284 return;
285
286 if (nbh == NULL) { /* blocksize == pagesize */
287 opage = obh->b_page;
288 if (unlikely(oldkey != opage->index))
289 NILFS_PAGE_BUG(opage,
290 "invalid oldkey %lld (newkey=%lld)",
291 (unsigned long long)oldkey,
292 (unsigned long long)newkey);
Ryusuke Konishib1e19e52009-11-03 00:25:53 +0900293 nilfs_btnode_mark_dirty(obh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700294
295 spin_lock_irq(&btnc->tree_lock);
296 radix_tree_delete(&btnc->page_tree, oldkey);
297 radix_tree_tag_set(&btnc->page_tree, newkey,
298 PAGECACHE_TAG_DIRTY);
299 spin_unlock_irq(&btnc->tree_lock);
300
301 opage->index = obh->b_blocknr = newkey;
302 unlock_page(opage);
303 } else {
304 nilfs_copy_buffer(nbh, obh);
305 nilfs_btnode_mark_dirty(nbh);
306
307 nbh->b_blocknr = newkey;
308 ctxt->bh = nbh;
309 nilfs_btnode_delete(obh); /* will decrement bh->b_count */
310 }
311}
312
313/**
314 * nilfs_btnode_abort_change_key
315 * abort the change_key operation prepared by prepare_change_key().
316 */
317void nilfs_btnode_abort_change_key(struct address_space *btnc,
318 struct nilfs_btnode_chkey_ctxt *ctxt)
319{
320 struct buffer_head *nbh = ctxt->newbh;
321 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
322
323 if (oldkey == newkey)
324 return;
325
326 if (nbh == NULL) { /* blocksize == pagesize */
327 spin_lock_irq(&btnc->tree_lock);
328 radix_tree_delete(&btnc->page_tree, newkey);
329 spin_unlock_irq(&btnc->tree_lock);
330 unlock_page(ctxt->bh->b_page);
331 } else
332 brelse(nbh);
333}