2 * Copyright (c) 2004-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 #include "../headers/BTreesPrivate.h"
29 #include "sys/malloc.h"
30 #include <kern/locks.h>
40 * Each kernel thread can have it's own reserve of b-tree
41 * nodes. This reserve info is kept in a hash table.
43 * Don't forget to call BTReleaseReserve when you're finished
44 * or you will leave stale node reserves in the hash.
49 * BE CAREFUL WHEN INCREASING THE SIZE OF THIS STRUCT!
51 * It must remain equal in size to the opaque cat_cookie_t
52 * struct (in hfs_catalog.h).
55 LIST_ENTRY(nreserve
) nr_hash
; /* hash chain */
56 int nr_nodecnt
; /* count of nodes held in reserve */
57 int nr_newnodes
; /* nodes that were allocated */
58 struct vnode
*nr_btvp
; /* b-tree file vnode */
59 void *nr_tag
; /* unique tag (per thread) */
62 #define NR_GET_TAG() (current_thread())
66 #define NR_HASH(btvp, tag) \
67 (&nr_hashtbl[((((int)(btvp)) >> 8) ^ ((int)(tag) >> 4)) & nr_hashmask])
69 LIST_HEAD(nodereserve
, nreserve
) *nr_hashtbl
;
73 lck_grp_t
* nr_lck_grp
;
74 lck_grp_attr_t
* nr_lck_grp_attr
;
75 lck_attr_t
* nr_lck_attr
;
79 /* Internal Node Reserve Hash Routines (private) */
80 static void nr_insert (struct vnode
*, struct nreserve
*nrp
, int);
81 static void nr_delete (struct vnode
*, struct nreserve
*nrp
, int *);
82 static void nr_update (struct vnode
*, int);
86 * BTReserveSetup - initialize the node reserve hash table
92 if (sizeof(struct nreserve
) != sizeof(cat_cookie_t
))
93 panic("BTReserveSetup: nreserve size != opaque struct size");
95 nr_hashtbl
= hashinit(NR_CACHE
, M_HFSMNT
, &nr_hashmask
);
97 nr_lck_grp_attr
= lck_grp_attr_alloc_init();
98 nr_lck_grp
= lck_grp_alloc_init("btree_node_reserve", nr_lck_grp_attr
);
100 nr_lck_attr
= lck_attr_alloc_init();
102 lck_mtx_init(&nr_mutex
, nr_lck_grp
, nr_lck_attr
);
107 * BTReserveSpace - obtain a node reserve (for current thread)
109 * Used by the Catalog Layer (hfs_catalog.c) to reserve space.
111 * When data is NULL, we only insure that there's enough space
112 * but it is not reserved (assumes you keep the b-tree lock).
116 BTReserveSpace(FCB
*file
, int operations
, void* data
)
118 BTreeControlBlock
*btree
;
119 int rsrvNodes
, availNodes
, totalNodes
;
121 int inserts
, deletes
;
125 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
126 clumpsize
= file
->ff_clumpsize
;
128 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
131 * The node reserve is based on the number of b-tree
132 * operations (insert/deletes) and the height of the
135 height
= btree
->treeDepth
;
137 height
= 2; /* prevent underflow in rsrvNodes calculation */
138 inserts
= operations
& 0xffff;
139 deletes
= operations
>> 16;
142 * Allow for at least one root split.
144 * Each delete operation can propogate a big key up the
145 * index. This can cause a split at each level up.
147 * Each insert operation can cause a local split and a
148 * split at each level up.
150 rsrvNodes
= 1 + (deletes
* (height
- 2)) + (inserts
* (height
- 1));
152 availNodes
= btree
->freeNodes
- btree
->reservedNodes
;
154 if (rsrvNodes
> availNodes
) {
155 u_int32_t reqblks
, freeblks
, rsrvblks
;
156 struct hfsmount
*hfsmp
;
158 /* Try and reserve the last 5% of the disk space for file blocks. */
159 hfsmp
= VTOVCB(btree
->fileRefNum
);
160 rsrvblks
= ((u_int64_t
)hfsmp
->allocLimit
* 5) / 100;
161 rsrvblks
= MIN(rsrvblks
, HFS_MAXRESERVE
/ hfsmp
->blockSize
);
162 freeblks
= hfs_freeblks(hfsmp
, 0);
163 if (freeblks
<= rsrvblks
) {
164 /* When running low, disallow adding new items. */
165 if ((inserts
> 0) && (deletes
== 0)) {
170 freeblks
-= rsrvblks
;
172 reqblks
= clumpsize
/ hfsmp
->blockSize
;
174 if (reqblks
> freeblks
) {
175 reqblks
= ((rsrvNodes
- availNodes
) * btree
->nodeSize
) / hfsmp
->blockSize
;
176 /* When running low, disallow adding new items. */
177 if ((reqblks
> freeblks
) && (inserts
> 0) && (deletes
== 0)) {
180 file
->ff_clumpsize
= freeblks
* hfsmp
->blockSize
;
182 totalNodes
= rsrvNodes
+ btree
->totalNodes
- availNodes
;
184 /* See if we also need a map node */
185 if (totalNodes
> (int)CalcMapBits(btree
)) {
188 if ((err
= ExtendBTree(btree
, totalNodes
))) {
192 /* Save this reserve if this is a persistent request. */
194 btree
->reservedNodes
+= rsrvNodes
;
195 nr_insert(btree
->fileRefNum
, (struct nreserve
*)data
, rsrvNodes
);
198 /* Put clump size back if it was changed. */
199 if (file
->ff_clumpsize
!= clumpsize
)
200 file
->ff_clumpsize
= clumpsize
;
207 * BTReleaseReserve - release the node reserve held by current thread
209 * Used by the Catalog Layer (hfs_catalog.c) to relinquish reserved space.
213 BTReleaseReserve(FCB
*file
, void* data
)
215 BTreeControlBlock
*btree
;
218 btree
= (BTreeControlBlockPtr
)file
->fcbBTCBPtr
;
220 REQUIRE_FILE_LOCK(btree
->fileRefNum
, true);
222 nr_delete(btree
->fileRefNum
, (struct nreserve
*)data
, &nodecnt
);
225 btree
->reservedNodes
-= nodecnt
;
231 * BTUpdateReserve - update a node reserve for allocations that occurred.
235 BTUpdateReserve(BTreeControlBlockPtr btreePtr
, int nodes
)
237 nr_update(btreePtr
->fileRefNum
, nodes
);
241 /*----------------------------------------------------------------------------*/
242 /* Node Reserve Hash Functions (private) */
249 * Insert a new node reserve.
252 nr_insert(struct vnode
* btvp
, struct nreserve
*nrp
, int nodecnt
)
254 struct nodereserve
*nrhead
;
255 struct nreserve
*tmp_nrp
;
256 void * tag
= NR_GET_TAG();
259 * Check the cache - there may already be a reserve
261 lck_mtx_lock(&nr_mutex
);
262 nrhead
= NR_HASH(btvp
, tag
);
263 for (tmp_nrp
= nrhead
->lh_first
; tmp_nrp
;
264 tmp_nrp
= tmp_nrp
->nr_hash
.le_next
) {
265 if ((tmp_nrp
->nr_tag
== tag
) && (tmp_nrp
->nr_btvp
== btvp
)) {
267 tmp_nrp
->nr_nodecnt
+= nodecnt
;
268 lck_mtx_unlock(&nr_mutex
);
273 nrp
->nr_nodecnt
= nodecnt
;
274 nrp
->nr_newnodes
= 0;
277 LIST_INSERT_HEAD(nrhead
, nrp
, nr_hash
);
279 lck_mtx_unlock(&nr_mutex
);
283 * Delete a node reserve.
286 nr_delete(struct vnode
* btvp
, struct nreserve
*nrp
, int *nodecnt
)
288 void * tag
= NR_GET_TAG();
290 lck_mtx_lock(&nr_mutex
);
292 if ((nrp
->nr_tag
!= tag
) || (nrp
->nr_btvp
!= btvp
))
293 panic("nr_delete: invalid NR (%p)", nrp
);
294 LIST_REMOVE(nrp
, nr_hash
);
295 *nodecnt
= nrp
->nr_nodecnt
;
296 bzero(nrp
, sizeof(struct nreserve
));
301 lck_mtx_unlock(&nr_mutex
);
306 * Update a node reserve for any allocations that occurred.
309 nr_update(struct vnode
* btvp
, int nodecnt
)
311 struct nodereserve
*nrhead
;
312 struct nreserve
*nrp
;
313 void* tag
= NR_GET_TAG();
315 lck_mtx_lock(&nr_mutex
);
317 nrhead
= NR_HASH(btvp
, tag
);
318 for (nrp
= nrhead
->lh_first
; nrp
; nrp
= nrp
->nr_hash
.le_next
) {
319 if ((nrp
->nr_tag
== tag
) && (nrp
->nr_btvp
== btvp
)) {
320 nrp
->nr_newnodes
+= nodecnt
;
324 lck_mtx_unlock(&nr_mutex
);