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 lck_grp_attr_setstat(nr_lck_grp_attr
);
95 nr_lck_grp
= lck_grp_alloc_init("btree_node_reserve", nr_lck_grp_attr
);
97 nr_lck_attr
= lck_attr_alloc_init();
98 lck_attr_setdebug(nr_lck_attr
);
100 lck_mtx_init(&nr_mutex
, nr_lck_grp
, nr_lck_attr
);
105 * BTAvailNodes - obtain the actual available nodes (for current thread)
110 BTAvailableNodes(BTreeControlBlock
*btree
)
114 availNodes
= (SInt32
)btree
->freeNodes
- (SInt32
)btree
->reservedNodes
;
116 return (availNodes
+ nr_lookup(btree
->fileRefNum
));
121 * BTReserveSpace - obtain a node reserve (for current thread)
123 * Used by the Catalog Layer (hfs_catalog.c) to reserve space.
127 BTReserveSpace(FCB
*file
, int operations
, void* data
)
129 BTreeControlBlock
*btree
;
130 int rsrvNodes
, availNodes
, totalNodes
;
132 int inserts
, deletes
;
135 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
137 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
140 * The node reserve is based on the number of b-tree
141 * operations (insert/deletes) and the height of the
144 height
= btree
->treeDepth
;
145 inserts
= operations
& 0xffff;
146 deletes
= operations
>> 16;
148 rsrvNodes
= 1; /* allow for at least one root split */
150 rsrvNodes
+= (deletes
* (height
- 1)) - 1;
152 rsrvNodes
+= (inserts
* height
) + 1;
154 availNodes
= btree
->freeNodes
- btree
->reservedNodes
;
156 if (rsrvNodes
> availNodes
) {
157 totalNodes
= rsrvNodes
+ btree
->totalNodes
- availNodes
;
159 /* See if we also need a map node */
160 if (totalNodes
> (int)CalcMapBits(btree
))
162 if ((err
= ExtendBTree(btree
, totalNodes
)))
166 btree
->reservedNodes
+= rsrvNodes
;
167 nr_insert(btree
->fileRefNum
, (struct nreserve
*)data
, rsrvNodes
);
173 * BTReleaseReserve - release the node reserve held by current thread
175 * Used by the Catalog Layer (hfs_catalog.c) to relinquish reserved space.
179 BTReleaseReserve(FCB
*file
, void* data
)
181 BTreeControlBlock
*btree
;
184 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
186 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
188 nr_delete(btree
->fileRefNum
, (struct nreserve
*)data
, &nodecnt
);
191 btree
->reservedNodes
-= nodecnt
;
197 * BTUpdateReserve - update a node reserve for allocations that occurred.
201 BTUpdateReserve(BTreeControlBlockPtr btreePtr
, int nodes
)
203 nr_update(btreePtr
->fileRefNum
, nodes
);
207 /*----------------------------------------------------------------------------*/
208 /* Node Reserve Hash Functions (private) */
215 * Insert a new node reserve.
218 nr_insert(struct vnode
* btvp
, struct nreserve
*nrp
, int nodecnt
)
220 struct nodereserve
*nrhead
;
221 struct nreserve
*tmp_nrp
;
222 void * tag
= NR_GET_TAG();
225 * Check the cache - there may already be a reserve
227 lck_mtx_lock(&nr_mutex
);
228 nrhead
= NR_HASH(btvp
, tag
);
229 for (tmp_nrp
= nrhead
->lh_first
; tmp_nrp
;
230 tmp_nrp
= tmp_nrp
->nr_hash
.le_next
) {
231 if ((tmp_nrp
->nr_tag
== tag
) && (tmp_nrp
->nr_btvp
== btvp
)) {
233 lck_mtx_unlock(&nr_mutex
);
238 nrp
->nr_nodecnt
= nodecnt
;
239 nrp
->nr_newnodes
= 0;
242 LIST_INSERT_HEAD(nrhead
, nrp
, nr_hash
);
244 lck_mtx_unlock(&nr_mutex
);
248 * Delete a node reserve.
251 nr_delete(struct vnode
* btvp
, struct nreserve
*nrp
, int *nodecnt
)
253 void * tag
= NR_GET_TAG();
255 lck_mtx_lock(&nr_mutex
);
257 if ((nrp
->nr_tag
!= tag
) || (nrp
->nr_btvp
!= btvp
))
258 panic("nr_delete: invalid NR (%08x)", nrp
);
259 LIST_REMOVE(nrp
, nr_hash
);
260 *nodecnt
= nrp
->nr_nodecnt
;
261 bzero(nrp
, sizeof(struct nreserve
));
266 lck_mtx_unlock(&nr_mutex
);
270 * Lookup a node reserve.
273 nr_lookup(struct vnode
* btvp
)
275 struct nodereserve
*nrhead
;
276 struct nreserve
*nrp
;
277 void* tag
= NR_GET_TAG();
279 lck_mtx_lock(&nr_mutex
);
281 nrhead
= NR_HASH(btvp
, tag
);
282 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
283 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
284 lck_mtx_unlock(&nr_mutex
);
285 return (nrp
->nr_nodecnt
- nrp
->nr_newnodes
);
288 lck_mtx_unlock(&nr_mutex
);
293 * Update a node reserve for any allocations that occurred.
296 nr_update(struct vnode
* btvp
, int nodecnt
)
298 struct nodereserve
*nrhead
;
299 struct nreserve
*nrp
;
300 void* tag
= NR_GET_TAG();
302 lck_mtx_lock(&nr_mutex
);
304 nrhead
= NR_HASH(btvp
, tag
);
305 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
306 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
307 nrp
->nr_newnodes
+= nodecnt
;
311 lck_mtx_unlock(&nr_mutex
);