blob: 4ba45caf593929d00b8d665687715521ce737eb1 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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
10#include "hfs_fs.h"
11
12typedef int (*btree_keycmp)(const btree_key *, const btree_key *);
13
14#define NODE_HASH_SIZE 256
15
16/* A HFS BTree held in memory */
17struct hfs_btree {
18 struct super_block *sb;
19 struct inode *inode;
20 btree_keycmp keycmp;
21
22 u32 cnid;
23 u32 root;
24 u32 leaf_count;
25 u32 leaf_head;
26 u32 leaf_tail;
27 u32 node_count;
28 u32 free_nodes;
29 u32 attributes;
30
31 unsigned int node_size;
32 unsigned int node_size_shift;
33 unsigned int max_key_len;
34 unsigned int depth;
35
36 //unsigned int map1_size, map_size;
Thomas Gleixner4a941032010-09-07 14:33:08 +000037 struct mutex tree_lock;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 unsigned int pages_per_bnode;
40 spinlock_t hash_lock;
41 struct hfs_bnode *node_hash[NODE_HASH_SIZE];
42 int node_hash_cnt;
43};
44
45/* A HFS BTree node in memory */
46struct hfs_bnode {
47 struct hfs_btree *tree;
48
49 u32 prev;
50 u32 this;
51 u32 next;
52 u32 parent;
53
54 u16 num_recs;
55 u8 type;
56 u8 height;
57
58 struct hfs_bnode *next_hash;
59 unsigned long flags;
60 wait_queue_head_t lock_wq;
61 atomic_t refcnt;
62 unsigned int page_offset;
Gustavo A. R. Silva5e01fdf2020-08-31 08:25:42 -050063 struct page *page[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
66#define HFS_BNODE_ERROR 0
67#define HFS_BNODE_NEW 1
68#define HFS_BNODE_DELETED 2
69
70struct hfs_find_data {
71 btree_key *key;
72 btree_key *search_key;
73 struct hfs_btree *tree;
74 struct hfs_bnode *bnode;
75 int record;
76 int keyoffset, keylength;
77 int entryoffset, entrylength;
78};
79
80
81/* btree.c */
82extern struct hfs_btree *hfs_btree_open(struct super_block *, u32, btree_keycmp);
83extern void hfs_btree_close(struct hfs_btree *);
84extern void hfs_btree_write(struct hfs_btree *);
Ernesto A. Fernández54640c72018-10-30 15:06:17 -070085extern int hfs_bmap_reserve(struct hfs_btree *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086extern struct hfs_bnode * hfs_bmap_alloc(struct hfs_btree *);
87extern void hfs_bmap_free(struct hfs_bnode *node);
88
89/* bnode.c */
90extern void hfs_bnode_read(struct hfs_bnode *, void *, int, int);
91extern u16 hfs_bnode_read_u16(struct hfs_bnode *, int);
92extern u8 hfs_bnode_read_u8(struct hfs_bnode *, int);
93extern void hfs_bnode_read_key(struct hfs_bnode *, void *, int);
94extern void hfs_bnode_write(struct hfs_bnode *, void *, int, int);
95extern void hfs_bnode_write_u16(struct hfs_bnode *, int, u16);
96extern void hfs_bnode_write_u8(struct hfs_bnode *, int, u8);
97extern void hfs_bnode_clear(struct hfs_bnode *, int, int);
98extern void hfs_bnode_copy(struct hfs_bnode *, int,
99 struct hfs_bnode *, int, int);
100extern void hfs_bnode_move(struct hfs_bnode *, int, int, int);
101extern void hfs_bnode_dump(struct hfs_bnode *);
102extern void hfs_bnode_unlink(struct hfs_bnode *);
103extern struct hfs_bnode *hfs_bnode_findhash(struct hfs_btree *, u32);
104extern struct hfs_bnode *hfs_bnode_find(struct hfs_btree *, u32);
105extern void hfs_bnode_unhash(struct hfs_bnode *);
106extern void hfs_bnode_free(struct hfs_bnode *);
107extern struct hfs_bnode *hfs_bnode_create(struct hfs_btree *, u32);
108extern void hfs_bnode_get(struct hfs_bnode *);
109extern void hfs_bnode_put(struct hfs_bnode *);
110
111/* brec.c */
112extern u16 hfs_brec_lenoff(struct hfs_bnode *, u16, u16 *);
113extern u16 hfs_brec_keylen(struct hfs_bnode *, u16);
114extern int hfs_brec_insert(struct hfs_find_data *, void *, int);
115extern int hfs_brec_remove(struct hfs_find_data *);
116
117/* bfind.c */
118extern int hfs_find_init(struct hfs_btree *, struct hfs_find_data *);
119extern void hfs_find_exit(struct hfs_find_data *);
120extern int __hfs_brec_find(struct hfs_bnode *, struct hfs_find_data *);
121extern int hfs_brec_find(struct hfs_find_data *);
122extern int hfs_brec_read(struct hfs_find_data *, void *, int);
123extern int hfs_brec_goto(struct hfs_find_data *, int);
124
125
126struct hfs_bnode_desc {
127 __be32 next; /* (V) Number of the next node at this level */
128 __be32 prev; /* (V) Number of the prev node at this level */
129 u8 type; /* (F) The type of node */
130 u8 height; /* (F) The level of this node (leaves=1) */
131 __be16 num_recs; /* (V) The number of records in this node */
132 u16 reserved;
133} __packed;
134
135#define HFS_NODE_INDEX 0x00 /* An internal (index) node */
136#define HFS_NODE_HEADER 0x01 /* The tree header node (node 0) */
137#define HFS_NODE_MAP 0x02 /* Holds part of the bitmap of used nodes */
138#define HFS_NODE_LEAF 0xFF /* A leaf (ndNHeight==1) node */
139
140struct hfs_btree_header_rec {
141 __be16 depth; /* (V) The number of levels in this B-tree */
142 __be32 root; /* (V) The node number of the root node */
143 __be32 leaf_count; /* (V) The number of leaf records */
144 __be32 leaf_head; /* (V) The number of the first leaf node */
145 __be32 leaf_tail; /* (V) The number of the last leaf node */
146 __be16 node_size; /* (F) The number of bytes in a node (=512) */
147 __be16 max_key_len; /* (F) The length of a key in an index node */
148 __be32 node_count; /* (V) The total number of nodes */
149 __be32 free_nodes; /* (V) The number of unused nodes */
150 u16 reserved1;
151 __be32 clump_size; /* (F) clump size. not usually used. */
152 u8 btree_type; /* (F) BTree type */
153 u8 reserved2;
154 __be32 attributes; /* (F) attributes */
155 u32 reserved3[16];
156} __packed;
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158#define BTREE_ATTR_BADCLOSE 0x00000001 /* b-tree not closed properly. not
159 used by hfsplus. */
160#define HFS_TREE_BIGKEYS 0x00000002 /* key length is u16 instead of u8.
161 used by hfsplus. */
162#define HFS_TREE_VARIDXKEYS 0x00000004 /* variable key length instead of
163 max key length. use din catalog
164 b-tree but not in extents
165 b-tree (hfsplus). */