1 // SPDX-License-Identifier: GPL-2.0
3 * linux/fs/hfsplus/bnode.c
6 * Brad Boyer (flar@allandria.com)
7 * (C) 2003 Ardis Technologies <roman@ardistech.com>
9 * Handle basic btree node operations
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/pagemap.h>
16 #include <linux/swap.h>
18 #include "hfsplus_fs.h"
19 #include "hfsplus_raw.h"
21 /* Copy a specified range of bytes from the raw data of a node */
22 void hfs_bnode_read(struct hfs_bnode *node, void *buf, int off, int len)
27 off += node->page_offset;
28 pagep = node->page + (off >> PAGE_SHIFT);
31 l = min_t(int, len, PAGE_SIZE - off);
32 memcpy(buf, kmap(*pagep) + off, l);
35 while ((len -= l) != 0) {
37 l = min_t(int, len, PAGE_SIZE);
38 memcpy(buf, kmap(*++pagep), l);
43 u16 hfs_bnode_read_u16(struct hfs_bnode *node, int off)
46 /* TODO: optimize later... */
47 hfs_bnode_read(node, &data, off, 2);
48 return be16_to_cpu(data);
51 u8 hfs_bnode_read_u8(struct hfs_bnode *node, int off)
54 /* TODO: optimize later... */
55 hfs_bnode_read(node, &data, off, 1);
59 void hfs_bnode_read_key(struct hfs_bnode *node, void *key, int off)
61 struct hfs_btree *tree;
65 if (node->type == HFS_NODE_LEAF ||
66 tree->attributes & HFS_TREE_VARIDXKEYS ||
67 node->tree->cnid == HFSPLUS_ATTR_CNID)
68 key_len = hfs_bnode_read_u16(node, off) + 2;
70 key_len = tree->max_key_len + 2;
72 hfs_bnode_read(node, key, off, key_len);
75 void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
80 off += node->page_offset;
81 pagep = node->page + (off >> PAGE_SHIFT);
84 l = min_t(int, len, PAGE_SIZE - off);
85 memcpy(kmap(*pagep) + off, buf, l);
86 set_page_dirty(*pagep);
89 while ((len -= l) != 0) {
91 l = min_t(int, len, PAGE_SIZE);
92 memcpy(kmap(*++pagep), buf, l);
93 set_page_dirty(*pagep);
98 void hfs_bnode_write_u16(struct hfs_bnode *node, int off, u16 data)
100 __be16 v = cpu_to_be16(data);
101 /* TODO: optimize later... */
102 hfs_bnode_write(node, &v, off, 2);
105 void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
110 off += node->page_offset;
111 pagep = node->page + (off >> PAGE_SHIFT);
114 l = min_t(int, len, PAGE_SIZE - off);
115 memset(kmap(*pagep) + off, 0, l);
116 set_page_dirty(*pagep);
119 while ((len -= l) != 0) {
120 l = min_t(int, len, PAGE_SIZE);
121 memset(kmap(*++pagep), 0, l);
122 set_page_dirty(*pagep);
127 void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
128 struct hfs_bnode *src_node, int src, int len)
130 struct page **src_page, **dst_page;
133 hfs_dbg(BNODE_MOD, "copybytes: %u,%u,%u\n", dst, src, len);
136 src += src_node->page_offset;
137 dst += dst_node->page_offset;
138 src_page = src_node->page + (src >> PAGE_SHIFT);
140 dst_page = dst_node->page + (dst >> PAGE_SHIFT);
144 l = min_t(int, len, PAGE_SIZE - src);
145 memcpy(kmap(*dst_page) + src, kmap(*src_page) + src, l);
147 set_page_dirty(*dst_page);
150 while ((len -= l) != 0) {
151 l = min_t(int, len, PAGE_SIZE);
152 memcpy(kmap(*++dst_page), kmap(*++src_page), l);
154 set_page_dirty(*dst_page);
158 void *src_ptr, *dst_ptr;
161 src_ptr = kmap(*src_page) + src;
162 dst_ptr = kmap(*dst_page) + dst;
163 if (PAGE_SIZE - src < PAGE_SIZE - dst) {
173 memcpy(dst_ptr, src_ptr, l);
175 set_page_dirty(*dst_page);
181 } while ((len -= l));
185 void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
187 struct page **src_page, **dst_page;
190 hfs_dbg(BNODE_MOD, "movebytes: %u,%u,%u\n", dst, src, len);
193 src += node->page_offset;
194 dst += node->page_offset;
197 src_page = node->page + (src >> PAGE_SHIFT);
198 src = (src & ~PAGE_MASK) + 1;
200 dst_page = node->page + (dst >> PAGE_SHIFT);
201 dst = (dst & ~PAGE_MASK) + 1;
205 memmove(kmap(*dst_page), kmap(*src_page), src);
207 set_page_dirty(*dst_page);
215 memmove(kmap(*dst_page) + src,
216 kmap(*src_page) + src, len);
218 set_page_dirty(*dst_page);
221 void *src_ptr, *dst_ptr;
224 src_ptr = kmap(*src_page) + src;
225 dst_ptr = kmap(*dst_page) + dst;
236 memmove(dst_ptr - l, src_ptr - l, l);
238 set_page_dirty(*dst_page);
240 if (dst == PAGE_SIZE)
244 } while ((len -= l));
247 src_page = node->page + (src >> PAGE_SHIFT);
249 dst_page = node->page + (dst >> PAGE_SHIFT);
253 l = min_t(int, len, PAGE_SIZE - src);
254 memmove(kmap(*dst_page) + src,
255 kmap(*src_page) + src, l);
257 set_page_dirty(*dst_page);
260 while ((len -= l) != 0) {
261 l = min_t(int, len, PAGE_SIZE);
262 memmove(kmap(*++dst_page),
263 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_SIZE - src <
285 memmove(dst_ptr, src_ptr, l);
287 set_page_dirty(*dst_page);
293 } while ((len -= l));
298 void hfs_bnode_dump(struct hfs_bnode *node)
300 struct hfs_bnode_desc desc;
304 hfs_dbg(BNODE_MOD, "bnode: %d\n", node->this);
305 hfs_bnode_read(node, &desc, 0, sizeof(desc));
306 hfs_dbg(BNODE_MOD, "%d, %d, %d, %d, %d\n",
307 be32_to_cpu(desc.next), be32_to_cpu(desc.prev),
308 desc.type, desc.height, be16_to_cpu(desc.num_recs));
310 off = node->tree->node_size - 2;
311 for (i = be16_to_cpu(desc.num_recs); i >= 0; off -= 2, i--) {
312 key_off = hfs_bnode_read_u16(node, off);
313 hfs_dbg(BNODE_MOD, " %d", key_off);
314 if (i && node->type == HFS_NODE_INDEX) {
317 if (node->tree->attributes & HFS_TREE_VARIDXKEYS ||
318 node->tree->cnid == HFSPLUS_ATTR_CNID)
319 tmp = hfs_bnode_read_u16(node, key_off) + 2;
321 tmp = node->tree->max_key_len + 2;
322 hfs_dbg_cont(BNODE_MOD, " (%d", tmp);
323 hfs_bnode_read(node, &cnid, key_off + tmp, 4);
324 hfs_dbg_cont(BNODE_MOD, ",%d)", be32_to_cpu(cnid));
325 } else if (i && node->type == HFS_NODE_LEAF) {
328 tmp = hfs_bnode_read_u16(node, key_off);
329 hfs_dbg_cont(BNODE_MOD, " (%d)", tmp);
332 hfs_dbg_cont(BNODE_MOD, "\n");
335 void hfs_bnode_unlink(struct hfs_bnode *node)
337 struct hfs_btree *tree;
338 struct hfs_bnode *tmp;
343 tmp = hfs_bnode_find(tree, node->prev);
346 tmp->next = node->next;
347 cnid = cpu_to_be32(tmp->next);
348 hfs_bnode_write(tmp, &cnid,
349 offsetof(struct hfs_bnode_desc, next), 4);
351 } else if (node->type == HFS_NODE_LEAF)
352 tree->leaf_head = node->next;
355 tmp = hfs_bnode_find(tree, node->next);
358 tmp->prev = node->prev;
359 cnid = cpu_to_be32(tmp->prev);
360 hfs_bnode_write(tmp, &cnid,
361 offsetof(struct hfs_bnode_desc, prev), 4);
363 } else if (node->type == HFS_NODE_LEAF)
364 tree->leaf_tail = node->prev;
367 if (!node->prev && !node->next)
368 hfs_dbg(BNODE_MOD, "hfs_btree_del_level\n");
373 set_bit(HFS_BNODE_DELETED, &node->flags);
376 static inline int hfs_bnode_hash(u32 num)
378 num = (num >> 16) + num;
380 return num & (NODE_HASH_SIZE - 1);
383 struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid)
385 struct hfs_bnode *node;
387 if (cnid >= tree->node_count) {
388 pr_err("request for non-existent node %d in B*Tree\n",
393 for (node = tree->node_hash[hfs_bnode_hash(cnid)];
394 node; node = node->next_hash)
395 if (node->this == cnid)
400 static struct hfs_bnode *__hfs_bnode_create(struct hfs_btree *tree, u32 cnid)
402 struct hfs_bnode *node, *node2;
403 struct address_space *mapping;
405 int size, block, i, hash;
408 if (cnid >= tree->node_count) {
409 pr_err("request for non-existent node %d in B*Tree\n",
414 size = sizeof(struct hfs_bnode) + tree->pages_per_bnode *
415 sizeof(struct page *);
416 node = kzalloc(size, GFP_KERNEL);
421 set_bit(HFS_BNODE_NEW, &node->flags);
422 atomic_set(&node->refcnt, 1);
423 hfs_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,
437 !test_bit(HFS_BNODE_NEW, &node2->flags));
440 spin_unlock(&tree->hash_lock);
442 mapping = tree->inode->i_mapping;
443 off = (loff_t)cnid << tree->node_size_shift;
444 block = off >> PAGE_SHIFT;
445 node->page_offset = off & ~PAGE_MASK;
446 for (i = 0; i < tree->pages_per_bnode; block++, i++) {
447 page = read_mapping_page(mapping, block, NULL);
450 if (PageError(page)) {
454 node->page[i] = page;
459 set_bit(HFS_BNODE_ERROR, &node->flags);
463 void hfs_bnode_unhash(struct hfs_bnode *node)
465 struct hfs_bnode **p;
467 hfs_dbg(BNODE_REFS, "remove_node(%d:%d): %d\n",
468 node->tree->cnid, node->this, atomic_read(&node->refcnt));
469 for (p = &node->tree->node_hash[hfs_bnode_hash(node->this)];
470 *p && *p != node; p = &(*p)->next_hash)
473 *p = node->next_hash;
474 node->tree->node_hash_cnt--;
477 /* Load a particular node out of a tree */
478 struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
480 struct hfs_bnode *node;
481 struct hfs_bnode_desc *desc;
482 int i, rec_off, off, next_off;
483 int entry_size, key_size;
485 spin_lock(&tree->hash_lock);
486 node = hfs_bnode_findhash(tree, num);
489 spin_unlock(&tree->hash_lock);
490 wait_event(node->lock_wq,
491 !test_bit(HFS_BNODE_NEW, &node->flags));
492 if (test_bit(HFS_BNODE_ERROR, &node->flags))
496 spin_unlock(&tree->hash_lock);
497 node = __hfs_bnode_create(tree, num);
499 return ERR_PTR(-ENOMEM);
500 if (test_bit(HFS_BNODE_ERROR, &node->flags))
502 if (!test_bit(HFS_BNODE_NEW, &node->flags))
505 desc = (struct hfs_bnode_desc *)(kmap(node->page[0]) +
507 node->prev = be32_to_cpu(desc->prev);
508 node->next = be32_to_cpu(desc->next);
509 node->num_recs = be16_to_cpu(desc->num_recs);
510 node->type = desc->type;
511 node->height = desc->height;
512 kunmap(node->page[0]);
514 switch (node->type) {
515 case HFS_NODE_HEADER:
517 if (node->height != 0)
521 if (node->height != 1)
525 if (node->height <= 1 || node->height > tree->depth)
532 rec_off = tree->node_size - 2;
533 off = hfs_bnode_read_u16(node, rec_off);
534 if (off != sizeof(struct hfs_bnode_desc))
536 for (i = 1; i <= node->num_recs; off = next_off, i++) {
538 next_off = hfs_bnode_read_u16(node, rec_off);
539 if (next_off <= off ||
540 next_off > tree->node_size ||
543 entry_size = next_off - off;
544 if (node->type != HFS_NODE_INDEX &&
545 node->type != HFS_NODE_LEAF)
547 key_size = hfs_bnode_read_u16(node, off) + 2;
548 if (key_size >= entry_size || key_size & 1)
551 clear_bit(HFS_BNODE_NEW, &node->flags);
552 wake_up(&node->lock_wq);
556 set_bit(HFS_BNODE_ERROR, &node->flags);
557 clear_bit(HFS_BNODE_NEW, &node->flags);
558 wake_up(&node->lock_wq);
560 return ERR_PTR(-EIO);
563 void hfs_bnode_free(struct hfs_bnode *node)
567 for (i = 0; i < node->tree->pages_per_bnode; i++)
569 put_page(node->page[i]);
573 struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
575 struct hfs_bnode *node;
579 spin_lock(&tree->hash_lock);
580 node = hfs_bnode_findhash(tree, num);
581 spin_unlock(&tree->hash_lock);
583 pr_crit("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_t(int, PAGE_SIZE, tree->node_size));
598 set_page_dirty(*pagep);
600 for (i = 1; i < tree->pages_per_bnode; i++) {
601 memset(kmap(*++pagep), 0, PAGE_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);
615 hfs_dbg(BNODE_REFS, "get_node(%d:%d): %d\n",
616 node->tree->cnid, node->this,
617 atomic_read(&node->refcnt));
621 /* Dispose of resources used by a node */
622 void hfs_bnode_put(struct hfs_bnode *node)
625 struct hfs_btree *tree = node->tree;
628 hfs_dbg(BNODE_REFS, "put_node(%d:%d): %d\n",
629 node->tree->cnid, node->this,
630 atomic_read(&node->refcnt));
631 BUG_ON(!atomic_read(&node->refcnt));
632 if (!atomic_dec_and_lock(&node->refcnt, &tree->hash_lock))
634 for (i = 0; i < tree->pages_per_bnode; i++) {
637 mark_page_accessed(node->page[i]);
640 if (test_bit(HFS_BNODE_DELETED, &node->flags)) {
641 hfs_bnode_unhash(node);
642 spin_unlock(&tree->hash_lock);
643 if (hfs_bnode_need_zeroout(tree))
644 hfs_bnode_clear(node, 0, tree->node_size);
646 hfs_bnode_free(node);
649 spin_unlock(&tree->hash_lock);
654 * Unused nodes have to be zeroed if this is the catalog tree and
655 * a corresponding flag in the volume header is set.
657 bool hfs_bnode_need_zeroout(struct hfs_btree *tree)
659 struct super_block *sb = tree->inode->i_sb;
660 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
661 const u32 volume_attr = be32_to_cpu(sbi->s_vhdr->attributes);
663 return tree->cnid == HFSPLUS_CAT_CNID &&
664 volume_attr & HFSPLUS_VOL_UNUSED_NODE_FIX;