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_from_page(buf, *pagep, off, l);
34 while ((len -= l) != 0) {
36 l = min_t(int, len, PAGE_SIZE);
37 memcpy_from_page(buf, *++pagep, 0, l);
41 u16 hfs_bnode_read_u16(struct hfs_bnode *node, int off)
44 /* TODO: optimize later... */
45 hfs_bnode_read(node, &data, off, 2);
46 return be16_to_cpu(data);
49 u8 hfs_bnode_read_u8(struct hfs_bnode *node, int off)
52 /* TODO: optimize later... */
53 hfs_bnode_read(node, &data, off, 1);
57 void hfs_bnode_read_key(struct hfs_bnode *node, void *key, int off)
59 struct hfs_btree *tree;
63 if (node->type == HFS_NODE_LEAF ||
64 tree->attributes & HFS_TREE_VARIDXKEYS ||
65 node->tree->cnid == HFSPLUS_ATTR_CNID)
66 key_len = hfs_bnode_read_u16(node, off) + 2;
68 key_len = tree->max_key_len + 2;
70 hfs_bnode_read(node, key, off, key_len);
73 void hfs_bnode_write(struct hfs_bnode *node, void *buf, int off, int len)
78 off += node->page_offset;
79 pagep = node->page + (off >> PAGE_SHIFT);
82 l = min_t(int, len, PAGE_SIZE - off);
83 memcpy_to_page(*pagep, off, buf, l);
84 set_page_dirty(*pagep);
86 while ((len -= l) != 0) {
88 l = min_t(int, len, PAGE_SIZE);
89 memcpy_to_page(*++pagep, 0, buf, l);
90 set_page_dirty(*pagep);
94 void hfs_bnode_write_u16(struct hfs_bnode *node, int off, u16 data)
96 __be16 v = cpu_to_be16(data);
97 /* TODO: optimize later... */
98 hfs_bnode_write(node, &v, off, 2);
101 void hfs_bnode_clear(struct hfs_bnode *node, int off, int len)
106 off += node->page_offset;
107 pagep = node->page + (off >> PAGE_SHIFT);
110 l = min_t(int, len, PAGE_SIZE - off);
111 memzero_page(*pagep, off, l);
112 set_page_dirty(*pagep);
114 while ((len -= l) != 0) {
115 l = min_t(int, len, PAGE_SIZE);
116 memzero_page(*++pagep, 0, l);
117 set_page_dirty(*pagep);
121 void hfs_bnode_copy(struct hfs_bnode *dst_node, int dst,
122 struct hfs_bnode *src_node, int src, int len)
124 struct page **src_page, **dst_page;
127 hfs_dbg(BNODE_MOD, "copybytes: %u,%u,%u\n", dst, src, len);
130 src += src_node->page_offset;
131 dst += dst_node->page_offset;
132 src_page = src_node->page + (src >> PAGE_SHIFT);
134 dst_page = dst_node->page + (dst >> PAGE_SHIFT);
138 l = min_t(int, len, PAGE_SIZE - src);
139 memcpy_page(*dst_page, src, *src_page, src, l);
140 set_page_dirty(*dst_page);
142 while ((len -= l) != 0) {
143 l = min_t(int, len, PAGE_SIZE);
144 memcpy_page(*++dst_page, 0, *++src_page, 0, l);
145 set_page_dirty(*dst_page);
148 void *src_ptr, *dst_ptr;
151 dst_ptr = kmap_local_page(*dst_page) + dst;
152 src_ptr = kmap_local_page(*src_page) + src;
153 if (PAGE_SIZE - src < PAGE_SIZE - dst) {
163 memcpy(dst_ptr, src_ptr, l);
164 kunmap_local(src_ptr);
165 set_page_dirty(*dst_page);
166 kunmap_local(dst_ptr);
171 } while ((len -= l));
175 void hfs_bnode_move(struct hfs_bnode *node, int dst, int src, int len)
177 struct page **src_page, **dst_page;
178 void *src_ptr, *dst_ptr;
181 hfs_dbg(BNODE_MOD, "movebytes: %u,%u,%u\n", dst, src, len);
184 src += node->page_offset;
185 dst += node->page_offset;
188 src_page = node->page + (src >> PAGE_SHIFT);
189 src = (src & ~PAGE_MASK) + 1;
191 dst_page = node->page + (dst >> PAGE_SHIFT);
192 dst = (dst & ~PAGE_MASK) + 1;
196 dst_ptr = kmap_local_page(*dst_page);
197 src_ptr = kmap_local_page(*src_page);
198 memmove(dst_ptr, src_ptr, src);
199 kunmap_local(src_ptr);
200 set_page_dirty(*dst_page);
201 kunmap_local(dst_ptr);
208 dst_ptr = kmap_local_page(*dst_page);
209 src_ptr = kmap_local_page(*src_page);
210 memmove(dst_ptr + src, src_ptr + src, len);
211 kunmap_local(src_ptr);
212 set_page_dirty(*dst_page);
213 kunmap_local(dst_ptr);
216 dst_ptr = kmap_local_page(*dst_page) + dst;
217 src_ptr = kmap_local_page(*src_page) + src;
228 memmove(dst_ptr - l, src_ptr - l, l);
229 kunmap_local(src_ptr);
230 set_page_dirty(*dst_page);
231 kunmap_local(dst_ptr);
232 if (dst == PAGE_SIZE)
236 } while ((len -= l));
239 src_page = node->page + (src >> PAGE_SHIFT);
241 dst_page = node->page + (dst >> PAGE_SHIFT);
245 l = min_t(int, len, PAGE_SIZE - src);
247 dst_ptr = kmap_local_page(*dst_page) + src;
248 src_ptr = kmap_local_page(*src_page) + src;
249 memmove(dst_ptr, src_ptr, l);
250 kunmap_local(src_ptr);
251 set_page_dirty(*dst_page);
252 kunmap_local(dst_ptr);
254 while ((len -= l) != 0) {
255 l = min_t(int, len, PAGE_SIZE);
256 dst_ptr = kmap_local_page(*++dst_page);
257 src_ptr = kmap_local_page(*++src_page);
258 memmove(dst_ptr, src_ptr, l);
259 kunmap_local(src_ptr);
260 set_page_dirty(*dst_page);
261 kunmap_local(dst_ptr);
265 dst_ptr = kmap_local_page(*dst_page) + dst;
266 src_ptr = kmap_local_page(*src_page) + src;
267 if (PAGE_SIZE - src <
278 memmove(dst_ptr, src_ptr, l);
279 kunmap_local(src_ptr);
280 set_page_dirty(*dst_page);
281 kunmap_local(dst_ptr);
286 } while ((len -= l));
291 void hfs_bnode_dump(struct hfs_bnode *node)
293 struct hfs_bnode_desc desc;
297 hfs_dbg(BNODE_MOD, "bnode: %d\n", node->this);
298 hfs_bnode_read(node, &desc, 0, sizeof(desc));
299 hfs_dbg(BNODE_MOD, "%d, %d, %d, %d, %d\n",
300 be32_to_cpu(desc.next), be32_to_cpu(desc.prev),
301 desc.type, desc.height, be16_to_cpu(desc.num_recs));
303 off = node->tree->node_size - 2;
304 for (i = be16_to_cpu(desc.num_recs); i >= 0; off -= 2, i--) {
305 key_off = hfs_bnode_read_u16(node, off);
306 hfs_dbg(BNODE_MOD, " %d", key_off);
307 if (i && node->type == HFS_NODE_INDEX) {
310 if (node->tree->attributes & HFS_TREE_VARIDXKEYS ||
311 node->tree->cnid == HFSPLUS_ATTR_CNID)
312 tmp = hfs_bnode_read_u16(node, key_off) + 2;
314 tmp = node->tree->max_key_len + 2;
315 hfs_dbg_cont(BNODE_MOD, " (%d", tmp);
316 hfs_bnode_read(node, &cnid, key_off + tmp, 4);
317 hfs_dbg_cont(BNODE_MOD, ",%d)", be32_to_cpu(cnid));
318 } else if (i && node->type == HFS_NODE_LEAF) {
321 tmp = hfs_bnode_read_u16(node, key_off);
322 hfs_dbg_cont(BNODE_MOD, " (%d)", tmp);
325 hfs_dbg_cont(BNODE_MOD, "\n");
328 void hfs_bnode_unlink(struct hfs_bnode *node)
330 struct hfs_btree *tree;
331 struct hfs_bnode *tmp;
336 tmp = hfs_bnode_find(tree, node->prev);
339 tmp->next = node->next;
340 cnid = cpu_to_be32(tmp->next);
341 hfs_bnode_write(tmp, &cnid,
342 offsetof(struct hfs_bnode_desc, next), 4);
344 } else if (node->type == HFS_NODE_LEAF)
345 tree->leaf_head = node->next;
348 tmp = hfs_bnode_find(tree, node->next);
351 tmp->prev = node->prev;
352 cnid = cpu_to_be32(tmp->prev);
353 hfs_bnode_write(tmp, &cnid,
354 offsetof(struct hfs_bnode_desc, prev), 4);
356 } else if (node->type == HFS_NODE_LEAF)
357 tree->leaf_tail = node->prev;
360 if (!node->prev && !node->next)
361 hfs_dbg(BNODE_MOD, "hfs_btree_del_level\n");
366 set_bit(HFS_BNODE_DELETED, &node->flags);
369 static inline int hfs_bnode_hash(u32 num)
371 num = (num >> 16) + num;
373 return num & (NODE_HASH_SIZE - 1);
376 struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *tree, u32 cnid)
378 struct hfs_bnode *node;
380 if (cnid >= tree->node_count) {
381 pr_err("request for non-existent node %d in B*Tree\n",
386 for (node = tree->node_hash[hfs_bnode_hash(cnid)];
387 node; node = node->next_hash)
388 if (node->this == cnid)
393 static struct hfs_bnode *__hfs_bnode_create(struct hfs_btree *tree, u32 cnid)
395 struct hfs_bnode *node, *node2;
396 struct address_space *mapping;
398 int size, block, i, hash;
401 if (cnid >= tree->node_count) {
402 pr_err("request for non-existent node %d in B*Tree\n",
407 size = sizeof(struct hfs_bnode) + tree->pages_per_bnode *
408 sizeof(struct page *);
409 node = kzalloc(size, GFP_KERNEL);
414 set_bit(HFS_BNODE_NEW, &node->flags);
415 atomic_set(&node->refcnt, 1);
416 hfs_dbg(BNODE_REFS, "new_node(%d:%d): 1\n",
417 node->tree->cnid, node->this);
418 init_waitqueue_head(&node->lock_wq);
419 spin_lock(&tree->hash_lock);
420 node2 = hfs_bnode_findhash(tree, cnid);
422 hash = hfs_bnode_hash(cnid);
423 node->next_hash = tree->node_hash[hash];
424 tree->node_hash[hash] = node;
425 tree->node_hash_cnt++;
427 spin_unlock(&tree->hash_lock);
429 wait_event(node2->lock_wq,
430 !test_bit(HFS_BNODE_NEW, &node2->flags));
433 spin_unlock(&tree->hash_lock);
435 mapping = tree->inode->i_mapping;
436 off = (loff_t)cnid << tree->node_size_shift;
437 block = off >> PAGE_SHIFT;
438 node->page_offset = off & ~PAGE_MASK;
439 for (i = 0; i < tree->pages_per_bnode; block++, i++) {
440 page = read_mapping_page(mapping, block, NULL);
443 node->page[i] = page;
448 set_bit(HFS_BNODE_ERROR, &node->flags);
452 void hfs_bnode_unhash(struct hfs_bnode *node)
454 struct hfs_bnode **p;
456 hfs_dbg(BNODE_REFS, "remove_node(%d:%d): %d\n",
457 node->tree->cnid, node->this, atomic_read(&node->refcnt));
458 for (p = &node->tree->node_hash[hfs_bnode_hash(node->this)];
459 *p && *p != node; p = &(*p)->next_hash)
462 *p = node->next_hash;
463 node->tree->node_hash_cnt--;
466 /* Load a particular node out of a tree */
467 struct hfs_bnode *hfs_bnode_find(struct hfs_btree *tree, u32 num)
469 struct hfs_bnode *node;
470 struct hfs_bnode_desc *desc;
471 int i, rec_off, off, next_off;
472 int entry_size, key_size;
474 spin_lock(&tree->hash_lock);
475 node = hfs_bnode_findhash(tree, num);
478 spin_unlock(&tree->hash_lock);
479 wait_event(node->lock_wq,
480 !test_bit(HFS_BNODE_NEW, &node->flags));
481 if (test_bit(HFS_BNODE_ERROR, &node->flags))
485 spin_unlock(&tree->hash_lock);
486 node = __hfs_bnode_create(tree, num);
488 return ERR_PTR(-ENOMEM);
489 if (test_bit(HFS_BNODE_ERROR, &node->flags))
491 if (!test_bit(HFS_BNODE_NEW, &node->flags))
494 desc = (struct hfs_bnode_desc *)(kmap_local_page(node->page[0]) +
496 node->prev = be32_to_cpu(desc->prev);
497 node->next = be32_to_cpu(desc->next);
498 node->num_recs = be16_to_cpu(desc->num_recs);
499 node->type = desc->type;
500 node->height = desc->height;
503 switch (node->type) {
504 case HFS_NODE_HEADER:
506 if (node->height != 0)
510 if (node->height != 1)
514 if (node->height <= 1 || node->height > tree->depth)
521 rec_off = tree->node_size - 2;
522 off = hfs_bnode_read_u16(node, rec_off);
523 if (off != sizeof(struct hfs_bnode_desc))
525 for (i = 1; i <= node->num_recs; off = next_off, i++) {
527 next_off = hfs_bnode_read_u16(node, rec_off);
528 if (next_off <= off ||
529 next_off > tree->node_size ||
532 entry_size = next_off - off;
533 if (node->type != HFS_NODE_INDEX &&
534 node->type != HFS_NODE_LEAF)
536 key_size = hfs_bnode_read_u16(node, off) + 2;
537 if (key_size >= entry_size || key_size & 1)
540 clear_bit(HFS_BNODE_NEW, &node->flags);
541 wake_up(&node->lock_wq);
545 set_bit(HFS_BNODE_ERROR, &node->flags);
546 clear_bit(HFS_BNODE_NEW, &node->flags);
547 wake_up(&node->lock_wq);
549 return ERR_PTR(-EIO);
552 void hfs_bnode_free(struct hfs_bnode *node)
556 for (i = 0; i < node->tree->pages_per_bnode; i++)
558 put_page(node->page[i]);
562 struct hfs_bnode *hfs_bnode_create(struct hfs_btree *tree, u32 num)
564 struct hfs_bnode *node;
568 spin_lock(&tree->hash_lock);
569 node = hfs_bnode_findhash(tree, num);
570 spin_unlock(&tree->hash_lock);
572 pr_crit("new node %u already hashed?\n", num);
576 node = __hfs_bnode_create(tree, num);
578 return ERR_PTR(-ENOMEM);
579 if (test_bit(HFS_BNODE_ERROR, &node->flags)) {
581 return ERR_PTR(-EIO);
585 memzero_page(*pagep, node->page_offset,
586 min_t(int, PAGE_SIZE, tree->node_size));
587 set_page_dirty(*pagep);
588 for (i = 1; i < tree->pages_per_bnode; i++) {
589 memzero_page(*++pagep, 0, PAGE_SIZE);
590 set_page_dirty(*pagep);
592 clear_bit(HFS_BNODE_NEW, &node->flags);
593 wake_up(&node->lock_wq);
598 void hfs_bnode_get(struct hfs_bnode *node)
601 atomic_inc(&node->refcnt);
602 hfs_dbg(BNODE_REFS, "get_node(%d:%d): %d\n",
603 node->tree->cnid, node->this,
604 atomic_read(&node->refcnt));
608 /* Dispose of resources used by a node */
609 void hfs_bnode_put(struct hfs_bnode *node)
612 struct hfs_btree *tree = node->tree;
615 hfs_dbg(BNODE_REFS, "put_node(%d:%d): %d\n",
616 node->tree->cnid, node->this,
617 atomic_read(&node->refcnt));
618 BUG_ON(!atomic_read(&node->refcnt));
619 if (!atomic_dec_and_lock(&node->refcnt, &tree->hash_lock))
621 for (i = 0; i < tree->pages_per_bnode; i++) {
624 mark_page_accessed(node->page[i]);
627 if (test_bit(HFS_BNODE_DELETED, &node->flags)) {
628 hfs_bnode_unhash(node);
629 spin_unlock(&tree->hash_lock);
630 if (hfs_bnode_need_zeroout(tree))
631 hfs_bnode_clear(node, 0, tree->node_size);
633 hfs_bnode_free(node);
636 spin_unlock(&tree->hash_lock);
641 * Unused nodes have to be zeroed if this is the catalog tree and
642 * a corresponding flag in the volume header is set.
644 bool hfs_bnode_need_zeroout(struct hfs_btree *tree)
646 struct super_block *sb = tree->inode->i_sb;
647 struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
648 const u32 volume_attr = be32_to_cpu(sbi->s_vhdr->attributes);
650 return tree->cnid == HFSPLUS_CAT_CNID &&
651 volume_attr & HFSPLUS_VOL_UNUSED_NODE_FIX;