2 * linux/fs/hfs/balloc.c
4 * Copyright (C) 1995-1997 Paul H. Hargrove
5 * This file may be distributed under the terms of the GNU General Public License.
7 * hfs_bnode_alloc() and hfs_bnode_bitop() are based on GPLed code
8 * Copyright (C) 1995 Michael Dreher
10 * This file contains the code to create and destroy nodes
11 * in the B-tree structure.
13 * "XXX" in a comment is a note to myself to consider changing something.
15 * In function preconditions the term "valid" applied to a pointer to
16 * a structure means that the pointer is non-NULL and the structure it
17 * points to has all fields initialized to consistent values.
19 * The code in this file initializes some structures which contain
20 * pointers by calling memset(&foo, 0, sizeof(foo)).
21 * This produces the desired behavior only due to the non-ANSI
22 * assumption that the machine representation of NULL is all zeros.
25 #include "hfs_btree.h"
27 /*================ File-local functions ================*/
32 * Get a buffer for a new node with out reading it from disk.
34 static hfs_buffer get_new_node(struct hfs_btree *tree, hfs_u32 node)
37 hfs_buffer retval = HFS_BAD_BUFFER;
39 tmp = hfs_extent_map(&tree->entry.u.file.data_fork, node, 0);
41 retval = hfs_buffer_get(tree->sys_mdb, tmp, 0);
50 * Initialize a newly allocated bnode.
52 * struct hfs_btree *tree: Pointer to a B-tree
53 * hfs_u32 node: the node number to allocate
57 * struct hfs_bnode_ref for the new node
59 * 'tree' points to a "valid" (struct hfs_btree)
60 * 'node' exists and has been allocated in the bitmap of bnodes.
63 * The node is not read from disk, nor added to the bnode cache.
64 * The 'sticky' and locking-related fields are all zero/NULL.
65 * The bnode's nd{[FB]Link, Type, NHeight} fields are uninitialized.
66 * The bnode's ndNRecs field and offsets table indicate an empty bnode.
68 * The node is deallocated.
70 static struct hfs_bnode_ref hfs_bnode_init(struct hfs_btree * tree,
73 #if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
74 extern int bnode_count;
76 struct hfs_bnode_ref retval;
78 retval.lock_type = HFS_LOCK_NONE;
79 if (!HFS_NEW(retval.bn)) {
80 hfs_warn("hfs_bnode_init: out of memory.\n");
84 /* Partially initialize the in-core structure */
85 memset(retval.bn, 0, sizeof(*retval.bn));
86 retval.bn->magic = HFS_BNODE_MAGIC;
87 retval.bn->tree = tree;
88 retval.bn->node = node;
89 hfs_init_waitqueue(&retval.bn->wqueue);
90 hfs_init_waitqueue(&retval.bn->rqueue);
91 hfs_bnode_lock(&retval, HFS_LOCK_WRITE);
93 retval.bn->buf = get_new_node(tree, node);
94 if (!hfs_buffer_ok(retval.bn->buf)) {
98 #if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
102 /* Partially initialize the on-disk structure */
103 memset(hfs_buffer_data(retval.bn->buf), 0, HFS_SECTOR_SIZE);
104 hfs_put_hs(sizeof(struct NodeDescriptor), RECTBL(retval.bn, 1));
109 HFS_DELETE(retval.bn);
111 /* clear the bit in the bitmap */
112 hfs_bnode_bitop(tree, node, 0);
120 * Initializes a given node as a mapnode in the given tree.
122 * struct hfs_bnode *bn: the node to add the mapnode after.
123 * hfs_u32: the node to use as a mapnode.
124 * Output Variable(s):
127 * struct hfs_bnode *: the new mapnode or NULL
129 * 'tree' is a valid (struct hfs_btree).
130 * 'node' is the number of the first node in 'tree' that is not
131 * represented by a bit in the existing mapnodes.
133 * On failure 'tree' is unchanged and NULL is returned.
134 * On success the node given by 'node' has been added to the linked
135 * list of mapnodes attached to 'tree', and has been initialized as
136 * a valid mapnode with its first bit set to indicate itself as
139 static struct hfs_bnode *init_mapnode(struct hfs_bnode *bn, hfs_u32 node)
141 #if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
142 extern int bnode_count;
144 struct hfs_bnode *retval;
146 if (!HFS_NEW(retval)) {
147 hfs_warn("hfs_bnode_add: out of memory.\n");
151 memset(retval, 0, sizeof(*retval));
152 retval->magic = HFS_BNODE_MAGIC;
153 retval->tree = bn->tree;
155 retval->sticky = HFS_STICKY;
156 retval->buf = get_new_node(bn->tree, node);
157 if (!hfs_buffer_ok(retval->buf)) {
162 #if defined(DEBUG_BNODES) || defined(DEBUG_ALL)
166 /* Initialize the bnode data structure */
167 memset(hfs_buffer_data(retval->buf), 0, HFS_SECTOR_SIZE);
169 retval->ndBLink = bn->node;
170 retval->ndType = ndMapNode;
171 retval->ndNHeight = 0;
173 hfs_put_hs(sizeof(struct NodeDescriptor), RECTBL(retval, 1));
174 hfs_put_hs(0x1fa, RECTBL(retval, 2));
175 *((hfs_u8 *)bnode_key(retval, 1)) = 0x80; /* set first bit of bitmap */
177 hfs_bnode_commit(retval);
181 hfs_bnode_commit(bn);
186 /*================ Global functions ================*/
192 * Allocate/free the requested node of a B-tree of the hfs filesystem
193 * by setting/clearing the corresponding bit in the B-tree bitmap.
194 * The size of the B-tree will not be changed.
196 * struct hfs_btree *tree: Pointer to a B-tree
197 * hfs_u32 bitnr: The node number to free
198 * int set: 0 to clear the bit, non-zero to set it.
199 * Output Variable(s):
203 * -1: The node was already allocated/free, nothing has been done.
204 * -2: The node is out of range of the B-tree.
205 * -4: not enough map nodes to hold all the bits
207 * 'tree' points to a "valid" (struct hfs_btree)
208 * 'bitnr' is a node number within the range of the btree, which is
209 * currently free/allocated.
211 * The bit number 'bitnr' of the node bitmap is set/cleared and the
212 * number of free nodes in the btree is decremented/incremented by one.
214 int hfs_bnode_bitop(struct hfs_btree *tree, hfs_u32 bitnr, int set)
216 struct hfs_bnode *bn; /* the current bnode */
217 hfs_u16 start; /* the start (in bits) of the bitmap in node */
218 hfs_u16 len; /* the len (in bits) of the bitmap in node */
219 hfs_u32 *u32; /* address of the u32 containing the bit */
221 if (bitnr >= tree->bthNNodes) {
222 hfs_warn("hfs_bnode_bitop: node number out of range.\n");
228 start = bnode_offset(bn, bn->ndNRecs) << 3;
229 len = (bnode_offset(bn, bn->ndNRecs + 1) << 3) - start;
235 /* continue on to next map node if available */
236 if (!(bn = bn->next)) {
237 hfs_warn("hfs_bnode_bitop: too few map nodes.\n");
243 /* Change the correct bit */
245 u32 = (hfs_u32 *)hfs_buffer_data(bn->buf) + (bitnr >> 5);
247 if ((set && hfs_set_bit(bitnr, u32)) ||
248 (!set && !hfs_clear_bit(bitnr, u32))) {
249 hfs_warn("hfs_bnode_bitop: bitmap corruption.\n");
252 hfs_buffer_dirty(bn->buf);
254 /* adjust the free count */
255 tree->bthFree += (set ? -1 : 1);
265 * Find a cleared bit in the B-tree node bitmap of the hfs filesystem,
266 * set it and return the corresponding bnode, with its contents zeroed.
267 * When there is no free bnode in the tree, an error is returned, no
268 * new nodes will be added by this function!
270 * struct hfs_btree *tree: Pointer to a B-tree
271 * Output Variable(s):
274 * struct hfs_bnode_ref for the new bnode
276 * 'tree' points to a "valid" (struct hfs_btree)
277 * There is at least one free bnode.
280 * The corresponding bit in the btree bitmap is set.
281 * The number of free nodes in the btree is decremented by one.
282 * The node is not read from disk, nor added to the bnode cache.
283 * The 'sticky' field is uninitialized.
285 struct hfs_bnode_ref hfs_bnode_alloc(struct hfs_btree *tree)
287 struct hfs_bnode *bn; /* the current bnode */
288 hfs_u32 bitnr = 0; /* which bit are we examining */
289 hfs_u16 first; /* the first clear bit in this bnode */
290 hfs_u16 start; /* the start (in bits) of the bitmap in node */
291 hfs_u16 end; /* the end (in bits) of the bitmap in node */
292 hfs_u32 *data; /* address of the data in this bnode */
296 start = bnode_offset(bn, bn->ndNRecs) << 3;
297 end = bnode_offset(bn, bn->ndNRecs + 1) << 3;
298 data = (hfs_u32 *)hfs_buffer_data(bn->buf);
300 /* search the current node */
301 first = hfs_find_zero_bit(data, end, start);
306 /* continue search in next map node */
310 hfs_warn("hfs_bnode_alloc: too few map nodes.\n");
313 bitnr += (end - start);
316 if ((bitnr += (first - start)) >= tree->bthNNodes) {
317 hfs_warn("hfs_bnode_alloc: no free nodes found, "
322 if (hfs_set_bit(first % 32, data + (first>>5))) {
323 hfs_warn("hfs_bnode_alloc: bitmap corruption.\n");
326 hfs_buffer_dirty(bn->buf);
328 /* decrement the free count */
332 return hfs_bnode_init(tree, bitnr);
335 return (struct hfs_bnode_ref){NULL, HFS_LOCK_NONE};
342 * Adds nodes to a B*-tree if possible.
344 * struct hfs_btree *tree: the btree to add nodes to.
345 * Output Variable(s):
350 * 'tree' is a valid (struct hfs_btree *).
352 * If possible the number of nodes indicated by the tree's clumpsize
353 * have been added to the tree, updating all in-core and on-disk
354 * allocation information.
355 * If insufficient disk-space was available then fewer nodes may have
356 * been added than would be expected based on the clumpsize.
357 * In the case of the extents B*-tree this function will add fewer
358 * nodes than expected if adding more would result in an extent
359 * record for the extents tree being added to the extents tree.
360 * The situation could be dealt with, but doing so confuses Macs.
362 void hfs_btree_extend(struct hfs_btree *tree)
364 struct hfs_bnode_ref head;
365 struct hfs_bnode *bn, *tmp;
366 struct hfs_cat_entry *entry = &tree->entry;
367 struct hfs_mdb *mdb = entry->mdb;
368 hfs_u32 old_nodes, new_nodes, total_nodes, new_mapnodes, seen;
370 old_nodes = entry->u.file.data_fork.psize;
372 entry->u.file.data_fork.lsize += 1; /* rounded up to clumpsize */
373 hfs_extent_adj(&entry->u.file.data_fork);
375 total_nodes = entry->u.file.data_fork.psize;
376 entry->u.file.data_fork.lsize = total_nodes << HFS_SECTOR_SIZE_BITS;
377 new_nodes = total_nodes - old_nodes;
382 head = hfs_bnode_find(tree, 0, HFS_LOCK_WRITE);
383 if (!(bn = head.bn)) {
384 hfs_warn("hfs_btree_extend: header node not found.\n");
391 seen += bnode_rsize(bn, bn->ndNRecs) << 3;
393 if (seen >= total_nodes) {
398 tmp = init_mapnode(bn, seen);
400 hfs_warn("hfs_btree_extend: "
401 "can't build mapnode.\n");
402 hfs_bnode_relse(&head);
409 hfs_bnode_relse(&head);
411 tree->bthNNodes = total_nodes;
412 tree->bthFree += (new_nodes - new_mapnodes);
415 /* write the backup MDB, not returning until it is written */
416 hfs_mdb_commit(mdb, 1);
424 * Remove a node from the cache and mark it free in the bitmap.
426 int hfs_bnode_free(struct hfs_bnode_ref *bnr)
428 hfs_u32 node = bnr->bn->node;
429 struct hfs_btree *tree = bnr->bn->tree;
431 if (bnr->bn->count != 1) {
432 hfs_warn("hfs_bnode_free: count != 1.\n");
436 hfs_bnode_relse(bnr);
437 hfs_bnode_bitop(tree, node, 0);