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>
17 #include "hfsplus_fs.h"
18 #include "hfsplus_raw.h"
20 /* Copy a specified range of bytes from the raw data of a node */
21 void hfs_bnode_read(struct hfs_bnode *node, void *buf, int off, int len)
26 off += node->page_offset;
27 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
28 off &= ~PAGE_CACHE_MASK;
30 l = min(len, (int)PAGE_CACHE_SIZE - off);
31 memcpy(buf, kmap(*pagep) + off, l);
34 while ((len -= l) != 0) {
36 l = min(len, (int)PAGE_CACHE_SIZE);
37 memcpy(buf, kmap(*++pagep), l);
42 u16 hfs_bnode_read_u16(struct hfs_bnode *node, int off)
45 /* TODO: optimize later... */
46 hfs_bnode_read(node, &data, off, 2);
47 return be16_to_cpu(data);
50 u8 hfs_bnode_read_u8(struct hfs_bnode *node, int off)
53 /* TODO: optimize later... */
54 hfs_bnode_read(node, &data, off, 1);
58 void hfs_bnode_read_key(struct hfs_bnode *node, void *key, int off)
60 struct hfs_btree *tree;
64 if (node->type == HFS_NODE_LEAF ||
65 tree->attributes & HFS_TREE_VARIDXKEYS ||
66 node->tree->cnid == HFSPLUS_ATTR_CNID)
67 key_len = hfs_bnode_read_u16(node, off) + 2;
69 key_len = tree->max_key_len + 2;
71 hfs_bnode_read(node, key, off, key_len);
74 void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
79 off += node->page_offset;
80 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
81 off &= ~PAGE_CACHE_MASK;
83 l = min(len, (int)PAGE_CACHE_SIZE - off);
84 memcpy(kmap(*pagep) + off, buf, l);
85 set_page_dirty(*pagep);
88 while ((len -= l) != 0) {
90 l = min(len, (int)PAGE_CACHE_SIZE);
91 memcpy(kmap(*++pagep), buf, l);
92 set_page_dirty(*pagep);
97 void hfs_bnode_write_u16(struct hfs_bnode *node, int off, u16 data)
99 __be16 v = cpu_to_be16(data);
100 /* TODO: optimize later... */
101 hfs_bnode_write(node, &v, off, 2);
104 void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
109 off += node->page_offset;
110 pagep = node->page + (off >> PAGE_CACHE_SHIFT);
111 off &= ~PAGE_CACHE_MASK;
113 l = min(len, (int)PAGE_CACHE_SIZE - off);
114 memset(kmap(*pagep) + off, 0, l);
115 set_page_dirty(*pagep);
118 while ((len -= l) != 0) {
119 l = min(len, (int)PAGE_CACHE_SIZE);
120 memset(kmap(*++pagep), 0, l);
121 set_page_dirty(*pagep);
126 void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
127 struct hfs_bnode *src_node, int src, int len)
129 struct hfs_btree *tree;
130 struct page **src_page, **dst_page;
133 dprint(DBG_BNODE_MOD, "copybytes: %u,%u,%u\n", dst, src, len);
136 tree = src_node->tree;
137 src += src_node->page_offset;
138 dst += dst_node->page_offset;
139 src_page = src_node->page + (src >> PAGE_CACHE_SHIFT);
140 src &= ~PAGE_CACHE_MASK;
141 dst_page = dst_node->page + (dst >> PAGE_CACHE_SHIFT);
142 dst &= ~PAGE_CACHE_MASK;
145 l = min(len, (int)PAGE_CACHE_SIZE - src);
146 memcpy(kmap(*dst_page) + src, kmap(*src_page) + src, l);
148 set_page_dirty(*dst_page);
151 while ((len -= l) != 0) {
152 l = min(len, (int)PAGE_CACHE_SIZE);
153 memcpy(kmap(*++dst_page), kmap(*++src_page), l);
155 set_page_dirty(*dst_page);
159 void *src_ptr, *dst_ptr;
162 src_ptr = kmap(*src_page) + src;
163 dst_ptr = kmap(*dst_page) + dst;
164 if (PAGE_CACHE_SIZE - src < PAGE_CACHE_SIZE - dst) {
165 l = PAGE_CACHE_SIZE - src;
169 l = PAGE_CACHE_SIZE - dst;
174 memcpy(dst_ptr, src_ptr, l);
176 set_page_dirty(*dst_page);
182 } while ((len -= l));
186 void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
188 struct page **src_page, **dst_page;
191 dprint(DBG_BNODE_MOD, "movebytes: %u,%u,%u\n", dst, src, len);
194 src += node->page_offset;
195 dst += node->page_offset;
198 src_page = node->page + (src >> PAGE_CACHE_SHIFT);
199 src = (src & ~PAGE_CACHE_MASK) + 1;
201 dst_page = node->page + (dst >> PAGE_CACHE_SHIFT);
202 dst = (dst & ~PAGE_CACHE_MASK) + 1;
206 memmove(kmap(*dst_page), kmap(*src_page), src);
208 set_page_dirty(*dst_page);
211 src = PAGE_CACHE_SIZE;
216 memmove(kmap(*dst_page) + src,
217 kmap(*src_page) + src, len);
219 set_page_dirty(*dst_page);
222 void *src_ptr, *dst_ptr;
225 src_ptr = kmap(*src_page) + src;
226 dst_ptr = kmap(*dst_page) + dst;
229 src = PAGE_CACHE_SIZE;
234 dst = PAGE_CACHE_SIZE;
237 memmove(dst_ptr - l, src_ptr - l, l);
239 set_page_dirty(*dst_page);
241 if (dst == PAGE_CACHE_SIZE)
245 } while ((len -= l));
248 src_page = node->page + (src >> PAGE_CACHE_SHIFT);
249 src &= ~PAGE_CACHE_MASK;
250 dst_page = node->page + (dst >> PAGE_CACHE_SHIFT);
251 dst &= ~PAGE_CACHE_MASK;
254 l = min(len, (int)PAGE_CACHE_SIZE - src);
255 memmove(kmap(*dst_page) + src,
256 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),
264 kmap(*++src_page), l);
266 set_page_dirty(*dst_page);
270 void *src_ptr, *dst_ptr;
273 src_ptr = kmap(*src_page) + src;
274 dst_ptr = kmap(*dst_page) + dst;
275 if (PAGE_CACHE_SIZE - src <
276 PAGE_CACHE_SIZE - dst) {
277 l = PAGE_CACHE_SIZE - src;
281 l = PAGE_CACHE_SIZE - dst;
286 memmove(dst_ptr, src_ptr, l);
288 set_page_dirty(*dst_page);
294 } while ((len -= l));
299 void hfs_bnode_dump(struct hfs_bnode *node)
301 struct hfs_bnode_desc desc;
305 dprint(DBG_BNODE_MOD, "bnode: %d\n", node->this);
306 hfs_bnode_read(node, &desc, 0, sizeof(desc));
307 dprint(DBG_BNODE_MOD, "%d, %d, %d, %d, %d\n",
308 be32_to_cpu(desc.next), be32_to_cpu(desc.prev),
309 desc.type, desc.height, be16_to_cpu(desc.num_recs));
311 off = node->tree->node_size - 2;
312 for (i = be16_to_cpu(desc.num_recs); i >= 0; off -= 2, i--) {
313 key_off = hfs_bnode_read_u16(node, off);
314 dprint(DBG_BNODE_MOD, " %d", key_off);
315 if (i && node->type == HFS_NODE_INDEX) {
318 if (node->tree->attributes & HFS_TREE_VARIDXKEYS ||
319 node->tree->cnid == HFSPLUS_ATTR_CNID)
320 tmp = hfs_bnode_read_u16(node, key_off) + 2;
322 tmp = node->tree->max_key_len + 2;
323 dprint(DBG_BNODE_MOD, " (%d", tmp);
324 hfs_bnode_read(node, &cnid, key_off + tmp, 4);
325 dprint(DBG_BNODE_MOD, ",%d)", be32_to_cpu(cnid));
326 } else if (i && node->type == HFS_NODE_LEAF) {
329 tmp = hfs_bnode_read_u16(node, key_off);
330 dprint(DBG_BNODE_MOD, " (%d)", tmp);
333 dprint(DBG_BNODE_MOD, "\n");
336 void hfs_bnode_unlink(struct hfs_bnode *node)
338 struct hfs_btree *tree;
339 struct hfs_bnode *tmp;
344 tmp = hfs_bnode_find(tree, node->prev);
347 tmp->next = node->next;
348 cnid = cpu_to_be32(tmp->next);
349 hfs_bnode_write(tmp, &cnid,
350 offsetof(struct hfs_bnode_desc, next), 4);
352 } else if (node->type == HFS_NODE_LEAF)
353 tree->leaf_head = node->next;
356 tmp = hfs_bnode_find(tree, node->next);
359 tmp->prev = node->prev;
360 cnid = cpu_to_be32(tmp->prev);
361 hfs_bnode_write(tmp, &cnid,
362 offsetof(struct hfs_bnode_desc, prev), 4);
364 } else if (node->type == HFS_NODE_LEAF)
365 tree->leaf_tail = node->prev;
368 if (!node->prev && !node->next)
369 dprint(DBG_BNODE_MOD, "hfs_btree_del_level\n");
374 set_bit(HFS_BNODE_DELETED, &node->flags);
377 static inline int hfs_bnode_hash(u32 num)
379 num = (num >> 16) + num;
381 return num & (NODE_HASH_SIZE - 1);
384 struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid)
386 struct hfs_bnode *node;
388 if (cnid >= tree->node_count) {
389 printk(KERN_ERR "hfs: request for non-existent node "
395 for (node = tree->node_hash[hfs_bnode_hash(cnid)];
396 node; node = node->next_hash)
397 if (node->this == cnid)
402 static struct hfs_bnode *__hfs_bnode_create(struct hfs_btree *tree, u32 cnid)
404 struct super_block *sb;
405 struct hfs_bnode *node, *node2;
406 struct address_space *mapping;
408 int size, block, i, hash;
411 if (cnid >= tree->node_count) {
412 printk(KERN_ERR "hfs: request for non-existent node "
418 sb = tree->inode->i_sb;
419 size = sizeof(struct hfs_bnode) + tree->pages_per_bnode *
420 sizeof(struct page *);
421 node = kzalloc(size, GFP_KERNEL);
426 set_bit(HFS_BNODE_NEW, &node->flags);
427 atomic_set(&node->refcnt, 1);
428 dprint(DBG_BNODE_REFS, "new_node(%d:%d): 1\n",
429 node->tree->cnid, node->this);
430 init_waitqueue_head(&node->lock_wq);
431 spin_lock(&tree->hash_lock);
432 node2 = hfs_bnode_findhash(tree, cnid);
434 hash = hfs_bnode_hash(cnid);
435 node->next_hash = tree->node_hash[hash];
436 tree->node_hash[hash] = node;
437 tree->node_hash_cnt++;
439 spin_unlock(&tree->hash_lock);
441 wait_event(node2->lock_wq,
442 !test_bit(HFS_BNODE_NEW, &node2->flags));
445 spin_unlock(&tree->hash_lock);
447 mapping = tree->inode->i_mapping;
448 off = (loff_t)cnid << tree->node_size_shift;
449 block = off >> PAGE_CACHE_SHIFT;
450 node->page_offset = off & ~PAGE_CACHE_MASK;
451 for (i = 0; i < tree->pages_per_bnode; block++, i++) {
452 page = read_mapping_page(mapping, block, NULL);
455 if (PageError(page)) {
456 page_cache_release(page);
459 page_cache_release(page);
460 node->page[i] = page;
465 set_bit(HFS_BNODE_ERROR, &node->flags);
469 void hfs_bnode_unhash(struct hfs_bnode *node)
471 struct hfs_bnode **p;
473 dprint(DBG_BNODE_REFS, "remove_node(%d:%d): %d\n",
474 node->tree->cnid, node->this, atomic_read(&node->refcnt));
475 for (p = &node->tree->node_hash[hfs_bnode_hash(node->this)];
476 *p && *p != node; p = &(*p)->next_hash)
479 *p = node->next_hash;
480 node->tree->node_hash_cnt--;
483 /* Load a particular node out of a tree */
484 struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
486 struct hfs_bnode *node;
487 struct hfs_bnode_desc *desc;
488 int i, rec_off, off, next_off;
489 int entry_size, key_size;
491 spin_lock(&tree->hash_lock);
492 node = hfs_bnode_findhash(tree, num);
495 spin_unlock(&tree->hash_lock);
496 wait_event(node->lock_wq,
497 !test_bit(HFS_BNODE_NEW, &node->flags));
498 if (test_bit(HFS_BNODE_ERROR, &node->flags))
502 spin_unlock(&tree->hash_lock);
503 node = __hfs_bnode_create(tree, num);
505 return ERR_PTR(-ENOMEM);
506 if (test_bit(HFS_BNODE_ERROR, &node->flags))
508 if (!test_bit(HFS_BNODE_NEW, &node->flags))
511 desc = (struct hfs_bnode_desc *)(kmap(node->page[0]) +
513 node->prev = be32_to_cpu(desc->prev);
514 node->next = be32_to_cpu(desc->next);
515 node->num_recs = be16_to_cpu(desc->num_recs);
516 node->type = desc->type;
517 node->height = desc->height;
518 kunmap(node->page[0]);
520 switch (node->type) {
521 case HFS_NODE_HEADER:
523 if (node->height != 0)
527 if (node->height != 1)
531 if (node->height <= 1 || node->height > tree->depth)
538 rec_off = tree->node_size - 2;
539 off = hfs_bnode_read_u16(node, rec_off);
540 if (off != sizeof(struct hfs_bnode_desc))
542 for (i = 1; i <= node->num_recs; off = next_off, i++) {
544 next_off = hfs_bnode_read_u16(node, rec_off);
545 if (next_off <= off ||
546 next_off > tree->node_size ||
549 entry_size = next_off - off;
550 if (node->type != HFS_NODE_INDEX &&
551 node->type != HFS_NODE_LEAF)
553 key_size = hfs_bnode_read_u16(node, off) + 2;
554 if (key_size >= entry_size || key_size & 1)
557 clear_bit(HFS_BNODE_NEW, &node->flags);
558 wake_up(&node->lock_wq);
562 set_bit(HFS_BNODE_ERROR, &node->flags);
563 clear_bit(HFS_BNODE_NEW, &node->flags);
564 wake_up(&node->lock_wq);
566 return ERR_PTR(-EIO);
569 void hfs_bnode_free(struct hfs_bnode *node)
574 for (i = 0; i < node->tree->pages_per_bnode; i++)
576 page_cache_release(node->page[i]);
581 struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
583 struct hfs_bnode *node;
587 spin_lock(&tree->hash_lock);
588 node = hfs_bnode_findhash(tree, num);
589 spin_unlock(&tree->hash_lock);
591 printk(KERN_CRIT "new node %u already hashed?\n", num);
595 node = __hfs_bnode_create(tree, num);
597 return ERR_PTR(-ENOMEM);
598 if (test_bit(HFS_BNODE_ERROR, &node->flags)) {
600 return ERR_PTR(-EIO);
604 memset(kmap(*pagep) + node->page_offset, 0,
605 min((int)PAGE_CACHE_SIZE, (int)tree->node_size));
606 set_page_dirty(*pagep);
608 for (i = 1; i < tree->pages_per_bnode; i++) {
609 memset(kmap(*++pagep), 0, PAGE_CACHE_SIZE);
610 set_page_dirty(*pagep);
613 clear_bit(HFS_BNODE_NEW, &node->flags);
614 wake_up(&node->lock_wq);
619 void hfs_bnode_get(struct hfs_bnode *node)
622 atomic_inc(&node->refcnt);
623 dprint(DBG_BNODE_REFS, "get_node(%d:%d): %d\n",
624 node->tree->cnid, node->this,
625 atomic_read(&node->refcnt));
629 /* Dispose of resources used by a node */
630 void hfs_bnode_put(struct hfs_bnode *node)
633 struct hfs_btree *tree = node->tree;
636 dprint(DBG_BNODE_REFS, "put_node(%d:%d): %d\n",
637 node->tree->cnid, node->this,
638 atomic_read(&node->refcnt));
639 BUG_ON(!atomic_read(&node->refcnt));
640 if (!atomic_dec_and_lock(&node->refcnt, &tree->hash_lock))
642 for (i = 0; i < tree->pages_per_bnode; i++) {
645 mark_page_accessed(node->page[i]);
648 if (test_bit(HFS_BNODE_DELETED, &node->flags)) {
649 hfs_bnode_unhash(node);
650 spin_unlock(&tree->hash_lock);
651 hfs_bnode_clear(node, 0,
652 PAGE_CACHE_SIZE * tree->pages_per_bnode);
654 hfs_bnode_free(node);
657 spin_unlock(&tree->hash_lock);