2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 #include "../headers/BTreesPrivate.h"
24 #include "sys/malloc.h"
25 #include <kern/locks.h>
36 * Each kernel thread can have it's own reserve of b-tree
37 * nodes. This reserve info is kept in a hash table.
39 * Don't forget to call BTReleaseReserve when you're finished
40 * or you will leave stale node reserves in the hash.
45 * BE CAREFUL WHEN INCREASING THE SIZE OF THIS STRUCT!
47 * It must remain equal in size to the opaque cat_cookie_t
48 * struct (in hfs_catalog.h).
51 LIST_ENTRY(nreserve
) nr_hash
; /* hash chain */
52 int nr_nodecnt
; /* count of nodes held in reserve */
53 int nr_newnodes
; /* nodes that were allocated */
54 struct vnode
*nr_btvp
; /* b-tree file vnode */
55 void *nr_tag
; /* unique tag (per thread) */
58 #define NR_GET_TAG() (current_thread())
62 #define NR_HASH(btvp, tag) \
63 (&nr_hashtbl[((((int)(btvp)) >> 8) ^ ((int)(tag) >> 4)) & nr_hashmask])
65 LIST_HEAD(nodereserve
, nreserve
) *nr_hashtbl
;
69 lck_grp_t
* nr_lck_grp
;
70 lck_grp_attr_t
* nr_lck_grp_attr
;
71 lck_attr_t
* nr_lck_attr
;
75 /* Internal Node Reserve Hash Routines (private) */
76 static void nr_insert (struct vnode
*, struct nreserve
*nrp
, int);
77 static void nr_delete (struct vnode
*, struct nreserve
*nrp
, int *);
78 static int nr_lookup (struct vnode
*);
79 static void nr_update (struct vnode
*, int);
83 * BTReserveSetup - initialize the node reserve hash table
89 if (sizeof(struct nreserve
) != sizeof(cat_cookie_t
))
90 panic("BTReserveSetup: nreserve size != opaque struct size");
92 nr_hashtbl
= hashinit(NR_CACHE
, M_HFSMNT
, &nr_hashmask
);
94 nr_lck_grp_attr
= lck_grp_attr_alloc_init();
95 lck_grp_attr_setstat(nr_lck_grp_attr
);
96 nr_lck_grp
= lck_grp_alloc_init("btree_node_reserve", nr_lck_grp_attr
);
98 nr_lck_attr
= lck_attr_alloc_init();
99 lck_attr_setdebug(nr_lck_attr
);
101 lck_mtx_init(&nr_mutex
, nr_lck_grp
, nr_lck_attr
);
106 * BTAvailNodes - obtain the actual available nodes (for current thread)
111 BTAvailableNodes(BTreeControlBlock
*btree
)
115 availNodes
= (SInt32
)btree
->freeNodes
- (SInt32
)btree
->reservedNodes
;
117 return (availNodes
+ nr_lookup(btree
->fileRefNum
));
122 * BTReserveSpace - obtain a node reserve (for current thread)
124 * Used by the Catalog Layer (hfs_catalog.c) to reserve space.
128 BTReserveSpace(FCB
*file
, int operations
, void* data
)
130 BTreeControlBlock
*btree
;
131 int rsrvNodes
, availNodes
, totalNodes
;
133 int inserts
, deletes
;
136 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
138 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
141 * The node reserve is based on the number of b-tree
142 * operations (insert/deletes) and the height of the
145 height
= btree
->treeDepth
;
146 inserts
= operations
& 0xffff;
147 deletes
= operations
>> 16;
149 rsrvNodes
= 1; /* allow for at least one root split */
151 rsrvNodes
+= (deletes
* (height
- 1)) - 1;
153 rsrvNodes
+= (inserts
* height
) + 1;
155 availNodes
= btree
->freeNodes
- btree
->reservedNodes
;
157 if (rsrvNodes
> availNodes
) {
158 totalNodes
= rsrvNodes
+ btree
->totalNodes
- availNodes
;
160 /* See if we also need a map node */
161 if (totalNodes
> (int)CalcMapBits(btree
))
163 if ((err
= ExtendBTree(btree
, totalNodes
)))
167 btree
->reservedNodes
+= rsrvNodes
;
168 nr_insert(btree
->fileRefNum
, (struct nreserve
*)data
, rsrvNodes
);
174 * BTReleaseReserve - release the node reserve held by current thread
176 * Used by the Catalog Layer (hfs_catalog.c) to relinquish reserved space.
180 BTReleaseReserve(FCB
*file
, void* data
)
182 BTreeControlBlock
*btree
;
185 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
187 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
189 nr_delete(btree
->fileRefNum
, (struct nreserve
*)data
, &nodecnt
);
192 btree
->reservedNodes
-= nodecnt
;
198 * BTUpdateReserve - update a node reserve for allocations that occurred.
202 BTUpdateReserve(BTreeControlBlockPtr btreePtr
, int nodes
)
204 nr_update(btreePtr
->fileRefNum
, nodes
);
208 /*----------------------------------------------------------------------------*/
209 /* Node Reserve Hash Functions (private) */
216 * Insert a new node reserve.
219 nr_insert(struct vnode
* btvp
, struct nreserve
*nrp
, int nodecnt
)
221 struct nodereserve
*nrhead
;
222 struct nreserve
*tmp_nrp
;
223 void * tag
= NR_GET_TAG();
226 * Check the cache - there may already be a reserve
228 lck_mtx_lock(&nr_mutex
);
229 nrhead
= NR_HASH(btvp
, tag
);
230 for (tmp_nrp
= nrhead
->lh_first
; tmp_nrp
;
231 tmp_nrp
= tmp_nrp
->nr_hash
.le_next
) {
232 if ((tmp_nrp
->nr_tag
== tag
) && (tmp_nrp
->nr_btvp
== btvp
)) {
234 lck_mtx_unlock(&nr_mutex
);
239 nrp
->nr_nodecnt
= nodecnt
;
240 nrp
->nr_newnodes
= 0;
243 LIST_INSERT_HEAD(nrhead
, nrp
, nr_hash
);
245 lck_mtx_unlock(&nr_mutex
);
249 * Delete a node reserve.
252 nr_delete(struct vnode
* btvp
, struct nreserve
*nrp
, int *nodecnt
)
254 void * tag
= NR_GET_TAG();
256 lck_mtx_lock(&nr_mutex
);
258 if ((nrp
->nr_tag
!= tag
) || (nrp
->nr_btvp
!= btvp
))
259 panic("nr_delete: invalid NR (%08x)", nrp
);
260 LIST_REMOVE(nrp
, nr_hash
);
261 *nodecnt
= nrp
->nr_nodecnt
;
262 bzero(nrp
, sizeof(struct nreserve
));
267 lck_mtx_unlock(&nr_mutex
);
271 * Lookup a node reserve.
274 nr_lookup(struct vnode
* btvp
)
276 struct nodereserve
*nrhead
;
277 struct nreserve
*nrp
;
278 void* tag
= NR_GET_TAG();
280 lck_mtx_lock(&nr_mutex
);
282 nrhead
= NR_HASH(btvp
, tag
);
283 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
284 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
285 lck_mtx_unlock(&nr_mutex
);
286 return (nrp
->nr_nodecnt
- nrp
->nr_newnodes
);
289 lck_mtx_unlock(&nr_mutex
);
294 * Update a node reserve for any allocations that occurred.
297 nr_update(struct vnode
* btvp
, int nodecnt
)
299 struct nodereserve
*nrhead
;
300 struct nreserve
*nrp
;
301 void* tag
= NR_GET_TAG();
303 lck_mtx_lock(&nr_mutex
);
305 nrhead
= NR_HASH(btvp
, tag
);
306 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
307 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
308 nrp
->nr_newnodes
+= nodecnt
;
312 lck_mtx_unlock(&nr_mutex
);