2 * Copyright (c) 2003-2005 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@
23 #include <sys/param.h>
24 #include <sys/systm.h>
25 #include <sys/fcntl.h>
26 #include <sys/kernel.h>
27 #include <sys/malloc.h>
29 #include <sys/vnode.h>
30 #include <sys/vnode_internal.h>
31 #include <sys/kauth.h>
34 #include <hfs/hfs_endian.h>
35 #include <hfs/hfs_format.h>
36 #include <hfs/hfs_mount.h>
37 #include <hfs/hfs_hotfiles.h>
39 #include "hfscommon/headers/BTreeScanner.h"
48 * Hot File List (runtime).
50 typedef struct hotfileinfo
{
52 u_int32_t hf_temperature
;
56 typedef struct hotfilelist
{
58 u_int32_t hfl_version
;
59 time_t hfl_duration
; /* duration of sample period */
60 int hfl_count
; /* count of hot files recorded */
61 int hfl_next
; /* next file to move */
62 int hfl_totalblocks
; /* total hot file blocks */
63 int hfl_reclaimblks
; /* blocks to reclaim in HFV */
64 u_int32_t hfl_spare
[2];
65 hotfileinfo_t hfl_hotfile
[1]; /* array of hot files */
70 * Hot File Entry (runtime).
72 typedef struct hotfile_entry
{
73 struct hotfile_entry
*left
;
74 struct hotfile_entry
*right
;
76 u_int32_t temperature
;
81 * Hot File Recording Data (runtime).
83 typedef struct hotfile_data
{
84 struct hfsmount
*hfsmp
;
86 int activefiles
; /* active number of hot files */
89 hotfile_entry_t
*rootentry
;
90 hotfile_entry_t
*freelist
;
91 hotfile_entry_t
*coldest
;
92 hotfile_entry_t entries
[1];
95 static int hfs_recording_start (struct hfsmount
*);
96 static int hfs_recording_stop (struct hfsmount
*);
100 * Hot File Data recording functions (in-memory binary tree).
102 static void hf_insert (hotfile_data_t
*, hotfile_entry_t
*);
103 static void hf_delete (hotfile_data_t
*, u_int32_t
, u_int32_t
);
104 static hotfile_entry_t
* hf_coldest (hotfile_data_t
*);
105 static hotfile_entry_t
* hf_getnewentry (hotfile_data_t
*);
106 static void hf_getsortedlist (hotfile_data_t
*, hotfilelist_t
*);
109 static hotfile_entry_t
* hf_lookup (hotfile_data_t
*, u_int32_t
, u_int32_t
);
110 static void hf_maxdepth(hotfile_entry_t
*, int, int *);
111 static void hf_printtree (hotfile_entry_t
*);
115 * Hot File misc support functions.
117 static int hotfiles_collect (struct hfsmount
*);
118 static int hotfiles_age (struct hfsmount
*);
119 static int hotfiles_adopt (struct hfsmount
*);
120 static int hotfiles_evict (struct hfsmount
*, struct proc
*);
121 static int hotfiles_refine (struct hfsmount
*);
122 static int hotextents(struct hfsmount
*, HFSPlusExtentDescriptor
*);
123 static int hfs_addhotfile_internal(struct vnode
*);
127 * Hot File Cluster B-tree (on disk) functions.
129 static int hfc_btree_create (struct hfsmount
*, int, int);
130 static int hfc_btree_open (struct hfsmount
*, struct vnode
**);
131 static int hfc_btree_close (struct hfsmount
*, struct vnode
*);
132 static int hfc_comparekeys (HotFileKey
*, HotFileKey
*);
135 char hfc_tag
[] = "CLUSTERED HOT FILES B-TREE ";
137 extern int UBCINFOEXISTS(struct vnode
* vp
);
138 extern int hfs_vnop_write(struct vnop_write_args
*ap
);
142 *========================================================================
143 * HOT FILE INTERFACE ROUTINES
144 *========================================================================
148 * Start recording the hotest files on a file system.
150 * Requires that the hfc_mutex be held.
153 hfs_recording_start(struct hfsmount
*hfsmp
)
155 hotfile_data_t
*hotdata
;
162 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
163 (hfsmp
->jnl
== NULL
) ||
164 (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) == 0) {
167 if (HFSTOVCB(hfsmp
)->freeBlocks
< (2 * (u_int32_t
)hfsmp
->hfs_hotfile_maxblks
)) {
170 if (hfsmp
->hfc_stage
!= HFC_IDLE
) {
173 hfsmp
->hfc_stage
= HFC_BUSY
;
176 * Dump previous recording data.
178 if (hfsmp
->hfc_recdata
) {
181 tmp
= hfsmp
->hfc_recdata
;
182 hfsmp
->hfc_recdata
= NULL
;
189 * On first startup check for suspended recording.
191 if (hfsmp
->hfc_timebase
== 0 &&
192 hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
) == 0) {
193 HotFilesInfo hotfileinfo
;
195 if ((BTGetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
,
196 sizeof(hotfileinfo
)) == 0) &&
197 (SWAP_BE32 (hotfileinfo
.magic
) == HFC_MAGIC
) &&
198 (SWAP_BE32 (hotfileinfo
.timeleft
) > 0) &&
199 (SWAP_BE32 (hotfileinfo
.timebase
) > 0)) {
200 hfsmp
->hfc_maxfiles
= SWAP_BE32 (hotfileinfo
.maxfilecnt
);
201 hfsmp
->hfc_timeout
= SWAP_BE32 (hotfileinfo
.timeleft
) + tv
.tv_sec
;
202 hfsmp
->hfc_timebase
= SWAP_BE32 (hotfileinfo
.timebase
);
204 printf("Resume recording hot files on %s (%d secs left)\n",
205 hfsmp
->vcbVN
, SWAP_BE32 (hotfileinfo
.timeleft
));
208 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
209 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
210 hfsmp
->hfc_timeout
= hfsmp
->hfc_timebase
+ HFC_DEFAULT_DURATION
;
212 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
213 hfsmp
->hfc_filevp
= NULL
;
215 struct cat_attr cattr
;
219 * Make sure a btree file exists.
221 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
223 !S_ISREG(cattr
.ca_mode
) &&
224 (error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
))) {
225 hfsmp
->hfc_stage
= HFC_IDLE
;
226 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
230 printf("HFS: begin recording hot files on %s\n", hfsmp
->vcbVN
);
232 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
233 hfsmp
->hfc_timeout
= tv
.tv_sec
+ HFC_DEFAULT_DURATION
;
235 /* Reset time base. */
236 if (hfsmp
->hfc_timebase
== 0) {
237 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
239 time_t cumulativebase
;
241 cumulativebase
= hfsmp
->hfc_timeout
- (HFC_CUMULATIVE_CYCLES
* HFC_DEFAULT_DURATION
);
242 hfsmp
->hfc_timebase
= MAX(hfsmp
->hfc_timebase
, cumulativebase
);
246 if ((hfsmp
->hfc_maxfiles
== 0) ||
247 (hfsmp
->hfc_maxfiles
> HFC_MAXIMUM_FILE_COUNT
)) {
248 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
250 maxentries
= hfsmp
->hfc_maxfiles
;
252 size
= sizeof(hotfile_data_t
) + (maxentries
* sizeof(hotfile_entry_t
));
253 MALLOC(hotdata
, hotfile_data_t
*, size
, M_TEMP
, M_WAITOK
);
254 bzero(hotdata
, size
);
256 for (i
= 1; i
< maxentries
; i
++)
257 hotdata
->entries
[i
-1].right
= &hotdata
->entries
[i
];
259 hotdata
->freelist
= &hotdata
->entries
[0];
261 * Establish minimum temperature and maximum file size.
263 hotdata
->threshold
= HFC_MINIMUM_TEMPERATURE
;
264 hotdata
->maxblocks
= HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
;
265 hotdata
->hfsmp
= hfsmp
;
267 hfsmp
->hfc_recdata
= hotdata
;
268 hfsmp
->hfc_stage
= HFC_RECORDING
;
269 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
274 * Stop recording the hotest files on a file system.
276 * Requires that the hfc_mutex be held.
279 hfs_recording_stop(struct hfsmount
*hfsmp
)
281 hotfile_data_t
*hotdata
;
282 hotfilelist_t
*listp
;
285 enum hfc_stage newstage
= HFC_IDLE
;
288 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
291 hotfiles_collect(hfsmp
);
293 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
296 hfsmp
->hfc_stage
= HFC_BUSY
;
299 * Convert hot file data into a simple file id list....
301 * then dump the sample data
304 printf("HFS: end of hot file recording on %s\n", hfsmp
->vcbVN
);
306 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
309 hfsmp
->hfc_recdata
= NULL
;
310 hfsmp
->hfc_stage
= HFC_EVALUATION
;
311 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
314 printf(" curentries: %d\n", hotdata
->activefiles
);
317 * If no hot files recorded then we're done.
319 if (hotdata
->rootentry
== NULL
) {
324 /* Open the B-tree file for writing... */
325 if (hfsmp
->hfc_filevp
)
326 panic("hfs_recording_stop: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
328 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
334 * Age the previous set of clustered hot files.
336 error
= hotfiles_age(hfsmp
);
338 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
339 hfsmp
->hfc_filevp
= NULL
;
344 * Create a sorted list of hotest files.
346 size
= sizeof(hotfilelist_t
);
347 size
+= sizeof(hotfileinfo_t
) * (hotdata
->activefiles
- 1);
348 MALLOC(listp
, hotfilelist_t
*, size
, M_TEMP
, M_WAITOK
);
351 hf_getsortedlist(hotdata
, listp
); /* NOTE: destroys hot file tree! */
353 listp
->hfl_duration
= tv
.tv_sec
- hfsmp
->hfc_timebase
;
354 hfsmp
->hfc_recdata
= listp
;
357 * Account for duplicates.
359 error
= hotfiles_refine(hfsmp
);
361 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
362 hfsmp
->hfc_filevp
= NULL
;
367 * Compute the amount of space to reclaim...
369 if (listp
->hfl_totalblocks
> hfsmp
->hfs_hotfile_freeblks
) {
370 listp
->hfl_reclaimblks
=
371 MIN(listp
->hfl_totalblocks
, hfsmp
->hfs_hotfile_maxblks
) -
372 hfsmp
->hfs_hotfile_freeblks
;
374 printf("hfs_recording_stop: need to reclaim %d blocks\n", listp
->hfl_reclaimblks
);
376 if (listp
->hfl_reclaimblks
)
377 newstage
= HFC_EVICTION
;
379 newstage
= HFC_ADOPTION
;
381 newstage
= HFC_ADOPTION
;
384 if (newstage
== HFC_ADOPTION
&& listp
->hfl_totalblocks
== 0) {
385 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
386 hfsmp
->hfc_filevp
= NULL
;
391 if (newstage
== HFC_EVICTION
)
392 printf("HFS: evicting coldest files\n");
393 else if (newstage
== HFC_ADOPTION
)
394 printf("HFS: adopting hotest files\n");
396 FREE(hotdata
, M_TEMP
);
398 hfsmp
->hfc_stage
= newstage
;
399 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
404 * Suspend recording the hotest files on a file system.
408 hfs_recording_suspend(struct hfsmount
*hfsmp
)
410 HotFilesInfo hotfileinfo
;
411 hotfile_data_t
*hotdata
= NULL
;
415 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
418 lck_mtx_lock(&hfsmp
->hfc_mutex
);
422 * A suspend can occur during eval/evict/adopt stage.
423 * In that case we would need to write out info and
424 * flush our HFBT vnode. Currently we just bail.
427 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
428 if (hotdata
== NULL
|| hfsmp
->hfc_stage
!= HFC_RECORDING
) {
432 hfsmp
->hfc_stage
= HFC_BUSY
;
435 printf("HFS: suspend hot file recording on %s\n", hfsmp
->vcbVN
);
437 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
439 printf("hfs_recording_suspend: err %d opening btree\n", error
);
443 if (hfs_start_transaction(hfsmp
) != 0) {
447 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
453 hotfileinfo
.magic
= SWAP_BE32 (HFC_MAGIC
);
454 hotfileinfo
.version
= SWAP_BE32 (HFC_VERSION
);
455 hotfileinfo
.duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
456 hotfileinfo
.timebase
= SWAP_BE32 (hfsmp
->hfc_timebase
);
457 hotfileinfo
.timeleft
= SWAP_BE32 (hfsmp
->hfc_timeout
- tv
.tv_sec
);
458 hotfileinfo
.threshold
= SWAP_BE32 (hotdata
->threshold
);
459 hotfileinfo
.maxfileblks
= SWAP_BE32 (hotdata
->maxblocks
);
460 hotfileinfo
.maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
461 strcpy(hotfileinfo
.tag
, hfc_tag
);
462 (void) BTSetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
, sizeof(hotfileinfo
));
464 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
465 hfs_end_transaction(hfsmp
);
467 if (hfsmp
->hfc_filevp
) {
468 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
469 hfsmp
->hfc_filevp
= NULL
;
472 FREE(hotdata
, M_TEMP
);
473 hfsmp
->hfc_recdata
= NULL
;
475 hfsmp
->hfc_stage
= HFC_DISABLED
;
476 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
478 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
488 hfs_recording_init(struct hfsmount
*hfsmp
)
491 CatalogRecord
* datap
;
493 HFSPlusCatalogFile
*filep
;
494 BTScanState scanstate
;
495 BTreeIterator
* iterator
;
496 FSBufferDescriptor record
;
498 filefork_t
* filefork
;
500 struct cat_attr cattr
;
504 int inserted
= 0; /* debug variables */
508 * For now, only the boot volume is supported.
510 if ((vfs_flags(HFSTOVFS(hfsmp
)) & MNT_ROOTFS
) == 0) {
511 hfsmp
->hfc_stage
= HFC_DISABLED
;
516 * If the Hot File btree exists then metadata zone is ready.
518 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
519 if (cnid
!= 0 && S_ISREG(cattr
.ca_mode
)) {
520 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
521 hfsmp
->hfc_stage
= HFC_IDLE
;
524 error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
);
527 printf("Error %d creating hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
532 * Open the Hot File B-tree file for writing.
534 if (hfsmp
->hfc_filevp
)
535 panic("hfs_recording_init: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
536 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
539 printf("Error %d opening hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
543 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
544 bzero(iterator
, sizeof(*iterator
));
545 key
= (HotFileKey
*) &iterator
->key
;
546 key
->keyLength
= HFC_KEYLENGTH
;
548 record
.bufferAddress
= &data
;
549 record
.itemSize
= sizeof(u_int32_t
);
550 record
.itemCount
= 1;
552 printf("Evaluating space for \"%s\" metadata zone...\n", HFSTOVCB(hfsmp
)->vcbVN
);
555 * Get ready to scan the Catalog file.
557 error
= BTScanInitialize(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), 0, 0, 0,
558 kCatSearchBufferSize
, &scanstate
);
560 printf("hfs_recording_init: err %d BTScanInit\n", error
);
565 * The writes to Hot File B-tree file are journaled.
567 if (hfs_start_transaction(hfsmp
) != 0) {
571 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
575 filefork
= VTOF(hfsmp
->hfc_filevp
);
578 * Visit all the catalog btree leaf records.
581 error
= BTScanNextRecord(&scanstate
, 0, (void **)&keyp
, (void **)&datap
, &dataSize
);
583 if (error
== btNotFound
)
586 printf("hfs_recording_init: err %d BTScanNext\n", error
);
589 if ((datap
->recordType
!= kHFSPlusFileRecord
) ||
590 (dataSize
!= sizeof(HFSPlusCatalogFile
))) {
593 filep
= (HFSPlusCatalogFile
*)datap
;
595 if (filep
->dataFork
.totalBlocks
== 0) {
599 * Any file that has blocks inside the hot file
600 * space is recorded for later eviction.
602 * For now, resource forks are ignored.
604 if (!hotextents(hfsmp
, &filep
->dataFork
.extents
[0])) {
607 cnid
= filep
->fileID
;
609 /* Skip over journal files. */
610 if (cnid
== hfsmp
->hfs_jnlfileid
|| cnid
== hfsmp
->hfs_jnlinfoblkid
) {
614 * XXX - need to skip quota files as well.
617 /* Insert a hot file entry. */
618 key
->keyLength
= HFC_KEYLENGTH
;
619 key
->temperature
= HFC_MINIMUM_TEMPERATURE
;
623 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
625 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
626 error
= MacToVFSError(error
);
630 /* Insert the corresponding thread record. */
631 key
->keyLength
= HFC_KEYLENGTH
;
632 key
->temperature
= HFC_LOOKUPTAG
;
635 data
= HFC_MINIMUM_TEMPERATURE
;
636 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
638 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
639 error
= MacToVFSError(error
);
644 (void) BTFlushPath(filefork
);
645 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
647 hfs_end_transaction(hfsmp
);
649 printf("%d files identified out of %d\n", inserted
, filecount
);
653 (void) BTScanTerminate(&scanstate
, &data
, &data
, &data
);
655 FREE(iterator
, M_TEMP
);
656 if (hfsmp
->hfc_filevp
) {
657 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
658 hfsmp
->hfc_filevp
= NULL
;
661 hfsmp
->hfc_stage
= HFC_IDLE
;
667 * Use sync to perform ocassional background work.
671 hfs_hotfilesync(struct hfsmount
*hfsmp
, struct proc
*p
)
673 if (hfsmp
->hfc_stage
) {
676 lck_mtx_lock(&hfsmp
->hfc_mutex
);
678 switch (hfsmp
->hfc_stage
) {
680 (void) hfs_recording_start(hfsmp
);
685 if (tv
.tv_sec
> hfsmp
->hfc_timeout
)
686 (void) hfs_recording_stop(hfsmp
);
690 (void) hotfiles_evict(hfsmp
, p
);
694 (void) hotfiles_adopt(hfsmp
);
700 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
706 * Add a hot file to the recording list.
708 * This can happen when a hot file gets reclaimed or at the
709 * end of the recording period for any active hot file.
711 * NOTE: Since both the data and resource fork can be hot,
712 * there can be two entries for the same file id.
714 * Note: the cnode is locked on entry.
718 hfs_addhotfile(struct vnode
*vp
)
724 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
727 lck_mtx_lock(&hfsmp
->hfc_mutex
);
728 error
= hfs_addhotfile_internal(vp
);
729 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
734 hfs_addhotfile_internal(struct vnode
*vp
)
736 hotfile_data_t
*hotdata
;
737 hotfile_entry_t
*entry
;
741 u_int32_t temperature
;
744 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
747 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
750 /* Skip resource forks for now. */
751 if (VNODE_IS_RSRC(vp
)) {
754 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
) {
760 if ((ffp
->ff_bytesread
== 0) ||
761 (ffp
->ff_blocks
== 0) ||
762 (ffp
->ff_blocks
> hotdata
->maxblocks
) ||
763 (cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ||
764 (cp
->c_flags
& UF_NODUMP
) ||
765 (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
769 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
770 if (temperature
< hotdata
->threshold
) {
774 * If there is room or this file is hotter than
775 * the coldest one then add it to the list.
778 if ((hotdata
->activefiles
< hfsmp
->hfc_maxfiles
) ||
779 (hotdata
->coldest
== NULL
) ||
780 (temperature
> hotdata
->coldest
->temperature
)) {
782 entry
= hf_getnewentry(hotdata
);
783 entry
->temperature
= temperature
;
784 entry
->fileid
= cp
->c_fileid
;
785 entry
->blocks
= ffp
->ff_blocks
;
786 hf_insert(hotdata
, entry
);
794 * Remove a hot file from the recording list.
796 * This can happen when a hot file becomes
797 * an active vnode (active hot files are
798 * not kept in the recording list until the
799 * end of the recording period).
801 * Note: the cnode is locked on entry.
805 hfs_removehotfile(struct vnode
*vp
)
807 hotfile_data_t
*hotdata
;
811 u_int32_t temperature
;
814 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
817 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
824 if ((ffp
->ff_bytesread
== 0) || (ffp
->ff_blocks
== 0) ||
825 (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
829 lck_mtx_lock(&hfsmp
->hfc_mutex
);
830 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
832 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
)
835 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
836 if (temperature
< hotdata
->threshold
)
839 if (hotdata
->coldest
&& (temperature
>= hotdata
->coldest
->temperature
)) {
841 hf_delete(hotdata
, VTOC(vp
)->c_fileid
, temperature
);
845 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
851 *========================================================================
852 * HOT FILE MAINTENANCE ROUTINES
853 *========================================================================
857 hotfiles_collect_callback(struct vnode
*vp
, __unused
void *cargs
)
859 if ((vnode_isreg(vp
) || vnode_islnk(vp
)) && !vnode_issystem(vp
))
860 (void) hfs_addhotfile_internal(vp
);
862 return (VNODE_RETURNED
);
866 * Add all active hot files to the recording list.
869 hotfiles_collect(struct hfsmount
*hfsmp
)
871 struct mount
*mp
= HFSTOVFS(hfsmp
);
873 if (vfs_busy(mp
, LK_NOWAIT
))
877 * hotfiles_collect_callback will be called for each vnode
878 * hung off of this mount point
880 * properly referenced and unreferenced around the callback
882 vnode_iterate(mp
, 0, hotfiles_collect_callback
, (void *)NULL
);
891 * Update the data of a btree record
892 * This is called from within BTUpdateRecord.
895 update_callback(const HotFileKey
*key
, u_int32_t
*data
, u_int32_t
*state
)
897 if (key
->temperature
== HFC_LOOKUPTAG
)
903 * Identify files already in hot area.
906 hotfiles_refine(struct hfsmount
*hfsmp
)
908 BTreeIterator
* iterator
;
910 filefork_t
* filefork
;
911 hotfilelist_t
*listp
;
912 FSBufferDescriptor record
;
919 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
922 mp
= HFSTOVFS(hfsmp
);
924 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
925 bzero(iterator
, sizeof(*iterator
));
926 key
= (HotFileKey
*) &iterator
->key
;
928 record
.bufferAddress
= &data
;
929 record
.itemSize
= sizeof(u_int32_t
);
930 record
.itemCount
= 1;
932 if (hfs_start_transaction(hfsmp
) != 0) {
936 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
940 filefork
= VTOF(hfsmp
->hfc_filevp
);
942 for (i
= 0; i
< listp
->hfl_count
; ++i
) {
944 * Check if entry (thread) is already in hot area.
946 key
->keyLength
= HFC_KEYLENGTH
;
947 key
->temperature
= HFC_LOOKUPTAG
;
948 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
950 (void) BTInvalidateHint(iterator
);
951 if (BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
) != 0) {
952 continue; /* not in hot area, so skip */
956 * Update thread entry with latest temperature.
958 error
= BTUpdateRecord(filefork
, iterator
,
959 (IterateCallBackProcPtr
)update_callback
,
960 &listp
->hfl_hotfile
[i
].hf_temperature
);
962 printf("hotfiles_refine: BTUpdateRecord failed %d (file %d)\n", error
, key
->fileID
);
963 error
= MacToVFSError(error
);
967 * Re-key entry with latest temperature.
969 key
->keyLength
= HFC_KEYLENGTH
;
970 key
->temperature
= data
;
971 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
973 /* Pick up record data. */
974 (void) BTInvalidateHint(iterator
);
975 (void) BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
);
976 error
= BTDeleteRecord(filefork
, iterator
);
978 printf("hotfiles_refine: BTDeleteRecord failed %d (file %d)\n", error
, key
->fileID
);
979 error
= MacToVFSError(error
);
982 key
->keyLength
= HFC_KEYLENGTH
;
983 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
984 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
986 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
988 printf("hotfiles_refine: BTInsertRecord failed %d (file %d)\n", error
, key
->fileID
);
989 error
= MacToVFSError(error
);
994 * Invalidate this entry in the list.
996 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
997 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1001 (void) BTFlushPath(filefork
);
1002 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1004 hfs_end_transaction(hfsmp
);
1006 FREE(iterator
, M_TEMP
);
1011 * Move new hot files into hot area.
1013 * Requires that the hfc_mutex be held.
1016 hotfiles_adopt(struct hfsmount
*hfsmp
)
1018 BTreeIterator
* iterator
;
1020 filefork_t
* filefork
;
1021 hotfilelist_t
*listp
;
1022 FSBufferDescriptor record
;
1025 enum hfc_stage stage
;
1031 int startedtrans
= 0;
1033 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1036 if (hfsmp
->hfc_stage
!= HFC_ADOPTION
) {
1039 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1043 stage
= hfsmp
->hfc_stage
;
1044 hfsmp
->hfc_stage
= HFC_BUSY
;
1047 last
= listp
->hfl_next
+ HFC_FILESPERSYNC
;
1048 if (last
> listp
->hfl_count
)
1049 last
= listp
->hfl_count
;
1051 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1052 bzero(iterator
, sizeof(*iterator
));
1053 key
= (HotFileKey
*) &iterator
->key
;
1054 key
->keyLength
= HFC_KEYLENGTH
;
1056 record
.bufferAddress
= &data
;
1057 record
.itemSize
= sizeof(u_int32_t
);
1058 record
.itemCount
= 1;
1060 filefork
= VTOF(hfsmp
->hfc_filevp
);
1062 for (i
= listp
->hfl_next
; (i
< last
) && (blksmoved
< HFC_BLKSPERSYNC
); ++i
) {
1064 * Skip invalid entries (already in hot area).
1066 if (listp
->hfl_hotfile
[i
].hf_temperature
== 0) {
1071 * Acquire a vnode for this file.
1073 error
= hfs_vget(hfsmp
, listp
->hfl_hotfile
[i
].hf_fileid
, &vp
, 0);
1075 if (error
== ENOENT
) {
1078 continue; /* stale entry, go to next */
1082 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1083 printf("hotfiles_adopt: huh, not a file %d (%d)\n", listp
->hfl_hotfile
[i
].hf_fileid
, VTOC(vp
)->c_cnid
);
1084 hfs_unlock(VTOC(vp
));
1086 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1088 continue; /* stale entry, go to next */
1090 if (hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1091 hfs_unlock(VTOC(vp
));
1093 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1095 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1096 continue; /* stale entry, go to next */
1098 fileblocks
= VTOF(vp
)->ff_blocks
;
1099 if (fileblocks
> hfsmp
->hfs_hotfile_freeblks
) {
1100 hfs_unlock(VTOC(vp
));
1103 listp
->hfl_totalblocks
-= fileblocks
;
1104 continue; /* entry too big, go to next */
1107 if ((blksmoved
> 0) &&
1108 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1109 hfs_unlock(VTOC(vp
));
1111 break; /* adopt this entry the next time around */
1113 /* Start a new transaction. */
1114 if (hfs_start_transaction(hfsmp
) != 0) {
1116 hfs_unlock(VTOC(vp
));
1122 if (VTOC(vp
)->c_desc
.cd_nameptr
)
1123 data
= *(u_int32_t
*)(VTOC(vp
)->c_desc
.cd_nameptr
);
1127 error
= hfs_relocate(vp
, hfsmp
->hfs_hotfile_start
, kauth_cred_get(), current_proc());
1128 hfs_unlock(VTOC(vp
));
1133 /* Keep hot file free space current. */
1134 hfsmp
->hfs_hotfile_freeblks
-= fileblocks
;
1135 listp
->hfl_totalblocks
-= fileblocks
;
1137 /* Insert hot file entry */
1138 key
->keyLength
= HFC_KEYLENGTH
;
1139 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
1140 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1143 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1145 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1146 error
= MacToVFSError(error
);
1151 /* Insert thread record */
1152 key
->keyLength
= HFC_KEYLENGTH
;
1153 key
->temperature
= HFC_LOOKUPTAG
;
1154 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1156 data
= listp
->hfl_hotfile
[i
].hf_temperature
;
1157 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1159 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1160 error
= MacToVFSError(error
);
1164 (void) BTFlushPath(filefork
);
1166 /* Transaction complete. */
1168 hfs_end_transaction(hfsmp
);
1172 blksmoved
+= fileblocks
;
1174 if (listp
->hfl_next
>= listp
->hfl_count
) {
1177 if (hfsmp
->hfs_hotfile_freeblks
<= 0) {
1179 printf("hotfiles_adopt: free space exhausted (%d)\n", hfsmp
->hfs_hotfile_freeblks
);
1186 printf("hotfiles_adopt: [%d] adopted %d blocks (%d left)\n", listp
->hfl_next
, blksmoved
, listp
->hfl_totalblocks
);
1188 /* Finish any outstanding transactions. */
1190 (void) BTFlushPath(filefork
);
1191 hfs_end_transaction(hfsmp
);
1194 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1196 if ((listp
->hfl_next
>= listp
->hfl_count
) || (hfsmp
->hfs_hotfile_freeblks
<= 0)) {
1198 printf("hotfiles_adopt: all done relocating %d files\n", listp
->hfl_count
);
1199 printf("hotfiles_adopt: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1203 FREE(iterator
, M_TEMP
);
1205 if (stage
!= HFC_ADOPTION
&& hfsmp
->hfc_filevp
) {
1206 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
1207 hfsmp
->hfc_filevp
= NULL
;
1209 hfsmp
->hfc_stage
= stage
;
1210 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1215 * Reclaim space by evicting the coldest files.
1217 * Requires that the hfc_mutex be held.
1220 hotfiles_evict(struct hfsmount
*hfsmp
, struct proc
*p
)
1222 BTreeIterator
* iterator
;
1225 filefork_t
* filefork
;
1226 hotfilelist_t
*listp
;
1227 enum hfc_stage stage
;
1228 u_int32_t savedtemp
;
1233 int startedtrans
= 0;
1236 if (hfsmp
->hfc_stage
!= HFC_EVICTION
) {
1240 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1243 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1247 stage
= hfsmp
->hfc_stage
;
1248 hfsmp
->hfc_stage
= HFC_BUSY
;
1250 filesmoved
= blksmoved
= 0;
1251 bt_op
= kBTreeFirstRecord
;
1253 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1254 bzero(iterator
, sizeof(*iterator
));
1255 key
= (HotFileKey
*) &iterator
->key
;
1257 filefork
= VTOF(hfsmp
->hfc_filevp
);
1259 while (listp
->hfl_reclaimblks
> 0 &&
1260 blksmoved
< HFC_BLKSPERSYNC
&&
1261 filesmoved
< HFC_FILESPERSYNC
) {
1264 * Obtain the first record (ie the coldest one).
1266 if (BTIterateRecord(filefork
, bt_op
, iterator
, NULL
, NULL
) != 0) {
1268 printf("hotfiles_evict: no more records\n");
1271 stage
= HFC_ADOPTION
;
1274 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1275 printf("hotfiles_evict: invalid key length %d\n", key
->keyLength
);
1279 if (key
->temperature
== HFC_LOOKUPTAG
) {
1281 printf("hotfiles_evict: ran into thread records\n");
1284 stage
= HFC_ADOPTION
;
1288 * Aquire the vnode for this file.
1290 error
= hfs_vget(hfsmp
, key
->fileID
, &vp
, 0);
1292 /* Start a new transaction. */
1293 if (hfs_start_transaction(hfsmp
) != 0) {
1295 hfs_unlock(VTOC(vp
));
1304 if (error
== ENOENT
) {
1305 goto delete; /* stale entry, go to next */
1307 printf("hotfiles_evict: err %d getting file %d\n",
1308 error
, key
->fileID
);
1312 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1313 printf("hotfiles_evict: huh, not a file %d\n", key
->fileID
);
1314 hfs_unlock(VTOC(vp
));
1316 goto delete; /* invalid entry, go to next */
1318 fileblocks
= VTOF(vp
)->ff_blocks
;
1319 if ((blksmoved
> 0) &&
1320 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1321 hfs_unlock(VTOC(vp
));
1326 * Make sure file is in the hot area.
1328 if (!hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1330 printf("hotfiles_evict: file %d isn't hot!\n", key
->fileID
);
1332 hfs_unlock(VTOC(vp
));
1334 goto delete; /* stale entry, go to next */
1338 * Relocate file out of hot area.
1340 error
= hfs_relocate(vp
, HFSTOVCB(hfsmp
)->nextAllocation
, proc_ucred(p
), p
);
1342 printf("hotfiles_evict: err %d relocating file %d\n", error
, key
->fileID
);
1343 hfs_unlock(VTOC(vp
));
1345 bt_op
= kBTreeNextRecord
;
1346 goto next
; /* go to next */
1350 // We do not believe that this call to hfs_fsync() is
1351 // necessary and it causes a journal transaction
1352 // deadlock so we are removing it.
1354 // (void) hfs_fsync(vp, MNT_WAIT, 0, p);
1356 hfs_unlock(VTOC(vp
));
1359 hfsmp
->hfs_hotfile_freeblks
+= fileblocks
;
1360 listp
->hfl_reclaimblks
-= fileblocks
;
1361 if (listp
->hfl_reclaimblks
< 0)
1362 listp
->hfl_reclaimblks
= 0;
1363 blksmoved
+= fileblocks
;
1366 error
= BTDeleteRecord(filefork
, iterator
);
1368 error
= MacToVFSError(error
);
1371 savedtemp
= key
->temperature
;
1372 key
->temperature
= HFC_LOOKUPTAG
;
1373 error
= BTDeleteRecord(filefork
, iterator
);
1375 error
= MacToVFSError(error
);
1378 key
->temperature
= savedtemp
;
1380 (void) BTFlushPath(filefork
);
1382 /* Transaction complete. */
1384 hfs_end_transaction(hfsmp
);
1391 printf("hotfiles_evict: moved %d files (%d blks, %d to go)\n", filesmoved
, blksmoved
, listp
->hfl_reclaimblks
);
1393 /* Finish any outstanding transactions. */
1395 (void) BTFlushPath(filefork
);
1396 hfs_end_transaction(hfsmp
);
1399 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1402 * Move to next stage when finished.
1404 if (listp
->hfl_reclaimblks
<= 0) {
1405 stage
= HFC_ADOPTION
;
1407 printf("hotfiles_evict: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1410 FREE(iterator
, M_TEMP
);
1411 hfsmp
->hfc_stage
= stage
;
1412 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1417 * Age the existing records in the hot files b-tree.
1420 hotfiles_age(struct hfsmount
*hfsmp
)
1422 BTreeInfoRec btinfo
;
1423 BTreeIterator
* iterator
;
1424 BTreeIterator
* prev_iterator
;
1425 FSBufferDescriptor record
;
1426 FSBufferDescriptor prev_record
;
1428 HotFileKey
* prev_key
;
1429 filefork_t
* filefork
;
1431 u_int32_t prev_data
;
1440 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1441 bzero(iterator
, 2 * sizeof(*iterator
));
1442 key
= (HotFileKey
*) &iterator
->key
;
1444 prev_iterator
= &iterator
[1];
1445 prev_key
= (HotFileKey
*) &prev_iterator
->key
;
1447 record
.bufferAddress
= &data
;
1448 record
.itemSize
= sizeof(data
);
1449 record
.itemCount
= 1;
1450 prev_record
.bufferAddress
= &prev_data
;
1451 prev_record
.itemSize
= sizeof(prev_data
);
1452 prev_record
.itemCount
= 1;
1455 * Capture b-tree changes inside a transaction
1457 if (hfs_start_transaction(hfsmp
) != 0) {
1461 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1465 filefork
= VTOF(hfsmp
->hfc_filevp
);
1467 error
= BTGetInformation(filefork
, 0, &btinfo
);
1469 error
= MacToVFSError(error
);
1472 if (btinfo
.numRecords
< 2) {
1477 /* Only want 1st half of leaf records */
1478 numrecs
= (btinfo
.numRecords
/= 2) - 1;
1480 error
= BTIterateRecord(filefork
, kBTreeFirstRecord
, iterator
, &record
, &reclen
);
1482 printf("hfs_agehotfiles: BTIterateRecord: %d\n", error
);
1483 error
= MacToVFSError(error
);
1486 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1489 for (i
= 0; i
< numrecs
; ++i
) {
1490 error
= BTIterateRecord(filefork
, kBTreeNextRecord
, iterator
, &record
, &reclen
);
1492 if (key
->temperature
< prev_key
->temperature
) {
1493 printf("hfs_agehotfiles: out of order keys!\n");
1497 if (reclen
!= sizeof(data
)) {
1498 printf("hfs_agehotfiles: invalid record length %d\n", reclen
);
1502 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1503 printf("hfs_agehotfiles: invalid key length %d\n", key
->keyLength
);
1507 } else if ((error
== fsBTEndOfIterationErr
|| error
== fsBTRecordNotFoundErr
) &&
1508 (i
== (numrecs
- 1))) {
1511 printf("hfs_agehotfiles: %d of %d BTIterateRecord: %d\n", i
, numrecs
, error
);
1512 error
= MacToVFSError(error
);
1515 if (prev_key
->temperature
== HFC_LOOKUPTAG
) {
1517 printf("hfs_agehotfiles: ran into thread record\n");
1522 error
= BTDeleteRecord(filefork
, prev_iterator
);
1524 printf("hfs_agehotfiles: BTDeleteRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1525 error
= MacToVFSError(error
);
1529 /* Age by halving the temperature (floor = 4) */
1530 newtemp
= MAX(prev_key
->temperature
>> 1, 4);
1531 prev_key
->temperature
= newtemp
;
1533 error
= BTInsertRecord(filefork
, prev_iterator
, &prev_record
, prev_record
.itemSize
);
1535 printf("hfs_agehotfiles: BTInsertRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1536 error
= MacToVFSError(error
);
1541 * Update thread entry with latest temperature.
1543 prev_key
->temperature
= HFC_LOOKUPTAG
;
1544 error
= BTUpdateRecord(filefork
, prev_iterator
,
1545 (IterateCallBackProcPtr
)update_callback
,
1548 printf("hfs_agehotfiles: %d of %d BTUpdateRecord failed %d (file %d, %d)\n",
1549 i
, numrecs
, error
, prev_key
->fileID
, newtemp
);
1550 error
= MacToVFSError(error
);
1554 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1561 printf("hfs_agehotfiles: aged %d records out of %d\n", aged
, btinfo
.numRecords
);
1563 (void) BTFlushPath(filefork
);
1565 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1567 hfs_end_transaction(hfsmp
);
1569 FREE(iterator
, M_TEMP
);
1574 * Return true if any blocks (or all blocks if all is true)
1575 * are contained in the hot file region.
1578 hotextents(struct hfsmount
*hfsmp
, HFSPlusExtentDescriptor
* extents
)
1584 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1585 b1
= extents
[i
].startBlock
;
1588 b2
= b1
+ extents
[i
].blockCount
- 1;
1589 if ((b1
>= hfsmp
->hfs_hotfile_start
&&
1590 b2
<= hfsmp
->hfs_hotfile_end
) ||
1591 (b1
< hfsmp
->hfs_hotfile_end
&&
1592 b2
> hfsmp
->hfs_hotfile_end
)) {
1602 *========================================================================
1603 * HOT FILE B-TREE ROUTINES
1604 *========================================================================
1608 * Open the hot files b-tree for writing.
1610 * On successful exit the vnode has a reference but not an iocount.
1613 hfc_btree_open(struct hfsmount
*hfsmp
, struct vnode
**vpp
)
1617 struct cat_desc cdesc
;
1618 struct cat_attr cattr
;
1619 struct cat_fork cfork
;
1620 static char filename
[] = HFC_FILENAME
;
1628 bzero(&cdesc
, sizeof(cdesc
));
1629 cdesc
.cd_parentcnid
= kRootDirID
;
1630 cdesc
.cd_nameptr
= filename
;
1631 cdesc
.cd_namelen
= strlen(filename
);
1633 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1635 error
= cat_lookup(hfsmp
, &cdesc
, 0, &cdesc
, &cattr
, &cfork
, NULL
);
1637 hfs_systemfile_unlock(hfsmp
, lockflags
);
1640 printf("hfc_btree_open: cat_lookup error %d\n", error
);
1644 cdesc
.cd_flags
|= CD_ISMETA
;
1645 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cdesc
, 0, &cattr
, &cfork
, &vp
);
1647 printf("hfc_btree_open: hfs_getnewvnode error %d\n", error
);
1648 cat_releasedesc(&cdesc
);
1651 if (!vnode_issystem(vp
)) {
1653 printf("hfc_btree_open: file has UBC, try again\n");
1655 hfs_unlock(VTOC(vp
));
1664 /* Open the B-tree file for writing... */
1665 error
= BTOpenPath(VTOF(vp
), (KeyCompareProcPtr
) hfc_comparekeys
);
1667 printf("hfc_btree_open: BTOpenPath error %d\n", error
);
1668 error
= MacToVFSError(error
);
1671 hfs_unlock(VTOC(vp
));
1674 vnode_ref(vp
); /* keep a reference while its open */
1678 if (!vnode_issystem(vp
))
1679 panic("hfc_btree_open: not a system file (vp = 0x%08x)", vp
);
1681 if (UBCINFOEXISTS(vp
))
1682 panic("hfc_btree_open: has UBCInfo (vp = 0x%08x)", vp
);
1688 * Close the hot files b-tree.
1690 * On entry the vnode has a reference.
1693 hfc_btree_close(struct hfsmount
*hfsmp
, struct vnode
*vp
)
1695 struct proc
*p
= current_proc();
1700 journal_flush(hfsmp
->jnl
);
1703 if (vnode_get(vp
) == 0) {
1704 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
1706 (void) hfs_fsync(vp
, MNT_WAIT
, 0, p
);
1707 error
= BTClosePath(VTOF(vp
));
1708 hfs_unlock(VTOC(vp
));
1719 * Create a hot files btree file.
1723 hfc_btree_create(struct hfsmount
*hfsmp
, int nodesize
, int entries
)
1725 struct vnode
*dvp
= NULL
;
1726 struct vnode
*vp
= NULL
;
1727 struct cnode
*cp
= NULL
;
1728 struct vfs_context context
;
1729 struct vnode_attr va
;
1730 struct componentname cname
;
1731 static char filename
[] = HFC_FILENAME
;
1734 context
.vc_proc
= current_proc();
1735 context
.vc_ucred
= kauth_cred_get();
1737 if (hfsmp
->hfc_filevp
)
1738 panic("hfc_btree_create: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
1740 error
= VFS_ROOT(HFSTOVFS(hfsmp
), &dvp
, &context
);
1744 cname
.cn_nameiop
= CREATE
;
1745 cname
.cn_flags
= ISLASTCN
;
1746 cname
.cn_context
= &context
;
1747 cname
.cn_pnbuf
= filename
;
1748 cname
.cn_pnlen
= sizeof(filename
);
1749 cname
.cn_nameptr
= filename
;
1750 cname
.cn_namelen
= strlen(filename
);
1752 cname
.cn_consume
= 0;
1755 VATTR_SET(&va
, va_type
, VREG
);
1756 VATTR_SET(&va
, va_mode
, S_IFREG
| S_IRUSR
| S_IWUSR
);
1757 VATTR_SET(&va
, va_uid
, 0);
1758 VATTR_SET(&va
, va_gid
, 0);
1760 /* call ourselves directly, ignore the higher-level VFS file creation code */
1761 error
= VNOP_CREATE(dvp
, &vp
, &cname
, &va
, &context
);
1763 printf("HFS: error %d creating HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1770 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1775 /* Don't use non-regular files or files with links. */
1776 if (!vnode_isreg(vp
) || cp
->c_nlink
!= 1) {
1781 printf("HFS: created HFBT on %s\n", HFSTOVCB(hfsmp
)->vcbVN
);
1783 if (VTOF(vp
)->ff_size
< (u_int64_t
)nodesize
) {
1787 BTNodeDescriptor
*ndp
;
1789 HotFilesInfo
*hotfileinfo
;
1795 * Mark it invisible (truncate will pull these changes).
1797 ((FndrFileInfo
*)&cp
->c_finderinfo
[0])->fdFlags
|=
1798 SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1800 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buffer
, nodesize
)) {
1804 bzero(buffer
, nodesize
);
1805 index
= (int16_t *)buffer
;
1807 entirespernode
= (nodesize
- sizeof(BTNodeDescriptor
) - 2) /
1808 (sizeof(HotFileKey
) + 6);
1809 nodecnt
= 2 + howmany(entries
* 2, entirespernode
);
1810 nodecnt
= roundup(nodecnt
, 8);
1811 filesize
= nodecnt
* nodesize
;
1813 /* FILL IN THE NODE DESCRIPTOR: */
1814 ndp
= (BTNodeDescriptor
*)buffer
;
1815 ndp
->kind
= kBTHeaderNode
;
1816 ndp
->numRecords
= SWAP_BE16 (3);
1817 offset
= sizeof(BTNodeDescriptor
);
1818 index
[(nodesize
/ 2) - 1] = SWAP_BE16 (offset
);
1820 /* FILL IN THE HEADER RECORD: */
1821 bthp
= (BTHeaderRec
*)((UInt8
*)buffer
+ offset
);
1822 bthp
->nodeSize
= SWAP_BE16 (nodesize
);
1823 bthp
->totalNodes
= SWAP_BE32 (filesize
/ nodesize
);
1824 bthp
->freeNodes
= SWAP_BE32 (nodecnt
- 1);
1825 bthp
->clumpSize
= SWAP_BE32 (filesize
);
1826 bthp
->btreeType
= kUserBTreeType
; /* non-metadata */
1827 bthp
->attributes
|= SWAP_BE32 (kBTBigKeysMask
);
1828 bthp
->maxKeyLength
= SWAP_BE16 (HFC_KEYLENGTH
);
1829 offset
+= sizeof(BTHeaderRec
);
1830 index
[(nodesize
/ 2) - 2] = SWAP_BE16 (offset
);
1832 /* FILL IN THE USER RECORD: */
1833 hotfileinfo
= (HotFilesInfo
*)((UInt8
*)buffer
+ offset
);
1834 hotfileinfo
->magic
= SWAP_BE32 (HFC_MAGIC
);
1835 hotfileinfo
->version
= SWAP_BE32 (HFC_VERSION
);
1836 hotfileinfo
->duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
1837 hotfileinfo
->timebase
= 0;
1838 hotfileinfo
->timeleft
= 0;
1839 hotfileinfo
->threshold
= SWAP_BE32 (HFC_MINIMUM_TEMPERATURE
);
1840 hotfileinfo
->maxfileblks
= SWAP_BE32 (HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
);
1841 hotfileinfo
->maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
1842 strcpy(hotfileinfo
->tag
, hfc_tag
);
1843 offset
+= kBTreeHeaderUserBytes
;
1844 index
[(nodesize
/ 2) - 3] = SWAP_BE16 (offset
);
1846 /* FILL IN THE MAP RECORD (only one node in use). */
1847 *((u_int8_t
*)buffer
+ offset
) = 0x80;
1848 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
1849 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
1850 index
[(nodesize
/ 2) - 4] = SWAP_BE16 (offset
);
1852 vnode_setnoflush(vp
);
1853 error
= hfs_truncate(vp
, (off_t
)filesize
, IO_NDELAY
, 0, &context
);
1855 printf("HFS: error %d growing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1858 cp
->c_flag
|= C_ZFWANTSYNC
;
1859 cp
->c_zftimeout
= 1;
1862 struct vnop_write_args args
;
1865 auio
= uio_create(1, 0, UIO_SYSSPACE32
, UIO_WRITE
);
1866 uio_addiov(auio
, (uintptr_t)buffer
, nodesize
);
1868 args
.a_desc
= &vnop_write_desc
;
1872 args
.a_context
= &context
;
1877 error
= hfs_vnop_write(&args
);
1879 printf("HFS: error %d writing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1883 kmem_free(kernel_map
, (vm_offset_t
)buffer
, nodesize
);
1899 * Compare two hot file b-tree keys.
1901 * Result: +n search key > trial key
1902 * 0 search key = trial key
1903 * -n search key < trial key
1906 hfc_comparekeys(HotFileKey
*searchKey
, HotFileKey
*trialKey
)
1909 * Compared temperatures first.
1911 if (searchKey
->temperature
== trialKey
->temperature
) {
1913 * Temperatures are equal so compare file ids.
1915 if (searchKey
->fileID
== trialKey
->fileID
) {
1917 * File ids are equal so compare fork types.
1919 if (searchKey
->forkType
== trialKey
->forkType
) {
1921 } else if (searchKey
->forkType
> trialKey
->forkType
) {
1924 } else if (searchKey
->fileID
> trialKey
->fileID
) {
1927 } else if (searchKey
->temperature
> trialKey
->temperature
) {
1936 *========================================================================
1937 * HOT FILE DATA COLLECTING ROUTINES
1938 *========================================================================
1942 * Lookup a hot file entry in the tree.
1945 static hotfile_entry_t
*
1946 hf_lookup(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
1948 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1951 entry
->temperature
!= temperature
&&
1952 entry
->fileid
!= fileid
) {
1954 if (temperature
> entry
->temperature
)
1955 entry
= entry
->right
;
1956 else if (temperature
< entry
->temperature
)
1957 entry
= entry
->left
;
1958 else if (fileid
> entry
->fileid
)
1959 entry
= entry
->right
;
1961 entry
= entry
->left
;
1968 * Insert a hot file entry into the tree.
1971 hf_insert(hotfile_data_t
*hotdata
, hotfile_entry_t
*newentry
)
1973 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1974 u_int32_t fileid
= newentry
->fileid
;
1975 u_int32_t temperature
= newentry
->temperature
;
1977 if (entry
== NULL
) {
1978 hotdata
->rootentry
= newentry
;
1979 hotdata
->coldest
= newentry
;
1980 hotdata
->activefiles
++;
1985 if (temperature
> entry
->temperature
) {
1987 entry
= entry
->right
;
1989 entry
->right
= newentry
;
1992 } else if (temperature
< entry
->temperature
) {
1994 entry
= entry
->left
;
1996 entry
->left
= newentry
;
1999 } else if (fileid
> entry
->fileid
) {
2001 entry
= entry
->right
;
2003 if (entry
->fileid
!= fileid
)
2004 entry
->right
= newentry
;
2009 entry
= entry
->left
;
2011 if (entry
->fileid
!= fileid
)
2012 entry
->left
= newentry
;
2018 hotdata
->activefiles
++;
2022 * Find the coldest entry in the tree.
2024 static hotfile_entry_t
*
2025 hf_coldest(hotfile_data_t
*hotdata
)
2027 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2031 entry
= entry
->left
;
2037 * Find the hottest entry in the tree.
2039 static hotfile_entry_t
*
2040 hf_hottest(hotfile_data_t
*hotdata
)
2042 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2045 while (entry
->right
)
2046 entry
= entry
->right
;
2052 * Delete a hot file entry from the tree.
2055 hf_delete(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
2057 hotfile_entry_t
*entry
, *parent
, *next
;
2060 entry
= hotdata
->rootentry
;
2063 entry
->temperature
!= temperature
&&
2064 entry
->fileid
!= fileid
) {
2067 if (temperature
> entry
->temperature
)
2068 entry
= entry
->right
;
2069 else if (temperature
< entry
->temperature
)
2070 entry
= entry
->left
;
2071 else if (fileid
> entry
->fileid
)
2072 entry
= entry
->right
;
2074 entry
= entry
->left
;
2079 * Reorginize the sub-trees spanning from our entry.
2081 if ((next
= entry
->right
)) {
2082 hotfile_entry_t
*pnextl
, *psub
;
2084 * Tree pruning: take the left branch of the
2085 * current entry and place it at the lowest
2086 * left branch of the current right branch
2090 /* Walk the Right/Left sub tree from current entry */
2091 while ((pnextl
= psub
->left
))
2094 /* Plug the old left tree to the new ->Right leftmost entry */
2095 psub
->left
= entry
->left
;
2097 } else /* only left sub-tree, simple case */ {
2101 * Now, plug the current entry sub tree to
2102 * the good pointer of our parent entry.
2105 hotdata
->rootentry
= next
;
2106 else if (parent
->left
== entry
)
2107 parent
->left
= next
;
2109 parent
->right
= next
;
2111 /* Place entry back on the free-list */
2114 entry
->temperature
= 0;
2116 entry
->right
= hotdata
->freelist
;
2117 hotdata
->freelist
= entry
;
2118 hotdata
->activefiles
--;
2120 if (hotdata
->coldest
== entry
|| hotdata
->coldest
== NULL
) {
2121 hotdata
->coldest
= hf_coldest(hotdata
);
2128 * Get a free hot file entry.
2130 static hotfile_entry_t
*
2131 hf_getnewentry(hotfile_data_t
*hotdata
)
2133 hotfile_entry_t
* entry
;
2136 * When the free list is empty then steal the coldest one
2138 if (hotdata
->freelist
== NULL
) {
2139 entry
= hf_coldest(hotdata
);
2140 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2142 entry
= hotdata
->freelist
;
2143 hotdata
->freelist
= entry
->right
;
2151 * Generate a sorted list of hot files (hottest to coldest).
2153 * As a side effect, every node in the hot file tree will be
2154 * deleted (moved to the free list).
2157 hf_getsortedlist(hotfile_data_t
* hotdata
, hotfilelist_t
*sortedlist
)
2160 hotfile_entry_t
*entry
;
2162 while ((entry
= hf_hottest(hotdata
)) != NULL
) {
2163 sortedlist
->hfl_hotfile
[i
].hf_fileid
= entry
->fileid
;
2164 sortedlist
->hfl_hotfile
[i
].hf_temperature
= entry
->temperature
;
2165 sortedlist
->hfl_hotfile
[i
].hf_blocks
= entry
->blocks
;
2166 sortedlist
->hfl_totalblocks
+= entry
->blocks
;
2169 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2172 sortedlist
->hfl_count
= i
;
2175 printf("HFS: hf_getsortedlist returned %d entries\n", i
);
2182 hf_maxdepth(hotfile_entry_t
* root
, int depth
, int *maxdepth
)
2186 if (depth
> *maxdepth
)
2188 hf_maxdepth(root
->left
, depth
, maxdepth
);
2189 hf_maxdepth(root
->right
, depth
, maxdepth
);
2194 hf_printtree(hotfile_entry_t
* root
)
2197 hf_printtree(root
->left
);
2198 printf("temperature: % 8d, fileid %d\n", root
->temperature
, root
->fileid
);
2199 hf_printtree(root
->right
);