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
;
1232 int startedtrans
= 0;
1234 if (hfsmp
->hfc_stage
!= HFC_EVICTION
) {
1238 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1241 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1245 stage
= hfsmp
->hfc_stage
;
1246 hfsmp
->hfc_stage
= HFC_BUSY
;
1248 filesmoved
= blksmoved
= 0;
1250 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1251 bzero(iterator
, sizeof(*iterator
));
1252 key
= (HotFileKey
*) &iterator
->key
;
1254 filefork
= VTOF(hfsmp
->hfc_filevp
);
1256 while (listp
->hfl_reclaimblks
> 0 &&
1257 blksmoved
< HFC_BLKSPERSYNC
&&
1258 filesmoved
< HFC_FILESPERSYNC
) {
1261 * Obtain the first record (ie the coldest one).
1263 if (BTIterateRecord(filefork
, kBTreeFirstRecord
, iterator
, NULL
, NULL
) != 0) {
1265 printf("hotfiles_evict: no more records\n");
1268 stage
= HFC_ADOPTION
;
1271 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1272 printf("hotfiles_evict: invalid key length %d\n", key
->keyLength
);
1276 if (key
->temperature
== HFC_LOOKUPTAG
) {
1278 printf("hotfiles_evict: ran into thread records\n");
1281 stage
= HFC_ADOPTION
;
1285 * Aquire the vnode for this file.
1287 error
= hfs_vget(hfsmp
, key
->fileID
, &vp
, 0);
1289 /* Start a new transaction. */
1290 if (hfs_start_transaction(hfsmp
) != 0) {
1292 hfs_unlock(VTOC(vp
));
1301 if (error
== ENOENT
) {
1302 (void) BTDeleteRecord(filefork
, iterator
);
1303 key
->temperature
= HFC_LOOKUPTAG
;
1304 (void) BTDeleteRecord(filefork
, iterator
);
1305 goto next
; /* 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 (void) BTDeleteRecord(filefork
, iterator
);
1317 key
->temperature
= HFC_LOOKUPTAG
;
1318 (void) BTDeleteRecord(filefork
, iterator
);
1319 goto next
; /* invalid entry, go to next */
1321 fileblocks
= VTOF(vp
)->ff_blocks
;
1322 if ((blksmoved
> 0) &&
1323 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1324 hfs_unlock(VTOC(vp
));
1329 * Make sure file is in the hot area.
1331 if (!hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1333 printf("hotfiles_evict: file %d isn't hot!\n", key
->fileID
);
1335 hfs_unlock(VTOC(vp
));
1337 (void) BTDeleteRecord(filefork
, iterator
);
1338 key
->temperature
= HFC_LOOKUPTAG
;
1339 (void) BTDeleteRecord(filefork
, iterator
);
1340 goto next
; /* 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 goto next
; /* go to next */
1355 // We do not believe that this call to hfs_fsync() is
1356 // necessary and it causes a journal transaction
1357 // deadlock so we are removing it.
1359 // (void) hfs_fsync(vp, MNT_WAIT, 0, p);
1361 hfs_unlock(VTOC(vp
));
1364 hfsmp
->hfs_hotfile_freeblks
+= fileblocks
;
1365 listp
->hfl_reclaimblks
-= fileblocks
;
1366 if (listp
->hfl_reclaimblks
< 0)
1367 listp
->hfl_reclaimblks
= 0;
1368 blksmoved
+= fileblocks
;
1371 error
= BTDeleteRecord(filefork
, iterator
);
1373 printf("hotfiles_evict: BTDeleteRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1374 error
= MacToVFSError(error
);
1377 key
->temperature
= HFC_LOOKUPTAG
;
1378 error
= BTDeleteRecord(filefork
, iterator
);
1380 printf("hotfiles_evict: BTDeleteRecord thread failed %d (fileid %d)\n", error
, key
->fileID
);
1381 error
= MacToVFSError(error
);
1385 (void) BTFlushPath(filefork
);
1387 /* Transaction complete. */
1389 hfs_end_transaction(hfsmp
);
1396 printf("hotfiles_evict: moved %d files (%d blks, %d to go)\n", filesmoved
, blksmoved
, listp
->hfl_reclaimblks
);
1398 /* Finish any outstanding transactions. */
1400 (void) BTFlushPath(filefork
);
1401 hfs_end_transaction(hfsmp
);
1404 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1407 * Move to next stage when finished.
1409 if (listp
->hfl_reclaimblks
<= 0) {
1410 stage
= HFC_ADOPTION
;
1412 printf("hotfiles_evict: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1415 FREE(iterator
, M_TEMP
);
1416 hfsmp
->hfc_stage
= stage
;
1417 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1422 * Age the existing records in the hot files b-tree.
1425 hotfiles_age(struct hfsmount
*hfsmp
)
1427 BTreeInfoRec btinfo
;
1428 BTreeIterator
* iterator
;
1429 BTreeIterator
* prev_iterator
;
1430 FSBufferDescriptor record
;
1431 FSBufferDescriptor prev_record
;
1433 HotFileKey
* prev_key
;
1434 filefork_t
* filefork
;
1436 u_int32_t prev_data
;
1445 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1446 bzero(iterator
, 2 * sizeof(*iterator
));
1447 key
= (HotFileKey
*) &iterator
->key
;
1449 prev_iterator
= &iterator
[1];
1450 prev_key
= (HotFileKey
*) &prev_iterator
->key
;
1452 record
.bufferAddress
= &data
;
1453 record
.itemSize
= sizeof(data
);
1454 record
.itemCount
= 1;
1455 prev_record
.bufferAddress
= &prev_data
;
1456 prev_record
.itemSize
= sizeof(prev_data
);
1457 prev_record
.itemCount
= 1;
1460 * Capture b-tree changes inside a transaction
1462 if (hfs_start_transaction(hfsmp
) != 0) {
1466 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1470 filefork
= VTOF(hfsmp
->hfc_filevp
);
1472 error
= BTGetInformation(filefork
, 0, &btinfo
);
1474 error
= MacToVFSError(error
);
1477 if (btinfo
.numRecords
< 2) {
1482 /* Only want 1st half of leaf records */
1483 numrecs
= (btinfo
.numRecords
/= 2) - 1;
1485 error
= BTIterateRecord(filefork
, kBTreeFirstRecord
, iterator
, &record
, &reclen
);
1487 printf("hfs_agehotfiles: BTIterateRecord: %d\n", error
);
1488 error
= MacToVFSError(error
);
1491 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1494 for (i
= 0; i
< numrecs
; ++i
) {
1495 error
= BTIterateRecord(filefork
, kBTreeNextRecord
, iterator
, &record
, &reclen
);
1497 if (key
->temperature
< prev_key
->temperature
) {
1498 printf("hfs_agehotfiles: out of order keys!\n");
1502 if (reclen
!= sizeof(data
)) {
1503 printf("hfs_agehotfiles: invalid record length %d\n", reclen
);
1507 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1508 printf("hfs_agehotfiles: invalid key length %d\n", key
->keyLength
);
1512 } else if ((error
== fsBTEndOfIterationErr
|| error
== fsBTRecordNotFoundErr
) &&
1513 (i
== (numrecs
- 1))) {
1516 printf("hfs_agehotfiles: %d of %d BTIterateRecord: %d\n", i
, numrecs
, error
);
1517 error
= MacToVFSError(error
);
1520 if (prev_key
->temperature
== HFC_LOOKUPTAG
) {
1522 printf("hfs_agehotfiles: ran into thread record\n");
1527 error
= BTDeleteRecord(filefork
, prev_iterator
);
1529 printf("hfs_agehotfiles: BTDeleteRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1530 error
= MacToVFSError(error
);
1534 /* Age by halving the temperature (floor = 4) */
1535 newtemp
= MAX(prev_key
->temperature
>> 1, 4);
1536 prev_key
->temperature
= newtemp
;
1538 error
= BTInsertRecord(filefork
, prev_iterator
, &prev_record
, prev_record
.itemSize
);
1540 printf("hfs_agehotfiles: BTInsertRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1541 error
= MacToVFSError(error
);
1546 * Update thread entry with latest temperature.
1548 prev_key
->temperature
= HFC_LOOKUPTAG
;
1549 error
= BTUpdateRecord(filefork
, prev_iterator
,
1550 (IterateCallBackProcPtr
)update_callback
,
1553 printf("hfs_agehotfiles: %d of %d BTUpdateRecord failed %d (file %d, %d)\n",
1554 i
, numrecs
, error
, prev_key
->fileID
, newtemp
);
1555 error
= MacToVFSError(error
);
1559 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1566 printf("hfs_agehotfiles: aged %d records out of %d\n", aged
, btinfo
.numRecords
);
1568 (void) BTFlushPath(filefork
);
1570 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1572 hfs_end_transaction(hfsmp
);
1574 FREE(iterator
, M_TEMP
);
1579 * Return true if any blocks (or all blocks if all is true)
1580 * are contained in the hot file region.
1583 hotextents(struct hfsmount
*hfsmp
, HFSPlusExtentDescriptor
* extents
)
1589 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1590 b1
= extents
[i
].startBlock
;
1593 b2
= b1
+ extents
[i
].blockCount
- 1;
1594 if ((b1
>= hfsmp
->hfs_hotfile_start
&&
1595 b2
<= hfsmp
->hfs_hotfile_end
) ||
1596 (b1
< hfsmp
->hfs_hotfile_end
&&
1597 b2
> hfsmp
->hfs_hotfile_end
)) {
1607 *========================================================================
1608 * HOT FILE B-TREE ROUTINES
1609 *========================================================================
1613 * Open the hot files b-tree for writing.
1615 * On successful exit the vnode has a reference but not an iocount.
1618 hfc_btree_open(struct hfsmount
*hfsmp
, struct vnode
**vpp
)
1622 struct cat_desc cdesc
;
1623 struct cat_attr cattr
;
1624 struct cat_fork cfork
;
1625 static char filename
[] = HFC_FILENAME
;
1633 bzero(&cdesc
, sizeof(cdesc
));
1634 cdesc
.cd_parentcnid
= kRootDirID
;
1635 cdesc
.cd_nameptr
= filename
;
1636 cdesc
.cd_namelen
= strlen(filename
);
1638 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1640 error
= cat_lookup(hfsmp
, &cdesc
, 0, &cdesc
, &cattr
, &cfork
, NULL
);
1642 hfs_systemfile_unlock(hfsmp
, lockflags
);
1645 printf("hfc_btree_open: cat_lookup error %d\n", error
);
1649 cdesc
.cd_flags
|= CD_ISMETA
;
1650 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cdesc
, 0, &cattr
, &cfork
, &vp
);
1652 printf("hfc_btree_open: hfs_getnewvnode error %d\n", error
);
1653 cat_releasedesc(&cdesc
);
1656 if (!vnode_issystem(vp
)) {
1658 printf("hfc_btree_open: file has UBC, try again\n");
1660 hfs_unlock(VTOC(vp
));
1669 /* Open the B-tree file for writing... */
1670 error
= BTOpenPath(VTOF(vp
), (KeyCompareProcPtr
) hfc_comparekeys
);
1672 printf("hfc_btree_open: BTOpenPath error %d\n", error
);
1673 error
= MacToVFSError(error
);
1676 hfs_unlock(VTOC(vp
));
1679 vnode_ref(vp
); /* keep a reference while its open */
1683 if (!vnode_issystem(vp
))
1684 panic("hfc_btree_open: not a system file (vp = 0x%08x)", vp
);
1686 if (UBCINFOEXISTS(vp
))
1687 panic("hfc_btree_open: has UBCInfo (vp = 0x%08x)", vp
);
1693 * Close the hot files b-tree.
1695 * On entry the vnode has a reference.
1698 hfc_btree_close(struct hfsmount
*hfsmp
, struct vnode
*vp
)
1700 struct proc
*p
= current_proc();
1705 journal_flush(hfsmp
->jnl
);
1708 if (vnode_get(vp
) == 0) {
1709 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
1711 (void) hfs_fsync(vp
, MNT_WAIT
, 0, p
);
1712 error
= BTClosePath(VTOF(vp
));
1713 hfs_unlock(VTOC(vp
));
1724 * Create a hot files btree file.
1728 hfc_btree_create(struct hfsmount
*hfsmp
, int nodesize
, int entries
)
1730 struct vnode
*dvp
= NULL
;
1731 struct vnode
*vp
= NULL
;
1732 struct cnode
*cp
= NULL
;
1733 struct vfs_context context
;
1734 struct vnode_attr va
;
1735 struct componentname cname
;
1736 static char filename
[] = HFC_FILENAME
;
1739 context
.vc_proc
= current_proc();
1740 context
.vc_ucred
= kauth_cred_get();
1742 if (hfsmp
->hfc_filevp
)
1743 panic("hfc_btree_create: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
1745 error
= VFS_ROOT(HFSTOVFS(hfsmp
), &dvp
, &context
);
1749 cname
.cn_nameiop
= CREATE
;
1750 cname
.cn_flags
= ISLASTCN
;
1751 cname
.cn_context
= &context
;
1752 cname
.cn_pnbuf
= filename
;
1753 cname
.cn_pnlen
= sizeof(filename
);
1754 cname
.cn_nameptr
= filename
;
1755 cname
.cn_namelen
= strlen(filename
);
1757 cname
.cn_consume
= 0;
1760 VATTR_SET(&va
, va_type
, VREG
);
1761 VATTR_SET(&va
, va_mode
, S_IFREG
| S_IRUSR
| S_IWUSR
);
1762 VATTR_SET(&va
, va_uid
, 0);
1763 VATTR_SET(&va
, va_gid
, 0);
1765 /* call ourselves directly, ignore the higher-level VFS file creation code */
1766 error
= VNOP_CREATE(dvp
, &vp
, &cname
, &va
, &context
);
1768 printf("HFS: error %d creating HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1775 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1780 /* Don't use non-regular files or files with links. */
1781 if (!vnode_isreg(vp
) || cp
->c_nlink
!= 1) {
1786 printf("HFS: created HFBT on %s\n", HFSTOVCB(hfsmp
)->vcbVN
);
1788 if (VTOF(vp
)->ff_size
< (u_int64_t
)nodesize
) {
1792 BTNodeDescriptor
*ndp
;
1794 HotFilesInfo
*hotfileinfo
;
1800 * Mark it invisible (truncate will pull these changes).
1802 ((FndrFileInfo
*)&cp
->c_finderinfo
[0])->fdFlags
|=
1803 SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1805 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buffer
, nodesize
)) {
1809 bzero(buffer
, nodesize
);
1810 index
= (int16_t *)buffer
;
1812 entirespernode
= (nodesize
- sizeof(BTNodeDescriptor
) - 2) /
1813 (sizeof(HotFileKey
) + 6);
1814 nodecnt
= 2 + howmany(entries
* 2, entirespernode
);
1815 nodecnt
= roundup(nodecnt
, 8);
1816 filesize
= nodecnt
* nodesize
;
1818 /* FILL IN THE NODE DESCRIPTOR: */
1819 ndp
= (BTNodeDescriptor
*)buffer
;
1820 ndp
->kind
= kBTHeaderNode
;
1821 ndp
->numRecords
= SWAP_BE16 (3);
1822 offset
= sizeof(BTNodeDescriptor
);
1823 index
[(nodesize
/ 2) - 1] = SWAP_BE16 (offset
);
1825 /* FILL IN THE HEADER RECORD: */
1826 bthp
= (BTHeaderRec
*)((UInt8
*)buffer
+ offset
);
1827 bthp
->nodeSize
= SWAP_BE16 (nodesize
);
1828 bthp
->totalNodes
= SWAP_BE32 (filesize
/ nodesize
);
1829 bthp
->freeNodes
= SWAP_BE32 (nodecnt
- 1);
1830 bthp
->clumpSize
= SWAP_BE32 (filesize
);
1831 bthp
->btreeType
= kUserBTreeType
; /* non-metadata */
1832 bthp
->attributes
|= SWAP_BE32 (kBTBigKeysMask
);
1833 bthp
->maxKeyLength
= SWAP_BE16 (HFC_KEYLENGTH
);
1834 offset
+= sizeof(BTHeaderRec
);
1835 index
[(nodesize
/ 2) - 2] = SWAP_BE16 (offset
);
1837 /* FILL IN THE USER RECORD: */
1838 hotfileinfo
= (HotFilesInfo
*)((UInt8
*)buffer
+ offset
);
1839 hotfileinfo
->magic
= SWAP_BE32 (HFC_MAGIC
);
1840 hotfileinfo
->version
= SWAP_BE32 (HFC_VERSION
);
1841 hotfileinfo
->duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
1842 hotfileinfo
->timebase
= 0;
1843 hotfileinfo
->timeleft
= 0;
1844 hotfileinfo
->threshold
= SWAP_BE32 (HFC_MINIMUM_TEMPERATURE
);
1845 hotfileinfo
->maxfileblks
= SWAP_BE32 (HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
);
1846 hotfileinfo
->maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
1847 strcpy(hotfileinfo
->tag
, hfc_tag
);
1848 offset
+= kBTreeHeaderUserBytes
;
1849 index
[(nodesize
/ 2) - 3] = SWAP_BE16 (offset
);
1851 /* FILL IN THE MAP RECORD (only one node in use). */
1852 *((u_int8_t
*)buffer
+ offset
) = 0x80;
1853 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
1854 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
1855 index
[(nodesize
/ 2) - 4] = SWAP_BE16 (offset
);
1857 vnode_setnoflush(vp
);
1858 error
= hfs_truncate(vp
, (off_t
)filesize
, IO_NDELAY
, 0, &context
);
1860 printf("HFS: error %d growing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1863 cp
->c_flag
|= C_ZFWANTSYNC
;
1864 cp
->c_zftimeout
= 1;
1867 struct vnop_write_args args
;
1870 auio
= uio_create(1, 0, UIO_SYSSPACE32
, UIO_WRITE
);
1871 uio_addiov(auio
, (uintptr_t)buffer
, nodesize
);
1873 args
.a_desc
= &vnop_write_desc
;
1877 args
.a_context
= &context
;
1882 error
= hfs_vnop_write(&args
);
1884 printf("HFS: error %d writing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1888 kmem_free(kernel_map
, (vm_offset_t
)buffer
, nodesize
);
1904 * Compare two hot file b-tree keys.
1906 * Result: +n search key > trial key
1907 * 0 search key = trial key
1908 * -n search key < trial key
1911 hfc_comparekeys(HotFileKey
*searchKey
, HotFileKey
*trialKey
)
1914 * Compared temperatures first.
1916 if (searchKey
->temperature
== trialKey
->temperature
) {
1918 * Temperatures are equal so compare file ids.
1920 if (searchKey
->fileID
== trialKey
->fileID
) {
1922 * File ids are equal so compare fork types.
1924 if (searchKey
->forkType
== trialKey
->forkType
) {
1926 } else if (searchKey
->forkType
> trialKey
->forkType
) {
1929 } else if (searchKey
->fileID
> trialKey
->fileID
) {
1932 } else if (searchKey
->temperature
> trialKey
->temperature
) {
1941 *========================================================================
1942 * HOT FILE DATA COLLECTING ROUTINES
1943 *========================================================================
1947 * Lookup a hot file entry in the tree.
1950 static hotfile_entry_t
*
1951 hf_lookup(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
1953 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1956 entry
->temperature
!= temperature
&&
1957 entry
->fileid
!= fileid
) {
1959 if (temperature
> entry
->temperature
)
1960 entry
= entry
->right
;
1961 else if (temperature
< entry
->temperature
)
1962 entry
= entry
->left
;
1963 else if (fileid
> entry
->fileid
)
1964 entry
= entry
->right
;
1966 entry
= entry
->left
;
1973 * Insert a hot file entry into the tree.
1976 hf_insert(hotfile_data_t
*hotdata
, hotfile_entry_t
*newentry
)
1978 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1979 u_int32_t fileid
= newentry
->fileid
;
1980 u_int32_t temperature
= newentry
->temperature
;
1982 if (entry
== NULL
) {
1983 hotdata
->rootentry
= newentry
;
1984 hotdata
->coldest
= newentry
;
1985 hotdata
->activefiles
++;
1990 if (temperature
> entry
->temperature
) {
1992 entry
= entry
->right
;
1994 entry
->right
= newentry
;
1997 } else if (temperature
< entry
->temperature
) {
1999 entry
= entry
->left
;
2001 entry
->left
= newentry
;
2004 } else if (fileid
> entry
->fileid
) {
2006 entry
= entry
->right
;
2008 if (entry
->fileid
!= fileid
)
2009 entry
->right
= newentry
;
2014 entry
= entry
->left
;
2016 if (entry
->fileid
!= fileid
)
2017 entry
->left
= newentry
;
2023 hotdata
->activefiles
++;
2027 * Find the coldest entry in the tree.
2029 static hotfile_entry_t
*
2030 hf_coldest(hotfile_data_t
*hotdata
)
2032 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2036 entry
= entry
->left
;
2042 * Find the hottest entry in the tree.
2044 static hotfile_entry_t
*
2045 hf_hottest(hotfile_data_t
*hotdata
)
2047 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2050 while (entry
->right
)
2051 entry
= entry
->right
;
2057 * Delete a hot file entry from the tree.
2060 hf_delete(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
2062 hotfile_entry_t
*entry
, *parent
, *next
;
2065 entry
= hotdata
->rootentry
;
2068 entry
->temperature
!= temperature
&&
2069 entry
->fileid
!= fileid
) {
2072 if (temperature
> entry
->temperature
)
2073 entry
= entry
->right
;
2074 else if (temperature
< entry
->temperature
)
2075 entry
= entry
->left
;
2076 else if (fileid
> entry
->fileid
)
2077 entry
= entry
->right
;
2079 entry
= entry
->left
;
2084 * Reorginize the sub-trees spanning from our entry.
2086 if ((next
= entry
->right
)) {
2087 hotfile_entry_t
*pnextl
, *psub
;
2089 * Tree pruning: take the left branch of the
2090 * current entry and place it at the lowest
2091 * left branch of the current right branch
2095 /* Walk the Right/Left sub tree from current entry */
2096 while ((pnextl
= psub
->left
))
2099 /* Plug the old left tree to the new ->Right leftmost entry */
2100 psub
->left
= entry
->left
;
2102 } else /* only left sub-tree, simple case */ {
2106 * Now, plug the current entry sub tree to
2107 * the good pointer of our parent entry.
2110 hotdata
->rootentry
= next
;
2111 else if (parent
->left
== entry
)
2112 parent
->left
= next
;
2114 parent
->right
= next
;
2116 /* Place entry back on the free-list */
2119 entry
->temperature
= 0;
2121 entry
->right
= hotdata
->freelist
;
2122 hotdata
->freelist
= entry
;
2123 hotdata
->activefiles
--;
2125 if (hotdata
->coldest
== entry
|| hotdata
->coldest
== NULL
) {
2126 hotdata
->coldest
= hf_coldest(hotdata
);
2133 * Get a free hot file entry.
2135 static hotfile_entry_t
*
2136 hf_getnewentry(hotfile_data_t
*hotdata
)
2138 hotfile_entry_t
* entry
;
2141 * When the free list is empty then steal the coldest one
2143 if (hotdata
->freelist
== NULL
) {
2144 entry
= hf_coldest(hotdata
);
2145 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2147 entry
= hotdata
->freelist
;
2148 hotdata
->freelist
= entry
->right
;
2156 * Generate a sorted list of hot files (hottest to coldest).
2158 * As a side effect, every node in the hot file tree will be
2159 * deleted (moved to the free list).
2162 hf_getsortedlist(hotfile_data_t
* hotdata
, hotfilelist_t
*sortedlist
)
2165 hotfile_entry_t
*entry
;
2167 while ((entry
= hf_hottest(hotdata
)) != NULL
) {
2168 sortedlist
->hfl_hotfile
[i
].hf_fileid
= entry
->fileid
;
2169 sortedlist
->hfl_hotfile
[i
].hf_temperature
= entry
->temperature
;
2170 sortedlist
->hfl_hotfile
[i
].hf_blocks
= entry
->blocks
;
2171 sortedlist
->hfl_totalblocks
+= entry
->blocks
;
2174 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2177 sortedlist
->hfl_count
= i
;
2180 printf("HFS: hf_getsortedlist returned %d entries\n", i
);
2187 hf_maxdepth(hotfile_entry_t
* root
, int depth
, int *maxdepth
)
2191 if (depth
> *maxdepth
)
2193 hf_maxdepth(root
->left
, depth
, maxdepth
);
2194 hf_maxdepth(root
->right
, depth
, maxdepth
);
2199 hf_printtree(hotfile_entry_t
* root
)
2202 hf_printtree(root
->left
);
2203 printf("temperature: % 8d, fileid %d\n", root
->temperature
, root
->fileid
);
2204 hf_printtree(root
->right
);