blob: 7086a2a1f7fadc81f344e36fb0c736234e507917 [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,
98 sector_t pblocknr, struct buffer_head **pbh,
99 int newblk)
100{
101 struct buffer_head *bh;
102 struct inode *inode = NILFS_BTNC_I(btnc);
103 int err;
104
105 bh = nilfs_grab_buffer(inode, btnc, blocknr, 1 << BH_NILFS_Node);
106 if (unlikely(!bh))
107 return -ENOMEM;
108
109 err = -EEXIST; /* internal code */
110 if (newblk) {
111 if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
112 buffer_dirty(bh))) {
113 brelse(bh);
114 BUG();
115 }
Ryusuke Konishi05b43582009-09-14 01:20:35 +0900116 memset(bh->b_data, 0, 1 << inode->i_blkbits);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700117 bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
118 bh->b_blocknr = blocknr;
119 set_buffer_mapped(bh);
120 set_buffer_uptodate(bh);
121 goto found;
122 }
123
124 if (buffer_uptodate(bh) || buffer_dirty(bh))
125 goto found;
126
127 if (pblocknr == 0) {
128 pblocknr = blocknr;
129 if (inode->i_ino != NILFS_DAT_INO) {
130 struct inode *dat =
131 nilfs_dat_inode(NILFS_I_NILFS(inode));
132
133 /* blocknr is a virtual block number */
134 err = nilfs_dat_translate(dat, blocknr, &pblocknr);
135 if (unlikely(err)) {
136 brelse(bh);
137 goto out_locked;
138 }
139 }
140 }
141 lock_buffer(bh);
142 if (buffer_uptodate(bh)) {
143 unlock_buffer(bh);
144 err = -EEXIST; /* internal code */
145 goto found;
146 }
147 set_buffer_mapped(bh);
148 bh->b_bdev = NILFS_I_NILFS(inode)->ns_bdev;
149 bh->b_blocknr = pblocknr; /* set block address for read */
150 bh->b_end_io = end_buffer_read_sync;
151 get_bh(bh);
152 submit_bh(READ, bh);
153 bh->b_blocknr = blocknr; /* set back to the given block address */
154 err = 0;
155found:
156 *pbh = bh;
157
158out_locked:
159 unlock_page(bh->b_page);
160 page_cache_release(bh->b_page);
161 return err;
162}
163
164int nilfs_btnode_get(struct address_space *btnc, __u64 blocknr,
165 sector_t pblocknr, struct buffer_head **pbh, int newblk)
166{
167 struct buffer_head *bh;
168 int err;
169
170 err = nilfs_btnode_submit_block(btnc, blocknr, pblocknr, pbh, newblk);
171 if (err == -EEXIST) /* internal code (cache hit) */
172 return 0;
173 if (unlikely(err))
174 return err;
175
176 bh = *pbh;
177 wait_on_buffer(bh);
178 if (!buffer_uptodate(bh)) {
179 brelse(bh);
180 return -EIO;
181 }
182 return 0;
183}
184
185/**
186 * nilfs_btnode_delete - delete B-tree node buffer
187 * @bh: buffer to be deleted
188 *
189 * nilfs_btnode_delete() invalidates the specified buffer and delete the page
190 * including the buffer if the page gets unbusy.
191 */
192void nilfs_btnode_delete(struct buffer_head *bh)
193{
194 struct address_space *mapping;
195 struct page *page = bh->b_page;
196 pgoff_t index = page_index(page);
197 int still_dirty;
198
199 page_cache_get(page);
200 lock_page(page);
201 wait_on_page_writeback(page);
202
203 nilfs_forget_buffer(bh);
204 still_dirty = PageDirty(page);
205 mapping = page->mapping;
206 unlock_page(page);
207 page_cache_release(page);
208
209 if (!still_dirty && mapping)
210 invalidate_inode_pages2_range(mapping, index, index);
211}
212
213/**
214 * nilfs_btnode_prepare_change_key
215 * prepare to move contents of the block for old key to one of new key.
216 * the old buffer will not be removed, but might be reused for new buffer.
217 * it might return -ENOMEM because of memory allocation errors,
218 * and might return -EIO because of disk read errors.
219 */
220int nilfs_btnode_prepare_change_key(struct address_space *btnc,
221 struct nilfs_btnode_chkey_ctxt *ctxt)
222{
223 struct buffer_head *obh, *nbh;
224 struct inode *inode = NILFS_BTNC_I(btnc);
225 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
226 int err;
227
228 if (oldkey == newkey)
229 return 0;
230
231 obh = ctxt->bh;
232 ctxt->newbh = NULL;
233
234 if (inode->i_blkbits == PAGE_CACHE_SHIFT) {
235 lock_page(obh->b_page);
236 /*
237 * We cannot call radix_tree_preload for the kernels older
238 * than 2.6.23, because it is not exported for modules.
239 */
Ryusuke Konishib1f1b8c2009-08-30 04:21:41 +0900240retry:
Ryusuke Konishia60be982009-04-06 19:01:25 -0700241 err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
242 if (err)
243 goto failed_unlock;
244 /* BUG_ON(oldkey != obh->b_page->index); */
245 if (unlikely(oldkey != obh->b_page->index))
246 NILFS_PAGE_BUG(obh->b_page,
247 "invalid oldkey %lld (newkey=%lld)",
248 (unsigned long long)oldkey,
249 (unsigned long long)newkey);
250
Ryusuke Konishia60be982009-04-06 19:01:25 -0700251 spin_lock_irq(&btnc->tree_lock);
252 err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page);
253 spin_unlock_irq(&btnc->tree_lock);
254 /*
255 * Note: page->index will not change to newkey until
256 * nilfs_btnode_commit_change_key() will be called.
257 * To protect the page in intermediate state, the page lock
258 * is held.
259 */
260 radix_tree_preload_end();
261 if (!err)
262 return 0;
263 else if (err != -EEXIST)
264 goto failed_unlock;
265
266 err = invalidate_inode_pages2_range(btnc, newkey, newkey);
267 if (!err)
268 goto retry;
269 /* fallback to copy mode */
270 unlock_page(obh->b_page);
271 }
272
Ryusuke Konishi45f49102009-11-13 16:25:19 +0900273 nbh = nilfs_btnode_create_block(btnc, newkey);
274 if (!nbh)
275 return -ENOMEM;
276
277 BUG_ON(nbh == obh);
278 ctxt->newbh = nbh;
279 return 0;
Ryusuke Konishia60be982009-04-06 19:01:25 -0700280
281 failed_unlock:
282 unlock_page(obh->b_page);
283 return err;
284}
285
286/**
287 * nilfs_btnode_commit_change_key
288 * commit the change_key operation prepared by prepare_change_key().
289 */
290void nilfs_btnode_commit_change_key(struct address_space *btnc,
291 struct nilfs_btnode_chkey_ctxt *ctxt)
292{
293 struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
294 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
295 struct page *opage;
296
297 if (oldkey == newkey)
298 return;
299
300 if (nbh == NULL) { /* blocksize == pagesize */
301 opage = obh->b_page;
302 if (unlikely(oldkey != opage->index))
303 NILFS_PAGE_BUG(opage,
304 "invalid oldkey %lld (newkey=%lld)",
305 (unsigned long long)oldkey,
306 (unsigned long long)newkey);
Ryusuke Konishib1e19e52009-11-03 00:25:53 +0900307 nilfs_btnode_mark_dirty(obh);
Ryusuke Konishia60be982009-04-06 19:01:25 -0700308
309 spin_lock_irq(&btnc->tree_lock);
310 radix_tree_delete(&btnc->page_tree, oldkey);
311 radix_tree_tag_set(&btnc->page_tree, newkey,
312 PAGECACHE_TAG_DIRTY);
313 spin_unlock_irq(&btnc->tree_lock);
314
315 opage->index = obh->b_blocknr = newkey;
316 unlock_page(opage);
317 } else {
318 nilfs_copy_buffer(nbh, obh);
319 nilfs_btnode_mark_dirty(nbh);
320
321 nbh->b_blocknr = newkey;
322 ctxt->bh = nbh;
323 nilfs_btnode_delete(obh); /* will decrement bh->b_count */
324 }
325}
326
327/**
328 * nilfs_btnode_abort_change_key
329 * abort the change_key operation prepared by prepare_change_key().
330 */
331void nilfs_btnode_abort_change_key(struct address_space *btnc,
332 struct nilfs_btnode_chkey_ctxt *ctxt)
333{
334 struct buffer_head *nbh = ctxt->newbh;
335 __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
336
337 if (oldkey == newkey)
338 return;
339
340 if (nbh == NULL) { /* blocksize == pagesize */
341 spin_lock_irq(&btnc->tree_lock);
342 radix_tree_delete(&btnc->page_tree, newkey);
343 spin_unlock_irq(&btnc->tree_lock);
344 unlock_page(ctxt->bh->b_page);
345 } else
346 brelse(nbh);
347}