1 // SPDX-License-Identifier: GPL-2.0
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Handle basic btree node operations
12 #include <linux/pagemap.h>
13 #include <linux/slab.h>
14 #include <linux/swap.h>
18 void hfs_bnode_read(struct hfs_bnode
*node
, void *buf
,
23 off
+= node
->page_offset
;
26 memcpy(buf
, kmap(page
) + off
, len
);
30 u16
hfs_bnode_read_u16(struct hfs_bnode
*node
, int off
)
34 hfs_bnode_read(node
, &data
, off
, 2);
35 return be16_to_cpu(data
);
38 u8
hfs_bnode_read_u8(struct hfs_bnode
*node
, int off
)
42 hfs_bnode_read(node
, &data
, off
, 1);
46 void hfs_bnode_read_key(struct hfs_bnode
*node
, void *key
, int off
)
48 struct hfs_btree
*tree
;
52 if (node
->type
== HFS_NODE_LEAF
||
53 tree
->attributes
& HFS_TREE_VARIDXKEYS
)
54 key_len
= hfs_bnode_read_u8(node
, off
) + 1;
56 key_len
= tree
->max_key_len
+ 1;
58 hfs_bnode_read(node
, key
, off
, key_len
);
61 void hfs_bnode_write(struct hfs_bnode
*node
, void *buf
, int off
, int len
)
65 off
+= node
->page_offset
;
68 memcpy(kmap(page
) + off
, buf
, len
);
73 void hfs_bnode_write_u16(struct hfs_bnode
*node
, int off
, u16 data
)
75 __be16 v
= cpu_to_be16(data
);
77 hfs_bnode_write(node
, &v
, off
, 2);
80 void hfs_bnode_write_u8(struct hfs_bnode
*node
, int off
, u8 data
)
83 hfs_bnode_write(node
, &data
, off
, 1);
86 void hfs_bnode_clear(struct hfs_bnode
*node
, int off
, int len
)
90 off
+= node
->page_offset
;
93 memset(kmap(page
) + off
, 0, len
);
98 void hfs_bnode_copy(struct hfs_bnode
*dst_node
, int dst
,
99 struct hfs_bnode
*src_node
, int src
, int len
)
101 struct page
*src_page
, *dst_page
;
103 hfs_dbg(BNODE_MOD
, "copybytes: %u,%u,%u\n", dst
, src
, len
);
106 src
+= src_node
->page_offset
;
107 dst
+= dst_node
->page_offset
;
108 src_page
= src_node
->page
[0];
109 dst_page
= dst_node
->page
[0];
111 memcpy(kmap(dst_page
) + dst
, kmap(src_page
) + src
, len
);
114 set_page_dirty(dst_page
);
117 void hfs_bnode_move(struct hfs_bnode
*node
, int dst
, int src
, int len
)
122 hfs_dbg(BNODE_MOD
, "movebytes: %u,%u,%u\n", dst
, src
, len
);
125 src
+= node
->page_offset
;
126 dst
+= node
->page_offset
;
127 page
= node
->page
[0];
129 memmove(ptr
+ dst
, ptr
+ src
, len
);
131 set_page_dirty(page
);
134 void hfs_bnode_dump(struct hfs_bnode
*node
)
136 struct hfs_bnode_desc desc
;
140 hfs_dbg(BNODE_MOD
, "bnode: %d\n", node
->this);
141 hfs_bnode_read(node
, &desc
, 0, sizeof(desc
));
142 hfs_dbg(BNODE_MOD
, "%d, %d, %d, %d, %d\n",
143 be32_to_cpu(desc
.next
), be32_to_cpu(desc
.prev
),
144 desc
.type
, desc
.height
, be16_to_cpu(desc
.num_recs
));
146 off
= node
->tree
->node_size
- 2;
147 for (i
= be16_to_cpu(desc
.num_recs
); i
>= 0; off
-= 2, i
--) {
148 key_off
= hfs_bnode_read_u16(node
, off
);
149 hfs_dbg_cont(BNODE_MOD
, " %d", key_off
);
150 if (i
&& node
->type
== HFS_NODE_INDEX
) {
153 if (node
->tree
->attributes
& HFS_TREE_VARIDXKEYS
)
154 tmp
= (hfs_bnode_read_u8(node
, key_off
) | 1) + 1;
156 tmp
= node
->tree
->max_key_len
+ 1;
157 hfs_dbg_cont(BNODE_MOD
, " (%d,%d",
158 tmp
, hfs_bnode_read_u8(node
, key_off
));
159 hfs_bnode_read(node
, &cnid
, key_off
+ tmp
, 4);
160 hfs_dbg_cont(BNODE_MOD
, ",%d)", be32_to_cpu(cnid
));
161 } else if (i
&& node
->type
== HFS_NODE_LEAF
) {
164 tmp
= hfs_bnode_read_u8(node
, key_off
);
165 hfs_dbg_cont(BNODE_MOD
, " (%d)", tmp
);
168 hfs_dbg_cont(BNODE_MOD
, "\n");
171 void hfs_bnode_unlink(struct hfs_bnode
*node
)
173 struct hfs_btree
*tree
;
174 struct hfs_bnode
*tmp
;
179 tmp
= hfs_bnode_find(tree
, node
->prev
);
182 tmp
->next
= node
->next
;
183 cnid
= cpu_to_be32(tmp
->next
);
184 hfs_bnode_write(tmp
, &cnid
, offsetof(struct hfs_bnode_desc
, next
), 4);
186 } else if (node
->type
== HFS_NODE_LEAF
)
187 tree
->leaf_head
= node
->next
;
190 tmp
= hfs_bnode_find(tree
, node
->next
);
193 tmp
->prev
= node
->prev
;
194 cnid
= cpu_to_be32(tmp
->prev
);
195 hfs_bnode_write(tmp
, &cnid
, offsetof(struct hfs_bnode_desc
, prev
), 4);
197 } else if (node
->type
== HFS_NODE_LEAF
)
198 tree
->leaf_tail
= node
->prev
;
201 if (!node
->prev
&& !node
->next
) {
202 printk(KERN_DEBUG
"hfs_btree_del_level\n");
208 set_bit(HFS_BNODE_DELETED
, &node
->flags
);
211 static inline int hfs_bnode_hash(u32 num
)
213 num
= (num
>> 16) + num
;
215 return num
& (NODE_HASH_SIZE
- 1);
218 struct hfs_bnode
*hfs_bnode_findhash(struct hfs_btree
*tree
, u32 cnid
)
220 struct hfs_bnode
*node
;
222 if (cnid
>= tree
->node_count
) {
223 pr_err("request for non-existent node %d in B*Tree\n", cnid
);
227 for (node
= tree
->node_hash
[hfs_bnode_hash(cnid
)];
228 node
; node
= node
->next_hash
) {
229 if (node
->this == cnid
) {
236 static struct hfs_bnode
*__hfs_bnode_create(struct hfs_btree
*tree
, u32 cnid
)
238 struct hfs_bnode
*node
, *node2
;
239 struct address_space
*mapping
;
241 int size
, block
, i
, hash
;
244 if (cnid
>= tree
->node_count
) {
245 pr_err("request for non-existent node %d in B*Tree\n", cnid
);
249 size
= sizeof(struct hfs_bnode
) + tree
->pages_per_bnode
*
250 sizeof(struct page
*);
251 node
= kzalloc(size
, GFP_KERNEL
);
256 set_bit(HFS_BNODE_NEW
, &node
->flags
);
257 atomic_set(&node
->refcnt
, 1);
258 hfs_dbg(BNODE_REFS
, "new_node(%d:%d): 1\n",
259 node
->tree
->cnid
, node
->this);
260 init_waitqueue_head(&node
->lock_wq
);
261 spin_lock(&tree
->hash_lock
);
262 node2
= hfs_bnode_findhash(tree
, cnid
);
264 hash
= hfs_bnode_hash(cnid
);
265 node
->next_hash
= tree
->node_hash
[hash
];
266 tree
->node_hash
[hash
] = node
;
267 tree
->node_hash_cnt
++;
269 spin_unlock(&tree
->hash_lock
);
271 wait_event(node2
->lock_wq
, !test_bit(HFS_BNODE_NEW
, &node2
->flags
));
274 spin_unlock(&tree
->hash_lock
);
276 mapping
= tree
->inode
->i_mapping
;
277 off
= (loff_t
)cnid
* tree
->node_size
;
278 block
= off
>> PAGE_SHIFT
;
279 node
->page_offset
= off
& ~PAGE_MASK
;
280 for (i
= 0; i
< tree
->pages_per_bnode
; i
++) {
281 page
= read_mapping_page(mapping
, block
++, NULL
);
284 if (PageError(page
)) {
288 node
->page
[i
] = page
;
293 set_bit(HFS_BNODE_ERROR
, &node
->flags
);
297 void hfs_bnode_unhash(struct hfs_bnode
*node
)
299 struct hfs_bnode
**p
;
301 hfs_dbg(BNODE_REFS
, "remove_node(%d:%d): %d\n",
302 node
->tree
->cnid
, node
->this, atomic_read(&node
->refcnt
));
303 for (p
= &node
->tree
->node_hash
[hfs_bnode_hash(node
->this)];
304 *p
&& *p
!= node
; p
= &(*p
)->next_hash
)
307 *p
= node
->next_hash
;
308 node
->tree
->node_hash_cnt
--;
311 /* Load a particular node out of a tree */
312 struct hfs_bnode
*hfs_bnode_find(struct hfs_btree
*tree
, u32 num
)
314 struct hfs_bnode
*node
;
315 struct hfs_bnode_desc
*desc
;
316 int i
, rec_off
, off
, next_off
;
317 int entry_size
, key_size
;
319 spin_lock(&tree
->hash_lock
);
320 node
= hfs_bnode_findhash(tree
, num
);
323 spin_unlock(&tree
->hash_lock
);
324 wait_event(node
->lock_wq
, !test_bit(HFS_BNODE_NEW
, &node
->flags
));
325 if (test_bit(HFS_BNODE_ERROR
, &node
->flags
))
329 spin_unlock(&tree
->hash_lock
);
330 node
= __hfs_bnode_create(tree
, num
);
332 return ERR_PTR(-ENOMEM
);
333 if (test_bit(HFS_BNODE_ERROR
, &node
->flags
))
335 if (!test_bit(HFS_BNODE_NEW
, &node
->flags
))
338 desc
= (struct hfs_bnode_desc
*)(kmap(node
->page
[0]) + node
->page_offset
);
339 node
->prev
= be32_to_cpu(desc
->prev
);
340 node
->next
= be32_to_cpu(desc
->next
);
341 node
->num_recs
= be16_to_cpu(desc
->num_recs
);
342 node
->type
= desc
->type
;
343 node
->height
= desc
->height
;
344 kunmap(node
->page
[0]);
346 switch (node
->type
) {
347 case HFS_NODE_HEADER
:
349 if (node
->height
!= 0)
353 if (node
->height
!= 1)
357 if (node
->height
<= 1 || node
->height
> tree
->depth
)
364 rec_off
= tree
->node_size
- 2;
365 off
= hfs_bnode_read_u16(node
, rec_off
);
366 if (off
!= sizeof(struct hfs_bnode_desc
))
368 for (i
= 1; i
<= node
->num_recs
; off
= next_off
, i
++) {
370 next_off
= hfs_bnode_read_u16(node
, rec_off
);
371 if (next_off
<= off
||
372 next_off
> tree
->node_size
||
375 entry_size
= next_off
- off
;
376 if (node
->type
!= HFS_NODE_INDEX
&&
377 node
->type
!= HFS_NODE_LEAF
)
379 key_size
= hfs_bnode_read_u8(node
, off
) + 1;
380 if (key_size
>= entry_size
/*|| key_size & 1*/)
383 clear_bit(HFS_BNODE_NEW
, &node
->flags
);
384 wake_up(&node
->lock_wq
);
388 set_bit(HFS_BNODE_ERROR
, &node
->flags
);
389 clear_bit(HFS_BNODE_NEW
, &node
->flags
);
390 wake_up(&node
->lock_wq
);
392 return ERR_PTR(-EIO
);
395 void hfs_bnode_free(struct hfs_bnode
*node
)
399 for (i
= 0; i
< node
->tree
->pages_per_bnode
; i
++)
401 put_page(node
->page
[i
]);
405 struct hfs_bnode
*hfs_bnode_create(struct hfs_btree
*tree
, u32 num
)
407 struct hfs_bnode
*node
;
411 spin_lock(&tree
->hash_lock
);
412 node
= hfs_bnode_findhash(tree
, num
);
413 spin_unlock(&tree
->hash_lock
);
415 pr_crit("new node %u already hashed?\n", num
);
419 node
= __hfs_bnode_create(tree
, num
);
421 return ERR_PTR(-ENOMEM
);
422 if (test_bit(HFS_BNODE_ERROR
, &node
->flags
)) {
424 return ERR_PTR(-EIO
);
428 memset(kmap(*pagep
) + node
->page_offset
, 0,
429 min((int)PAGE_SIZE
, (int)tree
->node_size
));
430 set_page_dirty(*pagep
);
432 for (i
= 1; i
< tree
->pages_per_bnode
; i
++) {
433 memset(kmap(*++pagep
), 0, PAGE_SIZE
);
434 set_page_dirty(*pagep
);
437 clear_bit(HFS_BNODE_NEW
, &node
->flags
);
438 wake_up(&node
->lock_wq
);
443 void hfs_bnode_get(struct hfs_bnode
*node
)
446 atomic_inc(&node
->refcnt
);
447 hfs_dbg(BNODE_REFS
, "get_node(%d:%d): %d\n",
448 node
->tree
->cnid
, node
->this,
449 atomic_read(&node
->refcnt
));
453 /* Dispose of resources used by a node */
454 void hfs_bnode_put(struct hfs_bnode
*node
)
457 struct hfs_btree
*tree
= node
->tree
;
460 hfs_dbg(BNODE_REFS
, "put_node(%d:%d): %d\n",
461 node
->tree
->cnid
, node
->this,
462 atomic_read(&node
->refcnt
));
463 BUG_ON(!atomic_read(&node
->refcnt
));
464 if (!atomic_dec_and_lock(&node
->refcnt
, &tree
->hash_lock
))
466 for (i
= 0; i
< tree
->pages_per_bnode
; i
++) {
469 mark_page_accessed(node
->page
[i
]);
472 if (test_bit(HFS_BNODE_DELETED
, &node
->flags
)) {
473 hfs_bnode_unhash(node
);
474 spin_unlock(&tree
->hash_lock
);
476 hfs_bnode_free(node
);
479 spin_unlock(&tree
->hash_lock
);