Commit | Line | Data |
---|---|---|
17c76b01 KS |
1 | /* |
2 | * btree.c - NILFS B-tree. | |
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 | * Written by Koji Sato <koji@osrg.net>. | |
21 | */ | |
22 | ||
23 | #include <linux/slab.h> | |
24 | #include <linux/string.h> | |
25 | #include <linux/errno.h> | |
26 | #include <linux/pagevec.h> | |
27 | #include "nilfs.h" | |
28 | #include "page.h" | |
29 | #include "btnode.h" | |
30 | #include "btree.h" | |
31 | #include "alloc.h" | |
c3a7abf0 | 32 | #include "dat.h" |
17c76b01 | 33 | |
b6b14e98 RK |
34 | static void __nilfs_btree_init(struct nilfs_bmap *bmap); |
35 | ||
f905440f | 36 | static struct nilfs_btree_path *nilfs_btree_alloc_path(void) |
17c76b01 | 37 | { |
f905440f LH |
38 | struct nilfs_btree_path *path; |
39 | int level = NILFS_BTREE_LEVEL_DATA; | |
17c76b01 | 40 | |
f905440f LH |
41 | path = kmem_cache_alloc(nilfs_btree_path_cache, GFP_NOFS); |
42 | if (path == NULL) | |
43 | goto out; | |
17c76b01 | 44 | |
f905440f | 45 | for (; level < NILFS_BTREE_LEVEL_MAX; level++) { |
17c76b01 KS |
46 | path[level].bp_bh = NULL; |
47 | path[level].bp_sib_bh = NULL; | |
48 | path[level].bp_index = 0; | |
49 | path[level].bp_oldreq.bpr_ptr = NILFS_BMAP_INVALID_PTR; | |
50 | path[level].bp_newreq.bpr_ptr = NILFS_BMAP_INVALID_PTR; | |
51 | path[level].bp_op = NULL; | |
52 | } | |
f905440f LH |
53 | |
54 | out: | |
55 | return path; | |
56 | } | |
57 | ||
73bb4886 | 58 | static void nilfs_btree_free_path(struct nilfs_btree_path *path) |
f905440f | 59 | { |
73bb4886 | 60 | int level = NILFS_BTREE_LEVEL_DATA; |
17c76b01 | 61 | |
73bb4886 | 62 | for (; level < NILFS_BTREE_LEVEL_MAX; level++) |
3218929d | 63 | brelse(path[level].bp_bh); |
73bb4886 LH |
64 | |
65 | kmem_cache_free(nilfs_btree_path_cache, path); | |
17c76b01 KS |
66 | } |
67 | ||
17c76b01 KS |
68 | /* |
69 | * B-tree node operations | |
70 | */ | |
e7c274f8 | 71 | static int nilfs_btree_get_new_block(const struct nilfs_bmap *btree, |
f198dbb9 RK |
72 | __u64 ptr, struct buffer_head **bhp) |
73 | { | |
e7c274f8 | 74 | struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache; |
45f4910b | 75 | struct buffer_head *bh; |
f198dbb9 | 76 | |
45f4910b RK |
77 | bh = nilfs_btnode_create_block(btnc, ptr); |
78 | if (!bh) | |
79 | return -ENOMEM; | |
80 | ||
81 | set_buffer_nilfs_volatile(bh); | |
82 | *bhp = bh; | |
83 | return 0; | |
f198dbb9 | 84 | } |
17c76b01 | 85 | |
7c397a81 | 86 | static int nilfs_btree_node_get_flags(const struct nilfs_btree_node *node) |
17c76b01 KS |
87 | { |
88 | return node->bn_flags; | |
89 | } | |
90 | ||
7c397a81 | 91 | static void |
6d28f7ea | 92 | nilfs_btree_node_set_flags(struct nilfs_btree_node *node, int flags) |
17c76b01 KS |
93 | { |
94 | node->bn_flags = flags; | |
95 | } | |
96 | ||
7c397a81 | 97 | static int nilfs_btree_node_root(const struct nilfs_btree_node *node) |
17c76b01 | 98 | { |
6d28f7ea | 99 | return nilfs_btree_node_get_flags(node) & NILFS_BTREE_NODE_ROOT; |
17c76b01 KS |
100 | } |
101 | ||
7c397a81 | 102 | static int nilfs_btree_node_get_level(const struct nilfs_btree_node *node) |
17c76b01 KS |
103 | { |
104 | return node->bn_level; | |
105 | } | |
106 | ||
7c397a81 | 107 | static void |
6d28f7ea | 108 | nilfs_btree_node_set_level(struct nilfs_btree_node *node, int level) |
17c76b01 KS |
109 | { |
110 | node->bn_level = level; | |
111 | } | |
112 | ||
7c397a81 | 113 | static int nilfs_btree_node_get_nchildren(const struct nilfs_btree_node *node) |
17c76b01 KS |
114 | { |
115 | return le16_to_cpu(node->bn_nchildren); | |
116 | } | |
117 | ||
7c397a81 | 118 | static void |
6d28f7ea | 119 | nilfs_btree_node_set_nchildren(struct nilfs_btree_node *node, int nchildren) |
17c76b01 KS |
120 | { |
121 | node->bn_nchildren = cpu_to_le16(nchildren); | |
122 | } | |
123 | ||
7c397a81 | 124 | static int nilfs_btree_node_size(const struct nilfs_bmap *btree) |
17c76b01 | 125 | { |
e7c274f8 | 126 | return 1 << btree->b_inode->i_blkbits; |
17c76b01 KS |
127 | } |
128 | ||
9b7b265c | 129 | static int nilfs_btree_nchildren_per_block(const struct nilfs_bmap *btree) |
17c76b01 | 130 | { |
5ad2686e | 131 | return btree->b_nchildren_per_block; |
17c76b01 KS |
132 | } |
133 | ||
7c397a81 | 134 | static __le64 * |
6d28f7ea | 135 | nilfs_btree_node_dkeys(const struct nilfs_btree_node *node) |
17c76b01 KS |
136 | { |
137 | return (__le64 *)((char *)(node + 1) + | |
6d28f7ea | 138 | (nilfs_btree_node_root(node) ? |
17c76b01 KS |
139 | 0 : NILFS_BTREE_NODE_EXTRA_PAD_SIZE)); |
140 | } | |
141 | ||
7c397a81 | 142 | static __le64 * |
9b7b265c | 143 | nilfs_btree_node_dptrs(const struct nilfs_btree_node *node, int ncmax) |
17c76b01 | 144 | { |
9b7b265c | 145 | return (__le64 *)(nilfs_btree_node_dkeys(node) + ncmax); |
17c76b01 KS |
146 | } |
147 | ||
7c397a81 | 148 | static __u64 |
6d28f7ea | 149 | nilfs_btree_node_get_key(const struct nilfs_btree_node *node, int index) |
17c76b01 | 150 | { |
25b8d7de | 151 | return le64_to_cpu(*(nilfs_btree_node_dkeys(node) + index)); |
17c76b01 KS |
152 | } |
153 | ||
7c397a81 | 154 | static void |
6d28f7ea | 155 | nilfs_btree_node_set_key(struct nilfs_btree_node *node, int index, __u64 key) |
17c76b01 | 156 | { |
25b8d7de | 157 | *(nilfs_btree_node_dkeys(node) + index) = cpu_to_le64(key); |
17c76b01 KS |
158 | } |
159 | ||
7c397a81 | 160 | static __u64 |
9b7b265c RK |
161 | nilfs_btree_node_get_ptr(const struct nilfs_btree_node *node, int index, |
162 | int ncmax) | |
17c76b01 | 163 | { |
9b7b265c | 164 | return le64_to_cpu(*(nilfs_btree_node_dptrs(node, ncmax) + index)); |
17c76b01 KS |
165 | } |
166 | ||
7c397a81 | 167 | static void |
9b7b265c RK |
168 | nilfs_btree_node_set_ptr(struct nilfs_btree_node *node, int index, __u64 ptr, |
169 | int ncmax) | |
17c76b01 | 170 | { |
9b7b265c | 171 | *(nilfs_btree_node_dptrs(node, ncmax) + index) = cpu_to_le64(ptr); |
17c76b01 KS |
172 | } |
173 | ||
9b7b265c RK |
174 | static void nilfs_btree_node_init(struct nilfs_btree_node *node, int flags, |
175 | int level, int nchildren, int ncmax, | |
17c76b01 KS |
176 | const __u64 *keys, const __u64 *ptrs) |
177 | { | |
178 | __le64 *dkeys; | |
179 | __le64 *dptrs; | |
180 | int i; | |
181 | ||
6d28f7ea RK |
182 | nilfs_btree_node_set_flags(node, flags); |
183 | nilfs_btree_node_set_level(node, level); | |
184 | nilfs_btree_node_set_nchildren(node, nchildren); | |
17c76b01 | 185 | |
6d28f7ea | 186 | dkeys = nilfs_btree_node_dkeys(node); |
9b7b265c | 187 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
17c76b01 | 188 | for (i = 0; i < nchildren; i++) { |
25b8d7de RK |
189 | dkeys[i] = cpu_to_le64(keys[i]); |
190 | dptrs[i] = cpu_to_le64(ptrs[i]); | |
17c76b01 KS |
191 | } |
192 | } | |
193 | ||
194 | /* Assume the buffer heads corresponding to left and right are locked. */ | |
9b7b265c | 195 | static void nilfs_btree_node_move_left(struct nilfs_btree_node *left, |
17c76b01 | 196 | struct nilfs_btree_node *right, |
9b7b265c | 197 | int n, int lncmax, int rncmax) |
17c76b01 KS |
198 | { |
199 | __le64 *ldkeys, *rdkeys; | |
200 | __le64 *ldptrs, *rdptrs; | |
201 | int lnchildren, rnchildren; | |
202 | ||
6d28f7ea | 203 | ldkeys = nilfs_btree_node_dkeys(left); |
9b7b265c | 204 | ldptrs = nilfs_btree_node_dptrs(left, lncmax); |
6d28f7ea | 205 | lnchildren = nilfs_btree_node_get_nchildren(left); |
17c76b01 | 206 | |
6d28f7ea | 207 | rdkeys = nilfs_btree_node_dkeys(right); |
9b7b265c | 208 | rdptrs = nilfs_btree_node_dptrs(right, rncmax); |
6d28f7ea | 209 | rnchildren = nilfs_btree_node_get_nchildren(right); |
17c76b01 KS |
210 | |
211 | memcpy(ldkeys + lnchildren, rdkeys, n * sizeof(*rdkeys)); | |
212 | memcpy(ldptrs + lnchildren, rdptrs, n * sizeof(*rdptrs)); | |
213 | memmove(rdkeys, rdkeys + n, (rnchildren - n) * sizeof(*rdkeys)); | |
214 | memmove(rdptrs, rdptrs + n, (rnchildren - n) * sizeof(*rdptrs)); | |
215 | ||
216 | lnchildren += n; | |
217 | rnchildren -= n; | |
6d28f7ea RK |
218 | nilfs_btree_node_set_nchildren(left, lnchildren); |
219 | nilfs_btree_node_set_nchildren(right, rnchildren); | |
17c76b01 KS |
220 | } |
221 | ||
222 | /* Assume that the buffer heads corresponding to left and right are locked. */ | |
9b7b265c | 223 | static void nilfs_btree_node_move_right(struct nilfs_btree_node *left, |
17c76b01 | 224 | struct nilfs_btree_node *right, |
9b7b265c | 225 | int n, int lncmax, int rncmax) |
17c76b01 KS |
226 | { |
227 | __le64 *ldkeys, *rdkeys; | |
228 | __le64 *ldptrs, *rdptrs; | |
229 | int lnchildren, rnchildren; | |
230 | ||
6d28f7ea | 231 | ldkeys = nilfs_btree_node_dkeys(left); |
9b7b265c | 232 | ldptrs = nilfs_btree_node_dptrs(left, lncmax); |
6d28f7ea | 233 | lnchildren = nilfs_btree_node_get_nchildren(left); |
17c76b01 | 234 | |
6d28f7ea | 235 | rdkeys = nilfs_btree_node_dkeys(right); |
9b7b265c | 236 | rdptrs = nilfs_btree_node_dptrs(right, rncmax); |
6d28f7ea | 237 | rnchildren = nilfs_btree_node_get_nchildren(right); |
17c76b01 KS |
238 | |
239 | memmove(rdkeys + n, rdkeys, rnchildren * sizeof(*rdkeys)); | |
240 | memmove(rdptrs + n, rdptrs, rnchildren * sizeof(*rdptrs)); | |
241 | memcpy(rdkeys, ldkeys + lnchildren - n, n * sizeof(*rdkeys)); | |
242 | memcpy(rdptrs, ldptrs + lnchildren - n, n * sizeof(*rdptrs)); | |
243 | ||
244 | lnchildren -= n; | |
245 | rnchildren += n; | |
6d28f7ea RK |
246 | nilfs_btree_node_set_nchildren(left, lnchildren); |
247 | nilfs_btree_node_set_nchildren(right, rnchildren); | |
17c76b01 KS |
248 | } |
249 | ||
250 | /* Assume that the buffer head corresponding to node is locked. */ | |
9b7b265c RK |
251 | static void nilfs_btree_node_insert(struct nilfs_btree_node *node, int index, |
252 | __u64 key, __u64 ptr, int ncmax) | |
17c76b01 KS |
253 | { |
254 | __le64 *dkeys; | |
255 | __le64 *dptrs; | |
256 | int nchildren; | |
257 | ||
6d28f7ea | 258 | dkeys = nilfs_btree_node_dkeys(node); |
9b7b265c | 259 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
6d28f7ea | 260 | nchildren = nilfs_btree_node_get_nchildren(node); |
17c76b01 KS |
261 | if (index < nchildren) { |
262 | memmove(dkeys + index + 1, dkeys + index, | |
263 | (nchildren - index) * sizeof(*dkeys)); | |
264 | memmove(dptrs + index + 1, dptrs + index, | |
265 | (nchildren - index) * sizeof(*dptrs)); | |
266 | } | |
25b8d7de RK |
267 | dkeys[index] = cpu_to_le64(key); |
268 | dptrs[index] = cpu_to_le64(ptr); | |
17c76b01 | 269 | nchildren++; |
6d28f7ea | 270 | nilfs_btree_node_set_nchildren(node, nchildren); |
17c76b01 KS |
271 | } |
272 | ||
273 | /* Assume that the buffer head corresponding to node is locked. */ | |
9b7b265c RK |
274 | static void nilfs_btree_node_delete(struct nilfs_btree_node *node, int index, |
275 | __u64 *keyp, __u64 *ptrp, int ncmax) | |
17c76b01 KS |
276 | { |
277 | __u64 key; | |
278 | __u64 ptr; | |
279 | __le64 *dkeys; | |
280 | __le64 *dptrs; | |
281 | int nchildren; | |
282 | ||
6d28f7ea | 283 | dkeys = nilfs_btree_node_dkeys(node); |
9b7b265c | 284 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
25b8d7de RK |
285 | key = le64_to_cpu(dkeys[index]); |
286 | ptr = le64_to_cpu(dptrs[index]); | |
6d28f7ea | 287 | nchildren = nilfs_btree_node_get_nchildren(node); |
17c76b01 KS |
288 | if (keyp != NULL) |
289 | *keyp = key; | |
290 | if (ptrp != NULL) | |
291 | *ptrp = ptr; | |
292 | ||
293 | if (index < nchildren - 1) { | |
294 | memmove(dkeys + index, dkeys + index + 1, | |
295 | (nchildren - index - 1) * sizeof(*dkeys)); | |
296 | memmove(dptrs + index, dptrs + index + 1, | |
297 | (nchildren - index - 1) * sizeof(*dptrs)); | |
298 | } | |
299 | nchildren--; | |
6d28f7ea | 300 | nilfs_btree_node_set_nchildren(node, nchildren); |
17c76b01 KS |
301 | } |
302 | ||
6d28f7ea | 303 | static int nilfs_btree_node_lookup(const struct nilfs_btree_node *node, |
17c76b01 KS |
304 | __u64 key, int *indexp) |
305 | { | |
306 | __u64 nkey; | |
307 | int index, low, high, s; | |
308 | ||
309 | /* binary search */ | |
310 | low = 0; | |
6d28f7ea | 311 | high = nilfs_btree_node_get_nchildren(node) - 1; |
17c76b01 KS |
312 | index = 0; |
313 | s = 0; | |
314 | while (low <= high) { | |
315 | index = (low + high) / 2; | |
6d28f7ea | 316 | nkey = nilfs_btree_node_get_key(node, index); |
17c76b01 KS |
317 | if (nkey == key) { |
318 | s = 0; | |
319 | goto out; | |
320 | } else if (nkey < key) { | |
321 | low = index + 1; | |
322 | s = -1; | |
323 | } else { | |
324 | high = index - 1; | |
325 | s = 1; | |
326 | } | |
327 | } | |
328 | ||
329 | /* adjust index */ | |
6d28f7ea RK |
330 | if (nilfs_btree_node_get_level(node) > NILFS_BTREE_LEVEL_NODE_MIN) { |
331 | if (s > 0 && index > 0) | |
17c76b01 KS |
332 | index--; |
333 | } else if (s < 0) | |
334 | index++; | |
335 | ||
336 | out: | |
17c76b01 KS |
337 | *indexp = index; |
338 | ||
339 | return s == 0; | |
340 | } | |
341 | ||
1d5385b9 RK |
342 | /** |
343 | * nilfs_btree_node_broken - verify consistency of btree node | |
344 | * @node: btree node block to be examined | |
345 | * @size: node size (in bytes) | |
346 | * @blocknr: block number | |
347 | * | |
348 | * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned. | |
349 | */ | |
350 | static int nilfs_btree_node_broken(const struct nilfs_btree_node *node, | |
351 | size_t size, sector_t blocknr) | |
352 | { | |
353 | int level, flags, nchildren; | |
354 | int ret = 0; | |
355 | ||
356 | level = nilfs_btree_node_get_level(node); | |
357 | flags = nilfs_btree_node_get_flags(node); | |
358 | nchildren = nilfs_btree_node_get_nchildren(node); | |
359 | ||
360 | if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN || | |
361 | level >= NILFS_BTREE_LEVEL_MAX || | |
362 | (flags & NILFS_BTREE_NODE_ROOT) || | |
363 | nchildren < 0 || | |
364 | nchildren > NILFS_BTREE_NODE_NCHILDREN_MAX(size))) { | |
365 | printk(KERN_CRIT "NILFS: bad btree node (blocknr=%llu): " | |
366 | "level = %d, flags = 0x%x, nchildren = %d\n", | |
367 | (unsigned long long)blocknr, level, flags, nchildren); | |
368 | ret = 1; | |
369 | } | |
370 | return ret; | |
371 | } | |
372 | ||
b6b14e98 RK |
373 | /** |
374 | * nilfs_btree_root_broken - verify consistency of btree root node | |
375 | * @node: btree root node to be examined | |
376 | * @ino: inode number | |
377 | * | |
378 | * Return Value: If node is broken, 1 is returned. Otherwise, 0 is returned. | |
379 | */ | |
380 | static int nilfs_btree_root_broken(const struct nilfs_btree_node *node, | |
381 | unsigned long ino) | |
382 | { | |
383 | int level, flags, nchildren; | |
384 | int ret = 0; | |
385 | ||
386 | level = nilfs_btree_node_get_level(node); | |
387 | flags = nilfs_btree_node_get_flags(node); | |
388 | nchildren = nilfs_btree_node_get_nchildren(node); | |
389 | ||
390 | if (unlikely(level < NILFS_BTREE_LEVEL_NODE_MIN || | |
c043edcc | 391 | level >= NILFS_BTREE_LEVEL_MAX || |
b6b14e98 RK |
392 | nchildren < 0 || |
393 | nchildren > NILFS_BTREE_ROOT_NCHILDREN_MAX)) { | |
394 | pr_crit("NILFS: bad btree root (inode number=%lu): level = %d, flags = 0x%x, nchildren = %d\n", | |
395 | ino, level, flags, nchildren); | |
396 | ret = 1; | |
397 | } | |
398 | return ret; | |
399 | } | |
400 | ||
1d5385b9 RK |
401 | int nilfs_btree_broken_node_block(struct buffer_head *bh) |
402 | { | |
4e13e66b RK |
403 | int ret; |
404 | ||
405 | if (buffer_nilfs_checked(bh)) | |
406 | return 0; | |
407 | ||
408 | ret = nilfs_btree_node_broken((struct nilfs_btree_node *)bh->b_data, | |
1d5385b9 | 409 | bh->b_size, bh->b_blocknr); |
4e13e66b RK |
410 | if (likely(!ret)) |
411 | set_buffer_nilfs_checked(bh); | |
412 | return ret; | |
1d5385b9 RK |
413 | } |
414 | ||
7c397a81 | 415 | static struct nilfs_btree_node * |
e7c274f8 | 416 | nilfs_btree_get_root(const struct nilfs_bmap *btree) |
17c76b01 | 417 | { |
e7c274f8 | 418 | return (struct nilfs_btree_node *)btree->b_u.u_data; |
17c76b01 KS |
419 | } |
420 | ||
7c397a81 | 421 | static struct nilfs_btree_node * |
6d28f7ea | 422 | nilfs_btree_get_nonroot_node(const struct nilfs_btree_path *path, int level) |
17c76b01 KS |
423 | { |
424 | return (struct nilfs_btree_node *)path[level].bp_bh->b_data; | |
425 | } | |
426 | ||
7c397a81 | 427 | static struct nilfs_btree_node * |
6d28f7ea | 428 | nilfs_btree_get_sib_node(const struct nilfs_btree_path *path, int level) |
17c76b01 KS |
429 | { |
430 | return (struct nilfs_btree_node *)path[level].bp_sib_bh->b_data; | |
431 | } | |
432 | ||
7c397a81 | 433 | static int nilfs_btree_height(const struct nilfs_bmap *btree) |
17c76b01 | 434 | { |
6d28f7ea | 435 | return nilfs_btree_node_get_level(nilfs_btree_get_root(btree)) + 1; |
17c76b01 KS |
436 | } |
437 | ||
9b7b265c | 438 | static struct nilfs_btree_node * |
e7c274f8 | 439 | nilfs_btree_get_node(const struct nilfs_bmap *btree, |
17c76b01 | 440 | const struct nilfs_btree_path *path, |
9b7b265c | 441 | int level, int *ncmaxp) |
17c76b01 | 442 | { |
9b7b265c RK |
443 | struct nilfs_btree_node *node; |
444 | ||
445 | if (level == nilfs_btree_height(btree) - 1) { | |
446 | node = nilfs_btree_get_root(btree); | |
447 | *ncmaxp = NILFS_BTREE_ROOT_NCHILDREN_MAX; | |
448 | } else { | |
449 | node = nilfs_btree_get_nonroot_node(path, level); | |
450 | *ncmaxp = nilfs_btree_nchildren_per_block(btree); | |
451 | } | |
452 | return node; | |
17c76b01 KS |
453 | } |
454 | ||
7c397a81 | 455 | static int |
9b945d53 RK |
456 | nilfs_btree_bad_node(struct nilfs_btree_node *node, int level) |
457 | { | |
458 | if (unlikely(nilfs_btree_node_get_level(node) != level)) { | |
459 | dump_stack(); | |
460 | printk(KERN_CRIT "NILFS: btree level mismatch: %d != %d\n", | |
461 | nilfs_btree_node_get_level(node), level); | |
462 | return 1; | |
463 | } | |
464 | return 0; | |
465 | } | |
466 | ||
464ece88 RK |
467 | struct nilfs_btree_readahead_info { |
468 | struct nilfs_btree_node *node; /* parent node */ | |
469 | int max_ra_blocks; /* max nof blocks to read ahead */ | |
470 | int index; /* current index on the parent node */ | |
471 | int ncmax; /* nof children in the parent node */ | |
472 | }; | |
473 | ||
474 | static int __nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr, | |
475 | struct buffer_head **bhp, | |
476 | const struct nilfs_btree_readahead_info *ra) | |
477 | { | |
478 | struct address_space *btnc = &NILFS_BMAP_I(btree)->i_btnode_cache; | |
479 | struct buffer_head *bh, *ra_bh; | |
480 | sector_t submit_ptr = 0; | |
481 | int ret; | |
482 | ||
483 | ret = nilfs_btnode_submit_block(btnc, ptr, 0, READ, &bh, &submit_ptr); | |
484 | if (ret) { | |
485 | if (ret != -EEXIST) | |
486 | return ret; | |
487 | goto out_check; | |
488 | } | |
489 | ||
490 | if (ra) { | |
491 | int i, n; | |
492 | __u64 ptr2; | |
493 | ||
494 | /* read ahead sibling nodes */ | |
495 | for (n = ra->max_ra_blocks, i = ra->index + 1; | |
496 | n > 0 && i < ra->ncmax; n--, i++) { | |
497 | ptr2 = nilfs_btree_node_get_ptr(ra->node, i, ra->ncmax); | |
498 | ||
499 | ret = nilfs_btnode_submit_block(btnc, ptr2, 0, READA, | |
500 | &ra_bh, &submit_ptr); | |
501 | if (likely(!ret || ret == -EEXIST)) | |
502 | brelse(ra_bh); | |
503 | else if (ret != -EBUSY) | |
504 | break; | |
505 | if (!buffer_locked(bh)) | |
506 | goto out_no_wait; | |
507 | } | |
508 | } | |
509 | ||
510 | wait_on_buffer(bh); | |
511 | ||
512 | out_no_wait: | |
513 | if (!buffer_uptodate(bh)) { | |
514 | brelse(bh); | |
515 | return -EIO; | |
516 | } | |
517 | ||
518 | out_check: | |
519 | if (nilfs_btree_broken_node_block(bh)) { | |
520 | clear_buffer_uptodate(bh); | |
521 | brelse(bh); | |
522 | return -EINVAL; | |
523 | } | |
524 | ||
525 | *bhp = bh; | |
526 | return 0; | |
527 | } | |
528 | ||
529 | static int nilfs_btree_get_block(const struct nilfs_bmap *btree, __u64 ptr, | |
530 | struct buffer_head **bhp) | |
531 | { | |
532 | return __nilfs_btree_get_block(btree, ptr, bhp, NULL); | |
533 | } | |
534 | ||
e7c274f8 | 535 | static int nilfs_btree_do_lookup(const struct nilfs_bmap *btree, |
17c76b01 | 536 | struct nilfs_btree_path *path, |
03bdb5ac RK |
537 | __u64 key, __u64 *ptrp, int minlevel, |
538 | int readahead) | |
17c76b01 KS |
539 | { |
540 | struct nilfs_btree_node *node; | |
03bdb5ac | 541 | struct nilfs_btree_readahead_info p, *ra; |
17c76b01 | 542 | __u64 ptr; |
ea64ab87 | 543 | int level, index, found, ncmax, ret; |
17c76b01 | 544 | |
17c76b01 | 545 | node = nilfs_btree_get_root(btree); |
6d28f7ea RK |
546 | level = nilfs_btree_node_get_level(node); |
547 | if (level < minlevel || nilfs_btree_node_get_nchildren(node) <= 0) | |
17c76b01 KS |
548 | return -ENOENT; |
549 | ||
6d28f7ea | 550 | found = nilfs_btree_node_lookup(node, key, &index); |
9b7b265c RK |
551 | ptr = nilfs_btree_node_get_ptr(node, index, |
552 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
17c76b01 KS |
553 | path[level].bp_bh = NULL; |
554 | path[level].bp_index = index; | |
555 | ||
9b7b265c | 556 | ncmax = nilfs_btree_nchildren_per_block(btree); |
ea64ab87 | 557 | |
03bdb5ac RK |
558 | while (--level >= minlevel) { |
559 | ra = NULL; | |
560 | if (level == NILFS_BTREE_LEVEL_NODE_MIN && readahead) { | |
561 | p.node = nilfs_btree_get_node(btree, path, level + 1, | |
562 | &p.ncmax); | |
563 | p.index = index; | |
564 | p.max_ra_blocks = 7; | |
565 | ra = &p; | |
566 | } | |
567 | ret = __nilfs_btree_get_block(btree, ptr, &path[level].bp_bh, | |
568 | ra); | |
17c76b01 KS |
569 | if (ret < 0) |
570 | return ret; | |
03bdb5ac | 571 | |
6d28f7ea | 572 | node = nilfs_btree_get_nonroot_node(path, level); |
9b945d53 RK |
573 | if (nilfs_btree_bad_node(node, level)) |
574 | return -EINVAL; | |
17c76b01 | 575 | if (!found) |
6d28f7ea | 576 | found = nilfs_btree_node_lookup(node, key, &index); |
17c76b01 KS |
577 | else |
578 | index = 0; | |
ea64ab87 | 579 | if (index < ncmax) { |
9b7b265c | 580 | ptr = nilfs_btree_node_get_ptr(node, index, ncmax); |
ea64ab87 | 581 | } else { |
1f5abe7e | 582 | WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN); |
17c76b01 KS |
583 | /* insert */ |
584 | ptr = NILFS_BMAP_INVALID_PTR; | |
585 | } | |
586 | path[level].bp_index = index; | |
587 | } | |
588 | if (!found) | |
589 | return -ENOENT; | |
590 | ||
591 | if (ptrp != NULL) | |
592 | *ptrp = ptr; | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
e7c274f8 | 597 | static int nilfs_btree_do_lookup_last(const struct nilfs_bmap *btree, |
17c76b01 KS |
598 | struct nilfs_btree_path *path, |
599 | __u64 *keyp, __u64 *ptrp) | |
600 | { | |
601 | struct nilfs_btree_node *node; | |
602 | __u64 ptr; | |
9b7b265c | 603 | int index, level, ncmax, ret; |
17c76b01 KS |
604 | |
605 | node = nilfs_btree_get_root(btree); | |
6d28f7ea | 606 | index = nilfs_btree_node_get_nchildren(node) - 1; |
17c76b01 KS |
607 | if (index < 0) |
608 | return -ENOENT; | |
6d28f7ea | 609 | level = nilfs_btree_node_get_level(node); |
9b7b265c RK |
610 | ptr = nilfs_btree_node_get_ptr(node, index, |
611 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
17c76b01 KS |
612 | path[level].bp_bh = NULL; |
613 | path[level].bp_index = index; | |
9b7b265c | 614 | ncmax = nilfs_btree_nchildren_per_block(btree); |
17c76b01 KS |
615 | |
616 | for (level--; level > 0; level--) { | |
f198dbb9 | 617 | ret = nilfs_btree_get_block(btree, ptr, &path[level].bp_bh); |
17c76b01 KS |
618 | if (ret < 0) |
619 | return ret; | |
6d28f7ea | 620 | node = nilfs_btree_get_nonroot_node(path, level); |
9b945d53 RK |
621 | if (nilfs_btree_bad_node(node, level)) |
622 | return -EINVAL; | |
6d28f7ea | 623 | index = nilfs_btree_node_get_nchildren(node) - 1; |
9b7b265c | 624 | ptr = nilfs_btree_node_get_ptr(node, index, ncmax); |
17c76b01 KS |
625 | path[level].bp_index = index; |
626 | } | |
627 | ||
628 | if (keyp != NULL) | |
6d28f7ea | 629 | *keyp = nilfs_btree_node_get_key(node, index); |
17c76b01 KS |
630 | if (ptrp != NULL) |
631 | *ptrp = ptr; | |
632 | ||
633 | return 0; | |
634 | } | |
635 | ||
e7c274f8 | 636 | static int nilfs_btree_lookup(const struct nilfs_bmap *btree, |
17c76b01 KS |
637 | __u64 key, int level, __u64 *ptrp) |
638 | { | |
17c76b01 | 639 | struct nilfs_btree_path *path; |
17c76b01 KS |
640 | int ret; |
641 | ||
6d28f7ea | 642 | path = nilfs_btree_alloc_path(); |
17c76b01 KS |
643 | if (path == NULL) |
644 | return -ENOMEM; | |
17c76b01 | 645 | |
03bdb5ac | 646 | ret = nilfs_btree_do_lookup(btree, path, key, ptrp, level, 0); |
17c76b01 | 647 | |
6d28f7ea | 648 | nilfs_btree_free_path(path); |
17c76b01 KS |
649 | |
650 | return ret; | |
651 | } | |
652 | ||
e7c274f8 | 653 | static int nilfs_btree_lookup_contig(const struct nilfs_bmap *btree, |
c3a7abf0 RK |
654 | __u64 key, __u64 *ptrp, unsigned maxblocks) |
655 | { | |
c3a7abf0 RK |
656 | struct nilfs_btree_path *path; |
657 | struct nilfs_btree_node *node; | |
658 | struct inode *dat = NULL; | |
659 | __u64 ptr, ptr2; | |
660 | sector_t blocknr; | |
661 | int level = NILFS_BTREE_LEVEL_NODE_MIN; | |
9b7b265c | 662 | int ret, cnt, index, maxlevel, ncmax; |
03bdb5ac | 663 | struct nilfs_btree_readahead_info p; |
c3a7abf0 | 664 | |
6d28f7ea | 665 | path = nilfs_btree_alloc_path(); |
c3a7abf0 RK |
666 | if (path == NULL) |
667 | return -ENOMEM; | |
f905440f | 668 | |
03bdb5ac | 669 | ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level, 1); |
c3a7abf0 RK |
670 | if (ret < 0) |
671 | goto out; | |
672 | ||
e7c274f8 RK |
673 | if (NILFS_BMAP_USE_VBN(btree)) { |
674 | dat = nilfs_bmap_get_dat(btree); | |
c3a7abf0 RK |
675 | ret = nilfs_dat_translate(dat, ptr, &blocknr); |
676 | if (ret < 0) | |
677 | goto out; | |
678 | ptr = blocknr; | |
679 | } | |
680 | cnt = 1; | |
681 | if (cnt == maxblocks) | |
682 | goto end; | |
683 | ||
684 | maxlevel = nilfs_btree_height(btree) - 1; | |
9b7b265c | 685 | node = nilfs_btree_get_node(btree, path, level, &ncmax); |
c3a7abf0 RK |
686 | index = path[level].bp_index + 1; |
687 | for (;;) { | |
6d28f7ea RK |
688 | while (index < nilfs_btree_node_get_nchildren(node)) { |
689 | if (nilfs_btree_node_get_key(node, index) != | |
c3a7abf0 RK |
690 | key + cnt) |
691 | goto end; | |
9b7b265c | 692 | ptr2 = nilfs_btree_node_get_ptr(node, index, ncmax); |
c3a7abf0 RK |
693 | if (dat) { |
694 | ret = nilfs_dat_translate(dat, ptr2, &blocknr); | |
695 | if (ret < 0) | |
696 | goto out; | |
697 | ptr2 = blocknr; | |
698 | } | |
699 | if (ptr2 != ptr + cnt || ++cnt == maxblocks) | |
700 | goto end; | |
701 | index++; | |
702 | continue; | |
703 | } | |
704 | if (level == maxlevel) | |
705 | break; | |
706 | ||
707 | /* look-up right sibling node */ | |
03bdb5ac RK |
708 | p.node = nilfs_btree_get_node(btree, path, level + 1, &p.ncmax); |
709 | p.index = path[level + 1].bp_index + 1; | |
710 | p.max_ra_blocks = 7; | |
711 | if (p.index >= nilfs_btree_node_get_nchildren(p.node) || | |
712 | nilfs_btree_node_get_key(p.node, p.index) != key + cnt) | |
c3a7abf0 | 713 | break; |
03bdb5ac RK |
714 | ptr2 = nilfs_btree_node_get_ptr(p.node, p.index, p.ncmax); |
715 | path[level + 1].bp_index = p.index; | |
c3a7abf0 RK |
716 | |
717 | brelse(path[level].bp_bh); | |
718 | path[level].bp_bh = NULL; | |
03bdb5ac RK |
719 | |
720 | ret = __nilfs_btree_get_block(btree, ptr2, &path[level].bp_bh, | |
721 | &p); | |
c3a7abf0 RK |
722 | if (ret < 0) |
723 | goto out; | |
6d28f7ea | 724 | node = nilfs_btree_get_nonroot_node(path, level); |
9b7b265c | 725 | ncmax = nilfs_btree_nchildren_per_block(btree); |
c3a7abf0 RK |
726 | index = 0; |
727 | path[level].bp_index = index; | |
728 | } | |
729 | end: | |
730 | *ptrp = ptr; | |
731 | ret = cnt; | |
732 | out: | |
6d28f7ea | 733 | nilfs_btree_free_path(path); |
c3a7abf0 RK |
734 | return ret; |
735 | } | |
736 | ||
e7c274f8 | 737 | static void nilfs_btree_promote_key(struct nilfs_bmap *btree, |
17c76b01 KS |
738 | struct nilfs_btree_path *path, |
739 | int level, __u64 key) | |
740 | { | |
741 | if (level < nilfs_btree_height(btree) - 1) { | |
742 | do { | |
17c76b01 | 743 | nilfs_btree_node_set_key( |
6d28f7ea | 744 | nilfs_btree_get_nonroot_node(path, level), |
17c76b01 KS |
745 | path[level].bp_index, key); |
746 | if (!buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 747 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 KS |
748 | } while ((path[level].bp_index == 0) && |
749 | (++level < nilfs_btree_height(btree) - 1)); | |
750 | } | |
751 | ||
752 | /* root */ | |
753 | if (level == nilfs_btree_height(btree) - 1) { | |
6d28f7ea | 754 | nilfs_btree_node_set_key(nilfs_btree_get_root(btree), |
17c76b01 KS |
755 | path[level].bp_index, key); |
756 | } | |
757 | } | |
758 | ||
e7c274f8 | 759 | static void nilfs_btree_do_insert(struct nilfs_bmap *btree, |
17c76b01 KS |
760 | struct nilfs_btree_path *path, |
761 | int level, __u64 *keyp, __u64 *ptrp) | |
762 | { | |
763 | struct nilfs_btree_node *node; | |
9b7b265c | 764 | int ncblk; |
17c76b01 KS |
765 | |
766 | if (level < nilfs_btree_height(btree) - 1) { | |
6d28f7ea | 767 | node = nilfs_btree_get_nonroot_node(path, level); |
9b7b265c RK |
768 | ncblk = nilfs_btree_nchildren_per_block(btree); |
769 | nilfs_btree_node_insert(node, path[level].bp_index, | |
770 | *keyp, *ptrp, ncblk); | |
17c76b01 | 771 | if (!buffer_dirty(path[level].bp_bh)) |
5fc7b141 | 772 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 KS |
773 | |
774 | if (path[level].bp_index == 0) | |
775 | nilfs_btree_promote_key(btree, path, level + 1, | |
6d28f7ea RK |
776 | nilfs_btree_node_get_key(node, |
777 | 0)); | |
17c76b01 KS |
778 | } else { |
779 | node = nilfs_btree_get_root(btree); | |
9b7b265c RK |
780 | nilfs_btree_node_insert(node, path[level].bp_index, |
781 | *keyp, *ptrp, | |
782 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
17c76b01 KS |
783 | } |
784 | } | |
785 | ||
e7c274f8 | 786 | static void nilfs_btree_carry_left(struct nilfs_bmap *btree, |
17c76b01 KS |
787 | struct nilfs_btree_path *path, |
788 | int level, __u64 *keyp, __u64 *ptrp) | |
789 | { | |
790 | struct nilfs_btree_node *node, *left; | |
9b7b265c | 791 | int nchildren, lnchildren, n, move, ncblk; |
17c76b01 | 792 | |
6d28f7ea RK |
793 | node = nilfs_btree_get_nonroot_node(path, level); |
794 | left = nilfs_btree_get_sib_node(path, level); | |
795 | nchildren = nilfs_btree_node_get_nchildren(node); | |
796 | lnchildren = nilfs_btree_node_get_nchildren(left); | |
9b7b265c | 797 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 KS |
798 | move = 0; |
799 | ||
800 | n = (nchildren + lnchildren + 1) / 2 - lnchildren; | |
801 | if (n > path[level].bp_index) { | |
802 | /* move insert point */ | |
803 | n--; | |
804 | move = 1; | |
805 | } | |
806 | ||
9b7b265c | 807 | nilfs_btree_node_move_left(left, node, n, ncblk, ncblk); |
17c76b01 KS |
808 | |
809 | if (!buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 810 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 | 811 | if (!buffer_dirty(path[level].bp_sib_bh)) |
5fc7b141 | 812 | mark_buffer_dirty(path[level].bp_sib_bh); |
17c76b01 | 813 | |
17c76b01 | 814 | nilfs_btree_promote_key(btree, path, level + 1, |
6d28f7ea | 815 | nilfs_btree_node_get_key(node, 0)); |
17c76b01 KS |
816 | |
817 | if (move) { | |
087d01b4 | 818 | brelse(path[level].bp_bh); |
17c76b01 KS |
819 | path[level].bp_bh = path[level].bp_sib_bh; |
820 | path[level].bp_sib_bh = NULL; | |
821 | path[level].bp_index += lnchildren; | |
822 | path[level + 1].bp_index--; | |
823 | } else { | |
087d01b4 | 824 | brelse(path[level].bp_sib_bh); |
17c76b01 KS |
825 | path[level].bp_sib_bh = NULL; |
826 | path[level].bp_index -= n; | |
827 | } | |
828 | ||
829 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); | |
830 | } | |
831 | ||
e7c274f8 | 832 | static void nilfs_btree_carry_right(struct nilfs_bmap *btree, |
17c76b01 KS |
833 | struct nilfs_btree_path *path, |
834 | int level, __u64 *keyp, __u64 *ptrp) | |
835 | { | |
836 | struct nilfs_btree_node *node, *right; | |
9b7b265c | 837 | int nchildren, rnchildren, n, move, ncblk; |
17c76b01 | 838 | |
6d28f7ea RK |
839 | node = nilfs_btree_get_nonroot_node(path, level); |
840 | right = nilfs_btree_get_sib_node(path, level); | |
841 | nchildren = nilfs_btree_node_get_nchildren(node); | |
842 | rnchildren = nilfs_btree_node_get_nchildren(right); | |
9b7b265c | 843 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 KS |
844 | move = 0; |
845 | ||
846 | n = (nchildren + rnchildren + 1) / 2 - rnchildren; | |
847 | if (n > nchildren - path[level].bp_index) { | |
848 | /* move insert point */ | |
849 | n--; | |
850 | move = 1; | |
851 | } | |
852 | ||
9b7b265c | 853 | nilfs_btree_node_move_right(node, right, n, ncblk, ncblk); |
17c76b01 KS |
854 | |
855 | if (!buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 856 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 | 857 | if (!buffer_dirty(path[level].bp_sib_bh)) |
5fc7b141 | 858 | mark_buffer_dirty(path[level].bp_sib_bh); |
17c76b01 | 859 | |
17c76b01 KS |
860 | path[level + 1].bp_index++; |
861 | nilfs_btree_promote_key(btree, path, level + 1, | |
6d28f7ea | 862 | nilfs_btree_node_get_key(right, 0)); |
17c76b01 KS |
863 | path[level + 1].bp_index--; |
864 | ||
865 | if (move) { | |
087d01b4 | 866 | brelse(path[level].bp_bh); |
17c76b01 KS |
867 | path[level].bp_bh = path[level].bp_sib_bh; |
868 | path[level].bp_sib_bh = NULL; | |
6d28f7ea | 869 | path[level].bp_index -= nilfs_btree_node_get_nchildren(node); |
17c76b01 KS |
870 | path[level + 1].bp_index++; |
871 | } else { | |
087d01b4 | 872 | brelse(path[level].bp_sib_bh); |
17c76b01 KS |
873 | path[level].bp_sib_bh = NULL; |
874 | } | |
875 | ||
876 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); | |
877 | } | |
878 | ||
e7c274f8 | 879 | static void nilfs_btree_split(struct nilfs_bmap *btree, |
17c76b01 KS |
880 | struct nilfs_btree_path *path, |
881 | int level, __u64 *keyp, __u64 *ptrp) | |
882 | { | |
883 | struct nilfs_btree_node *node, *right; | |
884 | __u64 newkey; | |
885 | __u64 newptr; | |
9b7b265c | 886 | int nchildren, n, move, ncblk; |
17c76b01 | 887 | |
6d28f7ea RK |
888 | node = nilfs_btree_get_nonroot_node(path, level); |
889 | right = nilfs_btree_get_sib_node(path, level); | |
890 | nchildren = nilfs_btree_node_get_nchildren(node); | |
9b7b265c | 891 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 KS |
892 | move = 0; |
893 | ||
894 | n = (nchildren + 1) / 2; | |
895 | if (n > nchildren - path[level].bp_index) { | |
896 | n--; | |
897 | move = 1; | |
898 | } | |
899 | ||
9b7b265c | 900 | nilfs_btree_node_move_right(node, right, n, ncblk, ncblk); |
17c76b01 KS |
901 | |
902 | if (!buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 903 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 | 904 | if (!buffer_dirty(path[level].bp_sib_bh)) |
5fc7b141 | 905 | mark_buffer_dirty(path[level].bp_sib_bh); |
17c76b01 | 906 | |
6d28f7ea | 907 | newkey = nilfs_btree_node_get_key(right, 0); |
17c76b01 KS |
908 | newptr = path[level].bp_newreq.bpr_ptr; |
909 | ||
910 | if (move) { | |
6d28f7ea | 911 | path[level].bp_index -= nilfs_btree_node_get_nchildren(node); |
9b7b265c RK |
912 | nilfs_btree_node_insert(right, path[level].bp_index, |
913 | *keyp, *ptrp, ncblk); | |
17c76b01 | 914 | |
6d28f7ea | 915 | *keyp = nilfs_btree_node_get_key(right, 0); |
17c76b01 KS |
916 | *ptrp = path[level].bp_newreq.bpr_ptr; |
917 | ||
087d01b4 | 918 | brelse(path[level].bp_bh); |
17c76b01 KS |
919 | path[level].bp_bh = path[level].bp_sib_bh; |
920 | path[level].bp_sib_bh = NULL; | |
921 | } else { | |
922 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); | |
923 | ||
6d28f7ea | 924 | *keyp = nilfs_btree_node_get_key(right, 0); |
17c76b01 KS |
925 | *ptrp = path[level].bp_newreq.bpr_ptr; |
926 | ||
087d01b4 | 927 | brelse(path[level].bp_sib_bh); |
17c76b01 KS |
928 | path[level].bp_sib_bh = NULL; |
929 | } | |
930 | ||
931 | path[level + 1].bp_index++; | |
932 | } | |
933 | ||
e7c274f8 | 934 | static void nilfs_btree_grow(struct nilfs_bmap *btree, |
17c76b01 KS |
935 | struct nilfs_btree_path *path, |
936 | int level, __u64 *keyp, __u64 *ptrp) | |
937 | { | |
938 | struct nilfs_btree_node *root, *child; | |
9b7b265c | 939 | int n, ncblk; |
17c76b01 | 940 | |
17c76b01 | 941 | root = nilfs_btree_get_root(btree); |
6d28f7ea | 942 | child = nilfs_btree_get_sib_node(path, level); |
9b7b265c | 943 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 | 944 | |
6d28f7ea | 945 | n = nilfs_btree_node_get_nchildren(root); |
17c76b01 | 946 | |
9b7b265c RK |
947 | nilfs_btree_node_move_right(root, child, n, |
948 | NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk); | |
6d28f7ea | 949 | nilfs_btree_node_set_level(root, level + 1); |
17c76b01 KS |
950 | |
951 | if (!buffer_dirty(path[level].bp_sib_bh)) | |
5fc7b141 | 952 | mark_buffer_dirty(path[level].bp_sib_bh); |
17c76b01 | 953 | |
17c76b01 KS |
954 | path[level].bp_bh = path[level].bp_sib_bh; |
955 | path[level].bp_sib_bh = NULL; | |
956 | ||
957 | nilfs_btree_do_insert(btree, path, level, keyp, ptrp); | |
958 | ||
6d28f7ea | 959 | *keyp = nilfs_btree_node_get_key(child, 0); |
17c76b01 KS |
960 | *ptrp = path[level].bp_newreq.bpr_ptr; |
961 | } | |
962 | ||
e7c274f8 | 963 | static __u64 nilfs_btree_find_near(const struct nilfs_bmap *btree, |
17c76b01 KS |
964 | const struct nilfs_btree_path *path) |
965 | { | |
966 | struct nilfs_btree_node *node; | |
9b7b265c | 967 | int level, ncmax; |
17c76b01 KS |
968 | |
969 | if (path == NULL) | |
970 | return NILFS_BMAP_INVALID_PTR; | |
971 | ||
972 | /* left sibling */ | |
973 | level = NILFS_BTREE_LEVEL_NODE_MIN; | |
974 | if (path[level].bp_index > 0) { | |
9b7b265c RK |
975 | node = nilfs_btree_get_node(btree, path, level, &ncmax); |
976 | return nilfs_btree_node_get_ptr(node, | |
977 | path[level].bp_index - 1, | |
978 | ncmax); | |
17c76b01 KS |
979 | } |
980 | ||
981 | /* parent */ | |
982 | level = NILFS_BTREE_LEVEL_NODE_MIN + 1; | |
983 | if (level <= nilfs_btree_height(btree) - 1) { | |
9b7b265c RK |
984 | node = nilfs_btree_get_node(btree, path, level, &ncmax); |
985 | return nilfs_btree_node_get_ptr(node, path[level].bp_index, | |
986 | ncmax); | |
17c76b01 KS |
987 | } |
988 | ||
989 | return NILFS_BMAP_INVALID_PTR; | |
990 | } | |
991 | ||
e7c274f8 | 992 | static __u64 nilfs_btree_find_target_v(const struct nilfs_bmap *btree, |
17c76b01 KS |
993 | const struct nilfs_btree_path *path, |
994 | __u64 key) | |
995 | { | |
996 | __u64 ptr; | |
997 | ||
e7c274f8 | 998 | ptr = nilfs_bmap_find_target_seq(btree, key); |
17c76b01 KS |
999 | if (ptr != NILFS_BMAP_INVALID_PTR) |
1000 | /* sequential access */ | |
1001 | return ptr; | |
1002 | else { | |
1003 | ptr = nilfs_btree_find_near(btree, path); | |
1004 | if (ptr != NILFS_BMAP_INVALID_PTR) | |
1005 | /* near */ | |
1006 | return ptr; | |
1007 | } | |
1008 | /* block group */ | |
e7c274f8 | 1009 | return nilfs_bmap_find_target_in_group(btree); |
17c76b01 KS |
1010 | } |
1011 | ||
e7c274f8 | 1012 | static int nilfs_btree_prepare_insert(struct nilfs_bmap *btree, |
17c76b01 KS |
1013 | struct nilfs_btree_path *path, |
1014 | int *levelp, __u64 key, __u64 ptr, | |
1015 | struct nilfs_bmap_stats *stats) | |
1016 | { | |
1017 | struct buffer_head *bh; | |
1018 | struct nilfs_btree_node *node, *parent, *sib; | |
1019 | __u64 sibptr; | |
9b7b265c | 1020 | int pindex, level, ncmax, ncblk, ret; |
2e0c2c73 | 1021 | struct inode *dat = NULL; |
17c76b01 KS |
1022 | |
1023 | stats->bs_nblocks = 0; | |
1024 | level = NILFS_BTREE_LEVEL_DATA; | |
1025 | ||
1026 | /* allocate a new ptr for data block */ | |
e7c274f8 | 1027 | if (NILFS_BMAP_USE_VBN(btree)) { |
17c76b01 | 1028 | path[level].bp_newreq.bpr_ptr = |
7cde31d7 | 1029 | nilfs_btree_find_target_v(btree, path, key); |
e7c274f8 | 1030 | dat = nilfs_bmap_get_dat(btree); |
2e0c2c73 | 1031 | } |
17c76b01 | 1032 | |
e7c274f8 | 1033 | ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat); |
17c76b01 KS |
1034 | if (ret < 0) |
1035 | goto err_out_data; | |
1036 | ||
9b7b265c | 1037 | ncblk = nilfs_btree_nchildren_per_block(btree); |
ea64ab87 | 1038 | |
17c76b01 KS |
1039 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; |
1040 | level < nilfs_btree_height(btree) - 1; | |
1041 | level++) { | |
6d28f7ea | 1042 | node = nilfs_btree_get_nonroot_node(path, level); |
9b7b265c | 1043 | if (nilfs_btree_node_get_nchildren(node) < ncblk) { |
17c76b01 KS |
1044 | path[level].bp_op = nilfs_btree_do_insert; |
1045 | stats->bs_nblocks++; | |
1046 | goto out; | |
1047 | } | |
1048 | ||
9b7b265c | 1049 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
17c76b01 KS |
1050 | pindex = path[level + 1].bp_index; |
1051 | ||
1052 | /* left sibling */ | |
1053 | if (pindex > 0) { | |
9b7b265c RK |
1054 | sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1, |
1055 | ncmax); | |
f198dbb9 | 1056 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
17c76b01 KS |
1057 | if (ret < 0) |
1058 | goto err_out_child_node; | |
1059 | sib = (struct nilfs_btree_node *)bh->b_data; | |
9b7b265c | 1060 | if (nilfs_btree_node_get_nchildren(sib) < ncblk) { |
17c76b01 KS |
1061 | path[level].bp_sib_bh = bh; |
1062 | path[level].bp_op = nilfs_btree_carry_left; | |
1063 | stats->bs_nblocks++; | |
1064 | goto out; | |
9b7b265c | 1065 | } else { |
087d01b4 | 1066 | brelse(bh); |
9b7b265c | 1067 | } |
17c76b01 KS |
1068 | } |
1069 | ||
1070 | /* right sibling */ | |
9b7b265c RK |
1071 | if (pindex < nilfs_btree_node_get_nchildren(parent) - 1) { |
1072 | sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1, | |
1073 | ncmax); | |
f198dbb9 | 1074 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
17c76b01 KS |
1075 | if (ret < 0) |
1076 | goto err_out_child_node; | |
1077 | sib = (struct nilfs_btree_node *)bh->b_data; | |
9b7b265c | 1078 | if (nilfs_btree_node_get_nchildren(sib) < ncblk) { |
17c76b01 KS |
1079 | path[level].bp_sib_bh = bh; |
1080 | path[level].bp_op = nilfs_btree_carry_right; | |
1081 | stats->bs_nblocks++; | |
1082 | goto out; | |
9b7b265c | 1083 | } else { |
087d01b4 | 1084 | brelse(bh); |
9b7b265c | 1085 | } |
17c76b01 KS |
1086 | } |
1087 | ||
1088 | /* split */ | |
1089 | path[level].bp_newreq.bpr_ptr = | |
1090 | path[level - 1].bp_newreq.bpr_ptr + 1; | |
e7c274f8 | 1091 | ret = nilfs_bmap_prepare_alloc_ptr(btree, |
2e0c2c73 | 1092 | &path[level].bp_newreq, dat); |
17c76b01 KS |
1093 | if (ret < 0) |
1094 | goto err_out_child_node; | |
f198dbb9 RK |
1095 | ret = nilfs_btree_get_new_block(btree, |
1096 | path[level].bp_newreq.bpr_ptr, | |
1097 | &bh); | |
17c76b01 KS |
1098 | if (ret < 0) |
1099 | goto err_out_curr_node; | |
1100 | ||
1101 | stats->bs_nblocks++; | |
1102 | ||
9b7b265c RK |
1103 | sib = (struct nilfs_btree_node *)bh->b_data; |
1104 | nilfs_btree_node_init(sib, 0, level, 0, ncblk, NULL, NULL); | |
17c76b01 KS |
1105 | path[level].bp_sib_bh = bh; |
1106 | path[level].bp_op = nilfs_btree_split; | |
1107 | } | |
1108 | ||
1109 | /* root */ | |
1110 | node = nilfs_btree_get_root(btree); | |
6d28f7ea | 1111 | if (nilfs_btree_node_get_nchildren(node) < |
ea64ab87 | 1112 | NILFS_BTREE_ROOT_NCHILDREN_MAX) { |
17c76b01 KS |
1113 | path[level].bp_op = nilfs_btree_do_insert; |
1114 | stats->bs_nblocks++; | |
1115 | goto out; | |
1116 | } | |
1117 | ||
1118 | /* grow */ | |
1119 | path[level].bp_newreq.bpr_ptr = path[level - 1].bp_newreq.bpr_ptr + 1; | |
e7c274f8 | 1120 | ret = nilfs_bmap_prepare_alloc_ptr(btree, &path[level].bp_newreq, dat); |
17c76b01 KS |
1121 | if (ret < 0) |
1122 | goto err_out_child_node; | |
f198dbb9 RK |
1123 | ret = nilfs_btree_get_new_block(btree, path[level].bp_newreq.bpr_ptr, |
1124 | &bh); | |
17c76b01 KS |
1125 | if (ret < 0) |
1126 | goto err_out_curr_node; | |
1127 | ||
9b7b265c RK |
1128 | nilfs_btree_node_init((struct nilfs_btree_node *)bh->b_data, |
1129 | 0, level, 0, ncblk, NULL, NULL); | |
17c76b01 KS |
1130 | path[level].bp_sib_bh = bh; |
1131 | path[level].bp_op = nilfs_btree_grow; | |
1132 | ||
1133 | level++; | |
1134 | path[level].bp_op = nilfs_btree_do_insert; | |
1135 | ||
1136 | /* a newly-created node block and a data block are added */ | |
1137 | stats->bs_nblocks += 2; | |
1138 | ||
1139 | /* success */ | |
1140 | out: | |
1141 | *levelp = level; | |
1142 | return ret; | |
1143 | ||
1144 | /* error */ | |
1145 | err_out_curr_node: | |
e7c274f8 | 1146 | nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat); |
17c76b01 KS |
1147 | err_out_child_node: |
1148 | for (level--; level > NILFS_BTREE_LEVEL_DATA; level--) { | |
9f098900 | 1149 | nilfs_btnode_delete(path[level].bp_sib_bh); |
e7c274f8 | 1150 | nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat); |
17c76b01 KS |
1151 | |
1152 | } | |
1153 | ||
e7c274f8 | 1154 | nilfs_bmap_abort_alloc_ptr(btree, &path[level].bp_newreq, dat); |
17c76b01 KS |
1155 | err_out_data: |
1156 | *levelp = level; | |
1157 | stats->bs_nblocks = 0; | |
1158 | return ret; | |
1159 | } | |
1160 | ||
e7c274f8 | 1161 | static void nilfs_btree_commit_insert(struct nilfs_bmap *btree, |
17c76b01 KS |
1162 | struct nilfs_btree_path *path, |
1163 | int maxlevel, __u64 key, __u64 ptr) | |
1164 | { | |
2e0c2c73 | 1165 | struct inode *dat = NULL; |
17c76b01 KS |
1166 | int level; |
1167 | ||
1168 | set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr)); | |
1169 | ptr = path[NILFS_BTREE_LEVEL_DATA].bp_newreq.bpr_ptr; | |
e7c274f8 | 1170 | if (NILFS_BMAP_USE_VBN(btree)) { |
dc935be2 | 1171 | nilfs_bmap_set_target_v(btree, key, ptr); |
e7c274f8 | 1172 | dat = nilfs_bmap_get_dat(btree); |
2e0c2c73 | 1173 | } |
17c76b01 KS |
1174 | |
1175 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) { | |
e7c274f8 | 1176 | nilfs_bmap_commit_alloc_ptr(btree, |
2e0c2c73 | 1177 | &path[level - 1].bp_newreq, dat); |
8acfbf09 | 1178 | path[level].bp_op(btree, path, level, &key, &ptr); |
17c76b01 KS |
1179 | } |
1180 | ||
e7c274f8 RK |
1181 | if (!nilfs_bmap_dirty(btree)) |
1182 | nilfs_bmap_set_dirty(btree); | |
17c76b01 KS |
1183 | } |
1184 | ||
e7c274f8 | 1185 | static int nilfs_btree_insert(struct nilfs_bmap *btree, __u64 key, __u64 ptr) |
17c76b01 | 1186 | { |
17c76b01 KS |
1187 | struct nilfs_btree_path *path; |
1188 | struct nilfs_bmap_stats stats; | |
1189 | int level, ret; | |
1190 | ||
6d28f7ea | 1191 | path = nilfs_btree_alloc_path(); |
17c76b01 KS |
1192 | if (path == NULL) |
1193 | return -ENOMEM; | |
17c76b01 KS |
1194 | |
1195 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, | |
03bdb5ac | 1196 | NILFS_BTREE_LEVEL_NODE_MIN, 0); |
17c76b01 KS |
1197 | if (ret != -ENOENT) { |
1198 | if (ret == 0) | |
1199 | ret = -EEXIST; | |
1200 | goto out; | |
1201 | } | |
1202 | ||
1203 | ret = nilfs_btree_prepare_insert(btree, path, &level, key, ptr, &stats); | |
1204 | if (ret < 0) | |
1205 | goto out; | |
1206 | nilfs_btree_commit_insert(btree, path, level, key, ptr); | |
be667377 | 1207 | nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks); |
17c76b01 KS |
1208 | |
1209 | out: | |
6d28f7ea | 1210 | nilfs_btree_free_path(path); |
17c76b01 KS |
1211 | return ret; |
1212 | } | |
1213 | ||
e7c274f8 | 1214 | static void nilfs_btree_do_delete(struct nilfs_bmap *btree, |
17c76b01 KS |
1215 | struct nilfs_btree_path *path, |
1216 | int level, __u64 *keyp, __u64 *ptrp) | |
1217 | { | |
1218 | struct nilfs_btree_node *node; | |
9b7b265c | 1219 | int ncblk; |
17c76b01 KS |
1220 | |
1221 | if (level < nilfs_btree_height(btree) - 1) { | |
6d28f7ea | 1222 | node = nilfs_btree_get_nonroot_node(path, level); |
9b7b265c RK |
1223 | ncblk = nilfs_btree_nchildren_per_block(btree); |
1224 | nilfs_btree_node_delete(node, path[level].bp_index, | |
1225 | keyp, ptrp, ncblk); | |
17c76b01 | 1226 | if (!buffer_dirty(path[level].bp_bh)) |
5fc7b141 | 1227 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 KS |
1228 | if (path[level].bp_index == 0) |
1229 | nilfs_btree_promote_key(btree, path, level + 1, | |
6d28f7ea | 1230 | nilfs_btree_node_get_key(node, 0)); |
17c76b01 KS |
1231 | } else { |
1232 | node = nilfs_btree_get_root(btree); | |
9b7b265c RK |
1233 | nilfs_btree_node_delete(node, path[level].bp_index, |
1234 | keyp, ptrp, | |
1235 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
17c76b01 KS |
1236 | } |
1237 | } | |
1238 | ||
e7c274f8 | 1239 | static void nilfs_btree_borrow_left(struct nilfs_bmap *btree, |
17c76b01 KS |
1240 | struct nilfs_btree_path *path, |
1241 | int level, __u64 *keyp, __u64 *ptrp) | |
1242 | { | |
1243 | struct nilfs_btree_node *node, *left; | |
9b7b265c | 1244 | int nchildren, lnchildren, n, ncblk; |
17c76b01 KS |
1245 | |
1246 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); | |
1247 | ||
6d28f7ea RK |
1248 | node = nilfs_btree_get_nonroot_node(path, level); |
1249 | left = nilfs_btree_get_sib_node(path, level); | |
1250 | nchildren = nilfs_btree_node_get_nchildren(node); | |
1251 | lnchildren = nilfs_btree_node_get_nchildren(left); | |
9b7b265c | 1252 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 KS |
1253 | |
1254 | n = (nchildren + lnchildren) / 2 - nchildren; | |
1255 | ||
9b7b265c | 1256 | nilfs_btree_node_move_right(left, node, n, ncblk, ncblk); |
17c76b01 KS |
1257 | |
1258 | if (!buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 1259 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 | 1260 | if (!buffer_dirty(path[level].bp_sib_bh)) |
5fc7b141 | 1261 | mark_buffer_dirty(path[level].bp_sib_bh); |
17c76b01 | 1262 | |
17c76b01 | 1263 | nilfs_btree_promote_key(btree, path, level + 1, |
6d28f7ea | 1264 | nilfs_btree_node_get_key(node, 0)); |
17c76b01 | 1265 | |
087d01b4 | 1266 | brelse(path[level].bp_sib_bh); |
17c76b01 KS |
1267 | path[level].bp_sib_bh = NULL; |
1268 | path[level].bp_index += n; | |
1269 | } | |
1270 | ||
e7c274f8 | 1271 | static void nilfs_btree_borrow_right(struct nilfs_bmap *btree, |
17c76b01 KS |
1272 | struct nilfs_btree_path *path, |
1273 | int level, __u64 *keyp, __u64 *ptrp) | |
1274 | { | |
1275 | struct nilfs_btree_node *node, *right; | |
9b7b265c | 1276 | int nchildren, rnchildren, n, ncblk; |
17c76b01 KS |
1277 | |
1278 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); | |
1279 | ||
6d28f7ea RK |
1280 | node = nilfs_btree_get_nonroot_node(path, level); |
1281 | right = nilfs_btree_get_sib_node(path, level); | |
1282 | nchildren = nilfs_btree_node_get_nchildren(node); | |
1283 | rnchildren = nilfs_btree_node_get_nchildren(right); | |
9b7b265c | 1284 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 KS |
1285 | |
1286 | n = (nchildren + rnchildren) / 2 - nchildren; | |
1287 | ||
9b7b265c | 1288 | nilfs_btree_node_move_left(node, right, n, ncblk, ncblk); |
17c76b01 KS |
1289 | |
1290 | if (!buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 1291 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 | 1292 | if (!buffer_dirty(path[level].bp_sib_bh)) |
5fc7b141 | 1293 | mark_buffer_dirty(path[level].bp_sib_bh); |
17c76b01 | 1294 | |
17c76b01 KS |
1295 | path[level + 1].bp_index++; |
1296 | nilfs_btree_promote_key(btree, path, level + 1, | |
6d28f7ea | 1297 | nilfs_btree_node_get_key(right, 0)); |
17c76b01 KS |
1298 | path[level + 1].bp_index--; |
1299 | ||
087d01b4 | 1300 | brelse(path[level].bp_sib_bh); |
17c76b01 KS |
1301 | path[level].bp_sib_bh = NULL; |
1302 | } | |
1303 | ||
e7c274f8 | 1304 | static void nilfs_btree_concat_left(struct nilfs_bmap *btree, |
17c76b01 KS |
1305 | struct nilfs_btree_path *path, |
1306 | int level, __u64 *keyp, __u64 *ptrp) | |
1307 | { | |
1308 | struct nilfs_btree_node *node, *left; | |
9b7b265c | 1309 | int n, ncblk; |
17c76b01 KS |
1310 | |
1311 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); | |
1312 | ||
6d28f7ea RK |
1313 | node = nilfs_btree_get_nonroot_node(path, level); |
1314 | left = nilfs_btree_get_sib_node(path, level); | |
9b7b265c | 1315 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 | 1316 | |
6d28f7ea | 1317 | n = nilfs_btree_node_get_nchildren(node); |
17c76b01 | 1318 | |
9b7b265c | 1319 | nilfs_btree_node_move_left(left, node, n, ncblk, ncblk); |
17c76b01 KS |
1320 | |
1321 | if (!buffer_dirty(path[level].bp_sib_bh)) | |
5fc7b141 | 1322 | mark_buffer_dirty(path[level].bp_sib_bh); |
17c76b01 | 1323 | |
9f098900 | 1324 | nilfs_btnode_delete(path[level].bp_bh); |
17c76b01 KS |
1325 | path[level].bp_bh = path[level].bp_sib_bh; |
1326 | path[level].bp_sib_bh = NULL; | |
6d28f7ea | 1327 | path[level].bp_index += nilfs_btree_node_get_nchildren(left); |
17c76b01 KS |
1328 | } |
1329 | ||
e7c274f8 | 1330 | static void nilfs_btree_concat_right(struct nilfs_bmap *btree, |
17c76b01 KS |
1331 | struct nilfs_btree_path *path, |
1332 | int level, __u64 *keyp, __u64 *ptrp) | |
1333 | { | |
1334 | struct nilfs_btree_node *node, *right; | |
9b7b265c | 1335 | int n, ncblk; |
17c76b01 KS |
1336 | |
1337 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); | |
1338 | ||
6d28f7ea RK |
1339 | node = nilfs_btree_get_nonroot_node(path, level); |
1340 | right = nilfs_btree_get_sib_node(path, level); | |
9b7b265c | 1341 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 | 1342 | |
6d28f7ea | 1343 | n = nilfs_btree_node_get_nchildren(right); |
17c76b01 | 1344 | |
9b7b265c | 1345 | nilfs_btree_node_move_left(node, right, n, ncblk, ncblk); |
17c76b01 KS |
1346 | |
1347 | if (!buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 1348 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 | 1349 | |
9f098900 | 1350 | nilfs_btnode_delete(path[level].bp_sib_bh); |
17c76b01 KS |
1351 | path[level].bp_sib_bh = NULL; |
1352 | path[level + 1].bp_index++; | |
1353 | } | |
1354 | ||
e7c274f8 | 1355 | static void nilfs_btree_shrink(struct nilfs_bmap *btree, |
17c76b01 KS |
1356 | struct nilfs_btree_path *path, |
1357 | int level, __u64 *keyp, __u64 *ptrp) | |
1358 | { | |
1359 | struct nilfs_btree_node *root, *child; | |
9b7b265c | 1360 | int n, ncblk; |
17c76b01 KS |
1361 | |
1362 | nilfs_btree_do_delete(btree, path, level, keyp, ptrp); | |
1363 | ||
17c76b01 | 1364 | root = nilfs_btree_get_root(btree); |
6d28f7ea | 1365 | child = nilfs_btree_get_nonroot_node(path, level); |
9b7b265c | 1366 | ncblk = nilfs_btree_nchildren_per_block(btree); |
17c76b01 | 1367 | |
9b7b265c RK |
1368 | nilfs_btree_node_delete(root, 0, NULL, NULL, |
1369 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
6d28f7ea RK |
1370 | nilfs_btree_node_set_level(root, level); |
1371 | n = nilfs_btree_node_get_nchildren(child); | |
9b7b265c RK |
1372 | nilfs_btree_node_move_left(root, child, n, |
1373 | NILFS_BTREE_ROOT_NCHILDREN_MAX, ncblk); | |
17c76b01 | 1374 | |
9f098900 | 1375 | nilfs_btnode_delete(path[level].bp_bh); |
17c76b01 KS |
1376 | path[level].bp_bh = NULL; |
1377 | } | |
1378 | ||
d4099053 RK |
1379 | static void nilfs_btree_nop(struct nilfs_bmap *btree, |
1380 | struct nilfs_btree_path *path, | |
1381 | int level, __u64 *keyp, __u64 *ptrp) | |
1382 | { | |
1383 | } | |
17c76b01 | 1384 | |
e7c274f8 | 1385 | static int nilfs_btree_prepare_delete(struct nilfs_bmap *btree, |
17c76b01 KS |
1386 | struct nilfs_btree_path *path, |
1387 | int *levelp, | |
2e0c2c73 RK |
1388 | struct nilfs_bmap_stats *stats, |
1389 | struct inode *dat) | |
17c76b01 KS |
1390 | { |
1391 | struct buffer_head *bh; | |
1392 | struct nilfs_btree_node *node, *parent, *sib; | |
1393 | __u64 sibptr; | |
fe744fdb | 1394 | int pindex, dindex, level, ncmin, ncmax, ncblk, ret; |
17c76b01 KS |
1395 | |
1396 | ret = 0; | |
1397 | stats->bs_nblocks = 0; | |
ea64ab87 | 1398 | ncmin = NILFS_BTREE_NODE_NCHILDREN_MIN(nilfs_btree_node_size(btree)); |
9b7b265c | 1399 | ncblk = nilfs_btree_nchildren_per_block(btree); |
ea64ab87 | 1400 | |
fe744fdb | 1401 | for (level = NILFS_BTREE_LEVEL_NODE_MIN, dindex = path[level].bp_index; |
17c76b01 KS |
1402 | level < nilfs_btree_height(btree) - 1; |
1403 | level++) { | |
6d28f7ea | 1404 | node = nilfs_btree_get_nonroot_node(path, level); |
17c76b01 | 1405 | path[level].bp_oldreq.bpr_ptr = |
fe744fdb | 1406 | nilfs_btree_node_get_ptr(node, dindex, ncblk); |
e7c274f8 | 1407 | ret = nilfs_bmap_prepare_end_ptr(btree, |
2e0c2c73 | 1408 | &path[level].bp_oldreq, dat); |
d4b96157 RK |
1409 | if (ret < 0) |
1410 | goto err_out_child_node; | |
17c76b01 | 1411 | |
ea64ab87 | 1412 | if (nilfs_btree_node_get_nchildren(node) > ncmin) { |
17c76b01 KS |
1413 | path[level].bp_op = nilfs_btree_do_delete; |
1414 | stats->bs_nblocks++; | |
1415 | goto out; | |
1416 | } | |
1417 | ||
9b7b265c | 1418 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
17c76b01 | 1419 | pindex = path[level + 1].bp_index; |
fe744fdb | 1420 | dindex = pindex; |
17c76b01 KS |
1421 | |
1422 | if (pindex > 0) { | |
1423 | /* left sibling */ | |
9b7b265c RK |
1424 | sibptr = nilfs_btree_node_get_ptr(parent, pindex - 1, |
1425 | ncmax); | |
f198dbb9 | 1426 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
17c76b01 KS |
1427 | if (ret < 0) |
1428 | goto err_out_curr_node; | |
1429 | sib = (struct nilfs_btree_node *)bh->b_data; | |
ea64ab87 | 1430 | if (nilfs_btree_node_get_nchildren(sib) > ncmin) { |
17c76b01 KS |
1431 | path[level].bp_sib_bh = bh; |
1432 | path[level].bp_op = nilfs_btree_borrow_left; | |
1433 | stats->bs_nblocks++; | |
1434 | goto out; | |
1435 | } else { | |
1436 | path[level].bp_sib_bh = bh; | |
1437 | path[level].bp_op = nilfs_btree_concat_left; | |
1438 | stats->bs_nblocks++; | |
1439 | /* continue; */ | |
1440 | } | |
1441 | } else if (pindex < | |
6d28f7ea | 1442 | nilfs_btree_node_get_nchildren(parent) - 1) { |
17c76b01 | 1443 | /* right sibling */ |
9b7b265c RK |
1444 | sibptr = nilfs_btree_node_get_ptr(parent, pindex + 1, |
1445 | ncmax); | |
f198dbb9 | 1446 | ret = nilfs_btree_get_block(btree, sibptr, &bh); |
17c76b01 KS |
1447 | if (ret < 0) |
1448 | goto err_out_curr_node; | |
1449 | sib = (struct nilfs_btree_node *)bh->b_data; | |
ea64ab87 | 1450 | if (nilfs_btree_node_get_nchildren(sib) > ncmin) { |
17c76b01 KS |
1451 | path[level].bp_sib_bh = bh; |
1452 | path[level].bp_op = nilfs_btree_borrow_right; | |
1453 | stats->bs_nblocks++; | |
1454 | goto out; | |
1455 | } else { | |
1456 | path[level].bp_sib_bh = bh; | |
1457 | path[level].bp_op = nilfs_btree_concat_right; | |
1458 | stats->bs_nblocks++; | |
fe744fdb RK |
1459 | /* |
1460 | * When merging right sibling node | |
1461 | * into the current node, pointer to | |
1462 | * the right sibling node must be | |
1463 | * terminated instead. The adjustment | |
1464 | * below is required for that. | |
1465 | */ | |
1466 | dindex = pindex + 1; | |
17c76b01 KS |
1467 | /* continue; */ |
1468 | } | |
1469 | } else { | |
1470 | /* no siblings */ | |
1471 | /* the only child of the root node */ | |
1f5abe7e | 1472 | WARN_ON(level != nilfs_btree_height(btree) - 2); |
6d28f7ea | 1473 | if (nilfs_btree_node_get_nchildren(node) - 1 <= |
17c76b01 KS |
1474 | NILFS_BTREE_ROOT_NCHILDREN_MAX) { |
1475 | path[level].bp_op = nilfs_btree_shrink; | |
1476 | stats->bs_nblocks += 2; | |
d4099053 RK |
1477 | level++; |
1478 | path[level].bp_op = nilfs_btree_nop; | |
1479 | goto shrink_root_child; | |
17c76b01 KS |
1480 | } else { |
1481 | path[level].bp_op = nilfs_btree_do_delete; | |
1482 | stats->bs_nblocks++; | |
d4099053 | 1483 | goto out; |
17c76b01 | 1484 | } |
17c76b01 KS |
1485 | } |
1486 | } | |
1487 | ||
d4099053 RK |
1488 | /* child of the root node is deleted */ |
1489 | path[level].bp_op = nilfs_btree_do_delete; | |
1490 | stats->bs_nblocks++; | |
1491 | ||
1492 | shrink_root_child: | |
17c76b01 KS |
1493 | node = nilfs_btree_get_root(btree); |
1494 | path[level].bp_oldreq.bpr_ptr = | |
fe744fdb | 1495 | nilfs_btree_node_get_ptr(node, dindex, |
9b7b265c | 1496 | NILFS_BTREE_ROOT_NCHILDREN_MAX); |
d4b96157 | 1497 | |
e7c274f8 | 1498 | ret = nilfs_bmap_prepare_end_ptr(btree, &path[level].bp_oldreq, dat); |
d4b96157 RK |
1499 | if (ret < 0) |
1500 | goto err_out_child_node; | |
1501 | ||
17c76b01 KS |
1502 | /* success */ |
1503 | out: | |
1504 | *levelp = level; | |
1505 | return ret; | |
1506 | ||
1507 | /* error */ | |
1508 | err_out_curr_node: | |
e7c274f8 | 1509 | nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat); |
17c76b01 KS |
1510 | err_out_child_node: |
1511 | for (level--; level >= NILFS_BTREE_LEVEL_NODE_MIN; level--) { | |
087d01b4 | 1512 | brelse(path[level].bp_sib_bh); |
e7c274f8 | 1513 | nilfs_bmap_abort_end_ptr(btree, &path[level].bp_oldreq, dat); |
17c76b01 KS |
1514 | } |
1515 | *levelp = level; | |
1516 | stats->bs_nblocks = 0; | |
1517 | return ret; | |
1518 | } | |
1519 | ||
e7c274f8 | 1520 | static void nilfs_btree_commit_delete(struct nilfs_bmap *btree, |
17c76b01 | 1521 | struct nilfs_btree_path *path, |
2e0c2c73 | 1522 | int maxlevel, struct inode *dat) |
17c76b01 KS |
1523 | { |
1524 | int level; | |
1525 | ||
1526 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; level <= maxlevel; level++) { | |
e7c274f8 | 1527 | nilfs_bmap_commit_end_ptr(btree, &path[level].bp_oldreq, dat); |
8acfbf09 | 1528 | path[level].bp_op(btree, path, level, NULL, NULL); |
17c76b01 KS |
1529 | } |
1530 | ||
e7c274f8 RK |
1531 | if (!nilfs_bmap_dirty(btree)) |
1532 | nilfs_bmap_set_dirty(btree); | |
17c76b01 KS |
1533 | } |
1534 | ||
e7c274f8 | 1535 | static int nilfs_btree_delete(struct nilfs_bmap *btree, __u64 key) |
17c76b01 KS |
1536 | |
1537 | { | |
17c76b01 KS |
1538 | struct nilfs_btree_path *path; |
1539 | struct nilfs_bmap_stats stats; | |
2e0c2c73 | 1540 | struct inode *dat; |
17c76b01 KS |
1541 | int level, ret; |
1542 | ||
6d28f7ea | 1543 | path = nilfs_btree_alloc_path(); |
17c76b01 KS |
1544 | if (path == NULL) |
1545 | return -ENOMEM; | |
f905440f | 1546 | |
17c76b01 | 1547 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, |
03bdb5ac | 1548 | NILFS_BTREE_LEVEL_NODE_MIN, 0); |
17c76b01 KS |
1549 | if (ret < 0) |
1550 | goto out; | |
1551 | ||
2e0c2c73 | 1552 | |
e7c274f8 | 1553 | dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL; |
2e0c2c73 RK |
1554 | |
1555 | ret = nilfs_btree_prepare_delete(btree, path, &level, &stats, dat); | |
17c76b01 KS |
1556 | if (ret < 0) |
1557 | goto out; | |
2e0c2c73 | 1558 | nilfs_btree_commit_delete(btree, path, level, dat); |
be667377 | 1559 | nilfs_inode_sub_blocks(btree->b_inode, stats.bs_nblocks); |
17c76b01 KS |
1560 | |
1561 | out: | |
6d28f7ea | 1562 | nilfs_btree_free_path(path); |
17c76b01 KS |
1563 | return ret; |
1564 | } | |
1565 | ||
e7c274f8 | 1566 | static int nilfs_btree_last_key(const struct nilfs_bmap *btree, __u64 *keyp) |
17c76b01 | 1567 | { |
17c76b01 KS |
1568 | struct nilfs_btree_path *path; |
1569 | int ret; | |
1570 | ||
6d28f7ea | 1571 | path = nilfs_btree_alloc_path(); |
17c76b01 KS |
1572 | if (path == NULL) |
1573 | return -ENOMEM; | |
17c76b01 KS |
1574 | |
1575 | ret = nilfs_btree_do_lookup_last(btree, path, keyp, NULL); | |
1576 | ||
6d28f7ea | 1577 | nilfs_btree_free_path(path); |
17c76b01 KS |
1578 | |
1579 | return ret; | |
1580 | } | |
1581 | ||
e7c274f8 | 1582 | static int nilfs_btree_check_delete(struct nilfs_bmap *btree, __u64 key) |
17c76b01 KS |
1583 | { |
1584 | struct buffer_head *bh; | |
17c76b01 KS |
1585 | struct nilfs_btree_node *root, *node; |
1586 | __u64 maxkey, nextmaxkey; | |
1587 | __u64 ptr; | |
1588 | int nchildren, ret; | |
1589 | ||
17c76b01 KS |
1590 | root = nilfs_btree_get_root(btree); |
1591 | switch (nilfs_btree_height(btree)) { | |
1592 | case 2: | |
1593 | bh = NULL; | |
1594 | node = root; | |
1595 | break; | |
1596 | case 3: | |
6d28f7ea | 1597 | nchildren = nilfs_btree_node_get_nchildren(root); |
17c76b01 KS |
1598 | if (nchildren > 1) |
1599 | return 0; | |
9b7b265c RK |
1600 | ptr = nilfs_btree_node_get_ptr(root, nchildren - 1, |
1601 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
f198dbb9 | 1602 | ret = nilfs_btree_get_block(btree, ptr, &bh); |
17c76b01 KS |
1603 | if (ret < 0) |
1604 | return ret; | |
1605 | node = (struct nilfs_btree_node *)bh->b_data; | |
1606 | break; | |
1607 | default: | |
1608 | return 0; | |
1609 | } | |
1610 | ||
6d28f7ea RK |
1611 | nchildren = nilfs_btree_node_get_nchildren(node); |
1612 | maxkey = nilfs_btree_node_get_key(node, nchildren - 1); | |
17c76b01 | 1613 | nextmaxkey = (nchildren > 1) ? |
6d28f7ea | 1614 | nilfs_btree_node_get_key(node, nchildren - 2) : 0; |
17c76b01 | 1615 | if (bh != NULL) |
087d01b4 | 1616 | brelse(bh); |
17c76b01 | 1617 | |
3033342a | 1618 | return (maxkey == key) && (nextmaxkey < NILFS_BMAP_LARGE_LOW); |
17c76b01 KS |
1619 | } |
1620 | ||
e7c274f8 | 1621 | static int nilfs_btree_gather_data(struct nilfs_bmap *btree, |
17c76b01 KS |
1622 | __u64 *keys, __u64 *ptrs, int nitems) |
1623 | { | |
1624 | struct buffer_head *bh; | |
17c76b01 KS |
1625 | struct nilfs_btree_node *node, *root; |
1626 | __le64 *dkeys; | |
1627 | __le64 *dptrs; | |
1628 | __u64 ptr; | |
9b7b265c | 1629 | int nchildren, ncmax, i, ret; |
17c76b01 | 1630 | |
17c76b01 KS |
1631 | root = nilfs_btree_get_root(btree); |
1632 | switch (nilfs_btree_height(btree)) { | |
1633 | case 2: | |
1634 | bh = NULL; | |
1635 | node = root; | |
9b7b265c | 1636 | ncmax = NILFS_BTREE_ROOT_NCHILDREN_MAX; |
17c76b01 KS |
1637 | break; |
1638 | case 3: | |
6d28f7ea | 1639 | nchildren = nilfs_btree_node_get_nchildren(root); |
1f5abe7e | 1640 | WARN_ON(nchildren > 1); |
9b7b265c RK |
1641 | ptr = nilfs_btree_node_get_ptr(root, nchildren - 1, |
1642 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
f198dbb9 | 1643 | ret = nilfs_btree_get_block(btree, ptr, &bh); |
17c76b01 KS |
1644 | if (ret < 0) |
1645 | return ret; | |
1646 | node = (struct nilfs_btree_node *)bh->b_data; | |
9b7b265c | 1647 | ncmax = nilfs_btree_nchildren_per_block(btree); |
17c76b01 KS |
1648 | break; |
1649 | default: | |
1650 | node = NULL; | |
1f5abe7e | 1651 | return -EINVAL; |
17c76b01 KS |
1652 | } |
1653 | ||
6d28f7ea | 1654 | nchildren = nilfs_btree_node_get_nchildren(node); |
17c76b01 KS |
1655 | if (nchildren < nitems) |
1656 | nitems = nchildren; | |
6d28f7ea | 1657 | dkeys = nilfs_btree_node_dkeys(node); |
9b7b265c | 1658 | dptrs = nilfs_btree_node_dptrs(node, ncmax); |
17c76b01 | 1659 | for (i = 0; i < nitems; i++) { |
25b8d7de RK |
1660 | keys[i] = le64_to_cpu(dkeys[i]); |
1661 | ptrs[i] = le64_to_cpu(dptrs[i]); | |
17c76b01 KS |
1662 | } |
1663 | ||
1664 | if (bh != NULL) | |
087d01b4 | 1665 | brelse(bh); |
17c76b01 KS |
1666 | |
1667 | return nitems; | |
1668 | } | |
1669 | ||
1670 | static int | |
e7c274f8 | 1671 | nilfs_btree_prepare_convert_and_insert(struct nilfs_bmap *btree, __u64 key, |
17c76b01 KS |
1672 | union nilfs_bmap_ptr_req *dreq, |
1673 | union nilfs_bmap_ptr_req *nreq, | |
1674 | struct buffer_head **bhp, | |
1675 | struct nilfs_bmap_stats *stats) | |
1676 | { | |
1677 | struct buffer_head *bh; | |
2e0c2c73 | 1678 | struct inode *dat = NULL; |
17c76b01 KS |
1679 | int ret; |
1680 | ||
17c76b01 KS |
1681 | stats->bs_nblocks = 0; |
1682 | ||
1683 | /* for data */ | |
1684 | /* cannot find near ptr */ | |
e7c274f8 | 1685 | if (NILFS_BMAP_USE_VBN(btree)) { |
7cde31d7 | 1686 | dreq->bpr_ptr = nilfs_btree_find_target_v(btree, NULL, key); |
e7c274f8 | 1687 | dat = nilfs_bmap_get_dat(btree); |
2e0c2c73 | 1688 | } |
7cde31d7 | 1689 | |
e7c274f8 | 1690 | ret = nilfs_bmap_prepare_alloc_ptr(btree, dreq, dat); |
17c76b01 KS |
1691 | if (ret < 0) |
1692 | return ret; | |
1693 | ||
1694 | *bhp = NULL; | |
1695 | stats->bs_nblocks++; | |
1696 | if (nreq != NULL) { | |
1697 | nreq->bpr_ptr = dreq->bpr_ptr + 1; | |
e7c274f8 | 1698 | ret = nilfs_bmap_prepare_alloc_ptr(btree, nreq, dat); |
17c76b01 KS |
1699 | if (ret < 0) |
1700 | goto err_out_dreq; | |
1701 | ||
f198dbb9 | 1702 | ret = nilfs_btree_get_new_block(btree, nreq->bpr_ptr, &bh); |
17c76b01 KS |
1703 | if (ret < 0) |
1704 | goto err_out_nreq; | |
1705 | ||
1706 | *bhp = bh; | |
1707 | stats->bs_nblocks++; | |
1708 | } | |
1709 | ||
1710 | /* success */ | |
1711 | return 0; | |
1712 | ||
1713 | /* error */ | |
1714 | err_out_nreq: | |
e7c274f8 | 1715 | nilfs_bmap_abort_alloc_ptr(btree, nreq, dat); |
17c76b01 | 1716 | err_out_dreq: |
e7c274f8 | 1717 | nilfs_bmap_abort_alloc_ptr(btree, dreq, dat); |
17c76b01 KS |
1718 | stats->bs_nblocks = 0; |
1719 | return ret; | |
1720 | ||
1721 | } | |
1722 | ||
1723 | static void | |
e7c274f8 | 1724 | nilfs_btree_commit_convert_and_insert(struct nilfs_bmap *btree, |
17c76b01 KS |
1725 | __u64 key, __u64 ptr, |
1726 | const __u64 *keys, const __u64 *ptrs, | |
3033342a | 1727 | int n, |
17c76b01 KS |
1728 | union nilfs_bmap_ptr_req *dreq, |
1729 | union nilfs_bmap_ptr_req *nreq, | |
1730 | struct buffer_head *bh) | |
1731 | { | |
17c76b01 | 1732 | struct nilfs_btree_node *node; |
2e0c2c73 | 1733 | struct inode *dat; |
17c76b01 | 1734 | __u64 tmpptr; |
9b7b265c | 1735 | int ncblk; |
17c76b01 KS |
1736 | |
1737 | /* free resources */ | |
e7c274f8 RK |
1738 | if (btree->b_ops->bop_clear != NULL) |
1739 | btree->b_ops->bop_clear(btree); | |
17c76b01 KS |
1740 | |
1741 | /* ptr must be a pointer to a buffer head. */ | |
1742 | set_buffer_nilfs_volatile((struct buffer_head *)((unsigned long)ptr)); | |
1743 | ||
1744 | /* convert and insert */ | |
e7c274f8 | 1745 | dat = NILFS_BMAP_USE_VBN(btree) ? nilfs_bmap_get_dat(btree) : NULL; |
b6b14e98 | 1746 | __nilfs_btree_init(btree); |
17c76b01 | 1747 | if (nreq != NULL) { |
e7c274f8 RK |
1748 | nilfs_bmap_commit_alloc_ptr(btree, dreq, dat); |
1749 | nilfs_bmap_commit_alloc_ptr(btree, nreq, dat); | |
17c76b01 KS |
1750 | |
1751 | /* create child node at level 1 */ | |
17c76b01 | 1752 | node = (struct nilfs_btree_node *)bh->b_data; |
9b7b265c RK |
1753 | ncblk = nilfs_btree_nchildren_per_block(btree); |
1754 | nilfs_btree_node_init(node, 0, 1, n, ncblk, keys, ptrs); | |
1755 | nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, ncblk); | |
17c76b01 | 1756 | if (!buffer_dirty(bh)) |
5fc7b141 | 1757 | mark_buffer_dirty(bh); |
e7c274f8 RK |
1758 | if (!nilfs_bmap_dirty(btree)) |
1759 | nilfs_bmap_set_dirty(btree); | |
17c76b01 | 1760 | |
087d01b4 | 1761 | brelse(bh); |
17c76b01 KS |
1762 | |
1763 | /* create root node at level 2 */ | |
1764 | node = nilfs_btree_get_root(btree); | |
1765 | tmpptr = nreq->bpr_ptr; | |
9b7b265c RK |
1766 | nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 2, 1, |
1767 | NILFS_BTREE_ROOT_NCHILDREN_MAX, | |
1768 | &keys[0], &tmpptr); | |
17c76b01 | 1769 | } else { |
e7c274f8 | 1770 | nilfs_bmap_commit_alloc_ptr(btree, dreq, dat); |
17c76b01 KS |
1771 | |
1772 | /* create root node at level 1 */ | |
1773 | node = nilfs_btree_get_root(btree); | |
9b7b265c RK |
1774 | nilfs_btree_node_init(node, NILFS_BTREE_NODE_ROOT, 1, n, |
1775 | NILFS_BTREE_ROOT_NCHILDREN_MAX, | |
1776 | keys, ptrs); | |
1777 | nilfs_btree_node_insert(node, n, key, dreq->bpr_ptr, | |
1778 | NILFS_BTREE_ROOT_NCHILDREN_MAX); | |
e7c274f8 RK |
1779 | if (!nilfs_bmap_dirty(btree)) |
1780 | nilfs_bmap_set_dirty(btree); | |
17c76b01 KS |
1781 | } |
1782 | ||
e7c274f8 | 1783 | if (NILFS_BMAP_USE_VBN(btree)) |
dc935be2 | 1784 | nilfs_bmap_set_target_v(btree, key, dreq->bpr_ptr); |
17c76b01 KS |
1785 | } |
1786 | ||
1787 | /** | |
1788 | * nilfs_btree_convert_and_insert - | |
1789 | * @bmap: | |
1790 | * @key: | |
1791 | * @ptr: | |
1792 | * @keys: | |
1793 | * @ptrs: | |
1794 | * @n: | |
17c76b01 | 1795 | */ |
e7c274f8 | 1796 | int nilfs_btree_convert_and_insert(struct nilfs_bmap *btree, |
17c76b01 | 1797 | __u64 key, __u64 ptr, |
3033342a | 1798 | const __u64 *keys, const __u64 *ptrs, int n) |
17c76b01 KS |
1799 | { |
1800 | struct buffer_head *bh; | |
1801 | union nilfs_bmap_ptr_req dreq, nreq, *di, *ni; | |
1802 | struct nilfs_bmap_stats stats; | |
1803 | int ret; | |
1804 | ||
1805 | if (n + 1 <= NILFS_BTREE_ROOT_NCHILDREN_MAX) { | |
1806 | di = &dreq; | |
1807 | ni = NULL; | |
1808 | } else if ((n + 1) <= NILFS_BTREE_NODE_NCHILDREN_MAX( | |
e7c274f8 | 1809 | 1 << btree->b_inode->i_blkbits)) { |
17c76b01 KS |
1810 | di = &dreq; |
1811 | ni = &nreq; | |
1812 | } else { | |
1813 | di = NULL; | |
1814 | ni = NULL; | |
1815 | BUG(); | |
1816 | } | |
1817 | ||
e7c274f8 | 1818 | ret = nilfs_btree_prepare_convert_and_insert(btree, key, di, ni, &bh, |
17c76b01 KS |
1819 | &stats); |
1820 | if (ret < 0) | |
1821 | return ret; | |
e7c274f8 | 1822 | nilfs_btree_commit_convert_and_insert(btree, key, ptr, keys, ptrs, n, |
3033342a | 1823 | di, ni, bh); |
be667377 | 1824 | nilfs_inode_add_blocks(btree->b_inode, stats.bs_nblocks); |
17c76b01 KS |
1825 | return 0; |
1826 | } | |
1827 | ||
e7c274f8 | 1828 | static int nilfs_btree_propagate_p(struct nilfs_bmap *btree, |
17c76b01 KS |
1829 | struct nilfs_btree_path *path, |
1830 | int level, | |
1831 | struct buffer_head *bh) | |
1832 | { | |
1833 | while ((++level < nilfs_btree_height(btree) - 1) && | |
1834 | !buffer_dirty(path[level].bp_bh)) | |
5fc7b141 | 1835 | mark_buffer_dirty(path[level].bp_bh); |
17c76b01 KS |
1836 | |
1837 | return 0; | |
1838 | } | |
1839 | ||
e7c274f8 | 1840 | static int nilfs_btree_prepare_update_v(struct nilfs_bmap *btree, |
17c76b01 | 1841 | struct nilfs_btree_path *path, |
2e0c2c73 | 1842 | int level, struct inode *dat) |
17c76b01 KS |
1843 | { |
1844 | struct nilfs_btree_node *parent; | |
9b7b265c | 1845 | int ncmax, ret; |
17c76b01 | 1846 | |
9b7b265c | 1847 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
17c76b01 | 1848 | path[level].bp_oldreq.bpr_ptr = |
9b7b265c RK |
1849 | nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index, |
1850 | ncmax); | |
17c76b01 | 1851 | path[level].bp_newreq.bpr_ptr = path[level].bp_oldreq.bpr_ptr + 1; |
2e0c2c73 RK |
1852 | ret = nilfs_dat_prepare_update(dat, &path[level].bp_oldreq.bpr_req, |
1853 | &path[level].bp_newreq.bpr_req); | |
17c76b01 KS |
1854 | if (ret < 0) |
1855 | return ret; | |
1856 | ||
1857 | if (buffer_nilfs_node(path[level].bp_bh)) { | |
1858 | path[level].bp_ctxt.oldkey = path[level].bp_oldreq.bpr_ptr; | |
1859 | path[level].bp_ctxt.newkey = path[level].bp_newreq.bpr_ptr; | |
1860 | path[level].bp_ctxt.bh = path[level].bp_bh; | |
1861 | ret = nilfs_btnode_prepare_change_key( | |
e7c274f8 | 1862 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
17c76b01 KS |
1863 | &path[level].bp_ctxt); |
1864 | if (ret < 0) { | |
2e0c2c73 RK |
1865 | nilfs_dat_abort_update(dat, |
1866 | &path[level].bp_oldreq.bpr_req, | |
1867 | &path[level].bp_newreq.bpr_req); | |
17c76b01 KS |
1868 | return ret; |
1869 | } | |
1870 | } | |
1871 | ||
1872 | return 0; | |
1873 | } | |
1874 | ||
e7c274f8 | 1875 | static void nilfs_btree_commit_update_v(struct nilfs_bmap *btree, |
17c76b01 | 1876 | struct nilfs_btree_path *path, |
2e0c2c73 | 1877 | int level, struct inode *dat) |
17c76b01 KS |
1878 | { |
1879 | struct nilfs_btree_node *parent; | |
9b7b265c | 1880 | int ncmax; |
17c76b01 | 1881 | |
2e0c2c73 RK |
1882 | nilfs_dat_commit_update(dat, &path[level].bp_oldreq.bpr_req, |
1883 | &path[level].bp_newreq.bpr_req, | |
e7c274f8 | 1884 | btree->b_ptr_type == NILFS_BMAP_PTR_VS); |
17c76b01 KS |
1885 | |
1886 | if (buffer_nilfs_node(path[level].bp_bh)) { | |
1887 | nilfs_btnode_commit_change_key( | |
e7c274f8 | 1888 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
17c76b01 KS |
1889 | &path[level].bp_ctxt); |
1890 | path[level].bp_bh = path[level].bp_ctxt.bh; | |
1891 | } | |
1892 | set_buffer_nilfs_volatile(path[level].bp_bh); | |
1893 | ||
9b7b265c RK |
1894 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
1895 | nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, | |
1896 | path[level].bp_newreq.bpr_ptr, ncmax); | |
17c76b01 KS |
1897 | } |
1898 | ||
e7c274f8 | 1899 | static void nilfs_btree_abort_update_v(struct nilfs_bmap *btree, |
17c76b01 | 1900 | struct nilfs_btree_path *path, |
2e0c2c73 | 1901 | int level, struct inode *dat) |
17c76b01 | 1902 | { |
2e0c2c73 RK |
1903 | nilfs_dat_abort_update(dat, &path[level].bp_oldreq.bpr_req, |
1904 | &path[level].bp_newreq.bpr_req); | |
17c76b01 KS |
1905 | if (buffer_nilfs_node(path[level].bp_bh)) |
1906 | nilfs_btnode_abort_change_key( | |
e7c274f8 | 1907 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
17c76b01 KS |
1908 | &path[level].bp_ctxt); |
1909 | } | |
1910 | ||
e7c274f8 | 1911 | static int nilfs_btree_prepare_propagate_v(struct nilfs_bmap *btree, |
17c76b01 | 1912 | struct nilfs_btree_path *path, |
2e0c2c73 RK |
1913 | int minlevel, int *maxlevelp, |
1914 | struct inode *dat) | |
17c76b01 KS |
1915 | { |
1916 | int level, ret; | |
1917 | ||
1918 | level = minlevel; | |
1919 | if (!buffer_nilfs_volatile(path[level].bp_bh)) { | |
2e0c2c73 | 1920 | ret = nilfs_btree_prepare_update_v(btree, path, level, dat); |
17c76b01 KS |
1921 | if (ret < 0) |
1922 | return ret; | |
1923 | } | |
1924 | while ((++level < nilfs_btree_height(btree) - 1) && | |
1925 | !buffer_dirty(path[level].bp_bh)) { | |
1926 | ||
1f5abe7e | 1927 | WARN_ON(buffer_nilfs_volatile(path[level].bp_bh)); |
2e0c2c73 | 1928 | ret = nilfs_btree_prepare_update_v(btree, path, level, dat); |
17c76b01 KS |
1929 | if (ret < 0) |
1930 | goto out; | |
1931 | } | |
1932 | ||
1933 | /* success */ | |
17c76b01 KS |
1934 | *maxlevelp = level - 1; |
1935 | return 0; | |
1936 | ||
1937 | /* error */ | |
1938 | out: | |
1939 | while (--level > minlevel) | |
2e0c2c73 | 1940 | nilfs_btree_abort_update_v(btree, path, level, dat); |
17c76b01 | 1941 | if (!buffer_nilfs_volatile(path[level].bp_bh)) |
2e0c2c73 | 1942 | nilfs_btree_abort_update_v(btree, path, level, dat); |
17c76b01 KS |
1943 | return ret; |
1944 | } | |
1945 | ||
e7c274f8 | 1946 | static void nilfs_btree_commit_propagate_v(struct nilfs_bmap *btree, |
17c76b01 | 1947 | struct nilfs_btree_path *path, |
2e0c2c73 RK |
1948 | int minlevel, int maxlevel, |
1949 | struct buffer_head *bh, | |
1950 | struct inode *dat) | |
17c76b01 KS |
1951 | { |
1952 | int level; | |
1953 | ||
1954 | if (!buffer_nilfs_volatile(path[minlevel].bp_bh)) | |
2e0c2c73 | 1955 | nilfs_btree_commit_update_v(btree, path, minlevel, dat); |
17c76b01 KS |
1956 | |
1957 | for (level = minlevel + 1; level <= maxlevel; level++) | |
2e0c2c73 | 1958 | nilfs_btree_commit_update_v(btree, path, level, dat); |
17c76b01 KS |
1959 | } |
1960 | ||
e7c274f8 | 1961 | static int nilfs_btree_propagate_v(struct nilfs_bmap *btree, |
17c76b01 | 1962 | struct nilfs_btree_path *path, |
2e0c2c73 | 1963 | int level, struct buffer_head *bh) |
17c76b01 | 1964 | { |
308f4419 | 1965 | int maxlevel = 0, ret; |
17c76b01 | 1966 | struct nilfs_btree_node *parent; |
e7c274f8 | 1967 | struct inode *dat = nilfs_bmap_get_dat(btree); |
17c76b01 | 1968 | __u64 ptr; |
9b7b265c | 1969 | int ncmax; |
17c76b01 KS |
1970 | |
1971 | get_bh(bh); | |
1972 | path[level].bp_bh = bh; | |
2e0c2c73 RK |
1973 | ret = nilfs_btree_prepare_propagate_v(btree, path, level, &maxlevel, |
1974 | dat); | |
17c76b01 KS |
1975 | if (ret < 0) |
1976 | goto out; | |
1977 | ||
1978 | if (buffer_nilfs_volatile(path[level].bp_bh)) { | |
9b7b265c RK |
1979 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
1980 | ptr = nilfs_btree_node_get_ptr(parent, | |
1981 | path[level + 1].bp_index, | |
1982 | ncmax); | |
2e0c2c73 | 1983 | ret = nilfs_dat_mark_dirty(dat, ptr); |
17c76b01 KS |
1984 | if (ret < 0) |
1985 | goto out; | |
1986 | } | |
1987 | ||
2e0c2c73 | 1988 | nilfs_btree_commit_propagate_v(btree, path, level, maxlevel, bh, dat); |
17c76b01 KS |
1989 | |
1990 | out: | |
1991 | brelse(path[level].bp_bh); | |
1992 | path[level].bp_bh = NULL; | |
1993 | return ret; | |
1994 | } | |
1995 | ||
e7c274f8 | 1996 | static int nilfs_btree_propagate(struct nilfs_bmap *btree, |
17c76b01 KS |
1997 | struct buffer_head *bh) |
1998 | { | |
17c76b01 KS |
1999 | struct nilfs_btree_path *path; |
2000 | struct nilfs_btree_node *node; | |
2001 | __u64 key; | |
2002 | int level, ret; | |
2003 | ||
1f5abe7e | 2004 | WARN_ON(!buffer_dirty(bh)); |
17c76b01 | 2005 | |
6d28f7ea | 2006 | path = nilfs_btree_alloc_path(); |
17c76b01 KS |
2007 | if (path == NULL) |
2008 | return -ENOMEM; | |
17c76b01 KS |
2009 | |
2010 | if (buffer_nilfs_node(bh)) { | |
2011 | node = (struct nilfs_btree_node *)bh->b_data; | |
6d28f7ea RK |
2012 | key = nilfs_btree_node_get_key(node, 0); |
2013 | level = nilfs_btree_node_get_level(node); | |
17c76b01 | 2014 | } else { |
e7c274f8 | 2015 | key = nilfs_bmap_data_get_key(btree, bh); |
17c76b01 KS |
2016 | level = NILFS_BTREE_LEVEL_DATA; |
2017 | } | |
2018 | ||
03bdb5ac | 2019 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0); |
17c76b01 | 2020 | if (ret < 0) { |
1f5abe7e | 2021 | if (unlikely(ret == -ENOENT)) |
17c76b01 KS |
2022 | printk(KERN_CRIT "%s: key = %llu, level == %d\n", |
2023 | __func__, (unsigned long long)key, level); | |
17c76b01 KS |
2024 | goto out; |
2025 | } | |
2026 | ||
e7c274f8 | 2027 | ret = NILFS_BMAP_USE_VBN(btree) ? |
7cde31d7 RK |
2028 | nilfs_btree_propagate_v(btree, path, level, bh) : |
2029 | nilfs_btree_propagate_p(btree, path, level, bh); | |
17c76b01 KS |
2030 | |
2031 | out: | |
6d28f7ea | 2032 | nilfs_btree_free_path(path); |
17c76b01 KS |
2033 | |
2034 | return ret; | |
2035 | } | |
2036 | ||
e7c274f8 | 2037 | static int nilfs_btree_propagate_gc(struct nilfs_bmap *btree, |
17c76b01 KS |
2038 | struct buffer_head *bh) |
2039 | { | |
e7c274f8 | 2040 | return nilfs_dat_mark_dirty(nilfs_bmap_get_dat(btree), bh->b_blocknr); |
17c76b01 KS |
2041 | } |
2042 | ||
e7c274f8 | 2043 | static void nilfs_btree_add_dirty_buffer(struct nilfs_bmap *btree, |
17c76b01 KS |
2044 | struct list_head *lists, |
2045 | struct buffer_head *bh) | |
2046 | { | |
2047 | struct list_head *head; | |
2048 | struct buffer_head *cbh; | |
2049 | struct nilfs_btree_node *node, *cnode; | |
2050 | __u64 key, ckey; | |
2051 | int level; | |
2052 | ||
2053 | get_bh(bh); | |
2054 | node = (struct nilfs_btree_node *)bh->b_data; | |
6d28f7ea RK |
2055 | key = nilfs_btree_node_get_key(node, 0); |
2056 | level = nilfs_btree_node_get_level(node); | |
cfa913a5 RK |
2057 | if (level < NILFS_BTREE_LEVEL_NODE_MIN || |
2058 | level >= NILFS_BTREE_LEVEL_MAX) { | |
2059 | dump_stack(); | |
2060 | printk(KERN_WARNING | |
2061 | "%s: invalid btree level: %d (key=%llu, ino=%lu, " | |
2062 | "blocknr=%llu)\n", | |
2063 | __func__, level, (unsigned long long)key, | |
e7c274f8 | 2064 | NILFS_BMAP_I(btree)->vfs_inode.i_ino, |
cfa913a5 RK |
2065 | (unsigned long long)bh->b_blocknr); |
2066 | return; | |
2067 | } | |
2068 | ||
17c76b01 KS |
2069 | list_for_each(head, &lists[level]) { |
2070 | cbh = list_entry(head, struct buffer_head, b_assoc_buffers); | |
2071 | cnode = (struct nilfs_btree_node *)cbh->b_data; | |
6d28f7ea | 2072 | ckey = nilfs_btree_node_get_key(cnode, 0); |
17c76b01 KS |
2073 | if (key < ckey) |
2074 | break; | |
2075 | } | |
2076 | list_add_tail(&bh->b_assoc_buffers, head); | |
2077 | } | |
2078 | ||
e7c274f8 | 2079 | static void nilfs_btree_lookup_dirty_buffers(struct nilfs_bmap *btree, |
17c76b01 KS |
2080 | struct list_head *listp) |
2081 | { | |
e7c274f8 | 2082 | struct address_space *btcache = &NILFS_BMAP_I(btree)->i_btnode_cache; |
17c76b01 KS |
2083 | struct list_head lists[NILFS_BTREE_LEVEL_MAX]; |
2084 | struct pagevec pvec; | |
2085 | struct buffer_head *bh, *head; | |
2086 | pgoff_t index = 0; | |
2087 | int level, i; | |
2088 | ||
2089 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; | |
2090 | level < NILFS_BTREE_LEVEL_MAX; | |
2091 | level++) | |
2092 | INIT_LIST_HEAD(&lists[level]); | |
2093 | ||
2094 | pagevec_init(&pvec, 0); | |
2095 | ||
2096 | while (pagevec_lookup_tag(&pvec, btcache, &index, PAGECACHE_TAG_DIRTY, | |
2097 | PAGEVEC_SIZE)) { | |
2098 | for (i = 0; i < pagevec_count(&pvec); i++) { | |
2099 | bh = head = page_buffers(pvec.pages[i]); | |
2100 | do { | |
2101 | if (buffer_dirty(bh)) | |
2102 | nilfs_btree_add_dirty_buffer(btree, | |
2103 | lists, bh); | |
2104 | } while ((bh = bh->b_this_page) != head); | |
2105 | } | |
2106 | pagevec_release(&pvec); | |
2107 | cond_resched(); | |
2108 | } | |
2109 | ||
2110 | for (level = NILFS_BTREE_LEVEL_NODE_MIN; | |
2111 | level < NILFS_BTREE_LEVEL_MAX; | |
2112 | level++) | |
0935db74 | 2113 | list_splice_tail(&lists[level], listp); |
17c76b01 KS |
2114 | } |
2115 | ||
e7c274f8 | 2116 | static int nilfs_btree_assign_p(struct nilfs_bmap *btree, |
17c76b01 KS |
2117 | struct nilfs_btree_path *path, |
2118 | int level, | |
2119 | struct buffer_head **bh, | |
2120 | sector_t blocknr, | |
2121 | union nilfs_binfo *binfo) | |
2122 | { | |
2123 | struct nilfs_btree_node *parent; | |
2124 | __u64 key; | |
2125 | __u64 ptr; | |
9b7b265c | 2126 | int ncmax, ret; |
17c76b01 | 2127 | |
9b7b265c RK |
2128 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
2129 | ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index, | |
2130 | ncmax); | |
17c76b01 KS |
2131 | if (buffer_nilfs_node(*bh)) { |
2132 | path[level].bp_ctxt.oldkey = ptr; | |
2133 | path[level].bp_ctxt.newkey = blocknr; | |
2134 | path[level].bp_ctxt.bh = *bh; | |
2135 | ret = nilfs_btnode_prepare_change_key( | |
e7c274f8 | 2136 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
17c76b01 KS |
2137 | &path[level].bp_ctxt); |
2138 | if (ret < 0) | |
2139 | return ret; | |
2140 | nilfs_btnode_commit_change_key( | |
e7c274f8 | 2141 | &NILFS_BMAP_I(btree)->i_btnode_cache, |
17c76b01 KS |
2142 | &path[level].bp_ctxt); |
2143 | *bh = path[level].bp_ctxt.bh; | |
2144 | } | |
2145 | ||
9b7b265c RK |
2146 | nilfs_btree_node_set_ptr(parent, path[level + 1].bp_index, blocknr, |
2147 | ncmax); | |
17c76b01 | 2148 | |
6d28f7ea | 2149 | key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index); |
17c76b01 | 2150 | /* on-disk format */ |
25b8d7de | 2151 | binfo->bi_dat.bi_blkoff = cpu_to_le64(key); |
17c76b01 KS |
2152 | binfo->bi_dat.bi_level = level; |
2153 | ||
2154 | return 0; | |
2155 | } | |
2156 | ||
e7c274f8 | 2157 | static int nilfs_btree_assign_v(struct nilfs_bmap *btree, |
17c76b01 KS |
2158 | struct nilfs_btree_path *path, |
2159 | int level, | |
2160 | struct buffer_head **bh, | |
2161 | sector_t blocknr, | |
2162 | union nilfs_binfo *binfo) | |
2163 | { | |
2164 | struct nilfs_btree_node *parent; | |
e7c274f8 | 2165 | struct inode *dat = nilfs_bmap_get_dat(btree); |
17c76b01 KS |
2166 | __u64 key; |
2167 | __u64 ptr; | |
2168 | union nilfs_bmap_ptr_req req; | |
9b7b265c | 2169 | int ncmax, ret; |
17c76b01 | 2170 | |
9b7b265c RK |
2171 | parent = nilfs_btree_get_node(btree, path, level + 1, &ncmax); |
2172 | ptr = nilfs_btree_node_get_ptr(parent, path[level + 1].bp_index, | |
2173 | ncmax); | |
17c76b01 | 2174 | req.bpr_ptr = ptr; |
2e0c2c73 RK |
2175 | ret = nilfs_dat_prepare_start(dat, &req.bpr_req); |
2176 | if (ret < 0) | |
17c76b01 | 2177 | return ret; |
2e0c2c73 | 2178 | nilfs_dat_commit_start(dat, &req.bpr_req, blocknr); |
17c76b01 | 2179 | |
6d28f7ea | 2180 | key = nilfs_btree_node_get_key(parent, path[level + 1].bp_index); |
17c76b01 | 2181 | /* on-disk format */ |
25b8d7de RK |
2182 | binfo->bi_v.bi_vblocknr = cpu_to_le64(ptr); |
2183 | binfo->bi_v.bi_blkoff = cpu_to_le64(key); | |
17c76b01 KS |
2184 | |
2185 | return 0; | |
2186 | } | |
2187 | ||
e7c274f8 | 2188 | static int nilfs_btree_assign(struct nilfs_bmap *btree, |
17c76b01 KS |
2189 | struct buffer_head **bh, |
2190 | sector_t blocknr, | |
2191 | union nilfs_binfo *binfo) | |
2192 | { | |
17c76b01 KS |
2193 | struct nilfs_btree_path *path; |
2194 | struct nilfs_btree_node *node; | |
2195 | __u64 key; | |
2196 | int level, ret; | |
2197 | ||
6d28f7ea | 2198 | path = nilfs_btree_alloc_path(); |
17c76b01 KS |
2199 | if (path == NULL) |
2200 | return -ENOMEM; | |
17c76b01 KS |
2201 | |
2202 | if (buffer_nilfs_node(*bh)) { | |
2203 | node = (struct nilfs_btree_node *)(*bh)->b_data; | |
6d28f7ea RK |
2204 | key = nilfs_btree_node_get_key(node, 0); |
2205 | level = nilfs_btree_node_get_level(node); | |
17c76b01 | 2206 | } else { |
e7c274f8 | 2207 | key = nilfs_bmap_data_get_key(btree, *bh); |
17c76b01 KS |
2208 | level = NILFS_BTREE_LEVEL_DATA; |
2209 | } | |
2210 | ||
03bdb5ac | 2211 | ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1, 0); |
17c76b01 | 2212 | if (ret < 0) { |
1f5abe7e | 2213 | WARN_ON(ret == -ENOENT); |
17c76b01 KS |
2214 | goto out; |
2215 | } | |
2216 | ||
e7c274f8 | 2217 | ret = NILFS_BMAP_USE_VBN(btree) ? |
7cde31d7 RK |
2218 | nilfs_btree_assign_v(btree, path, level, bh, blocknr, binfo) : |
2219 | nilfs_btree_assign_p(btree, path, level, bh, blocknr, binfo); | |
17c76b01 KS |
2220 | |
2221 | out: | |
6d28f7ea | 2222 | nilfs_btree_free_path(path); |
17c76b01 KS |
2223 | |
2224 | return ret; | |
2225 | } | |
2226 | ||
e7c274f8 | 2227 | static int nilfs_btree_assign_gc(struct nilfs_bmap *btree, |
17c76b01 KS |
2228 | struct buffer_head **bh, |
2229 | sector_t blocknr, | |
2230 | union nilfs_binfo *binfo) | |
2231 | { | |
17c76b01 KS |
2232 | struct nilfs_btree_node *node; |
2233 | __u64 key; | |
2234 | int ret; | |
2235 | ||
e7c274f8 | 2236 | ret = nilfs_dat_move(nilfs_bmap_get_dat(btree), (*bh)->b_blocknr, |
2e0c2c73 | 2237 | blocknr); |
17c76b01 KS |
2238 | if (ret < 0) |
2239 | return ret; | |
2240 | ||
2241 | if (buffer_nilfs_node(*bh)) { | |
2242 | node = (struct nilfs_btree_node *)(*bh)->b_data; | |
6d28f7ea | 2243 | key = nilfs_btree_node_get_key(node, 0); |
17c76b01 | 2244 | } else |
e7c274f8 | 2245 | key = nilfs_bmap_data_get_key(btree, *bh); |
17c76b01 KS |
2246 | |
2247 | /* on-disk format */ | |
2248 | binfo->bi_v.bi_vblocknr = cpu_to_le64((*bh)->b_blocknr); | |
25b8d7de | 2249 | binfo->bi_v.bi_blkoff = cpu_to_le64(key); |
17c76b01 KS |
2250 | |
2251 | return 0; | |
2252 | } | |
2253 | ||
e7c274f8 | 2254 | static int nilfs_btree_mark(struct nilfs_bmap *btree, __u64 key, int level) |
17c76b01 KS |
2255 | { |
2256 | struct buffer_head *bh; | |
17c76b01 KS |
2257 | struct nilfs_btree_path *path; |
2258 | __u64 ptr; | |
2259 | int ret; | |
2260 | ||
6d28f7ea | 2261 | path = nilfs_btree_alloc_path(); |
17c76b01 KS |
2262 | if (path == NULL) |
2263 | return -ENOMEM; | |
17c76b01 | 2264 | |
03bdb5ac | 2265 | ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1, 0); |
17c76b01 | 2266 | if (ret < 0) { |
1f5abe7e | 2267 | WARN_ON(ret == -ENOENT); |
17c76b01 KS |
2268 | goto out; |
2269 | } | |
f198dbb9 | 2270 | ret = nilfs_btree_get_block(btree, ptr, &bh); |
17c76b01 | 2271 | if (ret < 0) { |
1f5abe7e | 2272 | WARN_ON(ret == -ENOENT); |
17c76b01 KS |
2273 | goto out; |
2274 | } | |
2275 | ||
2276 | if (!buffer_dirty(bh)) | |
5fc7b141 | 2277 | mark_buffer_dirty(bh); |
087d01b4 | 2278 | brelse(bh); |
e7c274f8 RK |
2279 | if (!nilfs_bmap_dirty(btree)) |
2280 | nilfs_bmap_set_dirty(btree); | |
17c76b01 KS |
2281 | |
2282 | out: | |
6d28f7ea | 2283 | nilfs_btree_free_path(path); |
17c76b01 KS |
2284 | return ret; |
2285 | } | |
2286 | ||
2287 | static const struct nilfs_bmap_operations nilfs_btree_ops = { | |
2288 | .bop_lookup = nilfs_btree_lookup, | |
c3a7abf0 | 2289 | .bop_lookup_contig = nilfs_btree_lookup_contig, |
17c76b01 KS |
2290 | .bop_insert = nilfs_btree_insert, |
2291 | .bop_delete = nilfs_btree_delete, | |
2292 | .bop_clear = NULL, | |
2293 | ||
2294 | .bop_propagate = nilfs_btree_propagate, | |
2295 | ||
2296 | .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers, | |
2297 | ||
2298 | .bop_assign = nilfs_btree_assign, | |
2299 | .bop_mark = nilfs_btree_mark, | |
2300 | ||
2301 | .bop_last_key = nilfs_btree_last_key, | |
2302 | .bop_check_insert = NULL, | |
2303 | .bop_check_delete = nilfs_btree_check_delete, | |
2304 | .bop_gather_data = nilfs_btree_gather_data, | |
2305 | }; | |
2306 | ||
2307 | static const struct nilfs_bmap_operations nilfs_btree_ops_gc = { | |
2308 | .bop_lookup = NULL, | |
c3a7abf0 | 2309 | .bop_lookup_contig = NULL, |
17c76b01 KS |
2310 | .bop_insert = NULL, |
2311 | .bop_delete = NULL, | |
2312 | .bop_clear = NULL, | |
2313 | ||
2314 | .bop_propagate = nilfs_btree_propagate_gc, | |
2315 | ||
2316 | .bop_lookup_dirty_buffers = nilfs_btree_lookup_dirty_buffers, | |
2317 | ||
2318 | .bop_assign = nilfs_btree_assign_gc, | |
2319 | .bop_mark = NULL, | |
2320 | ||
2321 | .bop_last_key = NULL, | |
2322 | .bop_check_insert = NULL, | |
2323 | .bop_check_delete = NULL, | |
2324 | .bop_gather_data = NULL, | |
2325 | }; | |
2326 | ||
b6b14e98 | 2327 | static void __nilfs_btree_init(struct nilfs_bmap *bmap) |
17c76b01 | 2328 | { |
17c76b01 | 2329 | bmap->b_ops = &nilfs_btree_ops; |
5ad2686e RK |
2330 | bmap->b_nchildren_per_block = |
2331 | NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap)); | |
b6b14e98 RK |
2332 | } |
2333 | ||
2334 | int nilfs_btree_init(struct nilfs_bmap *bmap) | |
2335 | { | |
2336 | int ret = 0; | |
2337 | ||
2338 | __nilfs_btree_init(bmap); | |
2339 | ||
2340 | if (nilfs_btree_root_broken(nilfs_btree_get_root(bmap), | |
2341 | bmap->b_inode->i_ino)) | |
2342 | ret = -EIO; | |
2343 | return ret; | |
17c76b01 KS |
2344 | } |
2345 | ||
2346 | void nilfs_btree_init_gc(struct nilfs_bmap *bmap) | |
2347 | { | |
17c76b01 | 2348 | bmap->b_ops = &nilfs_btree_ops_gc; |
5ad2686e RK |
2349 | bmap->b_nchildren_per_block = |
2350 | NILFS_BTREE_NODE_NCHILDREN_MAX(nilfs_btree_node_size(bmap)); | |
17c76b01 | 2351 | } |