2 * Copyright (c) 2003-2006 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/ubc_internal.h>
36 #include <sys/vnode.h>
37 #include <sys/vnode_internal.h>
38 #include <sys/kauth.h>
41 #include <hfs/hfs_endian.h>
42 #include <hfs/hfs_format.h>
43 #include <hfs/hfs_mount.h>
44 #include <hfs/hfs_hotfiles.h>
46 #include "hfscommon/headers/BTreeScanner.h"
54 * Minimum post Tiger base time.
55 * Thu Mar 31 17:00:00 2005
57 #define HFC_MIN_BASE_TIME 0x424c8f00L
60 * Hot File List (runtime).
62 typedef struct hotfileinfo
{
64 u_int32_t hf_temperature
;
68 typedef struct hotfilelist
{
70 u_int32_t hfl_version
;
71 time_t hfl_duration
; /* duration of sample period */
72 int hfl_count
; /* count of hot files recorded */
73 int hfl_next
; /* next file to move */
74 int hfl_totalblocks
; /* total hot file blocks */
75 int hfl_reclaimblks
; /* blocks to reclaim in HFV */
76 u_int32_t hfl_spare
[2];
77 hotfileinfo_t hfl_hotfile
[1]; /* array of hot files */
82 * Hot File Entry (runtime).
84 typedef struct hotfile_entry
{
85 struct hotfile_entry
*left
;
86 struct hotfile_entry
*right
;
88 u_int32_t temperature
;
93 * Hot File Recording Data (runtime).
95 typedef struct hotfile_data
{
96 struct hfsmount
*hfsmp
;
98 int activefiles
; /* active number of hot files */
101 hotfile_entry_t
*rootentry
;
102 hotfile_entry_t
*freelist
;
103 hotfile_entry_t
*coldest
;
104 hotfile_entry_t entries
[1];
107 static int hfs_recording_start (struct hfsmount
*);
108 static int hfs_recording_stop (struct hfsmount
*);
112 * Hot File Data recording functions (in-memory binary tree).
114 static void hf_insert (hotfile_data_t
*, hotfile_entry_t
*);
115 static void hf_delete (hotfile_data_t
*, u_int32_t
, u_int32_t
);
116 static hotfile_entry_t
* hf_coldest (hotfile_data_t
*);
117 static hotfile_entry_t
* hf_getnewentry (hotfile_data_t
*);
118 static void hf_getsortedlist (hotfile_data_t
*, hotfilelist_t
*);
121 static hotfile_entry_t
* hf_lookup (hotfile_data_t
*, u_int32_t
, u_int32_t
);
122 static void hf_maxdepth(hotfile_entry_t
*, int, int *);
123 static void hf_printtree (hotfile_entry_t
*);
127 * Hot File misc support functions.
129 static int hotfiles_collect (struct hfsmount
*);
130 static int hotfiles_age (struct hfsmount
*);
131 static int hotfiles_adopt (struct hfsmount
*);
132 static int hotfiles_evict (struct hfsmount
*, vfs_context_t
);
133 static int hotfiles_refine (struct hfsmount
*);
134 static int hotextents(struct hfsmount
*, HFSPlusExtentDescriptor
*);
135 static int hfs_addhotfile_internal(struct vnode
*);
139 * Hot File Cluster B-tree (on disk) functions.
141 static int hfc_btree_create (struct hfsmount
*, unsigned int, unsigned int);
142 static int hfc_btree_open (struct hfsmount
*, struct vnode
**);
143 static int hfc_btree_close (struct hfsmount
*, struct vnode
*);
144 static int hfc_comparekeys (HotFileKey
*, HotFileKey
*);
147 char hfc_tag
[] = "CLUSTERED HOT FILES B-TREE ";
151 *========================================================================
152 * HOT FILE INTERFACE ROUTINES
153 *========================================================================
157 * Start recording the hotest files on a file system.
159 * Requires that the hfc_mutex be held.
162 hfs_recording_start(struct hfsmount
*hfsmp
)
164 hotfile_data_t
*hotdata
;
171 if ((hfsmp
->hfs_flags
& HFS_READ_ONLY
) ||
172 (hfsmp
->jnl
== NULL
) ||
173 (hfsmp
->hfs_flags
& HFS_METADATA_ZONE
) == 0) {
176 if (HFSTOVCB(hfsmp
)->freeBlocks
< (2 * (u_int32_t
)hfsmp
->hfs_hotfile_maxblks
)) {
179 if (hfsmp
->hfc_stage
!= HFC_IDLE
) {
182 hfsmp
->hfc_stage
= HFC_BUSY
;
185 * Dump previous recording data.
187 if (hfsmp
->hfc_recdata
) {
190 tmp
= hfsmp
->hfc_recdata
;
191 hfsmp
->hfc_recdata
= NULL
;
195 microtime(&tv
); /* Times are base on GMT time. */
198 * On first startup check for suspended recording.
200 if (hfsmp
->hfc_timebase
== 0 &&
201 hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
) == 0) {
202 HotFilesInfo hotfileinfo
;
204 if ((BTGetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
,
205 sizeof(hotfileinfo
)) == 0) &&
206 (SWAP_BE32 (hotfileinfo
.magic
) == HFC_MAGIC
) &&
207 (SWAP_BE32 (hotfileinfo
.timeleft
) > 0) &&
208 (SWAP_BE32 (hotfileinfo
.timebase
) > 0)) {
209 hfsmp
->hfc_maxfiles
= SWAP_BE32 (hotfileinfo
.maxfilecnt
);
210 hfsmp
->hfc_timeout
= SWAP_BE32 (hotfileinfo
.timeleft
) + tv
.tv_sec
;
211 hfsmp
->hfc_timebase
= SWAP_BE32 (hotfileinfo
.timebase
);
212 /* Fix up any bogus timebase values. */
213 if (hfsmp
->hfc_timebase
< HFC_MIN_BASE_TIME
) {
214 hfsmp
->hfc_timebase
= hfsmp
->hfc_timeout
- HFC_DEFAULT_DURATION
;
217 printf("Resume recording hot files on %s (%d secs left)\n",
218 hfsmp
->vcbVN
, SWAP_BE32 (hotfileinfo
.timeleft
));
221 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
222 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
223 hfsmp
->hfc_timeout
= hfsmp
->hfc_timebase
+ HFC_DEFAULT_DURATION
;
225 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
226 hfsmp
->hfc_filevp
= NULL
;
228 struct cat_attr cattr
;
232 * Make sure a btree file exists.
234 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
236 !S_ISREG(cattr
.ca_mode
) &&
237 (error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
))) {
238 hfsmp
->hfc_stage
= HFC_IDLE
;
239 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
243 printf("HFS: begin recording hot files on %s\n", hfsmp
->vcbVN
);
245 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
246 hfsmp
->hfc_timeout
= tv
.tv_sec
+ HFC_DEFAULT_DURATION
;
248 /* Reset time base. */
249 if (hfsmp
->hfc_timebase
== 0) {
250 hfsmp
->hfc_timebase
= tv
.tv_sec
+ 1;
252 time_t cumulativebase
;
254 cumulativebase
= hfsmp
->hfc_timeout
- (HFC_CUMULATIVE_CYCLES
* HFC_DEFAULT_DURATION
);
255 hfsmp
->hfc_timebase
= MAX(hfsmp
->hfc_timebase
, cumulativebase
);
259 if ((hfsmp
->hfc_maxfiles
== 0) ||
260 (hfsmp
->hfc_maxfiles
> HFC_MAXIMUM_FILE_COUNT
)) {
261 hfsmp
->hfc_maxfiles
= HFC_DEFAULT_FILE_COUNT
;
263 maxentries
= hfsmp
->hfc_maxfiles
;
265 size
= sizeof(hotfile_data_t
) + (maxentries
* sizeof(hotfile_entry_t
));
266 MALLOC(hotdata
, hotfile_data_t
*, size
, M_TEMP
, M_WAITOK
);
267 bzero(hotdata
, size
);
269 for (i
= 1; i
< maxentries
; i
++)
270 hotdata
->entries
[i
-1].right
= &hotdata
->entries
[i
];
272 hotdata
->freelist
= &hotdata
->entries
[0];
274 * Establish minimum temperature and maximum file size.
276 hotdata
->threshold
= HFC_MINIMUM_TEMPERATURE
;
277 hotdata
->maxblocks
= HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
;
278 hotdata
->hfsmp
= hfsmp
;
280 hfsmp
->hfc_recdata
= hotdata
;
281 hfsmp
->hfc_stage
= HFC_RECORDING
;
282 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
287 * Stop recording the hotest files on a file system.
289 * Requires that the hfc_mutex be held.
292 hfs_recording_stop(struct hfsmount
*hfsmp
)
294 hotfile_data_t
*hotdata
;
295 hotfilelist_t
*listp
;
298 enum hfc_stage newstage
= HFC_IDLE
;
301 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
304 hfsmp
->hfc_stage
= HFC_BUSY
;
306 hotfiles_collect(hfsmp
);
310 * Convert hot file data into a simple file id list....
312 * then dump the sample data
315 printf("HFS: end of hot file recording on %s\n", hfsmp
->vcbVN
);
317 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
320 hfsmp
->hfc_recdata
= NULL
;
321 hfsmp
->hfc_stage
= HFC_EVALUATION
;
322 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
325 printf(" curentries: %d\n", hotdata
->activefiles
);
328 * If no hot files recorded then we're done.
330 if (hotdata
->rootentry
== NULL
) {
335 /* Open the B-tree file for writing... */
336 if (hfsmp
->hfc_filevp
)
337 panic("hfs_recording_stop: hfc_filevp exists (vp = %p)", hfsmp
->hfc_filevp
);
339 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
345 * Age the previous set of clustered hot files.
347 error
= hotfiles_age(hfsmp
);
349 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
350 hfsmp
->hfc_filevp
= NULL
;
355 * Create a sorted list of hotest files.
357 size
= sizeof(hotfilelist_t
);
358 size
+= sizeof(hotfileinfo_t
) * (hotdata
->activefiles
- 1);
359 MALLOC(listp
, hotfilelist_t
*, size
, M_TEMP
, M_WAITOK
);
362 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
363 hfsmp
->hfc_filevp
= NULL
;
369 hf_getsortedlist(hotdata
, listp
); /* NOTE: destroys hot file tree! */
371 listp
->hfl_duration
= tv
.tv_sec
- hfsmp
->hfc_timebase
;
372 hfsmp
->hfc_recdata
= listp
;
375 * Account for duplicates.
377 error
= hotfiles_refine(hfsmp
);
379 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
380 hfsmp
->hfc_filevp
= NULL
;
385 * Compute the amount of space to reclaim...
387 if (listp
->hfl_totalblocks
> hfsmp
->hfs_hotfile_freeblks
) {
388 listp
->hfl_reclaimblks
=
389 MIN(listp
->hfl_totalblocks
, hfsmp
->hfs_hotfile_maxblks
) -
390 hfsmp
->hfs_hotfile_freeblks
;
392 printf("hfs_recording_stop: need to reclaim %d blocks\n", listp
->hfl_reclaimblks
);
394 if (listp
->hfl_reclaimblks
)
395 newstage
= HFC_EVICTION
;
397 newstage
= HFC_ADOPTION
;
399 newstage
= HFC_ADOPTION
;
402 if (newstage
== HFC_ADOPTION
&& listp
->hfl_totalblocks
== 0) {
403 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
404 hfsmp
->hfc_filevp
= NULL
;
409 if (newstage
== HFC_EVICTION
)
410 printf("HFS: evicting coldest files\n");
411 else if (newstage
== HFC_ADOPTION
)
412 printf("HFS: adopting hotest files\n");
414 FREE(hotdata
, M_TEMP
);
416 hfsmp
->hfc_stage
= newstage
;
417 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
422 * Suspend recording the hotest files on a file system.
426 hfs_recording_suspend(struct hfsmount
*hfsmp
)
428 HotFilesInfo hotfileinfo
;
429 hotfile_data_t
*hotdata
= NULL
;
433 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
436 lck_mtx_lock(&hfsmp
->hfc_mutex
);
440 * A suspend can occur during eval/evict/adopt stage.
441 * In that case we would need to write out info and
442 * flush our HFBT vnode. Currently we just bail.
445 hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
;
446 if (hotdata
== NULL
|| hfsmp
->hfc_stage
!= HFC_RECORDING
) {
450 hfsmp
->hfc_stage
= HFC_BUSY
;
453 printf("HFS: suspend hot file recording on %s\n", hfsmp
->vcbVN
);
455 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
457 printf("hfs_recording_suspend: err %d opening btree\n", error
);
461 if (hfs_start_transaction(hfsmp
) != 0) {
465 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
467 goto end_transaction
;
471 hotfileinfo
.magic
= SWAP_BE32 (HFC_MAGIC
);
472 hotfileinfo
.version
= SWAP_BE32 (HFC_VERSION
);
473 hotfileinfo
.duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
474 hotfileinfo
.timebase
= SWAP_BE32 (hfsmp
->hfc_timebase
);
475 hotfileinfo
.timeleft
= SWAP_BE32 (hfsmp
->hfc_timeout
- tv
.tv_sec
);
476 hotfileinfo
.threshold
= SWAP_BE32 (hotdata
->threshold
);
477 hotfileinfo
.maxfileblks
= SWAP_BE32 (hotdata
->maxblocks
);
478 hotfileinfo
.maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
479 strlcpy((char *)hotfileinfo
.tag
, hfc_tag
, sizeof hotfileinfo
.tag
);
480 (void) BTSetUserData(VTOF(hfsmp
->hfc_filevp
), &hotfileinfo
, sizeof(hotfileinfo
));
482 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
485 hfs_end_transaction(hfsmp
);
488 if (hfsmp
->hfc_filevp
) {
489 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
490 hfsmp
->hfc_filevp
= NULL
;
493 FREE(hotdata
, M_TEMP
);
494 hfsmp
->hfc_recdata
= NULL
;
496 hfsmp
->hfc_stage
= HFC_DISABLED
;
497 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
499 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
509 hfs_recording_init(struct hfsmount
*hfsmp
)
512 CatalogRecord
* datap
;
514 HFSPlusCatalogFile
*filep
;
515 BTScanState scanstate
;
516 BTreeIterator
* iterator
;
517 FSBufferDescriptor record
;
519 filefork_t
* filefork
;
521 struct cat_attr cattr
;
525 int inserted
= 0; /* debug variables */
529 * For now, only the boot volume is supported.
531 if ((vfs_flags(HFSTOVFS(hfsmp
)) & MNT_ROOTFS
) == 0) {
532 hfsmp
->hfc_stage
= HFC_DISABLED
;
537 * Tracking of hot files requires up-to-date access times.
538 * So if access time updates are disabled, then we disable
541 if (vfs_flags(HFSTOVFS(hfsmp
)) & MNT_NOATIME
) {
542 hfsmp
->hfc_stage
= HFC_DISABLED
;
547 * If the Hot File btree exists then metadata zone is ready.
549 cnid
= GetFileInfo(HFSTOVCB(hfsmp
), kRootDirID
, HFC_FILENAME
, &cattr
, NULL
);
550 if (cnid
!= 0 && S_ISREG(cattr
.ca_mode
)) {
551 if (hfsmp
->hfc_stage
== HFC_DISABLED
)
552 hfsmp
->hfc_stage
= HFC_IDLE
;
555 error
= hfc_btree_create(hfsmp
, HFSTOVCB(hfsmp
)->blockSize
, HFC_DEFAULT_FILE_COUNT
);
558 printf("Error %d creating hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
563 * Open the Hot File B-tree file for writing.
565 if (hfsmp
->hfc_filevp
)
566 panic("hfs_recording_init: hfc_filevp exists (vp = %p)", hfsmp
->hfc_filevp
);
567 error
= hfc_btree_open(hfsmp
, &hfsmp
->hfc_filevp
);
570 printf("Error %d opening hot file b-tree on %s \n", error
, hfsmp
->vcbVN
);
574 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
575 bzero(iterator
, sizeof(*iterator
));
576 key
= (HotFileKey
*) &iterator
->key
;
577 key
->keyLength
= HFC_KEYLENGTH
;
579 record
.bufferAddress
= &data
;
580 record
.itemSize
= sizeof(u_int32_t
);
581 record
.itemCount
= 1;
583 printf("Evaluating space for \"%s\" metadata zone...\n", HFSTOVCB(hfsmp
)->vcbVN
);
586 * Get ready to scan the Catalog file.
588 error
= BTScanInitialize(VTOF(HFSTOVCB(hfsmp
)->catalogRefNum
), 0, 0, 0,
589 kCatSearchBufferSize
, &scanstate
);
591 printf("hfs_recording_init: err %d BTScanInit\n", error
);
596 * The writes to Hot File B-tree file are journaled.
598 if (hfs_start_transaction(hfsmp
) != 0) {
602 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
606 filefork
= VTOF(hfsmp
->hfc_filevp
);
609 * Visit all the catalog btree leaf records.
612 error
= BTScanNextRecord(&scanstate
, 0, (void **)&keyp
, (void **)&datap
, &dataSize
);
614 if (error
== btNotFound
)
617 printf("hfs_recording_init: err %d BTScanNext\n", error
);
620 if ((datap
->recordType
!= kHFSPlusFileRecord
) ||
621 (dataSize
!= sizeof(HFSPlusCatalogFile
))) {
624 filep
= (HFSPlusCatalogFile
*)datap
;
626 if (filep
->dataFork
.totalBlocks
== 0) {
630 * Any file that has blocks inside the hot file
631 * space is recorded for later eviction.
633 * For now, resource forks are ignored.
635 if (!hotextents(hfsmp
, &filep
->dataFork
.extents
[0])) {
638 cnid
= filep
->fileID
;
640 /* Skip over journal files. */
641 if (cnid
== hfsmp
->hfs_jnlfileid
|| cnid
== hfsmp
->hfs_jnlinfoblkid
) {
645 * XXX - need to skip quota files as well.
648 /* Insert a hot file entry. */
649 key
->keyLength
= HFC_KEYLENGTH
;
650 key
->temperature
= HFC_MINIMUM_TEMPERATURE
;
654 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
656 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
657 error
= MacToVFSError(error
);
661 /* Insert the corresponding thread record. */
662 key
->keyLength
= HFC_KEYLENGTH
;
663 key
->temperature
= HFC_LOOKUPTAG
;
666 data
= HFC_MINIMUM_TEMPERATURE
;
667 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
669 printf("hfs_recording_init: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
670 error
= MacToVFSError(error
);
675 (void) BTFlushPath(filefork
);
676 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
679 hfs_end_transaction(hfsmp
);
681 printf("%d files identified out of %d\n", inserted
, filecount
);
685 (void) BTScanTerminate(&scanstate
, &data
, &data
, &data
);
687 FREE(iterator
, M_TEMP
);
688 if (hfsmp
->hfc_filevp
) {
689 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
690 hfsmp
->hfc_filevp
= NULL
;
693 hfsmp
->hfc_stage
= HFC_IDLE
;
699 * Use sync to perform ocassional background work.
703 hfs_hotfilesync(struct hfsmount
*hfsmp
, vfs_context_t ctx
)
705 if (hfsmp
->hfc_stage
) {
708 lck_mtx_lock(&hfsmp
->hfc_mutex
);
710 switch (hfsmp
->hfc_stage
) {
712 (void) hfs_recording_start(hfsmp
);
717 if (tv
.tv_sec
> hfsmp
->hfc_timeout
)
718 (void) hfs_recording_stop(hfsmp
);
722 (void) hotfiles_evict(hfsmp
, ctx
);
726 (void) hotfiles_adopt(hfsmp
);
732 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
738 * Add a hot file to the recording list.
740 * This can happen when a hot file gets reclaimed or at the
741 * end of the recording period for any active hot file.
743 * NOTE: Since both the data and resource fork can be hot,
744 * there can be two entries for the same file id.
746 * Note: the cnode is locked on entry.
750 hfs_addhotfile(struct vnode
*vp
)
756 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
759 lck_mtx_lock(&hfsmp
->hfc_mutex
);
760 error
= hfs_addhotfile_internal(vp
);
761 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
766 hfs_addhotfile_internal(struct vnode
*vp
)
768 hotfile_data_t
*hotdata
;
769 hotfile_entry_t
*entry
;
773 u_int32_t temperature
;
776 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
779 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
782 /* Skip resource forks for now. */
783 if (VNODE_IS_RSRC(vp
)) {
786 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
) {
792 if ((ffp
->ff_bytesread
== 0) ||
793 (ffp
->ff_blocks
== 0) ||
794 (ffp
->ff_size
== 0) ||
795 (ffp
->ff_blocks
> hotdata
->maxblocks
) ||
796 (cp
->c_flag
& (C_DELETED
| C_NOEXISTS
)) ||
797 (cp
->c_flags
& UF_NODUMP
) ||
798 (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
802 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
803 if (temperature
< hotdata
->threshold
) {
807 * If there is room or this file is hotter than
808 * the coldest one then add it to the list.
811 if ((hotdata
->activefiles
< hfsmp
->hfc_maxfiles
) ||
812 (hotdata
->coldest
== NULL
) ||
813 (temperature
> hotdata
->coldest
->temperature
)) {
815 entry
= hf_getnewentry(hotdata
);
816 entry
->temperature
= temperature
;
817 entry
->fileid
= cp
->c_fileid
;
818 entry
->blocks
= ffp
->ff_blocks
;
819 hf_insert(hotdata
, entry
);
827 * Remove a hot file from the recording list.
829 * This can happen when a hot file becomes
830 * an active vnode (active hot files are
831 * not kept in the recording list until the
832 * end of the recording period).
834 * Note: the cnode is locked on entry.
838 hfs_removehotfile(struct vnode
*vp
)
840 hotfile_data_t
*hotdata
;
844 u_int32_t temperature
;
847 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
850 if ((!vnode_isreg(vp
) && !vnode_islnk(vp
)) || vnode_issystem(vp
)) {
857 if ((ffp
->ff_bytesread
== 0) || (ffp
->ff_blocks
== 0) ||
858 (ffp
->ff_size
== 0) || (cp
->c_atime
< hfsmp
->hfc_timebase
)) {
862 lck_mtx_lock(&hfsmp
->hfc_mutex
);
863 if (hfsmp
->hfc_stage
!= HFC_RECORDING
)
865 if ((hotdata
= (hotfile_data_t
*)hfsmp
->hfc_recdata
) == NULL
)
868 temperature
= ffp
->ff_bytesread
/ ffp
->ff_size
;
869 if (temperature
< hotdata
->threshold
)
872 if (hotdata
->coldest
&& (temperature
>= hotdata
->coldest
->temperature
)) {
874 hf_delete(hotdata
, VTOC(vp
)->c_fileid
, temperature
);
878 lck_mtx_unlock(&hfsmp
->hfc_mutex
);
884 *========================================================================
885 * HOT FILE MAINTENANCE ROUTINES
886 *========================================================================
890 hotfiles_collect_callback(struct vnode
*vp
, __unused
void *cargs
)
892 if ((vnode_isreg(vp
) || vnode_islnk(vp
)) && !vnode_issystem(vp
))
893 (void) hfs_addhotfile_internal(vp
);
895 return (VNODE_RETURNED
);
899 * Add all active hot files to the recording list.
902 hotfiles_collect(struct hfsmount
*hfsmp
)
904 struct mount
*mp
= HFSTOVFS(hfsmp
);
906 if (vfs_busy(mp
, LK_NOWAIT
))
910 * hotfiles_collect_callback will be called for each vnode
911 * hung off of this mount point
913 * properly referenced and unreferenced around the callback
915 vnode_iterate(mp
, 0, hotfiles_collect_callback
, (void *)NULL
);
924 * Update the data of a btree record
925 * This is called from within BTUpdateRecord.
928 update_callback(const HotFileKey
*key
, u_int32_t
*data
, u_int32_t
*state
)
930 if (key
->temperature
== HFC_LOOKUPTAG
)
936 * Identify files already in hot area.
939 hotfiles_refine(struct hfsmount
*hfsmp
)
941 BTreeIterator
* iterator
;
943 filefork_t
* filefork
;
944 hotfilelist_t
*listp
;
945 FSBufferDescriptor record
;
952 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
955 mp
= HFSTOVFS(hfsmp
);
957 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
958 bzero(iterator
, sizeof(*iterator
));
959 key
= (HotFileKey
*) &iterator
->key
;
961 record
.bufferAddress
= &data
;
962 record
.itemSize
= sizeof(u_int32_t
);
963 record
.itemCount
= 1;
965 if (hfs_start_transaction(hfsmp
) != 0) {
969 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
973 filefork
= VTOF(hfsmp
->hfc_filevp
);
975 for (i
= 0; i
< listp
->hfl_count
; ++i
) {
977 * Check if entry (thread) is already in hot area.
979 key
->keyLength
= HFC_KEYLENGTH
;
980 key
->temperature
= HFC_LOOKUPTAG
;
981 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
983 (void) BTInvalidateHint(iterator
);
984 if (BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
) != 0) {
985 continue; /* not in hot area, so skip */
989 * Update thread entry with latest temperature.
991 error
= BTUpdateRecord(filefork
, iterator
,
992 (IterateCallBackProcPtr
)update_callback
,
993 &listp
->hfl_hotfile
[i
].hf_temperature
);
995 printf("hotfiles_refine: BTUpdateRecord failed %d (file %d)\n", error
, key
->fileID
);
996 error
= MacToVFSError(error
);
1000 * Re-key entry with latest temperature.
1002 key
->keyLength
= HFC_KEYLENGTH
;
1003 key
->temperature
= data
;
1004 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1006 /* Pick up record data. */
1007 (void) BTInvalidateHint(iterator
);
1008 (void) BTSearchRecord(filefork
, iterator
, &record
, NULL
, iterator
);
1009 error
= BTDeleteRecord(filefork
, iterator
);
1011 printf("hotfiles_refine: BTDeleteRecord failed %d (file %d)\n", error
, key
->fileID
);
1012 error
= MacToVFSError(error
);
1015 key
->keyLength
= HFC_KEYLENGTH
;
1016 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
1017 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1019 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1021 printf("hotfiles_refine: BTInsertRecord failed %d (file %d)\n", error
, key
->fileID
);
1022 error
= MacToVFSError(error
);
1027 * Invalidate this entry in the list.
1029 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1030 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1034 (void) BTFlushPath(filefork
);
1035 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1038 hfs_end_transaction(hfsmp
);
1040 FREE(iterator
, M_TEMP
);
1045 * Move new hot files into hot area.
1047 * Requires that the hfc_mutex be held.
1050 hotfiles_adopt(struct hfsmount
*hfsmp
)
1052 BTreeIterator
* iterator
;
1054 filefork_t
* filefork
;
1055 hotfilelist_t
*listp
;
1056 FSBufferDescriptor record
;
1059 enum hfc_stage stage
;
1065 int startedtrans
= 0;
1067 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1070 if (hfsmp
->hfc_stage
!= HFC_ADOPTION
) {
1073 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1077 stage
= hfsmp
->hfc_stage
;
1078 hfsmp
->hfc_stage
= HFC_BUSY
;
1081 last
= listp
->hfl_next
+ HFC_FILESPERSYNC
;
1082 if (last
> listp
->hfl_count
)
1083 last
= listp
->hfl_count
;
1085 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1086 bzero(iterator
, sizeof(*iterator
));
1087 key
= (HotFileKey
*) &iterator
->key
;
1088 key
->keyLength
= HFC_KEYLENGTH
;
1090 record
.bufferAddress
= &data
;
1091 record
.itemSize
= sizeof(u_int32_t
);
1092 record
.itemCount
= 1;
1094 filefork
= VTOF(hfsmp
->hfc_filevp
);
1096 for (i
= listp
->hfl_next
; (i
< last
) && (blksmoved
< HFC_BLKSPERSYNC
); ++i
) {
1098 * Skip invalid entries (already in hot area).
1100 if (listp
->hfl_hotfile
[i
].hf_temperature
== 0) {
1105 * Acquire a vnode for this file.
1107 error
= hfs_vget(hfsmp
, listp
->hfl_hotfile
[i
].hf_fileid
, &vp
, 0);
1109 if (error
== ENOENT
) {
1112 continue; /* stale entry, go to next */
1116 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1117 printf("hotfiles_adopt: huh, not a file %d (%d)\n", listp
->hfl_hotfile
[i
].hf_fileid
, VTOC(vp
)->c_cnid
);
1118 hfs_unlock(VTOC(vp
));
1120 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1122 continue; /* stale entry, go to next */
1124 if (hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1125 hfs_unlock(VTOC(vp
));
1127 listp
->hfl_hotfile
[i
].hf_temperature
= 0;
1129 listp
->hfl_totalblocks
-= listp
->hfl_hotfile
[i
].hf_blocks
;
1130 continue; /* stale entry, go to next */
1132 fileblocks
= VTOF(vp
)->ff_blocks
;
1133 if (fileblocks
> hfsmp
->hfs_hotfile_freeblks
) {
1134 hfs_unlock(VTOC(vp
));
1137 listp
->hfl_totalblocks
-= fileblocks
;
1138 continue; /* entry too big, go to next */
1141 if ((blksmoved
> 0) &&
1142 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1143 hfs_unlock(VTOC(vp
));
1145 break; /* adopt this entry the next time around */
1147 if (VTOC(vp
)->c_desc
.cd_nameptr
)
1148 data
= *(const u_int32_t
*)(VTOC(vp
)->c_desc
.cd_nameptr
);
1152 error
= hfs_relocate(vp
, hfsmp
->hfs_hotfile_start
, kauth_cred_get(), current_proc());
1153 hfs_unlock(VTOC(vp
));
1156 /* Move on to next item. */
1160 /* Keep hot file free space current. */
1161 hfsmp
->hfs_hotfile_freeblks
-= fileblocks
;
1162 listp
->hfl_totalblocks
-= fileblocks
;
1164 /* Insert hot file entry */
1165 key
->keyLength
= HFC_KEYLENGTH
;
1166 key
->temperature
= listp
->hfl_hotfile
[i
].hf_temperature
;
1167 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1170 /* Start a new transaction before calling BTree code. */
1171 if (hfs_start_transaction(hfsmp
) != 0) {
1177 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1179 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1180 error
= MacToVFSError(error
);
1185 /* Insert thread record */
1186 key
->keyLength
= HFC_KEYLENGTH
;
1187 key
->temperature
= HFC_LOOKUPTAG
;
1188 key
->fileID
= listp
->hfl_hotfile
[i
].hf_fileid
;
1190 data
= listp
->hfl_hotfile
[i
].hf_temperature
;
1191 error
= BTInsertRecord(filefork
, iterator
, &record
, record
.itemSize
);
1193 printf("hotfiles_adopt: BTInsertRecord failed %d (fileid %d)\n", error
, key
->fileID
);
1194 error
= MacToVFSError(error
);
1198 (void) BTFlushPath(filefork
);
1200 /* Transaction complete. */
1202 hfs_end_transaction(hfsmp
);
1206 blksmoved
+= fileblocks
;
1208 if (listp
->hfl_next
>= listp
->hfl_count
) {
1211 if (hfsmp
->hfs_hotfile_freeblks
<= 0) {
1213 printf("hotfiles_adopt: free space exhausted (%d)\n", hfsmp
->hfs_hotfile_freeblks
);
1220 printf("hotfiles_adopt: [%d] adopted %d blocks (%d left)\n", listp
->hfl_next
, blksmoved
, listp
->hfl_totalblocks
);
1222 /* Finish any outstanding transactions. */
1224 (void) BTFlushPath(filefork
);
1225 hfs_end_transaction(hfsmp
);
1228 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1230 if ((listp
->hfl_next
>= listp
->hfl_count
) || (hfsmp
->hfs_hotfile_freeblks
<= 0)) {
1232 printf("hotfiles_adopt: all done relocating %d files\n", listp
->hfl_count
);
1233 printf("hotfiles_adopt: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1237 FREE(iterator
, M_TEMP
);
1239 if (stage
!= HFC_ADOPTION
&& hfsmp
->hfc_filevp
) {
1240 (void) hfc_btree_close(hfsmp
, hfsmp
->hfc_filevp
);
1241 hfsmp
->hfc_filevp
= NULL
;
1243 hfsmp
->hfc_stage
= stage
;
1244 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1249 * Reclaim space by evicting the coldest files.
1251 * Requires that the hfc_mutex be held.
1254 hotfiles_evict(struct hfsmount
*hfsmp
, vfs_context_t ctx
)
1256 BTreeIterator
* iterator
;
1259 filefork_t
* filefork
;
1260 hotfilelist_t
*listp
;
1261 enum hfc_stage stage
;
1262 u_int32_t savedtemp
;
1267 int startedtrans
= 0;
1270 if (hfsmp
->hfc_stage
!= HFC_EVICTION
) {
1274 if ((listp
= (hotfilelist_t
*)hfsmp
->hfc_recdata
) == NULL
)
1277 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1281 stage
= hfsmp
->hfc_stage
;
1282 hfsmp
->hfc_stage
= HFC_BUSY
;
1284 filesmoved
= blksmoved
= 0;
1285 bt_op
= kBTreeFirstRecord
;
1287 MALLOC(iterator
, BTreeIterator
*, sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1288 bzero(iterator
, sizeof(*iterator
));
1289 key
= (HotFileKey
*) &iterator
->key
;
1291 filefork
= VTOF(hfsmp
->hfc_filevp
);
1293 while (listp
->hfl_reclaimblks
> 0 &&
1294 blksmoved
< HFC_BLKSPERSYNC
&&
1295 filesmoved
< HFC_FILESPERSYNC
) {
1298 * Obtain the first record (ie the coldest one).
1300 if (BTIterateRecord(filefork
, bt_op
, iterator
, NULL
, NULL
) != 0) {
1302 printf("hotfiles_evict: no more records\n");
1305 stage
= HFC_ADOPTION
;
1308 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1309 printf("hotfiles_evict: invalid key length %d\n", key
->keyLength
);
1313 if (key
->temperature
== HFC_LOOKUPTAG
) {
1315 printf("hotfiles_evict: ran into thread records\n");
1318 stage
= HFC_ADOPTION
;
1322 * Aquire the vnode for this file.
1324 error
= hfs_vget(hfsmp
, key
->fileID
, &vp
, 0);
1326 if (error
== ENOENT
) {
1327 goto delete; /* stale entry, go to next */
1329 printf("hotfiles_evict: err %d getting file %d\n",
1330 error
, key
->fileID
);
1334 if (!vnode_isreg(vp
) && !vnode_islnk(vp
)) {
1335 printf("hotfiles_evict: huh, not a file %d\n", key
->fileID
);
1336 hfs_unlock(VTOC(vp
));
1338 goto delete; /* invalid entry, go to next */
1340 fileblocks
= VTOF(vp
)->ff_blocks
;
1341 if ((blksmoved
> 0) &&
1342 (blksmoved
+ fileblocks
) > HFC_BLKSPERSYNC
) {
1343 hfs_unlock(VTOC(vp
));
1348 * Make sure file is in the hot area.
1350 if (!hotextents(hfsmp
, &VTOF(vp
)->ff_extents
[0])) {
1352 printf("hotfiles_evict: file %d isn't hot!\n", key
->fileID
);
1354 hfs_unlock(VTOC(vp
));
1356 goto delete; /* stale entry, go to next */
1360 * Relocate file out of hot area.
1362 error
= hfs_relocate(vp
, HFSTOVCB(hfsmp
)->nextAllocation
, vfs_context_ucred(ctx
), vfs_context_proc(ctx
));
1364 printf("hotfiles_evict: err %d relocating file %d\n", error
, key
->fileID
);
1365 hfs_unlock(VTOC(vp
));
1367 bt_op
= kBTreeNextRecord
;
1368 goto next
; /* go to next */
1372 // We do not believe that this call to hfs_fsync() is
1373 // necessary and it causes a journal transaction
1374 // deadlock so we are removing it.
1376 // (void) hfs_fsync(vp, MNT_WAIT, 0, p);
1378 hfs_unlock(VTOC(vp
));
1381 hfsmp
->hfs_hotfile_freeblks
+= fileblocks
;
1382 listp
->hfl_reclaimblks
-= fileblocks
;
1383 if (listp
->hfl_reclaimblks
< 0)
1384 listp
->hfl_reclaimblks
= 0;
1385 blksmoved
+= fileblocks
;
1388 /* Start a new transaction before calling BTree code. */
1389 if (hfs_start_transaction(hfsmp
) != 0) {
1395 error
= BTDeleteRecord(filefork
, iterator
);
1397 error
= MacToVFSError(error
);
1400 savedtemp
= key
->temperature
;
1401 key
->temperature
= HFC_LOOKUPTAG
;
1402 error
= BTDeleteRecord(filefork
, iterator
);
1404 error
= MacToVFSError(error
);
1407 key
->temperature
= savedtemp
;
1409 (void) BTFlushPath(filefork
);
1411 /* Transaction complete. */
1413 hfs_end_transaction(hfsmp
);
1420 printf("hotfiles_evict: moved %d files (%d blks, %d to go)\n", filesmoved
, blksmoved
, listp
->hfl_reclaimblks
);
1422 /* Finish any outstanding transactions. */
1424 (void) BTFlushPath(filefork
);
1425 hfs_end_transaction(hfsmp
);
1428 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1431 * Move to next stage when finished.
1433 if (listp
->hfl_reclaimblks
<= 0) {
1434 stage
= HFC_ADOPTION
;
1436 printf("hotfiles_evict: %d blocks free in hot file band\n", hfsmp
->hfs_hotfile_freeblks
);
1439 FREE(iterator
, M_TEMP
);
1440 hfsmp
->hfc_stage
= stage
;
1441 wakeup((caddr_t
)&hfsmp
->hfc_stage
);
1446 * Age the existing records in the hot files b-tree.
1449 hotfiles_age(struct hfsmount
*hfsmp
)
1451 BTreeInfoRec btinfo
;
1452 BTreeIterator
* iterator
;
1453 BTreeIterator
* prev_iterator
;
1454 FSBufferDescriptor record
;
1455 FSBufferDescriptor prev_record
;
1457 HotFileKey
* prev_key
;
1458 filefork_t
* filefork
;
1460 u_int32_t prev_data
;
1469 MALLOC(iterator
, BTreeIterator
*, 2 * sizeof(*iterator
), M_TEMP
, M_WAITOK
);
1470 bzero(iterator
, 2 * sizeof(*iterator
));
1471 key
= (HotFileKey
*) &iterator
->key
;
1473 prev_iterator
= &iterator
[1];
1474 prev_key
= (HotFileKey
*) &prev_iterator
->key
;
1476 record
.bufferAddress
= &data
;
1477 record
.itemSize
= sizeof(data
);
1478 record
.itemCount
= 1;
1479 prev_record
.bufferAddress
= &prev_data
;
1480 prev_record
.itemSize
= sizeof(prev_data
);
1481 prev_record
.itemCount
= 1;
1484 * Capture b-tree changes inside a transaction
1486 if (hfs_start_transaction(hfsmp
) != 0) {
1490 if (hfs_lock(VTOC(hfsmp
->hfc_filevp
), HFS_EXCLUSIVE_LOCK
) != 0) {
1494 filefork
= VTOF(hfsmp
->hfc_filevp
);
1496 error
= BTGetInformation(filefork
, 0, &btinfo
);
1498 error
= MacToVFSError(error
);
1501 if (btinfo
.numRecords
< 2) {
1506 /* Only want 1st half of leaf records */
1507 numrecs
= (btinfo
.numRecords
/= 2) - 1;
1509 error
= BTIterateRecord(filefork
, kBTreeFirstRecord
, iterator
, &record
, &reclen
);
1511 printf("hfs_agehotfiles: BTIterateRecord: %d\n", error
);
1512 error
= MacToVFSError(error
);
1515 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1518 for (i
= 0; i
< numrecs
; ++i
) {
1519 error
= BTIterateRecord(filefork
, kBTreeNextRecord
, iterator
, &record
, &reclen
);
1521 if (key
->temperature
< prev_key
->temperature
) {
1522 printf("hfs_agehotfiles: out of order keys!\n");
1526 if (reclen
!= sizeof(data
)) {
1527 printf("hfs_agehotfiles: invalid record length %d\n", reclen
);
1531 if (key
->keyLength
!= HFC_KEYLENGTH
) {
1532 printf("hfs_agehotfiles: invalid key length %d\n", key
->keyLength
);
1536 } else if ((error
== fsBTEndOfIterationErr
|| error
== fsBTRecordNotFoundErr
) &&
1537 (i
== (numrecs
- 1))) {
1540 printf("hfs_agehotfiles: %d of %d BTIterateRecord: %d\n", i
, numrecs
, error
);
1541 error
= MacToVFSError(error
);
1544 if (prev_key
->temperature
== HFC_LOOKUPTAG
) {
1546 printf("hfs_agehotfiles: ran into thread record\n");
1551 error
= BTDeleteRecord(filefork
, prev_iterator
);
1553 printf("hfs_agehotfiles: BTDeleteRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1554 error
= MacToVFSError(error
);
1558 /* Age by halving the temperature (floor = 4) */
1559 newtemp
= MAX(prev_key
->temperature
>> 1, 4);
1560 prev_key
->temperature
= newtemp
;
1562 error
= BTInsertRecord(filefork
, prev_iterator
, &prev_record
, prev_record
.itemSize
);
1564 printf("hfs_agehotfiles: BTInsertRecord failed %d (file %d)\n", error
, prev_key
->fileID
);
1565 error
= MacToVFSError(error
);
1570 * Update thread entry with latest temperature.
1572 prev_key
->temperature
= HFC_LOOKUPTAG
;
1573 error
= BTUpdateRecord(filefork
, prev_iterator
,
1574 (IterateCallBackProcPtr
)update_callback
,
1577 printf("hfs_agehotfiles: %d of %d BTUpdateRecord failed %d (file %d, %d)\n",
1578 i
, numrecs
, error
, prev_key
->fileID
, newtemp
);
1579 error
= MacToVFSError(error
);
1583 bcopy(iterator
, prev_iterator
, sizeof(BTreeIterator
));
1590 printf("hfs_agehotfiles: aged %d records out of %d\n", aged
, btinfo
.numRecords
);
1592 (void) BTFlushPath(filefork
);
1594 hfs_unlock(VTOC(hfsmp
->hfc_filevp
));
1596 hfs_end_transaction(hfsmp
);
1598 FREE(iterator
, M_TEMP
);
1603 * Return true if any blocks (or all blocks if all is true)
1604 * are contained in the hot file region.
1607 hotextents(struct hfsmount
*hfsmp
, HFSPlusExtentDescriptor
* extents
)
1613 for (i
= 0; i
< kHFSPlusExtentDensity
; ++i
) {
1614 b1
= extents
[i
].startBlock
;
1617 b2
= b1
+ extents
[i
].blockCount
- 1;
1618 if ((b1
>= hfsmp
->hfs_hotfile_start
&&
1619 b2
<= hfsmp
->hfs_hotfile_end
) ||
1620 (b1
< hfsmp
->hfs_hotfile_end
&&
1621 b2
> hfsmp
->hfs_hotfile_end
)) {
1631 *========================================================================
1632 * HOT FILE B-TREE ROUTINES
1633 *========================================================================
1637 * Open the hot files b-tree for writing.
1639 * On successful exit the vnode has a reference but not an iocount.
1642 hfc_btree_open(struct hfsmount
*hfsmp
, struct vnode
**vpp
)
1646 struct cat_desc cdesc
;
1647 struct cat_attr cattr
;
1648 struct cat_fork cfork
;
1649 static char filename
[] = HFC_FILENAME
;
1657 bzero(&cdesc
, sizeof(cdesc
));
1658 cdesc
.cd_parentcnid
= kRootDirID
;
1659 cdesc
.cd_nameptr
= (const u_int8_t
*)filename
;
1660 cdesc
.cd_namelen
= strlen(filename
);
1662 lockflags
= hfs_systemfile_lock(hfsmp
, SFL_CATALOG
, HFS_SHARED_LOCK
);
1664 error
= cat_lookup(hfsmp
, &cdesc
, 0, &cdesc
, &cattr
, &cfork
, NULL
);
1666 hfs_systemfile_unlock(hfsmp
, lockflags
);
1669 printf("hfc_btree_open: cat_lookup error %d\n", error
);
1673 cdesc
.cd_flags
|= CD_ISMETA
;
1674 error
= hfs_getnewvnode(hfsmp
, NULL
, NULL
, &cdesc
, 0, &cattr
, &cfork
, &vp
);
1676 printf("hfc_btree_open: hfs_getnewvnode error %d\n", error
);
1677 cat_releasedesc(&cdesc
);
1680 if (!vnode_issystem(vp
)) {
1682 printf("hfc_btree_open: file has UBC, try again\n");
1684 hfs_unlock(VTOC(vp
));
1693 /* Open the B-tree file for writing... */
1694 error
= BTOpenPath(VTOF(vp
), (KeyCompareProcPtr
) hfc_comparekeys
);
1696 printf("hfc_btree_open: BTOpenPath error %d\n", error
);
1697 error
= MacToVFSError(error
);
1700 hfs_unlock(VTOC(vp
));
1703 vnode_ref(vp
); /* keep a reference while its open */
1707 if (!vnode_issystem(vp
))
1708 panic("hfc_btree_open: not a system file (vp = %p)", vp
);
1714 * Close the hot files b-tree.
1716 * On entry the vnode has a reference.
1719 hfc_btree_close(struct hfsmount
*hfsmp
, struct vnode
*vp
)
1721 proc_t p
= current_proc();
1726 journal_flush(hfsmp
->jnl
);
1729 if (vnode_get(vp
) == 0) {
1730 error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
);
1732 (void) hfs_fsync(vp
, MNT_WAIT
, 0, p
);
1733 error
= BTClosePath(VTOF(vp
));
1734 hfs_unlock(VTOC(vp
));
1745 * Create a hot files btree file.
1749 hfc_btree_create(struct hfsmount
*hfsmp
, unsigned int nodesize
, unsigned int entries
)
1751 struct vnode
*dvp
= NULL
;
1752 struct vnode
*vp
= NULL
;
1753 struct cnode
*cp
= NULL
;
1754 vfs_context_t ctx
= vfs_context_current();
1755 struct vnode_attr va
;
1756 struct componentname cname
;
1757 static char filename
[] = HFC_FILENAME
;
1760 if (hfsmp
->hfc_filevp
)
1761 panic("hfc_btree_create: hfc_filevp exists (vp = %p)", hfsmp
->hfc_filevp
);
1763 error
= VFS_ROOT(HFSTOVFS(hfsmp
), &dvp
, ctx
);
1767 cname
.cn_nameiop
= CREATE
;
1768 cname
.cn_flags
= ISLASTCN
;
1769 cname
.cn_context
= ctx
;
1770 cname
.cn_pnbuf
= filename
;
1771 cname
.cn_pnlen
= sizeof(filename
);
1772 cname
.cn_nameptr
= filename
;
1773 cname
.cn_namelen
= strlen(filename
);
1775 cname
.cn_consume
= 0;
1778 VATTR_SET(&va
, va_type
, VREG
);
1779 VATTR_SET(&va
, va_mode
, S_IFREG
| S_IRUSR
| S_IWUSR
);
1780 VATTR_SET(&va
, va_uid
, 0);
1781 VATTR_SET(&va
, va_gid
, 0);
1783 /* call ourselves directly, ignore the higher-level VFS file creation code */
1784 error
= VNOP_CREATE(dvp
, &vp
, &cname
, &va
, ctx
);
1786 printf("HFS: error %d creating HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1793 if ((error
= hfs_lock(VTOC(vp
), HFS_EXCLUSIVE_LOCK
))) {
1798 /* Don't use non-regular files or files with links. */
1799 if (!vnode_isreg(vp
) || cp
->c_linkcount
!= 1) {
1804 printf("HFS: created HFBT on %s\n", HFSTOVCB(hfsmp
)->vcbVN
);
1806 if (VTOF(vp
)->ff_size
< nodesize
) {
1810 BTNodeDescriptor
*ndp
;
1812 HotFilesInfo
*hotfileinfo
;
1818 * Mark it invisible (truncate will pull these changes).
1820 ((FndrFileInfo
*)&cp
->c_finderinfo
[0])->fdFlags
|=
1821 SWAP_BE16 (kIsInvisible
+ kNameLocked
);
1823 if (kmem_alloc(kernel_map
, (vm_offset_t
*)&buffer
, nodesize
)) {
1827 bzero(buffer
, nodesize
);
1828 index
= (u_int16_t
*)buffer
;
1830 entirespernode
= (nodesize
- sizeof(BTNodeDescriptor
) - 2) /
1831 (sizeof(HotFileKey
) + 6);
1832 nodecnt
= 2 + howmany(entries
* 2, entirespernode
);
1833 nodecnt
= roundup(nodecnt
, 8);
1834 filesize
= nodecnt
* nodesize
;
1836 /* FILL IN THE NODE DESCRIPTOR: */
1837 ndp
= (BTNodeDescriptor
*)buffer
;
1838 ndp
->kind
= kBTHeaderNode
;
1839 ndp
->numRecords
= SWAP_BE16 (3);
1840 offset
= sizeof(BTNodeDescriptor
);
1841 index
[(nodesize
/ 2) - 1] = SWAP_BE16 (offset
);
1843 /* FILL IN THE HEADER RECORD: */
1844 bthp
= (BTHeaderRec
*)((u_int8_t
*)buffer
+ offset
);
1845 bthp
->nodeSize
= SWAP_BE16 (nodesize
);
1846 bthp
->totalNodes
= SWAP_BE32 (filesize
/ nodesize
);
1847 bthp
->freeNodes
= SWAP_BE32 (nodecnt
- 1);
1848 bthp
->clumpSize
= SWAP_BE32 (filesize
);
1849 bthp
->btreeType
= kUserBTreeType
; /* non-metadata */
1850 bthp
->attributes
|= SWAP_BE32 (kBTBigKeysMask
);
1851 bthp
->maxKeyLength
= SWAP_BE16 (HFC_KEYLENGTH
);
1852 offset
+= sizeof(BTHeaderRec
);
1853 index
[(nodesize
/ 2) - 2] = SWAP_BE16 (offset
);
1855 /* FILL IN THE USER RECORD: */
1856 hotfileinfo
= (HotFilesInfo
*)((u_int8_t
*)buffer
+ offset
);
1857 hotfileinfo
->magic
= SWAP_BE32 (HFC_MAGIC
);
1858 hotfileinfo
->version
= SWAP_BE32 (HFC_VERSION
);
1859 hotfileinfo
->duration
= SWAP_BE32 (HFC_DEFAULT_DURATION
);
1860 hotfileinfo
->timebase
= 0;
1861 hotfileinfo
->timeleft
= 0;
1862 hotfileinfo
->threshold
= SWAP_BE32 (HFC_MINIMUM_TEMPERATURE
);
1863 hotfileinfo
->maxfileblks
= SWAP_BE32 (HFC_MAXIMUM_FILESIZE
/ HFSTOVCB(hfsmp
)->blockSize
);
1864 hotfileinfo
->maxfilecnt
= SWAP_BE32 (HFC_DEFAULT_FILE_COUNT
);
1865 strlcpy((char *)hotfileinfo
->tag
, hfc_tag
,
1866 sizeof hotfileinfo
->tag
);
1867 offset
+= kBTreeHeaderUserBytes
;
1868 index
[(nodesize
/ 2) - 3] = SWAP_BE16 (offset
);
1870 /* FILL IN THE MAP RECORD (only one node in use). */
1871 *((u_int8_t
*)buffer
+ offset
) = 0x80;
1872 offset
+= nodesize
- sizeof(BTNodeDescriptor
) - sizeof(BTHeaderRec
)
1873 - kBTreeHeaderUserBytes
- (4 * sizeof(int16_t));
1874 index
[(nodesize
/ 2) - 4] = SWAP_BE16 (offset
);
1876 vnode_setnoflush(vp
);
1877 error
= hfs_truncate(vp
, (off_t
)filesize
, IO_NDELAY
, 0, ctx
);
1879 printf("HFS: error %d growing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1882 cp
->c_flag
|= C_ZFWANTSYNC
;
1883 cp
->c_zftimeout
= 1;
1886 struct vnop_write_args args
;
1889 auio
= uio_create(1, 0, UIO_SYSSPACE32
, UIO_WRITE
);
1890 uio_addiov(auio
, (uintptr_t)buffer
, nodesize
);
1892 args
.a_desc
= &vnop_write_desc
;
1896 args
.a_context
= ctx
;
1901 error
= hfs_vnop_write(&args
);
1903 printf("HFS: error %d writing HFBT on %s\n", error
, HFSTOVCB(hfsmp
)->vcbVN
);
1907 kmem_free(kernel_map
, (vm_offset_t
)buffer
, nodesize
);
1923 * Compare two hot file b-tree keys.
1925 * Result: +n search key > trial key
1926 * 0 search key = trial key
1927 * -n search key < trial key
1930 hfc_comparekeys(HotFileKey
*searchKey
, HotFileKey
*trialKey
)
1933 * Compared temperatures first.
1935 if (searchKey
->temperature
== trialKey
->temperature
) {
1937 * Temperatures are equal so compare file ids.
1939 if (searchKey
->fileID
== trialKey
->fileID
) {
1941 * File ids are equal so compare fork types.
1943 if (searchKey
->forkType
== trialKey
->forkType
) {
1945 } else if (searchKey
->forkType
> trialKey
->forkType
) {
1948 } else if (searchKey
->fileID
> trialKey
->fileID
) {
1951 } else if (searchKey
->temperature
> trialKey
->temperature
) {
1960 *========================================================================
1961 * HOT FILE DATA COLLECTING ROUTINES
1962 *========================================================================
1966 * Lookup a hot file entry in the tree.
1969 static hotfile_entry_t
*
1970 hf_lookup(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
1972 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1975 entry
->temperature
!= temperature
&&
1976 entry
->fileid
!= fileid
) {
1978 if (temperature
> entry
->temperature
)
1979 entry
= entry
->right
;
1980 else if (temperature
< entry
->temperature
)
1981 entry
= entry
->left
;
1982 else if (fileid
> entry
->fileid
)
1983 entry
= entry
->right
;
1985 entry
= entry
->left
;
1992 * Insert a hot file entry into the tree.
1995 hf_insert(hotfile_data_t
*hotdata
, hotfile_entry_t
*newentry
)
1997 hotfile_entry_t
*entry
= hotdata
->rootentry
;
1998 u_int32_t fileid
= newentry
->fileid
;
1999 u_int32_t temperature
= newentry
->temperature
;
2001 if (entry
== NULL
) {
2002 hotdata
->rootentry
= newentry
;
2003 hotdata
->coldest
= newentry
;
2004 hotdata
->activefiles
++;
2009 if (temperature
> entry
->temperature
) {
2011 entry
= entry
->right
;
2013 entry
->right
= newentry
;
2016 } else if (temperature
< entry
->temperature
) {
2018 entry
= entry
->left
;
2020 entry
->left
= newentry
;
2023 } else if (fileid
> entry
->fileid
) {
2025 entry
= entry
->right
;
2027 if (entry
->fileid
!= fileid
)
2028 entry
->right
= newentry
;
2033 entry
= entry
->left
;
2035 if (entry
->fileid
!= fileid
)
2036 entry
->left
= newentry
;
2042 hotdata
->activefiles
++;
2046 * Find the coldest entry in the tree.
2048 static hotfile_entry_t
*
2049 hf_coldest(hotfile_data_t
*hotdata
)
2051 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2055 entry
= entry
->left
;
2061 * Find the hottest entry in the tree.
2063 static hotfile_entry_t
*
2064 hf_hottest(hotfile_data_t
*hotdata
)
2066 hotfile_entry_t
*entry
= hotdata
->rootentry
;
2069 while (entry
->right
)
2070 entry
= entry
->right
;
2076 * Delete a hot file entry from the tree.
2079 hf_delete(hotfile_data_t
*hotdata
, u_int32_t fileid
, u_int32_t temperature
)
2081 hotfile_entry_t
*entry
, *parent
, *next
;
2084 entry
= hotdata
->rootentry
;
2087 entry
->temperature
!= temperature
&&
2088 entry
->fileid
!= fileid
) {
2091 if (temperature
> entry
->temperature
)
2092 entry
= entry
->right
;
2093 else if (temperature
< entry
->temperature
)
2094 entry
= entry
->left
;
2095 else if (fileid
> entry
->fileid
)
2096 entry
= entry
->right
;
2098 entry
= entry
->left
;
2103 * Reorginize the sub-trees spanning from our entry.
2105 if ((next
= entry
->right
)) {
2106 hotfile_entry_t
*pnextl
, *psub
;
2108 * Tree pruning: take the left branch of the
2109 * current entry and place it at the lowest
2110 * left branch of the current right branch
2114 /* Walk the Right/Left sub tree from current entry */
2115 while ((pnextl
= psub
->left
))
2118 /* Plug the old left tree to the new ->Right leftmost entry */
2119 psub
->left
= entry
->left
;
2121 } else /* only left sub-tree, simple case */ {
2125 * Now, plug the current entry sub tree to
2126 * the good pointer of our parent entry.
2129 hotdata
->rootentry
= next
;
2130 else if (parent
->left
== entry
)
2131 parent
->left
= next
;
2133 parent
->right
= next
;
2135 /* Place entry back on the free-list */
2138 entry
->temperature
= 0;
2140 entry
->right
= hotdata
->freelist
;
2141 hotdata
->freelist
= entry
;
2142 hotdata
->activefiles
--;
2144 if (hotdata
->coldest
== entry
|| hotdata
->coldest
== NULL
) {
2145 hotdata
->coldest
= hf_coldest(hotdata
);
2152 * Get a free hot file entry.
2154 static hotfile_entry_t
*
2155 hf_getnewentry(hotfile_data_t
*hotdata
)
2157 hotfile_entry_t
* entry
;
2160 * When the free list is empty then steal the coldest one
2162 if (hotdata
->freelist
== NULL
) {
2163 entry
= hf_coldest(hotdata
);
2164 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2166 entry
= hotdata
->freelist
;
2167 hotdata
->freelist
= entry
->right
;
2175 * Generate a sorted list of hot files (hottest to coldest).
2177 * As a side effect, every node in the hot file tree will be
2178 * deleted (moved to the free list).
2181 hf_getsortedlist(hotfile_data_t
* hotdata
, hotfilelist_t
*sortedlist
)
2184 hotfile_entry_t
*entry
;
2186 while ((entry
= hf_hottest(hotdata
)) != NULL
) {
2187 sortedlist
->hfl_hotfile
[i
].hf_fileid
= entry
->fileid
;
2188 sortedlist
->hfl_hotfile
[i
].hf_temperature
= entry
->temperature
;
2189 sortedlist
->hfl_hotfile
[i
].hf_blocks
= entry
->blocks
;
2190 sortedlist
->hfl_totalblocks
+= entry
->blocks
;
2193 hf_delete(hotdata
, entry
->fileid
, entry
->temperature
);
2196 sortedlist
->hfl_count
= i
;
2199 printf("HFS: hf_getsortedlist returned %d entries\n", i
);
2206 hf_maxdepth(hotfile_entry_t
* root
, int depth
, int *maxdepth
)
2210 if (depth
> *maxdepth
)
2212 hf_maxdepth(root
->left
, depth
, maxdepth
);
2213 hf_maxdepth(root
->right
, depth
, maxdepth
);
2218 hf_printtree(hotfile_entry_t
* root
)
2221 hf_printtree(root
->left
);
2222 printf("temperature: % 8d, fileid %d\n", root
->temperature
, root
->fileid
);
2223 hf_printtree(root
->right
);