summaryrefslogtreecommitdiffstats
path: root/fs/hfs/btree.c
blob: 900bddf3398a4e3bc8aa8cd24b9950dd29e7d3c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
 * linux/fs/hfs/btree.c
 *
 * Copyright (C) 1995-1997  Paul H. Hargrove
 * This file may be distributed under the terms of the GNU Public License.
 *
 * This file contains the code to manipulate the B-tree structure.
 * The catalog and extents files are both B-trees.
 *
 * "XXX" in a comment is a note to myself to consider changing something.
 *
 * In function preconditions the term "valid" applied to a pointer to
 * a structure means that the pointer is non-NULL and the structure it
 * points to has all fields initialized to consistent values.
 *
 * The code in this file initializes some structures which contain
 * pointers by calling memset(&foo, 0, sizeof(foo)).
 * This produces the desired behavior only due to the non-ANSI
 * assumption that the machine representation of NULL is all zeros.
 */

#include "hfs_btree.h"

/*================ File-local functions ================*/

/*
 * hfs_bnode_ditch() 
 *
 * Description:
 *   This function deletes an entire linked list of bnodes, so it
 *   does not need to keep the linked list consistent as
 *   hfs_bnode_delete() does.
 *   Called by hfs_btree_init() for error cleanup and by hfs_btree_free().
 * Input Variable(s):
 *   struct hfs_bnode *bn: pointer to the first (struct hfs_bnode) in
 *    the linked list to be deleted.
 * Output Variable(s):
 *   NONE
 * Returns:
 *   void
 * Preconditions:
 *   'bn' is NULL or points to a "valid" (struct hfs_bnode) with a 'prev'
 *    field of NULL.
 * Postconditions:
 *   'bn' and all (struct hfs_bnode)s in the chain of 'next' pointers
 *   are deleted, freeing the associated memory and hfs_buffer_put()ing
 *   the associated buffer.
 */
static void hfs_bnode_ditch(struct hfs_bnode *bn) {
	struct hfs_bnode *tmp;
#if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
	extern int bnode_count;
#endif

	while (bn != NULL) {
		tmp = bn->next;
#if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
		hfs_warn("deleting node %d from tree %d with count %d\n",
		         bn->node, (int)ntohl(bn->tree->entry.cnid), bn->count);
		--bnode_count;
#endif
		hfs_buffer_put(bn->buf); /* safe: checks for NULL argument */

		/* free all but the header */
		if (bn->node) {
			HFS_DELETE(bn);
		}
		bn = tmp;
	}
}

/*================ Global functions ================*/

/*
 * hfs_btree_free()
 *
 * Description:
 *   This function frees a (struct hfs_btree) obtained from hfs_btree_init().
 *   Called by hfs_put_super().
 * Input Variable(s):
 *   struct hfs_btree *bt: pointer to the (struct hfs_btree) to free
 * Output Variable(s):
 *   NONE
 * Returns:
 *   void
 * Preconditions:
 *   'bt' is NULL or points to a "valid" (struct hfs_btree)
 * Postconditions:
 *   If 'bt' points to a "valid" (struct hfs_btree) then all (struct
 *    hfs_bnode)s associated with 'bt' are freed by calling
 *    hfs_bnode_ditch() and the memory associated with the (struct
 *    hfs_btree) is freed.
 *   If 'bt' is NULL or not "valid" an error is printed and nothing
 *    is changed.
 */
void hfs_btree_free(struct hfs_btree *bt)
{
	int lcv;

	if (bt && (bt->magic == HFS_BTREE_MAGIC)) {
		hfs_extent_free(&bt->entry.u.file.data_fork);

		for (lcv=0; lcv<HFS_CACHELEN; ++lcv) {
#if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
			hfs_warn("deleting nodes from bucket %d:\n", lcv);
#endif
			hfs_bnode_ditch(bt->cache[lcv]);
		}

#if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
		hfs_warn("deleting header and bitmap nodes\n");
#endif
		hfs_bnode_ditch(&bt->head);

#if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
		hfs_warn("deleting root node\n");
#endif
		hfs_bnode_ditch(bt->root);

		HFS_DELETE(bt);
	} else if (bt) {
		hfs_warn("hfs_btree_free: corrupted hfs_btree.\n");
	}
}

/*
 * hfs_btree_init()
 *
 * Description:
 *   Given some vital information from the MDB (HFS superblock),
 *   initializes the fields of a (struct hfs_btree).
 * Input Variable(s):
 *   struct hfs_mdb *mdb: pointer to the MDB
 *   ino_t cnid: the CNID (HFS_CAT_CNID or HFS_EXT_CNID) of the B-tree
 *   hfs_u32 tsize: the size, in bytes, of the B-tree
 *   hfs_u32 csize: the size, in bytes, of the clump size for the B-tree
 * Output Variable(s):
 *   NONE
 * Returns:
 *   (struct hfs_btree *): pointer to the initialized hfs_btree on success,
 *    or NULL on failure
 * Preconditions:
 *   'mdb' points to a "valid" (struct hfs_mdb)
 * Postconditions:
 *   Assuming the inputs are what they claim to be, no errors occur
 *   reading from disk, and no inconsistencies are noticed in the data
 *   read from disk, the return value is a pointer to a "valid"
 *   (struct hfs_btree).  If there are errors reading from disk or
 *   inconsistencies are noticed in the data read from disk, then and
 *   all resources that were allocated are released and NULL is
 *   returned.	If the inputs are not what they claim to be or if they
 *   are unnoticed inconsistencies in the data read from disk then the
 *   returned hfs_btree is probably going to lead to errors when it is
 *   used in a non-trivial way.
 */
struct hfs_btree * hfs_btree_init(struct hfs_mdb *mdb, ino_t cnid,
				  hfs_byte_t ext[12],
				  hfs_u32 tsize, hfs_u32 csize)
{
	struct hfs_btree * bt;
	struct BTHdrRec * th;
	struct hfs_bnode * tmp;
	unsigned int next;
#if defined(DEBUG_HEADER) || defined(DEBUG_ALL)
	unsigned char *p, *q;
#endif

	if (!mdb || !ext || !HFS_NEW(bt)) {
		goto bail3;
	}

	bt->magic = HFS_BTREE_MAGIC;
	bt->sys_mdb = mdb->sys_mdb;
	bt->reserved = 0;
	bt->lock = 0;
	bt->wait = NULL;
	bt->dirt = 0;
	memset(bt->cache, 0, sizeof(bt->cache));

#if 0   /* this is a fake entry. so we don't need to initialize it. */
	memset(&bt->entry, 0, sizeof(bt->entry));
	hfs_init_waitqueue(&bt->entry.wait);
	INIT_LIST_HEAD(&bt->entry.hash);
	INIT_LIST_HEAD(&bt->entry.list);
#endif

	bt->entry.mdb = mdb;
	bt->entry.cnid = cnid;
	bt->entry.type = HFS_CDR_FIL;
	bt->entry.u.file.magic = HFS_FILE_MAGIC;
	bt->entry.u.file.clumpablks = (csize / mdb->alloc_blksz)
						>> HFS_SECTOR_SIZE_BITS;
	bt->entry.u.file.data_fork.entry = &bt->entry;
	bt->entry.u.file.data_fork.lsize = tsize;
	bt->entry.u.file.data_fork.psize = tsize >> HFS_SECTOR_SIZE_BITS;
	bt->entry.u.file.data_fork.fork = HFS_FK_DATA;
	hfs_extent_in(&bt->entry.u.file.data_fork, ext);

	hfs_bnode_read(&bt->head, bt, 0, HFS_STICKY);
	if (!hfs_buffer_ok(bt->head.buf)) {
		goto bail2;
	}
	th = (struct BTHdrRec *)((char *)hfs_buffer_data(bt->head.buf) +
						sizeof(struct NodeDescriptor));

	/* read in the bitmap nodes (if any) */
	tmp = &bt->head;
	while ((next = tmp->ndFLink)) {
		if (!HFS_NEW(tmp->next)) {
			goto bail2;
		}
		hfs_bnode_read(tmp->next, bt, next, HFS_STICKY);
		if (!hfs_buffer_ok(tmp->next->buf)) {
			goto bail2;
		}
		tmp->next->prev = tmp;
		tmp = tmp->next;
	}

	if (hfs_get_ns(th->bthNodeSize) != htons(HFS_SECTOR_SIZE)) {
		hfs_warn("hfs_btree_init: bthNodeSize!=512 not supported\n");
		goto bail2;
	}

	if (cnid == htonl(HFS_CAT_CNID)) {
		bt->compare = (hfs_cmpfn)hfs_cat_compare;
	} else if (cnid == htonl(HFS_EXT_CNID)) {
		bt->compare = (hfs_cmpfn)hfs_ext_compare;
	} else {
		goto bail2;
	}
	bt->bthDepth  = hfs_get_hs(th->bthDepth);
	bt->bthRoot   = hfs_get_hl(th->bthRoot);
	bt->bthNRecs  = hfs_get_hl(th->bthNRecs);
	bt->bthFNode  = hfs_get_hl(th->bthFNode);
	bt->bthLNode  = hfs_get_hl(th->bthLNode);
	bt->bthNNodes = hfs_get_hl(th->bthNNodes);
	bt->bthFree   = hfs_get_hl(th->bthFree);
	bt->bthKeyLen = hfs_get_hs(th->bthKeyLen);

#if defined(DEBUG_HEADER) || defined(DEBUG_ALL)
	hfs_warn("bthDepth %d\n", bt->bthDepth);
	hfs_warn("bthRoot %d\n", bt->bthRoot);
	hfs_warn("bthNRecs %d\n", bt->bthNRecs);
	hfs_warn("bthFNode %d\n", bt->bthFNode);
	hfs_warn("bthLNode %d\n", bt->bthLNode);
	hfs_warn("bthKeyLen %d\n", bt->bthKeyLen);
	hfs_warn("bthNNodes %d\n", bt->bthNNodes);
	hfs_warn("bthFree %d\n", bt->bthFree);
	p = (unsigned char *)hfs_buffer_data(bt->head.buf);
	q = p + HFS_SECTOR_SIZE;
	while (p < q) {
		hfs_warn("%02x %02x %02x %02x %02x %02x %02x %02x "
		         "%02x %02x %02x %02x %02x %02x %02x %02x\n",
			 *p++, *p++, *p++, *p++, *p++, *p++, *p++, *p++,
			 *p++, *p++, *p++, *p++, *p++, *p++, *p++, *p++);
	}
#endif

	/* Read in the root if it exists.
	   The header always exists, but the root exists only if the
	   tree is non-empty */
	if (bt->bthDepth && bt->bthRoot) {
		if (!HFS_NEW(bt->root)) {
			goto bail2;
		}
		hfs_bnode_read(bt->root, bt, bt->bthRoot, HFS_STICKY);
		if (!hfs_buffer_ok(bt->root->buf)) {
			goto bail1;
		}
	} else {
		bt->root = NULL;
	}

	return bt;

 bail1:
	hfs_bnode_ditch(bt->root);
 bail2:
	hfs_bnode_ditch(&bt->head);
	HFS_DELETE(bt);
 bail3:
	return NULL;
}

/*
 * hfs_btree_commit()
 *
 * Called to write a possibly dirty btree back to disk.
 */
void hfs_btree_commit(struct hfs_btree *bt, hfs_byte_t ext[12], hfs_lword_t size)
{
	if (bt->dirt) {
		struct BTHdrRec *th;
		th = (struct BTHdrRec *)((char *)hfs_buffer_data(bt->head.buf) +
						 sizeof(struct NodeDescriptor));

		hfs_put_hs(bt->bthDepth,  th->bthDepth);
		hfs_put_hl(bt->bthRoot,   th->bthRoot);
		hfs_put_hl(bt->bthNRecs,  th->bthNRecs);
		hfs_put_hl(bt->bthFNode,  th->bthFNode);
		hfs_put_hl(bt->bthLNode,  th->bthLNode);
		hfs_put_hl(bt->bthNNodes, th->bthNNodes);
		hfs_put_hl(bt->bthFree,   th->bthFree);
		hfs_buffer_dirty(bt->head.buf);

		/*
		 * Commit the bnodes which are not cached.
		 * The map nodes don't need to be committed here because
		 * they are committed every time they are changed.
		 */
		hfs_bnode_commit(&bt->head);
		if (bt->root) {
			hfs_bnode_commit(bt->root);
		}

	
		hfs_put_hl(bt->bthNNodes << HFS_SECTOR_SIZE_BITS, size);
		hfs_extent_out(&bt->entry.u.file.data_fork, ext);
		/* hfs_buffer_dirty(mdb->buf); (Done by caller) */

		bt->dirt = 0;
	}
}