2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 #include "../headers/BTreesPrivate.h"
23 #include "sys/malloc.h"
24 #include <kern/locks.h>
35 * Each kernel thread can have it's own reserve of b-tree
36 * nodes. This reserve info is kept in a hash table.
38 * Don't forget to call BTReleaseReserve when you're finished
39 * or you will leave stale node reserves in the hash.
44 * BE CAREFUL WHEN INCREASING THE SIZE OF THIS STRUCT!
46 * It must remain equal in size to the opaque cat_cookie_t
47 * struct (in hfs_catalog.h).
50 LIST_ENTRY(nreserve
) nr_hash
; /* hash chain */
51 int nr_nodecnt
; /* count of nodes held in reserve */
52 int nr_newnodes
; /* nodes that were allocated */
53 struct vnode
*nr_btvp
; /* b-tree file vnode */
54 void *nr_tag
; /* unique tag (per thread) */
57 #define NR_GET_TAG() (current_thread())
61 #define NR_HASH(btvp, tag) \
62 (&nr_hashtbl[((((int)(btvp)) >> 8) ^ ((int)(tag) >> 4)) & nr_hashmask])
64 LIST_HEAD(nodereserve
, nreserve
) *nr_hashtbl
;
68 lck_grp_t
* nr_lck_grp
;
69 lck_grp_attr_t
* nr_lck_grp_attr
;
70 lck_attr_t
* nr_lck_attr
;
74 /* Internal Node Reserve Hash Routines (private) */
75 static void nr_insert (struct vnode
*, struct nreserve
*nrp
, int);
76 static void nr_delete (struct vnode
*, struct nreserve
*nrp
, int *);
77 static int nr_lookup (struct vnode
*);
78 static void nr_update (struct vnode
*, int);
82 * BTReserveSetup - initialize the node reserve hash table
88 if (sizeof(struct nreserve
) != sizeof(cat_cookie_t
))
89 panic("BTReserveSetup: nreserve size != opaque struct size");
91 nr_hashtbl
= hashinit(NR_CACHE
, M_HFSMNT
, &nr_hashmask
);
93 nr_lck_grp_attr
= lck_grp_attr_alloc_init();
94 nr_lck_grp
= lck_grp_alloc_init("btree_node_reserve", nr_lck_grp_attr
);
96 nr_lck_attr
= lck_attr_alloc_init();
98 lck_mtx_init(&nr_mutex
, nr_lck_grp
, nr_lck_attr
);
103 * BTAvailNodes - obtain the actual available nodes (for current thread)
108 BTAvailableNodes(BTreeControlBlock
*btree
)
112 availNodes
= (SInt32
)btree
->freeNodes
- (SInt32
)btree
->reservedNodes
;
114 return (availNodes
+ nr_lookup(btree
->fileRefNum
));
119 * BTReserveSpace - obtain a node reserve (for current thread)
121 * Used by the Catalog Layer (hfs_catalog.c) to reserve space.
125 BTReserveSpace(FCB
*file
, int operations
, void* data
)
127 BTreeControlBlock
*btree
;
128 int rsrvNodes
, availNodes
, totalNodes
;
130 int inserts
, deletes
;
133 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
135 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
138 * The node reserve is based on the number of b-tree
139 * operations (insert/deletes) and the height of the
142 height
= btree
->treeDepth
;
143 inserts
= operations
& 0xffff;
144 deletes
= operations
>> 16;
146 rsrvNodes
= 1; /* allow for at least one root split */
148 rsrvNodes
+= (deletes
* (height
- 1)) - 1;
150 rsrvNodes
+= (inserts
* height
) + 1;
152 availNodes
= btree
->freeNodes
- btree
->reservedNodes
;
154 if (rsrvNodes
> availNodes
) {
155 totalNodes
= rsrvNodes
+ btree
->totalNodes
- availNodes
;
157 /* See if we also need a map node */
158 if (totalNodes
> (int)CalcMapBits(btree
))
160 if ((err
= ExtendBTree(btree
, totalNodes
)))
164 btree
->reservedNodes
+= rsrvNodes
;
165 nr_insert(btree
->fileRefNum
, (struct nreserve
*)data
, rsrvNodes
);
171 * BTReleaseReserve - release the node reserve held by current thread
173 * Used by the Catalog Layer (hfs_catalog.c) to relinquish reserved space.
177 BTReleaseReserve(FCB
*file
, void* data
)
179 BTreeControlBlock
*btree
;
182 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
184 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
186 nr_delete(btree
->fileRefNum
, (struct nreserve
*)data
, &nodecnt
);
189 btree
->reservedNodes
-= nodecnt
;
195 * BTUpdateReserve - update a node reserve for allocations that occurred.
199 BTUpdateReserve(BTreeControlBlockPtr btreePtr
, int nodes
)
201 nr_update(btreePtr
->fileRefNum
, nodes
);
205 /*----------------------------------------------------------------------------*/
206 /* Node Reserve Hash Functions (private) */
213 * Insert a new node reserve.
216 nr_insert(struct vnode
* btvp
, struct nreserve
*nrp
, int nodecnt
)
218 struct nodereserve
*nrhead
;
219 struct nreserve
*tmp_nrp
;
220 void * tag
= NR_GET_TAG();
223 * Check the cache - there may already be a reserve
225 lck_mtx_lock(&nr_mutex
);
226 nrhead
= NR_HASH(btvp
, tag
);
227 for (tmp_nrp
= nrhead
->lh_first
; tmp_nrp
;
228 tmp_nrp
= tmp_nrp
->nr_hash
.le_next
) {
229 if ((tmp_nrp
->nr_tag
== tag
) && (tmp_nrp
->nr_btvp
== btvp
)) {
231 lck_mtx_unlock(&nr_mutex
);
236 nrp
->nr_nodecnt
= nodecnt
;
237 nrp
->nr_newnodes
= 0;
240 LIST_INSERT_HEAD(nrhead
, nrp
, nr_hash
);
242 lck_mtx_unlock(&nr_mutex
);
246 * Delete a node reserve.
249 nr_delete(struct vnode
* btvp
, struct nreserve
*nrp
, int *nodecnt
)
251 void * tag
= NR_GET_TAG();
253 lck_mtx_lock(&nr_mutex
);
255 if ((nrp
->nr_tag
!= tag
) || (nrp
->nr_btvp
!= btvp
))
256 panic("nr_delete: invalid NR (%08x)", nrp
);
257 LIST_REMOVE(nrp
, nr_hash
);
258 *nodecnt
= nrp
->nr_nodecnt
;
259 bzero(nrp
, sizeof(struct nreserve
));
264 lck_mtx_unlock(&nr_mutex
);
268 * Lookup a node reserve.
271 nr_lookup(struct vnode
* btvp
)
273 struct nodereserve
*nrhead
;
274 struct nreserve
*nrp
;
275 void* tag
= NR_GET_TAG();
277 lck_mtx_lock(&nr_mutex
);
279 nrhead
= NR_HASH(btvp
, tag
);
280 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
281 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
282 lck_mtx_unlock(&nr_mutex
);
283 return (nrp
->nr_nodecnt
- nrp
->nr_newnodes
);
286 lck_mtx_unlock(&nr_mutex
);
291 * Update a node reserve for any allocations that occurred.
294 nr_update(struct vnode
* btvp
, int nodecnt
)
296 struct nodereserve
*nrhead
;
297 struct nreserve
*nrp
;
298 void* tag
= NR_GET_TAG();
300 lck_mtx_lock(&nr_mutex
);
302 nrhead
= NR_HASH(btvp
, tag
);
303 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
304 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
305 nrp
->nr_newnodes
+= nodecnt
;
309 lck_mtx_unlock(&nr_mutex
);