2 * Copyright (c) 2003-2005 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/param.h>
25 #include <sys/systm.h>
26 #include <sys/fcntl.h>
27 #include <sys/kernel.h>
28 #include <sys/malloc.h>
30 #include <sys/vnode.h>
31 #include <sys/vnode_internal.h>
32 #include <sys/kauth.h>
35 #include <hfs/hfs_endian.h>
36 #include <hfs/hfs_format.h>
37 #include <hfs/hfs_mount.h>
38 #include <hfs/hfs_hotfiles.h>
40 #include "hfscommon/headers/BTreeScanner.h"
49 * Hot File List (runtime).
51 typedef struct hotfileinfo
{
53 u_int32_t hf_temperature
;
57 typedef struct hotfilelist
{
59 u_int32_t hfl_version
;
60 time_t hfl_duration
; /* duration of sample period */
61 int hfl_count
; /* count of hot files recorded */
62 int hfl_next
; /* next file to move */
63 int hfl_totalblocks
; /* total hot file blocks */
64 int hfl_reclaimblks
; /* blocks to reclaim in HFV */
65 u_int32_t hfl_spare
[2];
66 hotfileinfo_t hfl_hotfile
[1]; /* array of hot files */
71 * Hot File Entry (runtime).
73 typedef struct hotfile_entry
{
74 struct hotfile_entry
*left
;
75 struct hotfile_entry
*right
;
77 u_int32_t temperature
;
82 * Hot File Recording Data (runtime).
84 typedef struct hotfile_data
{
85 struct hfsmount
*hfsmp
;
87 int activefiles
; /* active number of hot files */
90 hotfile_entry_t
*rootentry
;
91 hotfile_entry_t
*freelist
;
92 hotfile_entry_t
*coldest
;
93 hotfile_entry_t entries
[1];
96 static int hfs_recording_start (struct hfsmount
*);
97 static int hfs_recording_stop (struct hfsmount
*);
101 * Hot File Data recording functions (in-memory binary tree).
103 static void hf_insert (hotfile_data_t
*, hotfile_entry_t
*);
104 static void hf_delete (hotfile_data_t
*, u_int32_t
, u_int32_t
);
105 static hotfile_entry_t
* hf_coldest (hotfile_data_t
*);
106 static hotfile_entry_t
* hf_getnewentry (hotfile_data_t
*);
107 static void hf_getsortedlist (hotfile_data_t
*, hotfilelist_t
*);
110 static hotfile_entry_t
* hf_lookup (hotfile_data_t
*, u_int32_t
, u_int32_t
);
111 static void hf_maxdepth(hotfile_entry_t
*, int, int *);
112 static void hf_printtree (hotfile_entry_t
*);
116 * Hot File misc support functions.
118 static int hotfiles_collect (struct hfsmount
*);
119 static int hotfiles_age (struct hfsmount
*);
120 static int hotfiles_adopt (struct hfsmount
*);
121 static int hotfiles_evict (struct hfsmount
*, struct proc
*);
122 static int hotfiles_refine (struct hfsmount
*);
123 static int hotextents(struct hfsmount
*, HFSPlusExtentDescriptor
*);
124 static int hfs_addhotfile_internal(struct vnode
*);
128 * Hot File Cluster B-tree (on disk) functions.
130 static int hfc_btree_create (struct hfsmount
*, int, int);
131 static int hfc_btree_open (struct hfsmount
*, struct vnode
**);
132 static int hfc_btree_close (struct hfsmount
*, struct vnode
*);
133 static int hfc_comparekeys (HotFileKey
*, HotFileKey
*);
136 char hfc_tag
[] = "CLUSTERED HOT FILES B-TREE ";
138 extern int UBCINFOEXISTS(struct vnode
* vp
);
139 extern int hfs_vnop_write(struct vnop_write_args
*ap
);
143 *========================================================================
144 * HOT FILE INTERFACE ROUTINES
145 *========================================================================
149 * Start recording the hotest files on a file system.
151 * Requires that the hfc_mutex be held.
154 hfs_recording_start(struct hfsmount
*hfsmp
)
156 hotfile_data_t
*hotdata
;
163 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
164 (hfsmp
->jnl
== NULL
) ||
165 (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) == 0) {
168 if (HFSTOVCB(hfsmp
)->freeBlocks
< (2 * (u_int32_t
)hfsmp
->hfs_hotfile_maxblks
)) {
171 if (hfsmp
->hfc_stage
!= HFC_IDLE
) {
174 hfsmp
->hfc_stage
= HFC_BUSY
;
177 * Dump previous recording data.
179 if (hfsmp
->hfc_recdata
) {
182 tmp
= hfsmp
->hfc_recdata
;
183 hfsmp
->hfc_recdata
= NULL
;
190 * On first startup check for suspended recording.
192 if (hfsmp
->hfc_timebase
== 0 &&
193 hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
) == 0) {
194 HotFilesInfo hotfileinfo
;
196 if ((BTGetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
,
197 sizeof(hotfileinfo
)) == 0) &&
198 (SWAP_BE32 (hotfileinfo
.magic
) == HFC_MAGIC
) &&
199 (SWAP_BE32 (hotfileinfo
.timeleft
) > 0) &&
200 (SWAP_BE32 (hotfileinfo
.timebase
) > 0)) {
201 hfsmp
->hfc_maxfiles
= SWAP_BE32 (hotfileinfo
.maxfilecnt
);
202 hfsmp
->hfc_timeout
= SWAP_BE32 (hotfileinfo
.timeleft
) + tv
.tv_sec
;
203 hfsmp
->hfc_timebase
= SWAP_BE32 (hotfileinfo
.timebase
);
205 printf("Resume recording hot files on %s (%d secs left)\n",
206 hfsmp
->vcbVN
, SWAP_BE32 (hotfileinfo
.timeleft
));
209 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
210 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
211 hfsmp
->hfc_timeout
= hfsmp
->hfc_timebase
+ HFC_DEFAULT_DURATION
;
213 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
214 hfsmp
->hfc_filevp
= NULL
;
216 struct cat_attr cattr
;
220 * Make sure a btree file exists.
222 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
224 !S_ISREG(cattr
.ca_mode
) &&
225 (error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
))) {
226 hfsmp
->hfc_stage
= HFC_IDLE
;
227 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
231 printf("HFS: begin recording hot files on %s\n", hfsmp
->vcbVN
);
233 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
234 hfsmp
->hfc_timeout
= tv
.tv_sec
+ HFC_DEFAULT_DURATION
;
236 /* Reset time base. */
237 if (hfsmp
->hfc_timebase
== 0) {
238 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
240 time_t cumulativebase
;
242 cumulativebase
= hfsmp
->hfc_timeout
- (HFC_CUMULATIVE_CYCLES
* HFC_DEFAULT_DURATION
);
243 hfsmp
->hfc_timebase
= MAX(hfsmp
->hfc_timebase
, cumulativebase
);
247 if ((hfsmp
->hfc_maxfiles
== 0) ||
248 (hfsmp
->hfc_maxfiles
> HFC_MAXIMUM_FILE_COUNT
)) {
249 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
251 maxentries
= hfsmp
->hfc_maxfiles
;
253 size
= sizeof(hotfile_data_t
) + (maxentries
* sizeof(hotfile_entry_t
));
254 MALLOC(hotdata
, hotfile_data_t
*, size
, M_TEMP
, M_WAITOK
);
255 bzero(hotdata
, size
);
257 for (i
= 1; i
< maxentries
; i
++)
258 hotdata
->entries
[i
-1].right
= &hotdata
->entries
[i
];
260 hotdata
->freelist
= &hotdata
->entries
[0];
262 * Establish minimum temperature and maximum file size.
264 hotdata
->threshold
= HFC_MINIMUM_TEMPERATURE
;
265 hotdata
->maxblocks
= HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
;
266 hotdata
->hfsmp
= hfsmp
;
268 hfsmp
->hfc_recdata
= hotdata
;
269 hfsmp
->hfc_stage
= HFC_RECORDING
;
270 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
275 * Stop recording the hotest files on a file system.
277 * Requires that the hfc_mutex be held.
280 hfs_recording_stop(struct hfsmount
*hfsmp
)
282 hotfile_data_t
*hotdata
;
283 hotfilelist_t
*listp
;
286 enum hfc_stage newstage
= HFC_IDLE
;
289 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
292 hotfiles_collect(hfsmp
);
294 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
297 hfsmp
->hfc_stage
= HFC_BUSY
;
300 * Convert hot file data into a simple file id list....
302 * then dump the sample data
305 printf("HFS: end of hot file recording on %s\n", hfsmp
->vcbVN
);
307 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
310 hfsmp
->hfc_recdata
= NULL
;
311 hfsmp
->hfc_stage
= HFC_EVALUATION
;
312 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
315 printf(" curentries: %d\n", hotdata
->activefiles
);
318 * If no hot files recorded then we're done.
320 if (hotdata
->rootentry
== NULL
) {
325 /* Open the B-tree file for writing... */
326 if (hfsmp
->hfc_filevp
)
327 panic("hfs_recording_stop: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
329 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
335 * Age the previous set of clustered hot files.
337 error
= hotfiles_age(hfsmp
);
339 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
340 hfsmp
->hfc_filevp
= NULL
;
345 * Create a sorted list of hotest files.
347 size
= sizeof(hotfilelist_t
);
348 size
+= sizeof(hotfileinfo_t
) * (hotdata
->activefiles
- 1);
349 MALLOC(listp
, hotfilelist_t
*, size
, M_TEMP
, M_WAITOK
);
352 hf_getsortedlist(hotdata
, listp
); /* NOTE: destroys hot file tree! */
354 listp
->hfl_duration
= tv
.tv_sec
- hfsmp
->hfc_timebase
;
355 hfsmp
->hfc_recdata
= listp
;
358 * Account for duplicates.
360 error
= hotfiles_refine(hfsmp
);
362 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
363 hfsmp
->hfc_filevp
= NULL
;
368 * Compute the amount of space to reclaim...
370 if (listp
->hfl_totalblocks
> hfsmp
->hfs_hotfile_freeblks
) {
371 listp
->hfl_reclaimblks
=
372 MIN(listp
->hfl_totalblocks
, hfsmp
->hfs_hotfile_maxblks
) -
373 hfsmp
->hfs_hotfile_freeblks
;
375 printf("hfs_recording_stop: need to reclaim %d blocks\n", listp
->hfl_reclaimblks
);
377 if (listp
->hfl_reclaimblks
)
378 newstage
= HFC_EVICTION
;
380 newstage
= HFC_ADOPTION
;
382 newstage
= HFC_ADOPTION
;
385 if (newstage
== HFC_ADOPTION
&& listp
->hfl_totalblocks
== 0) {
386 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
387 hfsmp
->hfc_filevp
= NULL
;
392 if (newstage
== HFC_EVICTION
)
393 printf("HFS: evicting coldest files\n");
394 else if (newstage
== HFC_ADOPTION
)
395 printf("HFS: adopting hotest files\n");
397 FREE(hotdata
, M_TEMP
);
399 hfsmp
->hfc_stage
= newstage
;
400 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
405 * Suspend recording the hotest files on a file system.
409 hfs_recording_suspend(struct hfsmount
*hfsmp
)
411 HotFilesInfo hotfileinfo
;
412 hotfile_data_t
*hotdata
= NULL
;
416 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
419 lck_mtx_lock(&hfsmp
->hfc_mutex
);
423 * A suspend can occur during eval/evict/adopt stage.
424 * In that case we would need to write out info and
425 * flush our HFBT vnode. Currently we just bail.
428 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
429 if (hotdata
== NULL
|| hfsmp
->hfc_stage
!= HFC_RECORDING
) {
433 hfsmp
->hfc_stage
= HFC_BUSY
;
436 printf("HFS: suspend hot file recording on %s\n", hfsmp
->vcbVN
);
438 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
440 printf("hfs_recording_suspend: err %d opening btree\n", error
);
444 if (hfs_start_transaction(hfsmp
) != 0) {
448 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
454 hotfileinfo
.magic
= SWAP_BE32 (HFC_MAGIC
);
455 hotfileinfo
.version
= SWAP_BE32 (HFC_VERSION
);
456 hotfileinfo
.duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
457 hotfileinfo
.timebase
= SWAP_BE32 (hfsmp
->hfc_timebase
);
458 hotfileinfo
.timeleft
= SWAP_BE32 (hfsmp
->hfc_timeout
- tv
.tv_sec
);
459 hotfileinfo
.threshold
= SWAP_BE32 (hotdata
->threshold
);
460 hotfileinfo
.maxfileblks
= SWAP_BE32 (hotdata
->maxblocks
);
461 hotfileinfo
.maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
462 strcpy(hotfileinfo
.tag
, hfc_tag
);
463 (void) BTSetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
, sizeof(hotfileinfo
));
465 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
466 hfs_end_transaction(hfsmp
);
468 if (hfsmp
->hfc_filevp
) {
469 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
470 hfsmp
->hfc_filevp
= NULL
;
473 FREE(hotdata
, M_TEMP
);
474 hfsmp
->hfc_recdata
= NULL
;
476 hfsmp
->hfc_stage
= HFC_DISABLED
;
477 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
479 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
489 hfs_recording_init(struct hfsmount
*hfsmp
)
492 CatalogRecord
* datap
;
494 HFSPlusCatalogFile
*filep
;
495 BTScanState scanstate
;
496 BTreeIterator
* iterator
;
497 FSBufferDescriptor record
;
499 filefork_t
* filefork
;
501 struct cat_attr cattr
;
505 int inserted
= 0; /* debug variables */
509 * For now, only the boot volume is supported.
511 if ((vfs_flags(HFSTOVFS(hfsmp
)) & MNT_ROOTFS
) == 0) {
512 hfsmp
->hfc_stage
= HFC_DISABLED
;
517 * If the Hot File btree exists then metadata zone is ready.
519 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
520 if (cnid
!= 0 && S_ISREG(cattr
.ca_mode
)) {
521 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
522 hfsmp
->hfc_stage
= HFC_IDLE
;
525 error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
);
528 printf("Error %d creating hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
533 * Open the Hot File B-tree file for writing.
535 if (hfsmp
->hfc_filevp
)
536 panic("hfs_recording_init: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
537 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
540 printf("Error %d opening hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
544 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
545 bzero(iterator
, sizeof(*iterator
));
546 key
= (HotFileKey
*) &iterator
->key
;
547 key
->keyLength
= HFC_KEYLENGTH
;
549 record
.bufferAddress
= &data
;
550 record
.itemSize
= sizeof(u_int32_t
);
551 record
.itemCount
= 1;
553 printf("Evaluating space for \"%s\" metadata zone...\n", HFSTOVCB(hfsmp
)->vcbVN
);
556 * Get ready to scan the Catalog file.
558 error
= BTScanInitialize(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), 0, 0, 0,
559 kCatSearchBufferSize
, &scanstate
);
561 printf("hfs_recording_init: err %d BTScanInit\n", error
);
566 * The writes to Hot File B-tree file are journaled.
568 if (hfs_start_transaction(hfsmp
) != 0) {
572 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
576 filefork
= VTOF(hfsmp
->hfc_filevp
);
579 * Visit all the catalog btree leaf records.
582 error
= BTScanNextRecord(&scanstate
, 0, (void **)&keyp
, (void **)&datap
, &dataSize
);
584 if (error
== btNotFound
)
587 printf("hfs_recording_init: err %d BTScanNext\n", error
);
590 if ((datap
->recordType
!= kHFSPlusFileRecord
) ||
591 (dataSize
!= sizeof(HFSPlusCatalogFile
))) {
594 filep
= (HFSPlusCatalogFile
*)datap
;
596 if (filep
->dataFork
.totalBlocks
== 0) {
600 * Any file that has blocks inside the hot file
601 * space is recorded for later eviction.
603 * For now, resource forks are ignored.
605 if (!hotextents(hfsmp
, &filep
->dataFork
.extents
[0])) {
608 cnid
= filep
->fileID
;
610 /* Skip over journal files. */
611 if (cnid
== hfsmp
->hfs_jnlfileid
|| cnid
== hfsmp
->hfs_jnlinfoblkid
) {
615 * XXX - need to skip quota files as well.
618 /* Insert a hot file entry. */
619 key
->keyLength
= HFC_KEYLENGTH
;
620 key
->temperature
= HFC_MINIMUM_TEMPERATURE
;
624 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
626 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
627 error
= MacToVFSError(error
);
631 /* Insert the corresponding thread record. */
632 key
->keyLength
= HFC_KEYLENGTH
;
633 key
->temperature
= HFC_LOOKUPTAG
;
636 data
= HFC_MINIMUM_TEMPERATURE
;
637 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
639 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
640 error
= MacToVFSError(error
);
645 (void) BTFlushPath(filefork
);
646 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
648 hfs_end_transaction(hfsmp
);
650 printf("%d files identified out of %d\n", inserted
, filecount
);
654 (void) BTScanTerminate(&scanstate
, &data
, &data
, &data
);
656 FREE(iterator
, M_TEMP
);
657 if (hfsmp
->hfc_filevp
) {
658 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
659 hfsmp
->hfc_filevp
= NULL
;
662 hfsmp
->hfc_stage
= HFC_IDLE
;
668 * Use sync to perform ocassional background work.
672 hfs_hotfilesync(struct hfsmount
*hfsmp
, struct proc
*p
)
674 if (hfsmp
->hfc_stage
) {
677 lck_mtx_lock(&hfsmp
->hfc_mutex
);
679 switch (hfsmp
->hfc_stage
) {
681 (void) hfs_recording_start(hfsmp
);
686 if (tv
.tv_sec
> hfsmp
->hfc_timeout
)
687 (void) hfs_recording_stop(hfsmp
);
691 (void) hotfiles_evict(hfsmp
, p
);
695 (void) hotfiles_adopt(hfsmp
);
701 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
707 * Add a hot file to the recording list.
709 * This can happen when a hot file gets reclaimed or at the
710 * end of the recording period for any active hot file.
712 * NOTE: Since both the data and resource fork can be hot,
713 * there can be two entries for the same file id.
715 * Note: the cnode is locked on entry.
719 hfs_addhotfile(struct vnode
*vp
)
725 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
728 lck_mtx_lock(&hfsmp
->hfc_mutex
);
729 error
= hfs_addhotfile_internal(vp
);
730 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
735 hfs_addhotfile_internal(struct vnode
*vp
)
737 hotfile_data_t
*hotdata
;
738 hotfile_entry_t
*entry
;
742 u_int32_t temperature
;
745 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
748 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
751 /* Skip resource forks for now. */
752 if (VNODE_IS_RSRC(vp
)) {
755 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
) {
761 if ((ffp
->ff_bytesread
== 0) ||
762 (ffp
->ff_blocks
== 0) ||
763 (ffp
->ff_blocks
> hotdata
->maxblocks
) ||
764 (cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ||
765 (cp
->c_flags
& UF_NODUMP
) ||
766 (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
770 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
771 if (temperature
< hotdata
->threshold
) {
775 * If there is room or this file is hotter than
776 * the coldest one then add it to the list.
779 if ((hotdata
->activefiles
< hfsmp
->hfc_maxfiles
) ||
780 (hotdata
->coldest
== NULL
) ||
781 (temperature
> hotdata
->coldest
->temperature
)) {
783 entry
= hf_getnewentry(hotdata
);
784 entry
->temperature
= temperature
;
785 entry
->fileid
= cp
->c_fileid
;
786 entry
->blocks
= ffp
->ff_blocks
;
787 hf_insert(hotdata
, entry
);
795 * Remove a hot file from the recording list.
797 * This can happen when a hot file becomes
798 * an active vnode (active hot files are
799 * not kept in the recording list until the
800 * end of the recording period).
802 * Note: the cnode is locked on entry.
806 hfs_removehotfile(struct vnode
*vp
)
808 hotfile_data_t
*hotdata
;
812 u_int32_t temperature
;
815 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
818 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
825 if ((ffp
->ff_bytesread
== 0) || (ffp
->ff_blocks
== 0) ||
826 (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
830 lck_mtx_lock(&hfsmp
->hfc_mutex
);
831 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
833 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
)
836 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
837 if (temperature
< hotdata
->threshold
)
840 if (hotdata
->coldest
&& (temperature
>= hotdata
->coldest
->temperature
)) {
842 hf_delete(hotdata
, VTOC(vp
)->c_fileid
, temperature
);
846 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
852 *========================================================================
853 * HOT FILE MAINTENANCE ROUTINES
854 *========================================================================
858 hotfiles_collect_callback(struct vnode
*vp
, __unused
void *cargs
)
860 if ((vnode_isreg(vp
) || vnode_islnk(vp
)) && !vnode_issystem(vp
))
861 (void) hfs_addhotfile_internal(vp
);
863 return (VNODE_RETURNED
);
867 * Add all active hot files to the recording list.
870 hotfiles_collect(struct hfsmount
*hfsmp
)
872 struct mount
*mp
= HFSTOVFS(hfsmp
);
874 if (vfs_busy(mp
, LK_NOWAIT
))
878 * hotfiles_collect_callback will be called for each vnode
879 * hung off of this mount point
881 * properly referenced and unreferenced around the callback
883 vnode_iterate(mp
, 0, hotfiles_collect_callback
, (void *)NULL
);
892 * Update the data of a btree record
893 * This is called from within BTUpdateRecord.
896 update_callback(const HotFileKey
*key
, u_int32_t
*data
, u_int32_t
*state
)
898 if (key
->temperature
== HFC_LOOKUPTAG
)
904 * Identify files already in hot area.
907 hotfiles_refine(struct hfsmount
*hfsmp
)
909 BTreeIterator
* iterator
;
911 filefork_t
* filefork
;
912 hotfilelist_t
*listp
;
913 FSBufferDescriptor record
;
920 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
923 mp
= HFSTOVFS(hfsmp
);
925 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
926 bzero(iterator
, sizeof(*iterator
));
927 key
= (HotFileKey
*) &iterator
->key
;
929 record
.bufferAddress
= &data
;
930 record
.itemSize
= sizeof(u_int32_t
);
931 record
.itemCount
= 1;
933 if (hfs_start_transaction(hfsmp
) != 0) {
937 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
941 filefork
= VTOF(hfsmp
->hfc_filevp
);
943 for (i
= 0; i
< listp
->hfl_count
; ++i
) {
945 * Check if entry (thread) is already in hot area.
947 key
->keyLength
= HFC_KEYLENGTH
;
948 key
->temperature
= HFC_LOOKUPTAG
;
949 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
951 (void) BTInvalidateHint(iterator
);
952 if (BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
) != 0) {
953 continue; /* not in hot area, so skip */
957 * Update thread entry with latest temperature.
959 error
= BTUpdateRecord(filefork
, iterator
,
960 (IterateCallBackProcPtr
)update_callback
,
961 &listp
->hfl_hotfile
[i
].hf_temperature
);
963 printf("hotfiles_refine: BTUpdateRecord failed %d (file %d)\n", error
, key
->fileID
);
964 error
= MacToVFSError(error
);
968 * Re-key entry with latest temperature.
970 key
->keyLength
= HFC_KEYLENGTH
;
971 key
->temperature
= data
;
972 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
974 /* Pick up record data. */
975 (void) BTInvalidateHint(iterator
);
976 (void) BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
);
977 error
= BTDeleteRecord(filefork
, iterator
);
979 printf("hotfiles_refine: BTDeleteRecord failed %d (file %d)\n", error
, key
->fileID
);
980 error
= MacToVFSError(error
);
983 key
->keyLength
= HFC_KEYLENGTH
;
984 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
985 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
987 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
989 printf("hotfiles_refine: BTInsertRecord failed %d (file %d)\n", error
, key
->fileID
);
990 error
= MacToVFSError(error
);
995 * Invalidate this entry in the list.
997 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
998 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1002 (void) BTFlushPath(filefork
);
1003 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1005 hfs_end_transaction(hfsmp
);
1007 FREE(iterator
, M_TEMP
);
1012 * Move new hot files into hot area.
1014 * Requires that the hfc_mutex be held.
1017 hotfiles_adopt(struct hfsmount
*hfsmp
)
1019 BTreeIterator
* iterator
;
1021 filefork_t
* filefork
;
1022 hotfilelist_t
*listp
;
1023 FSBufferDescriptor record
;
1026 enum hfc_stage stage
;
1032 int startedtrans
= 0;
1034 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1037 if (hfsmp
->hfc_stage
!= HFC_ADOPTION
) {
1040 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1044 stage
= hfsmp
->hfc_stage
;
1045 hfsmp
->hfc_stage
= HFC_BUSY
;
1048 last
= listp
->hfl_next
+ HFC_FILESPERSYNC
;
1049 if (last
> listp
->hfl_count
)
1050 last
= listp
->hfl_count
;
1052 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1053 bzero(iterator
, sizeof(*iterator
));
1054 key
= (HotFileKey
*) &iterator
->key
;
1055 key
->keyLength
= HFC_KEYLENGTH
;
1057 record
.bufferAddress
= &data
;
1058 record
.itemSize
= sizeof(u_int32_t
);
1059 record
.itemCount
= 1;
1061 filefork
= VTOF(hfsmp
->hfc_filevp
);
1063 for (i
= listp
->hfl_next
; (i
< last
) && (blksmoved
< HFC_BLKSPERSYNC
); ++i
) {
1065 * Skip invalid entries (already in hot area).
1067 if (listp
->hfl_hotfile
[i
].hf_temperature
== 0) {
1072 * Acquire a vnode for this file.
1074 error
= hfs_vget(hfsmp
, listp
->hfl_hotfile
[i
].hf_fileid
, &vp
, 0);
1076 if (error
== ENOENT
) {
1079 continue; /* stale entry, go to next */
1083 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1084 printf("hotfiles_adopt: huh, not a file %d (%d)\n", listp
->hfl_hotfile
[i
].hf_fileid
, VTOC(vp
)->c_cnid
);
1085 hfs_unlock(VTOC(vp
));
1087 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1089 continue; /* stale entry, go to next */
1091 if (hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1092 hfs_unlock(VTOC(vp
));
1094 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1096 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1097 continue; /* stale entry, go to next */
1099 fileblocks
= VTOF(vp
)->ff_blocks
;
1100 if (fileblocks
> hfsmp
->hfs_hotfile_freeblks
) {
1101 hfs_unlock(VTOC(vp
));
1104 listp
->hfl_totalblocks
-= fileblocks
;
1105 continue; /* entry too big, go to next */
1108 if ((blksmoved
> 0) &&
1109 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1110 hfs_unlock(VTOC(vp
));
1112 break; /* adopt this entry the next time around */
1114 /* Start a new transaction. */
1115 if (hfs_start_transaction(hfsmp
) != 0) {
1117 hfs_unlock(VTOC(vp
));
1123 if (VTOC(vp
)->c_desc
.cd_nameptr
)
1124 data
= *(u_int32_t
*)(VTOC(vp
)->c_desc
.cd_nameptr
);
1128 error
= hfs_relocate(vp
, hfsmp
->hfs_hotfile_start
, kauth_cred_get(), current_proc());
1129 hfs_unlock(VTOC(vp
));
1134 /* Keep hot file free space current. */
1135 hfsmp
->hfs_hotfile_freeblks
-= fileblocks
;
1136 listp
->hfl_totalblocks
-= fileblocks
;
1138 /* Insert hot file entry */
1139 key
->keyLength
= HFC_KEYLENGTH
;
1140 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
1141 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1144 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1146 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1147 error
= MacToVFSError(error
);
1152 /* Insert thread record */
1153 key
->keyLength
= HFC_KEYLENGTH
;
1154 key
->temperature
= HFC_LOOKUPTAG
;
1155 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1157 data
= listp
->hfl_hotfile
[i
].hf_temperature
;
1158 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1160 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1161 error
= MacToVFSError(error
);
1165 (void) BTFlushPath(filefork
);
1167 /* Transaction complete. */
1169 hfs_end_transaction(hfsmp
);
1173 blksmoved
+= fileblocks
;
1175 if (listp
->hfl_next
>= listp
->hfl_count
) {
1178 if (hfsmp
->hfs_hotfile_freeblks
<= 0) {
1180 printf("hotfiles_adopt: free space exhausted (%d)\n", hfsmp
->hfs_hotfile_freeblks
);
1187 printf("hotfiles_adopt: [%d] adopted %d blocks (%d left)\n", listp
->hfl_next
, blksmoved
, listp
->hfl_totalblocks
);
1189 /* Finish any outstanding transactions. */
1191 (void) BTFlushPath(filefork
);
1192 hfs_end_transaction(hfsmp
);
1195 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1197 if ((listp
->hfl_next
>= listp
->hfl_count
) || (hfsmp
->hfs_hotfile_freeblks
<= 0)) {
1199 printf("hotfiles_adopt: all done relocating %d files\n", listp
->hfl_count
);
1200 printf("hotfiles_adopt: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1204 FREE(iterator
, M_TEMP
);
1206 if (stage
!= HFC_ADOPTION
&& hfsmp
->hfc_filevp
) {
1207 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
1208 hfsmp
->hfc_filevp
= NULL
;
1210 hfsmp
->hfc_stage
= stage
;
1211 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1216 * Reclaim space by evicting the coldest files.
1218 * Requires that the hfc_mutex be held.
1221 hotfiles_evict(struct hfsmount
*hfsmp
, struct proc
*p
)
1223 BTreeIterator
* iterator
;
1226 filefork_t
* filefork
;
1227 hotfilelist_t
*listp
;
1228 enum hfc_stage stage
;
1229 u_int32_t savedtemp
;
1234 int startedtrans
= 0;
1237 if (hfsmp
->hfc_stage
!= HFC_EVICTION
) {
1241 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1244 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1248 stage
= hfsmp
->hfc_stage
;
1249 hfsmp
->hfc_stage
= HFC_BUSY
;
1251 filesmoved
= blksmoved
= 0;
1252 bt_op
= kBTreeFirstRecord
;
1254 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1255 bzero(iterator
, sizeof(*iterator
));
1256 key
= (HotFileKey
*) &iterator
->key
;
1258 filefork
= VTOF(hfsmp
->hfc_filevp
);
1260 while (listp
->hfl_reclaimblks
> 0 &&
1261 blksmoved
< HFC_BLKSPERSYNC
&&
1262 filesmoved
< HFC_FILESPERSYNC
) {
1265 * Obtain the first record (ie the coldest one).
1267 if (BTIterateRecord(filefork
, bt_op
, iterator
, NULL
, NULL
) != 0) {
1269 printf("hotfiles_evict: no more records\n");
1272 stage
= HFC_ADOPTION
;
1275 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1276 printf("hotfiles_evict: invalid key length %d\n", key
->keyLength
);
1280 if (key
->temperature
== HFC_LOOKUPTAG
) {
1282 printf("hotfiles_evict: ran into thread records\n");
1285 stage
= HFC_ADOPTION
;
1289 * Aquire the vnode for this file.
1291 error
= hfs_vget(hfsmp
, key
->fileID
, &vp
, 0);
1293 /* Start a new transaction. */
1294 if (hfs_start_transaction(hfsmp
) != 0) {
1296 hfs_unlock(VTOC(vp
));
1305 if (error
== ENOENT
) {
1306 goto delete; /* stale entry, go to next */
1308 printf("hotfiles_evict: err %d getting file %d\n",
1309 error
, key
->fileID
);
1313 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1314 printf("hotfiles_evict: huh, not a file %d\n", key
->fileID
);
1315 hfs_unlock(VTOC(vp
));
1317 goto delete; /* invalid entry, go to next */
1319 fileblocks
= VTOF(vp
)->ff_blocks
;
1320 if ((blksmoved
> 0) &&
1321 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1322 hfs_unlock(VTOC(vp
));
1327 * Make sure file is in the hot area.
1329 if (!hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1331 printf("hotfiles_evict: file %d isn't hot!\n", key
->fileID
);
1333 hfs_unlock(VTOC(vp
));
1335 goto delete; /* stale entry, go to next */
1339 * Relocate file out of hot area.
1341 error
= hfs_relocate(vp
, HFSTOVCB(hfsmp
)->nextAllocation
, proc_ucred(p
), p
);
1343 printf("hotfiles_evict: err %d relocating file %d\n", error
, key
->fileID
);
1344 hfs_unlock(VTOC(vp
));
1346 bt_op
= kBTreeNextRecord
;
1347 goto next
; /* go to next */
1351 // We do not believe that this call to hfs_fsync() is
1352 // necessary and it causes a journal transaction
1353 // deadlock so we are removing it.
1355 // (void) hfs_fsync(vp, MNT_WAIT, 0, p);
1357 hfs_unlock(VTOC(vp
));
1360 hfsmp
->hfs_hotfile_freeblks
+= fileblocks
;
1361 listp
->hfl_reclaimblks
-= fileblocks
;
1362 if (listp
->hfl_reclaimblks
< 0)
1363 listp
->hfl_reclaimblks
= 0;
1364 blksmoved
+= fileblocks
;
1367 error
= BTDeleteRecord(filefork
, iterator
);
1369 error
= MacToVFSError(error
);
1372 savedtemp
= key
->temperature
;
1373 key
->temperature
= HFC_LOOKUPTAG
;
1374 error
= BTDeleteRecord(filefork
, iterator
);
1376 error
= MacToVFSError(error
);
1379 key
->temperature
= savedtemp
;
1381 (void) BTFlushPath(filefork
);
1383 /* Transaction complete. */
1385 hfs_end_transaction(hfsmp
);
1392 printf("hotfiles_evict: moved %d files (%d blks, %d to go)\n", filesmoved
, blksmoved
, listp
->hfl_reclaimblks
);
1394 /* Finish any outstanding transactions. */
1396 (void) BTFlushPath(filefork
);
1397 hfs_end_transaction(hfsmp
);
1400 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1403 * Move to next stage when finished.
1405 if (listp
->hfl_reclaimblks
<= 0) {
1406 stage
= HFC_ADOPTION
;
1408 printf("hotfiles_evict: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1411 FREE(iterator
, M_TEMP
);
1412 hfsmp
->hfc_stage
= stage
;
1413 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1418 * Age the existing records in the hot files b-tree.
1421 hotfiles_age(struct hfsmount
*hfsmp
)
1423 BTreeInfoRec btinfo
;
1424 BTreeIterator
* iterator
;
1425 BTreeIterator
* prev_iterator
;
1426 FSBufferDescriptor record
;
1427 FSBufferDescriptor prev_record
;
1429 HotFileKey
* prev_key
;
1430 filefork_t
* filefork
;
1432 u_int32_t prev_data
;
1441 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1442 bzero(iterator
, 2 * sizeof(*iterator
));
1443 key
= (HotFileKey
*) &iterator
->key
;
1445 prev_iterator
= &iterator
[1];
1446 prev_key
= (HotFileKey
*) &prev_iterator
->key
;
1448 record
.bufferAddress
= &data
;
1449 record
.itemSize
= sizeof(data
);
1450 record
.itemCount
= 1;
1451 prev_record
.bufferAddress
= &prev_data
;
1452 prev_record
.itemSize
= sizeof(prev_data
);
1453 prev_record
.itemCount
= 1;
1456 * Capture b-tree changes inside a transaction
1458 if (hfs_start_transaction(hfsmp
) != 0) {
1462 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1466 filefork
= VTOF(hfsmp
->hfc_filevp
);
1468 error
= BTGetInformation(filefork
, 0, &btinfo
);
1470 error
= MacToVFSError(error
);
1473 if (btinfo
.numRecords
< 2) {
1478 /* Only want 1st half of leaf records */
1479 numrecs
= (btinfo
.numRecords
/= 2) - 1;
1481 error
= BTIterateRecord(filefork
, kBTreeFirstRecord
, iterator
, &record
, &reclen
);
1483 printf("hfs_agehotfiles: BTIterateRecord: %d\n", error
);
1484 error
= MacToVFSError(error
);
1487 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1490 for (i
= 0; i
< numrecs
; ++i
) {
1491 error
= BTIterateRecord(filefork
, kBTreeNextRecord
, iterator
, &record
, &reclen
);
1493 if (key
->temperature
< prev_key
->temperature
) {
1494 printf("hfs_agehotfiles: out of order keys!\n");
1498 if (reclen
!= sizeof(data
)) {
1499 printf("hfs_agehotfiles: invalid record length %d\n", reclen
);
1503 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1504 printf("hfs_agehotfiles: invalid key length %d\n", key
->keyLength
);
1508 } else if ((error
== fsBTEndOfIterationErr
|| error
== fsBTRecordNotFoundErr
) &&
1509 (i
== (numrecs
- 1))) {
1512 printf("hfs_agehotfiles: %d of %d BTIterateRecord: %d\n", i
, numrecs
, error
);
1513 error
= MacToVFSError(error
);
1516 if (prev_key
->temperature
== HFC_LOOKUPTAG
) {
1518 printf("hfs_agehotfiles: ran into thread record\n");
1523 error
= BTDeleteRecord(filefork
, prev_iterator
);
1525 printf("hfs_agehotfiles: BTDeleteRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1526 error
= MacToVFSError(error
);
1530 /* Age by halving the temperature (floor = 4) */
1531 newtemp
= MAX(prev_key
->temperature
>> 1, 4);
1532 prev_key
->temperature
= newtemp
;
1534 error
= BTInsertRecord(filefork
, prev_iterator
, &prev_record
, prev_record
.itemSize
);
1536 printf("hfs_agehotfiles: BTInsertRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1537 error
= MacToVFSError(error
);
1542 * Update thread entry with latest temperature.
1544 prev_key
->temperature
= HFC_LOOKUPTAG
;
1545 error
= BTUpdateRecord(filefork
, prev_iterator
,
1546 (IterateCallBackProcPtr
)update_callback
,
1549 printf("hfs_agehotfiles: %d of %d BTUpdateRecord failed %d (file %d, %d)\n",
1550 i
, numrecs
, error
, prev_key
->fileID
, newtemp
);
1551 error
= MacToVFSError(error
);
1555 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1562 printf("hfs_agehotfiles: aged %d records out of %d\n", aged
, btinfo
.numRecords
);
1564 (void) BTFlushPath(filefork
);
1566 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1568 hfs_end_transaction(hfsmp
);
1570 FREE(iterator
, M_TEMP
);
1575 * Return true if any blocks (or all blocks if all is true)
1576 * are contained in the hot file region.
1579 hotextents(struct hfsmount
*hfsmp
, HFSPlusExtentDescriptor
* extents
)
1585 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1586 b1
= extents
[i
].startBlock
;
1589 b2
= b1
+ extents
[i
].blockCount
- 1;
1590 if ((b1
>= hfsmp
->hfs_hotfile_start
&&
1591 b2
<= hfsmp
->hfs_hotfile_end
) ||
1592 (b1
< hfsmp
->hfs_hotfile_end
&&
1593 b2
> hfsmp
->hfs_hotfile_end
)) {
1603 *========================================================================
1604 * HOT FILE B-TREE ROUTINES
1605 *========================================================================
1609 * Open the hot files b-tree for writing.
1611 * On successful exit the vnode has a reference but not an iocount.
1614 hfc_btree_open(struct hfsmount
*hfsmp
, struct vnode
**vpp
)
1618 struct cat_desc cdesc
;
1619 struct cat_attr cattr
;
1620 struct cat_fork cfork
;
1621 static char filename
[] = HFC_FILENAME
;
1629 bzero(&cdesc
, sizeof(cdesc
));
1630 cdesc
.cd_parentcnid
= kRootDirID
;
1631 cdesc
.cd_nameptr
= filename
;
1632 cdesc
.cd_namelen
= strlen(filename
);
1634 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1636 error
= cat_lookup(hfsmp
, &cdesc
, 0, &cdesc
, &cattr
, &cfork
, NULL
);
1638 hfs_systemfile_unlock(hfsmp
, lockflags
);
1641 printf("hfc_btree_open: cat_lookup error %d\n", error
);
1645 cdesc
.cd_flags
|= CD_ISMETA
;
1646 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cdesc
, 0, &cattr
, &cfork
, &vp
);
1648 printf("hfc_btree_open: hfs_getnewvnode error %d\n", error
);
1649 cat_releasedesc(&cdesc
);
1652 if (!vnode_issystem(vp
)) {
1654 printf("hfc_btree_open: file has UBC, try again\n");
1656 hfs_unlock(VTOC(vp
));
1665 /* Open the B-tree file for writing... */
1666 error
= BTOpenPath(VTOF(vp
), (KeyCompareProcPtr
) hfc_comparekeys
);
1668 printf("hfc_btree_open: BTOpenPath error %d\n", error
);
1669 error
= MacToVFSError(error
);
1672 hfs_unlock(VTOC(vp
));
1675 vnode_ref(vp
); /* keep a reference while its open */
1679 if (!vnode_issystem(vp
))
1680 panic("hfc_btree_open: not a system file (vp = 0x%08x)", vp
);
1682 if (UBCINFOEXISTS(vp
))
1683 panic("hfc_btree_open: has UBCInfo (vp = 0x%08x)", vp
);
1689 * Close the hot files b-tree.
1691 * On entry the vnode has a reference.
1694 hfc_btree_close(struct hfsmount
*hfsmp
, struct vnode
*vp
)
1696 struct proc
*p
= current_proc();
1701 journal_flush(hfsmp
->jnl
);
1704 if (vnode_get(vp
) == 0) {
1705 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
1707 (void) hfs_fsync(vp
, MNT_WAIT
, 0, p
);
1708 error
= BTClosePath(VTOF(vp
));
1709 hfs_unlock(VTOC(vp
));
1720 * Create a hot files btree file.
1724 hfc_btree_create(struct hfsmount
*hfsmp
, int nodesize
, int entries
)
1726 struct vnode
*dvp
= NULL
;
1727 struct vnode
*vp
= NULL
;
1728 struct cnode
*cp
= NULL
;
1729 struct vfs_context context
;
1730 struct vnode_attr va
;
1731 struct componentname cname
;
1732 static char filename
[] = HFC_FILENAME
;
1735 context
.vc_proc
= current_proc();
1736 context
.vc_ucred
= kauth_cred_get();
1738 if (hfsmp
->hfc_filevp
)
1739 panic("hfc_btree_create: hfc_filevp exists (vp = 0x%08x)", hfsmp
->hfc_filevp
);
1741 error
= VFS_ROOT(HFSTOVFS(hfsmp
), &dvp
, &context
);
1745 cname
.cn_nameiop
= CREATE
;
1746 cname
.cn_flags
= ISLASTCN
;
1747 cname
.cn_context
= &context
;
1748 cname
.cn_pnbuf
= filename
;
1749 cname
.cn_pnlen
= sizeof(filename
);
1750 cname
.cn_nameptr
= filename
;
1751 cname
.cn_namelen
= strlen(filename
);
1753 cname
.cn_consume
= 0;
1756 VATTR_SET(&va
, va_type
, VREG
);
1757 VATTR_SET(&va
, va_mode
, S_IFREG
| S_IRUSR
| S_IWUSR
);
1758 VATTR_SET(&va
, va_uid
, 0);
1759 VATTR_SET(&va
, va_gid
, 0);
1761 /* call ourselves directly, ignore the higher-level VFS file creation code */
1762 error
= VNOP_CREATE(dvp
, &vp
, &cname
, &va
, &context
);
1764 printf("HFS: error %d creating HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1771 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1776 /* Don't use non-regular files or files with links. */
1777 if (!vnode_isreg(vp
) || cp
->c_nlink
!= 1) {
1782 printf("HFS: created HFBT on %s\n", HFSTOVCB(hfsmp
)->vcbVN
);
1784 if (VTOF(vp
)->ff_size
< (u_int64_t
)nodesize
) {
1788 BTNodeDescriptor
*ndp
;
1790 HotFilesInfo
*hotfileinfo
;
1796 * Mark it invisible (truncate will pull these changes).
1798 ((FndrFileInfo
*)&cp
->c_finderinfo
[0])->fdFlags
|=
1799 SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1801 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buffer
, nodesize
)) {
1805 bzero(buffer
, nodesize
);
1806 index
= (int16_t *)buffer
;
1808 entirespernode
= (nodesize
- sizeof(BTNodeDescriptor
) - 2) /
1809 (sizeof(HotFileKey
) + 6);
1810 nodecnt
= 2 + howmany(entries
* 2, entirespernode
);
1811 nodecnt
= roundup(nodecnt
, 8);
1812 filesize
= nodecnt
* nodesize
;
1814 /* FILL IN THE NODE DESCRIPTOR: */
1815 ndp
= (BTNodeDescriptor
*)buffer
;
1816 ndp
->kind
= kBTHeaderNode
;
1817 ndp
->numRecords
= SWAP_BE16 (3);
1818 offset
= sizeof(BTNodeDescriptor
);
1819 index
[(nodesize
/ 2) - 1] = SWAP_BE16 (offset
);
1821 /* FILL IN THE HEADER RECORD: */
1822 bthp
= (BTHeaderRec
*)((UInt8
*)buffer
+ offset
);
1823 bthp
->nodeSize
= SWAP_BE16 (nodesize
);
1824 bthp
->totalNodes
= SWAP_BE32 (filesize
/ nodesize
);
1825 bthp
->freeNodes
= SWAP_BE32 (nodecnt
- 1);
1826 bthp
->clumpSize
= SWAP_BE32 (filesize
);
1827 bthp
->btreeType
= kUserBTreeType
; /* non-metadata */
1828 bthp
->attributes
|= SWAP_BE32 (kBTBigKeysMask
);
1829 bthp
->maxKeyLength
= SWAP_BE16 (HFC_KEYLENGTH
);
1830 offset
+= sizeof(BTHeaderRec
);
1831 index
[(nodesize
/ 2) - 2] = SWAP_BE16 (offset
);
1833 /* FILL IN THE USER RECORD: */
1834 hotfileinfo
= (HotFilesInfo
*)((UInt8
*)buffer
+ offset
);
1835 hotfileinfo
->magic
= SWAP_BE32 (HFC_MAGIC
);
1836 hotfileinfo
->version
= SWAP_BE32 (HFC_VERSION
);
1837 hotfileinfo
->duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
1838 hotfileinfo
->timebase
= 0;
1839 hotfileinfo
->timeleft
= 0;
1840 hotfileinfo
->threshold
= SWAP_BE32 (HFC_MINIMUM_TEMPERATURE
);
1841 hotfileinfo
->maxfileblks
= SWAP_BE32 (HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
);
1842 hotfileinfo
->maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
1843 strcpy(hotfileinfo
->tag
, hfc_tag
);
1844 offset
+= kBTreeHeaderUserBytes
;
1845 index
[(nodesize
/ 2) - 3] = SWAP_BE16 (offset
);
1847 /* FILL IN THE MAP RECORD (only one node in use). */
1848 *((u_int8_t
*)buffer
+ offset
) = 0x80;
1849 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
1850 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
1851 index
[(nodesize
/ 2) - 4] = SWAP_BE16 (offset
);
1853 vnode_setnoflush(vp
);
1854 error
= hfs_truncate(vp
, (off_t
)filesize
, IO_NDELAY
, 0, &context
);
1856 printf("HFS: error %d growing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1859 cp
->c_flag
|= C_ZFWANTSYNC
;
1860 cp
->c_zftimeout
= 1;
1863 struct vnop_write_args args
;
1866 auio
= uio_create(1, 0, UIO_SYSSPACE32
, UIO_WRITE
);
1867 uio_addiov(auio
, (uintptr_t)buffer
, nodesize
);
1869 args
.a_desc
= &vnop_write_desc
;
1873 args
.a_context
= &context
;
1878 error
= hfs_vnop_write(&args
);
1880 printf("HFS: error %d writing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1884 kmem_free(kernel_map
, (vm_offset_t
)buffer
, nodesize
);
1900 * Compare two hot file b-tree keys.
1902 * Result: +n search key > trial key
1903 * 0 search key = trial key
1904 * -n search key < trial key
1907 hfc_comparekeys(HotFileKey
*searchKey
, HotFileKey
*trialKey
)
1910 * Compared temperatures first.
1912 if (searchKey
->temperature
== trialKey
->temperature
) {
1914 * Temperatures are equal so compare file ids.
1916 if (searchKey
->fileID
== trialKey
->fileID
) {
1918 * File ids are equal so compare fork types.
1920 if (searchKey
->forkType
== trialKey
->forkType
) {
1922 } else if (searchKey
->forkType
> trialKey
->forkType
) {
1925 } else if (searchKey
->fileID
> trialKey
->fileID
) {
1928 } else if (searchKey
->temperature
> trialKey
->temperature
) {
1937 *========================================================================
1938 * HOT FILE DATA COLLECTING ROUTINES
1939 *========================================================================
1943 * Lookup a hot file entry in the tree.
1946 static hotfile_entry_t
*
1947 hf_lookup(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
1949 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1952 entry
->temperature
!= temperature
&&
1953 entry
->fileid
!= fileid
) {
1955 if (temperature
> entry
->temperature
)
1956 entry
= entry
->right
;
1957 else if (temperature
< entry
->temperature
)
1958 entry
= entry
->left
;
1959 else if (fileid
> entry
->fileid
)
1960 entry
= entry
->right
;
1962 entry
= entry
->left
;
1969 * Insert a hot file entry into the tree.
1972 hf_insert(hotfile_data_t
*hotdata
, hotfile_entry_t
*newentry
)
1974 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1975 u_int32_t fileid
= newentry
->fileid
;
1976 u_int32_t temperature
= newentry
->temperature
;
1978 if (entry
== NULL
) {
1979 hotdata
->rootentry
= newentry
;
1980 hotdata
->coldest
= newentry
;
1981 hotdata
->activefiles
++;
1986 if (temperature
> entry
->temperature
) {
1988 entry
= entry
->right
;
1990 entry
->right
= newentry
;
1993 } else if (temperature
< entry
->temperature
) {
1995 entry
= entry
->left
;
1997 entry
->left
= newentry
;
2000 } else if (fileid
> entry
->fileid
) {
2002 entry
= entry
->right
;
2004 if (entry
->fileid
!= fileid
)
2005 entry
->right
= newentry
;
2010 entry
= entry
->left
;
2012 if (entry
->fileid
!= fileid
)
2013 entry
->left
= newentry
;
2019 hotdata
->activefiles
++;
2023 * Find the coldest entry in the tree.
2025 static hotfile_entry_t
*
2026 hf_coldest(hotfile_data_t
*hotdata
)
2028 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2032 entry
= entry
->left
;
2038 * Find the hottest entry in the tree.
2040 static hotfile_entry_t
*
2041 hf_hottest(hotfile_data_t
*hotdata
)
2043 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2046 while (entry
->right
)
2047 entry
= entry
->right
;
2053 * Delete a hot file entry from the tree.
2056 hf_delete(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
2058 hotfile_entry_t
*entry
, *parent
, *next
;
2061 entry
= hotdata
->rootentry
;
2064 entry
->temperature
!= temperature
&&
2065 entry
->fileid
!= fileid
) {
2068 if (temperature
> entry
->temperature
)
2069 entry
= entry
->right
;
2070 else if (temperature
< entry
->temperature
)
2071 entry
= entry
->left
;
2072 else if (fileid
> entry
->fileid
)
2073 entry
= entry
->right
;
2075 entry
= entry
->left
;
2080 * Reorginize the sub-trees spanning from our entry.
2082 if ((next
= entry
->right
)) {
2083 hotfile_entry_t
*pnextl
, *psub
;
2085 * Tree pruning: take the left branch of the
2086 * current entry and place it at the lowest
2087 * left branch of the current right branch
2091 /* Walk the Right/Left sub tree from current entry */
2092 while ((pnextl
= psub
->left
))
2095 /* Plug the old left tree to the new ->Right leftmost entry */
2096 psub
->left
= entry
->left
;
2098 } else /* only left sub-tree, simple case */ {
2102 * Now, plug the current entry sub tree to
2103 * the good pointer of our parent entry.
2106 hotdata
->rootentry
= next
;
2107 else if (parent
->left
== entry
)
2108 parent
->left
= next
;
2110 parent
->right
= next
;
2112 /* Place entry back on the free-list */
2115 entry
->temperature
= 0;
2117 entry
->right
= hotdata
->freelist
;
2118 hotdata
->freelist
= entry
;
2119 hotdata
->activefiles
--;
2121 if (hotdata
->coldest
== entry
|| hotdata
->coldest
== NULL
) {
2122 hotdata
->coldest
= hf_coldest(hotdata
);
2129 * Get a free hot file entry.
2131 static hotfile_entry_t
*
2132 hf_getnewentry(hotfile_data_t
*hotdata
)
2134 hotfile_entry_t
* entry
;
2137 * When the free list is empty then steal the coldest one
2139 if (hotdata
->freelist
== NULL
) {
2140 entry
= hf_coldest(hotdata
);
2141 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2143 entry
= hotdata
->freelist
;
2144 hotdata
->freelist
= entry
->right
;
2152 * Generate a sorted list of hot files (hottest to coldest).
2154 * As a side effect, every node in the hot file tree will be
2155 * deleted (moved to the free list).
2158 hf_getsortedlist(hotfile_data_t
* hotdata
, hotfilelist_t
*sortedlist
)
2161 hotfile_entry_t
*entry
;
2163 while ((entry
= hf_hottest(hotdata
)) != NULL
) {
2164 sortedlist
->hfl_hotfile
[i
].hf_fileid
= entry
->fileid
;
2165 sortedlist
->hfl_hotfile
[i
].hf_temperature
= entry
->temperature
;
2166 sortedlist
->hfl_hotfile
[i
].hf_blocks
= entry
->blocks
;
2167 sortedlist
->hfl_totalblocks
+= entry
->blocks
;
2170 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2173 sortedlist
->hfl_count
= i
;
2176 printf("HFS: hf_getsortedlist returned %d entries\n", i
);
2183 hf_maxdepth(hotfile_entry_t
* root
, int depth
, int *maxdepth
)
2187 if (depth
> *maxdepth
)
2189 hf_maxdepth(root
->left
, depth
, maxdepth
);
2190 hf_maxdepth(root
->right
, depth
, maxdepth
);
2195 hf_printtree(hotfile_entry_t
* root
)
2198 hf_printtree(root
->left
);
2199 printf("temperature: % 8d, fileid %d\n", root
->temperature
, root
->fileid
);
2200 hf_printtree(root
->right
);