btree.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * linux/fs/hfs/btree.h
  4. *
  5. * Copyright (C) 2001
  6. * Brad Boyer (flar@allandria.com)
  7. * (C) 2003 Ardis Technologies <roman@ardistech.com>
  8. */
  9. #include "hfs_fs.h"
  10. typedef int (*btree_keycmp)(const btree_key *, const btree_key *);
  11. #define NODE_HASH_SIZE 256
  12. /* A HFS BTree held in memory */
  13. struct hfs_btree {
  14. struct super_block *sb;
  15. struct inode *inode;
  16. btree_keycmp keycmp;
  17. u32 cnid;
  18. u32 root;
  19. u32 leaf_count;
  20. u32 leaf_head;
  21. u32 leaf_tail;
  22. u32 node_count;
  23. u32 free_nodes;
  24. u32 attributes;
  25. unsigned int node_size;
  26. unsigned int node_size_shift;
  27. unsigned int max_key_len;
  28. unsigned int depth;
  29. //unsigned int map1_size, map_size;
  30. struct mutex tree_lock;
  31. unsigned int pages_per_bnode;
  32. spinlock_t hash_lock;
  33. struct hfs_bnode *node_hash[NODE_HASH_SIZE];
  34. int node_hash_cnt;
  35. };
  36. /* A HFS BTree node in memory */
  37. struct hfs_bnode {
  38. struct hfs_btree *tree;
  39. u32 prev;
  40. u32 this;
  41. u32 next;
  42. u32 parent;
  43. u16 num_recs;
  44. u8 type;
  45. u8 height;
  46. struct hfs_bnode *next_hash;
  47. unsigned long flags;
  48. wait_queue_head_t lock_wq;
  49. atomic_t refcnt;
  50. unsigned int page_offset;
  51. struct page *page[0];
  52. };
  53. #define HFS_BNODE_ERROR 0
  54. #define HFS_BNODE_NEW 1
  55. #define HFS_BNODE_DELETED 2
  56. struct hfs_find_data {
  57. btree_key *key;
  58. btree_key *search_key;
  59. struct hfs_btree *tree;
  60. struct hfs_bnode *bnode;
  61. int record;
  62. int keyoffset, keylength;
  63. int entryoffset, entrylength;
  64. };
  65. /* btree.c */
  66. extern struct hfs_btree *hfs_btree_open(struct super_block *, u32, btree_keycmp);
  67. extern void hfs_btree_close(struct hfs_btree *);
  68. extern void hfs_btree_write(struct hfs_btree *);
  69. extern int hfs_bmap_reserve(struct hfs_btree *, int);
  70. extern struct hfs_bnode * hfs_bmap_alloc(struct hfs_btree *);
  71. extern void hfs_bmap_free(struct hfs_bnode *node);
  72. /* bnode.c */
  73. extern void hfs_bnode_read(struct hfs_bnode *, void *, int, int);
  74. extern u16 hfs_bnode_read_u16(struct hfs_bnode *, int);
  75. extern u8 hfs_bnode_read_u8(struct hfs_bnode *, int);
  76. extern void hfs_bnode_read_key(struct hfs_bnode *, void *, int);
  77. extern void hfs_bnode_write(struct hfs_bnode *, void *, int, int);
  78. extern void hfs_bnode_write_u16(struct hfs_bnode *, int, u16);
  79. extern void hfs_bnode_write_u8(struct hfs_bnode *, int, u8);
  80. extern void hfs_bnode_clear(struct hfs_bnode *, int, int);
  81. extern void hfs_bnode_copy(struct hfs_bnode *, int,
  82. struct hfs_bnode *, int, int);
  83. extern void hfs_bnode_move(struct hfs_bnode *, int, int, int);
  84. extern void hfs_bnode_dump(struct hfs_bnode *);
  85. extern void hfs_bnode_unlink(struct hfs_bnode *);
  86. extern struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *, u32);
  87. extern struct hfs_bnode *hfs_bnode_find(struct hfs_btree *, u32);
  88. extern void hfs_bnode_unhash(struct hfs_bnode *);
  89. extern void hfs_bnode_free(struct hfs_bnode *);
  90. extern struct hfs_bnode *hfs_bnode_create(struct hfs_btree *, u32);
  91. extern void hfs_bnode_get(struct hfs_bnode *);
  92. extern void hfs_bnode_put(struct hfs_bnode *);
  93. /* brec.c */
  94. extern u16 hfs_brec_lenoff(struct hfs_bnode *, u16, u16 *);
  95. extern u16 hfs_brec_keylen(struct hfs_bnode *, u16);
  96. extern int hfs_brec_insert(struct hfs_find_data *, void *, int);
  97. extern int hfs_brec_remove(struct hfs_find_data *);
  98. /* bfind.c */
  99. extern int hfs_find_init(struct hfs_btree *, struct hfs_find_data *);
  100. extern void hfs_find_exit(struct hfs_find_data *);
  101. extern int __hfs_brec_find(struct hfs_bnode *, struct hfs_find_data *);
  102. extern int hfs_brec_find(struct hfs_find_data *);
  103. extern int hfs_brec_read(struct hfs_find_data *, void *, int);
  104. extern int hfs_brec_goto(struct hfs_find_data *, int);
  105. struct hfs_bnode_desc {
  106. __be32 next; /* (V) Number of the next node at this level */
  107. __be32 prev; /* (V) Number of the prev node at this level */
  108. u8 type; /* (F) The type of node */
  109. u8 height; /* (F) The level of this node (leaves=1) */
  110. __be16 num_recs; /* (V) The number of records in this node */
  111. u16 reserved;
  112. } __packed;
  113. #define HFS_NODE_INDEX 0x00 /* An internal (index) node */
  114. #define HFS_NODE_HEADER 0x01 /* The tree header node (node 0) */
  115. #define HFS_NODE_MAP 0x02 /* Holds part of the bitmap of used nodes */
  116. #define HFS_NODE_LEAF 0xFF /* A leaf (ndNHeight==1) node */
  117. struct hfs_btree_header_rec {
  118. __be16 depth; /* (V) The number of levels in this B-tree */
  119. __be32 root; /* (V) The node number of the root node */
  120. __be32 leaf_count; /* (V) The number of leaf records */
  121. __be32 leaf_head; /* (V) The number of the first leaf node */
  122. __be32 leaf_tail; /* (V) The number of the last leaf node */
  123. __be16 node_size; /* (F) The number of bytes in a node (=512) */
  124. __be16 max_key_len; /* (F) The length of a key in an index node */
  125. __be32 node_count; /* (V) The total number of nodes */
  126. __be32 free_nodes; /* (V) The number of unused nodes */
  127. u16 reserved1;
  128. __be32 clump_size; /* (F) clump size. not usually used. */
  129. u8 btree_type; /* (F) BTree type */
  130. u8 reserved2;
  131. __be32 attributes; /* (F) attributes */
  132. u32 reserved3[16];
  133. } __packed;
  134. #define BTREE_ATTR_BADCLOSE 0x00000001 /* b-tree not closed properly. not
  135. used by hfsplus. */
  136. #define HFS_TREE_BIGKEYS 0x00000002 /* key length is u16 instead of u8.
  137. used by hfsplus. */
  138. #define HFS_TREE_VARIDXKEYS 0x00000004 /* variable key length instead of
  139. max key length. use din catalog
  140. b-tree but not in extents
  141. b-tree (hfsplus). */