2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 #include "../headers/BTreesPrivate.h"
31 #include "sys/malloc.h"
32 #include <kern/locks.h>
43 * Each kernel thread can have it's own reserve of b-tree
44 * nodes. This reserve info is kept in a hash table.
46 * Don't forget to call BTReleaseReserve when you're finished
47 * or you will leave stale node reserves in the hash.
52 * BE CAREFUL WHEN INCREASING THE SIZE OF THIS STRUCT!
54 * It must remain equal in size to the opaque cat_cookie_t
55 * struct (in hfs_catalog.h).
58 LIST_ENTRY(nreserve
) nr_hash
; /* hash chain */
59 int nr_nodecnt
; /* count of nodes held in reserve */
60 int nr_newnodes
; /* nodes that were allocated */
61 struct vnode
*nr_btvp
; /* b-tree file vnode */
62 void *nr_tag
; /* unique tag (per thread) */
65 #define NR_GET_TAG() (current_thread())
69 #define NR_HASH(btvp, tag) \
70 (&nr_hashtbl[((((int)(btvp)) >> 8) ^ ((int)(tag) >> 4)) & nr_hashmask])
72 LIST_HEAD(nodereserve
, nreserve
) *nr_hashtbl
;
76 lck_grp_t
* nr_lck_grp
;
77 lck_grp_attr_t
* nr_lck_grp_attr
;
78 lck_attr_t
* nr_lck_attr
;
82 /* Internal Node Reserve Hash Routines (private) */
83 static void nr_insert (struct vnode
*, struct nreserve
*nrp
, int);
84 static void nr_delete (struct vnode
*, struct nreserve
*nrp
, int *);
85 static int nr_lookup (struct vnode
*);
86 static void nr_update (struct vnode
*, int);
90 * BTReserveSetup - initialize the node reserve hash table
96 if (sizeof(struct nreserve
) != sizeof(cat_cookie_t
))
97 panic("BTReserveSetup: nreserve size != opaque struct size");
99 nr_hashtbl
= hashinit(NR_CACHE
, M_HFSMNT
, &nr_hashmask
);
101 nr_lck_grp_attr
= lck_grp_attr_alloc_init();
102 nr_lck_grp
= lck_grp_alloc_init("btree_node_reserve", nr_lck_grp_attr
);
104 nr_lck_attr
= lck_attr_alloc_init();
106 lck_mtx_init(&nr_mutex
, nr_lck_grp
, nr_lck_attr
);
111 * BTAvailNodes - obtain the actual available nodes (for current thread)
116 BTAvailableNodes(BTreeControlBlock
*btree
)
120 availNodes
= (SInt32
)btree
->freeNodes
- (SInt32
)btree
->reservedNodes
;
122 return (availNodes
+ nr_lookup(btree
->fileRefNum
));
127 * BTReserveSpace - obtain a node reserve (for current thread)
129 * Used by the Catalog Layer (hfs_catalog.c) to reserve space.
133 BTReserveSpace(FCB
*file
, int operations
, void* data
)
135 BTreeControlBlock
*btree
;
136 int rsrvNodes
, availNodes
, totalNodes
;
138 int inserts
, deletes
;
141 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
143 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
146 * The node reserve is based on the number of b-tree
147 * operations (insert/deletes) and the height of the
150 height
= btree
->treeDepth
;
151 inserts
= operations
& 0xffff;
152 deletes
= operations
>> 16;
154 rsrvNodes
= 1; /* allow for at least one root split */
156 rsrvNodes
+= (deletes
* (height
- 1)) - 1;
158 rsrvNodes
+= (inserts
* height
) + 1;
160 availNodes
= btree
->freeNodes
- btree
->reservedNodes
;
162 if (rsrvNodes
> availNodes
) {
163 totalNodes
= rsrvNodes
+ btree
->totalNodes
- availNodes
;
165 /* See if we also need a map node */
166 if (totalNodes
> (int)CalcMapBits(btree
))
168 if ((err
= ExtendBTree(btree
, totalNodes
)))
172 btree
->reservedNodes
+= rsrvNodes
;
173 nr_insert(btree
->fileRefNum
, (struct nreserve
*)data
, rsrvNodes
);
179 * BTReleaseReserve - release the node reserve held by current thread
181 * Used by the Catalog Layer (hfs_catalog.c) to relinquish reserved space.
185 BTReleaseReserve(FCB
*file
, void* data
)
187 BTreeControlBlock
*btree
;
190 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
192 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
194 nr_delete(btree
->fileRefNum
, (struct nreserve
*)data
, &nodecnt
);
197 btree
->reservedNodes
-= nodecnt
;
203 * BTUpdateReserve - update a node reserve for allocations that occurred.
207 BTUpdateReserve(BTreeControlBlockPtr btreePtr
, int nodes
)
209 nr_update(btreePtr
->fileRefNum
, nodes
);
213 /*----------------------------------------------------------------------------*/
214 /* Node Reserve Hash Functions (private) */
221 * Insert a new node reserve.
224 nr_insert(struct vnode
* btvp
, struct nreserve
*nrp
, int nodecnt
)
226 struct nodereserve
*nrhead
;
227 struct nreserve
*tmp_nrp
;
228 void * tag
= NR_GET_TAG();
231 * Check the cache - there may already be a reserve
233 lck_mtx_lock(&nr_mutex
);
234 nrhead
= NR_HASH(btvp
, tag
);
235 for (tmp_nrp
= nrhead
->lh_first
; tmp_nrp
;
236 tmp_nrp
= tmp_nrp
->nr_hash
.le_next
) {
237 if ((tmp_nrp
->nr_tag
== tag
) && (tmp_nrp
->nr_btvp
== btvp
)) {
239 lck_mtx_unlock(&nr_mutex
);
244 nrp
->nr_nodecnt
= nodecnt
;
245 nrp
->nr_newnodes
= 0;
248 LIST_INSERT_HEAD(nrhead
, nrp
, nr_hash
);
250 lck_mtx_unlock(&nr_mutex
);
254 * Delete a node reserve.
257 nr_delete(struct vnode
* btvp
, struct nreserve
*nrp
, int *nodecnt
)
259 void * tag
= NR_GET_TAG();
261 lck_mtx_lock(&nr_mutex
);
263 if ((nrp
->nr_tag
!= tag
) || (nrp
->nr_btvp
!= btvp
))
264 panic("nr_delete: invalid NR (%08x)", nrp
);
265 LIST_REMOVE(nrp
, nr_hash
);
266 *nodecnt
= nrp
->nr_nodecnt
;
267 bzero(nrp
, sizeof(struct nreserve
));
272 lck_mtx_unlock(&nr_mutex
);
276 * Lookup a node reserve.
279 nr_lookup(struct vnode
* btvp
)
281 struct nodereserve
*nrhead
;
282 struct nreserve
*nrp
;
283 void* tag
= NR_GET_TAG();
285 lck_mtx_lock(&nr_mutex
);
287 nrhead
= NR_HASH(btvp
, tag
);
288 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
289 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
290 lck_mtx_unlock(&nr_mutex
);
291 return (nrp
->nr_nodecnt
- nrp
->nr_newnodes
);
294 lck_mtx_unlock(&nr_mutex
);
299 * Update a node reserve for any allocations that occurred.
302 nr_update(struct vnode
* btvp
, int nodecnt
)
304 struct nodereserve
*nrhead
;
305 struct nreserve
*nrp
;
306 void* tag
= NR_GET_TAG();
308 lck_mtx_lock(&nr_mutex
);
310 nrhead
= NR_HASH(btvp
, tag
);
311 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
312 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
313 nrp
->nr_newnodes
+= nodecnt
;
317 lck_mtx_unlock(&nr_mutex
);