2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/fcntl.h>
32 #include <sys/kernel.h>
33 #include <sys/malloc.h>
35 #include <sys/vnode.h>
36 #include <sys/vnode_internal.h>
37 #include <sys/kauth.h>
40 #include <hfs/hfs_endian.h>
41 #include <hfs/hfs_format.h>
42 #include <hfs/hfs_mount.h>
43 #include <hfs/hfs_hotfiles.h>
45 #include "hfscommon/headers/BTreeScanner.h"
54 * Hot File List (runtime).
56 typedef struct hotfileinfo
{
58 u_int32_t hf_temperature
;
62 typedef struct hotfilelist
{
64 u_int32_t hfl_version
;
65 time_t hfl_duration
; /* duration of sample period */
66 int hfl_count
; /* count of hot files recorded */
67 int hfl_next
; /* next file to move */
68 int hfl_totalblocks
; /* total hot file blocks */
69 int hfl_reclaimblks
; /* blocks to reclaim in HFV */
70 u_int32_t hfl_spare
[2];
71 hotfileinfo_t hfl_hotfile
[1]; /* array of hot files */
76 * Hot File Entry (runtime).
78 typedef struct hotfile_entry
{
79 struct hotfile_entry
*left
;
80 struct hotfile_entry
*right
;
82 u_int32_t temperature
;
87 * Hot File Recording Data (runtime).
89 typedef struct hotfile_data
{
90 struct hfsmount
*hfsmp
;
92 int activefiles
; /* active number of hot files */
95 hotfile_entry_t
*rootentry
;
96 hotfile_entry_t
*freelist
;
97 hotfile_entry_t
*coldest
;
98 hotfile_entry_t entries
[1];
101 static int hfs_recording_start (struct hfsmount
*);
102 static int hfs_recording_stop (struct hfsmount
*);
106 * Hot File Data recording functions (in-memory binary tree).
108 static void hf_insert (hotfile_data_t
*, hotfile_entry_t
*);
109 static void hf_delete (hotfile_data_t
*, u_int32_t
, u_int32_t
);
110 static hotfile_entry_t
* hf_coldest (hotfile_data_t
*);
111 static hotfile_entry_t
* hf_getnewentry (hotfile_data_t
*);
112 static void hf_getsortedlist (hotfile_data_t
*, hotfilelist_t
*);
115 static hotfile_entry_t
* hf_lookup (hotfile_data_t
*, u_int32_t
, u_int32_t
);
116 static void hf_maxdepth(hotfile_entry_t
*, int, int *);
117 static void hf_printtree (hotfile_entry_t
*);
121 * Hot File misc support functions.
123 static int hotfiles_collect (struct hfsmount
*);
124 static int hotfiles_age (struct hfsmount
*);
125 static int hotfiles_adopt (struct hfsmount
*);
126 static int hotfiles_evict (struct hfsmount
*, struct proc
*);
127 static int hotfiles_refine (struct hfsmount
*);
128 static int hotextents(struct hfsmount
*, HFSPlusExtentDescriptor
*);
129 static int hfs_addhotfile_internal(struct vnode
*);
133 * Hot File Cluster B-tree (on disk) functions.
135 static int hfc_btree_create (struct hfsmount
*, int, int);
136 static int hfc_btree_open (struct hfsmount
*, struct vnode
**);
137 static int hfc_btree_close (struct hfsmount
*, struct vnode
*);
138 static int hfc_comparekeys (HotFileKey
*, HotFileKey
*);
141 char hfc_tag
[] = "CLUSTERED HOT FILES B-TREE ";
143 extern int UBCINFOEXISTS(struct vnode
* vp
);
144 extern int hfs_vnop_write(struct vnop_write_args
*ap
);
148 *========================================================================
149 * HOT FILE INTERFACE ROUTINES
150 *========================================================================
154 * Start recording the hotest files on a file system.
156 * Requires that the hfc_mutex be held.
159 hfs_recording_start(struct hfsmount
*hfsmp
)
161 hotfile_data_t
*hotdata
;
168 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
169 (hfsmp
->jnl
== NULL
) ||
170 (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) == 0) {
173 if (HFSTOVCB(hfsmp
)->freeBlocks
< (2 * (u_int32_t
)hfsmp
->hfs_hotfile_maxblks
)) {
176 if (hfsmp
->hfc_stage
!= HFC_IDLE
) {
179 hfsmp
->hfc_stage
= HFC_BUSY
;
182 * Dump previous recording data.
184 if (hfsmp
->hfc_recdata
) {
187 tmp
= hfsmp
->hfc_recdata
;
188 hfsmp
->hfc_recdata
= NULL
;
195 * On first startup check for suspended recording.
197 if (hfsmp
->hfc_timebase
== 0 &&
198 hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
) == 0) {
199 HotFilesInfo hotfileinfo
;
201 if ((BTGetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
,
202 sizeof(hotfileinfo
)) == 0) &&
203 (SWAP_BE32 (hotfileinfo
.magic
) == HFC_MAGIC
) &&
204 (SWAP_BE32 (hotfileinfo
.timeleft
) > 0) &&
205 (SWAP_BE32 (hotfileinfo
.timebase
) > 0)) {
206 hfsmp
->hfc_maxfiles
= SWAP_BE32 (hotfileinfo
.maxfilecnt
);
207 hfsmp
->hfc_timeout
= SWAP_BE32 (hotfileinfo
.timeleft
) + tv
.tv_sec
;
208 hfsmp
->hfc_timebase
= SWAP_BE32 (hotfileinfo
.timebase
);
210 printf("Resume recording hot files on %s (%d secs left)\n",
211 hfsmp
->vcbVN
, SWAP_BE32 (hotfileinfo
.timeleft
));
214 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
215 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
216 hfsmp
->hfc_timeout
= hfsmp
->hfc_timebase
+ HFC_DEFAULT_DURATION
;
218 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
219 hfsmp
->hfc_filevp
= NULL
;
221 struct cat_attr cattr
;
225 * Make sure a btree file exists.
227 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
229 !S_ISREG(cattr
.ca_mode
) &&
230 (error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
))) {
231 hfsmp
->hfc_stage
= HFC_IDLE
;
232 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
236 printf("HFS: begin recording hot files on %s\n", hfsmp
->vcbVN
);
238 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
239 hfsmp
->hfc_timeout
= tv
.tv_sec
+ HFC_DEFAULT_DURATION
;
241 /* Reset time base. */
242 if (hfsmp
->hfc_timebase
== 0) {
243 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
245 time_t cumulativebase
;
247 cumulativebase
= hfsmp
->hfc_timeout
- (HFC_CUMULATIVE_CYCLES
* HFC_DEFAULT_DURATION
);
248 hfsmp
->hfc_timebase
= MAX(hfsmp
->hfc_timebase
, cumulativebase
);
252 if ((hfsmp
->hfc_maxfiles
== 0) ||
253 (hfsmp
->hfc_maxfiles
> HFC_MAXIMUM_FILE_COUNT
)) {
254 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
256 maxentries
= hfsmp
->hfc_maxfiles
;
258 size
= sizeof(hotfile_data_t
) + (maxentries
* sizeof(hotfile_entry_t
));
259 MALLOC(hotdata
, hotfile_data_t
*, size
, M_TEMP
, M_WAITOK
);
260 bzero(hotdata
, size
);
262 for (i
= 1; i
< maxentries
; i
++)
263 hotdata
->entries
[i
-1].right
= &hotdata
->entries
[i
];
265 hotdata
->freelist
= &hotdata
->entries
[0];
267 * Establish minimum temperature and maximum file size.
269 hotdata
->threshold
= HFC_MINIMUM_TEMPERATURE
;
270 hotdata
->maxblocks
= HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
;
271 hotdata
->hfsmp
= hfsmp
;
273 hfsmp
->hfc_recdata
= hotdata
;
274 hfsmp
->hfc_stage
= HFC_RECORDING
;
275 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
280 * Stop recording the hotest files on a file system.
282 * Requires that the hfc_mutex be held.
285 hfs_recording_stop(struct hfsmount
*hfsmp
)
287 hotfile_data_t
*hotdata
;
288 hotfilelist_t
*listp
;
291 enum hfc_stage newstage
= HFC_IDLE
;
294 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
297 hfsmp
->hfc_stage
= HFC_BUSY
;
299 hotfiles_collect(hfsmp
);
302 * Convert hot file data into a simple file id list....
304 * then dump the sample data
307 printf("HFS: end of hot file recording on %s\n", hfsmp
->vcbVN
);
309 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
312 hfsmp
->hfc_recdata
= NULL
;
313 hfsmp
->hfc_stage
= HFC_EVALUATION
;
314 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
317 printf(" curentries: %d\n", hotdata
->activefiles
);
320 * If no hot files recorded then we're done.
322 if (hotdata
->rootentry
== NULL
) {
327 /* Open the B-tree file for writing... */
328 if (hfsmp
->hfc_filevp
)
329 panic("hfs_recording_stop: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
331 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
337 * Age the previous set of clustered hot files.
339 error
= hotfiles_age(hfsmp
);
341 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
342 hfsmp
->hfc_filevp
= NULL
;
347 * Create a sorted list of hotest files.
349 size
= sizeof(hotfilelist_t
);
350 size
+= sizeof(hotfileinfo_t
) * (hotdata
->activefiles
- 1);
351 MALLOC(listp
, hotfilelist_t
*, size
, M_TEMP
, M_WAITOK
);
354 hf_getsortedlist(hotdata
, listp
); /* NOTE: destroys hot file tree! */
356 listp
->hfl_duration
= tv
.tv_sec
- hfsmp
->hfc_timebase
;
357 hfsmp
->hfc_recdata
= listp
;
360 * Account for duplicates.
362 error
= hotfiles_refine(hfsmp
);
364 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
365 hfsmp
->hfc_filevp
= NULL
;
370 * Compute the amount of space to reclaim...
372 if (listp
->hfl_totalblocks
> hfsmp
->hfs_hotfile_freeblks
) {
373 listp
->hfl_reclaimblks
=
374 MIN(listp
->hfl_totalblocks
, hfsmp
->hfs_hotfile_maxblks
) -
375 hfsmp
->hfs_hotfile_freeblks
;
377 printf("hfs_recording_stop: need to reclaim %d blocks\n", listp
->hfl_reclaimblks
);
379 if (listp
->hfl_reclaimblks
)
380 newstage
= HFC_EVICTION
;
382 newstage
= HFC_ADOPTION
;
384 newstage
= HFC_ADOPTION
;
387 if (newstage
== HFC_ADOPTION
&& listp
->hfl_totalblocks
== 0) {
388 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
389 hfsmp
->hfc_filevp
= NULL
;
394 if (newstage
== HFC_EVICTION
)
395 printf("HFS: evicting coldest files\n");
396 else if (newstage
== HFC_ADOPTION
)
397 printf("HFS: adopting hotest files\n");
399 FREE(hotdata
, M_TEMP
);
401 hfsmp
->hfc_stage
= newstage
;
402 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
407 * Suspend recording the hotest files on a file system.
411 hfs_recording_suspend(struct hfsmount
*hfsmp
)
413 HotFilesInfo hotfileinfo
;
414 hotfile_data_t
*hotdata
= NULL
;
418 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
421 lck_mtx_lock(&hfsmp
->hfc_mutex
);
425 * A suspend can occur during eval/evict/adopt stage.
426 * In that case we would need to write out info and
427 * flush our HFBT vnode. Currently we just bail.
430 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
431 if (hotdata
== NULL
|| hfsmp
->hfc_stage
!= HFC_RECORDING
) {
435 hfsmp
->hfc_stage
= HFC_BUSY
;
438 printf("HFS: suspend hot file recording on %s\n", hfsmp
->vcbVN
);
440 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
442 printf("hfs_recording_suspend: err %d opening btree\n", error
);
446 if (hfs_start_transaction(hfsmp
) != 0) {
450 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
456 hotfileinfo
.magic
= SWAP_BE32 (HFC_MAGIC
);
457 hotfileinfo
.version
= SWAP_BE32 (HFC_VERSION
);
458 hotfileinfo
.duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
459 hotfileinfo
.timebase
= SWAP_BE32 (hfsmp
->hfc_timebase
);
460 hotfileinfo
.timeleft
= SWAP_BE32 (hfsmp
->hfc_timeout
- tv
.tv_sec
);
461 hotfileinfo
.threshold
= SWAP_BE32 (hotdata
->threshold
);
462 hotfileinfo
.maxfileblks
= SWAP_BE32 (hotdata
->maxblocks
);
463 hotfileinfo
.maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
464 strcpy(hotfileinfo
.tag
, hfc_tag
);
465 (void) BTSetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
, sizeof(hotfileinfo
));
467 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
468 hfs_end_transaction(hfsmp
);
470 if (hfsmp
->hfc_filevp
) {
471 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
472 hfsmp
->hfc_filevp
= NULL
;
475 FREE(hotdata
, M_TEMP
);
476 hfsmp
->hfc_recdata
= NULL
;
478 hfsmp
->hfc_stage
= HFC_DISABLED
;
479 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
481 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
491 hfs_recording_init(struct hfsmount
*hfsmp
)
494 CatalogRecord
* datap
;
496 HFSPlusCatalogFile
*filep
;
497 BTScanState scanstate
;
498 BTreeIterator
* iterator
;
499 FSBufferDescriptor record
;
501 filefork_t
* filefork
;
503 struct cat_attr cattr
;
507 int inserted
= 0; /* debug variables */
511 * For now, only the boot volume is supported.
513 if ((vfs_flags(HFSTOVFS(hfsmp
)) & MNT_ROOTFS
) == 0) {
514 hfsmp
->hfc_stage
= HFC_DISABLED
;
519 * If the Hot File btree exists then metadata zone is ready.
521 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
522 if (cnid
!= 0 && S_ISREG(cattr
.ca_mode
)) {
523 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
524 hfsmp
->hfc_stage
= HFC_IDLE
;
527 error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
);
530 printf("Error %d creating hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
535 * Open the Hot File B-tree file for writing.
537 if (hfsmp
->hfc_filevp
)
538 panic("hfs_recording_init: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
539 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
542 printf("Error %d opening hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
546 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
547 bzero(iterator
, sizeof(*iterator
));
548 key
= (HotFileKey
*) &iterator
->key
;
549 key
->keyLength
= HFC_KEYLENGTH
;
551 record
.bufferAddress
= &data
;
552 record
.itemSize
= sizeof(u_int32_t
);
553 record
.itemCount
= 1;
555 printf("Evaluating space for \"%s\" metadata zone...\n", HFSTOVCB(hfsmp
)->vcbVN
);
558 * Get ready to scan the Catalog file.
560 error
= BTScanInitialize(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), 0, 0, 0,
561 kCatSearchBufferSize
, &scanstate
);
563 printf("hfs_recording_init: err %d BTScanInit\n", error
);
568 * The writes to Hot File B-tree file are journaled.
570 if (hfs_start_transaction(hfsmp
) != 0) {
574 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
578 filefork
= VTOF(hfsmp
->hfc_filevp
);
581 * Visit all the catalog btree leaf records.
584 error
= BTScanNextRecord(&scanstate
, 0, (void **)&keyp
, (void **)&datap
, &dataSize
);
586 if (error
== btNotFound
)
589 printf("hfs_recording_init: err %d BTScanNext\n", error
);
592 if ((datap
->recordType
!= kHFSPlusFileRecord
) ||
593 (dataSize
!= sizeof(HFSPlusCatalogFile
))) {
596 filep
= (HFSPlusCatalogFile
*)datap
;
598 if (filep
->dataFork
.totalBlocks
== 0) {
602 * Any file that has blocks inside the hot file
603 * space is recorded for later eviction.
605 * For now, resource forks are ignored.
607 if (!hotextents(hfsmp
, &filep
->dataFork
.extents
[0])) {
610 cnid
= filep
->fileID
;
612 /* Skip over journal files. */
613 if (cnid
== hfsmp
->hfs_jnlfileid
|| cnid
== hfsmp
->hfs_jnlinfoblkid
) {
617 * XXX - need to skip quota files as well.
620 /* Insert a hot file entry. */
621 key
->keyLength
= HFC_KEYLENGTH
;
622 key
->temperature
= HFC_MINIMUM_TEMPERATURE
;
626 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
628 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
629 error
= MacToVFSError(error
);
633 /* Insert the corresponding thread record. */
634 key
->keyLength
= HFC_KEYLENGTH
;
635 key
->temperature
= HFC_LOOKUPTAG
;
638 data
= HFC_MINIMUM_TEMPERATURE
;
639 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
641 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
642 error
= MacToVFSError(error
);
647 (void) BTFlushPath(filefork
);
648 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
650 hfs_end_transaction(hfsmp
);
652 printf("%d files identified out of %d\n", inserted
, filecount
);
656 (void) BTScanTerminate(&scanstate
, &data
, &data
, &data
);
658 FREE(iterator
, M_TEMP
);
659 if (hfsmp
->hfc_filevp
) {
660 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
661 hfsmp
->hfc_filevp
= NULL
;
664 hfsmp
->hfc_stage
= HFC_IDLE
;
670 * Use sync to perform ocassional background work.
674 hfs_hotfilesync(struct hfsmount
*hfsmp
, struct proc
*p
)
676 if (hfsmp
->hfc_stage
) {
679 lck_mtx_lock(&hfsmp
->hfc_mutex
);
681 switch (hfsmp
->hfc_stage
) {
683 (void) hfs_recording_start(hfsmp
);
688 if (tv
.tv_sec
> hfsmp
->hfc_timeout
)
689 (void) hfs_recording_stop(hfsmp
);
693 (void) hotfiles_evict(hfsmp
, p
);
697 (void) hotfiles_adopt(hfsmp
);
703 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
709 * Add a hot file to the recording list.
711 * This can happen when a hot file gets reclaimed or at the
712 * end of the recording period for any active hot file.
714 * NOTE: Since both the data and resource fork can be hot,
715 * there can be two entries for the same file id.
717 * Note: the cnode is locked on entry.
721 hfs_addhotfile(struct vnode
*vp
)
727 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
730 lck_mtx_lock(&hfsmp
->hfc_mutex
);
731 error
= hfs_addhotfile_internal(vp
);
732 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
737 hfs_addhotfile_internal(struct vnode
*vp
)
739 hotfile_data_t
*hotdata
;
740 hotfile_entry_t
*entry
;
744 u_int32_t temperature
;
747 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
750 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
753 /* Skip resource forks for now. */
754 if (VNODE_IS_RSRC(vp
)) {
757 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
) {
763 if ((ffp
->ff_bytesread
== 0) ||
764 (ffp
->ff_blocks
== 0) ||
765 (ffp
->ff_size
== 0) ||
766 (ffp
->ff_blocks
> hotdata
->maxblocks
) ||
767 (cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ||
768 (cp
->c_flags
& UF_NODUMP
) ||
769 (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
773 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
774 if (temperature
< hotdata
->threshold
) {
778 * If there is room or this file is hotter than
779 * the coldest one then add it to the list.
782 if ((hotdata
->activefiles
< hfsmp
->hfc_maxfiles
) ||
783 (hotdata
->coldest
== NULL
) ||
784 (temperature
> hotdata
->coldest
->temperature
)) {
786 entry
= hf_getnewentry(hotdata
);
787 entry
->temperature
= temperature
;
788 entry
->fileid
= cp
->c_fileid
;
789 entry
->blocks
= ffp
->ff_blocks
;
790 hf_insert(hotdata
, entry
);
798 * Remove a hot file from the recording list.
800 * This can happen when a hot file becomes
801 * an active vnode (active hot files are
802 * not kept in the recording list until the
803 * end of the recording period).
805 * Note: the cnode is locked on entry.
809 hfs_removehotfile(struct vnode
*vp
)
811 hotfile_data_t
*hotdata
;
815 u_int32_t temperature
;
818 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
821 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
828 if ((ffp
->ff_bytesread
== 0) || (ffp
->ff_blocks
== 0) ||
829 (ffp
->ff_size
== 0) || (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
833 lck_mtx_lock(&hfsmp
->hfc_mutex
);
834 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
836 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
)
839 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
840 if (temperature
< hotdata
->threshold
)
843 if (hotdata
->coldest
&& (temperature
>= hotdata
->coldest
->temperature
)) {
845 hf_delete(hotdata
, VTOC(vp
)->c_fileid
, temperature
);
849 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
855 *========================================================================
856 * HOT FILE MAINTENANCE ROUTINES
857 *========================================================================
861 hotfiles_collect_callback(struct vnode
*vp
, __unused
void *cargs
)
863 if ((vnode_isreg(vp
) || vnode_islnk(vp
)) && !vnode_issystem(vp
))
864 (void) hfs_addhotfile_internal(vp
);
866 return (VNODE_RETURNED
);
870 * Add all active hot files to the recording list.
873 hotfiles_collect(struct hfsmount
*hfsmp
)
875 struct mount
*mp
= HFSTOVFS(hfsmp
);
877 if (vfs_busy(mp
, LK_NOWAIT
))
881 * hotfiles_collect_callback will be called for each vnode
882 * hung off of this mount point
884 * properly referenced and unreferenced around the callback
886 vnode_iterate(mp
, 0, hotfiles_collect_callback
, (void *)NULL
);
895 * Update the data of a btree record
896 * This is called from within BTUpdateRecord.
899 update_callback(const HotFileKey
*key
, u_int32_t
*data
, u_int32_t
*state
)
901 if (key
->temperature
== HFC_LOOKUPTAG
)
907 * Identify files already in hot area.
910 hotfiles_refine(struct hfsmount
*hfsmp
)
912 BTreeIterator
* iterator
;
914 filefork_t
* filefork
;
915 hotfilelist_t
*listp
;
916 FSBufferDescriptor record
;
923 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
926 mp
= HFSTOVFS(hfsmp
);
928 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
929 bzero(iterator
, sizeof(*iterator
));
930 key
= (HotFileKey
*) &iterator
->key
;
932 record
.bufferAddress
= &data
;
933 record
.itemSize
= sizeof(u_int32_t
);
934 record
.itemCount
= 1;
936 if (hfs_start_transaction(hfsmp
) != 0) {
940 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
944 filefork
= VTOF(hfsmp
->hfc_filevp
);
946 for (i
= 0; i
< listp
->hfl_count
; ++i
) {
948 * Check if entry (thread) is already in hot area.
950 key
->keyLength
= HFC_KEYLENGTH
;
951 key
->temperature
= HFC_LOOKUPTAG
;
952 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
954 (void) BTInvalidateHint(iterator
);
955 if (BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
) != 0) {
956 continue; /* not in hot area, so skip */
960 * Update thread entry with latest temperature.
962 error
= BTUpdateRecord(filefork
, iterator
,
963 (IterateCallBackProcPtr
)update_callback
,
964 &listp
->hfl_hotfile
[i
].hf_temperature
);
966 printf("hotfiles_refine: BTUpdateRecord failed %d (file %d)\n", error
, key
->fileID
);
967 error
= MacToVFSError(error
);
971 * Re-key entry with latest temperature.
973 key
->keyLength
= HFC_KEYLENGTH
;
974 key
->temperature
= data
;
975 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
977 /* Pick up record data. */
978 (void) BTInvalidateHint(iterator
);
979 (void) BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
);
980 error
= BTDeleteRecord(filefork
, iterator
);
982 printf("hotfiles_refine: BTDeleteRecord failed %d (file %d)\n", error
, key
->fileID
);
983 error
= MacToVFSError(error
);
986 key
->keyLength
= HFC_KEYLENGTH
;
987 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
988 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
990 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
992 printf("hotfiles_refine: BTInsertRecord failed %d (file %d)\n", error
, key
->fileID
);
993 error
= MacToVFSError(error
);
998 * Invalidate this entry in the list.
1000 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1001 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1005 (void) BTFlushPath(filefork
);
1006 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1008 hfs_end_transaction(hfsmp
);
1010 FREE(iterator
, M_TEMP
);
1015 * Move new hot files into hot area.
1017 * Requires that the hfc_mutex be held.
1020 hotfiles_adopt(struct hfsmount
*hfsmp
)
1022 BTreeIterator
* iterator
;
1024 filefork_t
* filefork
;
1025 hotfilelist_t
*listp
;
1026 FSBufferDescriptor record
;
1029 enum hfc_stage stage
;
1035 int startedtrans
= 0;
1037 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1040 if (hfsmp
->hfc_stage
!= HFC_ADOPTION
) {
1043 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1047 stage
= hfsmp
->hfc_stage
;
1048 hfsmp
->hfc_stage
= HFC_BUSY
;
1051 last
= listp
->hfl_next
+ HFC_FILESPERSYNC
;
1052 if (last
> listp
->hfl_count
)
1053 last
= listp
->hfl_count
;
1055 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1056 bzero(iterator
, sizeof(*iterator
));
1057 key
= (HotFileKey
*) &iterator
->key
;
1058 key
->keyLength
= HFC_KEYLENGTH
;
1060 record
.bufferAddress
= &data
;
1061 record
.itemSize
= sizeof(u_int32_t
);
1062 record
.itemCount
= 1;
1064 filefork
= VTOF(hfsmp
->hfc_filevp
);
1066 for (i
= listp
->hfl_next
; (i
< last
) && (blksmoved
< HFC_BLKSPERSYNC
); ++i
) {
1068 * Skip invalid entries (already in hot area).
1070 if (listp
->hfl_hotfile
[i
].hf_temperature
== 0) {
1075 * Acquire a vnode for this file.
1077 error
= hfs_vget(hfsmp
, listp
->hfl_hotfile
[i
].hf_fileid
, &vp
, 0);
1079 if (error
== ENOENT
) {
1082 continue; /* stale entry, go to next */
1086 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1087 printf("hotfiles_adopt: huh, not a file %d (%d)\n", listp
->hfl_hotfile
[i
].hf_fileid
, VTOC(vp
)->c_cnid
);
1088 hfs_unlock(VTOC(vp
));
1090 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1092 continue; /* stale entry, go to next */
1094 if (hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1095 hfs_unlock(VTOC(vp
));
1097 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1099 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1100 continue; /* stale entry, go to next */
1102 fileblocks
= VTOF(vp
)->ff_blocks
;
1103 if (fileblocks
> hfsmp
->hfs_hotfile_freeblks
) {
1104 hfs_unlock(VTOC(vp
));
1107 listp
->hfl_totalblocks
-= fileblocks
;
1108 continue; /* entry too big, go to next */
1111 if ((blksmoved
> 0) &&
1112 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1113 hfs_unlock(VTOC(vp
));
1115 break; /* adopt this entry the next time around */
1117 /* Start a new transaction. */
1118 if (hfs_start_transaction(hfsmp
) != 0) {
1120 hfs_unlock(VTOC(vp
));
1126 if (VTOC(vp
)->c_desc
.cd_nameptr
)
1127 data
= *(u_int32_t
*)(VTOC(vp
)->c_desc
.cd_nameptr
);
1131 error
= hfs_relocate(vp
, hfsmp
->hfs_hotfile_start
, kauth_cred_get(), current_proc());
1132 hfs_unlock(VTOC(vp
));
1137 /* Keep hot file free space current. */
1138 hfsmp
->hfs_hotfile_freeblks
-= fileblocks
;
1139 listp
->hfl_totalblocks
-= fileblocks
;
1141 /* Insert hot file entry */
1142 key
->keyLength
= HFC_KEYLENGTH
;
1143 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
1144 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1147 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1149 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1150 error
= MacToVFSError(error
);
1155 /* Insert thread record */
1156 key
->keyLength
= HFC_KEYLENGTH
;
1157 key
->temperature
= HFC_LOOKUPTAG
;
1158 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1160 data
= listp
->hfl_hotfile
[i
].hf_temperature
;
1161 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1163 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1164 error
= MacToVFSError(error
);
1168 (void) BTFlushPath(filefork
);
1170 /* Transaction complete. */
1172 hfs_end_transaction(hfsmp
);
1176 blksmoved
+= fileblocks
;
1178 if (listp
->hfl_next
>= listp
->hfl_count
) {
1181 if (hfsmp
->hfs_hotfile_freeblks
<= 0) {
1183 printf("hotfiles_adopt: free space exhausted (%d)\n", hfsmp
->hfs_hotfile_freeblks
);
1190 printf("hotfiles_adopt: [%d] adopted %d blocks (%d left)\n", listp
->hfl_next
, blksmoved
, listp
->hfl_totalblocks
);
1192 /* Finish any outstanding transactions. */
1194 (void) BTFlushPath(filefork
);
1195 hfs_end_transaction(hfsmp
);
1198 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1200 if ((listp
->hfl_next
>= listp
->hfl_count
) || (hfsmp
->hfs_hotfile_freeblks
<= 0)) {
1202 printf("hotfiles_adopt: all done relocating %d files\n", listp
->hfl_count
);
1203 printf("hotfiles_adopt: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1207 FREE(iterator
, M_TEMP
);
1209 if (stage
!= HFC_ADOPTION
&& hfsmp
->hfc_filevp
) {
1210 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
1211 hfsmp
->hfc_filevp
= NULL
;
1213 hfsmp
->hfc_stage
= stage
;
1214 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1219 * Reclaim space by evicting the coldest files.
1221 * Requires that the hfc_mutex be held.
1224 hotfiles_evict(struct hfsmount
*hfsmp
, struct proc
*p
)
1226 BTreeIterator
* iterator
;
1229 filefork_t
* filefork
;
1230 hotfilelist_t
*listp
;
1231 enum hfc_stage stage
;
1232 u_int32_t savedtemp
;
1237 int startedtrans
= 0;
1240 if (hfsmp
->hfc_stage
!= HFC_EVICTION
) {
1244 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1247 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1251 stage
= hfsmp
->hfc_stage
;
1252 hfsmp
->hfc_stage
= HFC_BUSY
;
1254 filesmoved
= blksmoved
= 0;
1255 bt_op
= kBTreeFirstRecord
;
1257 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1258 bzero(iterator
, sizeof(*iterator
));
1259 key
= (HotFileKey
*) &iterator
->key
;
1261 filefork
= VTOF(hfsmp
->hfc_filevp
);
1263 while (listp
->hfl_reclaimblks
> 0 &&
1264 blksmoved
< HFC_BLKSPERSYNC
&&
1265 filesmoved
< HFC_FILESPERSYNC
) {
1268 * Obtain the first record (ie the coldest one).
1270 if (BTIterateRecord(filefork
, bt_op
, iterator
, NULL
, NULL
) != 0) {
1272 printf("hotfiles_evict: no more records\n");
1275 stage
= HFC_ADOPTION
;
1278 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1279 printf("hotfiles_evict: invalid key length %d\n", key
->keyLength
);
1283 if (key
->temperature
== HFC_LOOKUPTAG
) {
1285 printf("hotfiles_evict: ran into thread records\n");
1288 stage
= HFC_ADOPTION
;
1292 * Aquire the vnode for this file.
1294 error
= hfs_vget(hfsmp
, key
->fileID
, &vp
, 0);
1296 /* Start a new transaction. */
1297 if (hfs_start_transaction(hfsmp
) != 0) {
1299 hfs_unlock(VTOC(vp
));
1308 if (error
== ENOENT
) {
1309 goto delete; /* stale entry, go to next */
1311 printf("hotfiles_evict: err %d getting file %d\n",
1312 error
, key
->fileID
);
1316 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1317 printf("hotfiles_evict: huh, not a file %d\n", key
->fileID
);
1318 hfs_unlock(VTOC(vp
));
1320 goto delete; /* invalid entry, go to next */
1322 fileblocks
= VTOF(vp
)->ff_blocks
;
1323 if ((blksmoved
> 0) &&
1324 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1325 hfs_unlock(VTOC(vp
));
1330 * Make sure file is in the hot area.
1332 if (!hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1334 printf("hotfiles_evict: file %d isn't hot!\n", key
->fileID
);
1336 hfs_unlock(VTOC(vp
));
1338 goto delete; /* stale entry, go to next */
1342 * Relocate file out of hot area.
1344 error
= hfs_relocate(vp
, HFSTOVCB(hfsmp
)->nextAllocation
, proc_ucred(p
), p
);
1346 printf("hotfiles_evict: err %d relocating file %d\n", error
, key
->fileID
);
1347 hfs_unlock(VTOC(vp
));
1349 bt_op
= kBTreeNextRecord
;
1350 goto next
; /* go to next */
1354 // We do not believe that this call to hfs_fsync() is
1355 // necessary and it causes a journal transaction
1356 // deadlock so we are removing it.
1358 // (void) hfs_fsync(vp, MNT_WAIT, 0, p);
1360 hfs_unlock(VTOC(vp
));
1363 hfsmp
->hfs_hotfile_freeblks
+= fileblocks
;
1364 listp
->hfl_reclaimblks
-= fileblocks
;
1365 if (listp
->hfl_reclaimblks
< 0)
1366 listp
->hfl_reclaimblks
= 0;
1367 blksmoved
+= fileblocks
;
1370 error
= BTDeleteRecord(filefork
, iterator
);
1372 error
= MacToVFSError(error
);
1375 savedtemp
= key
->temperature
;
1376 key
->temperature
= HFC_LOOKUPTAG
;
1377 error
= BTDeleteRecord(filefork
, iterator
);
1379 error
= MacToVFSError(error
);
1382 key
->temperature
= savedtemp
;
1384 (void) BTFlushPath(filefork
);
1386 /* Transaction complete. */
1388 hfs_end_transaction(hfsmp
);
1395 printf("hotfiles_evict: moved %d files (%d blks, %d to go)\n", filesmoved
, blksmoved
, listp
->hfl_reclaimblks
);
1397 /* Finish any outstanding transactions. */
1399 (void) BTFlushPath(filefork
);
1400 hfs_end_transaction(hfsmp
);
1403 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1406 * Move to next stage when finished.
1408 if (listp
->hfl_reclaimblks
<= 0) {
1409 stage
= HFC_ADOPTION
;
1411 printf("hotfiles_evict: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1414 FREE(iterator
, M_TEMP
);
1415 hfsmp
->hfc_stage
= stage
;
1416 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1421 * Age the existing records in the hot files b-tree.
1424 hotfiles_age(struct hfsmount
*hfsmp
)
1426 BTreeInfoRec btinfo
;
1427 BTreeIterator
* iterator
;
1428 BTreeIterator
* prev_iterator
;
1429 FSBufferDescriptor record
;
1430 FSBufferDescriptor prev_record
;
1432 HotFileKey
* prev_key
;
1433 filefork_t
* filefork
;
1435 u_int32_t prev_data
;
1444 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1445 bzero(iterator
, 2 * sizeof(*iterator
));
1446 key
= (HotFileKey
*) &iterator
->key
;
1448 prev_iterator
= &iterator
[1];
1449 prev_key
= (HotFileKey
*) &prev_iterator
->key
;
1451 record
.bufferAddress
= &data
;
1452 record
.itemSize
= sizeof(data
);
1453 record
.itemCount
= 1;
1454 prev_record
.bufferAddress
= &prev_data
;
1455 prev_record
.itemSize
= sizeof(prev_data
);
1456 prev_record
.itemCount
= 1;
1459 * Capture b-tree changes inside a transaction
1461 if (hfs_start_transaction(hfsmp
) != 0) {
1465 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1469 filefork
= VTOF(hfsmp
->hfc_filevp
);
1471 error
= BTGetInformation(filefork
, 0, &btinfo
);
1473 error
= MacToVFSError(error
);
1476 if (btinfo
.numRecords
< 2) {
1481 /* Only want 1st half of leaf records */
1482 numrecs
= (btinfo
.numRecords
/= 2) - 1;
1484 error
= BTIterateRecord(filefork
, kBTreeFirstRecord
, iterator
, &record
, &reclen
);
1486 printf("hfs_agehotfiles: BTIterateRecord: %d\n", error
);
1487 error
= MacToVFSError(error
);
1490 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1493 for (i
= 0; i
< numrecs
; ++i
) {
1494 error
= BTIterateRecord(filefork
, kBTreeNextRecord
, iterator
, &record
, &reclen
);
1496 if (key
->temperature
< prev_key
->temperature
) {
1497 printf("hfs_agehotfiles: out of order keys!\n");
1501 if (reclen
!= sizeof(data
)) {
1502 printf("hfs_agehotfiles: invalid record length %d\n", reclen
);
1506 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1507 printf("hfs_agehotfiles: invalid key length %d\n", key
->keyLength
);
1511 } else if ((error
== fsBTEndOfIterationErr
|| error
== fsBTRecordNotFoundErr
) &&
1512 (i
== (numrecs
- 1))) {
1515 printf("hfs_agehotfiles: %d of %d BTIterateRecord: %d\n", i
, numrecs
, error
);
1516 error
= MacToVFSError(error
);
1519 if (prev_key
->temperature
== HFC_LOOKUPTAG
) {
1521 printf("hfs_agehotfiles: ran into thread record\n");
1526 error
= BTDeleteRecord(filefork
, prev_iterator
);
1528 printf("hfs_agehotfiles: BTDeleteRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1529 error
= MacToVFSError(error
);
1533 /* Age by halving the temperature (floor = 4) */
1534 newtemp
= MAX(prev_key
->temperature
>> 1, 4);
1535 prev_key
->temperature
= newtemp
;
1537 error
= BTInsertRecord(filefork
, prev_iterator
, &prev_record
, prev_record
.itemSize
);
1539 printf("hfs_agehotfiles: BTInsertRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1540 error
= MacToVFSError(error
);
1545 * Update thread entry with latest temperature.
1547 prev_key
->temperature
= HFC_LOOKUPTAG
;
1548 error
= BTUpdateRecord(filefork
, prev_iterator
,
1549 (IterateCallBackProcPtr
)update_callback
,
1552 printf("hfs_agehotfiles: %d of %d BTUpdateRecord failed %d (file %d, %d)\n",
1553 i
, numrecs
, error
, prev_key
->fileID
, newtemp
);
1554 error
= MacToVFSError(error
);
1558 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1565 printf("hfs_agehotfiles: aged %d records out of %d\n", aged
, btinfo
.numRecords
);
1567 (void) BTFlushPath(filefork
);
1569 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1571 hfs_end_transaction(hfsmp
);
1573 FREE(iterator
, M_TEMP
);
1578 * Return true if any blocks (or all blocks if all is true)
1579 * are contained in the hot file region.
1582 hotextents(struct hfsmount
*hfsmp
, HFSPlusExtentDescriptor
* extents
)
1588 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1589 b1
= extents
[i
].startBlock
;
1592 b2
= b1
+ extents
[i
].blockCount
- 1;
1593 if ((b1
>= hfsmp
->hfs_hotfile_start
&&
1594 b2
<= hfsmp
->hfs_hotfile_end
) ||
1595 (b1
< hfsmp
->hfs_hotfile_end
&&
1596 b2
> hfsmp
->hfs_hotfile_end
)) {
1606 *========================================================================
1607 * HOT FILE B-TREE ROUTINES
1608 *========================================================================
1612 * Open the hot files b-tree for writing.
1614 * On successful exit the vnode has a reference but not an iocount.
1617 hfc_btree_open(struct hfsmount
*hfsmp
, struct vnode
**vpp
)
1621 struct cat_desc cdesc
;
1622 struct cat_attr cattr
;
1623 struct cat_fork cfork
;
1624 static char filename
[] = HFC_FILENAME
;
1632 bzero(&cdesc
, sizeof(cdesc
));
1633 cdesc
.cd_parentcnid
= kRootDirID
;
1634 cdesc
.cd_nameptr
= filename
;
1635 cdesc
.cd_namelen
= strlen(filename
);
1637 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1639 error
= cat_lookup(hfsmp
, &cdesc
, 0, &cdesc
, &cattr
, &cfork
, NULL
);
1641 hfs_systemfile_unlock(hfsmp
, lockflags
);
1644 printf("hfc_btree_open: cat_lookup error %d\n", error
);
1648 cdesc
.cd_flags
|= CD_ISMETA
;
1649 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cdesc
, 0, &cattr
, &cfork
, &vp
);
1651 printf("hfc_btree_open: hfs_getnewvnode error %d\n", error
);
1652 cat_releasedesc(&cdesc
);
1655 if (!vnode_issystem(vp
)) {
1657 printf("hfc_btree_open: file has UBC, try again\n");
1659 hfs_unlock(VTOC(vp
));
1668 /* Open the B-tree file for writing... */
1669 error
= BTOpenPath(VTOF(vp
), (KeyCompareProcPtr
) hfc_comparekeys
);
1671 printf("hfc_btree_open: BTOpenPath error %d\n", error
);
1672 error
= MacToVFSError(error
);
1675 hfs_unlock(VTOC(vp
));
1678 vnode_ref(vp
); /* keep a reference while its open */
1682 if (!vnode_issystem(vp
))
1683 panic("hfc_btree_open: not a system file (vp = 0x%08x)", vp
);
1685 if (UBCINFOEXISTS(vp
))
1686 panic("hfc_btree_open: has UBCInfo (vp = 0x%08x)", vp
);
1692 * Close the hot files b-tree.
1694 * On entry the vnode has a reference.
1697 hfc_btree_close(struct hfsmount
*hfsmp
, struct vnode
*vp
)
1699 struct proc
*p
= current_proc();
1704 journal_flush(hfsmp
->jnl
);
1707 if (vnode_get(vp
) == 0) {
1708 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
1710 (void) hfs_fsync(vp
, MNT_WAIT
, 0, p
);
1711 error
= BTClosePath(VTOF(vp
));
1712 hfs_unlock(VTOC(vp
));
1723 * Create a hot files btree file.
1727 hfc_btree_create(struct hfsmount
*hfsmp
, int nodesize
, int entries
)
1729 struct vnode
*dvp
= NULL
;
1730 struct vnode
*vp
= NULL
;
1731 struct cnode
*cp
= NULL
;
1732 struct vfs_context context
;
1733 struct vnode_attr va
;
1734 struct componentname cname
;
1735 static char filename
[] = HFC_FILENAME
;
1738 context
.vc_proc
= current_proc();
1739 context
.vc_ucred
= kauth_cred_get();
1741 if (hfsmp
->hfc_filevp
)
1742 panic("hfc_btree_create: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
1744 error
= VFS_ROOT(HFSTOVFS(hfsmp
), &dvp
, &context
);
1748 cname
.cn_nameiop
= CREATE
;
1749 cname
.cn_flags
= ISLASTCN
;
1750 cname
.cn_context
= &context
;
1751 cname
.cn_pnbuf
= filename
;
1752 cname
.cn_pnlen
= sizeof(filename
);
1753 cname
.cn_nameptr
= filename
;
1754 cname
.cn_namelen
= strlen(filename
);
1756 cname
.cn_consume
= 0;
1759 VATTR_SET(&va
, va_type
, VREG
);
1760 VATTR_SET(&va
, va_mode
, S_IFREG
| S_IRUSR
| S_IWUSR
);
1761 VATTR_SET(&va
, va_uid
, 0);
1762 VATTR_SET(&va
, va_gid
, 0);
1764 /* call ourselves directly, ignore the higher-level VFS file creation code */
1765 error
= VNOP_CREATE(dvp
, &vp
, &cname
, &va
, &context
);
1767 printf("HFS: error %d creating HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1774 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1779 /* Don't use non-regular files or files with links. */
1780 if (!vnode_isreg(vp
) || cp
->c_nlink
!= 1) {
1785 printf("HFS: created HFBT on %s\n", HFSTOVCB(hfsmp
)->vcbVN
);
1787 if (VTOF(vp
)->ff_size
< (u_int64_t
)nodesize
) {
1791 BTNodeDescriptor
*ndp
;
1793 HotFilesInfo
*hotfileinfo
;
1799 * Mark it invisible (truncate will pull these changes).
1801 ((FndrFileInfo
*)&cp
->c_finderinfo
[0])->fdFlags
|=
1802 SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1804 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buffer
, nodesize
)) {
1808 bzero(buffer
, nodesize
);
1809 index
= (int16_t *)buffer
;
1811 entirespernode
= (nodesize
- sizeof(BTNodeDescriptor
) - 2) /
1812 (sizeof(HotFileKey
) + 6);
1813 nodecnt
= 2 + howmany(entries
* 2, entirespernode
);
1814 nodecnt
= roundup(nodecnt
, 8);
1815 filesize
= nodecnt
* nodesize
;
1817 /* FILL IN THE NODE DESCRIPTOR: */
1818 ndp
= (BTNodeDescriptor
*)buffer
;
1819 ndp
->kind
= kBTHeaderNode
;
1820 ndp
->numRecords
= SWAP_BE16 (3);
1821 offset
= sizeof(BTNodeDescriptor
);
1822 index
[(nodesize
/ 2) - 1] = SWAP_BE16 (offset
);
1824 /* FILL IN THE HEADER RECORD: */
1825 bthp
= (BTHeaderRec
*)((UInt8
*)buffer
+ offset
);
1826 bthp
->nodeSize
= SWAP_BE16 (nodesize
);
1827 bthp
->totalNodes
= SWAP_BE32 (filesize
/ nodesize
);
1828 bthp
->freeNodes
= SWAP_BE32 (nodecnt
- 1);
1829 bthp
->clumpSize
= SWAP_BE32 (filesize
);
1830 bthp
->btreeType
= kUserBTreeType
; /* non-metadata */
1831 bthp
->attributes
|= SWAP_BE32 (kBTBigKeysMask
);
1832 bthp
->maxKeyLength
= SWAP_BE16 (HFC_KEYLENGTH
);
1833 offset
+= sizeof(BTHeaderRec
);
1834 index
[(nodesize
/ 2) - 2] = SWAP_BE16 (offset
);
1836 /* FILL IN THE USER RECORD: */
1837 hotfileinfo
= (HotFilesInfo
*)((UInt8
*)buffer
+ offset
);
1838 hotfileinfo
->magic
= SWAP_BE32 (HFC_MAGIC
);
1839 hotfileinfo
->version
= SWAP_BE32 (HFC_VERSION
);
1840 hotfileinfo
->duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
1841 hotfileinfo
->timebase
= 0;
1842 hotfileinfo
->timeleft
= 0;
1843 hotfileinfo
->threshold
= SWAP_BE32 (HFC_MINIMUM_TEMPERATURE
);
1844 hotfileinfo
->maxfileblks
= SWAP_BE32 (HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
);
1845 hotfileinfo
->maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
1846 strcpy(hotfileinfo
->tag
, hfc_tag
);
1847 offset
+= kBTreeHeaderUserBytes
;
1848 index
[(nodesize
/ 2) - 3] = SWAP_BE16 (offset
);
1850 /* FILL IN THE MAP RECORD (only one node in use). */
1851 *((u_int8_t
*)buffer
+ offset
) = 0x80;
1852 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
1853 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
1854 index
[(nodesize
/ 2) - 4] = SWAP_BE16 (offset
);
1856 vnode_setnoflush(vp
);
1857 error
= hfs_truncate(vp
, (off_t
)filesize
, IO_NDELAY
, 0, &context
);
1859 printf("HFS: error %d growing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1862 cp
->c_flag
|= C_ZFWANTSYNC
;
1863 cp
->c_zftimeout
= 1;
1866 struct vnop_write_args args
;
1869 auio
= uio_create(1, 0, UIO_SYSSPACE32
, UIO_WRITE
);
1870 uio_addiov(auio
, (uintptr_t)buffer
, nodesize
);
1872 args
.a_desc
= &vnop_write_desc
;
1876 args
.a_context
= &context
;
1881 error
= hfs_vnop_write(&args
);
1883 printf("HFS: error %d writing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1887 kmem_free(kernel_map
, (vm_offset_t
)buffer
, nodesize
);
1903 * Compare two hot file b-tree keys.
1905 * Result: +n search key > trial key
1906 * 0 search key = trial key
1907 * -n search key < trial key
1910 hfc_comparekeys(HotFileKey
*searchKey
, HotFileKey
*trialKey
)
1913 * Compared temperatures first.
1915 if (searchKey
->temperature
== trialKey
->temperature
) {
1917 * Temperatures are equal so compare file ids.
1919 if (searchKey
->fileID
== trialKey
->fileID
) {
1921 * File ids are equal so compare fork types.
1923 if (searchKey
->forkType
== trialKey
->forkType
) {
1925 } else if (searchKey
->forkType
> trialKey
->forkType
) {
1928 } else if (searchKey
->fileID
> trialKey
->fileID
) {
1931 } else if (searchKey
->temperature
> trialKey
->temperature
) {
1940 *========================================================================
1941 * HOT FILE DATA COLLECTING ROUTINES
1942 *========================================================================
1946 * Lookup a hot file entry in the tree.
1949 static hotfile_entry_t
*
1950 hf_lookup(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
1952 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1955 entry
->temperature
!= temperature
&&
1956 entry
->fileid
!= fileid
) {
1958 if (temperature
> entry
->temperature
)
1959 entry
= entry
->right
;
1960 else if (temperature
< entry
->temperature
)
1961 entry
= entry
->left
;
1962 else if (fileid
> entry
->fileid
)
1963 entry
= entry
->right
;
1965 entry
= entry
->left
;
1972 * Insert a hot file entry into the tree.
1975 hf_insert(hotfile_data_t
*hotdata
, hotfile_entry_t
*newentry
)
1977 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1978 u_int32_t fileid
= newentry
->fileid
;
1979 u_int32_t temperature
= newentry
->temperature
;
1981 if (entry
== NULL
) {
1982 hotdata
->rootentry
= newentry
;
1983 hotdata
->coldest
= newentry
;
1984 hotdata
->activefiles
++;
1989 if (temperature
> entry
->temperature
) {
1991 entry
= entry
->right
;
1993 entry
->right
= newentry
;
1996 } else if (temperature
< entry
->temperature
) {
1998 entry
= entry
->left
;
2000 entry
->left
= newentry
;
2003 } else if (fileid
> entry
->fileid
) {
2005 entry
= entry
->right
;
2007 if (entry
->fileid
!= fileid
)
2008 entry
->right
= newentry
;
2013 entry
= entry
->left
;
2015 if (entry
->fileid
!= fileid
)
2016 entry
->left
= newentry
;
2022 hotdata
->activefiles
++;
2026 * Find the coldest entry in the tree.
2028 static hotfile_entry_t
*
2029 hf_coldest(hotfile_data_t
*hotdata
)
2031 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2035 entry
= entry
->left
;
2041 * Find the hottest entry in the tree.
2043 static hotfile_entry_t
*
2044 hf_hottest(hotfile_data_t
*hotdata
)
2046 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2049 while (entry
->right
)
2050 entry
= entry
->right
;
2056 * Delete a hot file entry from the tree.
2059 hf_delete(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
2061 hotfile_entry_t
*entry
, *parent
, *next
;
2064 entry
= hotdata
->rootentry
;
2067 entry
->temperature
!= temperature
&&
2068 entry
->fileid
!= fileid
) {
2071 if (temperature
> entry
->temperature
)
2072 entry
= entry
->right
;
2073 else if (temperature
< entry
->temperature
)
2074 entry
= entry
->left
;
2075 else if (fileid
> entry
->fileid
)
2076 entry
= entry
->right
;
2078 entry
= entry
->left
;
2083 * Reorginize the sub-trees spanning from our entry.
2085 if ((next
= entry
->right
)) {
2086 hotfile_entry_t
*pnextl
, *psub
;
2088 * Tree pruning: take the left branch of the
2089 * current entry and place it at the lowest
2090 * left branch of the current right branch
2094 /* Walk the Right/Left sub tree from current entry */
2095 while ((pnextl
= psub
->left
))
2098 /* Plug the old left tree to the new ->Right leftmost entry */
2099 psub
->left
= entry
->left
;
2101 } else /* only left sub-tree, simple case */ {
2105 * Now, plug the current entry sub tree to
2106 * the good pointer of our parent entry.
2109 hotdata
->rootentry
= next
;
2110 else if (parent
->left
== entry
)
2111 parent
->left
= next
;
2113 parent
->right
= next
;
2115 /* Place entry back on the free-list */
2118 entry
->temperature
= 0;
2120 entry
->right
= hotdata
->freelist
;
2121 hotdata
->freelist
= entry
;
2122 hotdata
->activefiles
--;
2124 if (hotdata
->coldest
== entry
|| hotdata
->coldest
== NULL
) {
2125 hotdata
->coldest
= hf_coldest(hotdata
);
2132 * Get a free hot file entry.
2134 static hotfile_entry_t
*
2135 hf_getnewentry(hotfile_data_t
*hotdata
)
2137 hotfile_entry_t
* entry
;
2140 * When the free list is empty then steal the coldest one
2142 if (hotdata
->freelist
== NULL
) {
2143 entry
= hf_coldest(hotdata
);
2144 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2146 entry
= hotdata
->freelist
;
2147 hotdata
->freelist
= entry
->right
;
2155 * Generate a sorted list of hot files (hottest to coldest).
2157 * As a side effect, every node in the hot file tree will be
2158 * deleted (moved to the free list).
2161 hf_getsortedlist(hotfile_data_t
* hotdata
, hotfilelist_t
*sortedlist
)
2164 hotfile_entry_t
*entry
;
2166 while ((entry
= hf_hottest(hotdata
)) != NULL
) {
2167 sortedlist
->hfl_hotfile
[i
].hf_fileid
= entry
->fileid
;
2168 sortedlist
->hfl_hotfile
[i
].hf_temperature
= entry
->temperature
;
2169 sortedlist
->hfl_hotfile
[i
].hf_blocks
= entry
->blocks
;
2170 sortedlist
->hfl_totalblocks
+= entry
->blocks
;
2173 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2176 sortedlist
->hfl_count
= i
;
2179 printf("HFS: hf_getsortedlist returned %d entries\n", i
);
2186 hf_maxdepth(hotfile_entry_t
* root
, int depth
, int *maxdepth
)
2190 if (depth
> *maxdepth
)
2192 hf_maxdepth(root
->left
, depth
, maxdepth
);
2193 hf_maxdepth(root
->right
, depth
, maxdepth
);
2198 hf_printtree(hotfile_entry_t
* root
)
2201 hf_printtree(root
->left
);
2202 printf("temperature: % 8d, fileid %d\n", root
->temperature
, root
->fileid
);
2203 hf_printtree(root
->right
);