2 * linux/fs/hfsplus/bnode.c
5 * Brad Boyer (flar@allandria.com)
6 * (C) 2003 Ardis Technologies <roman@ardistech.com>
8 * Handle basic btree node operations
11 #include <linux/string.h>
12 #include <linux/slab.h>
13 #include <linux/pagemap.h>
15 #include <linux/swap.h>
16 #include <linux/version.h>
18 #include "hfsplus_fs.h"
19 #include "hfsplus_raw.h"
23 /* Copy a specified range of bytes from the raw data of a node */
24 void hfs_bnode_read(struct hfs_bnode
*node
, void *buf
, int off
, int len
)
29 off
+= node
->page_offset
;
30 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
31 off
&= ~PAGE_CACHE_MASK
;
33 l
= min(len
, (int)PAGE_CACHE_SIZE
- off
);
34 memcpy(buf
, kmap(*pagep
) + off
, l
);
37 while ((len
-= l
) != 0) {
39 l
= min(len
, (int)PAGE_CACHE_SIZE
);
40 memcpy(buf
, kmap(*++pagep
), l
);
45 u16
hfs_bnode_read_u16(struct hfs_bnode
*node
, int off
)
49 hfs_bnode_read(node
, &data
, off
, 2);
50 return be16_to_cpu(data
);
53 u8
hfs_bnode_read_u8(struct hfs_bnode
*node
, int off
)
57 hfs_bnode_read(node
, &data
, off
, 1);
61 void hfs_bnode_read_key(struct hfs_bnode
*node
, void *key
, int off
)
63 struct hfs_btree
*tree
;
67 if (node
->type
== HFS_NODE_LEAF
||
68 tree
->attributes
& HFS_TREE_VARIDXKEYS
)
69 key_len
= hfs_bnode_read_u16(node
, off
) + 2;
71 key_len
= tree
->max_key_len
+ 2;
73 hfs_bnode_read(node
, key
, off
, key_len
);
76 void hfs_bnode_write(struct hfs_bnode
*node
, void *buf
, int off
, int len
)
81 off
+= node
->page_offset
;
82 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
83 off
&= ~PAGE_CACHE_MASK
;
85 l
= min(len
, (int)PAGE_CACHE_SIZE
- off
);
86 memcpy(kmap(*pagep
) + off
, buf
, l
);
87 set_page_dirty(*pagep
);
90 while ((len
-= l
) != 0) {
92 l
= min(len
, (int)PAGE_CACHE_SIZE
);
93 memcpy(kmap(*++pagep
), buf
, l
);
94 set_page_dirty(*pagep
);
99 void hfs_bnode_write_u16(struct hfs_bnode
*node
, int off
, u16 data
)
101 __be16 v
= cpu_to_be16(data
);
103 hfs_bnode_write(node
, &v
, off
, 2);
106 void hfs_bnode_clear(struct hfs_bnode
*node
, int off
, int len
)
111 off
+= node
->page_offset
;
112 pagep
= node
->page
+ (off
>> PAGE_CACHE_SHIFT
);
113 off
&= ~PAGE_CACHE_MASK
;
115 l
= min(len
, (int)PAGE_CACHE_SIZE
- off
);
116 memset(kmap(*pagep
) + off
, 0, l
);
117 set_page_dirty(*pagep
);
120 while ((len
-= l
) != 0) {
121 l
= min(len
, (int)PAGE_CACHE_SIZE
);
122 memset(kmap(*++pagep
), 0, l
);
123 set_page_dirty(*pagep
);
128 void hfs_bnode_copy(struct hfs_bnode
*dst_node
, int dst
,
129 struct hfs_bnode
*src_node
, int src
, int len
)
131 struct hfs_btree
*tree
;
132 struct page
**src_page
, **dst_page
;
135 dprint(DBG_BNODE_MOD
, "copybytes: %u,%u,%u\n", dst
, src
, len
);
138 tree
= src_node
->tree
;
139 src
+= src_node
->page_offset
;
140 dst
+= dst_node
->page_offset
;
141 src_page
= src_node
->page
+ (src
>> PAGE_CACHE_SHIFT
);
142 src
&= ~PAGE_CACHE_MASK
;
143 dst_page
= dst_node
->page
+ (dst
>> PAGE_CACHE_SHIFT
);
144 dst
&= ~PAGE_CACHE_MASK
;
147 l
= min(len
, (int)PAGE_CACHE_SIZE
- src
);
148 memcpy(kmap(*dst_page
) + src
, kmap(*src_page
) + src
, l
);
150 set_page_dirty(*dst_page
);
153 while ((len
-= l
) != 0) {
154 l
= min(len
, (int)PAGE_CACHE_SIZE
);
155 memcpy(kmap(*++dst_page
), kmap(*++src_page
), l
);
157 set_page_dirty(*dst_page
);
161 void *src_ptr
, *dst_ptr
;
164 src_ptr
= kmap(*src_page
) + src
;
165 dst_ptr
= kmap(*dst_page
) + dst
;
166 if (PAGE_CACHE_SIZE
- src
< PAGE_CACHE_SIZE
- dst
) {
167 l
= PAGE_CACHE_SIZE
- src
;
171 l
= PAGE_CACHE_SIZE
- dst
;
176 memcpy(dst_ptr
, src_ptr
, l
);
178 set_page_dirty(*dst_page
);
184 } while ((len
-= l
));
188 void hfs_bnode_move(struct hfs_bnode
*node
, int dst
, int src
, int len
)
190 struct page
**src_page
, **dst_page
;
193 dprint(DBG_BNODE_MOD
, "movebytes: %u,%u,%u\n", dst
, src
, len
);
196 src
+= node
->page_offset
;
197 dst
+= node
->page_offset
;
200 src_page
= node
->page
+ (src
>> PAGE_CACHE_SHIFT
);
201 src
= (src
& ~PAGE_CACHE_MASK
) + 1;
203 dst_page
= node
->page
+ (dst
>> PAGE_CACHE_SHIFT
);
204 dst
= (dst
& ~PAGE_CACHE_MASK
) + 1;
208 memmove(kmap(*dst_page
), kmap(*src_page
), src
);
210 set_page_dirty(*dst_page
);
213 src
= PAGE_CACHE_SIZE
;
218 memmove(kmap(*dst_page
) + src
, kmap(*src_page
) + src
, len
);
220 set_page_dirty(*dst_page
);
223 void *src_ptr
, *dst_ptr
;
226 src_ptr
= kmap(*src_page
) + src
;
227 dst_ptr
= kmap(*dst_page
) + dst
;
230 src
= PAGE_CACHE_SIZE
;
235 dst
= PAGE_CACHE_SIZE
;
238 memmove(dst_ptr
- l
, src_ptr
- l
, l
);
240 set_page_dirty(*dst_page
);
242 if (dst
== PAGE_CACHE_SIZE
)
246 } while ((len
-= l
));
249 src_page
= node
->page
+ (src
>> PAGE_CACHE_SHIFT
);
250 src
&= ~PAGE_CACHE_MASK
;
251 dst_page
= node
->page
+ (dst
>> PAGE_CACHE_SHIFT
);
252 dst
&= ~PAGE_CACHE_MASK
;
255 l
= min(len
, (int)PAGE_CACHE_SIZE
- src
);
256 memmove(kmap(*dst_page
) + src
, kmap(*src_page
) + src
, l
);
258 set_page_dirty(*dst_page
);
261 while ((len
-= l
) != 0) {
262 l
= min(len
, (int)PAGE_CACHE_SIZE
);
263 memmove(kmap(*++dst_page
), kmap(*++src_page
), l
);
265 set_page_dirty(*dst_page
);
269 void *src_ptr
, *dst_ptr
;
272 src_ptr
= kmap(*src_page
) + src
;
273 dst_ptr
= kmap(*dst_page
) + dst
;
274 if (PAGE_CACHE_SIZE
- src
< PAGE_CACHE_SIZE
- dst
) {
275 l
= PAGE_CACHE_SIZE
- src
;
279 l
= PAGE_CACHE_SIZE
- dst
;
284 memmove(dst_ptr
, src_ptr
, l
);
286 set_page_dirty(*dst_page
);
292 } while ((len
-= l
));
297 void hfs_bnode_dump(struct hfs_bnode
*node
)
299 struct hfs_bnode_desc desc
;
303 dprint(DBG_BNODE_MOD
, "bnode: %d\n", node
->this);
304 hfs_bnode_read(node
, &desc
, 0, sizeof(desc
));
305 dprint(DBG_BNODE_MOD
, "%d, %d, %d, %d, %d\n",
306 be32_to_cpu(desc
.next
), be32_to_cpu(desc
.prev
),
307 desc
.type
, desc
.height
, be16_to_cpu(desc
.num_recs
));
309 off
= node
->tree
->node_size
- 2;
310 for (i
= be16_to_cpu(desc
.num_recs
); i
>= 0; off
-= 2, i
--) {
311 key_off
= hfs_bnode_read_u16(node
, off
);
312 dprint(DBG_BNODE_MOD
, " %d", key_off
);
313 if (i
&& node
->type
== HFS_NODE_INDEX
) {
316 if (node
->tree
->attributes
& HFS_TREE_VARIDXKEYS
)
317 tmp
= hfs_bnode_read_u16(node
, key_off
) + 2;
319 tmp
= node
->tree
->max_key_len
+ 2;
320 dprint(DBG_BNODE_MOD
, " (%d", tmp
);
321 hfs_bnode_read(node
, &cnid
, key_off
+ tmp
, 4);
322 dprint(DBG_BNODE_MOD
, ",%d)", be32_to_cpu(cnid
));
323 } else if (i
&& node
->type
== HFS_NODE_LEAF
) {
326 tmp
= hfs_bnode_read_u16(node
, key_off
);
327 dprint(DBG_BNODE_MOD
, " (%d)", tmp
);
330 dprint(DBG_BNODE_MOD
, "\n");
333 void hfs_bnode_unlink(struct hfs_bnode
*node
)
335 struct hfs_btree
*tree
;
336 struct hfs_bnode
*tmp
;
341 tmp
= hfs_bnode_find(tree
, node
->prev
);
344 tmp
->next
= node
->next
;
345 cnid
= cpu_to_be32(tmp
->next
);
346 hfs_bnode_write(tmp
, &cnid
, offsetof(struct hfs_bnode_desc
, next
), 4);
348 } else if (node
->type
== HFS_NODE_LEAF
)
349 tree
->leaf_head
= node
->next
;
352 tmp
= hfs_bnode_find(tree
, node
->next
);
355 tmp
->prev
= node
->prev
;
356 cnid
= cpu_to_be32(tmp
->prev
);
357 hfs_bnode_write(tmp
, &cnid
, offsetof(struct hfs_bnode_desc
, prev
), 4);
359 } else if (node
->type
== HFS_NODE_LEAF
)
360 tree
->leaf_tail
= node
->prev
;
363 if (!node
->prev
&& !node
->next
) {
364 printk("hfs_btree_del_level\n");
370 set_bit(HFS_BNODE_DELETED
, &node
->flags
);
373 static inline int hfs_bnode_hash(u32 num
)
375 num
= (num
>> 16) + num
;
377 return num
& (NODE_HASH_SIZE
- 1);
380 struct hfs_bnode
*hfs_bnode_findhash(struct hfs_btree
*tree
, u32 cnid
)
382 struct hfs_bnode
*node
;
384 if (cnid
>= tree
->node_count
) {
385 printk("HFS+-fs: request for non-existent node %d in B*Tree\n", cnid
);
389 for (node
= tree
->node_hash
[hfs_bnode_hash(cnid
)];
390 node
; node
= node
->next_hash
) {
391 if (node
->this == cnid
) {
398 static struct hfs_bnode
*__hfs_bnode_create(struct hfs_btree
*tree
, u32 cnid
)
400 struct super_block
*sb
;
401 struct hfs_bnode
*node
, *node2
;
402 struct address_space
*mapping
;
404 int size
, block
, i
, hash
;
407 if (cnid
>= tree
->node_count
) {
408 printk("HFS+-fs: request for non-existent node %d in B*Tree\n", cnid
);
412 sb
= tree
->inode
->i_sb
;
413 size
= sizeof(struct hfs_bnode
) + tree
->pages_per_bnode
*
414 sizeof(struct page
*);
415 node
= kmalloc(size
, GFP_KERNEL
);
418 memset(node
, 0, size
);
421 set_bit(HFS_BNODE_NEW
, &node
->flags
);
422 atomic_set(&node
->refcnt
, 1);
423 dprint(DBG_BNODE_REFS
, "new_node(%d:%d): 1\n",
424 node
->tree
->cnid
, node
->this);
425 init_waitqueue_head(&node
->lock_wq
);
426 spin_lock(&tree
->hash_lock
);
427 node2
= hfs_bnode_findhash(tree
, cnid
);
429 hash
= hfs_bnode_hash(cnid
);
430 node
->next_hash
= tree
->node_hash
[hash
];
431 tree
->node_hash
[hash
] = node
;
432 tree
->node_hash_cnt
++;
434 spin_unlock(&tree
->hash_lock
);
436 wait_event(node2
->lock_wq
, !test_bit(HFS_BNODE_NEW
, &node2
->flags
));
439 spin_unlock(&tree
->hash_lock
);
441 mapping
= tree
->inode
->i_mapping
;
442 off
= (loff_t
)cnid
<< tree
->node_size_shift
;
443 block
= off
>> PAGE_CACHE_SHIFT
;
444 node
->page_offset
= off
& ~PAGE_CACHE_MASK
;
445 for (i
= 0; i
< tree
->pages_per_bnode
; block
++, i
++) {
446 page
= read_cache_page(mapping
, block
, (filler_t
*)mapping
->a_ops
->readpage
, NULL
);
449 if (PageError(page
)) {
450 page_cache_release(page
);
454 page_cache_release(page
);
456 node
->page
[i
] = page
;
461 set_bit(HFS_BNODE_ERROR
, &node
->flags
);
465 void hfs_bnode_unhash(struct hfs_bnode
*node
)
467 struct hfs_bnode
**p
;
469 dprint(DBG_BNODE_REFS
, "remove_node(%d:%d): %d\n",
470 node
->tree
->cnid
, node
->this, atomic_read(&node
->refcnt
));
471 for (p
= &node
->tree
->node_hash
[hfs_bnode_hash(node
->this)];
472 *p
&& *p
!= node
; p
= &(*p
)->next_hash
)
476 *p
= node
->next_hash
;
477 node
->tree
->node_hash_cnt
--;
480 /* Load a particular node out of a tree */
481 struct hfs_bnode
*hfs_bnode_find(struct hfs_btree
*tree
, u32 num
)
483 struct hfs_bnode
*node
;
484 struct hfs_bnode_desc
*desc
;
485 int i
, rec_off
, off
, next_off
;
486 int entry_size
, key_size
;
488 spin_lock(&tree
->hash_lock
);
489 node
= hfs_bnode_findhash(tree
, num
);
492 spin_unlock(&tree
->hash_lock
);
493 wait_event(node
->lock_wq
, !test_bit(HFS_BNODE_NEW
, &node
->flags
));
494 if (test_bit(HFS_BNODE_ERROR
, &node
->flags
))
498 spin_unlock(&tree
->hash_lock
);
499 node
= __hfs_bnode_create(tree
, num
);
501 return ERR_PTR(-ENOMEM
);
502 if (test_bit(HFS_BNODE_ERROR
, &node
->flags
))
504 if (!test_bit(HFS_BNODE_NEW
, &node
->flags
))
507 desc
= (struct hfs_bnode_desc
*)(kmap(node
->page
[0]) + node
->page_offset
);
508 node
->prev
= be32_to_cpu(desc
->prev
);
509 node
->next
= be32_to_cpu(desc
->next
);
510 node
->num_recs
= be16_to_cpu(desc
->num_recs
);
511 node
->type
= desc
->type
;
512 node
->height
= desc
->height
;
513 kunmap(node
->page
[0]);
515 switch (node
->type
) {
516 case HFS_NODE_HEADER
:
518 if (node
->height
!= 0)
522 if (node
->height
!= 1)
526 if (node
->height
<= 1 || node
->height
> tree
->depth
)
533 rec_off
= tree
->node_size
- 2;
534 off
= hfs_bnode_read_u16(node
, rec_off
);
535 if (off
!= sizeof(struct hfs_bnode_desc
))
537 for (i
= 1; i
<= node
->num_recs
; off
= next_off
, i
++) {
539 next_off
= hfs_bnode_read_u16(node
, rec_off
);
540 if (next_off
<= off
||
541 next_off
> tree
->node_size
||
544 entry_size
= next_off
- off
;
545 if (node
->type
!= HFS_NODE_INDEX
&&
546 node
->type
!= HFS_NODE_LEAF
)
548 key_size
= hfs_bnode_read_u16(node
, off
) + 2;
549 if (key_size
>= entry_size
|| key_size
& 1)
552 clear_bit(HFS_BNODE_NEW
, &node
->flags
);
553 wake_up(&node
->lock_wq
);
557 set_bit(HFS_BNODE_ERROR
, &node
->flags
);
558 clear_bit(HFS_BNODE_NEW
, &node
->flags
);
559 wake_up(&node
->lock_wq
);
561 return ERR_PTR(-EIO
);
564 void hfs_bnode_free(struct hfs_bnode
*node
)
568 //for (i = 0; i < node->tree->pages_per_bnode; i++)
569 // if (node->page[i])
570 // page_cache_release(node->page[i]);
574 struct hfs_bnode
*hfs_bnode_create(struct hfs_btree
*tree
, u32 num
)
576 struct hfs_bnode
*node
;
580 spin_lock(&tree
->hash_lock
);
581 node
= hfs_bnode_findhash(tree
, num
);
582 spin_unlock(&tree
->hash_lock
);
584 printk("new node %u already hashed?\n", num
);
587 node
= __hfs_bnode_create(tree
, num
);
589 return ERR_PTR(-ENOMEM
);
590 if (test_bit(HFS_BNODE_ERROR
, &node
->flags
)) {
592 return ERR_PTR(-EIO
);
596 memset(kmap(*pagep
) + node
->page_offset
, 0,
597 min((int)PAGE_CACHE_SIZE
, (int)tree
->node_size
));
598 set_page_dirty(*pagep
);
600 for (i
= 1; i
< tree
->pages_per_bnode
; i
++) {
601 memset(kmap(*++pagep
), 0, PAGE_CACHE_SIZE
);
602 set_page_dirty(*pagep
);
605 clear_bit(HFS_BNODE_NEW
, &node
->flags
);
606 wake_up(&node
->lock_wq
);
611 void hfs_bnode_get(struct hfs_bnode
*node
)
614 atomic_inc(&node
->refcnt
);
618 for (i
= 0; i
< node
->tree
->pages_per_bnode
; i
++)
619 get_page(node
->page
[i
]);
622 dprint(DBG_BNODE_REFS
, "get_node(%d:%d): %d\n",
623 node
->tree
->cnid
, node
->this, atomic_read(&node
->refcnt
));
627 /* Dispose of resources used by a node */
628 void hfs_bnode_put(struct hfs_bnode
*node
)
631 struct hfs_btree
*tree
= node
->tree
;
634 dprint(DBG_BNODE_REFS
, "put_node(%d:%d): %d\n",
635 node
->tree
->cnid
, node
->this, atomic_read(&node
->refcnt
));
636 if (!atomic_read(&node
->refcnt
))
638 if (!atomic_dec_and_lock(&node
->refcnt
, &tree
->hash_lock
)) {
640 for (i
= 0; i
< tree
->pages_per_bnode
; i
++)
641 put_page(node
->page
[i
]);
645 for (i
= 0; i
< tree
->pages_per_bnode
; i
++) {
646 mark_page_accessed(node
->page
[i
]);
648 put_page(node
->page
[i
]);
652 if (test_bit(HFS_BNODE_DELETED
, &node
->flags
)) {
653 hfs_bnode_unhash(node
);
654 spin_unlock(&tree
->hash_lock
);
656 hfs_bnode_free(node
);
659 spin_unlock(&tree
->hash_lock
);