2 * Copyright (c) 2003-2005 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@
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/fcntl.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
37 #include <sys/vnode.h>
38 #include <sys/vnode_internal.h>
39 #include <sys/kauth.h>
42 #include <hfs/hfs_endian.h>
43 #include <hfs/hfs_format.h>
44 #include <hfs/hfs_mount.h>
45 #include <hfs/hfs_hotfiles.h>
47 #include "hfscommon/headers/BTreeScanner.h"
56 * Hot File List (runtime).
58 typedef struct hotfileinfo
{
60 u_int32_t hf_temperature
;
64 typedef struct hotfilelist
{
66 u_int32_t hfl_version
;
67 time_t hfl_duration
; /* duration of sample period */
68 int hfl_count
; /* count of hot files recorded */
69 int hfl_next
; /* next file to move */
70 int hfl_totalblocks
; /* total hot file blocks */
71 int hfl_reclaimblks
; /* blocks to reclaim in HFV */
72 u_int32_t hfl_spare
[2];
73 hotfileinfo_t hfl_hotfile
[1]; /* array of hot files */
78 * Hot File Entry (runtime).
80 typedef struct hotfile_entry
{
81 struct hotfile_entry
*left
;
82 struct hotfile_entry
*right
;
84 u_int32_t temperature
;
89 * Hot File Recording Data (runtime).
91 typedef struct hotfile_data
{
92 struct hfsmount
*hfsmp
;
94 int activefiles
; /* active number of hot files */
97 hotfile_entry_t
*rootentry
;
98 hotfile_entry_t
*freelist
;
99 hotfile_entry_t
*coldest
;
100 hotfile_entry_t entries
[1];
103 static int hfs_recording_start (struct hfsmount
*);
104 static int hfs_recording_stop (struct hfsmount
*);
108 * Hot File Data recording functions (in-memory binary tree).
110 static void hf_insert (hotfile_data_t
*, hotfile_entry_t
*);
111 static void hf_delete (hotfile_data_t
*, u_int32_t
, u_int32_t
);
112 static hotfile_entry_t
* hf_coldest (hotfile_data_t
*);
113 static hotfile_entry_t
* hf_getnewentry (hotfile_data_t
*);
114 static void hf_getsortedlist (hotfile_data_t
*, hotfilelist_t
*);
117 static hotfile_entry_t
* hf_lookup (hotfile_data_t
*, u_int32_t
, u_int32_t
);
118 static void hf_maxdepth(hotfile_entry_t
*, int, int *);
119 static void hf_printtree (hotfile_entry_t
*);
123 * Hot File misc support functions.
125 static int hotfiles_collect (struct hfsmount
*);
126 static int hotfiles_age (struct hfsmount
*);
127 static int hotfiles_adopt (struct hfsmount
*);
128 static int hotfiles_evict (struct hfsmount
*, struct proc
*);
129 static int hotfiles_refine (struct hfsmount
*);
130 static int hotextents(struct hfsmount
*, HFSPlusExtentDescriptor
*);
131 static int hfs_addhotfile_internal(struct vnode
*);
135 * Hot File Cluster B-tree (on disk) functions.
137 static int hfc_btree_create (struct hfsmount
*, int, int);
138 static int hfc_btree_open (struct hfsmount
*, struct vnode
**);
139 static int hfc_btree_close (struct hfsmount
*, struct vnode
*);
140 static int hfc_comparekeys (HotFileKey
*, HotFileKey
*);
143 char hfc_tag
[] = "CLUSTERED HOT FILES B-TREE ";
145 extern int UBCINFOEXISTS(struct vnode
* vp
);
146 extern int hfs_vnop_write(struct vnop_write_args
*ap
);
150 *========================================================================
151 * HOT FILE INTERFACE ROUTINES
152 *========================================================================
156 * Start recording the hotest files on a file system.
158 * Requires that the hfc_mutex be held.
161 hfs_recording_start(struct hfsmount
*hfsmp
)
163 hotfile_data_t
*hotdata
;
170 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
171 (hfsmp
->jnl
== NULL
) ||
172 (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) == 0) {
175 if (HFSTOVCB(hfsmp
)->freeBlocks
< (2 * (u_int32_t
)hfsmp
->hfs_hotfile_maxblks
)) {
178 if (hfsmp
->hfc_stage
!= HFC_IDLE
) {
181 hfsmp
->hfc_stage
= HFC_BUSY
;
184 * Dump previous recording data.
186 if (hfsmp
->hfc_recdata
) {
189 tmp
= hfsmp
->hfc_recdata
;
190 hfsmp
->hfc_recdata
= NULL
;
197 * On first startup check for suspended recording.
199 if (hfsmp
->hfc_timebase
== 0 &&
200 hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
) == 0) {
201 HotFilesInfo hotfileinfo
;
203 if ((BTGetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
,
204 sizeof(hotfileinfo
)) == 0) &&
205 (SWAP_BE32 (hotfileinfo
.magic
) == HFC_MAGIC
) &&
206 (SWAP_BE32 (hotfileinfo
.timeleft
) > 0) &&
207 (SWAP_BE32 (hotfileinfo
.timebase
) > 0)) {
208 hfsmp
->hfc_maxfiles
= SWAP_BE32 (hotfileinfo
.maxfilecnt
);
209 hfsmp
->hfc_timeout
= SWAP_BE32 (hotfileinfo
.timeleft
) + tv
.tv_sec
;
210 hfsmp
->hfc_timebase
= SWAP_BE32 (hotfileinfo
.timebase
);
212 printf("Resume recording hot files on %s (%d secs left)\n",
213 hfsmp
->vcbVN
, SWAP_BE32 (hotfileinfo
.timeleft
));
216 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
217 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
218 hfsmp
->hfc_timeout
= hfsmp
->hfc_timebase
+ HFC_DEFAULT_DURATION
;
220 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
221 hfsmp
->hfc_filevp
= NULL
;
223 struct cat_attr cattr
;
227 * Make sure a btree file exists.
229 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
231 !S_ISREG(cattr
.ca_mode
) &&
232 (error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
))) {
233 hfsmp
->hfc_stage
= HFC_IDLE
;
234 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
238 printf("HFS: begin recording hot files on %s\n", hfsmp
->vcbVN
);
240 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
241 hfsmp
->hfc_timeout
= tv
.tv_sec
+ HFC_DEFAULT_DURATION
;
243 /* Reset time base. */
244 if (hfsmp
->hfc_timebase
== 0) {
245 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
247 time_t cumulativebase
;
249 cumulativebase
= hfsmp
->hfc_timeout
- (HFC_CUMULATIVE_CYCLES
* HFC_DEFAULT_DURATION
);
250 hfsmp
->hfc_timebase
= MAX(hfsmp
->hfc_timebase
, cumulativebase
);
254 if ((hfsmp
->hfc_maxfiles
== 0) ||
255 (hfsmp
->hfc_maxfiles
> HFC_MAXIMUM_FILE_COUNT
)) {
256 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
258 maxentries
= hfsmp
->hfc_maxfiles
;
260 size
= sizeof(hotfile_data_t
) + (maxentries
* sizeof(hotfile_entry_t
));
261 MALLOC(hotdata
, hotfile_data_t
*, size
, M_TEMP
, M_WAITOK
);
262 bzero(hotdata
, size
);
264 for (i
= 1; i
< maxentries
; i
++)
265 hotdata
->entries
[i
-1].right
= &hotdata
->entries
[i
];
267 hotdata
->freelist
= &hotdata
->entries
[0];
269 * Establish minimum temperature and maximum file size.
271 hotdata
->threshold
= HFC_MINIMUM_TEMPERATURE
;
272 hotdata
->maxblocks
= HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
;
273 hotdata
->hfsmp
= hfsmp
;
275 hfsmp
->hfc_recdata
= hotdata
;
276 hfsmp
->hfc_stage
= HFC_RECORDING
;
277 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
282 * Stop recording the hotest files on a file system.
284 * Requires that the hfc_mutex be held.
287 hfs_recording_stop(struct hfsmount
*hfsmp
)
289 hotfile_data_t
*hotdata
;
290 hotfilelist_t
*listp
;
293 enum hfc_stage newstage
= HFC_IDLE
;
296 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
299 hfsmp
->hfc_stage
= HFC_BUSY
;
301 hotfiles_collect(hfsmp
);
304 * Convert hot file data into a simple file id list....
306 * then dump the sample data
309 printf("HFS: end of hot file recording on %s\n", hfsmp
->vcbVN
);
311 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
314 hfsmp
->hfc_recdata
= NULL
;
315 hfsmp
->hfc_stage
= HFC_EVALUATION
;
316 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
319 printf(" curentries: %d\n", hotdata
->activefiles
);
322 * If no hot files recorded then we're done.
324 if (hotdata
->rootentry
== NULL
) {
329 /* Open the B-tree file for writing... */
330 if (hfsmp
->hfc_filevp
)
331 panic("hfs_recording_stop: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
333 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
339 * Age the previous set of clustered hot files.
341 error
= hotfiles_age(hfsmp
);
343 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
344 hfsmp
->hfc_filevp
= NULL
;
349 * Create a sorted list of hotest files.
351 size
= sizeof(hotfilelist_t
);
352 size
+= sizeof(hotfileinfo_t
) * (hotdata
->activefiles
- 1);
353 MALLOC(listp
, hotfilelist_t
*, size
, M_TEMP
, M_WAITOK
);
356 hf_getsortedlist(hotdata
, listp
); /* NOTE: destroys hot file tree! */
358 listp
->hfl_duration
= tv
.tv_sec
- hfsmp
->hfc_timebase
;
359 hfsmp
->hfc_recdata
= listp
;
362 * Account for duplicates.
364 error
= hotfiles_refine(hfsmp
);
366 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
367 hfsmp
->hfc_filevp
= NULL
;
372 * Compute the amount of space to reclaim...
374 if (listp
->hfl_totalblocks
> hfsmp
->hfs_hotfile_freeblks
) {
375 listp
->hfl_reclaimblks
=
376 MIN(listp
->hfl_totalblocks
, hfsmp
->hfs_hotfile_maxblks
) -
377 hfsmp
->hfs_hotfile_freeblks
;
379 printf("hfs_recording_stop: need to reclaim %d blocks\n", listp
->hfl_reclaimblks
);
381 if (listp
->hfl_reclaimblks
)
382 newstage
= HFC_EVICTION
;
384 newstage
= HFC_ADOPTION
;
386 newstage
= HFC_ADOPTION
;
389 if (newstage
== HFC_ADOPTION
&& listp
->hfl_totalblocks
== 0) {
390 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
391 hfsmp
->hfc_filevp
= NULL
;
396 if (newstage
== HFC_EVICTION
)
397 printf("HFS: evicting coldest files\n");
398 else if (newstage
== HFC_ADOPTION
)
399 printf("HFS: adopting hotest files\n");
401 FREE(hotdata
, M_TEMP
);
403 hfsmp
->hfc_stage
= newstage
;
404 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
409 * Suspend recording the hotest files on a file system.
413 hfs_recording_suspend(struct hfsmount
*hfsmp
)
415 HotFilesInfo hotfileinfo
;
416 hotfile_data_t
*hotdata
= NULL
;
420 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
423 lck_mtx_lock(&hfsmp
->hfc_mutex
);
427 * A suspend can occur during eval/evict/adopt stage.
428 * In that case we would need to write out info and
429 * flush our HFBT vnode. Currently we just bail.
432 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
433 if (hotdata
== NULL
|| hfsmp
->hfc_stage
!= HFC_RECORDING
) {
437 hfsmp
->hfc_stage
= HFC_BUSY
;
440 printf("HFS: suspend hot file recording on %s\n", hfsmp
->vcbVN
);
442 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
444 printf("hfs_recording_suspend: err %d opening btree\n", error
);
448 if (hfs_start_transaction(hfsmp
) != 0) {
452 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
458 hotfileinfo
.magic
= SWAP_BE32 (HFC_MAGIC
);
459 hotfileinfo
.version
= SWAP_BE32 (HFC_VERSION
);
460 hotfileinfo
.duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
461 hotfileinfo
.timebase
= SWAP_BE32 (hfsmp
->hfc_timebase
);
462 hotfileinfo
.timeleft
= SWAP_BE32 (hfsmp
->hfc_timeout
- tv
.tv_sec
);
463 hotfileinfo
.threshold
= SWAP_BE32 (hotdata
->threshold
);
464 hotfileinfo
.maxfileblks
= SWAP_BE32 (hotdata
->maxblocks
);
465 hotfileinfo
.maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
466 strcpy(hotfileinfo
.tag
, hfc_tag
);
467 (void) BTSetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
, sizeof(hotfileinfo
));
469 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
470 hfs_end_transaction(hfsmp
);
472 if (hfsmp
->hfc_filevp
) {
473 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
474 hfsmp
->hfc_filevp
= NULL
;
477 FREE(hotdata
, M_TEMP
);
478 hfsmp
->hfc_recdata
= NULL
;
480 hfsmp
->hfc_stage
= HFC_DISABLED
;
481 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
483 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
493 hfs_recording_init(struct hfsmount
*hfsmp
)
496 CatalogRecord
* datap
;
498 HFSPlusCatalogFile
*filep
;
499 BTScanState scanstate
;
500 BTreeIterator
* iterator
;
501 FSBufferDescriptor record
;
503 filefork_t
* filefork
;
505 struct cat_attr cattr
;
509 int inserted
= 0; /* debug variables */
513 * For now, only the boot volume is supported.
515 if ((vfs_flags(HFSTOVFS(hfsmp
)) & MNT_ROOTFS
) == 0) {
516 hfsmp
->hfc_stage
= HFC_DISABLED
;
521 * If the Hot File btree exists then metadata zone is ready.
523 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
524 if (cnid
!= 0 && S_ISREG(cattr
.ca_mode
)) {
525 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
526 hfsmp
->hfc_stage
= HFC_IDLE
;
529 error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
);
532 printf("Error %d creating hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
537 * Open the Hot File B-tree file for writing.
539 if (hfsmp
->hfc_filevp
)
540 panic("hfs_recording_init: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
541 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
544 printf("Error %d opening hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
548 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
549 bzero(iterator
, sizeof(*iterator
));
550 key
= (HotFileKey
*) &iterator
->key
;
551 key
->keyLength
= HFC_KEYLENGTH
;
553 record
.bufferAddress
= &data
;
554 record
.itemSize
= sizeof(u_int32_t
);
555 record
.itemCount
= 1;
557 printf("Evaluating space for \"%s\" metadata zone...\n", HFSTOVCB(hfsmp
)->vcbVN
);
560 * Get ready to scan the Catalog file.
562 error
= BTScanInitialize(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), 0, 0, 0,
563 kCatSearchBufferSize
, &scanstate
);
565 printf("hfs_recording_init: err %d BTScanInit\n", error
);
570 * The writes to Hot File B-tree file are journaled.
572 if (hfs_start_transaction(hfsmp
) != 0) {
576 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
580 filefork
= VTOF(hfsmp
->hfc_filevp
);
583 * Visit all the catalog btree leaf records.
586 error
= BTScanNextRecord(&scanstate
, 0, (void **)&keyp
, (void **)&datap
, &dataSize
);
588 if (error
== btNotFound
)
591 printf("hfs_recording_init: err %d BTScanNext\n", error
);
594 if ((datap
->recordType
!= kHFSPlusFileRecord
) ||
595 (dataSize
!= sizeof(HFSPlusCatalogFile
))) {
598 filep
= (HFSPlusCatalogFile
*)datap
;
600 if (filep
->dataFork
.totalBlocks
== 0) {
604 * Any file that has blocks inside the hot file
605 * space is recorded for later eviction.
607 * For now, resource forks are ignored.
609 if (!hotextents(hfsmp
, &filep
->dataFork
.extents
[0])) {
612 cnid
= filep
->fileID
;
614 /* Skip over journal files. */
615 if (cnid
== hfsmp
->hfs_jnlfileid
|| cnid
== hfsmp
->hfs_jnlinfoblkid
) {
619 * XXX - need to skip quota files as well.
622 /* Insert a hot file entry. */
623 key
->keyLength
= HFC_KEYLENGTH
;
624 key
->temperature
= HFC_MINIMUM_TEMPERATURE
;
628 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
630 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
631 error
= MacToVFSError(error
);
635 /* Insert the corresponding thread record. */
636 key
->keyLength
= HFC_KEYLENGTH
;
637 key
->temperature
= HFC_LOOKUPTAG
;
640 data
= HFC_MINIMUM_TEMPERATURE
;
641 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
643 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
644 error
= MacToVFSError(error
);
649 (void) BTFlushPath(filefork
);
650 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
652 hfs_end_transaction(hfsmp
);
654 printf("%d files identified out of %d\n", inserted
, filecount
);
658 (void) BTScanTerminate(&scanstate
, &data
, &data
, &data
);
660 FREE(iterator
, M_TEMP
);
661 if (hfsmp
->hfc_filevp
) {
662 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
663 hfsmp
->hfc_filevp
= NULL
;
666 hfsmp
->hfc_stage
= HFC_IDLE
;
672 * Use sync to perform ocassional background work.
676 hfs_hotfilesync(struct hfsmount
*hfsmp
, struct proc
*p
)
678 if (hfsmp
->hfc_stage
) {
681 lck_mtx_lock(&hfsmp
->hfc_mutex
);
683 switch (hfsmp
->hfc_stage
) {
685 (void) hfs_recording_start(hfsmp
);
690 if (tv
.tv_sec
> hfsmp
->hfc_timeout
)
691 (void) hfs_recording_stop(hfsmp
);
695 (void) hotfiles_evict(hfsmp
, p
);
699 (void) hotfiles_adopt(hfsmp
);
705 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
711 * Add a hot file to the recording list.
713 * This can happen when a hot file gets reclaimed or at the
714 * end of the recording period for any active hot file.
716 * NOTE: Since both the data and resource fork can be hot,
717 * there can be two entries for the same file id.
719 * Note: the cnode is locked on entry.
723 hfs_addhotfile(struct vnode
*vp
)
729 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
732 lck_mtx_lock(&hfsmp
->hfc_mutex
);
733 error
= hfs_addhotfile_internal(vp
);
734 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
739 hfs_addhotfile_internal(struct vnode
*vp
)
741 hotfile_data_t
*hotdata
;
742 hotfile_entry_t
*entry
;
746 u_int32_t temperature
;
749 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
752 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
755 /* Skip resource forks for now. */
756 if (VNODE_IS_RSRC(vp
)) {
759 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
) {
765 if ((ffp
->ff_bytesread
== 0) ||
766 (ffp
->ff_blocks
== 0) ||
767 (ffp
->ff_size
== 0) ||
768 (ffp
->ff_blocks
> hotdata
->maxblocks
) ||
769 (cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ||
770 (cp
->c_flags
& UF_NODUMP
) ||
771 (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
775 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
776 if (temperature
< hotdata
->threshold
) {
780 * If there is room or this file is hotter than
781 * the coldest one then add it to the list.
784 if ((hotdata
->activefiles
< hfsmp
->hfc_maxfiles
) ||
785 (hotdata
->coldest
== NULL
) ||
786 (temperature
> hotdata
->coldest
->temperature
)) {
788 entry
= hf_getnewentry(hotdata
);
789 entry
->temperature
= temperature
;
790 entry
->fileid
= cp
->c_fileid
;
791 entry
->blocks
= ffp
->ff_blocks
;
792 hf_insert(hotdata
, entry
);
800 * Remove a hot file from the recording list.
802 * This can happen when a hot file becomes
803 * an active vnode (active hot files are
804 * not kept in the recording list until the
805 * end of the recording period).
807 * Note: the cnode is locked on entry.
811 hfs_removehotfile(struct vnode
*vp
)
813 hotfile_data_t
*hotdata
;
817 u_int32_t temperature
;
820 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
823 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
830 if ((ffp
->ff_bytesread
== 0) || (ffp
->ff_blocks
== 0) ||
831 (ffp
->ff_size
== 0) || (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
835 lck_mtx_lock(&hfsmp
->hfc_mutex
);
836 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
838 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
)
841 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
842 if (temperature
< hotdata
->threshold
)
845 if (hotdata
->coldest
&& (temperature
>= hotdata
->coldest
->temperature
)) {
847 hf_delete(hotdata
, VTOC(vp
)->c_fileid
, temperature
);
851 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
857 *========================================================================
858 * HOT FILE MAINTENANCE ROUTINES
859 *========================================================================
863 hotfiles_collect_callback(struct vnode
*vp
, __unused
void *cargs
)
865 if ((vnode_isreg(vp
) || vnode_islnk(vp
)) && !vnode_issystem(vp
))
866 (void) hfs_addhotfile_internal(vp
);
868 return (VNODE_RETURNED
);
872 * Add all active hot files to the recording list.
875 hotfiles_collect(struct hfsmount
*hfsmp
)
877 struct mount
*mp
= HFSTOVFS(hfsmp
);
879 if (vfs_busy(mp
, LK_NOWAIT
))
883 * hotfiles_collect_callback will be called for each vnode
884 * hung off of this mount point
886 * properly referenced and unreferenced around the callback
888 vnode_iterate(mp
, 0, hotfiles_collect_callback
, (void *)NULL
);
897 * Update the data of a btree record
898 * This is called from within BTUpdateRecord.
901 update_callback(const HotFileKey
*key
, u_int32_t
*data
, u_int32_t
*state
)
903 if (key
->temperature
== HFC_LOOKUPTAG
)
909 * Identify files already in hot area.
912 hotfiles_refine(struct hfsmount
*hfsmp
)
914 BTreeIterator
* iterator
;
916 filefork_t
* filefork
;
917 hotfilelist_t
*listp
;
918 FSBufferDescriptor record
;
925 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
928 mp
= HFSTOVFS(hfsmp
);
930 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
931 bzero(iterator
, sizeof(*iterator
));
932 key
= (HotFileKey
*) &iterator
->key
;
934 record
.bufferAddress
= &data
;
935 record
.itemSize
= sizeof(u_int32_t
);
936 record
.itemCount
= 1;
938 if (hfs_start_transaction(hfsmp
) != 0) {
942 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
946 filefork
= VTOF(hfsmp
->hfc_filevp
);
948 for (i
= 0; i
< listp
->hfl_count
; ++i
) {
950 * Check if entry (thread) is already in hot area.
952 key
->keyLength
= HFC_KEYLENGTH
;
953 key
->temperature
= HFC_LOOKUPTAG
;
954 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
956 (void) BTInvalidateHint(iterator
);
957 if (BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
) != 0) {
958 continue; /* not in hot area, so skip */
962 * Update thread entry with latest temperature.
964 error
= BTUpdateRecord(filefork
, iterator
,
965 (IterateCallBackProcPtr
)update_callback
,
966 &listp
->hfl_hotfile
[i
].hf_temperature
);
968 printf("hotfiles_refine: BTUpdateRecord failed %d (file %d)\n", error
, key
->fileID
);
969 error
= MacToVFSError(error
);
973 * Re-key entry with latest temperature.
975 key
->keyLength
= HFC_KEYLENGTH
;
976 key
->temperature
= data
;
977 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
979 /* Pick up record data. */
980 (void) BTInvalidateHint(iterator
);
981 (void) BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
);
982 error
= BTDeleteRecord(filefork
, iterator
);
984 printf("hotfiles_refine: BTDeleteRecord failed %d (file %d)\n", error
, key
->fileID
);
985 error
= MacToVFSError(error
);
988 key
->keyLength
= HFC_KEYLENGTH
;
989 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
990 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
992 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
994 printf("hotfiles_refine: BTInsertRecord failed %d (file %d)\n", error
, key
->fileID
);
995 error
= MacToVFSError(error
);
1000 * Invalidate this entry in the list.
1002 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1003 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1007 (void) BTFlushPath(filefork
);
1008 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1010 hfs_end_transaction(hfsmp
);
1012 FREE(iterator
, M_TEMP
);
1017 * Move new hot files into hot area.
1019 * Requires that the hfc_mutex be held.
1022 hotfiles_adopt(struct hfsmount
*hfsmp
)
1024 BTreeIterator
* iterator
;
1026 filefork_t
* filefork
;
1027 hotfilelist_t
*listp
;
1028 FSBufferDescriptor record
;
1031 enum hfc_stage stage
;
1037 int startedtrans
= 0;
1039 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1042 if (hfsmp
->hfc_stage
!= HFC_ADOPTION
) {
1045 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1049 stage
= hfsmp
->hfc_stage
;
1050 hfsmp
->hfc_stage
= HFC_BUSY
;
1053 last
= listp
->hfl_next
+ HFC_FILESPERSYNC
;
1054 if (last
> listp
->hfl_count
)
1055 last
= listp
->hfl_count
;
1057 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1058 bzero(iterator
, sizeof(*iterator
));
1059 key
= (HotFileKey
*) &iterator
->key
;
1060 key
->keyLength
= HFC_KEYLENGTH
;
1062 record
.bufferAddress
= &data
;
1063 record
.itemSize
= sizeof(u_int32_t
);
1064 record
.itemCount
= 1;
1066 filefork
= VTOF(hfsmp
->hfc_filevp
);
1068 for (i
= listp
->hfl_next
; (i
< last
) && (blksmoved
< HFC_BLKSPERSYNC
); ++i
) {
1070 * Skip invalid entries (already in hot area).
1072 if (listp
->hfl_hotfile
[i
].hf_temperature
== 0) {
1077 * Acquire a vnode for this file.
1079 error
= hfs_vget(hfsmp
, listp
->hfl_hotfile
[i
].hf_fileid
, &vp
, 0);
1081 if (error
== ENOENT
) {
1084 continue; /* stale entry, go to next */
1088 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1089 printf("hotfiles_adopt: huh, not a file %d (%d)\n", listp
->hfl_hotfile
[i
].hf_fileid
, VTOC(vp
)->c_cnid
);
1090 hfs_unlock(VTOC(vp
));
1092 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1094 continue; /* stale entry, go to next */
1096 if (hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1097 hfs_unlock(VTOC(vp
));
1099 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1101 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1102 continue; /* stale entry, go to next */
1104 fileblocks
= VTOF(vp
)->ff_blocks
;
1105 if (fileblocks
> hfsmp
->hfs_hotfile_freeblks
) {
1106 hfs_unlock(VTOC(vp
));
1109 listp
->hfl_totalblocks
-= fileblocks
;
1110 continue; /* entry too big, go to next */
1113 if ((blksmoved
> 0) &&
1114 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1115 hfs_unlock(VTOC(vp
));
1117 break; /* adopt this entry the next time around */
1119 /* Start a new transaction. */
1120 if (hfs_start_transaction(hfsmp
) != 0) {
1122 hfs_unlock(VTOC(vp
));
1128 if (VTOC(vp
)->c_desc
.cd_nameptr
)
1129 data
= *(u_int32_t
*)(VTOC(vp
)->c_desc
.cd_nameptr
);
1133 error
= hfs_relocate(vp
, hfsmp
->hfs_hotfile_start
, kauth_cred_get(), current_proc());
1134 hfs_unlock(VTOC(vp
));
1139 /* Keep hot file free space current. */
1140 hfsmp
->hfs_hotfile_freeblks
-= fileblocks
;
1141 listp
->hfl_totalblocks
-= fileblocks
;
1143 /* Insert hot file entry */
1144 key
->keyLength
= HFC_KEYLENGTH
;
1145 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
1146 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1149 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1151 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1152 error
= MacToVFSError(error
);
1157 /* Insert thread record */
1158 key
->keyLength
= HFC_KEYLENGTH
;
1159 key
->temperature
= HFC_LOOKUPTAG
;
1160 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1162 data
= listp
->hfl_hotfile
[i
].hf_temperature
;
1163 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1165 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1166 error
= MacToVFSError(error
);
1170 (void) BTFlushPath(filefork
);
1172 /* Transaction complete. */
1174 hfs_end_transaction(hfsmp
);
1178 blksmoved
+= fileblocks
;
1180 if (listp
->hfl_next
>= listp
->hfl_count
) {
1183 if (hfsmp
->hfs_hotfile_freeblks
<= 0) {
1185 printf("hotfiles_adopt: free space exhausted (%d)\n", hfsmp
->hfs_hotfile_freeblks
);
1192 printf("hotfiles_adopt: [%d] adopted %d blocks (%d left)\n", listp
->hfl_next
, blksmoved
, listp
->hfl_totalblocks
);
1194 /* Finish any outstanding transactions. */
1196 (void) BTFlushPath(filefork
);
1197 hfs_end_transaction(hfsmp
);
1200 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1202 if ((listp
->hfl_next
>= listp
->hfl_count
) || (hfsmp
->hfs_hotfile_freeblks
<= 0)) {
1204 printf("hotfiles_adopt: all done relocating %d files\n", listp
->hfl_count
);
1205 printf("hotfiles_adopt: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1209 FREE(iterator
, M_TEMP
);
1211 if (stage
!= HFC_ADOPTION
&& hfsmp
->hfc_filevp
) {
1212 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
1213 hfsmp
->hfc_filevp
= NULL
;
1215 hfsmp
->hfc_stage
= stage
;
1216 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1221 * Reclaim space by evicting the coldest files.
1223 * Requires that the hfc_mutex be held.
1226 hotfiles_evict(struct hfsmount
*hfsmp
, struct proc
*p
)
1228 BTreeIterator
* iterator
;
1231 filefork_t
* filefork
;
1232 hotfilelist_t
*listp
;
1233 enum hfc_stage stage
;
1234 u_int32_t savedtemp
;
1239 int startedtrans
= 0;
1242 if (hfsmp
->hfc_stage
!= HFC_EVICTION
) {
1246 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1249 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1253 stage
= hfsmp
->hfc_stage
;
1254 hfsmp
->hfc_stage
= HFC_BUSY
;
1256 filesmoved
= blksmoved
= 0;
1257 bt_op
= kBTreeFirstRecord
;
1259 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1260 bzero(iterator
, sizeof(*iterator
));
1261 key
= (HotFileKey
*) &iterator
->key
;
1263 filefork
= VTOF(hfsmp
->hfc_filevp
);
1265 while (listp
->hfl_reclaimblks
> 0 &&
1266 blksmoved
< HFC_BLKSPERSYNC
&&
1267 filesmoved
< HFC_FILESPERSYNC
) {
1270 * Obtain the first record (ie the coldest one).
1272 if (BTIterateRecord(filefork
, bt_op
, iterator
, NULL
, NULL
) != 0) {
1274 printf("hotfiles_evict: no more records\n");
1277 stage
= HFC_ADOPTION
;
1280 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1281 printf("hotfiles_evict: invalid key length %d\n", key
->keyLength
);
1285 if (key
->temperature
== HFC_LOOKUPTAG
) {
1287 printf("hotfiles_evict: ran into thread records\n");
1290 stage
= HFC_ADOPTION
;
1294 * Aquire the vnode for this file.
1296 error
= hfs_vget(hfsmp
, key
->fileID
, &vp
, 0);
1298 /* Start a new transaction. */
1299 if (hfs_start_transaction(hfsmp
) != 0) {
1301 hfs_unlock(VTOC(vp
));
1310 if (error
== ENOENT
) {
1311 goto delete; /* stale entry, go to next */
1313 printf("hotfiles_evict: err %d getting file %d\n",
1314 error
, key
->fileID
);
1318 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1319 printf("hotfiles_evict: huh, not a file %d\n", key
->fileID
);
1320 hfs_unlock(VTOC(vp
));
1322 goto delete; /* invalid entry, go to next */
1324 fileblocks
= VTOF(vp
)->ff_blocks
;
1325 if ((blksmoved
> 0) &&
1326 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1327 hfs_unlock(VTOC(vp
));
1332 * Make sure file is in the hot area.
1334 if (!hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1336 printf("hotfiles_evict: file %d isn't hot!\n", key
->fileID
);
1338 hfs_unlock(VTOC(vp
));
1340 goto delete; /* stale entry, go to next */
1344 * Relocate file out of hot area.
1346 error
= hfs_relocate(vp
, HFSTOVCB(hfsmp
)->nextAllocation
, proc_ucred(p
), p
);
1348 printf("hotfiles_evict: err %d relocating file %d\n", error
, key
->fileID
);
1349 hfs_unlock(VTOC(vp
));
1351 bt_op
= kBTreeNextRecord
;
1352 goto next
; /* go to next */
1356 // We do not believe that this call to hfs_fsync() is
1357 // necessary and it causes a journal transaction
1358 // deadlock so we are removing it.
1360 // (void) hfs_fsync(vp, MNT_WAIT, 0, p);
1362 hfs_unlock(VTOC(vp
));
1365 hfsmp
->hfs_hotfile_freeblks
+= fileblocks
;
1366 listp
->hfl_reclaimblks
-= fileblocks
;
1367 if (listp
->hfl_reclaimblks
< 0)
1368 listp
->hfl_reclaimblks
= 0;
1369 blksmoved
+= fileblocks
;
1372 error
= BTDeleteRecord(filefork
, iterator
);
1374 error
= MacToVFSError(error
);
1377 savedtemp
= key
->temperature
;
1378 key
->temperature
= HFC_LOOKUPTAG
;
1379 error
= BTDeleteRecord(filefork
, iterator
);
1381 error
= MacToVFSError(error
);
1384 key
->temperature
= savedtemp
;
1386 (void) BTFlushPath(filefork
);
1388 /* Transaction complete. */
1390 hfs_end_transaction(hfsmp
);
1397 printf("hotfiles_evict: moved %d files (%d blks, %d to go)\n", filesmoved
, blksmoved
, listp
->hfl_reclaimblks
);
1399 /* Finish any outstanding transactions. */
1401 (void) BTFlushPath(filefork
);
1402 hfs_end_transaction(hfsmp
);
1405 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1408 * Move to next stage when finished.
1410 if (listp
->hfl_reclaimblks
<= 0) {
1411 stage
= HFC_ADOPTION
;
1413 printf("hotfiles_evict: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1416 FREE(iterator
, M_TEMP
);
1417 hfsmp
->hfc_stage
= stage
;
1418 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1423 * Age the existing records in the hot files b-tree.
1426 hotfiles_age(struct hfsmount
*hfsmp
)
1428 BTreeInfoRec btinfo
;
1429 BTreeIterator
* iterator
;
1430 BTreeIterator
* prev_iterator
;
1431 FSBufferDescriptor record
;
1432 FSBufferDescriptor prev_record
;
1434 HotFileKey
* prev_key
;
1435 filefork_t
* filefork
;
1437 u_int32_t prev_data
;
1446 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1447 bzero(iterator
, 2 * sizeof(*iterator
));
1448 key
= (HotFileKey
*) &iterator
->key
;
1450 prev_iterator
= &iterator
[1];
1451 prev_key
= (HotFileKey
*) &prev_iterator
->key
;
1453 record
.bufferAddress
= &data
;
1454 record
.itemSize
= sizeof(data
);
1455 record
.itemCount
= 1;
1456 prev_record
.bufferAddress
= &prev_data
;
1457 prev_record
.itemSize
= sizeof(prev_data
);
1458 prev_record
.itemCount
= 1;
1461 * Capture b-tree changes inside a transaction
1463 if (hfs_start_transaction(hfsmp
) != 0) {
1467 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1471 filefork
= VTOF(hfsmp
->hfc_filevp
);
1473 error
= BTGetInformation(filefork
, 0, &btinfo
);
1475 error
= MacToVFSError(error
);
1478 if (btinfo
.numRecords
< 2) {
1483 /* Only want 1st half of leaf records */
1484 numrecs
= (btinfo
.numRecords
/= 2) - 1;
1486 error
= BTIterateRecord(filefork
, kBTreeFirstRecord
, iterator
, &record
, &reclen
);
1488 printf("hfs_agehotfiles: BTIterateRecord: %d\n", error
);
1489 error
= MacToVFSError(error
);
1492 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1495 for (i
= 0; i
< numrecs
; ++i
) {
1496 error
= BTIterateRecord(filefork
, kBTreeNextRecord
, iterator
, &record
, &reclen
);
1498 if (key
->temperature
< prev_key
->temperature
) {
1499 printf("hfs_agehotfiles: out of order keys!\n");
1503 if (reclen
!= sizeof(data
)) {
1504 printf("hfs_agehotfiles: invalid record length %d\n", reclen
);
1508 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1509 printf("hfs_agehotfiles: invalid key length %d\n", key
->keyLength
);
1513 } else if ((error
== fsBTEndOfIterationErr
|| error
== fsBTRecordNotFoundErr
) &&
1514 (i
== (numrecs
- 1))) {
1517 printf("hfs_agehotfiles: %d of %d BTIterateRecord: %d\n", i
, numrecs
, error
);
1518 error
= MacToVFSError(error
);
1521 if (prev_key
->temperature
== HFC_LOOKUPTAG
) {
1523 printf("hfs_agehotfiles: ran into thread record\n");
1528 error
= BTDeleteRecord(filefork
, prev_iterator
);
1530 printf("hfs_agehotfiles: BTDeleteRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1531 error
= MacToVFSError(error
);
1535 /* Age by halving the temperature (floor = 4) */
1536 newtemp
= MAX(prev_key
->temperature
>> 1, 4);
1537 prev_key
->temperature
= newtemp
;
1539 error
= BTInsertRecord(filefork
, prev_iterator
, &prev_record
, prev_record
.itemSize
);
1541 printf("hfs_agehotfiles: BTInsertRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1542 error
= MacToVFSError(error
);
1547 * Update thread entry with latest temperature.
1549 prev_key
->temperature
= HFC_LOOKUPTAG
;
1550 error
= BTUpdateRecord(filefork
, prev_iterator
,
1551 (IterateCallBackProcPtr
)update_callback
,
1554 printf("hfs_agehotfiles: %d of %d BTUpdateRecord failed %d (file %d, %d)\n",
1555 i
, numrecs
, error
, prev_key
->fileID
, newtemp
);
1556 error
= MacToVFSError(error
);
1560 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1567 printf("hfs_agehotfiles: aged %d records out of %d\n", aged
, btinfo
.numRecords
);
1569 (void) BTFlushPath(filefork
);
1571 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1573 hfs_end_transaction(hfsmp
);
1575 FREE(iterator
, M_TEMP
);
1580 * Return true if any blocks (or all blocks if all is true)
1581 * are contained in the hot file region.
1584 hotextents(struct hfsmount
*hfsmp
, HFSPlusExtentDescriptor
* extents
)
1590 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1591 b1
= extents
[i
].startBlock
;
1594 b2
= b1
+ extents
[i
].blockCount
- 1;
1595 if ((b1
>= hfsmp
->hfs_hotfile_start
&&
1596 b2
<= hfsmp
->hfs_hotfile_end
) ||
1597 (b1
< hfsmp
->hfs_hotfile_end
&&
1598 b2
> hfsmp
->hfs_hotfile_end
)) {
1608 *========================================================================
1609 * HOT FILE B-TREE ROUTINES
1610 *========================================================================
1614 * Open the hot files b-tree for writing.
1616 * On successful exit the vnode has a reference but not an iocount.
1619 hfc_btree_open(struct hfsmount
*hfsmp
, struct vnode
**vpp
)
1623 struct cat_desc cdesc
;
1624 struct cat_attr cattr
;
1625 struct cat_fork cfork
;
1626 static char filename
[] = HFC_FILENAME
;
1634 bzero(&cdesc
, sizeof(cdesc
));
1635 cdesc
.cd_parentcnid
= kRootDirID
;
1636 cdesc
.cd_nameptr
= filename
;
1637 cdesc
.cd_namelen
= strlen(filename
);
1639 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1641 error
= cat_lookup(hfsmp
, &cdesc
, 0, &cdesc
, &cattr
, &cfork
, NULL
);
1643 hfs_systemfile_unlock(hfsmp
, lockflags
);
1646 printf("hfc_btree_open: cat_lookup error %d\n", error
);
1650 cdesc
.cd_flags
|= CD_ISMETA
;
1651 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cdesc
, 0, &cattr
, &cfork
, &vp
);
1653 printf("hfc_btree_open: hfs_getnewvnode error %d\n", error
);
1654 cat_releasedesc(&cdesc
);
1657 if (!vnode_issystem(vp
)) {
1659 printf("hfc_btree_open: file has UBC, try again\n");
1661 hfs_unlock(VTOC(vp
));
1670 /* Open the B-tree file for writing... */
1671 error
= BTOpenPath(VTOF(vp
), (KeyCompareProcPtr
) hfc_comparekeys
);
1673 printf("hfc_btree_open: BTOpenPath error %d\n", error
);
1674 error
= MacToVFSError(error
);
1677 hfs_unlock(VTOC(vp
));
1680 vnode_ref(vp
); /* keep a reference while its open */
1684 if (!vnode_issystem(vp
))
1685 panic("hfc_btree_open: not a system file (vp = 0x%08x)", vp
);
1687 if (UBCINFOEXISTS(vp
))
1688 panic("hfc_btree_open: has UBCInfo (vp = 0x%08x)", vp
);
1694 * Close the hot files b-tree.
1696 * On entry the vnode has a reference.
1699 hfc_btree_close(struct hfsmount
*hfsmp
, struct vnode
*vp
)
1701 struct proc
*p
= current_proc();
1706 journal_flush(hfsmp
->jnl
);
1709 if (vnode_get(vp
) == 0) {
1710 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
1712 (void) hfs_fsync(vp
, MNT_WAIT
, 0, p
);
1713 error
= BTClosePath(VTOF(vp
));
1714 hfs_unlock(VTOC(vp
));
1725 * Create a hot files btree file.
1729 hfc_btree_create(struct hfsmount
*hfsmp
, int nodesize
, int entries
)
1731 struct vnode
*dvp
= NULL
;
1732 struct vnode
*vp
= NULL
;
1733 struct cnode
*cp
= NULL
;
1734 struct vfs_context context
;
1735 struct vnode_attr va
;
1736 struct componentname cname
;
1737 static char filename
[] = HFC_FILENAME
;
1740 context
.vc_proc
= current_proc();
1741 context
.vc_ucred
= kauth_cred_get();
1743 if (hfsmp
->hfc_filevp
)
1744 panic("hfc_btree_create: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
1746 error
= VFS_ROOT(HFSTOVFS(hfsmp
), &dvp
, &context
);
1750 cname
.cn_nameiop
= CREATE
;
1751 cname
.cn_flags
= ISLASTCN
;
1752 cname
.cn_context
= &context
;
1753 cname
.cn_pnbuf
= filename
;
1754 cname
.cn_pnlen
= sizeof(filename
);
1755 cname
.cn_nameptr
= filename
;
1756 cname
.cn_namelen
= strlen(filename
);
1758 cname
.cn_consume
= 0;
1761 VATTR_SET(&va
, va_type
, VREG
);
1762 VATTR_SET(&va
, va_mode
, S_IFREG
| S_IRUSR
| S_IWUSR
);
1763 VATTR_SET(&va
, va_uid
, 0);
1764 VATTR_SET(&va
, va_gid
, 0);
1766 /* call ourselves directly, ignore the higher-level VFS file creation code */
1767 error
= VNOP_CREATE(dvp
, &vp
, &cname
, &va
, &context
);
1769 printf("HFS: error %d creating HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1776 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1781 /* Don't use non-regular files or files with links. */
1782 if (!vnode_isreg(vp
) || cp
->c_nlink
!= 1) {
1787 printf("HFS: created HFBT on %s\n", HFSTOVCB(hfsmp
)->vcbVN
);
1789 if (VTOF(vp
)->ff_size
< (u_int64_t
)nodesize
) {
1793 BTNodeDescriptor
*ndp
;
1795 HotFilesInfo
*hotfileinfo
;
1801 * Mark it invisible (truncate will pull these changes).
1803 ((FndrFileInfo
*)&cp
->c_finderinfo
[0])->fdFlags
|=
1804 SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1806 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buffer
, nodesize
)) {
1810 bzero(buffer
, nodesize
);
1811 index
= (int16_t *)buffer
;
1813 entirespernode
= (nodesize
- sizeof(BTNodeDescriptor
) - 2) /
1814 (sizeof(HotFileKey
) + 6);
1815 nodecnt
= 2 + howmany(entries
* 2, entirespernode
);
1816 nodecnt
= roundup(nodecnt
, 8);
1817 filesize
= nodecnt
* nodesize
;
1819 /* FILL IN THE NODE DESCRIPTOR: */
1820 ndp
= (BTNodeDescriptor
*)buffer
;
1821 ndp
->kind
= kBTHeaderNode
;
1822 ndp
->numRecords
= SWAP_BE16 (3);
1823 offset
= sizeof(BTNodeDescriptor
);
1824 index
[(nodesize
/ 2) - 1] = SWAP_BE16 (offset
);
1826 /* FILL IN THE HEADER RECORD: */
1827 bthp
= (BTHeaderRec
*)((UInt8
*)buffer
+ offset
);
1828 bthp
->nodeSize
= SWAP_BE16 (nodesize
);
1829 bthp
->totalNodes
= SWAP_BE32 (filesize
/ nodesize
);
1830 bthp
->freeNodes
= SWAP_BE32 (nodecnt
- 1);
1831 bthp
->clumpSize
= SWAP_BE32 (filesize
);
1832 bthp
->btreeType
= kUserBTreeType
; /* non-metadata */
1833 bthp
->attributes
|= SWAP_BE32 (kBTBigKeysMask
);
1834 bthp
->maxKeyLength
= SWAP_BE16 (HFC_KEYLENGTH
);
1835 offset
+= sizeof(BTHeaderRec
);
1836 index
[(nodesize
/ 2) - 2] = SWAP_BE16 (offset
);
1838 /* FILL IN THE USER RECORD: */
1839 hotfileinfo
= (HotFilesInfo
*)((UInt8
*)buffer
+ offset
);
1840 hotfileinfo
->magic
= SWAP_BE32 (HFC_MAGIC
);
1841 hotfileinfo
->version
= SWAP_BE32 (HFC_VERSION
);
1842 hotfileinfo
->duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
1843 hotfileinfo
->timebase
= 0;
1844 hotfileinfo
->timeleft
= 0;
1845 hotfileinfo
->threshold
= SWAP_BE32 (HFC_MINIMUM_TEMPERATURE
);
1846 hotfileinfo
->maxfileblks
= SWAP_BE32 (HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
);
1847 hotfileinfo
->maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
1848 strcpy(hotfileinfo
->tag
, hfc_tag
);
1849 offset
+= kBTreeHeaderUserBytes
;
1850 index
[(nodesize
/ 2) - 3] = SWAP_BE16 (offset
);
1852 /* FILL IN THE MAP RECORD (only one node in use). */
1853 *((u_int8_t
*)buffer
+ offset
) = 0x80;
1854 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
1855 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
1856 index
[(nodesize
/ 2) - 4] = SWAP_BE16 (offset
);
1858 vnode_setnoflush(vp
);
1859 error
= hfs_truncate(vp
, (off_t
)filesize
, IO_NDELAY
, 0, &context
);
1861 printf("HFS: error %d growing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1864 cp
->c_flag
|= C_ZFWANTSYNC
;
1865 cp
->c_zftimeout
= 1;
1868 struct vnop_write_args args
;
1871 auio
= uio_create(1, 0, UIO_SYSSPACE32
, UIO_WRITE
);
1872 uio_addiov(auio
, (uintptr_t)buffer
, nodesize
);
1874 args
.a_desc
= &vnop_write_desc
;
1878 args
.a_context
= &context
;
1883 error
= hfs_vnop_write(&args
);
1885 printf("HFS: error %d writing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1889 kmem_free(kernel_map
, (vm_offset_t
)buffer
, nodesize
);
1905 * Compare two hot file b-tree keys.
1907 * Result: +n search key > trial key
1908 * 0 search key = trial key
1909 * -n search key < trial key
1912 hfc_comparekeys(HotFileKey
*searchKey
, HotFileKey
*trialKey
)
1915 * Compared temperatures first.
1917 if (searchKey
->temperature
== trialKey
->temperature
) {
1919 * Temperatures are equal so compare file ids.
1921 if (searchKey
->fileID
== trialKey
->fileID
) {
1923 * File ids are equal so compare fork types.
1925 if (searchKey
->forkType
== trialKey
->forkType
) {
1927 } else if (searchKey
->forkType
> trialKey
->forkType
) {
1930 } else if (searchKey
->fileID
> trialKey
->fileID
) {
1933 } else if (searchKey
->temperature
> trialKey
->temperature
) {
1942 *========================================================================
1943 * HOT FILE DATA COLLECTING ROUTINES
1944 *========================================================================
1948 * Lookup a hot file entry in the tree.
1951 static hotfile_entry_t
*
1952 hf_lookup(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
1954 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1957 entry
->temperature
!= temperature
&&
1958 entry
->fileid
!= fileid
) {
1960 if (temperature
> entry
->temperature
)
1961 entry
= entry
->right
;
1962 else if (temperature
< entry
->temperature
)
1963 entry
= entry
->left
;
1964 else if (fileid
> entry
->fileid
)
1965 entry
= entry
->right
;
1967 entry
= entry
->left
;
1974 * Insert a hot file entry into the tree.
1977 hf_insert(hotfile_data_t
*hotdata
, hotfile_entry_t
*newentry
)
1979 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1980 u_int32_t fileid
= newentry
->fileid
;
1981 u_int32_t temperature
= newentry
->temperature
;
1983 if (entry
== NULL
) {
1984 hotdata
->rootentry
= newentry
;
1985 hotdata
->coldest
= newentry
;
1986 hotdata
->activefiles
++;
1991 if (temperature
> entry
->temperature
) {
1993 entry
= entry
->right
;
1995 entry
->right
= newentry
;
1998 } else if (temperature
< entry
->temperature
) {
2000 entry
= entry
->left
;
2002 entry
->left
= newentry
;
2005 } else if (fileid
> entry
->fileid
) {
2007 entry
= entry
->right
;
2009 if (entry
->fileid
!= fileid
)
2010 entry
->right
= newentry
;
2015 entry
= entry
->left
;
2017 if (entry
->fileid
!= fileid
)
2018 entry
->left
= newentry
;
2024 hotdata
->activefiles
++;
2028 * Find the coldest entry in the tree.
2030 static hotfile_entry_t
*
2031 hf_coldest(hotfile_data_t
*hotdata
)
2033 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2037 entry
= entry
->left
;
2043 * Find the hottest entry in the tree.
2045 static hotfile_entry_t
*
2046 hf_hottest(hotfile_data_t
*hotdata
)
2048 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2051 while (entry
->right
)
2052 entry
= entry
->right
;
2058 * Delete a hot file entry from the tree.
2061 hf_delete(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
2063 hotfile_entry_t
*entry
, *parent
, *next
;
2066 entry
= hotdata
->rootentry
;
2069 entry
->temperature
!= temperature
&&
2070 entry
->fileid
!= fileid
) {
2073 if (temperature
> entry
->temperature
)
2074 entry
= entry
->right
;
2075 else if (temperature
< entry
->temperature
)
2076 entry
= entry
->left
;
2077 else if (fileid
> entry
->fileid
)
2078 entry
= entry
->right
;
2080 entry
= entry
->left
;
2085 * Reorginize the sub-trees spanning from our entry.
2087 if ((next
= entry
->right
)) {
2088 hotfile_entry_t
*pnextl
, *psub
;
2090 * Tree pruning: take the left branch of the
2091 * current entry and place it at the lowest
2092 * left branch of the current right branch
2096 /* Walk the Right/Left sub tree from current entry */
2097 while ((pnextl
= psub
->left
))
2100 /* Plug the old left tree to the new ->Right leftmost entry */
2101 psub
->left
= entry
->left
;
2103 } else /* only left sub-tree, simple case */ {
2107 * Now, plug the current entry sub tree to
2108 * the good pointer of our parent entry.
2111 hotdata
->rootentry
= next
;
2112 else if (parent
->left
== entry
)
2113 parent
->left
= next
;
2115 parent
->right
= next
;
2117 /* Place entry back on the free-list */
2120 entry
->temperature
= 0;
2122 entry
->right
= hotdata
->freelist
;
2123 hotdata
->freelist
= entry
;
2124 hotdata
->activefiles
--;
2126 if (hotdata
->coldest
== entry
|| hotdata
->coldest
== NULL
) {
2127 hotdata
->coldest
= hf_coldest(hotdata
);
2134 * Get a free hot file entry.
2136 static hotfile_entry_t
*
2137 hf_getnewentry(hotfile_data_t
*hotdata
)
2139 hotfile_entry_t
* entry
;
2142 * When the free list is empty then steal the coldest one
2144 if (hotdata
->freelist
== NULL
) {
2145 entry
= hf_coldest(hotdata
);
2146 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2148 entry
= hotdata
->freelist
;
2149 hotdata
->freelist
= entry
->right
;
2157 * Generate a sorted list of hot files (hottest to coldest).
2159 * As a side effect, every node in the hot file tree will be
2160 * deleted (moved to the free list).
2163 hf_getsortedlist(hotfile_data_t
* hotdata
, hotfilelist_t
*sortedlist
)
2166 hotfile_entry_t
*entry
;
2168 while ((entry
= hf_hottest(hotdata
)) != NULL
) {
2169 sortedlist
->hfl_hotfile
[i
].hf_fileid
= entry
->fileid
;
2170 sortedlist
->hfl_hotfile
[i
].hf_temperature
= entry
->temperature
;
2171 sortedlist
->hfl_hotfile
[i
].hf_blocks
= entry
->blocks
;
2172 sortedlist
->hfl_totalblocks
+= entry
->blocks
;
2175 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2178 sortedlist
->hfl_count
= i
;
2181 printf("HFS: hf_getsortedlist returned %d entries\n", i
);
2188 hf_maxdepth(hotfile_entry_t
* root
, int depth
, int *maxdepth
)
2192 if (depth
> *maxdepth
)
2194 hf_maxdepth(root
->left
, depth
, maxdepth
);
2195 hf_maxdepth(root
->right
, depth
, maxdepth
);
2200 hf_printtree(hotfile_entry_t
* root
)
2203 hf_printtree(root
->left
);
2204 printf("temperature: % 8d, fileid %d\n", root
->temperature
, root
->fileid
);
2205 hf_printtree(root
->right
);